EaBIM

标题: Revit API判断点与线是否相交 [打印本页]

作者: 萧闫子    时间: 2014-1-9 13:07
标题: Revit API判断点与线是否相交


曲线有个curve.Distance(xyz)函数用来判断曲线与的距离。[TransactionAttribute(Autodesk.Revit.Attributes.TransactionMode.Manual)]
  1. <div><font color="#696969" face="Microsoft Yahei, Simsun"></font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">public</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">class cmdXyzLine : IExternalCommand</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">{</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">    public Result Execute(ExternalCommandData commandData, ref</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">string messages, ElementSet elements)</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">    {</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">        UIApplication uiApp = commandData.Application;</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">        Document doc = uiApp.ActiveUIDocument.Document;</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">        Selection sel = uiApp.ActiveUIDocument.Selection;</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">
  2. </font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">        Transaction ts =</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">new Transaction(doc, "http://revit.5d6d.com");</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">        ts.Start();</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">
  3. </font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">        //选取一直线,先风管吧。</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">        Duct duct = doc.GetElement(sel.PickObject(ObjectType.Element, "选取一风管")) as Duct;</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">        //选取一点</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">        XYZ xyz = sel.PickPoint("选取一点");</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">        //判断是否相交</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">        LocationCurve ductCurve = duct.Location as LocationCurve;</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">        TaskDialog.Show("distance", ductCurve.Curve.Distance(xyz).ToString());</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">        if (ductCurve.Curve.Distance(xyz) <</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">0.01)</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">            TaskDialog.Show("info", "相交");</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">        else</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">            TaskDialog.Show("info", "不相交");</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">        //风管上的点与风管曲线的距离</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">        TaskDialog.Show("dis", ductCurve.Curve.Distance(ductCurve.Curve.get_EndPoint(0)).ToString() +</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">"米");</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">
  4. </font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">        ts.Commit();</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">
  5. </font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">        return Result.Succeeded;</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">    }</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">}</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">
  6. </font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">另外一个方法,比较专业,抽时间仔细研究研究:</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">///</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun"><summary></font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">/// 两曲线通过轨迹相同且相连则可合并</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">///</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun"></summary></font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">///</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun"><param name="uiApp"></param></font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">///</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun"><param name="c1"></param></font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">///</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun"><param name="c2"></param></font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">///</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun"><returns></returns></font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">public</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">static Curve Combine2Curve(UIApplication uiApp, Curve c1, Curve c2)</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">{</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">    // 两曲线均为直线</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">    Line l1 = c1 as Line;</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">    if (l1 !=</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">null)</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">    {</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">        Line l2 = c2 as Line;</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">        if (l2 !=</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">null)</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">        {</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">            XYZ vt1 = LineAssistFunc.GetVector(l1);</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">            XYZ vt2 = LineAssistFunc.GetVector(l2);</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">            // 平行</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">
  7. </font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">if (GeoUtil.IsParallel(vt1, vt2))</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">            {</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                XYZ pts1 = l1.get_EndPoint(0);</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                XYZ pte1 = l1.get_EndPoint(1);</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                XYZ pts2 = l2.get_EndPoint(0);</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                XYZ pte2 = l2.get_EndPoint(1);</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                // 相连</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">
  8. </font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">if (pts1.DistanceTo(pts2) < precision || pts1.DistanceTo(pte2) < precision ||</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                    pte1.DistanceTo(pts2) < precision || pte1.DistanceTo(pte2) < precision)</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                {</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                    List<XYZ> arPts =</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">new List<XYZ>();</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                    arPts.Add(pts1);</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                    arPts.Add(pte1);</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                    arPts.Add(pts2);</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                    arPts.Add(pte2);</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                    GeoUtil.SortPointByDirect(uiApp, arPts, vt1);</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">
  9. </font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                    Line nLine = uiApp.Application.Create.NewLineBound(arPts.First(), arPts.Last());</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                    return nLine;</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                }</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">            }</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">        }</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">    }</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">
  10. </font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">    // 两曲线均为弧线</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">    Arc a1 = c1 as Arc;</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">    if (a1 !=</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">null)</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">    {</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">        Arc a2 = c2 as Arc;</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">        if (a2 !=</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">null)</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">        {</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">            double r1 = a1.Radius;</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">            double r2 = a2.Radius;</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">            XYZ ptc1 = a1.Center;</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">            XYZ ptc2 = a2.Center;</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">            // 共线</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">
  11. </font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">if (Math.Abs(r1 - r2) < precision && ptc1.DistanceTo(ptc2) < precision)</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">            {</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                XYZ pts1 = a1.get_EndPoint(0);</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                XYZ pte1 = a1.get_EndPoint(1);</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                XYZ pts2 = a2.get_EndPoint(0);</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                XYZ pte2 = a2.get_EndPoint(1);</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                // 相连</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">
  12. </font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">if (pts1.DistanceTo(pts2) < precision || pts1.DistanceTo(pte2) < precision ||</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                    pte1.DistanceTo(pts2) < precision || pte1.DistanceTo(pte2) < precision)</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                {</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                    // 相交的点为ptOnArc点,其余两点为弧线的起终点</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                    XYZ ptStart =</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">null, ptEnd =</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">null, ptOnArc =</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">null;</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                    if (pts1.DistanceTo(pts2) < precision)</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                    {</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                        ptStart = pte1;</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                        ptEnd = pte2;</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                        ptOnArc = pts1;</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                    }</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                    else</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">if (pts1.DistanceTo(pte2) < precision)</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                    {</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                        ptStart = pte1;</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                        ptEnd = pts2;</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                        ptOnArc = pts1;</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                    }</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                    else</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">if (pte1.DistanceTo(pts2) < precision)</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                    {</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                        ptStart = pts1;</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                        ptEnd = pte2;</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                        ptOnArc = pte1;</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                    }</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                    else</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">if (pte1.DistanceTo(pte2) < precision)</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                    {</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                        ptStart = pts1;</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                        ptEnd = pts2;</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                        ptOnArc = pte1;</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                    }</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">
  13. </font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                    Arc nArc = uiApp.Application.Create.NewArc(ptStart, ptEnd, ptOnArc);</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                    return nArc;</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">                }</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">            }</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">        }</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">    }</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">
  14. </font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">    return</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">null;</font></div><div><font color="#696969" face="Microsoft Yahei, Simsun">}</font></div>
复制代码

作者: 莞人莞事    时间: 2014-2-20 14:34
顶...... 楼下跟上.....




欢迎光临 EaBIM (https://eabim.net/) Powered by Discuz! X3.2