123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- 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<ObjectId> objid_all = new List<ObjectId>();
- public void attribt()
- {
- //存放所有图层名
- List<string> alllayername = new List<string>();
- List<List<string>> info_all = new List<List<string>>();
-
- 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<List<string>> 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<string> temp = new List<string>();
- 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<List<string>> Getlyobjinfo(string layername, int num)
- {
- List<List<string>> info = new List<List<string>>();//图层中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<string> temp = new List<string>();
- 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<string> temp = new List<string>();
- 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<string> temp = new List<string>();
- 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<string> temp = new List<string>();
- 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<string> temp = new List<string>();
- 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<string> temp = new List<string>();
- 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;
- }
- }
- }
|