Palette.incomingFromHTML Event

Parent Object: Palette
Defined in namespace "adsk::core" and the header file is <Core/UserInterface/Palette.h>

Description

This event is fired when the JavaScript associated with the HTML calls the adsk.fusionSendData function. This allows the HTML to communicate with the add-in by passing information to the add-in.

Syntax

-------- Import ---------
# Import fusion360utils folder, which includes event_utils.py.
from ...lib import fusion360utils as futil

-------- Global variables ---------
# Global variable used to maintain a reference to all event handlers.
local_handlers = []

-------- Connect the handler to the event. ---------
# "palette_var" is a variable referencing a Palette object.
# "palette_incomingFromHTML" is the event handler function.
futil.add_handler(palette_var.incomingFromHTML, palette_incomingFromHTML, local_handlers=local_handlers)

-------- Event handler function definition ---------
# Event handler for the incomingFromHTML event.
def palette_incomingFromHTML(args: adsk.core.HTMLEventArgs):
# Code to react to the event.
app.log('In palette_incomingFromHTML event handler.')
-------- Global variables ---------
# Global variable used to maintain a reference to all event handlers.
handlers = []

-------- Connect the handler to the event. ---------
# "palette_var" is a variable referencing a Palette object.
# "MyIncomingFromHTMLHandler" is the name of the class that handles the event.
onIncomingFromHTML = MyIncomingFromHTMLHandler()
palette_var.incomingFromHTML.add(onIncomingFromHTML)
handlers.append(onIncomingFromHTML)

-------- Event handler class definition ---------
# Event handler for the incomingFromHTML event.
class MyIncomingFromHTMLHandler(adsk.core.HTMLEventHandler):
def __init__(self):
super().__init__()
def notify(self, args: adsk.core.HTMLEventArgs):
# Code to react to the event.
app.log('In MyIncomingFromHTMLHandler event handler.')
--------- Required include files. ---------
#include <Core/UserInterface/Palette.h>
#include <Core/UserInterface/HTMLEvent.h>
#include <Core/UserInterface/HTMLEventHandler.h>
#include <Core/UserInterface/HTMLEventArgs.h>


--------- Event handler class definition and global declaration. ---------
// Event handler for the incomingFromHTML event.
class MyIncomingFromHTMLEventHandler : public adsk::core::HTMLEventHandler
{
public:
void notify(const Ptr<HTMLEventArgs>& eventArgs) override
{
// Code to react to the event.
ui->messageBox("In MyIncomingFromHTMLEventHandler event handler.");
}
} _incomingFromHTML;

--------- Connect the handler to the event. ---------
// "palette_var" is a variable referencing a Palette object.
// Connect the handler function to the event.
Ptr<HTMLEvent> incomingFromHTMLEvent = palette_var->incomingFromHTML();
if (!incomingFromHTMLEvent)
return;

bool isOk = incomingFromHTMLEvent->add(&_incomingFromHTML);
if (!isOk)
return;

Property Value

This is an event property that returns a HTMLEvent.

Samples

Name Description
Palette Sample

Demonstrates how to create a palette, how to dock and snap palettes and how Fusion communicates with the palette HTML. The sample is an add-in. To use it, create a new Python add-in and replace the code with the code below. You also need to create an html file using the name and code below. The html file needs to be in the same folder as the py file.

When you load the add-in, you'll see two new commands under the ADD-INS panel of the TOOLS tab. The "Show Custom Palette" command will cause the custom palette to be displayed. It will remain displayed until you click its Close button. Clicking the "Click to send info to Fusion" button on the palette, will send information to your add-in, which uses the API to display that information in a message box. Running the "Send Info to HTML" command to send data to the javascript running in the palette, which uses it to update the content of a paragraph.

palette.html

<!DOCTYPE html>
<html>
   <head>
   </head>
   <body>
       <p>Click the button below to send data to Fusion.</p>
       <button type='button' onclick='sendInfoToFusion()'>Click to send info to Fusion</button> 
       <p id='p1'>Run the "Send Info to HTML" command in the ADD-INS panel to update this text.</p>
       <br /><br />
	
   </body>
   <script>
       function sendInfoToFusion(){
           var today = new Date();
           var dd = String(today.getDate()).padStart(2, '0');
           var mm = String(today.getMonth() + 1).padStart(2, '0');
           var yyyy = today.getFullYear();

           var hours = String(today.getHours()).padStart(2, '0');
           var minutes = String(today.getMinutes()).padStart(2, '0');
           var seconds = String(today.getSeconds()).padStart(2, '0');

           var date = dd + '/' + mm + '/' + yyyy;
           var time = hours + ':' + minutes + ':' + seconds;
           var args = {
               arg1 : "Sample argument 1",
               arg2 : "Sample argument 2"
           };
           adsk.fusionSendData('send', JSON.stringify(args));
       }
       
       window.fusionJavaScriptHandler = {handle: function(action, data){
           try {
               if (action == 'send') {
					// Update a paragraph with the data passed in.
					document.getElementById('p1').innerHTML = data;
				}
				else if (action == 'debugger') {
                   debugger;
				}
				else {
					return 'Unexpected command type: ' + action;
               }
           } catch (e) {
               console.log(e);
               console.log('exception caught with command: ' + action + ', data: ' + data);
           }
           return 'OK';
       }};
   </script>
</html>

Version

Introduced in version March 2017