<?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: Create mutiple layer with c# in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/create-mutiple-layer-with-c/m-p/11688720#M10264</link>
    <description>&lt;P&gt;Thank for sussgestion, I have just test again code with your code. However, it only appear '' Layername2''&amp;nbsp; in layer box&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;[CommandMethod("CreateLayer")]
        public void CreateLayer()
        {
            Editor ed = AcAp.DocumentManager.MdiActiveDocument.Editor;
            Database db = AcAp.DocumentManager.MdiActiveDocument.Database;
            ObjectId layerId = db.LayerTableId;     
            Transaction trans = db.TransactionManager.StartTransaction();   
            LayerTable Layertb = trans.GetObject(layerId, OpenMode.ForWrite) as LayerTable;

            try
            {
                string Layername1 = "Newlayer1";
                string Layername2 = "Newlayer2";
                if (IsexistingLayer(trans, Layertb, Layername1,  Layername2) == false)
                {
                    LayerTableRecord ltb = new LayerTableRecord();
                    ltb.Name = Layername1;
                    Layertb.Add(ltb);
                    trans.AddNewlyCreatedDBObject(ltb, true);
                    ltb.Name = Layername2;
                    Layertb.Add(ltb);
                    trans.AddNewlyCreatedDBObject(ltb, true);
                }
                else
                {
                    Application.ShowAlertDialog("Layer is existing");
                }

            }

            catch (Autodesk.AutoCAD.Runtime.Exception ex)
            {
                //ed.WriteMessage("layer dupplicted");
                Application.ShowAlertDialog("Layer is existing");
            }
            trans.Commit(); 

        }
        private Boolean IsexistingLayer(Transaction trans, LayerTable ltb, string Layername1, string Layername2)
        {
            LayerTableRecord Lr;
            foreach (ObjectId ob in ltb)
            {
                Lr = trans.GetObject(ob, OpenMode.ForRead) as LayerTableRecord;
                if (Lr.Name == Layername1 &amp;amp; Lr.Name ==Layername2)
                {
                    return true;
                }
            }
            return false;

        }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 18 Jan 2023 17:09:19 GMT</pubDate>
    <dc:creator>traiduong014969</dc:creator>
    <dc:date>2023-01-18T17:09:19Z</dc:date>
    <item>
      <title>Create mutiple layer with c#</title>
      <link>https://forums.autodesk.com/t5/net-forum/create-mutiple-layer-with-c/m-p/11688612#M10262</link>
      <description>&lt;P&gt;Hi all,&amp;nbsp;&lt;/P&gt;&lt;P&gt;Recently i start to Learn C#.&lt;/P&gt;&lt;P&gt;I start with basic object is create "Name layer". So have some problem when i create multiple layer. It only appear a layer in layer table.In spite of i created two layer in my code.&amp;nbsp; You can see a snapshot below. Someone can help me fix this code&lt;/P&gt;&lt;P&gt;Thank you !!!&lt;/P&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image_2023-01-18_232236989.png" style="width: 734px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1164228iB199D60099113AC8/image-size/large?v=v2&amp;amp;px=999" role="button" title="image_2023-01-18_232236989.png" alt="image_2023-01-18_232236989.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 18 Jan 2023 16:28:51 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/create-mutiple-layer-with-c/m-p/11688612#M10262</guid>
      <dc:creator>traiduong014969</dc:creator>
      <dc:date>2023-01-18T16:28:51Z</dc:date>
    </item>
    <item>
      <title>Re: Create mutiple layer with c#</title>
      <link>https://forums.autodesk.com/t5/net-forum/create-mutiple-layer-with-c/m-p/11688659#M10263</link>
      <description>&lt;P&gt;Hi &lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/6377672"&gt;@traiduong014969&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You only use the Add method once. You need to Add each layer. Also, when you post code, please use the &amp;lt;/&amp;gt; button to paste code to a special window. Please use the box in the upper left of the window to choose C# as the language format. Do not use images. See code below.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;ltb.Name = "Name1";
Layertb.Add(ltb);
trans.AddNewlyCreatedDBOjbect(ltb, true);

ltb.Name = "Name2";
Layertb.Add(ltb);
trans.AddNewlyCreatedDBOjbect(ltb, true);&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 18 Jan 2023 16:47:14 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/create-mutiple-layer-with-c/m-p/11688659#M10263</guid>
      <dc:creator>Ed__Jobe</dc:creator>
      <dc:date>2023-01-18T16:47:14Z</dc:date>
    </item>
    <item>
      <title>Re: Create mutiple layer with c#</title>
      <link>https://forums.autodesk.com/t5/net-forum/create-mutiple-layer-with-c/m-p/11688720#M10264</link>
      <description>&lt;P&gt;Thank for sussgestion, I have just test again code with your code. However, it only appear '' Layername2''&amp;nbsp; in layer box&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;[CommandMethod("CreateLayer")]
        public void CreateLayer()
        {
            Editor ed = AcAp.DocumentManager.MdiActiveDocument.Editor;
            Database db = AcAp.DocumentManager.MdiActiveDocument.Database;
            ObjectId layerId = db.LayerTableId;     
            Transaction trans = db.TransactionManager.StartTransaction();   
            LayerTable Layertb = trans.GetObject(layerId, OpenMode.ForWrite) as LayerTable;

            try
            {
                string Layername1 = "Newlayer1";
                string Layername2 = "Newlayer2";
                if (IsexistingLayer(trans, Layertb, Layername1,  Layername2) == false)
                {
                    LayerTableRecord ltb = new LayerTableRecord();
                    ltb.Name = Layername1;
                    Layertb.Add(ltb);
                    trans.AddNewlyCreatedDBObject(ltb, true);
                    ltb.Name = Layername2;
                    Layertb.Add(ltb);
                    trans.AddNewlyCreatedDBObject(ltb, true);
                }
                else
                {
                    Application.ShowAlertDialog("Layer is existing");
                }

            }

            catch (Autodesk.AutoCAD.Runtime.Exception ex)
            {
                //ed.WriteMessage("layer dupplicted");
                Application.ShowAlertDialog("Layer is existing");
            }
            trans.Commit(); 

        }
        private Boolean IsexistingLayer(Transaction trans, LayerTable ltb, string Layername1, string Layername2)
        {
            LayerTableRecord Lr;
            foreach (ObjectId ob in ltb)
            {
                Lr = trans.GetObject(ob, OpenMode.ForRead) as LayerTableRecord;
                if (Lr.Name == Layername1 &amp;amp; Lr.Name ==Layername2)
                {
                    return true;
                }
            }
            return false;

        }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 18 Jan 2023 17:09:19 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/create-mutiple-layer-with-c/m-p/11688720#M10264</guid>
      <dc:creator>traiduong014969</dc:creator>
      <dc:date>2023-01-18T17:09:19Z</dc:date>
    </item>
    <item>
      <title>Re: Create mutiple layer with c#</title>
      <link>https://forums.autodesk.com/t5/net-forum/create-mutiple-layer-with-c/m-p/11688734#M10265</link>
      <description>&lt;LI-CODE lang="csharp"&gt;                    LayerTableRecord ltb = new LayerTableRecord();
                    ltb.Name = Layername1;
                    Layertb.Add(ltb);
                    trans.AddNewlyCreatedDBObject(ltb, true);
                    ltb = new LayerTableRecord(); //Add this line
                    ltb.Name = Layername2;
                    Layertb.Add(ltb);
                    trans.AddNewlyCreatedDBObject(ltb, true);&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 18 Jan 2023 17:18:02 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/create-mutiple-layer-with-c/m-p/11688734#M10265</guid>
      <dc:creator>Jeff_M</dc:creator>
      <dc:date>2023-01-18T17:18:02Z</dc:date>
    </item>
    <item>
      <title>Re: Create mutiple layer with c#</title>
      <link>https://forums.autodesk.com/t5/net-forum/create-mutiple-layer-with-c/m-p/11688737#M10266</link>
      <description>&lt;P&gt;Sorry, I left out that you have to create a new LayerTableRecord for each layer. Also, you probably want to set the other properties of the layer besides just the Name, e.g. Color, Linetype, etc. After you get all the properties set, then you can Add the layer. If you are writing this code to add a bunch of layers to a dwg that doesn't match your template. A better method would be to open the template in a side database and just copy the layers to the current dwg.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is an example I have for importing page setups from a template using the CopyFrom method.&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;
using (Transaction sourceTrans = sourceDb.TransactionManager.StartTransaction())
{
    // import template page setups
    // Importing page setups also brings in referenced views.
    DBDictionary sourceDict = sourceTrans.GetObject(sourceDb.PlotSettingsDictionaryId, OpenMode.ForRead) as DBDictionary;
    foreach (DBDictionaryEntry de in sourceDict)
    {
        PlotSettings SourcePlotSettings = sourceTrans.GetObject((ObjectId)(de.Value), OpenMode.ForRead) as PlotSettings;
        PlotSettings tempPlotSettings = new PlotSettings(SourcePlotSettings.ModelType);
        tempPlotSettings.CopyFrom(SourcePlotSettings);
        tempPlotSettings.AddToPlotSettingsDictionary(destDb);
        destTrans.AddNewlyCreatedDBObject(tempPlotSettings, true);
    }
    sourceTrans.Commit();
}            }&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 18 Jan 2023 17:23:29 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/create-mutiple-layer-with-c/m-p/11688737#M10266</guid>
      <dc:creator>Ed__Jobe</dc:creator>
      <dc:date>2023-01-18T17:23:29Z</dc:date>
    </item>
    <item>
      <title>Re: Create mutiple layer with c#</title>
      <link>https://forums.autodesk.com/t5/net-forum/create-mutiple-layer-with-c/m-p/11688807#M10267</link>
      <description>&lt;P&gt;I fixed my code via suggetion&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/14801"&gt;@Ed__Jobe&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/32637"&gt;@Jeff_M&lt;/a&gt;. However when i try delete either layer and retye command. on the screen appeare a notity "Layer is existing". I do not want that. I only want that the screen appear notify when all layer in my code have layer table. If&amp;nbsp; I olny delete a layer of all layers ,&amp;nbsp; screen will not show notify.&lt;/P&gt;&lt;P&gt;please help me fix that&lt;/P&gt;</description>
      <pubDate>Wed, 18 Jan 2023 18:02:38 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/create-mutiple-layer-with-c/m-p/11688807#M10267</guid>
      <dc:creator>traiduong014969</dc:creator>
      <dc:date>2023-01-18T18:02:38Z</dc:date>
    </item>
    <item>
      <title>Re: Create mutiple layer with c#</title>
      <link>https://forums.autodesk.com/t5/net-forum/create-mutiple-layer-with-c/m-p/11688918#M10268</link>
      <description>&lt;P&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/6377672"&gt;@traiduong014969&lt;/a&gt;&amp;nbsp;I would do something more like this:&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;        [CommandMethod("CreateLayer")]
        public void CreateLayer()
        {
            Editor ed = AcAp.DocumentManager.MdiActiveDocument.Editor;
            Database db = AcAp.DocumentManager.MdiActiveDocument.Database;
            ObjectId layerId = db.LayerTableId;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                LayerTable Layertb = trans.GetObject(layerId, OpenMode.ForWrite) as LayerTable;
                string[] layernames = { "Newlayer1", "Newlayer2" };
                LayerTableRecord ltb;
                foreach (string layername in layernames)
                {
                    if (Layertb.Has(layername))
                    {
                        AcAp.ShowAlertDialog("Layer " + layername + " is existing");
                        continue;
                    }
                    ltb = new LayerTableRecord();
                    ltb.Name = layername;
                    Layertb.Add(ltb);
                    trans.AddNewlyCreatedDBObject(ltb, true);
                }
                trans.Commit();
            }
        }&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I believe this will perform as you described.&lt;/P&gt;</description>
      <pubDate>Wed, 18 Jan 2023 18:50:17 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/create-mutiple-layer-with-c/m-p/11688918#M10268</guid>
      <dc:creator>Jeff_M</dc:creator>
      <dc:date>2023-01-18T18:50:17Z</dc:date>
    </item>
  </channel>
</rss>

