New CAM Operation API Sample

Description

Demonstrates adding a new toolpath into the document using an existing CAM template. You can get a sample template here. To use the sample, create a new design and add a block. Switch to the MANUFACTURE workspace and create a setup. Run the script and it will create a new facing operation in the setup.

Code Samples

#include <Core/CoreAll.h>
#include <Fusion/FusionAll.h>
#include <CAM/CAMAll.h>

using namespace adsk::core;
using namespace adsk::fusion;
using namespace adsk::cam;

extern "C" XI_EXPORT bool run(const char* context)
{
    Ptr<Application> app = Application::get();
    Ptr<UserInterface> ui = app->userInterface();
    // Get the CAM product
    Ptr<CAM> cam = app->activeDocument()->products()->itemByProductType("CAMProductType");

    // Define the path for the template
    std::string path = "E://face.f3dhsm-template";

    // iterate through each setup and insert the template into the setup
    for each (Ptr<Setup> setup in cam->setups())
    {
        // Add the template to each setup.
        Ptr<ObjectCollection> results = setup->createFromTemplate(path);

        // Get the operation that was created.What's created will
        // vary depending on what's defined in the template so you
        // may need more logic to find the result you want.
        Ptr<Operation> operation = results->item(0);

        operation->name("API added operation");
    }

    // Generate all toolpaths, skipping any that are already valid
    cam->generateAllToolpaths(true);
    return true;
}
import adsk.core, adsk.cam, os, traceback

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui = app.userInterface
        doc = app.activeDocument
        products = doc.products
        # Get the CAM product
        cam = adsk.cam.CAM.cast(products.itemByProductType("CAMProductType"))

        # List of all setups
        setups = cam.setups
        
        # Specify the full filename of the template.
        templateFilename = 'E:\\face.f3dhsm-template'
        
        # Check if the template exists (from the path specified above). Show an error if it doesn't exist.
        if not os.path.exists(templateFilename):
            ui.messageBox("The template '" + templateFilename + "' does not exist")
            return

        # Go through each setup in the document
        for setup in setups:
            # Add the template to each setup.
            templateInput = adsk.cam.CreateFromCAMTemplateInput.create()
            camTemplate = adsk.cam.CAMTemplate.createFromFile(templateFilename)
            templateInput.camTemplate = camTemplate
            results = setup.createFromCAMTemplate2(templateInput)

            # Get the operation that was created. What's created will
            # vary depending on what's defined in the template so you
            # may need more logic to find the result you want.
            operation = results[0]

            # Change the operation name
            operation.name = "API added operation"
        
        # Generate all toolpaths, skipping any that are already valid
        cam.generateAllToolpaths(True)

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))