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 then please go to 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.
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.
If you want to know more detail for Creating a New Project in Visual Studio, please visit Create a New project section of 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 explain each steps taken to create/add a “Source.cpp” into our project in Add Source file section of Solidworks C++ API - Open Solidworks & Hello World post.
This will add “Source.cpp” file into our project.
Add References to Solidworks Type Library files
This section is copy/paste from 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.
-
Select the OpenSolidworkTest project and and Click Right Mouse Button (RMB).
-
By doing this a context menu is appear as shown in below image.
-
From this context menu, select “Properties” option, which is the last one, as shown in below image.
This will open a new window as shown in below image.
Now following below steps:
-
Select C/C++ option
-
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.
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 Soldiworks object
CComPtr<ISldWorks> swApp;
// COM Pointer of Soldiworks Model Document
CComPtr<IModelDoc2> swDoc;
// Variable to check if function is
HRESULT result = NOERROR;
// COM Style String for message to user
CComBSTR _messageToUser;
// long type variable to store the result value by user
long _lMessageResult;
// 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
swApp->GetUserPreferenceStringValue(swUserPreferenceStringValue_e::swDefaultTemplatePart, &_documentType);
// Create a new Part Document
result = swApp->INewDocument2(_documentType, 0, 0, 0, &swDoc);
// If there are no Default Part document assign then show a message to user
// and Stop COM, Visible the Solidworks and return the function
if (result != S_OK)
{
// COM Style String for message to user
_messageToUser = (L"Failed to open document.\nPlease try again.");
// 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);
// Visible the Solidworks
swApp->put_Visible(VARIANT_TRUE);
// Stop COM
CoUninitialize();
return(0);
}
// If created successfully, then visible the Solidworks
swApp->put_Visible(VARIANT_TRUE);
}
// Stop COM
CoUninitialize();
}
Now Build the Solution as shown in below image.
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!!!
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.
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!!!