If your script ever needs to work with equations, you will frequently want to access either the left hand side (name) of the equals sign or the right hand side (value) of the equals sign. The following functions perform these tasks:
Getting Equations
IN this example there is are two equations, "x=1" and "y=2".
Get equations by name.
Dim eqn As Equation Dim eqn2 As Equation Set eqn = Project.Schematics(1).Equations("x") Debug.Print eqn.Expression Set eqn2 = Project.Schematics(1).Equations("y") Debug.Print eqn2.Expression 'Get equation by index Dim eqn As Equation Dim eqn2 As Equation Set eqn = Project.Schematics(1).Equations(1) Debug.Print eqn.Expression Set eqn2 = Project.Schematics(1).Equations(2) Debug.Print eqn2.Expression
Both of these scripts will produce this output in the debug window
x=1 y=2
Equation Name
Function GetEqName(e As String) As String GetEqName = Trim(Left(e, InStr(e,"=")-1)) End Function
Equation Value
Function GetEqVal(e As String) As Double GetEqVal = Val(Right(e, Len(e) - InStr(e,"="))) End Function
Since MWO handles the entire equation as a string through the Expression method (i.e. Equation.Expression), I usually call these functions inline, as follows:
Dim x as String ' Equation name Dim y as Double ' Equation value x = GetEqName(project.Equations("EquationName").Expression) y = GetEqVal(project.Equations("EquationName").Expression)
Manipulating groups of Equations
MWO handles the entire group of equation as a string through the Expression method (i.e. Equation.Expression). Each equation in the group separated from the next equation with carriage return and line feed characters. To read all equations in the group:
Dim x as String x = Project.Schematics(1).Equations(1).Expression
To set the group of equations:
project.Schematics(1).Equations(1).Expression = "x=11"+vbCRLF+"y=21"+vbCRLF+"z=31",
Or:
project.Schematics(1).Equations(1).Expression = "x=11"+chr(13)+chr(10)+"y=21"+chr(13)+chr(10)+"z=31"
So the following works only for groups:
vbCRLF = chr(13) + chr(10)
To get the number of groups of equations:
Debug.Print Project.Schematics(1).Equations.Count
To loop through groups of equations:
For Each eqn In Project.Schematics(1).Equations Next eqn
There are some equation properties for individual equations within the group. It takes two commands. First, you tell it which eqn you want to work on, and then you make the actual property change. (In the following code second equation in the group is disabled). To select which equation in the group to access, use the "ActiveIndex" property on the equation object:
project.Schematics(1).Equations(1).ActiveIndex=2 project.Schematics(1).Equations(1).Enabled=False
So, when you access the Enabled or Tune properties, it works on whatever equation in the group is selected by ActiveIndex. By default, ActiveIndex is set to one.
If equations were organized as separate equations without making a group of equations they will be treated as they were before, as the usual collection of equations.