Hello,
How can I get the rotation of the occurrence? I can measure the angle between the axis from the assembly and the occurrence, but then I have no direction.
In my case, all occurrences are parallel to the main coordinate axes
Hi @Hubert_Los. I do not think that data you are pointing out in that image is available for us to access through the iProperties by code. Most of the data on that tab is specific to one assembly component occurrence within an assembly. While iProperties are stored at the document level, not at the component occurrence level.
There is one exception...with the 'instance' properties that were introduced in 2022 version of Inventor (along with ModelStates), which are not even initially enabled, so there are normally not any, until we create some on purpose. Then those instance properties get stored with the 'custom' iProperties.
So, the only way to get those values by code is to measure them ourselves. I know you said that you already know how to measure between occurrence axes and assembly axes, but here is an example iLogic routine you can review for extracting this type of data from a 'top level' component. The component being 'top level' is important, because the component must exist within the same context (3D coordinate space) as the main assembly, or else it will be impossible to measure between them. Therefore, if you want to measure where a component down within a sub assembly is in relation to the main assembly's context, you must have the top level 'Proxy' of that component, because that top level proxy will exist within the context of the main assembly, instead of within the context of the sub assembly.
Just so you know, if our code calls the 'Pick' function to be used while an assembly is the 'active' document, and we are not in 'edit mode' of any components, then the 'Pick' function will always return the top level proxy of anything we select for us automatically, because it was 'Picked' within the context of the main assembly.
This example code uses a custom Sub routine, which accepts a ComponentOccurrence as 'input', then extracts all necessary data from that object and its properties. At its end, it will write a 'report' to the iLogic Log window, for convenience. But, the iLogic Log tab should already be visible (but not necessarily the 'active' tab) before the rule runs, or it will not capture the report. If it is not visible, go to the View tab, Windows panel, and click on the User Interface drop-down, then make sure there is a checkmark in the box next to "iLogic Log". The report contains both position and rotation data of the component, in reference to the origin, and origin axes of the assembly. And if you do not understand 'direction' of the angles, just keep the 'right-hand rule' of 3D coordinate systems in mind. But it was not super clear exactly what you are looking for (trying to do) so, if this does not answer your question(s), then please explain it in as much detail as possible.
Sub Main
Dim oPickedOcc As ComponentOccurrence = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyOccurrenceFilter, "Select a Component.")
If oPickedOcc Is Nothing Then Return
LogComponentPositionAndRotation(oPickedOcc)
End Sub
Sub LogComponentPositionAndRotation(oOcc As ComponentOccurrence)
'get Assembly Origin Axes Directions
Dim oADef As AssemblyComponentDefinition = oOcc.Parent
Dim oAWAs As WorkAxes = oADef.WorkAxes
Dim oADirs As New List(Of UnitVector)
For i As Integer = 1 To 3 : oADirs.Add(oAWAs(i).Line.Direction) : Next
'get component Matrix
Dim oMatrix As Matrix = oOcc.Transformation
'get Assembly UnitsOfMeasure for Converting 'database Units' to 'document Units' later
Dim oADoc As AssemblyDocument = oADef.Document
Dim UOM As UnitsOfMeasure = oADoc.UnitsOfMeasure
'get component tranlation (position in X, Y, & Z)
Dim oTrans As Inventor.Vector = oMatrix.Translation
Dim oPos As New List(Of Double)
oPos.Add(Round(UOM.ConvertUnits(oTrans.X, "cm", UOM.LengthUnits), 6))
oPos.Add(Round(UOM.ConvertUnits(oTrans.Y, "cm", UOM.LengthUnits), 6))
oPos.Add(Round(UOM.ConvertUnits(oTrans.Z, "cm", UOM.LengthUnits), 6))
'get component rotation angles compared to assembly origin axes
'to do that, first get component Matrix coordinate system axis directions
Dim oMOrigin As Inventor.Point, oMXAxis, oMYAxis, oMZAxis As Inventor.Vector
oMatrix.GetCoordinateSystem(oMOrigin, oMXAxis, oMYAxis, oMZAxis)
'now get angles between assembly axes and component Matrix axes
Dim oMDirs As New List(Of UnitVector)
oMDirs.AddRange({oMXAxis.AsUnitVector, oMYAxis.AsUnitVector, oMZAxis.AsUnitVector })
'now compute angles
Dim oAngles As New List(Of Double)
For i As Integer = 0 To 2
oAngles.Add(Round(UOM.ConvertUnits(oADirs(i).AngleTo(oMDirs(i)), "rad", UOM.AngleUnits), 2))
Next
'now create report using the collected data
Dim sReport As String = "Component Position & Rotation:" _
& vbCrLf & "Position: " & oPos(0) & " ; " & oPos(1) & " ; " & oPos(2) _
& vbCrLf & "X Rotation: " & oAngles(0) _
& vbCrLf & "Y Rotation: " & oAngles(1) _
& vbCrLf & "Z Rotation: " & oAngles(2)
'now write the report to the iLogic Log window
Logger.Info(vbCrLf & sReport)
End Sub
If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.
Wesley Crihfield
(Not an Autodesk Employee)
Can't find what you're looking for? Ask the community or share your knowledge.