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: 

Component Transform Rotates Correctly but Moves Incorrectly

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

Component Transform Rotates Correctly but Moves Incorrectly

Hi,

 

Been wheel spinning for hours on this.  I have inserted some code below that is supposed to rotate an occurrence, followed by moving it a specific distance.  The rotation works but the move does not work.  Its supposed to rotate it 90 degrees and then move it 1 inch in the X direction.  It ends up being moved in both x and y by unknown distances.  Can anyone help with this.  I'm sure I am doing something fundamentally wrong.

 

Eric

 

 

def rotateMoveComp(comp,angleDeg,x,y,z):
    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 bounding box min
        bbMin = comp.boundingBox.minPoint
        
        # get Occurrence transform
        mat: adsk.core.Matrix3D = comp.transform

        import math
        mat.setToRotation(math.radians(angleDeg), adsk.core.Vector3D.create(0,0,1), bbMin)

        # set transform
        comp.transform = mat

        vector = adsk.core.Vector3D.create(x, y, z)

        # Matrix translation
        mat.translation = vector

        # set transform
        comp.transform = mat

        print('')

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

app = adsk.core.Application.get()
ui  = app.userInterface
sel = ui.selectEntity('Choose Cushion Component', 'Occurrences')
cushion = adsk.fusion.Occurrence.cast(sel.entity)
rotateMoveComp(cushion,90,1*2.54,0,0)

 

 

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

I believe I solved this using the following line of code for creation of the translation vector:

 

 

 

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

 

 

This would replace line 28 below.  

 

Eric

Message 3 of 3
Jorge_Jaramillo
in reply to: ebunn3

hi @ebunn3 ,

 

It is better to make a matrix multiplication to get the translation once you had rotated the component.

So you multiply the rotated matrix by a translation matrix.

This is how you can implement it:

 

        #get bounding box min
        bbMin = comp.boundingBox.minPoint
        
        # get Occurrence transform
        mat: adsk.core.Matrix3D = comp.transform

        # import math
        mat.setToRotation(math.radians(angleDeg), adsk.core.Vector3D.create(0,0,1), bbMin)

        # set transform
        comp.transform = mat
        app.log(f'mat transform rotate 90: {mat.asArray()}')

        vector = adsk.core.Vector3D.create(x, y, z)

        mat_translate = adsk.core.Matrix3D.create()
        # Matrix translation
        mat_translate.translation = vector

        mat.transformBy(mat_translate)
        # set transform
        comp.transform = mat
        app.log(f'resulting matrix: {mat.asArray()}')

 

 

You could check that the only value that changes is the fourth value of the matrix, which corresponds to the X displacement.

 

Hope this help.

 

Regards,
Jorge

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