SOLIDWORKS C# API - Browse/Open SOLIDWORKS Document
OBJECTIVE
I want to:
-
Browse and Open an SOLIDWORKS Document using
- SOLIDWORKS C# API
- Syncfusion WPF UI Controls
- WPF Prism MVVM Framework
RESULT WE GET
Below image shows the result we get.
To get the correct result, please follow the steps correctly.
DEMO VIDEO
Please see below video on how to “Browse and Open SOLIDWORKS Document” using SOLIDWORKS C# API from Desktop 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.
CREATE A NEW PRISM PROJECT
Create a new Prism project as shown below 👇🏻.
-
All the steps has been already explained in 🚀 SOLIDWORKS C# API - Open SOLIDWORKS article.
-
This will open a new window as shown in below image 👇🏻.
BUILD SOLUTION
After we create our “BrowseAndOpenSolidworksDocument” project, we need to select “Build Solution” option as shown in below image.
- Select “Build” Option from application “Menu”.
- Select “Build Solution” option.
WHY WE BUILD SOLUTION ?
We build our solution because we want to make sure everything is working and there are no broken references.
Below image show MainWindow.xaml
file before building solution.
Below image show MainWindow.xaml
file after building solution.
ADD NUGET PACKAGES
Now we need to add following NuGet packages:
- Syncfusion.Tools.WPF
- Syncfusion.Shared.WPF
- Syncfusion.SfSkinManager.WPF
- Syncfusion.Themes.FluentLight.WPF
- Syncfusion.UI.WPF.NET
Steps to Add NuGet packages:
- Open NuGet Package Manager by 1) Right Click on “Dependencies” then 2) Select highlighted option as shown in below image.
- A new screen open as shown below.
- Search and Add “Syncfusion.Tools.WPF” package as shown below.
- In similar way, we add remaining packages. List of added Nuget Packages are shown below.
ADD USER INTERFACE CONTROLS
Below we add some UI control for user interaction.
UPDATE WINDOW START-UP LOCATION AND HEIGHT/WIDTH
In below image, we update ‘Start-up location’ and ‘Width & Height’ of our window.
First we update Start-up location of window by adding following line.
WindowStartupLocation="CenterScreen"
After this, we update window’s Height and Width to following values.
Height="250" Width="500"
REMOVE CONTENT CONTROL
Now we will do:
- Add below 👇🏻 Syncfusion namespaces to our window.
xmlns:syncfusion="http://schemas.syncfusion.com/wpf"
syncfusion:SfSkinManager.Theme="{syncfusion:SkinManagerExtension ThemeName=FluentLight}"
- In below image we remove
ContentControl
Tag in Grid. Also, we changeGrid
toSfBusyIndicator
from Syncfusion library for helding our UI Controls.
UPDATE BUSY INDICATOR
Now, we update properties of Busy Indicator as shown below.
<syncfusion:SfBusyIndicator IsBusy="{Binding ShowIndicator,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
Header="Loading..."
AnimationType="Gear">
</syncfusion:SfBusyIndicator>
ADD GRID TO HOLD CONTROLS
In below image, we add Grid
to hold our UI controls.
Grid
with Row and Column definition
is given below.
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="15" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="15" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="15" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="15" />
<RowDefinition Height="Auto" />
<RowDefinition Height="15" />
<RowDefinition Height="*" />
<RowDefinition Height="15" />
</Grid.RowDefinitions>
</Grid>
ADD TEXTBOX FOR FILE PATH
In below image we add syncfusion:SfTextBoxExt
inside Grid
for showing selected document file path.
syncfusion:SfTextBoxExt
with Text
and other properties are given below.
<syncfusion:SfTextBoxExt Grid.Row="1"
Grid.Column="1"
TextWrapping="WrapWithOverflow"
TextAlignment="Center"
Text="{Binding FilePath, Mode=TwoWay}">
</syncfusion:SfTextBoxExt>
ADD COMBOBOX FOR DOCUMENTS LIST
In below image we add ComboBox
inside StackPanel
for holding our list of SOLIDWORKS documents.
ComboBox
with set properties are given below.
<ComboBox Width="350"
Height="30"
Margin="10"
VerticalAlignment="Center"
FontSize="16" />
ADD BUTTON FOR SELECTED DOCUMENT
In below image we add Button
to open selected SOLIDWORKS document.
Button
with Content
and other properties are given below.
<Button Width="350"
Height="50"
FontSize="18"
FontWeight="Medium"
Content="Open Solidworks" />
UPDATE VIEWMODEL
Now, we update our MainWindowViewModel
viewmodel, for showing data and adding functionalities.
ADD DOCUMENTS LIST
In below image we a list of SOLIDWORKS document in MainWindowViewModel
.
For this we use below code.
private ObservableCollection<string> _DocumentsList;
public ObservableCollection<string> DocumentsList
{
get { return _DocumentsList; }
set { SetProperty(ref _DocumentsList, value); }
}
In above code, _DocumentsList
is private member of our MainWindowViewModel
class, whose value we set in the Constructor
of MainWindowViewModel
class.
DocumentsList
will use for Binding
document list to our ComboBox
as ItemSource
.
Here we use ObservableCollection<T>
because of MVVM.
For more details please visit this link.
public MainWindowViewModel()
{
_DocumentsList = new ObservableCollection<string>
{
"Part Document",
"Assembly Document",
"Drawing Document"
};
}
In above code, we add SOLIDWORKS documents into our _DocumentsList
list.
BINDING DOCUMENT LIST TO COMBOBOX
In below image we Bind our document list i.e. DocumentsList
to ComboBox
as ItemSource
.
For Binding DocumentsList
we add following line.
ItemsSource="{Binding DocumentsList}"
After this update our ComboBox looks like as:
<ComboBox Width="350"
Height="30"
Margin="10"
VerticalAlignment="Center"
FontSize="16"
ItemsSource="{Binding DocumentsList}"/>
When we Run
our code, we get following window.
As I have mentioned in above image, if there are no item selected, we will get error when we click “Open Solidworks” button.
To avoid this error we define SelectedIndex
property of ComboBox
to 0.
After this update our ComboBox looks like as:
<ComboBox Width="350"
Height="30"
Margin="10"
VerticalAlignment="Center"
FontSize="16"
SelectedIndex="0"
ItemsSource="{Binding DocumentsList}"/>
When we Run
our code, we get following window.
ADD SELECTED VALUE IN COMBOBOX
In our program, we want to open selected SOLIDWORKS Document.
To get the selected value, we need a property i.e. SelectedDocument
in our MainWindowViewModel
ViewModel and bind this property to SelectValue
property of ComboBox
.
For more details please see below image.
ADD COMMAND TO VIEWMODEL
In our application to open selected SOLIDWORKS document, we need add a Command to our button.
For this we need to do following:
-
We need to create a Prism Command i.e.
OpenSolidworksCommand
inMainWindowViewModel
ViewModel. -
Bind this
OpenSolidworksCommand
to our button.
In below we see how to do this, also we checked the selected value.
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.
OPEN SOLIDWORKS DOCUMENT
Now for opening SOLIDWORKS Document we need to add following code as shown in below image.
void ExecuteOpenSolidworksCommand()
{
// Create a new Instance of Solidworks Application
SldWorks.SldWorks swApp = new SldWorks.SldWorks();
// Make Solidworks visible
swApp.Visible = true;
// Variable to hold selected document's template path
string templatePath = string.Empty;
// Switch Conditional Statement
switch (SelectedDocument)
{
case "Part Document":
// Get default Part template path
templatePath = swApp.GetUserPreferenceStringValue((int)SwConst.swUserPreferenceStringValue_e.swDefaultTemplatePart);
break;
case "Assembly Document":
// Get default Assembly template path
templatePath = swApp.GetUserPreferenceStringValue((int)SwConst.swUserPreferenceStringValue_e.swDefaultTemplateAssembly);
break;
case "Drawing Document":
// Get default Drawing template path
templatePath = swApp.GetUserPreferenceStringValue((int)SwConst.swUserPreferenceStringValue_e.swDefaultTemplateDrawing);
break;
}
// Create a new Document as ModelDoc2 object
SldWorks.ModelDoc2 swDoc = swApp.NewDocument(templatePath, 0, 0, 0);
}
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.
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!!!