AutoCAD has supported the eXtended Data for long time, back to the ADS era. In the AutoCAD .NET world, those functionalities regarding the XData manipulations and even those extended DXF codes are wrapped nicely into some classes, structs, methods and enums.
However, because of the different syntaxes in the new AutoCAD .NET and the old ADS, even for people coming from the ADS time and using those old ResultBuffer a lot, they may still be confused about some classes, methods, and enum values as far as XData and ResultBuffer are concerned.
In this post, let’s copy some XData of a picked entity to all entities in the current space (model or paper).
[CommandMethod("CopyXDataToAll")]
public static void CopyXDataToAll_Method()
{
const string TestAppName = "XDataAppName1";
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
try
{
PromptEntityResult prEntRes = ed.GetEntity(string.Format("Select an Entity having the XDATA {0}", TestAppName));
if (prEntRes.Status == PromptStatus.OK)
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
Entity ent = (Entity)tr.GetObject(prEntRes.ObjectId, OpenMode.ForRead);
using (ResultBuffer rb = ent.GetXDataForApplication(TestAppName))
{
if (rb == null)
{
ed.WriteMessage("The entity does not have the XDATA.");
return;
}
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForRead);
foreach (ObjectId id in btr)
{
Entity otherEnt = (Entity)tr.GetObject(id, OpenMode.ForWrite);
otherEnt.XData = rb;
}
}
tr.Commit();
}
}
}
catch (System.Exception ex)
{
ed.WriteMessage(ex.ToString());
}
}
The code seems simple and straightforward, but there are quite some tips and tricks there.
• The DBObject.GetXDataForApplication() method can retrieve the XData with the specified application name from a DBObject or Entity.
• All XData starts with the 1001 (DxfCode.ExtendedDataRegAppName) code.
• The XData Application Name can be registered through creating a RegAppTableRecord and adding it to the RegAppTable (another kind of AutoCAD symbol table).
• The ResultBuffer is a list of TypedValue which is a pair of type and value.
• The ResultBuffer is not database resident, so we can assign a single result buffer to multiple instances of DBObject or Entity through its XData property.
• We’d better release the ResultBuffer as soon as possible. Using using seems a good practice too here.
• Whether we must dispose of ResultBuffer (as we have to do to those instances of in-memory DBObject or Entity) or we can leave it to the .NET GC is something needing further explorations.
The leading edge AutoCAD .NET Addin Wizard (AcadNetAddinWizard) provides project wizards in C#, VB.NET and CLI/Managed C++, and various item wizards such as Event Handlers, Command/LispFunction Definers, and Entity/Draw Jiggers in both C# and VB.NET, to help program AutoCAD addins.
Posted by: |