Message 1 of 7
Connecting command created handler to command execute handler
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi, I am new to fusion API and am trying to make a simple script that will take inputs from the user and output a propeller hub. I've been able to create the geometry and the dialog box but can't seem to connect the two together. I am using the work flow described on this page http://help.autodesk.com/view/fusion360/ENU/?guid=GUID-3922697A-7BF1-4799-9A5B-C8539DF57051 For some reason the user inputs do not connect to the geometry and no part is produced unless the values are defined in the Command Execute Handler. My code is posted below with the area of interest in green font. Any help or other examples of a UI customization would be greatly appreciated.
Thank you!
import adsk.core, adsk.fusion, adsk.cam, traceback # Global list to keep all event handlers in scope. # This is only needed with Python. handlers = [] def run(context): ui = None try: app = adsk.core.Application.get() ui = app.userInterface # Get the CommandDefinitions collection. cmdDefs = ui.commandDefinitions # Create a button command definition. propButton = cmdDefs.addButtonDefinition('propButtonId', 'Create Propeller', 'Propeller button tooltip') # Connect to the command created event. propCommandCreated = propCommandCreatedEventHandler() propButton.commandCreated.add(propCommandCreated) handlers.append(propCommandCreated) # Execute the command. propButton.execute() # Keep the script running. adsk.autoTerminate(False) except: if ui: ui.messageBox('Failed:\n{}'.format(traceback.format_exc())) def stop(context): try: app = adsk.core.Application.get() ui = app.userInterface # Delete the command definition. cmdDef = ui.commandDefinitions.itemById('propButtonId') if cmdDef: cmdDef.deleteMe() except: if ui: ui.messageBox('Failed:\n{}'.format(traceback.format_exc())) # Event handler for the commandCreated event. class propCommandCreatedEventHandler(adsk.core.CommandCreatedEventHandler): def __init__(self): super().__init__() def notify(self, args): # Verify that a sketch is active. app = adsk.core.Application.get() eventArgs = adsk.core.CommandCreatedEventArgs.cast(args) # Get the command cmd = eventArgs.command # Get the CommandInputs collection to create new command inputs. inputs = cmd.commandInputs app = adsk.core.Application.get() des = adsk.fusion.Design.cast(app.activeProduct) inputs.addValueInput('outerDiameter', 'outer diameter', 'cm', adsk.core.ValueInput.createByReal(5.0)) inputs.addValueInput('innerDiameter','inner diameter','cm',adsk.core.ValueInput.createByReal(2.0)) inputs.addValueInput('height','height','cm',adsk.core.ValueInput.createByReal(1.0)) # Connect to the execute event. onExecute = propCommandExecuteHandler() cmd.execute.add(onExecute) handlers.append(onExecute) # Connect to the inputChanged event. onInputChanged = propCommandInputChangedHandler() cmd.inputChanged.add(onInputChanged) handlers.append(onInputChanged) # Event handler for the inputChanged event. class propCommandInputChangedHandler(adsk.core.InputChangedEventHandler): def __init__(self): super().__init__() def notify(self, args): eventArgs = adsk.core.InputChangedEventArgs.cast(args) # Event handler for the execute event. class propCommandExecuteHandler(adsk.core.CommandEventHandler): def __init__(self): super().__init__() def notify(self, args): eventArgs = adsk.core.CommandEventArgs.cast(args) # Get the values from the command inputs. inputs = eventArgs.command.commandInputs unitsMgr = app.activeProduct.unitsManager OD = inputs.itemById('outerDiameter').value ID = inputs.itembyId('innerDiameter').value h = inputs.itembyId('height').value #There is something wrong with getting the values from the #CommandCreatedEventHanlder. When the values are defined as below it #works fine but nothing happens when the values are defined as above #OD = 6 #ID = adsk.core.ValueInput.createByString('4 cm') #h = adsk.core.ValueInput.createByString('5 cm') createHub(OD,ID,h) def createHub(outerDiameter, innerDiameter, distance):#def drawTriangle(baseLength, heightScale): app = adsk.core.Application.get() ui = app.userInterface doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType) design = app.activeProduct #what's open at the time # Get the root component of the active design. rootComp = design.rootComponent #sets up the top level component of the design in fusion # Get extrude features extrudes = rootComp.features.extrudeFeatures # Create a new sketch on the xy plane. sketches = rootComp.sketches #sketches comes from the object model(that big tree) xyPlane = rootComp.xYConstructionPlane # tell it to use xy plane sketch = sketches.add(xyPlane) #take sketches object and add it to the xy plane # Draw some circles. circles = sketch.sketchCurves.sketchCircles circle1 = circles.addByCenterRadius(adsk.core.Point3D.create(0, 0, 0), outerDiameter/2) #get sketch profile prof = sketch.profiles.item(0) #distance = adsk.core.ValueInput.createByString('5 cm') extrude1 = extrudes.addSimple(prof, distance, adsk.fusion.FeatureOperations.NewBodyFeatureOperation) # Get the extrusion body body1 = extrude1.bodies.item(0) body1.name = "Hub" # Get the end face of the extrusion endFaces = extrude1.endFaces endFace = endFaces.item(0) # Create a construction plane by offsetting the end face planes = rootComp.constructionPlanes planeInput = planes.createInput() offsetVal = adsk.core.ValueInput.createByString('0 cm') planeInput.setByOffset(endFace, offsetVal) offsetPlane = planes.add(planeInput) # Create a sketch on the new construction plane and add a sketch point offsetSketch = sketches.add(offsetPlane) offsetSketchPoints = offsetSketch.sketchPoints sPt0 = offsetSketchPoints.add(adsk.core.Point3D.create(0, 0, 0)) # Create a hole input holes = rootComp.features.holeFeatures holeInput = holes.createSimpleInput(innerDiameter) holeInput.setPositionBySketchPoint(sPt0) holeInput.setDistanceExtent(distance) hole = holes.add(holeInput)