This does not have much to do with Inventor, it’s simply about using a web browser component (CefSharp, WebView2 or something else) and send messages from the displayed website to the app.
I created a very basic sample to prove this works and used WebView2 just like my colleague for the Vault add-in sample.
Took the SimpleAddIn sample available in the Inventor SDK under “DeveloperToolsSamplesVCSharp.NETAddInsSimpleAddIn”, added a Form to it that includes a WebView2 component.
using System.Windows.Forms;
namespace SimpleAddIn
{
public partial class MyBrowserForm : Form
{
Inventor.Application _app;
public MyBrowserForm(Inventor.Application app)
{
_app = app;
// This seems to be needed
string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData);
System.Environment.SetEnvironmentVariable("WEBVIEW2_USER_DATA_FOLDER", path);
InitializeComponent();
myWebView2.WebMessageReceived += MyWebView2_WebMessageReceived;
myWebView2.CoreWebView2InitializationCompleted += MyWebView2_CoreWebView2InitializationCompleted;
myWebView2.EnsureCoreWebView2Async();
}
private void MyWebView2_CoreWebView2InitializationCompleted(object sender, Microsoft.Web.WebView2.Core.CoreWebView2InitializationCompletedEventArgs e)
{
var htmlContent = @"
<html>
<body>
<script>
function onClick() {
window.chrome.webview.postMessage('Hello from web page');
}
</script>
<button onclick=""onClick()"">Hello</button>
</body>
</html>
";
myWebView2.NavigateToString(htmlContent);
}
private void MyWebView2_WebMessageReceived(object sender, Microsoft.Web.WebView2.Core.CoreWebView2WebMessageReceivedEventArgs e)
{
var text = e.TryGetWebMessageAsString();
var docName = _app.ActiveDocument.DisplayName;
MessageBox.Show(text + " for " + docName);
}
}
}
Then show that Form when the “Draw Slot” button is clicked.
override protected void ButtonDefinition_OnExecute(NameValueMap context)
{
var myBrowserForm = new MyBrowserForm(InventorApplication);
myBrowserForm.Show();
}
When the user clicks the “Hello” button of the web page that will send a message back to the container.
The message can be caught using the WebMessageReceived event handler.
See it in action:
Source code: https://github.com/adamenagy/InventorWebView2

Leave a Reply