Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

SelectionEventHandler with palette

4 REPLIES 4
Reply
Message 1 of 5
WrapItToTapIt
142 Views, 4 Replies

SelectionEventHandler with palette

Is there any examples of using a select tool with a html palette to show what item clicked on?

4 REPLIES 4
Message 2 of 5

Hi,

 

Here there is an example you can use:

class testCommandSelectHandler(adsk.core.SelectionEventHandler):
    def __init__(self):
        super().__init__()
    class bodyEncoder(json.JSONEncoder):
        def default(self, obj):
            if isinstance(obj, adsk.fusion.BRepBody):
                return {'name': obj.name, 
                        'boundingBox': {
                            'minPoint': obj.boundingBox.minPoint.asArray(), 
                            'maxPoint': obj.boundingBox.maxPoint.asArray()
                        },
                        'isSolid': obj.isSolid
                }
            else:
                return json.JSONEncoder.default(self, obj)
    def notify(self, args: adsk.core.SelectionEventArgs) -> None:
        try:
            if args.selection:
                palette = app.userInterface.palettes.itemById(PALETTE_ID)
                if palette:
                    obj = adsk.fusion.BRepBody.cast(args.selection.entity)
                    if obj:
                        palette.sendInfoToHTML('send', json.dumps(obj, cls=testCommandSelectHandler.bodyEncoder))
                    else:
                        app.log(f'not body selected: ({obj})')
        except:
            app.log('Failed:\n{}'.format(traceback.format_exc()))

 

This is the palette.html I used:

<!DOCTYPE html>
<html>
    <head>
        <title>Palette</title>
        <style>
            body {
                font-size: 16px;
                font-family: 'Roboto', sans-serif;
                color: white;
                background-color: black;
                margin: 0;
                padding: 0;
            }
        </style>
    </head>
    <body>
        <p>Object received:</p>
        <p id='fusionObject'></p>
    </body>
    <script>
        window.fusionJavaScriptHandler = {handle: function(action, data){
            try {
                if (action == 'send') {
					document.getElementById('fusionObject').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>

 

And this is the result once you choose a body:

Captura de pantalla 2022-07-15 141147.png

 

Notice that you have to build a function to serialize (convert to JSON format) the object you like to send. In this case I use the class bodyEncoder to include only the body's attributes name and boundingBox; you can include all you need there, according to the object type you choose in the selection command input (BRepBody in my case).

 

Hope this help. Let me know it it works for you.

 

Regards,

Jorge

Message 3 of 5

Thanks for the reply, not quite what i was trying to do.

I want to be able to recreate the fusion 360 select button inside a html palette.

Want it to act the same way have a html button that you click then you get a select tool to select a body, then once the body is selected send the selected to html.

Message 4 of 5
BrianEkins
in reply to: WrapItToTapIt

It's not currently possible. It was discussed at one time, but there would be a lot of work to be able to support it. A possible workaround is to create a command with a dialog, where you can have the selection input, and if you need to display additional information that the current command inputs don't support you can create an HTML command input and create whatever you want in it.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 5 of 5
kandennti
in reply to: WrapItToTapIt

Hi @WrapItToTapIt .

 

The main advantage of using palettes is that they can be used as modeless dialogs.
If you really want to use a palette, I think you can use the UserInterface.activeSelectionChanged event to achieve a similar behavior.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-19D8AC9F-E4F7-4E8A-8B3C-9D128EF932F3 

 

If you don't mind using a modal dialog, you can place a SelectionCommandInput, hide it, and use a BrowserCommandInput, and it will look the same as the palette.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-GUID-GUID-70140D54-8784-4947-A446-DCC85F72A1... 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report