Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

copyPasteBodies Problem with Linked Files

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
ebunn3
118 Views, 2 Replies

copyPasteBodies Problem with Linked Files

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()))

 

2 REPLIES 2
Message 2 of 3
kandennti
in reply to: ebunn3

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

 

Message 3 of 3
ebunn3
in reply to: kandennti

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

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report