今天看到有群员有个需求:在选择的图元的时候 第一次选中后,后面的选中的图元也只能是第一次选中的同类别图元。比如说我第一次选中了门,后面选择的图元也只能是门
直接上代码~
实现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;
}
}
实现效果
标签:Element,doc,Revit,uidoc,element,二次开发,图元,public From: https://www.cnblogs.com/youngala/p/17237256.html