Revit二次开发-根据两个点创建剖面视图

        /// <summary>
        /// 根据两个点创建剖面视图
        /// </summary>
        /// <param name="doc">文档</param>
        /// <param name="el">元素</param>
        /// <param name="pointF">起点</param>
        /// <param name="pointT">终点</param>
        /// <param name="bodyWidth">视图可见范围的长度</param>
        private void CreateSectionBox(Document doc, Element el, XYZ pointF, XYZ pointT, double bodyWidth)
        {
            var elOffset=el.get_Parameter(BuiltInParameter.INSTANCE_FREE_HOST_OFFSET_PARAM)?.AsDouble();

            // Determine view family type to use
            ViewFamilyType viewFamilyType
              = new FilteredElementCollector(doc)
                .OfClass(typeof(ViewFamilyType))
                .Cast<ViewFamilyType>()
                .FirstOrDefault<ViewFamilyType>(x =>
                 ViewFamily.Section == x.ViewFamily);

            // Determine section box
            XYZ vecPoint = pointT - pointF;

            BoundingBoxXYZ  boundingBoxXYZ = el.get_BoundingBox(null);
            double minZ = boundingBoxXYZ.Min.Z;
            double maxZ = boundingBoxXYZ.Max.Z;

            double len = vecPoint.GetLength();
            double offset = 0.1 * len;

            var boxWidth = bodyWidth / 304.8;

            if (elOffset == null)
            {
                elOffset = 0;
            }

            XYZ min = new XYZ(-len, minZ - offset- (double)elOffset, -offset);
            XYZ max = new XYZ(len, maxZ + offset- (double)elOffset, boxWidth);

            XYZ midpoint = pointF + 0.5 * vecPoint;
            XYZ eldir = vecPoint.Normalize();
            XYZ up = XYZ.BasisZ;
            XYZ viewdir = eldir.CrossProduct(up);

            Transform transform = Transform.Identity;
            transform.Origin = midpoint;
            transform.BasisX = eldir;
            transform.BasisY = up;
            transform.BasisZ = viewdir;

            BoundingBoxXYZ sectionBox = new BoundingBoxXYZ();
            sectionBox.Transform = transform;
            sectionBox.Min = min;
            sectionBox.Max = max;

            // Create section view
            using (Transaction tx = new Transaction(doc))
            {
                tx.Start("Create Section View");

                var temp = ViewSection.CreateSection(doc, viewFamilyType.Id, sectionBox);
              
                tx.Commit();
            }
        }

关于SectionBox的方向:把两个点调换一下就可以翻转SectionBox的方向。

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值