.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

change any Entity layer

6 REPLIES 6
SOLVED
Reply
Message 1 of 7
Amremad
767 Views, 6 Replies

change any Entity layer

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, .... )

6 REPLIES 6
Message 2 of 7
_gile
in reply to: Amremad

Hi,

 

What's wrong with simply doing:

ent.Layer = layerName;


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 7
Amremad
in reply to: _gile

🙂 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

Message 4 of 7
_gile
in reply to: Amremad

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
in reply to: _gile

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

Message 6 of 7
_gile
in reply to: Amremad

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
in reply to: _gile

yes that what i need exactly 🙂 

 

thanks for your help

 

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Forma Design Contest


Autodesk Design & Make Report