SOLIDWORKS C# API - Edit Solidworks Sketch

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.

Edit Sketch into Solidworks Part Document

Important

Please note that there are no explanations in the video.
Explanation of each step and why we write the code this way is provided 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
Figure: Add SOLIDWORKS References
Add SOLIDWORKS References
Figure: 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
Figure: 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
Figure: 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
Figure: 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.
SelectByID2 Parameters
Parameter NameDescription
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.

SelectByID2 Parameter Values Used
Parameter NameParameter 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.

Execute Edit Sketch Method
Figure: Execute Edit Sketch Method
Execute Edit Sketch Or Single Sketch Feature Method
Figure: Execute Edit Sketch Or Single Sketch Feature Method

FINAL RESULT

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

Please see below πŸ‘‡πŸ» image for reference.

Sketch for Demo
Figure: 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 Demo
Figure: Result Demo
  • Edit Sketch with ExecuteEditSketchOrSingleSketchFeatureMethod() method
Result Demo 2
Figure: Result Demo 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!!!