本文演示如何选择Revit文件中的线和面。
1.用Revit绘制模型
2.选择面
示例代码:
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using System.Windows.Forms;
namespace RevitAddin4
{
[TransactionAttribute(TransactionMode.Manual)]
public class RevitAddin : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIDocument uiDoc = commandData.Application.ActiveUIDocument;
Document doc = uiDoc.Document;
Selection selection = uiDoc.Selection;
Reference reference = selection.PickObject(ObjectType.Face);
Element element = doc.GetElement(reference);
object obj = element.GetGeometryObjectFromReference(reference);
Face face = obj as Face;
if (face != null)
{
MessageBox.Show( "被选择面的面积 = "+ face.Area.ToString(), "信息");
}
return Result.Succeeded;
}
}
}
选择墙的立面,执行结果:
选择柱的立面,执行结果:
3.选择线
示例代码:
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using System.Windows.Forms;
namespace RevitAddin4
{
[TransactionAttribute(TransactionMode.Manual)]
public class RevitAddin : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIDocument uiDoc = commandData.Application.ActiveUIDocument;
Document doc = uiDoc.Document;
Selection selection = uiDoc.Selection;
Reference reference = selection.PickObject(ObjectType.Edge);
Element element = doc.GetElement(reference);
object obj = element.GetGeometryObjectFromReference(reference);
Edge edge = obj as Edge;
if (edge != null)
{
MessageBox.Show( "被选择线的长度 = "+ edge.ApproximateLength.ToString(), "信息");
}
return Result.Succeeded;
}
}
}
选择墙的长度,执行结果:
选择柱的高度,执行结果: