Hide Table of Contents

Create Solid Bodies Using Geometry and Topology Methods Example (C#)

This example shows how to create a solid body using SOLIDWORKS geometry and topology methods.

//--------------------------------------------------
// Preconditions: Verify that the specified part
// document template exists.
//
// Postconditions:
// 1. Opens a new part document.
// 2. Creates a part containing a single solid
//    body feature that is the union of a box and
//    a cone.
// 3. Examine the graphics area and FeatureManager 
//    design tree.
//--------------------------------------------------
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.Runtime.InteropServices;
using System;
using System.Diagnostics;
 
namespace Macro1CSharp.csproj
{
    public partial class SolidWorksMacro
    { 
 
        public void Main()
        {
            ModelDoc2 swModel = default(ModelDoc2);
            Modeler swModeler = default(Modeler);
            Feature swFeat = default(Feature);
            double[] nConeParam = new double[9];
            object vConeArr = null;
            Body2 swConeBody = default(Body2);
            double[] nBoxParam = new double[9];
            object vBoxArr = null;
            Body2 swBoxBody = default(Body2);
            object[] vNewBodyArr = null;
            object vNewBody = null;
            PartDoc swNewPart = default(PartDoc);
            Body2 swNewBody = default(Body2);
            FaultEntity swFaultEnt = default(FaultEntity);
            int nRetVal = 0;
            int nCount = 0;
 
            // Form cone
            // Face center
            nConeParam[0] = 0.0;
            nConeParam[1] = 0.1;
            nConeParam[2] = 0.0;
            // Axis
            nConeParam[3] = 0.0;
            nConeParam[4] = 0.0;
            nConeParam[5] = 1.0;
            // Base radius
            nConeParam[6] = 0.2;
            // Top radius
            nConeParam[7] = 0.1;
            // Height
            nConeParam[8] = 0.3;
            vConeArr = nConeParam;
 
            // Form box
            // Face center
            nBoxParam[0] = 0.0;
            nBoxParam[1] = 0.1;
            nBoxParam[2] = 0.2;
            // Axis
            nBoxParam[3] = 0.0;
            nBoxParam[4] = 0.0;
            nBoxParam[5] = 1.0;
            // Width
            nBoxParam[6] = 0.3;
            // Length
            nBoxParam[7] = 0.25;
            //Height
            nBoxParam[8] = 0.4;
            vBoxArr = nBoxParam;
 
            swModeler = (Modeler)swApp.GetModeler();
            swConeBody = (Body2)swModeler.CreateBodyFromCone((vConeArr));
            swBoxBody = (Body2)swModeler.CreateBodyFromBox((vBoxArr));
            swFaultEnt = (FaultEntity)swConeBody.Check3;
            nCount = swFaultEnt.Count;
            if (nCount != 0)
            {
                Debug.Print("Faulty cone!");
                return;
            }
            swFaultEnt = (FaultEntity)swBoxBody.Check3;
            nCount = swFaultEnt.Count;
            if (nCount != 0)
            {
                Debug.Print("Faulty box!");
                return;
            }
            vNewBodyArr = (object[])swConeBody.Operations2((int)swBodyOperationType_e.SWBODYADD, swBoxBody, out nRetVal);
 
            swNewPart = (PartDoc)swApp.NewDocument("C:\\ProgramData\\SOLIDWORKS\\SOLIDWORKS 2014\\templates\\Part.prtdot", 0, 0, 0);
            foreach (object vNewBody_loopVariable in vNewBodyArr)
            {
                vNewBody = vNewBody_loopVariable;
                swNewBody = (Body2)vNewBody;
                // Create solid body feature
                swFeat = (Feature)swNewPart.CreateFeatureFromBody3(swNewBody, false, (int)swCreateFeatureBodyOpts_e.swCreateFeatureBodyCheck + (int)swCreateFeatureBodyOpts_e.swCreateFeatureBodySimplify);
            }
 
            swModel = (ModelDoc2)swNewPart;
            swModel.ViewZoomtofit2();
        }
 
        /// <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:   Create Solid Bodies Using Geometry and Topology Methods 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) 2019 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.