Part Position (Matrix) Question

Part Position (Matrix) Question

Anonymous
Not applicable
645 Views
2 Replies
Message 1 of 3

Part Position (Matrix) Question

Anonymous
Not applicable
In the code from below (copied from help file) I can SET the roation of a part to be inserted

Public Sub AddOccurrence()
' Set a reference to the assembly component definintion.
' This assumes an assembly document is open.
Dim oAsmCompDef As AssemblyComponentDefinition
Set oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

' Set a reference to the transient geometry object.
Dim oTG As TransientGeometry
Set oTG = ThisApplication.TransientGeometry

' Create a matrix. A new matrix is initialized with an identity matrix.
Dim oMatrix As Matrix
Set oMatrix = oTG.CreateMatrix

' Set the rotation of the matrix for a 45 degree rotation about the Z axis.
Call oMatrix.SetToRotation(3.14159265358979 / 4, _
oTG.CreateVector(0, 0, 1), oTG.CreatePoint(0, 0, 0))

' Set the translation portion of the matrix so the part will be positioned
' at (3,2,1).
Call oMatrix.SetTranslation(oTG.CreateVector(3, 2, 1))

' Add the occurrence.
Dim oOcc As ComponentOccurrence
Set oOcc = oAsmCompDef.Occurrences.Add("C:\Temp\Part1.ipt", oMatrix)
End Sub


How do I GET the rotational info of a part already inserted?

Cheers
MattH
0 Likes
646 Views
2 Replies
Replies (2)
Message 2 of 3

Anonymous
Not applicable
The following is a subroutine that starts with an identity matrix, sets the
rotation about a vector and about the origin by a particular angle. It then
computes back this rotation angle about the same vector (after making it a
unit vector) from the matrix cell data. So, you can calculate the rotation
angle about any vector (for example, by changing the rotation vector before
calculating the angle). You have to make sure that the vector is a unit
vector.

Public Sub SetGetRotation()

Dim PI As Double
PI = 4 * Math.Atn(1)

Dim Matrix As Matrix
Set Matrix = ThisApplication.TransientGeometry.CreateMatrix

Dim RotationVector As Vector
Set RotationVector = ThisApplication.TransientGeometry.CreateVector(0,
0, 1)

'set the rotation for the matrix (works for angle 0 - 360 degrees)
Call Matrix.SetToRotation(PI / 4, RotationVector,
ThisApplication.TransientGeometry.CreatePoint(0, 0, 0))

'now, get back the rotation angle about the above specified rotation
vector (from the matrix)
Dim UnitRotationVector As UnitVector
Set UnitRotationVector = RotationVector.AsUnitVector

Dim cosangle As Double
If Not UnitRotationVector.X = 1 Then
cosangle = 1 - (Matrix.Cell(1, 1) - 1) / (UnitRotationVector.X *
UnitRotationVector.X - 1)
ElseIf Not UnitRotationVector.Y = 1 Then
cosangle = 1 - (Matrix.Cell(2, 2) - 1) / (UnitRotationVector.Y *
UnitRotationVector.Y - 1)
ElseIf Not UnitRotationVector.Z = 1 Then
cosangle = 1 - (Matrix.Cell(3, 3) - 1) / (UnitRotationVector.Z *
UnitRotationVector.Z - 1)
End If

Dim singangle As Double
If Not UnitRotationVector.X = 0 Then
sinangle = -(Matrix.Cell(2, 3) - (1 - cosangle) *
UnitRotationVector.Y * UnitRotationVector.Z) / UnitRotationVector.X
ElseIf Not UnitRotationVector.Y = 0 Then
sinangle = (Matrix.Cell(1, 3) - (1 - cosangle) *
UnitRotationVector.X * UnitRotationVector.Z) / UnitRotationVector.Y
ElseIf Not UnitRotationVector.Z = 0 Then
sinangle = -(Matrix.Cell(1, 2) - (1 - cosangle) *
UnitRotationVector.X * UnitRotationVector.Y) / UnitRotationVector.Z
End If

'get the angle of rotation in degrees
Dim angle As Double

If Not Math.Abs(cosangle) < 0.000000001 Then
angle = Math.Atn(sinangle / cosangle) * 180 / PI
Else
If sinangle > 0 Then
angle = 90
Else
angle = 270
End If
End If
End Sub

-Venkatesh Thiyagarajan.

wrote in message news:5244568@discussion.autodesk.com...
In the code from below (copied from help file) I can SET the roation of a
part to be inserted

Public Sub AddOccurrence()
' Set a reference to the assembly component definintion.
' This assumes an assembly document is open.
Dim oAsmCompDef As AssemblyComponentDefinition
Set oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

' Set a reference to the transient geometry object.
Dim oTG As TransientGeometry
Set oTG = ThisApplication.TransientGeometry

' Create a matrix. A new matrix is initialized with an identity matrix.
Dim oMatrix As Matrix
Set oMatrix = oTG.CreateMatrix

' Set the rotation of the matrix for a 45 degree rotation about the Z
axis.
Call oMatrix.SetToRotation(3.14159265358979 / 4, _
oTG.CreateVector(0, 0, 1), oTG.CreatePoint(0, 0,
0))

' Set the translation portion of the matrix so the part will be
positioned
' at (3,2,1).
Call oMatrix.SetTranslation(oTG.CreateVector(3, 2, 1))

' Add the occurrence.
Dim oOcc As ComponentOccurrence
Set oOcc = oAsmCompDef.Occurrences.Add("C:\Temp\Part1.ipt", oMatrix)
End Sub


How do I GET the rotational info of a part already inserted?

Cheers
MattH
0 Likes
Message 3 of 3

Anonymous
Not applicable
The subroutine code that I posted previously didn't correctly calculate the
angle value if they were greater than 90 degrees. I made some small changes,
hopefully, the following should work for all angles between 0 and 360
degrees.

Public Sub MatrixManipulation()

Dim PI As Double
PI = 4 * Math.Atn(1)

Dim Matrix As Matrix
Set Matrix = ThisApplication.TransientGeometry.CreateMatrix

Dim RotationVector As Vector
Set RotationVector =
ThisApplication.TransientGeometry.CreateVector(2, -3, 7)

'set the rotation for the matrix
Call Matrix.SetToRotation(PI * 45 / 180, RotationVector,
ThisApplication.TransientGeometry.CreatePoint(0, 0, 0))

'now, get back the rotation angle about the above specified rotation
vector (from the matrix)
Dim UnitRotationVector As UnitVector
Set UnitRotationVector = RotationVector.AsUnitVector

Dim cosangle As Double
If Not UnitRotationVector.X = 1 Then
cosangle = 1 - (Matrix.Cell(1, 1) - 1) / (UnitRotationVector.X *
UnitRotationVector.X - 1)
ElseIf Not UnitRotationVector.Y = 1 Then
cosangle = 1 - (Matrix.Cell(2, 2) - 1) / (UnitRotationVector.Y *
UnitRotationVector.Y - 1)
ElseIf Not UnitRotationVector.Z = 1 Then
cosangle = 1 - (Matrix.Cell(3, 3) - 1) / (UnitRotationVector.Z *
UnitRotationVector.Z - 1)
End If

Dim sinangle As Double
If Not UnitRotationVector.X = 0 Then
sinangle = -(Matrix.Cell(2, 3) - (1 - cosangle) *
UnitRotationVector.Y * UnitRotationVector.Z) / UnitRotationVector.X
ElseIf Not UnitRotationVector.Y = 0 Then
sinangle = (Matrix.Cell(1, 3) - (1 - cosangle) *
UnitRotationVector.X * UnitRotationVector.Z) / UnitRotationVector.Y
ElseIf Not UnitRotationVector.Z = 0 Then
sinangle = -(Matrix.Cell(1, 2) - (1 - cosangle) *
UnitRotationVector.X * UnitRotationVector.Y) / UnitRotationVector.Z
End If

'get the angle of rotation in degrees
Dim angle As Double

If Not Math.Abs(cosangle) < 0.000000001 Then
angle = Math.Atn(Abs(sinangle) / Abs(cosangle)) * 180 / PI

If sinangle > 0 Then
If cosangle < 0 Then
angle = 180 - angle
End If
ElseIf sinangle < 0 Then
If cosangle < 0 Then
angle = 180 + angle
Else
angle = 360 - angle
End If
End If
Else
If sinangle > 0 Then
angle = 90
Else
angle = 270
End If
End If

MsgBox CStr(angle)
End Sub

"Venkatesh Thiyagarajan (Autodesk)"
wrote in message news:5245234@discussion.autodesk.com...
The following is a subroutine that starts with an identity matrix, sets the
rotation about a vector and about the origin by a particular angle. It then
computes back this rotation angle about the same vector (after making it a
unit vector) from the matrix cell data. So, you can calculate the rotation
angle about any vector (for example, by changing the rotation vector before
calculating the angle). You have to make sure that the vector is a unit
vector.

Public Sub SetGetRotation()

Dim PI As Double
PI = 4 * Math.Atn(1)

Dim Matrix As Matrix
Set Matrix = ThisApplication.TransientGeometry.CreateMatrix

Dim RotationVector As Vector
Set RotationVector = ThisApplication.TransientGeometry.CreateVector(0,
0, 1)

'set the rotation for the matrix (works for angle 0 - 360 degrees)
Call Matrix.SetToRotation(PI / 4, RotationVector,
ThisApplication.TransientGeometry.CreatePoint(0, 0, 0))

'now, get back the rotation angle about the above specified rotation
vector (from the matrix)
Dim UnitRotationVector As UnitVector
Set UnitRotationVector = RotationVector.AsUnitVector

Dim cosangle As Double
If Not UnitRotationVector.X = 1 Then
cosangle = 1 - (Matrix.Cell(1, 1) - 1) / (UnitRotationVector.X *
UnitRotationVector.X - 1)
ElseIf Not UnitRotationVector.Y = 1 Then
cosangle = 1 - (Matrix.Cell(2, 2) - 1) / (UnitRotationVector.Y *
UnitRotationVector.Y - 1)
ElseIf Not UnitRotationVector.Z = 1 Then
cosangle = 1 - (Matrix.Cell(3, 3) - 1) / (UnitRotationVector.Z *
UnitRotationVector.Z - 1)
End If

Dim singangle As Double
If Not UnitRotationVector.X = 0 Then
sinangle = -(Matrix.Cell(2, 3) - (1 - cosangle) *
UnitRotationVector.Y * UnitRotationVector.Z) / UnitRotationVector.X
ElseIf Not UnitRotationVector.Y = 0 Then
sinangle = (Matrix.Cell(1, 3) - (1 - cosangle) *
UnitRotationVector.X * UnitRotationVector.Z) / UnitRotationVector.Y
ElseIf Not UnitRotationVector.Z = 0 Then
sinangle = -(Matrix.Cell(1, 2) - (1 - cosangle) *
UnitRotationVector.X * UnitRotationVector.Y) / UnitRotationVector.Z
End If

'get the angle of rotation in degrees
Dim angle As Double

If Not Math.Abs(cosangle) < 0.000000001 Then
angle = Math.Atn(sinangle / cosangle) * 180 / PI
Else
If sinangle > 0 Then
angle = 90
Else
angle = 270
End If
End If
End Sub

-Venkatesh Thiyagarajan.

wrote in message news:5244568@discussion.autodesk.com...
In the code from below (copied from help file) I can SET the roation of a
part to be inserted

Public Sub AddOccurrence()
' Set a reference to the assembly component definintion.
' This assumes an assembly document is open.
Dim oAsmCompDef As AssemblyComponentDefinition
Set oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

' Set a reference to the transient geometry object.
Dim oTG As TransientGeometry
Set oTG = ThisApplication.TransientGeometry

' Create a matrix. A new matrix is initialized with an identity matrix.
Dim oMatrix As Matrix
Set oMatrix = oTG.CreateMatrix

' Set the rotation of the matrix for a 45 degree rotation about the Z
axis.
Call oMatrix.SetToRotation(3.14159265358979 / 4, _
oTG.CreateVector(0, 0, 1), oTG.CreatePoint(0, 0,
0))

' Set the translation portion of the matrix so the part will be
positioned
' at (3,2,1).
Call oMatrix.SetTranslation(oTG.CreateVector(3, 2, 1))

' Add the occurrence.
Dim oOcc As ComponentOccurrence
Set oOcc = oAsmCompDef.Occurrences.Add("C:\Temp\Part1.ipt", oMatrix)
End Sub


How do I GET the rotational info of a part already inserted?

Cheers
MattH
0 Likes