change any Entity layer

change any Entity layer

Amremad
Collaborator Collaborator
1,020 Views
6 Replies
Message 1 of 7

change any Entity layer

Amremad
Collaborator
Collaborator

Hello 

 

I need to Create a Function called ChangeLayer

public void ChangeLayer (Entity ent, string layerName)
{
// Here can i change layer 
}

for any entity (Line, PLine, BlockReference, circle, .... )

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

_gile
Consultant
Consultant

Hi,

 

What's wrong with simply doing:

ent.Layer = layerName;


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 7

Amremad
Collaborator
Collaborator

🙂 sorry i missed 

 

i meant how can i read the layer of block when i insert it from other drawing,

 

i mean i need to insert the block from other dwg with the same it's layer. 

 

and can i use clone directly form other drawing to current ?? something like copy between drawings

0 Likes
Message 4 of 7

_gile
Consultant
Consultant
Accepted solution

Your request is not very clear.

You should post what you've written so far.

 

If you use the WBlockCloneObjects() with a BlockReference instance, you'll import also all symbolTableRecord instances (Layer, Linetype, etc.) linked to the block reference.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 5 of 7

Amremad
Collaborator
Collaborator

sorry about my english 

 

i have Drawing 'A" has a block which is name "blk" and it;s layer is "my Layer" with color "1"

now iam opening Drawing "B" , i need to insert "blk" in drawing "B" with the same it's layer "my layer"

 

i hope now you understand me

0 Likes
Message 6 of 7

_gile
Consultant
Consultant
Accepted solution

What's confusing is the world "block" which may mean BlockReference or BlockTableRecord.

As you say: "a block which is name "blk" and it;s layer is "my Layer" " I suppose you're talking about a block reference (a BlockTableRecord, aka block definition, does not 'have' a Layer).

As said upper, if you 'WBlockClone' (i.e. deep cloning) a block reference, you also clone the Layer the block reference is inserted on.

 

Here's a little example:

        private static ObjectId ImportBlockReference(Database targetDb, string fileName, string blockName)
        {
            using (var sourceDb = new Database(false, true))
            {
                sourceDb.ReadDwgFile(fileName, FileOpenMode.OpenForReadAndAllShare, false, null);
                using (var tr = sourceDb.TransactionManager.StartOpenCloseTransaction())
                {
                    var ms = (BlockTableRecord)tr.GetObject(
                        SymbolUtilityServices.GetBlockModelSpaceId(sourceDb), OpenMode.ForRead);
                    ObjectId brId = ObjectId.Null;
                    foreach (ObjectId id in ms)
                    {
                        if (id.ObjectClass.DxfName == "INSERT")
                        {
                            var br = (BlockReference)tr.GetObject(id, OpenMode.ForRead);
                            if (br.Name == blockName)
                                brId = id;
                        }
                    }
                    if (!brId.IsNull)
                    {
                        var ids = new ObjectIdCollection();
                        ids.Add(brId);
                        var targetId = SymbolUtilityServices.GetBlockModelSpaceId(targetDb);
                        var mapping = new IdMapping();
                        sourceDb.WblockCloneObjects(ids, targetId, mapping, DuplicateRecordCloning.Ignore, false);
                        if (mapping[brId].IsCloned)
                            return mapping[brId].Value;
                    }
                }
                return ObjectId.Null;
            }
        }

Running:

var id = ImportBlockReference(databaseOfDrawingB, @"A.dwg", "blk");

Will return the ObjectId of the cloned block reference in drawing B if a reference of "blk" have been found in drawing "A" model space.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 7 of 7

Amremad
Collaborator
Collaborator

yes that what i need exactly 🙂 

 

thanks for your help

 

 

0 Likes