miki969696

revit api创建墙体剖面视图

using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using System;
using System.Linq;

namespace WallSectionCreator
{
    [Transaction(TransactionMode.Manual)]
    public class CreateWallSectionCommand : IExternalCommand
    {
        private Document _doc;

        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIApplication uiApp = commandData.Application;
            UIDocument uiDoc = uiApp.ActiveUIDocument;
            _doc = uiDoc.Document;

            try
            {
                // 让用户选择一个墙体
                Reference reference = uiDoc.Selection.PickObject(ObjectType.Element,
                    new WallSelectionFilter(), "请选择一个墙体");

                Wall selectedWall = _doc.GetElement(reference) as Wall;
                if (selectedWall == null)
                {
                    TaskDialog.Show("错误", "请选择一个有效的墙体");
                    return Result.Cancelled;
                }

                // 获取剖面视图类型
                ViewFamilyType viewFamilyType = GetSectionViewFamilyType();
                if (viewFamilyType == null)
                {
                    TaskDialog.Show("错误", "找不到可用的剖面视图类型");
                    return Result.Failed;
                }

                // 计算剖面创建所需的参数
                LocationCurve wallLocation = selectedWall.Location as LocationCurve;
                if (wallLocation == null)
                {
                    TaskDialog.Show("错误", "选择的墙体没有有效的位置曲线");
                    return Result.Failed;
                }

                Line wallLine = wallLocation.Curve as Line;
                if (wallLine == null)
                {
                    TaskDialog.Show("错误", "选择的墙体不是直线墙体");
                    return Result.Failed;
                }

                // 计算墙体中点作为剖面中心位置
                XYZ centerLoc = wallLine.Evaluate(0.5, true);
                // 剖面方向沿墙体长度方向
                XYZ direction = wallLine.Direction;

                // 创建墙剖面
                Element sectionView = CreateWallSection(selectedWall, viewFamilyType, centerLoc, direction);

                if (sectionView != null)
                {
                    TaskDialog.Show("成功", $"已为墙体 {selectedWall.Id.IntegerValue} 创建剖面视图");
                    uiDoc.ActiveView = sectionView as View;
                    return Result.Succeeded;
                }
                else
                {
                    TaskDialog.Show("失败", "创建剖面视图时发生错误");
                    return Result.Failed;
                }
            }
            catch (OperationCanceledException)
            {
                // 用户取消了选择
                return Result.Cancelled;
            }
            catch (Exception ex)
            {
                message = ex.Message;
                return Result.Failed;
            }
        }

        /// <summary>
        /// 创建墙剖面图
        /// </summary>
        private Element CreateWallSection(Wall wall, ViewFamilyType viewFamilyType, XYZ centerloc, XYZ direction, double depth = 5)
        {
            try
            {
                if (wall == null) return null;

                Line line = (wall.Location as LocationCurve).Curve as Line;
                double length = line.Length / 2 + 0.5;
                BoundingBoxXYZ box = wall.get_BoundingBox(null);

                XYZ min = new XYZ(-length, box.Min.Z, 0);
                XYZ max = new XYZ(length, box.Max.Z, depth);
                XYZ 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;

                // 执行事务创建剖面
                using (Transaction trans = new Transaction(_doc, "创建墙剖面"))
                {
                    trans.Start();

                    viewSection = ViewSection.CreateSection(_doc, viewFamilyType.Id, newbox);
                    if (viewSection != null)
                    {
                        viewSection.Name = "pmm1";
                        viewSection.CropBoxActive = true;
                        viewSection.DisplayStyle = DisplayStyle.ShadingWithEdges;
                        viewSection.DetailLevel = ViewDetailLevel.Fine;
                        viewSection.IsolateElementTemporary(wall.Id);
                    }

                    trans.Commit();
                }

                return viewSection;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 获取剖面视图类型
        /// </summary>
        private ViewFamilyType GetSectionViewFamilyType()
        {
            return new FilteredElementCollector(_doc)
                .OfClass(typeof(ViewFamilyType))
                .Cast<ViewFamilyType>()
                .FirstOrDefault(vft => vft.ViewFamily == ViewFamily.Section);
        }

        /// <summary>
        /// 墙体选择过滤器
        /// </summary>
        private class WallSelectionFilter : ISelectionFilter
        {
            public bool AllowElement(Element elem)
            {
                return elem is Wall;
            }

            public bool AllowReference(Reference reference, XYZ position)
            {
                return true;
            }
        }
    }
}

如果参数不对就会爆出这个错误,创建剖面视图时出错:The BoundingBoxXYZ is not appropriate for detail views. The basis vectors of must be unit length andorthonormal. The near and far bound offsets cannot be reversed or too close to each other. MinEnabled and MaxEnabled must be set to true for all three directions.Parameter name: sectionBox

image

posted on 2025-11-12 08:34  sswsswssw1996  阅读(2)  评论(0)    收藏  举报

导航