Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Create a setup with selected Body or Component

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
j_zander
283 Views, 4 Replies

Create a setup with selected Body or Component

Hi! In my daily workflow i have to create a lot of setups, so I'd like to make this a little bit faster by creating a script. But i'm struggeling getting my selected Body as setup.models and my Component Fixtures as setup.fixtures. Everything else is working as it is supposed to be. 

 

I hope anybody can help me.

 

jzander_0-1699392442440.png

 

 

import adsk.core, adsk.fusion, adsk.cam, traceback
import os
from enum import Enum



def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        product = app.activeProduct
        design = adsk.fusion.Design.cast(product)
            # use existing document, load 2D Strategies model from the Fusion CAM Samples folder
        doc = app.activeDocument
          

        #Start AutoSetup
        ui.messageBox('Start AutoSetup')

        # Select Part

        
        # Select Part
        ui.messageBox('Select Part')
        selectedEntity = ui.selectEntity('Select the part to machine', 'Bodies')

        # Überprüfen, ob ein Körper ausgewählt wurde
        if selectedEntity is None:
            ui.messageBox('Kein Körper ausgewählt. Das Skript wird beendet.')
            return

        ui.messageBox('Part Selected')

        
       
        #WCS

        WCS = design.userParameters.itemByName('wcs').expression

        ui.messageBox('WCS {} is selected'.format(WCS))

         # switch to manufacturing space
        camWS = ui.workspaces.itemById('CAMEnvironment') 
        camWS.activate()

        # get the CAM product
        products = doc.products
        
       
       #################### create setup ####################
      #################### create setup ####################
        cam = adsk.cam.CAM.cast(products.itemByProductType("CAMProductType"))
        setups = cam.setups
        setupInput = setups.createInput(adsk.cam.OperationTypes.MillingOperation)
        
        
        
        # This part is not working !
        models = adsk.core.ObjectCollection.create()
        models.add(selectedEntity[0])
        setupInput.models = models


       
        # create the setup
        setup = setups.add(setupInput) 
        docname = app.activeDocument.name
        setup.name = docname
        setup.fixtureEnabled = True
        
        programNameParam = setup.parameters.itemByName('job_programName')
        programNameParam.expression = "'{}'".format(docname)
        programWorkoffsetParam = setup.parameters.itemByName('job_workOffset')
        programWorkoffsetParam.expression = WCS
       

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

 

Tags (4)
4 REPLIES 4
Message 2 of 5
kandennti
in reply to: j_zander

Hi @j_zander -San.

 

The most likely reason is that you are using an ObjectCollection object.
You can set it up using a list.
I was also able to set the fixtures using cam.SetupInput.


The following sample was tested with the attached f3d.

# Fusion360API Python script

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

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
        root: fusion.Component = rootOcc.component

        setup: cam.Setup = create_setup(
            camObj,
            root.bRepBodies[0],
            root.allOccurrences[0]
        )

        ui.messageBox("Done")

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


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

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

    return setup


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()


The result looks like this

1.png

Message 3 of 5
j_zander
in reply to: j_zander

Thanks for your help. The Solution is ok but in your script the setupmodel has to be  a body and can't be a component. And in my project Fixture is a vise with a few components. The script just adds the first component of the fixture components. Also the script uses the first component it's finding. In my case it is the stockmodel i use to position my parts inside my vise. This should be ignored. 

 

Is it possible to Select a whole component by name with everything inside of it? Then i just could put my body that should be machined inside of the targetBodyComponent and its automatically selectet. 

jzander_0-1699460432240.png

I attach my fixture file here. Thanks for your help 🙂

Message 4 of 5
kandennti
in reply to: j_zander

@j_zander -San.

 

I may not be understanding what you mean.
Do you mean like this?

# Fusion360API Python script

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

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
        root: fusion.Component = rootOcc.component

        # models
        modelsOcc: fusion.Occurrence = root.allOccurrences.itemByName("targetBody:1")
        if not modelsOcc: return

        # fixtures:
        fixturesOcc: fusion.Occurrence = root.allOccurrences.itemByName("Fixture:1")
        if not fixturesOcc: return

        setup: cam.Setup = create_setup(
            camObj,
            [modelsOcc],
            [fixturesOcc],
        )

        ui.messageBox("Done")

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


def create_setup(
    camObj: cam.CAM,
    models: list[core.Base],
    fixtures: list[core.Base],
    ) -> cam.Setup:

    setups: cam.Setups = camObj.setups
    setupIpt: cam.SetupInput = setups.createInput(
        cam.OperationTypes.MillingOperation
    )
    setupIpt.models = models
    setupIpt.fixtures = fixtures
    setup: cam.Setup = setups.add(setupIpt)

    return setup


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()
Message 5 of 5
j_zander
in reply to: kandennti

Perfect! That's exactly what i wanted! Thank you very much!

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Technology Administrators


Autodesk Design & Make Report