unit uflu41;

{$mode delphi}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  StdCtrls, ExtCtrls, LvlGraphCtrl,
  ComCtrls, urawdata, FPimage,
{$IFDEF OLD_FLUCAR}
  uprincipal,
{$ENDIF}
  uprincipal_raw, uactoresflucar;

type

  { TForm1 }

  TForm1 = class(TForm)
    btOldFlucar: TButton;
    btPrincipal_raw: TButton;
    Lienzo: TLvlGraphControl;
    TV_Flujo: TTreeView;
    Taps: TCheckBox;
    OpenDialog1: TOpenDialog;
    OpenDialog2: TOpenDialog;
    Timer1: TTimer;
    procedure btOldFlucarClick(Sender: TObject);
    procedure btPrincipal_rawClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure Mostrar_datosFlucar(sala:TSalaFlucar);
  private

  public

  end;

var
  Form1: TForm1;

implementation

{ TForm1 }

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  timer1.Enabled := False;
  //if paramcount = 2 then
    //correr_raw_con_modificaciones(ParamStr(1), ParamStr(2));
end;

procedure TForm1.btOldFlucarClick(Sender: TObject);
begin
  {$IFDEF OLD_FLUCAR}
  if (OpenDialog1.Execute) then
    Principal(OpenDialog1.FileName);
  {$ELSE}
    btOldFlucar.Enabled:= false;
  {$ENDIF}



end;

procedure TForm1.btPrincipal_rawClick(Sender: TObject);
var
  sala: TSalaFlucar;
begin
  TV_flujo.Items.Clear;
  Lienzo.Graph.Clear;

  if (OpenDialog2.Execute) then
  begin
//    sala:=TSalaFlucar.Create;  // Esto esta sobrando, se hace adentro de PrincipalRaw
    sala:=Principal_Raw(OpenDialog2.FileName,taps.Checked);//    Principal_Raw( OpenDialog2.FileName );
    Mostrar_datosFlucar(sala);
  end;

end;


procedure TForm1.FormCreate(Sender: TObject);
var
  RootNode: TTreeNode;
begin
  RootNode := TV_flujo.Items.AddFirst(nil, OpenDialog2.FileName);
  TV_flujo.Items.AddChild(RootNode, 'DESCRIPCION');
  TV_flujo.Items.AddChild(RootNode, 'BARRAS');
  TV_flujo.Items.AddChild(RootNode, 'CARGAS');
  TV_flujo.Items.AddChild(RootNode, 'GENERADORES');
  TV_flujo.Items.AddChild(RootNode, 'LINEAS');
  TV_flujo.Items.AddChild(RootNode, 'TRAFOS');
  TV_flujo.Items.AddChild(RootNode, 'SWITCHED SHUNTS');
  TV_flujo.Items.AddChild(RootNode, 'FIXED SHUNTS');
  TV_flujo.Items.AddChild(RootNode, 'MULTITERMINAL DC');
  TV_flujo.Items.AddChild(RootNode, 'VSC');
  TV_flujo.Items.AddChild(RootNode, 'FACTS');
  RootNode.Expanded := True;
end;

procedure TForm1.Mostrar_datosFlucar(sala: TSalaFlucar);
var
  RootNode, DescripcionTree, BarrasTree, CargasTree, GeneradoresTree,
    LineasTree, TrafosTree, SwitchedShuntTree, FixedShuntTree,
    TerminalesDCTree, VSCTree, FactsTree: TTreeNode;
  indice: Integer;
  nombreAux: String;
  NodoBarras, NodoTransformer: TLvlGraphNode;
begin
  with TV_Flujo do
  begin
    RootNode := Items.AddFirst(nil, OpenDialog2.FileName);

    DescripcionTree := Items.AddChild(RootNode, 'DESCRIPCION');
    Items.AddChild(DescripcionTree, 'IC='+IntToStr(TRaw_CaseIdentification(sala.caso[0]).IC));
    Items.AddChild(DescripcionTree, 'SBASE='+FloatToStr(TRaw_CaseIdentification(sala.caso[0]).sbase));
    Items.AddChild(DescripcionTree, 'REV='+IntToStr(TRaw_CaseIdentification(sala.caso[0]).REV));
    Items.AddChild(DescripcionTree, 'XFRRAT='+FloatToStr(TRaw_CaseIdentification(sala.caso[0]).XFRRAT));
    Items.AddChild(DescripcionTree, 'NXFRAT='+FloatToStr(TRaw_CaseIdentification(sala.caso[0]).NXFRAT));
    Items.AddChild(DescripcionTree, 'BASFRQ='+FloatToStr(TRaw_CaseIdentification(sala.caso[0]).BASFRQ));
    Items.AddChild(DescripcionTree, TRaw_CaseIdentification(sala.caso[0]).ds1);
    Items.AddChild(DescripcionTree, TRaw_CaseIdentification(sala.caso[0]).ds2);
    DescripcionTree.Expanded:=True;

    BarrasTree := Items.AddChild(RootNode, 'BARRAS');
    for indice:=0 to sala.Barras.Count-1 do
    begin
        Items.AddChild(BarrasTree,IntToStr(TRaw_Bus(sala.Barras[indice]).I));
    end;

    CargasTree := Items.AddChild(RootNode, 'CARGAS');
    for indice:=0 to sala.Cargas.Count-1 do
    begin
        Items.AddChild(CargasTree,IntToStr(TRaw_Load(sala.Cargas[indice]).I));
    end;

    GeneradoresTree := Items.AddChild(RootNode, 'GENERADORES');
    for indice:=0 to sala.Generadores.Count-1 do
    begin
        Items.AddChild(GeneradoresTree,IntToStr(TRaw_Generator(sala.Generadores[indice]).I));
    end;

    LineasTree := Items.AddChild(RootNode, 'LINEAS');
    for indice:=0 to sala.Lineas.Count-1 do
    begin
        Items.AddChild(LineasTree, IntToStr(TRaw_Branch(sala.Lineas[indice]).I) + '---' + IntToStr(TRaw_Branch(sala.Lineas[indice]).J));
    end;

    TrafosTree := Items.AddChild(RootNode, 'TRAFOS');
    for indice:=0 to sala.TrafosAdjust.Count-1 do
    begin
        nombreAux:=IntToStr(TRaw_TransformerAdjust(sala.TrafosAdjust[indice]).I) + '---' + IntToStr(TRaw_TransformerAdjust(sala.TrafosAdjust[indice]).J);
        if (TRaw_TransformerAdjust(sala.TrafosAdjust[indice]).K<>0) then
           nombreAux := nombreAux + '---' + IntToStr(TRaw_TransformerAdjust(sala.TrafosAdjust[indice]).K);
        Items.AddChild(TrafosTree, nombreAux);
    end;

    SwitchedShuntTree:=Items.AddChild(RootNode, 'SWITCHED SHUNTS');
    for indice:=0 to sala.Shunts.Count-1 do
    begin
        Items.AddChild(SwitchedShuntTree, IntToStr(TRaw_SwitchedShunt(sala.Shunts[indice]).I));
    end;

    FixedShuntTree:=Items.AddChild(RootNode, 'FIXED SHUNTS');
    for indice:=0 to sala.ShuntsFijos.Count-1 do
    begin
        Items.AddChild(FixedShuntTree, IntToStr(TRaw_FixedShunt(sala.ShuntsFijos[indice]).I));
    end;

    TerminalesDCTree := Items.AddChild(RootNode, 'TERMINALES DC');
    for indice:=0 to sala.TerminalesDC.Count-1 do
    begin
        Items.AddChild(TerminalesDCTree, IntToStr(TRaw_TwoTerminal_DCLine(sala.TerminalesDC[indice]).I) + '---' + IntToStr(TRaw_TwoTerminal_DCLine(sala.TerminalesDC[indice]).J));
    end;

    VSCTree := Items.AddChild(RootNode, 'VSC');
    for indice:=0 to sala.VSC.Count-1 do
    begin
        Items.AddChild(VSCTree, IntToStr(TRaw_VoltageSourceConverter_DCLine(sala.VSC[indice]).I) + '---' + IntToStr(TRaw_VoltageSourceConverter_DCLine(sala.VSC[indice]).J));
    end;

    FactsTree := Items.AddChild(RootNode, 'FACTS');
    for indice:=0 to sala.Facts.Count-1 do
    begin
        Items.AddChild(FactsTree, IntToStr(TRaw_Facts(sala.Facts[indice]).I) + '---' + IntToStr(TRaw_Facts(sala.Facts[indice]).J));
    end;
  end;
  RootNode.Expanded := True;

  with Lienzo do
  begin
    for indice:=0 to sala.Barras.Count-1 do
      NodoBarras:=Graph.GetNode(IntToStr(TRaw_Bus(sala.Barras[indice]).I),TRUE);

    for indice:=0 to sala.Lineas.Count-1 do
      Graph.GetEdge(IntToStr(TRaw_Branch(sala.Lineas[indice]).I), IntToStr(TRaw_Branch(sala.Lineas[indice]).J), true);

    for indice:=0 to sala.TrafosAdjust.Count-1 do
    begin
      NodoTransformer:=Graph.GetNode('T'+IntToStr(indice){+(TRaw_TransformerAdjust(sala.TrafosAdjust[indice]).Name)}, TRUE);
      Graph.GetEdge(IntToStr(TRaw_TransformerAdjust(sala.TrafosAdjust[indice]).I), NodoTransformer.Caption, true);
      Graph.GetEdge(NodoTransformer.Caption, IntToStr(TRaw_TransformerAdjust(sala.TrafosAdjust[indice]).J), true);
      if TRaw_TransformerAdjust(sala.TrafosAdjust[indice]).K<>0 then
         Graph.GetEdge(NodoTransformer.Caption, IntToStr(TRaw_TransformerAdjust(sala.TrafosAdjust[indice]).K), true);
    end;

    NodeStyle.Shape := lgnsEllipse;
    NodeStyle.Width := 15;
    NodeStyle.GapTop := 8;
    NodeStyle.CaptionPosition := lgncBottom;
    Font.Size:= 11;

  end;

end;


initialization
  {$I uflu41.lrs}

end.

