Analyze Part Example (VBA)
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.
'--------------------------------------------------------
'
' This example shows how to create a static analysis of
a part.
'
' 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. Modify
the path to solidworks materials.sldmat
if needed.
' 4. Open
the Immediate window.
' 5. Run
the macro.
'
' Postconditions:
' 1. Study
called Static-solid is created.
' 2. Material
is applied to the model.
' 3. Restraints
and pressure are applied to the selected faces.
' 4. Mesh
is created.
' 5. Analysis
is run.
' 6. Strain
and stress results are written to the Immediate window.
'
' NOTE: Because the part document is used by an online
' SolidWorks Simulation tutorial, do not save any changes
' when closing the document.
'
'-------------------------------------
Option Explicit
Sub main()
Dim
SwApp As SldWorks.SldWorks
Dim
COSMOSWORKS As Object
Dim
CWObject As CosmosWorksLib.CwAddincallback
Dim
ActDoc As CosmosWorksLib.CWModelDoc
Dim
StudyMngr As CosmosWorksLib.CWStudyManager
Dim
Study As CosmosWorksLib.CWStudy
Dim
SolidMgr As CosmosWorksLib.CWSolidManager
Dim
SolidComponent As CosmosWorksLib.CWSolidComponent
Dim
SolidBody As CosmosWorksLib.CWSolidBody
Dim
CwMesh As CosmosWorksLib.CwMesh
Dim
CWResult As CosmosWorksLib.CWResults
Dim
Part As SldWorks.ModelDoc2
Dim
LBCMgr As CosmosWorksLib.CWLoadsAndRestraintsManager
Dim
CWPressure As CosmosWorksLib.CWPressure
Dim
CWRes1 As CosmosWorksLib.CWRestraint
Dim
pointer1 As Object, pointer2 As Object, pointer3 As Object
Dim
CWMat As CosmosWorksLib.CWMaterial
Dim
var1 As Variant, var2 As Variant, var3 As Variant
Dim
varArray1 As Variant
Dim
varArray2 As Variant
Dim
stress As Variant
Dim
bApp As Boolean
Dim
selection1 As String
Dim
selection2 As String
Dim
selection3 As String
Dim
longstatus As Long, longwarnings As Long
Dim
errCode As Long
Dim
NSource As Long
Dim
el As Double, tl As Double
'
Connect to SolidWorks
If
SwApp Is Nothing Then Set SwApp = Application.SldWorks
'
Open SolidWorks part document
Set
Part = SwApp.OpenDoc6("C:\Program
Files\SolidWorks Corp\SolidWorks\Simulation\Examples\Tutor1.SLDPRT ",
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
Set
CWObject = SwApp.GetAddInObject("CosmosWorks.CosmosWorks")
If
CWObject Is Nothing Then ErrorMsg SwApp, "CWObject object not found.",
True
Set
COSMOSWORKS = CWObject.COSMOSWORKS
If
COSMOSWORKS Is Nothing Then ErrorMsg SwApp, "CWObject not found.",
True
'
Get active document
Set
ActDoc = COSMOSWORKS.ActiveDoc()
If
ActDoc Is Nothing Then ErrorMsg SwApp, "No active document.",
True
'
Create new static study
Set
StudyMngr = ActDoc.StudyManager()
If
StudyMngr Is Nothing Then ErrorMsg SwApp, "StudyMngr object not there.",
True
Set
Study = StudyMngr.CreateNewStudy("Static_solid",
swsAnalysisStudyTypeStatic, swsMeshTypeSolid, errCode)
If
Study Is Nothing Then ErrorMsg SwApp, "Study not created.",
True
'
Set material from the SolidWorks material library
Set
SolidMgr = Study.SolidManager
If
SolidMgr Is Nothing Then ErrorMsg SwApp, "SolidMgr object not there.",
True
Set
SolidComponent = SolidMgr.GetComponentAt(0,
errCode)
If
errCode <> 0 Then ErrorMsg SwApp, "No solid component.",
True
Set
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
Set
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
Set
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
Set
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
Set
pointer3 = Part.Extension.GetObjectByPersistReference3((var3),
longstatus)
'
Create variant arrays
varArray1
= Array(pointer1, pointer2)
varArray2
= Array(pointer3)
'
Add fixed restraint
Set
LBCMgr = Study.LoadsAndRestraintsManager
If
LBCMgr Is Nothing Then ErrorMsg SwApp, "No loads and restraints manager.",
False
Set
CWRes1 = LBCMgr.AddRestraint(0,
(varArray1), Nothing, errCode)
If
errCode <> 0 Then ErrorMsg SwApp, "No fixed restraint created.",
True
'
Apply pressure normal to selected face
Set
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
Set
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
Set
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 Long
Dim
selEntity As Variant
Dim
status As Long
Dim
entity(0) As SldWorks.entity
'
First face selected for restraint
Set
entity(0) = pointer1
i
= 0
Debug.Print
" Strain
value by node:"
For
Each selEntity In CWResult.GetStrainForEntities2(True,
swsStrainComponent_e.swsStrainComponentESTRN, 1, Nothing, (entity), status)
Debug.Print
" "
& selEntity
Next
Debug.Print
" "
Debug.Print
" Stress
value by node:"
For
Each selEntity In CWResult.GetStressForEntities2(True,
swsStressComponent_e.swsStressComponentVON, 1, Nothing, (entity), swsStrengthUnitPascal,
status)
Debug.Print
" "
& selEntity
Next
End Sub
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
' Parse string into an array
Function StringtoArray(inputSTR As String, varEntity As
Variant)
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 Function