Active Member
Posts: 7
Registered: ‎10-24-2012
Message 1 of 3 (215 Views)

Determine units of a property in .Net

215 Views, 2 Replies
10-24-2012 09:36 AM

I am selecting items and grabbing their DBObjects, and then reflecting on those objects to inspect properties of the object.  For example, I might have a DBObject that is actually of type Pipe, Duct, DuctFitting, Line, etc, so I am using TypeDescriptor reflection to handle various cases in as abstract a way as possible.


My first question is this: Is this the smartest way to grab all of the attributes of a give object?  Is there another call I can make the retrieve all of the attributes for a Pipe and a Line (to very different types of objects).


Second, when I see a property named Length and it is a double I know it has to be a length and that I can use the units from the drawing.  Is there a way to programatically discover this other than the name of the property and its type (double)?  In other words is there some metadata I could retrieve about any given property against which I am reflecting, or is there a smarter way to get these attributes which might provided said metadata?

Valued Mentor
Posts: 370
Registered: ‎05-06-2012
Message 2 of 3 (190 Views)

Re: Determine units of a property in .Net

10-25-2012 11:19 AM in reply to: jasonleejackson

If the API you're using was implemented correctly, then PropertyDescriptors for properties of type System.Double will have a UnitTypeAttribute attached to them, which has a UnitType property that tells you if the property represents a distance, angle, area, scale, etc. 


The UnitType enum:


public enum UnitType 
  Angle = 2,
  AngleNotTransformed = 0x10000,
  Area = 4,
  Currency = 0x10,
  Distance = 1,
  Percentage = 0x20,
  Unitless = 0,
  Volume = 8



As far as how you're getting the properties, that's the way it should be done, but you need to be careful because Autodesk has internalized the use of System.ComponentModel, and allows the property representation of DBObjects to be mangled by AutoCAD Data Extraction. In other words, after data extraction is used in a session, you will find all sorts of additional properties that correspond to the fields that can appear in a data extraction table (like for example, file properties like Title, LastSavedBy, and so on, which you will get on every single entity type).


Had it been done correctly (no it certainly was not), they would have used TypeDescriptor.AddProvider() and RemoveProvider() to add/remove the data extraction properties so they will only be returned when they're being requested by data extraction, rather than by other components. Unfortunately, it wasn't done that way, and  so once Data Extraction has been run in the session, those additional properties will remain for the duration of the session.


You can probably eliminate them by filtering on attributes and/or the runtime type of the PropertyDescriptor, but you'll still have a real mess on your hands.


What I usually do is write my own custom typedescriptors and have them bypass the Autodesk-provided ones to avoid having to contend with all that junk that's added by data extraction.


Active Member
Posts: 7
Registered: ‎10-24-2012
Message 3 of 3 (137 Views)

Re: Determine units of a property in .Net

11-14-2012 11:33 AM in reply to: DiningPhilosopher

I discovered the Attribute for the units on StackOverflow and got that to work.  I am using the TypeDescriptor out of .Net 4 to do my reflection against the various types I encounter and that seems to be working well.


Thanks for the help!

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.