Drive Assembly Dimensions with Equations API
Use the SolidWorks Equation Manager to read, update, and rebuild assembly dimensions from external code. The foundation for any parametric configurator or WPF-driven design tool.
using SolidWorks.Interop.sldworks;
public class EquationManagerService
{
public void UpdateGlobalVariables(
ModelDoc2 assemblyDoc,
Dictionary<string, string> globalVariables)
{
var eqMgr = assemblyDoc.GetEquationMgr();
for (int i = 0; i < eqMgr.GetCount(); i++)
{
string equation = eqMgr.Equation[i];
if (string.IsNullOrEmpty(equation)) continue;
var parts = equation.Split('=');
if (parts.Length < 2) continue;
string eqName = parts[0].Trim().Trim('"');
if (globalVariables.ContainsKey(eqName))
{
string newValue = globalVariables[eqName];
eqMgr.Equation[i] = $"\"{eqName}\"={newValue}";
}
}
assemblyDoc.ForceRebuild3(false);
}
}
// WPF ViewModel usage:
private void OnWidthChanged(double newWidth)
{
var vars = new Dictionary<string, string>
{
{ "Width", newWidth.ToString() },
{ "Height", Height.ToString() }
};
_eqService.UpdateGlobalVariables(_assemblyDoc, vars);
}How It Works
SolidWorks equations follow the format "VariableName"=value. The Equation Manager API lets you iterate all equations, parse their names and values, and update them programmatically.
The key steps:
- Get the equation manager:
assemblyDoc.GetEquationMgr() - Iterate equations:
eqMgr.GetCount()andeqMgr.Equation[i] - Parse the name: split on
=, trim whitespace and quotes - Update the value: assign back to
eqMgr.Equation[i] - Rebuild:
ForceRebuild3(false)regenerates all geometry
Setting Up Equations in SolidWorks
Before your code can drive dimensions, link them to global variables in SolidWorks:
- Open the assembly and go to Tools > Equations
- Add a global variable:
"Width" = 1000 - Link a dimension to it:
D1@Sketch1 = "Width"
Now when your code updates the Width equation, every dimension referencing it updates automatically after rebuild.
WPF Integration
Bind your WPF controls to ViewModel properties that trigger equation updates:
<!-- XAML -->
<Slider Value="{Binding Width}" Minimum="500"
Maximum="3000" TickFrequency="50" />
<TextBlock Text="{Binding Width, StringFormat={}{0}mm}" />In your ViewModel, call UpdateGlobalVariables when the property changes. Add a short debounce (200-500ms) to avoid rebuilding on every slider tick — SolidWorks rebuilds can take 1-5 seconds for complex assemblies.
Tips
- Units matter: SolidWorks stores dimensions in meters internally. If your equation value is in millimeters, the dimension will be 1000x too large. Either convert in code or set the equation to include units:
"Width"=1000mm - ForceRebuild3(false) rebuilds the active document only. Use
ForceRebuild3(true)to rebuild all referenced documents (slower but thorough). - Equation indexing: Equations are 0-based. Adding/removing equations shifts all indices — always iterate by name, never hardcode indices.
- Configuration-specific: By default, equations apply to all configurations. Use
eqMgr.SetEquationAndConfigurationOptionto target specific configs. - Performance: Batch all equation updates before calling
ForceRebuild3. Each rebuild is expensive — never rebuild after each individual equation change.