SOLIDWORKS C# API - Edit Solidworks Sketch
Objective
I want to:
-
Edit Sketch into Solidworks Part Document using Solidworks C# API
-
We will continue from previous article ๐ Edit Solidworks Sketch UI.
-
Extend article by adding Solidworks C# API to previous code.
Demo Video
Please see below video on how to โEdit Sketch into Solidworks Part 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.
- Please see below ๐๐ป image from ๐ Browse Solidworks Document article for how to add reference files.
Update ViewModel
In this section, we update MainWindowViewModel for editing Sketch.
Add Private Fields
We need 2 Private Fields as shown in below code sample.
#region Private Fields
private SldWorks.SldWorks swApp;
private ModelDoc2 swDoc;
#endregion
Please see below ๐๐ป image for reference.
private SldWorks.SldWorks swApp;
- Above code create a private field (variable) for [Solidworks application].
- Type:
SldWorks.SldWorks - Name:
swApp
private ModelDoc2 swDoc;
- Above code create a private field (variable) for [Solidworks document].
- Type:
ModelDoc2 - Name:
swDoc
Initialize Constructor
Now we initialize contructor for creating Solidworks application variable.
This open a new Solidworks application when constructor getting called.
Please see below ๐๐ป code example for reference.
#region Contructor
public MainWindowViewModel()
{
swApp = new SldWorks.SldWorks();
}
#endregion
Please see below ๐๐ป image for reference.
Create Method - SelectSketch
In this section, we create a method for selecting Sketch method.
private bool SelectSketch()
{
swDoc = null;
swDoc = swApp.ActiveDoc;
bool isSelect = swDoc?.Extension.SelectByID2("", "SKETCH", 0, 0, 0, false, 4, null, (int)swSelectOption_e.swSelectOptionDefault) ?? false;
return isSelect;
}
In above code, we create a method.
- Method Name: โ
SelectSketchโ - Return Type: โ
boolโ
Please see below ๐๐ป image for reference.
swDoc = null;
- In above code, we set Solidworks Document variable to
null. - This make sure, we did not use previous data in
swDocvariable.
swDoc = swApp.ActiveDoc;
- In above line, we set value of
swDocvariable. - For this we use
ActiveDocproperty ofswAppvariable. ActiveDocproperty, get currently open/active document in Solidworks.- After that we set the currently open/active document as value of
swDocvariable. - Reference Doc: ๐ActiveDoc Property
bool isSelect = swDoc?.Extension.SelectByID2("Circle", "SKETCH", 0, 0, 0, false, 4, null, (int)swSelectOption_e.swSelectOptionDefault) ?? false;
In above line select a sketch.
Sketch Name: โCircleโ
After selecting Sketch, we assign value of isSelect variable.
Undertstand Code
In this section we understand above code.
bool isSelect
- In above code, we create a variable.
- Variable Type:
bool - Variable Name:
isSelect
swDoc?
- Above code, checking if
swDocvariable isnullor not. - If
swDocisnull, it will return null value. - This is very helpful feature of C# programming language.
- With this feature, we eliminate the additional code for null check.
swDoc?.Extension
- In above code, we are using
Extensionproperty ofswDocvariable. Extensionproperty is part ofModelDoc2object.-
- Reference Doc: ๐Extension Property
swDoc?.Extension.SelectByID2()
- In above code, we use
SelectByID2()method. - This
SelectByID2()method use for selecting elements in Solidworks application. SelectByID2()method is accessible fromExtensionproperty ofswDocvariable. ThisSelectByID2()method takes following parameters.
| Parameter Name | Description |
|---|---|
| Name | Name of object to select or an empty string. |
| Type | Type of object (uppercase) as defined in swSelectType_e or an empty
string. |
| X | X selection location or 0 |
| Y | Y selection location or 0 |
| Z | Z selection location or 0 |
| Append | True OR
False |
| Mark | Value that you want to use as a mark; this value is used by other functions that require ordered selection. |
| Callout | Pointer to the associated callout |
| SelectOption | Selection option as defined in swSelectOption_e |
Reference Links ๐
-
swSelectType_eโก๏ธ swSelectType_e Enumeration. -
swSelectOption_eโก๏ธ swSelectOption_e Enumeration.
Return Value:
Trueif item was successfully selected,Falseif not.
Parameter Value Used:
Please see below table for parameter value we used in our code.
| Parameter Name | Parameter Value |
|---|---|
| Name | Circle |
| Type | SKETCH |
| X | 0 |
| Y | 0 |
| Z | 0 |
| Append | false |
| Mark | 4 |
| Callout | null |
| SelectOption | (int)swSelectOption_e.swSelectOptionDefault
|
?? null;
-
After the we use
??operator. -
This is called null-coalescing operator.
-
It returns the value of its left-hand operand if it isnโt
null. -
otherwise, it evaluates the right-hand operand and returns its result.
-
In our case, if left side expression is
nullthen we returnnullvalue as we defined on right side of operator.
return isSelect;
In above code, we return the value of isSelect variable.
This statement marks as end of SelectSketch() method.
Update Command Methods
We have 2 command methods.
ExecuteEditSketchMethod()ExecuteEditSketchOrSingleSketchFeatureMethod()
In both functions we have below code.
await Task.Run(() =>
{
// Sleeping with 5 sec
Thread.Sleep(5000);
});
Now, we need to replace above code with below code.
await Task.Run(() =>
{
if (SelectSketch())
swDoc.EditSketch();
});
Please see below ๐๐ป images for reference.
FINAL RESULT
For Demo, we already had sketch we want to edit with our program.
Please see below ๐๐ป image for reference.
Now, we have done everything needed to Edit Solidwork Sketch into Solidworks Document through our application.
Please see below image for final result of our work.
- Edit Sketch with
ExecuteEditSketchMethod()method
- Edit Sketch with
ExecuteEditSketchOrSingleSketchFeatureMethod()method
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 Edit Solidworks Sketch 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!!!









