Loading

Loading

Revit二次开发-连续选择同一类别的图元

今天看到有群员有个需求:在选择的图元的时候 第一次选中后,后面的选中的图元也只能是第一次选中的同类别图元。比如说我第一次选中了门,后面选择的图元也只能是门

直接上代码~

实现ISelectionFilter接口

ISelectionFilter

public class SelectionFilter : ISelectionFilter
    {
        private readonly Element m_targetElement;
        public SelectionFilter(Element e)
        {
            m_targetElement = e;
        }
        public bool AllowElement(Element elem)
        {
            return elem.Category?.Id == m_targetElement.Category?.Id;
        }
        public bool AllowReference(Reference reference, XYZ position)
        {
            return false;
        }
    }

IExternalCommand

IExternalCommand
 [Transaction(TransactionMode.Manual)]
    public class PickSelector : IExternalCommand
    {
        private UIDocument m_uidoc;
        private Document m_doc;
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            m_uidoc = commandData.Application.ActiveUIDocument;
            m_doc = m_uidoc.Document;
            const string m_prompt = "pick a element";
            Element m_pickedElement = m_doc.GetElement(m_uidoc.Selection.PickObject(ObjectType.Element, m_prompt));
            while (true)
            {
                try
                {
                    Element m_element = m_doc.GetElement(m_uidoc.Selection.PickObject(ObjectType.Element, 
                        new SelectionFilter(m_pickedElement), m_prompt));
                    TaskDialog.Show("Prompt", $"The name of selected element is {m_element.Name}.");
                }
                catch (Autodesk.Revit.Exceptions.OperationCanceledException)
                {
                    break;
                }
                catch (Exception e)
                {
                    Debug.Assert(false, e.Message);
                    break;
                }
            }
            return Result.Succeeded;
        }
    }

 

实现效果

 

posted @ 2023-03-20 18:14  热情定无变  阅读(210)  评论(0)    收藏  举报