Skip to main content

I am writing some python code using PyCharm to generate a non-sequential file from scratch. I’ve used the Zemax API extensively in the past for similar projects which either add or modify lots of rows in sequence, but I haven’t run any of those since before the Ansys merger. Now I’m starting to see some strange behavior as I debug.

 

I have commands which I know and can verify work, even simple ones like:

TheNCE.AddObject()

While the program is running, it has no issues with these lines. If I save the file and open it, I can see everything is being added, comments are being set, colors are being set, etc. etc. The code works. But once I reach a break point and start debugging, I start getting this error, even when I run these commands that again, I have confirmed work as expected:

System.Runtime.Remoting.RemotingException: Failed to write to an IPC Port: The pipe is being closed.

I did some Googling and while I see some issues relating to server communication, I don’t see any references to Zemax. But, I only see this error when interacting with the Zemax API. I also only see it after I’ve been running for a while. The first ~hour or so after a fresh boot of the PC, it works normally. I can arrive at a break point and start troubleshooting and trying new code and it works as expected. Eventually, it just stops accepting commands, even correct ones, and I start getting this error. The only fix appears to be to reboot the PC again - closing all Zemax instances, restarting the python kernel, nothing else seems to make a difference. My first thought was maybe too many Zemax instances are being “left open” if the program exits before 

del zosapi
zosapi = None

can be run, but I’m not sure how to check if any of those instances remain open or how to force them to close.

Hi,

This could be a bug and it is indeed related to problems with the API connection. I get exactly this exception if I first close the connection (using IZOSAPI_Application.CloseApplication), and then try to access any API members. However, I only get this exception after manually closing the connection, and not after waiting for a specific time.

Regarding the last part of your post, about the open Zemax instances: it might be worth trying ZOSPy, a Python library for optical simulations in OpticStudio. While using this library may not solve your problems, it handles the API connection for you and makes sure only a single connection can be open at the same time. This prevents some problems, since it is technically not possible to open multiple connections from the same Python process (although Python will not always complain immediately when you try to open a second connection, and may simply give you a reference to the existing connection).


I tried installing ZOSPy, I get an error when trying to import it however.

I only get this IPC port error when running in Pycharm curiously, I tried in Spyder and it works okay, although I ran in to other issues with regards to calling certain object features. It seems like versions of python after 3.8 have a variety of issues interacting with the Zemax API, and this is one of them… another big one I’ve seen is an issue with implicit vs. explicit type for Zemax “objects” (the API thinks all objects are “row” objects and therefore don’t ever get the features they’re supposed to have for e.g. detector rectangles, cylindrical volumes, etc.). But, you can still interact with them by explicitly going in and fetching the column.

ZOSAPI.Editors.NCE.ObjectColumn.XPosition

or

ZOSAPI.Editors.NCE.ObjectColumn.Par1

 

To be more specific, while this should work in python 3.8:

non_seq_ed.AddObject()

obj = non_seq_ed.GetObjectAt(non_seq_ed.NumberOfRows)

obj.ChangeType(obj.GetObjectTypeSettings(ZOSAPI.Editors.NCE.ObjectType.DetectorRectangle))

obj.GetObjectTypeSettings(ZOSAPI.Editors.NCE.ObjectType.DetectorRectangle)

obj.ObjectData.XHalfWidth = 3

obj.ObjectData.YHalfWidth = 3

obj.ObjectData.NumberXPixels = 1

obj.ObjectData.NumberYPixels = 1

obj.ObjectData.FrontOnly = 1

 

These lines will simply not do anything and don’t throw errors for me. What I have to do instead is this:

non_seq_ed.AddObject()

obj = non_seq_ed.GetObjectAt(non_seq_ed.NumberOfRows)

obj.ChangeType(obj.GetObjectTypeSettings(ZOSAPI.Editors.NCE.ObjectType.DetectorRectangle))

obj.GetObjectCell(ZOSAPI.Editors.NCE.ObjectColumn.Par1).DoubleValue = 3.

obj.GetObjectCell(ZOSAPI.Editors.NCE.ObjectColumn.Par2).DoubleValue = 3.

obj.GetObjectCell(ZOSAPI.Editors.NCE.ObjectColumn.Par3).IntegerValue = 1

obj.GetObjectCell(ZOSAPI.Editors.NCE.ObjectColumn.Par4).IntegerValue = 1

obj.GetObjectCell(ZOSAPI.Editors.NCE.ObjectColumn.Par10).IntegerValue = 1 (front only)

 

Hopefully this gets fixed in the API, or at least the documentation gets updated since it was quite confusing and difficult to figure out why explicitly following the examples was not working.


Hi @Brett W,

  1. Did you try to install ZOSPy using Python 3.8? That Python version is not supported, since some of our dependencies (e.g. Numpy) do not officially support Python 3.8 anymore. If you used a newer Python version, what error did you get? It's really appreciated if you open an issue about this in our GitHub repository.
  2. It’s indeed interesting that the connection issue only occurs in PyCharm and not in Spyder. Are you writing plain scripts in PyCharm, or do you use it's Python Console / Jupyter Notebook functionality  as well?
  3. Which version of Python.NET are you using? If you are using Python.NET 3.0.0 or higher, that would perfectly explain why the first code snippet doesn't work. If you change this:
    obj.ObjectData.XHalfWidth = 3

    To this:

    object_data = obj.ObjectData.__implementation__
    object_data .XHalfWidth = 3

    (And of course change all similar lines accordingly), does that solve your problem? If so, I recommend to read the topics about Python.NET 3.0.0 on this forum. They will explain quite clearly what's going on here.


Reply