SOLIDWORKS C# API - Edit Solidworks Sketch

4 minute read

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.

add-solidworks-references

add-solidworks-references


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.

add-private-fields

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.

initialize-constructor

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.

create-select-sketch-method

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
swDoc?
  • 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.
swDoc?.Extension
  • In above code, we are using Extension property of swDoc variable.
  • Extension property is part of ModelDoc2 object.
swDoc?.Extension.SelectByID2()
  • 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
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 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
    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.

ExecuteEditSketchMethod

ExecuteEditSketchOrSingleSketchFeatureMethod


FINAL RESULT

For Demo, we already had sketch we want to edit with our program.

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

sketch-for-demo

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

result-1

  • Edit Sketch with ExecuteEditSketchOrSingleSketchFeatureMethod() method

result-2


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

Updated: