Hide Table of Contents

Fill Holes in Part Example (C#)

In CAM drilling operations, it might be useful to deduce the appearance of an item before machining begins. This is slightly different than calculating the minimum amount of raw material required, i.e., the stock size. This example shows how to use some of the geometry- and topology-related methods and properties to fill all holes in a part.

// Preconditions: Part containing holes and only one
// solid body is open.
// Postconditions:
// 1. New part is created.
// 2. New part is similar to original part, but new
//    part's holes are filled.
// 3. To see the names of the Surface-Imported, Surface-Knit, 
//    and Thicken features created by filling the holes, examine 
//    the FeatureManager design tree.
// * Only holes that completely reside on a face are filled.
// * Fillets and chamfers are not taken into account.
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.Runtime.InteropServices;
using System;
using System.Diagnostics;
namespace FillHolesInPartCSharp.csproj
    public partial class SolidWorksMacro
        public void Main()
            ModelDoc2 swModel = default(ModelDoc2);
            PartDoc swPart = default(PartDoc);
            Body2 swBody = default(Body2);
            Face2 swFace = default(Face2);
            Loop2 swLoop = default(Loop2);
            object[] vEdgeArr = null;
            Curve[] swCurve = new Curve[1];
            object[] vCurveArr = null;
            Edge swEdge = default(Edge);
            Body2 swTempBody = default(Body2);
            Surface swSurf = default(Surface);
            Surface swSurfCopy = default(Surface);
            string sPartTemplateName = null;
            ModelDoc2 swNewModel = default(ModelDoc2);
            PartDoc swNewPart = default(PartDoc);
            Feature[] swFeats = new Feature[1];
            Feature swFeat = default(Feature);
            Feature swKnitFeat = default(Feature);
            Feature swThickFeat = default(Feature);
            FeatureManager swNewFeatMgr = default(FeatureManager);
            int i = 0;
            bool bRet = false;
            object[] vBodies = null;
            swModel = (ModelDoc2)swApp.ActiveDoc;
            swPart = (PartDoc)swModel;
            vBodies = (object[])swPart.GetBodies2((int)swBodyType_e.swSolidBody, false);
            swBody = (Body2)vBodies[0];
            // Create new part
            sPartTemplateName = swApp.GetUserPreferenceStringValue((int)swUserPreferenceStringValue_e.swDefaultTemplatePart);
            swNewModel = (ModelDoc2)swApp.NewDocument(sPartTemplateName, (int)swDwgPaperSizes_e.swDwgPaperAsize, 0.0, 0.0);
            swNewFeatMgr = (FeatureManager)swNewModel.FeatureManager;
            swNewPart = (PartDoc)swNewModel;
            swFace = (Face2)swBody.GetFirstFace();
            while ((swFace != null))
                swLoop = (Loop2)swFace.GetFirstLoop();
                while ((swLoop != null))
                    if (swLoop.IsOuter())
                        vEdgeArr = (object[])swLoop.GetEdges();
                        if ((vEdgeArr.GetUpperBound(0)) >= 0)
                            Array.Resize(ref swCurve, (vEdgeArr.GetUpperBound(0) + 1));
                            for (i = 0; i <= (vEdgeArr.GetUpperBound(0)); i++)
                                swEdge = (Edge)vEdgeArr[i];
                                swCurve[i] = (Curve)swEdge.GetCurve();
                            vCurveArr = (Curve[])swCurve;
                            swSurf = (Surface)swFace.GetSurface();
                            swSurfCopy = (Surface)swSurf.Copy();
                            swTempBody = (Body2)swSurfCopy.CreateTrimmedSheet4((vCurveArr), false);
                            // Typically nothing is returned if the loop is
                            // perpendicular to the surface, as in the
                            // end loops of a cylinder
                            if ((swTempBody != null))
                                // Sheet body only has one face
                                swFeat = (Feature)swNewPart.CreateFeatureFromBody3(swTempBody, false, (int)swCreateFeatureBodyOpts_e.swCreateFeatureBodyCheck);
                                swFeats[swFeats.GetUpperBound(0)] = swFeat; 
                                Debug.Assert((swFeats[swFeats.GetUpperBound(0)] != null));
                                Array.Resize(ref swFeats, (swFeats.GetUpperBound(0)) + 2);
                    swLoop = (Loop2)swLoop.GetNext();
                swFace = (Face2)swFace.GetNextFace();
            Array.Resize(ref swFeats, (swFeats.GetUpperBound(0)));
            for (i = 0; i <= (swFeats.GetUpperBound(0)); i++)
                bRet = swFeats[i].Select2(true, 1);
            swNewFeatMgr.InsertSewRefSurface(truefalsefalse, 3.001639406912E-05, 0.0001);
            // Make sure surfaces are successfully sewn together
            swKnitFeat = (Feature)swNewModel.FeatureByPositionReverse(0);
            Debug.Assert((swKnitFeat != null));
            Debug.Assert("SewRefSurface" == swKnitFeat.GetTypeName());
            bRet = swKnitFeat.Select2(false, 1);
            swThickFeat = (Feature)swNewFeatMgr.FeatureBossThicken(0.01, 0, 0, truetruetruetrue);
            Debug.Assert((swThickFeat != null));
        /// <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:   Fill Holes in Part 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) 2015 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.