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.
//
// NOTES:
// * 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)));
swNewModel.ClearSelection2(true);
for (i = 0; i <= (swFeats.GetUpperBound(0)); i++)
{
bRet = swFeats[i].Select2(true, 1);
Debug.Assert(bRet);
}
swNewFeatMgr.InsertSewRefSurface(true, false, false, 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);
Debug.Assert(bRet);
swThickFeat = (Feature)swNewFeatMgr.FeatureBossThicken(0.01, 0, 0, true, true, true, true);
Debug.Assert((swThickFeat != null));
}
/// <summary>
/// The SldWorks swApp variable is pre-assigned for you.
/// </summary>
public SldWorks swApp;
}
}