make existing layer as active layer

make existing layer as active layer

almutaz_86
Advocate Advocate
946 Views
6 Replies
Message 1 of 7

make existing layer as active layer

almutaz_86
Advocate
Advocate

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));
}
}
}

 

0 Likes
Accepted solutions (1)
947 Views
6 Replies
Replies (6)
Message 2 of 7

_gile
Consultant
Consultant
Accepted solution

Hi,

Try to make the things as simple as possible. You shoud not get the working database in a using statement, you do not really need to check which is the current layer (anyway, Convert.ToString(dbs.Clayer) always return false because dbs.Clayer is an ObjectId) and overall you do not need to iterate through the layer table and do all the stuff for each layer (even committing the transaction).

 

private void CreateLayer(string layerName)
{
    var db = HostApplicationServices.WorkingDatabase;
    using (var tr = db.TransactionManager.StartTransaction())
    {
        var layerTable = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead);
        // if the layer table does not already contain the layer, create it
        if (!layerTable.Has(layerName))
        {
            tr.GetObject(db.LayerTableId, OpenMode.ForWrite);
            var layer = new LayerTableRecord { Name = layerName };
            layerTable.Add(layer);
            tr.AddNewlyCreatedDBObject(layer, true);
        }
        // make layerName current
        db.Clayer = layerTable[layerName];
        tr.Commit();
    }
}

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 7

almutaz_86
Advocate
Advocate

I appreciate you very much @_gile 
and thank you 
I'm really struggling to keep codes simple, but I dont have enough experience in .net

 

if you may please to recommend a toturial for AutoCAD .net api  ?

Thanks again 

0 Likes
Message 4 of 7

jaboone
Advocate
Advocate

How would you apply all the other things to this function so that it modifies the color, lock, transparency, noplot, on/off, description and linestyle?  Did I miss anything?

Learning as I go
0 Likes
Message 5 of 7

_gile
Consultant
Consultant

@jaboone  a écrit :

How would you apply all the other things to this function so that it modifies the color, lock, transparency, noplot, on/off, description and linestyle?  Did I miss anything?


Simply set the properties  of the LayerTableRecord instance.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 6 of 7

jaboone
Advocate
Advocate

Thanks.  I see you are completely right to add to the table.

I modified this line

Dim layer = New LayerTableRecord With {.Name = LayerName, .LineWeight = LineWeight.ByLayer, .IsLocked = False, .Description = LayerDescription, .IsPlottable = IsItPlottable, .IsFrozen = False}

 

Thanks for the help

Learning as I go
0 Likes
Message 7 of 7

jaboone
Advocate
Advocate

One problem I see with this layer table is that I can't set the layer created to a current layer with adding a new command line to do it.  Can you talk to your API group to add a .SetCurrent = True to make it a all for one, then I could use a create layer each time I want to set a layer?  Or maybe there is a better method?

Learning as I go
0 Likes