Hide Table of Contents

Traverse Assembly Example (C++ COM)

This example shows how to traverse an assembly using the IComponent2 object. The method IComponent2::IGetChildren returns an array, so this code must be used in an in-process DLL. Otherwise, use the method IComponent2::GetChildren that returns a VARIANT.

 

void GetModelAssembly(ISldWorks* m_pSldWorks)

{

 LPCONFIGURATION pConfiguration = NULL;

 LPCOMPONENT pRootComponent = NULL;

 

 LPMODELDOC2 pModelDoc = NULL;

 HRESULT hres = S_OK;

 long RecurseLevel = 0;

 

 hres = m_pSldWorks->get_IActiveDoc2( &pModelDoc );  

 

 // Retrieve model document pointer

 if( S_OK != hres || pModelDoc == NULL )

  return;  

 

 

 // Get the active configuration and root component

 if ( pModelDoc->IGetActiveConfiguration(&pConfiguration) == S_OK )

 {

  if ( pConfiguration->IGetRootComponent(&pRootComponent) == S_OK )

  {

   CString MyString;

   TraverseChildren(RecurseLevel,&MyString,pRootComponent);

 

   pRootComponent->Release();

 

   // Display the structure in a message box

   AfxMessageBox (MyString);

  }

  pConfiguration->Release();

 }

 pModelDoc->Release();

 

}

 

//////////////////////////////////////////////////////////////////////////

// Function: TraverseChildren

// Arguments: RecurseLevel - Level of recursion

//    MyString - Textural record of assembly

//    pComponent - Component to traverse

// Return: Number of children

// Description: Perform any operations specific to the component, then

//    if the component has children, recursively call

//    this function for each child.

//////////////////////////////////////////////////////////////////////////

int TraverseChildren(long RecurseLevel, CString* MyString, LPCOMPONENT pComponent)

{

 LPCOMPONENT* pChildren = NULL;

 int   nChildren;

 int   i;

 BSTR   Name;

 HRESULT  hres = S_OK;

 LPMODELDOC  pModelDoc = NULL;

 

 // Retrieve the component name

 if(RecurseLevel==0)

 {

  // Special case of top-level components

  hres = m_pSldWorks->get_IActiveDoc( &pModelDoc );

  if( S_OK == hres || pModelDoc != NULL )

   hres = pModelDoc->GetTitle(&Name);

 }

 else

 {

  // Get the component name

  hres = pComponent->get_Name(&Name);

 }

 

 if( S_OK == hres && Name != NULL )

 {

  CString tempstr;

  for( i=1; I<=RecurseLevel; i++)

  {

   tempstr += " ";

  }

 

  CString Tmp(Name);

  tempstr += Tmp;

  tempstr += "\r\n";

  *MyString = *MyString + tempstr;

 }

 

 RecurseLevel++;

 

 hres = pComponent->IGetChildrenCount(&nChildren);

 

 // Check if this component has children

 if ( S_OK == hres || nChildren > 0 )

 {

  pChildren = new LPCOMPONENT [nChildren];

 

  hres = pComponent->IGetChildren((LPCOMPONENT**)&pChildren);

  if(S_OK == hres)

  {

   // Recursively traverse the children

   for ( i=0; i<nChildren; i++ )

   {

    TraverseChildren(RecurseLevel,MyString,pChildren[i]);

    pChildren[i]->Release();

   }

  }

  delete [] pChildren;

 }

 

 RecurseLevel--;

 return nChildren;

}

 

 



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:   Traverse Assembly Example (C++ COM)
*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) 2022 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.