Not that this is anywhere near done, but for those following along, here is where Im at
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.EditorInput;
namespace LayerCode
{
public class LayerCode
{
[CommandMethod("CD3")]
static public void createduhlayer()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead, false) as LayerTable;
if (lt == null)
return;
if (lt.Has("DSH"))
return;
LayerTableRecord newLTR = new LayerTableRecord();
newLTR.Name = "DSH";
newLTR.Color = Color.FromColorIndex(ColorMethod.ByAci, 2);
// set any other properties
lt.UpgradeOpen();
lt.Add(newLTR);
tr.AddNewlyCreatedDBObject(newLTR, true);
tr.Commit();
}
//createLayer("Duh2");
createLayerXML("Duh3");
}
static public void createLayer(string Lname)
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead, false) as LayerTable;
if (lt == null)
return;
if (lt.Has(Lname))
return;
LayerTableRecord newLTR = new LayerTableRecord();
newLTR.Name = Lname;
newLTR.Color = Color.FromColorIndex(ColorMethod.ByAci, 2);
// set any other properties
lt.UpgradeOpen();
lt.Add(newLTR);
tr.AddNewlyCreatedDBObject(newLTR, true);
tr.Commit();
}
}
static public void createLayerXML(string Lname)
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead, false) as LayerTable;
if (lt == null)
return;
if (lt.Has(Lname))
{
return;
}
else
{
DataSet dsLayerData = new DataSet();
dsLayerData.ReadXml(@"c:\LayerDefinitions.xml");
DataView dsLayerDefinition = new DataView(dsLayerData.Tables["Layer"]);
dsLayerDefinition.Sort = "LayerName";
int rowIndex = dsLayerDefinition.Find(Lname);
if (rowIndex == -1)
{
ed.WriteMessage("Bogus Layer Name");
}
else
{
short lC = Convert.ToInt16(dsLayerDefinition[rowIndex]["Color"]);
string lnT = dsLayerDefinition[rowIndex]["LineType"].ToString();
LinetypeTable LTT = tr.GetObject(db.LinetypeTableId, OpenMode.ForRead, false) as LinetypeTable;
if (LTT == null)
return;
if (LTT.Has(lnT))
return;
else
{
LinetypeTableRecord newLTTR = new LinetypeTableRecord();
newLTTR.Name = lnT;
LTT.UpgradeOpen();
LTT.Add(newLTTR);
tr.AddNewlyCreatedDBObject(newLTTR, true);
}
//string layerColor = dsLayerDefinition[rowIndex]["Color"].ToString();
//short lC = Convert.ToInt16(layerColor);
LayerTableRecord newLTR = new LayerTableRecord();
newLTR.Name = Lname;
newLTR.Color = Color.FromColorIndex(ColorMethod.ByAci, lC);
newLTR.LinetypeObjectId = newLTTR;
lt.UpgradeOpen();
lt.Add(newLTR);
tr.AddNewlyCreatedDBObject(newLTR, true);
}
}
// set any other properties
tr.Commit();
}
}}