思路分享以下:javascript
使用鼠標框選須要鏈接的噴頭,固然也能夠不框選,這裏框選是爲了方便。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);
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源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。插件