How to convert multiple assembly components back to solid bodies in Fusion 360

How to convert multiple assembly components back to solid bodies in Fusion 360

yamamoto.mas
Contributor Contributor
1,291 Views
4 Replies
Message 1 of 5

How to convert multiple assembly components back to solid bodies in Fusion 360

yamamoto.mas
Contributor
Contributor

I want to convert many components automatically into solid bodies.

I have found a nice script "Back_to_body.py" in AUTODESK support dated on Feb 04 2021:

 

https://knowledge.autodesk.com/support/fusion-360/troubleshooting/caas/sfdcarticles/sfdcarticles/How...

 

But there is one problem; it can't convert copied component correctly.

In the picture below, one rectangle component is copied from the other.

The scripts get error saying 'target body is lost'.

I've tested cutPasteBodies -> copyPastBodies, but only one body is copied.

Is there any workaround to this ?

yamamotomas_0-1637492724624.png

 

 

``` Back_to_body.py

#Author-Boopathi Sivakumar
#Description- Moves all the component bodies to root component

 

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)

 

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

 

        features_ = rootComp.features.removeFeatures

 

        # Set the design type to Parametric if not
        design.designType = 1

 

        # Build a list of occurrences
        occs = []
        for occ in rootComp.occurrences:

            occs.append(occ)
            for occ_ in occ.childOccurrences:
                occs.append(occ_)

 

        occ: adsk.fusion.Occurrence
        dialouge = ui.messageBox('Do you want to move all the component bodies to Root Component?','Back to body',adsk.core.MessageBoxButtonTypes.YesNoButtonType)

 

        sourceBodies = adsk.core.ObjectCollection.create()
        if dialouge == adsk.core.DialogResults.DialogYes:
            for occ in occs:
                for body in occ.bRepBodies:
                    sourceBodies.add(body)
       else:
           return
     rootComp.features.cutPasteBodies.add(sourceBodies)

 

    #remove the empty componets using
    for occ in occs:
        features_.add(occ)

    #Block this code if you want to capture the design history
    design.designType = 0

 

    ui.messageBox("All the component bodies are moved to root component")
except:
    if ui:
        ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

```

 

0 Likes
Accepted solutions (2)
1,292 Views
4 Replies
Replies (4)
Message 2 of 5

tykapl.breuil
Advocate
Advocate
Accepted solution

I have no idea why the initial code using copyPaste doesn't work but if you move the copyPasteBodies.add to inside the for loop, you have the intended result.

Also, the code you have only works if components only have one subcomponent, else it won't actually get all occurences, for that you need to use a recursive function.(but that may not be a problem depending on your use case).

Message 3 of 5

yamamoto.mas
Contributor
Contributor

Thanks for comments.

I'll explain the experiment I have done in more details.

 

yamamotomas_1-1637541417930.png

 

In the left, I have prepared a model I want to convert.

There are two kinds of components, three cylinders made by "copy/paste" command, and two rectangles made by "copy/paste new" command.

 

When I convert the model using "copyPasteBodies.add", two cylinders are dropped. I'm afraid the function judge that copied components are unnecessary, but they are important for me.

 

So I would like to get the same 5 bodies in the right somehow.

 

0 Likes
Message 4 of 5

j.han97
Advocate
Advocate
Accepted solution

Hi @yamamoto.mas ,

 

First, here is a script that should work for you:

import adsk.core, adsk.fusion, traceback

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

        design = app.activeProduct
        design.designType = adsk.fusion.DesignTypes.ParametricDesignType

        # Get the root component of the active design.
        root_comp = design.rootComponent

        #Get list of occurrences
        occs = list(root_comp.allOccurrences)
        
        #Copy bodies to root_comp
        for occ in occs:
            for body in occ.bRepBodies:
                body.copyToComponent(root_comp)

        #Delete occurrences
        remove = root_comp.features.removeFeatures
        for occ in occs:
            remove.add(occ)
        
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

You could tweak and modify a little to better suit your use.

 

For your case, the cylinders are actually multiple occurrences of a single component, while the rectangles are different components. This depends on the command used to copy them ('Copy/Paste' or 'Copy/Paste New').

 

If you use CutPasteBodies command on multiple occurrences of same component (source), the component becomes empty after being cut for the first time. Therefore, it showed the error message 'target lost' because it has nothing to cut.

 

The CopyPasteBodies command might be able to work, but I haven't tested it. While they should be doing the same thing, bRepBody.copyToComponent() command is more efficient to me (less typing).

 

On the other hand, it is better to put your code using the 'Code sample' function here for better readability.

jhan97_0-1637545043247.png

 

Message 5 of 5

yamamoto.mas
Contributor
Contributor

Great thanks !

It worked perfect for me.

yamamotomas_0-1637557205391.png

 

0 Likes