using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using GrxCAD.ApplicationServices; using GrxCAD.DatabaseServices; using GrxCAD.EditorInput; using System.Collections; using GrxCAD.Geometry; namespace HCTools { public struct points { public Point3d pt; public ObjectId objID; } class ElePtCheck { public static int blc;//比例尺 public static string EleLayerName;//高程点图层名 public double radius;//半径 //public double length;//搜索半径 int j = 0;//记录错误点个数 //存放所有高程点数组 public List pointarrlist = new List(); //存放有问题的高程点数组 public List errorpointarrlist = new List(); /// /// 检查错误的高程点并画圆标记 /// public void pointcheck() { double flagradius = 4.0 * blc / 1000;//画圆标记半径 if (blc == 500) { radius = 1.5; } else if (blc == 1000) { radius = 2.5; } else if (blc == 2000) { radius = 4; } else if (blc == 5000) { radius = 10.5; } //外切圆半径 //length = radius * Math.Pow(2, 0.5); //检查高程点与注记是否一致或为零 check(); //创建存放错误点的图层 LayerControl layerscontrol = new LayerControl(); string layname = "错误高程点"; if (!layerscontrol.haslayername(layname)) { colorgb col = new colorgb(255, 0, 255); layerscontrol.creatlayer(layname, col); layerscontrol.movelayertofront(layname); } else layerscontrol.movelayertofront(layname); //创建错误点标记的图层 LayerControl layerscontrol1 = new LayerControl(); string layname1 = "错误高程点标记"; if (!layerscontrol1.haslayername(layname1)) { colorgb col = new colorgb(0, 225, 255); layerscontrol1.creatlayer(layname1, col); layerscontrol1.movelayertofront(layname1); } else layerscontrol1.movelayertofront(layname1); Database database = HostApplicationServices.WorkingDatabase; Document doc = Application.DocumentManager.MdiActiveDocument; Editor editor = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; using (Transaction traction = database.TransactionManager.StartTransaction()) { DocumentLock documentlock = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument(); BlockTable blocktable = traction.GetObject(database.BlockTableId,OpenMode.ForWrite) as BlockTable; for (int i = 0; i < errorpointarrlist.Count; i++) { BasicFunction functions = new BasicFunction(); BlockReference blr= traction.GetObject(errorpointarrlist[i].objID,OpenMode.ForWrite)as BlockReference; blr.Layer = "错误高程点"; functions.makeflag(errorpointarrlist[i].pt.X, errorpointarrlist[i].pt.Y, 0, flagradius, "错误高程点标记"); Point3d p1 = new Point3d(errorpointarrlist[i].pt.X - radius, errorpointarrlist[i].pt.Y - radius, 0); Point3d p2 = new Point3d(errorpointarrlist[i].pt.X + radius, errorpointarrlist[i].pt.Y + radius, 0); TypedValue[] typedvalue = new TypedValue[2]; typedvalue.SetValue(new TypedValue((int)DxfCode.Start, "Text"), 0); typedvalue.SetValue(new TypedValue((int)DxfCode.LayerName, EleLayerName), 1); SelectionFilter filter = new SelectionFilter(typedvalue); PromptSelectionResult psr = editor.SelectCrossingWindow(p1, p2, filter); if (psr.Status == PromptStatus.OK) { SelectionSet set = psr.Value; ObjectId[] objs = new ObjectId[set.Count]; objs = set.GetObjectIds(); for (int ii = 0; ii < objs.Length; ii++) { DBText Text = (DBText)traction.GetObject(objs[ii], OpenMode.ForWrite); Text.Layer = "错误高程点"; } } } traction.Commit(); documentlock.Dispose(); traction.Dispose(); } editor.WriteMessage("\n共有" + j + "处错误"); } /// /// 检查高程点与注记是否一致或为零 /// private void check() { string LayerName = EleLayerName; getptInLayer(LayerName); if (pointarrlist == null) return; Editor editor = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; for (int i = 0; i < pointarrlist.Count; i++) { Point3d pt = pointarrlist[i].pt; string getword = GetTxt(pt, LayerName); if (pt.Z == 0) { errorpointarrlist.Add(pointarrlist[i]); j++; } else if (!getword.Contains('.') || getword.Contains(".0")) { errorpointarrlist.Add(pointarrlist[i]); j++; } else { if (pt.Z.ToString("f1") == getword && pt.Z % (blc / 1000) != 0) { continue; } else { errorpointarrlist.Add(pointarrlist[i]); j++; } } } } /// /// 获取图层上高程点坐标 /// private void getptInLayer(string LayerName) { Document document = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; Database db = HostApplicationServices.WorkingDatabase; Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; TypedValue[] value = new TypedValue[] { new TypedValue((int)DxfCode.LayerName,LayerName), new TypedValue((int)DxfCode.Start,"Insert") };//设置筛选条件 SelectionFilter filter = new SelectionFilter(value); //选择区域中全部对象 //PromptSelectionResult psr = ed.SelectAll(filter); // 要求在图形区域中手动选择对象 PromptSelectionResult psr = ed.GetSelection(filter); if (psr.Status == PromptStatus.OK) { using (Transaction transaction = db.TransactionManager.StartTransaction()) { SelectionSet ss = psr.Value; ObjectId[] ids = ss.GetObjectIds(); points ptlist = new points(); foreach (ObjectId etId in ids) { BlockReference blr = transaction.GetObject(etId, OpenMode.ForRead) as BlockReference; ptlist.pt = blr.Position; ptlist.objID = blr.Id; pointarrlist.Add(ptlist); } transaction.Commit(); } } } /// /// 筛选坐标点附近文字信息 /// private static String GetTxt(Point3d pt, string LayerName) { ObjectId[] objss = null;//文本对象的ID数组 Editor ed = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; TypedValue[] val = new TypedValue[] { new TypedValue(0, "TEXT"), new TypedValue((int)DxfCode.LayerName,LayerName) };//需要筛选的类型值 SelectionFilter fil = new SelectionFilter(val);//选择过滤器 PromptSelectionResult pro = ed.SelectCrossingWindow(new Point3d(pt.X - 4.0 * blc / 2000, pt.Y - 4.0 * blc / 2000, 0), new Point3d(pt.X + 4.0 * blc / 2000, pt.Y + 4.0 * blc / 2000, 0), fil); //PromptSelectionResult pro = ed.SelectAll(fil);//获取结果 SelectionSet ss = pro.Value;//赋值给选择集 String txt = null; if (ss != null) { objss = ss.GetObjectIds();//将选择集内对象ID赋值给ID数组 if (objss.Length > 1) { for (int i = 0; i < objss.Length; i++) { DBText buftext = GetDBObject(objss[i]) as DBText; if (pt.Z.ToString("f1") == buftext.TextString) txt = buftext.TextString; } } else { DBText buftext = GetDBObject(objss[0]) as DBText; txt = buftext.TextString; } } //for (int i = 0; i < objss.Length; i++) //{ // DBText buftext = GetDBObject(objss[i]) as DBText;//遍历数组ID将对象赋值给单行文本 // Point3d po = buftext.Position;//获取单行文本的坐标点 // if (Math.Sqrt((po.X - x) * (po.X - x) + (po.Y - y) * (po.Y - y)) < 10) // { // txt = buftext.TextString;//筛选出的单行文本对象 // } // else // continue; //} return txt; } private static Entity GetDBObject(ObjectId i) { Document doc = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Entity ent = null; using (Transaction tr = db.TransactionManager.StartTransaction()) { ent = (Entity)tr.GetObject(i, OpenMode.ForRead, true); tr.Commit(); } return ent; } } }