using GrxCAD.ApplicationServices; using GrxCAD.DatabaseServices; using GrxCAD.EditorInput; using GrxCAD.Geometry; using GrxCAD.GraphicsInterface; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace HCTools { class ContourLineFix { public void Clfix() { Database db = HostApplicationServices.WorkingDatabase; Document doc = Application.DocumentManager.MdiActiveDocument; DocumentLock doclock = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument(); ObjectId jigid = new ObjectId(); PromptPointResult pPtRes; PromptPointOptions pPtOpts = new PromptPointOptions(""); pPtOpts.Message = "选择起点\n "; pPtRes = doc.Editor.GetPoint(pPtOpts); Point3d ptStart = pPtRes.Value; if (pPtRes.Status == PromptStatus.OK) { LineJig lJig = new LineJig(ptStart); PromptResult PR = doc.Editor.Drag(lJig); if (PR.Status == PromptStatus.OK) { jigid = BasicFunction.AddObj(doc, lJig.line_1); GrxCAD.DatabaseServices.Line jigl = (GrxCAD.DatabaseServices.Line)BasicFunction.GetDBObject(jigid); List plllist = Pllsort(jigl); GrxCAD.DatabaseServices.Polyline pll_s = (GrxCAD.DatabaseServices.Polyline)BasicFunction.GetDBObject(plllist[0]); GrxCAD.DatabaseServices.Polyline pll_w = (GrxCAD.DatabaseServices.Polyline)BasicFunction.GetDBObject(plllist[plllist.Count - 1]); double dgj = (pll_s.Elevation - pll_w.Elevation)/(plllist.Count - 1); double ele = pll_s.Elevation; using (Transaction tr = db.TransactionManager.StartTransaction()) { for (int i = 1; i < plllist.Count - 1; i++) { GrxCAD.DatabaseServices.Polyline pll = (GrxCAD.DatabaseServices.Polyline)tr.GetObject(plllist[i], OpenMode.ForWrite, true); pll.Elevation = ele - dgj; ele = ele - dgj; } GrxCAD.DatabaseServices.Line jl = (GrxCAD.DatabaseServices.Line)tr.GetObject(jigid, OpenMode.ForWrite, true); jl.Erase(); tr.Commit(); } Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; ed.WriteMessage("修改完成\n"); } } doclock.Dispose(); } private static List Pllsort(Line jigl) { List sortedid = new List(); Point3dCollection ptcoll = new Point3dCollection(); var plane = new Plane(Point3d.Origin, Vector3d.ZAxis); ptcoll.Add(jigl.StartPoint); ptcoll.Add(jigl.EndPoint); Database db = HostApplicationServices.WorkingDatabase; Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; TypedValue[] value = new TypedValue[] { new TypedValue((int)DxfCode.Start,"LWPOLYLINE"), };//设置筛选条件 SelectionFilter filter = new SelectionFilter(value); // 要求在图形区域中手动选择对象 PromptSelectionResult psr = ed.SelectFence(ptcoll, filter); if (psr.Status == PromptStatus.OK) { SelectionSet ss = psr.Value; ObjectIdCollection idcoll = new ObjectIdCollection(ss.GetObjectIds()); using (Transaction trans = db.TransactionManager.StartTransaction()) { for (int i = 1; i < idcoll.Count; i++) { GrxCAD.DatabaseServices.Polyline pll = (GrxCAD.DatabaseServices.Polyline)BasicFunction.GetDBObject(idcoll[i]); Point3dCollection itsresult1 = new Point3dCollection(); pll.IntersectWith(jigl, Intersect.OnBothOperands, plane, itsresult1, IntPtr.Zero, IntPtr.Zero); double x1 = itsresult1[0].X; double y1 = itsresult1[0].Y; double x2 = jigl.StartPoint.X; double y2 = jigl.StartPoint.Y; double tempdist = Math.Abs(Math.Sqrt(Math.Pow(x1 - x2, 2) + Math.Pow(y1 - y2, 2))); ObjectId temp = idcoll[i]; for (int j = i - 1; j >=0; j--) { GrxCAD.DatabaseServices.Polyline pll2 = (GrxCAD.DatabaseServices.Polyline)BasicFunction.GetDBObject(idcoll[j]); Point3dCollection itsresult2 = new Point3dCollection(); pll2.IntersectWith(jigl, Intersect.OnBothOperands, plane, itsresult2, IntPtr.Zero, IntPtr.Zero); double x3 = itsresult2[0].X; double y3 = itsresult2[0].Y; double tempdist2 = Math.Abs(Math.Sqrt(Math.Pow(x3 - x2, 2) + Math.Pow(y3 - y2, 2))); if (tempdist < tempdist2) { idcoll[j + 1] = idcoll[j]; idcoll[j] = temp; } else break; } } foreach (ObjectId id in idcoll) { sortedid.Add(id); } trans.Commit(); } } return sortedid; } } public class LineJig : DrawJig { public Line line_1; public Point3d BasePnt; public Point3d AcquirePnt; public LineJig(Point3d _basePnt) { line_1 = new Line(); BasePnt = _basePnt; } protected override SamplerStatus Sampler(JigPrompts prompts) { JigPromptPointOptions JPPO = new JigPromptPointOptions(); JPPO.Message = "选择终点\n"; JPPO.UserInputControls = (UserInputControls.Accept3dCoordinates | UserInputControls.NullResponseAccepted | UserInputControls.AnyBlankTerminatesInput); PromptPointResult PR = prompts.AcquirePoint(JPPO); if (PR.Status != PromptStatus.OK) return SamplerStatus.Cancel; if (PR.Value == AcquirePnt) return SamplerStatus.NoChange; AcquirePnt = PR.Value; return SamplerStatus.OK; } protected override bool WorldDraw(WorldDraw draw) { Point3d point_1 = new Point3d(BasePnt.X, BasePnt.Y, 0); Point3d point_2 = new Point3d(AcquirePnt.X, AcquirePnt.Y, 0); line_1.StartPoint = point_1; line_1.EndPoint = point_2; line_1.Color = GrxCAD.Colors.Color.FromColorIndex(GrxCAD.Colors.ColorMethod.ByColor, 7); draw.Geometry.Draw(line_1); return true; } } }