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);
                
// 相连
                if (pts1.DistanceTo(pts2) < precision || pts1.DistanceTo(pte2) < precision ||
                    pte1.DistanceTo(pts2) 
< precision || pte1.DistanceTo(pte2) < precision)
                {
                    
// 相交的点为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)
                    {
                        ptStart 
= pte1;
                        ptEnd 
= pts2;
                        ptOnArc 
= pts1;
                    }
                    
else if (pte1.DistanceTo(pts2) < precision)
                    {
                        ptStart 
= pts1;
                        ptEnd 
= pte2;
                        ptOnArc 
= pte1;
                    }
                    
else if (pte1.DistanceTo(pte2) < precision)
                    {
                        ptStart 
= pts1;
                        ptEnd 
= pts2;
                        ptOnArc 
= pte1;
                    }

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

    
return null;
}
from:http://revit.5d6d.com/thread-1379-1-1.html
posted @ 2012-03-31 13:11  大气象  阅读(3554)  评论(4编辑  收藏  举报
http://www.tianqiweiqi.com