SOLIDWORKS C# API - Delete Selected Sketch

4 minute read

Objective

I want to:

  • Delete Selected Sketch into Solidworks Part Document using Solidworks C# API

  • We will continue from previous article 🚀 Delete Selected Sketch UI.


Demo Video

Please see below video on how to “Delete Selected 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.


Update Method - ExecuteDeleteCommand

In this section, we update ExecuteDeleteCommand method.

  • First we replace below 👇🏻 code from ExecuteDeleteCommand method.
await Task.Run(() =>
{
    // Sleeping with 2 sec
    Thread.Sleep(2000);
});
  • Then we use below code.
bool result = await Task.Run(DeleteSketch);

In above code, we are doing following.

  1. Create a bool type variable.
  2. Name of this variable = result
  3. Value of this result variable is set by DeleteSketch method.
  4. In above code, we use await keyword. This will wait for completion of DeleteSketch method.

Since DeleteSketch method did not exist hence we need to create a new method as shown below 👇🏻.

bool DeleteSketch()
{
    return true;
}
  • In above code DeleteSketch() method return true as default value.
  • In ExecuteDeleteCommand() method, we will add following code.
if (result)
    this.eventAggregator.GetEvent<InformationMessagesService>().Publish(messageToShow);
else
    this.eventAggregator.GetEvent<ErrorMessagesService>().Publish(messageToShow);
  • In above code, we are checking value of result variable.
  • If value of result variable = true
  • Then we will show an Information message.
  • For showing message we use this.eventAggregator field.
  • This field get InformationMessagesService class and publish a message.
  • If value of result variable = false
  • Then we will show an Error message.
  • For showing message we use this.eventAggregator field.
  • This field get ErrorMessagesService class and publish a message.
  • In both cases message depend upon messageToShow variable.
  • Value of this messageToShow variable comes from DeleteSketch() method.

Update Method - DeleteSketch

  • Now we need to update DeleteSketch() method for deleting selected Sketch.
swApp = new SldWorks.SldWorks();
  • In above code we are setting value of swApp variable to new SldWorks.SldWorks() type.
  • Reference: Please visit 🚀 online SOLIDWORKS API Help.
if (swApp == null)
{
    messageToShow = "Failed to find Solidworks application.";
    return false;
}
  • In above code, we are checking value of swApp variable.
  • We are checking if swApp variable is null or not?
  • If value is null, then we will execute code inside this if condition.
messageToShow = "Failed to find Solidworks application.";
return false;
  • Above code execute when value of swApp variable is null .
  • In above code, we are setting value of messageToShow variable to "Failed to find Solidworks application.".
  • This variable will be use to show messages to user.
  • After this we return false and exit the ChangeSketchName() method.
swDoc = swApp.ActiveDoc;
  • In above code we are setting value of swDoc variable to new swApp.ActiveDoc property.
  • This ActiveDoc property is part of swApp variable.
  • If there is NO document active then this ActiveDoc property return null.
  • Reference: Please visit 🚀 online SOLIDWORKS API Help.
if (swDoc == null)
{
    messageToShow = "Failed to get Solidworks document.";
    return false;
}
  • In above code, we are checking value of swDoc variable.
  • We are checking if swDoc variable is null or not?
  • If value is null, then we will execute code inside this if condition.
messageToShow = "Failed to get Solidworks document.";
return false;
  • Above code execute when value of swDoc variable is null .
  • In above code, we are setting value of messageToShow variable to "Failed to get Solidworks document.".
  • This variable will be use to show messages to user.
  • After this we return false and exit the ChangeSketchName() method.
bool result = swDoc.Extension.DeleteSelection2((int)swDeleteSelectionOptions_e.swDelete_Children);
  • In above code, we delete selected Sketch.
  • For changing the name, we use DeleteSelection2() method.
  • DeleteSelection2() method is part of Extension property.
  • Extension property is part of swDoc variable.
  • DeleteSelection2() method takes following parameters.
    • DeleteOptions: Options as defined in swDeleteSelectionOptions_e.

      Member Description
      swDelete_Absorbed 2
      swDelete_Advanced 4
      swDelete_Children 1
  • Return Value : true if selected item is deleted, false if not.

  • In our code, I have used following values:

    Parameter Name Value Used
    DeleteOptions swDeleteSelectionOptions_e.swDelete_Children
  • We store return value of DeleteSelection2() method in result variable.
  • result variable Type : bool
if (result == false)
{
    messageToShow = "Failed to delete selected sketch.";
    return false;
}
  • We check value of result variable and successfully delete Selected Sketch or not.
  • In above code, we are checking value of result variable and successfully deleted Sketch or not.
  • We are checking if result variable is true or false?
  • If value is false, then we will execute code inside this if condition.
messageToShow = "Failed to delete selected sketch.";
return false;
  • Above code execute when value of result variable is false .
  • In above code, we are setting value of messageToShow variable to "Failed to delete selected sketch.".
  • This variable will be use to show messages to user.
  • After this we return false and exit the DeleteSketch() method.
messageToShow = "Selected Sketch is deleted.";
return true;
  • Above code execute when value of result variable is true and did not execute code inside if condition.
  • In above code, we are setting value of messageToShow variable to "Selected Sketch is deleted.".
  • This variable will be use to show messages to user.
  • After this we return true and exit the DeleteSketch() method.

FINAL RESULT

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

Please see below 👇🏻 image for reference.

feature-sketch-we-want-to-delete

Now, we have done everything needed to Delete Solidworks Sketch into Solidworks Document through our application.

Please see below image for final result of our work.

result


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 Delete Selected 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: