Solidworks macro to rotate parts with respect to the assembly axis

1k Views Asked by At

I hope everybody's fine.

I am working with a solidworks macro that rotates an assembly component with respect to the assembly axis as its point of rotation. I have this code below:

Sub RotateX()

    Dim swApp               As SldWorks.SldWorks
    Dim swModel             As SldWorks.ModelDoc2
    Dim swSelMgr            As SldWorks.SelectionMgr
    Dim swMath              As SldWorks.MathUtility
    Dim swComp              As SldWorks.Component2
    Dim compTransform       As SldWorks.MathTransform
    Dim swVect              As MathVector
    
        Set swApp = Application.SldWorks
        Set swModel = swApp.ActiveDoc
        Set swSelMgr = swModel.SelectionManager
        Set swComp = swSelMgr.GetSelectedObjectsComponent2(1)
        Set swMath = swApp.GetMathUtility
'==============================
    On Error Resume Next
'==============================
        Set compTransform = swComp.Transform2
 
    Dim dirArr(2) As Double
        dirArr(0) = 1
        dirArr(1) = 0#
        dirArr(2) = 0#
    'Rotate about x axis {1,0,0}
    
        Set swVect = swMath.CreateVector((dirArr))
        Set swVect = swVect.MultiplyTransform(compTransform)
        'rotate the x axis into the component's reference frame
    
    Dim vData  As Variant
        dirArr(0) = 0#
        dirArr(1) = 0#
        dirArr(2) = 0#
        'rotate about origin
   
            vData = dirArr
    Dim swPoint As SldWorks.MathPoint
        Set swPoint = swMath.CreatePoint(vData)
        Set swPoint = swPoint.MultiplyTransform(compTransform)
    Dim swXform As SldWorks.MathTransform
        Set swXform = swMath.CreateTransformRotateAxis(swPoint, swVect, 90# * RadPerDeg)
            swComp.Transform2 = compTransform.Multiply(swXform)
            swModel.EditRebuild3
End Sub

But this code rotates using the part axis as point of rotation. I want to make a macro that rotates the part using assembly axis, but the part location will still doesnt move its position, just rotates. I hope someone could help give me answer on how to make this part rotate on assembly axis.

1

There are 1 best solutions below

2
On

by using:

Set swPoint = swPoint.MultiplyTransform(compTransform)

you're defining the point of rotation to be the center of the part. Remove it and it will work.

Note: You might also have the same kind of problem with:

Set swVect = swVect.MultiplyTransform(compTransform)

where the rotation axis will depend of the orientation of the part.