<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Calling Revit command from chromium browser. in Revit API Forum</title>
    <link>https://forums.autodesk.com/t5/revit-api-forum/calling-revit-command-from-chromium-browser/m-p/12413381#M8312</link>
    <description>&lt;P&gt;Thank you very much indeed for a very nice example of implementing a minimal external event and using it to call Revit from outside, from a modeless context. This is a perfect fit for The Building Coder topic group:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="https://thebuildingcoder.typepad.com/blog/about-the-author.html#5.28" target="_blank"&gt;https://thebuildingcoder.typepad.com/blog/about-the-author.html#5.28&lt;/A&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'll gladly add it to the blog next week, if I may. Happy weekend to you!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 01 Dec 2023 14:56:56 GMT</pubDate>
    <dc:creator>jeremy_tammik</dc:creator>
    <dc:date>2023-12-01T14:56:56Z</dc:date>
    <item>
      <title>Calling Revit command from chromium browser.</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/calling-revit-command-from-chromium-browser/m-p/12413281#M8311</link>
      <description>&lt;P&gt;This is another guide on chromium browser using cef sharp. Hope some one finds it usefull.&lt;BR /&gt;This is a continuation of this post :&amp;nbsp;&lt;A href="https://forums.autodesk.com/t5/revit-api-forum/simple-wpf-with-a-chromium-browser-guide/td-p/12396552" target="_blank" rel="noopener"&gt;Solved: Simple WPF with a chromium browser guide - Autodesk Community - Revit Products&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Basicly what I wanted was for a button in the browser (on a webpage) to trigger a command in revit. This works by "binding" a javascript method to a c# object and its method. In the Javascript we await for the object and call its function.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So lets make a dummy object for binding and a method in it. In order to call a Revit method it will need a reference to an external event handler and its event.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;   public class BoundObject
   {

       public int Add(int a, int b)
       {
           ExtApp.handler.a = a;
           ExtApp.handler.b = b;
           ExtApp.testEvent.Raise();

           return a+b;
       }
   }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The event and its handler are saved in the External app as static for ease of access.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;  internal class ExtApp : IExternalApplication
  {
      public static IExternalApplication MyApp;
      public static ChromiumWebBrowser browser;
      public static ExternalEvent testEvent;
      public static MyEvent handler;
      public Result OnShutdown(UIControlledApplication application)
      {
         // Cef.Shutdown();
          return Result.Succeeded;
      }

      public Result OnStartup(UIControlledApplication application)
      {
          MyApp = this;
          //code for making a button
         
          handler = new MyEvent();
          testEvent= ExternalEvent.Create(handler);


          return Result.Succeeded;
      }
  }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the wpf the browser is embeded like this&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;&amp;lt;Window x:Class="RevitTestProject.TestWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:local="clr-namespace:RevitTestProject"
        xmlns:cef="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf"
        mc:Ignorable="d"
        Width="1000" Height="500"&amp;gt;
    &amp;lt;Grid Background="PapayaWhip"&amp;gt;

        &amp;lt;cef:ChromiumWebBrowser Name="ChromiumBrowser" Address="http://www.google.com" Width="900" Height="450"  /&amp;gt;
    &amp;lt;/Grid&amp;gt;
&amp;lt;/Window&amp;gt;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and the code behind&amp;nbsp; of the window is :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;        public TestWindow()
        {

 
            InitializeComponent();
            ChromiumBrowser.Address = "https://www.google.com";
            ChromiumBrowser.Address = "C:\\Users\\XXX\\Desktop\\index.html";
            BoundObject bo = new BoundObject();
            ChromiumBrowser.JavascriptObjectRepository.Register("boundAsync", bo, true, BindingOptions.DefaultBinder);
          

        }

        public void Dispose()
        {
            this.Dispose();
        }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So to use it make an index.html and make the path to it in the browser address.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;The Test webpage look like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;&amp;lt;html&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;title&amp;gt;Bridge Test&amp;lt;/title&amp;gt;
    &amp;lt;!-- &amp;lt;script src="script.js"&amp;gt;&amp;lt;/script&amp;gt; --&amp;gt;
    &amp;lt;script type="text/javascript"&amp;gt;
     async function callCSharpAction() {
        await CefSharp.BindObjectAsync("boundAsync");

        boundAsync.add(16, 2);
    }
    &amp;lt;/script&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
    &amp;lt;button id="action1" onclick="callCSharpAction()"&amp;gt;Action 1&amp;lt;/button&amp;gt;
    &amp;lt;button id="action2" onclick="alert('Button is working')"&amp;gt;Action 2&amp;lt;/button&amp;gt;
    &amp;lt;button id="action3"&amp;gt;Action 3&amp;lt;/button&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The handler code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;  internal class MyEvent : IExternalEventHandler
  {
      public int a;
      public int b;
      public void Execute(UIApplication app)
      {
          TaskDialog.Show("yoyoy", "data is "+a.ToString()+" and "+b.ToString()+".");
      }

      public string GetName()
      {
          return "YOYOOY";
      }
  }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2023 14:19:22 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/calling-revit-command-from-chromium-browser/m-p/12413281#M8311</guid>
      <dc:creator>Andrej.Licanin</dc:creator>
      <dc:date>2023-12-01T14:19:22Z</dc:date>
    </item>
    <item>
      <title>Re: Calling Revit command from chromium browser.</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/calling-revit-command-from-chromium-browser/m-p/12413381#M8312</link>
      <description>&lt;P&gt;Thank you very much indeed for a very nice example of implementing a minimal external event and using it to call Revit from outside, from a modeless context. This is a perfect fit for The Building Coder topic group:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="https://thebuildingcoder.typepad.com/blog/about-the-author.html#5.28" target="_blank"&gt;https://thebuildingcoder.typepad.com/blog/about-the-author.html#5.28&lt;/A&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'll gladly add it to the blog next week, if I may. Happy weekend to you!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2023 14:56:56 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/calling-revit-command-from-chromium-browser/m-p/12413381#M8312</guid>
      <dc:creator>jeremy_tammik</dc:creator>
      <dc:date>2023-12-01T14:56:56Z</dc:date>
    </item>
    <item>
      <title>Re: Calling Revit command from chromium browser.</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/calling-revit-command-from-chromium-browser/m-p/12413444#M8313</link>
      <description>&lt;P&gt;Sure Jeremy, id love to see what people can do with it &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2023 15:21:25 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/calling-revit-command-from-chromium-browser/m-p/12413444#M8313</guid>
      <dc:creator>Andrej.Licanin</dc:creator>
      <dc:date>2023-12-01T15:21:25Z</dc:date>
    </item>
    <item>
      <title>Re: Calling Revit command from chromium browser.</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/calling-revit-command-from-chromium-browser/m-p/12422698#M8314</link>
      <description>&lt;P&gt;To make a callback from c# function to the browser, you just need an instance of the browser, and a function in the javascript code that will be called.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;here is an edited index.html&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;&amp;lt;html&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;title&amp;gt;Bridge Test&amp;lt;/title&amp;gt;
    &amp;lt;!-- &amp;lt;script src="script.js"&amp;gt;&amp;lt;/script&amp;gt; --&amp;gt;
    &amp;lt;script type="text/javascript"&amp;gt;
     async function callCSharpAction() {
        await CefSharp.BindObjectAsync("boundAsync");

        boundAsync.add(16, 2);
    }


    function showAlert(arg1) {
        // Your JavaScript logic here
        alert("Function called with arguments: " + arg1);
        return ;
    }
    &amp;lt;/script&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
    &amp;lt;button id="action1" onclick="callCSharpAction()"&amp;gt;Action 1&amp;lt;/button&amp;gt;
    &amp;lt;button id="action2" onclick="alert('Button is working')"&amp;gt;Action 2&amp;lt;/button&amp;gt;
    &amp;lt;button id="action3"&amp;gt;Action 3&amp;lt;/button&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and in our bound class we save a instance to the browser so we can use it on command&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;    public class BoundObject
    {
        public int aS;
        public int bS;
        internal ChromiumWebBrowser browser;

        public void CallCSharpMethod()
        {
            MessageBox.Show("C# method called!");
            // Add more code here as needed
        }
        public int Add(int a, int b)
        {
            ExtApp.handler.a = a;
            ExtApp.handler.b = b;
            ExtApp.testEvent.Raise();

            return a+b;

        }

        public int SendSomeDataFromLocal(int a)
        {
            browser.ExecuteScriptAsync("showAlert("+a.ToString()+")");
            return a;
        }
    }&lt;/LI-CODE&gt;&lt;P&gt;i passed it when i created the browser in the window codebehind.&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;public TestWindow()
{

 
    InitializeComponent();
    ChromiumBrowser.Address = "https://www.google.com";
    ChromiumBrowser.Address = "C:\\Users\\XXX\\Desktop\\index.html";
    BoundObject bo = new BoundObject();
    //ExtApp.boundObj = bo;
    bo.browser = ChromiumBrowser;
    ChromiumBrowser.JavascriptObjectRepository.Register("boundAsync", bo, true, BindingOptions.DefaultBinder);
  

}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;finally you can call it from revit&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;   public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
   {
       ExtApp.boundObj.SendSomeDataFromLocal(999);


       return Result.Succeeded;
   }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;this concludes a round trip from the browser and back. I hope anyone reading this finds it usefull.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Dec 2023 08:11:17 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/calling-revit-command-from-chromium-browser/m-p/12422698#M8314</guid>
      <dc:creator>Andrej.Licanin</dc:creator>
      <dc:date>2023-12-06T08:11:17Z</dc:date>
    </item>
  </channel>
</rss>

