Issue of copying entities to other layer

Issue of copying entities to other layer

MarkJamesRogolino
Advocate Advocate
4,570 Views
21 Replies
Message 1 of 22

Issue of copying entities to other layer

MarkJamesRogolino
Advocate
Advocate

Hello everyone.

Now I am making an AutoCad Plugin copies all layers. But I made only copying layers module.

How can I make all the entities are copied to duplicated layers(e.g. New_layername)?

Can someone help me? Thanks in after.

 

0 Likes
Accepted solutions (1)
4,571 Views
21 Replies
Replies (21)
Message 2 of 22

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

one way going through each entity (if you really want to copy each single entity and not just content of modelspace):

  • go through each BlockTableRecord in BlockTable,
  • each BlockTableRecord has Entities,
  • copy the entity,
  • assign the copied entity the layer you like to have
  • add this copied item to the BlockTableRecord.

 

Or do you have another question or where is the point you can't continue?

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 3 of 22

MarkJamesRogolino
Advocate
Advocate

Now I am making it with c# .net.

Do you have some similar sample code?

 

0 Likes
Message 4 of 22

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

there exist so many help already in www about how to copy objects using AutoCAD with .NET like this:

>>>click<<<

 

And no, I don't have such a code ready, so please start with your part and show then the code with the question where you can't continue.

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 5 of 22

MarkJamesRogolino
Advocate
Advocate

okay, I will try, Thanks.

0 Likes
Message 6 of 22

MarkJamesRogolino
Advocate
Advocate

my issue is copying between layers in one drawing.

but I can not find such help url.

0 Likes
Message 7 of 22

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

>> my issue is copying between layers in one drawing.

You first need to copy an entity ... as shown >>>here<<<

Then, before adding the object back to the BlockTableRecord use

Ent.Layer = "0" ... or the layername you want to assign it to (the layer has to exist, better/faster would be to assign it the LayerID)

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
Message 8 of 22

MarkJamesRogolino
Advocate
Advocate

Hi, Thanks for your help. but there are some errors.

My code is as followings.

 

Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
var objectIds = new ObjectIdCollection();
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
var modelspace = (BlockTableRecord)acTrans.GetObject(
acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForRead);
foreach (ObjectId id in modelspace)
{
objectIds.Add(id);
Entity ent = (Entity)acTrans.GetObject(id, OpenMode.ForRead);
string lyrnm = ent.Layer.ToString();
string newlyrNm = convertedLyrNm(lyrnm);
Entity entcpy = ent.Clone() as Entity;
entcpy.Layer = newlyrNm;
acBlkTblRec.AppendEntity(entcpy);
acTrans.AddNewlyCreatedDBObject(entcpy, true);
}
acTrans.Commit();
}

0 Likes
Message 9 of 22

MarkJamesRogolino
Advocate
Advocate

I have to paste copied object to same position with source object. Thanks.

0 Likes
Message 10 of 22

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

I don't see the code of "convertedLyrNm" used in this line:

string newlyrNm = convertedLyrNm(lyrnm);

 

For the crash ... please show the line where you code crashes during debug.

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 11 of 22

_gile
Consultant
Consultant
Accepted solution

Hi,

The RXObject.Clone() method does not deep clone the entity (i.e. does not copy the entity xdata and extension dictionary), you should use DeepCloneObjects instead.

 

Assuming convertedLayerNames is a Dictionary<string, string> where each pair key is the old layer name and each pair value is the corresponding new layer name, assuming these new layers already exist in the drawing LayerTable, you can do something laike this:

 

        public void CopyModelSpaceEntitiesToNewLayer()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            using (var tr = db.TransactionManager.StartTransaction())
            {
                // Deep clone all the entities in model space
                var modelSpaceId = SymbolUtilityServices.GetBlockModelSpaceId(db);
                var modelSpace = (BlockTableRecord)tr.GetObject(modelSpaceId, OpenMode.ForWrite);
                var ids = new ObjectIdCollection();
                foreach (ObjectId id in modelSpace)
                {
                    ids.Add(id);
                }
                var mapping = new IdMapping();
                db.DeepCloneObjects(ids, modelSpaceId, mapping, false);

                // change the layer of cloned entities
                foreach (IdPair pair in mapping)
                {
                    if (pair.IsCloned && pair.IsPrimary)
                    {
                        var entity = (Entity)tr.GetObject(pair.Value, OpenMode.ForWrite);
                        entity.Layer = convertedLayerNames[entity.Layer];
                    }
                }
                tr.Commit();
            }
        }

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 12 of 22

MarkJamesRogolino
Advocate
Advocate

goldhorsemillion_0-1660698494300.png

I think acTrans.Commit(); occurs an error.

I will expect good result.

0 Likes
Message 13 of 22

MarkJamesRogolino
Advocate
Advocate

convertedLyrNm() function is only for getting layername to be copied.

I think acTrans.Commit(); occurs an error.

I will expect good result.

0 Likes
Message 14 of 22

MarkJamesRogolino
Advocate
Advocate
Thanks for your great help.
There was no error, but some entities are not pasted.
Anyway this will be allowable. Thanks again.
0 Likes
Message 15 of 22

MarkJamesRogolino
Advocate
Advocate
PS. This function successfully copied about a little number of entities that I draw.
but many entities in opened document are not copied.
0 Likes
Message 16 of 22

_gile
Consultant
Consultant

Take care with copy/paste of code snippets.

By writing all the code yourself, you would certainly avoid this type of mistake:

BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
var modelspace = (BlockTableRecord)acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForRead);

You open the model space table record twice, one time for write, the other time for read...



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 17 of 22

_gile
Consultant
Consultant

@MarkJamesRogolino  a écrit :
PS. This function successfully copied about a little number of entities that I draw.
but many entities in opened document are not copied.

Can you elaborate about which kind of entities and/or attach a drawing sample containing such entities.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 18 of 22

MarkJamesRogolino
Advocate
Advocate

I can not copy areatable layer with your code.

0 Likes
Message 19 of 22

_gile
Consultant
Consultant

I cannot reproduce what you describe.

From the tests I did, all the entities on the layer "_AREATABLE" are also copied.

Try with this new code which moves the cloned entities of 700 units aong X axis.

        public static void CopyModelSpaceEntitiesToNewLayer()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            using (var tr = db.TransactionManager.StartTransaction())
            {
                // Deep clone all model space entities
                var modelSpaceId = SymbolUtilityServices.GetBlockModelSpaceId(db);
                var modelSpace = (BlockTableRecord)tr.GetObject(modelSpaceId, OpenMode.ForWrite);
                var ids = new ObjectIdCollection();
                foreach (ObjectId id in modelSpace)
                {
                    ids.Add(id);
                }
                var mapping = new IdMapping();
                db.DeepCloneObjects(ids, modelSpaceId, mapping, false);

                // change the layer of cloned entities
                foreach (IdPair pair in mapping)
                {
                    if (pair.IsCloned && pair.IsPrimary)
                    {
                        var entity = (Entity)tr.GetObject(pair.Value, OpenMode.ForWrite);
                        entity.Layer = convertedLayerNames[entity.Layer];

                        // move the cloned entity of 700 units along X axis
                        entity.TransformBy(Matrix3d.Displacement(Vector3d.XAxis * 700.0));
                    }
                }
                tr.Commit();
            }
        }

 

PS: Opening your file with the RECOVER command finds 3 errors.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 20 of 22

MarkJamesRogolino
Advocate
Advocate

Thanks, gile.

Followings are my code.

public void CopyModelSpaceEntitiesToNewLayer()
{
var doc = Application.DocumentManager.MdiActiveDocument;
var db = doc.Database;
using (var tr = db.TransactionManager.StartTransaction())
{
// Deep clone all the entities in model space
var modelSpaceId = SymbolUtilityServices.GetBlockModelSpaceId(db);
var modelSpace = (BlockTableRecord)tr.GetObject(modelSpaceId, OpenMode.ForWrite);
var ids = new ObjectIdCollection();
foreach (ObjectId id in modelSpace)
{
ids.Add(id);
}
var mapping = new IdMapping();
db.DeepCloneObjects(ids, modelSpaceId, mapping, false);

// change the layer of cloned entities
foreach (IdPair pair in mapping)
{
if (pair.IsCloned && pair.IsPrimary)
{
var entity = (Entity)tr.GetObject(pair.Value, OpenMode.ForWrite);
entity.Layer = convertedLyrNm(entity.Layer);
}
}
tr.Commit();
}
}

Here I can not copy _AreaTable layer.

 

0 Likes