1. RevitAPI 基础(上)

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

1. RevitAPI 基础(上)

会猫猫的喵喵   2020-01-29 我要评论

一 重要的基本设置:

1. 类库:revitAPI.DLL, revitAPIUI.DLL,个人理解前者包括了revit软件所特有的数据类型及软件中存在的全部后台数据,而后者是包含了大量与实现UI交互相关的接口,主要有IExternalCommand, IExternalApplication, Seletion选择功能, 菜单制作与任务对话框的制作

2. IExternalCommand:规定了外部命令扩展功能的接口供类(class)使用,只有一个抽象函数Execute,其有三个参数,commandData(输入参数,主要是Application, View, JournalData三种类型),message(输出参数,以string类为主,主要用来返回错误信息),elements(错误发生后高亮显示的元素)。

3.IExternalApplication:个人理解这个接口主要用于实现已有解决方案的拼接。这个接口本身定义了OnStartup和OnShutdown两个函数用于拼接已存在的dll以及定制UI,其不提供直接操作软件后台数据的方法。

4.IExternalDBApplication:这个口用于处理数据库级别的事件(即不涉及UI交互的后台数据增删改查),本质是3的一种特殊形式,其也通过上述两个方法拼接解决方案,但未提供定制UI的方法。

5.在继承IExternalCommand接口之前,必须定义事务Transaction的处理方式,[Autodesk.Revit.Attributes.Transaction(TransactionMode.Manual)]。除mannual外还有两个readonly与Automatic两种。

二  revit文档的获取(commandData, UIApplication, Application, DocumentSet, Documents, Document, ActiveDocument之间的关系)

commandData:revit后台数据的集合及相关的属性信息   UIApplication:后台数据的集合,只能通过Application方法引用,commandData和两种document都包含UIApplication。 UIDocument:个人理解其表示可交互的文档中包含信息的集合,并提供了通过不同UI交互过程提取信息的方法,如seletion交互获取文档,只能通过uiapplication进行引用。ActiveDocument:当前的活动文档,只能通过uidocument进行引用,是UIdocument的一部分。

Document:代表一个独立的工程文件,不一定是最顶级的。ActiveUIDocument.Document代表引用了当前活动文档。Documents代表后台中的全部文档,只能用Application.Documents进行引用。

真的太乱了!还是画图吧!其实最乱的就是Document和UIDocument,其主要区别在于Document中不能用seletion方法,因而其主要用过滤器过滤元素,而UIDocument中出现了大量有交互有关的方法。

 

 三 最后贴一段代码,主要是IExternalCommand的使用,遍历了用seletion选择的元素

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.Attributes;

namespace chapter2
{
    [Autodesk.Revit.Attributes.Transaction(TransactionMode.Manual)]
    public class class1 : IExternalCommand//让class1继承这个接口中的方法,即Execute
    {

        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            try
            {
                
                UIApplication uIApplication = commandData.Application;
                UIDocument uIDocument = uIApplication.ActiveUIDocument;
                Document document = uIDocument.Document;
                List<ElementId> selectedElem = new List<ElementId>();
                List<Element> walls = new List<Element>();
                List<Element> beams = new List<Element>();
                List<ElementId> beamid= new List<ElementId>();
                
                int countW = 0;int countBeam = 0;
                foreach ( var id in uIDocument.Selection.GetElementIds())
                {
                      Element element=uIDocument.Document.GetElement(id);
                    if(element is Wall)
                    {
                        selectedElem.Add(id);
                        walls.Add(element);
                        countW++;
                    }
                    if (element is BeamSystem)
                    {
                        beamid.Add(id);
                        beams.Add(element);
                        countBeam++;
                    }
                    
                }
                TaskDialog.Show("hello revit","模型中共有"+countW.ToString()+"个墙"+countBeam.ToString()+"个柱");
                foreach(var wall in walls)
                { TaskDialog.Show("墙的编号是",wall.Name);}

                TaskDialogResult taskDialogResult = TaskDialog.Show(
                    "revit",
                    "TES to continue," + "NO to cancel",
                    TaskDialogCommonButtons.Yes | TaskDialogCommonButtons.No);
                if(TaskDialogResult.Yes==taskDialogResult)
                {
                    return Result.Succeeded;
                }
                else if(TaskDialogResult.No == taskDialogResult)
                {
                    return Result.Cancelled;
                }
                else
                {
                    return Result.Failed;
                }
            }
            catch
            {
                message = "unexpected errors";
                return Result.Failed;
            }
            

            

            //throw new NotImplementedException();
        }
    }


}

 

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们