How do I copy a layer with C#?

How do I copy a layer with C#?

sonny3g
Collaborator Collaborator
2,084 Views
3 Replies
Message 1 of 4

How do I copy a layer with C#?

sonny3g
Collaborator
Collaborator

I need to "clone" or copy a layer in a drawing and then rename it use C#.  What I am starting with is the layer name of a MLeader object that I need to create a new MLeader object on a different layer with the same properties as the layer the original MLeader object is located on. 

 

Right now, I have the layer name of the existing MLeader and have created the new layer name.  I am able to change to the new layer if it exists, but if it does not, I have to create the new layer and then change to it before creating the new MLeader object.

 

I am struggling with retrieving the layer color from the original MLeader object layer.  The MLeader color is by layer, so I have to get the color from the layer itself.

 

Code so far:

 

[CommandMethod("SetUnitLayerSibling")]

private static void SetUnitLayerSibling(Database acCurDb, Document acDoc, string ulLayNam)

 

{

using (Transaction lTrans = acCurDb.TransactionManager.StartTransaction())

 

{

// Getting and setting MLeader layer information

 

String MtrLayName;

String tag;

MtrLayName = ulLayNam.Replace("_UNIT", "_MTR");

 

LayerTable lTbl;

lTbl = lTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForRead) as LayerTable;

if (lTbl.Has(MtrLayName) == true)

 

{

// Set the layer

 

acCurDb.Clayer = lTbl[MtrLayName];

}

else

 

{

// Get unit layer parameters

 

LayerTableRecord ulRec;

// Clone unit layer and re-name

LayerTableRecord lTblRec = new LayerTableRecord();

 

lTblRec.Color =

}

lTrans.Commit();

}

 

}

 

 

Thanks in advance!

 

 

Scott G. Sawdy
scott.sawdy@bluecoyotecad.com
0 Likes
Accepted solutions (1)
2,085 Views
3 Replies
Replies (3)
Message 2 of 4

Juergen_Becker
Advocate
Advocate
Accepted solution

Hi,

 

give this a try.

The LayerTableRecord has a Clone Methode which you can use.

 

LayerTableRecord m_LayerTableRecord = new LayerTableRecord();
LayerTableRecord m_LayerTableRecordCloned = null;
m_LayerTableRecordCloned = (LayerTableRecord)m_LayerTableRecord.Clone();

Regards Jürgen

I hope my tip helps. If so then give me kudos and mark the tip as a solution.
Thanks.

Jürgen A. Becker
Building Services

Development and Support
Autodesk Forge Spezialist


CAD-Becker.de
https://www.CAD-Becker.de

Message 3 of 4

_Tharwat
Advisor
Advisor

Hi,

 

Manual method here. Smiley Happy

 

        public static void CloneLayerName ()
        {
            Document doc = App.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            String Old = "OldLayer";
            String New = "NewLayer";

            using (Transaction lTrans = db.TransactionManager.StartTransaction())
            {
                LayerTable lTbl = (LayerTable)lTrans.GetObject(db.LayerTableId, OpenMode.ForWrite);
                if (lTbl.Has(Old) && !lTbl.Has(New))
                {
                    using (LayerTableRecord lay = new LayerTableRecord())
                    {
                        LayerTableRecord Oldlayer = (LayerTableRecord)lTbl[Old].GetObject(OpenMode.ForRead);
                        lay.Name = New;
                        lay.Color = Oldlayer.Color;
                        lay.LineWeight = Oldlayer.LineWeight;
                        lay.LinetypeObjectId = Oldlayer.LinetypeObjectId;
                        lay.Description = Oldlayer.Description;
                        lTbl.Add(lay);
                    }   
                }
                lTrans.Commit();               
            }
           
        }
0 Likes
Message 4 of 4

sonny3g
Collaborator
Collaborator

Thanks.  These posts got me to where I needed to go.  Here is my code snippet that is doing exactly what I needed done.

 

using (Transaction lTrans = acCurDb.TransactionManager.StartTransaction())

 

{

string MtrLayNam = ulLayNam.Replace("_UNIT", "_MTR");

 

LayerTable lTbl;

lTbl = lTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForRead) as LayerTable;

//SetUnitLayerSibling(acCurDb, acDoc, ulLayNam);

if (lTbl.Has(MtrLayNam) == false)

 

{

LayerTableRecord lRec;

lTbl.UpgradeOpen();

lRec = lTrans.GetObject(lTbl[ulLayNam], OpenMode.ForRead).Clone() as LayerTableRecord;

 

lRec.Name = MtrLayNam;

lTbl.Add(lRec);

lTrans.AddNewlyCreatedDBObject(lRec, true);

 

lTrans.Commit();

}

acCurDb.Clayer = lTbl[MtrLayNam];

//lTrans.Commit();

 

}

 

 

Thanks again!!

Scott G. Sawdy
scott.sawdy@bluecoyotecad.com
0 Likes