.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Determine units of a property in .Net

2 REPLIES 2
Reply
Message 1 of 3
jasonleejackson
483 Views, 2 Replies

Determine units of a property in .Net

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?

2 REPLIES 2
Message 2 of 3

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.

 

Message 3 of 3

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!

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost