xzos / PyZDDE

Zemax/ OpticStudio Extension using Python
MIT License
159 stars 67 forks source link

zSetField not working #95

Open Allvvaro opened 5 years ago

Allvvaro commented 5 years ago

Hi there!. I am having problems with zSetField function. Either when using n=0 or n!=0, it returns that number of arguments are not what were expected. I am not sure what I am doing wrong.

For example, using "zSetField(0, 0, 9, 1)" I get TypeError: Expected 5 arguments, got 2.

Similar, if I try "zSetField(3, 0.001, 0.003, 1, 0, 0, 0, 0, 0)" I get "TypeError: Expected 8 arguments, got 2"

What am I missing? Thank you!

LucVV commented 4 years ago

Hi @Allvvaro,

First of all, sorry for the delayed reply. There was a probleem with the zSetField function for newer Zemax versions, as the return was corrupted. I made a fix, but this is not yet in the package that you install through pip. To use this fix, you could either clone the code from GitHub, or adjust your local package using this fix. Using this fix, the return is as expected again.

Does this help you?

Kind regards,

Luc

turygj commented 2 years ago

The Zemax version I used is 181119, and I have the same problems. I have tried the fix, but I still haven't solved the problem. The problem is "fieldData = fd._make([float(elem) for elem in rs])". When I use "print(len(rs))", the result is "2". So I think that the problem maybe the length of "rs". How can I solve this problem? Thank you!

LucVV commented 2 years ago

Sorry to hear that the fix is not working properly for you. I made that fix because I had a bug on my version, but it seems as if the bug changed for you. I do not have your version, so I cannot really reproduce your issue. However, in the fix linked above, the code that should be executed in your case (as len(rs)==2) is:

if len(rs) == 2:  # The behaviour with the Zemax bug
    fieldData = self.zGetField(n)

Can you debug to see why this is not the case? If you are not using the return obtained with zSetField(), and just want to make a field adjustment, you could try to define a function that does not handle the reply, such as:

def zSetFieldNoReply(self, n, arg1, arg2, arg3=None, vdx=0.0, vdy=0.0,
    fd = _co.namedtuple('fieldData', ['xf', 'yf', 'wgt',
                                      'vdx', 'vdy',
                                      'vcx', 'vcy', 'van'])
    arg3 = 1.0 if arg3 is None else arg3 # default weight
    cmd = ("SetField,{:d},{:1.20g},{:1.20g},{:1.20g},{:1.20g},{:1.20g}"
           ",{:1.20g},{:1.20g},{:1.20g}"
           .format(n, arg1, arg2, arg3, vdx, vdy, vcx, vcy, van))

    reply = self._sendDDEcommand(cmd)

However, please check carefully if this works, it could be that the bug is located in the send side of the DDE. In that case, I'm afraid you will have to switch to the API.