How to connect to the ZOS-API with the Interactive Extension using MATLAB

In this article, we will show how to connect to the ZOS-API with the Interactive Extension using MATLAB. The Interactive Extension mode allows scripting environments such as MATLAB or Python to directly connect to OpticStudio so that changes made to the script can be executed in OpticStudio in real time.

Authored By Thomas Pickering

Introduction

The ZOS-API is a tool that allows you to connect and interact with OpticStudio through your preferred scripting language. This feature is especially convenient for directly managing lengthy analyses or for handling repetetive processes. One feature of the API is the Interactive Extension mode which allows external programs to be called in conjunction with OpticStudio for editing your system. One of the programs that can utilize the Interactive Extension is MATLAB. 

In this article, we will show you how to activate the Interactive Extension and use it to program OpticStudio through MATLAB. 

Creating an Interactive Extension script

To create a boilerplate template to connect to the Interactive Extension, go to the Programming tab and click ZOS-API.NET Application Builders...MATLAB...Interactive Extension.

 

Run Interactive Extension

 

The script will be placed in the ZOS-API Project Folder: {Zemax}\ZOS-API Projects\MATLABZOSConnection. If you create more than one of these, then the file names will be successively numbered. MATLAB then automatically launches with the boilerplate script open in the Editor.

 

Matlab Launch

 

Connecting to the Interactive Extension

To start the Interactive Extension, navigate to the Programming tab in OpticStudio and select Interactive Extension.

 

Connect Interactive Extension

 

A dialog window will open, and OpticStudio will wait for an external application to connect. 

 

Waiting for Connection

 

The dialog box shows the instance ID for the running instance of OpticStudio. This allows MATLAB to connect to a specific instance of OpticStudio if more than one is running.

  • Click Run

 

Run Matlab
 

Once run, the status of the Interactive Extension shows as connected:

 

 

The MATLAB Workspace shows a variable ans.

 

ans

 

 

Then in the Command Window, write:

TheApplication = ans;

 

Or in the Command Window, specify the OpticStudio instance by putting in the correct number in the parentheses. The function returns an object of type ZOSAPI_Application. The connection mode is seen as Extension.

 

 

Now you can use the MATLAB Command Window to execute individual commands to the API.

 

Terminating the connection

You can terminate the connection either by clicking the Terminate button in the Interactive Extension dialog box or by running the CloseApplication method in the MATLAB Command Window:

 

 

 

KA-01606

Was this article helpful?
0 out of 0 found this helpful

Comments

0 comments

Article is closed for comments.