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: 

Copy and Move feature in loop

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
rocco.mayr
469 Views, 3 Replies

Copy and Move feature in loop

In try to copy a body 6 times and move it to different positions in a loop. I tried different things but can't get it work.

 

Here is the code and the error:

app = adsk.core.Application.get()
        ui  = app.userInterface
        design = adsk.fusion.Design.cast(app.activeProduct)

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

        # 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)
        
        for i in range(6):
             
            # Copy/paste body
            subComp1.features.copyPasteBodies.add(baseBody)
        
            baseBodyCopy = subComp1.bRepBodies.item(i+1)
            
            # Create a collection of entities for move
            bodies = adsk.core.ObjectCollection.create()
            bodies.add(baseBodyCopy)
            
            # Create a transform to do move
            vector = adsk.core.Vector3D.create(5 * i, 0, 0)
            transform = adsk.core.Matrix3D.create()
            transform.translation = vector
            
            moveFeats = subComp1.features.moveFeatures
            moveFeatureInput = moveFeats.createInput(bodies, transform)
            moveFeats.add(moveFeatureInput)

 

Error:

Failed:
Traceback (most recent call last):
File "...py", line 83, in run
moveFeats.add(moveFeatureInput)
File "/Library/Application Support/Autodesk/webdeploy/production/3f1b40e356e6df065e6a8f3470590c17c5502c2a/Autodesk Fusion 360.app/Contents/Api/Python/packages/adsk/fusion.py", line 21839, in add
return _fusion.MoveFeatures_add(self, input)
RuntimeError: 3 : invalid transform
3 REPLIES 3
Message 2 of 4
rocco.mayr
in reply to: rocco.mayr

After reading this post I could solve the problem using TemporaryBRepManager and BaseFeature:

https://forums.autodesk.com/t5/fusion-360-api-and-scripts/built-in-circular-pattern-fusion-crashes-d...

 

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

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

# Create a base feature
baseFeats = subComp1.features.baseFeatures
baseFeat = baseFeats.add()

# Get TemporaryBRepManager
tempBrepMgr = adsk.fusion.TemporaryBRepManager.get()
        
baseFeat.startEdit()

for i in range(6):
    # Creates a temporary copy of the input BRepBody
    tempBody = tempBrepMgr.copy(baseBody)
    TransformBody(tempBody, 5 * i)
    copiedBody = subComp1.bRepBodies.add(tempBody, baseFeat)

baseFeat.finishEdit()

def TransformBody(body, posX):
    # Get TemporaryBRepManager
    tempBrepMgr = adsk.fusion.TemporaryBRepManager.get() 
    
    transform = adsk.core.Matrix3D.create()
    transform.translation = adsk.core.Vector3D.create(posX, 0.0, 0.0)
    
    # Transforms the brep body using the specified transformation matrix
    tempBrepMgr.transform(body, transform)

 

TemporaryBRepManager:

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-2CA08FA9-8C23-4449-87C4-1CCBA87941C1

 

BaseFeature:

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-ddc16c4a-ea73-11e5-b9fc-3417ebd3d5be

 

Message 3 of 4
BrianEkins
in reply to: rocco.mayr

The reason for the failure is that the first time through the loop i is 0, which results in creating a zero length vector for the translation.  That's not valid for the Move feature so it's failing with an invalid transform error.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 4 of 4
rocco.mayr
in reply to: BrianEkins


@BrianEkins  schrieb:

The reason for the failure is that the first time through the loop i is 0, which results in creating a zero length vector for the translation.  That's not valid for the Move feature so it's failing with an invalid transform error.


Good to know, thx

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