Message 1 of 7
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Good morning,
there is some thing wrong with this code, I cant figure it
when layer exist , it dosen't make it as active layer
private void Layercreate(string Layer_Name)
{
using (var dbs = HostApplicationServices.WorkingDatabase)
using (var Trans = dbs.TransactionManager.StartTransaction())
{
try
{
LayerTable Layer_Table = (LayerTable)Trans.GetObject(dbs.LayerTableId, OpenMode.ForRead);
LayerTableRecord layer_chk;
foreach (ObjectId layer_chk_id in Layer_Table)
{
layer_chk = Trans.GetObject(layer_chk_id, OpenMode.ForRead) as LayerTableRecord;
if (Layer_Table.Has(Layer_Name))// check if layer exist
{
string Active_Layer = Convert.ToString(dbs.Clayer);
if (Active_Layer != Layer_Name)//check if requsted layer is the current layer
{
dbs.Clayer = Layer_Table[Layer_Name];//make existing layer as current
break;
}
else
{
Trans.Commit();
break;
}
}
else
{ //create layer if it is not exist
ObjectId Layer_Object_ID = dbs.LayerTableId;
Layer_Table = Trans.GetObject(Layer_Object_ID, OpenMode.ForWrite) as LayerTable;
LayerTableRecord Layer_Tablerec = new LayerTableRecord
{
Name = Layer_Name
};
Layer_Table.Add(Layer_Tablerec);
Trans.AddNewlyCreatedDBObject(Layer_Tablerec, true);
Application.SetSystemVariable("clayer", Layer_Name);//make it current, is there better way to do this ??
Trans.Commit();
break;
}
}
}
catch (Autodesk.AutoCAD.Runtime.Exception _Exeption)
{
Editor editor = Application.DocumentManager.MdiActiveDocument.Editor;
editor.WriteMessage("\n" + Convert.ToString(_Exeption));
}
}
}
Solved! Go to Solution.