[ZOS-API] plot spot diagrams using for loop

13 Ansichten (letzte 30 Tage)
Jong-hwan Kim
Jong-hwan Kim am 22 Okt. 2020
Hello!
I am trying to plot spot diagrams as I change Zernike values using MATLAB.
I spent a lot of time to create a for loop, but the same diagrams are printed over and over again.
Could you check this and let me know what the problem is?
function [ r ] = MATLABStandaloneApplication( args )
if ~exist('args', 'var')
args = [];
end
% Initialize the OpticStudio connection
TheApplication = InitConnection();
if isempty(TheApplication)
% failed to initialize a connection
r = [];
else
try
r = BeginApplication(TheApplication, args);
CleanupConnection(TheApplication);
catch err
CleanupConnection(TheApplication);
rethrow(err);
end
end
end
function [r] = BeginApplication(TheApplication, args)
import ZOSAPI.*;
TheSystem = TheApplication.PrimarySystem;
sampleDir = TheApplication.SamplesDir;
% Open file
testFile = System.String.Concat(sampleDir, '\API\Matlab\model.zmx');
TheSystem.LoadFile(testFile,false);
rayTracer = TheSystem.Tools.OpenBatchRayTrace();
nSurf = TheSystem.LDE.NumberOfSurfaces;
nRays = 10000;
realRayType = ZOSAPI.Tools.RayTrace.RaysType.Real;
hx = 0; hy = 0;
for r = 1:nRays
rand_px = rand() * 2 - 1;
rand_py = rand() * 2 - 1;
while (rand_px^2 + rand_py^2 > 1)
rand_py = rand() * 2 - 1;
end
px(r) = rand_px;
py(r) = rand_py;
end
nWaves = double(TheSystem.SystemData.Wavelengths.NumberOfWavelengths);
final_x = zeros(nRays,nWaves);
final_y = zeros(nRays,nWaves);
n = TheSystem.LDE.GetSurfaceAt(2).GetCellAt(34).DoubleValue; %zernike #9
for n = -2:2:2
normUnPolData = rayTracer.CreateNormUnpol(nRays,realRayType,nSurf);
normUnPolData.ClearData();
wave = 1;
waveNumber = wave;
calcOPD = ZOSAPI.Tools.RayTrace.OPDMode.None;
for r = 1:nRays
normUnPolData.AddRay(waveNumber, hx, hy, px(r), py(r), calcOPD);
end
rayTracer.RunAndWaitForCompletion();
normUnPolData.StartReadingResults();
[success, rayNumber, errCode, vigCode, x, y,~,~,~,~,~,~,~,~,~]=normUnPolData.ReadNextResult();
while success
if ((errCode == 0 ) && (vigCode == 0))
final_x(rayNumber,wave) = x;
final_y(rayNumber,wave) = y;
end
[success, rayNumber, errCode, vigCode, x, y,~,~,~,~,~,~,~,~,~]=normUnPolData.ReadNextResult();
end
figure; hold off;
plot(final_x,final_y,'.w','MarkerSize',15)
set(gca,'Color','k')
axis('square');
grid off;
s1 = 'C:\Users\layno\Desktop\z9\';
s2 = num2str(n);
s3 = '.jpg';
FileName = strcat(s1, s2, s3);
fig = gcf;
fig.InvertHardcopy = 'off';
end
r = [];
end
function 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\layno\Documents\Zemax\ZOS-API\Libraries\ZOSAPI_NetHelper.dll';
% This is the path to OpticStudio
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 == 1
LogMessage(strcat('Found OpticStudio at: ', char(ZOSAPI_NetHelper.ZOSAPI_Initializer.GetZemaxDirectory())));
else
app = [];
return;
end
% Now load the ZOS-API assemblies
NET.addAssembly(AssemblyName('ZOSAPI_Interfaces'));
NET.addAssembly(AssemblyName('ZOSAPI'));
% Create the initial connection class
TheConnection = 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!');
end
if ~app.IsValidLicenseForAPI
HandleError('License check failed!');
app = [];
end
end
function LogMessage(msg)
disp(msg);
end
function HandleError(error)
ME = MException('zosapi:HandleError', error);
throw(ME);
end
function 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

Antworten (0)

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Produkte


Version

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by