SOLIDWORKS C# API - Browse/Open SOLIDWORKS Document

4 minute read

OBJECTIVE

I want to:

  • Open browsed SOLIDWORKS Document using Solidworks C# API

  • We will continue from previous article ๐Ÿš€ Browse Solidworks Document UI.

  • Extend article by adding Solidworks C# API to previous code.


RESULT WE GET

Below image shows the result we get.

browse-and-open-solidworks-file

To get the correct result, please follow the steps correctly.


DEMO VIDEO

Please see below video on how to โ€œOpen browsed SOLIDWORKS Documentโ€ using SOLIDWORKS C# API from WPF Application.


Please note that there are no explanation in the video.
Explanation of each step and why we write code this way is given in this post.


ADD SOLIDWORKS REFERENCES

  • For opening SOLIDWORKS we need to add some references into our project.

  • Please see below image for how to add SOLIDWORKS reference.

add-solidworks-references

CHECK BEFORE OPEN DOCUMENT

In this section we apply some checks.

  • First, we check if we have a file path, which we browsed.

  • Please see below ๐Ÿ‘‡๐Ÿป image for reference.

check-we-browsed-a-file

  • Please see below ๐Ÿ‘‡๐Ÿป code sample for reference.
// Check if we browsed a file
if (string.IsNullOrEmpty(FilePath))
    return;
  • In above code, we are checking, if we browsed a file.

  • Condition: string.IsNullOrEmpty(FilePath)

    • Browsed file path is saved into FilePath variable.

    • Please see previous article ๐Ÿš€ Browse Solidworks Document UI for more details.

    • If we donโ€™t browse any file, then FilePath variable did not have any value.

    • We check if value of FilePath variable is null or empty by IsNullOrEmpty() method.

    • When this condition is true, means we donโ€™t have any file path, we exit the function from here.

  • Now we check if browsed file is exist in at said location.

  • Please see below ๐Ÿ‘‡๐Ÿป image for reference.

check-file-exist

  • Please see below ๐Ÿ‘‡๐Ÿป code sample for reference.
// Check if browsed file exist or not
if (File.Exists(FilePath) == false)
    return;
  • Condition: File.Exists(FilePath) == false

    • Browsed file path is saved into FilePath variable.

    • Please see previous article ๐Ÿš€ Browse Solidworks Document UI for more details.

    • We check if browsed file is exist or not.

    • We do this by Exists method. This Exists method is part of File class.

    • This Exists method take path of file as parameter.

    • If file exist, then it returns true. If file does not exis, then return false.

    • We are comparing that return value Exists method equals to false.

    • When this condition is true, means we donโ€™t have browsed file hence we exit the function from here.

OPEN SOLIDWORKS DOCUMENT

In this section, we open browsed Solidworks document.

  • Create a new function OpenSolidworksDocument().

  • Please see below ๐Ÿ‘‡๐Ÿป image for reference.

add-opensolidworksdocument-function

  • Please see below ๐Ÿ‘‡๐Ÿป code sample for reference.
private void OpenSolidworksDocument()
{

}
  • Now we call this OpenSolidworksDocument() function in ExecuteClickCommand() method.

  • Please see below ๐Ÿ‘‡๐Ÿป image for reference.

call-new-function

  • Please see below ๐Ÿ‘‡๐Ÿป code sample for reference.
async void ExecuteClickCommand()
{
    // Show busy indicator
    IsBusy = true;

    await Task.Run(() =>
    {
        OpenSolidworksDocument();
    });

    // Hide busy indicator
    IsBusy = false;
}
  • Now, we get browsed file extension.

  • Please see below ๐Ÿ‘‡๐Ÿป image for reference.

get-browsed-file-info

  • Please see below ๐Ÿ‘‡๐Ÿป code sample for reference.
private void OpenSolidworksDocument()
{
    FileInfo fileInfo = new FileInfo(FilePath);
}
  • In above line, we store file information in fileInfo variable.

  • Type of fileInfo variable is FileInfo.

  • When we create fileInfo variable, we pass browsed file path.

  • We want to get information of this browsed file.

  • Now we want to get the file extension from this fileInfo variable.

  • Then we store this file extension in a string type variable.

  • Please see below ๐Ÿ‘‡๐Ÿป image for reference.

get-file-extension

  • Please see below ๐Ÿ‘‡๐Ÿป code sample for reference.
private void OpenSolidworksDocument()
{
    FileInfo fileInfo = new FileInfo(FilePath);

    string fileExtension = fileInfo.Extension;
}
  • Now we will get the document type of selected file.

  • For this we will check the file extension, we get previously in fileExtension variable.

  • For this, first we create a new variable docType of int data type.

  • Please see below ๐Ÿ‘‡๐Ÿป image for reference.

add-data-type-variable

  • Please see below ๐Ÿ‘‡๐Ÿป code sample for reference.
private void OpenSolidworksDocument()
{
    FileInfo fileInfo = new FileInfo(FilePath);

    string fileExtension = fileInfo.Extension;

    int docType = default(int);
}
  • We set the value of docType variable to Solidworks Document type we have browsed.

  • Please see below ๐Ÿ‘‡๐Ÿป image for reference.

get-browsed-document-type

  • Please see below ๐Ÿ‘‡๐Ÿป code sample for reference.
if (string.Compare(fileExtension, ".sldprt", true) == 0)
    docType = (int)swDocumentTypes_e.swDocPART;
else if (string.Compare(fileExtension, ".sldasm", true) == 0)
    docType = (int)swDocumentTypes_e.swDocASSEMBLY;
else
    docType = (int)swDocumentTypes_e.swDocDRAWING;
  • In above code sample, we check for 3 different conditions as follows.

    1. First we check if fileExtension is .sldprt. If this condition is true, then we set the value of docType variable to (int)swDocumentTypes_e.swDocPART.

    2. After that we check if fileExtension is .sldasm. If this condition is true, then we set the value of docType variable to (int)swDocumentTypes_e.swDocASSEMBLY.

    3. Lastly if above any codition did not get true, then we set the value of docType variable to (int)swDocumentTypes_e.swDocASSEMBLY.

  • Now we open browsed file in Solidworks.

  • Please see below ๐Ÿ‘‡๐Ÿป image for reference.

open-solidworks-document

  • Please see below ๐Ÿ‘‡๐Ÿป code sample for reference.
SldWorks.SldWorks swApp = new SldWorks.SldWorks();

swApp.OpenDoc(FilePath, docType);

swApp.Visible = true;
  • In above code, we create Solidworks application variable.

  • Then, we open browsed document.

  • After opening document, we make Solidworks visible.

FINAL RESULT

Now, we have done everything needed to Open SOLIDWORKS Document through our application.

Please see below image for final result of our work.

final-result


This is it !!!

I hope my efforts will helpful to someone!

If you found anything to add or update, please let me know on my e-mail.

Hope this post helps you to Open SOLIDWORKS Documents from WPF PRISM Application.

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

Do let me know by you like this post or not!

Till then, Happy learning!!!

Updated: