Message 1 of 10
How to set an entity's XData
Not applicable
08-23-2006
09:13 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Due to very poor documentation of .NET API (or should I say, no
documentation), I am having trouoble to attach Xdata to acadentity.
Here are the code that I wrote to set XData to a user picked entity:
using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using System.Windows.Forms;
namespace ClassLibraryTest
{
public class MyClass
{
public MyClass()
{
}
[CommandMethod("SetMyXData")]
public void SetTheXData()
{
Document dwg =
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
ObjectId id=ObjectId.Null;
//A method to let user to Pick an entity of given type, ObjectId
is returned via an output parameter
PromptStatus status = PickAcadEntity(dwg, "Please pick a
polyline: ", typeof(Polyline), out id);
if (id==ObjectId.Null)
{
MessageBox.Show("No valid entity is picked: must be a
POLYLINE.");
return;
}
if (status != PromptStatus.OK)
{
MessageBox.Show("Invalid pick: no object is picked or picked
object is not a Polyline.");
return;
}
//Check if "MyApp" is registered app for XData or not
using (Transaction tran =
dwg.TransactionManager.StartTransaction())
{
RegAppTable appTable =
(RegAppTable)tran.GetObject(dwg.Database.RegAppTableId, OpenMode.ForWrite);
bool exist = false;
foreach (ObjectId aID in appTable)
{
RegAppTableRecord app =
(RegAppTableRecord)tran.GetObject(aID, OpenMode.ForRead);
if (app.Name.ToUpper() == "MyApp")
{
exist = true;
break;
}
}
//Create RegAppTableRecord, if needed
if (!exist)
{
RegAppTableRecord tr = new RegAppTableRecord();
tr.Name = "MyApp";
appTable.Add(tr);
}
tran.Commit();
}
//Set XData to picked entity
using (Transaction tran =
dwg.Database.TransactionManager.StartTransaction())
{
try
{
Entity ent = (Entity)tran.GetObject(id,
OpenMode.ForWrite, false);
//Create XData
TypedValue[] xdata = new TypedValue[]
{
new
TypedValue(Convert.ToInt16(DxfCode.ExtendedDataRegAppName),"MyApp"),
new
TypedValue(Convert.ToInt16(DxfCode.ExtendedDataAsciiString),"MyData")
};
ResultBuffer res = new ResultBuffer(xdata);
ent.XData = res;
tran.Commit();
MessageBox.Show("XData attached!");
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
MessageBox.Show("Attaching XData failed: " +
ex.Message);
}
}
}
}
}
When I run above code, it seems OK (Mseeage box pops up, showing "XData
attached!"). However, When I run another command that trying to retrieve the
Xdata on that entity, I get nothing. In that command I use
Entity.GetXDataForApplication("MyApp").
I also used VBA code (AcadEntity.GetXData()) to see if there is XData
attached or not. I got nothing also.
More serious problem is, after running the "SetMyXData" command (above
code), if I do not close the drawing, the entity being processed by the code
seems OK. But if I save and close the drawing, then open is again, I got
"Drawing cannot be opened due to corrupted data" error and asked to recorver
the drawing. Once recovering is done, the enity is gone (being discarded
during recovering).
Clearing, the code above corrupted the entity, although running it seems OK.
Can anyone point out what I am doing wrong, or the correct way to set/get
XData to/from an entity in .NET API?
documentation), I am having trouoble to attach Xdata to acadentity.
Here are the code that I wrote to set XData to a user picked entity:
using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using System.Windows.Forms;
namespace ClassLibraryTest
{
public class MyClass
{
public MyClass()
{
}
[CommandMethod("SetMyXData")]
public void SetTheXData()
{
Document dwg =
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
ObjectId id=ObjectId.Null;
//A method to let user to Pick an entity of given type, ObjectId
is returned via an output parameter
PromptStatus status = PickAcadEntity(dwg, "Please pick a
polyline: ", typeof(Polyline), out id);
if (id==ObjectId.Null)
{
MessageBox.Show("No valid entity is picked: must be a
POLYLINE.");
return;
}
if (status != PromptStatus.OK)
{
MessageBox.Show("Invalid pick: no object is picked or picked
object is not a Polyline.");
return;
}
//Check if "MyApp" is registered app for XData or not
using (Transaction tran =
dwg.TransactionManager.StartTransaction())
{
RegAppTable appTable =
(RegAppTable)tran.GetObject(dwg.Database.RegAppTableId, OpenMode.ForWrite);
bool exist = false;
foreach (ObjectId aID in appTable)
{
RegAppTableRecord app =
(RegAppTableRecord)tran.GetObject(aID, OpenMode.ForRead);
if (app.Name.ToUpper() == "MyApp")
{
exist = true;
break;
}
}
//Create RegAppTableRecord, if needed
if (!exist)
{
RegAppTableRecord tr = new RegAppTableRecord();
tr.Name = "MyApp";
appTable.Add(tr);
}
tran.Commit();
}
//Set XData to picked entity
using (Transaction tran =
dwg.Database.TransactionManager.StartTransaction())
{
try
{
Entity ent = (Entity)tran.GetObject(id,
OpenMode.ForWrite, false);
//Create XData
TypedValue[] xdata = new TypedValue[]
{
new
TypedValue(Convert.ToInt16(DxfCode.ExtendedDataRegAppName),"MyApp"),
new
TypedValue(Convert.ToInt16(DxfCode.ExtendedDataAsciiString),"MyData")
};
ResultBuffer res = new ResultBuffer(xdata);
ent.XData = res;
tran.Commit();
MessageBox.Show("XData attached!");
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
MessageBox.Show("Attaching XData failed: " +
ex.Message);
}
}
}
}
}
When I run above code, it seems OK (Mseeage box pops up, showing "XData
attached!"). However, When I run another command that trying to retrieve the
Xdata on that entity, I get nothing. In that command I use
Entity.GetXDataForApplication("MyApp").
I also used VBA code (AcadEntity.GetXData()) to see if there is XData
attached or not. I got nothing also.
More serious problem is, after running the "SetMyXData" command (above
code), if I do not close the drawing, the entity being processed by the code
seems OK. But if I save and close the drawing, then open is again, I got
"Drawing cannot be opened due to corrupted data" error and asked to recorver
the drawing. Once recovering is done, the enity is gone (being discarded
during recovering).
Clearing, the code above corrupted the entity, although running it seems OK.
Can anyone point out what I am doing wrong, or the correct way to set/get
XData to/from an entity in .NET API?