checking if an enitities layer is off

checking if an enitities layer is off

a.kouchakzadeh
Advocate Advocate
681 Views
8 Replies
Message 1 of 9

checking if an enitities layer is off

a.kouchakzadeh
Advocate
Advocate

hi every one

 

I searched the net to find out how is it possible to check if an entities layer is set to off, but I couldn't come up with anything. can anyone give me a hand on this?

 

i extracted the entity using the transaction

I can set the entity.layer="..."

but i dont know how to check if its layer is off.

entity.layer.isoff doesn't work

0 Likes
Accepted solutions (1)
682 Views
8 Replies
Replies (8)
Message 2 of 9

_gile
Consultant
Consultant

Hi,

You do not need to check all the entities, you have to directly check each LayerTableRecord. This is done iterating through the layer table.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 9

a.kouchakzadeh
Advocate
Advocate

Hello sir

what I want to do is to check if an entity is in a layer which is set to off, I want to leave it as it is, otherwise, I want to set its layer to lets say "0".

the first thing I did was get the layer table record, and:

 

string hiddenlayers;

if(layer.isoff)

{

     trans.getobject(layertabID , openmode.forwrite);

     hiddenlayers +=layer.name + ","

}

 

blocktalbe bt = tr.getobject(db.BlockTable, openmode.forread) as BlockTable;

foreach (objectID btid in bt)

{     

     BlockTableRecord btr = tr.GetObject(btid, openmode.forread) as BlockTableRecord;

     foreach( ObjectID id in btr)

     {

          entity ent= tr.GetObject(id, openmode.forwrite) as entity;

          if(hiddenlayers.contains(entity.layer + ",") continue;

          entity.layer="0";

          entity.color=256;

     }

}

          

but when i run the program, even entities in layers which are set to off, are set to layer "0" and are visible.

I cant figure out how to fix it.

               

0 Likes
Message 4 of 9

_gile
Consultant
Consultant

Rather than using a string to store the name of off layers, you should use a List<string> or, more efficient with Contains, a HashSet<string>.

 

    using (var tr = db.TransactionManager.StartTransaction())
    {
        // collect all off layer names
        var layerTable = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead);
        var offLayers = new HashSet<string>();
        foreach (ObjectId id in layerTable)
        {
            var layer = (LayerTableRecord)tr.GetObject(id, OpenMode.ForRead);
            if (layer.IsOff)
                offLayers.Add(layer.Name);
        }
        // get all entities
        var blockTable = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
        foreach (ObjectId btrId in blockTable)
        {
            var btr = (BlockTableRecord)tr.GetObject(btrId, OpenMode.ForRead);
            foreach (ObjectId id in btr)
            {
                var entity = (Entity)tr.GetObject(id, OpenMode.ForRead);
                if (offLayers.Contains(entity.Layer))
                    continue;
                entity.Layer = "0";
                entity.ColorIndex = 256;
            }
        }
        tr.Commit();
    }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 5 of 9

a.kouchakzadeh
Advocate
Advocate

I have done exactly as you said, but it sill doesnt work properly.

for example, in this drawing file, fancoildet layer is set to off. after you run the program, it sets that layer to what ever you have chosen as well.

any idea why this happens?

0 Likes
Message 6 of 9

_gile
Consultant
Consultant

I do not understand what you mean with " it sill doesnt work properly."

From the test I did the layer and color of entities is changed if the layer of the entity is On and remain the same if the layer is Off.

 


@a.kouchakzadeh  a écrit :

for example, in this drawing file, fancoildet layer is set to off. after you run the program, it sets that layer to what ever you have chosen as well.


It doesn't seems to be the case for me.

_gile_0-1645786878999.png

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 7 of 9

a.kouchakzadeh
Advocate
Advocate

I assume Im not explaining my problem correctly.

in this picture, which is the original drawing before I run the program, you can see there are elements which are in layers that are set to off. you can see where they are, but they are hidden:

before.JPG

after I run the program, the elements that were in off layers are suppose to stay on off layers.

but this is not the case because they appear and that means their layer has changed to the one that the user has selected:

after.JPG

0 Likes
Message 8 of 9

_gile
Consultant
Consultant
Accepted solution

What you describe is not due to the code I provided.

The entites you're talking about are on a frozen layer (not off).



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 9 of 9

a.kouchakzadeh
Advocate
Advocate

my bad 🤐

thank you sir

0 Likes