06-07-2024
12:04 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
06-07-2024
12:04 AM
This did not give me the exact result I was expecting, it gave it the correct rotation, but the distances between components were in the directions of the assembly origin, not the UCS. However when I use PreMultiplyBy instead of PostMultiplyBy it gives me the desired result.
After I got this working I decided not to go with using a UCS at all but instead just created the matrix that I'd have to create for the definition of the UCS anyway and use this directly for the PreMultiplyBy.
Here's my final code (not iLogic):
Public Shared Function PlaceOccurrence(ByVal sFilePath As String, ByVal Location_X As Double, ByVal Location_Y As Double, ByVal Location_Z As Double, ByVal Rotation_X As Double, ByVal Rotation_Y As Double, ByVal Rotation_Z As Double, ByVal Base_Location_X As Double, ByVal Base_Location_Y As Double, ByVal Base_Location_Z As Double, ByVal Base_Rotation_X As Double, ByVal Base_Rotation_Y As Double, ByVal Base_Rotation_Z As Double, Optional ByVal bGrounded As Boolean = True) As ComponentOccurrence
Dim oResult As ComponentOccurrence
' Set a reference to the Assembly Document
Dim oAsmDoc As AssemblyDocument = g_inventorApplication.ActiveDocument
' Set a reference to the Assembly Component Definition
Dim oAsmCompDef As AssemblyComponentDefinition = oAsmDoc.ComponentDefinition
' Create a Matrix
Dim oOccMatrix As Matrix = MS_Matrices.CreateMatrixFromLocationAndRotation(Location_X, Location_Y, Location_Z, Rotation_X, Rotation_Y, Rotation_Z)
' Create a Matrix
Dim oBaseMatrix As Matrix = MS_Matrices.CreateMatrixFromLocationAndRotation(Base_Location_X, Base_Location_Y, Base_Location_Z, Base_Rotation_X, Base_Rotation_Y, Base_Rotation_Z)
' PreMultiply the Occurrence Matrix by the Base Matrix
oOccMatrix.PreMultiplyBy(oBaseMatrix)
' Set a reference to the Component Occurrences
Dim oOccurrences As ComponentOccurrences = oAsmCompDef.Occurrences
' Place a Component Occurrence
Dim oOcc As ComponentOccurrence = oOccurrences.Add(sFilePath, oOccMatrix)
' Ground the Component Occurrence
oOcc.Grounded = bGrounded
' Set the Default View Representation if the Component Occurrence is an Assembly
If oOcc.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then oOcc.SetDesignViewRepresentation("Default", , True)
' Return the Component Occurrence
oResult = oOcc
Return oResult
End Function
Public Shared Function CreateMatrixFromLocationAndRotation(ByVal Location_X As Double, ByVal Location_Y As Double, ByVal Location_Z As Double, ByVal Rotation_X As Double, ByVal Rotation_Y As Double, ByVal Rotation_Z As Double) As Matrix
Dim oResult As Matrix
' Set a reference to the Transient Geometry
Dim oTransientGeometry As TransientGeometry = g_inventorApplication.TransientGeometry
' Create Matrices
Dim oMatrix As Matrix = oTransientGeometry.CreateMatrix
Dim oMatrix_Z As Matrix = oTransientGeometry.CreateMatrix
Dim oMatrix_Y As Matrix = oTransientGeometry.CreateMatrix
Dim oMatrix_X As Matrix = oTransientGeometry.CreateMatrix
' Set the rotation of the Matrix about the Z axis
oMatrix_Z.SetToRotation(Rotation_Z * (Math.PI / 180), oTransientGeometry.CreateVector(0, 0, 1), oTransientGeometry.CreatePoint(0, 0, 0))
' Set the rotation of the Matrix about the Y axis
oMatrix_Y.SetToRotation(Rotation_Y * (Math.PI / 180), oTransientGeometry.CreateVector(0, 1, 0), oTransientGeometry.CreatePoint(0, 0, 0))
' Set the rotation of the Matrix about the X axis
oMatrix_X.SetToRotation(Rotation_X * (Math.PI / 180), oTransientGeometry.CreateVector(1, 0, 0), oTransientGeometry.CreatePoint(0, 0, 0))
' PreMultiply the Matrix by the Matrices about each axis
oMatrix.PreMultiplyBy(oMatrix_Z)
oMatrix.PreMultiplyBy(oMatrix_Y)
oMatrix.PreMultiplyBy(oMatrix_X)
' Set the Translation of the matrix
oMatrix.SetTranslation(oTransientGeometry.CreateVector(Location_X / 10, Location_Y / 10, Location_Z / 10))
' Return the Component Occurrence
oResult = oMatrix
Return oResult
End Function
