剖面视图创建

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;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值