Skip to Main Content

      

ANSYS BLOG

March 29, 2022

Programming for All: How to Enhance Simulation with OpticStudio and ZOS-API

Nowadays you don’t have to be a computer programmer to find your way around code. Many built-in tools provide user-friendly assistance to guide you, whether you are a beginner or an expert, democratizing technology, simulation, and computer-aided engineering (CAE). For OpticStudio, this type of easy-to-use tool is the Zemax OpticStudio application programming interface (ZOS-API).

By using object-oriented programming (OOP) concepts, ZOS-API enhances your experience with OpticStudio, the industry standard for optical design software from Zemax, an Ansys company. Put simply, OOP languages are mainly created around objects, rather than logic or theories, which lends a hand to those only moderately familiar with code. For reference, some well-known OOP languages include Java, C++, C#, Python, and JavaScript.

With ZOS-API, you can significantly boost the capabilities of OpticStudio and ultimately, your simulations. ZOS-API not only allows you to access everything in OpticStudio, but it also allows you to customize the software for your specific designs. Further, it makes it easy to embed OpticStudio data into your simulations to solve greater challenges, create more possibilities, and enhance your optical design projects.

To get the most value from ZOS-API and be able to examine and implement its code more easily, it is beneficial for designers to have a deeper understanding of this built-in interface.

Representation of application programming interface (API) objects

Figure 1. Representation of application programming interface (API) objects

Representation of an API class, a shared characteristic of an API object

Figure 2. Representation of an API class, a shared characteristic of an API object

As its inner workings operate similarly to OOP, let’s start there.

Programming Behind the Scenes

As mentioned above, within OOP languages the data is categorized into objects, which are part of classes. This makes the code easily accessible for use in future projects. This form of code inheritance saves significantly on time. An example of this concept can be seen in an OpticStudio file.

Each Zemax (.zmx) file represents a unique optical system, which can be presented in sequential and/or non-sequential mode — each with an individual number of objects or surfaces — though optical systems may share common characteristics, such as wavelengths or surfaces.

In OOP, these optical systems are classified as API objects, while common characteristics are grouped into a master list referred to as an API class or an interface. 

These API classes are then grouped into libraries referred to as namespaces.

This structured, or grouped, modularity of OOP language simplifies any troubleshooting, data correction, or data revision as you can navigate directly to the point in question, without having to sort through entire pages of code.

ZOS-API Front and Center

Now that you are better versed in OOP, you can think of ZOS-API as a namespace (shown in Figure 3), or higher rank of interfaces, that determines which attributes, or characteristics, are available for the object. 

ZOS-API acts as a namespace in object-oriented programming language

Figure 3. ZOS-API acts as a namespace in object-oriented programming language

Therefore, when you engage ZOS-API, you are ultimately constructing API objects with attributes.

These attributes are properties or functions available to you. A property allows you to define or read a stored value, or setting, while a function serves as a command, or action, that you can assign to your object.

For example, if we look at the class INSCRayTrace, which is the non-sequential raytracing class, a property can be used to define its settings, such as ignoring errors, as shown in Figure 4. A function can be used to clear detectors before running the raytrace, as shown in Figure 5.

Using ZOS-API, a property can be used to define the settings of a non-sequential raytrace

Figure 4. Using ZOS-API, a property can be used to define the settings of a non-sequential raytrace

In ZOS-API, you can use a function to clear the detectors before running a raytrace

Figure 5. In ZOS-API, you can use a function to clear the detectors before running a raytrace

Essentially, ZOS-API’s property and function capabilities allow you to perform the same operations at this stage that you can in the interface stage — highlighting one of the structure’s main advantages — that everything can be automated and post-processed.

Selecting a function or property for an object returns results ranging from numbers to text strings or a Boolean true or false response, for example: int value = Object1.NumberOfFields.

Another possible result may be a new object, for example: IField New_Object = Object1.GetField(1).

Some properties require you to either get or set a value, as follows:

  • To get a value, the syntax is the value = Object1.Normalization;
  • To set a value, the syntax is the normalization = value

A Closer Look at Code

You can take a closer look at your code structure by applying sample code via the ZOS-API help function under the programming tab in the OpticStudio task bar, as pictured in Figure 6.

First, open the following sample file in OpticStudio:

\Zemax\ZOS-API Sample Code\C#\CSharpStandalone_01_new_file_and_quickfocus.cs

Though the sample code is written in C# programming language, you can easily open it with a text editor, regardless of the programming language you are using.

Next, open the ZOS-API Syntax Help option, also shown in Figure 6.

The ZOS-API help function

Figure 6. The ZOS-API help function in OpticStudio is found under the programming tab in the task bar and includes the ZOS-API Syntax Help function

The main interface is identified as IOpticalSystem, as seen in Figure 7, and is equivalent to a single .zmx file.

ZOS-API Interface Documentation can be found within the ZOS-API Syntax Help function

Figure 7. ZOS-API Interface Documentation can be found within the ZOS-API Syntax Help function

Interface data is available within the IOpticalSystem interface

Figure 8. Interface data is available within the IOpticalSystem interface

Here you can easily access data for the interface, including functions and properties, by selecting ZOSAPI.IOpticalSystem as shown in Figure 8.

With its ease-of-use, accessibility, and capabilities, ZOS-API enables you to optimize OpticStudio and provides direct access to the data and coding you need to enhance your simulations and optical design projects.

To learn more about the ZOS-API structure, browse a free tutorial on the Zemax website that includes information, articles, and training resources.

To explore OpticStudio, register for a free trial today.

Erfahren Sie, wie Ansys Ihnen helfen kann

Kontaktieren Sie uns heute

* = Pflichtfeld

Danke für die Kontaktaufnahme

We’re here to answer your questions and look forward to speaking with you. A member of our Ansys sales team will contact you shortly.

Bild in der Fußzeile