Create mutiple layer with c#

Create mutiple layer with c#

traiduong014969
Collaborator Collaborator
1,346 Views
6 Replies
Message 1 of 7

Create mutiple layer with c#

traiduong014969
Collaborator
Collaborator

Hi all, 

Recently i start to Learn C#.

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.  You can see a snapshot below. Someone can help me fix this code

Thank you !!!

 
 

image_2023-01-18_232236989.png

 

 

 

0 Likes
Accepted solutions (2)
1,347 Views
6 Replies
Replies (6)
Message 2 of 7

Ed__Jobe
Mentor
Mentor

Hi @traiduong014969 

You only use the Add method once. You need to Add each layer. Also, when you post code, please use the </> 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.

 

ltb.Name = "Name1";
Layertb.Add(ltb);
trans.AddNewlyCreatedDBOjbect(ltb, true);

ltb.Name = "Name2";
Layertb.Add(ltb);
trans.AddNewlyCreatedDBOjbect(ltb, true);

 

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 3 of 7

traiduong014969
Collaborator
Collaborator

Thank for sussgestion, I have just test again code with your code. However, it only appear '' Layername2''  in layer box

[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 & Lr.Name ==Layername2)
                {
                    return true;
                }
            }
            return false;

        }

 

0 Likes
Message 4 of 7

Jeff_M
Consultant
Consultant
                    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);
Jeff_M, also a frequent Swamper
EESignature
0 Likes
Message 5 of 7

Ed__Jobe
Mentor
Mentor
Accepted solution

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.

 

Here is an example I have for importing page setups from a template using the CopyFrom method.


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();
}            }

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

Message 6 of 7

traiduong014969
Collaborator
Collaborator

I fixed my code via suggetion @Ed__Jobe and @Jeff_M. 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  I olny delete a layer of all layers ,  screen will not show notify.

please help me fix that

0 Likes
Message 7 of 7

Jeff_M
Consultant
Consultant
Accepted solution

@traiduong014969 I would do something more like this:

        [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();
            }
        }

 

I believe this will perform as you described.

Jeff_M, also a frequent Swamper
EESignature