Help with API script trying to copy a body from a component

Help with API script trying to copy a body from a component

rusty.bird
Advocate Advocate
2,019 Views
9 Replies
Message 1 of 10

Help with API script trying to copy a body from a component

rusty.bird
Advocate
Advocate

So I have uploaded a CAD Model to fusion and opened the File.  I would like to make a copy of the components body and then split the body to get a sketch profile and then revolve that sketch profile.  This is my long term goal but I am stuck on the first part which is trying to copy a body of a component.

 

 I am very new to this so I am learning along the way and reading lots.  I keep getting this error "Bad Index Parameter" when running this code.  Any help would be appreciated.

import adsk.core, adsk.fusion, traceback

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        
        product = app.activeProduct
        design = adsk.fusion.Design.cast(product)
        if not design:
            ui.messageBox('No active Fusion design', 'No Design')
            return

        # Get the root component of the active design.
        rootComp = design.rootComponent
        features = rootComp.features

        # Get the first sub component
        occs = rootComp.occurrences
        subComp1 = occs.item(0).component

        # Get the first body in sub component 1  
        baseBody = subComp1.bRepBodies.item(0)
        baseBody.name = "Stick_1"

        # Copy/paste bodies
        subComp1.features.copyPasteBodies.add(baseBody)

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

 

 

0 Likes
Accepted solutions (1)
2,020 Views
9 Replies
Replies (9)
Message 2 of 10

j.han97
Advocate
Advocate

Hi @rusty.bird ,

 

The CopyPasteBodies feature is supposed to be called from the component you wish to copy to (i.e., destination). For an operation to copy a body from compA to compB, you should have:

bodyA = compA.bRepBodies[0] #E.g., take the first body from compA
compB.features.copyPasteBodies.add(bodyA) #This will copy body from compA from comp B

Please refer to this example for more guide: Fusion 360 Help | Copy Paste Bodies API Sample | Autodesk

 

However, I personally use another approach: 

bodyA = compA.bRepBodies[0] #Similarly, take first body from compA
bodyA.copyToComponent(occB) #Here occB is an occurrence which refers to compB

Both methods produce the same result I believe (at least for this step). Just putting it for reference so that you can choose whatever is more convenient for you.

Message 3 of 10

rusty.bird
Advocate
Advocate

I have tryed your examples I am having trouble running it.  I still get that same error.  I have also tryed copy and pasting the code directly from  Copy Paste Bodies API Sample API Sample and I get the same error.  

 

I am sure there is something I am missing.  

0 Likes
Message 4 of 10

rusty.bird
Advocate
Advocate

This is the error I am getting: 

Failed:

Traceback (most recent call last):

File "C:/Users/......./AppData/Roaming/Autodesk/Autodesk Fusion 360/API/Scripts/NewScript3/NewScript3.py", line 23, in run

compA = occs.item(0).component

File "C:/Users/...../AppData/Local/Autodesk/webdeploy/production/96174d2c4918a1a613b00371fd422e0f30e1d187/Api/Python/packages\adsk\fusion.py", line 25828, in item

return _fusion.Occurrences_item(self, index)

RuntimeError: 3 : Bad index parameter

 

With this layout Originally and want a copy of Body1 under Bodies.  I have changed the code to the code below from what j.han97 recommended:  

 image.png 

import adsk.core, adsk.fusion, traceback

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

        # Get active design
        product = app.activeProduct
        design = adsk.fusion.Design.cast(product)
        if not design:
            ui.messageBox('No active Fusion design', 'No Design')
            return

        # Get root component in this design
        rootComp = design.rootComponent
        features = rootComp.features

        # Get the first sub component
        occs = rootComp.occurrences
        compA = occs.item(0).component

        # Get the second sub component
        compB = occs.item(1).component

        # Get the first body in sub component 1  
        bodyA = compA.bRepBodies[0]
        
        # Copy/paste body from sub component 1 to sub component 2
        compB.features.copyPasteBodies.add(bodyA)
            
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

0 Likes
Message 5 of 10

BrianEkins
Mentor
Mentor

It's not entirely clear to me what you're trying to do.  Have you gone through the entire process interactively?  I think reading this topic may help you better understand some of the internals of Fusion too.

 

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-88A4DB43-CFDD-4CFF-B124-7EE67915A07A

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

rusty.bird
Advocate
Advocate

Thanks for the reply.  I have read that document which did help me understand it better.

 

What I am trying to do is simply copy/paste a body.  I can do it manually in the UI.  So just copy and past the Body1 so in the UI it would be Body2 underneath the Body1.

0 Likes
Message 7 of 10

BrianEkins
Mentor
Mentor

The part that isn't clear to me is where is the existing body and where do you want to copy it. Do you want to create a new component for each copied body?

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

rusty.bird
Advocate
Advocate

When I upload a CAD file.  There is one componant and in that componant there is a Body1.  So my goal is to copy that Body1 and paste it under that same componants body.  So in the UI there now would be two bodies for that componant which would be identical.

 

I guess if this isn't possible I could try just duplicating the conponant.

0 Likes
Message 9 of 10

j.han97
Advocate
Advocate
Accepted solution

Hi @rusty.bird ,

 

I will first put the code for you:

 

_app = adsk.core.Application.get()
root_comp = adsk.fusion.Design.cast(_app.activeProduct).rootComponent
body1 = root_comp.bRepBodies[0] #Get Body1
root_comp.features.copyPasteBodies.add(body1) #Copy Body1

 

This code essentially copy Body1 to root component, which is equivalent to the location of Body1 itself (put Body2 together with Body1 as you asked). 

The reason of your error is that the body (Body1) is under root component, instead of other components. You might need to spend some time to fully understand the definitions of root component, components and occurrences, which are available in the link from Brian.

In addition, the error message is an important method to understand what has gone wrong. There was no component (hence, no occurrence) under the root component in your model, hence the error message "bad index parameter while trying to accessing occs.item(0)". Although the error messages are usually difficult to interpret, they point directly to the reason of error.

0 Likes
Message 10 of 10

rusty.bird
Advocate
Advocate

What you explained there is exactly what I am trying to do.  Sorry for the confusion and thanks for help.  I will try this out in the morning.  I do understand why I got that error now.  I will have to read that link again and do some samples to make sure I fully grasp it.

0 Likes