function [ r ] = MATLABStandaloneApplication1( args )
if ~exist('args', 'var')
disp('no args')
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;
% Add your custom code here...
disp("test...")
% Single Ray Trace
TheAnalyses = TheSystem.Analyses;
newWin = TheAnalyses.New_Analysis(ZOSAPI.Analysis.AnalysisIDM.RayTrace);
newWin_Settings = newWin.GetSettings();
newWin_Settings.Hx = 0;
newWin_Settings.Hy = 0.56;
newWin_Settings.Px = 0;
newWin_Settings.Py = 0;
newWin_Settings.Wavelength.SetWavelengthNumber(2);
newWin_Settings.Field.UseAllFields();
newWin_Settings.Type = ZOSAPI.Analysis.Settings.Aberrations.RayTraceType.DirectionCosines;
newWin.ApplyAndWaitForCompletion();
% Get and Save the results
newWin_Results = newWin.GetResults();
newWin_Results.GetTextFile('c:\temp\output_file.txt');
r=[0,0];
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\pc\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
Matlab 与 Zemax混合编程
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 作者博客:bot7.cc 利用MEX进行混合编程时,常常会用到 std::cout ,但是直接使用是不会在Matl...
- 在matlab里面将计算复杂度高的函数写成c++是非常实用的,尤其是对于我这种对c++编译不是很理解的小白。以前每...
- 作者博客:bot7.cc Matlab与C/C++混合编程有很多种方式,分别适用于不同的情况。 程序主体用Matl...
- Java与Matlab混合编程是一种运用Matlab数学方面的优势处理一些数学算法、数据、图像后,转换成Java可...
- 作者博客:bot7.cc 本文介绍用MEX-file的形式进行Matlab C/C++混合编程中添加第三方动态链接...