03-08-2016
06:24 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
03-08-2016
06:24 AM
Yes.
You need to fetch the Axes from the occurrence in the context of the assembly by using a Proxy.
With this WorkAxisProxy, you can then do the necessary vector maths to rotate this peice.
Also, the settoRotation and rotate functions are weird. I was having issues trying to use them, so I just went back to straight vector/matrix math. Although it's a little more programming, it is easier to understand/control it seems.
'This is actually a tough one because it depends on what axis you rotate about first. Just think about it.
Sub Main()
Dim oFloorNormalSelected = "Y"
Dim oDoc As Document
oDoc = ThisApplication.ActiveDocument
Dim oOcc As ComponentOccurrence
Dim oSelectSet As SelectSet
oSelectSet = oDoc.SelectSet
If oSelectSet.Count = 1
oOcc = oSelectSet.Item(1)
Else
MsgBox("Invalid Selection Set!" & vbLf & vbLf & "Aborting rule!")
Exit Sub
End If
If oFloorNormalSelected = ""
r = InputBox("Set values of relative rotation of occurence in context of assembly:" & vbLf & "X:90 OR other format for reset", "Rotation", "X:0")
Try
Dim substring() As String = r.Split(":")
Call RotateOccurrence(oDoc, oOcc, substring(1), substring(0))
Catch
Call RotateOccurrence(oDoc, oOcc, 0, "")
End Try
Else
r = InputBox("Set values of relative rotation of occurence in context of assembly:" & vbLf & vbLf & "ie; 90" & vbLf & "or make blank for current rotations", "Rotation", "0")
Try
Call RotateOccurrence(oDoc, oOcc, r, oFloorNormalSelected)
Catch
GetCurrentRotation(oDoc, oOcc)
End Try
End If
End Sub
Sub RotateOccurrence(oAsmDoc As Document, oOcc As ComponentOccurrence, oRotationDeg As Double, oAxis As String)
Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
Dim oRx As Matrix
oRx = oTG.CreateMatrix()
oRx.Cell(2,2) = Math.Cos(oRotationDeg*Math.PI/180)
oRx.Cell(2,3) = -Math.Sin(oRotationDeg*Math.PI/180)
oRx.Cell(3,2) = Math.Sin(oRotationDeg*Math.PI/180)
oRx.Cell(3,3) = Math.Cos(oRotationDeg*Math.PI/180)
Dim oRy As Matrix
oRy = oTG.CreateMatrix()
oRy.Cell(1,1) = Math.Cos(oRotationDeg*Math.PI/180)
oRy.Cell(1,3) = Math.Sin(oRotationDeg*Math.PI/180)
oRy.Cell(3,1) = -Math.Sin(oRotationDeg*Math.PI/180)
oRy.Cell(3,3) = Math.Cos(oRotationDeg*Math.PI/180)
Dim oRz As Matrix
oRz = oTG.CreateMatrix()
oRz.Cell(1,1) = Math.Cos(oRotationDeg*Math.PI/180)
oRz.Cell(1,2) = -Math.Sin(oRotationDeg*Math.PI/180)
oRz.Cell(2,1) = Math.Sin(oRotationDeg*Math.PI/180)
oRz.Cell(2,2) = Math.Cos(oRotationDeg*Math.PI/180)
Dim oOccTransform As Matrix = oOcc.Transformation
Dim oTransVec As Vector = oOccTransform.Translation
Select Case True
Case oAxis = "X"
oOccTransform.PreMultiplyBy(oRx)
Case oAxis = "Y"
oOccTransform.PreMultiplyBy(oRy)
Case oAxis = "Z"
oOccTransform.PreMultiplyBy(oRz)
Case Else
MsgBox("Error in rotating matrix; returning to identity")
oOccTransform.SetToIdentity
End Select
oOccTransform.SetTranslation(oTransVec, False)
oOcc.SetTransformWithoutConstraints(oOccTransform)
'PrintMatrix(oOccTransform)
End Sub
Sub PrintMatrix(oMatrix As Matrix)
For i = 4 To 1 Step -1
For j = 4 To 1 Step -1
oStr = oMatrix.Cell(i,j) & " " & oStr
Next
oStr = vbLf & oStr
Next
MsgBox(oStr)
End Sub
--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.
Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization

iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread
Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects
Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help
Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization
iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread
Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects
Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help
Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type