python extrude, component's sketch goes invisible, then component is out of sync
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I'm trying to extrude a profile contained in an included component.
It works OK from the user interface. In an empty design, include the attached file. The single component has a single sketch containing two profiles. Select one or both of the profiles and extrude a 1mm body. The sketch in the included component remains visible. I expected this behavior, sketch visibility for included components should be locked.
If done through the python script below, the extrusion works but the sketch containing the profile goes invisible. The component is then shown as out-of-date, which isn't surprising since it is now different from the reference component. Requesting an update of the out-of-date component makes the out-of-date indication go away, but the sketch remains invisible.
As a work-around, the script can set isVisible to True after the extrude. Try again but check "Restore Visibility" in the command dialog. It's not perfect, since the component now shows as out-of-date. A user of the script would be surprised and confused by this behavior.
# -*- coding: utf-8 -*- """ Created on Tue Aug 28 07:56:27 2018 @author: metd01567 """ import adsk.core, adsk.fusion, traceback # Global set of event handlers to keep them referenced for the duration of the command _handlers = [] # 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(self, args): try: app = adsk.core.Application.get() ui = app.userInterface # When the command is done, terminate the script # This will release all globals which will remove all event handlers adsk.terminate() except: if ui: ui.messageBox('Failed:\n{}'.format(traceback.format_exc())) # Event handler that reacts when the command definition is executed which # results in the command being created and this event being fired. class MyCommandCreatedHandler(adsk.core.CommandCreatedEventHandler): def __init__(self): super().__init__() def notify(self, args): try: # check workspace, must be in Model app = adsk.core.Application.get() ui = app.userInterface product = app.activeProduct design = adsk.fusion.Design.cast(product) if not design: ui.messageBox('This script is not supported in current workspace, please change to MODEL workspace and try again.') return False # Get the command that was created. cmd = adsk.core.Command.cast(args.command) ################################### # create and register event handlers onDestroy = MyCommandDestroyHandler() cmd.destroy.add(onDestroy) _handlers.append(onDestroy) onExecute = MyExecuteHandler() cmd.execute.add(onExecute) _handlers.append(onExecute) ################################### # add controls # Create a selection input for extrude profiles profileSelectionInput = cmd.commandInputs.addSelectionInput('profileSelection', 'Profiles', 'The selected profiles will be extruded') profileSelectionInput.setSelectionLimits(1) profileSelectionInput.addSelectionFilter("Profiles") # Create a checkbox input for restoring sketch visibility restoreVisibilityInput = cmd.commandInputs.addBoolValueInput('restoreVisibility', 'Restore Visibility', True) restoreVisibilityInput.value = False except: if ui: ui.messageBox('Failed:\n{}'.format(traceback.format_exc())) def run(context): try: app = adsk.core.Application.get() ui = app.userInterface # Get the existing command definition or create it if it doesn't already exist. cmdDef = ui.commandDefinitions.itemById('testExtruder') if not cmdDef: cmdDef = ui.commandDefinitions.addButtonDefinition('testExtruder', 'Test Extruder', 'Command to extrude.') # Connect to the command created event. onCommandCreated = MyCommandCreatedHandler() cmdDef.commandCreated.add(onCommandCreated) _handlers.append(onCommandCreated) # Execute the command definition. cmdDef.execute() # Prevent this module from being terminated when the script returns, because we are waiting for event handlers to fire. adsk.autoTerminate(False) except: if ui: ui.messageBox('Failed:\n{}'.format(traceback.format_exc())) # Event handler that reacts to any changes the user makes to any of the command inputs. class MyExecuteHandler(adsk.core.CommandEventHandler): def __init__(self): super().__init__() def notify(self, args): try: app = adsk.core.Application.get() ui = app.userInterface eventArgs = adsk.core.CommandEventArgs.cast(args) # call the executor and terminate doExtrude(eventArgs.command.commandInputs) adsk.terminate() except: if ui: ui.messageBox('Failed:\n{}'.format(traceback.format_exc())) # get the profile input, and extrude it def doExtrude(inputs): app = adsk.core.Application.get() ui = app.userInterface product = app.activeProduct design = adsk.fusion.Design.cast(product) comp = design.rootComponent ############################################### # verify selections profileSelectionInput = inputs.itemById('profileSelection') if not profileSelectionInput: return False restoreVisibilityInput = inputs.itemById("restoreVisibility") if not restoreVisibilityInput: return False restoreVisibility = restoreVisibilityInput.value ############################################### # copy the profiles into a collection selectedProfiles = adsk.core.ObjectCollection.create() for thisEntity in range(profileSelectionInput.selectionCount): selectedProfiles.add(profileSelectionInput.selection(thisEntity).entity) if selectedProfiles == None: ui.messageBox('software error: profile selection could not be used') return False ############################################### # create a thin extrusion ############################################### # extrude a thin body from the reference profile. thickness is arbitrary, but keep it well above the model's resolution thickness = adsk.core.ValueInput.createByReal(0.1) extrude = comp.features.extrudeFeatures.addSimple(selectedProfiles, thickness, adsk.fusion.FeatureOperations.NewBodyFeatureOperation) if restoreVisibility: selectedProfiles[0].parentSketch.isVisible = True return True