Create Body From Deformed Shape Example (VBA)
This example shows how to save the deformed shape resulting from
running 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 type library as a reference
' (in the IDE, click Tools > References > SOLIDWORKS
' Simulation version type library).
' 3. Ensure that the specified model exists.
' 4. Open an Immediate window.
'
' Postconditions:
' 1. Opens the model document.
' 2. Gets the static study.
' 3. Meshes the model.
' 4. Sets the solver type.
' 5. Analyzes the study.
' 6. Saves the deformed shape as
' install_dir\samples\tutorial\api\deformedBody.sldprt.
' 7. Inspect the Immediate window.
'
' NOTE: Because the model is used elsewhere, do not save changes.
'---------------------------------------------------------------------------
Option Explicit
Sub main()
Dim SwApp
As SldWorks.SldWorks
Dim Part
As SldWorks.ModelDoc2
Dim fileName As
String
Dim errors
As Long
Dim warnings As
Long
Dim errCode
As Long
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 StaticOptions As
CosmosWorksLib.CWStaticStudyOptions
Dim CWFeatObj As
CosmosWorksLib.CWMesh
Dim res
As CosmosWorksLib.CWResults
Const MeshEleSize
As Double = 1.4769
Const MeshTol
As Double = 0.073847
Set SwApp = Application.SldWorks
If SwApp Is Nothing Then Exit Sub
fileName = "C:\Program Files\SOLIDWORKS
Corp\SOLIDWORKS\samples\tutorial\api\tjoint.sldprt"
Set Part = SwApp.OpenDoc6(fileName, swDocPART, 1, "",
errors, warnings)
Set CWAddinCallBack = SwApp.GetAddInObject("SldWorks.Simulation")
Set COSMOSWORKS = CWAddinCallBack.COSMOSWORKS
Set ActDoc = COSMOSWORKS.ActiveDoc()
Set StudyMngr = ActDoc.StudyManager()
Set Study = StudyMngr.GetStudy(0)
Study.ActivateConfiguration
StudyMngr.ActiveStudy = 0
'Mesh
Set CWFeatObj = Study.Mesh
CWFeatObj.MesherType = 0
CWFeatObj.Quality = 0
errCode = Study.CreateMesh(0, MeshEleSize, MeshTol)
Set CWFeatObj = Nothing
'Set solver type to FFEPlus
Set StaticOptions = Study.StaticStudyOptions
StaticOptions.SolverType = 2
'Run analysis
errCode = Study.RunAnalysis
Set res = Study.Results
res.CreateDeformedBody swsCreateDeformedBodyAsPart, "deformedBody",
errCode
Debug.Print "Create deformed body result code as defined in
swsCreateDeformedBodyError_e: " & errCode
End Sub