SOLIDWORKS C# API - Edit Solidworks Sketch

4 minute read


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.



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;


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();


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 swDoc variable.
swDoc = swApp.ActiveDoc;
  • In above line, we set value of swDoc variable.
  • For this we use ActiveDoc property of swApp variable.
  • ActiveDoc property, get currently open/active document in Solidworks.
  • After that we set the currently open/active document as value of swDoc variable.
  • 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
  • Above code, checking if swDoc variable is null or not.
  • If swDoc is null, 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.
  • In above code, we are using Extension property of swDoc variable.
  • Extension property is part of ModelDoc2 object.
  • In above code, we use SelectByID2() method.
  • This SelectByID2() method use for selecting elements in Solidworks application.
  • SelectByID2() method is accessible from Extension property of swDoc variable. This SelectByID2() 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 ๐Ÿš€

Return Value:

  • True if item was successfully selected, False if not.

Parameter Value Used:

Please see below table for parameter value we used in our code.

Parameter Name Parameter Value
Name Circle
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 null then we return null value 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.

  1. ExecuteEditSketchMethod()
  2. ExecuteEditSketchOrSingleSketchFeatureMethod()

In both functions we have below code.

await Task.Run(() =>
    // Sleeping with 5 sec

Now, we need to replace above code with below code.

await Task.Run(() =>
    if (SelectSketch())

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




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