Hide Table of Contents

Expand or Collapse FeatureManager Design Tree Nodes Example (C#)

This example shows how to traverse, expand, and collapse the nodes of a FeatureManager design tree.

//---------------------------------------------------------------------------
// Preconditions: Open a part or assembly document.
//
// Postconditions:
// 1. Inspect the Immediate window.
// 2. All of the FeatureManager design tree nodes are expanded.
// 3. Collapse all nodes.
//--------------------------------------------------------------------------
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 ExpandCollapseFMDesignTree_CSharp.csproj
{
    
partial class SolidWorksMacro
    {
        
int traverseLevel;
        
bool expandThis;

        
public void Main()
        {
            
int i = 0;

            
ModelDoc2 myModel = default(ModelDoc2);
            
FeatureManager featureMgr = default(FeatureManager);
            
TreeControlItem rootNode = default(TreeControlItem);

            myModel = (
ModelDoc2)swApp.ActiveDoc;
            featureMgr = myModel.FeatureManager;
            rootNode = featureMgr.GetFeatureTreeRootItem2((
int)swFeatMgrPane_e.swFeatMgrPaneBottom);

            expandThis =
true;

            
for (i = 0; i <= 1; i++)
            {
                
if ((rootNode != null))
                {
                    
Debug.Print("");
                    traverseLevel = 0;
                    traverse_node(rootNode);
                }

                expandThis =
false;

                
if (i == 0)
                {
                    
MessageBox.Show("Ok to Collapse All");
                }
            }
        }


        
private void traverse_node(TreeControlItem node)
        {
            
TreeControlItem childNode = default(TreeControlItem);
            
Feature featureNode = default(Feature);
            
Component2 componentNode = default(Component2);
            
int nodeObjectType = 0;
            
object nodeObject = null;
            
string restOfString = "";
            
string indent = "";
            
int i = 0;
            
bool displayNodeInfo = false;
            
string compName = null;
            
int suppr = 0;
            
string supprString = "";
            
int vis = 0;
            
string visString = "";
            
string notfloatingString = "";
            
bool notfloating = false;
            
object componentDoc = null;
            
string docString = "";
            
string refConfigName = "";

            displayNodeInfo =
false;
            nodeObjectType = node.ObjectType;
            nodeObject = node.Object;

            
switch (nodeObjectType)
            {
                
case (int)swTreeControlItemType_e.swFeatureManagerItem_Feature:

                    displayNodeInfo =
true;
                    
if ((nodeObject != null))
                    {
                        featureNode = (
Feature)nodeObject;
                        restOfString =
"[FEATURE: " + featureNode.Name + "]";
                    }
                    
else
                    {
                        restOfString =
"[FEATURE: object Null?!]";
                    }

                    
break;
                
case (int)swTreeControlItemType_e.swFeatureManagerItem_Component:

                    displayNodeInfo =
true;

                    
if ((nodeObject != null))
                    {
                        componentNode = (
Component2)nodeObject;
                        compName = componentNode.Name2;

                        
if ((string.IsNullOrEmpty(compName)))
                        {
                            compName =
"???";
                        }

                        suppr = componentNode.GetSuppression();

                        
switch ((suppr))
                        {

                            
case (int)swComponentSuppressionState_e.swComponentFullyResolved:
                                supprString =
"Resolved";

                                
break;
                            
case (int)swComponentSuppressionState_e.swComponentLightweight:
                                supprString =
"Lightweight";

                                
break;
                            
case (int)swComponentSuppressionState_e.swComponentSuppressed:
                                supprString =
"Suppressed";

                                
break;
                        }

                        vis = componentNode.Visible;

                        
switch ((vis))
                        {

                            
case (int)swComponentVisibilityState_e.swComponentHidden:
                                visString =
"Hidden";

                                
break;
                            
case (int)swComponentVisibilityState_e.swComponentVisible:
                                visString =
"Visible";

                                
break;
                        }

                        notfloating = componentNode.IsFixed();
                        
                        
if (notfloating == false)
                        {
                            notfloatingString =
"Floating";
                        }
                        
else
                        {
                            notfloatingString =
"Fixed";
                        }

                        componentDoc = componentNode.GetModelDoc2();

                        
if (componentDoc == null)
                        {
                            docString =
"NotLoaded";
                        }
                        
else
                        {
                            docString =
"Loaded";
                        }

                        refConfigName = componentNode.ReferencedConfiguration;

                        
if ((string.IsNullOrEmpty(refConfigName)))
                        {
                            refConfigName =
"???";
                        }

                        restOfString =
"[COMPONENT: " + compName + " " + docString + " " + supprString + " " + visString + " " + refConfigName + "]";
                    }
                    
else
                    {
                        restOfString =
"[COMPONENT: object Null?!]";
                    }

                    
break;
                
default:

                    displayNodeInfo =
true;

                    
if ((nodeObject != null))
                    {
                        restOfString =
"[object type not handled]";
                    }
                    
else
                    {
                        restOfString =
"[object Null?!]";
                    }

                    
break;
            }

            
for (i = 1; i <= traverseLevel; i++)
            {
                indent = indent +
"  ";
            }

            
if ((displayNodeInfo))
            {
                
Debug.Print(indent + node.Text + " : " + restOfString);
            }

            
// Expand the node
            node.Expanded = expandThis;
            traverseLevel = traverseLevel + 1;
            childNode = node.GetFirstChild();

            
while ((childNode != null))
            {
                
Debug.Print(indent + "Node is expanded: " + childNode.Expanded);
                traverse_node(childNode);
                childNode = childNode.GetNext();
            }

            traverseLevel = traverseLevel - 1;

        }

        
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:   Solidworks Help
*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) 2012 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.