Announcements

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

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: 

Is there a better way to detect when user picks a named view?

5 REPLIES 5
Reply
Message 1 of 6
soswow
340 Views, 5 Replies

Is there a better way to detect when user picks a named view?

my current solution is to hook up to camera changed event and then on each event go through all named views and compare their camera parameters (eye, target, up_Vector) with the current view camera. It is very hacky and generates multiple events. All doable, but I thought maybe there was a more elegant way.

5 REPLIES 5
Message 2 of 6
kandennti
in reply to: soswow

Hi @soswow -San.

 

Fusion360 executes commands for many operations.

The UserInterface.commandStarting event is fired when a command is executed.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-EBB6C82A-A256-4AB7-9A86-0F7A9653A7E9 

 

By using the UserInterface.activeCommand property, the ID of the command being executed can be known.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-635C6C94-BA41-44D6-A97D-61BFA56C6688 

 

If you click here, “RestoreCameraCommand” is executing.
1.png

Message 3 of 6
soswow
in reply to: soswow

@kandennti -San,
Thank you! How do I know which of named views was clicked though? I've looked through 

ApplicationCommandEventArgs and all it's tree and I can't seem to understand how do I get to which view was selected.
Message 4 of 6
soswow
in reply to: soswow

It's ok. I've seen some of your other answers for similar question and it sounds like there is no way to get input information for command this way. I guess that just means I can't use named views as an input for my purposes.

I'd find another way.

Message 5 of 6
kandennti
in reply to: soswow

@soswow -San.

 

After the command occurs, we created a sample that compares the current Camera with the Camera in the NamedView and determines that the one that matches is the one that was clicked.

#FusionAPI_python addin
import traceback
import adsk.core as core
import adsk.fusion as fusion

_app = core.Application.cast(None)
_ui  = core.UserInterface.cast(None)
_handlers = []

_cmdInfo = {
    "id": "kantoku_DumpPickNamedViewTest",
    "name": "DumpPickNamedViewTest",
    "tooltip": "DumpPickNamedViewTest",
    "workSpace": "FusionSolidEnvironment",
    "panelId": "SolidScriptsAddinsPanel",
}


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

        cmdDefs: core.CommandDefinitions = _ui.commandDefinitions

        global _cmdInfo
        cmdDef: core.CommandDefinition = cmdDefs.itemById(_cmdInfo["id"])
        if cmdDef:
            cmdDef.deleteMe()

        cmdDef = cmdDefs.addButtonDefinition(
            _cmdInfo["id"],
            _cmdInfo["name"],
            _cmdInfo["tooltip"])

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

        workSpace: core.Workspace = _ui.workspaces.itemById(_cmdInfo["workSpace"])
        targetpanel :core.ToolbarPanel = workSpace.toolbarPanels.itemById(_cmdInfo["panelId"])

        controls :core.ToolbarControls = targetpanel.controls
        cmdControl :core.ToolbarPanel = controls.addCommand(cmdDef)

        cmdControl.isVisible = True

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


class CommandCreatedHandler(core.CommandCreatedEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:
            global _app, _ui, _handlers
            cmd = core.Command.cast(args.command)
            des :fusion.Design = _app.activeDocument

            core.Camera.isEqual = isEqual

            # event
            onCommandTerminated = MyCommandTerminatedHandler()
            _ui.commandTerminated.add(onCommandTerminated)
            _handlers.append(onCommandTerminated)

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


class MyCommandTerminatedHandler(core.ApplicationCommandEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args: core.ApplicationCommandEventArgs):
        if not _ui.activeCommand == "RestoreCameraCommand": return

        stateCam: core.Camera = _app.activeViewport.camera

        des: fusion.Design = _app.activeProduct
        views: core.NamedViews = des.namedViews
        view: core.NamedView = None
        for view in views:
            if stateCam.isEqual(view.camera):
                _app.log(f"Pick NamedView {view.name}!!")
                return


def isEqual(self: core.Camera, cam: core.Camera) -> bool:
    if not self.cameraType == cam.cameraType:
        return False
    if not self.eye.isEqualTo(cam.eye):
        return False
    if not self.target.isEqualTo(cam.target):
        return False
    if not self.upVector.isEqualTo(cam.upVector):
        return False
    if abs(self.perspectiveAngle - cam.perspectiveAngle) > 0.001:
        return False

    return True


def stop(context):
    try:
        global _cmdInfo
        workSpace: core.Workspace = _ui.workspaces.itemById(_cmdInfo["workSpace"])
        panel: core.ToolbarPanel = workSpace.toolbarPanels.itemById(_cmdInfo["panelId"])

        if panel:
            panel.controls.itemById(_cmdInfo["id"]).deleteMe()

        cmdDefs: core.CommandDefinitions = _ui.commandDefinitions
        cmdDef: core.CommandDefinition = cmdDefs.itemById(_cmdInfo["id"])
        if cmdDef:
            cmdDef.deleteMe()

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

1.png

 

Since Design.namedViews contains only the NamedViews that you have created yourself, if you need to detect the use of the default one, you should check the Camera data in advance.

Message 6 of 6
soswow
in reply to: kandennti

oh yes! Smart people think alike I suppose 😃 I've come up with the same thing. Actually, I found even nicer solution for my case. 

 

Basically, I decided not to worry about named views entirely. Through naming convention and way to detect when canvas is turned on and off, I can move the camera to the required frame, since I have all cameras information when user selects specific convas. And also I hide canvas when camera is moved away.

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

Post to forums  

Autodesk Design & Make Report