copyPasteBodies Problem with Linked Files

copyPasteBodies Problem with Linked Files

ebunn3
Advocate Advocate
402 Views
2 Replies
Message 1 of 3

copyPasteBodies Problem with Linked Files

ebunn3
Advocate
Advocate

Hi,

 

Been working on a problem all morning that I cannot solve so I thought I'd reach out to the experts.  The code, I have pasted in below, used in conjunction with the file I have uploaded (Linked File Test v6.f3z) works with standard components but not with linked components.  The file I have attached has a linked component and an internal component.  You will see two copyPasteBodies features created, one having a body within it and one not.  Code does not crash, it just does not work for the linked component.  Can someone tell me how this can be repaired or give me a work around.  Thanks in advance!!!

 

Eric

 

ebunn3_0-1648575242707.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)
 
        # Get root component in this design
        rootComp = design.rootComponent

        # Get the first sub component
        occs = rootComp.occurrences
        subComp1 = occs.itemByName('Test v3:1').component

        # Get the second sub component
        subComp2 = occs.itemByName('SubComp2:1').component

        # Get the third sub component
        subComp3 = occs.itemByName('Test v3 Unlinked:1').component
        
        # Get the first body in sub component 1  
        bodyInSubComp1 = subComp1.bRepBodies.item(0)

        # Get the first body in sub component 1  
        bodyInSubComp3 = subComp3.bRepBodies.item(0)

        copyPasteBody = subComp2.features.copyPasteBodies.add(bodyInSubComp1)
        copyPasteBody1 = subComp2.features.copyPasteBodies.add(bodyInSubComp3)

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

 

0 Likes
Accepted solutions (1)
403 Views
2 Replies
Replies (2)
Message 2 of 3

kandennti
Mentor
Mentor
Accepted solution

Hi @ebunn3 .

 

The same operation in the GUI did not paste the Body.

If you don't mind not linking, you can use the TemporaryBRepManager.copy method.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-26E53439-9DE5-4A11-93AD-7E0570BAA657 

 

Fixed.

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)
 
        # Get root component in this design
        rootComp = design.rootComponent

        # Get the first sub component
        occs = rootComp.occurrences
        subComp1 = occs.itemByName('Test v1:1').component

        # Get the second sub component
        subComp2 = occs.itemByName('SubComp2:1').component

        # Get the third sub component
        subComp3 = occs.itemByName('Test v3 Unlinked:1').component
        
        # Get the first body in sub component 1  
        bodyInSubComp1 = subComp1.bRepBodies.item(0)

        # Get the first body in sub component 1  
        bodyInSubComp3 = subComp3.bRepBodies.item(0)

        # copyPasteBody = subComp2.features.copyPasteBodies.add(bodyInSubComp1)
        opyPasteBody = cloneBody(
            bodyInSubComp1,
            occs.itemByName('SubComp2:1')
        )
        copyPasteBody1 = subComp2.features.copyPasteBodies.add(bodyInSubComp3)

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

def cloneBody(
    targetBody: adsk.fusion.BRepBody,
    targetOcc: adsk.fusion.Occurrence) -> adsk.fusion.BRepBody:

    tmpMgr: adsk.fusion.TemporaryBRepManager = adsk.fusion.TemporaryBRepManager.get()
    clone: adsk.fusion.BRepBody = tmpMgr.copy(targetBody)

    app: adsk.core.Application = adsk.core.Application.get()
    des: adsk.fusion.Design = app.activeProduct
    comp: adsk.fusion.Component = targetOcc.component

    baseFeat: adsk.fusion.BaseFeature = None
    if des.designType == adsk.fusion.DesignTypes.ParametricDesignType:
        baseFeat = comp.features.baseFeatures.add()

    bodies: adsk.fusion.BRepBodies = comp.bRepBodies

    body: adsk.fusion.BRepBody = None
    if baseFeat:
        # ParametricDesign
        baseFeat.startEdit()
        try:
            body = bodies.add(clone, baseFeat)
        except:
            pass
        finally:
            baseFeat.finishEdit()
    else:
        # DirectDesign
        body = bodies.add(clone)
    
    return body

 

0 Likes
Message 3 of 3

ebunn3
Advocate
Advocate

I don’t care about the copied body not being linked.  I’ll try this out.   Thanks.    

0 Likes