Interfacing function
eng

##
MBLOCK

### File content

    function [x,y,typ]=MBLOCK(job,arg1,arg2)
    //
    // Copyright INRIA Oct 2006
    x=[];y=[];typ=[];
    select job
    case 'plot' then
      standard_draw(arg1)
    case 'getinputs' then
      [x,y,typ]=standard_inputs(arg1)
    case 'getoutputs' then
      [x,y,typ]=standard_outputs(arg1)
    case 'getorigin' then
      [x,y]=standard_origin(arg1)
    case 'set' then
     x=arg1
     model=arg1.model
     graphics=arg1.graphics
     //label=graphics.exprs;
     exprs=graphics.exprs

     //Compatibility
     if type(exprs)==15 then
      paramv=list();
      pprop=[];
      //must do something better than that !
      for i=1:size(model.rpar,'*')
         paramv($+1)=string(model.rpar(i))
         pprop($+1) = 0
      end

      exprs = tlist(["MBLOCK","in","intype","out","outtype",...
                     "param","paramv","pprop","nameF","funtxt"],...
                     exprs(1)(1),..
                     exprs(1)(2),..
                     exprs(1)(3),..
                     exprs(1)(4),..
                     exprs(1)(5),..
                     paramv,...
                     sci2exp(pprop(:)),..
                     exprs(1)(7),exprs(2))

     end

     //lab_1 = [in,intype,out,outtype,param,nameF]
     lab_1 = list(exprs.in,..       //1
                  exprs.intype,..   //2
                  exprs.out,..      //3
                  exprs.outtype,..  //4
                  exprs.param,..    //5
                  exprs.pprop,..    //6
                  exprs.nameF)      //7

     //lab_2 = [paramv]
     lab_2 = exprs.paramv //already a list

     //store exprs before modif
     //exprs.prev_exprs=exprs;
     //exprs.prev_exprs.prev_exprs=tlist("MBLOCK");

     while %t do
    //     [ok,Tin,Tintype,Tout,Touttype,Tparam,Tparamv,Tfunam,lab]=..
    //         getvalue('Set Modelica generic block parameters',..
    //         ['Input variables:       ';..
    //          'Input variables types: ';..
    //          'Output variables:      ';..
    //          'Output variables types:';..
    //          'Parameters in Modelica:';..
    //          'Parameter  values:     ';..
    //          'Function name:         '],..
    //         list('str',-1,'str',-1,'str',-1,'str',-1,'str',-1,'str',-1,'str',-1),label(1))

       //warning here lab_1 is a list
       [ok,Tin,Tintype,Tout,Touttype,Tparam,pprop,Tfunam,lab_1]=..
           getvalue('Set Modelica generic block parameters',..
           ['Input variables:       ';..
            'Input variables types: ';..
            'Output variables:      ';..
            'Output variables types:';..
            'Parameters in Modelica:';..
            'Parameters properties: ';..
            'Function name:         '],..
            list('str',-1,'str',-1,'str',-1,'str',-1,'str',-1,..
                 'vec',-1,'str',-1),lab_1)

       //Cancel case
       if ~ok then
         break
       end
    //   ierr=execstr("in=stripblanks(evstr(Tin));..
    //   intype=stripblanks(evstr(Tintype));..
    //   out=stripblanks(evstr(Tout));..
    //   outtype=stripblanks(evstr(Touttype));..
    //   param=stripblanks(evstr(Tparam));..
    //   paramv=evstr(Tparamv);..
    //   funam=stripblanks(Tfunam)",'errcatch')
    //
       //first stage of checking variable name
       //put string symbolic parameters in variable
       ierr=execstr("in=stripblanks(evstr(Tin));..
                     intype=stripblanks(evstr(Tintype));..
                     out=stripblanks(evstr(Tout));..
                     outtype=stripblanks(evstr(Touttype));..
                     param=stripblanks(evstr(Tparam));..
                     funam=stripblanks(Tfunam)",'errcatch')

       if ierr<>0 then
         //You loose ! Try again ! Insert coin !
         //x_message("Error, try again please!")
         x_message("Error in evaluation of variables.")
         ok=%f
         //break
       end

       //check for valid name variable
       //in
       if ok then
         for i=1:size(in,'*')
           r=%f;
           ierr=execstr('r=validvar(in(i))','errcatch')
           if ~r then
             ok=%f
             break
           end
         end
         if ~ok then
           x_message(["Invalid variable name for the input "+string(i)+".";
                      """"+in(i)+"""";
                      "Please choose another variable name."] );
         end
       end
       //out
       if ok then
         for i=1:size(out,'*')
           r=%f;
           ierr=execstr('r=validvar(out(i))','errcatch')
           if ~r then
             ok=%f
             break
           end
         end
         if ~ok then
           x_message(["Invalid variable name for the output "+string(i)+".";
                      """"+out(i)+"""";
                      "Please choose another variable name."] );
         end
       end
       //param
       if ok then
         param=param(:)
         for i=1:size(param,'*')
           r=%f;
           ierr=execstr('r=validvar(param(i))','errcatch')
           if ~r then
             ok=%f
             break
           end
         end
         if ~ok then
           x_message(["Invalid variable name for the parameter "+string(i)+".";
                      """"+param(i)+"""";
                      "Please choose another variable name."] );
         end
       end

       // type checking
       //typeok=%t;
       if ok then
         for i=1:size(intype,'*')
           if intype(i)<>'E'&intype(i)<>'I' then
             //typeok=%f;
             x_message("Input type should be ''E'' or ''I''!");
             ok=%f
             break
           end
         end
       end
    //    if ~typeok then
    //      ok=%f
    //      //break
    //    end

       if ok then
         for i=1:size(outtype,'*')
           if outtype(i)<>'E'&outtype(i)<>'I' then
             //typeok=%f;
             x_message("Output type should be ''E'' or ''I''!");
             ok=%f
             break
           end
         end
       end
    //   if ~typeok then break, end

       //cross size checking
       if ok then
         if or(size(intype)<>size(in)) then
           x_message("Input variables are not well defined!");
           ok=%f
           //break;
         end
       end

       if ok then
         if or(size(outtype)<>size(out)) then
           x_message("Output variables are not well defined!");
           ok=%f
           //break;
         end
       end

       //check param properties
       if ok then
         pprop = pprop(:)
         if (size(param,'*')<>size(pprop,'*')) then
           x_message(["There is differences in";
                      "size of param and size ";
                      "of param properties." ])
           ok=%f
           //break;
         end
       end

       if ok then
         if max(pprop)>2 | min(pprop)<0 then
           x_message(["Parameters properties must be :";
                      "0 : for simple paramater,";
                      "1 : for initial state value,";
                      "2 : for a fixed initial state value." ])
           ok=%f
           //break;
         end
       end

       //check name of modelica file
       if ok then
         if funam=='' then
           x_message("The filename is not defined!")
           ok=%f
           //break
         end
       end

       if ok then
         [dirF,nameF,extF]=fileparts(funam);
         if (extF<>'' & extF<>'.mo')|(dirF<>'' & extF<>'.mo') then
           x_message("Filename extention should be ''.mo '' !")
           ok=%f
           //break
         end
       end

       if ok then
         //set_io checking
         intypex=find(intype=='I'); outtypex=find(outtype=='I');
         //[model,graphics,ok]=check_io(model,graphics,ones(in),..
         //                             ones(out),[],[],intypex,outtypex)
         [model,graphics,ok]=set_io(model,graphics,list([ones(in),ones(in)],ones(in)),..
                                                   list([ones(out),ones(out)],ones(out)),..
                                                   [],[],intypex,outtypex)
       end

       //*****************************************************************************
       //generate second dialog box from Tparam
       if ok then

         Tparam_lab=evstr(Tparam) //labels
         Tparam_sz=size(Tparam_lab,'*') //numbers of labels

         //adjust size of lab_2 according to size of Tparam
         if Tparam_sz>lstsize(lab_2) then
           for i=1:(Tparam_sz-lstsize(lab_2))
             lab_2($+1)="0"
           end
         elseif Tparam_sz<lstsize(lab_2) then
           lab_2_tmp=list()
           if Tparam_sz<>0 then //if param
             for i=1:(lstsize(lab_2)-Tparam_sz)
               lab_2_tmp(i)=lab_2(i)
             end
           end
           lab_2=lab_2_tmp
         end

         //generate lhs, label and rhs txt for getvalue
         if Tparam_sz<>0 then //if param
           lhs_txt="" //lhs txt
           lab_txt="" //label txt
           rhs_txt="" //rhs txt
           for i=1:Tparam_sz
             lhs_txt=lhs_txt+'%v'+string(i)+','
             if pprop(i) == 0 then
               lab_txt=lab_txt+''''+Tparam_lab(i)+''';'
             elseif  pprop(i) == 1 then
               lab_txt=lab_txt+''''+Tparam_lab(i)+' (state) '';'
             elseif  pprop(i) == 2 then
               lab_txt=lab_txt+''''+Tparam_lab(i)+' (fixed state) '';'
             end
             rhs_txt=rhs_txt+'''vec'',-1,'
           end
           lhs_txt=part(lhs_txt,1:length(lhs_txt)-1)
           lab_txt=part(lab_txt,1:length(lab_txt)-1)
           rhs_txt=part(rhs_txt,1:length(rhs_txt)-1)

           //generate main getvalue cmd
           //warning here lab_2 is a list in input and a string in output
           getvalue_txt = '[ok,'+lhs_txt+',lab_2]=getvalue(''Set parameters values'',['+..
                          lab_txt+'],'+..
                          'list('+rhs_txt+'),lab_2)'

           //display the second dialog box
           execstr(getvalue_txt)

           //restore original lab_2 if not ok
           if ~ok then
             lab_2 = exprs.paramv
           end
         end
       end

       //put output param in the form of a list
       //in paramv
       if ok then
         paramv=list();
         for i=1:Tparam_sz
           execstr('paramv('+string(i)+')=%v'+string(i))
         end
       end
       //*****************************************************************************

       //============================---------MBLOCK2
       if ok then
         if extF=='.mo' & fileinfo(funam)<>[] then
           tt=mgetl(funam)
         else
           tt=exprs.funtxt
           mo=model.equations
           //if model.sim(1)<>nameF then
           if mo.model<>nameF then
             tt=[]
           end
         end
       //============================
         [ok,tt]=MODCOM(funam,tt,in,out,param,paramv,pprop)
         if ~ok then
           break
         end
       end

       if ok then
         //------------------
         mo=modelica()
         mo.model=nameF
         mo.inputs=in;
         mo.outputs=out;
         if max(pprop)>0 then
           mo.parameters=list(param',paramv,pprop')
         else
           mo.parameters=list(param',paramv)
         end
         model.equations=mo
         //------------------
         //model.rpar=paramv;
         model.rpar=[]
         for i=1:lstsize(paramv)
            model.rpar=[model.rpar;
                        paramv(i)(:)]//value
         end
         model.sim(1)=funam
         //------------------
         exprs.in=lab_1(1)
         exprs.intype=lab_1(2)
         exprs.out=lab_1(3)
         exprs.outtype=lab_1(4)
         exprs.param=lab_1(5)
         exprs.paramv=list();
         if Tparam_sz<>0 then //if param
           if type(lab_2)==15 then //for eval
             for i=1:lstsize(lab_2)
               exprs.paramv(i)=lab_2(i);//string
             end
           else
             for i=1:size(lab_2,'*')
               exprs.paramv(i)=lab_2(i);
             end
           end
         end
         exprs.pprop=lab_1(6)
         exprs.nameF=lab_1(7)
         exprs.funtxt=tt
         //label(2)=tt
         //------------------
         x.model=model
         graphics.gr_i(1)(1)='txt=[''Modelica'';'' ' + nameF + ' ''];'
         graphics.in_implicit =intype
         graphics.out_implicit=outtype
         //graphics.exprs=label
         graphics.exprs=exprs
         x.graphics=graphics
         break
       end
    end

    //Define
    case 'define' then
      in=['u1']
      intype=['I']
      out=['y1';'y2']
      outtype=['I';'E']
      param=['R';'L'];
      //paramv=[0.1;0.0001];
      paramv=list(0.1,.0001)
      pprop=[0;0];
      nameF='generic'

      //label=list([sci2exp(in),sci2exp(intype,sci2exp(out),..
      //           sci2exp(outtype),sci2exp(param),sci2exp(paramv),nameF]',[])

      exprs = tlist(["MBLOCK","in","intype","out","outtype",...
                     "param","paramv","pprop","nameF","funtxt"],...
                     sci2exp(in(:)),..
                     sci2exp(intype(:)),..
                     sci2exp(out(:)),..
                     sci2exp(outtype(:)),..
                     sci2exp(param(:)),..
                     list(string(0.1),string(.0001)),...
                     sci2exp(pprop(:)),..
                     nameF,[])

      model=scicos_model()
      model.blocktype='c'
      model.dep_ut=[%f %t]
      //model.rpar=paramv;
      model.rpar=[]
      for i=1:lstsize(paramv)
        model.rpar=[model.rpar;
                    paramv(i)(:)]
      end

      mo=modelica()
      mo.model=nameF
      mo.parameters=list(param,paramv)
      model.sim=list(mo.model,30004)
      mo.inputs=in
      mo.outputs=out
      model.in=ones(size(mo.inputs,'r'),1)
      model.out=ones(size(mo.outputs,'r'),1)
      model.equations=mo
      gr_i=['txt=[''Modelica'';'' '+nameF+' ''];';
                  'xstringb(orig(1),orig(2),txt,sz(1),sz(2),''fill'')']
      //x=standard_define([3 2],model,label,gr_i)
      x=standard_define([3 2],model,exprs,gr_i)
      x.graphics.in_implicit =intype
      x.graphics.out_implicit=outtype
    end
    endfunction