SOLIDWORKS C# API - Update Dimension State

4 minute read

This is a miscellaneous type article. I am writing this article for future reference and will write a proper article in future.

Objective

I want to:

  • Open Solidworks from WPF Application.
  • Select a particular dimension
  • Check Dimension state if it Driven dimension.
  • If it is Driven dimension then update the state of dimension.

Macro Video

Below 🎬 video shows demo of update Dimension State from WPF Application.


Above video is just for visualization and there are no explanation.

I have explained each and every line in this article.

It is advisable to watch video, since it help you to better understand the process.

WPF Window

In this section we create a WPF Window for our C# code as shown below.

  • Create a new WPF Application in Visual Studio of your choice.
    • We are not creating any Prism WPF application or MVVM based application.
    • You can name it what ever you like.
  • Add Solidworks References to the projects.
  • Add 2 buttons to WPF Windows, as shown in below image.

wpf-window

  • Below image shows “Open Part” button and its code.

open-part-button-and-code

  • Below image shows “Change Dimension State” button and its code.

change-dimension-state-button

Global Variables

In this section we add 2 global variables before Constructor.

These variables are given below.

/// <summary>
/// Solidworks Application field
/// </summary>
SldWorks.SldWorks swApp = default(SldWorks.SldWorks);

/// <summary>
/// Solidworks Document field
/// </summary>
SldWorks.ModelDoc2 swDoc = default(SldWorks.ModelDoc2);

See below image for reference.

global-variables

Open Part Function

In this section we write code for “Open a Part document”.

  • Press F12 on Click event of “Open Part” button as shown below.

open-part-click-event

  • Write below code into the Click event function.
/// <summary>
/// Click event to open desired part document.
/// </summary>
/// <param name="sender">Open Part button</param>
/// <param name="e">Event arguments</param>
private void Button_Click_1(object sender, RoutedEventArgs e)
{
  // Create a new Instance of Solidworks Application
  swApp = new SldWorks.SldWorks();

  // Make Solidworks visible
  swApp.Visible = true;

  // Open Desired Part document
  swDoc = swApp.OpenDoc(@"D:\Solidworks Parts\Scale-feature-part.SLDPRT", (int)swDocumentTypes_e.swDocPART);
}

In above code sample, I have done following.

  1. Create a new Instance of Solidworks Application
  2. Make Solidworks visible
  3. Open Desired Part document. I have open a fix part, but you can use an opened document.

Below image shows part with Driven dimension.

part-with-driven-dimension

Above is straigh-forward and self-explained.

Change Dimension State Function

In this section we write code for “Change Dimension State”.

  • Press F12 on Click event of “Change Dimension State” button as shown below.

change-dimension-state-function

  • Write below code into the Click event function.
/// <summary>
/// Change Dimension State event
/// </summary>
/// <param name="sender">Change Dimension State button</param>
/// <param name="e">Event arguments</param>
private void Button_Click(object sender, RoutedEventArgs e)
{
  // Select the Driven dimension
  bool boolStatus = swDoc.Extension.SelectByID2("D2@Sketch1@Scale-feature-part.SLDPRT", "DIMENSION", 0, 0, 0, true, 0, null, (int)swSelectOption_e.swSelectOptionDefault);

  // Get the Display dimension of Driven dimension
  SldWorks.DisplayDimension displayDimension = swDoc.ISelectionManager.GetSelectedObject6(1, -1);

  // Get Dimension from Display dimension
  SldWorks.Dimension dimension = displayDimension.GetDimension();

  // Output dimension status at output window
  Debug.Print($"Dimension State : [{dimension.DrivenState}]");

  // Check if Driven status of selected dimension is Driven or not
  if (dimension.DrivenState == (int)swDimensionDrivenState_e.swDimensionDriven)
  {
    // Update Driven state
    dimension.DrivenState = (int)swDimensionDrivenState_e.swDimensionDriving;

    // Output dimension status at output window
    Debug.Print($"Dimension State : [{dimension.DrivenState}]");
  }
}

In above code sample, I have done following.

  1. Select the Driven dimension of our part
  2. Get the Display dimension of Driven dimension
  3. Get Dimension from Display dimension.
  4. Output dimension status at output window
  5. Check if Driven status of selected dimension is Driven or not
  6. Update Driven state
  7. Output dimension status at output window
// Select the Driven dimension
bool boolStatus = swDoc.Extension.SelectByID2("D2@Sketch1@Scale-feature-part.SLDPRT", "DIMENSION", 0, 0, 0, true, 0, null, (int)swSelectOption_e.swSelectOptionDefault);
  • In above line of code, we are selecting driven dimension by SelectByID2 method.
    • Although I have not explained this method, it is fairly simple method if you are following my VBA tutorials.
  • If the selection is done then boolStatus = true otherwise boolStatus = false.
// Get the Display dimension of Driven dimension
SldWorks.DisplayDimension displayDimension = swDoc.ISelectionManager.GetSelectedObject6(1, -1);
  • In above code sample, we get the Display dimension of Driven dimension by GetSelectedObject6 method.
  • GetSelectedObject6 is part of ISelectionManager interface.
  • ISelectionManager interface is part of ModelDoc2 i.e. swDoc variable.
  • From GetSelectedObject6 method we get Display dimension, which we store in a new Variable.
    • Variable Name: displayDimension
    • Type: SldWorks.DisplayDimension
// Get Dimension from Display dimension
SldWorks.Dimension dimension = displayDimension.GetDimension();
  • In above code sample, we get the dimension from displayDimension variable by GetDimension method.
  • From GetDimension method we get dimension, which we store in a new Variable.
    • Variable Name: dimension
    • Type: SldWorks.Dimension
// Output dimension status at output window
Debug.Print($"Dimension State : [{dimension.DrivenState}]");
  • In above line of code, we print the current DrivenState to output window.
// Check if Driven status of selected dimension is Driven or not
if (dimension.DrivenState == (int)swDimensionDrivenState_e.swDimensionDriven)
{
}
  • In above code block, we check Driven State of dimension.
  • If dimension is Driven, then this condition fulfil and below code run.
// Update Driven state
dimension.DrivenState = (int)swDimensionDrivenState_e.swDimensionDriving;

// Output dimension status at output window
Debug.Print($"Dimension State : [{dimension.DrivenState}]");
  • In above line of code we update the Driven State of dimension.
  • From Driven ➡ to Driving.
  • After that we print the status to output window.

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 update Dimension Driven State with SOLIDWORKS C# API.

For more such tutorials on SOLIDWORKS C# API, do come to this website after sometime.

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: