using GrxCAD.ApplicationServices; using GrxCAD.DatabaseServices; using GrxCAD.EditorInput; using GrxCAD.Geometry; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace HCTools { class EleptDenck { public static string ptlayer; public static string tklayer; public static double density; public static int flag; public void Ptdensity() { ObjectId[] ids = TKget(); Database db = HostApplicationServices.WorkingDatabase; DocumentLock doclock = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument(); Editor editor = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; TypedValue[] typedvalue = new TypedValue[2]; typedvalue.SetValue(new TypedValue((int)DxfCode.LayerName, ptlayer), 0); typedvalue.SetValue(new TypedValue((int)DxfCode.Start, "Insert"), 1); SelectionFilter selectionfilter = new SelectionFilter(typedvalue); for (int i = 0; i < ids.Length; i++) { Entity current_entity = BasicFunction.GetDBObject(ids[i]); ZoomToExtent(current_entity.GeometricExtents); using (Transaction trans = db.TransactionManager.StartTransaction()) { Polyline pll = (Polyline)trans.GetObject(ids[i], OpenMode.ForWrite); Point3dCollection ptcoll = new Point3dCollection(); for (int j = 0; j < pll.NumberOfVertices; j++) { ptcoll.Add(pll.GetPoint3dAt(j)); } PromptSelectionResult psr = editor.SelectCrossingPolygon(ptcoll,selectionfilter); if (psr.Status == PromptStatus.OK) { SelectionSet selectionset = psr.Value; int ptnums = selectionset.Count; double den = ptnums / pll.Area; if (den < density && flag == 1) { LayerControl layerscontrol = new LayerControl(); string layname = "小于密度"; if (!layerscontrol.haslayername(layname)) { colorgb col = new colorgb(255, 0, 0); layerscontrol.creatlayer(layname, col); pll.Layer = "小于密度"; } else pll.Layer = "小于密度"; //pll.Color = GrxCAD.Colors.Color.FromColorIndex(GrxCAD.Colors.ColorMethod.ByColor, 1); } else if (den > density && flag == 2) { LayerControl layerscontrol = new LayerControl(); string layname = "大于密度"; if (!layerscontrol.haslayername(layname)) { colorgb col = new colorgb(255, 0, 0); layerscontrol.creatlayer(layname, col); pll.Layer = "大于密度"; } else pll.Layer = "大于密度"; //pll.Color = GrxCAD.Colors.Color.FromColorIndex(GrxCAD.Colors.ColorMethod.ByColor, 1); } else if (flag == 3) { if (den < density || den > density) { LayerControl layerscontrol = new LayerControl(); string layname = "不等于密度"; if (!layerscontrol.haslayername(layname)) { colorgb col = new colorgb(255, 0, 0); layerscontrol.creatlayer(layname, col); pll.Layer = "不等于密度"; } else pll.Layer = "不等于密度"; //pll.Color = GrxCAD.Colors.Color.FromColorIndex(GrxCAD.Colors.ColorMethod.ByColor, 1); } } } trans.Commit(); } } doclock.Dispose(); } private static ObjectId[] TKget() { Editor editor = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; TypedValue[] typedvalue = new TypedValue[1]; typedvalue.SetValue(new TypedValue((int)DxfCode.LayerName, tklayer), 0); SelectionFilter selectionfilter = new SelectionFilter(typedvalue); PromptSelectionResult psr = editor.SelectAll(selectionfilter); SelectionSet selectionset = psr.Value; ObjectId[] obj = new ObjectId[selectionset.Count]; if (psr.Status == PromptStatus.OK) { obj = selectionset.GetObjectIds(); } return obj; } public static void ZoomToExtent(Extents3d extent) { try { Point3d pMin = extent.MinPoint; Point3d pMax = extent.MaxPoint; //获取当前文档及数据库 Document acDoc = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; Database acCurDb = acDoc.Database; // 启动事务 using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) { using (ViewTableRecord acView = acDoc.Editor.GetCurrentView()) { //设置视图的高 acView.Height = Math.Abs(pMin.Y - pMax.Y); //设置视图的宽 acView.Width = Math.Abs(pMin.X - pMax.X); // 设置视图中心 acView.CenterPoint = new Point2d((pMin.X + pMax.X) / 2, (pMin.Y + pMax.Y) / 2); // 更新当前视图 acDoc.Editor.SetCurrentView(acView); } // 提交更改 acTrans.Commit(); } } catch (Exception) { throw; } } } }