<?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: C# Bulk layer creation crashing AutoCAD in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/c-bulk-layer-creation-crashing-autocad/m-p/6422786#M35456</link>
    <description>&lt;P&gt;It crashes in the red highlight if i try to create the layer.&lt;/P&gt;&lt;P&gt;If i try to do just a foreach to print the selected layernames, this also crashes.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;foreach (var layer in layerList)
{
     MessabeBox.Show(layer.Name);
}&lt;/PRE&gt;</description>
    <pubDate>Thu, 07 Jul 2016 01:58:04 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2016-07-07T01:58:04Z</dc:date>
    <item>
      <title>C# Bulk layer creation crashing AutoCAD</title>
      <link>https://forums.autodesk.com/t5/net-forum/c-bulk-layer-creation-crashing-autocad/m-p/6422769#M35454</link>
      <description>&lt;P&gt;Hello Guys,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;This is what I am trying to do,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;- Access a remote dwg and get the lsit of layers&lt;/P&gt;&lt;P&gt;- Check if the layers exists in the current drawing&amp;nbsp;&lt;/P&gt;&lt;P&gt;- Create the layers (bulk)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am able to first two points, last point is there AutoCAD crahsing.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a collection of layertablerecord. Which says the count is 4 when do a foreach on that to read the layer names it is crashing autocad.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;List&amp;lt;LayerTableRecord&amp;gt; layers = new List&amp;lt;LayerTableRecord&amp;gt;();
                    layers = Helper.CADHelper.GetLayerfromDWG(GV.dwgTemplatePath, layernames);&lt;/PRE&gt;&lt;PRE&gt;public static void AddLayersToCurrentDrawing(List&amp;lt;LayerTableRecord&amp;gt; layerList)
        {
            if (layerList == null)
                throw new ArgumentNullException("layerList");

            if (layerList.Count == 0)
                throw new ArgumentOutOfRangeException("layerList");

            var curDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            if (curDoc == null)
                throw new ArgumentNullException("MdiActiveDocument");

            var curDb = curDoc.Database;

            try
            {
                using (var trans = curDb.TransactionManager.StartOpenCloseTransaction())
                {
                    &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;var layerTable = (LayerTable)trans.GetObject(curDb.LayerTableId, OpenMode.ForWrite);&lt;/STRONG&gt;&lt;/FONT&gt;
                    foreach (var layer in layerList)
                    {
                        if (layerTable.Has(layer.Name))
                            continue;

                        layerTable.Add(layer);
                        trans.AddNewlyCreatedDBObject(layer, true);
                    }

                    trans.Commit();
                }
            }
            catch (Autodesk.AutoCAD.Runtime.Exception ex)
            {
                System.Diagnostics.Debug.Write(ex.ToString());
                curDoc.Editor.WriteMessage(ex.ToString());
            }
            catch (SystemException ex)
            {
                curDoc.Editor.WriteMessage(ex.ToString());
            }
        }&lt;/PRE&gt;&lt;P&gt;it is crashing at the highlighted line. When creating layer.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please advise.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Jul 2016 01:39:04 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/c-bulk-layer-creation-crashing-autocad/m-p/6422769#M35454</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2016-07-07T01:39:04Z</dc:date>
    </item>
    <item>
      <title>Re: C# Bulk layer creation crashing AutoCAD</title>
      <link>https://forums.autodesk.com/t5/net-forum/c-bulk-layer-creation-crashing-autocad/m-p/6422784#M35455</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Raghulan,&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Your description is contradictory.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;The preamble says it crashes on the foreach ;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;The footnote says it crashes on the&amp;nbsp;highlighted line ( before the foreach).&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Also : What is the error message ??&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regards,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Jul 2016 01:54:52 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/c-bulk-layer-creation-crashing-autocad/m-p/6422784#M35455</guid>
      <dc:creator>kerry_w_brown</dc:creator>
      <dc:date>2016-07-07T01:54:52Z</dc:date>
    </item>
    <item>
      <title>Re: C# Bulk layer creation crashing AutoCAD</title>
      <link>https://forums.autodesk.com/t5/net-forum/c-bulk-layer-creation-crashing-autocad/m-p/6422786#M35456</link>
      <description>&lt;P&gt;It crashes in the red highlight if i try to create the layer.&lt;/P&gt;&lt;P&gt;If i try to do just a foreach to print the selected layernames, this also crashes.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;foreach (var layer in layerList)
{
     MessabeBox.Show(layer.Name);
}&lt;/PRE&gt;</description>
      <pubDate>Thu, 07 Jul 2016 01:58:04 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/c-bulk-layer-creation-crashing-autocad/m-p/6422786#M35456</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2016-07-07T01:58:04Z</dc:date>
    </item>
    <item>
      <title>Re: C# Bulk layer creation crashing AutoCAD</title>
      <link>https://forums.autodesk.com/t5/net-forum/c-bulk-layer-creation-crashing-autocad/m-p/6422967#M35457</link>
      <description>&lt;P&gt;Hi Raghu,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please try with below mentioned function.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;public static void CreateLayer(this Database db, string sLayerName, Transaction tr, int color = 7)&lt;BR /&gt;{&lt;BR /&gt;var acLyrTbl = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;&lt;/P&gt;&lt;P&gt;if (!acLyrTbl.Has(sLayerName))&lt;BR /&gt;{&lt;BR /&gt;var acLyrTblRec = new LayerTableRecord();&lt;BR /&gt;acLyrTblRec.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByAci, (short)color);&lt;BR /&gt;acLyrTblRec.Name = sLayerName;&lt;BR /&gt;acLyrTbl.UpgradeOpen();&lt;BR /&gt;acLyrTbl.Add(acLyrTblRec);&lt;BR /&gt;tr.AddNewlyCreatedDBObject(acLyrTblRec, true);&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;</description>
      <pubDate>Thu, 07 Jul 2016 05:36:15 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/c-bulk-layer-creation-crashing-autocad/m-p/6422967#M35457</guid>
      <dc:creator>nagamalliv</dc:creator>
      <dc:date>2016-07-07T05:36:15Z</dc:date>
    </item>
    <item>
      <title>Re: C# Bulk layer creation crashing AutoCAD</title>
      <link>https://forums.autodesk.com/t5/net-forum/c-bulk-layer-creation-crashing-autocad/m-p/6423659#M35458</link>
      <description>&lt;P&gt;So, you get a list of LayerTableRecords from a drawing, then bring these DBobjects out of the Transaction where the LayerTableRecords were created. Then you do to another drawing (current MdiActiveDocument in AutoCAD) and try to add the LayerTableRecord (from other drawing database) into LayerTable in a completely different Transaction!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In general, a database-residing object, such as LayerTableRecord, should be only used within a Transaction scope (unless your code creates it and has not added it into the Database). Also, a DBObject in one Database cannot be used in other database (you can use DeepCloneObjects()/WBlock()/WBlockCloneObjects()...to bring objects between databases).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In your case, if you need to compare layers in different drawings, you need to first get a list of layer information from other drawing, as you did in first step. But instead of LayerTableRecord, you may create a class that hold necessary layer information (name, color, linetype..., and store the information of the layers in a list of the class.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then, in your current drawing, loop through the list of your class, comparing layer name to see if the layer with the same name exists or not (or, if exists, compare if the layer properties, color, linetype..., are the same or not. Then add new layer (i.e. create a new LayerTableRecord) or update the existing layertablerecord.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Jul 2016 13:46:09 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/c-bulk-layer-creation-crashing-autocad/m-p/6423659#M35458</guid>
      <dc:creator>norman.yuan</dc:creator>
      <dc:date>2016-07-07T13:46:09Z</dc:date>
    </item>
    <item>
      <title>Re: C# Bulk layer creation crashing AutoCAD</title>
      <link>https://forums.autodesk.com/t5/net-forum/c-bulk-layer-creation-crashing-autocad/m-p/6424830#M35459</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Personally, I'd look at something like giles' sample:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;sourceDb.WblockCloneObjects(idCol, targetTableId, idMap, DuplicateRecordCloning.Ignore, false);

&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://www.theswamp.org/index.php?topic=42539.msg477455#msg477455" target="_blank"&gt;https://www.theswamp.org/index.php?topic=42539.msg477455#msg477455&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Jul 2016 23:55:25 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/c-bulk-layer-creation-crashing-autocad/m-p/6424830#M35459</guid>
      <dc:creator>kerry_w_brown</dc:creator>
      <dc:date>2016-07-07T23:55:25Z</dc:date>
    </item>
    <item>
      <title>Re: C# Bulk layer creation crashing AutoCAD</title>
      <link>https://forums.autodesk.com/t5/net-forum/c-bulk-layer-creation-crashing-autocad/m-p/6424916#M35460</link>
      <description>Thanks Mate, I understand what you saying, ill try it and let you know.</description>
      <pubDate>Fri, 08 Jul 2016 01:43:28 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/c-bulk-layer-creation-crashing-autocad/m-p/6424916#M35460</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2016-07-08T01:43:28Z</dc:date>
    </item>
  </channel>
</rss>

