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.
We have talked about many different aspects about AutoCAD .Net XData. In this post, let’s look a simple but common matter. Can we use only the AppName (ExtendedDataRegAppName) in the result buffer (ResultBuffer)?
Here is the small experiment.
[CommandMethod("XDataAppNameOnly")]
public static void XDataAppNameOnly_Method()
{
const string TestAppName = "MyEntity";
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
try
{
PromptEntityResult prEntRes = ed.GetEntity("Select entity");
if (prEntRes.Status == PromptStatus.OK)
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
RegAppTable regAppTable = (RegAppTable)tr.GetObject(db.RegAppTableId, OpenMode.ForRead);
if (!regAppTable.Has(TestAppName))
{
using (RegAppTableRecord regAppRecord = new RegAppTableRecord())
{
regAppRecord.Name = TestAppName;
regAppTable.UpgradeOpen();
regAppTable.Add(regAppRecord);
tr.AddNewlyCreatedDBObject(regAppRecord, true);
}
}
Entity ent = (Entity)tr.GetObject(prEntRes.ObjectId, OpenMode.ForWrite);
ent.XData = new ResultBuffer
(
new TypedValue((int)DxfCode.ExtendedDataRegAppName, TestAppName)
);
tr.Commit();
}
}
}
catch (System.Exception ex)
{
ed.WriteMessage(ex.ToString());
}
}
After the command runs in AutoCAD and an entity is selected, things seem fine since no error at all. But if we check the XDATA with either code or tool, we will find the XDATA is not there at all.
So in case even if we just want to identity some entities with XDATA and don’t want to really store any data into there, we have to assign some XData type in the result buffer (ResultBuffer). Using an XData string type is a common practice.
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: |