About Transforming Objects (VBA/ActiveX)

You move, scale, or rotate an object given a 4 by 4 transformation matrix using the TransformBy method.

The following table demonstrates the transformation matrix configuration, where R = Rotation and T = Translation:

Transformation matrix configuration
R00 R01 R02 T0
R10 R11 R12 T1
R20 R21 R22 T2
0 0 0 1

To transform an object, first initialize the transformation matrix. The following example shows a transformation matrix, assigned to the variable tMatrix, which will rotate an entity by 90 degrees about the point (0, 0, 0):

tMatrix(0,0) = 0.0
tMatrix(0,1) = -1.0
tMatrix(0,2) = 0.0
tMatrix(0,3) = 0.0
tMatrix(1,0) = 1.0
tMatrix(1,1) = 0.0
tMatrix(1,2) = 0.0
tMatrix(1,3) = 0.0
tMatrix(2,0) = 0.0
tMatrix(2,1) = 0.0
tMatrix(2,2) = 1.0
tMatrix(2,3) = 0.0
tMatrix(3,0) = 0.0
tMatrix(3,1) = 0.0
tMatrix(3,2) = 0.0
tMatrix(3,3) = 1.0

After the transformation matrix is complete, apply the matrix to the object using the TransformBy method. The following line of code demonstrates applying a matrix (tMatrix) to an object (anObj):

anObj.TransformBy tMatrix

Rotate a line with a transformation matrix

This example creates a line and rotates it 90 degrees using a transformation matrix.

Sub Ch4_TransformBy()
  ' Create a line
  Dim lineObj As AcadLine
  Dim startPt(0 To 2) As Double
  Dim endPt(0 To 2) As Double
  startPt(0) = 2
  startPt(1) = 1
  startPt(2) = 0
  endPt(0) = 5
  endPt(1) = 1
  endPt(2) = 0
  Set lineObj = ThisDrawing.ModelSpace.AddLine(startPt, endPt)
  ZoomAll

  ' Initialize the transMat variable with a
  ' transformation matrix that will rotate
  ' an object by 90 degrees about the point(0,0,0)
  Dim transMat(0 To 3, 0 To 3) As Double
  transMat(0, 0) = 0#: transMat(0, 1) = -1#
  transMat(0, 2) = 0#: transMat(0, 3) = 0#
  transMat(1, 0) = 1#: transMat(1, 1) = 0#
  transMat(1, 2) = 0#: transMat(1, 3) = 0#
  transMat(2, 0) = 0#: transMat(2, 1) = 0#
  transMat(2, 2) = 1#: transMat(2, 3) = 0#
  transMat(3, 0) = 0#: transMat(3, 1) = 0#
  transMat(3, 2) = 0#: transMat(3, 3) = 1#

  ' Transform the line using the defined transformation matrix
  lineObj.TransformBy transMat
  lineObj.Update
End Sub

The following are more examples of transformation matrices:

Rotation Matrix: 90 degrees about point (0, 0, 0)
0.0 -1.0 0.0 0.0
1.0 0.0 0.0 0.0
0.0 0.0 1.0 0.0
0.0 0.0 0.0 1.0
Rotation Matrix: 45 degrees about point (5, 5, 0)
0.707107 -0.707107 0.0 5.0
0.707107 0.707107 0.0 -2.071068
0.0 0.0 1.0 0.0
0.0 0.0 0.0 1.0
Translation Matrix: move an entity by (10, 10, 0)
1.0 0.0 0.0 10.0
0.0 1.0 0.0 10.0
0.0 0.0 1.0 0.0
0.0 0.0 0.0 1.0
Scaling Matrix: scale by 10,10 at point (0, 0, 0)
10.0 0.0 0.0 0.0
0.0 10.0 0.0 0.0
0.0 0.0 10.0 0.0
0.0 0.0 0.0 1.0
Scaling Matrix: scale by 10,10 at point (2, 2, 0)
10.0 0.0 0.0 -18.0
0.0 10.0 0.0 -18.0
0.0 0.0 10.0 0.0
0.0 0.0 0.0 1.0