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();
}
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.
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.
Hi Michael,
I found the problem. It’s all about using the correct IOpticalSystem.
The above code that I included above uses the “PrimarySystem” for accessing the local optimization tool.
It looks like the correct procedure is to use the IOpticalSystem from the Load operation. See below.
IOpticalSystem opticalSystem = application.LoadNewSystem(modelFilename);
ZOSAPI.Tools.Optimization.ILocalOptimization optimize= opticalSystem.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();