Hide Table of Contents

Get Bodies in Body Folders Example (C#)

This example shows how to get the bodies in body folder features.

//----------------------------------------------------------------------------
// Preconditions: Open a part document that contains a body folder that contains
// one or more solid, surface, cut-list, or weldment items.
//
// Postconditions: Inspect the Immediate window.
//----------------------------------------------------------------------------
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace BodyFolderType_CSharp.csproj
{
    
partial class SolidWorksMacro
    {

        
ModelDoc2 swPart;
        
Feature swFeat;

        
string[] BodyFolderType = new string[6];

        
public void Main()
        {
            BodyFolderType[0] =
"dummy";
            BodyFolderType[1] =
"swSolidBodyFolder";
            BodyFolderType[2] =
"swSurfaceBodyFolder";
            BodyFolderType[3] =
"swBodySubFolder";
            BodyFolderType[4] =
"swWeldmentSubFolder";
            BodyFolderType[5] =
"swWeldmentCutListFolder";

            swPart = (
ModelDoc2)swApp.ActiveDoc;

            
Debug.Print("File = " + swPart.GetPathName());

            swFeat = (
Feature)swPart.FirstFeature();
            TraverseFeatures(swFeat,
true);

        }

        
public void DoTheWork(Feature thisFeat)
        {
            
bool IsBodyFolder = false;
            IsBodyFolder =
false;

            
string FeatType = null;
            FeatType = thisFeat.GetTypeName();

            
if (FeatType == "SolidBodyFolder" | FeatType == "SurfaceBodyFolder" | FeatType == "CutListFolder" | FeatType == "SubWeldFolder" | FeatType == "SubAtomFolder")
            {
                IsBodyFolder =
true;
            }


            
if (IsBodyFolder)
            {
                
BodyFolder BodyFolder = default(BodyFolder);
                BodyFolder = (
BodyFolder)thisFeat.GetSpecificFeature2();

                
int BodyFolderTypeE = 0;
                BodyFolderTypeE = BodyFolder.Type;
                
Debug.Print(thisFeat.Name + "," + FeatType + "," + BodyFolderType[BodyFolderTypeE] + " type " + BodyFolderTypeE);

                
int BodyCount = 0;
                BodyCount = BodyFolder.GetBodyCount();

                
Debug.Print("   Body Count is " + BodyCount);

                
object[] vBodies = null;
                vBodies = (
object[])BodyFolder.GetBodies();

                
int i = 0;

                
if ((vBodies != null))
                {
                    
for (i = vBodies.GetLowerBound(0); i <= vBodies.GetUpperBound(0); i++)
                    {
                        
Body2 Body = default(Body2);
                        Body = (
Body2)vBodies[i];
                        
Debug.Print("   " + Body.Name);
                    }
                }

                
Feature FeatureFromBodyFolder = default(Feature);
                FeatureFromBodyFolder = BodyFolder.GetFeature();

                
if ((!object.ReferenceEquals(FeatureFromBodyFolder, thisFeat)))
                {
                    
MessageBox.Show("Features don't match!");
                }

            }
            
else
            {
            }

        }

        
public void TraverseFeatures(Feature thisFeat, bool isTopLevel)
        {
            
Feature curFeat = default(Feature);
            curFeat = thisFeat;

            
while ((curFeat != null))
            {
                DoTheWork(curFeat);
//Do the thing that we are doing this feature traversal for

                Feature subfeat = default(Feature);
                subfeat = (
Feature)curFeat.GetFirstSubFeature();

                
while ((subfeat != null))
                {
                    TraverseFeatures(subfeat,
false);
                    
Feature nextSubFeat = default(Feature);
                    nextSubFeat = (
Feature)subfeat.GetNextSubFeature();
                    subfeat = nextSubFeat;
                    nextSubFeat =
null;
                }

                subfeat =
null;

                
Feature nextFeat = default(Feature);

                
if (isTopLevel)
                {
                    nextFeat = (
Feature)curFeat.GetNextFeature();
                }
                
else
                {
                    nextFeat =
null;
                }

                curFeat = nextFeat;
                nextFeat =
null;

            }

        }



        
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:   Get Bodies in Body Folders 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) 2013 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.