本帖最后由 lzh741206 于 2010-12-22 13:16 编辑
还是把代码贴上吧,简单的注释了下
注意先引用我的TlsBasal.dll(重贴了一次)
http://bbs.mjtd.com/forum.php?mod=viewthread&tid=75701&page=1&extra=#pid403883
- [CommandMethod("tttt")]
- public static void tttt()
- {
- var doc = Application.DocumentManager.MdiActiveDocument;
- var db = doc.Database;
- var ed = doc.Editor;
- //获取直线选集
- SelectionFilter sf = new ResultTree{ { 0, "line" } };
- var resSel = ed.GetSelection(sf);
-
- using (var tr = new DBTransaction())
- {
- //遍历选择集,按方向将直线分组
- var dict = new Dictionary<LineSegment3d, List<Line>>();
- foreach (Line line in resSel.Value.GetEntitys<Line>(tr))
- {
- //转换DBLine为GeLineSegment3d
- bool flag = false;
- LineSegment3d ls = line.ToCurve3d();
- //遍历分组字典
- foreach (var lls in dict)
- {
- //如果共线,加入当前组
- if (lls.Key.IsColinearTo(ls))
- {
- flag = true;
- lls.Value.Add(line);
- break;
- }
- }
- //如果没有找到,新建一个组
- if (!flag)
- dict.Add(ls, new List<Line> { line });
- }
- //遍历分组字典
- foreach (var lls in dict)
- {
- var lines = lls.Value;
- if (lines.Count > 1)
- {
- //将直线的起点终点放入同一集合
- var pts = new List<Point3d>();
- foreach (var line in lines)
- {
- pts.Add(line.StartPoint);
- pts.Add(line.EndPoint);
- }
- //按XYZ排序方式找到最小点和最大点
- var ptarr = pts.FindByExt(GeometryEx.CompareTo);
- //删除原直线,并生成新的
- tr.Erase(lines);
- var btr = tr.OpenCurrentSpace();
- tr.AddEntity(btr, new Line(ptarr[0], ptarr[1]));
- }
- }
- }
- }
|