// Automatic Torque calculation of multiple files for magnetic gears 
// Mustafa Husain
// 3-8-2012 (15 Ramadan 2012)

//Opening SciFemm Toolbox that connects FEMM and Matlab 
clc
exec('C:\femm42\scifemm\scifemm.sci',-1);
exec('C:\Program Files (x86)\scilab-5.3.3\modules\xls_link_0.2\loader.sce',-1);
exec('C:\Program Files (x86)\scilab-5.3.3\modules\myFunctions\rms.sce',-1);
openfemm

r = xls_NewExcel(); //New excel 
xls_AddWorkbook(); //workbook
T3=[]; T2=[];T1=[];
T1_rms=[];T2_rms=[];T3_rms=[];
for k =1:10
    file=sprintf('C:\Mustafa\PhD\Thesis\Simulations\Optimization\PL+PH\22-4-26\Pitch factor\HSR varied\LSR 0.5\%d.fem',k)
     opendocument(file)
     mi_zoomnatural
     
    for j=1:17
        mi_analyze(1);
        mi_loadsolution;
        mo_groupselectblock(3);
        T3=[T3, mo_blockintegral(22)];
        mi_selectgroup(3);
        mi_moverotate(0,0,0.5);
        mi_clearselected;
    end
        mi_selectgroup(3);
        mi_moverotate(0,0,-8.5);
        mi_clearselected;
        
       
    for j=1:14
        mi_analyze(1);
        mi_loadsolution;
        mo_groupselectblock(2);
        T2=[T2, mo_blockintegral(22)];
        mi_selectgroup(2);
        mi_moverotate(0,0,0.5);
        mi_clearselected;
    end
        mi_selectgroup(2);
        mi_moverotate(0,0,-7);
        mi_clearselected;
        
        
    for j=1:90
        mi_analyze(1);
        mi_loadsolution;
        mo_groupselectblock(1);
        T1=[T1, mo_blockintegral(22)];
        mi_selectgroup(1);
        mi_moverotate(0,0,0.5);
        mi_clearselected;
    end
        mi_selectgroup(1);
        mi_moverotate(0,0,-45);
        mi_clearselected;
        
        T1_rms=[T1_rms,rms(T1)];
        T2_rms=[T2_rms,rms(T2)];
        T3_rms=[T3_rms,rms(T3)];
        
        
xls_SetWorksheet(k);    // Set Worksheet k 
xls_DisplayAlerts(%f); // disable some excel messagebox

xls_SetData("A1", "T1 (Nm)");
xls_SetData("B1", "T2 (Nm)");
xls_SetData("C1", "T3 (Nm)");
xls_SetData("A2", T1');
xls_SetData("B2", T2');
xls_SetData("C2", T3');

T3=[]; T2=[];T1=[];
end
xls_SaveAs("C:\Mustafa\PhD\Thesis\Simulations\Optimization\PL+PH\22-4-26\Pitch factor\HSR varied\LSR 0.5\allTorques.xls");
r = xls_Close(); // close Workbook
r = xls_Quit(); // quit excel

T_rms=[T1_rms' T2_rms' T3_rms'];


r = xls_NewExcel(); 
xls_AddWorkbook(); 
xls_SetWorksheet(1);    
xls_DisplayAlerts(%f); 
xls_SetData("A1", "T1 rms(Nm)");
xls_SetData("B1", "T2 rms(Nm)");
xls_SetData("C1", "T3 rms(Nm)");
xls_SetData("A2", T_rms);
xls_SaveAs("C:\Mustafa\PhD\Thesis\Simulations\Optimization\PL+PH\22-4-26\Pitch factor\HSR varied\LSR 0.5\rmsTorques.xls");
