xycost 发表于 2019-9-16 22:34:15

「BIM开发」Revit二次开发入门-选择组并复制

先贴出代码:
[*]
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using Autodesk.Revit.ApplicationServices;using Autodesk.Revit.Attributes;using Autodesk.Revit.DB;using Autodesk.Revit.UI;using Autodesk.Revit.UI.Selection;
namespace PickObject{ public class Command : IExternalCommand { public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { //Get application and documnet objects UIApplication uiapp = commandData.Application; Document doc = uiapp.ActiveUIDocument.Document; try { //Define a reference Object to accept the pick result Reference pickedRef = null;
//Pick a group Selection sel = uiapp.ActiveUIDocument.Selection; GroupPickFilter selFilter = new GroupPickFilter();
pickedRef = sel.PickObject(ObjectType.Element, selFilter, "Please select a group"); Element elem = doc.GetElement(pickedRef); Group group = elem as Group;
//Pick point XYZ point = sel.PickPoint("Please pick a point to place group");
//Place the group Transaction trans = new Transaction(doc); trans.Start("Lab"); doc.Create.PlaceGroup(point, group.GroupType); trans.Commit(); return Result.Succeeded; } //If the user right-clicks or presses Esc, handle the exception catch (Autodesk.Revit.Exceptions.OperationCanceledException) { return Result.Cancelled; } catch (Exception ex) { message = ex.Message; return Result.Failed; }
} // Filter to constrain picking to model groups. Only model groups // are highlighted and can be selected when cursor is hovering. public class GroupPickFilter : ISelectionFilter { public bool AllowElement(Element e) { return (e.Category.Id.IntegerValue.Equals((int)BuiltInCategory.OST_IOSModelGroups)); } public bool AllowReference(Reference r, XYZ p) { return false; } } }
}
下面的部分解释:public Reference PickObject(ObjectType objectType,ISelectionFilter pSelFilter,string statusPrompt);此重载要求您传递三个参数:对象类型,选择过滤器和提示字符串。第二个参数应该是实现ISelectionFilter 接口的类的实例 。该ISelectionFilter接口必须实施twomethods:AllowElement() 和 AllowReference() :你需要在你的GroupPickFilter类来实现这两种。AllowElement()和AllowReference()的函数参数签名在Revit API中指定。这两种方法的签名如下面的代码所示。以下是新GroupPickFilter类的框架:
public class GroupPickFilter : ISelectionFilter
{
public bool AllowElement(Element elem)
{
//insert your code here
}
public bool AllowReference(Reference reference, XYZ position)
{
//insert your code here
}
}
在选择过程中,当光标悬停在元素上时,该元素将被传递给AllowElement()方法。该AllowElement()方法允许你检查的元素,并确定它是否应该被强调与否。如果从此方法返回true,则可以突出显示该元素并将其选中。如果返回false,则既不突出显示也不选择该元素。在AllowElement()的实现中,您通过检查其类别来决定是否可以选择该元素。这是检查给定元素的类别类型是否为“模型组”的代码行:return (elem.Category.Id.IntegerValue.Equals((int)BuiltInCategory.OST_IOSModelGroups));elem.Category.Id.IntegerValue - 从作为参数传入的元素elem获取类别ID的整数值到AllowElement()。BuiltInCategory.OST_IOSModelGroups - 指向标识内置“模型组”类别的数字,我们从固定整数值的BuiltInCategory集合中检索该类别。BuiltInCategory集合称为枚举,使用C#语言中的enum关键字进行声明。由于枚举的成员实际上是整数,因此您执行了转换以将BuildingCategory.OST_IOSModelGroups转换为整数,以便能够将其与类别ID值进行比较。如果传入AllowElement()的元素的类别ID等于模型组的类别ID,则此元素是一个组,因此应该是可选的。由于AllowElement()返回一个布尔值(“true”或“false”),因此可以在方法实现中将代码简化为一行:
public bool AllowElement(Element elem)
{
return (elem.Category.Id.IntegerValue.Equals((int)BuiltInCategory.OST_IOSModelGroups));
}
您需要实现的第二种方法是AllowReference()。在选择过程中,如果光标悬停在引用上,则此引用将传递给AllowReference()方法。再一次,如果从此方法返回true,则可以突出显示并选择引用。如果返回false,则既不能突出显示也不能选择引用。还需要实现AllowReference()方法才能完成ISelectionFilter,但由于您不关心插件中的引用,因此只需从中返回false:
public bool AllowReference(Reference reference, XYZ position)
{
return false;
}
页: [1]
查看完整版本: 「BIM开发」Revit二次开发入门-选择组并复制