Hide Table of Contents

Get and Set Beams and Joints Example (C#)

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 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. Mixed 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.


using SolidWorks.Interop.sldworks;

using SolidWorks.Interop.swconst;

using SolidWorks.Interop.cosworks;

using System;

using System.Diagnostics;

namespace Macro1.csproj


    partial class SolidWorksMacro


        public void Main()


            CosmosWorks COSMOSWORKS = default(CosmosWorks);

            CwAddincallback COSMOSObject = default(CwAddincallback);

            CWModelDoc ActDoc = default(CWModelDoc);

            CWStudyManager StudyMngr = default(CWStudyManager);

            CWStudy Study = default(CWStudy);

            CWBeamManager BeamMgr = default(CWBeamManager);

            CWBeamBody BeamBody = default(CWBeamBody);

            CWJoints Joints = default(CWJoints);

            CWMesh Mesh = default(CWMesh);

            int nbrBeamBodies = 0;

            int beamBodyType = 0;

            double ElementSize = 0;

            double Tolerance = 0;

            int errors = 0;

            int warnings = 0;

            int errCode = 0;

            int j = 0;

            bool keepJointUpdates = true;


            // Get the SolidWorks Simulation object

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

            if (COSMOSObject == null) ErrorMsg("COSMOSObject object not found.", true);

            COSMOSWORKS = COSMOSObject.CosmosWorks;

            if (COSMOSWORKS == null) ErrorMsg("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", (int)swDocumentTypes_e.swDocPART, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", ref errors, ref warnings);

            ActDoc = (CWModelDoc)COSMOSWORKS.ActiveDoc;

            if (ActDoc == null) ErrorMsg("No active document.", true);


            //Create new static study named frame

            StudyMngr = (CWStudyManager)ActDoc.StudyManager;

            if (StudyMngr == null) ErrorMsg("StudyMngr object not there.", true);

            Study = (CWStudy)StudyMngr.CreateNewStudy("frame", (int)swsAnalysisStudyType_e.swsAnalysisStudyTypeStatic, (int)swsMeshType_e.swsMeshTypeMixed, out errCode);

            if (Study == null) ErrorMsg("Study not created.", true);


            // Get and set beam info

            BeamMgr = (CWBeamManager)Study.BeamManager;

            nbrBeamBodies = BeamMgr.BeamCount;


            Debug.Print(" Number of beams: " + nbrBeamBodies);

            BeamBody = null;

            for (j = 0; j <= (nbrBeamBodies - 1); j++)


                BeamBody = (CWBeamBody)BeamMgr.GetBeamBodyAt(j, out errCode);

                if (errCode != 0) ErrorMsg("No beam body.", true);

                Debug.Print(" Name of beam body: " + BeamBody.BeamBodyName);

                beamBodyType = BeamBody.BeamType;

                if (beamBodyType == 0)


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




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


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

                if (errCode == 0) ErrorMsg("No material applied.", true);

                BeamBody = null;



            // Calculate joints

            Joints = (CWJoints)BeamMgr.GetJointGroup(out errCode);

            Debug.Print(" ");


            if (errCode != 0) ErrorMsg("No joint group.", true);


            Joints.IncludeAllSelectedBeam = true;

            Joints.IncludeDisplayNeutralAxis = true;



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

            keepJointUpdates = Joints.IncludeKeepModifiedJointOnUpdate;

            if (keepJointUpdates == true)


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



            Debug.Print(" Keep joint udpates: no");

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

            switch (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");




            // Mesh the part

            Mesh = Study.Mesh;

            if (Mesh == null) ErrorMsg("No mesh object.", false);

            Mesh.Quality = (int)swsMeshQuality_e.swsMeshQualityHigh;

            Mesh.GetDefaultElementSizeAndTolerance((int)swsLinearUnit_e.swsLinearUnitMillimeters, out ElementSize, out Tolerance);

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

            if (errCode != 0) ErrorMsg("Mesh failed.", true);

            Debug.Print(" ");


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

            switch (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");



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

            switch (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");





        //Error routine

        private void ErrorMsg(string Message, bool EndTest)


            swApp.SendMsgToUser2(Message, 0, 0);

            swApp.RecordLine("'*** WARNING - General");

            swApp.RecordLine("'*** " + Message);


            if (EndTest)





        /// <summary>

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

        /// </summary>

        public SldWorks swApp;



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 (C#)
*   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.