Announcements

Starting in December, we will archive content from the community that is 10 years and older. This FAQ provides more information.

Custom Graphics mouse events?

thomasa88
Advocate
Advocate

Custom Graphics mouse events?

thomasa88
Advocate
Advocate

Hi, is it possible to get mouse events from Custom Graphics?

 

I want to react to a user hovering the mouse over or clicking on a custom graphic element (e.g. Mesh or Point).

 

0 Likes
Reply
206 Views
2 Replies
Replies (2)

kandennti
Mentor
Mentor

Hi @thomasa88 -San.

 

It is not just custom graphics, but you can do it by using preSelect, preSelectMouseMove, preSelectStart, etc.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-0550963a-ff63-4183-b0a7-a1bf0c99f821 


The following sample uses SelectionCommandInput and PreSelectStart and PreSelectEnd to display the minimum and maximum coordinates of the BoundingBox when the mouse cursor hovers over the custom graphic mesh.

# Fusion360API Python script

import traceback
import adsk
import adsk.core as core
import adsk.fusion as fusion

_app: core.Application = None
_ui: core.UserInterface = None
_handlers = []

CMD_INFO = {
    "id": "kantoku_test",
    "name": "test",
    "tooltip": "test"
}

_selIpt: core.SelectionCommandInput = None
_txtIpt: core.TextBoxCommandInput = None


class MyCommandCreatedHandler(core.CommandCreatedEventHandler):
    def __init__(self):
        super().__init__()

    def notify(self, args: core.CommandCreatedEventArgs):
        core.Application.get().log(args.firingEvent.name)
        try:
            global _handlers
            cmd: core.Command = core.Command.cast(args.command)
            cmd.isOKButtonVisible = False

            onDestroy = MyCommandDestroyHandler()
            cmd.destroy.add(onDestroy)
            _handlers.append(onDestroy)

            onPreSelectStart = MyPreSelectStartHandler()
            cmd.preSelectStart.add(onPreSelectStart)
            _handlers.append(onPreSelectStart)

            onPreSelectEnd = MyPreSelectEndHandler()
            cmd.preSelectEnd.add(onPreSelectEnd)
            _handlers.append(onPreSelectEnd)

            inputs: core.CommandInputs = cmd.commandInputs

            global _selIpt
            _selIpt = inputs.addSelectionInput(
                "selIptId",
                "Hover to CustomGraphicsMesh",
                "Hover to CustomGraphicsMesh",
            )
            _selIpt.isVisible = False

            global _txtIpt
            _txtIpt = inputs.addTextBoxCommandInput(
                "txtIpt",
                "CustomGraphicsMesh\n BoundingBox",
                "-",
                4,
                True
            )

        except:
            _ui.messageBox("Failed:\n{}".format(traceback.format_exc()))


class MyPreSelectStartHandler(core.SelectionEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args: core.SelectionEventArgs):
        args.isSelectable = False # not working
        if args.selection.entity.classType() == fusion.CustomGraphicsMesh.classType():
            
            cGMesh: fusion.CustomGraphicsMesh = args.selection.entity
            bBox: core.BoundingBox3D = cGMesh.boundingBox

            global _txtIpt
            _txtIpt.text = f"Max{bBox.maxPoint.asArray()}\nMin{bBox.minPoint.asArray()}"


class MyPreSelectEndHandler(core.SelectionEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args: core.SelectionEventArgs):
        global _txtIpt
        _txtIpt.text = "-"


class MyCommandDestroyHandler(core.CommandEventHandler):
    def __init__(self):
        super().__init__()

    def notify(self, args: core.CommandEventArgs):
        core.Application.get().log(args.firingEvent.name)
        adsk.terminate()


def run(context):
    try:
        global _app, _ui
        _app = core.Application.get()
        _ui = _app.userInterface

        cmdDef: core.CommandDefinition = _ui.commandDefinitions.itemById(
            CMD_INFO["id"]
        )

        if not cmdDef:
            cmdDef = _ui.commandDefinitions.addButtonDefinition(
                CMD_INFO["id"],
                CMD_INFO["name"],
                CMD_INFO["tooltip"]
            )

        global _handlers
        onCommandCreated = MyCommandCreatedHandler()
        cmdDef.commandCreated.add(onCommandCreated)
        _handlers.append(onCommandCreated)

        cmdDef.execute()

        adsk.autoTerminate(False)

    except:
        if _ui:
            _ui.messageBox("Failed:\n{}".format(traceback.format_exc()))

 

1.png

1 Like

thomasa88
Advocate
Advocate

Thank you, kandetti!

 

I would ideally like to handle mouse events without having to execute a Command, but I might be able to adjust my code.

1 Like