SOLIDWORKS C# API - Change Sketch Name

6 minute read

Objective

I want to:

  • Change Selected Sketch Name into Solidworks Part Document using Solidworks C# API

  • We will continue from previous article ๐Ÿš€ Change Sketch Name UI.


Demo Video

Please see below video on how to โ€œChange Selected Sketch Name 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 Constructor

In this section we add Constructor to our view model class.

Please see below code for Constructor.

public MainWindowViewModel()
{
    
}
  • In above constructor, we will pass Prismโ€™s IEventAggregator parameter as shown below.
public MainWindowViewModel(IEventAggregator eventAggregator)
{
    
}

Resolving of IEventAggregator eventAggregator parameter is done by Unity.

  • Now we need to assign this parameter value to private field.
  • For this we need a private field as shown below.
private readonly IEventAggregator eventAggregator;
  • After creating this field we need to set the value of this field as shown below.
public MainWindowViewModel(IEventAggregator eventAggregator)
{
    this.eventAggregator = eventAggregator;
}
  • Full code will look like below.
private readonly IEventAggregator eventAggregator;
public MainWindowViewModel(IEventAggregator eventAggregator)
{
    this.eventAggregator = eventAggregator;
}

Add Private Fields

Now we need some private fields.

These fields are listed below.

private SldWorks.SldWorks swApp;

private ModelDoc2 swDoc;

private string messageToShow;

1st field -

  • Name = swApp
  • Type = SldWorks.SldWorks

2nd field -

  • Name = swDoc
  • Type = ModelDoc2

3rd field -

  • Name = messageToShow
  • Type = string

Update Method - ExecuteChangeNameCommand

In this section, we update ExecuteChangeNameCommand method.

  • First we replace below ๐Ÿ‘‡๐Ÿป code from ExecuteChangeNameCommand method.
await Task.Run(() =>
{
    // Sleeping with 5 sec
    Thread.Sleep(5000);
});
  • Then we use below code.
bool result = await Task.Run(ChangeSketchName);

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 ChangeSketchName method.
  4. In above code, we use await keyword. This will wait for completion of ChangeSketchName method.

Since ChangeSketchName method did not exist hence we need to create a new method as shown below ๐Ÿ‘‡๐Ÿป.

bool ChangeSketchName()
{
    return true;
}
  • In above code ChangeSketchName() method return true as default value.
  • In ExecuteChangeNameCommand() 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 ChangeSketchName() method.

Update Method - ChangeSketchName

  • Now we need to update ChangeSketchName() method for changing selected Sketch name.
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 isSketchRenamed = swDoc.SelectedFeatureProperties(0, 0, 0, 0, 0, 0, 0, true, false, SelectedName);
  • In above code, we change the name of selected Sketch.
  • For changing the name, we use SelectedFeatureProperties() method.
  • SelectedFeatureProperties() method is part of swDoc variable.
  • SelectedFeatureProperties() method takes following parameters.
    • RgbColor: Color of feature.
    • Ambient: 0.0 <= Ambient value <= 1.0.
    • Diffuse: 0.0 <= Diffuse value <= 1.0.
    • Specular: 0.0 <= Specular value <= 1.0.
    • Shininess: 0.0 <= Shininess value <= 1.0.
    • Transparency: 0.0 <= Transparency value <= 1.0.
    • Emission: 0.0 <= Emission value <= 1.0.
    • UsePartProps: true if the feature inherits the properties from the part, false if not.
    • Suppressed: true if the feature is suppressed, false if not.
    • FeatureName: Name of the feature.
  • Return Value : true if featureโ€™s properties are successfully set, false if not.

  • In our code, I have used following values:

    Parameter Name Value Used
    RgbColor 0
    Ambient 0
    Diffuse 0
    Specular 0
    Shininess 0
    Transparency 0
    Emission 0
    UsePartProps true
    Suppressed false
    FeatureName SelectedName
  • We store return value of SelectedFeatureProperties() method in isSketchRenamed variable.
  • isSketchRenamed variable Type : bool
if (isSketchRenamed == false)
{
    messageToShow = "Failed to rename selected sketch.";
    return false;
}
  • We check value of isSketchRenamed variable and successfully changed Sketch Name or not.
  • In above code, we are checking value of isSketchRenamed variable and successfully changed Sketch Name or not.
  • We are checking if swDoc variable is true or false?
  • If value is false, then we will execute code inside this if condition.
messageToShow = "Failed to rename selected sketch.";
return false;
  • Above code execute when value of isSketchRenamed variable is false .
  • In above code, we are setting value of messageToShow variable to "Failed to rename selected sketch.".
  • This variable will be use to show messages to user.
  • After this we return false and exit the ChangeSketchName() method.
messageToShow = "Selected Sketch is renamed.";
return true;
  • Above code execute when value of isSketchRenamed 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 renamed.".
  • This variable will be use to show messages to user.
  • After this we return true and exit the ChangeSketchName() method.

FINAL RESULT

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

Now, we have done everything needed to Change Sketch Name 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 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: