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.
As demonstrated before, the DxfCode.ExtendedDataInteger32 and the DxfCode.ExtendedDataInteger16 both work well for small integer XData values. However, people may wonder what will happen if we feed a big enough 32 bit integer to the XData DxfCode.ExtendedDataInteger16, for example. In this post, let’s figure it out.
Here is the code and command.
public static void XData16N32_Method()
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
string TestAppName = "XData16N32";
PromptEntityResult prEntRes = ed.GetEntity("Select an Entity to attach XDATA");
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);
Int32 int32Data = Int32.MaxValue;
Int16 int16Data = Int16.MaxValue;
ent.XData = new ResultBuffer
new TypedValue((int)DxfCode.ExtendedDataRegAppName, TestAppName), //1001
new TypedValue((int)DxfCode.ExtendedDataInteger16, int16Data),
new TypedValue((int)DxfCode.ExtendedDataInteger32, int32Data),
new TypedValue((int)DxfCode.ExtendedDataInteger16, int32Data), //System.OverflowException: Value was either too large or too small for an Int16.
new TypedValue((int)DxfCode.ExtendedDataInteger32, int16Data)
catch (System.Exception ex)
The following exception would come up for the above code/command, particularly at the code line, new TypedValue((int)DxfCode.ExtendedDataInteger16, int32Data).
Value was either too large or too small for an Int16.
at System.Convert.ToInt16(Int32 value)
at Autodesk.AutoCAD.DatabaseServices.ResultBuffer..ctor(TypedValue values)
It’s good actually since we can spot possible data loss at the earliest moment. As can be seen, the benefit is provided by using the .NET System.Convert.ToInt16(Int32) call. If some implicit data conversion approach were used, the problem would have been much more serious and hard to track down. Lucky us this time!
If the problematic code line is commented out, the command will run well and the XData Int32 and Int16 data can be both read out with the ReadXData command that we introduced a while back.
Select an Entity to attach XDATA:
XData of AppName to read: XData16N32
Select an Entity:
XData of appliation name (1001) XData16N32:
Int16 (1070): 32767
Int32 (1071): 2147483647
Int32 (1071): 32767
The code seems simple and straightforward, but there are quite some tips and tricks there.
• The XData has to be started with the 1001 (DxfCode.ExtendedDataRegAppName) code.
• The XData Application Name has to be registered beforehand.
• The XData Application Name can be registered through creating a RegAppTableRecord and adding it to the RegAppTable (another kind of AutoCAD symbol table).
• The RegAppTable had better be opened for READ first to check the existence of the RegAppTableRecord.
• If the RegAppTableRecord does not exist in the RegAppTable, it can be upgraded to WRITE through the UpgradeOpen() call.
• The UpgradeOpen() had better be balanced with the DowngradeOpen() call though not necessary if the object/entity is closed right after the modification.
• The Entity.XData property can be used to retrieve all the XData associated with the entity if any or to set some XData to it.
• The XData property is inherited from the DBObject actually indicating that all AutoCAD database resident objects having graphics or not such as layers and line types all can have XData.
• 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 DxfCode.ExtendedDataInteger16 (1070) and DxfCode.ExtendedDataInteger32 (1071) are supposed to store 16 bit integer and 32 bit integer respectively. Why two types of the integer XData flavors are provided is not so clear. Likely to save spaces both on hard drive and in memory as originally intended.
• The good side effect, however, is that it avoids some incompatibility issues in the current fast pacing world, such as the case of storing an integer in the latest AutoCAD (2013) running on the latest hardware architecture (64bit) but reading it back in an earlier version of AutoCAD (2005, 2000, 14 or even 12).
• Whether a DxfCode.ExtendedDataInteger64 comes out or not in the modern 64 bit world is also not clear. Not a big deal though since the limitation can be easily worked around through using a double/float XData value.
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.