MACRO LIBRARY

Lesson 5.3 - Rotate using CreateTransformRotateAxis

Free public access. No account or subscription required.

Files

Lesson 5.3 - Rotate component using CreateTransformRotateAxis Associated model(s)

Code

'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("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
    Dim deg As Double
    deg = 45
    Set swNewTransform = swMathUtil.CreateTransformRotateAxis(swOriginPt, swAxisVect, _
        deg * 4 * Atn(1) / 180)
    
    'Multiply the original transform by the new transform
    Set swNewTransform = swTransform.Multiply(swNewTransform)
    
    'Apply new transform to component and update model
    swComp.Transform2 = swNewTransform
    swModel.ForceRebuild3 False
End Sub