C#ATIA

↑タイトル詐欺 主にFusion360API 偶にCATIA V5 VBA(絶賛ネタ切れ中)

MouseMoveEvent サンプル

やっと出来た・・・この程度?ってレベルなんですけど。

FusionAPIはイベントが利用できるのですが、海外のフォーラムを見ても
後ろ向きな回答ばかり。
Mouse Click Event - Autodesk Community

Solved: How to use KeyDownEvent - Autodesk Community
(tomo1230さん すごいですね)


何となくマウスやキーボードのイベントは、コマンドのダイアログのようなやつを表示させれば
利用できるよ って解釈し、大嫌いなイベント処理に挑戦してみました。

#FusionAPI_python
#Author-kantoku
#Description-MousePositionTest
import adsk.core, adsk.fusion, traceback

app = None
ui  = None
commandId = 'MousePositionTest'
commandName = 'Mouse_Position_Test'
commandDescription = 'マウスカーソル位置表示のテスト'
Input1 = None
handlers = []

class MyCommandDestroyHandler(adsk.core.CommandEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:
            adsk.terminate()
        except:
            if ui:
                ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

class MyCommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:
            cmd = args.command
            onDestroy = MyCommandDestroyHandler()
            cmd.destroy.add(onDestroy)
            handlers.append(onDestroy)
            onMouseMove = MyMouseMoveHandler()
            cmd.mouseMove.add(onMouseMove)
            handlers.append(onMouseMove)
            
            inputs = cmd.commandInputs
            global commandId
            global Input1
            Input1 = inputs.addTextBoxCommandInput(commandId + '_textBox', 'マウスカーソル位置(2D)', "", 1, 

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

def run(context):
    ui = None
    try:
        global app
        app = adsk.core.Application.get()
        global ui
        ui = app.userInterface

        global commandId
        global commandName
        global commandDescription

        cmdDef = ui.commandDefinitions.itemById(commandId)
        if not cmdDef:
            cmdDef = ui.commandDefinitions.addButtonDefinition(commandId, commandName, commandDescription)

        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()))
            
class MyMouseMoveHandler(adsk.core.MouseEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        eventArgs = adsk.core.MouseEventArgs.cast(args)
        pos = eventArgs.position
        global Input1
        Input1.formattedText = "{0} : {1}".format(pos.x, pos.y)

スプリクトを実行するとダイアログ(これは何と呼べば良いのでしょう?)が表示され
マウスを動かすと座標値が変更されます。
f:id:kandennti:20160921190730p:plain
コード的に無駄があるような気もするし、MVVM(不勉強なので理解し切れていません)的
にもイケてないのですが、とりあえず利用できそう。


但しブライアンさんが書いていますが、画面(ディスプレイ)上の座標値なので、3D空間とは
全く無関係な位置になっています。 変換の関数あるのかな?