-
- [CommandMethod("t3")]
- public static void Test3()
- {
- Document doc = Application.DocumentManager.MdiActiveDocument;
- Editor ed = doc.Editor;
- Database db = doc.Database;
- var resPt1 = ed.GetPoint("\n选择第一点");
- if (resPt1.Status != PromptStatus.OK) return;
- var pt1 = resPt1.Value;
- var optPt2 = new PromptPointOptions("\n选择第二点");
- optPt2.UseBasePoint = true;
- optPt2.BasePoint = pt1;
- var resPt2 = ed.GetPoint(optPt2);
- if (resPt2.Status != PromptStatus.OK) return;
- var pt2 = resPt2.Value;
- //栏选
- var resSel =
- ed.SelectFence(
- new Point3dCollection { pt1, pt2 },
- new ResultList { { 0, "line" } });
- if (resSel.Status != PromptStatus.OK) return;
- using (Transaction tr = db.TransactionManager.StartTransaction())
- {
- //按两点生成一条直线,并两侧偏移
- Line line = new Line(pt1, pt2);
- Line line1 = line.GetOffsetCurves(5)[0] as Line;
- Line line2 = line.GetOffsetCurves(-5)[0] as Line;
- BlockTableRecord btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
- //遍历选择集
- SelectionSet ss = resSel.Value;
- for (int i = 0; i 0)
- {
- //按交点在直线上的位置排序,并转换为参数集合
- var pars =
- pts.Cast()
- .Select(pnt => iline.GetParameterAtPoint(iline.GetClosestPointTo(pnt, false)))
- .OrderBy(par => par);
- //按参数集合打断,并排除掉包含栏选点的直线
- var lines =
- from Line l in iline.GetSplitCurves(new DoubleCollection(pars.ToArray()))
- let ls3d = new LineSegment3d(l.StartPoint, l.EndPoint)
- where !ls3d.IsOn(pt, new Tolerance(1e-4, 1e-4))
- select l;
- AddEntity(tr, btr, lines);
- iline.Erase();
- }
- }
- AddEntity(tr, btr, line1);
- AddEntity(tr, btr, line2);
- tr.Commit();
- }
- }
- public static ObjectId AddEntity(Transaction tr, BlockTableRecord btr, Entity ent)
- {
- ObjectId id = btr.AppendEntity(ent);
- tr.AddNewlyCreatedDBObject(ent, true);
- return id;
- }
- public static List AddEntity(Transaction tr, BlockTableRecord btr, IEnumerable ents) where T : Entity
- {
- List ids = new List();
- foreach (T ent in ents)
- {
- ids.Add(AddEntity(tr, btr, ent));
- }
- return ids;
- }
} |