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?
Solved! Go to Solution.
Solved by sudokuxls. Go to Solution.
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.
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...
@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.
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();
}
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;
}
}
}
}
}
}
}
Can't find what you're looking for? Ask the community or share your knowledge.