Hide Table of Contents

Get and Set Units Example (C++ COM)

This example shows how to get the current unit settings of the document and change them to inches. Be sure to import swconst.tlb.

The method IModelDoc2::IGetUnits returns an array, so this code must be used in an in-process DLL. Otherwise, use the method IModelDoc2::GetUnits that returns a VARIANT.

 

GetAndSetUnits(ISldWorks* m_pSldWorks)

{

 LPMODELDOC2 pModelDoc = NULL;

 // Retrieve Model Document pointer

 if( S_OK != m_pSldWorks->get_IActiveDoc2( &pModelDoc ) || pModelDoc == NULL )

  return;  

 short unitArray[5];

 HRESULT hres = S_OK;

 hres = pModelDoc->IGetUnits(unitArray);

 CString message;    // Create message string

 message.Format(_T("Unit Settings are:\n%d \t%d \t%d \t%d \t%d"),

 unitArray[0],unitArray[1],unitArray[2],unitArray[3],unitArray[4]);

 // Send message to the user

 AfxMessageBox (message);   

 // If units are not inches, change them to inches

 if (unitArray[0] != swINCHES)

 {

  short denom = 16;

  hres = pModelDoc->SetUnits(swINCHES, swFRACTION, denom, unitArray[3], FALSE);

 }

 pModelDoc->Release();

}



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 and Set Units Example (C++ COM)
*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) 2017 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.