Announcements
Autodesk Community will be read-only between April 26 and April 27 as we complete essential maintenance. We will remove this banner once completed. Thanks for your understanding

html button event send info to html Palette

JanNavratil
Advocate Advocate
1,035 Views
9 Replies
Message 1 of 10

html button event send info to html Palette

JanNavratil
Advocate
Advocate

Hi. 

Little tricky question, are we able on button click from HTML pallete use palette.sendInfoToHTML to show some informations from Fusion on pallete? In sample there is on command click to send info to HTML but Im not able to get it working with button clikc on the pallete - it always freez.

  if data['action']=='count':
                    palette = _ui.palettes.itemById('myPalette')
                    palette.sendInfoToHTML('send''test'
 
Can anyone advice please?

Thanks,
Jan

 

Reply
Reply
0 Likes
Accepted solutions (1)
1,036 Views
9 Replies
Replies (9)
Message 2 of 10

goyals
Autodesk
Autodesk

I am not able to understand the problem you are noticing. If possible please share script so We can take a look and try to reproduce the problem on our side. Thanks.



Shyam Goyal
Sr. Software Dev. Manager
Reply
Reply
0 Likes
Message 3 of 10

JanNavratil
Advocate
Advocate

Hi, Im sorry - here is the code
On the html anction - button click I need to get the info from fusion to appear in HTML as text.


# Event handler for the palette HTML event.                
class MyHTMLEventHandler(adsk.core.HTMLEventHandler😞
    def __init__(self😞
        super().__init__()
    def notify(selfargs😞
        global _ui, _app, num
        _app = adsk.core.Application.get()
        _ui  = _app.userInterface

        doc = _app.activeDocument
        products = doc.products
        product = products.itemByProductType('CAMProductType')
        _cam = adsk.cam.CAM.cast(product)
        try:
            htmlArgs = adsk.core.HTMLEventArgs.cast(args)            
            data = json.loads(htmlArgs.data)

            

            if data['action']=='verze':
                    palette = _ui.palettes.itemById('myPalette')
                    if palette :
                        _ui.messageBox(_app.version)  
                         
            if data['action']=='count':
                    if palette :                   
                        palette = _ui.palettes.itemById('myPalette')
                        palette.sendInfoToHTML('send', 'value I need to send') 
 
        except:
            _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))  
           
def run(context😞
    try:
        global _ui, _app
        _app = adsk.core.Application.get()
        _ui  = _app.userInterface
        
        # Add a command that displays the panel.
        showPaletteCmdDef = _ui.commandDefinitions.itemById('showPalette')
        if not showPaletteCmdDef:
            showPaletteCmdDef = _ui.commandDefinitions.addButtonDefinition('showPalette''Zobrazit CAM CS+ Tools''Show the custom palette''')

            # Connect to Command Created event.
            onCommandCreated = ShowPaletteCommandCreatedHandler()
            showPaletteCmdDef.commandCreated.add(onCommandCreated)
            handlers.append(onCommandCreated)
         
  
        # Add the command to the toolbar.
        panel = _ui.allToolbarPanels.itemById('SolidScriptsAddinsPanel')
        cntrl = panel.controls.itemById('showPalette')
        if not cntrl:
            panel.controls.addCommand(showPaletteCmdDef)
     

        # Create and display the palette.
            palette = _ui.palettes.itemById('myPalette')
            if not palette:
                
                palette = _ui.palettes.add('myPalette''Tools''palette.html'TrueTrueTrue300200)
                
                # Dock the palette to the right side of Fusion window.
                palette.dockingState = adsk.core.PaletteDockingStates.PaletteDockStateRight
    
                # Add handler to HTMLEvent of the palette.
                onHTMLEvent = MyHTMLEventHandler()
                palette.incomingFromHTML.add(onHTMLEvent)   
                handlers.append(onHTMLEvent)
    
                # Add handler to CloseEvent of the palette.
                onClosed = MyCloseEventHandler()
                palette.closed.add(onClosed)
                handlers.append(onClosed)   
            else:
                palette.isVisible = True                         
    except:
        if _ui:
            _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))


def stop(context😞
    try:        
        # Delete the palette created by this add-in.
        palette = _ui.palettes.itemById('myPalette')
        if palette:
            palette.deleteMe()
            
        # Delete controls and associated command definitions created by this add-ins
        panel = _ui.allToolbarPanels.itemById('SolidScriptsAddinsPanel')
        cmd = panel.controls.itemById('showPalette')
        if cmd:
            cmd.deleteMe()
        cmdDef = _ui.commandDefinitions.itemById('showPalette')
        if cmdDef:
            cmdDef.deleteMe() 

        cmd = panel.controls.itemById('sendInfoToHTML')
        if cmd:
            cmd.deleteMe()
        cmdDef = _ui.commandDefinitions.itemById('sendInfoToHTML')
        if cmdDef:
            cmdDef.deleteMe() 
            
        #_ui.messageBox('Stop addin')
    except:
        if _ui:
            _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
Reply
Reply
0 Likes
Message 4 of 10

JeromeBriot
Mentor
Mentor

Hello,

 

First, read the explanations by @marshaltu here: Can't get Javascript add-in to communicate with palette

 

Next, do not create the palette inside the run function. See there : [bug] Palette created in the run function and Run on Startup enabled

 

Last, please use the button </> to insert code in your message.

Reply
Reply
0 Likes
Message 5 of 10

JanNavratil
Advocate
Advocate

Hi,

 

thank you, Ive checked the post but I did not find reason why I'm getting stucked on the button click with action "count".

 

# Event handler for the palette HTML event.                
class MyHTMLEventHandler(adsk.core.HTMLEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        global _ui, _app, num
        _app = adsk.core.Application.get()
        _ui  = _app.userInterface

        doc = _app.activeDocument
        products = doc.products
        product = products.itemByProductType('CAMProductType')
        _cam = adsk.cam.CAM.cast(product)
        try:
            htmlArgs = adsk.core.HTMLEventArgs.cast(args)            
            data = json.loads(htmlArgs.data)
         
            if data['action']=='verze':
                    palette = _ui.palettes.itemById('CStool')
                    if palette :
                        _ui.messageBox(_app.version)  
                         
            if data['action']=='count':
                    palette = _ui.palettes.itemById('CStool')
                    if palette:
                        palette.sendInfoToHTML('send', 'test') 
       
        except:
            _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))  
           

I just need to send data to palette to show some string in <p> from fusion.

But like this, the fusion always get frozen. 😞

 

Thank you for help !
Jan

 

Reply
Reply
0 Likes
Message 6 of 10

JeromeBriot
Mentor
Mentor

Can you share the complete add-in (Python & HTML files) ?

Reply
Reply
0 Likes
Message 7 of 10

JanNavratil
Advocate
Advocate

Yes, please find attached files.

many thanks,
Jan

Reply
Reply
0 Likes
Message 8 of 10

JeromeBriot
Mentor
Mentor
Accepted solution

Analyze this code:

 

#Author-JaNa
#Description-CAM Addin as part of Fusion360 CS+

import adsk.core, adsk.fusion, adsk.cam, traceback # pylint: disable=import-error
import json
import threading

# global set of event handlers to keep them referenced for the duration of the command
handlers = []
app = adsk.core.Application.cast(None)
ui = adsk.core.UserInterface.cast(None)

myCustomEventId = 'myCustomEvent'
myCustomEvent = None
stopFlag = None
myThread = None

sendMsgToPalette = False

num = 0

# Event handler for the commandExecuted event.
class ShowPaletteCommandExecuteHandler(adsk.core.CommandEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:
            # Create and display the palette.
            palette = ui.palettes.itemById('CStool')
            if not palette:
                palette = ui.palettes.add('CStool', 'CAM CS+ Tools', 'palette.html', True, True, True, 300, 200)

                # Dock the palette to the right side of Fusion window.
                palette.dockingState = adsk.core.PaletteDockingStates.PaletteDockStateRight
    
                # Add handler to HTMLEvent of the palette.
                onHTMLEvent = MyHTMLEventHandler()
                palette.incomingFromHTML.add(onHTMLEvent)   
                handlers.append(onHTMLEvent)

            else:
                palette.isVisible = True                               
        except:
            ui.messageBox('Command executed failed: {}'.format(traceback.format_exc()))


# Event handler for the commandCreated event.
class ShowPaletteCommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
    def __init__(self):
        super().__init__()              
    def notify(self, args):
        try:
            command = args.command
            onExecute = ShowPaletteCommandExecuteHandler()
            command.execute.add(onExecute)
            handlers.append(onExecute)                                     
        except:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))     


# Event handler for the palette HTML event.                
class MyHTMLEventHandler(adsk.core.HTMLEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):

        global sendMsgToPalette

        doc = app.activeDocument
        products = doc.products
        product = products.itemByProductType('CAMProductType')
        _cam = adsk.cam.CAM.cast(product)
        try:
            htmlArgs = adsk.core.HTMLEventArgs.cast(args)            
            data = json.loads(htmlArgs.data)
         
            if data['action']=='verze':
                palette = ui.palettes.itemById('CStool')
                if palette :
                    ui.messageBox(app.version)  
                         
            if data['action']=='count':
                sendMsgToPalette = True
                

        except:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))  


# The event handler that responds to the custom event being fired.
class ThreadEventHandler(adsk.core.CustomEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):

        global sendMsgToPalette
        global num

        try:

            if sendMsgToPalette:
                num +=1
                
                palette = ui.palettes.itemById('CStool')
                palette.sendInfoToHTML('send', 'test: {}'.format(num)) 
                sendMsgToPalette = False

        except:
            if ui:
                ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

class MyThread(threading.Thread):
    def __init__(self, event):
        threading.Thread.__init__(self)
        self.stopped = event

    def run(self):
        while not self.stopped.wait(0.5):
            app.fireCustomEvent(myCustomEventId)
         

def run(context):

    global app, ui
    global myCustomEvent
    global stopFlag   
    global myThread
        
    ui = None
    try:

        app = adsk.core.Application.get()
        ui  = app.userInterface

        # Register the custom event and connect the handler.
        myCustomEvent = app.registerCustomEvent(myCustomEventId)
        onThreadEvent = ThreadEventHandler()
        myCustomEvent.add(onThreadEvent)
        handlers.append(onThreadEvent)

        # Create a new thread for the other processing.       
        stopFlag = threading.Event()
        myThread = MyThread(stopFlag)
        myThread.start()

        showPaletteCmdDef = ui.commandDefinitions.addButtonDefinition('showPalette', 'Zobrazit CAM CS+ Tools', 'Zobrazit CAM CS+ Tools', '')

        # Add the command to the toolbar.
        panel = ui.allToolbarPanels.itemById('SolidScriptsAddinsPanel')
        panel.controls.addCommand(showPaletteCmdDef)    

        # Connect to Command Created event.
        onCommandCreated = ShowPaletteCommandCreatedHandler()
        showPaletteCmdDef.commandCreated.add(onCommandCreated)
        handlers.append(onCommandCreated)      
                 
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))


def stop(context):

    try:        

        if handlers.count:
            myCustomEvent.remove(handlers[0])

        stopFlag.set() 

        app.unregisterCustomEvent(myCustomEventId)

        # Delete the palette created by this add-in.
        palette = ui.palettes.itemById('CStool')
        if palette:
            palette.deleteMe()
            
        # Delete controls and associated command definitions created by this add-ins
        panel = ui.allToolbarPanels.itemById('SolidScriptsAddinsPanel')
        cmd = panel.controls.itemById('showPalette')
        if cmd:
            cmd.deleteMe()
        cmdDef = ui.commandDefinitions.itemById('showPalette')
        if cmdDef:
            cmdDef.deleteMe() 
            
        #ui.messageBox('Stop addin')

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))


 

Reply
Reply
0 Likes
Message 9 of 10

JanNavratil
Advocate
Advocate

Ah cool! thank you, so I have to create new thread for that.

Thank you so much for help !

 

Jan

Reply
Reply
0 Likes
Message 10 of 10

JeromeBriot
Mentor
Mentor

@JanNavratil wrote:

Ah cool! thank you, so I have to create new thread for that.


Yes. You can't use sendInfoToHTML inside MyHTMLEventHandler.

That's what @marshaltu explained (see the link I provided in my first reply).

Reply
Reply
0 Likes