完善主體資料,免費(fèi)贈(zèng)送VIP會(huì)員!
* 主體類(lèi)型
* 企業(yè)名稱(chēng)
* 信用代碼
* 所在行業(yè)
* 企業(yè)規(guī)模
* 所在職位
* 姓名
* 所在行業(yè)
* 學(xué)歷
* 工作性質(zhì)
請(qǐng)先選擇行業(yè)
您還可以選擇以下福利:
行業(yè)福利,領(lǐng)完即止!

下載app免費(fèi)領(lǐng)取會(huì)員

NULL

ad.jpg

Revit技巧 | Revit二次開(kāi)發(fā)能干什么?Revit和Revit API的應(yīng)用詳解

發(fā)布于:2024-09-30 09:32:30

BIM中文網(wǎng)

更多

    文章來(lái)源:CSDN

image.png

    Revit和RevitAPI

        Revit是為建筑信息模型而設(shè)計(jì)的軟件,為建筑不同專(zhuān)業(yè)提供BIM解決方案。

        參數(shù)化:是指模型的所有元素之間的關(guān)系,這些關(guān)系可實(shí)現(xiàn)Revit提供的協(xié)調(diào)和變更管理功能。這些關(guān)系可以由軟件自動(dòng)創(chuàng)建,也可以由設(shè)計(jì)者在項(xiàng)目開(kāi)發(fā)期間創(chuàng)建。

    RevitAPI

        RevitAPI可以做什么

        訪問(wèn)模型的圖形數(shù)據(jù)。

        訪問(wèn)模型的參數(shù)數(shù)據(jù)。

        創(chuàng)建、修改、刪除模型元素。

        創(chuàng)建插件UI進(jìn)行增強(qiáng)。

        創(chuàng)建插件完成對(duì)重復(fù)自有工作的自動(dòng)化。

        集成第三個(gè)程序。

        執(zhí)行一切種類(lèi)的BIM分析。

        自動(dòng)創(chuàng)建項(xiàng)目文檔。

image.png

    1、第一個(gè)應(yīng)用程序HelloWorld

    1.anewproject

    選擇新建項(xiàng)目中的C#–類(lèi)庫(kù),建立項(xiàng)目名稱(chēng)為“HelloWorld”。

    2.AddReference

    1)在Revit安裝路徑中添加RevitAPI.dll。

    2)添加引用后,右鍵選擇RevitAPI.dll屬性,將復(fù)制本地種的true改為false。

    3)RevitAPIUI.dll按以上步驟添加。RevitprojectsusereferencesofRevitAPI.dll,RevitAPIUI.dllandRevitAddInUtility.dll

    3.AddCode

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;

        }

    }

}

    4.BuildtheProgram

    (1)a.addinmanifestfile

    在C#中新建一個(gè)文本文件,將代碼寫(xiě)入

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

 <RevitAddIns>

<Assembly>E:C#sampleHelloWorldHelloWorldinDebugHelloWorld.dll</Assembly>

<AddIn Type=”Command”>

//此處的HelloWorld.dll在項(xiàng)目生成后才會(huì)有,開(kāi)始看不到

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

 //此處的ID在解決方案管理器項(xiàng)目中Properties的AssemblyInfo.cs中可以查看

 <FullClassName>HelloWorld.Class1</FullClassName>

 //此處的HelloWorld為項(xiàng)目名,Class1為類(lèi)名

<Text>HelloWorld</Text>

<VendorId>ADSK</VendorId>

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

</AddIn>

</RevitAddIns>

    1.將文本格式保存在.addin格式,并保存在C:ProgramDataAutodeskRevitAddins2018(2018為目前revit的版本)

    注:C:ProgramData為隱藏文件,若要顯示,步驟如下:

    組織–文件夾和搜索選項(xiàng)–查看–顯示隱藏的文件、文件夾和驅(qū)動(dòng)器

    2.DebugtheAdd-in

    在解決方案資源管理器中,將項(xiàng)目的內(nèi)容展示出來(lái),點(diǎn)擊其中的Properties,窗口會(huì)出現(xiàn)。

    擊其中的調(diào)試(Debug),選擇啟動(dòng)外部程序,將revit.exe的路徑寫(xiě)出來(lái)即可。

    最后進(jìn)行調(diào)試,調(diào)試時(shí)會(huì)自動(dòng)開(kāi)啟revit軟件,在添加外部工具中可找到helloworld。

image.png

    2、外部命令和外部應(yīng)用

    RevitAPI.dll程序集包括了訪問(wèn)Revit中DB級(jí)別的Application、Document、Element、以及Parameter方法。RevitAPIUI.dll包括了所有操作和定制RevitUI的接口,包括了:IExternalCommand、IExternalApplication、ion、菜單RibbonPanel、RibbonItem、以及其子類(lèi)。想通過(guò)API來(lái)訪問(wèn)或擴(kuò)展Revit,需要用戶在自己的插件中實(shí)現(xiàn)特殊的接口,就是IExternalCommand、IExternalApplication、IExternalDBApplication。

    3、IExternalCommand外部命令

    (1)基本原理:如果Revit沒(méi)有其他命令在調(diào)用,或者是沒(méi)有處于編輯模式,ExternalCommand會(huì)被激活。一旦插件被選中,外部命令對(duì)象就會(huì)被創(chuàng)建出來(lái),并且執(zhí)行Execute函數(shù)。執(zhí)行完畢后,外部命令對(duì)象就被銷(xiāo)毀。在兩個(gè)命令之間數(shù)據(jù)不能保存在對(duì)象中,要通過(guò)其他方式來(lái)保存。

    (2)IExternalCommand,用戶通過(guò)外部命令來(lái)拓展功能的話,必須實(shí)現(xiàn)這個(gè)接口。重載Execute函數(shù)。作為外部命令的主函數(shù)來(lái)調(diào)用。

 //

    // 摘要:

    //     An interface that should be implemented to provide the implementation for a Revit

    //     add-in External Command.

    //

    // 備注:

    //     To add an external command to Autodesk Revit the developer should implement an

    //     object that supports the IExternalCommand interface.

    public interface IExternalCommand

    {

        //

        // 摘要:

        //     Overload this method to implement and external command within Revit.

        //

        // 參數(shù):

        //   commandData:

        //     An ExternalCommandData object which contains reference to Application and View

        //     needed by external command.

        //

        //   message:

        //     Error message can be returned by external command. This will be displayed only

        //     if the command status was "Failed". There is a limit of 1023 characters for this

        //     message; strings longer than this will be truncated.

        //

        //   elements:

        //     Element set indicating problem elements to display in the failure dialog. This

        //     will be used only if the command status was "Failed".

        //

        // 返回結(jié)果:

        //     The result indicates if the execution fails, succeeds, or was canceled by user.

        //     If it does not succeed, Revit will undo any changes made by the external command.

        Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements);

    }

    (3)輸入?yún)?shù)ExternalCommandData

    包含了外部命令所需要的Application以及一些視圖的引用。在外部命令中,所有的Revit的數(shù)據(jù)都可以通過(guò)這個(gè)參數(shù)直接或者間接的取到。

    UIApplicationuiApplication=revit.Application;

    Applicationapplication=uiApplication.Application;

    UIDcumentuiDocument=uiApplication.ActiveUIDocument;

    Documentdocument=uiDocument.Document;

    (4)輸出參數(shù)message

    外部命令可以通過(guò)這個(gè)參數(shù)返回執(zhí)行過(guò)程中的錯(cuò)誤信息。這個(gè)參數(shù)作用于整個(gè)外部命令的執(zhí)行過(guò)程,用戶可以在外部命令執(zhí)行過(guò)程中的任何時(shí)候給這個(gè)信息設(shè)值或者追加信息。當(dāng)外部命令的Execute函數(shù)返回Autodesk.Revit.UI.Result.Failed或者Autodesk.Revit.UI.Result.Canceled,這個(gè)錯(cuò)誤信息將被顯示在UI上。

    (5)輸出參數(shù)elements(ElementSet)

    當(dāng)外部命令返回Autodesk.Revit.UI.Result.Failed或者Autodesk.Revit.UI.Result.Canceled并且message參數(shù)不為空的時(shí)候,錯(cuò)誤或者警告對(duì)話框會(huì)彈出來(lái),點(diǎn)擊上面的顯示按鈕,elements參數(shù)中的元素將被高亮顯示。

    (6)Execute函數(shù)的返回值

    表示外部命令的執(zhí)行狀態(tài),有三種情況:Autodesk.Revit.UI.Result.Succeeded,Autodesk.Revit.UI.Result.Failed或者Autodesk.Revit.UI.Result.Canceled

    如果返回不是Succeeded,那么Revit會(huì)把外部命令所做的所有操作和修改都撤銷(xiāo)。

    // 摘要:

    // Informs Autodesk Revit of the status of your application after execution.

    public enum Result

    {

        //

        // 摘要:

        //     The external application was unable to complete its task.

        Failed = -1,

        //

        // 摘要:

        //     The external application completed successfully. Autodesk Revit will keep this

        //     object during the entire Revit session.

        Succeeded = 0,

        //

        // 摘要:

        //     Signifies that the external application is cancelled.

        Cancelled = 1

    }

    4、IExternalApplication外部應(yīng)用

    插件開(kāi)發(fā)者同樣可以通過(guò)IExternalApplication來(lái)添加自己的應(yīng)用,Revit同樣通過(guò).addin文件來(lái)識(shí)別和加載IExternalApplication的外部插件。

    IExternalApplication接口有兩個(gè)抽象函數(shù)OnStartup和OnShutdown。用戶可以通過(guò)在實(shí)現(xiàn)了IExternalApplication的外部應(yīng)用中重載OnStartup和OnShutdown函數(shù),在Revit啟動(dòng)和關(guān)閉的時(shí)候定制所需要的功能。

  //

    // 摘要:

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

    //

    // 備注:

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

    //     and rs to the session.

    public interface IExternalApplication

    {

        //

        // 摘要:

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

        //

        // 參數(shù):

        //   application:

        //     A handle to the application being shut down.

        //

        // 返回結(jié)果:

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

        Result OnShutdown(UIControlledApplication application);

        //

        // 摘要:

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

        //

        // 參數(shù):

        //   application:

        //     A handle to the application being started.

        //

        // 返回結(jié)果:

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

        Result OnStartup(UIControlledApplication application);

    }

    參數(shù)UIControlledApplication:提供了訪問(wèn)定制UI和注冊(cè)事件的方法。

    1.數(shù)據(jù)庫(kù)DB級(jí)別的外部應(yīng)用

    數(shù)據(jù)庫(kù)DB級(jí)別的外部應(yīng)用:它用于一般事件的處理。

   //

    // 摘要:

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

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

    //

    // 備注:

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

    //     session. They cannot or modify UI.

    public interface IExternalDBApplication

    {

        //

        // 摘要:

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

        //

        // 參數(shù):

        //   application:

        //     Handle to the Revit Application object.

        //

        // 返回結(jié)果:

        //     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.

        //

        // 參數(shù):

        //   application:

        //     Handle to the Revit Application object.

        //

        // 返回結(jié)果:

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

        //

        // 備注:

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

        ExternalDBApplicationResult OnStartup(ControlledApplication application);

    }

    6、addin文件

    (1)注冊(cè)

    windows7用戶,如果希望所有登錄用戶都可以使用的話,要把.addin文件放在C:ProgramDataAutodeskRevitAddins2018,三種方式的插件它的addin文件是有區(qū)別的。

    (2)外部命令的addin文件示例

    *****放置我們的開(kāi)發(fā)的插件.DLL的路徑;

    619fa21b-37c6-4efb-b886-91c24e30d13b從解決方案的AssemblyInfo.cs里面的[assembly:Guid("619fa21b-37c6-4efb-b886-91c24e30d13b")]獲取。

    <FullClassName>HelloWorld.Class1</FullClassName>類(lèi)的全名:命名空間.類(lèi)名。

    一個(gè).addin文件可以包含過(guò)個(gè)插件。

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

<RevitAddIns>

   <AddIn Type="Command">

     <Assembly>C:RevitProjectHelloWorldHelloWorldinDebugHelloWorld.dll</Assembly>

      <AddInId>619fa21b-37c6-4efb-b886-91c24e30d13b</AddInId>

      <FullClassName>HelloWorld.Class1</FullClassName>

      <Name>HelloWorld</Name>

      <VendorId>ADSK</VendorId>

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

   </AddIn>

<AddIn Type="Command">

   ....... 

   </AddIn>

<AddIn Type="Command">

   ....... 

   </AddIn>

</RevitAddIns>

    (3)外部應(yīng)用類(lèi)型的addin文件

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

<RevitAddIns>

   <AddIn Type="Application">

      <Assembly>C:Program FilesAutodeskRevit 2018RevitLookup.dll</Assembly>

      <ClientId>356CDA5A-E6C5-4c2f-A9EF-B3222116B8C8</ClientId>

      <FullClassName>RevitLookup.App</FullClassName>

      <Name>Revit Lookup</Name>

      <VendorId>ADSK</VendorId>

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

   </AddIn>

</RevitAddIns>

    (4)DB級(jí)別的外部應(yīng)用

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

<RevitAddIns>

   <AddIn Type="DBApplication">

      <Assembly>C:Program FilesAutodeskRevit 2018RevitLookup.dll</Assembly>

      <ClientId>356CDA5A-E6C5-4c2f-A9EF-B3222116B8C8</ClientId>

      <FullClassName>RevitLookup.App</FullClassName>

      <Name>Revit Lookup</Name>

      <VendorId>ADSK</VendorId>

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

   </AddIn>

</RevitAddIns>

    7、Transaction

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

    TransactionMode,控制RevitAPI框架如何在調(diào)用外部命令時(shí)處理其中的事務(wù)。

 public enum TransactionMode

    {

        //

        // 摘要:

        //     The API framework will not a transaction (but will an outer group

        //     to roll back all changes if the external command returns a failure status). Instead,

        //     you may use combinations of transactions, sub-transactions, and groups. You will

        //     have to follow all rules regarding use of transactions and related classes. You

        //     will have to give your transactions names, which will then appear in the undo

        //     menu. Revit will check that all transactions (also groups and sub-transaction)

        //     are properly closed upon return from an external command. If not, it will discard

        //     all changes to the model.

        Manual = 1,

        //

        // 摘要:

        //     No transaction (nor group) will be d, and no transaction may be d

        //     for the lifetime of the command. The External command may use methods that only

        //     read from the model, but not methods that write anything to it. Exceptions will

        //     be thrown if the command either tries to start a transaction (or group) or attempts

        //     to write to the model.

        //

        // 備注:

        //     A command with this transaction mode should not be associated to a command visible

        //     when there is no active document. The command will not be permitted to be started.

        ReadOnly = 2

    }


    Revit中文網(wǎng)作為國(guó)內(nèi)知名BIM軟件培訓(xùn)交流平臺(tái),幾十萬(wàn)Revit軟件愛(ài)好者和你一起學(xué)習(xí)Revit,不僅僅為廣大用戶提供相關(guān)BIM軟件下載與相關(guān)行業(yè)資訊,同時(shí)也有部分網(wǎng)絡(luò)培訓(xùn)與在線培訓(xùn),歡迎廣大用戶咨詢。

    網(wǎng)校包含各類(lèi)BIM課程320余套,各類(lèi)學(xué)習(xí)資源270余種,是目前國(guó)內(nèi)BIM類(lèi)網(wǎng)校中課程最有深度、涵蓋性最廣、資源最為齊全的網(wǎng)校。網(wǎng)校課程包含Revit、Dynamo、Lumion、Navisworks、Civil 3D等幾十種BIM相關(guān)軟件的教學(xué)課程,專(zhuān)業(yè)涵蓋土建、機(jī)電、裝飾、市政、后期、開(kāi)發(fā)等多個(gè)領(lǐng)域。

    需要更多BIM考試教程或BIM視頻教程,可以咨詢客服獲得更多免費(fèi)Revit教學(xué)視頻。

l  BIM培訓(xùn)網(wǎng)校課程目錄

l  Revit零基礎(chǔ)培訓(xùn)教程

l  BIM項(xiàng)目實(shí)戰(zhàn)訓(xùn)練營(yíng)



本文版權(quán)歸腿腿教學(xué)網(wǎng)及原創(chuàng)作者所有,未經(jīng)授權(quán),謝絕轉(zhuǎn)載。

未標(biāo)題-1.jpg

上一篇:Revit技巧 | Revit工作集怎么用?Revit中在中心模型中如何隱藏別人的工作集

下一篇:Revit技巧 | revit過(guò)濾規(guī)則怎么設(shè)置?如何使用dynamo批量生成過(guò)濾規(guī)則

60acb4e0ef112.png