Create Plots for a Static Study Example (VB.NET)
This example shows how to create plots of displacement, stress, and strain
results in a static study.
'----------------------------------------------------------------------------
' 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, click Add Reference, click the Browse tab,
' navigate to install_dir\api\redist\CLR2,
' click SolidWorks.Interop.cosworks.dll, and click OK).
' 3. Open install_dir\Simulation\Examples\tutor1.sldprt.
'
' Postconditions:
' 1. Deletes all the default static study
plots from the model document.
' 2. Activates the Ready static study.
' 3. Meshes and runs the study.
' 4. Creates displacement, stress, and strain plots.
' 5. Gets the minimum and maximum values for the result plots.
' 6. Click OK to close any error dialog boxes.
'
' NOTE: Because the model is used elsewhere, do not save changes.
'----------------------------------------------------------------------------
Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst
Imports System.Runtime.InteropServices
Imports System
Imports SolidWorks.Interop.cosworks
Partial Class SolidWorksMacro
Sub main()
Dim COSMOSWORKS As COSMOSWORKS
Dim CWAddinCallBack As CwAddincallback
Dim ActDoc As CWModelDoc
Dim StudyMngr As CWStudyManager
Dim Study As CWStudy
Dim CWMesh As CWMesh
Dim CWResult As CWResults
Dim CWCFf As CWPlot
Dim errCode As Integer
Dim el As Double, tl As Double
Dim Disp As Object, Stress As Object, Strn As Object
Const URESMax As Double = 1004928 'nm
Const URESMin As Double = 0.0# 'nm
Const VONMax As Double = 284 'MPa
Const VONMin As Double = 0.797 'MPa
Const MaxStrn As Double = 0.00352
Const MinStrn As Double = 0.00000294
CWAddinCallBack = SwApp.GetAddInObject("CosmosWorks.CosmosWorks")
If CWAddinCallBack Is Nothing Then ErrorMsg(SwApp, "No CWAddinCallBack object", True)
COSMOSWORKS = CWAddinCallBack.COSMOSWORKS
If COSMOSWORKS Is Nothing Then ErrorMsg(SwApp, "No CosmosWorks object", True)
'Get active document
ActDoc = COSMOSWORKS.ActiveDoc()
If ActDoc Is Nothing Then ErrorMsg(SwApp, "No active document", True)
'Delete all default static study plots from this model
ActDoc.DeleteAllDefaultStaticStudyPlots()
'Get Ready study
StudyMngr = ActDoc.StudyManager()
If StudyMngr Is Nothing Then ErrorMsg(SwApp, "No
study manager object", True)
StudyMngr.ActiveStudy = 0
Study = StudyMngr.GetStudy(0)
If Study Is Nothing Then ErrorMsg(SwApp, "No
study object", True)
'Mesh
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
CWResult = Study.Results
If CWResult Is Nothing Then ErrorMsg(SwApp, "No result object", False)
'Create displacement plot
CWCFf = CWResult.CreatePlot(swsPlotResultTypes_e.swsResultDisplacementOrAmplitude, swsStaticResultDisplacementComponentTypes_e.swsStaticDisplacement_URES, swsUnit_e.swsUnitSI, False, errCode)
If CWCFf Is Nothing Then ErrorMsg(SwApp, "Failed to create plot", False)
'Activate plot
errCode = CWCFf.ActivatePlot()
If errCode <> 0 Then ErrorMsg(SwApp, "Plot is not activated", True)
'Get min/max resultant displacements from plot
Disp = CWCFf.GetMinMaxResultValues(errCode)
If errCode <> 0 Then ErrorMsg(SwApp, "No displacement result", True)
If (Disp(1) < 0.95 * URESMin) Or (Disp(1) > 1.05 * URESMin) Then
ErrorMsg(SwApp, "URES minimum % error = " & (((Disp(1) - URESMin) / URESMin) * 100), False)
End If
If (Disp(3) < 0.95 * URESMax) Or (Disp(3) > 1.05 * URESMax) Then
ErrorMsg(SwApp, "URES maximum % error = " & (((Disp(3) - URESMax) / URESMax) * 100), False)
End If
'Create stress plot
CWCFf = CWResult.CreatePlot(swsPlotResultTypes_e.swsResultStress, swsStaticResultNodalStressComponentTypes_e.swsStaticNodalStress_VON, swsUnit_e.swsUnitSI, False, errCode)
If errCode <> 0 Then ErrorMsg(SwApp, "Failed to create plot", True)
'Activate plot
errCode = CWCFf.ActivatePlot()
If errCode <> 0 Then ErrorMsg(SwApp, "Plot is not activated", True)
'Get min/max von Mises stresses from plot
Stress = CWCFf.GetMinMaxResultValues(errCode)
If errCode <> 0 Then ErrorMsg(SwApp, "No stress result", True)
If (Stress(1) < 0.95 * VONMin) Or (Stress(1) > 1.05 * VONMin) Then
ErrorMsg(SwApp, " Minimum von Mises stress % error = " & (((Stress(1) - VONMin) / VONMin) * 100), False)
End If
If (Stress(3) < 0.95 * VONMax) Or (Stress(3) > 1.05 * VONMax) Then
ErrorMsg(SwApp, "Maximum von Mises stress % error = " & (((Stress(3) - VONMax) / VONMax) * 100), False)
End If
'Create strain plot
CWCFf = CWResult.CreatePlot(swsPlotResultTypes_e.swsResultStrain, swsStaticResultElementalStrainComponentTypes_e.swsStaticElementalStrain_ESTRN, swsUnit_e.swsUnitEnglish, True, errCode)
If errCode <> 0 Then ErrorMsg(SwApp, "Failed to create plot", True)
'Activate plot
errCode = CWCFf.ActivatePlot()
If errCode <> 0 Then ErrorMsg(SwApp, "Plot is not activated", True)
'Get min/max strains from plot
Strn = CWCFf.GetMinMaxResultValues(errCode)
If errCode <> 0 Then ErrorMsg(SwApp, "No stress result", True)
If (Strn(1) < 0.95 * MinStrn) Or (Strn(1) > 1.05 * MinStrn) Then
ErrorMsg(SwApp, "Minimum strain % error = " & (((Strn(1) - MinStrn) / MinStrn) * 100), False)
End If
If (Strn(3) < 0.95 * MaxStrn) Or (Strn(3) > 1.05 * MaxStrn) Then
ErrorMsg(SwApp, "Maximum strain % error = " & (((Strn(3) - MaxStrn) / MaxStrn) * 100), False)
End If
End Sub
Sub ErrorMsg(ByVal SwApp As SldWorks, ByVal Message As String, ByVal EndTest As Boolean)
SwApp.SendMsgToUser2(Message, 0, 0)
SwApp.RecordLine("'*** WARNING - General")
SwApp.RecordLine("'*** " & Message)
SwApp.RecordLine("")
End Sub
Public swApp As SldWorks
End Class