Component Transform Rotates Correctly but Moves Incorrectly

Component Transform Rotates Correctly but Moves Incorrectly

ebunn3
Advocate Advocate
448 Views
2 Replies
Message 1 of 3

Component Transform Rotates Correctly but Moves Incorrectly

ebunn3
Advocate
Advocate

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)

 

 

0 Likes
Accepted solutions (1)
449 Views
2 Replies
Replies (2)
Message 2 of 3

ebunn3
Advocate
Advocate
Accepted solution

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

0 Likes
Message 3 of 3

Jorge_Jaramillo
Collaborator
Collaborator

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

0 Likes