Can't copy bodys from occ2 to occ1

Can't copy bodys from occ2 to occ1

maurizio_manzi
Advocate Advocate
127 Views
2 Replies
Message 1 of 3

Can't copy bodys from occ2 to occ1

maurizio_manzi
Advocate
Advocate

Hello,
I try to copy the bodys of occ2 to occ1.
I don't get an error message, but the bodys was not copied.
After this, the bodys in occ2 and occ2 itself should be deleted.

Best regards
Maurizio

comp1 = occ1.component
comp2 = occ2.component
tempBRepMgr = adsk.fusion.TemporaryBRepManager.get()


baseFeatures = comp1.features.baseFeatures
if baseFeatures.count == 0:
    bfInput = baseFeatures.createInput()
    baseFeature = baseFeatures.add(bfInput)
else:
    baseFeature = baseFeatures.item(0)

try:
    
    baseFeature.startEdit()

    
    for body in comp2.bRepBodies:
        if not body.isSolid:
            continue
        bodyCopy = tempBRepMgr.copy(body)
        comp1.bRepBodies.add(bodyCopy, baseFeature)
        body.deleteMe()  # optional

finally:
    
    baseFeature.finishEdit()
0 Likes
Accepted solutions (1)
128 Views
2 Replies
Replies (2)
Message 2 of 3

BrianEkins
Mentor
Mentor
Accepted solution

Here's a light variation of your code that works.

import traceback
import adsk.core
import adsk.fusion

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


def run(_context: str):
    des: adsk.fusion.Design = app.activeProduct
    root = des.rootComponent
    occ1 = root.occurrences[0]
    occ2 = root.occurrences[1]

    try:
        comp1 = occ1.component
        comp2 = occ2.component
        tempBRepMgr = adsk.fusion.TemporaryBRepManager.get()

        baseFeatures = comp2.features.baseFeatures
        if baseFeatures.count == 0:
            baseFeature = baseFeatures.add()
        else:
            baseFeature = baseFeatures.item(0)

        try:
            baseFeature.startEdit()

            originalBodies = []
            for body in comp1.bRepBodies:
                originalBodies.append(body)
                if not body.isSolid:
                    continue
                bodyCopy = tempBRepMgr.copy(body)
                comp2.bRepBodies.add(bodyCopy, baseFeature)

            for body in originalBodies:
                body.deleteMe()
        finally:
            baseFeature.finishEdit()
    except:  #pylint:disable=bare-except
        # Write the error message to the TEXT COMMANDS window.
        app.log(f'Failed:\n{traceback.format_exc()}')
---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 3 of 3

maurizio_manzi
Advocate
Advocate

Hello,
thank you.
I still don't get an error message, but still no bodies are copied.
I've extended the code with messageBoxes (see code), and it looks like it doesn't find any bodies, even though there are definitely some (see construction).
The message box "both occs okay" is displayed, "start edit okay" is also displayed. But "body found: ..." is not displayed.
Best regards
Maurizio

if occ1 != None and occ2 != None:
            ui.messageBox("both occs okay")
            try:
                comp1 = occ1.component
                comp2 = occ2.component
                tempBRepMgr = adsk.fusion.TemporaryBRepManager.get()

                baseFeatures = comp2.features.baseFeatures
                if baseFeatures.count == 0:
                    baseFeature = baseFeatures.add()
                else:
                    baseFeature = baseFeatures.item(0)

                try:
                    baseFeature.startEdit()
                    ui.messageBox("startEdit okay")
                    originalBodies = []
                    for body in comp1.bRepBodies:
                        originalBodies.append(body)
                        ui.messageBox("body found: " + body.name)
                        if not body.isSolid:
                            continue
                        ui.messageBox("body is solid: " + body.name)
                        bodyCopy = tempBRepMgr.copy(body)
                        comp2.bRepBodies.add(bodyCopy, baseFeature)

                    for body in originalBodies:
                        body.deleteMe()
                finally:
                    baseFeature.finishEdit()
            except:  #pylint:disable=bare-except
                # Write the error message to the TEXT COMMANDS window.
                app.log(f'Failed:\n{traceback.format_exc()}')


 

0 Likes