Revit API判断直线相交关系移动风管

时间:2022-08-22 18:00:14

start

//风管对齐
[TransactionAttribute(Autodesk.Revit.Attributes.TransactionMode.Manual)]
public class cmdDuctAlign : IExternalCommand
{
    private XYZ GetIntersection(Line line1, Line line2)
    {
        IntersectionResultArray results;         SetComparisonResult result
            = line1.Intersect(line2, out results);
        //SetComparisonResult.Subset
        //SetComparisonResult.Superset 
        if (SetComparisonResult.Overlap == result)//没有交点,可能是平行,也可以是延长线相交。
            throw new InvalidOperationException(
                "Overlap");
        if (SetComparisonResult.Disjoint == result)//不相交
            throw new InvalidOperationException(
                "Disjoint");
        if (SetComparisonResult.Superset == result)//包含,子集
            throw new InvalidOperationException(
                "Superset");
        if (SetComparisonResult.Subset == result)//交集
            throw new InvalidOperationException(
                "Subset");
        if (results == null || results.Size != )
            throw new InvalidOperationException(
                "Could not extract line intersection point.");         IntersectionResult iResult
            = results.get_Item();         return iResult.XYZPoint;
    }
    public Result Execute(ExternalCommandData commandData, ref string messages, ElementSet elements)
    {
        UIApplication app = commandData.Application;
        Document doc = app.ActiveUIDocument.Document;
        Selection sel = app.ActiveUIDocument.Selection;         SplitButtonData splitButtonData = new SplitButtonData("", "");
        PushButton pbtn = new PushButton();
        RibbonPanel rpanel = new RibbonPanel();
                     Transaction ts = new Transaction(doc, "revit");
        ts.Start();         IList<Reference> refDucts = sel.PickObjects(ObjectType.Element, "duct");
        Duct duct1 = doc.GetElement(refDucts.ElementAt()) as Duct;
        Duct duct2 = doc.GetElement(refDucts.ElementAt()) as Duct;
        LocationCurve lCurve1 = duct1.Location as LocationCurve;
        LocationCurve lCurve2 = duct1.Location as LocationCurve;
        XYZ xyz11 = lCurve1.Curve.get_EndPoint();
        XYZ xyz12 = lCurve1.Curve.get_EndPoint();
        XYZ xyz21 = lCurve2.Curve.get_EndPoint();
        XYZ xyz22 = lCurve2.Curve.get_EndPoint();
        //判断线段相交关系
        Line line1 = Line.get_Bound(xyz11, xyz12);
        Line line2 = Line.get_Bound(xyz21, xyz22);
        GetIntersection(line1, line2);         #region 风管移动         //转化到平面
        XYZ xyz1 = new XYZ(xyz11.X, xyz11.Y, );
        XYZ xyz2 = new XYZ(xyz12.X, xyz12.Y, );
        XYZ xyz3 = new XYZ(xyz21.X, xyz21.Y, );
        XYZ xyz4 = new XYZ(xyz22.X, xyz22.Y, );
        //找到与直线垂直的向量
        XYZ vec1 = xyz2 - xyz1;
        XYZ zVec = new XYZ(, , );
        XYZ nVec = vec1.CrossProduct(zVec).Normalize();//两条线相交的面对应的向量
        TaskDialog.Show("vec", nVec.CrossProduct(zVec).Normalize().ToString());
        lCurve2.Move(nVec);         #endregion         ts.Commit();         return Result.Succeeded;
    }
}

url: http://greatverve.cnblogs.com/p/revit-api-line-SetComparisonResult.html

参考:
http://revit.haotui.com/thread-171-1-32.html
http://revit.haotui.com/thread-489-1-23.html