Hide Table of Contents

Get Names of Configurations Using Variant Example (C++)

This example shows how to:

  • get the names of the configurations in the active model.

  • pass a Variant to IModelDoc2::GetConfigurationNames.

  • use the ISafeArrayUtility interface to get the names of the configurations.

//-------------------------------------------------------
// Preconditions: 
// 1. Start up SOLIDWORKS and open a part or assembly document.
// 2. Start Microsoft Visual Studio 2010.
//    a. Click File > New > Project > Visual C++ > Win32 Console Application.
//    b. Type the name of your project in Name.
//    c. Click OK.
//    d. Click Next.
//    e. Select ATL and click Finish.
//    f. Click Project > Properties > Configuration Properties > 
//       C/C++ and type the path to sldworks.tlb and swconst.tlb, 
//       typically C:\Program Files\SOLIDWORKS Corp\SOLIDWORKS, in
//       Additional Include Directories.
//    g. Click OK.
// 3. Replace the code in the code window with this code.
// 4. Click Debug > Start Debugging
// 5. Click Yes.
//
// Postconditions: 
// 1. Pops up a message box for each configuration in the active
//    model, containing the name of that configuration.
// 2. Click OK to close each message box.
//--------------------------------------------------------
 
//This code 
#include "stdafx.h" 
#import "sldworks.tlb" raw_interfaces_only, raw_native_types, no_namespace, named_guids  // SOLIDWORKS type library
#import "swconst.tlb" raw_interfaces_only, raw_native_types, no_namespace, named_guids   // SOLIDWORKS constants type library
 
 
int _tmain(int argc, _TCHAR* argv[])
{
	// Initialize COM
	// Do this before using ATL smart pointers so that 
	// COM is available
	CoInitialize(NULL); 
 
	// Use a block so that the smart pointers are destructed when 
	// the scope of this block is exited
	{
		// Use ATL smart pointers
		CComPtr<ISldWorks>  pSwApp; 
 
		if(pSwApp.CoCreateInstance(__uuidof(SldWorks), NULL, CLSCTX_LOCAL_SERVER) != S_OK) {
			return(0);            
		} 
 
		pSwApp->put_UserControl(VARIANT_TRUE);
		pSwApp->put_Visible(VARIANT_TRUE); 
 
		CComPtr<IModelDoc2>  pSwModel;
		pSwApp->get_IActiveDoc2(&pSwModel); 
 
		if (! pSwModel) {
			return(0);
		} 
 
		CComBSTR strModelTitle;
		long nDocumentType;  // swDocumentTypes_e 
 
		pSwModel->GetTitle(&strModelTitle);
		pSwModel->GetType(&nDocumentType); 
 
		VARIANT  vConfigurationNames; 
		::VariantInit(&vConfigurationNames);
 
		// Get the names of the configurations in the active model
                  // NOTE: This is an out-of-process client, so you cannot 
                  // use ModelDoc2::IGetConfigurationNames
		pSwModel->GetConfigurationNames(&vConfigurationNames); 
		
		CComPtr<IDispatch> pDispatchSafeArray = NULL;
		CComPtr<ISafeArrayUtility> pSwSafeArray = NULL;
		HRESULT hres;
		hres = pSwApp->GetSafeArrayUtility(&pDispatchSafeArray);
		hres = pDispatchSafeArray.QueryInterface<ISafeArrayUtility>(&pSwSafeArray);
 
		long saCount = 0;
		long saType = 0;
		long* visible = 0;
 
		long nbrConfigs = 0;
		pSwModel->GetConfigurationCount(&nbrConfigs);
 
		//Get the name of each configuration
		for (long i = 0; i < nbrConfigs; i++) {
			BSTR configurationName;
			pSwSafeArray->GetBstr(vConfigurationNames, i, &configurationName);
			CComBSTR msg;
			msg = (OLESTR("Name of configuration: "));
			msg.Append(configurationName);
			long res;
			pSwApp->SendMsgToUser2(msg, 0,0, &res);
		}
 
 
	} 
	// ATL smart pointers are destructed so that all COM objects 
	// held on to are released
	// Shut down COM because you no longer need it
 
	// Stop COM
	CoUninitialize(); 
 
	return(0);
}


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:   Get Names of Configurations Using Variant Example (C++)
*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) 2018 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.