Copy Body and Move by Code

Copy Body and Move by Code

rocco.mayr
Enthusiast Enthusiast
2,050 Views
3 Replies
Message 1 of 4

Copy Body and Move by Code

rocco.mayr
Enthusiast
Enthusiast

I made a copy of a body and now I want to move it, but I get an error:

 

Failed:

Traceback (most recent call last):

File "...copyAndMove.py", line 46, in run

moveFeats.add(moveFeatureInput)

File ".../Library/Application Support/Autodesk/webdeploy/production/cdedd4b05efae2d277df7be3d315d4ccce03a01d/Autodesk Fusion 360.app/Contents/Api/Python/packages/adsk/fusion.py", line 21834, in add

return _fusion.MoveFeatures_add(self, input)

RuntimeError: 2 : InternalValidationError : Utils::getObjectPath(entity.get(), objPath, nullptr, compPath)

 

Here is the code:

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)
        if not design:
            ui.messageBox('No active Fusion design', 'No Design')
            return

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

        # 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)
        baseBody.name = "Stick_1"

        # Copy/paste bodies
        subComp1.features.copyPasteBodies.add(baseBody)
        
        # Rename Body
        baseBodyCopy = subComp1.bRepBodies.item(1)
        baseBodyCopy.name = "Stick_2"

         # 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.0, 10.0, 6.0)
        transform = adsk.core.Matrix3D.create()
        transform.translation = vector
        
        # Create a move feature
        moveFeats = features.moveFeatures
        moveFeatureInput = moveFeats.createInput(bodies, transform)
        moveFeats.add(moveFeatureInput)

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

 

Accepted solutions (1)
2,051 Views
3 Replies
Replies (3)
Message 2 of 4

kandennti
Mentor
Mentor
Accepted solution

Hi @rocco.mayr .

 

If you get the moveFeatures from the Component that the body belongs to, it will work correctly.

・・・
        # Create a move feature
        # moveFeats = features.moveFeatures
        moveFeats = subComp1.features.moveFeatures
        moveFeatureInput = moveFeats.createInput(bodies, transform)
        moveFeats.add(moveFeatureInput)
・・・
Message 3 of 4

rocco.mayr
Enthusiast
Enthusiast

Yes now it works, thx!

Could you tell me what we have to do to move the whole component "subComp1"?

I tried:

# Create a collection of entities for move
bodies = adsk.core.ObjectCollection.create()
#bodies.add(baseBodyCopy)
bodies.add(subComp1)
        
# Create a transform to do move
vector = adsk.core.Vector3D.create(0, 0, 6.0)
transform = adsk.core.Matrix3D.create()
transform.translation = vector
        
# Create a move feature
moveFeats = subComp1.features.moveFeatures
moveFeatureInput = moveFeats.createInput(bodies, transform)
moveFeats.add(moveFeatureInput)

This error is thrown:

Failed:
Traceback (most recent call last):
File "/....copyAndMove.py", line 48, in run
moveFeats.add(moveFeatureInput)
File ".../Library/Application Support/Autodesk/webdeploy/production/cdedd4b05efae2d277df7be3d315d4ccce03a01d/Autodesk Fusion 360.app/Contents/Api/Python/packages/adsk/fusion.py", line 21834, in add
return _fusion.MoveFeatures_add(self, input)
RuntimeError: 3 : unsupported entity type

Message 4 of 4

kandennti
Mentor
Mentor

@rocco.mayr .

Moving an Occurrence is different from moving a Body . You can actually check it in the GUI.

1.png

 

In the case of Occurrence, use the transform property.
Don't forget to perform a snapshot.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-7fb9ea55-5c24-4c27-b1c4-2e92f42774f7 

# Fusion360API Python script
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)
        if not design:
            ui.messageBox('No active Fusion design', 'No Design')
            return

        # Get the root component
        rootComp = design.rootComponent

        # Get the first Occurrence
        occs = rootComp.occurrences
        occ: adsk.fusion.Occurrence = occs.item(0)

        # Create vector
        vector = adsk.core.Vector3D.create(5.0, 10.0, 6.0)
        
        # get Occurrence transform
        mat: adsk.core.Matrix3D = occ.transform

        # Matrix translation
        mat.translation = vector

        # set transform
        occ.transform = mat

        # snapshot - Determining the position is important!!!
        design.snapshots.add()

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