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

Get layers from current file only (not referenced files)

6 REPLIES 6
SOLVED
Reply
Message 1 of 7
ottosson_mathias
1493 Views, 6 Replies

Get layers from current file only (not referenced files)

I'm trying to get all layers from a file. But I get all layers from the referenced files as well, and I don't know how to distinguish them from one and other.

 

using (Transaction transaction = db.TransactionManager.StartTransaction())
{
    var layerTable = transaction.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;

    foreach (var layer in layerTable)
    {
        var currentLayer = layer.GetObject(OpenMode.ForRead) as LayerTableRecord;

        // Check if layer is from current file or referenced file...
    }

    transaction.Commit();
}

 

How can I know if a layer belongs to an external reference and not the opened file?

6 REPLIES 6
Message 2 of 7
_Tharwat
in reply to: ottosson_mathias

Layer names that belong to external files contain the pipe symbol ( | ) within every layer name so you need to check if the layer name has not that symbol.

Message 3 of 7
ottosson_mathias
in reply to: _Tharwat

I was thinking about that as well, and now I double checked and it's not possible to name a layer using |.

So if there is no other way I guess that could work. Even though it feels a bit inefficient to have to check the names.

 

I also tried to detach all xrefs, which worked, and that felt a bit more efficient, but hacky...

Message 4 of 7
_Tharwat
in reply to: ottosson_mathias


@ottosson_mathias wrote:

I guess that could work. Even though it feels a bit inefficient to have to check the names.

 


How come it is inefficient to check each name of the layer from the Layer Table ?

I used to have tens of operations in a program then the codes running as fast as a blink of an eye without any delay.

Message 5 of 7

You can use IsDependent property.

Quote from arx documentation:

"Returns true when the SymbolTableRecord is a dependent (that is, part of) of an attached xref drawing; otherwise false is returned."

 

 

 using (Transaction transaction = db.TransactionManager.StartTransaction())
 {
           var layerTable = transaction.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;

           foreach (var layer in layerTable)
           {
               var currentLayer = layer.GetObject(OpenMode.ForRead) as LayerTableRecord;

               if (currentLayer.IsDependent)
               {
                   //is part of a xref drawing
               }
               else
               {
                   //not part of a xref drawing
               }
           }

           transaction.Commit();
}

 

 

 

Message 6 of 7
hosneyalaa
in reply to: sudokuxls

namespace Layering
{
    public class LayersGroup
    {
        short colorindex = 1;
        [CommandMethod("GL", CommandFlags.UsePickSet)]
        public void GroupLayers()
        {
            Dictionary<string, List<Entity>> entitiesList = new Dictionary<string, List<Entity>>();
            Database db = Application.DocumentManager.MdiActiveDocument.Database;
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            StringBuilder sb = new StringBuilder("List of created layers:"+System.Environment.NewLine);
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                PromptSelectionResult sPrompt = ed.SelectImplied();
                if (sPrompt.Status != PromptStatus.OK)
                {
                    sPrompt = ed.GetSelection();
                }

                if (sPrompt.Status == PromptStatus.OK)
                {
                    foreach (SelectedObject item in sPrompt.Value)
                    {
                        if (item != null)
                        {
                            Entity entity = trans.GetObject(item.ObjectId, OpenMode.ForWrite) as Entity;
                            if (entity != null)
                            {
                                string s = entity.GetType().Name;
                                switch (s)
                                {
                                    case "DBText":
                                    case "MText":
                                        s = "Text";
                                        break;
                                    
                                    default:
                                        break;
                                }
                                if (!entitiesList.ContainsKey(s))
                                {
                                    entitiesList.Add(s,new List<Entity>());
                                }
                                entitiesList[s].Add(entity);
                            }
                        }
                    }
                    LayerTable lT = trans.GetObject(db.LayerTableId, OpenMode.ForWrite) as LayerTable;
                    
                    foreach (var item in entitiesList.Keys)
                    {
                        sb.AppendLine(item);
                        if (!lT.Has(item))
                        {
                            LayerTableRecord newLayer = new LayerTableRecord();
                            newLayer.Name = item;
                            newLayer.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByAci, colorindex++);
                            lT.Add(newLayer);
                            trans.AddNewlyCreatedDBObject(newLayer, true);
                        }

                        foreach (var ent in entitiesList[item])
                        {
                            ent.Layer = item;
                        }
                    }
 
                }     
            }
        }
   }
        }
Message 7 of 7

That looks like the approach I was expecting! Thanks!

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

Post to forums  

Forma Design Contest


Autodesk Design & Make Report