Skip to main content

We can get and set the folders in Project Preferences in OpticStudio using ZOS API.

  1. Save the Project Preferences Configuration(.CFG) file in Zemax LLC\Documents\Zemax\Configs or you can use default Project Prefrences configuration file OpticStudio
    Save Project Prefrences Configuration file

     

  2. Add the following code for connecting saved configuration file                                 Python Code                                                               print('\n===PreferencesFile===\n')
    cfgFile = r"ZemaxLLC\Documents\Zemax\Configs\Project_Prefferences_test.CFG"
    print(os.path.exists(cfgFile))
            if (cfgFile):
                self.TheConnection.PreferencesFile = cfgFile
            else:
                print('Default OpticStudio.CFG preferences used\n')                           MATLAB Code:                                                                                                             fprintf('\n===PreferencesFile===\n')
     cfgFile = 'C:\Users\Documents\Zemax\Configs\Project_Prefferences_test.CFG';
     if exist(cfgFile, 'file')
               TheConnection.PreferencesFile = cfgFile;
               fprintf('PreferencesFile: %s\n', char(TheConnection.PreferencesFile))
     else
             fprintf('Default OpticStudio.CFG preferences used\n');
     end
    Python and MATLAB Sample code
    1. Change the code in the BeginApplication method                                  % Add your custom code here...
      % Define variables for Project Preferences 
      Preference = TheApplication.Preferences;
      PrefG = Preference.Folders;
      Logic = {'False', 'True'};
      % Read and print the initial settings
      fprintf('\n===Check initial Settings===\n');
      fprintf('ZplDirectory: %s\n', char(PrefG.ZplDirectory));
      PrefG.ZplDirectory="C:\test\POP\POP\BEAMFILES";
      disp(PrefG.ZplDirectory);
      MATLAB 

          Preference = TheApplication.Preferences
          PrefG = Preference.Folders
          Logic = {'False', 'True'}
          # Read and print the initia settings
          print('\n===Check initial Settings===\n')
          print('ZplDirectory:', PrefG.ZplDirectory)
          PrefG.ZplDirectory="C:\test\POP\POP\BEAMFILES"
          print(PrefG.ZplDirectory)                                                                         

      Python

       

Before finding this post, I had already tried step 3, using MATLAB (standalone mode).  While I could retrieve and display directory paths from Preferences.Folders, I could not change them - trying to set new paths had no effect.

After steps 1 and 2 (preparing and loading a configuration file), ZOSAPI allows me to complete step 3 - changing as well as displaying directory paths.

Many thanks for this post!


Reply