MACRO LIBRARY

Rotate about axis using CreateTransformRotateAxis

Free public access. No account or subscription required.

Files

Rotate a component about an axis using CreateTransformRotateAxis

Code

'This demonstrates rotating a component about an axis using _
    IMathUtility::CreateTransformRotateAxis
    
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
    
'Precondition: "transform_test.SLDASM" is open
'Result: component is rotated a specified angle about a specified axis at a specified pt
    
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swAssy As SldWorks.AssemblyDoc
Dim swComp As SldWorks.Component2
    
Dim swMathUtil As SldWorks.MathUtility
Dim swAxisVect As SldWorks.MathVector
Dim swOriginPt As SldWorks.MathPoint
Dim swTransform As SldWorks.MathTransform
Dim swNewTransform As SldWorks.MathTransform
    
Dim vPt As Variant
Dim dblPt(2) As Double
    
Sub main()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swAssy = swModel
    Set swMathUtil = swApp.GetMathUtility
    
    'Current original component transform
    Set swComp = swAssy.GetComponentByName("transform_test_block-1")
    Set swTransform = swComp.Transform2
    
    'Create center point of axis
    dblPt(0) = 0
    dblPt(1) = 0
    dblPt(2) = 0
    vPt = dblPt
    Set swOriginPt = swMathUtil.CreatePoint(vPt)
    
    'Create axis vector
    dblPt(0) = 0
    dblPt(1) = 0
    dblPt(2) = 1
    vPt = dblPt
    Set swAxisVect = swMathUtil.CreateVector(vPt)
    
    'Create new transform based on specified angle of rotation at specified pt about specified axis
    Set swNewTransform = swMathUtil.CreateTransformRotateAxis(swOriginPt, swAxisVect, _
        180 * 4 * Atn(1) / 180)
    
    'Multiply the original transform by the new transform
    Set swNewTransform = swTransform.Multiply(swNewTransform)
    
    'Following code simply displays the new array data values; not essential to this macro
    Dim i As Integer
    Dim vData As Variant
    vData = swNewTransform.ArrayData
    For i = 0 To 11
        Debug.Print Round(vData(i), 4)
    Next i
    
    'Apply new transform to component
    swComp.Transform2 = swNewTransform
    
    'Update model
    swModel.ForceRebuild3 False
End Sub