- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
This question is related to matrix multiplication. I'm not mathematician and I don't want to explain why it works and how the matrix multiplication is realized. For mathematical background see another resource (for example wikipedia is good source)
But few facts from theory is required.
1) Multiplication of matrices is not commutative
- If you have two real numbers a and b then a*b = b*a. -> Is commutative
- This is not true for matrices. If you have two matrices A and B then A*B =/= B*A. -> Is NOT commutative
2) You can change multiplication order (A*B)*C = A*(B*C).
Few facts of transformation matrices
1) Multiplication of matrices may not be always defined. But for transformation matrices is defined every time.
2) Transformation matrices describes individual moves (rotation and translation) and can combine them to single complex move.
3) Each simple move is always defined in object's coordinate system.
What API Methods does:
Following API method multiplies two matrices in defined order
PostMutiplyBy: A.PostMutiplyBy(B) => A*B
PreMultiplyBy: A.PreMultiplyBy(B) => B*A
Practical impacts
Let you have two transformation matrices Tx which describes translation in X direction and Tr which describes rotation about object's origin of PI/4 rad (45 deg counter clockwise) and block which can be transformed by matrix T.
Situation 1
In this situation we apply translation first and then apply rotation T = Tx * Tr. The result will look like this
Situation 2
In this situation we apply rotation first and then apply translation T = Tr * Tx. The result will look like this
Code sample for testing
'Initialization
Dim part As PartDocument = ThisDoc.Document
Dim sketch As PlanarSketch = part.ComponentDefinition.Sketches(1)
Dim block As SketchBlock = sketch.SketchBlocks(1)
'Setup matrices
Dim translationMtx As Matrix2d = ThisApplication.TransientGeometry.CreateMatrix2d()
Dim translationVector As Vector2d = ThisApplication.TransientGeometry.CreateVector2d(3, 0)
translationMtx.SetTranslation(translationVector)
Dim rotationMtx As Matrix2d = ThisApplication.TransientGeometry.CreateMatrix2d()
Dim center As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(0, 0)
rotationMtx.SetToRotation(Math.PI / 4, center)
Dim invertedRotationMtx = rotationMtx.Copy()
invertedRotationMtx.Invert()
Dim blockTransformation As Matrix2d = ThisApplication.TransientGeometry.CreateMatrix2d()
'Uncomment following situations for testing
'Situation 1
' with PostMultiplyBy
blockTransformation.PostMultiplyBy(translationMtx) 'Tx
blockTransformation.PostMultiplyBy(rotationMtx) 'Tr
''Situation 2
'' with PostMultiplyBy
'blockTransformation.PostMultiplyBy(rotationMtx) 'Tr
'blockTransformation.PostMultiplyBy(translationMtx) 'Tx
''Situation 2
'' with PreMultiplyBy
'blockTransformation.PostMultiplyBy(translationMtx) 'Tx
'blockTransformation.PreMultiplyBy(rotationMtx) 'Tr
block.Transformation = blockTransformation
ThisApplication.ActiveView.Update