|
public class Class1
{
[CommandMethod("GetLayerPro")]
public static void GetLayerPro()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
//新建一个数据库对象以读取Dwg文件
Database db = new Database(false, true);
string fileName = "C:/Users/admin/Desktop/1.dwg";
//如果指定文件名的文件存在
if (System.IO.File.Exists(fileName))
{
//把文件读入到数据库中
db.ReadDwgFile(fileName, System.IO.FileShare.Read, true, null);
using (Transaction trans = db.TransactionManager.StartTransaction())
{
//获取数据库的图层表对象
LayerTable lt = (LayerTable)trans.GetObject(db.LayerTableId, OpenMode.ForRead);
//循环遍历每个图层
foreach (ObjectId layerId in lt)
{
LayerTableRecord ltr = (LayerTableRecord)trans.GetObject(layerId, OpenMode.ForRead);
if (ltr != null)
{
List entitys = GetLayerEntity(ltr.Name);
foreach (ObjectId id in entitys)
{
Entity hatchobj = trans.GetObject(id, OpenMode.ForWrite) as Entity;
if (hatchobj != null)
{
Debug.WriteLine("===============");
TypedValue[] typedValue = hatchobj.XData.AsArray();
if (typedValue[0].Value.Equals("PIPELINE") || typedValue[0].Value.Equals("PIPE"))
{
foreach (TypedValue obj in hatchobj.XData)
{
Debug.WriteLine(obj.Value);
}
}
}
}
}
Autodesk..Colors.Color layerColor = ltr.Color;
ed.WriteMessage("\n图层名称为:" + ltr.Name);
ed.WriteMessage("\n图层颜色为:" + layerColor.ToString());
}
trans.Commit();
}
}
}
//根据图层名获取该图层下的实体id
public static List GetLayerEntity(String layerName)
{
List entitys = new List();
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
TypedValue[] values = { new TypedValue((int)DxfCode.LayerName, layerName) };
SelectionFilter filters = new SelectionFilter(values);
PromptSelectionResult result = ed.SelectAll(filters );
PromptSelectionResult result = ed.SelectAll();
if (result.Status == PromptStatus.OK)
{
ObjectId[] ids = result.Value.GetObjectIds();
foreach (ObjectId id in ids)
{
entitys.Add(id);
}
}
else
{
return entitys;
}
return entitys;
}
}
}
红色部分为什么获取的都是error , 求大神来解释一下 |
|