【Revit】 Revit二次开发针对墙创建剖切面

发布时间 2023-08-12 22:39:13作者: 梦琪小生

任何一个视图View,有三个方向,分别是ViewDirection(朝着观察者的方向),RightDirection(朝着屏幕的右边),UpDirection(朝着屏幕的上边)。

ViewSection.CreatSection方法,可以创建剖面,该方法中的第三个参数是一个BoundingBoxXYZ类型:sectionBox。

sectionBox.Transform.BasisZ的值指定了生成的剖面的ViewDirection;

sectionBox.Transform.BasisY的值指定了生成的剖面的UpDirection;

sectionBox.Transform.BasisX的值指定了生成的剖面的RightDirection;

直接上代码:

        /// <summary>
        /// 创建墙剖面图
        /// </summary>
        /// <param name="wall"></param>
        /// <param name="viewFamilyType"></param>
        /// <param name="centerloc">中间点的位置(单位英寸)</param>
        /// <param name="direction">方向(单位英寸)</param>
        /// <param name="depth">深度(单位英寸)</param>
        /// <returns></returns>
        private Element CreateWallSection(Wall wall, ViewFamilyType viewFamilyType, XYZ centerloc, XYZ direction, double depth = 5)
        {
            try
            {
                if (wall == null) return null;
                var line = ((wall.Location as LocationCurve).Curve as Autodesk.Revit.DB.Line);
                var length = line.Length / 2 + 0.5;
                var box = wall.get_BoundingBox(null);
                var min = new XYZ(-length, box.Min.Z, 0);
                var max = new XYZ(length, box.Max.Z, depth);
                var center = new XYZ(centerloc.X, centerloc.Y, (box.Min.Z + box.Max.Z) / 2);

                Transform transform = Transform.Identity;
                transform.Origin = center;
                transform.BasisX = -direction.CrossProduct(XYZ.BasisZ).Normalize();
                transform.BasisY = XYZ.BasisZ;
                transform.BasisZ = direction;
                var newbox = new BoundingBoxXYZ() { Min = min, Max = max, Transform = transform };

                ViewSection viewSection = null;
          //开事务
                TransactionUtils.InvokeTransaction(_doc, "创建墙剖面", () =>
                {
                    viewSection = ViewSection.CreateSection(_doc, viewFamilyType.Id, newbox);
                    viewSection.SetViewName("剖面_" + wall.Id.IntegerValue);
                    viewSection.CropBoxActive = true;
                    viewSection.DisplayStyle = DisplayStyle.ShadingWithEdges;
                    viewSection.DetailLevel = ViewDetailLevel.Fine;
                    viewSection.IsolateElementTemporary(wall.Id);
                });
                return viewSection;
            }
            catch
            { return null; }
        }

其中:

//筛选对应的类型,此处使用了自己写的扩展方法
ViewFamilyType viewFamilyType = _doc.WherePasses(new ElementClassFilter(typeof(ViewFamilyType))).FirstOrDefault(x => ViewFamily.Section == (x as ViewFamilyType).ViewFamily) as ViewFamilyType;