API Event handlers

  • 19 August 2020
  • 4 replies
  • 14 views

Hello comunity, 


I'm developing a C# standalone application and I want to know if it is possible and how to get access to the event handlers, something like 'primarySystem.Saved'; 'primarySystem.Opened' and so on in order to do other operations like update UI or get data. 


Thank you in advance. 


Regards


4 replies

Userlevel 6
Badge +2

Hi Luca,


For opening or saving a system in C#, you can use the code:


 


            IOpticalSystem TheSystem = TheApplication.PrimarySystem;

            string sampleDir = TheApplication.SamplesDir;

            string testFile = sampleDir + '\\API\\CS\\e01_new_file_and_quickfocus.zmx';

            TheSystem.SaveAs(testFile);

            TheSystem.LoadFile(testFile, false);

 


We have some examples of this in the {Zemax}\ZOS-API Sample Code folder. I pulled the above snippets from samples #1 and #3. Once you have opened a system, you can access the Editors (LDE, MFE, etc.) for updates or data extraction. This is also shown in samples #1 and #3. 


If you are looking for a way to confirm that these commands have completed successfully, you can check for the system name by using the SystemName property from within the IOpticalSystem Interface:


 



 


Let me know if you have any other questions about this!


Best,


Allie

Hi Allie, 

Thank you for your reply. 


What I want to do is check if the user has performed an operation like to save the optical system and act accordingly. 

To be more specific, I want to export the data from the LDE to a Xml file just after the user press the save button. Is there a method, something like primarySytem.SavedEventHandler, that I can use to achive that?


Thanks again

Regards


Regards


 

Userlevel 6
Badge +2

Hi Luca,


It sounds to me like you are trying to have the API run at all times in the background while a user is working in OpticStudio. Is that right? In that case, it would always be monitoring user actions? I have discussed this with one of the API developers. Here is his response:


 


We don't have Event Handlers in the API because we don't have events themselves.  Depending on what the user is trying to do, they should handle secondary functions/methods based on calling the API functions themselves.


The closest that we have is the RetrieveLogMessages() which will pipe back any of the UI prompts that are normally warnings/errors in the GUI.


Since we can't guarantee thread-safe code if the API and the GUI are trying to modify the same object at the same time, we don't allow the API to be running at the same time a user is interacting with the GUI.  All API scripts called from the GUI are modal


 


I think you could get close to this functionality by creating a User Extension. When run, the User Extension would save the file and also save the LDE separately. But that would be a separate button from 'Save.' Could that work for you instead? We have some guidance on User Extensions here: https://my.zemax.com/en-US/Knowledge-Base/kb-article/?ka=KA-01824.


Best,


Allie

Hi Allie, 


Thank you so much for your answer. I assumed it was something like that. 

Since I'm developing a standalone application I solved the issue by using a backgroundworker that checks the if the system has saved watching for the function primarySystem.NeedsSave. 


Thank you again for your support. 

Reply