using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace View3Dset2
{
/// <summary>
/// 拾取墙体创建平行剖面
/// </summary>
[Transaction(TransactionMode.Manual)]
public class Class1 : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIDocument uidoc = commandData.Application.ActiveUIDocument;
Document doc = uidoc.Document;
View activeView = uidoc.ActiveView;
//获取默认剖面类型
ElementId typeid = doc.GetDefaultElementTypeId(ElementTypeGroup.ViewTypeSection);
//选择构件
Reference wallrefer = uidoc.Selection.PickObject(ObjectType.Element, "选择构件");
//本案例以墙体为例
Wall wall = doc.GetElement(wallrefer) as Wall;
//获取墙体定位线
Curve wallcurve = (wall.Location as LocationCurve).Curve;
//获取定位线方向
XYZ wallDir = wallcurve.ComputeDerivatives(0.5, true).BasisX.Normalize();
//视图深度,两侧往外拓展500mm
double lenght = wallcurve.Length + 500 / 304.8;
//视图深度,设为2000
double deep = 2000 / 304.8;
//获得墙体的boundingbox,求中心点及总高度(包络框外扩2000)
BoundingBoxXYZ wallBox = wall.get_BoundingBox(activeView);
XYZ center = (wallBox.Max + wallBox.Min) / 2;
double h = wallBox.Max.Z - wallBox.Min.Z + 2000 / 304.8;
//构造剖面的boundingbox,设置其最大最小控制点
BoundingBoxXYZ sectionbox = new BoundingBoxXYZ();
sectionbox.Max = new XYZ(lenght / 2, h / 2, deep / 2);
sectionbox.Min = new XYZ(-lenght / 2, -h / 2, -deep / 2);//等价于在原点附近创建了等于墙体大小的范围框
//构造一个坐标转换器,按前面的分析设置xyz方向的值
Transform transform = Transform.Identity;
transform.Origin = center;
transform.BasisX = -wallDir;
transform.BasisY = XYZ.BasisZ;
//上述两方向的叉积获取第三方向
transform.BasisZ = (-wallDir).CrossProduct(XYZ.BasisZ);
//将坐标转换器应用到剖面boundingbox
sectionbox.Transform = transform;
//新建并启动事务
Transaction transaction = new Transaction(doc, "创建剖面视图实例");
transaction.Start();
//创建剖面视图
ViewSection viewSection = ViewSection.CreateSection(doc, typeid, sectionbox);
//提交事务
transaction.Commit();
//将当前视图跳转到剖面
uidoc.ActiveView = viewSection;
return Result.Succeeded;
}
}
}
剖面视图创建
最新推荐文章于 2024-07-21 21:46:04 发布