最近小编在使用某榄山翻喷淋的模型的时候,发现有很多的喷淋喷头和管道都没办法自动连接,而且喷淋的量比较大,因此写了一个自动喷头与管道连接的插件让其自动连接,分享一下方法和代码。
先看一下使用某榄山喷淋翻模后的效果,喷头和管道没有自动连接。接下来自己写代码,简简单单全连上。
思路分享如下:
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.为了加快筛选的效率,小编使用了喷头的范围框进行筛选
小编的这个喷淋有2种喷头,直立型和下垂型,因此范围框筛选有两种方式,代码如下:
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. 获取连接器,将管道与喷头连接
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
{
}
}
}
}
这里小编试了一下使用,如下这两种方法都可以实现连接。
doc.Create.NewUnionFitting(ppconn2, co);
doc.Create.NewTransitionFitting(ppconn2, co);
虽然这个接头显示的是Transition。
具体原因可以参考另一篇文章《Revit二次开发之机电管道的各种管件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
{
}
}
}
}
}
}
}
由于小编最近接了几篇宣传文案,要在头条发布,还请粉丝们多担待,感兴趣可以多点点多看看,不感兴趣请忽略,多有打扰敬请谅解。