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

How can I use .net api to realize the function as CAD ‘laytrans ’ command

6 REPLIES 6
SOLVED
Reply
Message 1 of 7
kevinsir
617 Views, 6 Replies

How can I use .net api to realize the function as CAD ‘laytrans ’ command

How can I use .net api to  realize the  function as CAD ‘laytrans ’  command。I want to use it by code!

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

Hi,

 

It depends on how you get the new layers and if new layers have the same or different names from the old ones.

You can use WBlockCloneObjects() method to import layers from a file (dwg or dwt) or create them 'on the fly'.

If you import layers which have the same names as the old ones, using DuplicateRecordCloning.Replace with WBlockCloneObjects() should do the trick.

If you create layers 'on the fly', for layer names which already exist in the drawing, just change the layers properties.

 

In both cases, if layer names are different, you can iterate the whole database to change entities layer from old ones to new ones and then delete the old ones.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 7

You mean that you want to add a new Command to Autocad but you want want that it functions like one of Autocad's Command and in addition you want to produce this code with .net ?

 

Message 4 of 7
_gile
in reply to: kevinsir

Hi,

 

Here's a little snippet importing layers from a dwg or dwt file. The method arguments are: a dictionary containing pairs of type: old_layer_name, new_layer_name and the source file name.

 

        private void LayTrans(Dictionary<string, string> layerNames, string fileName)
        {
            Document doc = AcAp.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                LayerTable lt = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead);
                List<string> toRemove = new List<string>();
                foreach (KeyValuePair<string, string> pair in layerNames)
                {
                    if (!lt.Has(pair.Key))
                    {
                        ed.WriteMessage("\nTarget drawing does not contain '{0}'", pair.Key);
                        toRemove.Add(pair.Key);
                    }
                }
                foreach (string layer in toRemove)
                {
                    layerNames.Remove(layer);
                }
                List<LayerTableRecord> oldLayers = new List<LayerTableRecord>();
                using (Database sourceDb = new Database())
                {
                    sourceDb.ReadDwgFile(fileName, System.IO.FileShare.Read, false, "");
                    using (Transaction trx = sourceDb.TransactionManager.StartTransaction())
                    {
                        LayerTable sourceTable = (LayerTable)trx.GetObject(sourceDb.LayerTableId, OpenMode.ForRead);
                        ObjectIdCollection idCol = new ObjectIdCollection();
                        foreach (KeyValuePair<string, string> pair in layerNames)
                        {
                            if (sourceTable.Has(pair.Value))
                            {
                                idCol.Add(sourceTable[pair.Value]);
                                LayerTableRecord ltr = (LayerTableRecord)tr.GetObject(lt[pair.Key], OpenMode.ForWrite);
                                oldLayers.Add(ltr);
                            }
                            else
                            {
                                ed.WriteMessage("\nSource drawing does not contain '{0}'", pair.Value);
                            }
                        }
                        foreach (LayerTableRecord ltr in oldLayers)
                        {
                            ltr.Name = "Old_" + ltr.Name;
                        }
                        IdMapping idMap = new IdMapping();
                        sourceDb.WblockCloneObjects(
                            idCol, db.LayerTableId, idMap, DuplicateRecordCloning.Ignore, false);
                        trx.Commit();
                    }
                }
                BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                foreach (ObjectId btrId in bt)
                {
                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(btrId, OpenMode.ForRead);
                    foreach (ObjectId id in btr)
                    {
                        Entity ent = (Entity)tr.GetObject(id, OpenMode.ForRead);
                        if (ent.Layer.StartsWith("Old_"))
                        {
                            string name = ent.Layer.Substring(4);
                            ent.UpgradeOpen();
                            ent.Layer = layerNames[name];
                        }
                    }
                }
                foreach (LayerTableRecord ltr in oldLayers)
                {
                    ltr.Erase();
                }
                tr.Commit();
            }
        }

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 5 of 7
kevinsir
in reply to: kevinsir

Thanks for all! I am sorry that CAD forum does not  send me any Email when you replied! I will try all you suggestion!

Message 6 of 7

I just want to  do the  function follows

1、select one dwg file

2、import to the seleced dwg file to the open dwg 

3、 create one new layer on the open dwg 

4、 move all seleced dwg file entities   to the new layer

Message 7 of 7
kevinsir
in reply to: kevinsir

I used "WBlockCloneObjects“  ,however, it take all  information of  layers to the open dwg file.Then I want to use CAD ‘laytrans ’  command to delete these information of  layers before use "WBlockCloneObjects",but where is the function?

Can anyone help me ? Thanks very much!

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

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost