Analyze Part Example (VB.NET)
This example shows how to create a static analysis of a part.
NOTE: To get persistent reference
identifiers (PIDs) for model selections, you can use
pidcollector.exe
or IModelDocExtension::GetPersistReference3.
'----------------------------------------------------------------------------
' 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 as needed.
' 4. Open the Immediate window.
' 5. Run the macro.
'
' Postconditions:
'
1. Adds a default static study results plot.
' 2. Creates a study called Static-solid.
' 3. Applies material to the model.
' 4. Applies restraints and pressure to the selected faces.
' 5. Creates a mesh.
' 6. Runs the analysis.
' 7. Prints strain and stress results to the Immediate window.
' 8. Hides fixture and force symbols. Examine the graphics area
' to verify.
' 9. Press F5 to continue.
' 10. Shows fixture and force symbols. Examine the graphics area
' to verify.
' 11. Inspect the static study results plot.
'
' NOTE: Because the part document is used elsewhere, do not
' save any changes when closing it.
'----------------------------------------------------------------------------
Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst
Imports SolidWorks.Interop.cosworks
Imports System
Imports System.Diagnostics
Imports System.Runtime.InteropServices
Partial Class SolidWorksMacro
Public Sub main()
Dim COSMOSWORKS As Object
Dim CWObject 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 stress As Object
Dim Part As ModelDoc2
Dim LBCMgr As CWLoadsAndRestraintsManager
Dim CWPressure As CWPressure
Dim CWRes1 As CWRestraint
Dim pointer1 As Object
Dim pointer2 As Object
Dim pointer3 As Object
Dim CWMat As CWMaterial
Dim var1 As Object = Nothing
Dim var2 As Object = Nothing
Dim var3 As Object = Nothing
Dim bApp As Boolean
Dim selection1 As String
Dim selection2 As String
Dim selection3 As String
Dim longstatus As Integer
Dim longwarnings As Integer
Dim errCode As Integer
Dim NSource As Integer
Dim el As Double
Dim tl As Double
' Open SolidWorks part document
Part = swApp.OpenDoc6("C:\Program Files\SolidWorks Corp\SolidWorks\Simulation\Examples\Tutor1.SLDPRT ", swDocumentTypes_e.swDocPART, 1, "", longstatus, longwarnings)
If Part Is Nothing Then ErrorMsg(swApp, "Failed to open C:\Program Files\SolidWorks Corp\SolidWorks\Simulation\Examples\Tutor1.SLDPRT.", True)
'Get SolidWorks Simulation object
CWObject = swApp.GetAddInObject("SldWorks.Simulation")
If CWObject Is Nothing Then ErrorMsg(swApp, "CWObject object not found.", True)
COSMOSWORKS = CWObject.CosmosWorks
If COSMOSWORKS Is Nothing Then ErrorMsg(swApp, "CWObject not found.", True)
' Get active document
ActDoc = COSMOSWORKS.ActiveDoc()
If ActDoc Is Nothing Then ErrorMsg(swApp, "No active document.", True)
' Add default
static study results plot
errCode =
ActDoc.AddDefaultStaticStudyPlot(swsDefaultStaticResultTypes_e.swsStaticResultElementalStrain,
swsStaticResultElementalStrainComponentTypes_e.swsStaticElementalStrain_ENERGY)
' Create new static study
StudyMngr = ActDoc.StudyManager()
If StudyMngr Is Nothing Then ErrorMsg(swApp, "StudyMngr object not there.", True)
Study = StudyMngr.CreateNewStudy3("Static_solid", swsAnalysisStudyType_e.swsAnalysisStudyTypeStatic, swsMeshType_e.swsMeshTypeSolid, errCode)
If Study Is Nothing Then ErrorMsg(swApp, "Study not created.", True)
' Set material from the SolidWorks material library
SolidMgr = Study.SolidManager
If SolidMgr Is Nothing Then ErrorMsg(swApp, "SolidMgr object not there.", True)
SolidComponent = SolidMgr.GetComponentAt(0, errCode)
If errCode <> 0 Then ErrorMsg(swApp, "No solid component.", True)
SolidBody = SolidComponent.GetSolidBodyAt(0, errCode)
If errCode <> 0 Then ErrorMsg(swApp, "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(swApp, "No material applied.", True)
CWMat = SolidBody.GetDefaultMaterial
NSource = CWMat.Source
' Get the PIDs of the faces
' First two selections are the faces for restraints
' Third selection is the face where pressure is applied
selection1 = "216,14,0,0,3,0,0,0,255,254,255,0,0,0,0,0,255,255,1,0,11,0,109,111,70,97,99,101,82,101,102,95,99,1,0,0,0,0,0,0,0,5,0,0,0,0,3,0,0,0,0,0,0,125,195,148,37,173,73,178,84,125,195,148,37,173,73,178,84,0,0,255,255,1,0,21,0,109,111,76,80,97,116,116,101,114,110,83,117,114,102,73,100,82,101,112,95,99,0,0,255,255,1,0,6,0,109,111,70,82,95,99,255,255,1,0,13,0,109,111,69,120,116,79,98,106,101,99,116,95,99,255,255,1,0,14,0,109,111,79,98,106,70,105,108,101,68,101,102,95,99,255,255,1,0,17,0,109,111,67,83,116,114,105,110,103,72,97,110,100,108,101,95,99,255,254,255,6,84,0,117,0,116,0,111,0,114,0,49,0,123,228,186,50,11,128,255,254,255,62,67,0,58,0,92,0,80,0,114,0,111,0,103,0,114,0,97,0,109,0,32,0,70,0,105,0,108,0,101,0,115,0,92,0,83,0,111,0,108,0,105,0,100,0,87,0,111,0,114,0,107,0,115,0,92,0,67,0,79,0,83,0,77,0,79,0,83,0,87,0,111,0,114,0,107,0,115,0,92,0,69,0,120,0,97,0,109,0,112,0,108,0,101,0,115,0,92,0,84,0,117,0,116,0,111,0,114,0,49,0,46,0,83,0,76,0,68,0,80,0,82,0,84,0,97,23,28,65,0,0,0,0"
selection1 = selection1 & ",2,0,1,0,0,0,0,0,0,0,1,0,0,0,255,254,255,7,68,0,101,0,102,0,97,0,117,0,108,0,116,0,0,0,0,0,0,0,0,0,0,0,0,0,17,0,0,0,156,231,186,50,255,255,1,0,23,0,109,111,70,114,111,109,83,107,116,69,110,116,83,117,114,102,73,100,82,101,112,95,99,0,0,5,128,8,0,14,0,0,0,88,231,186,50,3,0,0,0,1,0,0,0,0,0,0,0,255,255,1,0,20,0,109,111,69,110,100,70,97,99,101,83,117,114,102,73,100,82,101,112,95,99,0,0,5,128,8,0,6,0,0,0,211,228,186,50,1,0,0,0,255,255,255,255,0,0,17,128,0,0,5,128,8,0,6,0,0,0,211,228,186,50,0,0,0,0,255,255,255,255,0,0,0,0,0,0"
selection1 = selection1 & ",Type=1"
StringtoArray(selection1, var1)
pointer1 = Part.Extension.GetObjectByPersistReference3((var1), longstatus)
selection2 = "216,14,0,0,3,0,0,0,255,254,255,0,0,0,0,0,255,255,1,0,11,0,109,111,70,97,99,101,82,101,102,95,99,1,0,0,0,0,0,0,0,5,0,0,0,0,3,0,0,0,0,0,0,125,195,148,37,173,73,178,84,125,195,148,37,173,73,178,84,0,0,255,255,1,0,23,0,109,111,70,114,111,109,83,107,116,69,110,116,83,117,114,102,73,100,82,101,112,95,99,0,0,255,255,1,0,6,0,109,111,70,82,95,99,255,255,1,0,13,0,109,111,69,120,116,79,98,106,101,99,116,95,99,255,255,1,0,14,0,109,111,79,98,106,70,105,108,101,68,101,102,95,99,255,255,1,0,17,0,109,111,67,83,116,114,105,110,103,72,97,110,100,108,101,95,99,255,254,255,6,84,0,117,0,116,0,111,0,114,0,49,0,123,228,186,50,11,128,255,254,255,62,67,0,58,0,92,0,80,0,114,0,111,0,103,0,114,0,97,0,109,0,32,0,70,0,105,0,108,0,101,0,115,0,92,0,83,0,111,0,108,0,105,0,100,0,87,0,111,0,114,0,107,0,115,0,92,0,67,0,79,0,83,0,77,0,79,0,83,0,87,0,111,0,114,0,107,0,115,0,92,0,69,0,120,0,97,0,109,0,112,0,108,0,101,0,115,0,92,0,84,0,117,0,116,0,111,0,114,0,49,0,46,0,83,0,76,0,68,0,80,0,82,0,84,0,97,23,28,"
selection2 = selection2 & "65,0,0,0,0,2,0,1,0,0,0,0,0,0,0,1,0,0,0,255,254,255,7,68,0,101,0,102,0,97,0,117,0,108,0,116,0,0,0,0,0,0,0,0,0,0,0,0,0,14,0,0,0,88,231,186,50,3,0,0,0,255,255,1,0,20,0,109,111,69,110,100,70,97,99,101,83,117,114,102,73,100,82,101,112,95,99,0,0,5,128,8,0,6,0,0,0,211,228,186,50,1,0,0,0,255,255,255,255,0,0,14,128,0,0,5,128,8,0,6,0,0,0,211,228,186,50,0,0,0,0,255,255,255,255,0,0,0,0,0,0"
selection2 = selection2 & ",Type=1"
StringtoArray(selection2, var2)
pointer2 = Part.Extension.GetObjectByPersistReference3((var2), longstatus)
selection3 = "216,14,0,0,3,0,0,0,255,254,255,0,0,0,0,0,255,255,1,0,11,0,109,111,70,97,99,101,82,101,102,95,99,1,0,0,0,0,0,0,0,8,0,0,0,0,3,0,0,0,0,0,0,125,195,148,37,173,73,178,84,125,195,148,37,173,73,178,84,0,0,255,255,1,0,20,0,109,111,69,110,100,70,97,99,101,83,117,114,102,73,100,82,101,112,95,99,0,0,255,255,1,0,6,0,109,111,70,82,95,99,255,255,1,0,13,0,109,111,69,120,116,79,98,106,101,99,116,95,99,255,255,1,0,14,0,109,111,79,98,106,70,105,108,101,68,101,102,95,99,255,255,1,0,17,0,109,111,67,83,116,114,105,110,103,72,97,110,100,108,101,95,99,255,254,255,6,84,0,117,0,116,0,111,0,114,0,49,0,123,228,186,50,11,128,255,254,255,62,67,0,58,0,92,0,80,0,114,0,111,0,103,0,114,0,97,0,109,0,32,0,70,0,105,0,108,0,101,0,115,0,92,0,83,0,111,0,108,0,105,0,100,0,87,0,111,0,114,0,107,0,115,0,92,0,67,0,79,0,83,0,77,0,79,0,83,0,87,0,111,0,114,0,107,0,115,0,92,0,69,0,120,0,97,0,109,0,112,0,108,0,101,0,115,0,92,0,84,0,117,0,116,0,111,0,114,0,49,0,46,0,83,0,76,0,68,0,80,0,82,0,84,0,97,23,28,65,0,0,0,0,2,0,"
selection3 = selection3 & "1,0,0,0,0,0,0,0,1,0,0,0,255,254,255,7,68,0,101,0,102,0,97,0,117,0,108,0,116,0,0,0,0,0,0,0,0,0,0,0,0,0,11,0,0,0,51,230,186,50,1,0,0,0,255,255,255,255,255,255,1,0,23,0,109,111,70,114,111,109,83,107,116,69,110,116,83,117,114,102,73,100,82,101,112,95,99,0,0,5,128,8,0,8,0,0,0,218,229,186,50,4,0,0,0,0,0,255,255,1,0,18,0,109,111,80,76,105,110,101,83,117,114,102,73,100,82,101,112,95,99,0,0,5,128,8,0,31,0,0,0,210,195,223,50,17,128,0,0,5,128,8,0,23,0,0,0,160,192,223,50,14,128,0,0,5,128,8,0,19,0,0,0,181,233,186,50,1,0,0,0,1,0,0,0,2,0,0,0,17,128,0,0,5,128,8,0,31,0,0,0,210,195,223,50,17,128,0,0,5,128,8,0,23,0,0,0,160,192,223,50,14,128,0,0,5,128,8,0,19,0,0,0,181,233,186,50,1,0,0,0,1,0,0,0,1,0,0,0,17,128,0,0,5,128,8,0,31,0,0,0,210,195,223,50,17,128,0,0,5,128,8,0,23,0,0,0,160,192,223,50,14,128,0,0,5,128,8,0,19,0,0,0,181,233,186,50,1,0,0,0,2,0,0,0,3,0,0,0,17,128,0,0,5,128,8,0,31,0,0,0,210,195,223,50,17,128,0,0,5,128,8,0,23,0,0,0,160,192,223,50,14,"
selection3 = selection3 & "128,0,0,5,128,8,0,19,0,0,0,181,233,186,50,1,0,0,0,2,0,0,0,4,0,0,0,0,0,0,0,0,0"
selection3 = selection3 & ",Type=1"
StringtoArray(selection3, var3)
pointer3 = Part.Extension.GetObjectByPersistReference3((var3), longstatus)
' Create VARIANT arrays
Dim varArray1 As Object() = {pointer1, pointer2}
Dim varArray2 As Object() = {pointer3}
' Add fixed restraint
LBCMgr = Study.LoadsAndRestraintsManager
If LBCMgr Is Nothing Then ErrorMsg(swApp, "No loads and restraints manager.", False)
CWRes1 = LBCMgr.AddRestraint(0, (varArray1), Nothing, errCode)
If errCode <> 0 Then ErrorMsg(swApp, "No fixed restraint created.", True)
' Apply pressure normal to selected face
CWPressure = LBCMgr.AddPressure(0, (varArray2), Nothing, errCode)
If errCode <> 0 Then ErrorMsg(swApp, "No normal pressure created.", True)
Call CWPressure.PressureBeginEdit()
CWPressure.Unit = 1
CWPressure.Value = 1000
errCode = CWPressure.PressureEndEdit
If errCode <> 0 Then ErrorMsg(swApp, "Nonuniform pressure distribution not set.", True)
' Create mesh
CwMesh = Study.Mesh
If CwMesh Is Nothing Then ErrorMsg(swApp, "No mesh object.", False)
CwMesh.Quality = 1
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)
CWResult = Study.Results
If CWResult Is Nothing Then ErrorMsg(swApp, "No result object.", False)
stress = CWResult.GetMinMaxStress(0, 0, 1, Nothing, 0, errCode)
' Get strain and stress results for
' first face selected for restraint
Dim i As Integer
Dim status As Integer
Dim dispWrapper(0) As DispatchWrapper
dispWrapper(0) = New DispatchWrapper(CType(pointer1, Entity))
Dim results() As Object
Dim resultsString As String
Debug.Print(" Strain value by node:")
results = CWResult.GetStrainForEntities3(True, CType(swsStrainComponent_e.swsStrainComponentESTRN, Integer), 1, Nothing, dispWrapper, status,
0, False, errCode)
For i = 0 To UBound(results)
resultsString = Convert.ToString(results(i))
Debug.Print(" " & resultsString)
Next i
Debug.Print(" ")
Debug.Print(" Stress value by node:")
results = CWResult.GetStressForEntities3(True, CType(swsStressComponent_e.swsStressComponentVON, Integer), 1, Nothing, dispWrapper, swsStrengthUnit_e.swsStrengthUnitPascal, status,
0, False, errCode)
For i = 0 To UBound(results)
resultsString = Convert.ToString(results(i))
Debug.Print(" " & resultsString)
Next i
' Hide fixture and force symbols
Study.ShowOrHideFixtures = False
Study.ShowOrHideForce = False
Stop
' Show fixture and force symbols
Study.ShowOrHideFixtures = True
Study.ShowOrHideForce = True
End Sub
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
' Parse string into an array
Private Sub StringtoArray(ByVal inputSTR As String, ByRef varEntity As Object)
Dim PID() As Byte
Dim i As Integer
varEntity = Split(inputSTR, ",")
ReDim PID(UBound(varEntity))
For i = 0 To (UBound(varEntity) - 1)
PID(i) = varEntity(i)
Next i
varEntity = PID
End Sub
''' <summary>
''' The SldWorks swApp variable is pre-assigned for you.
''' </summary>
Public swApp As SldWorks
End Class