Hide Table of Contents

Get Object's Persistent Reference ID Example (C++)

This example shows how to:

  • get the selected object's persistent reference ID using IModelDocExtension::GetPersistReference3.
  • use the ISafeArrayUtility to parse the Variant SafeArray returned by IModelDocExtension::GetPersistReference3.
  • get the size of the persistent reference ID for the selected object.
//-----------------------------------------
// Preconditions: 
// 1. 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 > projectname 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.
// 2. Replace the code in the code window with this code.
// 3. Start up SOLIDWORKS and open a model document. 
// 4. Select a vertex, line, face, or some other selectable 
//    entity.
// 5. Click Debug > Start Debugging
// 6. Click Yes. 
//
// Postconditions: 
// 1. Gets each byte value in a Variant SafeArray of byte values
//    of the persistent reference ID.
// 2. Pops up a message box showing the size of of the persistent
//    reference ID assigned to the selected object.
// 3. Click OK to close the message box.
//-----------------------------------------
 
//This code
 
#include "stdafx.h"
#include <string>
 
#import "sldworks.tlb" raw_interfaces_only, raw_native_types, no_namespace, named_guids //SOLIDWORKS type library
#import "swconst.tlb" no_namespace, raw_native_types, no_namespace, named_guids //SOLIDWORKS constants type library
 
template <typename T>
std::string number_to_string(T number)
{
		return dynamic_cast<std::stsringstream *> (&(std::stringstream() << number))->str();
}
 
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
		//scope of this block is exited
		{
			//Use ATL smart pointers
			CComPtr<ISldWorks> swApp;
			if(swApp.CoCreateInstance(L"SldWorks.Application", NULL, CLSCTX_LOCAL_SERVER) != S_OK){
					return(0);
			} 
 
			CComPtr<IModelDoc2> swDoc;
			swApp->get_IActiveDoc2(&swDoc); 
 
			CComPtr<IModelDocExtension> swDocExt;
			swDoc->get_Extension(&swDocExt); 
 
			CComPtr<ISelectionMgr> swSelMgr;
			swDoc->get_ISelectionManager(&swSelMgr); 
 
			CComPtr<IDispatch> disp;
			swSelMgr->GetSelectedObject6(1, -1, &disp); 
 
			//Get the persistent reference ID for the selected object
			_variant_t varPersist;
			::VariantInit(&varPersist);
			swDocExt->GetPersistReference3(disp, &varPersist); 
 
			CComPtr<IDispatch> pDispatchSafeArray = NULL;
			CComPtr<ISafeArrayUtility> swSafeArray = NULL;
			HRESULT hres;
			hres = swApp->GetSafeArrayUtility(&pDispatchSafeArray);
			hres = pDispatchSafeArray.QueryInterface<ISafeArrayUtility>(&swSafeArray);
 
			long sizePersistIDs;
			swDocExt->GetPersistReferenceCount3(disp, &sizePersistIDs);
 
			for(int i = 0; i < sizePersistIDs; i++){
				byte byteValue;
				//Get each byte value in the Variant SafeArray of byte values
				swSafeArray->GetByte(varPersist, i, &byteValue);
				//TODO: Do something with each persistent reference ID
			}
 
			_bstr_t s;
			s = (long)sizePersistIDs;
			CComBSTR msg;
			msg = (OLESTR("Size of persistent reference ID: "));
			msg.AppendBSTR(s);
			long res;
			//Pop up a message box with the size of persistent
			//reference ID for the selected object
			swApp->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
		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 Object's Persistent Reference ID 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) 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.