Hide Table of Contents

Cut Body and Keep All Bodies Example (C#)

This example shows how to cut a body and keep all bodies.

//----------------------------------------------------------------------------
// Preconditions:
//  1. Verify that the specified part document template exists.
//  2. Open the Immediate window.
//
// Postconditions:
// 1. Opens a new part document.
// 2. Creates a body.
// 3. Splits the body into two bodies.
// 4. Examine the graphics area and Immediate window.
//-----------------------------------------------------------------------------
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 PartDoc swPart;
        ModelDoc2 Part;
        bool boolstatus;
        Feature Feature;
 
        public void Main()
        {
            //Open new part document
            Part = (ModelDoc2)swApp.NewDocument("C:\\ProgramData\\SOLIDWORKS\\SOLIDWORKS 2015\\templates\\part.prtdot", 0, 0, 0);
 
            //Set up event
            swPart = (PartDoc)Part;
            AttachEventHandlers();
 
            //Create body
            CreateBodiesAndSketch();
            boolstatus = Part.Extension.SelectByID2("Sketch2""SKETCH", 0, 0, 0, false, 0, null, 0);
            Feature = (Feature)Part.FeatureManager.FeatureCut3(true, false, false, (int)swEndConditions_e.swEndCondThroughAll, (int)swEndConditions_e.swEndCondBlind, 0.01, 0.01, false, false, false, false, 0.01745329251994, 0.01745329251994, false, false, false, false, false, true, true, false, false, false, (int)swStartConditions_e.swStartSketchPlane, 0, false);
             if ((Feature == null))
            {
                Debug.Print("No feature created.");
            }
        }
 
        public void CreateBodiesAndSketch()
        {
            //Create body
            boolstatus = Part.Extension.SelectByID2("Front Plane""PLANE", -0.06869486923422, 0.06291203863612, -0.006492164309718, false, 0, null, 0);
            Part.ClearSelection2(true);
            Part.SketchRectangle(-0.0424567617866, 0.0388405707196, 0, 0.05638579404467, -0.03750124069479, 0, false);
            Part.ShowNamedView2("*Trimetric", 8);
            Part.ClearSelection2(true);
            boolstatus = Part.Extension.SelectByID2("Line2""SKETCHSEGMENT", 0, 0, 0, false, 0, null, 0);
            boolstatus = Part.Extension.SelectByID2("Line1""SKETCHSEGMENT", 0, 0, 0, true, 0, null, 0);
            boolstatus = Part.Extension.SelectByID2("Line4""SKETCHSEGMENT", 0, 0, 0, true, 0, null, 0);
            boolstatus = Part.Extension.SelectByID2("Line3""SKETCHSEGMENT", 0, 0, 0, true, 0, null, 0);
            Part.FeatureManager.FeatureExtrusion3(truefalsefalse, 0, 0, 0.12, 0.01, falsefalsefalse,
            false, 0.01745329251994, 0.01745329251994, falsefalsefalsefalsefalsefalsefalse,
            0, 0, false);
            Part.ClearSelection2(true);
 
            //Create sketch for cut feature
            boolstatus = Part.Extension.SelectByID2("""FACE", -0.02909828822015, 0.03884057071963, 0.09843602253397, false, 0, null, 0);
            Part.SketchManager.InsertSketch(true);
            Part.ClearSelection2(true);
            object[] vSkLines = null;
            vSkLines = (object[])Part.SketchManager.CreateCornerRectangle(-0.0628943705795, -0.07743122635196, 0, 0.1160562766823, -0.04532565168643, 0);
            Part.ClearSelection2(true);
            boolstatus = Part.Extension.SelectByID2("Line2""SKETCHSEGMENT", 0, 0, 0, false, 0, null, 0);
            boolstatus = Part.Extension.SelectByID2("Line1""SKETCHSEGMENT", 0, 0, 0, true, 0, null, 0);
            boolstatus = Part.Extension.SelectByID2("Line4""SKETCHSEGMENT", 0, 0, 0, true, 0, null, 0);
            boolstatus = Part.Extension.SelectByID2("Line3""SKETCHSEGMENT", 0, 0, 0, true, 0, null, 0);
 
        }
 
        public void AttachEventHandlers()
        {
            AttachSWEvents();
        }
 
        public void AttachSWEvents()
        {
            swPart.PromptBodiesToKeepNotify += this.swPart_PromptBodiesToKeepNotify;
        }
 
        private int swPart_PromptBodiesToKeepNotify(object swFeat, ref object bodies)
        {
            Debug.Print("PartDoc_PromptBodiesToKeepNotify fired.");
            Feature theFeature = default(Feature);
            object[] bodiesArr = null;
            bodiesArr = (object[])bodies;
            if ((swFeat != null))
            {
                theFeature = (Feature)swFeat;
                object[] bodiesToKeep = new object[1];
                //Change BodyOption to Body1 or Body2 to show other options
                string BodyOption = null;
                BodyOption = "AllBodies";
                switch (BodyOption)
                {
                    case "AllBodies":
                        theFeature.SetBodiesToKeep(true, bodiesToKeep, (int)swInConfigurationOpts_e.swThisConfiguration, null);
                        break;
                    case "Body1":
                        bodiesToKeep[0] = bodiesArr[0];
                        theFeature.SetBodiesToKeep(false, bodiesToKeep, (int)swInConfigurationOpts_e.swThisConfiguration, null);
                        break;
                    case "Body2":
                        bodiesToKeep[0] = bodiesArr[1];
                        theFeature.SetBodiesToKeep(false, bodiesToKeep, (int)swInConfigurationOpts_e.swThisConfiguration, null);
                        break;
                }
            }
            return 1;
        }
 
        /// <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:   Cut Body and Keep All Bodies 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) 2018 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.