Reputation: 17260
I am grabbing all of the objects in the ModelSpace for an AutoCAD drawing. These objects are of various types like Pipe, Duct, DuctFitting, Line, Mesh, Solid3D, etc, so I am writing logic to determine their properties by reflecting with TypeDescriptor. I am wondering if there is a smarter way to go about getting all of the attributes of an object, and if there is a way to determine the units of measure for a given item. I know how to look up what units the drawing is using, but how can I tell that a property called "Length" is actually a length using the units from the drawing like feet or meters?
Here is the code I have to enumerate over all of the items I want:
using (var transaction = database.TransactionManager.StartTransaction())
{
BlockTable blockTable = (BlockTable)transaction.GetObject(database.BlockTableId, OpenMode.ForRead);
BlockTableRecord blockTableRecord = (BlockTableRecord)transaction.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForRead);
foreach (DBObject dbObject in blockTableRecord
.Cast<ObjectId>()
.Where(id => id.IsValid && !id.IsNull && !id.IsEffectivelyErased && !id.IsErased)
.Select(id => transaction.GetObject(id, OpenMode.ForRead))
{
//DISCOVER PROPERTIES AND THEIR UNITS HERE
}
}
Upvotes: 1
Views: 2435
Reputation: 186
This will help you.
UnitsValue unitValue=Application.DocumentManager.MdiActiveDocument.Database.Insunits;
UnitValue
is enum which support 20 unit types including LightYear
, Armstrong
, etc...
Upvotes: 4
Reputation: 56
Reflection seems like a reasonable approach to me.
To find properties that have units associated with them you can check each property for Autodesk.AutoCAD.DatabaseServices.UnitTypeAttribute
. For example, if you are using TypeDescriptor
and are looking for properties that are distances it might look like this:
var distanceAttribute = new UnitTypeAttribute(UnitType.Distance);
foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(dbObject))
{
if (property.Attributes.Matches(distanceAttribute))
{
// Here's a property that is a distance.
}
}
This relies on the API actually having UnitTypeAttribute
applied where you would expect it.
Upvotes: 2