AnsweredAssumed Answered

Move and rotate component

Question asked by Michael Rauter on Feb 12, 2018
Latest reply on Feb 13, 2018 by Michael Rauter

I am in desperate need of some help with a portion of a macro that is supposed to rotate a part.


I (macro) recorded the necessary step and ended up with the code below.

Even with my limited programming skills I recodnice SW seems to control rotation and movement with the array TransformData()

 

Controlling regular movement in x, y and z is easy enough:
You just need to put the value / 1000 in vTransformData(9), vTransformData(10), vTransformData(11)

 

Rotation is the problem:
TransformData(0 to 8) needs to be filled with values between -1 and 1. But it seems impossible to me get to them from regular angles [°].

Troughs the process of elimination I ended up with the following calculation. It seems to work well enough with most angles. But when one of the angles is 90° (etc.) you get a multiplication with 0. But you don’t get 0 when you (macro) record the same rotation.

 

vTransformData(0) = COS(Y/180*PI) * COS(Z/180*PI)

vTransformData(1) = COS(X/180*PI) * SIN(Z/180*PI) <<

vTransformData(2) = SIN(Z/180*PI) * SIN(X/180*PI) <<

vTransformData(3) = -COS(Y/180*PI) * SIN(Z/180*PI)

vTransformData(4) = COS(Z/180*PI) * COS(X/180*PI) <<

vTransformData(5) = COS(Z/180*PI) * SIN(X/180*PI) <<

vTransformData(6) = SIN(Y/180*PI)

vTransformData(7) = -SIN(X/180*PI) * COS(Y/180*PI)

vTransformData(8) = COS(X/180*PI) * COS(Y/180*PI)

 

Dim swApp As Object

Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long

Sub main()

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc
boolstatus = Part.Extension.SelectByID2("Point1@Ursprung", "EXTSKETCHPOINT", 0, 0, 0, False, 0, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("Modell-9@Baugruppe1", "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)
Part.ClearSelection2 True
boolstatus = Part.Extension.SelectByID2("Modell-9@Baugruppe1", "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)
Dim TransformData() As Double
ReDim TransformData(0 To 15) As Double
TransformData(0) = 0
TransformData(1) = 1
TransformData(2) = 0
TransformData(3) = -1
TransformData(4) = 0
TransformData(5) = 0
TransformData(6) = 0
TransformData(7) = 0
TransformData(8) = 1
TransformData(9) = 0
TransformData(10) = 0
TransformData(11) = 0
TransformData(12) = 1
TransformData(13) = 0
TransformData(14) = 0
TransformData(15) = 0
Dim TransformDataVariant As Variant
TransformDataVariant = TransformData
Dim swMathUtil As Object
Set swMathUtil = swApp.GetMathUtility()
Dim swTransform As Object
Set swTransform = swMathUtil.CreateTransform((TransformDataVariant))
Dim swComp As Object
Set swComp = Part.SelectionManager.GetSelectedObjectsComponent4(1, -1)
boolstatus = swComp.SetTransformAndSolve2(swTransform)
boolstatus = Part.ForceRebuild3(False)
Part.ClearSelection2 True
End Sub

Outcomes