Revit MEP二次开发之连接器Connector

连接器是mep的专有概念,相当于管类的内置几何参数。
比如一条风管,相当于一条线段,线段就有两个端点。
这两个端点就保存在连接器中。
其他可能有不止两个端点的概念。
[Transaction(TransactionMode.Automatic)]
[Regeneration(RegenerationOption.Manual)]
[Journaling(JournalingMode.NoCommandData)]
public class GetConnector : IExternalCommand
{
    
public Result Execute(ExternalCommandData commandData, ref string messages, ElementSet elements)
    {
        
try
        {
            
if (null == commandData)
            {
                
throw new ArgumentNullException("commandData");
            }

            UIApplication uiApp 
= commandData.Application;
            Document doc 
= uiApp.ActiveUIDocument.Document;
            Selection sel 
= uiApp.ActiveUIDocument.Selection;

            MessageBox.Show(
"请选择一个风管");

            Reference refelem 
= sel.PickObject(ObjectType.Element, "请选择一个风管");

            Duct duct 
= refelem.Element as Duct;
            
if (duct == null)
            {
                
return Result.Cancelled;
            }

            
//取得对象连接器集合的方法
            ConnectorSetIterator csi = duct.ConnectorManager.Connectors.ForwardIterator();

            IList
<XYZ> xyzList = new List<XYZ>();
            
while (csi.MoveNext())
            {
                Connector conn 
= csi.Current as Connector;//遍历所有的连接器
                xyzList.Add(conn.Origin);
            }

            doc.Delete(duct);
//删除风管
            
//NewLine(xyz1,xyz2,是线段还是直线);在原来的位置画一条直线直线,却不显示。
            uiApp.Application.Create.NewLine(xyzList.ElementAt(0), xyzList.ElementAt(1), true);

            
//在原来的位置创建一个Pipe
            FilteredElementCollector filterCollector = new FilteredElementCollector(doc);
            filterCollector.OfClass(
typeof(PipeType));
            PipeType pipeType 
= filterCollector.FirstElement() as PipeType;

            
if (null != pipeType)
            {
                doc.Create.NewPipe(xyzList.ElementAt(
0), xyzList.ElementAt(1), pipeType);
            }
        }
        
catch (Exception e)
        {
            messages 
= e.Message;
            
return Result.Failed;
        }

        
return Result.Succeeded;
    }
}
20110330更新
连接器的方向
/*判断风管的流方向
 * 双向     FlowDirectionType.Bidirectional
 * 只进不出 FlowDirectionType.In
 * 只出不进 FlowDirectionType.Out
 
*/
[Transaction(TransactionMode.Automatic)]
[Regeneration(RegenerationOption.Automatic)]
public class theDirectionType : IExternalCommand
{
    
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
    {
        UIApplication uiApp 
= commandData.Application;
        Document doc 
= uiApp.ActiveUIDocument.Document;
        Selection sel 
= uiApp.ActiveUIDocument.Selection;

        Reference refelem 
= sel.PickObject(ObjectType.Element, "请选择一个风管");
        Duct duct 
= refelem.Element as Duct;

        ConnectorSetIterator csi 
= duct.ConnectorManager.Connectors.ForwardIterator();
        
while (csi.MoveNext())
        {
            Connector conn 
= csi.Current as Connector;
            
if (conn.Direction == FlowDirectionType.Bidirectional)
            {
                MessageBox.Show(
"双向");
            }
            
else if (conn.Direction == FlowDirectionType.In)
            {
                MessageBox.Show(
"只进不出");
            }
            
else if (conn.Direction == FlowDirectionType.Out)
            {
                MessageBox.Show(
"只出不进");
            }
        }

        
return Result.Succeeded;
    }
}
end
posted @ 2011-03-18 10:49  大气象  阅读(2757)  评论(1编辑  收藏  举报
http://www.tianqiweiqi.com