Change block layer - NOT blockreference

Change block layer - NOT blockreference

Anonymous
Not applicable
1,866 Views
4 Replies
Message 1 of 5

Change block layer - NOT blockreference

Anonymous
Not applicable

Hello,

I am working with C#.

I have got a dwg with defined blocks in different layers - but not layer 0. I am able to change blockreferences layer. But, how can I have access to block layer ? - again not blockreference- to put it back on layer 0.

Thanks

 

0 Likes
Accepted solutions (1)
1,867 Views
4 Replies
Replies (4)
Message 2 of 5

norman.yuan
Mentor
Mentor

Block, or block definition, or BlockTableRecord (assuming you use C# with Acad .NET API) itself does not have "layer" property for you to change. It is the entities contained inside the block definition that have "layer" associated. So, you need to go inside a block definition and examine/change each entity's layer property as needed. The code would look like:

 

using (var tran=theDatabase.TransactionManager.StartTransaction())

{

    var bt=(BlockTable)tran.GetObject(myDatabase.BlockTableId, OpenMode.ForRead);

    if (bt.Has(theBlockName)

    {

        var br=(BlockTableRecord)tran.GetObject(bt[theBlockName], OpenMode.ForRead);

        foreach (ObjectId id in br)

        {

            var ent=(Entity)tran.GetObject(id,OpenMode.ForRead);

            if (ent.Layer!="0")

            {

                 ent.UpgradeOpen();

                 ent.Layer="0";

            }

        }

    }

    tran.Commit()

}

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 5

Anonymous
Not applicable
Hello,
thanks a lot for your solution. It works perfectly.
You are right, I wanted to change layers from all entities within the
block definition. Your solution is easier than the one I was working on
: explode the block, change layers, reconstruct an object, and then a
block ...
By the way, what is the command to set color for entities to "color from
layer" ?
Thanks again
0 Likes
Message 4 of 5

norman.yuan
Mentor
Mentor
Accepted solution

You set Color or ColorIndex proeprty of an Entity. To set color to "ByLayer", you can simply

 

Entity.ColorIndex=256;

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 5 of 5

Anonymous
Not applicable
Thanks !
0 Likes