- Home
- Programming ZEMAX
- Extensions
- How Do I Write an Extension in C?
How Do I Write an Extension in C?
- By Mark Nicholson
- Published 4 April 2006
- Extensions
-
Rating:




Introduction
ZEMAX has a very powerful feature which allows another Windows program to establish a communication link with ZEMAX, and for this other program to get data from ZEMAX on the lens. This is called an Extension. The idea is that a program can use ZEMAX to create an optical system, trace rays through the it, and then the data is sent to the other program for further analysis or computation.
The communication between application and ZEMAX is accomplished using Dynamic Data Exchange (DDE). DDE is a protocol defined within the Windows operating system for sharing data between programs. Two programs can establish a DDE link, with one program acting as the "server" and the other the "client". The client generally requests specific data from the server, and the server sends the data back to the client. ZEMAX is intended to be the server, and any Windows program with the right modifications can be made a client.
Obviously, DDE programming for Windows is not for the faint of heart; it involves numerous programming skills and significant familiarity with message loops, DDE, pointers, atoms, and global handles. However, most of the code used to communicate with ZEMAX is standard "boiler plate" code which is common to any DDE client program. This code has all been written already, and has been placed into a single source code module program called ZCLIENT. ZCLIENT is provided in source code form with ZEMAX, and may be freely copied (as long as the copyright is retained), and used in new Extensions.
ZCLIENT handles all of the communication with ZEMAX transparently. Embedded within ZCLIENT is a call to a single function given the name "UserFunction". This function is placed in a separate C program provided by the user, and is compiled along with ZCLIENT to create the Extension executable.
This article shows how to write a simple "hello world" program to link a user-written C-program to ZEMAX using ZCLIENT. The program will get one piece of information from ZEMAX (the name of the currently loaded file) and it will make one change to the currently loaded file (it will make field point 3 have the value (x=20, y=20) to demonstrate both receiving information and sending commands.
One important point: your application program can work on a file different to that shown in the ZEMAX graphical user interface. If your program is to change lens data, and you want to see that changed data in the graphical user interface, you must "push" the copy of the lens data your application is using to the normal ZEMAX user interface. How to do this is covered in this article. However, a setting must be correctly made under File -> Preferences -> Editors to allow this:

If you want your application to be able to change the data in the editors and analysis windows of the currently open ZEMAX session, then "Allow Extensions To Push Lenses" must be checked.
This control is provided to prevent accidental erasure of data in the current user interface by an extension program.