Here's a script that will process the active setup and go through the operations. It posts the operations as sets, where a set is defined as operations that are sequential and use the same tool.
In my test I had the following 6 operations.
Pocket1 (Tool 1)
Pocket2 (Tool 2)
Pocket3 (Tool 2)
Pocket4 (Tool 1)
Pocket5 (Tool 2)
Pocket6 (Tool 2)
After running this I ended up with 4 NC files
1001 - Pocket1
1002 - Pocket2 and Pocket3
1003 - Pocket 4
1004 - Pocket 5 and Pocket 6
You'll need to edit which post-processor it uses and you'll likely want to change the logic for the naming of the output files but besides that, I think it should do what you want. For my use, I'll probably change the filename so it includes the description of the tool used for that set. The code shows how to get the description of the tool but currently isn't using it anywhere.
import adsk.core, adsk.fusion, adsk.cam, traceback
import os, json
app = adsk.core.Application.get()
ui = app.userInterface
def run(context):
try:
# Verify that the CAM product is active.
cam = adsk.cam.CAM.cast(app.activeProduct)
if cam is None:
ui.messageBox('The MANUFACTURE workspace must be active.')
# Get the currently active setup.
setup: adsk.cam.Setup = None
for setup in cam.setups:
if setup.isActive:
break
# Get the output folder.
folderDialog = ui.createFolderDialog()
folderDialog.title = 'Select output folder'
dlgResults = folderDialog.showDialog()
if dlgResults != adsk.core.DialogResults.DialogOK:
return
else:
resultFolder = folderDialog.folder
# Define the common post information.
postNumber = 1001
units = adsk.cam.PostOutputUnitOptions.DocumentUnitsOutput
post = os.path.join(cam.genericPostFolder, 'fanuc.cps')
# Iterate through the operations, posting those that are sequential in order
# and use the same tool in the same output file.
op: adsk.cam.Operation
currentTool = ''
currentOps = adsk.core.ObjectCollection.create()
for op in setup.operations:
tool = op.toolJson
if currentTool == tool:
# This operation uses the same tool as the previous operations
# so add it to the list and keep processing.
currentOps.add(op)
else:
# This operation uses a different tool than previous operations
# so post the previous operations or if this is the first operation
# start processing.
if currentOps.count > 0:
# Define the post information.
postInput = adsk.cam.PostProcessInput.create(str(postNumber), post, resultFolder, units)
postInput.isOpenInEditor = False
postInput.programComment = getToolComment(currentOps)
postNumber += 1
# Post the operations.
cam.postProcess(currentOps, postInput)
# Clear the list and add this operation to start the next set.
currentOps.clear()
currentOps.add(op)
currentTool = op.toolJson
else:
currentTool = tool
currentOps.add(op)
# Post the final set of operations.
toolJson = json.loads(currentTool)
toolDesc = toolJson['description']
postInput = adsk.cam.PostProcessInput.create(str(postNumber), post, resultFolder, units)
postInput.isOpenInEditor = False
postInput.programComment = getToolComment(currentOps)
cam.postProcess(currentOps, postInput)
ui.messageBox('Finished posting.')
except:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
def getToolComment(currentOps: adsk.core.ObjectCollection) -> str:
op: adsk.cam.Operation
comment = ''
for op in currentOps:
if comment == '':
comment = f'Post for "{op.name}"'
else:
comment = f'{comment}, "{op.name}"'
return comment
---------------------------------------------------------------
Brian EkinsInventor and Fusion 360 API Expert
Website/Blog:
https://EkinsSolutions.com