Hide Table of Contents

Get Bodies Example (C++ COM)

This example shows how to get the bodies in a part document.


// -------------------------------------------------------------------

void APITestFunction()


    HRESULT                                 hr = S_OK;

    VARIANT_BOOL                            bRet = VARIANT_FALSE;

    long                                    nRetVal = -1;

    CComPtr <ISldWorks>                     pSldWorks;

    CComPtr <IModelDoc2>                    pModel;

    CComQIPtr <IPartDoc>                    pPart;

    VARIANT                                 vBodyArr;

    long                                    i = -1;

    // Connect to SolidWorks

    pSldWorks = TheApplication->GetSWApp();


    hr = pSldWorks->get_IActiveDoc2(&pModel);


    hr = pModel->ClearSelection2(VARIANT_TRUE);

    pPart = pModel;


    hr = pPart->GetBodies2(swSolidBody, VARIANT_TRUE, &vBodyArr);

    ASSERT(NULL != vBodyArr.pparray);

    // Process bodies

    SAFEARRAY*              psaBody = V_ARRAY(&vBodyArr);

    LPDISPATCH*             pBodyDispArray = NULL;

    long                    nBodyHighIndex = -1;

    long                    nBodyCount = -1;

    hr = SafeArrayAccessData(psaBody, (void **) &pBodyDispArray);

    ASSERT(S_OK == hr);

    ASSERT(NULL != pBodyDispArray);

    // Get index number of highest array element

    // The array range is from 0 to highIndex

    hr = SafeArrayGetUBound(psaBody, 1, &nBodyHighIndex);

    ASSERT(S_OK == hr);

    // Actual number of array elements is nBodyHighIndex + 1

    nBodyCount = nBodyHighIndex + 1;

    // Process each body

    for (i = 0; i < nBodyCount; i++)


        CComQIPtr <IBody2>          pBody;

        // Calls AddRef() on IDispatch ---> refcount = 2

        pBody = pBodyDispArray[i];


        hr = pBody->Select(VARIANT_TRUE, 0, &bRet);


        // After this call ---> refcount = 1

        // When it goes out of scope ---> refcount = 0



    // Unaccess & destroy the component SafeArray

    hr = SafeArrayUnaccessData(psaBody);

    hr = SafeArrayDestroy(psaBody);


} //void APITestFunction()

// -------------------------------------------------------------------

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 Bodies 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) 2012 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.