Revit API射线法读取空间中相交的元素

Revit API提供根据射线来寻找经过的元素。方法是固定模式,没什么好说。
关键代码: doc.FindReferencesWithContextByDirection(ptStart, (ptEnd - ptStart), view3d)
// 射线法寻找穿过的对象
[TransactionAttribute(Autodesk.Revit.Attributes.TransactionMode.Manual)]
public  class FindSupporting : IExternalCommand
{
     public Result Execute(ExternalCommandData commandData,  ref  string messages, ElementSet elements)
    {

        UIApplication app = commandData.Application;
        Document doc = app.ActiveUIDocument.Document;
        Transaction trans =  new Transaction(doc,  " ExComm ");
        trans.Start();

        Selection sel = app.ActiveUIDocument.Selection;
         // Reference ref1 = sel.PickObject(ObjectType.Element, "Please pick a beam");
        
// FamilyInstance beam = doc.GetElement(ref1) as FamilyInstance;
        Reference ref1 = sel.PickObject(ObjectType.Element,  " Please pick a duct ");
        Duct duct = doc.GetElement(ref1)  as Duct;

         // Read the beam's location line
        
// LocationCurve lc = beam.Location as LocationCurve;
        LocationCurve lc = duct.Location  as LocationCurve;
        Curve curve = lc.Curve;
         // 取得线端点的方法
        XYZ ptStart = curve.get_EndPoint( 0);
        XYZ ptEnd = curve.get_EndPoint( 1);

         // move the two point a little bit lower, so the ray can go through the wall
        XYZ offset =  new XYZ( 000.01); // 向量偏移的方法,这里向下偏移。
        ptStart = ptStart - offset;
        ptEnd = ptEnd - offset;

        View3D view3d =  null;
        view3d = doc.ActiveView  as View3D;
         if (view3d ==  null)
        {
            TaskDialog.Show( " 3D view "" current view should be 3D view ");
             return Result.Failed;
        }

         double beamLen = curve.Length;
         // 终点-起点就是线的方向。这里是射线法的关键代码。必须在三维视图下。
        IList<ReferenceWithContext> references = doc.FindReferencesWithContextByDirection(ptStart, (ptEnd - ptStart), view3d);

         // ElementSet wallSet = app.Application.Create.NewElementSet();

        sel.Elements.Clear();
         double tolerate =  0.00001;
         foreach (ReferenceWithContext reference  in references)
        {
            Reference ref2 = reference.GetReference(); // 取得引用
            ElementId id = ref2.ElementId;
            Element elem = doc.get_Element(id);

             if (elem  is Wall)
            {
                 if (reference.Proximity < (beamLen + tolerate)) // Proximity接近,即与射线原点的距离。
                {
                    sel.Elements.Add(elem);
                }
            }
        }


        trans.Commit();

         return Result.Succeeded;
    }
}
url: http://greatverve.cnblogs.com/p/FindReferencesWithContextByDirection.html
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值