前言
在Revit建模过程及后期出图的时候都有需要用到剖面,然而每次我们都通过手动去创建剖面再去对剖面视图进行调整就会显得很麻烦,工作量也是相当的大。
如果我们通过使用RevitAPI只需要简单的代码就可以实现一个选择构件自动生成剖面的功能。
Demo
下面演示如何通过使用代码在Revit中选择构件并生成构件的剖面视图,最后将当前显示视图设置为新生成的剖面视图。
namespace LjsGo.Example
{
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
public class CreateSection : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
var uiDoc = commandData.Application.ActiveUIDocument;
var doc = commandData.Application.ActiveUIDocument.Document;
var elem = commandData.Application.ActiveUIDocument.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Face);
var instance = commandData.Application.ActiveUIDocument.Document.GetElement(elem) as FamilyInstance;
var face = instance.GetGeometryObjectFromReference(elem) as Face;
// 过滤视图类型
var viewFamilyTypes = new FilteredElementCollector(doc).OfClass(typeof(ViewFamilyType)).Cast<ViewFamilyType>().ToList();
var viewFamilyType = viewFamilyTypes.FirstOrDefault(o => o.ViewFamily == ViewFamily.Section);
if (viewFamilyType == null)
{
return Result.Failed;
}
Transaction transaction = new Transaction(doc, "Create Section");
transaction.Start();
// 创建剖面
UV uvmax = face.GetBoundingBox().Max;
UV uvmin = face.GetBoundingBox().Min;
double width = uvmax.U - uvmin.U + 200 / 304.8;
double height = uvmax.V - uvmin.V + 200 / 304.8;
//生成剖面
XYZ face_Y = face.ComputeDerivatives(new UV(0, 0)).BasisY;
XYZ face_Z = -face.ComputeNormal(new UV(0, 0));
XYZ face_X = -face_Z.CrossProduct(face_Y);
XYZ center = face.Evaluate((uvmax + uvmin) / 2);
Transform tf = null;
tf = Transform.Identity;
tf.Origin = center;
tf.BasisX = face_X;
tf.BasisY = face_Y;
tf.BasisZ = face_Z;
BoundingBoxXYZ box = new BoundingBoxXYZ();
box.Transform = tf;
box.Min = new XYZ(-width / 2, -height / 2, 0);
box.Max = new XYZ(width / 2, height / 2, 500 / 304.8);
var viewSection = ViewSection.CreateSection(doc, viewFamilyType.Id, box);
viewSection.get_Parameter(BuiltInParameter.SECTION_COARSER_SCALE_PULLDOWN_IMPERIAL).Set(1000);
viewSection.CropBoxActive = true;
viewSection.DisplayStyle = DisplayStyle.ShadingWithEdges;
viewSection.DetailLevel = ViewDetailLevel.Fine;
viewSection.IsolateElementTemporary(instance.Id);
transaction.Commit();
// 跳转到改视图
uiDoc.ActiveView = viewSection;
return Result.Succeeded;
}
}
}
总结
- 通过以上代码就可以直接创建出剖面视图了。
- 注意:生成剖面的方向和BoundingBoxXYZ中的Transform有关。