- 积分
- 24557
- 明经币
- 个
- 注册时间
- 2004-3-17
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
发表于 2010-12-10 20:06:13
|
显示全部楼层
本帖最后由 lzh741206 于 2010-12-10 20:10 编辑
- [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 < ss.Count; i++ )
- {
- FenceSelectedObject selObj = ss[i] as FenceSelectedObject;
- //获取被栏选的直线和栏选点
- Line iline = tr.GetObject(selObj.ObjectId, OpenMode.ForWrite) as Line;
- Point3d pt = selObj.GetIntersectionPoints()[0].PointOnLine;
- //获取直线和偏移界线的交点集合
- Point3dCollection pts = new Point3dCollection();
- iline.IntersectWith(line1, Intersect.ExtendArgument, pts, 0, 0);
- iline.IntersectWith(line2, Intersect.ExtendArgument, pts, 0, 0);
- if (pts.Count > 0)
- {
- //按交点在直线上的位置排序,并转换为参数集合
- var pars =
- pts.Cast<Point3d>()
- .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<ObjectId> AddEntity<T>(Transaction tr, BlockTableRecord btr, IEnumerable<T> ents) where T : Entity
- {
- List<ObjectId> ids = new List<ObjectId>();
- foreach (T ent in ents)
- {
- ids.Add(AddEntity(tr, btr, ent));
- }
- return ids;
- }
} |
|