Get Stresses for Selected Beams Example (VBA)
This example shows how to get the worst-case stress values for the selected
beams.
'----------------------------------------------------------------------------
' 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 beams.
' 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 stresses.
' 7. Run the macro.
'
' Postconditions: The selected beams' stresses 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
beamStress 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
arrBeamStesses 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' stresses
Set
BeamMgr = Study.BeamManager
beamStress
= swsBeamStressWorstCase
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
arrBeamStesses
= Results.GetBeamStressForEntities(beamStress,
nbrSteps, (varArray), unit, errCode)
For
i = 0 To UBound(arrBeamStesses)
Debug.Print
(arrBeamStesses(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