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"];
}
}

Leave a Reply to DeepikaCancel reply