{"id":953,"date":"2025-01-20T11:54:12","date_gmt":"2025-01-20T14:54:12","guid":{"rendered":"https:\/\/ricaun.com\/?p=953"},"modified":"2025-01-20T18:51:26","modified_gmt":"2025-01-20T21:51:26","slug":"revit-addin-context","status":"publish","type":"post","link":"https:\/\/ricaun.com\/br\/revit-addin-context\/","title":{"rendered":"Revit AddIn Context"},"content":{"rendered":"<p>Last year I shared how to figure out if Revit is in the API Context using events to trigger some exception, Revit only allow to add a new event in <strong>Idling <\/strong>or other Revit API event when Revit is in context.<\/p>\n\n\n\n<p>Now I find out a easier and faster way using the <strong>ActiveAddInId<\/strong>, and better explain what the Revit API context means.<\/p>\n\n\n\n<p>Like the add events in Revit API the <strong>ActiveAddInId<\/strong> is only available when Revit is in context, if is not in context the <strong>ActiveAddInId<\/strong> returns null.<\/p>\n\n\n\n<p>Meaning Revit tracks the Revit AddIn Context with the propriety <strong>ActiveAddInId<\/strong>. <\/p>\n\n\n\n<p>And every time your code add\/remove a Revit API event, create a push button, create an <strong>ExternalEvent<\/strong>, or interact with Revit API in some way, Revit verify what AddIn is executing that code to update the <strong>ActiveAddInId<\/strong> when the event is trigger, or the push button, or <strong>ExternalEvent<\/strong>, and so on.<\/p>\n\n\n\n<p>For me <strong>ActiveAddInId<\/strong> explain very well how Revit works, you need to have a valid <strong>AddInId<\/strong> to execute Revit API code, and yes, is the same AddIn the user need to allow when an AddIn is installed and Revit first open showing Security popup.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"526\" src=\"https:\/\/ricaun.com\/wp-content\/uploads\/2025\/01\/Security-Signed-Add-in-ricaun-Ltda-1024x526.png\" alt=\"\" class=\"wp-image-960\" srcset=\"https:\/\/ricaun.com\/wp-content\/uploads\/2025\/01\/Security-Signed-Add-in-ricaun-Ltda-1024x526.png 1024w, https:\/\/ricaun.com\/wp-content\/uploads\/2025\/01\/Security-Signed-Add-in-ricaun-Ltda-300x154.png 300w, https:\/\/ricaun.com\/wp-content\/uploads\/2025\/01\/Security-Signed-Add-in-ricaun-Ltda-768x394.png 768w, https:\/\/ricaun.com\/wp-content\/uploads\/2025\/01\/Security-Signed-Add-in-ricaun-Ltda-18x9.png 18w, https:\/\/ricaun.com\/wp-content\/uploads\/2025\/01\/Security-Signed-Add-in-ricaun-Ltda.png 1204w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">AddIn Context<\/h4>\n\n\n\n<p>The video below shows some explanation how <strong>ActiveAddInId<\/strong> is used inside Revit API to track what AddIn is executing in the moment a Revit API method is executed, an invalid <strong>ActiveAddInId<\/strong> could generate an exception and some methods really require the same <strong>AddInId<\/strong> to work without issue.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<div class=\"nv-iframe-embed\"><iframe loading=\"lazy\" title=\"ActiveAddInId == AddIn Context - Revit API\" width=\"1200\" height=\"675\" src=\"https:\/\/www.youtube.com\/embed\/bQUFxfSMI6E?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/div>\n<\/div><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>To check if Revit is in AddIn Context the code below could be used.<\/p>\n\n\n<p><script src=\"https:\/\/gist.github.com\/ricaun\/a491da0efa658664c62a9b21a7e75dba.js\"><\/script><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Design Automation for Revit<\/h4>\n\n\n\n<p>The main reason I decide to look closer in the propriety <strong>ActiveAddInId<\/strong> was an issue inside Design Automation: <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/forums.autodesk.com\/t5\/revit-api-forum\/revit-design-automation-extensible-storage-quot-writing-of\/m-p\/12835940\/highlight\/true#M79509\" target=\"_blank\" rel=\"noopener\" title=\"\">Revit Design Automation | Extensible Storage &#8220;Writing of Entities of this Schema is not allowed to the current add-in.&#8221; Error<\/a><\/li>\n<\/ul>\n\n\n\n<p>The issue is related with Extensible Storage, and the <a href=\"https:\/\/www.revitapidocs.com\/2019\/9817e7db-8367-ea4e-1769-0488f3faa37f.htm\" target=\"_blank\" rel=\"noreferrer noopener\">Schema<\/a> have this two method <a href=\"https:\/\/www.revitapidocs.com\/2019\/e691bb52-66a1-96fd-274d-8ae3f30e5c0c.htm\" target=\"_blank\" rel=\"noreferrer noopener\">ReadAccessGranted<\/a> and <a href=\"https:\/\/www.revitapidocs.com\/2019\/3f089f69-3a06-cff9-dbc7-a2ed58192f85.htm\" target=\"_blank\" rel=\"noreferrer noopener\">WriteAccessGranted<\/a>, and if the AccessLevel was configured to <strong>Vendor<\/strong> or <strong>Application<\/strong>, Design Automation fails.<\/p>\n\n\n\n<p>Design Automation for Revit uses a custom library to execute the event <strong>DesignAutomationReadyEvent<\/strong>, that event is executed when Design Automation is ready to execute. Because is not an official event inside Revit API the event never tracks the <strong>ActiveAddInId<\/strong> and when the event is executed the <strong>ActiveAddInId<\/strong> is invalid and return null.<\/p>\n\n\n\n<p>I created a RevitTest project to NUnit test the Extensible Storage with different AccessLevel.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/github.com\/ricaun-io\/ricaun.Revit.DA\" target=\"_blank\" rel=\"noopener\" title=\"\">https:\/\/github.com\/ricaun-io\/ricaun.Revit.DA<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/github.com\/ricaun-io\/RevitTestStorage.Tests\" target=\"_blank\" rel=\"noopener\" title=\"\">https:\/\/github.com\/ricaun-io\/RevitTestStorage.Tests<\/a><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">How to Fix ActiveAddInId in DA4R<\/h4>\n\n\n\n<p>To fix <strong>ActiveAddInId<\/strong> in DA4R the best approach I found was to register a custom ExternalService in the OnStartup and in the <strong>DesignAutomationReadyEvent<\/strong> event I can request and execute the ExternalService. <\/p>\n\n\n\n<p>ExternalService exists in the main Revit API and tracks the <strong>ActiveAddInId<\/strong>, and every time the ExternalService executes the same <strong>ActiveAddInId<\/strong> owner is apply in the service.<\/p>\n\n\n\n<p>The ExternalService is a little advanced topic, here some resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/thebuildingcoder.typepad.com\/blog\/2015\/12\/external-services.html#6\" target=\"_blank\" rel=\"noopener\" title=\"\">https:\/\/thebuildingcoder.typepad.com\/blog\/2015\/12\/external-services.html#6<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/thebuildingcoder.typepad.com\/files\/sd10752_revit_external_services_arnost_lobel_handout.pdf\" target=\"_blank\" rel=\"noopener\" title=\"\">https:\/\/thebuildingcoder.typepad.com\/files\/sd10752_revit_external_services_arnost_lobel_handout.pdf<\/a><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">ricaun.Revit.DA<\/h4>\n\n\n\n<p>I&#8217;m planing to create a library to fix two problems inside DA4R.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/github.com\/ricaun-io\/RevitAddin.DA.Tester\/issues\">https:\/\/github.com\/ricaun-io\/RevitAddin.DA.Tester\/issues<\/a><\/li>\n<\/ul>\n\n\n\n<p>The library would simplify and force the <strong>DesignAutomationReadyEvent<\/strong> to execute a custom ExternalService to force the code to run with a valid <strong>ActiveAddInId<\/strong>.<\/p>\n\n\n\n<p>And force to reload the correct &#8216;.addin&#8217; inside the &#8216;PackageContents.xml&#8217; in the bundle configuration, at the moment DA4R load the first AddIn in the configuration and ignore the version. <\/p>\n\n\n\n<p>Stay tuned for a video in <a href=\"https:\/\/ricaun.com\/youtube\/\" target=\"_blank\" rel=\"noopener\" title=\"\">youtube<\/a> about that library. <\/p>\n\n\n\n<h4 class=\"wp-block-heading\">References<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/forums.autodesk.com\/t5\/revit-api-forum\/revit-design-automation-extensible-storage-quot-writing-of\/m-p\/12835940\/highlight\/true#M79509\" target=\"_blank\" rel=\"noopener\" title=\"\">Revit API Forum &#8211; Design Automation issue Extensible Storage<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/forums.autodesk.com\/t5\/revit-api-forum\/how-to-know-if-revit-api-is-in-context\/m-p\/13012476\/highlight\/true#M81395\" target=\"_blank\" rel=\"noopener\" title=\"\">Revit API Forum &#8211; How to know if Revit API is in Context.<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/github.com\/ricaun-io\/ricaun.Revit.DA\" target=\"_blank\" rel=\"noopener\" title=\"\">GitHub &#8211; ricaun.Revit.DA<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/github.com\/ricaun-io\/RevitAddin.DA.Tester\" target=\"_blank\" rel=\"noopener\" title=\"\">GitHub &#8211; RevitAddin.DA.Tester<\/a><\/li>\n<\/ul>\n\n\n\n<p><\/p>","protected":false},"excerpt":{"rendered":"<p>Last year I shared how to figure out if Revit is in the API Context using events to trigger some exception, Revit only allow to add a new event in Idling or other Revit API event when Revit is in context. Now I find out a easier and faster way using the ActiveAddInId, and better&hellip;&nbsp;<a href=\"https:\/\/ricaun.com\/br\/revit-addin-context\/\" class=\"\" rel=\"bookmark\">Continue a ler &raquo;<span class=\"screen-reader-text\">Revit AddIn Context<\/span><\/a><\/p>","protected":false},"author":1,"featured_media":971,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","_ti_tpc_template_sync":false,"_ti_tpc_template_id":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-953","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-revit-api"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/ricaun.com\/br\/wp-json\/wp\/v2\/posts\/953","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ricaun.com\/br\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ricaun.com\/br\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ricaun.com\/br\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ricaun.com\/br\/wp-json\/wp\/v2\/comments?post=953"}],"version-history":[{"count":17,"href":"https:\/\/ricaun.com\/br\/wp-json\/wp\/v2\/posts\/953\/revisions"}],"predecessor-version":[{"id":973,"href":"https:\/\/ricaun.com\/br\/wp-json\/wp\/v2\/posts\/953\/revisions\/973"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ricaun.com\/br\/wp-json\/wp\/v2\/media\/971"}],"wp:attachment":[{"href":"https:\/\/ricaun.com\/br\/wp-json\/wp\/v2\/media?parent=953"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ricaun.com\/br\/wp-json\/wp\/v2\/categories?post=953"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ricaun.com\/br\/wp-json\/wp\/v2\/tags?post=953"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}