Starting Zemax:

function h = StartZemax(ZemaxFile)

%USAGE: h = StartZemax(ZemaxFile);

%Sets up a DDE connection to Zemax and loads

% the specified file.

%Note: With recent (2004) Zemax versions, an instance of Zemax

% must already be running to use DDE from Matlab, although

% a particular file need not be loaded.

%INPUTS:

% ZemaxFile: Zemax file name (and path) to be loaded

% If not entered, no file is loaded, but

% the channel handle, h, is still returned

%OUTPUT:

% h: The DDE channel to zemax

%

%START ZEMAX

h = ddeinit('zemax','notopic');

if h == 0

error('Zemax not started')

end

%

if nargin > 0

%LOAD ZEMAX FILE:

loadstr = ['LoadFile,' ZemaxFile];

rc = ddereq(h,loadstr);

if rc == -999

error('Zemax file load failed')

elseif rc ~= 0

error('Zemax update failed')

end

end

Getting a Zemax Analysis Text file:

function [A,Hdr] = GetZText(channel,Type,SetFile)

%USAGE: [A,Hdr] = GetZText(channel,Type,SetFile);

%Get 2-D text file from Zemax system

%INPUTS:

% channel: The DDE channel to the Zemax system

% (from "StartZemaxFile.m")

% Type: A string with the three-letter identifier

% indicating the Zemax analysis window to run.

% Examples:

% 'Fps' => Fourier Transform PSF

% 'Wfm' => Wavefrpmt map

% 'Mdm' => FFT Surface MTF

% SetFile: A string with the path and name of the configuration

% file to use for the Zemax analysis. Default is

% the last saved configuration for the analysis.

%OUTPUTS:

% A: The 2D array from the Zemax analysis

% Hdr: The header lines from the Zemax file

%

%Time to wait for Zemax (ms):

Tout = 90e3;

%

%Pass data through temp text file in current directory:

D = pwd;

F = [D '\PSFText.txt'];

%

if nargin == 3 %(Use Settings File)

reqstr = ['GetTextFile, "' F '" , ' Type ', "' SetFile '" , 1'];

else %(Use Default config file)

reqstr = ['GetTextFile, "' F '" , ' Type];

end

rc = ddereq(channel,reqstr, [1 0], Tout);

%

%Read text file into Matlab array:

[A,Hdr] = readZMX(F);

%Delete temp file to ensure against false data:

%delete(F);

Changing Surface Data in Zemax:

function Pval = PutSurfData(channel,Surf,Type,Val)

%USAGE: Pval = PutSurfData(channel,Surf,Type,Val);

%Inserts surface parameter into Zemax surface.

%INPUTS:

% channel: DDE channel to a Zemax file

% Surf: the number of the surface to change.

% Type: The parameter type to change. Valid values are

% 'CURV', 'THIC', 'SDIA', and 'CONN' (same abrv. as Zemax)

%OUTPUTS:

% Pval: The newly set value. Might be slightly different

% from Val due to significant digit effects. It is up

% to the user to check to see if it is close enough.

%

%Set Accuracy of number insertion

% (depends on editor "Decimals" setting)

%

SetErr = 1e-3; %(coarse)

%

SurfStr = num2str(Surf);

reqstr0 = ['SetSurfaceData,' SurfStr ','];

switch Type

case 'CURV'

code = '2'; %Zemax data code for curvature

case 'THIC'

code = '3'; %Zemax data code for Thickness

case 'SDIA'

code = '5'; %Code for Semidiameter

case 'CONN'

code = '6'; %Code for conic constant

end %(switch)

reqstr = [reqstr0 code ',' num2str(Val)];

Pval = ddereq(channel,reqstr,[1 1]);

%Pval = str2num(Pval);

Er = ddereq(channel, 'GetUpdate',[1 1]);

Getting Surface Data from Zemax:

function X = GetParameter(channel,Surf,P)

%USAGE: X = GetParameter(channel,Surf,P);

%Gets values, X, from Zemax LDE parameter columns, P

%INPUTS:

% channel: Open DDE channel to Zemax (with file loaded)

% P: A vector indicating which parameters to get.

%OUTPUT:

% X: A cell array with the requested data

%

%Set up the base DDE request string:

L = 'GetSurfaceParameter,';

S = [num2str(Surf) ','];

fmt = [1 0]; %Return numeric data

%

for ii = 1:length(P)

Pstr = num2str(P(ii));

%Set up the DDE request string:

reqstr = [L S Pstr];

%

%Get the parameter:

rc = ddereq(channel, reqstr, fmt);

X{ii} = rc;

end %(over size(P))

%

return