Hide Table of Contents

Get Sketches Example (C++ COM)

If you use a sketch to create a feature on your solid body, that sketch is a subfeature of that solid feature. This is illustrated by expanding and examining the FeatureManager design tree of a SOLIDWORKS feature.

This example shows how to traverse the FeatureManager design tree and use subfeature traversal to grab the ISketch objects used by each feature.


// Precompiled header

#include "stdafx.h"   

#include <mbstring.h>


// SOLIDWORKS includes

#include <amapp.h>   


// User Dll includes

#include "userapp.h"   


void GetSketchesCOM()




HRESULT hres = UserApp->getSWApp()->get_IActiveDoc( &pModelDoc );




// Get first feature in document

hres = pModelDoc->IFirstFeature(&pFeature);    

int count = 0;


// While features exist

while (pFeature)        



BSTR bFeatName;

// Get the feature name

hres = pFeature->get_Name( &bFeatName );    

CString featName( bFeatName );

CString message;

message.Format( _T("\n\nFeature: %s"), bFeatName);




// Get the subfeature

hres = pFeature->IGetFirstSubFeature( &pSubFeat );   


if (pSubFeat != NULL)

// Print to Debug Output Window



// Popup dialog for Release builds

//  AfxMessageBox( message );      


// While subfeatures exist

while ( pSubFeat )        



BSTR bFeatureTypeName;


// Get the subfeature type

hres = pSubFeat->GetTypeName(&bFeatureTypeName);   

CString featureTypeName(bFeatureTypeName);


// If the sub-feature is a Sketch

if (featureTypeName == "ProfileFeature")    




// Get the Sketch interface

hres = pSubFeat->IGetSpecificFeature( &iUnk );  



hres = iUnk->QueryInterface( IID_ISketch, (LPVOID*)&pSketch);


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


BSTR bSubFeatName;


// Get the sub-feature name

hres = pSubFeat->get_Name( &bSubFeatName );  

CString subFeatName( bSubFeatName );

CString message;

message.Format( _T("\n\tSketch:\t%s\n"), bSubFeatName );


// Print to Debug Output Window



// Popup dialog for Release builds

// AfxMessageBox( message );     


// Now that you have the Sketch used by this feature (pFeature) and

// you have its name, you can Select the sketch by calling

// SelectByID and passing the sketch name, or you can use the

// m_Sketch object to call the methods and properties available

// with the Sketch class.


// < YOUR CODE >





// End if subfeature is a sketch



LPFEATURE pNextSubFeature;


// Get next subfeature

hres = pSubFeat->IGetNextSubFeature( & pNextSubFeature);  


pSubFeat = pNextSubFeature;


// End while subfeatures exist



LPFEATURE pNextFeature;


// Get next feature

hres = pFeature->IGetNextFeature(&pNextFeature);   


pFeature = pNextFeature;


// End while features exist





// End function




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

Subject:   Feedback on Help Topics
Page:   Get Sketches Example (C++ COM)
*   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:


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

Web Help Content Version: API Help (English only) 2015 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.