摘要:
本文主要是对Selection交互API的应用:
1.选择一个点,在该点创建一个柱子; |
2.选择刚创建的柱子,计算其体积; |
3.框选若干元素,得到选中的墙的数量。 |
项目完整代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Structure;// StructuralType.NonStructural
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection; //Selection
using Autodesk.Revit.Attributes;
namespace Sele
{
[TransactionAttribute(TransactionMode.Manual)]
[RegenerationAttribute(RegenerationOption.Manual)]
public class Class1 : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIDocument uidoc = commandData.Application.ActiveUIDocument;
Document doc = uidoc.Document;
//选择一个点
Selection s1 = uidoc.Selection;//using Autodesk.Revit.UI.Selection;
XYZ point = null;
try
{
point = s1.PickPoint("请选择一个点"); //将在Revit的左下角提示
}
catch
{
return Result.Succeeded;
}
//由于需要修改点,所以需要创建一个事务Transaction
Transaction t1 = new Transaction(doc,"t1");
t1.Start();
FamilySymbol familySymbol = doc.GetElement(new ElementId(338378)) as FamilySymbol;//创建一个柱子,其Id为338378
//判断族类型
if (!familySymbol.IsActive)
{
familySymbol.Activate();
}
Level level = null;//声明标高
FamilyInstance fi = doc.Create.NewFamilyInstance(point, familySymbol, level, StructuralType.NonStructural);//using Autodesk.Revit.DB.Structure;
Selection s2 = uidoc.Selection;
Reference re = s2.PickObject(ObjectType.Edge, "请选择一个物体");
Element ele = doc.GetElement(re);
Options opt = new Options();
GeometryElement gelem = ele.get_Geometry(opt);
double v = 0.0;//声明体积
v = GetSolid(gelem).Sum(m => m.Volume) * 0.3048 * 0.3048 * 0.3048;
TaskDialog.Show("Hint", "选中的物体体积为:" + v.ToString("f3"));
//过滤元素"墙"
IList<Element> pickedElements = s2.PickElementsByRectangle(new WallSelectionFilter(), "请框选目标物体");
double num = pickedElements.Count();
TaskDialog.Show("Hint", "已选中墙的数量为:" + num);
return Result.Succeeded;
}
private List<Solid> GetSolid(GeometryElement gelem)
{
List<Solid> solids = new List<Solid>();
foreach(GeometryObject obj in gelem)//遍历数组gelem中每一个GeometryObject类型的对象
{
if(obj is Solid)//元素是3d实体
{
solids.Add(obj as Solid);
}
if(obj is GeometryElement)//GeometryElement:元素的几何表示。
{
solids.AddRange(GetSolid(obj as GeometryElement));
}
if(obj is GeometryInstance)
{
GeometryInstance gins = obj as GeometryInstance;
GeometryElement gelm = gins.GetInstanceGeometry();
solids.AddRange(GetSolid(gelem));
}
}
return solids;
}
}
//自定义过滤器
public class WallSelectionFilter:ISelectionFilter
{
public bool AllowElement(Element elem)
{
return elem is Wall;
}
public bool AllowReference(Autodesk.Revit.DB.Reference reference,Autodesk.Revit.DB.XYZ position)
{
return true;
}
}
}
效果:
参考文献:
周婧祎《Autodesk Revit 2016二次开发入门教程》