Skip to main content
In the following replies, we have shared some tricks about using the diffractive DLL in OpticStudio.




Hello Michael,



Thank you for the useful post.



In my application, when OpticStudio asks the DLL to calculate and return data for a specific order, my DLL calculates the directions and 3D complex electric fields for all the 2M diffraction orders (transmission and reflection diffractions) and return data for just that specific order.



My question: Is there a way to store the calculated data for the remaining 2M-1 orders and return the appropriate data when asked by OpticStudio? The motivation is to save computing time because I do not have to calculate all 2M diffraction orders each time OpticStudio requests data for only one of the 2M diffraction orders.



Your help is greatly appreciated.



Thanks,



Ian Nguyen


Hi Ian,



It's possible but quite complicated to do.



Note below are only some guide but users need to resolve any issues happen when following it as this is far from the scope of supoprt which focuses on resolving issues accessing to OpticStudio. If complicated physics and coding for a DLL is desired, please send a case in Zemax portal for further discussion.



 



Based on above ...



 



I think you can declare a global vairable where all DLL threads can share the same data. Global variable needs to be declared outside of the function UserDiffraction(). I would suggest you simply use STL containers like std::vector to save your data.



And to avoid that multiple DLL threads may access the same global variable and cause crashes, you need to use the trick of critical section. The critical section should be declared globally, initialized in DLL_PROCESS_ATTAH, and deleted in DLL_PROCESS_DETACH.



 



Inside each thread, you then need to make sure you first enter critical section before any access to global variable, and make sure you leave the critical section well or the program will lock and possibly crash.



 


I was experimenting with non-sequential DLLs a while ago, and found I could make values persist between calls using a static variable.  I didn't explore it extensively, but used a satic integer to count the number of times the DLL was being called.  The value was then written to a text file for debugging purposes.  Maybe you could use something similar.  



Another suggestion might be to store your data to file and retrieve it as needed.



 


I have also used static variables for this purpose when writing a dll in C++. The values of static variables persist over multiple calls to the dll. As an example, I have written code for 'Source DLL' objects in which I wanted to serve a defined set of rays rather than random rays from a distribution. The set of rays and the index of the next ray to serve are held in static variables. With each call for analyisis or layout rays, the next ray is served, so rays are served in sequential order from the prevously defined set.


Reply