Encountered an error while using CefSharp

Encountered an error while using CefSharp

u0416033
Explorer Explorer
1,160 Views
11 Replies
Message 1 of 12

Encountered an error while using CefSharp

u0416033
Explorer
Explorer

I try to use CefSharp for add browser to the revit api plugin,but when application run to  new CefSettings() an error occurred

u0416033_0-1693444884428.png

    public partial class Form1 : System.Windows.Forms.Form
    {
        UIDocument uidoc;
        Document doc;
        Selection Selection;

        public Form1(UIDocument uidoc, Document doc, Selection selection)
        {
            InitializeComponent();
            this.uidoc = uidoc;
            this.doc = doc;
            this.Selection = selection;

            InitBrowser();
        }
        public ChromiumWebBrowser browser;

        public void InitBrowser()
        {

            var settings = new CefSettings();
            settings.RegisterScheme(new CefCustomScheme
            {
                SchemeName = "localfolder",
                SchemeHandlerFactory = new FolderSchemeHandlerFactory(
                                       rootFolder: @":\Users\User\source\repos\revitForgePlugin_2\revitForgePlugin_2\index.html",
                                       hostName: "localfolder",
                                       defaultPage: "index.html")                 
            });

            Cef.Initialize(settings);
            browser = new ChromiumWebBrowser("file:///C:/Users/User/source/repos/revitForgePlugin_2/revitForgePlugin_2/index.html"); // CefSharp needs a initial page...
            browser.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
     | System.Windows.Forms.AnchorStyles.Left)
     | System.Windows.Forms.AnchorStyles.Right)));
            browser.MinimumSize = new System.Drawing.Size(20, 20);
            browser.Name = "webBrowser1";
            browser.TabIndex = 1;
            browser.Dock = DockStyle.Fill;
            panel1.Controls.Add(browser);




        }


    }


Do I use wrong version of CefSharp ? How can I fix it? Thanks!

0 Likes
1,161 Views
11 Replies
Replies (11)
Message 2 of 12

jeremy_tammik
Alumni
Alumni

Quite ;possibly, probably yes. You need to use the same version of CefSharp as Revit does:

  

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 3 of 12

u0416033
Explorer
Explorer
  • It means that I need to use different version of cefsharp if I use revit 2022 and 2024
0 Likes
Message 4 of 12

jeremy_tammik
Alumni
Alumni

You need to use the same version of CefSharp as the Revit executable into which you load your add-in. Other solutions are possible, but more harder to implement, e.g.:

  

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
Message 5 of 12

ricaun
Advisor
Advisor

As Jeremy mentioned probably you are using the wrong version of the CefSharp.

 

Here is a list of each Revit Version / CefSharp

 

  • Revit 2024 - 105.3.390
  • Revit 2023 - 92.0.260
  • Revit 2022 - 65.0.1
  • Revit 2021 - 65.0.1
  • Revit 2020 - 65.0.1
  • Revit 2019 - 57.0.0

Another thing that probably gonna fail is that by default Revit already initializes CefSharp before any addin is loaded, which means your application cannot change the Cef configuration, and your application doesn't need to initialize CefSharp.

 

And CefSharp references assembly doesn't need to be copied, Revit already has that dlls.

 

I created a library that has the reference for each version of CefSharp for Revit.

 

https://github.com/ricaun-io/ricaun.Revit.CefSharp

 

Luiz Henrique Cassettari

ricaun.com - Revit API Developer

AppLoader EasyConduit WireInConduit ConduitMaterial CircuitName ElectricalUtils

Message 6 of 12

u0416033
Explorer
Explorer

Thank you ! But I  am using the winform in my project .Is this related to the .NET framework version I'm using?

0 Likes
Message 7 of 12

ricaun
Advisor
Advisor

Revit has the CefSharp for the WPF version:

  • CefSharp.Core.dll
  • CefSharp.Wpf.dll
  • CefSharp.dll

If you are using CefSharp.WinForms and Form inside Revit, oh boy that's gonna be a challenge to make work.

 

Probably is simple to change to WPF, here is a project example: https://github.com/ricaun-io/RevitAddin.CefSharpExample

 

 

Luiz Henrique Cassettari

ricaun.com - Revit API Developer

AppLoader EasyConduit WireInConduit ConduitMaterial CircuitName ElectricalUtils

0 Likes
Message 8 of 12

u0416033
Explorer
Explorer

Thank you a lot . Very good youtube Channel I have subscribed.I have suceessed load website with the example but when I use the local HTML file that used autodesk forge API  it does not show anything . Do I need to do something when I use the local HTML that has used other CDN?

        static WebView WebView { get; set; }
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elementSet)
        {
            UIApplication uiapp = commandData.Application;

            //OpenWebView(App.GetTextBoxValue());
           // OpenWebView("https://www.youtube.com/watch?v=lYjHDkpbsas");
            OpenWebView("file:///C:/Users/User/Desktop/code/revitApiWebForm/Viewer.html");

            return Result.Succeeded;
        }

        public static void OpenWebView(string localFilePath)
        {
            /*
            WebView?.SetAddress(address);
            if (WebView is null)
            {
                WebView = new WebView(address);
                WebView.Closed += (s, e) => { WebView = null; };
                WebView.Show();
            }
            WebView?.Activate();
            */

            string fileUri = new Uri(localFilePath).AbsoluteUri;

            WebView?.SetAddress(fileUri);
            if (WebView is null)
            {
                WebView = new WebView(fileUri);
                WebView.Closed += (s, e) => { WebView = null; };
                WebView.Show();
            }
            WebView?.Activate();
        }

 

HTML file

<html><head>
  <meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1, user-scalable=no">
  <meta charset="utf-8">

  <!-- The Viewer CSS -->
  <link rel="stylesheet" href="https://developer.api.autodesk.com/modelderivative/v2/viewers/7.*/style.css" type="text/css">

  <!-- Developer CSS -->
  <style>
    body {
      margin: 0;
    }

    #MyViewerDiv {
      width: 100%;
      height: 100%;
      margin: 0;
    }
  </style>
</head>
<body>
  <!-- The Viewer will be instantiated here -->
  <div id="MyViewerDiv"></div>

  <!-- The Viewer JS -->
  <script src="https://developer.api.autodesk.com/modelderivative/v2/viewers/7.*/viewer3D.js"></script>

  <!-- Developer JS -->
  <script>
    var urn ="modelUrn"
    var token ="token"
    if (urn != null && token != null) {
      showModel(urn, token);
    }

    // Viewer tutorial
    var viewer;

    function showModel(urn, token) {
      debugger;
      var options = {
        env: "AutodeskProduction",
        accessToken: token,
      };
      var documentId = "urn:" + urn;
      Autodesk.Viewing.Initializer(options, function onInitialized() {
        viewer = new Autodesk.Viewing.GuiViewer3D(document.getElementById("MyViewerDiv"));
        viewer.start();
        Autodesk.Viewing.Document.load(documentId, onDocumentLoadSuccess, onDocumentLoadFailure);
      });
    }

    function onDocumentLoadSuccess(doc) {
      const geom = doc.getRoot().getDefaultGeometry();
      viewer.loadDocumentNode(doc, geom);
    }

    function onDocumentLoadFailure(viewerErrorCode) {
      alert("File not translated or not viewable");
      console.error("onDocumentLoadFailure() - errorCode:" + viewerErrorCode);
    }
  </script>

</body></html>
0 Likes
Message 9 of 12

ricaun
Advisor
Advisor

I guess the 'file:///' is not necessary, and depending on the version of the CefSharp the javascript does not work very well.

 

The version 57.0.0 in Revit 2019 is really bad, most of the thing does not work.

 

Luiz Henrique Cassettari

ricaun.com - Revit API Developer

AppLoader EasyConduit WireInConduit ConduitMaterial CircuitName ElectricalUtils

0 Likes
Message 10 of 12

u0416033
Explorer
Explorer

Yes...I think so. I use revit 2022 that use the version 65.0.1.0 seems doesn't provide webGL to display forge viewer service. I think I need to build a desktop app  with higher version of cefsharp

u0416033_0-1693621185495.png

 

0 Likes
Message 11 of 12

RPTHOMAS108
Mentor
Mentor

I'm not familiar with CefSharp but I expect the Forge viewer existed prior to Revit 2022 and likely still needs to support Revit 2022, so it that the actual reason?

 

The other option is you could compile the cefsharp available on GitHub with slightly different assembly identity. I'm sure there must be a better solution to that e.g. ILMerge  ILRepack  etc. I've never really used these. You have to try and limit your dependencies on other things you don't control the evolution of.

 

You can try to load side by side with assembly load event but that is probably not a good idea for third party assemblies since you don't know what they are doing internally. The more complicated the assembly is the more chances that will not work or will cause unexpected behaviour.

 

The .net runtime will only load the same assembly once or use the latest version of it referenced so since the Revit process loads it first that is what you get.

0 Likes
Message 12 of 12

u0416033
Explorer
Explorer

Thank you ! I will try it .

0 Likes