Skip to main content
Question

Local Optimization failure


Brian

I am using the ZOS-API in C# with 4.8.1 framework using Zemax OpticStudio 2024 R1.02 and getting an error message when running the Local Optimization tool.

The error message returned from running the tool is “Input settings are invalid”.

This occurs only when I load from an Archive File (.ZAR).   

If I load from a model file (.ZMX) then the Local Optimization tool works as expected.

For the fields that I set, there are only 2; Algorithm = DampedLeastSquares and Cycles = Automatic

It’s weird and I can’t understand the difference between accessing the model from an Archive would create this issue. NOTE, that I did load the ZAR file from the actual Zemax application and Optimization worked fine.

 

If anyone has any ideas as to the issue then please let me know

 

Thanks

B.

3 replies

Brian
  • Author
  • Infrared
  • 6 replies
  • June 11, 2025

I’ve included the below code snippet to show the issue.  It’s pretty repeatable for me.

 

IRestoreArchive archiveTool = application.PrimarySystem.Tools.OpenRestoreZAR();

ArchiveFileStatus archiveFileStatus = archiveTool.SetFileName(“MyArchive.zar”);

if (archiveFileStatus != ArchiveFileStatus.Okay)

      throw new Exception(“Bad Archive”);
 

archiveTool.RunAndWaitForCompletion();

 

if (!archiveTool.Succeeded || !archiveTool.IsValid)

       throw new Exception(“Failed to Restore : “ + archiveTool.ErrorMessage);

 

string modelFilename = null;

for(int idx = 0; idx < archiveTool.NumberOfFilesInArchive; idx++)

 {

      string archivedFile = archiveTool.GetFileNameInArchive(idx);

      if (ZemaxUtils.IsZemaxModelFile(archivedFile))

      {

          modelFilename = archivedFile;

                       

          break;

       }

}

archiveTool.Close();

if (modelFilename != null)

{

      application.LoadNewSystem(modelFilename);

       ZOSAPI.Tools.Optimization.ILocalOptimization optimize=

             application.PrimarySystem.Tools.OpenLocalOptimization();

 optimize.Algorithm= ZOSAPI.Tools.Optimization.OptimizationAlgorithm.DampedLeastSquares;

 optimize.Cycles = ZOSAPI.Tools.Optimization.OptimizationCycles.Automatic;

 

       if (!optimize.RunAndWaitForCompletion())

        throw new Exception(“Optimize Failed : “ + optimize.ErrorMessage();

}

 


Brian
  • Author
  • Infrared
  • 6 replies
  • June 12, 2025

New information that I’ve observed...

When Zemax API performs the “OpenRestoreZar()” call, Zemax places the .zmx file in a holding directory.  For me, Zemax places it in a directory at Documents\Zemax\Samples\

 

What’s interesting is when I load the Zemax model (.zmx) that was placed in this holding directory, I’m unable to Optimize.  The application reports an error (“There are no variables defined”). It’s like the Zemax file lost all the variables for Optimization from  the OpenRestoreZar().

 

When saving an Archive, does the Archive save the Variables for Optimization? 

Or is there something else that I might doing wrong here?

B.

 


MichaelH
Ansys Staff
Forum|alt.badge.img+2
  • Ansys Staff
  • 366 replies
  • June 16, 2025

Hi Brian, in the archiveTool variable, what is the value of the archiveTool.GetOutputFolder()?  Is this set to Documents\Zemax\Samples?  You should make sure to set all the properties/methods for a tool to make sure you have expected behavior.  Namely, you should make sure you have the following coded in your script:

archiveTool.SetFileName("filename.zar")
archiveTool.SetOutputFolder("folder_name")
archiveTool.SetFilesAllOverwrite()  # or archiveTool.SetFilesNoOverwrite()
archiveTool.ExtractAllFilesToProjectDirectory = False

And yes, the variables are saved in the ZMX file (whether you are saving via the GUI or the API) and the ZMX with the variables is then saved in the ZAR archive.  For the file stored in the “holding directory”, I would suggest either checking the meta data like Last Modified or renaming this file and running your code again to make sure you are actually extracting the file you think you’re extracting. 


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings