Change Beam to Solid Body and Back Example (VB.NET)
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 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 <SolidWorks_install_dir>\api\redist folder and
' select
SolidWorks.Interop.cosworks.dll).
' 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.
'
'-------------------------------
Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst
Imports SolidWorks.Interop.cosworks
Imports System
Imports System.Diagnostics
Partial Class SolidWorksMacro
Public
Sub main()
Dim
COSMOSWORKS As CosmosWorks
Dim
COSMOSObject As CwAddincallback
Dim
ActDoc As CWModelDoc
Dim
StudyMngr As CWStudyManager
Dim
Study As CWStudy
Dim
BeamMgr As CWBeamManager
Dim
BeamBody As CWBeamBody
Dim
SolidMgr As CWSolidManager
Dim
SolidComponent As CWSolidComponent
Dim
SolidBody As 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
'
Get the SolidWorks Simulation object
COSMOSObject
= swApp.GetAddInObject("CosmosWorks.CosmosWorks")
If
COSMOSObject Is Nothing Then ErrorMsg(swApp, "COSMOSObject object
not found.", True)
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", swDocumentTypes_e.swDocPART,
swOpenDocOptions_e.swOpenDocOptions_Silent, "", errors, warnings)
ActDoc
= COSMOSWORKS.ActiveDoc()
If
ActDoc Is Nothing Then ErrorMsg(swApp, "No active document.",
True)
'Get
study
StudyMngr
= ActDoc.StudyManager()
If
StudyMngr Is Nothing Then ErrorMsg(swApp, "StudyMngr object not there.",
True)
StudyMngr.ActiveStudy = 0
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...")
BeamMgr
= Study.BeamManager
nbrBeamBodies
= BeamMgr.BeamCount
Debug.Print("
Number
of beams: " & nbrBeamBodies)
BeamBody
= Nothing
'
Convert beams to solid bodies
For
j = 0 To (nbrBeamBodies - 1)
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
BeamBody
= Nothing
Next
j
Debug.Print("
")
'
Convert solid bodies to beams
Debug.Print("Solid
components and bodies...")
'
Get solid bodies and components
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)
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)
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.")
SolidBody
= Nothing
Next
k
Next
j
End
Sub
'Error
function
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
'''
<summary>
'''
The SldWorks swApp variable is pre-assigned for you.
'''
</summary>
Public
swApp As SldWorks
End Class