{"id":915,"date":"2024-02-20T17:01:29","date_gmt":"2024-02-20T20:01:29","guid":{"rendered":"https:\/\/ricaun.com\/?p=915"},"modified":"2024-02-20T18:30:05","modified_gmt":"2024-02-20T21:30:05","slug":"revit-api-context","status":"publish","type":"post","link":"https:\/\/ricaun.com\/br\/revit-api-context\/","title":{"rendered":"Revit API Context"},"content":{"rendered":"<p>Revit API Context is the time Revit is ready to execute Revit API code in a safe place.<\/p>\n\n\n\n<p>If your code tries to access some Revit API methods outside the Revit API Context probably you gonna receive an Exception: <strong>Invalid call to Revit API! Revit is currently not within an API context.<\/strong><\/p>\n\n\n\n<p>The most common way to place that your code is running outside the Revit API Context is when you create a modeless WPF, and usually is necessary to create an <strong>ExternalEvent <\/strong>that is a way to send a request to the Revit API to run that code in Revit API Context.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">What is Revit API Context?<\/h4>\n\n\n\n<p>The video below shows some examples where Revit API is in context and outside context. And have the implementation to know if the code is InContext.<\/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=\"What is Revit API Context?\" width=\"1200\" height=\"675\" src=\"https:\/\/www.youtube.com\/embed\/4Pr2uJILaso?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" allowfullscreen><\/iframe><\/div>\n<\/div><\/figure>\n\n\n\n<p>Subscribing to an event in the <strong>UIApplication <\/strong>or <strong>UIControllerApplication <\/strong>is only allowed when Revit API is in Context, this could be used to know if your code is inside or outside Revit API Context.<\/p>\n\n\n<p><script src=\"https:\/\/gist.github.com\/ricaun\/f79a086a0390fa1df2feb182c563bf6d.js\"><\/script><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>This implementation is used in the library <a href=\"https:\/\/github.com\/ricaun-io\/ricaun.Revit.UI.Tasks\" target=\"_blank\" rel=\"noopener\" title=\"\">ricaun.Revit.UI.Tasks<\/a> to execute the code if is in context instead of waiting for the ExternalEvent to be executed by Revit API.<\/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=\"Tasks &amp; InContext - Revit API\" width=\"1200\" height=\"675\" src=\"https:\/\/www.youtube.com\/embed\/gyo6xGN5DDU?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" allowfullscreen><\/iframe><\/div>\n<\/div><\/figure>\n\n\n\n<p>Now this <strong>InContext <\/strong>implementation the library gonna handle if the code gonna run right away or wait for the Revit API code to run in the <strong>ExternalEvent<\/strong>.<\/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:\/\/github.com\/ricaun-io\/ricaun.Revit.UI.Tasks\" target=\"_blank\" rel=\"noopener\" title=\"\">ricaun.Revit.UI.Tasks<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Revit API Context is the time Revit is ready to execute Revit API code in a safe place. If your code tries to access some Revit API methods outside the Revit API Context probably you gonna receive an Exception: Invalid call to Revit API! Revit is currently not within an API context. The most common&hellip;&nbsp;<a href=\"https:\/\/ricaun.com\/br\/revit-api-context\/\" class=\"\" rel=\"bookmark\">Continue a ler &raquo;<span class=\"screen-reader-text\">Revit API Context<\/span><\/a><\/p>","protected":false},"author":1,"featured_media":921,"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-915","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\/915","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=915"}],"version-history":[{"count":3,"href":"https:\/\/ricaun.com\/br\/wp-json\/wp\/v2\/posts\/915\/revisions"}],"predecessor-version":[{"id":920,"href":"https:\/\/ricaun.com\/br\/wp-json\/wp\/v2\/posts\/915\/revisions\/920"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ricaun.com\/br\/wp-json\/wp\/v2\/media\/921"}],"wp:attachment":[{"href":"https:\/\/ricaun.com\/br\/wp-json\/wp\/v2\/media?parent=915"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ricaun.com\/br\/wp-json\/wp\/v2\/categories?post=915"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ricaun.com\/br\/wp-json\/wp\/v2\/tags?post=915"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}