SOLIDWORKS C# API - Open SOLIDWORKS

3 minute read

OBJECTIVE

How to Open SOLIDWORKS using SOLIDWORKS C# API from WPF Prism Desktop Application.

I hope you have installed Visual Studio Community Edition on your machine.

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


DEMO VIDEO

Please see below video on how to Open SOLIDWORKS using SOLIDWORKS C# API from WPF Prism Desktop Application.


Please note that there are no explanation in the video.

Explanation of each step/line and why we write code this way is given in this post.


CREATE A NEW PRISM PROJECT

In the below image I have shown you how to create a new Prism Template project.

create-prism-project

For creating a new Prism Project follow these steps:

  • Open Visual Studio 2019 (or 2017). This bring us a new window as shown in below image.

    open-visual-studio-2019

  • In this window, we need to select “Create a new project” option as shown in below image.

    select-create-a-new-project-option

  • This will open a new window of “Create a new project” as shown in below image.

    create-a-new-project-window

  • In this window, we need to apply filter for “Prism” project by type “Prism” in filter section as shown in below images.

    filter-prism-project

  • After applying this filter ‘select’ “Prism Blank App (WPF)” option from list and press “Next” as shown in below image.

    select-prism-blank-app-option

  • This will open a new window of “Configure your new project” as shown in below image.

    configure-your-project-window

  • In this new window, we define “Project Name”. For project name we used “OpenSolidworks” value and press “Create” as shown in below image.

    update-project-name-and-create-project

  • Pressing “Create” button open a new window of “PRISM PROJECT WIZARD”. In this window select “CREATE PROJECT” button as shown in below image.

    prism-project-wizard

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

openSolidworks-window

BUILD SOLUTION

After we create our “OpenSolidworks” project, we need to select “Build Solution” option.

We build our solution because we want to make sure everything is working and there are no broken references.

Please see below image for reference.

build-solution

Below image show MainWindow.xaml file before building solution.

before-build-solution

Below image show MainWindow.xaml file after building solution.

after-build-solution

ADD BUTTON TO WINDOW

To add button in window, we need to update MainWindow.xaml file as shown in below image.

create-button-xaml

Understand what we have changed.

WindowStartupLocation="CenterScreen"

Above code help us show window at center of screen.

<Button Content="Open Solidworks"
        Width="250"
        Height="100" />

Above code add button to our window.

In above code we set 3 properties as follows:

  1. Content="Open Solidworks"

  2. Width="250"

  3. Height="100"

Below image shows above parameters.

button-parameters

ADD COMMAND TO VIEWMODEL

Now we need to add Prism Command to our MainWindowViewModel.cs file as shown in below image.

create-button-xaml

In MainWindowViewModel.cs file, we add Prism Command by cmd code snippet.

Using this code snippet, we add following code.

private DelegateCommand _OpenSolidworksCommand;
public DelegateCommand OpenSolidworksCommand =>
    _OpenSolidworksCommand ?? (_OpenSolidworksCommand = new DelegateCommand(ExecuteOpenSolidworksCommand));

void ExecuteOpenSolidworksCommand()
{

}

In above code, we use DelegateCommand Prism Command from Prism.Command namespace.

Please forgive me, I am not able to explained Prism Command correctly.

private DelegateCommand _OpenSolidworksCommand;
public DelegateCommand OpenSolidworksCommand =>
    _OpenSolidworksCommand ?? (_OpenSolidworksCommand = new DelegateCommand(ExecuteOpenSolidworksCommand));

Above line of code shows Prism Commands.

For more detail about Prism Commands please visit below video.


void ExecuteOpenSolidworksCommand()
{

}

Above code shows function we execute when command is run.

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

OPEN SOLIDWORKS

Now for opening SOLIDWORKS we need to add following code as shown in below image.

void ExecuteOpenSolidworksCommand()
{
    // Create a new SOLIDWORKS instance
    SldWorks.SldWorks swApp = new SldWorks.SldWorks();

    // Make solidworks visible
    swApp.Visible = true;
}

open-solidworks-code

ADD COMMAND TO BUTTON

Now we need to Bind our OpenSolidworksCommand command to button as shown in below image.

add-command-to-button

FINAL RESULT

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

Please see below image for final result of our work.

final-result

As you can see from above image, we have done followings:

  • Build Solution as shown in below image.

build-solution

  • Start app as shown in below image.

run-application-by-start-button

  • By Start app we have a window, in this window we need to press “Open Solidworks” button as shown in below image.

Open-Solidworks


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