How can I use .net api to realize the function as CAD ‘laytrans ’ command。I want to use it by code!
Solved! Go to Solution.
Solved by _gile. Go to Solution.
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.
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 ?
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(); } }
Thanks for all! I am sorry that CAD forum does not send me any Email when you replied! I will try all you suggestion!
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
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.