Auto generate Setup and NC Output

Auto generate Setup and NC Output

patrikrump
Participant Participant
1,305 Views
10 Replies
Message 1 of 11

Auto generate Setup and NC Output

patrikrump
Participant
Participant

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
Accepted solutions (3)
1,306 Views
10 Replies
Replies (10)
Message 2 of 11

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.  🙂

Did you find a post helpful? Then feel free to give likes to these posts!
Did your question get successfully answered? Then just click on the 'Accept solution' button.  Thanks and Enjoy!



Chris Benner

Community Manager - NAMER / D&M


0 Likes
Message 3 of 11

patrikrump
Participant
Participant

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
Message 4 of 11

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

Andrew Laas
Senior Machinist, Scott Automation


EESignature

Message 5 of 11

patrikrump
Participant
Participant

Thank you for moving my post.

unfortunately I have not yet found a solution

0 Likes
Message 6 of 11

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
Message 7 of 11

patrikrump
Participant
Participant

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

Message 8 of 11

patrikrump
Participant
Participant
Accepted solution

@kandennti 

 

I tried to insert my cam templates directly into the setup creation, but unfortunately without any success. Is there any way to do this?

I use Fusion but without the manufacturing extension.

 

 

0 Likes
Message 9 of 11

kandennti
Mentor
Mentor
Accepted solution

Hi @patrikrump -san.

No extensions are required.

I create a setup, load the CAM template, and run the calculations.

# Fusion360API Python script

import traceback
import adsk.core as core
import adsk.fusion as fusion
import adsk.cam as cam
import sys
from pathlib import Path

def run(context):
    ui: core.UserInterface = None
    try:
        # Please modify the template path.
        templatePath: str = r"C:\temp\face.f3dhsm-template"

        if not Path(templatePath).exists():
            return

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

        camObj: cam.CAM = get_cam_product()

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

        # setup
        setup: cam.Setup = create_setup(camObj, bodies[0])

        # template
        template: cam.CAMTemplate = cam.CAMTemplate.createFromFile(templatePath)
        setup.createFromCAMTemplate(template)

        # calculate
        camObj.generateAllToolpaths(True)

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


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 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
Message 10 of 11

patrikrump
Participant
Participant

Hello Kandennti,

 

A really great solution.
I'm really glad you took the trouble to do this.

I've added to it so that setups are also generated for all objects.

Would it be possible to add to this so that the NC programs are also generated?

 

# Fusion360API Python script

import traceback
import adsk.core as core
import adsk.fusion as fusion
import adsk.cam as cam
import sys
from pathlib import Path

def run(context):
    ui: core.UserInterface = None
    try:
        # Please modify the template path.
        templatePath: str = r"C:\temp\face.f3dhsm-template"

        if not Path(templatePath).exists():
            return

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

        camObj: cam.CAM = get_cam_product()

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

        # template laden
        template: cam.CAMTemplate = cam.CAMTemplate.createFromFile(templatePath)

        # für alle Körper ein Setup erzeugen
        for body in bodies:
            setup: cam.Setup = create_setup(camObj, body)
            setup.createFromCAMTemplate(template)

        # calculate
        camObj.generateAllToolpaths(True)

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


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 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
Message 11 of 11

kandennti
Mentor
Mentor

Hi @patrikrump -san.

 

I think we should configure it more precisely, but here's a simple example.
Functions not listed here are the same as before.

# Fusion360API Python script

import traceback
import adsk.core as core
import adsk.fusion as fusion
import adsk.cam as cam
import sys
from pathlib import Path

def run(context):
    ui: core.UserInterface = None
    try:
        # Please modify the template path.
        templatePath: str = r"C:\temp\face.f3dhsm-template"

        if not Path(templatePath).exists():
            return

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

        camObj: cam.CAM = get_cam_product()

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

        # template laden
        template: cam.CAMTemplate = cam.CAMTemplate.createFromFile(templatePath)

        # für alle Körper ein Setup erzeugen
        for body in bodies:
            setup: cam.Setup = create_setup(camObj, body)
            setup.createFromCAMTemplate(template)
            create_nc_program(
                camObj,
                setup,
            )

        # calculate
        camObj.generateAllToolpaths(True)

    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)

・・・
0 Likes