Create UCSs from List with iLogic

Create UCSs from List with iLogic

Curtis_Waguespack
Consultant Consultant
203 Views
3 Replies
Message 1 of 4

Create UCSs from List with iLogic

Curtis_Waguespack
Consultant
Consultant

I thought I would share this example here in case it is of help to someone in the future.

 

This creates UCSs based on a list setting the XYZ locations and rotations. If the named UCS exists already it simply updates it.

 

A big thank you to @JhoelForshav for his example from this link to create the matrix

https://forums.autodesk.com/t5/inventor-programming-ilogic/ilogic-place-a-part-and-define-rotation/m...

 

Curtis_Waguespack_0-1741896682562.png

 

EESignature

0 Likes
204 Views
3 Replies
Replies (3)
Message 2 of 4

Curtis_Waguespack
Consultant
Consultant

 


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
']

 

 

 

 

EESignature

Message 3 of 4

WCrihfield
Mentor
Mentor

Hi @Curtis_Waguespack.  Nice code example.  I haven't tried it yet, but while quickly reading down over the code in your post, I saw something that I thought I should point-out, just in case it may be a typo or copy/paste mistake type situation.  In the image below of a portion of the code within the CreateParams method, I have something circled.  Seems like that last 5 should be a 6, but I could be wrong.

WCrihfield_0-1741956696534.png

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 4

Curtis_Waguespack
Consultant
Consultant

 

Curtis_Waguespack_0-1741957537465.gif

 

@WCrihfield, nice catch!

You're exactly right.

I was reusing some code as the basis for this.

I updated the previous post to fix the string array. And changed also changed the name of the sub from CreateParms to ParseStrings, which makes a bit more sense.

Thanks!

 

 

EESignature

0 Likes