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 DeletePts { public static string gcdly;//高程点所在图层 public static List delely = new List();//需要删除的图层 public static int blc;//比例尺 public double radius;//半径 public void delept() { List deleids = new List(); 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; } //创建新图层 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); for (int i = 0; i < delely.Count; i++) { List temp = Idget(delely[i]); for (int j = 0; j < temp.Count; j++) { deleids.Add(temp[j]); } } Database db = HostApplicationServices.WorkingDatabase; DocumentLock doclock = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument(); Editor editor = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; TypedValue[] typedvalue = new TypedValue[] { new TypedValue((int)DxfCode.LayerName, gcdly), new TypedValue((int)DxfCode.Start, "Insert") }; SelectionFilter selectionfilter = new SelectionFilter(typedvalue); for (int i = 0; i < deleids.Count; i++) { using (Transaction trans = db.TransactionManager.StartTransaction()) { Entity ent = (Entity)trans.GetObject(deleids[i], OpenMode.ForWrite); if (ent is Polyline) { Polyline pll = (Polyline)trans.GetObject(deleids[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; ObjectId[] ids = selectionset.GetObjectIds(); for (int j = 0; j < ids.Count(); j++) { BlockReference br = (BlockReference)trans.GetObject(ids[j], OpenMode.ForWrite); br.Layer = "已删除"; Point3d p1 = new Point3d(br.Position.X - radius, br.Position.Y - radius, 0); Point3d p2 = new Point3d(br.Position.X + radius, br.Position.Y + radius, 0); TypedValue[] tpvl = new TypedValue[] { new TypedValue((int)DxfCode.Start, "Text"), new TypedValue((int)DxfCode.LayerName, gcdly) }; SelectionFilter filter = new SelectionFilter(tpvl); PromptSelectionResult prosr = editor.SelectCrossingWindow(p1, p2, filter); if (prosr.Status == PromptStatus.OK) { SelectionSet seleset = prosr.Value; ObjectId[] selid = seleset.GetObjectIds(); for (int ii = 0; ii < selid.Count(); ii++) { DBText text = trans.GetObject(selid[ii], OpenMode.ForWrite) as DBText; text.Layer = "已删除"; } } } } trans.Commit(); } } } editor.WriteMessage("删除完成\n"); doclock.Dispose(); } private static List Idget(string ly) { Editor editor = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; TypedValue[] typedvalue = new TypedValue[1]; typedvalue.SetValue(new TypedValue((int)DxfCode.LayerName, ly), 0); SelectionFilter selectionfilter = new SelectionFilter(typedvalue); PromptSelectionResult psr = editor.SelectAll(selectionfilter); SelectionSet selectionset = psr.Value; ObjectId[] obj = new ObjectId[selectionset.Count]; List ids = new List(); if (psr.Status == PromptStatus.OK) { obj = selectionset.GetObjectIds(); for (int i = 0; i < obj.Length; i++) { ids.Add(obj[i]); } } return ids; } } }