Script to create Cam setups

Script to create Cam setups

JYZMT
Advocate Advocate
2,074 Views
6 Replies
Message 1 of 7

Script to create Cam setups

JYZMT
Advocate
Advocate

Hello,

 

Currently I'm having to create a lot of Cam setups manually for each component in my assembly. The worst part of this for me is naming the set ups correctly, because I want the set-up to be named specifically as the component name. I've meticulously named the components in the model space. Is it possible to write a script/add that pulls the name of the part into the name of the setup? Its not a problem to confirm the other options in setup dialogue box, because I need to align the orientation axis manually anyway, but to have the name fill out automatically would be so helpful.

 

I was hoping either I select the component, then run the add in, or I click an add in button and then click on a component? Or is there a better way?

 

Is this possible, and where do I start?

 

Interestingly, I've noticed that when you start a set up manually, you can right click on the list of components, and add it to the "model", and also the x axix and the y axis etc, but I can't find a way to name it in the same method, so I thought maybe a script could do it?

 

I'll also put a right click "add name to set up" request on the ideas stations, but its not exactly the most exciting of requests, so likely will get few votes.

 

Thanks everyone.

 

 

 

0 Likes
Accepted solutions (1)
2,075 Views
6 Replies
Replies (6)
Message 2 of 7

BrianEkins
Mentor
Mentor
Accepted solution

I can't guarantee that this will work in all cases but it seems to work correctly in my test.

 

It's a Python script.  The easiest way to use it is to use the "Scripts and Add-Ins" command and create a new Python script named whatever you want.  Then select it from the list and choose the "Edit" button.  In the Python editor, replace all of the code with the code below and save it.  Now you can run it by running the "Scripts and Add-Ins" command, choose the script, and click the "Run" button.  It expects the CAM workspace to be active.

 

import adsk.core, adsk.fusion, adsk.cam, traceback

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

        # Make sure CAM is active.
        if not type(app.activeProduct) is adsk.cam.CAM:
            ui.messageBox('The CAM workspace must be active.')

        cam = adsk.cam.CAM.cast(app.activeProduct)
        des = adsk.fusion.Design.cast(app.activeDocument.products.itemByProductType("DesignProductType"))

        # Iterate through the setups.
        setup = adsk.cam.Setup.cast(None)
        for setup in cam.setups:
            # Get the first element that the setup references.
            model = setup.models.item(0)
            
            if type(model) is adsk.fusion.BRepBody:
                body = adsk.fusion.BRepBody.cast(model)

                # Get the parent component this body is in.
                parentComp = body.parentComponent
                if parentComp != des.rootComponent:
                    setup.name = parentComp.name                
            elif type(model) is adsk.fusion.Occurrence:
                occ = adsk.fusion.Occurrence.cast(model)
                setup.name = occ.component.name
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 3 of 7

JYZMT
Advocate
Advocate

Thanks so much for taking the time to help.

 

Unfortunately I can't get this to do anything. Although this is most likely due to user error. I have followed your steps, created a python script, replaced the code, saved it. But when I run it, nothing happens. But there is no "failed:" alert either.

 

I tried running it outside the CAM space, and it does bring up the alert "the CAM space must be active" as expected so something is happening.

 

In the CAM space however, nothing happens? I've tried pre selecting a component and running the script, pre selecting a body and running the script, running the script and clicking on the  browser, and on the model ??? nothing happens - no error alert either of failed: alert. 

 

So my guess is I'm not using it as you intended? Any suggestions?

 

Thanks

 

 

0 Likes
Message 4 of 7

JYZMT
Advocate
Advocate

Ah...  I have it now.

Smiley Wink

Create all the setups, then run the script once, and it updates the names of the set up based on the body or component that is in the "model"

If there is nothing in the model... the name does not change.

If there is more than one component inthe model, it seems to use the first one selected as the name.

 

This is great, probably easier to use than my suggested method. 

Thanks so much.

Message 5 of 7

JYZMT
Advocate
Advocate

Ah, this is awesome. Thanks

 

Next problem....

 

On this basis, it looks to me like the api can run iteratively through names, and grab text strings? So I hoping the following might also be possible, based on this script as a starting point?:

 

- iteratively run through the names of the parts in the model space, and re-name them based on a CONCATENATE (this is what it would be in excel anyway), combining the part number and the material name.

 

my beginner thoughts below your code (you will quickly see coding is new to me, but I'm keen to learn)

 

import adsk.core, adsk.fusion, adsk.cam, traceback

- adsk.cam changed to model space? adsk.model ??? whats the handle here?

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

- no changes here?

# Make sure CAM is active.
        if not type(app.activeProduct) is adsk.cam.CAM:
            ui.messageBox('The CAM workspace must be active.')

 - the CAM has to change to Model space, so its whatever the adsk.model.MODEL handle is?

cam = adsk.cam.CAM.cast(app.activeProduct)
        des = adsk.fusion.Design.cast(app.activeDocument.products.itemByProductType("DesignProductType"))

- again the cam bit changed to the model handle.

- not sure what the des bit is ??

# Iterate through the setups.
        setup = adsk.cam.Setup.cast(None)
        for setup in cam.setups:
            # Get the first element that the setup references.
            model = setup.models.item(0)

name = adsk.model.name.item.cast(none)

??? nope I'm lost!

 

if type(model) is adsk.fusion.BRepBody:
                body = adsk.fusion.BRepBody.cast(model)

                # Get the parent component this body is in.
                parentComp = body.parentComponent
                if parentComp != des.rootComponent:
                    setup.name = parentComp.name                
            elif type(model) is adsk.fusion.Occurrence:
                occ = adsk.fusion.Occurrence.cast(model)
                setup.name = occ.component.name

I can read what your code is doing I think, i.e. getting the component name, or if a body get the the parent component name.  But I'm lost transferring it across to the new script. Its needs to be:

new name = get part number, get material name and combine.

 

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

And this bit seems to be in all the script samples, so I guess its needed at the end.

 

So, miles off as you can see. needless to say it does not work when I try it! But at least I'm trying - I'll get there. Any tips?

 

Thanks

 

 

.

0 Likes
Message 6 of 7

BrianEkins
Mentor
Mentor

I think the best tip that I can give you that will hopefully help you with any project you might want to do in the future is to point you to the existing documentation.  This describes and points you to the various pieces of the documentation and provides some links to additional resources.

 

http://help.autodesk.com/view/fusion360/ENU/?guid=GUID-A92A4B10-3781-4925-94C6-47DA85A4F65A

 

If you get stuck, the forum here is always a good place to ask questions.

 

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

JYZMT
Advocate
Advocate

Thanks, I'll start the journey!

 

0 Likes