Revit二次开发之拾取轴网交点和标高
参考:
https://blog.csdn.net/imfour/article/details/80148470
http://greatverve.cnblogs.com/archive/2011/03/16/revit-Intersection.html
需要找到到布设脚手架起点,所以最终决定利用轴网来实现此功能
1 public static XYZ Grid(UIDocument uiDocument) 2 { 3 Document document = uiDocument.Document; 4 5 //选择点 6 MessageBox.Show("选择轴网交点(大概就可以会自动识别)"); 7 XYZ sel_point = uiDocument.Selection.PickPoint(ObjectSnapTypes.None,"选择轴网交点"); 8 9 //获取所有轴网 10 FilteredElementCollector filteredElementCollector = new FilteredElementCollector(document); 11 filteredElementCollector.OfClass(typeof(Grid)); 12 13 //过滤出所有直线轴网 14 List<Grid> lineGrid = new List<Grid>(); 15 foreach (Grid g in filteredElementCollector) 16 { 17 if ((g.Curve as Line) != null) lineGrid.Add(g); 18 } 19 //变量; 20 Grid grid_n1 = null; 21 Grid grid_n2 = null; 22 double dis1 = double.MaxValue; 23 double dis2 = double.MaxValue; 24 //遍历轴网,计算出离选择点最近的一条轴网 25 foreach (Grid g in lineGrid) 26 { 27 if (g.Curve.Distance(sel_point) < dis1) 28 { 29 grid_n1 = g; 30 dis1 = g.Curve.Distance(sel_point); 31 } 32 } 33 //遍历轴网,计算出离选择点最近的第二条轴网 34 foreach (Grid g in lineGrid) 35 { 36 if (!(g.Curve as Line).Direction.IsAlmostEqualTo((grid_n1.Curve as Line).Direction) && g.Curve.Distance(sel_point) < dis2) 37 { 38 grid_n2 = g; 39 dis2 = g.Curve.Distance(sel_point); 40 } 41 } 42 43 Line line1 = grid_n1.Curve as Line; 44 Line line2 = grid_n2.Curve as Line; 45 46 IntersectionResultArray results;//交点数组 47 SetComparisonResult result = line1.Intersect(line2, out results); 48 49 if (result != SetComparisonResult.Overlap)//重叠,没有重叠就是平行 50 { 51 throw new InvalidOperationException("重叠,没有重叠就是平行"); 52 } 53 if (results == null || results.Size != 1)//没有交点或者交点不是1个 54 { 55 throw new InvalidOperationException("没有交点或者交点不是1个"); 56 } 57 IntersectionResult iResult = results.get_Item(0);//取得交点 58 XYZ intersectionPoint = iResult.XYZPoint;//取得交点坐标 59 60 return intersectionPoint; 61 }
但随后发现,上述方法得到的Z坐标一直都是0,所以无奈,只能在去寻找标高,来作为Z坐标
但要注意单位,方法返回的数值为英尺,包括坐标系的单位均为英尺,所以如果想化成毫米要*304.8
1 private static double level(UIDocument uiDocument,string LevelName) 2 { 3 Document document = uiDocument.Document; 4 5 Selection selection = uiDocument.Selection; 6 7 Transaction transaction = new Transaction(document, "level"); 8 transaction.Start(); 9 10 FilteredElementCollector collector = new FilteredElementCollector(uiDocument.Document); 11 ICollection<Element> collection = collector.OfClass(typeof(Level)).ToElements(); 12 Level level = null; 13 foreach (Element e in collection) 14 { 15 if (e.Name == LevelName) 16 { 17 level = (Level)e; 18 } 19 } 20 transaction.Commit(); 21 22 double Y = level.ProjectElevation; 23 return Y; 24 }