Hide Table of Contents

Get Scale Factor of Each Model View Example (C++)

This example shows how to get the scale factor of each model view in a part document.

// 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.
//    h. Click Tools > Options > Debugging > General and select 
//       Redirect all Output Window Text to the Immediate Window.
//    i. Click OK.
// 2. Open the Immediate Window.
// 3. Replace the code in the code window with this code.
// 4. Start up SOLIDWORKS and open a part document.
// 5. Click Window > Viewport > Four View.
// 6. Click a model view and spin the middle-mouse
//    button forward or back.
// 7. Click Debug > Start Debugging
// 8. Click Yes. 
// Postconditions: 
// 1. Gets the number of model views in the part document.
// 2. Gets the scale factor of each model view.
// 3. Examine the Immediate Window.
//This code 
#include "stdafx.h"
#include <iostream>
#include <sstream>
#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
		//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){
			HRESULT hres;
			CComPtr<IModelDoc2> swDoc;
			//Get number of model views
			CComPtr<IModelDocExtension> swDocExt;
			hres = swDoc->get_Extension(&swDocExt); 
			long nbrModelViews;
			hres = swDocExt->GetModelViewCount(&nbrModelViews);
			_bstr_t s;
			s =(long)nbrModelViews;
			OutputDebugString("Number of model views: " + s + "\n");
			//Get an ISafeArrayUtility object
			CComPtr<IDispatch> dispatchSafeArray = NULL;
			CComPtr<ISafeArrayUtility> swSafeArray = NULL;
			hres = swApp->GetSafeArrayUtility(&dispatchSafeArray);
			hres = dispatchSafeArray->QueryInterface<ISafeArrayUtility>(&swSafeArray);
			//Pack a Variant SafeArray of Dispatch-based objects
			IDispatch** dispArray = new IDispatch*[nbrModelViews];
			VARIANT vPacked;
			int arrType = swDispatchArray;
			hres = swSafeArray->PackVariant(&vPacked, nbrModelViews, arrType, (long*)dispArray);
			//Get the model views in the model document
			hres = swDocExt->GetModelViews(&vPacked);
			//Get scale factor of each model view
			for (int i = 0; i < nbrModelViews; i++)
				IDispatch *pDisp;
				hres = swSafeArray->GetDispatch(vPacked, i, &pDisp);
				pDisp->QueryInterface(IID_IModelView, (void**)&swModelView);
				_bstr_t t;
				double scale;
				hres = swModelView->get_Scale2(&scale);
				t = (double)scale;
				OutputDebugString("Scale of this model view is: " + t + "\n");
	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

Subject:   Feedback on Help Topics
Page:   Get Scale Factor of Each Model View Example (C++)
*   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) 2019 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.