Revit二次開發小案例之噴頭與管道自動鏈接

最近小編在使用某欖山翻噴淋的模型的時候,發現有不少的噴淋噴頭和管道都沒辦法自動鏈接,並且噴淋的量比較大,所以寫了一個自動噴頭與管道鏈接的插件讓其自動鏈接,分享一下方法和代碼。
先看一下使用某欖山噴淋翻模後的效果,噴頭和管道沒有自動鏈接。 接下來本身寫代碼,簡簡單單全連上。

思路分享以下:javascript

  1. 使用鼠標框選須要鏈接的噴頭,固然也能夠不框選,這裏框選是爲了方便。java

 PTFamilyInstance f = new PTFamilyInstance(); IList<Element> ell = sel.PickElementsByRectangle(f,"請選擇要鏈接的噴頭");

這裏是一個過濾篩選的寫法,不會的小夥伴再看一下
微信

 public class PTFamilyInstance : ISelectionFilter { public bool AllowElement(Element element) {

if (element.Category.Name == "噴頭") { return true; }
return false;


} public bool AllowReference(Reference refer, XYZ point) { return false; }

}

2.爲了加快篩選的效率,小編使用了噴頭的範圍框進行篩選app

小編的這個噴淋有2種噴頭,直立型和下垂型,所以範圍框篩選有兩種方式,代碼以下:ui

 if (co.IsConnected == false) { conn1.Add(co);
BoundingBoxXYZ box = eel.get_BoundingBox(doc.ActiveView);//用噴頭的範圍框快速過濾
double Maxx = box.Max.X; double Maxy = box.Max.Y;
double Minx = box.Min.X; double Miny = box.Min.Y;
double MZz = box.Max.Z; Outline myOutLn = null; if (eel.GetParameters("族與類型")[0].AsValueString().Contains("直立型")) { myOutLn = new Outline(new XYZ(Minx, Miny, MZz - (50 / 304.8)), new XYZ(Maxx, Maxy, MZz - (50 / 304.8))); } else { myOutLn = new Outline(new XYZ(Minx, Miny, MZz + (50 / 304.8)), new XYZ(Maxx, Maxy, MZz + (50 / 304.8))); }
//用噴頭的範圍框過濾管道 BoundingBoxIntersectsFilter boxee = new BoundingBoxIntersectsFilter(myOutLn); FilteredElementCollector collector = new FilteredElementCollector(doc, doc.ActiveView.Id); collector.OfClass(typeof(Pipe)); pipe1 = collector.WherePasses(boxee).ToElements();//與噴頭範圍框相交的管道

   3. 獲取鏈接器,將管道與噴頭鏈接spa

 foreach (Element elp in pipe1) { Pipe pp = elp as Pipe; ConnectorSetIterator ppconn = pp.ConnectorManager.Connectors.ForwardIterator(); while (ppconn.MoveNext()) { Connector ppconn2 = ppconn.Current as Connector; if (ppconn2.IsConnected == false) {
ppconnlist.Add(ppconn2);

try { //doc.Create.NewUnionFitting(ppconn2, co); doc.Create.NewTransitionFitting(ppconn2, co);

} catch {
}



} }

}

  這裏小編試了一下使用,以下這兩種方法均可以實現鏈接。.net

doc.Create.NewUnionFitting(ppconn2, co); doc.Create.NewTransitionFitting(ppconn2, co);

             

雖然這個接頭顯示的是 Transition。
具體緣由能夠參考另外一篇文章《 Re vit二 次開發之機電管道的各類管件Fitting的認識與區別
來看看效果:


完整代碼以下:
Selection sel = uiDoc.Selection; List<ElementId> listt = new List<ElementId>();

PTFamilyInstance f = new PTFamilyInstance(); IList<Element> ell = sel.PickElementsByRectangle(f,"請選擇要鏈接的噴頭"); IList<Connector> conn1 = new List<Connector>(); IList<Connector> ppconnlist = new List<Connector>(); IList<Element> pipe1 = new List<Element>(); foreach (Element eel in ell) { FamilyInstance fa = eel as FamilyInstance; ConnectorSetIterator connector1 = fa.MEPModel.ConnectorManager.Connectors.ForwardIterator(); while (connector1.MoveNext()) { Connector co = connector1.Current as Connector; if (co.IsConnected == false) { conn1.Add(co);
BoundingBoxXYZ box = eel.get_BoundingBox(doc.ActiveView);//用噴頭的範圍框快速過濾
double Maxx = box.Max.X; double Maxy = box.Max.Y;
double Minx = box.Min.X; double Miny = box.Min.Y;
double MZz = box.Max.Z; Outline myOutLn = null; if (eel.GetParameters("族與類型")[0].AsValueString().Contains("直立型")) { myOutLn = new Outline(new XYZ(Minx, Miny, MZz - (50 / 304.8)), new XYZ(Maxx, Maxy, MZz - (50 / 304.8))); } else { myOutLn = new Outline(new XYZ(Minx, Miny, MZz + (50 / 304.8)), new XYZ(Maxx, Maxy, MZz + (50 / 304.8))); }
//用噴頭的範圍框過濾管道 BoundingBoxIntersectsFilter boxee = new BoundingBoxIntersectsFilter(myOutLn); FilteredElementCollector collector = new FilteredElementCollector(doc, doc.ActiveView.Id); collector.OfClass(typeof(Pipe)); pipe1 = collector.WherePasses(boxee).ToElements();//與噴頭範圍框相交的管道 foreach (Element elp in pipe1) { Pipe pp = elp as Pipe; ConnectorSetIterator ppconn = pp.ConnectorManager.Connectors.ForwardIterator(); while (ppconn.MoveNext()) { Connector ppconn2 = ppconn.Current as Connector; if (ppconn2.IsConnected == false) {
ppconnlist.Add(ppconn2);

try { //doc.Create.NewUnionFitting(ppconn2, co); doc.Create.NewTransitionFitting(ppconn2, co);

} catch {
}



} }
} }
} }
因爲小編最近接了幾篇宣傳文案,要在頭條發佈,還請粉絲們多擔待,感興趣能夠多點點多看看,不感興趣請忽略, 多有打擾敬請諒解。

  

本文分享自微信公衆號 - 精講Revit二次開發(HelloRevit)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。插件

相關文章
相關標籤/搜索