using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Colors;
namespace mylayerClassLibrary1
{
public class Class1
{
[CommandMethod("SetLayerColor")]
public static void SetLayerColor()
{
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
LinetypeTable acLinTbl;
acLinTbl = acTrans.GetObject(acCurDb.LinetypeTableId,
OpenMode.ForRead) as LinetypeTable;
string[] acLineTyps
= new string[3]; acLineTyps[0] = "BORDER";
acLineTyps[1] = "BORDER2";
acLineTyps[2] = "BORDERX2";
foreach (string acLineTyp in acLineTyps)
{
if (!acLinTbl.Has("acLineTyps"))
{
acCurDb.LoadLineTypeFile(acLineTyp, "C:/Program Files/Autodesk/AutoCAD 2014/linetypes/acad");
}
}
LayerTable acLyrTbl;
acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId,
OpenMode.ForRead) as LayerTable;
string[] sLayerNames
= new string[3]; sLayerNames[0] = "G-ANNO-REFR";
sLayerNames[1] = "M-ANNO-TTLB";
sLayerNames[2] = "M-ANNO-TEXT-NEWW";
Color
[] acColors
= new Color
[3]; acColors[0] = Color.FromColorIndex(ColorMethod.ByAci, 15);
acColors[1] = Color.FromColorIndex(ColorMethod.ByAci, 7);
acColors[2] = Color.FromColorIndex(ColorMethod.ByAci, 7);
int nCnt = 0;
foreach (string sLayerName in sLayerNames)
{
if (!acLyrTbl.Has(sLayerName))
{
using (LayerTableRecord acLyrTblRec
= new LayerTableRecord
()) {
acLyrTblRec.Name = sLayerName;
if (!acLyrTbl.IsWriteEnabled) acLyrTbl.UpgradeOpen();
acLyrTbl.Add(acLyrTblRec);
acTrans.AddNewlyCreatedDBObject(acLyrTblRec, true);
acLyrTblRec.Color = acColors[nCnt];
}
}
else
{
LayerTableRecord acLyrTblRec = acTrans.GetObject(acLyrTbl[sLayerName],
OpenMode.ForWrite) as LayerTableRecord;
acLyrTblRec.Color = acColors[nCnt];
}
++ nCnt;
}
// Open the Layer table for read
string NEWsLayerName = "G-ANNO-REFR";
{
LayerTableRecord acLyrTblRec = acTrans.GetObject(acLyrTbl[NEWsLayerName],
OpenMode.ForRead) as LayerTableRecord;
if (acLinTbl.Has("BORDERX2") == true)
{
acLyrTblRec.UpgradeOpen();
acLyrTblRec.LinetypeObjectId = acLinTbl["BORDERX2"];
}
}
string ANEWsLayerName = "M-ANNO-TTLB";
{
LayerTableRecord acLyrTblRec = acTrans.GetObject(acLyrTbl[ANEWsLayerName],
OpenMode.ForRead) as LayerTableRecord;
if (acLinTbl.Has("BORDER2") == true)
{
acLyrTblRec.UpgradeOpen();
acLyrTblRec.LinetypeObjectId = acLinTbl["BORDER2"];
}
}
string BNEWsLayerName = "M-ANNO-TEXT-NEWW";
{
LayerTableRecord acLyrTblRec = acTrans.GetObject(acLyrTbl[BNEWsLayerName],
OpenMode.ForRead) as LayerTableRecord;
if (acLinTbl.Has("BORDER") == true)
{
acLyrTblRec.UpgradeOpen();
acLyrTblRec.LinetypeObjectId = acLinTbl["BORDER"];
}
}
acTrans.Commit();
}
}
}
}