Let us look at a cool AutoCAD .NET coding gadget in this post which can export all good properties of a selected AutoCAD Entity base on its type inheritance to a CSV file, which can be opened in the Microsoft Excel or any other CSV supported tools for us to check the detailed information for each single property. The CSV file can be reused for some other more meaningful purposes as well for sure.
In the old days without the reflection technology, this is quite a nasty task. We generally had to cast the AutoCAD generic object/entity to each available good type and access to its properties. Many short points are with this approach. The two most prominent are not efficient at all likely hundreds of even thousands of line of code have to be made and not compatible with old or new object models so likely some properties are missing in one version but some others are not available in another.
With the wonderful reflection technology handy, especially the tool classes TypeDescriptor, PropertyDescriptor and PropertyDescriptorCollection, which are introduced in the recent .NET Framework versions, all these problems have gone away completely. We can make the same work done more nicely and professionally in less code:
[CommandMethod("DumpEntityInfo3")]
public void DumpEntityInfo3_Method()
{
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
try
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
//TODO: add your code below.
Debug.WriteLine("DumpEntityInfo3 ran.");
ed.WriteMessage("DumpEntityInfo3 ran.\n");
PromptEntityResult selRes = ed.GetEntity("Pick an AutoCAD entity:");
if (selRes.Status == PromptStatus.OK)
{
Entity ent = tr.GetObject(selRes.ObjectId, OpenMode.ForRead) as Entity;
Dump3(ent);
}
tr.Commit();
}
}
catch (System.Exception ex)
{
ed.WriteMessage(ex.ToString());
}
}
void Dump3(DBObject obj)
{
List<Type> lists = new List<Type>();
Type type = obj.GetType();
do
{
lists.Add(type);
type = type.BaseType;
} while (type is Object);
lists.Reverse();
string line = "Name, Value, Category, ComponentType, Description, DisplayName, IsLocalizable, IsReadOnly, PropertyType";
using (StreamWriter sw = new StreamWriter(@"c:\temp\AcadObjectProperties.csv"))
{
PropertyDescriptorCollection propertiesFromBase = null;
foreach (Type t in lists)
{
sw.WriteLine("** Type {0} properties: ", t.Name);
sw.WriteLine(line);
PropertyDescriptorCollection allprops = TypeDescriptor.GetProperties(t).Sort();
PropertyDescriptorCollection tempProperties = propertiesFromBase;
propertiesFromBase = allprops;
List<PropertyDescriptor> propList = new List<PropertyDescriptor>();
foreach (PropertyDescriptor pd in allprops)
if (tempProperties == null || tempProperties.Find(pd.Name, false) == null)
propList.Add(pd);
PropertyDescriptorCollection props = new PropertyDescriptorCollection(propList.ToArray());
foreach (PropertyDescriptor prop in props)
{
object valueObj = null;
string valueString = string.Empty;
try
{
valueObj = prop.GetValue(obj);
if (valueObj != null)
{
valueString = valueObj.ToString();
valueString = valueString.Replace(",", ";");
string line1 = string.Format("{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}", prop.Name, valueString, prop.Category, prop.ComponentType.Name, prop.Description, prop.DisplayName, prop.IsLocalizable, prop.IsReadOnly, prop.PropertyType.Name);
sw.WriteLine(line1);
}
}
catch (System.Exception ex)
{
if (ex.InnerException is Autodesk.AutoCAD.Runtime.Exception &&
(ex.InnerException as Autodesk.AutoCAD.Runtime.Exception).ErrorStatus == ErrorStatus.NotApplicable)
continue;
else
throw;
}
}
sw.WriteLine();
}
}
}
[CommandMethod("CreateCircleInCurrent")]
public void Test_CreateCircleInCurrent()
{
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
Matrix3d ucs = ed.CurrentUserCoordinateSystem;
try
{
PromptPointOptions prOpt = new PromptPointOptions("\nCircle center: ");
PromptPointResult pr = ed.GetPoint(prOpt);
if (pr.Status != PromptStatus.OK) return;
PromptDistanceOptions prOpt1 = new PromptDistanceOptions("\nCircle radius: ");
prOpt1.UseBasePoint = true;
prOpt1.UseDashedLine = true;
prOpt1.BasePoint = pr.Value;
PromptDoubleResult pr1 = ed.GetDistance(prOpt1);
if (pr1.Status != PromptStatus.OK) return;
using (Circle cir = new Circle(pr.Value, Vector3d.ZAxis, pr1.Value))
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
cir.TransformBy(ed.CurrentUserCoordinateSystem);
btr.AppendEntity(cir);
tr.AddNewlyCreatedDBObject(cir, true);
tr.Commit();
}
}
}
catch (System.Exception ex)
{
ed.WriteMessage(ex.ToString());
}
}
If a circle is selected for example, the content of the output CSV may look like:
** Type Object properties:
Name, Value, Category, ComponentType, Description, DisplayName, IsLocalizable, IsReadOnly, PropertyType
** Type MarshalByRefObject properties:
Name, Value, Category, ComponentType, Description, DisplayName, IsLocalizable, IsReadOnly, PropertyType
** Type DisposableWrapper properties:
Name, Value, Category, ComponentType, Description, DisplayName, IsLocalizable, IsReadOnly, PropertyType
AutoDelete, False, Misc, DisposableWrapper, , AutoDelete, False, True, Boolean
IsDisposed, False, Misc, DisposableWrapper, , IsDisposed, False, True, Boolean
UnmanagedObject, 709086560, Misc, DisposableWrapper, , UnmanagedObject, False, True, IntPtr
** Type RXObject properties:
Name, Value, Category, ComponentType, Description, DisplayName, IsLocalizable, IsReadOnly, PropertyType
** Type Drawable properties:
Name, Value, Category, ComponentType, Description, DisplayName, IsLocalizable, IsReadOnly, PropertyType
Bounds, ((-41.5524906496138;-56.6698736987233;6.90467291850713);(-30.4752220703741;-42.0160247719119;19.2894356902477)), Misc, Drawable, , Bounds, False, True, Nullable`1
DrawableType, Geometry, Misc, Drawable, , DrawableType, False, True, DrawableType
Id, (8796087821456), Misc, Drawable, , Id, False, True, ObjectId
IsPersistent, True, Misc, Drawable, , IsPersistent, False, True, Boolean
** Type DBObject properties:
Name, Value, Category, ComponentType, Description, DisplayName, IsLocalizable, IsReadOnly, PropertyType
AcadObject, System.__ComObject, Misc, DBObject, , AcadObject, False, True, Object
Annotative, NotApplicable, Misc, DBObject, , Annotative, False, False, AnnotativeStates
ClassID, 8229fcda-0225-4ef2-960e-a93bcf521a2b, Misc, DBObject, , ClassID, False, True, Guid
Database, Autodesk.AutoCAD.DatabaseServices.Database, Misc, DBObject, , Database, False, True, Database
Drawable, Autodesk.AutoCAD.DatabaseServices.Circle, Misc, DBObject, , Drawable, False, True, Drawable
ExtensionDictionary, (0), Misc, DBObject, , ExtensionDictionary, False, True, ObjectId
Handle, 3C9, Misc, DBObject, , Handle, False, True, Handle
HasFields, False, Misc, DBObject, , HasFields, False, True, Boolean
HasSaveVersionOverride, False, Misc, DBObject, , HasSaveVersionOverride, False, False, Boolean
IsAProxy, False, Misc, DBObject, , IsAProxy, False, True, Boolean
IsCancelling, False, Misc, DBObject, , IsCancelling, False, True, Boolean
IsErased, False, Misc, DBObject, , IsErased, False, True, Boolean
IsEraseStatusToggled, False, Misc, DBObject, , IsEraseStatusToggled, False, True, Boolean
IsModified, False, Misc, DBObject, , IsModified, False, True, Boolean
IsModifiedGraphics, False, Misc, DBObject, , IsModifiedGraphics, False, True, Boolean
IsModifiedXData, False, Misc, DBObject, , IsModifiedXData, False, True, Boolean
IsNewObject, False, Misc, DBObject, , IsNewObject, False, True, Boolean
IsNotifyEnabled, False, Misc, DBObject, , IsNotifyEnabled, False, True, Boolean
IsNotifying, False, Misc, DBObject, , IsNotifying, False, True, Boolean
IsObjectIdsInFlux, False, Misc, DBObject, , IsObjectIdsInFlux, False, True, Boolean
IsReadEnabled, True, Misc, DBObject, , IsReadEnabled, False, True, Boolean
IsReallyClosing, False, Misc, DBObject, , IsReallyClosing, False, True, Boolean
IsTransactionResident, True, Misc, DBObject, , IsTransactionResident, False, True, Boolean
IsUndoing, False, Misc, DBObject, , IsUndoing, False, True, Boolean
IsWriteEnabled, False, Misc, DBObject, , IsWriteEnabled, False, True, Boolean
MergeStyle, Ignore, Misc, DBObject, , MergeStyle, False, False, DuplicateRecordCloning
ObjectBirthVersion, AC1012;Release0, Misc, DBObject, , ObjectBirthVersion, False, True, FullDwgVersion
ObjectId, (8796087821456), Misc, DBObject, , ObjectId, False, True, ObjectId
OwnerId, (8796087806448), Misc, DBObject, , OwnerId, False, False, ObjectId
PaperOrientation, NotApplicable, Misc, DBObject, , PaperOrientation, False, True, PaperOrientationStates
** Type Entity properties:
Name, Value, Category, ComponentType, Description, DisplayName, IsLocalizable, IsReadOnly, PropertyType
BlockId, (8796087806448), Misc, Entity, , BlockId, False, True, ObjectId
BlockName, *Model_Space, Misc, Entity, , BlockName, False, True, String
CastShadows, False, 3D Visualization, Entity, , CastShadows, False, False, Boolean
CloneMeForDragging, True, Misc, Entity, , CloneMeForDragging, False, True, Boolean
CollisionType, Solid, Geometry, Entity, , CollisionType, False, True, CollisionType
Color, BYLAYER, General, Entity, , Color, False, False, Color
ColorIndex, 256, General, Entity, , ColorIndex, False, False, Int32
Ecs, ((0.447213595499958;-0.547722557505166;0.707106781186548;0);(0.894427190999916;0.273861278752583;-0.353553390593274;0);(0;0.790569415042095;0.612372435695795;0);(0;0;0;1)), Misc, Entity, , Ecs, False, True, Matrix3d
EdgeStyleId, (0), 3D Visualization, Entity, , EdgeStyleId, False, False, ObjectId
EntityColor, Autodesk.AutoCAD.Colors.EntityColor, General, Entity, , EntityColor, False, True, EntityColor
FaceStyleId, (0), 3D Visualization, Entity, , FaceStyleId, False, False, ObjectId
ForceAnnoAllVisible, True, Misc, Entity, , ForceAnnoAllVisible, False, False, Boolean
GeometricExtents, ((-41.5524906496138;-56.6698736987233;6.90467291850713);(-30.4752220703741;-42.0160247719119;19.2894356902477)), Geometry, Entity, , GeometricExtents, False, True, Extents3d
Hyperlinks, Autodesk.AutoCAD.DatabaseServices.HyperLinkCollection, General, Entity, , Hyperlinks, False, True, HyperLinkCollection
IsPlanar, True, Geometry, Entity, , IsPlanar, False, True, Boolean
Layer, 0, General, Entity, , Layer, False, False, String
LayerId, (8796087806208), General, Entity, , LayerId, False, False, ObjectId
Linetype, ByLayer, General, Entity, , Linetype, False, False, String
LinetypeId, (8796087806288), General, Entity, , LinetypeId, False, False, ObjectId
LinetypeScale, 1, General, Entity, , LinetypeScale, False, False, Double
LineWeight, ByLayer, General, Entity, , LineWeight, False, False, LineWeight
Material, ByLayer, 3D Visualization, Entity, , Material, False, False, String
MaterialId, (8796087807456), 3D Visualization, Entity, , MaterialId, False, False, ObjectId
PlotStyleName, ByLayer, General, Entity, , PlotStyleName, False, False, String
PlotStyleNameId, ((0);PlotStyleNameByLayer), General, Entity, , PlotStyleNameId, False, False, PlotStyleDescriptor
ReceiveShadows, False, 3D Visualization, Entity, , ReceiveShadows, False, False, Boolean
Transparency, (0), General, Entity, , Transparency, False, False, Transparency
Visible, True, General, Entity, , Visible, False, False, Boolean
VisualStyleId, (0), 3D Visualization, Entity, , VisualStyleId, False, False, ObjectId
** Type Curve properties:
Name, Value, Category, ComponentType, Description, DisplayName, IsLocalizable, IsReadOnly, PropertyType
Area, 192.745943590753, Geometry, Curve, , Area, False, True, Double
Closed, True, Geometry, Curve, , Closed, False, True, Boolean
EndParam, 6.28318530717959, Geometry, Curve, , EndParam, False, True, Double
EndPoint, (-32.510916469937;-42.3370694552038;13.0970543043774), Geometry, Curve, , EndPoint, False, False, Point3d
IsPeriodic, True, Geometry, Curve, , IsPeriodic, False, True, Boolean
Spline, Autodesk.AutoCAD.DatabaseServices.Spline, Geometry, Curve, , Spline, False, True, Spline
StartParam, 0, Geometry, Curve, , StartParam, False, True, Double
StartPoint, (-32.510916469937;-42.3370694552038;13.0970543043774), Geometry, Curve, , StartPoint, False, False, Point3d
** Type Circle properties:
Name, Value, Category, ComponentType, Description, DisplayName, IsLocalizable, IsReadOnly, PropertyType
Center, (-36.0138563599939;-49.3429492353176;13.0970543043774), Geometry, Circle, , Center, False, False, Point3d
Circumference, 49.215007483244, Geometry, Circle, , Circumference, False, False, Double
Diameter, 15.6656234305258, Geometry, Circle, , Diameter, False, False, Double
Normal, (0.707106781186548;-0.353553390593274;0.612372435695795), Geometry, Circle, , Normal, False, False, Vector3d
Radius, 7.83281171526291, Geometry, Circle, , Radius, False, False, Double
Thickness, 0, General, Circle, , Thickness, False, False, Double
As can be seen, the selected AutoCAD entity information will be printed out in the CSV file based on the type hierarchy tree and in the ascending alphabetic order. Each type does not include the properties of its base types and only holds its own information.
Enjoy it! More coding gadgets will be created and demonstrated in the future. Please stay tuned.
The leading edge AutoCAD .NET Addin Wizard (AcadNetAddinWizard) provides various project wizards, item wizards, coders and widgets to help program AutoCAD .NET addins.
hi Spiderinnet1, how can i export topology into oracle spatial using c#? i'm new in autocad net. thaks for help
Posted by: jemmy monepa | 07/26/2016 at 12:28 AM
jemmy monepa, it depends on what topology of autocad. anyway, it's all about using AutoCAD and/or its vertical .NET APIs and database .NET APIs such as ADO.net. all similar. if more details provided, we may have some clearer ideas.
Posted by: Spiderinnet1 | 07/26/2016 at 04:07 AM
thanks for reply Spiderinnet1. the idea is to export 2d parcel topology to the oracle spatial, like we use "_oraexport" command and select some filtering layer (ex. "020100" layer) that have topology using c# .net (include connect to oracle spatial database with c# listing). Thanks for help again.
Posted by: jemmy monepa | 07/26/2016 at 06:18 AM
thanks for the detail. it seems you are talking about MAP, Civil 3D vertical or similar. they also have .NET APIs so you could iterate through all those '2d parcel topology' objects and retrieve any information you like available there. in terms of oracle spatial database connection, there are lots of sample code on line, from MSDN about ADO.net or similar.
Posted by: Spiderinnet1 | 07/26/2016 at 05:30 PM