MACRO LIBRARY

Rotate a component about an axis using ArrayData

Free public access. No account or subscription required.

Files

Rotate a component about an axis using ArrayData

Code

'This demonstrates rotating a component about an axis using IMathTransform::ArrayData
    
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
    
'Preconditions: transform_test.SLDASM is open
    
'Rotating with the array data is done in terms of unit vectors, hence the values are always _
    between 0 and 1
'Using IMathUtility::CreateTransformRotateAxis is recommended; _
    it is easier and gives better control
    
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 swTransform As SldWorks.MathTransform
Dim vData As Variant
Dim swConfig As SldWorks.Configuration
    
Sub main()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swAssy = swModel
    Set swMathUtil = swApp.GetMathUtility
    Set swComp = swAssy.GetComponentByName("transform_test_block-1")
    Set swTransform = swComp.Transform2
    vData = swTransform.ArrayData
    
    'If we want to rotate about z axis, we need to modify z axis components which are elements _
        6 through 8. The z axis unit vector would be (0, 0, 1) or (0, 0, -1). The easy way to _
        determine what values are needed is to use a CreateTransformRotateAxis macro that _
        successfully rotates the component and determine what values are found in that _
        final transform
    
    '30 deg
    vData(6) = -0.866
    vData(7) = -0.5
    vData(8) = 0
    
    '180 deg
    'vData(6) = 1
    'vData(7) = 0
    'vData(8) = 0
    
    Set swTransform = swMathUtil.CreateTransform(vData)
    swComp.Transform2 = swTransform
    swModel.ForceRebuild3 False
End Sub