Add move feature to base feature

Add move feature to base feature

ebunn3
Advocate Advocate
551 Views
2 Replies
Message 1 of 3

Add move feature to base feature

ebunn3
Advocate
Advocate

Hello,

 

I am trying to add a move feature in conjunction with a base feature and I have something fundamentally wrong with my code.   I don't know if this is possible to do or not.  Can someone tell me if this is possible or not and how I would go about achieving this.  Ultimately the base feature will contain more than just a single move feature.  The example is just an attempt to work out the code.

 

Thanks in advance.

 

Eric

def moveBody_BF(body,x,y,z):
    """This function will move a single body. x,y and z should be in cm.
     """
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        design = app.activeProduct
        rootComp = design.rootComponent

        baseFeats = rootComp.features.baseFeatures
        baseFeat = baseFeats.add()
        baseFeat.startEdit()

        inputEnts = adsk.core.ObjectCollection.create()
        inputEnts.add(body)

        #create the Matrix3D
        mat = adsk.core.Matrix3D.create()

        #define the vector
        vector = adsk.core.Vector3D.create(x, y, z)
        mat.translation = vector

        #make the move
        moveFeatures = baseFeat.parentComponent.features.moveFeatures
        input = moveFeatures.createInput(inputEnts, mat)
        input.targetBaseFeature = baseFeat
        moveFeature = moveFeatures.add(input)

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


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

#Select body
body = ui.selectEntity('Select a body', 'Bodies').entity

#define the distances to move
x=20.00*2.54;y=0.00*2.54;z=0.00*2.54

#move body
moveBody_BF(body,x,y,z)
0 Likes
Accepted solutions (1)
552 Views
2 Replies
Replies (2)
Message 2 of 3

BrianEkins
Mentor
Mentor
Accepted solution

Here's a modified version of your program that works.

 

I'm not sure what your intent is but you can't move a parametric body while a base feature is active. My code copies the selected body into the base feature. The copy is owned by the base feature and is not parametric and can be moved within the base feature. You'll see the same limitations when using the UI. If you want to move the parametric body, then there's no reason to have a base feature and you can use a move feature to move it.

 

def moveBody_BF(body,x,y,z):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        design: adsk.fusion.Design = app.activeProduct
        rootComp = design.rootComponent

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

        # Set it to edit mode.
        baseFeat.startEdit()

        # Copy the selected body into the base feature.
        baseFeatBody = rootComp.bRepBodies.add(body, baseFeat)

        # Add the new body to a collection.
        inputEnts = adsk.core.ObjectCollection.create()
        inputEnts.add(baseFeatBody)

        # Define the transform.
        mat = adsk.core.Matrix3D.create()
        vector = adsk.core.Vector3D.create(x, y, z)
        mat.translation = vector

        # Move the body, using a move feature.
        moveFeatures = baseFeat.parentComponent.features.moveFeatures
        input = moveFeatures.createInput(inputEnts, mat)
        input.targetBaseFeature = baseFeat
        moveFeature = moveFeatures.add(input)

        # Finish the base feature edit.
        baseFeat.finishEdit()
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes
Message 3 of 3

ebunn3
Advocate
Advocate

Thank you Brian.  I think this should do it.

 

Eric

0 Likes