Solidworks C++ API - Open Solidworks & Hello World

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

I hope you have setup Visual Studio community version.

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


Content

This post is divided into following sections.

Feel free to select the topic you want to.


Video of Code on YouTube

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


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.

There are 3 different ways for creating a new project.

  1. From File -> New -> Project

  2. From New Prject icon.

  3. Keyboard Short-cut i.e. Ctrl+Shift+N.

Below image show how to create a New Project from File option:

new-project-file-option

In above image, see Red color box.

Below image show how to create a New Project from New Project Icon option:

new-project-from-icon

In above image, see Red color box.

When we select one of the above option we get a new window which is shown in below.

create-project

In above image I have numbered the Red colored box.

These numbers are explained below:

  1. The programming language you want to use for New Project. For our purpose, we use “Visual C++”.

  2. It is, which type of project you want to create. There are 3 different type of projects we can create. In above image, we will create an empty project.

  3. It is the name of project we want to create. We named our project as OpenSolidworkTest.

  4. The location of project we want. We use default location provided in above image.

  5. It is option if we want to create a Solution file for this project or not. In our case, we want to create a Solution file.

  6. Hit Ok button after completing all fields.


Add Source file

After creating a new project, we get a screen as shown in below image.

after-new-project

This project has no file to write.

Now we add a cpp file into Source Files filter folder.

For this please follow given steps:

  1. For this select Source Files filter folder 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 “Add” –> “New Item”, as shown in below image.

add-new-cpp-file

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

add-new-cpp-file-window

Just select “Add” option as shown in above image.

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


Add References to Solidworks Type Library files

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

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

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-solidowrks-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.cpp 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 Soldiworks object
		CComPtr<ISldWorks> swApp;

		// 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);
		}

		// If created successfully, then visible the Solidworks
		swApp->put_Visible(VARIANT_TRUE);

		// COM Style String for message to user
		CComBSTR _messageToUser(L"Hello World!!! I am from Solidworks C++ API.");

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

		// Send a message to user and store the return value in _lMessageResult by referencing it
		swApp->SendMsgToUser2(_messageToUser, swMessageBoxIcon_e::swMbInformation, swMessageBoxBtn_e::swMbOk, &_lMessageResult);
	}

	// Stop COM 
	CoUninitialize();
}

Now Build the Solution as shown in below image.

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!!!

hello-world-message.png


This is it !!!

We have completed our Hello World 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!!!