2 Replies Latest reply on Feb 13, 2018 10:32 AM by Michael Rauter

# Move and rotate component

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

• ###### Re: Move and rotate component
```Option Explicit
Const PI                As Double = 3.14159
Const RadPerDeg         As Double = PI / 180#
Dim swApp As SldWorks.SldWorks

Sub main()

Dim swModel As ModelDoc2
Dim swAssy As AssemblyDoc
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim swComp As Component2
Dim vComponents As Variant
Dim swTransform As MathTransform
Dim swRotTransform As MathTransform

Dim nPts(2) As Double
Dim vData As Variant
Dim swOriginPt As SldWorks.MathPoint
Dim swX_Axis As SldWorks.MathVector

Set swApp = Application.SldWorks

Set swModel = swApp.ActiveDoc

swModel.ClearSelection2 True
Set swAssy = swModel

vComponents = swAssy.GetComponents(True)
Set swComp = vComponents(0)

Set swTransform = swComp.Transform2
Dim swMathUtil As MathUtility
Set swMathUtil = swApp.GetMathUtility()

nPts(0) = swTransform.ArrayData(9)
nPts(1) = swTransform.ArrayData(10)
nPts(2) = swTransform.ArrayData(11)
vData = nPts
Set swOriginPt = swMathUtil.CreatePoint(vData)

nPts(0) = swTransform.ArrayData(0)
nPts(1) = swTransform.ArrayData(1)
nPts(2) = swTransform.ArrayData(2)
vData = nPts
Set swX_Axis = swMathUtil.CreateVector(vData)

Set swRotTransform = swMathUtil.CreateTransformRotateAxis(swOriginPt, swX_Axis, 10# * RadPerDeg)
Set swTransform = swTransform.Multiply(swRotTransform)
boolstatus = swComp.SetTransformAndSolve2(swTransform)
boolstatus = swModel.ForceRebuild3(False)
swModel.ClearSelection2 True

End Sub
```