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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM