Hide Table of Contents

Rotate Assembly Component on Axis Using IDragOperator::Drag Example (VBA)

This example shows how to rotate an assembly component about an assembly axis using IDragOperator::Drag.




' Problem:

'       This code shows how to use the MathUtility object

'       to directly create a transformation matrix (object)

'       that represents rotation about a point and an axis,

'       without having to know details of the OpenGL transformations.


' Preconditions:

'       (1) Assembly is open and fully resolved.

'       (2) Assembly component is selected.


' Postconditions: Selected component is rotated 90° about assembly X axis.




Option Explicit


Const PI                As Double = 3.14159

Const RadPerDeg         As Double = PI / 180#

'   DragOperator::TransformType

'       Translation     0

'       Transform is translation only


'       Axial rotation  1

'       Transform is rotation only


'       General         2

'       Transform can be translation or rotation or both

'   DragOperator::DragMode

'       Minimum Move    0

'       Move smallest number of geometries


'       Maximum Move    1

'       Move geometries rigidly if possible


'       Relaxation      2

'       Solve geometries using relaxation

Sub main()


    Dim swApp                   As SldWorks.SldWorks

    Dim swModel                 As SldWorks.ModelDoc2

    Dim swAssy                  As SldWorks.AssemblyDoc

    Dim swDragOp                As SldWorks.DragOperator

    Dim swSelMgr                As SldWorks.SelectionMgr

    Dim swComp                  As SldWorks.Component2

    Dim swXform                 As SldWorks.MathTransform

    Dim swMathUtil              As SldWorks.MathUtility

    Dim swOriginPt              As SldWorks.MathPoint

    Dim swX_Axis                As SldWorks.MathVector

    Dim nPts(2)                 As Double

    Dim vData                   As Variant

    Dim nNow                    As Single

    Dim i                       As Long

    Dim bRet                    As Boolean



    Set swApp = CreateObject("SldWorks.Application")

    Set swModel = swApp.ActiveDoc

    Set swAssy = swModel

    Set swDragOp = swAssy.GetDragOperator

    Set swSelMgr = swModel.SelectionManager

    Set swComp = swSelMgr.GetSelectedObjectsComponent2(1)

    Set swMathUtil = swApp.GetMathUtility


    nPts(0) = 0#

    nPts(1) = 0#

    nPts(2) = 0#

    vData = nPts

    Set swOriginPt = swMathUtil.CreatePoint(vData)


    nPts(0) = 1#

    nPts(1) = 0#

    nPts(2) = 0#

    vData = nPts

    Set swX_Axis = swMathUtil.CreateVector(vData)

    ' This is an incremental rotation,

    ' so angle is always the same

    Set swXform = swMathUtil.CreateTransformRotateAxis( _

                        swOriginPt, swX_Axis, 1# * RadPerDeg)



    bRet = swDragOp.AddComponent(swComp, False)

    Debug.Assert bRet


    swDragOp.CollisionDetectionEnabled = False

    swDragOp.DynamicClearanceEnabled = False


    ' Axial rotation

    swDragOp.TransformType = 1


    ' Solve by relaxation

    swDragOp.DragMode = 2


    bRet = swDragOp.BeginDrag

    Debug.Assert bRet

    For i = 0 To 90

        ' Returns false if drag fails, for example, because of a collision

        bRet = swDragOp.Drag(swXform)


        ' Wait for 0.1 secs

        nNow = Timer

        While Timer < nNow + 0.1

            ' Process event loop



    Next i

    bRet = swDragOp.EndDrag

    Debug.Assert bRet

End Sub


Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

Subject:   Feedback on Help Topics
Page:   Rotate Assembly Component on Axis Using IDragOperator::Drag Example (VBA)
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:


We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again

Web Help Content Version: API Help (English only) 2012 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.