Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Auto generate Setup and NC Output

patrikrump
Explorer

Auto generate Setup and NC Output

patrikrump
Explorer
Explorer

Dear Fusion Community,

 

I have tried to write a code where a setup and a NC output is always created for each visible body.

Currently only setup and NC output should be created and named.

 

Unfortunately this does not work although no error is displayed. Can you perhaps help me with this?

Thank you very much

 

 

 

import adsk.core
import adsk.fusion
import traceback

def run(context):
    try:
        # Zugriff auf die aktive Fusion 360-Anwendung
        app = adsk.core.Application.get()
        ui = app.userInterface

        # Zugriff auf das aktive Design
        design = app.activeProduct
        if not design:
            ui.messageBox('Kein aktives Design gefunden. Bitte auf die Konstruktions Seite wechseln')
            return

        # Zugriff auf die aktive Komponente
        root_comp = design.rootComponent

        # Zugriff auf alle sichtbaren Körper im Design
        bodies = root_comp.bRepBodies
        for body in bodies:
            # Erstelle ein Setup für jeden Körper
            setup_name = f'Setup_{body.name}'
            setup = root_comp.manufacturingSetups.addMillSetup(body, setup_name)

            # Setze den Namen des Körpers als Setup-Namen
            setup.name = body.name

            # Erstelle eine NC-Ausgabe mit dem Körpernamen
            nc_program_name = f'NC_{body.name}'
            nc_program = setup.createNCProgram(root_comp, nc_program_name)

        ui.messageBox('Setups und NC-Ausgaben wurden erfolgreich erstellt.')

    except Exception as e:
        ui.messageBox('Fehler aufgetreten:\n{}'.format(traceback.format_exc()))

run(None)

 

 

 

0 Likes
Reply
Accepted solutions (1)
651 Views
6 Replies
Replies (6)

CGBenner
Community Manager
Community Manager

@patrikrump 

Welcome to the Community.

I noticed that your code is in German.  If you would feel more comfortable, I can move this post to the German Fusion forum.  If you are happy to communicate in English, I can leave it here.  Whatever you prefer.  🙂


Chris Benner
Industry Community Manager – Design & Manufacturing


If a response answers your question, please use  ACCEPT SOLUTION  to assist other users later.


Also be generous with Likes!  Thank you and enjoy!


Become an Autodesk Fusion Insider
Inventor/Beta Feedback Project
0 Likes

patrikrump
Explorer
Explorer

I don't care about the language.
It's just important to me that many people see it because of the possible help

0 Likes

a.laasW8M6T
Mentor
Mentor

Hi

This is probably best asked in the API Forum:

https://forums.autodesk.com/t5/fusion-api-and-scripts/bd-p/22

1 Like

patrikrump
Explorer
Explorer

Thank you for moving my post.

unfortunately I have not yet found a solution

0 Likes

kandennti
Mentor
Mentor
Accepted solution

Hi @patrikrump -San.

 

I believe that would be made by chatGPT, etc. It is a pretty disappointing code.

I created a basic sample.
Setup and NC program for each body shown. It is still lacking in functionality.
This can be done from the design workspace.

# Fusion360API Python script

import traceback
import adsk.core as core
import adsk.fusion as fusion
import adsk.cam as cam
import sys

def run(context):
    ui: core.UserInterface = None
    try:
        app: core.Application = core.Application.get()
        ui = app.userInterface

        camObj: cam.CAM = get_cam_product()

        rootOcc: fusion.Occurrence = camObj.designRootOccurrence
        bodies = get_show_bodies(rootOcc.component)
        if len(bodies) < 1: return

        for body in bodies:
            create_nc_program(
                camObj,
                create_setup(
                    camObj,
                    body
                )
            )

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


def create_nc_program(
        camObj: cam.CAM,
        targetSetup: cam.Setup,
) -> cam.NCProgram:
    
    ncPrograms: cam.NCPrograms = camObj.ncPrograms
    ncIpt: cam.NCProgramInput = ncPrograms.createInput()
    ncIpt.displayName = targetSetup.name

    set_cam_parameter(
        ncIpt,
        "nc_program_filename",
        targetSetup.name,
    )

    set_cam_parameter(
        ncIpt,
        "nc_program_openInEditor",
        True,
    )

    ncIpt.operations = [targetSetup]
    ncPrograms.add(ncIpt)


def get_show_bodies(
        root: fusion.Component
) -> list[fusion.BRepBody]:
    
    bodies = root.findBRepUsingPoint(
        core.Point3D.create(0,0,0),
        fusion.BRepEntityTypes.BRepBodyEntityType,
        sys.float_info.max,
        True,
    )

    return list(bodies)


def create_setup(
        camObj: cam.CAM,
        targetBody: fusion.BRepBody,
) -> cam.Setup:

    setups: cam.Setups = camObj.setups
    setupIpt: cam.SetupInput = setups.createInput(
        cam.OperationTypes.MillingOperation
    )
    setupIpt.models = [targetBody]
    setup: cam.Setup = setups.add(setupIpt)
    setup.name = targetBody.name

    set_cam_parameter(
        setup,
        'job_stockMode',
        'default',
    )

    set_cam_parameter(
        setup,
        'job_stockOffsetMode',
        'keep',
    )

    return setup


def get_cam_parameter(
        camEntity,
        name: str,
) -> cam.CAMParameter:
    try:
        prm: cam.CAMParameter = camEntity.parameters.itemByName(
            name
        )
        if not prm: return None

        return prm.value.value

    except:
        return None


def set_cam_parameter(
        camEntity,
        name: str,
        value: str,
) -> bool:
    
    try:
        prm: cam.CAMParameter = camEntity.parameters.itemByName(
            name
        )
        if not prm: return False

        prm.value.value = value

        return True

    except:
        return False


def get_cam_product() -> cam.CAM:

    app: core.Application = core.Application.get()
    activete_cam_env()

    return app.activeProduct


def activete_cam_env() -> None:

    app: core.Application = core.Application.get()
    ui: core.UserInterface = app.userInterface

    camWS: core.Workspace = ui.workspaces.itemById('CAMEnvironment') 
    camWS.activate()
0 Likes

patrikrump
Explorer
Explorer

kandennti thank you so much for this great help,

 

The code does what it should, I have to create the appropriate edits depending on the requirements of the furniture.

 

I have an average of 8 to 12 setups per piece of furniture, so this work is very annoying. Thanks to your script I can concentrate on the work and not on the organization.

 

Thank you very much

1 Like