# Assuming you have not changed the general structure of the template no modification is needed in this file. # from . import commands from tkinter import E from .lib import fusion360utils as futil import threading import adsk.core import adsk.fusion import adsk.cam import traceback import json app = None ui = adsk.core.UserInterface.cast(None) handlers = [] stopFlag = None myCustomEvent = 'MyCustomEventId' customEvent = None # The event handler that responds to the custom event being fired. class ThreadEventHandler(adsk.core.CustomEventHandler): def __init__(self): super().__init__() self.viewport = app.activeViewport def notify(self, args: adsk.core.CustomEventArgs): global app try: # Make sure a command isn't running before changes are made. activeCmd = ui.activeCommand if activeCmd != 'SelectCommand': ui.commandDefinitions.itemById('SelectCommand').execute() camera = self.viewport.camera eye = camera.eye target = camera.target up = camera.upVector zoom = camera.viewExtents front = eye.vectorTo(target) right = up.crossProduct(front) rotate_matrix = adsk.core.Matrix3D.create() rotate_matrix.setWithCoordinateSystem(target, right, front, up) mat = rotate_matrix.asArray() # Get the value from the JSON data passed through the event. eventArgs = json.loads(args.additionalInfo) X_radians = float(eventArgs['X_rotation']) if X_radians: rotate_matrix.setToRotation(X_radians, front, target) eye.transformBy(rotate_matrix) up.transformBy(rotate_matrix) Y_radians = float(eventArgs['Y_rotation']) if Y_radians: rotate_matrix.setToRotation(Y_radians, right, target) eye.transformBy(rotate_matrix) up.transformBy(rotate_matrix) Z_radians = float(eventArgs['Z_rotation']) if Z_radians: rotate_matrix.setToRotation(Z_radians, up, target) eye.transformBy(rotate_matrix) up.transformBy(rotate_matrix) X_pan = float(eventArgs['X_pan']) Y_pan = float(eventArgs['Y_pan']) Z_pan = float(eventArgs['Z_pan']) if any([X_pan, Z_pan]): move_vector = adsk.core.Vector3D.create(X_pan, 0, Z_pan) move_vector.transformBy(rotate_matrix) eye.translateBy(move_vector) target.translateBy(move_vector) if Y_pan: zoom = zoom + Y_pan * 10 camera.isSmoothTransition = True camera.eye = eye camera.upVector = up camera.target = target camera.viewExtents = zoom app.activeViewport.camera = camera app.activeViewport.refresh() except: if ui: ui.messageBox('Failed:\n{}'.format(traceback.format_exc())) def rotate(self, X_radians: float, Y_radians: float, Z_radians: float): pass # The class for the new thread. class MyThread(threading.Thread): def __init__(self, event): threading.Thread.__init__(self) self.stopped = event def run(self): # Every five seconds fire a custom event, passing a random number. while not self.stopped.wait(2): args = {'X_rotation': 0.0, 'Y_rotation': 0.0, 'Z_rotation': 0.0, 'X_pan': 0.0, 'Y_pan': 1.0, 'Z_pan': 0.0} app.fireCustomEvent(myCustomEvent, json.dumps(args)) def run(context): global ui global app try: app = adsk.core.Application.get() ui = app.userInterface # Register the custom event and connect the handler. global customEvent customEvent = app.registerCustomEvent(myCustomEvent) onThreadEvent = ThreadEventHandler() customEvent.add(onThreadEvent) handlers.append(onThreadEvent) # Create a new thread for the other processing. global stopFlag stopFlag = threading.Event() myThread = MyThread(stopFlag) myThread.start() except: futil.handle_error('run') if ui: ui.messageBox('Failed:\n{}'.format(traceback.format_exc())) def stop(context): try: if handlers.count: customEvent.remove(handlers[0]) stopFlag.set() app.unregisterCustomEvent(myCustomEvent) ui.messageBox('Stop addin') except: futil.handle_error('stop') if ui: ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))