TAGGED: ZOS-API, ZOS-API: MATLAB
-
-
May 28, 2024 at 5:12 amAnonymous 1996SubscriberI am trying to carry non-sequential raytracing on a system of mine, I tried using a predefined script that works very well on an existing system but when I replace this with my system I am facing some errors. I am not able to resolve them and don't know why this is happening.Here is my code and error,function [ r ] = MATLABStandalone_NSC_ray_trace( args )if ~exist('args', 'var')args = [];end% Initialize the OpticStudio connectionTheApplication = InitConnection();if isempty(TheApplication)% failed to initialize a connectionr = [];elsetryr = BeginApplication(TheApplication, args);CleanupConnection(TheApplication);catch errCleanupConnection(TheApplication);rethrow(err);endendendfunction [r] = BeginApplication(TheApplication, args)import ZOSAPI.*;% creates a new API directoryapiPath = System.String.Concat(TheApplication.SamplesDir, '\API\Matlab');if (exist(char(apiPath)) == 0) mkdir(char(apiPath)); end;% Set up primary optical systemTheSystem = TheApplication.PrimarySystem;sampleDir = TheApplication.SamplesDir;%! [e02s01_m]% Open filetestFile = System.String.Concat(sampleDir, '"D:\Practice\Zemax Practice\Beam_expander-NONSEQ.zos"');TheSystem.LoadFile(testFile, false);%! [e02s01_m]%! [e02s02_m]% Create ray traceNSCRayTrace = TheSystem.Tools.OpenNSCRayTrace();NSCRayTrace.SplitNSCRays = false;NSCRayTrace.ScatterNSCRays = false;NSCRayTrace.UsePolarization = false;NSCRayTrace.IgnoreErrors = true;NSCRayTrace.SaveRays = false;%NSCRayTrace.ClearDetectors(0);% Run ray traceNSCRayTrace.RunAndWaitForCompletion();NSCRayTrace.Close();%! [e02s02_m]% Non-sequential component editorTheNCE = TheSystem.NCE;%! [e02s03_m]% Get detector datadata = NET.createArray('System.Double', TheNCE.GetDetectorSize(6));TheNCE.GetAllDetectorData(6, 1, TheNCE.GetDetectorSize(6), data);[~, rows, cols] = TheNCE.GetDetectorDimensions(6);% rotates and flips image to reflect ZOS orientationdetectorData = flipud(rot90(reshape(data.double, rows, cols)));%! [e02s03_m]% Plotclose all;figure('position', [50, 150, 700, 600])surf(detectorData);axis([1 100 1 100]);shading flat;xlabel('Column #');ylabel('Row #');title('Incoherent Illuminance [lm/cm^2]');colorbar;r = [];endfunction app = InitConnection()import System.Reflection.*;% Find the installed version of OpticStudio.zemaxData = winqueryreg('HKEY_CURRENT_USER', 'Software\Zemax', 'ZemaxRoot');NetHelper = strcat(zemaxData, '\ZOS-API\Libraries\ZOSAPI_NetHelper.dll');% Note -- uncomment the following line to use a custom NetHelper path% NetHelper = 'C:\Users\Documents\Zemax\ZOS-API\Libraries\ZOSAPI_NetHelper.dll';NET.addAssembly(NetHelper);success = ZOSAPI_NetHelper.ZOSAPI_Initializer.Initialize();% Note -- uncomment the following line to use a custom initialization path% success = ZOSAPI_NetHelper.ZOSAPI_Initializer.Initialize('C:\Program Files\OpticStudio\');if success == 1LogMessage(strcat('Found OpticStudio at: ', char(ZOSAPI_NetHelper.ZOSAPI_Initializer.GetZemaxDirectory())));elseapp = [];return;end% Now load the ZOS-API assembliesNET.addAssembly(AssemblyName('ZOSAPI_Interfaces'));NET.addAssembly(AssemblyName('ZOSAPI'));% Create the initial connection classTheConnection = ZOSAPI.ZOSAPI_Connection();% Attempt to create a Standalone connection% NOTE - if this fails with a message like 'Unable to load one or more of% the requested types', it is usually caused by try to connect to a 32-bit% version of OpticStudio from a 64-bit version of MATLAB (or vice-versa).% This is an issue with how MATLAB interfaces with .NET, and the only% current workaround is to use 32- or 64-bit versions of both applications.app = TheConnection.CreateNewApplication();if isempty(app)HandleError('An unknown connection error occurred!');endif ~app.IsValidLicenseForAPIHandleError('License check failed!');app = [];endendfunction LogMessage(msg)disp(msg);endfunction HandleError(error)ME = MXException(error);throw(ME);endfunction CleanupConnection(TheApplication)% Note - this will close down the connection.% If you want to keep the application open, you should skip this step% and store the instance somewhere instead.TheApplication.CloseApplication();end
Error,Beam_expander_NSC_raytraceFound OpticStudio at:c:\program files\ansys zemax opticstudio 2023 r2.00Unrecognized field name "RunAndWaitForCompletion".Error in Beam_expander_NSC_raytrace>BeginApplication (line 55)NSCRayTrace.RunAndWaitForCompletion();Error in Beam_expander_NSC_raytrace (line 16)r = BeginApplication(TheApplication, args);Any help in any regards is much appreciated.Thank you and regards,Navaneeth -
July 10, 2024 at 9:22 amNiki PapachristouAnsys Employee
Hi Navaneeth,
Thank you for reaching out to us!
This message typically means that the NSCRayTrace tool is no longer open or that you have another tool open already. I would assume that the tool hasn't closed based on the code that you shared. Therefore, I suspect it might be another tool that is open. You can only have one tool open at a time, so it is worth double checking that all other preceding tools have been closed.
One quick test would be calling the code below. If test is None, then that would confirm that another tool is not already open. Otherwise, the code will return the current tool name.
test = TheSystem.Tools.CurrenTool
There can also be an issue with other tools running asynchronously. If you call Run() on a previous tool, that tool might still be running in the background (or did not properly close). Instead, it might be easier to manage execution by using RunAndWaitForCompletion() for all tools you call. This can also get tricky if you are calling many tools within loops.Kind Regards,
Niki
-
- The topic ‘I am new to use MATLAB ZOS-API and struggling to execute this file.’ is closed to new replies.
- Assistance Needed for Wavelength Adjustment in Optical Ring Modulator Simulation
- Optic studio-zemax operand values are different from the plot (Genc and Denc)
- Issue when evaluating Ansys Academic Research Optics License
- RMS spot size interpretation
- STEP File Exports
- Plot coupling coefficient versus power from power meter
- Relay Lens Optimization
- ZEMAX CAD export problems with tilted surfaces
- one_sided cylindrical lens array
- 请问怎么在fdtd里面椭圆偏振光源?
-
1191
-
513
-
488
-
225
-
209
© 2024 Copyright ANSYS, Inc. All rights reserved.
0 Comments