<?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 Re: How to attach an instance of a custom .NET class to an AutoCAD drawing in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/how-to-attach-an-instance-of-a-custom-net-class-to-an-autocad/m-p/8439671#M24107</link>
    <description>&lt;P&gt;Here's a little example assuming the Name property have to be unique in a drawing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;    public class ReinforcementDocument
    {
        public string Name { get; }
        public string SlabsLayer { get; private set; } = "";
        public double SlabThickness { get; private set; }

        public ReinforcementDocument(string name)
        {
            Name = name;
        }

        public ReinforcementDocument(string name, string slabsLayer, double slabThickness)
        {
            Name = name;
            SlabsLayer = slabsLayer;
            SlabThickness = slabThickness;
        }

        public void UpdateDocument(string slabsLayer, double slabThickness)
        {
            SlabsLayer = slabsLayer;
            SlabThickness = slabThickness;
        }

        public void Save()
        {
            var db = HostApplicationServices.WorkingDatabase;
            using (var tr = db.TransactionManager.StartOpenCloseTransaction())
            {
                var NOD = (DBDictionary)tr.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForRead);
                DBDictionary dict;
                if (NOD.Contains("ReinforcementDocument"))
                {
                    dict = (DBDictionary)tr.GetObject(NOD.GetAt("ReinforcementDocument"), OpenMode.ForRead);
                }
                else
                {
                    NOD.UpgradeOpen();
                    dict = new DBDictionary();
                    NOD.SetAt("ReinforcementDocument", new DBDictionary());
                    tr.AddNewlyCreatedDBObject(dict, true);
                }
                Xrecord xrec;
                if (dict.Contains(Name))
                {
                    xrec = (Xrecord)tr.GetObject(dict.GetAt(Name), OpenMode.ForWrite);
                }
                else
                {
                    if (!dict.IsWriteEnabled) dict.UpgradeOpen();
                    xrec = new Xrecord();
                    dict.SetAt(Name, dict);
                    tr.AddNewlyCreatedDBObject(xrec, true);
                }
                xrec.Data = new ResultBuffer(new TypedValue(1, SlabsLayer), new TypedValue());
                tr.Commit();
            }
        }

        public static bool TryGetInstance(string name, out ReinforcementDocument instance)
        {
            var db = HostApplicationServices.WorkingDatabase;
            using (var tr = db.TransactionManager.StartOpenCloseTransaction())
            {
                var NOD = (DBDictionary)tr.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForRead);
                if (NOD.Contains("ReinforcementDocument"))
                {
                    var dict = (DBDictionary)tr.GetObject(NOD.GetAt("ReinforcementDocument"), OpenMode.ForRead);
                    if (dict.Contains(name))
                    {
                        var xrec = (Xrecord)tr.GetObject(dict.GetAt(name), OpenMode.ForRead);
                        var data = xrec.Data.AsArray();
                        instance = new ReinforcementDocument(name, (string)data[0].Value, (double)data[1].Value);
                        return true;
                    }
                }
            }
            instance = null;
            return false;
        }
    }&lt;/PRE&gt;</description>
    <pubDate>Sun, 02 Dec 2018 21:37:53 GMT</pubDate>
    <dc:creator>_gile</dc:creator>
    <dc:date>2018-12-02T21:37:53Z</dc:date>
    <item>
      <title>How to attach an instance of a custom .NET class to an AutoCAD drawing</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-attach-an-instance-of-a-custom-net-class-to-an-autocad/m-p/8439591#M24105</link>
      <description>&lt;P&gt;I've seen posts which discuss how to add data to an AutoCAD drawing particularly in:&amp;nbsp;&lt;A href="https://forums.autodesk.com/t5/net/xrecord/td-p/7784239.&amp;nbsp;" target="_blank"&gt;https://forums.autodesk.com/t5/net/xrecord/td-p/7784239.&amp;nbsp;&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is great. But what i need is a way to assign an instance of a class which contains some properties and functions to a document and save it.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is that possible?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My class looks something like this:&lt;/P&gt;
&lt;PRE&gt;Public Class ReinforcementDocument


    Public property name as String
    Public Property slabsLayer As String
    Public Property slabThickness As Double


    Public Sub New(ByRef name As String)

        Me.name = name

    End Sub
	
    Public Sub New(ByRef name As String, ByRef slabsLayer As String, ByRef slabThickness As Double)

        Me.name = name
        Me.slabsLayer = slabsLayer
        Me.slabThickness = slabThickness

    End Sub
    Public Sub updateDocument(ByRef slabsLayer As String, ByRef slabThickness As Double)

        Me.slabsLayer = slabsLayer
        Me.slabThickness = slabThickness

    End Sub

End Class&lt;/PRE&gt;
&lt;P&gt;I need to store an instance of that class into the current opened drawing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you in advance.&lt;/P&gt;</description>
      <pubDate>Sun, 02 Dec 2018 20:07:13 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-attach-an-instance-of-a-custom-net-class-to-an-autocad/m-p/8439591#M24105</guid>
      <dc:creator>jwe03</dc:creator>
      <dc:date>2018-12-02T20:07:13Z</dc:date>
    </item>
    <item>
      <title>Re: How to attach an instance of a custom .NET class to an AutoCAD drawing</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-attach-an-instance-of-a-custom-net-class-to-an-autocad/m-p/8439619#M24106</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The easiest way should be storing the properties of the instance in an Xrecord so that you can easily re-create a new instance of the class passing these values to the constructor.&lt;/P&gt;
&lt;P&gt;Another way (not so trivial) should be using serialization (you can see &lt;A href="https://adndevblog.typepad.com/autocad/2012/05/serialize-a-net-class-into-an-autocad-drawing-database.html" target="_blank"&gt;&lt;STRONG&gt;this topic&lt;/STRONG&gt;&lt;/A&gt;).&lt;/P&gt;</description>
      <pubDate>Sun, 02 Dec 2018 20:56:59 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-attach-an-instance-of-a-custom-net-class-to-an-autocad/m-p/8439619#M24106</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2018-12-02T20:56:59Z</dc:date>
    </item>
    <item>
      <title>Re: How to attach an instance of a custom .NET class to an AutoCAD drawing</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-attach-an-instance-of-a-custom-net-class-to-an-autocad/m-p/8439671#M24107</link>
      <description>&lt;P&gt;Here's a little example assuming the Name property have to be unique in a drawing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;    public class ReinforcementDocument
    {
        public string Name { get; }
        public string SlabsLayer { get; private set; } = "";
        public double SlabThickness { get; private set; }

        public ReinforcementDocument(string name)
        {
            Name = name;
        }

        public ReinforcementDocument(string name, string slabsLayer, double slabThickness)
        {
            Name = name;
            SlabsLayer = slabsLayer;
            SlabThickness = slabThickness;
        }

        public void UpdateDocument(string slabsLayer, double slabThickness)
        {
            SlabsLayer = slabsLayer;
            SlabThickness = slabThickness;
        }

        public void Save()
        {
            var db = HostApplicationServices.WorkingDatabase;
            using (var tr = db.TransactionManager.StartOpenCloseTransaction())
            {
                var NOD = (DBDictionary)tr.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForRead);
                DBDictionary dict;
                if (NOD.Contains("ReinforcementDocument"))
                {
                    dict = (DBDictionary)tr.GetObject(NOD.GetAt("ReinforcementDocument"), OpenMode.ForRead);
                }
                else
                {
                    NOD.UpgradeOpen();
                    dict = new DBDictionary();
                    NOD.SetAt("ReinforcementDocument", new DBDictionary());
                    tr.AddNewlyCreatedDBObject(dict, true);
                }
                Xrecord xrec;
                if (dict.Contains(Name))
                {
                    xrec = (Xrecord)tr.GetObject(dict.GetAt(Name), OpenMode.ForWrite);
                }
                else
                {
                    if (!dict.IsWriteEnabled) dict.UpgradeOpen();
                    xrec = new Xrecord();
                    dict.SetAt(Name, dict);
                    tr.AddNewlyCreatedDBObject(xrec, true);
                }
                xrec.Data = new ResultBuffer(new TypedValue(1, SlabsLayer), new TypedValue());
                tr.Commit();
            }
        }

        public static bool TryGetInstance(string name, out ReinforcementDocument instance)
        {
            var db = HostApplicationServices.WorkingDatabase;
            using (var tr = db.TransactionManager.StartOpenCloseTransaction())
            {
                var NOD = (DBDictionary)tr.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForRead);
                if (NOD.Contains("ReinforcementDocument"))
                {
                    var dict = (DBDictionary)tr.GetObject(NOD.GetAt("ReinforcementDocument"), OpenMode.ForRead);
                    if (dict.Contains(name))
                    {
                        var xrec = (Xrecord)tr.GetObject(dict.GetAt(name), OpenMode.ForRead);
                        var data = xrec.Data.AsArray();
                        instance = new ReinforcementDocument(name, (string)data[0].Value, (double)data[1].Value);
                        return true;
                    }
                }
            }
            instance = null;
            return false;
        }
    }&lt;/PRE&gt;</description>
      <pubDate>Sun, 02 Dec 2018 21:37:53 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-attach-an-instance-of-a-custom-net-class-to-an-autocad/m-p/8439671#M24107</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2018-12-02T21:37:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to attach an instance of a custom .NET class to an AutoCAD drawing</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-attach-an-instance-of-a-custom-net-class-to-an-autocad/m-p/8440126#M24108</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/109424"&gt;@_gile&lt;/a&gt;,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for your prompt reply.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the example you provided, you are saving the property slabLayer in the xRecord.&lt;/P&gt;
&lt;P&gt;That's good, but ultimately what I would want is to save the whole ReinforcementDocument instance into the current drawing instead of saving individual properties.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is that possible?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Dec 2018 06:44:49 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-attach-an-instance-of-a-custom-net-class-to-an-autocad/m-p/8440126#M24108</guid>
      <dc:creator>jwe03</dc:creator>
      <dc:date>2018-12-03T06:44:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to attach an instance of a custom .NET class to an AutoCAD drawing</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-attach-an-instance-of-a-custom-net-class-to-an-autocad/m-p/8440384#M24109</link>
      <description>&lt;P&gt;In the example I provided, I save the Name property (as Xrecord key) and the SlabsLayer and SlabThickness properties (as Xrecord key) which is all you need to re-create a new instance of ReinforcementDocument with the TryGetInstance() method.&lt;/P&gt;
&lt;PRE&gt;ReinforcementDocument instance = null;&lt;BR /&gt;if (ReinforcementDocument.TryGetInstance(someName, out instance))
{
    // an instance of ReinforcementDocument with Name == someName have been previously saved
    // you can do your stuff with 'instance'
}
else
{
    // none instance of ReinforcementDocument with Name == someName have been previously saved
}&lt;/PRE&gt;</description>
      <pubDate>Mon, 03 Dec 2018 09:14:11 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-attach-an-instance-of-a-custom-net-class-to-an-autocad/m-p/8440384#M24109</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2018-12-03T09:14:11Z</dc:date>
    </item>
    <item>
      <title>Re: How to attach an instance of a custom .NET class to an AutoCAD drawing</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-attach-an-instance-of-a-custom-net-class-to-an-autocad/m-p/8440424#M24110</link>
      <description>&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/109424"&gt;@_gile&lt;/a&gt;, yes sure it is all I need to recreate the instance.&lt;/P&gt;
&lt;P&gt;So, as a rule of thumb, if i need to save an instance of a class to a drawing, I would have to save all its properties (rather than the instance itself) and then recreate it when i try to get that data back. Is that right?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Dec 2018 09:28:50 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-attach-an-instance-of-a-custom-net-class-to-an-autocad/m-p/8440424#M24110</guid>
      <dc:creator>jwe03</dc:creator>
      <dc:date>2018-12-03T09:28:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to attach an instance of a custom .NET class to an AutoCAD drawing</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-attach-an-instance-of-a-custom-net-class-to-an-autocad/m-p/8440680#M24111</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/5439984"&gt;@jwe03&lt;/a&gt;&amp;nbsp; a écrit&amp;nbsp;:&lt;BR /&gt;
&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/109424"&gt;@_gile&lt;/a&gt;, yes sure it is all I need to recreate the instance.&lt;/P&gt;
&lt;P&gt;So, as a rule of thumb, if i need to save an instance of a class to a drawing, I would have to save all its properties (rather than the instance itself) and then recreate it when i try to get that data back. Is that right?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You could also use seralization like shown in the link I provided upper, but with such a simple class as yours, it seems to me it's not necessary.&lt;/P&gt;</description>
      <pubDate>Mon, 03 Dec 2018 11:44:45 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-attach-an-instance-of-a-custom-net-class-to-an-autocad/m-p/8440680#M24111</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2018-12-03T11:44:45Z</dc:date>
    </item>
  </channel>
</rss>

