by Vladimir Ananyev

Q: I'm trying to create a matrix to do a polar move of block A from point 1 to point 3. In both case the ' axis' of block 'A' stays in line with a vector from the point on the cylinder to the axis through its center. How can I do this?

A: It’s much easier to define separate matrices for rotation and translation and then use the matrix functionality to combine them. Multiplying two matrices together creates a new matrix that contains the transforms of both of them combined. Matrix.TransformBy method can do this for us.

The following code sample takes the current matrix for the box and multiplies “rotation” matrix with it. Then the result is transformed by “translation” matrix in the similar way. The order that you multiply matrices makes a difference because the changes are applied in order.

Sub Matrix2()

Dim oAsmDoc As AssemblyDocument

Set oAsmDoc = ThisApplication.ActiveDocument

Dim oAsmDef As AssemblyComponentDefinition

Set oAsmDef = oAsmDoc.ComponentDefinition

Dim oTG As TransientGeometry

Set oTG = ThisApplication.TransientGeometry

Dim oOccs As ComponentOccurrences

Set oOccs = oAsmDef.Occurrences

' the 1st component

Dim oCylinder As ComponentOccurrence

Set oCylinder = oOccs.ItemByName("Cylinder:1")

' the 2nd component

Dim oBox As ComponentOccurrence

Set oBox = oOccs.ItemByName("Box:1")

'Step 1: get the current matrix

'""""""""""""""""""""""

'get the current matrix from the box component

Dim oMat As Matrix

Set oMat = oBox.Transformation

'Step 2: add rotation

'""""""""""""""""""""""

Dim oMat2 As Matrix

Set oMat2 = ThisApplication.TransientGeometry.CreateMatrix

'origin point

Dim oCenter As Point

Set oCenter = oAsmDef.WorkPoints.Item(1).Point

'axis of rotation

Dim oVector As Vector

Set oVector = oAsmDef.WorkAxes.Item(3).Line.Direction.AsVector

'angle of rotation

Dim Angle As Double

Angle = oAsmDef.Parameters.Item("Angle").value

Call oMat2.SetToRotation(-Angle, oVector, oCenter)

'a pre-multiplication of oMat by the matrix oMat2

Call oMat.TransformBy(oMat2)

'Step 3: add translation

'""""""""""""""""""""""

Dim Distance As Double

Distance = oAsmDef.Parameters.Item("Shift").value 'translation value

Call oVector.ScaleBy(-Distance)

'apply translation

Call oMat2.SetToIdentity

Call oMat2.SetTranslation(oVector)

Call oMat.TransformBy(oMat2)

'Step 4: Create the new instance of the box in the new position defined by oMat

'""""""""""""""""""""""

Dim oBox2 As ComponentOccurrence

Set oBox2 = oOccs.AddByComponentDefinition(oBox.Definition, oMat)

End Sub