Understanding Component Transforms
Master MathTransform in the SolidWorks API — the 4x4 matrix that controls component position and rotation in assemblies. Essential for coordinate conversion and component placement.
public void TransformDemo(SldWorks swApp, AssemblyDoc assy)
{
var model = (ModelDoc2)assy;
var mathUtil = (MathUtility)swApp.GetMathUtility();
// Get a component's transform
var comp = (Component2)assy.GetComponentByName(
"Bracket-1");
var xform = comp.Transform2;
// Read the 16-element array (SW's non-standard order!)
var data = (double[])xform.ArrayData;
// Elements 0-8: rotation (3x3 matrix, row-major)
// Element 9: scale factor
// Elements 10-12: translation X, Y, Z
double tx = data[9]; // translation X
double ty = data[10]; // translation Y
double tz = data[11]; // translation Z
Console.WriteLine($"Position: ({tx}, {ty}, {tz})");
// Convert a point from part coords to assembly coords
var partPoint = (MathPoint)mathUtil.CreatePoint(
new double[] { 0.05, 0, 0 }); // 50mm in part space
var assyPoint = (MathPoint)partPoint.MultiplyTransform(xform);
var coords = (double[])assyPoint.ArrayData;
Console.WriteLine($"In assembly: ({coords[0]}, {coords[1]}, {coords[2]})");
// Move a component: create a new transform
data[9] = 0.2; // move to X = 200mm
data[10] = 0.1; // move to Y = 100mm
data[11] = 0.0; // Z = 0
var newXform = (MathTransform)mathUtil.CreateTransform(data);
comp.Transform2 = newXform;
model.EditRebuild3();
}What Is a MathTransform?
Every component in a SolidWorks assembly has a transform — a 4x4 matrix that defines its position, rotation, and scale relative to the assembly origin. When you move or rotate a component in the graphics area, you're changing its transform.
The 16-Element Array
SolidWorks stores the transform as a 16-element double array, but in a non-standard order that confuses everyone:
Index: [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15]
Means: R00 R01 R02 R10 R11 R12 R20 R21 R22 Tx Ty Tz Scale 0 0 0- Elements 0-8: The 3x3 rotation matrix (row-major order)
- Element 9, 10, 11: Translation X, Y, Z (in meters!)
- Element 12: Uniform scale factor (usually 1.0)
- Elements 13-15: Unused (always 0)
This is different from the standard 4x4 matrix layout used in OpenGL, DirectX, and most 3D math libraries, where translation is in elements 12-14.
Coordinate Conversion
The most common use: converting a point from part coordinates to assembly coordinates.
- Part-to-assembly: point.MultiplyTransform(component.Transform2)
- Assembly-to-part: point.MultiplyTransform(component.Transform2.Inverse())
This is essential when you need to find the real-world position of a hole, face center, or datum point from a sub-component.
Composing Transforms
To combine multiple transforms (e.g., navigate from a sub-sub-component to assembly coordinates), multiply them:
var totalTransform = subComp.Transform2.Multiply(parentComp.Transform2);For deeply nested assemblies, chain the transforms from the leaf component up to the root.
Common Pitfalls
- Units: All translation values are in meters, regardless of your document units. If your assembly uses millimeters, divide by 1000 when reading transform positions
- Identity transform: If a component is at the assembly origin with no rotation, its transform is the identity matrix. But reading Transform2 on a root component may return null
- Suppressed components: Suppressed components have no transform. Check the suppression state before accessing Transform2
- Read-only in drawings: Component transforms in drawing views include the view's own transform — don't confuse assembly-space transforms with drawing-space positions