Skip to main content
Solved

Working on a copy and update when done


Sebastien G.

Hello,

 

I am working on a macro as described in the following article, but converted in Python:

https://support.zemax.com/hc/en-us/articles/1500005579082-Modeling-Rotationally-Symmetric-Irregularity-RSI-with-the-API

As working directly on the system, each modification saves an UNDO file and the action is repeated for each Zernike coefficient, for each surfaces, that slows down the execution.

The actual way :

Get the system with :

TheSystem = TheApplication.PrimarySystem

and work on it directly

 

I tried to get a copy of the system with the CopySystem function, modify the copy and push it back :

TheSystem = TheApplication.PrimarySystem

new_system = TheSystem.CopySystem()

# does work here

TheSystem = new_system.CopySystem()

 

When looking at the surface type of TheSystem, I can see that it changed correctly, however it is not applied in the Zemax main window.

I used the TheSystem.UpdateStatus() function, but does not help.

 

Ideally my goal would be to be able to make all modifications outside of Zemax and update it when done, and hence get only one step of UNDO.

 

Is it possible in Python ? If so, what did I missed ?

 

Would that be possible in C# ?

 

Thank you for your help.

Best answer by Sebastien G.

Hello,

loading a cfg file is not possible at any time, only before connecting to the API, so not relevant on how I wanted to use it.

// Define a Preferences File.
            // Preferences file is defined on the IZOSAPIConnection interface (prior to connecting to the API)
            // If no PreferencesFile is defined it will use the default OpticStudio.CFG file however changes will not persist between sessions. 
            // If a PreferencesFile is defined, then any changes will save automatically. 
            Console.WriteLine("===PreferencesFile===");
            string cfgFile = @"C:\Users\Documents\Zemax\Configs\OpticStudio.CFG";
            if (System.IO.File.Exists(cfgFile))
            {
                TheConnection.PreferencesFile = cfgFile;
                Console.WriteLine("PreferencesFile: " + TheConnection.PreferencesFile);
            }
            else
            {
                Console.WriteLine("Default OpticStudio.CFG prefernces used");
            }

So, as it is possible to work on a copy of the system (without any GUI update), I have modified my script to run as a MonteCarlo, and to save the best and worst runs.

Not exactly what I wanted to do, but at least it runs faster.

    (TheConnection,TheApplication,TheSystem) = Connect()

    surfaces = [1,2,3,4,5,6,8,9,10,11,12,13,14,15,16,17]

    nb_run = 20
    Runs = []
    spots= []
    for j in range(nb_run):

        print("Iteration #:"+ str(j))

        # To work on a copy of the initial system
        new_system = TheSystem.CopySystem()

        for i in surfaces:
            prepareSurface(new_system,i)
            # AddA(TheSystem,i,632.8,5)
            AddBC(new_system,i,632.8,0.5,0.1,10,randomize=True)

        # Use compensators defined as variables
        Optimize(new_system)

        # Retrieve the Geometric Spot size for each configurations
        Spot = GetSpot(new_system)

        # Add the system and its result in the list
        Runs.append((new_system, Spot))

    # Sort the list on the max geo spot
    Runs.sort(key= lambda a:a[1].max())

    # Get the name and path of the system to save in the same
    # directory
    name = os.path.splitext(TheSystem.SystemFile)

    # Save the worst run (last list index)
    Runs[nb_run-1][0].SaveAs(name[0]+"_RSI_worst.zmx")

    # Save the best run (first list index)
    Runs[0][0].SaveAs(name[0]+"_RSI_best.zmx")

    TheApplication.CloseApplication()

 

View original
Did this topic help you find an answer to your question?

David.Nguyen
Luminary
Forum|alt.badge.img+2

@Sebastien G. 

 

Its not available through the ZOSAPI (AFAIK), but you can disable the UNDO feature by changing: Setup..OpticStudio Preferences..General..Undo to None.

Then you can save the file at whichever stage of your design you want in the ZOSAPI.

I hope this helps and take care,

 

David


Sebastien G.

Hello,

thank you for your reply.

 

Yes I looked at this solution, but it is also not possible to change it programmatically, so I am not very keen to use it, as it is possible to forget to change it back.

 

There might be a solution to save a special .cfg and to load it at the beginning of the script and to reload the intial .cfg,  but I have not tried it yet.

 


Sebastien G.

Hello,

loading a cfg file is not possible at any time, only before connecting to the API, so not relevant on how I wanted to use it.

// Define a Preferences File.
            // Preferences file is defined on the IZOSAPIConnection interface (prior to connecting to the API)
            // If no PreferencesFile is defined it will use the default OpticStudio.CFG file however changes will not persist between sessions. 
            // If a PreferencesFile is defined, then any changes will save automatically. 
            Console.WriteLine("===PreferencesFile===");
            string cfgFile = @"C:\Users\Documents\Zemax\Configs\OpticStudio.CFG";
            if (System.IO.File.Exists(cfgFile))
            {
                TheConnection.PreferencesFile = cfgFile;
                Console.WriteLine("PreferencesFile: " + TheConnection.PreferencesFile);
            }
            else
            {
                Console.WriteLine("Default OpticStudio.CFG prefernces used");
            }

So, as it is possible to work on a copy of the system (without any GUI update), I have modified my script to run as a MonteCarlo, and to save the best and worst runs.

Not exactly what I wanted to do, but at least it runs faster.

    (TheConnection,TheApplication,TheSystem) = Connect()

    surfaces = [1,2,3,4,5,6,8,9,10,11,12,13,14,15,16,17]

    nb_run = 20
    Runs = []
    spots= []
    for j in range(nb_run):

        print("Iteration #:"+ str(j))

        # To work on a copy of the initial system
        new_system = TheSystem.CopySystem()

        for i in surfaces:
            prepareSurface(new_system,i)
            # AddA(TheSystem,i,632.8,5)
            AddBC(new_system,i,632.8,0.5,0.1,10,randomize=True)

        # Use compensators defined as variables
        Optimize(new_system)

        # Retrieve the Geometric Spot size for each configurations
        Spot = GetSpot(new_system)

        # Add the system and its result in the list
        Runs.append((new_system, Spot))

    # Sort the list on the max geo spot
    Runs.sort(key= lambda a:a[1].max())

    # Get the name and path of the system to save in the same
    # directory
    name = os.path.splitext(TheSystem.SystemFile)

    # Save the worst run (last list index)
    Runs[nb_run-1][0].SaveAs(name[0]+"_RSI_worst.zmx")

    # Save the best run (first list index)
    Runs[0][0].SaveAs(name[0]+"_RSI_best.zmx")

    TheApplication.CloseApplication()

 


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