Sub Main
Dim UCSList As New ArrayList
'UCS Name | X location| Y location| Z location| X rotation | Y rotation | Z rotation
UCSList.Add("UCS01|0|0|12|010|0|0")
UCSList.Add("UCS02|0|0|24|045|0|0")
UCSList.Add("UCS03|0|0|36|135|0|0")
'send striing to sub routine
For Each ListString In UCSList : ParseStrings(ListString) : Next
iLogicVb.UpdateWhenDone = True
End Sub
Sub ParseStrings(ListString As String)
' Get the active document
Dim oDoc As Document = ThisDoc.Document
Dim oCompDef As ComponentDefinition = oDoc.ComponentDefinition
'split each string using the | as the split character
StringArray = Split(ListString, "|")
ArrayCount = UBound(StringArray)
If ArrayCount < 6 Then
MsgBox(ListString & vbLf & "Only " & ArrayCount & " ' | ' seperator chrs in string" & vbLf & "Expected 6", , "ilogic")
Exit Sub
End If
'set up conversion factors
Dim DocumentUnitsOfMeasure As UnitsOfMeasure = oDoc.UnitsOfMeasure
Cnvt_in_to_cm = DocumentUnitsOfMeasure.ConvertUnits(1, UnitsTypeEnum.kInchLengthUnits, UnitsTypeEnum.kCentimeterLengthUnits)
UCSName = StringArray(0) 'first item in array
UCS_LocationX = StringArray(1) * Cnvt_in_to_cm
UCS_LocationY = StringArray(2) * Cnvt_in_to_cm
UCS_LocationZ = StringArray(3) * Cnvt_in_to_cm
UCS_RotationX = StringArray(4)
UCS_RotationY = StringArray(5)
UCS_RotationZ = StringArray(6)
' Get the UCS collection
Dim oUCSs As UserCoordinateSystems = oCompDef.UserCoordinateSystems
Dim oUCS As UserCoordinateSystem = Nothing
'check for existing UCS
For Each oExistingUCS In oUCSs
If oExistingUCS.name = UCSName Then
oUCS = oExistingUCS
Exit For
End If
Next
Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
Dim oUCSDef As UserCoordinateSystemDefinition = oUCSs.CreateDefinition()
Dim oMatrix As Inventor.Matrix = oTG.CreateMatrix()
'add new UCS if needed
If oUCS Is Nothing Then oUCS = oUCSs.Add(oUCSDef)
'create posistion and rotation matrix
oMatrix = CreateMatrixRotate(UCS_LocationX, UCS_LocationY, UCS_LocationZ, UCS_RotationX, UCS_RotationY, UCS_RotationZ, "X,Y,Z")
oUCS.Transformation = oMatrix
'set name
oUCS.Name = UCSName
oUCS.Visible = True
End Sub
'JhoelForshav's matrix function and subs
'https://forums.autodesk.com/t5/inventor-programming-ilogic/ilogic-place-a-part-and-define-rotation/m-p/8983364/highlight/true#M100656
'[ expand to see code
Function CreateMatrixRotate(X As Double, Y As Double, Z As Double, _
XAngleDeg As Double, YAngleDeg As Double, ZAngleDeg As Double, oRotateOrder As String) As Matrix
Dim XAngle As Double = XAngleDeg * (Math.PI / 180)
Dim YAngle As Double = YAngleDeg * (Math.PI / 180)
Dim ZAngle As Double = ZAngleDeg * (Math.PI / 180)
Dim oTg As Inventor.TransientGeometry = ThisApplication.TransientGeometry
Dim oMatrix As Matrix = oTg.CreateMatrix
'ROTATE MATRIX
Dim oFirstAxis As String = oRotateOrder.Split(",")(0)
Dim oSecondAxis As String = oRotateOrder.Split(",")(1)
Dim oThirdAxis As String = oRotateOrder.Split(",")(2)
If oFirstAxis = "X"
RotateAroundX(oMatrix, XAngle)
ElseIf oFirstAxis = "Y"
RotateAroundY(oMatrix, YAngle)
ElseIf oFirstAxis = "Z"
RotateAroundZ(oMatrix, ZAngle)
End If
If oSecondAxis = "X"
RotateAroundX(oMatrix, XAngle)
ElseIf oSecondAxis = "Y"
RotateAroundY(oMatrix, YAngle)
ElseIf oSecondAxis = "Z"
RotateAroundZ(oMatrix, ZAngle)
End If
If oThirdAxis = "X"
RotateAroundX(oMatrix, XAngle)
ElseIf oThirdAxis = "Y"
RotateAroundY(oMatrix, YAngle)
ElseIf oThirdAxis = "Z"
RotateAroundZ(oMatrix, ZAngle)
End If
'--------------------------------------------
'Translate Matrix
Dim oTranslateVector As Vector = oTg.CreateVector(X / 10, Y / 10, Z / 10)
oMatrix.SetTranslation(oTranslateVector, False)
Return oMatrix
End Function
Sub RotateAroundX(oMatrix As Matrix, rotationAngle As Double)
Dim oRotateMatrix As Matrix = ThisApplication.TransientGeometry.CreateMatrix
oRotateMatrix.Cell(1, 1) = 1
oRotateMatrix.Cell(1, 2) = 0
oRotateMatrix.Cell(1, 3) = 0
oRotateMatrix.Cell(2, 1) = 0
oRotateMatrix.Cell(2, 2) = Math.Cos(rotationAngle)
oRotateMatrix.Cell(2, 3) = -Math.Sin(rotationAngle)
oRotateMatrix.Cell(3, 1) = 0
oRotateMatrix.Cell(3, 2) = Math.Sin(rotationAngle)
oRotateMatrix.Cell(3, 3) = Math.Cos(rotationAngle)
oMatrix.TransformBy(oRotateMatrix)
End Sub
Sub RotateAroundY(oMatrix As Matrix, rotationAngle As Double)
Dim oRotateMatrix As Matrix = ThisApplication.TransientGeometry.CreateMatrix
oRotateMatrix.Cell(1, 1) = Math.Cos(rotationAngle)
oRotateMatrix.Cell(1, 2) = 0
oRotateMatrix.Cell(1, 3) = Math.Sin(rotationAngle)
oRotateMatrix.Cell(2, 1) = 0
oRotateMatrix.Cell(2, 2) = 1
oRotateMatrix.Cell(2, 3) = 0
oRotateMatrix.Cell(3, 1) = -Math.Sin(rotationAngle)
oRotateMatrix.Cell(3, 2) = 0
oRotateMatrix.Cell(3, 3) = Math.Cos(rotationAngle)
oMatrix.TransformBy(oRotateMatrix)
End Sub
Sub RotateAroundZ(oMatrix As Inventor.Matrix, rotationAngle As Double)
Dim oRotateMatrix As Matrix = ThisApplication.TransientGeometry.CreateMatrix
oRotateMatrix.Cell(1, 1) = Math.Cos(rotationAngle)
oRotateMatrix.Cell(1, 2) = -Math.Sin(rotationAngle)
oRotateMatrix.Cell(1, 3) = 0
oRotateMatrix.Cell(2, 1) = Math.Sin(rotationAngle)
oRotateMatrix.Cell(2, 2) = Math.Cos(rotationAngle)
oRotateMatrix.Cell(2, 3) = 0
oRotateMatrix.Cell(3, 1) = 0
oRotateMatrix.Cell(3, 2) = 0
oRotateMatrix.Cell(3, 3) = 1
oMatrix.TransformBy(oRotateMatrix)
End Sub
']
