Hide Table of Contents

Change Beam to Solid Body and Back Example (C#)

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.

//

//-------------------------------

using SolidWorks.Interop.sldworks;

using SolidWorks.Interop.swconst;

using SolidWorks.Interop.cosworks;

using System;

using System.Diagnostics;

namespace BeamToSolidSolidToBeamCSharp.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);

            CWSolidManager SolidMgr = default(CWSolidManager);

            CWSolidComponent SolidComponent = default(CWSolidComponent);

            CWSolidBody SolidBody = default(CWSolidBody);

            int nbrBeamBodies = 0;

            int beamBodyType = 0;

            int errors = 0;

            int warnings = 0;

            int errCode = 0;

            int j = 0;

            int nbrSolidComponents = 0;

            int nbrSolidBodies = 0;

            int k = 0;

 

            // Get the SolidWorks Simulation object

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

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

            COSMOSWORKS = (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\\Simulation\\Examples\\Beams\\Beam_Truss.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);

            //Get study

            StudyMngr = (CWStudyManager)ActDoc.StudyManager;

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

            StudyMngr.ActiveStudy = 0;

            Study = (CWStudy)StudyMngr.GetStudy(0);

            if (Study == null) ErrorMsg("No study at that index.", true);

 

            // Get and set beams to solids and solids to beams

 

            Debug.Print("Beams...");

            BeamMgr = (CWBeamManager)Study.BeamManager;

            nbrBeamBodies = BeamMgr.BeamCount;

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

            BeamBody = null;

            // Convert beams to solid bodies

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

            {

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

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

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

                beamBodyType = BeamBody.BeamType;

                if (beamBodyType == 0)

                {

                    BeamBody.ConvertToSolidBody();

                    Debug.Print("     Beam converted to solid body.");

                }

                BeamBody = null;

            }

            Debug.Print(" ");

 

            // Convert solid bodies to beams

            Debug.Print("Solid components and bodies...");

            // Get solid bodies and components

            SolidMgr = (CWSolidManager)Study.SolidManager;

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

            nbrSolidComponents = SolidMgr.ComponentCount;

            Debug.Print(" Number of solid components: " + nbrSolidComponents);

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

            {

                SolidComponent = (CWSolidComponent)SolidMgr.GetComponentAt(j, out errCode);

                if (SolidComponent == null) ErrorMsg("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; k <= (nbrSolidBodies - 1); k++)

                {

                    SolidBody = (CWSolidBody)SolidComponent.GetSolidBodyAt(0, out errCode);

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

                    Debug.Print("       Name of solid body: " + SolidBody.SolidBodyName);

                    SolidBody.ConvertToBeamBody();

                    Debug.Print("         Solid body converted to beam.");

                    SolidBody = null;

                }

            }

        }

        //Error function

        private void ErrorMsg(string Message, bool EndTest)

        {

            swApp.SendMsgToUser2(Message, 0, 0);

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

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

            swApp.RecordLine("");

            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

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Change Beam to Solid Body and Back Example (C#)
*Comment:  
*   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:

x

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
x

Web Help Content Version: API Help (English only) 2011 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.