how to use mouseDown , Up , click , move Handlers

Anonymous

how to use mouseDown , Up , click , move Handlers

Anonymous
Not applicable

i need script tutorial how to use mouse Down , Up , click ,double clicks , move   Handlers in sketch mode to get position and insert point

Thanks

0 Likes
Reply
Accepted solutions (2)
576 Views
4 Replies
Replies (4)

kandennti
Mentor
Mentor

Hi chaibi_mustapha.

 

I've tried 'MouseMoveEvent' in the past.
(Sorry, it is in Japanese)

http://kantoku.hatenablog.com/entry/2018/06/04/191443 

 

The coordinate value obtained with MouseEventArgs.position is the coordinate value on the screen.
http://help.autodesk.com/view/fusion360/ENU/?guid=GUID-a8f311ac-e642-463a-96d4-778e6ae99cbd 

 

In order to get the coordinate value of 3D space, it is necessary to calculate using 'Viewport.camera'.
http://help.autodesk.com/view/fusion360/ENU/?guid=GUID-2358aa1b-6965-4e46-b6af-d38e6e2f0f68 

 

It takes a lot of work.


If you simply want to create a sketch point at the clicked position, this method is easy.

#Fusion360API python
import adsk.core, adsk.fusion, traceback

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
       
        #click
        msg = 'Click on the surface'
        sel = ui.selectEntity(msg,'Faces')
        if sel is None:
            return
        clickPoint = sel.point

        #sketch
        root = adsk.fusion.Component.cast(app.activeProduct.rootComponent)
        skt = root.sketches.add(root.xYConstructionPlane)
        skt.sketchPoints.add(clickPoint)

        ui.messageBox('done')

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

Anonymous
Not applicable

Hi ,

thanks  kandennti

i try your code but the mouse move handler not work (no reaction)

this is my code :

 

import adsk.core, adsk.fusion, adsk.cam, traceback
 
tbPanel = None
handlers = []

# //////////////////////////////////////// EventHandlers ///////////////////////////////////////////////////
# Event handler for the commandCreated event.
class MyCommandCreatedHandler(adsk.core.CommandCreatedEventHandler😞
    def __init__(self😞
        super().__init__()
    def notify(selfargs😞

        eventArgs = adsk.core.CommandCreatedEventArgs.cast(args)
        cmd = eventArgs.command
        app = adsk.core.Application.get()
        ui = app.userInterface
        ui.messageBox('Event Create Handler OK')

        #  _______________________________  Connect to Events handler _______________________

        # Connect to the command destroyed event.
        onDestroy = MyCommandDestroyHandler()
        cmd.destroy.add(onDestroy)
        handlers.append(onDestroy)

        # Connect to the Mouse Up event
        onMouseMove = MyMouseMoveHandler()
        cmd.mouseMove.add(onMouseMove)
        handlers.append(onMouseMove)
         

# Event handler for the execute event.----------------------------------------------------------------
class MyCommandExecuteHandler(adsk.core.CommandEventHandler😞
    def __init__(self😞
        super().__init__()
    def notify(selfargs😞
        
        # Code to react to the event.
        eventArgs = adsk.core.CommandEventArgs.cast(args)
        cmd = eventArgs.command
        app = adsk.core.Application.get()
        ui  = app.userInterface
        ui.messageBox('In command execute event handler.')
        
     
# Event MouseMoveHandler.----------------------------------------------------------------     
class MyMouseMoveHandler(adsk.core.MouseEventHandler😞
    def __init__(self😞
        super().__init__()
    def notify(selfargs😞
        eventArgs = adsk.core.MouseEventArgs.cast(args)
        cmd = eventArgs.firingEvent.sender
        inputs = cmd.commandInputs  
        app = adsk.core.Application.get()     
        ui  = app.userInterface
        ui.messageBox('In command Mouse Down event handler.')

        #click
        msg = 'Click on the Sketch'
        sel = ui.selectEntity(msg,'SketchCurves'
        if sel is None:
            return
        clickPoint = sel.point

        #sketch
        root = adsk.fusion.Component.cast(app.activeProduct.rootComponent)
        skt = root.sketches.add(root.xYConstructionPlane)
        skt.sketchPoints.add(clickPoint)


# Event handler that reacts to when the command is destroyed. This terminates the script.---------------            
class MyCommandDestroyHandler(adsk.core.CommandEventHandler😞
    def __init__(self😞
        super().__init__()
    def notify(selfargs😞
        try:
            adsk.terminate()
        except:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))


#/////////////////////////////////////////////// My Class ////////////////////////////////////////////////

def run(context😞
    ui = None
    try:
        app = adsk.core.Application.get()
        ui = app.userInterface
                     
        workSpace = ui.workspaces.itemById('FusionSolidEnvironment')
        tbPanels = workSpace.toolbarPanels
        # Add new tbPanel
        MyPanel = tbPanels.itemById('MyPanel')
        if not MyPanel: 
       
           MyPanel = tbPanels.add('MyPanel''My Panel''Select'False)
          
        # Vérif si les commands existe
        cmdDefButton = ui.commandDefinitions.itemById('cmdDefButton')
               
        if not cmdDefButton :
           
            #Create a button command definition.
            cmdDefButton = ui.commandDefinitions.addButtonDefinition('cmdDefButton''MyButton','tooltip')
 
            # Connect to the command created event.
            commandCreated = MyCommandCreatedHandler()
            cmdDefButton.commandCreated.add(commandCreated)
            handlers.append(commandCreated)
           
            # Add the buttons to the bottom of the panel.------------------------------
            cntrlButton = MyPanel.controls.addCommand(cmdDefButton)
            # Make the button available in the panel.
            cntrlButton.isPromotedByDefault = True
            cntrlButton.isPromoted = True

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


# ---------------------------------------------------------------------------------
def stop(context😞
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        ui.messageBox('STOP')
        
        # Clean up the UI Remove the CommandDefinitions and buttons.
        cmdDefButton = ui.commandDefinitions.itemById('cmdDefButton')
        if cmdDefButton :
            cmdDefButton.deleteMe()
                   
        MyPanel = ui.allToolbarPanels.itemById('MyPanel')

        cntrlButton = MyPanel.controls.itemById('cmdDefButton')
        if cntrlButton:
           cntrlButton.deleteMe()
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc())) 


0 Likes

kandennti
Mentor
Mentor
Accepted solution

I hardly understand English.
I hope that you will communicate well.

 

I have never made an add-in.
Therefore, I am not good at event processing.
I remembered,When using events, a dialog must be shown.

・・・
class MyCommandCreatedHandler(adsk.core.CommandCreatedEventHandler):

・・・
        # Connect to the Mouse Up event
        onMouseMove = MyMouseMoveHandler()
        cmd.mouseMove.add(onMouseMove)
        handlers.append(onMouseMove)

        #When using events, a dialog must be shown!!!
        inputs = cmd.commandInputs
        inputs.addTextBoxCommandInput('dammy', 'dammy', 'dammy', 1, True)


Also, I don't know the details, but command.inputs and
I don't think userInterface.selectEntity can be used together.

・・・
class MyMouseMoveHandler(adsk.core.MouseEventHandler):
   ・・・   
        ui  = app.userInterface
        ui.messageBox('In command Mouse Move event handler.')

        #An error will occur after this point.
        #I don't know in detail, but command.inputs and
        #I think userInterface.selectEntity cannot be used together.

        #click
        msg = 'Click on the Sketch'
        sel = ui.selectEntity(msg,'SketchCurves') 
 ・・・

 

Attach the file.

 

0 Likes

Anonymous
Not applicable
Accepted solution

Thank you very much , Kandennti

 

0 Likes