SOLIDWORKS C# API - Open SOLIDWORKS

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.

Open SOLIDWORKS using SOLIDWORKS C# API

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
Figure: 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
Figure: 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
Figure: 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
Figure: 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
Figure: 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
Figure: 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
Figure: 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
Figure: 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
Figure: Prism Project Wizard

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

Open SOLIDWORKS Window
Figure: Open SOLIDWORKS 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
Figure: Build Solution

Below image show MainWindow.xaml file before building solution.

Before Build Solution
Figure: Before Build Solution

Below image show MainWindow.xaml file after building solution.

After Build Solution
Figure: 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
Figure: 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
Figure: 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
Figure: 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.

Clarification

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.

(Add your video caption here)

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
Figure: 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
Figure: 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
Figure: 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
Figure: Final Result

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

  • Build Solution as shown in below image.
Build Solution
Figure: Build Solution
  • Start app as shown in below image.
Run Application by Start Button
Figure: 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
Figure: 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!!!