Hide Table of Contents

Get and Set Beams and Joints Example (VB.NET)

This example shows how to get and set beams and joints.


' 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 if needed.

' 4. Open the Immediate window.

' 5. Run the macro.


' Postconditions:

' 1. A static study, named frame, is created.

' 2. Beam information is printed to the Immediate window.

' 3. Plain Carbon Steel material is applied to all beams.

' 4. Joints are calculated for all beams, and

'    a neutral axis is shown for each beam. Pinball tolerance

'    value and unit are printed to the Immediate window.

' 5. Mesh is created, and type and state printed to Immediate window.



' *  Beam elements are created by default for parts with

'    structural members.

' *  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 Joints As CWJoints

        Dim Mesh As CWMesh

        Dim nbrBeamBodies As Long

        Dim beamBodyType As Long

        Dim ElementSize As Double

        Dim Tolerance As Double

        Dim errors As Long, warnings As Long

        Dim errCode As Long

        Dim j As Integer

        Dim bApp As Boolean

       Dim keepJointUpdates As Boolean


        ' Get the SolidWorks Simulation object

        COSMOSObject = swApp.GetAddInObject("SldWorks.Simulation")

        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\samples\tutorial\weldments\weldment_box2.sldprt", swDocumentTypes_e.swDocPART, swOpenDocOptions_e.swOpenDocOptions_Silent, "", errors, warnings)

        ActDoc = COSMOSWORKS.ActiveDoc()

        If ActDoc Is Nothing Then ErrorMsg(swApp, "No active document.", True)


        'Create new static study named frame

        StudyMngr = ActDoc.StudyManager()

        If StudyMngr Is Nothing Then ErrorMsg(swApp, "StudyMngr object not there.", True)

        Study = StudyMngr.CreateNewStudy("frame", swsAnalysisStudyType_e.swsAnalysisStudyTypeStatic, swsMeshType_e.swsMeshTypeMixed, errCode)

        If Study Is Nothing Then ErrorMsg(swApp, "Study not created.", True)


        ' Get and set beam info

        BeamMgr = Study.BeamManager

        nbrBeamBodies = BeamMgr.BeamCount


        Debug.Print("  Number of beams: " & nbrBeamBodies)

        BeamBody = Nothing

        For j = 0 To (nbrBeamBodies - 1)

            BeamBody = BeamMgr.GetBeamBodyAt(j, 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

                Debug.Print("      Type of beam body: beam")


                Debug.Print("      Type of beam body: truss")

            End If

            bApp = BeamBody.SetLibraryMaterial("C:\Program Files\SolidWorks Corp\SolidWorks\lang\english\sldmaterials\solidworks materials.sldmat", "Plain Carbon Steel")

            If bApp = False Then ErrorMsg(swApp, "No material applied.", True)

            BeamBody = Nothing

        Next j


        ' Calculate joints

        Joints = BeamMgr.GetJointGroup(errCode)

        Debug.Print(" ")


        If errCode <> 0 Then ErrorMsg(swApp, "No joint group.", True)


        Joints.IncludeAllSelectedBeam = True

        Joints.IncludeDisplayNeutralAxis = True



        keepJointUpdates = Joints.IncludeKeepModifiedJointOnUpdate

        If (keepJointUpdates = True) Then

           Debug.Print(" Keep joint updates: yes")


           Debug.Print(" Keep joint updates: no")

        End If

        Debug.Print(" Overwrite pinball value: " & Joints.IncludeTreatAsJointForClearanceLessThan)

        Debug.Print("  Pinball radius: " & Joints.PinBallRadius * 0.001)

        Select Case Joints.PinBallRadiusUnit

            Case 0

                Debug.Print("  Pinball radius unit: mm")

            Case 1

                Debug.Print("  Pinball radius unit: cm")

            Case 2

                Debug.Print("  Pinball radius unit: m")

            Case 3

                Debug.Print("  Pinball radius unit: in")

            Case 4

                Debug.Print("  Pinball radius unit: ft")

            Case 5

                Debug.Print("  Pinball radius unit: ft-in")

            Case 6

                Debug.Print("  Pinball radius unit: am")

            Case 7

                Debug.Print("  Pinball radius unit: nm")

            Case 8

                Debug.Print("  Pinball radius unit: micron")

            Case 9

                Debug.Print("  Pinball radius unit: mil")

            Case 10

                Debug.Print("  Pinball radius unit: MicroIn")

        End Select


        ' Mesh the part

        Mesh = Study.Mesh

        If Mesh Is Nothing Then ErrorMsg(swApp, "No mesh object.", False)

        Mesh.Quality = swsMeshQuality_e.swsMeshQualityHigh

        Mesh.GetDefaultElementSizeAndTolerance(swsLinearUnit_e.swsLinearUnitMillimeters, ElementSize, Tolerance)

        errCode = Study.CreateMesh(swsLinearUnit_e.swsLinearUnitMillimeters, ElementSize, Tolerance)

        If errCode <> 0 Then ErrorMsg(swApp, "Mesh failed.", True)

        Debug.Print(" ")


        Debug.Print("  Time to create mesh (hh:mm:ss): " & Mesh.TimeToCompleteMesh)

        Select Case Mesh.MeshType

            Case 0

                Debug.Print("  Mesh type: solid")

            Case 1

                Debug.Print("  Mesh type: midsurface")

            Case 2

                Debug.Print("  Mesh type: surface")

            Case 3

                Debug.Print("  Mesh type: mixed")

            Case 4

                Debug.Print("  Mesh type: beam")

        End Select

        Debug.Print("  Number of mesh controls: " & Mesh.MeshControlCount)

        Select Case Mesh.MeshState

            Case 0

                Debug.Print("  Mesh state: no mesh")

            Case 1

                Debug.Print("  Mesh state: exists and is current")

            Case 2

                Debug.Print("  Mesh state: exists and is not current")

            Case 3

                Debug.Print("  Mesh state: failed")

            Case 4

                Debug.Print("  Mesh state: interrupted")

        End Select

    End Sub


    'Error routine

    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)


        If EndTest Then

        End If

    End Sub


    ''' <summary>

    ''' The SldWorks swApp variable is pre-assigned for you.

    ''' </summary>

    Public swApp As SldWorks

End Class

Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

Subject:   Feedback on Help Topics
Page:   Get and Set Beams and Joints Example (VB.NET)
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:


We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again

Web Help Content Version: API Help (English only) 2014 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.