{Copyright or © or Copr. :

CNRS - Laboratoire Mouvement & Perception
franck.buloup@univmed.fr

This software is a computer program whose purpose is to highly simplify  
the use of ADwinPro realtime systems when you develop experimental
protocols  on life's sciences area.

This software is governed by the CeCILL  license under French law and
abiding by the rules of distribution of free software.  You can  use, 
modify and/ or redistribute the software under the terms of the CeCILL
license as circulated by CEA, CNRS and INRIA at the following URL
"http://www.cecill.info". 

As a counterpart to the access to the source code and  rights to copy,
modify and redistribute granted by the license, users are provided only
with a limited warranty  and the software's author,  the holder of the
economic rights,  and the successive licensors  have only  limited
liability. 

In this respect, the user's attention is drawn to the risks associated
with loading,  using,  modifying and/or developing or reproducing the
software by the user in light of its specific status of free software,
that may mean  that it is complicated to manipulate,  and  that  also
therefore means  that it is reserved for developers  and  experienced
professionals having in-depth computer knowledge. Users are therefore
encouraged to load and test the software's suitability as regards their
requirements in conditions enabling the security of their systems and/or 
data to be ensured and,  more generally, to use and operate it in the 
same conditions as regards security. 

The fact that you are presently reading this means that you have had
knowledge of the CeCILL license and that you accept its terms.}
unit USimulFrame;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
  Dialogs, ExtCtrls, Tabs, StdCtrls, ComCtrls, Grids, ValEdit, CheckLst,
  Mask, Spin, ToolWin, ActnList, USimulThread, XMLClasses, UGrapheSimulFrame,
  Series, ActnColorMaps, ActnMan,IniFiles, Menus, ImgList, XMLIntf, Buttons,
  UGrapheTabSimulForm;

const sETAT = ' Etat : ';
      sTEMPS = 'Temps : '; 

type
  TMyValueListEditor = class(TValueListEditor)
  public
    function GetCaretPosition : integer;
  end;
  TSimulFrame = class(TFrame)
    TitlePanel: TPanel;
    Shape1: TShape;
    ContainerSplitter1: TSplitter;
    GraphScrollBox: TScrollBox;
    Shape2: TShape;
    ActionList1: TActionList;
    ReinitSimulAction: TAction;
    RunSimulAction: TAction;
    StopSimulAction: TAction;
    InspectCodeSimulAction: TAction;
    AfficherCacherListeGrapheCanauxAction: TAction;
    InspectCodeSplitter: TSplitter;
    GeneratorPopupMenu: TPopupMenu;
    Utiliserlegenerateur1: TMenuItem;
    GeneratorAction: TAction;
    Generateur1: TMenuItem;
    UseGeneratorAction: TAction;
    Panel2: TPanel;
    STPanel: TPanel;
    STCombobox: TComboBox;
    ToolBar1: TToolBar;
    ReinitToolButton: TToolButton;
    ToolButton2: TToolButton;
    RunToolButton: TToolButton;
    StopToolButton: TToolButton;
    ToolButton11: TToolButton;
    SimulStatusBar: TStatusBar;
    Image1: TImage;
    Shape3: TShape;
    ContainerCanOptMessPanel: TPanel;
    CanOptMessPanel: TPanel;
    TabSet1: TTabSet;
    CanPanel: TPanel;
    Splitter2: TSplitter;
    GroupBox1: TGroupBox;
    GraphCheckListBox: TCheckListBox;
    MessageRichEdit: TRichEdit;
    ShowHidePanel1: TPanel;
    ShowCanOptMessPanelSpeedButt: TSpeedButton;
    HideCanOptMessPanelSpeedButt: TSpeedButton;
    InspectCodePanel: TPanel;
    InspectCodePanel2: TPanel;
    InspectCodeMemo: TMemo;
    LigneNumPanel: TPanel;
    ShowHidePanel3: TPanel;
    showInspectCodeSpeedButt: TSpeedButton;
    HideInspectCodeSpeedButt: TSpeedButton;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    OptionsGraphesGroupBox: TGroupBox;
    EpaisseurLabel: TLabel;
    AfficherPtSimulGroupBox: TGroupBox;
    TypePtLabel: TLabel;
    TailleLabel: TLabel;
    TypePtComboBox: TComboBox;
    AfficherValeurPtCheckBox: TCheckBox;
    TailleSpinEdit: TSpinEdit;
    AfficherGrillesCheckBox: TCheckBox;
    AfficherAxesOrdonneesCheckBox: TCheckBox;
    AfficherAxesAbscissesCheckBox: TCheckBox;
    CourbesEscalierCheckBox: TCheckBox;
    AfficherPtsSimulCheckBox: TCheckBox;
    EpaisseurSpinEdit: TSpinEdit;
    OptionsSimulGroupBox: TGroupBox;
    SecondesLabel1: TLabel;
    SecondesLabel2: TLabel;
    TailleFenetreVisuLabeledEdit: TLabeledEdit;
    MAJAffichageLabeledEdit: TLabeledEdit;
    procedure ContainerSplitter1Moved(Sender: TObject);
    procedure TabSet1Click(Sender: TObject);
    procedure AfficherPtsSimulCheckBoxClick(Sender: TObject);
    procedure CourbesEscalierCheckBoxClick(Sender: TObject);
    procedure MaskValueKeyPress(Sender: TObject;
      var Key: Char);
    procedure CanauxValueListEditorDrawCell(Sender: TObject; ACol,
      ARow: Integer; Rect: TRect; State: TGridDrawState);
    procedure CanauxValueListEditorKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure CanauxValueListEditorMouseDown(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure ReinitSimulActionExecute(Sender: TObject);
    procedure RunSimulActionExecute(Sender: TObject);
    procedure StopSimulActionExecute(Sender: TObject);
    procedure OnTerminateSimulThread(Sender: TObject);
    procedure GraphCheckListBoxClickCheck(Sender: TObject);
    procedure FrameResize(Sender: TObject);
    procedure InspectCodeSplitterMoved(Sender: TObject);
    procedure InspectCodeMemoKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure InspectCodeMemoEnter(Sender: TObject);
    procedure AfficherAxesAbscissesCheckBoxClick(Sender: TObject);
    procedure AfficherAxesOrdonneesCheckBoxClick(Sender: TObject);
    procedure TypePtComboBoxChange(Sender: TObject);
    procedure AfficherValeurPtCheckBoxClick(Sender: TObject);
    procedure TailleSpinEditChange(Sender: TObject);
    procedure EpaisseurSpinEditChange(Sender: TObject);
    procedure AfficherGrillesCheckBoxClick(Sender: TObject);
    procedure GeneratorActionExecute(Sender: TObject);
    procedure GeneratorPopupMenuPopup(Sender: TObject);
    procedure UseGeneratorActionExecute(Sender: TObject);
    procedure GraphCheckListBoxDblClick(Sender: TObject);
    procedure HelpImageClick(Sender: TObject);
    procedure Image2Click(Sender: TObject);
    procedure ShowGraphOptionsSpeedButClick(Sender: TObject);
    procedure HideGraphOptionsSpeedButClick(Sender: TObject);
    procedure ShowCanOptMessPanelSpeedButtClick(Sender: TObject);
    procedure HideCanOptMessPanelSpeedButtClick(Sender: TObject);
    procedure HideInspectCodeSpeedButtClick(Sender: TObject);
    procedure showInspectCodeSpeedButtClick(Sender: TObject);
    procedure STComboboxSelect(Sender: TObject);
  private
    { Déclarations privées }
    NBGraphes : integer;
    procedure GeneCodeSimul;
    procedure MAJ_TailleGraphes;
    procedure AjouterGraphe(Nom:string);
    procedure SupprimerGraphe(TheGrapheSimulFrame:TGrapheSimulFrame);overload;
    procedure SupprimerGraphe(Nom:string);overload;
    procedure SupprimerGraphe(TheGrapheTabSimulForm:TGrapheTabSimulForm);overload;
  public
    { Déclarations publiques }
    CanauxValueListEditor : TMyValueListEditor;
    CurentXMLST : IXMLSTType;
    SimulThread : TSimulThread;
    VisuWidth : double;
    MAJ_After : double;
    Library_Handle : THandle;
    ListeGrapheTabSimulForm : TList;
    procedure MAJIHM;
    procedure ClearIHM;
    procedure OptPanelMAJ;
    procedure MAJ_SplitterGraphes(Width : integer);
    procedure ClearGenerators;
  end;

implementation

uses UDocometre, UActStimTraitObject, UGenerator,
     GeneratorClass, UGlobal;

var SelectedValueListRow : integer;

{$R *.dfm}

function TMyValueListEditor.GetCaretPosition:integer;
begin
  if InplaceEditor <> nil
  then result := InplaceEditor.SelStart
  else result := -1;
end;

procedure TSimulFrame.ClearIHM;
var AGrapheSimulFrame : TGrapheSimulFrame;
begin
  StopSimulActionExecute(self);
  
  CanauxValueListEditor.Free;
  CanauxValueListEditor := nil;
	STCombobox.Clear;
	STCombobox.Hint := '';

  while(GraphCheckListBox.Items.Count > 0) do
  begin
    if GraphCheckListBox.Items.Objects[0] <> nil
    then (GraphCheckListBox.Items.Objects[0] as TGenerator).Free;
    GraphCheckListBox.Items.delete(0);
  end;


  while(GraphScrollBox.ControlCount > 0) do
  begin
    AGrapheSimulFrame := (GraphScrollBox.Controls[0] as TGrapheSimulFrame);
    SupprimerGraphe(AGrapheSimulFrame);
  end;

  MessageRichEdit.Clear;
end;

procedure TSimulFrame.MAJIHM;
var InfoCanaux : TInfoCanaux;
    i,j : integer;
    Key : char;
    SelectedNameST : String;
    AGrapheSimulFrame : TGrapheSimulFrame;

    function GraphCheckListBoxChannelIsPresent(Nom:string):boolean;
    begin
      result := (GraphCheckListBox.Items.IndexOf(Nom) > -1);
    end;

    function ChannelHasDisapeared(Nom:string):boolean;
    begin
      result := (InfoCanaux.Nom.IndexOf(Nom) = -1);
    end;

    function CanauxValueListEditorChannelIsPresent(Nom:string):boolean;
    var j_local : integer;
    begin
      result := false;
      for j_local := 1 to CanauxValueListEditor.RowCount - 1
      do if CanauxValueListEditor.Keys[j_local] = Nom then
         begin
           result := true;
         end;
    end;

begin
  Self.StopSimulActionExecute(Self);
	if CanauxValueListEditor = nil then
  begin
    //Creation de la liste des canaux la premiere fois
    CanauxValueListEditor := TMyValueListEditor.Create(self);
    CanauxValueListEditor.Name := 'CanauxValueListEditor';
    CanauxValueListEditor.Parent := TabSheet1;
    CanauxValueListEditor.Align := alCLient;
    CanauxValueListEditor.OnDrawCell := CanauxValueListEditorDrawCell;
    CanauxValueListEditor.OnKeyPress := MaskValueKeyPress;
    CanauxValueListEditor.OnMouseDown := CanauxValueListEditorMouseDown;
    CanauxValueListEditor.OnKeyDown := CanauxValueListEditorKeyDown;
		CanauxValueListEditor.TitleCaptions[0] := 'Canal';
		CanauxValueListEditor.TitleCaptions[1] := 'Valeur';
		CanauxValueListEditor.DisplayOptions := CanauxValueListEditor.DisplayOptions
                                            - [doKeyColfixed];
    CanauxValueListEditor.Options := CanauxValueListEditor.Options - [goColSizing];
    CanauxValueListEditor.DefaultDrawing := false;
    //Taille du panneau gauche
    //CanOptMessPanel.Width := 374;
    //init des propriete align
    //OptPanel.Align := alClient;
    CanPanel.Align := alClient;
    MessageRichEdit.Align := alClient;
    //Ligne selectionne par defaut de la liste de valeur
    SelectedValueListRow := 1;
		NBGraphes := 0;
		InspectCodePanel.Height := ShowHidePanel3.Height;

	end;
	//MAJ Liste des ST et ST selectionnée
	SelectedNameST := STCombobox.Text;
	STCombobox.Clear;
	for i := 0 to DocDOCOMETRE.ST.Count - 1
	do STCombobox.Items.Add(DocDOCOMETRE.ST.Items[i].NOM);
	STCombobox.ItemIndex := STCombobox.Items.IndexOf(SelectedNameST);

	//Recup liste des canaux
	InfoCanaux := DocDOCOMETRE.RendreListeInfoCanaux;

	//MAJ de la liste des graphes
	//Ajout des nouveaux canaux
	for i := 0 to InfoCanaux.Nom.Count - 1 do
	begin
		//if InfoCanaux.ISArray[i] = '0' then
		//begin
		if not GraphCheckListBoxChannelIsPresent(InfoCanaux.Nom[i])
		then GraphCheckListBox.Items.Add(InfoCanaux.Nom[i]);
		//end;
	end;
	//Suppression des canaux disparus
	i := 0;
	while(GraphCheckListBox.Items.Count > i) do
	begin
		if ChannelHasDisapeared(GraphCheckListBox.Items[i]) then
		begin
			for j := 0 to GraphScrollBox.ControlCount - 1 do
			begin
				AGrapheSimulFrame := (GraphScrollBox.Controls[j] as TGrapheSimulFrame);
				if AGrapheSimulFrame.NomCanalChartPanel.Caption  = GraphCheckListBox.Items[i] then
				begin
					SupprimerGraphe(GraphScrollBox.Controls[j] as TGrapheSimulFrame);
					break;
				end;
			end;
			if GraphCheckListBox.Items.Objects[i] <> nil
			then (GraphCheckListBox.Items.Objects[i] as TGenerator).Free;
			GraphCheckListBox.Items.delete(i);
			dec(i);
		end else
		begin
			for j := 0 to GraphScrollBox.ControlCount - 1 do
			begin
				AGrapheSimulFrame := (GraphScrollBox.Controls[j] as TGrapheSimulFrame);
				if AGrapheSimulFrame.NomCanalChartPanel.Caption  = GraphCheckListBox.Items[i] then
				begin
					AGrapheSimulFrame.Fe :=
DocDOCOMETRE.EXPERIENCE.FEG/strtofloat(InfoCanaux.RappFreq[InfoCanaux.Nom.IndexOf(GraphCheckListBox.Items[i])]);
					AGrapheSimulFrame.Series1.Clear;
					AGrapheSimulFrame.IncrementIndex := strtoint(InfoCanaux.RappFreq[InfoCanaux.Nom.IndexOf(GraphCheckListBox.Items[i])]);
					AGrapheSimulFrame.Data_Index := strtoint(InfoCanaux.NumCanal[InfoCanaux.Nom.IndexOf(GraphCheckListBox.Items[i])]);
					break;
				end;
			end;
		end;
		inc(i);
	end;

	//MAJ Liste de valeur
	//Ajout de nouveaux canaux
	for i := 0 to InfoCanaux.Nom.Count - 1 do
	begin
		if not CanauxValueListEditorChannelIsPresent(InfoCanaux.Nom[i]) then
		begin
			//if InfoCanaux.ISArray[i] = '0' then
			//begin
			if InfoCanaux.floatORint[i] = 'float' then
			begin
				CanauxValueListEditor.Strings.Add(InfoCanaux.Nom[i]+'=0.0');
        j := CanauxValueListEditor.Strings.IndexOf(InfoCanaux.Nom[i]+'=0.0');
				CanauxValueListEditor.ItemProps[j].EditStyle := esSimple;
				if (InfoCanaux.inORoutORcanint[i] = 'out')
				then CanauxValueListEditor.ItemProps[j].ReadOnly := true
				else CanauxValueListEditor.ItemProps[j].ReadOnly := false;
			end else
			begin
				CanauxValueListEditor.Strings.Add(InfoCanaux.Nom[i]+'=0');
				j := CanauxValueListEditor.Strings.IndexOf(InfoCanaux.Nom[i]+'=0');
				if (InfoCanaux.inORoutORcanint[i] = 'in') then
				begin
				 CanauxValueListEditor.ItemProps[j].EditStyle := esPickList;
				 CanauxValueListEditor.ItemProps[j].PickList.Add('0');
				 CanauxValueListEditor.ItemProps[j].PickList.Add('1');
				 CanauxValueListEditor.ItemProps[j].ReadOnly := false;
				end else
				begin
					CanauxValueListEditor.ItemProps[j].EditStyle := esSimple;
					CanauxValueListEditor.ItemProps[j].ReadOnly := true;
				end;
			end;
			//end;
		end;
	end;
	//Suppression des canaux disparus
	i := 1;
	while(CanauxValueListEditor.Strings.Count >= i) do
	begin
		if ChannelHasDisapeared(CanauxValueListEditor.Keys[i])
		then CanauxValueListEditor.Strings.Delete(i-1)
		else inc(i);
	end;

	InfoCanaux.Free;

	ContainerSplitter1Moved(self);
	Key := Char(VK_RETURN);
	MaskValueKeyPress(CanauxValueListEditor,key);


	STPanel.AutoSize := false;
	STPanel.AutoSize := true;

	ContainerSplitter1.Refresh;
	InspectCodeSplitter.Refresh;
	{
	InspectCodeMemo.Visible := false;
	InspectCodeSplitter.Visible := false;
	InspectCodeSimulToolButton.Down := true;}
end;

procedure TSimulFrame.OptPanelMAJ;
begin
//  OptionsGraphesGroupBox.Left := OptPanel.Width div 2 - OptionsGraphesGroupBox.Width div 2;
//  OptionsSimulGroupBox.Left := OptPanel.Width div 2 - OptionsSimulGroupBox.Width div 2;
end;

procedure TSimulFrame.ContainerSplitter1Moved(Sender: TObject);
begin
	//STPanel.Width := CanOptMessPanel.Width;
	STCombobox.Width := CanOptMessPanel.ClientWidth+ShowHidePanel1.Width;
	//CanOptMessPanel.Width := CanOptMessPanel.Width - ShowHidePanel1.Width;
  OptPanelMAJ;
end;

procedure TSimulFrame.TabSet1Click(Sender: TObject);
begin
//  OptPanel.Visible := false;
  CanPanel.Visible := false;
  MessageRichEdit.Visible := false;
  case Tabset1.TabIndex of
    0:begin
        CanPanel.Visible := true;
        //OptPanel.Visible := true;
        OptPanelMAJ;
      end;
    1:MessageRichEdit.Visible := true;
  end;
end;

procedure TSimulFrame.MaskValueKeyPress(Sender: TObject;  var Key: Char);
var S : string;
    value : double;
    Code : integer;
    InfoCanaux : TInfoCanaux;
begin
  InfoCanaux := DocDOCOMETRE.RendreListeInfoCanaux;
  try
    if Sender is TValueListEditor
    then S := CanauxValueListEditor.Values[CanauxValueListEditor.Keys[SelectedValueListRow]]
    else S := (Sender as TLabeledEdit).Text;

    if not (key in ['0'..'9','.','-',Char(VK_BACK),Char(VK_RETURN)]) then key := #0;
    if (pos('.',S) <> 0) and (key = '.') then key := #0;
    if Sender is TLabeledEdit then if key = '-' then key := #0;

    if Sender is TValueListEditor
    then if (length(S) > 0) and (key = '-')
         then if CanauxValueListEditor.GetCaretPosition <> 0 then key := #0;

    if key = Char(VK_RETURN) then
    begin
      Val(S,Value,Code);
      if (Code <> 0) or (Value = 0) then
      begin
        if InfoCanaux.Nom.Count > 0
        then if InfoCanaux.floatORint[SelectedValueListRow-1] = 'float'
             then S := '0.0'
             else S := '0';
        if Sender is TValueListEditor
        then CanauxValueListEditor.Values[CanauxValueListEditor.Keys[SelectedValueListRow]] := S
        else (Sender as TLabeledEdit).Text := S;
      end;
      Try
        CanauxValueListEditor.SetFocus;
        CanPanel.SetFocus;
      Except
        On EInvalidOperation do;
      end;
    end;
  finally
    InfoCanaux.free;
  end;
end;

procedure TSimulFrame.CanauxValueListEditorDrawCell(Sender: TObject; ACol,
  ARow: Integer; Rect: TRect; State: TGridDrawState);
begin
  with (Sender as TValueListEditor) do
  begin
    Canvas.Brush.Color := clWhite;
    if ARow > 0 then
    begin
      if (ARow mod 2) = 0 then Canvas.Brush.Color := $DDEEFF;
      //if (ARow = SelectedValueListRow) then Canvas.Brush.Color := $FFF8DF;
    end else Canvas.Brush.Color := clBtnFace;
    CanauxValueListEditor.Canvas.FillRect(Rect);
    DrawText(Canvas.Handle, PChar(Cells[ACol,ARow]), -1, Rect ,
                DT_CENTER or DT_VCENTER or DT_SINGLELINE	 );
  end;
end;

procedure TSimulFrame.CanauxValueListEditorKeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
  if Key = VK_UP then Key := 0;
  if Key = VK_DOWN then Key := 0;  
end;

procedure TSimulFrame.CanauxValueListEditorMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var dummy1,dummy2 : integer;
begin
  CanauxValueListEditor.MouseToCell(X,Y,dummy1,dummy2);
  if dummy2 > 0 then SelectedValueListRow := dummy2;
end;

procedure TSimulFrame.OnTerminateSimulThread(Sender: TObject);
begin
  SimulThread := nil;
  SimulStatusBar.Panels[0].Text := ' Etat : arrêt';
  SimulStatusBar.Panels[1].Text := 'Temps : ';
  RunSimulAction.Enabled := false;
  if not FreeLibrary(Library_Handle)
  then showmessage('PBME liberation DLL');
//  DocoForm.RefreshIHMTimer.Enabled := true;
end;

procedure TSimulFrame.GeneCodeSimul;
var i,j,n,m : integer;
    InfoCanaux : TInfoCanaux;
    FORMAT,S : string;
    
    CodeSimul_INCLUDE : TStringList;
    CodeSimul_IMPORT  : TStringList;
    CodeSimul_VAR     : TStringList;
    CodeSimul_LOWINIT : TStringList;
    CodeSimul_CODE    : TStringList;
    CodeSimul_FINISH  : TStringList;
    CodeSimul_FCTSSP  : TStringList;
    CodeSimul_LIBRARY : TStringList;
    CodeSimul_generation : TStringList;
    CodeSimul_Init_AcqGen : TStringList;
    CodeSimul : TStringList;


  procedure TraiterPSerie(Type_Syteme:string;num_pserie:integer);
  var NomFichier_pnf : string;
      ABloc: IXMLBlocType;
      NewFonction : IXMLFONCTIONType;
      NewENTREE : IXMLENTREEType;
      NewPARAM : IXMLPARAMType;
      AActSTimTrait : TActStimTrait;
      Local_i : integer;

  begin
    {'ICE'
    'BERGERLAHR'}

    if Type_Syteme = 'Polhemus'
    then NomFichier_pnf := 'polhemus';
    if Type_Syteme = 'Bird'
    then NomFichier_pnf := 'fob';
    if Type_Syteme = 'SummaGrid IV'
    then NomFichier_pnf := 'SummaGridIV';

    ABloc := CurentXMLST.BLOC.Items[1];
    NewFonction := ABloc.FONCTION.Insert(-1);

    NewFonction.FICHIER := NomFichier_pnf;
    NewFonction.GENRE := 'A';
    NewFonction.DUREE := 0.0;
    NewFonction.TITRE := 'Sans titre';
    NewFonction.AddChild('ADBASIC');
    NewFonction.AddChild('PASCAL');

    NewPARAM := NewFonction.PARAM.Add;
    NewPARAM.NOM := inttostr(num_pserie);

    NewPARAM := NewFonction.PARAM.Add;
    NewPARAM.NOM :=
    inttostr(round(DocDOCOMETRE.EXPERIENCE.FEG/
                   DocDOCOMETRE.PSERIE.Items[num_pserie - 1].FREQUENCE)
                   );

    for Local_i := 0 to InfoCanaux.Nom.Count - 1 do
    begin
      if pos(DocDOCOMETRE.PSERIE.Items[num_pserie - 1].NOM,InfoCanaux.Nom[Local_i]) <> 0
      then if InfoCanaux.ISPSerie[Local_i] = '1' then
           begin
             NewENTREE := NewFonction.ENTREE.Add;
             NewENTREE.NOM := InfoCanaux.Nom[Local_i];
             NewPARAM := NewFonction.PARAM.Add;
             NewPARAM.NOM := InfoCanaux.NumCanal[Local_i];
           end;
    end;

    NewPARAM := NewFonction.PARAM.Add;
    NewPARAM.NOM := inttostr(num_pserie);

    DocoForm.DescriptFrame.CurentXMLBloc := ABloc;

    AActSTimTrait := DocoForm.DescriptFrame.AjouterFonction(NewFonction);
    AActSTimTrait.MAJCodeADBASIC_PASCAL;

    for Local_i := 0 to AActSTimTrait.RefXMLFunction.PASCAL_.INCLUDE.Count - 1
    do CodeSimul_INCLUDE.Add(AActSTimTrait.RefXMLFunction.PASCAL_.INCLUDE.Items[Local_i].TEXT);

    for Local_i := 0 to AActSTimTrait.RefXMLFunction.PASCAL_.IMPORT.Count - 1
    do CodeSimul_IMPORT.Add(AActSTimTrait.RefXMLFunction.PASCAL_.IMPORT.Items[Local_i].TEXT);

    for Local_i := 0 to AActSTimTrait.RefXMLFunction.PASCAL_.VAR_.Count - 1
    do CodeSimul_VAR.Add(AActSTimTrait.RefXMLFunction.PASCAL_.VAR_.Items[Local_i].TEXT);

    for Local_i := 0 to AActSTimTrait.RefXMLFunction.PASCAL_.LOWINIT.Count - 1
    do CodeSimul_Init_AcqGen.Add(AActSTimTrait.RefXMLFunction.PASCAL_.LOWINIT.Items[Local_i].TEXT);

    for Local_i := 0 to AActSTimTrait.RefXMLFunction.PASCAL_.CODE.Count - 1
    do CodeSimul_CODE.Add(AActSTimTrait.RefXMLFunction.PASCAL_.CODE.Items[Local_i].TEXT);

    for Local_i := 0 to AActSTimTrait.RefXMLFunction.PASCAL_.FINISH.Count - 1
    do CodeSimul_FINISH.Add(AActSTimTrait.RefXMLFunction.PASCAL_.FINISH.Items[Local_i].TEXT);

    for Local_i := 0 to AActSTimTrait.RefXMLFunction.PASCAL_.FCTSSP.Count - 1
    do CodeSimul_FCTSSP.Add(AActSTimTrait.RefXMLFunction.PASCAL_.FCTSSP.Items[Local_i].TEXT);

    for Local_i := 0 to AActSTimTrait.RefXMLFunction.PASCAL_.LIBRARY_.Count - 1
    do CodeSimul_LIBRARY.Add(AActSTimTrait.RefXMLFunction.PASCAL_.LIBRARY_.Items[Local_i].TEXT);

    AActSTimTrait.DeleteFonction(self);

  end;

  procedure GENECODE_BLOC(ABloc : IXMLBlocType;Niveau : integer);
  var Local_i,Local_j : integer;
      AFonction : IXMLFonctionType;
      indent : string;
      Num_Fonction,N : integer;
      ANode,ANodeParent : IXMLNode;
  begin
    indent := '';
    for Local_i := 0 to Niveau - 1
    do indent := indent + '   ';
    //introduction Code condition bloc
    if ABloc.GENRE = 'DEBUT'
    then CodeSimul_LOWINIT.Add(ABloc.DELPHICODE1)
    else if ABloc.GENRE = 'FIN'
         then CodeSimul_FINISH.Insert(0,ABloc.DELPHICODE1)
         else CodeSimul_CODE.Add(indent + ABloc.DELPHICODE1);
         
    //Code des fonctions du bloc
    for Local_i := 0 to ABloc.FONCTION.Count - 1 do
    begin
      AFonction := ABloc.FONCTION.Items[Local_i];

      //MAJ de la fonction au cas ou changement notamaent n° canal global
      ANode := ABloc as IXMLNode;
      Num_fonction := ANode.ChildNodes.IndexOf(AFonction) + 1;
      ANodeParent := ANode.ParentNode;
      N := 10;
      repeat
        Num_fonction := Num_fonction + (ANodeParent.ChildNodes.IndexOf(ANode)+1)*N;
        ANode := ANodeParent;
        ANodeParent :=  ANodeParent.ParentNode;
        N := 10*N;
      until ANodeParent.NodeName = 'ROOT';
      AFonction.MAJCodeADBasic_Pascal(Num_fonction, Rep_FichiersInf,InfoCanaux);

      //INCLUDE
      for Local_j := 0 to AFonction.PASCAL_.INCLUDE.Count - 1
      do CodeSimul_INCLUDE.Add(AFonction.PASCAL_.INCLUDE.Items[Local_j].TEXT);
      //IMPORT
      for Local_j := 0 to AFonction.PASCAL_.IMPORT.Count - 1
      do CodeSimul_IMPORT.Add(AFonction.PASCAL_.IMPORT.Items[Local_j].TEXT);
      //VAR
      for Local_j := 0 to AFonction.PASCAL_.VAR_.Count - 1 do
      begin
        if CodeSimul_VAR.IndexOf(AFonction.PASCAL_.VAR_.Items[Local_j].TEXT) = -1
        then CodeSimul_VAR.Add(AFonction.PASCAL_.VAR_.Items[Local_j].TEXT);
      end;
      //LOWINIT
      for Local_j := 0 to AFonction.PASCAL_.LOWINIT.Count - 1
      do CodeSimul_LOWINIT.Add(AFonction.PASCAL_.LOWINIT.Items[Local_j].TEXT);
      //CODE
      if (ABloc.GENRE <> 'DEBUT') and (ABloc.GENRE <> 'FIN')  then
      begin
        if (ABloc.GENRE <> 'NORMAL') then
        begin
          for Local_j := 0 to AFonction.PASCAL_.CODE.Count - 1
          do CodeSimul_CODE.Add(indent+ '   ' + AFonction.PASCAL_.CODE.Items[Local_j].TEXT);
        end else
        begin
          for Local_j := 0 to AFonction.PASCAL_.CODE.Count - 1
          do CodeSimul_CODE.Add(indent + AFonction.PASCAL_.CODE.Items[Local_j].TEXT);
        end;
      end;
      if (ABloc.GENRE = 'DEBUT') then
      begin
        for Local_j := 0 to AFonction.PASCAL_.CODE.Count - 1
        do CodeSimul_LOWINIT.Add(AFonction.PASCAL_.CODE.Items[Local_j].TEXT);
      end;
      if (ABloc.GENRE = 'FIN') then
      begin
        for Local_j := 0 to AFonction.PASCAL_.CODE.Count - 1
        do CodeSimul_FINISH.Add(AFonction.PASCAL_.CODE.Items[Local_j].TEXT);
      end;
      //FINISH
      for Local_j := 0 to AFonction.PASCAL_.FINISH.Count - 1
      do CodeSimul_FINISH.Add(AFonction.PASCAL_.FINISH.Items[Local_j].TEXT);
      //FCTSSP
      for Local_j := 0 to AFonction.PASCAL_.FCTSSP.Count - 1
      do CodeSimul_FCTSSP.Add(AFonction.PASCAL_.FCTSSP.Items[Local_j].TEXT);
      //LIBRARY
      for Local_j := 0 to AFonction.PASCAL_.LIBRARY_.Count - 1
      do CodeSimul_LIBRARY.Add(AFonction.PASCAL_.LIBRARY_.Items[Local_j].TEXT);
    end;
    //Appel recursif des blocs fils
    for Local_i := 0 to ABloc.BLOC.Count - 1 do
    begin
      GENECODE_BLOC(ABloc.BLOC[Local_i],Niveau+1);
    end;
    //Cloture Code condition bloc
    if (ABloc.GENRE <> 'DEBUT') and (ABloc.GENRE <> 'FIN')
    then CodeSimul_CODE.Add(indent + ABloc.DELPHICODE2);
  end;

begin
  CodeSimul_INCLUDE := TStringList.Create;
  CodeSimul_IMPORT  := TStringList.Create;

  //il faut gerer les doublons à la "main" car pas de tri possible
  CodeSimul_VAR     := TStringList.Create;
  CodeSimul_VAR.CaseSensitive := false;
  
  CodeSimul_LOWINIT := TStringList.Create;
  CodeSimul_CODE    := TStringList.Create;
  CodeSimul_FINISH  := TStringList.Create;
  CodeSimul_FCTSSP  := TStringList.Create;
  CodeSimul_LIBRARY := TStringList.Create;
  CodeSimul_generation := TStringList.Create;
  CodeSimul_Init_AcqGen := TStringList.Create;
  CodeSimul := TStringList.Create;
  CurentXMLST := DocDOCOMETRE.ST.Items[STCombobox.ItemIndex];

  //Ajout des entetes et declarations
  CodeSimul.Add('Library ST' + inttostr(STCombobox.itemIndex + 1) + ';');
  CodeSimul.Add(' ');
  
  if CurentXMLST.UseStim <> ''
  then CodeSimul.Add('USES MATH, SysUtils, docoLib, StimuliClass in ''' + Rep_Racine + 'StimuliClass.pas'';' )
  else CodeSimul.Add('USES MATH, SysUtils, docoLib;');
  
  CodeSimul.Add(' ');
  CodeSimul.Add('CONST NMAX = 1024;');
  CodeSimul.Add('      RLS_STOPPED = -1;');
  CodeSimul.Add('      RLS_INIT  =  0;');
  CodeSimul.Add('      RLS_EVENT   =  1;');
  CodeSimul.Add('      RLS_FINISH  =  2;');
  CodeSimul.Add(' ');

  CodeSimul.Add('TYPE TTAB_DATA_PARAM  = array[1..NMAX] of DOUBLE;');
  CodeSimul.Add(' ');

  CodeSimul_VAR.Add('VAR  FEG : DOUBLE;');
  CodeSimul_Init_AcqGen.Add(' ');
  CodeSimul_Init_AcqGen.Add('FEG := ' + inttostr(DocDOCOMETRE.EXPERIENCE.FEG) + ';');
  CodeSimul_CODE.Add(' ');
  CodeSimul_CODE.Add('Temps := (INDEX-1)/FEG;');
  CodeSimul_CODE.Add(' ');

  InfoCanaux := DocDOCOMETRE.RendreListeInfoCanaux;

  //Entree analogiques
  for i := 0 to DocDocometre.EAN.Count - 1 do
  begin
    n := InfoCanaux.Nom.IndexOf(DocDocometre.EAN.Items[i].NOM);
    CodeSimul_VAR.Add('     ' + InfoCanaux.Nom[n] + ' : DOUBLE;');
    CodeSimul_VAR.Add('     Acquisition_' + InfoCanaux.Nom[n] + ' : INTEGER;');
    CodeSimul_VAR.Add('     Nouveau_' + InfoCanaux.Nom[n] + ' : INTEGER;');
    CodeSimul_Init_AcqGen.Add('Acquisition_' + InfoCanaux.Nom[n] + ' := 1;');
    CodeSimul_CODE.Add('Nouveau_'+InfoCanaux.Nom[n]+' := 0;');
    CodeSimul_CODE.Add('IF (index = Acquisition_' + InfoCanaux.Nom[n] + ') THEN BEGIN');
    CodeSimul_CODE.Add('   ' + InfoCanaux.Nom[n] + ' := TAB_DATA['+InfoCanaux.NumCanal[n]+'];');
    CodeSimul_CODE.Add('   acquisition_' + InfoCanaux.Nom[n] + ' := acquisition_'+InfoCanaux.Nom[n]+'+'+InfoCanaux.RappFreq[n]+';');
    CodeSimul_CODE.Add('   Nouveau_' + InfoCanaux.Nom[n] +' := 1;');
    CodeSimul_CODE.Add('END;');
    CodeSimul_CODE.Add('');
  end;

  //Sorties analogiques
  for i := 0 to DocDocometre.SAN.Count - 1 do
  begin
    n := InfoCanaux.Nom.IndexOf(DocDocometre.SAN.Items[i].NOM);
    CodeSimul_VAR.Add('     ' + InfoCanaux.Nom[n] + ' : DOUBLE;');
    CodeSimul_VAR.Add('     Generation_' + InfoCanaux.Nom[n] + ' : INTEGER;');
    CodeSimul_Init_AcqGen.Add('Generation_' + InfoCanaux.Nom[n] + ' := 1;');
    CodeSimul_generation.Add('IF (index = Generation_' + InfoCanaux.Nom[n] + ') THEN BEGIN');
    CodeSimul_generation.Add('   TAB_DATA['+InfoCanaux.NumCanal[n]+'] := '+InfoCanaux.Nom[n]+';');
    CodeSimul_generation.Add('   generation_' + InfoCanaux.Nom[n] + ' := generation_'+InfoCanaux.Nom[n]+'+'+InfoCanaux.RappFreq[n]+';');
    CodeSimul_generation.Add('END;');
    CodeSimul_generation.Add('');
  end;

  //Entrees numériques
  If DocDocometre.ENUM.Count > 0 then
  begin
    CodeSimul_VAR.Add('     Acquisition_ENUM : INTEGER;');
    CodeSimul_Init_AcqGen.Add('Acquisition_ENUM := 1;');
    CodeSimul_CODE.Add('IF (index=acquisition_ENUM) THEN BEGIN');
    for i:=0 to DocDOCOMETRE.ENUM.Count - 1 do
    begin
      n := InfoCanaux.Nom.IndexOf(DocDOCOMETRE.ENUM.Items[i].NOM);
      CodeSimul_VAR.Add('     ' + InfoCanaux.Nom[n] + ' : INTEGER;');
      CodeSimul_CODE.Add('   ' + InfoCanaux.Nom[n] + ' := Round(TAB_DATA['+InfoCanaux.NumCanal[n]+']);');
    end;
    n := InfoCanaux.Nom.IndexOf(DocDOCOMETRE.ENUM.Items[0].NOM);
    CodeSimul_CODE.Add('   acquisition_ENUM := acquisition_ENUM +'+InfoCanaux.RappFreq[n]+';');
    CodeSimul_CODE.Add('END;');
    CodeSimul_CODE.Add('');
  end;

  //Sorties numériques
  If DocDocometre.SNUM.Count > 0 then
  begin
    CodeSimul_VAR.Add('     generation_SNUM : INTEGER;');
    CodeSimul_Init_AcqGen.Add('generation_SNUM := 1;');
    CodeSimul_generation.Add('IF (index=generation_SNUM) THEN BEGIN');
    for i := 0 to DocDocometre.SNUM.Count - 1 do
    begin
      n := InfoCanaux.Nom.IndexOf(DocDOCOMETRE.SNUM.Items[i].NOM);
      CodeSimul_VAR.Add('     '+InfoCanaux.Nom[n]+' : INTEGER;');
      CodeSimul_generation.add('   TAB_DATA['+InfoCanaux.NumCanal[n]+'] := '+InfoCanaux.Nom[n]+';');
    end;
    n := InfoCanaux.Nom.IndexOf(DocDOCOMETRE.SNUM.Items[0].NOM);
    CodeSimul_generation.add('   generation_SNUM := generation_SNUM+'+InfoCanaux.RappFreq[n]+';');
    CodeSimul_generation.Add('END;');
    CodeSimul_generation.Add('');
  end;

  //Canaux internes
  for i:=0 to DocDOCOMETRE.CANINT.Count - 1 do
  begin

    if DocDOCOMETRE.CANINT.Items[i].TAILLE = 1
    then n := InfoCanaux.Nom.IndexOf(DocDOCOMETRE.CANINT.Items[i].NOM)
    else n := InfoCanaux.Nom.IndexOf(DocDOCOMETRE.CANINT.Items[i].NOM + '[1]');
    if DocDOCOMETRE.CANINT.Items[i].FORMAT = 'Flottant'
    then FORMAT := 'DOUBLE';
    if DocDOCOMETRE.CANINT.Items[i].FORMAT = 'Entier'
    then FORMAT := 'INTEGER';

    If InfoCanaux.purORparam[n] = 'PUR' then
    begin
      if DocDOCOMETRE.CANINT.Items[i].TAILLE = 1
      then CodeSimul_VAR.Add('     ' + DocDOCOMETRE.CANINT.Items[i].NOM + ' : ' + FORMAT + ';')
      else CodeSimul_VAR.Add('     ' + DocDOCOMETRE.CANINT.Items[i].NOM + ' : array[1..' +
                                   inttostr(DocDOCOMETRE.CANINT.Items[i].TAILLE)
                                                      + '] OF ' + FORMAT + ';');

      CodeSimul_VAR.Add('     Nouveau_' + DocDOCOMETRE.CANINT.Items[i].NOM + ' : INTEGER;');
      CodeSimul_VAR.Add('     Acquisition_' + DocDOCOMETRE.CANINT.Items[i].NOM + ' : INTEGER;');
      CodeSimul_Init_AcqGen.Add('Acquisition_' + DocDOCOMETRE.CANINT.Items[i].NOM + ' := 1;');
      CodeSimul_CODE.Add('Nouveau_' + DocDOCOMETRE.CANINT.Items[i].NOM + ' := 0;');
      CodeSimul_CODE.Add('IF (index = acquisition_' + DocDOCOMETRE.CANINT.Items[i].NOM +') THEN BEGIN');
      CodeSimul_CODE.Add('   acquisition_' + DocDOCOMETRE.CANINT.Items[i].NOM+' := acquisition_'+DocDOCOMETRE.CANINT.Items[i].NOM+'+'+InfoCanaux.RappFreq[n]+';');
      CodeSimul_CODE.Add('   Nouveau_'+DocDOCOMETRE.CANINT.Items[i].NOM+' := 1;');
      CodeSimul_CODE.Add('END;');
      CodeSimul_CODE.Add('');
      CodeSimul_generation.Add('IF (nouveau_'+DocDOCOMETRE.CANINT.Items[i].NOM+' = 1) THEN BEGIN');
      if DocDOCOMETRE.CANINT.Items[i].TAILLE = 1
      then CodeSimul_generation.Add('   TAB_DATA['+InfoCanaux.NumCanal[n]+'] := '+InfoCanaux.Nom[n]+';')
      else begin
        m := strtoint(InfoCanaux.NumCanal[n]);
        for j := 1 to DocDOCOMETRE.CANINT.Items[i].TAILLE do
        begin
          CodeSimul_generation.Add('   TAB_DATA['+inttostr(m+j-1)+'] := '+DocDOCOMETRE.CANINT.Items[i].NOM+'['+inttostr(j)+'];')
        end;
      end;
      CodeSimul_generation.Add('END;');
      CodeSimul_generation.Add('');
    end;
    if InfoCanaux.purORparam[n] = 'PARAM' then
    begin
      CodeSimul_VAR.Add('     ' + InfoCanaux.Nom[n] + ' : DOUBLE;');
      CodeSimul_Init_AcqGen.Add(InfoCanaux.Nom[n] + ' := TAB_PARAM[' + InfoCanaux.NumCanal[n] + '];');
    end;

  end;

  //Ports series
  for i := 0 to DocDOCOMETRE.PSERIE.Count - 1 do
  begin
    TraiterPSerie(DocDocometre.PSERIE.Items[i].SYSTEME,i+1);
  end;

  CodeSimul_Init_AcqGen.Add('REALTIME_LOOP_STATE := RLS_EVENT;');
  CodeSimul_Init_AcqGen.Add(' ');

  for i := 0 to CurentXMLST.BLOC.Count - 1 do
  begin
    GENECODE_BLOC(CurentXMLST.BLOC[i],0);
  end;


  //Cloture Code condition bloc DEBUT ET FIN
  for i := 0 to CurentXMLST.BLOC.Count - 1 do
  begin
    if CurentXMLST.BLOC[i].GENRE = 'DEBUT' then
    begin
      for j := CodeSimul_Init_AcqGen.Count - 1 downto 0
      do CodeSimul_LOWINIT.Insert(1,CodeSimul_Init_AcqGen[j]);
      CodeSimul_LOWINIT.Add(CurentXMLST.BLOC[i].DELPHICODE2);
    end;
    if CurentXMLST.BLOC[i].GENRE = 'FIN' then
    begin
      CodeSimul_FINISH.Add(' ');
      CodeSimul_FINISH.Add('REALTIME_LOOP_STATE := RLS_STOPPED;');
      CodeSimul_FINISH.Add(' ');
      if CurentXMLST.UseStim <> '' then
      begin
        S := CurentXMLST.BLOC[i].DELPHICODE2;
        system.Delete(S,pos('END.',S),4);
        S := S + '  DLLProc := @LibraryProc;' + #10 + 'END.';
        CodeSimul_FINISH.Add(S);
      end else CodeSimul_FINISH.Add(CurentXMLST.BLOC[i].DELPHICODE2);
    end;
  end;

  InfoCanaux.Free;
  CodeSimul_VAR.Add('     TEMPS : DOUBLE;');
  CodeSimul_VAR.Add(' ');

  //CodeSimul_generation.Add('inc(INDEX);');

  CodeSimul.AddStrings(CodeSimul_INCLUDE);
  CodeSimul.AddStrings(CodeSimul_IMPORT);
  CodeSimul.AddStrings(CodeSimul_VAR);


  if CurentXMLST.UseStim <> '' then
  begin
    CodeSimul_FCTSSP.Add(' ');
    CodeSimul_FCTSSP.Add('PROCEDURE LibraryProc(Reason : integer);');
    CodeSimul_FCTSSP.Add('BEGIN');
    CodeSimul_FCTSSP.Add('  IF Reason = 0 THEN');
    CodeSimul_FCTSSP.Add('  BEGIN');
    CodeSimul_FCTSSP.Add('    ' + CurentXMLST.UseStim);
    S := CurentXMLST.UseStim;
    repeat
      System.Delete(S,pos('Values',S),6);
    until pos('FreeValues',S) = 0;
    CodeSimul_FCTSSP.Add('    ' + S);

    CodeSimul_FCTSSP.Add('  END;');
    CodeSimul_FCTSSP.Add('END;');
    CodeSimul_FCTSSP.Add(' ')
  end;

  CodeSimul.AddStrings(CodeSimul_FCTSSP);
  CodeSimul.AddStrings(CodeSimul_LIBRARY);

  CodeSimul.AddStrings(CodeSimul_LOWINIT);
  CodeSimul.AddStrings(CodeSimul_CODE);
  CodeSimul.AddStrings(CodeSimul_generation);
  CodeSimul.AddStrings(CodeSimul_FINISH);

  ForceDirectories(Rep_Manip + 'BinSources');
  CodeSimul.SaveToFile(Rep_Manip + 'BinSources\ST' +
                       inttostr(STCombobox.itemIndex + 1) + '.PAS');

  CodeSimul_INCLUDE.Free;
  CodeSimul_IMPORT.Free;
  CodeSimul_VAR.Free;
  CodeSimul_LOWINIT.Free;
  CodeSimul_CODE.Free;
  CodeSimul_FINISH.Free;
  CodeSimul_FCTSSP.Free;
  CodeSimul_LIBRARY.Free;
  CodeSimul_generation.Free;
  CodeSimul_Init_AcqGen.Free;
  CodeSimul.Free;
end;

procedure TSimulFrame.ReinitSimulActionExecute(Sender: TObject);
var Library_Name,Fichier_Sortie,Stemp : string;
    RapportErreur : TStringList;
    i : integer;
    ErreurCompil : boolean;
    AGrapheSimulFrame : TGrapheSimulFrame;
    AGrapheTabSimulForm : TGrapheTabSimulForm;
    Freq, c1, c2 : int64;
    time : double;

    function CompilST:boolean;
    var buffer     :   LPTSTR;
        StartupInfo : TStartupInfo;
        ProcessInfo : TProcessInformation;
        LigneDeCmde : TStringList;
    begin
      Fichier_Sortie := Rep_Manip + 'BinSources\ST' +
                        inttostr(STCombobox.itemIndex + 1) + '.OUT';
      if fileexists(Fichier_Sortie) then deleteFile(Fichier_Sortie);

      Fichier_Sortie := Rep_Manip + 'BinSources\ST' +
                        inttostr(STCombobox.itemIndex + 1) + '.dll';
      if fileexists(Fichier_Sortie) then deleteFile(Fichier_Sortie);

      Fichier_Sortie := Rep_Manip + 'BinSources\ST' +
                        inttostr(STCombobox.itemIndex + 1) + '.OUT';

      //Creation de la ligne de commande
      //DCC32.EXE -DSIMUL c:\....\ST5.pas -> ST5.out
      //Sauvegarde ds Compile.bat
      Stemp := Rep_racine + 'libs';
      LigneDeCmde := TStringList.Create;
      LigneDeCmde.Add('"' + Rep_racine + 'DCC32.EXE" -DSIMUL -B -U"' + Stemp + '" -E"'+Rep_Manip + 'BinSources" "'
                        + Rep_Manip + 'BinSources\ST');
      LigneDeCmde[0] := LigneDeCmde[0] + inttostr(STCombobox.itemIndex + 1)
                        + '.pas" > "' + Rep_manip + 'BinSources\ST';
      LigneDeCmde[0] := LigneDeCmde[0] + inttostr(STCombobox.itemIndex+1) + '.out"';
      LigneDeCmde.SaveToFile(Rep_Manip + 'BinSources\Compile.bat');
      LigneDeCmde.free;
      //Execution de Compile.bat
      buffer := LPCTSTR(Rep_manip + 'BinSources\Compile.bat');

      FillChar(StartupInfo,Sizeof(StartupInfo),#0);
      StartupInfo.cb := Sizeof(StartupInfo);
      StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
      StartupInfo.wShowWindow := SW_HIDE;	//SW_SHOW;

      ErreurCompil := false;

      result := CreateProcess(buffer,
                              nil,   { pointer to command line string }
                              nil,   { pointer to process security attributes }
                              nil,   { pointer to thread security attributes }
                              false, { handle inheritance flag }
                              CREATE_NEW_CONSOLE or
                              NORMAL_PRIORITY_CLASS, { creation flags }
                              nil,   { pointer to new environment block }
                              nil,   { pointer to current directory name }
                              StartupInfo,  { pointer to STARTUPINFO }
                              ProcessInfo) ; { pointer to PROCESS_INF }

      WaitForSingleObject(ProcessInfo.hProcess,INFINITE);
      
    end;

begin
  Screen.Cursor := crHourGlass	;
  StopSimulActionExecute(Self);
  if SimulThread <> nil then
  begin
    QueryPerformanceFrequency(Freq);
    QueryPerformanceCounter(c1);
    c2 := c1;
    repeat
      QueryPerformanceCounter(c2);
      time := (c2-c1)/freq;
      if SimulThread <> nil
      then WaitForSingleObject(SimulThread.Handle,10);
      Application.ProcessMessages;
    until (time > 1.0);
  end;
  if STCombobox.itemIndex > -1 then
  begin
    MessageRichEdit.Lines.Clear;
    //generation code pascal
    MessageRichEdit.SelAttributes.Style := [fsBold,fsUnderLine];
    MessageRichEdit.SelAttributes.Color := clWindowText;
    MessageRichEdit.Lines.Add('Etape génération de code - Traitement de "'
                                 + STCombobox.Text + '" :');
    MessageRichEdit.Lines.Add('');
    GeneCodeSimul;
    MessageRichEdit.SelAttributes.Style := [];
    MessageRichEdit.SelAttributes.Color := clWindowText;
    MessageRichEdit.Lines.Add('Génération OK');
    MessageRichEdit.Lines.Add('');
    //Compilation du code
    if not CompilST
    then showmessage('le fichier à exécuter n''existe pas')
    else begin
      DocoForm.delay(1000000);
      MessageRichEdit.SelAttributes.Style := [fsBold,fsUnderLine];
      MessageRichEdit.SelAttributes.Color := clWindowText;
      MessageRichEdit.Lines.Add('Etape compilation - Traitement de "'
                                 + STCombobox.Text + '" :');
      MessageRichEdit.Lines.Add('');
      if fileexists(Fichier_Sortie) then
      begin
        RapportErreur := TStringList.Create;
        RapportErreur.Sorted := false;
        RapportErreur.LoadFromFile(Fichier_Sortie);
        for i := 0 to RapportErreur.Count - 1 do
        begin
          MessageRichEdit.SelAttributes.Style := [];
          MessageRichEdit.SelAttributes.Color := clWindowText;
          if (pos('Erreur',RapportErreur.Strings[i]) <> 0) or
             (pos('Fatale',RapportErreur.Strings[i]) <> 0) or
             (pos('Impossible',RapportErreur.Strings[i]) <> 0) or
             (pos('Error',RapportErreur.Strings[i]) <> 0) or
             (pos('Fatal',RapportErreur.Strings[i]) <> 0) or
             (pos('Impossible',RapportErreur.Strings[i]) <> 0)
             then
          begin
            ErreurCompil := true;
            MessageRichEdit.SelAttributes.Style := [fsBold];
            MessageRichEdit.SelAttributes.Color := clRed;
            RapportErreur.Strings[i] := '*** ' + RapportErreur.Strings[i];
          end else RapportErreur.Strings[i] := '      ' + RapportErreur.Strings[i];
          MessageRichEdit.Lines.Add(RapportErreur.Strings[i]);
        end;
        RapportErreur.Free;
        MessageRichEdit.SelAttributes.Style := [];
        MessageRichEdit.SelAttributes.Color := clWindowText;
        if ErreurCompil
        then MessageRichEdit.Lines.Add('Compilation KO')
        else MessageRichEdit.Lines.Add('Compilation OK');
      end else MessageRichEdit.Lines.Add('Compilation OK');
      MessageRichEdit.SelAttributes.Color := clWindowText;
      MessageRichEdit.Lines.Add('');
    end;

    //Reinitialisation des differents Param de la simulation
    if not ErreurCompil then
    begin
      Library_Name := Rep_Manip + 'BinSources\ST' + inttostr(STCombobox.itemIndex + 1) + '.dll';
      SimulThread := TSimulThread.Create(DocDOCOMETRE.EXPERIENCE.FEG,Library_Name);
      MessageRichEdit.SelAttributes.Style := [fsBold];
      MessageRichEdit.SelAttributes.Color := clWindowText;
      MessageRichEdit.Lines.Add(SimulThread.Initialize);
      DocoForm.Delay(1000000);
      if SimulThread <> nil then
      begin
        SimulStatusBar.Panels[0].Text := sETAT + 'prêt';
        SimulStatusBar.Panels[1].Text := sTEMPS + '0.0';
        RunSimulAction.Enabled := true;
      end;
      for i := 0 to GraphScrollBox.controlCount - 1 do
      begin
        AGrapheSimulFrame := (GraphScrollBox.Controls[i] as  TGrapheSimulFrame);
        AGrapheSimulFrame.Reinit;
      end;

      if ListeGrapheTabSimulForm <> nil
      then for i := 0 to ListeGrapheTabSimulForm.Count - 1 do
           begin
             AGrapheTabSimulForm := ListeGrapheTabSimulForm[i];
             AGrapheTabSimulForm.Reinit;
           end;

    end else
    begin
      SimulStatusBar.Panels[0].Text := sETAT + 'ERREUR';
      SimulStatusBar.Panels[1].Text := sTEMPS;
      TabSet1.TabIndex := 1;
      TabSet1Click(Self);
    end;

    if InspectCodePanel2.Visible then
    begin
      i := InspectCodeMemo.SelStart;
      showInspectCodeSpeedButtClick(self);
      InspectCodeMemo.SelStart := i;
      SendMessage(InspectCodeMemo.handle, EM_SCROLLCARET,0,0);
    end;
  end;
  Screen.Cursor := crDefault;
end;

procedure TSimulFrame.RunSimulActionExecute(Sender: TObject);
var i : integer;
    AGrapheSimulFrame : TGrapheSimulFrame;
begin
  if SimulThread <> nil then
  begin
    if SimulThread.Suspended then
    begin
      RunSimulAction.ImageIndex := 122;
      if SimulThread.REALTIME_LOOP_STATE = RLS_READY then
      begin
        VisuWidth := strtofloat(TailleFenetreVisuLabeledEdit.Text);
        MAJ_After := strtofloat(MAJAffichageLabeledEdit.Text);
        for i := 0 to GraphScrollBox.ControlCount - 1 do
        begin
          AGrapheSimulFrame := (GraphScrollBox.Controls[i] as  TGrapheSimulFrame);
          AGrapheSimulFrame.SetVisuBufferLength(VisuWidth,MAJ_After);
        end;
      end;
      RunSimulAction.Hint := 'Suspendre la simulation';
      SimulThread.Resume;
    end else
    begin
      RunSimulAction.ImageIndex := 65;
      RunSimulAction.Hint := 'Lancer la simulation';
      SimulStatusBar.Panels[0].Text := SimulStatusBar.Panels[0].Text + ' / PAUSE';
      SimulThread.Suspend;
      for i := 0 to GraphScrollBox.ControlCount - 1 do
      begin
        AGrapheSimulFrame := (GraphScrollBox.Controls[i] as  TGrapheSimulFrame);
        AGrapheSimulFrame.FlushGraph;
      end;
      SimulThread.MAJValueList;
    end;
  end;
end;

procedure TSimulFrame.StopSimulActionExecute(Sender: TObject);
begin
  if SimulThread <> nil then
  begin
    if SimulThread.Suspended then SimulThread.Resume;
    SimulThread.Terminate;
  end;
  RunSimulAction.Hint := 'Lancer la simulation';
  RunSimulAction.ImageIndex := 65;
end;

procedure TSimulFrame.MAJ_TailleGraphes;
var i : integer;
    AGrapheSimulFrame : TGrapheSimulFrame;
begin
  for i := GraphScrollBox.ControlCount - 1 downto 0  do
  begin
    AGrapheSimulFrame := (GraphScrollBox.Controls[i] as  TGrapheSimulFrame);
    AGrapheSimulFrame.Visible := false;
  end;

  for i := 0 to GraphScrollBox.ControlCount - 1 do
  begin
    AGrapheSimulFrame := (GraphScrollBox.Controls[i] as  TGrapheSimulFrame);
    AGrapheSimulFrame.Height := round(GraphScrollBox.ClientHeight / NBGraphes);
  end;

  for i := 0 to GraphScrollBox.ControlCount - 1 do
  begin
    AGrapheSimulFrame := (GraphScrollBox.Controls[i] as  TGrapheSimulFrame);
    AGrapheSimulFrame.Visible := true;
  end;
end;


procedure TSimulFrame.AjouterGraphe(Nom:string);
var NewGrapheSimulFrame,AGrapheSimulFrame : TGrapheSimulFrame;
    NewGrapheTabSimulForm : TGrapheTabSimulForm;
    i : integer;
    InfoCanaux : TInfoCanaux;
    NomObjetGraphe : String;
    test1,test2 : boolean;
begin
  NomObjetGraphe := Nom;
  InfoCanaux := DocDOCOMETRE.RendreListeInfoCanaux;

  test1 := InfoCanaux.ISArray[InfoCanaux.Nom.IndexOf(Nom)] = '0';//c pas un tab
  test2 := InfoCanaux.ISArray[InfoCanaux.Nom.IndexOf(Nom)] = '1';//c un tab
  test2 := test2 and (pos('[',NomObjetGraphe) > 0);//c un elem de tab

  if (test1 or test2) then
  begin
    if InfoCanaux.ISArray[InfoCanaux.Nom.IndexOf(Nom)] = '1' then
    begin
       delete(NomObjetGraphe,pos('[',NomObjetGraphe),1);
       delete(NomObjetGraphe,pos(']',NomObjetGraphe),1);
    end;
    NewGrapheSimulFrame := TGrapheSimulFrame.Create(Self);
    NewGrapheSimulFrame.Parent := GraphScrollBox;
    NewGrapheSimulFrame.Name := NomObjetGraphe + 'GrapheSimulFrame';
    NewGrapheSimulFrame.NomCanalChartPanel.Caption := Nom;
    //NewGrapheSimulFrame.Top := 1000;
    NewGrapheSimulFrame.Align := alTop;
    NewGrapheSimulFrame.NomCanalChartPanel.Width :=
    (GraphScrollBox.Controls[0] as  TGrapheSimulFrame).NomCanalChartPanel.Width;
    NewGrapheSimulFrame.CanalChart.BottomAxis.Visible :=
                          AfficherAxesAbscissesCheckBox.Checked;
    NewGrapheSimulFrame.CanalChart.LeftAxis.Visible :=
                          AfficherAxesOrdonneesCheckBox.Checked;
    NewGrapheSimulFrame.CanalChart.LeftAxis.Grid.Visible :=
                          AfficherGrillesCheckBox.Checked;
    NewGrapheSimulFrame.CanalChart.BottomAxis.Grid.Visible :=
                          AfficherGrillesCheckBox.Checked;
    NewGrapheSimulFrame.Series1.Stairs := CourbesEscalierCheckBox.Checked;


    NewGrapheSimulFrame.Fe := DocDOCOMETRE.EXPERIENCE.FEG/strtofloat(InfoCanaux.RappFreq[InfoCanaux.Nom.IndexOf(Nom)]);
    NewGrapheSimulFrame.IncrementIndex := strtoint(InfoCanaux.RappFreq[InfoCanaux.Nom.IndexOf(Nom)]);
    NewGrapheSimulFrame.Data_Index := strtoint(InfoCanaux.NumCanal[InfoCanaux.Nom.IndexOf(Nom)]);
    NewGrapheSimulFrame.inORoutORcanint := InfoCanaux.inORoutORcanint[InfoCanaux.Nom.IndexOf(Nom)];

    inc(NBGraphes);
    //Ajouter les graphes existants a la liste du nouveau graphe
    for i := 0 to GraphScrollBox.ControlCount - 1 do
    begin
      AGrapheSimulFrame := (GraphScrollBox.Controls[i] as  TGrapheSimulFrame);
      if AGrapheSimulFrame <> NewGrapheSimulFrame
      then NewGrapheSimulFrame.ListeGrapheSimulFrame.Add(AGrapheSimulFrame);
    end;
    //Ajouter le nouveau graphe aux listes des graphes existants
    for i := 0 to GraphScrollBox.ControlCount - 1 do
    begin
      AGrapheSimulFrame := (GraphScrollBox.Controls[i] as  TGrapheSimulFrame);
      if AGrapheSimulFrame <> NewGrapheSimulFrame
      then AGrapheSimulFrame.ListeGrapheSimulFrame.Add(NewGrapheSimulFrame);
    end;
    MAJ_TailleGraphes;
  end else
  begin
    //c un tableau
    NewGrapheTabSimulForm := TGrapheTabSimulForm.Create(Self);
    NewGrapheTabSimulForm.Name := NomObjetGraphe + 'GrapheTabSimulForm';
    NewGrapheTabSimulForm.Parent := Self;
    NewGrapheTabSimulForm.Visible := true;

    NewGrapheTabSimulForm.CanalChart.BottomAxis.Visible :=
                          AfficherAxesAbscissesCheckBox.Checked;
    NewGrapheTabSimulForm.CanalChart.LeftAxis.Visible :=
                          AfficherAxesOrdonneesCheckBox.Checked;
    NewGrapheTabSimulForm.CanalChart.LeftAxis.Grid.Visible :=
                          AfficherGrillesCheckBox.Checked;
    NewGrapheTabSimulForm.CanalChart.BottomAxis.Grid.Visible :=
                          AfficherGrillesCheckBox.Checked;
    NewGrapheTabSimulForm.Series1.Stairs := CourbesEscalierCheckBox.Checked;

    NewGrapheTabSimulForm.Fe := DocDOCOMETRE.EXPERIENCE.FEG/strtofloat(InfoCanaux.RappFreq[InfoCanaux.Nom.IndexOf(Nom)]);
    NewGrapheTabSimulForm.IncrementIndex := strtoint(InfoCanaux.RappFreq[InfoCanaux.Nom.IndexOf(Nom)]);
    NewGrapheTabSimulForm.Data_Index := strtoint(InfoCanaux.NumCanal[InfoCanaux.Nom.IndexOf(Nom)]);
    NewGrapheTabSimulForm.SetNom(Nom);
    NewGrapheTabSimulForm.Data_length := strtoint(InfoCanaux.ArraySizeOrElementNum[infoCanaux.Nom.IndexOf(Nom)]);
    if ListeGrapheTabSimulForm = nil then ListeGrapheTabSimulForm := TList.Create;
    ListeGrapheTabSimulForm.Add(NewGrapheTabSimulForm);

  end;

  InfoCanaux.Free;
end;


procedure TSimulFrame.SupprimerGraphe(Nom:string);
var i : integer;              
    InfoCanaux : TInfoCanaux;
    TheGrapheSimulFrame,AGrapheSimulFrame : TGrapheSimulFrame;
    TheGrapheTabSimulForm : TGrapheTabSimulForm;
    test1,test2 : boolean;
begin
  InfoCanaux := DocDOCOMETRE.RendreListeInfoCanaux;
  test1 := InfoCanaux.ISArray[InfoCanaux.Nom.IndexOf(Nom)] = '0';//c pas un tab
  test2 := InfoCanaux.ISArray[InfoCanaux.Nom.IndexOf(Nom)] = '1';//c un tab
  test2 := test2 and (pos('[',Nom) > 0);//c un elem de tab

  if (test1 or test2) then
  begin
    TheGrapheSimulFrame := nil;
    for i := 0 to GraphScrollBox.ControlCount - 1 do
    begin
      TheGrapheSimulFrame := (GraphScrollBox.Controls[i] as  TGrapheSimulFrame);
      if TheGrapheSimulFrame.NomCanalChartPanel.Caption = Nom
      then break;
    end;

    for i := 0 to GraphScrollBox.ControlCount - 1 do
    begin
      AGrapheSimulFrame := (GraphScrollBox.Controls[i] as  TGrapheSimulFrame);
      if AGrapheSimulFrame <> TheGrapheSimulFrame
      then AGrapheSimulFrame.SuppRefGrapheSimulFrame(TheGrapheSimulFrame);
    end;

    TheGrapheSimulFrame.ListeGrapheSimulFrame.Free;
    TheGrapheSimulFrame.Free;
    dec(NBGraphes);
    MAJ_TailleGraphes;
  end else
  begin

    for i := 0 to ListeGrapheTabSimulForm.Count - 1 do
    begin
      TheGrapheTabSimulForm := ListeGrapheTabSimulForm[i];
      if TheGrapheTabSimulForm.GetNom = Nom then
      begin
        ListeGrapheTabSimulForm.Delete(i);
        TheGrapheTabSimulForm.Close;
        break;
      end;
    end;

    if ListeGrapheTabSimulForm.Count = 0 then
    begin
      ListeGrapheTabSimulForm.Free;
      ListeGrapheTabSimulForm := nil;
    end;

  end;
  InfoCanaux.Free;
end;


procedure TSimulFrame.SupprimerGraphe(TheGrapheSimulFrame:TGrapheSimulFrame);
var i : integer;
    AGrapheSimulFrame : TGrapheSimulFrame;
begin
  for i := 0 to GraphScrollBox.ControlCount - 1 do
  begin
    AGrapheSimulFrame := (GraphScrollBox.Controls[i] as  TGrapheSimulFrame);
    if AGrapheSimulFrame <> TheGrapheSimulFrame
    then AGrapheSimulFrame.SuppRefGrapheSimulFrame(TheGrapheSimulFrame);
  end;

  TheGrapheSimulFrame.ListeGrapheSimulFrame.Free;
  TheGrapheSimulFrame.Free;
  dec(NBGraphes);
  MAJ_TailleGraphes;
end;

procedure TSimulFrame.SupprimerGraphe(TheGrapheTabSimulForm:TGrapheTabSimulForm);
begin
  //
end;

procedure TSimulFrame.GraphCheckListBoxClickCheck(Sender: TObject);
begin
  if GraphCheckListBox.Checked[GraphCheckListBox.ItemIndex]
  then AjouterGraphe(GraphCheckListBox.Items[GraphCheckListBox.ItemIndex])
  else SupprimerGraphe(GraphCheckListBox.Items[GraphCheckListBox.ItemIndex]);
end;

procedure TSimulFrame.FrameResize(Sender: TObject);
begin
	MAJ_TailleGraphes;
	ContainerSplitter1.Refresh;
	InspectCodeSplitter.Refresh;
	Self.Refresh;
end;

procedure TSimulFrame.InspectCodeSplitterMoved(Sender: TObject);
begin
	MAJ_TailleGraphes;
	InspectCodePanel.Refresh;
  InspectCodeSplitter.Refresh;
end;

procedure TSimulFrame.MAJ_SplitterGraphes(Width : integer);
var i : integer;
		AGrapheSimulFrame : TGrapheSimulFrame;
begin
  for i := 0 to GraphScrollBox.ControlCount - 1 do
  begin
    AGrapheSimulFrame := (GraphScrollBox.Controls[i] as  TGrapheSimulFrame);
    AGrapheSimulFrame.NomCanalChartPanel.Width := Width;
  end;
end;

procedure TSimulFrame.InspectCodeMemoKeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
  LigneNumPanel.Caption:=' Ligne n°'+IntToStr(InspectCodeMemo.CaretPos.Y+1);
end;

procedure TSimulFrame.InspectCodeMemoEnter(Sender: TObject);
begin
  LigneNumPanel.Caption:=' Ligne n°'+IntToStr(InspectCodeMemo.CaretPos.Y+1);
end;

procedure TSimulFrame.AfficherAxesAbscissesCheckBoxClick(Sender: TObject);
var i : integer;
    AGrapheSimulFrame : TGrapheSimulFrame;
    GrapheTabSimulForm : TGrapheTabSimulForm;
begin
  for i := 0 to GraphScrollBox.ControlCount - 1 do
  begin
    AGrapheSimulFrame := (GraphScrollBox.Controls[i] as  TGrapheSimulFrame);
    AGrapheSimulFrame.CanalChart.BottomAxis.Visible :=
              AfficherAxesAbscissesCheckBox.Checked;
  end;
  if Self.ListeGrapheTabSimulForm <> nil
  then for i := 0 to Self.ListeGrapheTabSimulForm.Count - 1 do
       begin
         GrapheTabSimulForm := ListeGrapheTabSimulForm[i];
         GrapheTabSimulForm.CanalChart.BottomAxis.Visible :=
              AfficherAxesAbscissesCheckBox.Checked;
       end;
end;

procedure TSimulFrame.AfficherAxesOrdonneesCheckBoxClick(Sender: TObject);
var i : integer;
    AGrapheSimulFrame : TGrapheSimulFrame;
    GrapheTabSimulForm : TGrapheTabSimulForm;
begin
  for i := 0 to GraphScrollBox.ControlCount - 1 do
  begin
    AGrapheSimulFrame := (GraphScrollBox.Controls[i] as  TGrapheSimulFrame);
    AGrapheSimulFrame.CanalChart.LeftAxis.Visible :=
              AfficherAxesOrdonneesCheckBox.Checked;
  end;
  if Self.ListeGrapheTabSimulForm <> nil
  then for i := 0 to Self.ListeGrapheTabSimulForm.Count - 1 do
       begin
         GrapheTabSimulForm := ListeGrapheTabSimulForm[i];
         GrapheTabSimulForm.CanalChart.LeftAxis.Visible :=
              AfficherAxesOrdonneesCheckBox.Checked;
       end;

end;

procedure TSimulFrame.AfficherGrillesCheckBoxClick(Sender: TObject);
var i : integer;
    AGrapheSimulFrame : TGrapheSimulFrame;
    GrapheTabSimulForm : TGrapheTabSimulForm;
begin
  for i := 0 to GraphScrollBox.ControlCount - 1 do
  begin
    AGrapheSimulFrame := (GraphScrollBox.Controls[i] as  TGrapheSimulFrame);
    AGrapheSimulFrame.CanalChart.LeftAxis.Grid.Visible :=
                AfficherGrillesCheckBox.Checked;
    AGrapheSimulFrame.CanalChart.BottomAxis.Grid.Visible :=
                AfficherGrillesCheckBox.Checked;
  end;
  if Self.ListeGrapheTabSimulForm <> nil
  then for i := 0 to Self.ListeGrapheTabSimulForm.Count - 1 do
       begin
         GrapheTabSimulForm := ListeGrapheTabSimulForm[i];
         GrapheTabSimulForm.CanalChart.LeftAxis.Grid.Visible :=
                AfficherGrillesCheckBox.Checked;
         GrapheTabSimulForm.CanalChart.BottomAxis.Grid.Visible :=
                AfficherGrillesCheckBox.Checked;
       end;

end;

procedure TSimulFrame.AfficherPtsSimulCheckBoxClick(Sender: TObject);
var i : integer;
    AGrapheSimulFrame : TGrapheSimulFrame;
    GrapheTabSimulForm : TGrapheTabSimulForm;
begin
  AfficherPtSimulGroupBox.Enabled := AfficherPtsSimulCheckBox.Checked;
  typePtCombobox.Enabled := AfficherPtSimulGroupBox.Enabled;
  TailleSpinEdit.Enabled := AfficherPtSimulGroupBox.Enabled;
  AfficherValeurPtCheckBox.Enabled := AfficherPtSimulGroupBox.Enabled;
  for i := 0 to GraphScrollBox.ControlCount - 1 do
  begin
    AGrapheSimulFrame := (GraphScrollBox.Controls[i] as  TGrapheSimulFrame);
    AGrapheSimulFrame.Series1.Pointer.Visible := AfficherPtsSimulCheckBox.Checked;
    AGrapheSimulFrame.Series1.Marks.Visible := AfficherValeurPtCheckBox.Checked;
  end;

  if Self.ListeGrapheTabSimulForm <> nil
  then for i := 0 to Self.ListeGrapheTabSimulForm.Count - 1 do
       begin
         GrapheTabSimulForm := ListeGrapheTabSimulForm[i];
         GrapheTabSimulForm.Series1.Pointer.Visible := AfficherPtsSimulCheckBox.Checked;
         GrapheTabSimulForm.Series1.Marks.Visible := AfficherValeurPtCheckBox.Checked;
       end;

  if AfficherPtsSimulCheckBox.Checked then
  begin
    TypePtComboBoxChange(Self);
    //AfficherValeurPtCheckBoxClick(self);
    TailleSpinEditChange(Self);
  end;
end;

procedure TSimulFrame.TypePtComboBoxChange(Sender: TObject);
var i : integer;
    AGrapheSimulFrame : TGrapheSimulFrame;
    GrapheTabSimulForm : TGrapheTabSimulForm;
begin
  for i := 0 to GraphScrollBox.ControlCount - 1 do
  begin
    AGrapheSimulFrame := (GraphScrollBox.Controls[i] as  TGrapheSimulFrame);
    AGrapheSimulFrame.Series1.Pointer.Style := TSeriesPointerStyle(TypePtComboBox.ItemIndex);
  end;
  if Self.ListeGrapheTabSimulForm <> nil
  then for i := 0 to Self.ListeGrapheTabSimulForm.Count - 1 do
       begin
         GrapheTabSimulForm := ListeGrapheTabSimulForm[i];
         GrapheTabSimulForm.Series1.Pointer.Style := TSeriesPointerStyle(TypePtComboBox.ItemIndex);
       end;

end;

procedure TSimulFrame.AfficherValeurPtCheckBoxClick(Sender: TObject);
var i : integer;
    AGrapheSimulFrame : TGrapheSimulFrame;
    GrapheTabSimulForm : TGrapheTabSimulForm;
begin
  for i := 0 to GraphScrollBox.ControlCount - 1 do
  begin
    AGrapheSimulFrame := (GraphScrollBox.Controls[i] as  TGrapheSimulFrame);
    AGrapheSimulFrame.Series1.Marks.Visible := AfficherValeurPtCheckBox.Checked;
  end;

  if Self.ListeGrapheTabSimulForm <> nil
  then for i := 0 to Self.ListeGrapheTabSimulForm.Count - 1 do
       begin
         GrapheTabSimulForm := ListeGrapheTabSimulForm[i];
         GrapheTabSimulForm.Series1.Marks.Visible := AfficherValeurPtCheckBox.Checked;
       end;

end;

procedure TSimulFrame.TailleSpinEditChange(Sender: TObject);
var i : integer;
    AGrapheSimulFrame : TGrapheSimulFrame;
    GrapheTabSimulForm : TGrapheTabSimulForm;
begin
  for i := 0 to GraphScrollBox.ControlCount - 1 do
  begin
    AGrapheSimulFrame := (GraphScrollBox.Controls[i] as  TGrapheSimulFrame);
    AGrapheSimulFrame.Series1.Pointer.HorizSize := TailleSpinEdit.Value;
    AGrapheSimulFrame.Series1.Pointer.VertSize := TailleSpinEdit.Value;
  end;
  if Self.ListeGrapheTabSimulForm <> nil
  then for i := 0 to Self.ListeGrapheTabSimulForm.Count - 1 do
       begin
         GrapheTabSimulForm := ListeGrapheTabSimulForm[i];
         GrapheTabSimulForm.Series1.Pointer.HorizSize := TailleSpinEdit.Value;
         GrapheTabSimulForm.Series1.Pointer.VertSize := TailleSpinEdit.Value;
       end;

end;

procedure TSimulFrame.CourbesEscalierCheckBoxClick(Sender: TObject);
var i : integer;
    AGrapheSimulFrame : TGrapheSimulFrame;
begin
  for i := 0 to GraphScrollBox.ControlCount - 1 do
  begin
    AGrapheSimulFrame := (GraphScrollBox.Controls[i] as  TGrapheSimulFrame);
    AGrapheSimulFrame.Series1.Stairs := CourbesEscalierCheckBox.Checked;
  end;
  if Self.ListeGrapheTabSimulForm <> nil
  then for i := 0 to Self.ListeGrapheTabSimulForm.Count - 1 do
       begin
         GrapheTabSimulForm := ListeGrapheTabSimulForm[i];
         GrapheTabSimulForm.Series1.Stairs := CourbesEscalierCheckBox.Checked;
       end;

end;

procedure TSimulFrame.EpaisseurSpinEditChange(Sender: TObject);
var i : integer;
    AGrapheSimulFrame : TGrapheSimulFrame;
    GrapheTabSimulForm : TGrapheTabSimulForm;
begin
  for i := 0 to GraphScrollBox.ControlCount - 1 do
  begin
    AGrapheSimulFrame := (GraphScrollBox.Controls[i] as  TGrapheSimulFrame);
    AGrapheSimulFrame.Series1.LinePen.Width := EpaisseurSpinEdit.Value;
  end;
  if Self.ListeGrapheTabSimulForm <> nil
  then for i := 0 to Self.ListeGrapheTabSimulForm.Count - 1 do
       begin
         GrapheTabSimulForm := ListeGrapheTabSimulForm[i];
         GrapheTabSimulForm.Series1.LinePen.Width := EpaisseurSpinEdit.Value;
       end;

end;

procedure TSimulFrame.ClearGenerators;
var i : integer;
begin
  for i := 0 to GraphCheckListBox.Items.Count - 1 do
  begin
    if GraphCheckListBox.Items.Objects[i] <> nil
    then (GraphCheckListBox.Items.Objects[i] as TGenerator).FreeValues;
    //(GraphCheckListBox.Items.Objects[i] as TGenerator).Free;
    GraphCheckListBox.Items.Objects[i].Free;
    GraphCheckListBox.Items.Objects[i] := nil;
  end;
end;

procedure TSimulFrame.UseGeneratorActionExecute(Sender: TObject);
var n : integer;
begin
  n := GraphCheckListBox.ItemIndex;
  if GraphCheckListBox.Items.Objects[n] = nil then GeneratorActionExecute(Self);
  (GraphCheckListBox.Items.Objects[n] as TGenerator).Used := UseGeneratorAction.Checked;
end;

procedure TSimulFrame.GeneratorActionExecute(Sender: TObject);
var InfoCanaux : TInfoCanaux;
    RF,n : integer;
    Generator : TGenerator;
begin
  n := GraphCheckListBox.ItemIndex;
  GeneratorForm.ChannelName := GraphCheckListBox.Items[n];
  if GraphCheckListBox.Items.Objects[n] <> nil
  then GeneratorForm.Generator := GraphCheckListBox.Items.Objects[n] as TGenerator
  else begin
    InfoCanaux := DocDOCOMETRE.RendreListeInfoCanaux;
    RF := strtoint(InfoCanaux.RappFreq[InfoCanaux.Nom.IndexOf(GeneratorForm.ChannelName)]);
    Generator := TGenerator.CreateGenerator(DocDOCOMETRE.EXPERIENCE.FEG / RF);
    Generator.FloatOrInt := InfoCanaux.floatORint[InfoCanaux.Nom.IndexOf(GeneratorForm.ChannelName)];
    InfoCanaux.Free;
    GraphCheckListBox.Items.Objects[n] := Generator;
    GeneratorForm.Generator := Generator;
  end;
  GeneratorForm.Hide;
  GeneratorForm.Show;
end;

procedure TSimulFrame.GeneratorPopupMenuPopup(Sender: TObject);
var n,index : integer;
    InfoCanaux : TInfoCanaux;
begin
  n := GraphCheckListBox.ItemIndex;
  GeneratorAction.Enabled := false;
  UseGeneratorAction.Enabled := false;
  if n > -1 then
  begin
    InfoCanaux := DocDOCOMETRE.RendreListeInfoCanaux;
    index := InfoCanaux.Nom.IndexOf(GraphCheckListBox.Items[n]);
    GeneratorAction.Enabled := (InfoCanaux.inORoutORcanint[index] = 'in');
    UseGeneratorAction.Enabled := (InfoCanaux.inORoutORcanint[index] = 'in');
    InfoCanaux.Free;
    if GraphCheckListBox.Items.Objects[n] <> nil
    then UseGeneratorAction.Checked := (GraphCheckListBox.Items.Objects[n] as TGenerator).Used
    else UseGeneratorAction.Checked :=  false;
  end;
end;



procedure TSimulFrame.GraphCheckListBoxDblClick(Sender: TObject);
var n,index : integer;
    InfoCanaux : TInfoCanaux;
begin
  n := GraphCheckListBox.ItemIndex;
  if n > -1 then
  begin
    InfoCanaux := DocDOCOMETRE.RendreListeInfoCanaux;
    index := InfoCanaux.Nom.IndexOf(GraphCheckListBox.Items[n]);
    if (InfoCanaux.inORoutORcanint[index] = 'in') then
    begin
      GeneratorActionExecute(Self);
    end;
    InfoCanaux.free;
  end;

end;

procedure TSimulFrame.HelpImageClick(Sender: TObject);
begin
  DocoForm.ShowHelp((Sender as TImage).Tag);
end;

procedure TSimulFrame.Image2Click(Sender: TObject);
begin
//  optPanel.Visible := false;
end;

procedure TSimulFrame.ShowGraphOptionsSpeedButClick(Sender: TObject);
begin
//	OptPanel.Visible := true;
//	OptPanel.Top := OptPanel.Parent.Height;
end;

procedure TSimulFrame.HideGraphOptionsSpeedButClick(Sender: TObject);
begin
//  OptPanel.Visible := false;
end;

procedure TSimulFrame.ShowCanOptMessPanelSpeedButtClick(Sender: TObject);
begin
	ContainerSplitter1.Visible := true;
	ContainerCanOptMessPanel.Width := CanOptMessPanel.Width + ShowHidePanel1.Width;
	CanOptMessPanel.Visible := true;
	ContainerSplitter1.Refresh;
	InspectCodeSplitter.Refresh;
end;

procedure TSimulFrame.HideCanOptMessPanelSpeedButtClick(Sender: TObject);
begin
	CanOptMessPanel.Visible := false;
	ContainerCanOptMessPanel.Width := ShowHidePanel1.Width;
	ContainerSplitter1.Visible := false;
	InspectCodeSplitter.Refresh;
end;

procedure TSimulFrame.showInspectCodeSpeedButtClick(Sender: TObject);
var fileName : string;
begin
	fileName := Rep_Manip + 'BinSources\ST' +
													 inttostr(STCombobox.itemIndex + 1) + '.PAS';
	if (STCombobox.itemIndex > -1) and fileExists(fileName) then
	begin
    InspectCodeMemo.Height := 300;

    InspectCodeMemo.Clear;
    InspectCodeMemo.Lines.LoadFromFile(fileName);

		InspectCodeSplitter.Visible := true;
		InspectCodePanel.Height := InspectCodePanel2.Height + ShowHidePanel3.Height;
		InspectCodePanel2.Visible := true;
		InspectCodeSplitter.Refresh;
		
		MAJ_TailleGraphes;
	end else showmessage('Aucune séquence type n''est sélectionnée ou il n''y a pas de'+
	                ' fichier source pour cette séquence type !');
	ContainerSplitter1.Refresh;
	InspectCodePanel.Refresh;
end;

procedure TSimulFrame.HideInspectCodeSpeedButtClick(Sender: TObject);
begin
	InspectCodePanel2.Visible := false;
	InspectCodePanel.Height := ShowHidePanel3.Height;
	InspectCodeSplitter.Visible := false;
	ContainerSplitter1.Refresh;
	MAJ_TailleGraphes;
end;

procedure TSimulFrame.STComboboxSelect(Sender: TObject);
begin
  STCombobox.Hint := STCombobox.Text;
end;

end.


