1. 程式人生 > >Revit二次開發之射線相交法

Revit二次開發之射線相交法

using Autodesk.Revit.UI;

using Autodesk.Revit.DB;

using Autodesk.Revit.Attributes;

using System.Windows.Media.Imaging;

using Autodesk.Revit.UI.Selection;

namespace HelloWorld

{

    [Transaction(TransactionMode.Manual)]

    [Regeneration(RegenerationOption.Manual)]

    public class Test : 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 List ReferenceIntersectElement(Document doc, View3D view3d, XYZ origin, XYZ normal)

        {

            List list = new List();

            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;

                if (instance.Symbol.Family.Name.Contains("鋼樑"))

                {

                }

            }

            return list;

        }

    }

}