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.
#include "stdafx.h"
#include <mbstring.h>
#include <amapp.h>
#include "userapp.h"
void GetSketchesCOM()
{
LPMODELDOC pModelDoc;
HRESULT hres = UserApp->getSWApp()->get_IActiveDoc( &pModelDoc );
LPFEATURE pFeature = NULL;
hres = pModelDoc->IFirstFeature(&pFeature);
int count = 0;
// While features exist
while (pFeature)
{
BSTR bFeatName;
hres = pFeature->get_Name(
&bFeatName );
CString featName(
bFeatName );
CString message;
message.Format(
_T("\n\nFeature: %s"), bFeatName);
LPFEATURE pSubFeat;
hres = pFeature->IGetFirstSubFeature( &pSubFeat );
if (pSubFeat
!= NULL)
TRACE1("%s",message);
// AfxMessageBox(
message );
while ( pSubFeat
)
{
BSTR bFeatureTypeName;
hres = pSubFeat->GetTypeName(&bFeatureTypeName);
CString featureTypeName(bFeatureTypeName);
if (featureTypeName
== "ProfileFeature")
{
LPUNKNOWN iUnk
= NULL;
hres = pSubFeat->IGetSpecificFeature( &iUnk );
LPSKETCH pSketch;
hres = iUnk->QueryInterface(
IID_ISketch, (LPVOID*)&pSketch);
if ( (hres ==
S_OK) && (pSketch != NULL) )
{
BSTR bSubFeatName;
hres = pSubFeat->get_Name( &bSubFeatName );
CString subFeatName(
bSubFeatName );
CString message;
message.Format(
_T("\n\tSketch:\t%s\n"), bSubFeatName );
TRACE1("%s",message);
// AfxMessageBox( message );
pSketch->Release();
}
}
LPFEATURE pNextSubFeature;
hres = pSubFeat->IGetNextSubFeature( & pNextSubFeature);
pSubFeat->Release();
pSubFeat = pNextSubFeature;
}
LPFEATURE pNextFeature;
hres = pFeature->IGetNextFeature(&pNextFeature);
pFeature->Release();
pFeature = pNextFeature;
}
pModelDoc->Release();
}