Solved

ZOSAPI_NetHelper cannot be added


Userlevel 1

Hi.


I am using the ZOS-API for Python and try to switch to the new recommended method of using PythonNET instead of the COM interface. To do so, I created a new Python template using 'Programming>Python>Interactive Extension' (same problem occurs with 'Standalone Application'). Running this template fails when trying to add the NetHelper DLL:



import clr, os, winreg
from itertools import islice
# determine the Zemax working directory
aKey = winreg.OpenKey(winreg.ConnectRegistry(None, winreg.HKEY_CURRENT_USER), r'Software\Zemax', 0, winreg.KEY_READ)
zemaxData = winreg.QueryValueEx(aKey, 'ZemaxRoot')
NetHelper = os.path.join(os.sep, zemaxData[0], r'ZOS-API\Libraries\ZOSAPI_NetHelper.dll')
winreg.CloseKey(aKey)
# add the NetHelper DLL for locating the OpticStudio install folder
clr.AddReference(NetHelper)
import ZOSAPI_NetHelper


---------------------------------------------------------------------------
FileNotFoundException Traceback (most recent call last)
in
9
10 # add the NetHelper DLL for locating the OpticStudio install folder
---> 11 clr.AddReference(NetHelper)
12 import ZOSAPI_NetHelper
FileNotFoundException: Unable to find assembly 'Z:\...\Zemax\ZOS-API\Libraries\ZOSAPI_NetHelper.dll'.
at Python.Runtime.CLRModule.AddReference(String name)

I checked, and the dll is present at the correct location. I know that the NetHelper is just used to find the install folder of Zemax and that I can circumvent this problem by specifying the Zemax folder location directly:



import clr, os
zemaxDir = 'C:\Program Files\Zemax OpticStudio 20.2'
# load the ZOS-API assemblies
clr.AddReference(os.path.join(os.sep, zemaxDir, r'ZOSAPI.dll'))
clr.AddReference(os.path.join(os.sep, zemaxDir, r'ZOSAPI_Interfaces.dll'))
clr.setPreload(True)
import ZOSAPI

This works fine and I can connect to the ZOS-API and interact with OpticStudio. However, I would prefer the solution with the NetHelper since this way my code remains the same when installing a new version of OpticStudio or when changing the computer.


Do you know why the NetHelper cannot be added and how to fix this?


Best,


Erik

icon

Best answer by Thomas.Reimann 4 March 2022, 14:30

View original

13 replies

Badge +1

Hi Erik,


The following knowledgebase article outlines how to use Python.NET:


https://my.zemax.com/en-US/Knowledge-Base/kb-article/?ka=KA-01951


Best,


Ali

Userlevel 1

Hi Ali,


thank you for the reply. I already know the article and followed it. However, this article doesn't mention the NetHelper.dll that I found not to be working.


Best,


Erik

Userlevel 6
Badge +2

Hi Erik


You have probably checked but is this line giving you the correct path to the NetHelper:



NetHelper = os.path.join(os.sep, zemaxData[0], r'ZOS-API\Libraries\ZOSAPI_NetHelper.dll')

If this is correct, I would try to re-run OpticStudio  prerequisites and see if that fixes the issue.

The prerequisites are here:


'C:\Program Files\Zemax OpticStudio 20.2.2\OpticStudio_Prerequisites.exe'.

Sometimes when we uninstall / reinstall OpticStudio, it can cause issues in the registry.


Let us know if this helps or not.


Sandrine

Userlevel 1

Hi Sandrine,


I checked the path to the DLL again and it is correct and readable by my user. I also re-run the prerequisites of OpticStudio 20.2, but this also didn't fix the issue.


Best,


Erik

Userlevel 6
Badge +2

Hi Erik


When you have the time, would you mind trying to reinstall OpticStudio? I am wondering if there is an issue with your version of ZOSAPI_NetHelper.dll.


I'll send a message to my colleague who is more an expert in Python that I am. He may know the reason of this issue.


Sandrine

Badge +1

Hi Erik,


It looks like your ZemaxData directory is on a network drive (the Z drive).  Although the file exists on disk, based on the following snippet, Python is looking for the file one level higher than the Z drive:



FileNotFoundException: Unable to find assembly 'Z:\...\Zemax\ZOS-API\Libraries\ZOSAPI_NetHelper.dll'.

I tried moving my ZOSAPI_NetHelper.dll to a network drive but I am unable to reproduce the error.  Rather than relying on the Windows Registry to parse the location of the NetHelper, can you try to hard-code it by using something like the following:



NetHelper = r'Z:\Zemax\ZOS-API\Libraries\ZOSAPI_NetHelper.dll'
Userlevel 1

Hi Sandrine and Michael,


thanks for your suggestions. I tried specifying the location of the DLL directly, but this didn't change anything. Also, I tried copying the DLL to the local drive of the computer. The error remains.


Tomorrow I can try reinstalling the newest version of OpticStudio and give it another shot.


Best,


Erik

Badge +1

Hi Erik,


Do you have access to Matlab?  Matlab uses a similar technique to connect to the ZOSAPI_NetHelper.dll as the new Python template does, so if you cannot connect via Matlab either, I think the issue lies in the DLL and installing/uninstalling/reinstalling OpticStudio.

Userlevel 1

Hi,


I tried reinstalling OpticStudio. After installation of OpticStudio the new files were copied into the data directory, including a new version of the ZOSAPI_NetHelper.dll. However, this did not help. The same error comes again when trying to run the Python code.


Unfortunately, I don't have Matlab available to try the connection.


Best,


Erik

Userlevel 6
Badge +2

Hi Erik,


I think it's a good idea to make sure we have everything set up properly. Please let us know the following:



  • What version of Python are you using? (3.8, 3.7, etc?) Keep in mind that 3.8 will not work with the ZOS-API at this time.

  • What are you using to run Python? Are you using IDLE, Visual Studio, Spyder, etc?

  • Does the {DATA} directory as seen by OS match the location where the NetHelper.DLL is located? In OpticStudio, we can check where the DATA folder is located by navigating to Setup...Project Preferences...Folders. You want to ensure that the location here is the same one you are trying to connect to on your Z: drive:


 



 


The boilerplate is set up to check the DATA folder for the \\ZOS-API\\Libraries folder. That DATA folder will be stored as ZemaxDataThe version of the NetHelper which you just installed should be located in this same folder. Note: this is a different location than ZemaxDir which is the location of the program in the Program Files. 


If this is configured properly, then we may need to take a closer look by opening up a case. Are you available for a screenshare session? You can set up a time here: https://my.zemax.com/en-US/support/support-by-phone/.


Best,


Allie

Badge

Hi Erik,

My advice is just run the Python code in IDLE but not in VS code.

You’ll find something confusing.

Check it~

I’m running into the same issue, is there a solution? From what I gathered, the problem is that pythonnet is imported used the ‘clr’ hook, but that same hook is also used by a different module. When I import clr and type “help(clr)”, I see the “package contents” = style builder and the “data” are color and formatting objects. Is there an alternative hook to call for importing pythonnet?

 

I’m using Anaconda3, Python 3.8.

 

-Patrick

Hello,

I was having a similar issue. In my case the problem was caused by the fact that the Python installation (Anaconda 3 Python 3.8) was 32bit. Replacing it with a 64bit version resolved the problem completely. 

Best regards,

Thomas

 

Reply