Copy layer from another DWG in c#

Copy layer from another DWG in c#

alexisgacia
Advocate Advocate
1,957 Views
4 Replies
Message 1 of 5

Copy layer from another DWG in c#

alexisgacia
Advocate
Advocate

Hello,

 

Good day. I'm searching a code to copy a layer from dwg to current. I did some testing code from the net and I was not able to figure it out the right solution.

 

Please share working code on copying a layer either vb or C#

 

Thank you very much in advance

 

 

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

kerry_w_brown
Advisor
Advisor

The code will vary depending if the source drawing is open in AutoCAD or not.
Will the user have a choice of the source drawing ? or will the source be constant ?

 

Judicious use of Google should provide several possible solutions.


// Called Kerry or kdub in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect. ~ kdub
Sometimes the question is more important than the answer. ~ kdub

NZST UTC+12 : class keyThumper<T> : Lazy<T>;      another  Swamper
0 Likes
Message 3 of 5

alexisgacia
Advocate
Advocate

The path is a constant. 

 

The function looks like below, but I wasn't able to figure out to properly copy the layer to the current drawing:

 

 

public boolean GetLayerFromLib(LayerName as string) 
{
string dwgPath = @"c:\test.dwg";

'open dwgPath database
'read the LayerTable
'Check if has the LayerName
'if exist copy to current database and return true
'else return false

}

 

0 Likes
Message 4 of 5

_gile
Consultant
Consultant
Accepted solution

Here's an example:

        public bool GetLayerFromLib(string layerName)
        {
            var curDb = HostApplicationServices.WorkingDatabase;
            string dwgPath = @"c:\test.dwg";
            using (var db = new Database(false, true))
            {
                db.ReadDwgFile(dwgPath, FileOpenMode.OpenForReadAndAllShare, true, null);
                using (var tr = db.TransactionManager.StartOpenCloseTransaction())
                {
                    var layerTable = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead);
                    if (!layerTable.Has(layerName))
                        return false;
                    var ids = new ObjectIdCollection();
                    ids.Add(layerTable[layerName]);
                    var mapping = new IdMapping();
                    db.WblockCloneObjects(ids, curDb.LayerTableId, mapping, DuplicateRecordCloning.Replace, false);
                    return true;
                }
            }
        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 5 of 5

alexisgacia
Advocate
Advocate
It works!
Thank you very much.
0 Likes