.NET

Reply
Valued Contributor
kevinsir
Posts: 78
Registered: ‎08-20-2012
Message 1 of 7 (277 Views)
Accepted Solution

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

277 Views, 6 Replies
12-31-2012 09:34 PM

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

*Expert Elite*
_gile
Posts: 2,077
Registered: ‎04-29-2006
Message 2 of 7 (254 Views)

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

01-01-2013 04:20 AM 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
Contributor
autodeskprogrammer
Posts: 21
Registered: ‎08-20-2008
Message 3 of 7 (250 Views)

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

01-01-2013 05:02 AM in reply to: kevinsir

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 ?

 

*Expert Elite*
_gile
Posts: 2,077
Registered: ‎04-29-2006
Message 4 of 7 (233 Views)

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

01-01-2013 10:23 AM 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
Valued Contributor
kevinsir
Posts: 78
Registered: ‎08-20-2012
Message 5 of 7 (164 Views)

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

01-09-2013 05:28 PM 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!

Valued Contributor
kevinsir
Posts: 78
Registered: ‎08-20-2012
Message 6 of 7 (158 Views)

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

01-09-2013 06:10 PM in reply to: autodeskprogrammer

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

Valued Contributor
kevinsir
Posts: 78
Registered: ‎08-20-2012
Message 7 of 7 (151 Views)

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

01-09-2013 07:00 PM 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!

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community