Insert Surface-cut Feature Example (C#)
This example shows how to insert a surface-cut feature.
//------------------------------------------------------------------------------
// Preconditions:
// 1. Verify that the specified file to open exists.
// 2. Open the Immediate window.
//
// Postconditions:
// 1. Opens the part whose intersecting solid bodies to cut with a plane.
// 2. Creates a plane named Plane1.
// 3. Selects Plane1 to cut all intersecting solid bodies.
// 4. Inserts the surface-cut feature, which cuts all intersecting
// solid bodies by the plane.
// 5. Examine the graphics area and Immediate window to verify.
//
// NOTE: Because this part document is used elsewhere, do not save changes.
//------------------------------------------------------------------------------
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.Runtime.InteropServices;
using System;
using System.Diagnostics;
namespace InsertCutSurface2CSharp.csproj
{
partial class SolidWorksMacro
{
public void Main()
{
PartDoc swPart = default(PartDoc);
ModelDoc2 swModel = default(ModelDoc2);
ModelDocExtension swModelDocExt = default(ModelDocExtension);
Feature swFeature = default(Feature);
FeatureManager swFeatureManager = default(FeatureManager);
RefPlane swRefPlane = default(RefPlane);
SurfCutFeatureData swSurfaceCutFeature = default(SurfCutFeatureData);
bool status = false;
string fileName = null;
int errors = 0;
int warnings = 0;
// Open part to cut with a plane
fileName = "C:\\Users\\Public\\Documents\\SOLIDWORKS\\SOLIDWORKS 2018\\samples\\tutorial\\multibody\\multi_inter.sldprt";
swPart = (PartDoc)swApp.OpenDoc6(fileName, (int)swDocumentTypes_e.swDocPART, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", ref errors, ref warnings);
swModel = (ModelDoc2)swPart;
swModelDocExt = (ModelDocExtension)swModel.Extension;
// Create and select the plane to cut the
// intersecting solid bodies in the part
status = swModelDocExt.SelectByID2("Front", "PLANE", 0, 0, 0, true, 0, null, 0);
swFeatureManager = (FeatureManager)swModel.FeatureManager;
swRefPlane = (RefPlane)swFeatureManager.InsertRefPlane((int)swRefPlaneReferenceConstraints_e.swRefPlaneReferenceConstraint_Distance, 0.045, 0, 0, 0, 0);
status = swModelDocExt.SelectByID2("Plane1", "PLANE", 0, 0, 0, true, 0, null, 0);
// Insert surface-cut feature to cut
// all intersecting solid bodies
swFeature = (Feature)swFeatureManager.InsertCutSurface(false, 0, false, true, null, out errors);
Debug.Print("Were any errors generated by the surface cut (0 = no errors)? " + errors);
// Get surface-cut feature and some properties
swSurfaceCutFeature = (SurfCutFeatureData)swFeature.GetDefinition();
Debug.Print("Name of surface-cut feature: " + swFeature.Name);
Debug.Print(" Is feature scope on? " + swSurfaceCutFeature.FeatureScope);
Debug.Print(" Number of bodies cut by the plane: " + swSurfaceCutFeature.GetFeatureScopeBodiesCount());
Debug.Print(" Is auto-select on? " + swSurfaceCutFeature.AutoSelect);
}
/// <summary>
/// The SldWorks swApp variable is pre-assigned for you.
/// </summary>
public SldWorks swApp;
}
}