MACRO LIBRARY

Rotate component using SetData

Free public access. No account or subscription required.

Files

Rotate component using SetData

Code

'Found online and simplified. This demonstrates IMathTransform::SetData _
    and IComponent2::SetTransformAndSolve2. No rotation occurs.
    
'Modified by Keith Rice
'CADSharp LLC
'www.cadsharp.com
    
'Preconditions: transform_test.SLDASM is open
    
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swAssy As SldWorks.AssemblyDoc
Dim swMathUtil As SldWorks.MathUtility
Dim swComp As SldWorks.Component2
Dim swTransform  As SldWorks.MathTransform
    
Sub main()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swAssy = swModel
    Set swComp = swAssy.GetComponentByName("transform_test_block-1")
    Set swMathUtil = swApp.GetMathUtility
    
    Set swTransform = swComp.Transform2
    Debug.Print "X = " & Round(swTransform.ArrayData(0), 4) & ", " & _
        Round(swTransform.ArrayData(1), 4) & ", " & Round(swTransform.ArrayData(2), 4)
    Debug.Print "Y = " & Round(swTransform.ArrayData(3), 4) & ", " & _
        Round(swTransform.ArrayData(4), 4) & ", " & Round(swTransform.ArrayData(5), 4)
    Debug.Print "Z = " & Round(swTransform.ArrayData(6), 4) & ", " & _
        Round(swTransform.ArrayData(7), 4) & ", " & Round(swTransform.ArrayData(8), 4)
    Debug.Print "pos = " & Round(swTransform.ArrayData(9), 4) & ", " & _
        Round(swTransform.ArrayData(10), 4) & ", " & Round(swTransform.ArrayData(11), 4)
    
    '##### CREATE VECTORS #####
    
    'X vector
    Dim vxVector As Variant
    Dim xVector(2) As Double
    xVector(0) = swTransform.ArrayData(0)
    xVector(1) = swTransform.ArrayData(1)
    xVector(2) = swTransform.ArrayData(2)
    vxVector = xVector
    Dim swX As SldWorks.MathVector
    Set swX = swMathUtil.CreateVector((vxVector))
    
    'Y vector
    Dim vyVector As Variant
    Dim yVector(2) As Double
    yVector(0) = swTransform.ArrayData(3)
    yVector(1) = swTransform.ArrayData(4)
    yVector(2) = swTransform.ArrayData(5)
    vyVector = yVector
    Dim swY As SldWorks.MathVector
    Set swY = swMathUtil.CreateVector((vyVector))
    
    'Z vector
    Dim vzVector As Variant
    Dim zVector(2) As Double
    zVector(0) = swTransform.ArrayData(6)
    zVector(1) = swTransform.ArrayData(7)
    zVector(2) = swTransform.ArrayData(8)
    vzVector = zVector
    Dim swZ As SldWorks.MathVector
    Set swZ = swMathUtil.CreateVector((vzVector))
    
    'Point vector
    Dim vpVector As Variant
    Dim pVector(2) As Double
    pVector(0) = 0
    pVector(1) = 0
    pVector(2) = 0
    vpVector = pVector
    Dim swP As SldWorks.MathVector
    Set swP = swMathUtil.CreateVector((vpVector))
    
    'Apply the new rotation, position, and scaling data to the current transform
    swTransform.SetData swX, swY, swZ, swP, 1#
    swComp.SetTransformAndSolve2 swTransform
    Set swTransform = swComp.Transform2
    Debug.Print "new pos= " & Round(swTransform.ArrayData(9), 4) & ", " & _
        Round(swTransform.ArrayData(10), 4) & ", " & Round(swTransform.ArrayData(11), 4)
        
    swModel.ForceRebuild3 False
End Sub