Change Beam to Solid Body and Back Example (VBA)
This example shows how to change a beam to a solid body and then back to a beam.
'---------------------------------------------------------------------------
' 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. Open the Immediate window.
' 4. Run the macro.
'
' Postconditions: Examine the output in the Immediate
window to verify
' that
beams were converted to solid bodies, and then converted back to beams.
' You
can also expand and examine the Simulation Study tree to verify the
' macro.
'
' NOTES: Because the part document is used with
' a
SolidWorks Simulation online tutorial, do not save any
' changes
when closing the document.
'---------------------------------------------------------------------------
Option Explicit
Sub main()
Dim
SwApp As SldWorks.SldWorks
Dim
COSMOSWORKS As CosmosWorksLib.COSMOSWORKS
Dim
COSMOSObject As CosmosWorksLib.CwAddincallback
Dim
ActDoc As CosmosWorksLib.CWModelDoc
Dim
StudyMngr As CosmosWorksLib.CWStudyManager
Dim
Study As CosmosWorksLib.CWStudy
Dim
BeamMgr As CosmosWorksLib.CWBeamManager
Dim
BeamBody As CosmosWorksLib.CWBeamBody
Dim
SolidMgr As CosmosWorksLib.CWSolidManager
Dim
SolidComponent As CosmosWorksLib.CWSolidComponent
Dim
SolidBody As CosmosWorksLib.CWSolidBody
Dim
nbrBeamBodies As Long
Dim
beamBodyType As Long
Dim
errors As Long, warnings As Long
Dim
errCode As Long
Dim
j As Long
Dim
nbrSolidComponents As Long
Dim
nbrSolidBodies As Long
Dim
k As Long
'
Connect to SolidWorks
If
SwApp Is Nothing Then Set SwApp = Application.SldWorks
'
Get the SolidWorks Simulation object
Set
COSMOSObject = SwApp.GetAddInObject("SldWorks.Simulation")
If
COSMOSObject Is Nothing Then ErrorMsg SwApp, "COSMOSObject object
not found.", True
Set
COSMOSWORKS = COSMOSObject.COSMOSWORKS
If
COSMOSWORKS Is Nothing Then ErrorMsg SwApp, "COSMOSWORKS object not
found.", True
'Open
and get the active document
SwApp.OpenDoc6 "C:\Program Files\SolidWorks
Corp\SolidWorks\Simulation\Examples\Beams\Beam_Truss.sldprt", swDocPART,
swOpenDocOptions_Silent, "", errors, warnings
Set
ActDoc = COSMOSWORKS.ActiveDoc()
If
ActDoc Is Nothing Then ErrorMsg SwApp, "No active document.",
True
'Get
study
Set
StudyMngr = ActDoc.StudyManager()
If
StudyMngr Is Nothing Then ErrorMsg SwApp, "StudyMngr object not there.",
True
StudyMngr.ActiveStudy = 0
Set
Study = StudyMngr.GetStudy(0)
If
Study Is Nothing Then ErrorMsg SwApp, "No study.", True
'
Get and set beams to solids and solids to beams
Debug.Print
"Beams..."
Set
BeamMgr = Study.BeamManager
nbrBeamBodies
= BeamMgr.BeamCount
Debug.Print
" Number
of beams: " & nbrBeamBodies
Set
BeamBody = Nothing
'
Convert beams to solid bodies
For
j = 0 To (nbrBeamBodies - 1)
Set
BeamBody = BeamMgr.GetBeamBodyAt(0,
errCode)
If
errCode <> 0 Then ErrorMsg SwApp, "No beam body.", True
Debug.Print
" Name
of beam body: " & BeamBody.BeamBodyName
beamBodyType
= BeamBody.BeamType
If
beamBodyType = 0 Then
BeamBody.ConvertToSolidBody
Debug.Print
" Beam
converted to solid body."
End
If
Set
BeamBody = Nothing
Next
j
Debug.Print
" "
'
Convert solid bodies to beams
Debug.Print
"Solid components and bodies..."
'
Get solid bodies and components
Set
SolidMgr = Study.SolidManager
If
SolidMgr Is Nothing Then ErrorMsg SwApp, "SolidMgr object not created.",
True
nbrSolidComponents
= SolidMgr.ComponentCount
Debug.Print
" Number
of solid components: " & nbrSolidComponents
For
j = 0 To (nbrSolidComponents - 1)
Set
SolidComponent = SolidMgr.GetComponentAt(j,
errCode)
If
SolidComponent Is Nothing Then ErrorMsg SwApp, "No solid component.",
True
Debug.Print
" Name
of solid components: " & SolidComponent.ComponentName
'
Get solid bodies
nbrSolidBodies
= SolidComponent.SolidBodyCount
Debug.Print
" Number
of solid bodies: " & nbrSolidBodies
For
k = 0 To (nbrSolidBodies - 1)
Set
SolidBody = SolidComponent.GetSolidBodyAt(0,
errCode)
If
errCode <> 0 Then ErrorMsg SwApp, "No solid body.", True
Debug.Print
" Name
of solid body: " & SolidBody.SolidBodyName
SolidBody.ConvertToBeamBody
Debug.Print
" Solid
body converted to beam."
Set
SolidBody = Nothing
Next
k
Next
j
End Sub
'Error function
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