Post CAM Operations by grouping them by Tool ?

Post CAM Operations by grouping them by Tool ?

GermanWings
Advocate Advocate
543 Views
3 Replies
Message 1 of 4

Post CAM Operations by grouping them by Tool ?

GermanWings
Advocate
Advocate

Hello, 

I am building a script that posts operation from a setup , the operation can be single or multiple operations from a setup but not all the operations may be posted. I built a script which uses cam.postProcess() method to post single operations. However if 2 operations use the same tool I get 2 Different Post Output. I want to group consecutive operations based on same tools to be output in a single cam.postProcess output. I aslo could not get Tool Information from the operation object. What am I missing ? 

 

Thanks,

Bhavar 🌸🌸🌸

0 Likes
544 Views
3 Replies
Replies (3)
Message 2 of 4

BrianEkins
Mentor
Mentor

Unfortunately, the CAM API is not complete and tool information is something is missing. 

 

However, there is one undocumented and unsupported property on the Operation object that returns the tool information as a JSON string. You can use that to determine if two operations are using the same tool.  For example:

 

 

if op1.toolJson == op2.toolJson:
    ui.messageBox('They use the same tool.')
else:
    ui.messageBox('The do not use the same tool.')

 

 

Because this is not supported, there is a chance it could go away at some point but I think if that happens the API should have formal support for tool information and you can easily switch to that.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes
Message 3 of 4

GermanWings
Advocate
Advocate

Thanks, for sharing this information I will check what is inside tooJson ? Property.  I also went through the Documents and found no way to post 2 operations together , any information on that ? 

Thanks 🌸

0 Likes
Message 4 of 4

BrianEkins
Mentor
Mentor

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 Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com