C# Revit二次开发基础/核心编程---开发环境配置

一、本节课程
C# Revit二次开发基础/核心编程---开发环境配置

二、本节要讲解的知识点

如何配置Revit二次开发的环境,创建Hello World应用程序

  • 具体内容
  1. 安装Revit 2018:  AUTODESK官方网站,下载。https://www.autodesk.com.cn/products/revit/free-trial
  2. 安装VS2015及其更新:从微软的官方网站,下载。

https://visualstudio.microsoft.com/zh-hans/vs/older-downloads/

VS的更新 http://download.microsoft.com/download/9/4/f/94f9f500-fe47-4a4e-be00-85350e2cf87a/vs2015.3%20rc.exe

  1. Autodesk官方的Revit API下载:

https://www.autodesk.com/developer-network/platform-technologies/revit

2018版本的更新一点SDK

http://download.autodesk.com/us/revit-sdk/REVIT_2018_2_SDK.msi

 DocumentationLearn more about Revit's .NET API through Revit's API

The SDK is included in every Revit product. There are two ways to install the Revit SDK:

  • From the main page of the Revit installer, click “Install Tools and Utilities” and choose “Revit Software Development Kit”.
  • Alternatively, you can also find the SDK in the extraction folder, under: <extraction folder>\support\SDK\RevitSDK.exe

You can also download the updated Revit SDK here:

The Revit LookUp Tool can help debug the Revit Database and understand the elements and its parameters. The source code can be found at our GitHub repository.

https://github.com/jeremytammik/RevitLookup

 

4revit2018的addinmanager安装

Revit二次开发 _AddInManager 安装步骤:

1已装Revit2018

2安装Revit SDK2018

3打开SDK2018 文件夹中的 Add-In Manager,找到 Autodesk.AddInManager.addin 文件,然后复制该文件到Revit 2018安装文件Addins中。

 

  需要将.addin文件放置到C:\ProgramData\Autodesk\Revit\Addins\2018,这是一个隐藏文件夹,如果没有显示的话可以使用文件夹选项的“设置”将隐藏文件夹显示。

 

 

4用记事本(或用编程软件)打开复制过的 Autodesk.AddInManager.addin 文件。

然后修改下图红色框内的内容,修改为:自己SDK2018中Add-In Manager文件安装位置(下图已修改好)。

 

5打开Revit2018 ,查看

 

5revit2018的revitlookup安装

(1)参照“3”下载revitlookup的源代码文件,并解压。

 

 

(2)编译生成revitlookup.dll文件:用VS2015打开源代码后,首先需要重新连接引用的Revit的DLL,如下图,配置一下属性,生成DLL。

 

(3)revitlookup.dll放到revit的安装路径;

(4)再将revitlookup.addin放到C:\ProgramData\Autodesk\Revit\Addins\2018下;

(5)最后将addin文件的<Assembly>处的路径改为.dll所在处的路径,并保存;重新启动revit2018即可。

 

  1. 运行Revit软件

运行Revit软件后,Revit会自动添加插件。可能有安全性提示,点击总是载入即可。

 

加载后的界面如图所示:

 

 

 

  1. Hello World
  1. Create a new project 
    选择新建项目中的C#–类库,建立项目名称为“HelloWorld”。 
    2Add Reference 
    1)在Revit安装路径中添加RevitAPI.dll。 
    2)添加引用后,右键选择
    RevitAPI.dll属性,将复制本地种的true改为false。 
    3)
    RevitAPIUI.dll 按以上步骤添加。 


3Add Code

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Autodesk.Revit.UI;

using Autodesk.Revit.DB;

 

namespace HelloWorld

{ [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]

    public class Class1 : IExternalCommand

    {

        public Autodesk.Revit.UI.Result Execute(ExternalCommandData revit, ref string message, ElementSet elements)

        {

            TaskDialog.Show("Revit", "Hello World");

            return Autodesk.Revit.UI.Result.Succeeded;

        }

    }

}

  1. Build the Program 
    5Create a .addin manifest file 
    在C#中新建一个文本文件,将代码写入

<?xml version="1.0" encoding="utf-8" standalone="no"?>

 <RevitAddIns>

<Assembly>E:\C#\sample\HelloWorld\HelloWorld\bin\Debug\HelloWorld.dll</Assembly>

<AddIn Type=”Command”>

//此处的HelloWorld.dll在项目生成后才会有,开始看不到

<AddInId>239BD853-36E4-461f-9171-C5ACEDA4E721</AddInId>

 //此处的ID在解决方案管理器项目中Properties的AssemblyInfo.cs中可以查看

 <FullClassName>HelloWorld.Class1</FullClassName>

 //此处的HelloWorld为项目名,Class1为类名

<Text>HelloWorld</Text>

<VendorId>ADSK</VendorId>

<VendorDescription>Autodesk, www.autodesk.com</VendorDescription>

</AddIn>

</RevitAddIns>

  1. 将文本格式保存在.addin格式,并保存在C:\ProgramData\Autodesk\Revit\Addins\2018(2018为目前revit的版本) 
    注:C:\ProgramData为隐藏文件,若要显示,步骤如下: 
    组织–文件夹和搜索选项–查看–显示隐藏的文件、文件夹和驱动器 
  2. Debug the Add-in 
    在解决方案资源管理器中,将项目的内容展示出来,点击其中的Properties,窗口会出现。 

 

击其中的调试(Debug),选择启动外部程序,将revit.exe的路径写出来即可。

最后进行调试,调试时会自动开启revit软件,在添加外部工具中可找到helloworld。 

 

四、总结

(一)  public Autodesk.Revit.UI.Result Execute(ExternalCommandData revit, ref string message, ElementSet elements)

        {

            TaskDialog.Show("Revit", "Hello World");

            return Autodesk.Revit.UI.Result.Succeeded;

        }

  1. 输入参数 ExternalCommandData :包含了外部命令所需要的Application以及一些视图的引用。在外部命令中,所有的Revit的数据都可以通过这个参数直接或者间接的取到。

UIApplication uiApplication =revit.Application;

Application application=uiApplication.Application;

UIDcument uiDocument=uiApplication.ActiveUIDocument;

Document document=uiDocument.Document;

 

  1. 输出参数message

外部命令可以通过这个参数返回执行过程中的错误信息。这个参数作用于整个外部命令的执行过程,用户可以在外部命令执行过程中的任何时候给这个信息设值或者追加信息。当外部命令的Execute函数返回Autodesk.Revit.UI.Result.Failed或者Autodesk.Revit.UI.Result.Canceled,这个错误信息将被显示在UI上。

 

  1. 输出参数elements(ElementSet)

当外部命令返回Autodesk.Revit.UI.Result.Failed或者Autodesk.Revit.UI.Result.Canceled并且message参数不为空的时候,错误或者警告对话框会弹出来,点击上面的显示按钮,elements参数中的元素将被高亮显示。

4、返回值 Execute函数的返回值表示外部命令的执行状态,有三种情况:Autodesk.Revit.UI.Result.Succeeded,Autodesk.Revit.UI.Result.Failed或者Autodesk.Revit.UI.Result.Canceled

如果返回不是Succeeded ,那么Revit会把外部命令所做的所有操作和修改都撤销。

(二)插件开发者同样可以通过IExternalApplication来添加自己的应用,Revit同样通过.addin文件来识别和加载IExternalApplication的外部插件。

IExternalApplication接口有两个抽象函数OnStartup和OnShutdown。用户可以通过在实现了IEcternalApplication的外部应用中重载OnStartup和OnShutdown函数,在Revit启动和关闭的时候定制所需要的功能。

  //

    // 摘要:

    //     An interface that supports addition of external applications to Revit.

    //

    // 备注:

    //     External applications are permitted to customize the Revit UI, and to add events

    //     and updaters to the session.

    public interface IExternalApplication

    {

        //

        // 摘要:

        //     Implement this method to execute some tasks when Autodesk Revit shuts down.

        //

        // 参数:

        //   application:

        //     A handle to the application being shut down.

        //

        // 返回结果:

        //     Indicates if the external application completes its work successfully.

        Result OnShutdown(UIControlledApplication application);

        //

        // 摘要:

        //     Implement this method to execute some tasks when Autodesk Revit starts.

        //

        // 参数:

        //   application:

        //     A handle to the application being started.

        //

        // 返回结果:

        //     Indicates if the external application completes its work successfully.

        Result OnStartup(UIControlledApplication application);

    }

 

  • 数据库DB级别的外部应用

   //

    // 摘要:

    //     An interface that supports addition of DB-level external applications to Revit,

    //     to subscribe to DB-level events and updaters.

    //

    // 备注:

    //     DB-level applications are permitted to add DB-level events and updaters to the

    //     session. They cannot create or modify UI.

    public interface IExternalDBApplication

    {

        //

        // 摘要:

        //     Implement this method to execute some tasks when Autodesk Revit shuts down.

        //

        // 参数:

        //   application:

        //     Handle to the Revit Application object.

        //

        // 返回结果:

        //     Indicates if the external db application completes its work successfully.

        ExternalDBApplicationResult OnShutdown(ControlledApplication application);

        //

        // 摘要:

        //     Implement this method to execute some tasks when Autodesk Revit starts.

        //

        // 参数:

        //   application:

        //     Handle to the Revit Application object.

        //

        // 返回结果:

        //     Indicates if the external db application completes its work successfully.

        //

        // 备注:

        //     Typically, event handlers and updaters are registered in this method.

        ExternalDBApplicationResult OnStartup(ControlledApplication application);

    }

 

猜你喜欢

转载自blog.csdn.net/yunyouxy/article/details/81255764