Hide Table of Contents

Get Forces for Selected Beams Example (VBA)

This example shows how to get the force load values for structural members.

'----------------------------------------------------------------------------

' Preconditions:

' 1. Add the SolidWorks Simulation as an add-in

'    (in SolidWorks, click Tools > Add-ins > SolidWorks Simulation).

' 2. Add the SolidWorks Simulation type library as a reference

'    (in the IDE, click Tools > References > SolidWorks

'    Simulation version type library).

' 3. Open the Immediate window.

' 4. Open a SolidWorks model that has structural members and a

'    Simulation static study with force loads on the

'    structural members.

' 5. Select the Simulation study tab.

' 6. Click the Run button on the Simulation CommandManager to

'    refresh the study results.

' 6. In the Simulation Study tree, select the beams for which

'    you want to know their force loads.

' 7. Run the macro.

'

' Postconditions: The selected beams' forces are printed to the

'    the Immediate window.

'-------------------------------

Option Explicit

 

Sub main()

 

   Dim SwApp As SldWorks.SldWorks

   Dim swModel As ModelDoc2

   Dim swSelMgr As SldWorks.SelectionMgr

   Dim COSMOSWORKS As CosmosWorksLib.COSMOSWORKS

   Dim COSMOSObject As CosmosWorksLib.CwAddincallback

   Dim ActDoc As CosmosWorksLib.CWModelDoc

   Dim StudyMngr As CosmosWorksLib.CWStudyManager

   Dim Study As CosmosWorksLib.CWStudy

   Dim BeamMgr As CosmosWorksLib.CWBeamManager

   Dim BeamBody As CosmosWorksLib.CWBeamBody

   Dim Results As CosmosWorksLib.CWResults

   Dim actStudy As Long

   Dim beamForce As Long

   Dim nbrSteps As Long

   Dim unit As Long

   Dim errCode As Long

   Dim selBeams() As Object

   Dim nbrSelectedBeams As Long

   Dim varArray As Variant

   Dim arrBeamForces As Variant

   Dim i As Long

   Dim k As Long

 

    ' Connect to SolidWorks

    If SwApp Is Nothing Then Set SwApp = Application.SldWorks

    Set swModel = SwApp.ActiveDoc

    Set swSelMgr = swModel.SelectionManager

 

    ' Get the SolidWorks Simulation object

    Set COSMOSObject = SwApp.GetAddInObject("SldWorks.Simulation")

    If COSMOSObject Is Nothing Then ErrorMsg SwApp, "COSMOSObject object not found.", True

    Set COSMOSWORKS = COSMOSObject.COSMOSWORKS

    If COSMOSWORKS Is Nothing Then ErrorMsg SwApp, "COSMOSWORKS object not found.", True

 

    ' Get the active document

    Set ActDoc = COSMOSWORKS.ActiveDoc()

    If ActDoc Is Nothing Then ErrorMsg SwApp, "No active document.", True

    

    ' Get the active study

    Set StudyMngr = ActDoc.StudyManager()

    If StudyMngr Is Nothing Then ErrorMsg SwApp, "StudyMngr object not there.", True

    actStudy = StudyMngr.ActiveStudy

    Set Study = StudyMngr.GetStudy(actStudy)

    If Study Is Nothing Then ErrorMsg SwApp, "Study not created.", True

 

    ' Get the results

    Set Results = Study.Results

 

    ' Get the selected beams' forces

    Set BeamMgr = Study.BeamManager

    beamForce = swsBeamForceMomentDirection1

    nbrSteps = 1

    unit = swsUnitSI

    

    nbrSelectedBeams = swSelMgr.GetSelectedObjectCount2(-1)

    ReDim selBeams(nbrSelectedBeams)

    For k = 0 To (nbrSelectedBeams - 1)

        Set selBeams(k) = swSelMgr.GetSelectedObject6(k + 1, -1)

    Next k

   

    ReDim Preserve selBeams(nbrSelectedBeams - 1)

    varArray = selBeams

    arrBeamForces = Results.GetBeamForcesForEntities(beamForce, nbrSteps, (varArray), unit, errCode)

    For i = 0 To UBound(arrBeamForces)

         Debug.Print (arrBeamForces(i))

    Next

End Sub

 

'Error function

Function ErrorMsg(SwApp As Object, Message As String, EndTest As Boolean)

    SwApp.SendMsgToUser2 Message, 0, 0

    SwApp.RecordLine "'*** WARNING - General"

    SwApp.RecordLine "'*** " & Message

    SwApp.RecordLine ""

    If EndTest Then

    End If

End Function



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:   Get Forces for Selected Beams (VBA)
*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) 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.