using GrxCAD.ApplicationServices; using GrxCAD.DatabaseServices; using GrxCAD.EditorInput; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace HCTools { class Attributeget { private static List objid_all = new List(); public void attribt() { //存放所有图层名 List alllayername = new List(); List> info_all = new List>(); Database db = HostApplicationServices.WorkingDatabase; Editor ed = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; using (Transaction trans = db.TransactionManager.StartTransaction()) { LayerTable layertable = trans.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable; foreach (ObjectId objid in layertable) { LayerTableRecord layertablerecord = trans.GetObject(objid, OpenMode.ForRead) as LayerTableRecord; alllayername.Add(layertablerecord.Name); } alllayername.Sort(); trans.Commit(); } int num = 0; for (int i = 0; i < alllayername.Count; i++) { List> info = Getlyobjinfo(alllayername[i], num); num = num + info.Count; for (int j = 0; j < info.Count; j++) { string id = info[j][0]; string name = info[j][1]; string type = info[j][2]; string X = info[j][3]; string Y = info[j][4]; string Z = info[j][5]; string lenth = info[j][6]; string area = info[j][7]; List temp = new List(); temp.Add(id); temp.Add(name); temp.Add(alllayername[i]); temp.Add(type); temp.Add(X); temp.Add(Y); temp.Add(Z); temp.Add(lenth); temp.Add(area); info_all.Add(temp); } } AttributeForm Frm = new AttributeForm(); Frm.GetInfo(info_all,objid_all); Frm.Show(); } private static List> Getlyobjinfo(string layername, int num) { List> info = new List>();//图层中obj信息 Database database = HostApplicationServices.WorkingDatabase; using (Transaction traction = database.TransactionManager.StartTransaction()) { // 获得当前文档的编辑器 Editor editor = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; // 创建一个 TypedValue 数组,用于定义过滤条件 TypedValue[] typedvalue = new TypedValue[1]; typedvalue.SetValue(new TypedValue((int)DxfCode.LayerName, layername), 0); // 赋值过滤条件给 SelectionFilter 对象 SelectionFilter selectionfilter = new SelectionFilter(typedvalue); // 要求在图形区域中手动选择对象 PromptSelectionResult psr = editor.SelectAll(selectionfilter); if (psr.Status == PromptStatus.OK) { SelectionSet selectionset = psr.Value; ObjectId[] obj = new ObjectId[selectionset.Count]; obj = selectionset.GetObjectIds(); for (int i = 0; i < obj.Length; i++) { //获取实体 ObjectId objid = obj[i]; Entity entity = (Entity)traction.GetObject(objid, OpenMode.ForRead); objid_all.Add(objid); if (entity is Polyline) { num++; Polyline pll = (Polyline)entity; string name = "无"; string type = "多段线"; double X = pll.StartPoint.X; double Y = pll.StartPoint.Y; double Z = pll.Elevation; double length = pll.Length; string area; if(pll.Closed) { area = pll.Area.ToString("0.000"); } else { area = "无"; } List temp = new List(); temp.Add(num.ToString()); temp.Add(name); temp.Add(type); temp.Add(X.ToString("0.000")); temp.Add(Y.ToString("0.000")); temp.Add(Z.ToString("0.000")); temp.Add(length.ToString("0.000")); temp.Add(area); info.Add(temp); } if (entity is Polyline2d) { num++; Polyline2d pll = (Polyline2d)entity; string name = "无"; string type = "二维多段线"; double X = pll.StartPoint.X; double Y = pll.StartPoint.Y; double Z = pll.Elevation; double length = pll.Length; string area; if (pll.Closed) { area = pll.Area.ToString("0.000"); } else { area = "无"; } List temp = new List(); temp.Add(num.ToString()); temp.Add(name); temp.Add(type); temp.Add(X.ToString("0.000")); temp.Add(Y.ToString("0.000")); temp.Add(Z.ToString("0.000")); temp.Add(length.ToString("0.000")); temp.Add(area); info.Add(temp); } else if (entity is Line) { num++; Line line = (Line)entity; string name = "无"; string type = "直线"; double X = line.StartPoint.X; double Y = line.StartPoint.Y; double Z = line.StartPoint.Z; double length = line.Length; string area; if (line.Closed) { area = line.Area.ToString("0.000"); } else { area = "无"; } List temp = new List(); temp.Add(num.ToString()); temp.Add(name); temp.Add(type); temp.Add(X.ToString("0.000")); temp.Add(Y.ToString("0.000")); temp.Add(Z.ToString("0.000")); temp.Add(length.ToString("0.000")); temp.Add(area); info.Add(temp); } else if (entity is DBText) { num++; DBText txt = (DBText)entity; string name = txt.TextString; string type = "文字"; double X = txt.Position.X; double Y = txt.Position.Y; double Z = txt.Position.Z; string length = "无"; string area = "无"; List temp = new List(); temp.Add(num.ToString()); temp.Add(name); temp.Add(type); temp.Add(X.ToString("0.000")); temp.Add(Y.ToString("0.000")); temp.Add(Z.ToString("0.000")); temp.Add(length); temp.Add(area); info.Add(temp); } else if (entity is BlockReference) { num++; BlockReference blk = (BlockReference)entity; string name = blk.Name; string type = "块参照"; double X = blk.Position.X; double Y = blk.Position.Y; double Z = blk.Position.Z; string length = "无"; string area = "无"; List temp = new List(); temp.Add(num.ToString()); temp.Add(name); temp.Add(type); temp.Add(X.ToString("0.000")); temp.Add(Y.ToString("0.000")); temp.Add(Z.ToString("0.000")); temp.Add(length); temp.Add(area); info.Add(temp); } else if(entity is Hatch) { num++; Hatch hc = (Hatch)entity; string name = hc.PatternName; string type = "图案填充"; double X = hc.Origin.X; double Y = hc.Origin.Y; double Z = 0; string length = "无"; string area = hc.Area.ToString(); List temp = new List(); temp.Add(num.ToString()); temp.Add(name); temp.Add(type); temp.Add(X.ToString("0.000")); temp.Add(Y.ToString("0.000")); temp.Add(Z.ToString("0.000")); temp.Add(length); temp.Add(area); info.Add(temp); } } } traction.Commit(); } return info; } } }