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 us address the XData LayerName type indicated by the DxfCode.ExtendedDataLayerName enum value. Here is some code and command to attach a layer name XData to a selected entity.
public static void XDataLayerTest_Method()
const string TestAppName = "XDataLayerTest_TestApp";
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
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);
using (RegAppTableRecord regAppRecord = new RegAppTableRecord())
regAppRecord.Name = TestAppName;
Entity ent = (Entity)tr.GetObject(prEntRes.ObjectId, OpenMode.ForWrite);
ent.XData = new ResultBuffer
new TypedValue((int)DxfCode.ExtendedDataRegAppName, TestAppName),
new TypedValue((int)DxfCode.ExtendedDataLayerName, "LayerForXDataTest")
catch (System.Exception ex)
Here is the output in the AutoCAD command line window after it is run and some other operations follow.
Specify center point for circle or [3P/2P/Ttr (tan tan radius)]:
Specify radius of circle or [Diameter]:
Select entity: Autodesk.AutoCAD.Runtime.Exception: eInvalidInput
Current layer: "0"
Enter an option
Enter name for new layer (becomes the current layer) <0>: LayerForXDataTest
Enter an option
Select entity: last
Select objects: 1 found
Select objects: last
As can be seen, the layer with the name has to be there in the database; otherwise the eInvalidInput would error out. Another very important implication (that exactly indicate the very useful DxfCode.ExtendedDataLayerName XData type and its difference from the ordinary XData string type) about the LayerName XData is that when the single object having the XData is wblocked out into a new drawing the referenced layer would be created automatically. So, the layer name XData behaves as a hard pointer instead of a not so meaningful arbitrary string. Please open the wblocked-out drawing and check the layers and you will not miss it.
So, to recap, the layer name XData (DxfCode.ExtendedDataLayerName) has to point to an existing layer and the layer will be cloned along with the object which has been attached the layer name XData during wblocking.
The code seems simple and straightforward, but there are still some tips and tricks there.
• 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 DxfCode.ExtendedDataLayerName (integer 1003) specifies that the XData type is of Layer Name instead of an arbitrary string.
• The XData property of AutoCAD DBObject holds the extended data and is of type ResultBuffer.
• The ResultBuffer is a list of TypedValue which is a pair of type and value.
• The first element of the ResultBuffer has to be the registered XData application name with the integer 1001 or the enum value DxfCode.ExtendedDataRegAppName.
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.