.NET

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

Determine units of a property in .Net

200 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
DiningPhilosopher
Posts: 370
Registered: ‎05-06-2012
Message 2 of 3 (175 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
jasonleejackson
Posts: 7
Registered: ‎10-24-2012
Message 3 of 3 (122 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!

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community