Get Forces for Selected Beams Example (VB.NET)
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 primary interop assembly
as
' a
reference (in the IDE's Project Explorer, right-click
' the
project name, select Add Reference,
click the Browse tab,
' navigate
to the <SolidWorks_install_dir>\api\redist folder and
' select
SolidWorks.Interop.cosworks.dll).
' 3. Open the Immediate window.
' 4. Open a SolidWorks model that has structural members
and a
' Simulation
static study with force loads
' applied
to the structural members.
' 5. Select the Simulation study tab.
' 6. Click the Run button on the Simulation CommandManager
to
' refresh
the study results.
' 7. In the Simulation Study tree, select the beams for
which
' you
want to know their force loads.
' 8. Run the macro.
'
' Postconditions: The selected beams' forces are printed
to the
' the
Immediate window.
'-------------------------------
Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst
Imports SolidWorks.Interop.cosworks
Imports System
Imports System.Diagnostics
Partial Class SolidWorksMacro
Sub
main()
Dim
swModel As ModelDoc2
Dim
swSelMgr As SelectionMgr
Dim
COSMOSWORKS As COSMOSWORKS
Dim
COSMOSObject As CwAddincallback
Dim
ActDoc As CWModelDoc
Dim
StudyMngr As CWStudyManager
Dim
Study As CWStudy
Dim
BeamMgr As CWBeamManager
Dim
Results As 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 Object
Dim
arrBeamForces As Object
Dim
i As Long
Dim
k As Long
'
Connect to SolidWorks
swModel
= swApp.ActiveDoc
swSelMgr
= swModel.SelectionManager
'
Get the SolidWorks Simulation object
COSMOSObject
= swApp.GetAddInObject("SldWorks.Simulation")
If
COSMOSObject Is Nothing Then ErrorMsg(swApp, "COSMOSObject object
not found.", True)
COSMOSWORKS
= COSMOSObject.COSMOSWORKS
If
COSMOSWORKS Is Nothing Then ErrorMsg(swApp, "COSMOSWORKS object not
found.", True)
'
Get the active document
ActDoc
= COSMOSWORKS.ActiveDoc()
If
ActDoc Is Nothing Then ErrorMsg(swApp, "No active document.",
True)
'
Get the active study
StudyMngr
= ActDoc.StudyManager()
If
StudyMngr Is Nothing Then ErrorMsg(swApp, "StudyMngr object not there.",
True)
actStudy
= StudyMngr.ActiveStudy
Study
= StudyMngr.GetStudy(actStudy)
If
Study Is Nothing Then ErrorMsg(swApp, "Study not created.",
True)
'
Get the results
Results
= Study.Results
'
Get the selected beams' forces
BeamMgr
= Study.BeamManager
beamForce
= swsBeamForceType_e.swsBeamForceMomentDirection1
nbrSteps
= 1
unit
= swsUnit_e.swsUnitSI
nbrSelectedBeams
= swSelMgr.GetSelectedObjectCount2(-1)
ReDim
selBeams(nbrSelectedBeams)
For
k = 0 To (nbrSelectedBeams - 1)
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
routine
Private
Sub ErrorMsg(ByVal SwApp As Object, ByVal Message As String, ByVal EndTest
As Boolean)
SwApp.SendMsgToUser2(Message, 0, 0)
SwApp.RecordLine("'*** WARNING - General")
SwApp.RecordLine("'*** " & Message)
SwApp.RecordLine("")
If
EndTest Then
End
If
End
Sub
Public
swApp As SldWorks
End Class