Adding a new layer and making it current layer

By Virupaksha Aithal

Below sample code shows the procedure to add a new layer. All layers are stored in a symbol table called layer table. To make any layer as current layer, set the Database property Clayer with the object id of the layer.

[CommandMethod("AddLayer")]
public void AddLayer()
{
    Document doc = Application.DocumentManager.MdiActiveDocument;
    Database db = doc.Database;
    Editor ed = doc.Editor;
 
    using (Transaction tr = db.TransactionManager.StartTransaction())
    {
        LayerTable ltb = (LayerTable)tr.GetObject(db.LayerTableId, 
                                                  OpenMode.ForRead);
        //create a new layout.
        if (!ltb.Has("NewLayer"))
        {
            ltb.UpgradeOpen();
            LayerTableRecord newLayer = new LayerTableRecord();
            newLayer.Name = "NewLayer";
 
            newLayer.LineWeight = LineWeight.LineWeight005;
            newLayer.Description = "This is new layer";
 
            //red color
            newLayer.Color =
                    Autodesk.AutoCAD.Colors.Color.FromRgb(255, 0, 0);
 
            ltb.Add(newLayer);
            tr.AddNewlyCreatedDBObject(newLayer, true);
        }
 
        tr.Commit();
        //make it as current
        db.Clayer = ltb["NewLayer"];
    }
 
}

Comments

8 responses to “Adding a new layer and making it current layer”

  1. Just a note:
    When I don’t need the editor object, like in this case, I obtain the Database object from HostApplicationServices.WorkingDatabase because it’s thread-safe.
    Obtaining it this way is not.

  2. When a user runs a command, they expect it to use the database for the document running the command, which is not necessarily the working database.

  3. Artvegas Avatar
    Artvegas

    The layer Description property doesn’t seem to get set to. Didn’t work in either 2012 or 2013. Reading user-defined layer description worked ok.
    What’s up with that?

  4. Nikhil Ruikar Avatar
    Nikhil Ruikar

    The above written code works fine when I am creating a single layer. It fails when creating multiple layers.
    Here’s my code which fails at,
    LayerTable layerTable = (LayerTable)this.transaction.GetObject(this.database.LayerTableId, OpenMode.ForRead);
    Code:
    /////////////////////////////////////////////////////////////////////////////////////////////
    foreach (LayerAttributeColor item in layerColorAttributeValues)
    {
    LayerTable layerTable = (LayerTable)this.transaction.GetObject(this.database.LayerTableId, OpenMode.ForRead);
    if (!layerTable.Has(item.LayerName))
    {
    LayerTableRecord ltr = new LayerTableRecord();
    // … and set its properties
    ltr.Name = item.LayerName;
    ltr.Color = Autodesk.AutoCAD.Colors.Color.FromRgb(255, 0, 0);
    // Add the new layer to the layer table
    layerTable.UpgradeOpen();
    ObjectId ltId = layerTable.Add(ltr);
    layerTable.DowngradeOpen();
    this.transaction.AddNewlyCreatedDBObject(ltr, true);
    // Set the layer to be current for this drawing
    //this.database.Clayer = ltId;
    // Commit the transaction
    this.transaction.Commit();
    ltr.Dispose();
    }
    }
    /////////////////////////////////////////////////////////////////////////////////////////////
    The exception message reads as,
    “Operation is not valid due to the current state of the object”.
    Please advise.

  5. Use TrueCAD-intellicad Software.One of the best AutoCAD alternative cad software at affordable price. here is reference link “https://actcad.com/download-actcad-intellicad-software.php”

  6. The best part of using CMS IntelliCAD was finding out it has all the CAD features I’d already worked with. I was pretty worried about finding a good fit for me, but CMS IntelliCAD is definitely unbeatable. Registered for free and had a free trial before payment, I’m glad I could test it out before any payment. CMS IntelliCAD is definitely worth the hype.

  7. Was a bit hesitant to try at first but CMS IntelliCAD turned out to be a good alternative for me. The free trial worked immediately, which was good to try it out before paying for it. All the features it offers are user-friendly and easy to work with, which was another win for me. Good job.

  8. Nataly Avatar
    Nataly

    I’ve been using CADHOBBY IntelliCAD for my personal projects for a few months now, and I can say that it’s one of the best CAD software for hobbyists. It’s user-friendly, and the learning curve is not steep.

Leave a Reply to NatalyCancel reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading