<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic How to set an entity's XData in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/how-to-set-an-entity-s-xdata/m-p/1744400#M80650</link>
    <description>Due to very poor documentation of .NET API (or should I say, no &lt;BR /&gt;
documentation), I am having trouoble to attach Xdata to acadentity.&lt;BR /&gt;
&lt;BR /&gt;
Here are the code that I wrote to set XData to a user picked entity:&lt;BR /&gt;
&lt;BR /&gt;
using System;&lt;BR /&gt;
using Autodesk.AutoCAD.Runtime;&lt;BR /&gt;
using Autodesk.AutoCAD.EditorInput;&lt;BR /&gt;
using Autodesk.AutoCAD.ApplicationServices;&lt;BR /&gt;
using Autodesk.AutoCAD.DatabaseServices;&lt;BR /&gt;
using System.Windows.Forms;&lt;BR /&gt;
&lt;BR /&gt;
namespace ClassLibraryTest&lt;BR /&gt;
{&lt;BR /&gt;
    public class MyClass&lt;BR /&gt;
    {&lt;BR /&gt;
        public MyClass()&lt;BR /&gt;
        {&lt;BR /&gt;
&lt;BR /&gt;
        }&lt;BR /&gt;
&lt;BR /&gt;
        [CommandMethod("SetMyXData")]&lt;BR /&gt;
        public void SetTheXData()&lt;BR /&gt;
        {&lt;BR /&gt;
            Document dwg = &lt;BR /&gt;
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;&lt;BR /&gt;
            ObjectId id=ObjectId.Null;&lt;BR /&gt;
&lt;BR /&gt;
            //A method to let user to Pick an entity of given type, ObjectId &lt;BR /&gt;
is returned via an output parameter&lt;BR /&gt;
            PromptStatus status = PickAcadEntity(dwg, "Please pick a &lt;BR /&gt;
polyline: ", typeof(Polyline), out id);&lt;BR /&gt;
            if (id==ObjectId.Null)&lt;BR /&gt;
            {&lt;BR /&gt;
                MessageBox.Show("No valid entity is picked: must be a &lt;BR /&gt;
POLYLINE.");&lt;BR /&gt;
                return;&lt;BR /&gt;
            }&lt;BR /&gt;
&lt;BR /&gt;
            if (status != PromptStatus.OK)&lt;BR /&gt;
            {&lt;BR /&gt;
                MessageBox.Show("Invalid pick: no object is picked or picked &lt;BR /&gt;
object is not a Polyline.");&lt;BR /&gt;
                return;&lt;BR /&gt;
            }&lt;BR /&gt;
&lt;BR /&gt;
            //Check if "MyApp" is registered app for XData or not&lt;BR /&gt;
            using (Transaction tran = &lt;BR /&gt;
dwg.TransactionManager.StartTransaction())&lt;BR /&gt;
            {&lt;BR /&gt;
                RegAppTable appTable = &lt;BR /&gt;
(RegAppTable)tran.GetObject(dwg.Database.RegAppTableId, OpenMode.ForWrite);&lt;BR /&gt;
                bool exist = false;&lt;BR /&gt;
                foreach (ObjectId aID in appTable)&lt;BR /&gt;
                {&lt;BR /&gt;
                    RegAppTableRecord app = &lt;BR /&gt;
(RegAppTableRecord)tran.GetObject(aID, OpenMode.ForRead);&lt;BR /&gt;
                    if (app.Name.ToUpper() == "MyApp")&lt;BR /&gt;
                    {&lt;BR /&gt;
                        exist = true;&lt;BR /&gt;
                        break;&lt;BR /&gt;
                    }&lt;BR /&gt;
                }&lt;BR /&gt;
&lt;BR /&gt;
                 //Create RegAppTableRecord, if needed&lt;BR /&gt;
                if (!exist)&lt;BR /&gt;
                {&lt;BR /&gt;
                    RegAppTableRecord tr = new RegAppTableRecord();&lt;BR /&gt;
                    tr.Name = "MyApp";&lt;BR /&gt;
                    appTable.Add(tr);&lt;BR /&gt;
                }&lt;BR /&gt;
&lt;BR /&gt;
                tran.Commit();&lt;BR /&gt;
            }&lt;BR /&gt;
&lt;BR /&gt;
            //Set XData to picked entity&lt;BR /&gt;
            using (Transaction tran = &lt;BR /&gt;
dwg.Database.TransactionManager.StartTransaction())&lt;BR /&gt;
            {&lt;BR /&gt;
                try&lt;BR /&gt;
                {&lt;BR /&gt;
                    Entity ent = (Entity)tran.GetObject(id, &lt;BR /&gt;
OpenMode.ForWrite, false);&lt;BR /&gt;
&lt;BR /&gt;
                    //Create XData&lt;BR /&gt;
                    TypedValue[] xdata = new TypedValue[]&lt;BR /&gt;
                                                {&lt;BR /&gt;
                                                    new &lt;BR /&gt;
TypedValue(Convert.ToInt16(DxfCode.ExtendedDataRegAppName),"MyApp"),&lt;BR /&gt;
                                                    new &lt;BR /&gt;
TypedValue(Convert.ToInt16(DxfCode.ExtendedDataAsciiString),"MyData")&lt;BR /&gt;
                                                };&lt;BR /&gt;
&lt;BR /&gt;
                    ResultBuffer res = new ResultBuffer(xdata);&lt;BR /&gt;
&lt;BR /&gt;
                    ent.XData = res;&lt;BR /&gt;
&lt;BR /&gt;
                    tran.Commit();&lt;BR /&gt;
&lt;BR /&gt;
                    MessageBox.Show("XData attached!");&lt;BR /&gt;
                }&lt;BR /&gt;
                catch (Autodesk.AutoCAD.Runtime.Exception ex)&lt;BR /&gt;
                {&lt;BR /&gt;
                    MessageBox.Show("Attaching XData failed: " + &lt;BR /&gt;
ex.Message);&lt;BR /&gt;
                }&lt;BR /&gt;
            }&lt;BR /&gt;
        }&lt;BR /&gt;
    }&lt;BR /&gt;
}&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
When I run above code, it seems OK (Mseeage box pops up, showing "XData &lt;BR /&gt;
attached!"). However, When I run another command that trying to retrieve the &lt;BR /&gt;
Xdata on that entity, I get nothing. In that command I use &lt;BR /&gt;
Entity.GetXDataForApplication("MyApp").&lt;BR /&gt;
&lt;BR /&gt;
I also used VBA code (AcadEntity.GetXData()) to see if there is XData &lt;BR /&gt;
attached or not. I got nothing also.&lt;BR /&gt;
&lt;BR /&gt;
More serious problem is, after running the "SetMyXData" command (above &lt;BR /&gt;
code), if I do not close the drawing, the entity being processed by the code &lt;BR /&gt;
seems OK. But if I save and close the drawing, then open is again, I got &lt;BR /&gt;
"Drawing cannot be opened due to corrupted data" error and asked to recorver &lt;BR /&gt;
the drawing. Once recovering is done, the enity is gone (being discarded &lt;BR /&gt;
during recovering).&lt;BR /&gt;
&lt;BR /&gt;
Clearing, the code above corrupted the entity, although running it seems OK.&lt;BR /&gt;
&lt;BR /&gt;
Can anyone point out what I am doing wrong, or the correct way to set/get &lt;BR /&gt;
XData to/from an entity in .NET API?</description>
    <pubDate>Wed, 23 Aug 2006 16:13:06 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2006-08-23T16:13:06Z</dc:date>
    <item>
      <title>How to set an entity's XData</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-set-an-entity-s-xdata/m-p/1744400#M80650</link>
      <description>Due to very poor documentation of .NET API (or should I say, no &lt;BR /&gt;
documentation), I am having trouoble to attach Xdata to acadentity.&lt;BR /&gt;
&lt;BR /&gt;
Here are the code that I wrote to set XData to a user picked entity:&lt;BR /&gt;
&lt;BR /&gt;
using System;&lt;BR /&gt;
using Autodesk.AutoCAD.Runtime;&lt;BR /&gt;
using Autodesk.AutoCAD.EditorInput;&lt;BR /&gt;
using Autodesk.AutoCAD.ApplicationServices;&lt;BR /&gt;
using Autodesk.AutoCAD.DatabaseServices;&lt;BR /&gt;
using System.Windows.Forms;&lt;BR /&gt;
&lt;BR /&gt;
namespace ClassLibraryTest&lt;BR /&gt;
{&lt;BR /&gt;
    public class MyClass&lt;BR /&gt;
    {&lt;BR /&gt;
        public MyClass()&lt;BR /&gt;
        {&lt;BR /&gt;
&lt;BR /&gt;
        }&lt;BR /&gt;
&lt;BR /&gt;
        [CommandMethod("SetMyXData")]&lt;BR /&gt;
        public void SetTheXData()&lt;BR /&gt;
        {&lt;BR /&gt;
            Document dwg = &lt;BR /&gt;
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;&lt;BR /&gt;
            ObjectId id=ObjectId.Null;&lt;BR /&gt;
&lt;BR /&gt;
            //A method to let user to Pick an entity of given type, ObjectId &lt;BR /&gt;
is returned via an output parameter&lt;BR /&gt;
            PromptStatus status = PickAcadEntity(dwg, "Please pick a &lt;BR /&gt;
polyline: ", typeof(Polyline), out id);&lt;BR /&gt;
            if (id==ObjectId.Null)&lt;BR /&gt;
            {&lt;BR /&gt;
                MessageBox.Show("No valid entity is picked: must be a &lt;BR /&gt;
POLYLINE.");&lt;BR /&gt;
                return;&lt;BR /&gt;
            }&lt;BR /&gt;
&lt;BR /&gt;
            if (status != PromptStatus.OK)&lt;BR /&gt;
            {&lt;BR /&gt;
                MessageBox.Show("Invalid pick: no object is picked or picked &lt;BR /&gt;
object is not a Polyline.");&lt;BR /&gt;
                return;&lt;BR /&gt;
            }&lt;BR /&gt;
&lt;BR /&gt;
            //Check if "MyApp" is registered app for XData or not&lt;BR /&gt;
            using (Transaction tran = &lt;BR /&gt;
dwg.TransactionManager.StartTransaction())&lt;BR /&gt;
            {&lt;BR /&gt;
                RegAppTable appTable = &lt;BR /&gt;
(RegAppTable)tran.GetObject(dwg.Database.RegAppTableId, OpenMode.ForWrite);&lt;BR /&gt;
                bool exist = false;&lt;BR /&gt;
                foreach (ObjectId aID in appTable)&lt;BR /&gt;
                {&lt;BR /&gt;
                    RegAppTableRecord app = &lt;BR /&gt;
(RegAppTableRecord)tran.GetObject(aID, OpenMode.ForRead);&lt;BR /&gt;
                    if (app.Name.ToUpper() == "MyApp")&lt;BR /&gt;
                    {&lt;BR /&gt;
                        exist = true;&lt;BR /&gt;
                        break;&lt;BR /&gt;
                    }&lt;BR /&gt;
                }&lt;BR /&gt;
&lt;BR /&gt;
                 //Create RegAppTableRecord, if needed&lt;BR /&gt;
                if (!exist)&lt;BR /&gt;
                {&lt;BR /&gt;
                    RegAppTableRecord tr = new RegAppTableRecord();&lt;BR /&gt;
                    tr.Name = "MyApp";&lt;BR /&gt;
                    appTable.Add(tr);&lt;BR /&gt;
                }&lt;BR /&gt;
&lt;BR /&gt;
                tran.Commit();&lt;BR /&gt;
            }&lt;BR /&gt;
&lt;BR /&gt;
            //Set XData to picked entity&lt;BR /&gt;
            using (Transaction tran = &lt;BR /&gt;
dwg.Database.TransactionManager.StartTransaction())&lt;BR /&gt;
            {&lt;BR /&gt;
                try&lt;BR /&gt;
                {&lt;BR /&gt;
                    Entity ent = (Entity)tran.GetObject(id, &lt;BR /&gt;
OpenMode.ForWrite, false);&lt;BR /&gt;
&lt;BR /&gt;
                    //Create XData&lt;BR /&gt;
                    TypedValue[] xdata = new TypedValue[]&lt;BR /&gt;
                                                {&lt;BR /&gt;
                                                    new &lt;BR /&gt;
TypedValue(Convert.ToInt16(DxfCode.ExtendedDataRegAppName),"MyApp"),&lt;BR /&gt;
                                                    new &lt;BR /&gt;
TypedValue(Convert.ToInt16(DxfCode.ExtendedDataAsciiString),"MyData")&lt;BR /&gt;
                                                };&lt;BR /&gt;
&lt;BR /&gt;
                    ResultBuffer res = new ResultBuffer(xdata);&lt;BR /&gt;
&lt;BR /&gt;
                    ent.XData = res;&lt;BR /&gt;
&lt;BR /&gt;
                    tran.Commit();&lt;BR /&gt;
&lt;BR /&gt;
                    MessageBox.Show("XData attached!");&lt;BR /&gt;
                }&lt;BR /&gt;
                catch (Autodesk.AutoCAD.Runtime.Exception ex)&lt;BR /&gt;
                {&lt;BR /&gt;
                    MessageBox.Show("Attaching XData failed: " + &lt;BR /&gt;
ex.Message);&lt;BR /&gt;
                }&lt;BR /&gt;
            }&lt;BR /&gt;
        }&lt;BR /&gt;
    }&lt;BR /&gt;
}&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
When I run above code, it seems OK (Mseeage box pops up, showing "XData &lt;BR /&gt;
attached!"). However, When I run another command that trying to retrieve the &lt;BR /&gt;
Xdata on that entity, I get nothing. In that command I use &lt;BR /&gt;
Entity.GetXDataForApplication("MyApp").&lt;BR /&gt;
&lt;BR /&gt;
I also used VBA code (AcadEntity.GetXData()) to see if there is XData &lt;BR /&gt;
attached or not. I got nothing also.&lt;BR /&gt;
&lt;BR /&gt;
More serious problem is, after running the "SetMyXData" command (above &lt;BR /&gt;
code), if I do not close the drawing, the entity being processed by the code &lt;BR /&gt;
seems OK. But if I save and close the drawing, then open is again, I got &lt;BR /&gt;
"Drawing cannot be opened due to corrupted data" error and asked to recorver &lt;BR /&gt;
the drawing. Once recovering is done, the enity is gone (being discarded &lt;BR /&gt;
during recovering).&lt;BR /&gt;
&lt;BR /&gt;
Clearing, the code above corrupted the entity, although running it seems OK.&lt;BR /&gt;
&lt;BR /&gt;
Can anyone point out what I am doing wrong, or the correct way to set/get &lt;BR /&gt;
XData to/from an entity in .NET API?</description>
      <pubDate>Wed, 23 Aug 2006 16:13:06 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-set-an-entity-s-xdata/m-p/1744400#M80650</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2006-08-23T16:13:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to set an entity's XData</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-set-an-entity-s-xdata/m-p/1744401#M80651</link>
      <description>Try adding XData without adding your application to the registered application names. After a quick look, your XData routine seems almost exactly the same as I use, so my guess is that the error is in adding your application to the registered app table.... Either way, trying them seperately will give you a better idea of where exactly the problem is.</description>
      <pubDate>Wed, 23 Aug 2006 17:11:26 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-set-an-entity-s-xdata/m-p/1744401#M80651</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2006-08-23T17:11:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to set an entity's XData</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-set-an-entity-s-xdata/m-p/1744402#M80652</link>
      <description>I am not sure what do you mean here by "the error is in adding your&lt;BR /&gt;
application to the registered app table".&lt;BR /&gt;
&lt;BR /&gt;
If I did not check if an app name is registered for XData in the database,&lt;BR /&gt;
and set the first item in ResultBuffer object as&lt;BR /&gt;
DxfCode.ExtendedDataRegAppName, I got error "eRegappidNotFound";&lt;BR /&gt;
&lt;BR /&gt;
if I did not check if an app name is registered for XData in database, and&lt;BR /&gt;
set the first item in ResultBuffer object as DxfCode.ExtendedDataAsciiString&lt;BR /&gt;
(or any thing other than ExtendedRegAppName, i.e without supply RegAppName),&lt;BR /&gt;
I got error "eBadDxfcodeSequency", meaning Acad cannot find registered app&lt;BR /&gt;
name for this XData.&lt;BR /&gt;
&lt;BR /&gt;
So, I tried your suggestion and did the regstering app name for XData in&lt;BR /&gt;
seperate command, that is, register app name first (if not registered), and&lt;BR /&gt;
then attach XData for that app in another command later.&lt;BR /&gt;
&lt;BR /&gt;
It gives me the same result: command runs OK until you close the drawing and&lt;BR /&gt;
open it again: the entity with XData attached become corrupted, recovering&lt;BR /&gt;
would drop this entity out (recovering say "Error 34 (eWrongObjectType).&lt;BR /&gt;
Object discarded").&lt;BR /&gt;
&lt;BR /&gt;
Could you post your code snappet on this that works?&lt;BR /&gt;
&lt;BR /&gt;
&lt;SMCCLURE&gt; wrote in message news:5304386@discussion.autodesk.com...&lt;BR /&gt;
Try adding XData without adding your application to the registered&lt;BR /&gt;
application names. After a quick look, your XData routine seems almost&lt;BR /&gt;
exactly the same as I use, so my guess is that the error is in adding your&lt;BR /&gt;
application to the registered app table.... Either way, trying them&lt;BR /&gt;
seperately will give you a better idea of where exactly the problem is.&lt;/SMCCLURE&gt;</description>
      <pubDate>Wed, 23 Aug 2006 18:24:54 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-set-an-entity-s-xdata/m-p/1744402#M80652</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2006-08-23T18:24:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to set an entity's XData</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-set-an-entity-s-xdata/m-p/1744403#M80653</link>
      <description>Heh.... I think this may be a difference in versions of AutoCAD. I have added XData without registering the app name before, but I am running  2005 before all the checks on XData consistency were added, it seems (there is no method to retrieve XData by app. name, for instance).&lt;BR /&gt;
&lt;BR /&gt;
I have one more suggestion. If you are wondering about the internal state of AutoCAD after executing your command, you can use the ARXDBG utility. This is an old program that was originally a ObjectARX sample that I have compiled. It is attached.</description>
      <pubDate>Wed, 23 Aug 2006 18:50:20 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-set-an-entity-s-xdata/m-p/1744403#M80653</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2006-08-23T18:50:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to set an entity's XData</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-set-an-entity-s-xdata/m-p/1744404#M80654</link>
      <description>Ooops.... I forgot to explain how to use it. Load the ARX and right click anywhere to get to the menu. To see XData on a block, view the entity information and click the "XData" button. Viewing the database information will allow you to see the registered apps.</description>
      <pubDate>Wed, 23 Aug 2006 18:54:33 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-set-an-entity-s-xdata/m-p/1744404#M80654</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2006-08-23T18:54:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to set an entity's XData</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-set-an-entity-s-xdata/m-p/1744405#M80655</link>
      <description>Thamks for your help. I finally found the problem (very tricky) and solved &lt;BR /&gt;
it (costing entire morning and the first hour after lunch!).&lt;BR /&gt;
&lt;BR /&gt;
Your first instinct was right, it has something to do with registering app &lt;BR /&gt;
name for the XData.&lt;BR /&gt;
&lt;BR /&gt;
Here is what causes the problem: if you create a new RegAppTableRecord and &lt;BR /&gt;
add it into RegAppTable, you have to call Dispose().&lt;BR /&gt;
&lt;BR /&gt;
Below is the part of code I posted previously, plus the little change I &lt;BR /&gt;
made:&lt;BR /&gt;
&lt;BR /&gt;
    Note: I added Dispose() in 3 places(marked with "!!!===") and tested the &lt;BR /&gt;
code by commented out one or more of them in different combination, in the &lt;BR /&gt;
end, only one matters.&lt;BR /&gt;
&lt;BR /&gt;
            //Check if "MyApp" is registered app for XData or not&lt;BR /&gt;
            using (Transaction tran = &lt;BR /&gt;
dwg.TransactionManager.StartTransaction())&lt;BR /&gt;
            {&lt;BR /&gt;
                RegAppTable appTable = &lt;BR /&gt;
(RegAppTable)tran.GetObject(dwg.Database.RegAppTableId, OpenMode.ForWrite);&lt;BR /&gt;
                bool exist = false;&lt;BR /&gt;
                foreach (ObjectId aID in appTable)&lt;BR /&gt;
                {&lt;BR /&gt;
                    RegAppTableRecord app = &lt;BR /&gt;
(RegAppTableRecord)tran.GetObject(aID, OpenMode.ForRead);&lt;BR /&gt;
                    if (app.Name.ToUpper() == "MyApp")&lt;BR /&gt;
                    {&lt;BR /&gt;
                        exist = true;&lt;BR /&gt;
                        break;&lt;BR /&gt;
                    }&lt;BR /&gt;
&lt;BR /&gt;
                    //!!!===Add Dispose() here, but has not affect&lt;BR /&gt;
                    //app.Dispose();&lt;BR /&gt;
                }&lt;BR /&gt;
&lt;BR /&gt;
                //Create RegAppTableRecord, if needed&lt;BR /&gt;
                if (!exist)&lt;BR /&gt;
                {&lt;BR /&gt;
                    RegAppTableRecord ct = new RegAppTableRecord();&lt;BR /&gt;
                    ct.Name = "MyApp";&lt;BR /&gt;
                    appTable.Add(ct);&lt;BR /&gt;
&lt;BR /&gt;
                    //!!!===Added Dispose() here. Only this is required, if &lt;BR /&gt;
you want to add XData to this application. However&lt;BR /&gt;
                    //!!!===if you do registering app name and attaching &lt;BR /&gt;
XData in seperate command, AND you close the drawing&lt;BR /&gt;
                    //!!!===after restering app name, and open the drawing &lt;BR /&gt;
again to attach XData, then you do not need this Dispose().&lt;BR /&gt;
                    ct.Dispose();&lt;BR /&gt;
                }&lt;BR /&gt;
&lt;BR /&gt;
                tran.Commit();&lt;BR /&gt;
&lt;BR /&gt;
                //!!!===Add Dispose() here, but has not affect&lt;BR /&gt;
                appTable.Dispose();&lt;BR /&gt;
            }&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
It is just so tricky to find out the cause, and even more confusing when or &lt;BR /&gt;
if to call object.Dispose(). In this particular case, I just do not see a &lt;BR /&gt;
reason to call RegAppTableRecord.Dispose() after adding it into RegAppTable &lt;BR /&gt;
object: this variable pointing to the newly created RegAppTableRecord is &lt;BR /&gt;
going out of scope, the the RegAppTableRecord itself is handed to its &lt;BR /&gt;
container, a RegAppTable object, why dispose it?&lt;BR /&gt;
&lt;BR /&gt;
But anyways, after more than 5 hours struggling, I solved it. Whether it is &lt;BR /&gt;
a .NET API implementation bug, or it is just an undocumented trick, I do not &lt;BR /&gt;
expect there would be an explanation in future ObjectARX .NET API &lt;BR /&gt;
documentation (if there would be such thing).&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;SMCCLURE&gt; wrote in message news:5304553@discussion.autodesk.com...&lt;BR /&gt;
Heh.... I think this may be a difference in versions of AutoCAD. I have &lt;BR /&gt;
added XData without registering the app name before, but I am running  2005 &lt;BR /&gt;
before all the checks on XData consistency were added, it seems (there is no &lt;BR /&gt;
method to retrieve XData by app. name, for instance).&lt;BR /&gt;
&lt;BR /&gt;
I have one more suggestion. If you are wondering about the internal state of &lt;BR /&gt;
AutoCAD after executing your command, you can use the ARXDBG utility. This &lt;BR /&gt;
is an old program that was originally a ObjectARX sample that&lt;BR /&gt;
 I have compiled. It is attached.&lt;/SMCCLURE&gt;</description>
      <pubDate>Wed, 23 Aug 2006 21:40:41 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-set-an-entity-s-xdata/m-p/1744405#M80655</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2006-08-23T21:40:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to set an entity's XData</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-set-an-entity-s-xdata/m-p/1744406#M80656</link>
      <description>The reason why your code worked when you added&lt;BR /&gt;
the call to Dispose(), is because you failed to add&lt;BR /&gt;
the new RegAppTableRecord to the transaction via&lt;BR /&gt;
AddNewlyCreatedDBObject().&lt;BR /&gt;
&lt;BR /&gt;
When you do that, the Transaction will do the same&lt;BR /&gt;
thing that calling Dispose() on the RegAppTableRecord&lt;BR /&gt;
does (e.g., call Close() or Cancel() depending on if&lt;BR /&gt;
you Commit() or Abort() the transaction, respectively).&lt;BR /&gt;
&lt;BR /&gt;
Regarding when you must call Dispose(), it depends&lt;BR /&gt;
on two things:&lt;BR /&gt;
&lt;BR /&gt;
1.  Is the object database resident?&lt;BR /&gt;
&lt;BR /&gt;
2.  Is the object transaction-resident?&lt;BR /&gt;
&lt;BR /&gt;
If the answer to both questions is no, you should&lt;BR /&gt;
call Dispose(), but you don't really need to because&lt;BR /&gt;
when the garbage collector finalizes the object, the&lt;BR /&gt;
wrapper will release/free the unmanaged resource.&lt;BR /&gt;
&lt;BR /&gt;
If the object is database resident, then whether&lt;BR /&gt;
you need to call Dispose() depends on whether the&lt;BR /&gt;
object is transaction resident (e.g., it was returned&lt;BR /&gt;
by calling GetObject() on the transaction, or added&lt;BR /&gt;
to the transaction via AddNewlyCreatedDBObject()).&lt;BR /&gt;
&lt;BR /&gt;
If the object is transaction-resident, you don't need&lt;BR /&gt;
to call Dispose() as the transaction is responsible&lt;BR /&gt;
for calling (and will call) either Close() or Cancel() on&lt;BR /&gt;
the DBObject, when the transaction ends (which will&lt;BR /&gt;
happen when you Commit(), Abort() or Dispose() it).&lt;BR /&gt;
&lt;BR /&gt;
If a DBObject is not transaction-resident (e.g., you&lt;BR /&gt;
used ObjectId.Open() to get the DBObject), then you&lt;BR /&gt;
_MUST_ call either Close(), Cancel(), or Dispose() on&lt;BR /&gt;
the object, and if you don't, it will most likely cause &lt;BR /&gt;
AutoCAD to crash.&lt;BR /&gt;
&lt;BR /&gt;
-- &lt;BR /&gt;
http://www.caddzone.com&lt;BR /&gt;
&lt;BR /&gt;
AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006/2007&lt;BR /&gt;
http://www.acadxtabs.com&lt;BR /&gt;
&lt;BR /&gt;
"Norman Yuan" &lt;NOTREAL&gt; wrote in message news:5305024@discussion.autodesk.com...&lt;BR /&gt;
Thamks for your help. I finally found the problem (very tricky) and solved &lt;BR /&gt;
it (costing entire morning and the first hour after lunch!).&lt;BR /&gt;
&lt;BR /&gt;
Your first instinct was right, it has something to do with registering app &lt;BR /&gt;
name for the XData.&lt;BR /&gt;
&lt;BR /&gt;
Here is what causes the problem: if you create a new RegAppTableRecord and &lt;BR /&gt;
add it into RegAppTable, you have to call Dispose().&lt;BR /&gt;
&lt;BR /&gt;
Below is the part of code I posted previously, plus the little change I &lt;BR /&gt;
made:&lt;BR /&gt;
&lt;BR /&gt;
    Note: I added Dispose() in 3 places(marked with "!!!===") and tested the &lt;BR /&gt;
code by commented out one or more of them in different combination, in the &lt;BR /&gt;
end, only one matters.&lt;BR /&gt;
&lt;BR /&gt;
            //Check if "MyApp" is registered app for XData or not&lt;BR /&gt;
            using (Transaction tran = &lt;BR /&gt;
dwg.TransactionManager.StartTransaction())&lt;BR /&gt;
            {&lt;BR /&gt;
                RegAppTable appTable = &lt;BR /&gt;
(RegAppTable)tran.GetObject(dwg.Database.RegAppTableId, OpenMode.ForWrite);&lt;BR /&gt;
                bool exist = false;&lt;BR /&gt;
                foreach (ObjectId aID in appTable)&lt;BR /&gt;
                {&lt;BR /&gt;
                    RegAppTableRecord app = &lt;BR /&gt;
(RegAppTableRecord)tran.GetObject(aID, OpenMode.ForRead);&lt;BR /&gt;
                    if (app.Name.ToUpper() == "MyApp")&lt;BR /&gt;
                    {&lt;BR /&gt;
                        exist = true;&lt;BR /&gt;
                        break;&lt;BR /&gt;
                    }&lt;BR /&gt;
&lt;BR /&gt;
                    //!!!===Add Dispose() here, but has not affect&lt;BR /&gt;
                    //app.Dispose();&lt;BR /&gt;
                }&lt;BR /&gt;
&lt;BR /&gt;
                //Create RegAppTableRecord, if needed&lt;BR /&gt;
                if (!exist)&lt;BR /&gt;
                {&lt;BR /&gt;
                    RegAppTableRecord ct = new RegAppTableRecord();&lt;BR /&gt;
                    ct.Name = "MyApp";&lt;BR /&gt;
                    appTable.Add(ct);&lt;BR /&gt;
&lt;BR /&gt;
                    //!!!===Added Dispose() here. Only this is required, if &lt;BR /&gt;
you want to add XData to this application. However&lt;BR /&gt;
                    //!!!===if you do registering app name and attaching &lt;BR /&gt;
XData in seperate command, AND you close the drawing&lt;BR /&gt;
                    //!!!===after restering app name, and open the drawing &lt;BR /&gt;
again to attach XData, then you do not need this Dispose().&lt;BR /&gt;
                    ct.Dispose();&lt;BR /&gt;
                }&lt;BR /&gt;
&lt;BR /&gt;
                tran.Commit();&lt;BR /&gt;
&lt;BR /&gt;
                //!!!===Add Dispose() here, but has not affect&lt;BR /&gt;
                appTable.Dispose();&lt;BR /&gt;
            }&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
It is just so tricky to find out the cause, and even more confusing when or &lt;BR /&gt;
if to call object.Dispose(). In this particular case, I just do not see a &lt;BR /&gt;
reason to call RegAppTableRecord.Dispose() after adding it into RegAppTable &lt;BR /&gt;
object: this variable pointing to the newly created RegAppTableRecord is &lt;BR /&gt;
going out of scope, the the RegAppTableRecord itself is handed to its &lt;BR /&gt;
container, a RegAppTable object, why dispose it?&lt;BR /&gt;
&lt;BR /&gt;
But anyways, after more than 5 hours struggling, I solved it. Whether it is &lt;BR /&gt;
a .NET API implementation bug, or it is just an undocumented trick, I do not &lt;BR /&gt;
expect there would be an explanation in future ObjectARX .NET API &lt;BR /&gt;
documentation (if there would be such thing).&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;SMCCLURE&gt; wrote in message news:5304553@discussion.autodesk.com...&lt;BR /&gt;
Heh.... I think this may be a difference in versions of AutoCAD. I have &lt;BR /&gt;
added XData without registering the app name before, but I am running  2005 &lt;BR /&gt;
before all the checks on XData consistency were added, it seems (there is no &lt;BR /&gt;
method to retrieve XData by app. name, for instance).&lt;BR /&gt;
&lt;BR /&gt;
I have one more suggestion. If you are wondering about the internal state of &lt;BR /&gt;
AutoCAD after executing your command, you can use the ARXDBG utility. This &lt;BR /&gt;
is an old program that was originally a ObjectARX sample that&lt;BR /&gt;
 I have compiled. It is attached.&lt;/SMCCLURE&gt;&lt;/NOTREAL&gt;</description>
      <pubDate>Wed, 23 Aug 2006 23:49:31 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-set-an-entity-s-xdata/m-p/1744406#M80656</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2006-08-23T23:49:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to set an entity's XData</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-set-an-entity-s-xdata/m-p/1744407#M80657</link>
      <description>A good trick is to use the "using" command... Any variables created at the beginning of the "using" block will have disposed called at the end of the block - it is basically a try-finally block with a new name and syntax. I tend to wrap most of my DB objects in these blocks&lt;BR /&gt;
&lt;BR /&gt;
Question for Tony (or anyone who knows this stuff really well...) while you dont _need_ to call disposed on DB objects created under a transaction, I frequently do (the call is after Transaction.Commit()) This has never given me an error, and I do it mostly out of habit - I tend to wrap all AutoCAD related code in a try-finally/using block. Is this a bad idea?</description>
      <pubDate>Thu, 24 Aug 2006 13:11:18 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-set-an-entity-s-xdata/m-p/1744407#M80657</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2006-08-24T13:11:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to set an entity's XData</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-set-an-entity-s-xdata/m-p/1744408#M80658</link>
      <description>Bad idea?  No, but largely pointless.&lt;BR /&gt;
&lt;BR /&gt;
Committing/Aborting the transaction does exactly what calling &lt;BR /&gt;
Dispose() on a DBObject that's open in the transaction does, &lt;BR /&gt;
which is to Close()/Cancel() the open DBObject, so calling &lt;BR /&gt;
Dispose() after the transaction is ended, is largely pointless, &lt;BR /&gt;
and does nothing. &lt;BR /&gt;
&lt;BR /&gt;
That is because in the case of a DB-resident object, there is &lt;BR /&gt;
no need to release/free unmanaged resources (e.g., the &lt;BR /&gt;
'wrapped' native AcDbObject).&lt;BR /&gt;
&lt;BR /&gt;
However (and I have not researched this), if you add a new&lt;BR /&gt;
DBObject (e.g., created with the new() operator) to a transaction&lt;BR /&gt;
via AddNewlyCreatedDBObject(); but don't add the object to the &lt;BR /&gt;
database; and you abort the transaction, in that case, you may &lt;BR /&gt;
need to call Dispose() to free the unmanaged resource.&lt;BR /&gt;
&lt;BR /&gt;
-- &lt;BR /&gt;
http://www.caddzone.com&lt;BR /&gt;
&lt;BR /&gt;
AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006/2007&lt;BR /&gt;
http://www.acadxtabs.com&lt;BR /&gt;
&lt;BR /&gt;
&lt;SMCCLURE&gt; wrote in message news:5305828@discussion.autodesk.com...&lt;BR /&gt;
A good trick is to use the "using" command... Any variables created at the beginning of the "using" block will have disposed called at the end of the block - it is basically a try-finally block with a new name and syntax. I tend to wrap most of my DB objects in these blocks&lt;BR /&gt;
&lt;BR /&gt;
Question for Tony (or anyone who knows this stuff really well...) while you dont _need_ to call disposed on DB objects created under a transaction, I frequently do (the call is after Transaction.Commit()) This has never given me an error, and I do it mostly out of habit - I tend to wrap all AutoCAD related code in a try-finally/using block. Is this a bad idea?&lt;/SMCCLURE&gt;</description>
      <pubDate>Thu, 24 Aug 2006 14:46:07 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-set-an-entity-s-xdata/m-p/1744408#M80658</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2006-08-24T14:46:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to set an entity's XData</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-set-an-entity-s-xdata/m-p/1744409#M80659</link>
      <description>Hi, I'm new to .NET and I'm trying to use de ArxDbg tool to see the database of a drawing but couldn't compile the arxdbg with VC++ express and the arxdbgd.arx file doesn't work in AutoCAD 2007.&lt;BR /&gt;
Can anyone help please?</description>
      <pubDate>Sun, 14 Oct 2007 02:26:27 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-set-an-entity-s-xdata/m-p/1744409#M80659</guid>
      <dc:creator>juanjo44</dc:creator>
      <dc:date>2007-10-14T02:26:27Z</dc:date>
    </item>
  </channel>
</rss>

