Solved

matlab代碼轉換成python代碼相關問題

  • 13 June 2023
  • 1 reply
  • 191 views

想跟大家詢問一下 我這邊寫了一組matlab code去抓出公差,但不知道要怎麼把matlab的代碼轉成python去做控制,以下是我想改的matlab代碼,想請問有沒有甚麼方法可以轉成python呢?

~~~
 

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;

    %Figure Setting
    xA = "Two Mirror Gap Change (um)";
    yA = "Contrast";
    yA1 = "Contrast Deformation";
    Leg1 = ['X - Contrast'; 'Y - Contrast'];
    Ptitle = 'Contrast in X/Y vs. 2 Mirror Gap';
    Ptitle1 = 'Contrast Deformation vs. 2 Mirror Gap';

    sqn = 20;
    % Set up primary optical system
%     TheSystem = TheApplication.PrimarySystem;
    sampleDir = TheApplication.SamplesDir;
        % Open file
    testFile = System.String.Concat(sampleDir, '\z_5a.zma');
    if (exist(char(testFile)) == 0)
        fprintf('You need to run Z_5a.zmx before running this example\n');
        r = [];
        return;
    end
    TheSystem.LoadFile(testFile,false);
    TheLDE = TheSystem.LDE;
    Surface_0 = TheLDE.GetSurfaceAt(0);
    Surface_1 = TheLDE.GetSurfaceAt(1);
    Surface_2 = TheLDE.GetSurfaceAt(2);
    Surface_5 = TheLDE.GetSurfaceAt(5);


% Set the calculation cycle number
CN = 41;  Ar_nb = CN-1;
% set max gap variation, unit = micron
Dmzx = -20;   % Gap
Dmzx_Step = -2*Dmzx/(CN-1);

DmN = (1:CN);
DmXX = Dmzx + (DmN - 1)*Dmzx_Step;

Conx = zeros(1, Ar_nb);
Cony = Conx;
for jj = 1:CN
     Surface_2.Thickness = (Dmzx + (jj-1)*Dmzx_Step)/1000; %Wafer gap change
     TheSystem.Save();

     pop = TheSystem.Analyses.Get_AnalysisAtIndex(4);
     pop_setting = pop.GetSettings();
     cfg = System.String.Concat(sampleDir,'\so_lens.cfg');
     pop_setting.SaveTo(cfg);
     pop_setting.LoadFrom(cfg);
     pop_setting.ModifySettings(cfg, 'EXD_FILESIZE', 0.832) %square array size. 
     pop_setting.ModifySettings(cfg, 'EXD_DISPLAYSIZE', 0.832) %square array size. 
     pop.ApplyAndWaitForCompletion();
     results = pop.GetResults();
     header = results.HeaderData;
     AAA = results.DataGrids(1).Values.double;
     [ax, ay] = size(AAA);
     arxn = round(ax/10);
     aryn = round(ay/10);
     BBB = AAA(arxn:ax-arxn, aryn:ay-aryn);


    Iy0 = max(BBB, [], 1); Ix0 = max(BBB, [], 2); 
    Iy = sort(Iy0, 'descend'); Ix = sort(Ix0, 'descend');
    Iymx = mean(Iy(1:sqn)); Iymn = mean(Iy(end-sqn:end));
    Ixmx = mean(Ix(1:sqn)); Ixmn = mean(Ix(end-sqn:end));
    Conx(jj) = (Ixmx - Ixmn)/(Ixmx + Ixmn);
    Cony(jj) = (Iymx - Iymn)/(Iymx + Iymn);   
end

ConDiff = 2*abs(Cony-Conx)./(Conx+Cony);
figure(1)
plot(DmXX, Conx, 'b', DmXX, Cony, 'r');
legend(Leg1);
xlabel(xA);
ylabel(yA);
title(Ptitle)
fontsize(gcf,scale=1.2);

figure(2)
plot(DmXX, ConDiff);
% legend(Leg1);
xlabel(xA);
ylabel(yA1);
title(Ptitle1);
fontsize(gcf,scale=1.2);

    Surface_0.Thickness = 186.13848;
    Surface_2.Thickness = 0;
    TheSystem.Save();

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\swang250\OneDrive - JNJ\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
~~~~

icon

Best answer by yuan.chen 15 June 2023, 03:49

View original

1 reply

Userlevel 5
Badge +3

您好,ZOSAPI的Matlab跟Python的代码不大,您可以参考我们系统自带的代码范例进行对比修改。

Reply