Hide Table of Contents

Rotate Assembly Component on Axis Using IDragOperator::Drag Example (VB.NET)

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

NOTE: The code shows how to use a 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: Verify that the specified assembly document exists.
'
' Postconditions:
' 1. Opens the specified assembly document, which is fully resolved.
' 2. Selects a floating component.
' 3. Watch the selected component in the graphics area rotate 90° 
'    about the assembly's x axis.
'
NOTE: Because this assembly is used elsewhere, do not save any changes.
'------------------------------------------------------------------
Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst
Imports System.Runtime.InteropServices
Imports System
Imports System.Diagnostics
Imports System.Windows.Forms.Application
 
 
Partial Class SolidWorksMacro
 
    Const PI As Double = 3.14159
    Const RadPerDeg As Double = PI / 180.0#
 
    Public Sub main()
 
        Dim swModel As ModelDoc2
        Dim swModelDocExt As ModelDocExtension
        Dim swAssy As AssemblyDoc
        Dim swDragOp As DragOperator
        Dim swSelMgr As SelectionMgr
        Dim swComp As Component2
        Dim swXform As MathTransform
        Dim swMathUtil As MathUtility
        Dim swOriginPt As MathPoint
        Dim swX_Axis As MathVector
        Dim fileName As String
        Dim errors As Integer
        Dim warnings As Integer
        Dim status As Boolean
        Dim nPts(2) As Double
        Dim vData As Object
        Dim nNow As Single
        Dim i As Integer
        Dim bRet As Boolean
 
        fileName = "C:\Program Files\SolidWorks Corp\SolidWorks\samples\tutorial\api\assem2.sldasm"
        swModel = swApp.OpenDoc6(fileName, swDocumentTypes_e.swDocASSEMBLY, swOpenDocOptions_e.swOpenDocOptions_Silent, "", errors, warnings)
        swModelDocExt = swModel.Extension
        status = swModelDocExt.SelectByID2("Part3^Assem2-1@assem2""COMPONENT", 0, 0, 0, False, 0, Nothing, 0)
        swAssy = swModel

        swDragOp = swAssy.GetDragOperator
        swSelMgr = swModel.SelectionManager
        swComp = swSelMgr.GetSelectedObjectsComponent2(1)
        swMathUtil = swApp.GetMathUtility

        nPts(0) = 0.0#
        nPts(1) = 0.0#
        nPts(2) = 0.0#
        vData = nPts
        swOriginPt = swMathUtil.CreatePoint(vData)

        nPts(0) = 1.0#
        nPts(1) = 0.0#
        nPts(2) = 0.0#
        vData = nPts
        swX_Axis = swMathUtil.CreateVector(vData)

        ' This is an incremental rotation,
        ' so angle is always the same
        swXform = swMathUtil.CreateTransformRotateAxis(swOriginPt, swX_Axis, 1.0# * RadPerDeg)

        bRet = swDragOp.AddComponent(swComp, False)

        swDragOp.CollisionDetectionEnabled = False
        swDragOp.DynamicClearanceEnabled = False

        ' Axial rotation
        swDragOp.TransformType = 1

        ' Solve by relaxation
        swDragOp.DragMode = 2

        bRet = swDragOp.BeginDrag

        For i = 0 To 90
            ' Returns false if drag fails
            bRet = swDragOp.Drag(swXform)
            ' Wait for 0.1 secs
            nNow = Timer
            While Timer < nNow + 0.1
                ' Process event loop
                DoEvents()
            End While
        Next i

        bRet = swDragOp.EndDrag 
 
    End Sub
 
    ''' <summary>
    ''' The SldWorks swApp variable is pre-assigned for you.
    ''' </summary>
    Public swApp As SldWorks
 
 
End Class


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

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Rotate Assembly Component on Axis Using IDragOperator::Drag Example (VB.NET)
*Comment:  
*   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:



x

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
x

Web Help Content Version: API Help (English only) 2016 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.