Solved

Matlab/Zemax LDE object manipulation

  • 4 December 2021
  • 6 replies
  • 115 views

Userlevel 1
Badge

I had earlier posted a question as a follow up to Syntax help. Reposting here for the benefit of others and to go further with the topic.

 

What I’m actually after is how to tilt an object in the Non-sequential Lens Data Editor, from Matlab. The Matlab connection is working (using the NSE Ray Trace example).

Thanks,

Josh

 

David Nguyen has replied:

Hi Josh,

 

Assuming the Interactive Extension works for you, you’ll need those lines of code:

TheObject = TheSystem.NCE.GetObjectAt(1)



TheObject.TiltAboutX = 15

The property TilteAboutX is read and write so you can access it directly. The same property exists for Y, and Z. GetObjectAt() will return the corresponding object from the NSC editor (it starts at 1).

Let me know if that works for you.

Take care,

David

 

Thanks David,

It does seem like it’s working, thanks!

Playing around with it more, it seems that Matlab just requests OpticStudio to open a file, do the manipulation, run a ray trace, return the values and does not interact with whatever may have been already open in OpticStudio. Is that true?

How can I use OpticStudio to execute the visualization from Matlab? One of the nicest features of OpticStudio is the built-in graphical layouts, which make it easy to visualize and see what is going on. It would be really handy for me to be able to see changes made from Matlab in the Lens Data Editor and in the NSC 3D layout update. Do I need to open the ZOS file in OpticStudio first? Is there a ZOS-API function like “Update All” as used in the Macros? 

Thanks,

Josh

icon

Best answer by Jeff.Wilde 5 December 2021, 20:09

View original

6 replies

Userlevel 1
Badge

And similar to TiltAboutX, there must be a handle for manipulating the X, Y, and Z positions for an object in the LDE? I’ve been looking in the Syntax help and am unable to find it so far. 

Thanks,

Josh

Userlevel 5
Badge +2

As David mentioned, you might want to try the interactive mode in which you first open up your OpticStudio model, and then place the model into interactive mode by executing the “Interactive Extension” function:

Note that the instance number corresponds to that shown at the top of the window.  At this point you can move the Interactive Extension window around if it is hiding something of interest, but all of the other windows are locked inside the GUI (but they can still be viewed and manipulated remotely).

Now, go to your open Matlab shell and execute code that looks something like this:

TheApplication = MATLABZOSConnection(7);
TheSystem = TheApplication.PrimarySystem;
TheNCE = TheSystem.NCE;

RectVol = TheNCE.GetObjectAt(2);
RectVol.TiltAboutX = -10;

NSC_3D_Layout = TheSystem.Analyses.Get_AnalysisAtIndex(1);
NSC_3D_Layout.Apply; % update layout window

In my simple NSC model, this first completes the interactive communication link (note that the connection number in Matlab must be the same as the Instance Number in OpticStudio), then defines “RectVol” to be Object 2 in the NCE, changes the Tilt About X value of this object to be -10 degrees, and lastly updates the 3D Layout window using the “Apply” method to show this change.  Note that an instance of the layout window (which is already open inside the OpticStudio model) is created in Matlab before issuing the Apply (update) method:

To see the Properties associated with an object (e.g., RectVol as previously defined), simply type the name of the object.  You can easily read/write these properties and see the changes interactively in the NCE:

Userlevel 5
Badge +2

Hi Josh,

 

To complement @Jeff.Wilde‘s comprehensive answer, a non-sequential object is an instance of the INCERow interface and its properties are listed in the ZOS-API Syntax Help under ZOSAPI > Editors > NCE > INCERow (use the ZOSAPI Syntax Help search feature with the keyword INCERow and select the first result).

Among the many properties of INCERow you’ll find:

  • XPosition
  • YPosition
  • ZPosition

Which you can use to control the position of an object. For your information those are [get/set] meaning you can edit, and read the position directly with lines of code like this:

TheObject.XPosition = 1.23

print(TheObject.XPosition)

Take care,

 

David

Userlevel 1
Badge

Thanks @Jeff.Wilde and @David.Nguyen for the input and pointer to the section of the ZOSAPI manual.

The problem seems to be that I was using a stand alone application of Matlab (based on one of the examples, which are all standalone), which I believe opens a separate instance of OpticStudio that doesn’t seem to be visible. 

Through quite a bit of experimenting around with the interactive Matlab extension, I was able to get it working and can now tilt and reposition objects and see the updates in the LDE and NSC 3D :). 

This is probably already obvious to many.. I needed to first create a new Matlab interactive extension using the provided buttons in OpticStudio, as shown below. This created a new Matlab function file that I can call from another Matlab file that had the code @Jeff.Wilde sent, replacing “TheApplication = MATLABZOSConnection(7)” with “TheApplication =  NAME_OF_CREATED_INTERACTIVE_EXTENSION(instance)”. 

Thanks again!

Josh

 

Userlevel 5
Badge +2

See post below...

Userlevel 5
Badge +2

Hi Josh,

Glad to hear you were able to get the Interactive Extension working.  However, the approach you describe is more complicated than the one I typically use.  Did you try clicking on the “Interactive Extension” icon?

 

Once you do that, then your instance of OpticStudio should start waiting for a remote connection.  For example, if the Instance Number = 1, to establish connection you go to Matlab and simply type:

TheApplication = MATLABZOSConnection(1);

 

 

In OpticStudio, the Interactive Extension window should go from a wait state to a connected state:

At this point you can continue to enter commands in Matlab as needed.  In summary, for a more automated approach, you should be able to just click the Interactive Extension icon and then execute a Matlab script with a few key commands (as I provided previously). 

In any event, you have it working now which is the main objective.  Have fun!

Regards,

Jeff

Reply