Create Amplitude Plot and Set Mode Shape Example (VBA)
This example shows how to create an amplitude plot and set its mode shape
number.
'----------------------------------------------------------------------------
' 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 install_dir\Simulation\Examples\tutor1.sldprt.
' 4. Create a frequency study and apply material to the model.
'
' Postconditions:
' 1. Activates the Frequency1 study.
' 2. Meshes and analyzes the study.
' 3. Creates one or more default resultant amplitude plots, depending on your
' Tools > Simulation > Options > Default Options >
Frequency/Buckling Study
' Results settings.
' 4. Creates another resultant amplitude plot and sets its mode shape number
' to 4.
' 5. Gets the minimum and maximum resultant amplitudes for the last plot.
' 6. Click OK to close any error dialog boxes.
'
' NOTE: Because the model is used elsewhere, do not save changes.
' ---------------------------------------------------------------------------
Option Explicit
Sub main()
Dim SwApp As SldWorks.SldWorks
Dim COSMOSWORKS As
CosmosWorksLib.COSMOSWORKS
Dim CWAddinCallBack As CosmosWorksLib.CWAddinCallBack
Dim ActDoc As CosmosWorksLib.CWModelDoc
Dim StudyMngr As CosmosWorksLib.CWStudyManager
Dim Study As CosmosWorksLib.CWStudy
Dim CWMesh As CosmosWorksLib.CWMesh
Dim CWResult As CosmosWorksLib.CWResults
Dim CWCFf As CosmosWorksLib.CWPlot
Dim errCode As Long
Dim el As Double, tl As Double
Dim Disp As Variant
Const DispMax As Double = 1.424
Const DispMin As Double = 0.1
If SwApp Is Nothing Then Set SwApp =
Application.SldWorks
Set CWAddinCallBack = SwApp.GetAddInObject("CosmosWorks.CosmosWorks")
If CWAddinCallBack Is Nothing Then ErrorMsg SwApp, "No
CWAddinCallBack object", True
Set COSMOSWORKS = CWAddinCallBack.COSMOSWORKS
If COSMOSWORKS Is Nothing Then ErrorMsg SwApp, "No
CosmosWorks object", True
'Get active document
Set ActDoc = COSMOSWORKS.ActiveDoc()
If ActDoc Is Nothing Then ErrorMsg SwApp, "No active
document", True
'Get Frequency1 study
Set StudyMngr = ActDoc.StudyManager()
If StudyMngr Is Nothing Then ErrorMsg SwApp, "No study
manager object", True
StudyMngr.ActiveStudy = 1
Set Study = StudyMngr.GetStudy(1)
If Study Is Nothing Then ErrorMsg SwApp, "No study object",
True
'Mesh
Set CWMesh = Study.Mesh
If CWMesh Is Nothing Then ErrorMsg SwApp, "No mesh object",
False
CWMesh.Quality = 0
Call CWMesh.GetDefaultElementSizeAndTolerance(0, el,
tl)
errCode = Study.CreateMesh(0, el, tl)
If errCode <> 0 Then ErrorMsg SwApp, "Mesh failed", True
'Run analysis
errCode = Study.RunAnalysis
If errCode <> 0 Then ErrorMsg SwApp, "Analysis failed", True
'Get results
Set CWResult = Study.results
If CWResult Is Nothing Then ErrorMsg SwApp, "No result
object", False
'Create resultant amplitude plot
Set CWCFf = CWResult.CreatePlot(swsResultDisplacementOrAmplitude, swsFrequencyBucklingDisplacement_URES, swsUnitSI, False, errCode)
If errCode <> 0 Then ErrorMsg SwApp, "Failure to create
plot", True
'Set mode shape number of plot to 4
errCode = CWCFf.SetModeShape(4)
If errCode <> 0 Then ErrorMsg SwApp, "Plot is not
set for specified mode shape", True
errCode = CWCFf.ActivatePlot()
If errCode <> 0 Then ErrorMsg SwApp, "Plot is not activated",
True
'Get min/max resultant amplitudes from
plot
Disp = CWCFf.GetMinMaxResultValues(errCode)
If errCode <> 0 Then ErrorMsg SwApp, "No amplitude values", True
If (Disp(1) < 0.95 * DispMin) Or (Disp(1)
> 1.05 * DispMin) Then
ErrorMsg SwApp, " Minimum amplitude % error = " & (((Disp(1)
- DispMin) / DispMin) * 100), False
End If
If (Disp(3) < 0.95 * DispMax) Or (Disp(3)
> 1.05 * DispMax) Then
ErrorMsg SwApp, "Maximum amplitude % error = " & (((Disp(3) -
DispMax) / DispMax) * 100), False
End If
End Sub
Sub ErrorMsg(SwApp As SldWorks.SldWorks, Message As String,
EndTest As Boolean)
SwApp.SendMsgToUser2 Message, 0, 0
SwApp.RecordLine "'*** WARNING - General"
SwApp.RecordLine "'*** " & Message
SwApp.RecordLine ""
End Sub