2

I am working on a autoCAD .net project in which I create a MVVM pattern to select and modify an entity. I want to store the selected entity 's info/values into the view model, change the info/values (through the WPF UI that pops up and through my code as well) then apply the changes to the entity.

The problem is: if I want to apply the changes to the entity, I have to know "which" entity will receive the changes. Thus, I want to store the entity ObjectId and retrieve it later WITHOUT using ObjectId struct from acdbmgd.dll of AutoCAD since that will make my UI project depends on a specific version of AutoCAD (Yes, it is a REQUIREMENT that my UI project works on .net framework alone).

Is there any way I can do this? I intend to store the objectId in an object then cast it back to objecId but it didn't work. Please help. Thank you very much.

Thế Long
  • 516
  • 7
  • 19
  • What is the type of `ObjectId` in the database? – grek40 Oct 11 '17 at 07:11
  • I am sorry that I don't really understand your question. Just to clear things up: 1. "the entity ObjectId " is the Id of the entity I want to make changes. 2. "ObjectId" is of type: Autodesk.AutoCAD.DatabaseServices.ObjectId. And it 's a struct. If this is not what you want to know, please explain to me. I am a beginner, so there is much I don't know. – Thế Long Oct 11 '17 at 09:23
  • If you say your UI Project can't depend on anything other than the .Net Framework, does this mean it's ok for the UI Project to depend on a Model Project and for a Model Project to depend on AutoCAD, as long as the public interface of the ModelProject is independent from AutoCAD and thus the UI Project has no direct dependency on AutoCAD? Otherwise I don't really understand how the data transfer between AutoCAD and the UI would work – grek40 Oct 11 '17 at 10:56

2 Answers2

1

As you may or may not know, every entity in a drawing can be retrieved in 3 unique ways: ObjectID, Handle and an Instance Pointer. Object ids are created for each entity each time the drawing database is opened, thus they are only unique until the current drawing is closed. Once it is reopened, all ObjectIDs will be completely different. Handles, however, persist between sessions and, as mavious stated, can be converted to a long and back. Not sure if you care to keep the reference to entities between sessions, but this at least solves your data type issue. Believe me, I completely understand your desire to keep autodesk separate from your main assembly. This post will have all the information that you may need.

Nik
  • 1,780
  • 1
  • 14
  • 23
  • thanks for your suggestion, I did find a solution for this by storing the entity handle as a string field in the view model then retrieve it later by recover its ObjectId as follow: viewModel.EntityHandle = entity.Handle.ToString(); ObjectId cylinderId = acDb.GetObjectId(false, new Handle(Convert.ToInt64(viewModel.EntityHandle, 16)), 0); I never use a handle to retrieve an entity directly before. Would that be more easier or better performance? Your advice is much appreciated. Sorry for the messy comment, still struggling with the line break – Thế Long Oct 12 '17 at 02:25
  • I don't believe you can directly access an entity with a handle. The .Net API is based around using the ObjectID for pretty well everything which can be retrieved using the Handle. One of the main reasons you would use a handle is to have a persistent reference to specific entities in a given drawing between sessions. – Nik Oct 12 '17 at 02:50
  • related reference: https://forums.autodesk.com/t5/net/is-the-quot-objectid-quot-unique-in-a-drawing-file/m-p/6527799/highlight/true#M49953 – Gangula Jan 23 '23 at 13:00
0

Instead of ObjectId, you can store handles value as (long). You will still need to convert handles value to an Autocad Handle when retrieving an entity, but I don't see a way to escape that

mavios
  • 210
  • 1
  • 8