Revit API风管对齐

start
[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
public class cmd : IExternalCommand
{
    /// <summary>
    
/// 取得风管两端连接器
    
/// </summary>
    
/// <param name="duct"></param>
    
/// <returns></returns>
    private List<Connector> getConn(Duct duct)
    {
        List<Connector> listConn = new List<Connector>();
        ConnectorSetIterator csi = duct.ConnectorManager.Connectors.ForwardIterator();
        while (csi.MoveNext())
        {
            Connector conn = csi.Current as Connector;
            if (ConnectorType.End == conn.ConnectorType)
            {
                listConn.Add(conn);
            }
            else if (ConnectorType.Curve == conn.ConnectorType)
            {
                //return conn;
            }
        }
        return listConn;
    }
    public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elements)
    {
        UIDocument uiDoc = cmdData.Application.ActiveUIDocument;
        UIApplication uiApp = cmdData.Application;
        Document doc = uiDoc.Document;
        Selection sel = uiDoc.Selection;

        Transaction ts = new Transaction(doc, "http://revit.5d6d.com");
        ts.Start();
        try
        {
            Element elDuct0 = doc.GetElement(sel.PickObject(ObjectType.Element, "选择风管1:"));
            Element elDuct1 = doc.GetElement(sel.PickObject(ObjectType.Element, "选择风管2:"));
            Duct duct0 = elDuct0 as Duct;
            Duct duct1 = elDuct1 as Duct;
            List<Connector> listConn0 = getConn(duct0);
            List<Connector> listConn1 = getConn(duct1);
            Line line0 = Line.get_Bound(listConn0[0].Origin, listConn0[1].Origin);
            Line line1 = Line.get_Bound(listConn1[0].Origin, listConn1[1].Origin);
            double dDis = line1.Distance(line0.get_EndPoint(0));//直接间距
            XYZ xyzVector = GetXYVector(line0, line1);
            LocationCurve lc = elDuct1.Location as LocationCurve;
            lc.Move(dDis * xyzVector);
            ts.Commit();
        }
        catch (Autodesk.Revit.Exceptions.OperationCanceledException)
        {
            ts.RollBack();
            return Result.Cancelled;
        }
        catch (Exception ex)
        {
            ts.RollBack();
        }

        return Result.Succeeded;
    }

    /// <summary>
    
/// 取得平行线的单位向量
    
/// </summary>
    
/// <param name="line0"></param>
    
/// <param name="line1"></param>
    
/// <returns></returns>
    public XYZ GetXYVector(Line line0, Line line1)
    {
        //基准
        XYZ startPoint0 = line0.get_EndPoint(0);
        XYZ endPoint0 = line0.get_EndPoint(1);
        //参照
        XYZ startPoint1 = line1.get_EndPoint(0);
        XYZ endPoint1 = line1.get_EndPoint(1);
        //
        XYZ resultVector = new XYZ(000);

        XYZ zStart = new XYZ(startPoint1.X, startPoint1.Y, 0);
        XYZ zEnd = new XYZ(endPoint1.X, endPoint1.Y, 0);

        Line line = Line.get_Bound(new XYZ(startPoint0.X, startPoint0.Y, 0), new XYZ(endPoint0.X, endPoint0.Y, 0));
        line.MakeUnbound();

        //取得一条直线所在直线相垂直的直线
        XYZ tPoint = zStart - zEnd;
        XYZ zVector = new XYZ(001);
        XYZ nPoint = tPoint.CrossProduct(zVector).Normalize() + zStart;
        Line lineN = Line.get_Bound(zStart, nPoint);
        lineN.MakeUnbound();

        //与另外一条风管的交点
        IntersectionResultArray intersectionR = new IntersectionResultArray();
        SetComparisonResult comparisonR;

        comparisonR = line.Intersect(lineN, out intersectionR);

        if (SetComparisonResult.Disjoint != comparisonR)
        {
            if (!intersectionR.IsEmpty)
            {
                resultVector = intersectionR.get_Item(0).XYZPoint;
            }
        }

        if (!zStart.IsAlmostEqualTo(resultVector))
        {
            resultVector = (resultVector - zStart).Normalize();
        }

        return resultVector;
    }
}
url:http://greatverve.cnblogs.com/p/revit-api-duct-alignment.html
原文地址:https://www.cnblogs.com/greatverve/p/revit-api-duct-alignment.html