Open Solidworks Part Document

In this post, I tell you about how to Open Solidworks Part Document using Solidworks C++ API from Visual Studio.

I hope you have setup Visual Studio community version.

If not, please go to the Solidworks C++ API – Prerequisite post and watch the suggested videos before proceeding further.


Video of Code on YouTube

Please see below video on how to Open Solidworks Part Document using Solidworks C++ API from Visual Studio.

How to Open Solidworks Part Document using Solidworks C++ API

Please note that there are no explaination in the video.

Explaination of each line and why we write code this way is given in this post.


Create a New project

Fist, we will create a new project in Visual Studio.

As shown in below image I have created a new project named OpenPartDocument in Visual Studio.

New Project Window
Figure: New Project Window

If you want to know more details about creating a new project in Visual Studio, please visit the Create a New Project section of the Solidworks C++ API – Open Solidworks & Hello World post.


Add Source file

After creating a new project, we need to Add Source file to our project.

I have already explained each step involved in creating and adding a “Source.cpp” file to the project in the Add Source File section of the Solidworks C++ API – Open Solidworks & Hello World post.

This will add “Source.cpp” file into our project.


Add References to Solidworks Type Library files

Note

⚠️ This section is copy/paste from the Solidworks C++ API – Open Solidworks & Hello World

post.

Now we need to add References to Solidworks Type Library files.

For this please follow below steps.

  1. Select the OpenSolidworkTest project and and Click Right Mouse Button (RMB).

  2. By doing this a context menu is appear as shown in below image.

  3. From this context menu, select “Properties” option, which is the last one, as shown in below image.

Open Property Window
Figure: Open Property Window

This will open a new window as shown in below image.

Project Property Window
Figure: Project Property Window

Now following below steps:

  1. Select C/C++ option

  2. Add SOLIDWORKS folders path to 2nd Red colored box as shown in below image. Usually this path is “C:\Program Files\ Solidworks Corp\SOLIDWORKS” if installed in default location.

Add SolidWorks Reference
Figure: Add SolidWorks Reference

After adding the folder path, select “Apply” button.

This complete the process of adding References to Solidworks Type Library files.


Add Code to Source file

Now we need to add to Source.cpp file.

Please copy the below code sample to your Source.cpp file.

#include <atlbase.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 main()
{
    // Initialize COM
    // Do this before using ATL smart pointers so COM is available.
    CoInitialize(NULL);

    // Use a block, so the smart pointers are destructed when the scope of this block is left
    {
        // COM Pointer of SolidWorks object
        CComPtr<ISldWorks> swApp;

        // COM Pointer of SolidWorks Model Document
        CComPtr<IModelDoc2> swDoc;

        // Variable to check if function is successful
        HRESULT result = NOERROR;

        // COM-style string for message to user
        CComBSTR messageToUser;

        // long type variable to store the result value by user
        long messageResult;

        // Create an instance of SolidWorks application
        // If it fails then return 0 and close program
        if (swApp.CoCreateInstance(__uuidof(SldWorks), NULL, CLSCTX_LOCAL_SERVER) != S_OK)
        {
            // Stop COM
            CoUninitialize();
            return 0;
        }

        // COM-style string to store document type
        CComBSTR documentType;

        // Get the default Part document template
        swApp->GetUserPreferenceStringValue(
            swUserPreferenceStringValue_e::swDefaultTemplatePart,
            &documentType
        );

        // Create a new Part document
        result = swApp->INewDocument2(documentType, 0, 0, 0, &swDoc);

        // If no default Part document is assigned
        if (result != S_OK)
        {
            messageToUser = L"Failed to open document.\nPlease try again.";

            // Send message to user
            swApp->SendMsgToUser2(
                messageToUser,
                swMessageBoxIcon_e::swMbInformation,
                swMessageBoxBtn_e::swMbOk,
                &messageResult
            );

            // Make SolidWorks visible
            swApp->put_Visible(VARIANT_TRUE);

            // Stop COM
            CoUninitialize();
            return 0;
        }

        // If created successfully, make SolidWorks visible
        swApp->put_Visible(VARIANT_TRUE);
    }

    // Stop COM
    CoUninitialize();
}

Now Build the Solution as shown in below image.

Build Solution
Figure: Build Solution

After Building Solution run the program by pressing F5.


Final Result

After running the program wait for few minute.

You will get result as shown in below image!!!

Open Part Window
Figure: Open Part Window

Issues and Solutions

There are some issues I can think of which might be come to some people.

Those are as follows:

Issue 1:

Program closed without open Solidworks!

Solution:

I you face this issue, then open Solidworks Manually one time, close it and then run the program.

It will solve this issue.


Issue 2:

Program unable to create Part Document.

Solution:

This is a tricky one!!!

If you face this issue, then you need to confirm in Solidworks that you have assigned the “default Part template”.

Please refer to below image for proper setting to fix this issue.

Default Template Locations
Figure: Default Template Locations

I hope this will helpful!!!


This is it !!!

We have completed our Open Part Document program in Solidworks using Solidworks C++ APIs.

Hope this post helps you to start with Solidworks C++ API.

If you like the post then please share it with your friends also.

Do let me know by you like this post or not! I will continue creating Solidworks C++ posts.

Till then, Happy learning!!!