• Revit二次开发——视图


    视图

    视图是从Revit模型产生的图像,其对文件中存储的数据有特许访问权。它们可以是图形,如平面图;或文本,如明细表。每个项目文件都有一个或多个不同视图。最后一个焦点窗口是活动视图ActiveView。每个视图的生成都是从三维对象投影到二维投影面的过程。
    Autodesk. Revit.DB. View类是Revit文件中所有视图类型的基类。Autodesk. Revit.Ul.UIView类表示在Revit用户界面中的窗口视图。

    //获得当前视图id
    ElementId viewId = uidoc.ActiveView.Id;
    

    视图类型

    Autodesk.Revit.DB.View类是Revit文件中所有视图类型的基类;Autodesk.Revit.UI.UIView类表示在Revit用户界面中的窗口视图。

    常见视图类型

    视图视图类型视图类
    三维视图ViewType.ThreeDView3D
    天花板平面视图ViewType.CeilingPlanViewPlan
    面积平面视图ViewType.AreaPlanViewPlan
    剖面视图ViewType.SectionViewSection
    图纸视图ViewType.DrawingSheetViewSheet
    明细表视图ViewType.ScheduleViewSchedule
    //获取视图类型
    //Autodesk.Revit.DB.View view = GetView();
    //两种判断视图类型的方法:
    //第一种:
    ViewType viewType = view.ViewType;
    switch (viewType)
    {
     case Autodesk.Revit.DB.ViewType.ThreeD:
      // 视图类型是三维视图
      break;
     // 其他类型
    }
    
    // 第二种:
    if (view is View3D)
    {
     // view的类类型是三维视图
    }
    
    public void GetViewType(Autodesk.Revit.DB.View view)
    {
        // Get the view type of the given view, and format the prompt string
        String prompt = "The view is ";
    
        switch (view.ViewType)
        {
            case ViewType.AreaPlan:
                prompt += "an area view.";
                break;
            case ViewType.CeilingPlan:
                prompt += "a reflected ceiling plan view.";
                break;
            case ViewType.ColumnSchedule:
                prompt += "a column schedule view.";
                break;
            case ViewType.CostReport:
                prompt += "a cost report view.";
                break;
            case ViewType.Detail:
                prompt += "a detail view.";
                break;
            case ViewType.DraftingView:
                prompt += "a drafting view.";
                break;
            case ViewType.DrawingSheet:
                prompt += "a drawing sheet view.";
                break;
            case ViewType.Elevation:
                prompt += "an elevation view.";
                break;
            case ViewType.EngineeringPlan:
                prompt += "an engineering view.";
                break;
            case ViewType.FloorPlan:
                prompt += "afloor plan view.";
                break;
            case ViewType.Internal:
                prompt += "Revit's internal view.";
                break;
            case ViewType.Legend:
                prompt += "a legend view.";
                break;
            case ViewType.LoadsReport:
                prompt += "a loads report view.";
                break;
            case ViewType.PanelSchedule:
                prompt += "a panel schedule view.";
                break;
            case ViewType.PresureLossReport:
                prompt += "a pressure loss report view.";
                break;
            case ViewType.Rendering:
                prompt += "a rendering view.";
                break;
            case ViewType.Report:
                prompt += "a report view.";
                break;
            case ViewType.Schedule:
                prompt += "a schedule view.";
                break;
            case ViewType.Section:
                prompt += "a cross section view.";
                break;
            case ViewType.ThreeD:
                prompt += "a 3-D view.";
                break;
            case ViewType.Undefined:
                prompt += "an undefined/unspecified view.";
                break;
            case ViewType.Walkthrough:
                prompt += "a walkthrough view.";
                break;
            default:
                break;
        }
    
        // Give the user some information
        TaskDialog.Show("Revit",prompt);
    }
    

    视图的方法

    新建视图

    每个视图类都有相应的函数来创建视图对象

    例如:

    public static ViewPlan Create(Document document, ElementId viewFamilyTypeId, ElementId levelId);
    public static ViewPlan CreateAreaPlan(Document document, ElementId areaSchemeId, ElementId levelId);
    
    参数类型说明
    documentType: Autodesk.Revit.DB.DocumentViewPlan将被添加到其中的文档。
    viewFamilyTypeIdType: Autodesk.Revit.DB.ElementIdViewFamilyType的id,它将被新的ViewPlan使用。类型需要是楼板平面FloorPlan, 天花板平面CeilingPlan,面积平面 AreaPlan,或结构平面StructuralPlan视图类型。
    levelIdType: Autodesk.Revit.DB.ElementId与新视图关联的标高id。
    public Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
            {
                try
                {
                    Document doc = commandData.Application.ActiveUIDocument.Document;
                    Transaction tran = new Transaction(doc);
                    tran.Start("创建标高");
                    CreateViewPlane(doc, GetAllLevels(doc).Last()).Name = "新建楼层平面";
                    tran.Commit();
                    return Autodesk.Revit.UI.Result.Succeeded;
                }
                catch (Exception ex)
                {
                    message = ex.Message;
                    return Autodesk.Revit.UI.Result.Failed;
                }
            }
            private ViewPlan CreateViewPlane(Document doc, Level level)
            {
                List<ViewPlan> allFloorPlanes = new FilteredElementCollector(doc)
                    .OfCategory(BuiltInCategory.OST_Views)
                    .OfClass(typeof(ViewPlan))
                    .WhereElementIsNotElementType().OfType<ViewPlan>()
                    .ToList();
                foreach (ViewPlan view in allFloorPlanes)
                {
                    if (view.ViewType == ViewType.FloorPlan && view.GenLevel != null && view.GenLevel.Id == level.Id)
                    {
                        doc.Delete(view.Id);
                        break;
                    }
                }
                doc.Regenerate();
                return ViewPlan.Create(doc, doc.GetDefaultElementTypeId(ElementTypeGroup.ViewTypeFloorPlan), level.Id);
            }
    
            private IList<Level> GetAllLevels(Document doc)
            {
                return new FilteredElementCollector(doc)
                     .OfCategory(BuiltInCategory.OST_Levels)
                     .OfClass(typeof(Level))
                     .ToElements().OfType<Level>().ToList();
            }
    

    要删除一个视图可以使用Document. Delete方法,传入一个视图的ID,doc.Delete(view.Id);。也可以通过删除该视图所依赖的对象来间接删除该视图,例如,删除标高,相应的平面视图也会被删除,或者删除相机,相应的三维视图也会被删除。

    隐藏注释

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Autodesk.Revit.DB;
    using Autodesk.Revit.UI;
    namespace ViewBasicDemo
    {
    
        [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
        public class HiddenAnnotatoinCmd : IExternalCommand
        {
            public Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
            {
                try
                {
                    View activeView = commandData.Application.ActiveUIDocument.ActiveView;
                    HiddenAnnotaionElements(activeView);
    
                    return Autodesk.Revit.UI.Result.Succeeded;
                }
                catch (Exception ex)
                {
                    message = ex.Message;
                    return Autodesk.Revit.UI.Result.Failed;
                }
            }
    
            private void HiddenAnnotaionElements(View view)
            {
                Transaction tran = new Transaction(view.Document);
                tran.Start("隐藏注释");
                view.AreAnnotationCategoriesHidden = true;
                tran.Commit();
            }
        }
    }
    
    private void Getinfo_View(Autodesk.Revit.DB.View view)
    {
        string message = "View: ";
    
        //get the name of the view
        message += "\nView name: " + view.Name;
    
        //The crop box sets display bounds of the view
        BoundingBoxXYZ cropBox = view.CropBox;
        XYZ max = cropBox.Max; //Maximum coordinates (upper-right-front corner of the box).
        XYZ min = cropBox.Min; //Minimum coordinates (lower-left-rear corner of the box).
        message += "\nCrop Box: ";
        message += "\nMaximum coordinates: (" + max.X + ", " + max.Y + ", " + max.Z + ")";
        message += "\nMinimum coordinates: (" + min.X + ", " + min.Y + ", " + min.Z + ")";
    
    
       //get the origin of the screen
        XYZ origin = view.Origin;
        message += "\nOrigin: (" + origin.X + ", " + origin.Y + ", " + origin.Z + ")";
    
    
        //The bounds of the view in paper space (in inches).
        BoundingBoxUV outline = view.Outline;
        UV maxUv = outline.Max; //Maximum coordinates (upper-right corner of the box).
        UV minUv = outline.Min; //Minimum coordinates (lower-left corner of the box).
        message += "\nOutline: ";
        message += "\nMaximum coordinates: (" + maxUv.U + ", " + maxUv.V + ")";
        message += "\nMinimum coordinates: (" + minUv.U + ", " + minUv.V + ")";
    
        //The direction towards the right side of the screen
        XYZ rightDirection = view.RightDirection;
        message += "\nRight direction: (" + rightDirection.X + ", " +
                       rightDirection.Y + ", " + rightDirection.Z + ")";
    
        //The direction towards the top of the screen
        XYZ upDirection = view.UpDirection;
        message += "\nUp direction: (" + upDirection.X + ", " +
                       upDirection.Y + ", " + upDirection.Z + ")";
    
        //The direction towards the viewer
        XYZ viewDirection = view.ViewDirection;
        message += "\nView direction: (" + viewDirection.X + ", " +
                       viewDirection.Y + ", " + viewDirection.Z + ")";
    
        //The scale of the view
        message += "\nScale: " + view.Scale;
        // Scale is meaningless for Schedules
        if (view.ViewType != ViewType.Schedule)
        {
            int testScale = 5;
            //set the scale of the view
            view.Scale = testScale;
            message += "\nScale after set: " + view.Scale;
        }
    
        TaskDialog.Show("Revit",message);
    }
    

    参考

    //============代码片段8-2:获取视图中可见的元素============
    //找到视图中所有可见的元素
    FilteredElementCollector elemCollector = new FilteredElementCollector(document, viewId);
    foreach (Element elem in elemCollector)
    {
        //操作元素elem
    }
    
    //============代码片段8-3:创建一个正交三维视图============
    private void CreateView3D(Autodesk.Revit.Document doc, ElementId viewTypeId)
    {
      try
      {
       // Create a new View3D
       XYZ direction = new XYZ(1, 1, 1);
       View3D view3D = View3D.CreateIsometric(doc, viewTypeId);
       if (null == view3D)
        return;
    
       // The created View3D isn't perspective.
       Debug.Assert(false == view3D.IsPerspective);
      }
      catch (Exception e)
      {
       Debug.WriteLine(e.ToString());
      }
    }
    
    //============代码片段8-4:显示剖面框============
    private void ShowHideSectionBox(View3D view3D)
    {
      view3D.IsSectionBoxActive = true;
    }
    
    //============代码片段8-5:创建楼层平面和天花板平面============
    private void CreatePlanView(Autodesk.Revit.Document doc, ElementId viewTypeId)
    {
      try
      {
       using(Transaction tr = new Transaction(doc))
       {
        tr.Start(“创建平面视图”);
        double elevation = 10.0;
        Level level1 = doc.Create.NewLevel(elevation);
        ViewPlan floorView = ViewPlan.Create(doc, viewTypeId, level1.Id);
        tr.Commit();
       }
      }
      catch (Exception exp)
      {
       MessageBox.Show(exp.ToString());
      }
    }
    
    //============代码片段8-6:创建和打印一个图纸视图============
    private void CreateSheetView(Autodesk.Revit.Document doc)
    {
      // Get an available title block from document
      FamilySymbolSet fsSet = doc.TitleBlocks;
      if (fsSet.Size == 0)
      {
       MessageBox.Show("No title blocks");
       return;
      }
    
      FamilySymbol fs = null;
      foreach (FamilySymbol f in fsSet)
      {
       if (null != f)
       {
        fs = f;
        break;
       }
      }
    
      try
      {
       // Create a sheet view
       ViewSheet viewSheet = ViewSheet.Create(doc, fs);
       if (null == viewSheet)
        return;
    
       // Add current view onto the center of the sheet
       UV location = new UV(
         (viewSheet.Outline.Max.U - viewSheet.Outline.Min.U) / 2,
         (viewSheet.Outline.Max.V - viewSheet.Outline.Min.V) / 2);
    
       XYZ point = new XYZ(UV.U, UV.V, 0);
       Viewport.Create(doc, viewSheep.Id, doc.ActiveView.Id, point);
    
       // Print the sheet out
       if (viewSheet.CanBePrinted)
       {
        if (MessageBox.Show("Print the sheet?", "Revit",
    MessageBoxButtons.YesNo) == DialogResult.Yes)
           viewSheet.Print();
       }
      }
      catch (Exception e)
      {
       MessageBox.Show(e.ToString());
      }
    }
    
  • 相关阅读:
    OA项目之我的审批(查询&会议签字)
    redis内存描述
    egg中使用Sequelize老报错?看了这篇相信你会有思路……
    hexo发生错误 Error: Spawn failed
    LLVM系列第十七章:控制流语句for
    windows版:TensorRT安装教程
    docker镜像详解
    YOLOv7魔改,YOLOv7改进之PPSPPCSPC助力涨点
    24点问题(带输出构造方式)
    【Matplotlib绘制图像大全】(十五):Matplotlib绘制误差曲线
  • 原文地址:https://blog.csdn.net/weixin_44037272/article/details/126460463