The Merit Function Editor (IMeritFunctionEditor)

The Merit Function is a numerical representation of how closely an optical system meets a specified set of goals. Ansys Zemax OpticStudio uses a list of operands which individually represent different constraints or goals for the system. Operands represent goals such as total flux, unformity, and many others.

The Merit Function Editor (MFE) interface allows you to add/edit/delete the operands and their values.

As with all Editors, there are Rows comprised of Columns. Each Row in the Merit Function Editor is thought of as an Operand. The Columns define the Parameters which modify how the Operand provides its result. Each Operand has its own specific set of Parameters.

IMeritFunctionEditor MFE = ThePrimarySystem.MFE;
//  Merit Functions can interact with the File System.
//  You sould take care to specify a fully qualified path name.
//  The interface assumes you "know what you're doing".
string MFEFolder = MFE.MeritFunctionDirectory;
MFE.SaveMeritFunction(Path.Combine("C:\\Temp", "MF One.mf"));
//
//  LoadMeritFunction will clear the existing Merit Function
//  and Load a new one from the given file.
//  Notice we try to load unqualfied file name "MF One".
//  This file will be searched for in the 'Current Directory' which is most
//  likely the directory in which your version of ZOS is installed.
//
MFE.LoadMeritFunction("MF One");
//
//  InsertMeritFunction will insert all the Operands defined in the file
//  at the given Operand.
//  Again notice the non-fully qualified path name.
//
MFE.InsertMeritFunction(fileName: "MF One", operandNumber: 1);
//
//  Get the Merit Function Folder.
//  Get the array of Merit Function Files that exist in that Folder.
//
string[] MFEFiles = MFE.GetMeritFunctionFiles();
Console.WriteLine(String.Format("Merit Function Folder: {0}", MFEFolder));
foreach (string tS in MFEFiles)
{
    Debug.Assert(Path.GetDirectoryName(tS) == MFEFolder);
    String fN = Path.GetFileName(tS);
    Console.WriteLine(String.Format("\tMerit Function File Name: {0}", fN));
}
//  Add a new 'BLNK' Operand. This Operand is the 'last' Operand.
IMFERow LastOperand = MFE.AddOperand();
if (LastOperand != null)
{
}
//
//  Insert New inserts a new default (BLNK) Operand BEFORE the given operand.
//  This example will makes its insertion next to last.
//
LastOperand = MFE.InsertNewOperandAt(MFE.NumberOfOperands);
//
//  Remove At will remove the given Operand number.
//
if (!MFE.RemoveOperandAt(operandNumber: MFE.NumberOfOperands))
{
    Debug.Fail("Unable to remove the Last Operand");
}
//
//  RemoveOperandsAt: will remove 'numOperands' starting with the given one.
//  This example removes the Last 2 Operands.
//
int nRmed = MFE.RemoveOperandsAt(MFE.NumberOfOperands - 1, 2);
if (nRmed != 2)
{
    Debug.Fail("Unable to remove the last two operands.");
}
//
//  We've previously removed all the Operands.
//
Debug.Assert(MFE.NumberOfOperands == 0);
MFE.LoadMeritFunction(Path.Combine(MFEFolder, "script_demo_1.mf"));
for (int idx = 1; idx <= MFE.NumberOfOperands; idx++)
{
    //
    //  Visit all the Operands currently defined.
    //
    IMFERow mfeRow = MFE.GetOperandAt(operandNumber: idx);
 
}

Next: