Create Frequency Study with Solid Mesh Example (VB.NET)
This example shows how to create a frequency study with solid mesh.
'----------------------------------------------------------------------------
' 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 install_dir\api\redist\CLR2 folder and
' select SolidWorks.Interop.cosworks.dll).
' 3. Modify the path to solidworks materials.sldmat if needed.
' 4. Run the macro.
'
' Postconditions:
' 1. Assembly opened.
' 2. Default frequency study results plots are specified.
' 3. Frequency study with solid mesh created.
' 4. Same material applied to all bodies.
' 5. Mesh created with default global size and tolerance parameters.
' 6. Solver set to automatic.
' 7. Number of frequencies set.
' 8. Analysis run.
' 9. Results returned: frequencies and mass participation factors.
' 10. Inspect the frequency study results plots.
' -----------------------------------------------------------------
Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst
Imports SolidWorks.Interop.cosworks
Imports System
Partial Class SolidWorksMacro
Public
Sub main()
Dim
COSMOSWORKS As Object
Dim
COSMOSObject As CwAddincallback
Dim
ActDoc As CWModelDoc
Dim
StudyMngr As CWStudyManager
Dim
Study As CWStudy
Dim
SolidMgr As CWSolidManager
Dim
SolidComponent As CWSolidComponent
Dim
SolidBody As CWSolidBody
Dim
CwMesh As CwMesh
Dim
CWResult As CWResults
Dim
FrequencyOptions As CWFrequencyStudyOptions
Dim
longstatus As Long, longwarnings As Long
Dim
errCode As Long, errorCode As Long
Dim
CompCount As Integer
Dim
BodyCount As Integer
Dim
j As Integer
Dim
i As Integer
Dim
bApp As Boolean
Dim
Freq As Object
Dim
MassPart As Object
Dim
el As Double, tl As Double
'
Get SOLIDWORKS Simulation object
COSMOSObject
= swApp.GetAddInObject("SldWorks.Simulation")
If
COSMOSObject Is Nothing Then ErrorMsg("COSMOSObject object not found",
True)
COSMOSWORKS
= COSMOSObject.COSMOSWORKS
If
COSMOSWORKS Is Nothing Then ErrorMsg("COSMOSWORKS object not found",
True)
'
Get active document
swApp.OpenDoc6("C:\Program Files\SOLIDWORKS
Corp\SOLIDWORKS\Simulation\Examples\shaft.sldasm", swDocumentTypes_e.swDocASSEMBLY,
swOpenDocOptions_e.swOpenDocOptions_Silent, "", longstatus,
longwarnings)
ActDoc
= COSMOSWORKS.ActiveDoc()
If
ActDoc Is Nothing Then ErrorMsg("No active document.", True)
' Add default frequency study results plots of resultant amplitudes
for all mode shapes
errCode = ActDoc.AddDefaultFrequencyOrBucklingStudyPlot(True,
0, True,
swsFrequencyBucklingResultDisplacementComponentTypes_e.swsFrequencyBucklingDisplacement_URES)
'
Create new frequency study
StudyMngr
= ActDoc.StudyManager()
If
StudyMngr Is Nothing Then ErrorMsg("No StudyMngr object.", True)
Study
= StudyMngr.CreateNewStudy3("Frequency",
swsAnalysisStudyType_e.swsAnalysisStudyTypeFrequency, swsMeshType_e.swsMeshTypeSolid,
errCode)
If
Study Is Nothing Then ErrorMsg("Study not created.", True)
'
Get number of solid components
SolidMgr
= Study.SolidManager
If
SolidMgr Is Nothing Then ErrorMsg("SolidManager object not created.",
True)
CompCount
= SolidMgr.ComponentCount
'
Apply SOLIDWORKS material to rest of components
SolidBody
= Nothing
SolidComponent
= Nothing
For
j = 0 To (CompCount - 1)
SolidComponent
= SolidMgr.GetComponentAt(j, errorCode)
BodyCount
= SolidComponent.SolidBodyCount
For
i = 0 To (BodyCount - 1)
SolidBody
= SolidComponent.GetSolidBodyAt(i,
errCode)
If
errCode <> 0 Then ErrorMsg("No solid body", True)
bApp
= SolidBody.SetLibraryMaterial("c:\Program
Files\SOLIDWORKS Corp\SOLIDWORKS\lang\english\sldmaterials\solidworks
materials.sldmat", "Ductile Iron (SN)")
If
bApp = False Then ErrorMsg("No material applied.", True)
SolidBody
= Nothing
Next
i
Next
j
'
Set meshing
CwMesh
= Study.Mesh
If
CwMesh Is Nothing Then ErrorMsg("No mesh object.", False)
CwMesh.Quality = 1
Call
CwMesh.GetDefaultElementSizeAndTolerance(0,
el, tl)
errCode
= Study.CreateMesh(0, el, tl)
If
errCode <> 0 Then ErrorMsg("Mesh failed.", True)
'
Set solver type as automatic
FrequencyOptions
= Study.FrequencyStudyOptions
If
FrequencyOptions Is Nothing Then ErrorMsg("No FrequencyOptions object.",
False)
FrequencyOptions.SolverType = 1
FrequencyOptions.NoOfFrequencies = 8
'
Run analysis
errCode
= Study.RunAnalysis
If
errCode <> 0 Then ErrorMsg("Analysis failed", True)
'
Get results: frequencies and mass participation factors
CWResult
= Study.Results
If
CWResult Is Nothing Then ErrorMsg("No result object.", False)
Freq
= CWResult.GetResonantFrequencies(errCode)
MassPart
= CWResult.GetMassParticipation(errCode)
End
Sub
'
Error routine
Private
Sub ErrorMsg(ByVal Message As String, ByVal EndTest As Boolean)
swApp.SendMsgToUser2(Message, 0, 0)
swApp.RecordLine("'*** WARNING - General")
swApp.RecordLine("'*** " & Message)
swApp.RecordLine("")
End
Sub
'''
<summary>
'''
The SldWorks swApp variable is pre-assigned for you.
'''
</summary>
Public
swApp As SldWorks
End Class