revit-二次开发射线法

using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LOOKupidandlocation
{
    [Transaction(TransactionMode.Manual)]
    //射线法寻找穿过的对象

    public class Command : IExternalCommand

    {

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

        {

            UIDocument uidoc = commandData.Application.ActiveUIDocument;

            Document doc = uidoc.Document;

            View3D view3d = doc.ActiveView as View3D;

            //XYZ.Zero为射线原点 XYZ.BasisZ为射线向量方向 

            ReferenceIntersectElement(doc, view3d, XYZ.Zero, XYZ.BasisZ);

            return Result.Succeeded;

        }
        //射线相交法只能在三维视图下使用

         private void ReferenceIntersectElement(Document doc, View3D view3d, XYZ origin, XYZ normal)

        {

            ElementClassFilter filter = new ElementClassFilter(typeof(FamilyInstance));

            ReferenceIntersector refInter = new ReferenceIntersector(filter, FindReferenceTarget.Element, view3d);

            IList listContext = refInter.Find(origin, normal);

            foreach (ReferenceWithContext reference in listContext)

            {

                Reference refer = reference.GetReference();

                ElementId id = refer.ElementId;

                FamilyInstance instance = doc.GetElement(id) as FamilyInstance;
                MessageBox.Show(instance.Name);

                


            }


          }

    }

}

你可能感兴趣的:(revit二次开发)