Let us look at a cool AutoCAD .NET coding gadget in this post which can dump all applicable properties to a string variable and in turn to the AutoCAD command line window or a message box.
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 all these problems have gone away completely. We can make the same work done more nicely and professionally in just about a dozen of line of code:
…
void Dump(DBObject obj)
{
string msg = string.Format("Properties of the {0} with handle {1}:\n", obj.GetType().Name, obj.Handle);
PropertyInfo[] piArr = obj.GetType().GetProperties();
foreach (PropertyInfo pi in piArr)
{
object value = null;
try
{
value = pi.GetValue(obj, null);
}
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;
}
msg += string.Format("\t{0}: {1}\n", pi.Name, value);
}
ed.WriteMessage("\n" + msg);
MessageBox.Show(msg, "DBObject/Entity Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
…
As can be noticed, the NotApplicable or eNotApplicable case has also been taken care of in the succinct but cool coding gadget. For the sake of convenience, the command used to test it has been appended below:
#region Namespaces
using System;
using System.Text;
using System.Linq;
using System.Xml;
using System.Reflection;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Forms;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Windows;
using AcadApplication = Autodesk.AutoCAD.ApplicationServices.Application;
using AcadDocument = Autodesk.AutoCAD.ApplicationServices.Document;
using AcadWindows = Autodesk.AutoCAD.Windows;
#endregion
namespace AcadNetAddinWizard_Namespace
{
public class TestCommands
{
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = AcadApplication.DocumentManager.MdiActiveDocument.Editor;
[CommandMethod("DumpEntityInfo")]
public void DumpEntityInfo_Method()
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
//TODO: add your code below.
Debug.WriteLine("DumpEntityInfo ran.");
ed.WriteMessage("DumpEntityInfo ran.\n");
PromptEntityResult selRes = ed.GetEntity("Pick an entity:");
if (selRes.Status == PromptStatus.OK)
{
Entity ent = tr.GetObject(selRes.ObjectId, OpenMode.ForRead) as Entity;
Dump(ent);
}
tr.Commit();
}
}
}
}
If a circle is selected for example, the command line output may look like:
Command: DumpEntityInfo
DumpEntityInfo ran.
Pick an entity:
Properties of the Circle with handle 231:
Diameter: 19.5663433028452
Circumference: 61.4694803778344
Normal: (0,0,1)
Thickness: 0
Radius: 9.78317165142261
Center: (-16.1101582423863,8.87769678998828,0)
Area: 300.683238930054
Spline: Autodesk.AutoCAD.DatabaseServices.Spline
EndPoint: (-6.32698659096368,8.87769678998828,0)
StartPoint: (-6.32698659096368,8.87769678998828,0)
EndParam: 6.28318530717959
StartParam: 0
IsPeriodic: True
Closed: True
EdgeStyleId: (0)
FaceStyleId: (0)
VisualStyleId: (0)
ForceAnnoAllVisible: True
BlockName: *Model_Space
MaterialMapper:
MaterialId: (8796087807456)
Material: ByLayer
ReceiveShadows: False
CastShadows: False
Hyperlinks: Autodesk.AutoCAD.DatabaseServices.HyperLinkCollection
CloneMeForDragging: True
GeometricExtents:
((-25.8933299038089,-0.905474871434328,-1E-08),(-6.32698658096368,18.66086845141
09,1E-08))
Ecs: ((1,0,0,0),(0,1,0,0),(0,0,1,0),(0,0,0,1))
IsPlanar: True
CollisionType: Solid
LineWeight: ByLayer
Visible: True
LinetypeScale: 1
LinetypeId: (8796087806288)
Linetype: ByLayer
LayerId: (8796087806208)
Layer: 0
PlotStyleNameId: ((0),PlotStyleNameByLayer)
PlotStyleName: ByLayer
Transparency: (0)
EntityColor: Autodesk.AutoCAD.Colors.EntityColor
ColorIndex: 256
Color: BYLAYER
BlockId: (8796087806448)
PaperOrientation: NotApplicable
Annotative: NotApplicable
HasFields: False
AcadObject: System.__ComObject
ClassID: 8229fcda-0225-4ef2-960e-a93bcf521a2b
ObjectBirthVersion: AC1012,Release0
HasSaveVersionOverride: False
IsObjectIdsInFlux: False
UndoFiler:
IsAProxy: False
IsTransactionResident: True
IsReallyClosing: False
IsCancelling: False
IsUndoing: False
IsNotifying: False
IsNewObject: False
IsModifiedGraphics: False
IsModifiedXData: False
IsModified: False
IsNotifyEnabled: False
IsWriteEnabled: False
IsReadEnabled: True
IsErased: False
IsEraseStatusToggled: False
XData:
MergeStyle: Ignore
ExtensionDictionary: (0)
Drawable: Autodesk.AutoCAD.DatabaseServices.Circle
Database: Autodesk.AutoCAD.DatabaseServices.Database
Handle: 231
OwnerId: (8796087806448)
ObjectId: (8796087820048)
Id: (8796087820048)
IsPersistent: True
Bounds:
((-25.8933299038089,-0.905474871434328,-1E-08),(-6.32698658096368,18.66086845141
09,1E-08))
DrawableType: Geometry
AutoDelete: False
IsDisposed: False
UnmanagedObject: 717429680
Some special properties may need to be tweaked a bit further or skipped at all. I would like to leave it as an exercise to readers who have the need!
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.
Posted by: |