Hide Table of Contents

Get Selected Feature Example

This example shows how to get the selected feature and its type, and return the object to the calling method.


LPFEATURE CGetSelectedFeatureApp::GetSelectedFeature1( LPMODELDOC2 pModelDoc, CString* Message )


 long lres = 0;




 LPFEATURE pSelectedFeature = NULL;

 BSTR bFeatureTypeName;

 BSTR bFeatureName;


 // Get the Selection Manager for this doc

 if(S_OK != pModelDoc->get_ISelectionManager( &pSelectMgr ) ||

pSelectMgr == NULL)


  *Message = "Unable to get the Selection Manager.\r\n";


 else if(S_OK != pSelectMgr->GetSelectedObjectType( 1, &lres )

|| lres == 0)


 // Get the Selected Object type

  *Message = "No Feature is currently selected.\r\n";


 else if(S_OK != pSelectMgr->IGetSelectedObject2( 1, &pUnk )

|| pUnk == NULL)


 // Get the Selected Object of Unknown Type

  *Message = "Unable to get the selected Object.\r\n";


 else if(S_OK != pUnk->QueryInterface( IID_IFeature,

(LPVOID *)&pSelectedFeature) || pSelectedFeature == NULL)


 // Get the Selected Feature

  *Message = "Unable to get the selected Feature.\r\n";


 else if(S_OK != pSelectedFeature->GetTypeName(&bFeatureTypeName)||

bFeatureTypeName == NULL)


 // Get the Selected Feature Type Name


  *Message = "Unable to get the selected Feature Type Name.\r\n";


 else if(S_OK != pSelectedFeature->get_Name(&bFeatureName)||

bFeatureName == NULL)


 // Get the Selected Feature Name

  *Message = "Unable to get the selected Feature Name.\r\n";




  // Everything is okay. Clean up and report back with object.


  // Release the Selection Manager


  // Release the unknown object



  // Form the return message

  CString FeatureTypeName(bFeatureTypeName);

  CString FeatureName(bFeatureName);

  *Message = "Found a Feature called " + FeatureName + " of type "

+ FeatureTypeName + ".\r\n";




  // Return with Feature object

  return pSelectedFeature;     


 // Something went wrong. Clean up and report back without object.

 // Release the Selection Manager


 // Release the unknown object


 // Return with no object

 return NULL;




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 Selected Feature 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) 2010 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.