Getting AutoCAD Object Properties in C #

I am trying to interrogate AutoCAD objects with C #. I am interested in the ability to capture all the properties of this object and display them. For example, in the code snippet below, I look through all the elements on the screen and simply reflect their properties of the first class. The objects I care about often have the first-class AcadObject property, which seems to store the data that I follow. The problem is that it is a __ComObject and that many of its nested proposition objects do not provide properties through reflection. For example, obj.AcadObject.Connectors seems to be a collection of connector objects that I am very interested in. I can reflect this depth using the debugger, but from there I left an assumption about the properties of the Connectors collection and its objects (the .Net debugger shows the Count property that repelled me). Using the C # keyword and DLR / COM binders built into .Net 4 I can examine these objects. For example, I can use a dynamic expression to capture obj.AcadObject.Connectors [0] .Name, assuming it has a name attribute. I am ready to use dynamic expressions to capture these properties, but I need to know which properties in the first place. I have researched quite a lot and do not seem to see a link to how these objects look. There are several more objects that depend on AcadObject, as well as what I would like to export.

var currentDocument = Application.DocumentManager.MdiActiveDocument; var editor = currentDocument.Editor; var database = editor.Document.Database; var result = editor.SelectAll(); using (var transaction = database.TransactionManager.StartTransaction()) { foreach (var id in result.Value.GetObjectIds()) { var obj = transaction.GetObject(id, OpenMode.ForRead); var properties = TypeDescriptor.GetProperties(obj.AcadObject).Cast<PropertyDescriptor>().OrderBy(prop => prop.Name); writer.WriteLine("{0} ID:{1}", obj.GetType().Name, obj.Id); writer.WriteLine("\r\n\r\n"); foreach (var property in properties) { var propertyObject = property.GetValue(obj.AcadObject); writer.WriteLine(" {0} = {1}", property.Name, propertyObject); } writer.Write("\r\n\r\n\r\n"); } } 
+4
source share
4 answers

If you want to get ALL items from the database, use it:

 // Get all items from drawing Database. All ObjectIds will grouped by types; Database db = Application.DocumentManager.MdiActiveDocument.Database; Dictionary<string, List<ObjectId>> dict = new Dictionary<string, List<ObjectId>>(); using (Transaction t = db.TransactionManager.StartTransaction()) { for (long i = db.BlockTableId.Handle.Value; i < db.Handseed.Value; i++) { ObjectId id = ObjectId.Null; Handle h = new Handle(i); if (db.TryGetObjectId(h, out id)) { string type = id.ObjectClass.Name; if (!dict.Keys.Contains(type)) dict.Add(type, new List<ObjectId>()); dict[type].Add(id); } } t.Commit(); } 

Hello

+2
source

You can find the MgdDbg utility, which will be especially useful when studying this drawing database ... Depending on what you need, your version of AutoCAD, like Events, etc., is hard-coded, not dynamic, because Tony's fortunes are here .

+1
source

This is not an ideal answer, but I am going to bring it here to help someone else. Using the direct window, I pass the obj.Acad object to the dynamic, and then refer to the object in question, such Connectors [0]. At this moment, I redid it to the object and was able to inspect it for hours. An example of code that will be executed in a direct window with a debugger after typing obj:

 dynamic acad = obj.AcadObject; object connector0 = (object)acad.Connectors[0]; 

The best I can understand is COM binding for DLR, for which I am going to redo the material, and then as soon as I get it back. The .Net debugger can define properties. I am working to ensure that this discovery works with reflection ...

0
source

You can try this utility to get more information about your COM objects: http://www.codeproject.com/Articles/523417/Reflection-with-IDispatch-based-COM-objects

0
source

Source: https://habr.com/ru/post/1435351/


All Articles