天天看點

Revit API判斷點與線是否相交

曲線有個curve.Distance(xyz)函數用來判斷曲線與的距離。

[TransactionAttribute(Autodesk.Revit.Attributes.TransactionMode.Manual)]

public class cmdXyzLine : IExternalCommand

{

    public Result Execute(ExternalCommandData commandData, ref string messages, ElementSet elements)

    {

        UIApplication uiApp = commandData.Application;

        Document doc = uiApp.ActiveUIDocument.Document;

        Selection sel = uiApp.ActiveUIDocument.Selection;

        Transaction ts = new Transaction(doc, "http://revit.5d6d.com");

        ts.Start();

        //選取一直線,先風管吧。

        Duct duct = doc.GetElement(sel.PickObject(ObjectType.Element, "選取一風管")) as Duct;

        //選取一點

        XYZ xyz = sel.PickPoint("選取一點");

        //判斷是否相交

        LocationCurve ductCurve = duct.Location as LocationCurve;

        TaskDialog.Show("distance", ductCurve.Curve.Distance(xyz).ToString());

        if (ductCurve.Curve.Distance(xyz) < 0.01)

            TaskDialog.Show("info", "相交");

        else

            TaskDialog.Show("info", "不相交");

        //風管上的點與風管曲線的距離

        TaskDialog.Show("dis", ductCurve.Curve.Distance(ductCurve.Curve.get_EndPoint(0)).ToString() + "米");

        ts.Commit();

        return Result.Succeeded;

    }

}

另外一個方法,比較專業,抽時間仔細研究研究:

/// <summary>

/// 兩曲線通過軌迹相同且相連則可合并

/// </summary>

/// <param name="uiApp"></param>

/// <param name="c1"></param>

/// <param name="c2"></param>

/// <returns></returns>

public static Curve Combine2Curve(UIApplication uiApp, Curve c1, Curve c2)

    // 兩曲線均為直線

    Line l1 = c1 as Line;

    if (l1 != null)

        Line l2 = c2 as Line;

        if (l2 != null)

        {

            XYZ vt1 = LineAssistFunc.GetVector(l1);

            XYZ vt2 = LineAssistFunc.GetVector(l2);

            // 平行

            if (GeoUtil.IsParallel(vt1, vt2))

            {

                XYZ pts1 = l1.get_EndPoint(0);

                XYZ pte1 = l1.get_EndPoint(1);

                XYZ pts2 = l2.get_EndPoint(0);

                XYZ pte2 = l2.get_EndPoint(1);

                // 相連

                if (pts1.DistanceTo(pts2) < precision || pts1.DistanceTo(pte2) < precision ||

                    pte1.DistanceTo(pts2) < precision || pte1.DistanceTo(pte2) < precision)

                {

                    List<XYZ> arPts = new List<XYZ>();

                    arPts.Add(pts1);

                    arPts.Add(pte1);

                    arPts.Add(pts2);

                    arPts.Add(pte2);

                    GeoUtil.SortPointByDirect(uiApp, arPts, vt1);

                    Line nLine = uiApp.Application.Create.NewLineBound(arPts.First(), arPts.Last());

                    return nLine;

                }

            }

        }

    // 兩曲線均為弧線

    Arc a1 = c1 as Arc;

    if (a1 != null)

        Arc a2 = c2 as Arc;

        if (a2 != null)

            double r1 = a1.Radius;

            double r2 = a2.Radius;

            XYZ ptc1 = a1.Center;

            XYZ ptc2 = a2.Center;

            // 共線

            if (Math.Abs(r1 - r2) < precision && ptc1.DistanceTo(ptc2) < precision)

                XYZ pts1 = a1.get_EndPoint(0);

                XYZ pte1 = a1.get_EndPoint(1);

                XYZ pts2 = a2.get_EndPoint(0);

                XYZ pte2 = a2.get_EndPoint(1);

                    // 相交的點為ptOnArc點,其餘兩點為弧線的起終點

                    XYZ ptStart = null, ptEnd = null, ptOnArc = null;

                    if (pts1.DistanceTo(pts2) < precision)

                    {

                        ptStart = pte1;

                        ptEnd = pte2;

                        ptOnArc = pts1;

                    }

                    else if (pts1.DistanceTo(pte2) < precision)

                        ptEnd = pts2;

                    else if (pte1.DistanceTo(pts2) < precision)

                        ptStart = pts1;

                        ptOnArc = pte1;

                    else if (pte1.DistanceTo(pte2) < precision)

                    Arc nArc = uiApp.Application.Create.NewArc(ptStart, ptEnd, ptOnArc);

                    return nArc;

    return null;