public static void CreateLayer(string sLayerName, LineWeight lineWeight, Color color)
{
// Get the current document and database
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
// Start a transaction
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Open the Layer table for read
LayerTable acLyrTbl;
acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId,
OpenMode.ForRead) as LayerTable;
//string sLayerName= "Center";
LayerTableRecord acLyrTblRec;
if (acLyrTbl.Has(sLayerName) == false)
{
acLyrTblRec = new LayerTableRecord();
// Assign the layer the ACI color 1 and a name
acLyrTblRec.Color = color;
acLyrTblRec.LineWeight = lineWeight;
acLyrTblRec.Name = sLayerName;
// Upgrade the Layer table for write
acLyrTbl.UpgradeOpen();
// Append the new layer to the Layer table and the transaction
acLyrTbl.Add(acLyrTblRec);
acTrans.AddNewlyCreatedDBObject(acLyrTblRec, true);
}
else {
acLyrTblRec = acTrans.GetObject(acLyrTbl[sLayerName],
OpenMode.ForRead) as LayerTableRecord;
}
// Open the Layer table for read
LinetypeTable acLinTbl;
acLinTbl = acTrans.GetObject(acCurDb.LinetypeTableId,
OpenMode.ForRead) as LinetypeTable;
if (acLinTbl.Has("Center") == true)
{
// Upgrade the Layer Table Record for write
acLyrTblRec.UpgradeOpen();
// Set the linetype for the layer
acLyrTblRec.LinetypeObjectId = acLinTbl["Center"];
}
// Save the changes and dispose of the transaction
acTrans.Commit();
}
}