//---------------------------------------------------------------------------
#include <vcl.h>
#include <stdio.h>
#include <string.h>
#pragma hdrstop

#include <cstdlib>  //převod DoubleToString
#include <iostream> //převod DoubleToString
#include <sstream>  //převod DoubleToString
using namespace std;//převod DoubleToString

#include <math.h>
#include <cmath.h>
#include <conio.h>
#include <string.h>
#include <math.hpp>
#include "Main.h" // početní operace
#include "Graf.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
 float freq_axis[2000];
 float dBvalue[2000];
 float Pvalue[2000];
 boolean ZmenaNetList = false;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
 CompR = new TElePart();
 CompRS1 = new TElePart();
 CompRS2 = new TElePart();
 CompL = new TElePartL();
 CompC = new TElePart();
 Switch = new TElePartS();
 AOIdoz = new TEleAO();
 CompBuf = new TElePartB();
 CompM = new TElePartM();
 Nodes = new TEleNodes();
 CompIdoz = new TEleIDOZ();
 ZmenaNetList = false;
 // user friendly
   TabSheet2->TabVisible = false;
   TabSheet3->TabVisible = false;
   TabSheet4->TabVisible = false;
   TabSheet5->TabVisible = false;
   TabSheet6->TabVisible = false;
   TabSheet7->TabVisible = false;
   Button3->Visible = false;
   Button2->Visible = false;
   Button8->Visible = false;
   Panelin->Visible = false;
   Panelout->Visible = false;
   Label39->Visible = false;
   Label40->Visible = false;
   Label43->Visible = false;
   EditFS->Visible = false;
   EditD->Visible = false;
   CBHodDek->Visible = false;
   CBXmin->Visible = false;
   CBXmax->Visible = false;

}

//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
 Memo1->Clear();
 Memo2->Clear();
 Memo3->Clear();
 LV1->Clear();
 LV2->Clear();
 LV3->Clear();
 LV4->Clear();
 LV5->Clear();
 LV6->Clear();
 LV7->Clear();
 LV8->Clear();
 LV9->Clear();
 LV10->Clear();
 LV11->Clear();
 LV12->Clear();
 LV13->Clear();
 delete CompR;
 delete CompRS1;
 delete CompRS2;
 delete CompL;
 delete CompC;
 delete Switch;
 delete AOIdoz;
 delete CompBuf;
 delete CompM;
 delete Nodes;
 delete CompIdoz;
 delete FCircuitArrayY1;
 delete FCircuitArrayC12;
 delete FCircuitArrayC21;
 delete FCircuitArrayY2;
 delete FCircuitArrayY1s;
 delete FCircuitArrayC12s;
 delete FCircuitArrayC21s;
 delete FCircuitArrayY2s;

 delete FComplexCircuitArrayY1;
 delete FComplexCircuitArrayC12;
 delete FComplexCircuitArrayC21;
 delete FComplexCircuitArrayY2;
 delete FComplexCircuitArrayY1f;
 delete FComplexCircuitArrayC12f;
 delete FComplexCircuitArrayC21f;
 delete FComplexCircuitArrayY2f;
 delete FComplexCircuitArrayY1short;
 delete FComplexCircuitArrayC12short;
 delete FComplexCircuitArrayC21short;
 delete FComplexCircuitArrayY2short;

 delete matfreq;
 delete matexp;
 delete matdb;
 delete matfaze;
}

//---------------------------------------------------------------------------

void __fastcall TForm1::OtevritClick(TObject *Sender)
{
if (OpenDialog1->Execute())
  {
   Button1->Visible = true;
   Button8->Visible = false;
   Panelin->Visible = false;
   Panelout->Visible = false;
   Label39->Visible = false;
   Label40->Visible = false;
   Label43->Visible = false;
   EditFS->Visible = false;
   EditD->Visible = false;
   CBHodDek->Visible = false;
   CBXmin->Visible = false;
   CBXmax->Visible = false;

   Memo1->Clear();
   Memo2->Clear();
   Memo3->Clear();
   LV1->Clear();
   LV2->Clear();
   LV3->Clear();
   LV4->Clear();
   LV5->Clear();
   LV6->Clear();
   LV7->Clear();
   LV8->Clear();
   LV9->Clear();
   LV10->Clear();
   LV11->Clear();
   LV12->Clear();
   LV13->Clear();

   VstupSoub = OpenDialog1->FileName;
   Memo1->Lines->LoadFromFile(VstupSoub);
  }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::UlozitClick(TObject *Sender)
{
 AnsiString SaveName;
 if(SaveDialog1->Execute())
 {
   SaveName = SaveDialog1->FileName;
   if (SaveDialog1->FilterIndex == 1) SaveName = SaveName + ".m";
   if (SaveDialog1->FilterIndex == 2) SaveName = SaveName + ".txt";
   Memo3->Lines->SaveToFile(SaveName);
 }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::KonecClick(TObject *Sender)
{
 Close();
}
//---------------------------------------------------------------------------
AnsiString ValidatingValue(AnsiString str)
{
 AnsiString pol[26][2] = {
   {"o",""},
   {"v",""},
   {"a",""},
   {"h",""},
   {"f",""},


   {"meg","e+6"},
   {"g","e+9"},
   {"t","e+12"},
   {"p","e-12"},
   {"n","e-9"},
   {"u","e-6"},
   {"m","e-3"},
   {"k","e+3"},

   {"b",""},
   {"c",""},
   {"d",""},
   {"i",""},
   {"j",""},
   {"l",""},
   {"q",""},
   {"r",""},
   {"s",""},
   {"w",""},
   {"x",""},
   {"y",""},
   {"z",""}


 };

 int posend = 0;
 for(int i = 0; i < 26;i++)
 {
   posend = str.LowerCase().Pos(pol[i][0]);
   if (posend)
   {
     str = str.SubString(1,posend-1) + pol[i][1];
   }
 }
 return str;
}
//---------------------------------------------------------------------------
AnsiString ValueWithDot(AnsiString str)
{
 int posend = 0;

 posend = str.LowerCase().Pos(',');
 if (posend)
 {
   str = str.SubString(1,posend-1) + '.' + str.SubString(posend+1,str.Length()-posend);
 }

 return str;
}
//---------------------------------------------------------------------------
double ValueStrToDouble(AnsiString Value)
{
 double ValueDouble;
 char hodnota[40];

 Value = ValueWithDot(Value);

 for ( int i=0; i<Value.Length(); i++)
   {
    hodnota[i] = Value.operator [](i+1);
   }

 ValueDouble = atof(hodnota);

 return ValueDouble;
}
//---------------------------------------------------------------------------
AnsiString ValueDoubleToStr(double real)
{
 ostringstream os;
 os << real;
 string realAsString = os.str();
 return AnsiString(realAsString.c_str());
 
}//---------------------------------------------------------------------------
AnsiString StrValueRepair(AnsiString Value) //mění v čísle čárku za tečku
{
 AnsiString Result = "";

 for ( int i=0; i<Value.Length(); i++)
 {
   if(Value.operator [](i+1) == ',')
     Result = Result + ".";
   else
     Result = Result + Value.operator [](i+1);
 }
 
 return Result;
}
//---------------------------------------------------------------------------
void TrideniDoListView(TListView*ListView, AnsiString radek)
{
 AnsiString znakradku = "", jmeno = "", LVX = "";
 AnsiString nazevsouc = "", prvni = "", druhy = "", treti = "", ctvrty = "", paty = "", sesty = "", sedmy = "", osmy = "", devaty = "", koment = "";

   int c = 0;
   int chc = 0;
   int begpos = 1;
   AnsiString str;
   int desc = 0;
   for(int i = 1; i < (radek.Length()+1);i++)
   {
     if (radek[i] == 59)
     {
       desc = 1;
       chc = 0;
       begpos = i + 1;
     }else {
     if (( (radek[i] == 32) || (radek[i] == 9) ) && (!desc) )  //&& ||
     {
       if (chc)
       {
         str = radek.SubString(begpos,chc);
         switch (c) {
           case 0 : nazevsouc = str; break;
           case 1 : prvni = str; break;
           case 2 : druhy = str; break;
           case 3 : treti = str; break;
           case 4 : ctvrty = str; break;
           case 5 : paty = str; break;
           case 6 : sesty = str; break;
           case 7 : sedmy = str; break;
           case 8 : osmy = str; break;
           case 9 : devaty = str; break;
           default : ;
         }
         //ShowMessage(str);
         c++;
         chc = 0;
       }
       begpos = i + 1;
     }else {
       chc++;
     }
     };
   }
   if (chc)
   {
     str = radek.SubString(begpos,chc);
     if (desc)
     {
       koment = str; //ShowMessage("Note: "+str);
     }else {
       switch (c) {
           case 0 : nazevsouc = str; break;
           case 1 : prvni = str; break;
           case 2 : druhy = str; break;
           case 3 : treti = str; break;
           case 4 : ctvrty = str; break;
           case 5 : paty = str; break;
           case 6 : sesty = str; break;
           case 7 : sedmy = str; break;
           case 8 : osmy = str; break;
           case 9 : devaty = str; break;
           default : ;
       }
       //ShowMessage(str);
     }
   }

// rozšíření pro rozpoznání až 99ti součástek
if (ListView->Name.Length() == 3){ LVX = StrToInt(ListView->Name.SubString(3,1)); }
if (ListView->Name.Length() == 4){ LVX = StrToInt(ListView->Name.SubString(3,2)); }

switch (StrToInt(LVX))
  {
   case 1: // R
   {
    TListItem *polozka;
    polozka=ListView->Items->Add();
    polozka->Caption = nazevsouc;
    polozka->SubItems->Add(prvni);
    polozka->SubItems->Add(druhy);
    polozka->SubItems->Add(ValidatingValue(treti));
    polozka->SubItems->Add(koment);
   } ; break;

   case 2:  // L
   {
    TListItem *polozka;
    polozka=ListView->Items->Add();
    polozka->Caption = nazevsouc;
    polozka->SubItems->Add(prvni);
    polozka->SubItems->Add(druhy);
    polozka->SubItems->Add(ValidatingValue(treti));
    polozka->SubItems->Add(ValidatingValue(ctvrty));
    polozka->SubItems->Add(koment);
   } ; break;

   case 3:  // C
   {
    TListItem *polozka;
    polozka=ListView->Items->Add();
    polozka->Caption = nazevsouc;
    polozka->SubItems->Add(prvni);
    polozka->SubItems->Add(druhy);
    polozka->SubItems->Add(ValidatingValue(treti));
    polozka->SubItems->Add(koment);
   } ; break;

   case 4:  // E
   {
    TListItem *polozka;
    polozka=ListView->Items->Add();
    polozka->Caption = nazevsouc;
    polozka->SubItems->Add(prvni);
    polozka->SubItems->Add(druhy);
    polozka->SubItems->Add(treti);
    polozka->SubItems->Add(ctvrty);
    polozka->SubItems->Add(ValidatingValue(paty));
    polozka->SubItems->Add(koment);
   } ;  break;

   case 5:  // F
   {
    TListItem *polozka;
    polozka=ListView->Items->Add();
    polozka->Caption = nazevsouc;
    polozka->SubItems->Add(prvni);
    polozka->SubItems->Add(druhy);
    polozka->SubItems->Add(treti);
    polozka->SubItems->Add(ValidatingValue(ctvrty));
    polozka->SubItems->Add(koment);
   } ; break;

   case 6:  // G
   {
    TListItem *polozka;
    polozka=ListView->Items->Add();
    polozka->Caption = nazevsouc;
    polozka->SubItems->Add(prvni);
    polozka->SubItems->Add(druhy);
    polozka->SubItems->Add(treti);
    polozka->SubItems->Add(ctvrty);
    polozka->SubItems->Add(ValidatingValue(paty));
    polozka->SubItems->Add(koment);
   } ; break;

   case 7:  // H
   {
    TListItem *polozka;
    polozka=ListView->Items->Add();
    polozka->Caption = nazevsouc;
    polozka->SubItems->Add(prvni);
    polozka->SubItems->Add(druhy);
    polozka->SubItems->Add(treti);
    polozka->SubItems->Add(ValidatingValue(ctvrty));
    polozka->SubItems->Add(koment);
   } ; break;

   case 8:  // OA1p
   {
    TListItem *polozka;
    polozka=ListView->Items->Add();
    polozka->Caption = nazevsouc;
    polozka->SubItems->Add(prvni);
    polozka->SubItems->Add(druhy);
    polozka->SubItems->Add(treti);
    polozka->SubItems->Add(ValidatingValue(ctvrty));
    polozka->SubItems->Add(ValidatingValue(paty));
    polozka->SubItems->Add(ValidatingValue(sesty));
    polozka->SubItems->Add(koment);
   } ; break;

   case 9:  // S
   {
    TListItem *polozka;
    polozka=ListView->Items->Add();
    polozka->Caption = nazevsouc;
    polozka->SubItems->Add(prvni);
    polozka->SubItems->Add(druhy);
    polozka->SubItems->Add(treti);
    polozka->SubItems->Add(ValidatingValue(ctvrty));
    polozka->SubItems->Add(koment);
   } ; break;

   case 10:  // Zdroje U
   {
    TListItem *polozka;
    polozka=ListView->Items->Add();
    polozka->Caption = nazevsouc;
    polozka->SubItems->Add(prvni);
    polozka->SubItems->Add(druhy);
    polozka->SubItems->Add(ValidatingValue(treti));
    polozka->SubItems->Add(koment);
   } ; break;

   case 11:  // Vzájemná indukčnost M
   {
    TListItem *polozka;
    polozka=ListView->Items->Add();
    polozka->Caption = nazevsouc;
    polozka->SubItems->Add(prvni);
    polozka->SubItems->Add(druhy);
    polozka->SubItems->Add(treti);
    polozka->SubItems->Add(ctvrty);
    polozka->SubItems->Add(ValidatingValue(paty));
    polozka->SubItems->Add(ValidatingValue(sesty));
    polozka->SubItems->Add(ValidatingValue(sedmy));
    polozka->SubItems->Add(ValidatingValue(osmy));
    polozka->SubItems->Add(ValidatingValue(devaty));
    polozka->SubItems->Add(koment);
   } ;  break;

   case 12:  // BUFFER
   {
    TListItem *polozka;
    polozka=ListView->Items->Add();
    polozka->Caption = nazevsouc;
    polozka->SubItems->Add(prvni);
    polozka->SubItems->Add(druhy);
    polozka->SubItems->Add(ValidatingValue(treti));
    polozka->SubItems->Add(ValidatingValue(ctvrty));
    polozka->SubItems->Add(koment);
   } ; break;

   case 13:  // IDOZ
   {
    TListItem *polozka;
    polozka=ListView->Items->Add();
    polozka->Caption = nazevsouc;
    polozka->SubItems->Add(prvni);
    polozka->SubItems->Add(druhy);
    polozka->SubItems->Add(treti);
    polozka->SubItems->Add(ValidatingValue(ctvrty));
    polozka->SubItems->Add(ValidatingValue(paty));
    polozka->SubItems->Add(koment);
   } ; break;
   
   default : ;

  }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)  // roztřízení do jednotlivých ListView
{
  // user friendly
   Button1->Visible = false;
   Button3->Visible = true;

  AnsiString seradek, vsuvka;
  int i, iii, typsouc, nic = 0;

  iii=1;
 LV1->Clear(); LV2->Clear(); LV3->Clear(); LV4->Clear(); LV5->Clear();
 LV6->Clear(); LV7->Clear(); LV8->Clear(); LV9->Clear(); LV10->Clear();
 LV11->Clear(); LV12->Clear(); LV13->Clear();
 Memo3->Clear(); // Memo2->Clear();

  LPrvniRadek->Caption = Memo1->Lines->operator [](0);

  for (i=1; i<=(Memo1->Lines->Count-1); i++)     // funkce pro načtení každého řádku
   {
     seradek = Memo1->Lines->operator [](iii++);

   if ((seradek.SubString(1,1) == 'V') || (seradek.SubString(1,1) == 'v')) typsouc = 0;
   else if ((seradek.SubString(1,1) == 'R') || (seradek.SubString(1,1) == 'r')) typsouc = 1;
   else if ((seradek.SubString(1,1) == 'L') || (seradek.SubString(1,1) == 'l')) typsouc = 2;
   else if ((seradek.SubString(1,1) == 'C') || (seradek.SubString(1,1) == 'c')) typsouc = 3;

   else if ((seradek.SubString(1,1) == 'M') || (seradek.SubString(1,1) == 'm')) typsouc = 11;
   else if ((seradek.SubString(1,4) == "OA1P") || (seradek.SubString(1,4) == "oa1p") || (seradek.SubString(1,4) == "OA1p")) typsouc = 8;
   else if ((seradek.SubString(1,3) == "IOA") || (seradek.SubString(1,3) == "ioa") || (seradek.SubString(1,3) == "Ioa")) typsouc = 13;

   else if ((seradek.SubString(1,1) == 'E') || (seradek.SubString(1,1) == 'e')) typsouc = 4;
   else if ((seradek.SubString(1,1) == 'F') || (seradek.SubString(1,1) == 'f')) typsouc = 5;
   else if ((seradek.SubString(1,1) == 'G') || (seradek.SubString(1,1) == 'g')) typsouc = 6;

   else if ((seradek.SubString(1,1) == 'H') || (seradek.SubString(1,1) == 'h')) typsouc = 7;
   else if ((seradek.SubString(1,1) == 'O') || (seradek.SubString(1,1) == 'o')) typsouc = 12;
   else if ((seradek.SubString(1,1) == 'S') || (seradek.SubString(1,1) == 's')) typsouc = 9;

   else   typsouc = 10;

   switch (typsouc)
    {
   case 0: TrideniDoListView(LV10, seradek); break;
   case 1: TrideniDoListView(LV1, seradek); break;
   case 2: TrideniDoListView(LV2, seradek); break;
   case 3: TrideniDoListView(LV3, seradek); break;

   case 4: TrideniDoListView(LV4, seradek); break;
   case 5: TrideniDoListView(LV5, seradek); break;
   case 6: TrideniDoListView(LV6, seradek); break;

   case 7: TrideniDoListView(LV7, seradek); break;
   case 8: TrideniDoListView(LV8, seradek); break;
   case 9: TrideniDoListView(LV9, seradek); break;

   case 11: TrideniDoListView(LV11, seradek); break;
   case 12: TrideniDoListView(LV12, seradek); break;
   case 13: TrideniDoListView(LV13, seradek); break;

   case 10: ;
     {
      nic = 0;
      if (seradek.Length() > 0)
        { if ((seradek[1] == 32) || (seradek[1] == 9) ) nic = 1; }
      else nic = 1;
      if (nic == 0) Memo2->Lines->Add(seradek);
     }  break;
   default : ;
    }
   }
   Label6->Caption = "Celkem: " + IntToStr( LV1->Items->Count);
   Label7->Caption = "Celkem: " + IntToStr( LV2->Items->Count);
   Label8->Caption = "Celkem: " + IntToStr( LV3->Items->Count);
   Label17->Caption = "Celkem: " + IntToStr( LV4->Items->Count);
   Label18->Caption = "Celkem: " + IntToStr( LV5->Items->Count);
   Label19->Caption = "Celkem: " + IntToStr( LV6->Items->Count);
   Label20->Caption = "Celkem: " + IntToStr( LV7->Items->Count);
   Label21->Caption = "Celkem: " + IntToStr( LV8->Items->Count);
   Label22->Caption = "Celkem: " + IntToStr( LV9->Items->Count);
   Label24->Caption = "Celkem: " + IntToStr( LV10->Items->Count);
   Label26->Caption = "Celkem: " + IntToStr( LV11->Items->Count);
   Label27->Caption = "Celkem: " + IntToStr( LV12->Items->Count);
}
//---------------------------------------------------------------------------
int NejdelsiRetezecSloupcu(TListView *ListView, int j)
{
 int max = 0;
 for(int i = 0; i < ListView->Items->Count; i++)   // zjištění nejdelší buňky ve sloupci
 {
   TListItem *ListItem = ListView->Items->operator [](i);
   if (j == 0)
   {
     if (ListItem->Caption.Length() > max)
       max = ListItem->Caption.Length();
   }else if (j > 0) {
     if (ListItem->SubItems->operator [](j-1).Length() > max)
       max = ListItem->SubItems->operator [](j+-1).Length();
   }
 }
 return max;
}
//---------------------------------------------------------------------------

void TvorbaMFile(TListView *ListView, char typ)   //výpis
{
AnsiString r = "", r0 = "", r1 = "", r2 = "", r3 = "", r4 = "", m = "", t ="";
int p3, p4;
int p = NejdelsiRetezecSloupcu(ListView, 0);
int p0 = NejdelsiRetezecSloupcu(ListView, 1);
int p1 = NejdelsiRetezecSloupcu(ListView, 2);
int p2 = NejdelsiRetezecSloupcu(ListView, 3);

switch (StrToInt(ListView->Name.SubString(3,1))-3)
  {
   case 1:
   {
    p3 = NejdelsiRetezecSloupcu(ListView, 4);
    p4 = NejdelsiRetezecSloupcu(ListView, 5);
   } ; break;

   case 2:
   {
    p3 = NejdelsiRetezecSloupcu(ListView, 4);
   } ; break;

   case 3:
   {
    p3 = NejdelsiRetezecSloupcu(ListView, 4);
    p4 = NejdelsiRetezecSloupcu(ListView, 5);
   } ; break;
   case 4:
   {
    p3 = NejdelsiRetezecSloupcu(ListView, 4);
   } ;  break;

   case 5:
   {
    p3 = NejdelsiRetezecSloupcu(ListView, 4);
    p4 = NejdelsiRetezecSloupcu(ListView, 5);
   } ; break;

   case 6:
   {
    p3 = NejdelsiRetezecSloupcu(ListView, 4);
   } ; break;
   default : ;

  }


for(int i = 0; i < ListView->Items->Count; i++)
 {
   TListItem *ListItem = ListView->Items->operator [](i);
   r = ListItem->Caption;
   while ( p > r.Length()) r = r + " " ;
   for(int j = 0; j < (ListItem->SubItems->Count - 1); j++)
   {
    switch (j)
     {
     case 0:
      {
       r0 = ListItem->SubItems->operator [](j);
       while ( p0 > r0.Length()) r0 = r0 + " " ; break;
      }
     case 1:
      {
       r1 = ListItem->SubItems->operator [](j);
       while ( p1 > r1.Length()) r1 = r1 + " " ; break;
      }
     case 2: {
       r2 = ListItem->SubItems->operator [](j);
       while ( p2 > r2.Length()) r2 = r2 + " " ; break;
      }
      case 3: {
       r3 = ListItem->SubItems->operator [](j);
       while ( p3 > r3.Length()) r3 = r3 + " " ; break;
      }
      case 4: {
       r4 = ListItem->SubItems->operator [](j);
       while ( p4 > r4.Length()) r4 = r4 + " " ; break;
      }
     default : ;
     }
   }

   m = m +" '"+ r +"' '"+ r0 +"' '"+ r1 +"' '"+ r2 +"' ";

   switch (StrToInt(ListView->Name.SubString(3,1))-3)
  {
   case 1: m = m +" '"+ r3 +"' '"+ r4 +"' "; break;
   case 2: m = m +" '"+ r3 +"' "; break;
   case 3: m = m +" '"+ r3 +"' '"+ r4 +"' "; break;
   case 4: m = m +" '"+ r3 +"' "; break;
   case 5: m = m +" '"+ r3 +"' '"+ r4 +"' "; break;
   case 6: m = m +" '"+ r3 +"' "; break;
   default : ;

  }
   if (i < (ListView->Items->Count - 1)) m = m +" ; ";
 }
 t = typ;
 Form1->Memo3->Lines->Add(t+ " = [ "+ m +" ]");
}
//---------------------------------------------------------------------------
//načtení obecné součástky
void LoadFromListView ( TListView* AListView, TElePart* AElePart, TEleNodes* AEleNodes )
{
  TElePartItem EPItem;
  TListItem* ListItem;

  for ( int i=0; i< AListView->Items->Count; i++)
  {
     ListItem = AListView->Items->operator [](i);
     EPItem.Name = ListItem->Caption;
     EPItem.Value = ListItem->SubItems->operator [](2);
     EPItem.Input = AEleNodes->CompareAdd( ListItem->SubItems->operator [](0));
     EPItem.Output = AEleNodes->CompareAdd( ListItem->SubItems->operator [](1));

     AElePart->Add(EPItem);
  }
}
//---------------------------------------------------------------------------
//načtení cívek
void LoadFromListViewL ( TListView* AListView, TElePartL* AElePart, TEleNodes* AEleNodes )
{
  TElePartItemL EPItem;
  TListItem* ListItem;

  for ( int i=0; i< AListView->Items->Count; i++)
  {
     ListItem = AListView->Items->operator [](i);
     EPItem.Name = ListItem->Caption;
     EPItem.ValueLs = ListItem->SubItems->operator [](2);
     EPItem.ValueRs = ListItem->SubItems->operator [](3);
     EPItem.Input = AEleNodes->CompareAdd( ListItem->SubItems->operator [](0));
     EPItem.Output = AEleNodes->CompareAdd( ListItem->SubItems->operator [](1));

     AElePart->Add(EPItem);
  }
}
//---------------------------------------------------------------------------
//načtení spínačů
void LoadFromListViewS ( TListView* AListView, TElePartS* AElePart, TEleNodes* AEleNodes )
{
  TElePartItemS EPItem;
  TListItem* ListItem;

  for ( int i=0; i< AListView->Items->Count; i++)
  {
     ListItem = AListView->Items->operator [](i);
     EPItem.Name = ListItem->Caption;
     EPItem.Value = ListItem->SubItems->operator [](2);

    if (ListItem->SubItems->operator [](3).Length() > 0)
    {
      EPItem.ValueR = ListItem->SubItems->operator [](3);
    } else {
      EPItem.ValueR = "0";
    }
     EPItem.Input = AEleNodes->CompareAdd( ListItem->SubItems->operator [](0));
     EPItem.Output = AEleNodes->CompareAdd( ListItem->SubItems->operator [](1));

     AElePart->Add(EPItem);
  }
}
//---------------------------------------------------------------------------
//načtení vzájemné indukčnosti
void LoadFromListViewM ( TListView* AListView, TElePartM* AElePart, TEleNodes* AEleNodes )
{
  TElePartItemM EPItem;
  TListItem* ListItem;

  for ( int i=0; i< AListView->Items->Count; i++)
  {
     ListItem = AListView->Items->operator [](i);
     EPItem.Name = ListItem->Caption;
     EPItem.ValueL1 = ListItem->SubItems->operator [](4);
     EPItem.ValueL2 = ListItem->SubItems->operator [](5);
     EPItem.ValueK = ListItem->SubItems->operator [](6);
     EPItem.InputA = AEleNodes->CompareAdd( ListItem->SubItems->operator [](0));
     EPItem.OutputB = AEleNodes->CompareAdd( ListItem->SubItems->operator [](1));
     EPItem.InputC = AEleNodes->CompareAdd( ListItem->SubItems->operator [](2));
     EPItem.OutputD = AEleNodes->CompareAdd( ListItem->SubItems->operator [](3));

     AElePart->Add(EPItem);
  }
}
//---------------------------------------------------------------------------
//načtení ideálního OZ
void LoadFromEleIDOZ ( TListView* AListView, TEleIDOZ* AAOIdoz, TEleNodes* AEleNodes )
{
  TListItem* ListItem;
  TEleIDOZItem EAOItem;

  for ( int i=0; i< AListView->Items->Count; i++)
  {
     ListItem = AListView->Items->operator [](i);
     EAOItem.Name = ListItem->Caption;
     EAOItem.InputA = AEleNodes->CompareAdd( ListItem->SubItems->operator [](0));
     EAOItem.InputB = AEleNodes->CompareAdd( ListItem->SubItems->operator [](1));
     EAOItem.Output = AEleNodes->CompareAdd( ListItem->SubItems->operator [](2));
     AAOIdoz->Add(EAOItem);

  }
}
//---------------------------------------------------------------------------
//načtení operáku
void LoadFromEleAO ( TListView* AListView, TEleAO* AAOIdoz, TEleNodes* AEleNodes )
{
  TListItem* ListItem;
  TEleAOItem EAOItem;

  for ( int i=0; i< AListView->Items->Count; i++)
  {
     ListItem = AListView->Items->operator [](i);
     EAOItem.Name = ListItem->Caption;
     EAOItem.ValueA = ListItem->SubItems->operator [](3);
     EAOItem.ValueFt = ListItem->SubItems->operator [](4);
     EAOItem.ValueRout = ListItem->SubItems->operator [](5);
     EAOItem.InputA = AEleNodes->CompareAdd( ListItem->SubItems->operator [](0));
     EAOItem.InputB = AEleNodes->CompareAdd( ListItem->SubItems->operator [](1));
     EAOItem.Output = AEleNodes->CompareAdd( ListItem->SubItems->operator [](2));

     AAOIdoz->Add(EAOItem);

  }
}
//---------------------------------------------------------------------------
//načtení bufferu
void LoadFromListViewBuf ( TListView* AListView, TElePartB* AElePart, TEleNodes* AEleNodes )
{
  TElePartItemB EPItem;
  TListItem* ListItem;

  for ( int i=0; i< AListView->Items->Count; i++)
  {
     ListItem = AListView->Items->operator [](i);
     EPItem.Name = ListItem->Caption;
     EPItem.Input = AEleNodes->CompareAdd( ListItem->SubItems->operator [](0));
     EPItem.Output = AEleNodes->CompareAdd( ListItem->SubItems->operator [](1));

     AElePart->Add(EPItem);
  }
}

//---------------------------------------------------------------------------

void WriteEleAO ( TEleAO* AAOIdoz, TListView* AListView )
{
  TEleAOItem EAOItem;
  TListItem* ListItem;

  for ( int i=0; i< AAOIdoz->Count(); i++)
  {
     EAOItem = AAOIdoz->GetItem(i);
     ListItem = AListView->Items->Add();
     ListItem->Caption = EAOItem.Name;
     ListItem->SubItems->Add( IntToStr( EAOItem.InputA ));
     ListItem->SubItems->Add( IntToStr( EAOItem.InputB ));
     ListItem->SubItems->Add( IntToStr( EAOItem.Output ));
     ListItem->SubItems->Add( EAOItem.ValueA );
     ListItem->SubItems->Add( EAOItem.ValueFt );
     ListItem->SubItems->Add( EAOItem.ValueRout );
  }
}
//---------------------------------------------------------------------------
// všechny načtené uzly
void WriteEleNodes ( TEleNodes* AEleNodes, TListView* AListView )
{
  TEleNodeItem ENItem;
  TListItem* ListItem;

  for ( int i=0; i< AEleNodes->Count(); i++)
  {
     ENItem = AEleNodes->GetItem(i);
     ListItem = AListView->Items->Add();
     ListItem->Caption = IntToStr(ENItem.ID);
     ListItem->SubItems->Add( ENItem.Name );
     ListItem->SubItems->Add( IntToStr( ENItem.ModAU ));
     ListItem->SubItems->Add( IntToStr( ENItem.ModAI ));
     ListItem->SubItems->Add( IntToStr( ENItem.ModBU ));
     ListItem->SubItems->Add( IntToStr( ENItem.ModBI ));
  }
}

//---------------------------------------------------------------------------
// setřízené uzly
void WriteSortEleNodes ( TEleNodes* AEleNodes, TListView* AListView )
{
  TStringList* StringList = new TStringList();
  TEleNodeItem ENItem;
  TListItem* ListItem;

  for ( int i=0; i< AEleNodes->Count(); i++)
  {
     ENItem = AEleNodes->GetItem(i);
     StringList->Add(ENItem.Name);
     StringList->Objects[StringList->Count-1] = (TObject*) i;
  }

  StringList->Sort();

  for ( int i=0; i< StringList->Count; i++)
  {
     ENItem = AEleNodes->GetItem( (int) StringList->Objects[i] );
     ListItem = AListView->Items->Add();
     ListItem->Caption = IntToStr(ENItem.ID);
     ListItem->SubItems->Add( ENItem.Name );
     ListItem->SubItems->Add( IntToStr( ENItem.ModAU ));
     ListItem->SubItems->Add( IntToStr( ENItem.ModAI ));
     ListItem->SubItems->Add( IntToStr( ENItem.ModBU ));
     ListItem->SubItems->Add( IntToStr( ENItem.ModBI ));
  }

  delete StringList;
}
//---------------------------------------------------------------------------

void WriteElePart ( TElePart* AElePart, TListView* AListView )
{
  TElePartItem EPItem;
  TListItem* ListItem;

  for ( int i=0; i< AElePart->Count(); i++)
  {
     EPItem = AElePart->GetItem(i);
     ListItem = AListView->Items->Add();
     ListItem->Caption = EPItem.Name;
     ListItem->SubItems->Add( IntToStr( EPItem.Input ));
     ListItem->SubItems->Add( IntToStr( EPItem.Output ));
     ListItem->SubItems->Add( EPItem.Value );
  }
}

//---------------------------------------------------------------------------

void WriteElePartL ( TElePartL* AElePart, TListView* AListView )
{
  TElePartItemL EPItem;
  TListItem* ListItem;

  for ( int i=0; i< AElePart->Count(); i++)
  {
     EPItem = AElePart->GetItem(i);
     ListItem = AListView->Items->Add();
     ListItem->Caption = EPItem.Name;
     ListItem->SubItems->Add( IntToStr( EPItem.Input ));
     ListItem->SubItems->Add( IntToStr( EPItem.Output ));
     ListItem->SubItems->Add( EPItem.ValueLs );
     ListItem->SubItems->Add( EPItem.ValueRs );
  }
}

//---------------------------------------------------------------------------

void WriteElePartS ( TElePartS* AElePart, TListView* AListView )
{
  TElePartItemS EPItem;
  TListItem* ListItem;

  for ( int i=0; i< AElePart->Count(); i++)
  {
     EPItem = AElePart->GetItem(i);
     ListItem = AListView->Items->Add();
     ListItem->Caption = EPItem.Name;
     ListItem->SubItems->Add( IntToStr( EPItem.Input ));
     ListItem->SubItems->Add( IntToStr( EPItem.Output ));
     ListItem->SubItems->Add( EPItem.Value );
     ListItem->SubItems->Add( EPItem.ValueR );
  }
}
//---------------------------------------------------------------------------

void WriteIOList ( TIOList* AIOList , TListView* AListView )
{
  TIOListItem IOLItem;
  TListItem* ListItem;

  for ( int i=0; i< AIOList->Count(); i++)
  {
     IOLItem = AIOList->GetItem(i);
     ListItem = AListView->Items->Add();
     ListItem->Caption = "";
     ListItem->SubItems->Add( IntToStr( IOLItem.Input ));
     ListItem->SubItems->Add( IntToStr( IOLItem.Output ));

  }
}
//---------------------------------------------------------------------------

void IDProcessing ( TEleNodes* AEleNodes )
{
  TEleNodeItem ENItem;
  TEleNodeItem ENItemID;

   for ( int i=0; i< AEleNodes->Count(); i++ )
   {
      ENItem = AEleNodes->GetItem(i);
      ENItemID = AEleNodes->GetItem(ENItem.ModAU-1);
      ENItem.ModAU = StrToInt( ENItemID.Name );

      ENItemID = AEleNodes->GetItem(ENItem.ModAI-1);
      ENItem.ModAI = StrToInt( ENItemID.Name );

      ENItemID = AEleNodes->GetItem(ENItem.ModBU-1);
      ENItem.ModBU = StrToInt( ENItemID.Name );

      ENItemID = AEleNodes->GetItem(ENItem.ModBI-1);
      ENItem.ModBI = StrToInt( ENItemID.Name );

      AEleNodes->SetItem( i, ENItem );
   }
}
//---------------------------------------------------------------------------

int IsNotConnectToComp(int NodeID, TElePart* ACompR, TElePart* ACompRS, TElePartL* ACompL, TElePart* ACompC,  TElePartM* ACompM, TElePartB* ACompBuf, TEleIDOZ* ACompIdoz, TEleAO* AAOIdoz)
{
  int run = 1;
  int i = 0;
  while((i < ACompR->Count())&&(run)) {
    if(ACompR->GetItem(i).Input == NodeID)
      run = 0;
    if(ACompR->GetItem(i).Output == NodeID)
      run = 0;
    i++;
  }
  i = 0;
  while((i < ACompRS->Count())&&(run)) {
    if(ACompRS->GetItem(i).Input == NodeID)
      run = 0;
    if(ACompRS->GetItem(i).Output == NodeID)
      run = 0;
    i++;
  }
  i = 0;
  while((i < ACompL->Count())&&(run)) {
    if(ACompL->GetItem(i).Input == NodeID)
      run = 0;
    if(ACompL->GetItem(i).Output == NodeID)
      run = 0;
    i++;
  }
  i = 0;
  while((i < ACompC->Count())&&(run)) {
    if(ACompC->GetItem(i).Input == NodeID)
      run = 0;
    if(ACompC->GetItem(i).Output == NodeID)
      run = 0;
    i++;
  }
  i = 0;
  while((i < ACompM->Count())&&(run)) {
    if(ACompM->GetItem(i).InputA == NodeID)
      run = 0;
    if(ACompM->GetItem(i).InputC == NodeID)
      run = 0;
    if(ACompM->GetItem(i).OutputB == NodeID)
      run = 0;
    if(ACompM->GetItem(i).OutputD == NodeID)
      run = 0;
    i++;
  }
  i = 0;
  while((i < ACompBuf->Count())&&(run)) {
    if(ACompBuf->GetItem(i).Input == NodeID)
      run = 0;
    if(ACompBuf->GetItem(i).Output == NodeID)
      run = 0;
    i++;
  }
  i = 0;
  while((i < ACompIdoz->Count())&&(run)) {
    if(ACompIdoz->GetItem(i).InputA == NodeID)
      run = 0;
    if(ACompIdoz->GetItem(i).InputB == NodeID)
      run = 0;
    if(ACompIdoz->GetItem(i).Output == NodeID)
      run = 0;
    i++;
  }
  i = 0;
  while((i < AAOIdoz->Count())&&(run)) {
    if(AAOIdoz->GetItem(i).InputA == NodeID)
      run = 0;
    if(AAOIdoz->GetItem(i).InputB == NodeID)
      run = 0;
    if(AAOIdoz->GetItem(i).Output == NodeID)
      run = 0;
    i++;
  }
  //i = 0;
  //while((i < )&&(run)) {
  //  if(  == NodeID) {
  //    run = 0;
  //  }
  //  i++;
  //}

  if(run)
    return 0;
  else
    return 1;
}

void ChangeModBothID( TEleNodes* AEleNodes, int AMod, int AModID )
{
  for(int j = 0; j < AEleNodes->Count(); j++) {
    TEleNodeItem ENItemChange = AEleNodes->GetItem(j);
    if (AMod) {
      if (ENItemChange.ModBU == AModID ) {
        ENItemChange.ModBU = 0;
        ENItemChange.ModBI = 0;
      }
    } else {
      if (ENItemChange.ModAU == AModID ) {
        ENItemChange.ModAU = 0;
        ENItemChange.ModAI = 0;
      }
    }
    AEleNodes->SetItem( j, ENItemChange );
  }
}

int CompareModID(TEleNodeItem ENItem, int AMod, int AModID)
{
  if (AMod) {
    if(ENItem.ModBU == AModID)
      return 1;
    else
      return 0;
  } else {
    if(ENItem.ModAU == AModID)
      return 1;
    else
      return 0;
  }
}

void ProcessModAloneNod ( TEleNodes* AEleNodes, TElePart* ACompR, TElePart* ACompRS, TElePartL* ACompL, TElePart* ACompC, TElePartM* ACompM, TElePartB* ACompBuf, TEleIDOZ* ACompIdoz, TEleAO* AAOIdoz, TIOList* ANodeMod, int AMod, int AModID  )
{

  int IndexCache = ANodeMod->IndexOfInput(AModID);
  if ( IndexCache == -1) {
    TEleNodeItem ENItem;
    int run = 1;
    int i = 0;
    while((i < AEleNodes->Count())&&(run))
    {
      ENItem = AEleNodes->GetItem(i);
      if (CompareModID(ENItem, AMod, AModID)) {
        if (IsNotConnectToComp(ENItem.ID, ACompR, ACompRS, ACompL, ACompC, ACompM, ACompBuf, ACompIdoz, AAOIdoz)) {
          run = 0;
        }
      }
      i++;
    }
    if (run) {
      //přidat do cache (ANodeMod) a PŘEČÍSLOVAT !!!!!
      TIOListItem IOLItem;
      IOLItem.Input = AModID;
      IOLItem.Output = 1;
      ANodeMod->Add(IOLItem);
      ChangeModBothID(AEleNodes, AMod, AModID);
    } else {
      //přidat do cache (ANodeMod)
      TIOListItem IOLItem;
      IOLItem.Input = AModID;
      IOLItem.Output = 0;
      ANodeMod->Add(IOLItem);
    }
  } else { // Mod Nod ID v cache (ANodeMod)
    ANodeMod->GetItem(IndexCache).Output;

  }

};


void TransAloneSwitch ( TEleNodes* AEleNodes, TElePartS* ASwitch, TElePart* ACompR, TElePart* ACompRS1, TElePart* ACompRS2, TElePartL* ACompL, TElePart* ACompC, TElePartM* ACompM, TElePartB* ACompBuf, TEleIDOZ* ACompIdoz, TEleAO* AAOIdoz )
{
  TIOList* NodeModA = new TIOList();
  TIOList* NodeModB = new TIOList();

  for ( int i=0; i< ASwitch->Count(); i++ )
  {
    TElePartItemS EPItem = ASwitch->GetItem(i);

    if (( CompareStr(EPItem.Value, "on") == 0 ) && ( EPItem.ValueR.operator AnsiString().Length() > 0 ))
    {
      //modB
      int ModID;
      ModID = AEleNodes->GetItem(AEleNodes->IndexOfID(EPItem.Input)).ModBU;
      if(ModID != 0)
        ProcessModAloneNod( AEleNodes, ACompR, ACompRS2, ACompL, ACompC, ACompM, ACompBuf, ACompIdoz, AAOIdoz, NodeModB, 1, ModID );
      ModID = AEleNodes->GetItem(AEleNodes->IndexOfID(EPItem.Output)).ModBU;
      if(ModID != 0)
        ProcessModAloneNod( AEleNodes, ACompR, ACompRS2, ACompL, ACompC, ACompM, ACompBuf, ACompIdoz, AAOIdoz, NodeModB, 1, ModID );

    } else {
      //modA
      int ModID;
      ModID = AEleNodes->GetItem(AEleNodes->IndexOfID(EPItem.Input)).ModAU;
      if(ModID != 0)
        ProcessModAloneNod( AEleNodes, ACompR, ACompRS1, ACompL, ACompC, ACompM, ACompBuf, ACompIdoz, AAOIdoz, NodeModA, 0, ModID );
      ModID = AEleNodes->GetItem(AEleNodes->IndexOfID(EPItem.Output)).ModAU;
      if(ModID != 0)
        ProcessModAloneNod( AEleNodes, ACompR, ACompRS1, ACompL, ACompC, ACompM, ACompBuf, ACompIdoz, AAOIdoz, NodeModA, 0, ModID );

    }
    // TEleNodeItem ... AEleNodes->GetItem(AEleNodes->IndexOfID(EPItem.Input));
    // TEleNodeItem ... AEleNodes->GetItem(AEleNodes->IndexOfID(EPItem.Output));

  }
  delete NodeModA;
  delete NodeModB;

}
//---------------------------------------------------------------------------

void TransSwitch ( TElePartS* AElePart, TEleNodes* AEleNodes, TElePart* AElePartRS1,  TElePart* AElePartRS2 )
{

  TElePartItemS EPItem;
  TElePartItem EPItemRS1;
  TElePartItem EPItemRS2;
  TEleNodeItem ENItem;

  for ( int i=0; i< AElePart->Count(); i++)
  {
     EPItem = AElePart->GetItem(i);
     if (( CompareStr(EPItem.Value, "on") == 0 ) && ( CompareStr(EPItem.ValueR, "0") == 0 ))
     {
       int id = AEleNodes->GetItem(EPItem.Output-1).ModAI;
       int ida = AEleNodes->GetItem(EPItem.Input-1).ModAI;
       if ( id > ida )
       {
         int temp = id ;
         id = ida;
         ida = temp;
       }

       for ( int j=0; j< AEleNodes->Count(); j++)
       {
         ENItem = AEleNodes->GetItem(j);
         if ( ENItem.ModAI == ida ) {
           ENItem.ModAI = id;
           ENItem.ModAU = id;
         }
         AEleNodes->SetItem( j, ENItem );
       }
     }
     if (( CompareStr(EPItem.Value, "on") == 0 ) && ( CompareStr(EPItem.ValueR, "0") != 0 ))
     {
       EPItemRS1.Name = ( "R" + EPItem.Name );
       EPItemRS1.Input = EPItem.Input;
       EPItemRS1.Output = EPItem.Output;
       EPItemRS1.Value = EPItem.ValueR;
       AElePartRS1->Add(EPItemRS1);
     }

     if (( CompareStr(EPItem.Value, "off") == 0 ) && ( CompareStr(EPItem.ValueR, "0") == 0 ))
     {
       int id = AEleNodes->GetItem(EPItem.Output-1).ModBI;
       int ida = AEleNodes->GetItem(EPItem.Input-1).ModBI;
       if ( id > ida )
       {
         int temp = id ;
         id = ida;
         ida = temp;
       }

       for ( int j=0; j< AEleNodes->Count(); j++)
       {
         ENItem = AEleNodes->GetItem(j);
         if ( ENItem.ModBI == ida ) {
           ENItem.ModBI = id;
           ENItem.ModBU = id;
         }
         AEleNodes->SetItem( j, ENItem );
       }
     }
     if (( CompareStr(EPItem.Value, "off") == 0 ) && ( CompareStr(EPItem.ValueR, "0") != 0 ))
     {
       EPItemRS2.Name = ( "R" + EPItem.Name );
       EPItemRS2.Input = EPItem.Input;
       EPItemRS2.Output = EPItem.Output;
       EPItemRS2.Value = EPItem.ValueR;
       AElePartRS2->Add(EPItemRS2);
     }
  }
}
//---------------------------------------------------------------------------
void ChangeModID( TEleNodes* AEleNodes, int AMod, int AModType, int AModID, int ANewModID )
{
  TEleNodeItem ENItem;
  for ( int j=0; j< AEleNodes->Count(); j++)
  {
    ENItem = AEleNodes->GetItem(j);
    if (AMod) {
      // ModB
      if (AModType) {
        if (ENItem.ModBU == AModID)
          ENItem.ModBU = ANewModID;
      } else {
        if (ENItem.ModBI == AModID)
          ENItem.ModBI = ANewModID;
      }
    } else {
      // ModA
      if (AModType) {
        if (ENItem.ModAU == AModID)
          ENItem.ModAU = ANewModID;
      } else {
        if (ENItem.ModAI == AModID)
          ENItem.ModAI = ANewModID;
      }
    }
    AEleNodes->SetItem( j, ENItem );
  }
}
//---------------------------------------------------------------------------

void TransBuf ( TElePartB* Buf, TEleNodes* AEleNodes )
{
  TElePartItemB EPItem;
  TEleNodeItem ENItem;

  for ( int i=0; i< Buf->Count(); i++)
  {
    EPItem = Buf->GetItem(i);
    int id;
    int ida;

    // pro mod A
    id = AEleNodes->GetItem(EPItem.Output-1).ModAU;
    ida = AEleNodes->GetItem(EPItem.Input-1).ModAU;
    if ( id > ida )
    {
      int temp = id ;
      id = ida;
      ida = temp;
    }

    //for ( int j=0; j< AEleNodes->Count(); j++)
    //{
    //  ENItem = AEleNodes->GetItem(j);
    //  if ( ENItem.ModAI == ida ) {
    //    ENItem.ModAU = id;
    //  }
    //  AEleNodes->SetItem( j, ENItem );
    //}
    ChangeModID(AEleNodes, 0, 1, ida, id);
    ChangeModID(AEleNodes, 0, 0,AEleNodes->GetItem(EPItem.Output-1).ModAI, 0);

    // pro mod B
    id = AEleNodes->GetItem(EPItem.Output-1).ModBU;
    ida = AEleNodes->GetItem(EPItem.Input-1).ModBU;

    if ( id > ida )
    {
      int temp = id ;
      id = ida;
      ida = temp;
    }

    //for ( int j=0; j< AEleNodes->Count(); j++)
    //{
    //  ENItem = AEleNodes->GetItem(j);
    //  if ( ENItem.ModBI == ida ) {
    //    ENItem.ModBU = id;
    //  }
    //  AEleNodes->SetItem( j, ENItem );
    //}
    ChangeModID(AEleNodes, 1, 1, ida, id);
    ChangeModID(AEleNodes, 1, 0,AEleNodes->GetItem(EPItem.Output-1).ModBI, 0);

  }
}
//---------------------------------------------------------------------------

void TransAO ( TEleIDOZ* AO, TEleNodes* AEleNodes )
{
  TEleIDOZItem EAOItem;
  TEleNodeItem ENItem;

  for ( int i=0; i< AO->Count(); i++)
  {
    EAOItem = AO->GetItem(i);
    int id;
    int ida;
    int idb;

    // pro mod A
    //id = AEleNodes->GetItem(EAOItem.Output-1).ModAU
    ida = AEleNodes->GetItem(EAOItem.InputA-1).ModAU;
    idb = AEleNodes->GetItem(EAOItem.InputB-1).ModAU;

    if ( idb > ida )
    {
      int temp = idb ;
      idb = ida;
      ida = temp;
    }

    //if ( id > ida )
    //{
    //  int temp = id ;
    //  id = ida;
    //  ida = temp;
    //}
    //if ( id > idb )
    //{
    //  int temp = id ;
    //  id = idb;
    //  idb = temp;
    //}

    //for ( int j=0; j< AEleNodes->Count(); j++)
    //{
    //  ENItem = AEleNodes->GetItem(j);
    //  if ( ENItem.ModAI == ida ) {
    //    ENItem.ModAU = id;
    //  }
    //  AEleNodes->SetItem( j, ENItem );
    //}
    ChangeModID(AEleNodes, 0, 1, ida, idb);
    ChangeModID(AEleNodes, 0, 0, AEleNodes->GetItem(EAOItem.Output-1).ModAI, 0);

    // pro mod B
    //id = AEleNodes->GetItem(EAOItem.Output-1).ModBU;
    ida = AEleNodes->GetItem(EAOItem.InputA-1).ModBU;
    idb = AEleNodes->GetItem(EAOItem.InputB-1).ModBU;

    if ( idb > ida )
    {
      int temp = idb ;
      idb = ida;
      ida = temp;
    }

    //if ( id > ida )
    //{
    //  int temp = id ;
    //  id = ida;
    //  ida = temp;
    //}
    //if ( id > idb )
    //{
    //  int temp = id ;
    //  id = idb;
    //  idb = temp;
    //}

    //for ( int j=0; j< AEleNodes->Count(); j++)
    //{
    //  ENItem = AEleNodes->GetItem(j);
    //  if ( ENItem.ModBI == ida ) {
    //    ENItem.ModBU = id;
    //  }
    //  AEleNodes->SetItem( j, ENItem );
    //}
    ChangeModID(AEleNodes, 1, 1, ida, idb);
    ChangeModID(AEleNodes, 1, 0,AEleNodes->GetItem(EAOItem.Output-1).ModBI, 0);

  }
}
//---------------------------------------------------------------------------

void SortChangeAllMod(TEleNodes* AEleNodes)
{
  TIOList* IOList;

  // ModAU
  IOList = new TIOList;
  for ( int i=0; i < AEleNodes->Count(); i++) {
    TEleNodeItem ENItem = AEleNodes->GetItem(i);
    if (IOList->IndexOfInput(ENItem.ModAU) == -1) {
      TIOListItem IOLItem;
      IOLItem.Input = ENItem.ModAU;
      IOList->Add(IOLItem);
    }
  }

  for ( int i=0; i < IOList->Count()-1; i++) {
    for ( int j=0; j < IOList->Count()-1; j++) {
      TIOListItem IOLItemA = IOList->GetItem(j);
      TIOListItem IOLItemB = IOList->GetItem(j+1);
      TIOListItem tempIOLItem;
      if(IOLItemA.Input > IOLItemB.Input) {
        tempIOLItem = IOLItemA;
        IOLItemA = IOLItemB;
        IOLItemB = tempIOLItem;
      }
      IOList->SetItem(j, IOLItemA);
      IOList->SetItem(j+1, IOLItemB);
    }
  }

  for ( int i=0; i < AEleNodes->Count(); i++) {
    TEleNodeItem ENItem = AEleNodes->GetItem(i);
    ENItem.ModAU = IOList->IndexOfInput(ENItem.ModAU);
    AEleNodes->SetItem(i, ENItem);
  }
  delete IOList;


  // ModAI
  IOList = new TIOList;
  for ( int i=0; i < AEleNodes->Count(); i++) {
    TEleNodeItem ENItem = AEleNodes->GetItem(i);
    if (IOList->IndexOfInput(ENItem.ModAI) == -1) {
      TIOListItem IOLItem;
      IOLItem.Input = ENItem.ModAI;
      IOList->Add(IOLItem);
    }
  }

  for ( int i=0; i < IOList->Count()-1; i++) {
    for ( int j=0; j < IOList->Count()-1; j++) {
      TIOListItem IOLItemA = IOList->GetItem(j);
      TIOListItem IOLItemB = IOList->GetItem(j+1);
      TIOListItem tempIOLItem;
      if(IOLItemA.Input > IOLItemB.Input) {
        tempIOLItem = IOLItemA;
        IOLItemA = IOLItemB;
        IOLItemB = tempIOLItem;
      }
      IOList->SetItem(j, IOLItemA);
      IOList->SetItem(j+1, IOLItemB);
    }
  }

  for ( int i=0; i < AEleNodes->Count(); i++) {
    TEleNodeItem ENItem = AEleNodes->GetItem(i);
    ENItem.ModAI = IOList->IndexOfInput(ENItem.ModAI);
    AEleNodes->SetItem(i, ENItem);
  }
  delete IOList;


  // ModBU
  IOList = new TIOList;
  for ( int i=0; i < AEleNodes->Count(); i++) {
    TEleNodeItem ENItem = AEleNodes->GetItem(i);
    if (IOList->IndexOfInput(ENItem.ModBU) == -1) {
      TIOListItem IOLItem;
      IOLItem.Input = ENItem.ModBU;
      IOList->Add(IOLItem);
    }
  }

  for ( int i=0; i < IOList->Count()-1; i++) {
    for ( int j=0; j < IOList->Count()-1; j++) {
      TIOListItem IOLItemA = IOList->GetItem(j);
      TIOListItem IOLItemB = IOList->GetItem(j+1);
      TIOListItem tempIOLItem;
      if(IOLItemA.Input > IOLItemB.Input) {
        tempIOLItem = IOLItemA;
        IOLItemA = IOLItemB;
        IOLItemB = tempIOLItem;
      }
      IOList->SetItem(j, IOLItemA);
      IOList->SetItem(j+1, IOLItemB);
    }
  }

  for ( int i=0; i < AEleNodes->Count(); i++) {
    TEleNodeItem ENItem = AEleNodes->GetItem(i);
    ENItem.ModBU = IOList->IndexOfInput(ENItem.ModBU);
    AEleNodes->SetItem(i, ENItem);
  }
  delete IOList;


  // ModBI
  IOList = new TIOList;
  for ( int i=0; i < AEleNodes->Count(); i++) {
    TEleNodeItem ENItem = AEleNodes->GetItem(i);
    if (IOList->IndexOfInput(ENItem.ModBI) == -1) {
      TIOListItem IOLItem;
      IOLItem.Input = ENItem.ModBI;
      IOList->Add(IOLItem);
    }
  }

  for ( int i=0; i < IOList->Count()-1; i++) {
    for ( int j=0; j < IOList->Count()-1; j++) {
      TIOListItem IOLItemA = IOList->GetItem(j);
      TIOListItem IOLItemB = IOList->GetItem(j+1);
      TIOListItem tempIOLItem;
      if(IOLItemA.Input > IOLItemB.Input) {
        tempIOLItem = IOLItemA;
        IOLItemA = IOLItemB;
        IOLItemB = tempIOLItem;
      }
      IOList->SetItem(j, IOLItemA);
      IOList->SetItem(j+1, IOLItemB);
    }
  }

  for ( int i=0; i < AEleNodes->Count(); i++) {
    TEleNodeItem ENItem = AEleNodes->GetItem(i);
    ENItem.ModBI = IOList->IndexOfInput(ENItem.ModBI);
    AEleNodes->SetItem(i, ENItem);
  }
  delete IOList;
}
//---------------------------------------------------------------------------
// Transformace uzlů
void __fastcall TForm1::Button3Click(TObject *Sender)
{
  // user friendly
   Button3->Visible = false;
   Button2->Visible = true;

  LoadFromListView( LV1, CompR, Nodes );
  LoadFromListViewL( LV2, CompL, Nodes );
  LoadFromListView( LV3, CompC, Nodes );
  LoadFromListViewS( LV9, Switch, Nodes );
  LoadFromListViewM( LV11, CompM, Nodes );
  LoadFromEleAO( LV8, AOIdoz, Nodes);
  LoadFromListViewBuf( LV12, CompBuf, Nodes );
  LoadFromEleIDOZ( LV13, CompIdoz, Nodes);

  //WriteEleNodes(Nodes, ListView1);
  //WriteSortEleNodes(Nodes, ListView1);

  // Změna v Mod Idenifikátory na jména uzlů
  IDProcessing( Nodes );

  // Transformace působením spínačů
  TransSwitch ( Switch, Nodes, CompRS1, CompRS2 );

  // Vynechání nepřipojených uzlů
  TransAloneSwitch( Nodes, Switch, CompR, CompRS1, CompRS2, CompL, CompC, CompM, CompBuf, CompIdoz, AOIdoz);

  // Transformace vlivem neregulárních prvků 1-BUFFER 2-IDOZ
  TransBuf( CompBuf, Nodes );
  TransAO( CompIdoz, Nodes );

  WriteEleNodes(Nodes, ListView1);       // výpis uzlů
  WriteSortEleNodes(Nodes, ListView7);   // seřazení uzlů

  // Vzestupné přečíslování koeficientů
  SortChangeAllMod(Nodes);

  //WriteEleNodes(Nodes, ListView8);
  WriteSortEleNodes(Nodes, ListView8);   // Výpis po transformaci uzlů

  WriteElePartS(Switch, ListView2);     // spínače
  WriteElePartL(CompL, ListView3);      // cívky
  WriteElePart(CompRS1, ListView4);     // odpor spínačů sepnutých ve fázi 1
  WriteElePart(CompRS2, ListView5);     // odpor spínačů sepnutých ve fázi 2
  WriteEleAO (AOIdoz, ListView6);       // ideální OZ
}
//---------------------------------------------------------------------------
// hledání nejvyšího čísla uzlu pro tvorbu obvodové matice symbolicky
void MaxNodValueAllMod(TEleNodes* AEleNodes, int* AModAUCount, int* AModAICount, int* AModBUCount, int* AModBICount)

{
 TIOList* IOList;

  // ModAU
  IOList = new TIOList;
  for ( int i=0; i < AEleNodes->Count(); i++) {
    TEleNodeItem ENItem = AEleNodes->GetItem(i);
    if (IOList->IndexOfInput(ENItem.ModAU) == -1) {
      TIOListItem IOLItem;
      IOLItem.Input = ENItem.ModAU;
      IOList->Add(IOLItem);
    }
  }
  *AModAUCount = IOList->Count();
  delete IOList;

  // ModAI
  IOList = new TIOList;
  for ( int i=0; i < AEleNodes->Count(); i++) {
    TEleNodeItem ENItem = AEleNodes->GetItem(i);
    if (IOList->IndexOfInput(ENItem.ModAI) == -1) {
      TIOListItem IOLItem;
      IOLItem.Input = ENItem.ModAI;
      IOList->Add(IOLItem);
    }
  }
  *AModAICount = IOList->Count();
  delete IOList;

  // ModBU
  IOList = new TIOList;
  for ( int i=0; i < AEleNodes->Count(); i++) {
    TEleNodeItem ENItem = AEleNodes->GetItem(i);
    if (IOList->IndexOfInput(ENItem.ModBU) == -1) {
      TIOListItem IOLItem;
      IOLItem.Input = ENItem.ModBU;
      IOList->Add(IOLItem);
    }
  }
  *AModBUCount = IOList->Count();
  delete IOList;

  // ModBI
  IOList = new TIOList;
  for ( int i=0; i < AEleNodes->Count(); i++) {
    TEleNodeItem ENItem = AEleNodes->GetItem(i);
    if (IOList->IndexOfInput(ENItem.ModBI) == -1) {
      TIOListItem IOLItem;
      IOLItem.Input = ENItem.ModBI;
      IOList->Add(IOLItem);
    }
  }
  *AModBICount = IOList->Count();
  delete IOList;
}

//---------------------------------------------------------------------------
void WriteSubArray ( TListView* AListView, TStringArray* FCircuitArray )
{

  for(int i = 0; i < FCircuitArray->GetColCount(); i++) {
    TListColumn* ListColumn = AListView->Columns->Add();
    ListColumn->Caption = IntToStr(i);
  }
  for(int j = 0; j < FCircuitArray->GetRowCount(); j++) {
    TListItem* ListItem = AListView->Items->Add();
    for(int i = 0; i < FCircuitArray->GetColCount(); i++) {
      if (i == 0) {
        ListItem->Caption = FCircuitArray->GetItem(i, j);
      } else {
        ListItem->SubItems->Add(FCircuitArray->GetItem(i, j));
      }
    }
  }

}
//---------------------------------------------------------------------------
void WriteSubComplexArray ( TListView* AListView, TComplexArray* FComplexArray )
{
  FCOMPLEX Cvalue;
  AnsiString kompl;

for(int i = 0; i < FComplexArray->GetColCount(); i++)
  {
    TListColumn* ListColumn = AListView->Columns->Add();
    ListColumn->Caption = IntToStr(i);
  }
  for(int j = 0; j < FComplexArray->GetRowCount(); j++)
  {
    TListItem* ListItem = AListView->Items->Add();
    for(int i = 0; i < FComplexArray->GetColCount(); i++)
    {
      Cvalue = FComplexArray->GetItem(i,j);
      if (i == 0)
      {
        kompl = ValueDoubleToStr(Cvalue.r) + "+(" + ValueDoubleToStr(Cvalue.i) + ")i";
        ListItem->Caption = kompl;
      } else {
        kompl = ValueDoubleToStr(Cvalue.r) + "+(" + ValueDoubleToStr(Cvalue.i) + ")i";
        ListItem->SubItems->Add(kompl);
      }
    }
  }

}
//---------------------------------------------------------------------------
AnsiString WriteSubComplexArrayToM( TComplexArray* FComplexArray, AnsiString Matice )
{
  AnsiString HodnotaS;
  FCOMPLEX HodnotaC;

  Matice = Matice + "=[";
   for ( int i=0; i< FComplexArray->GetRowCount(); i++)
   {
     for ( int ii=0; ii< FComplexArray->GetColCount(); ii++)
     {

       HodnotaC = FComplexArray->GetItem(ii,i);
       HodnotaS = ValueDoubleToStr(HodnotaC.r);
       // pro vypsání reálné i imaginarní části do Mema MFile
       //HodnotaS = ValueDoubleToStr(HodnotaC.r) + "+(" + ValueWithDot(HodnotaC.i) + ")*i";

       if (ii==0) {
         Matice = Matice + HodnotaS;
       } else {
         Matice = Matice + ',' + HodnotaS;
       }
     }
     if (i==FComplexArray->GetRowCount()-1) {
       Matice = Matice + "];";
     } else {
       Matice = Matice + ';';
     }
   }
  return Matice;
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Button2Click(TObject *Sender) // obvodové matice - numericky
/* Obvodové matice numericky pro metodu výpočtu přes submatice     *******************ZAČÁTEK*******************
// vytvoří 4 matice Y1, C12, C21, Y2
{
  Memo3->Clear();
  ListView9->Clear();
  ListView10->Clear();
  ListView11->Clear();
  ListView12->Clear();
  ListView13->Clear();
  ListView14->Clear();
  ListView15->Clear();
  ListView16->Clear();

  int ModAUCount=0; int ModAICount=0; int ModBUCount=0; int ModBICount=0;
  int CRAdd=0; //přídavný řádek a sloupec

  int ic=0; int ir=0; // číslo přídavného sloupce a řádku

  int a1u=0; int a2u=0; int b1u=0; int b2u=0;
  int a1u2=0; int a2u2=0; int b1u2=0; int b2u2=0;
  int a1i=0; int a2i=0; int b1i=0; int b2i=0;
  int a1i2=0; int a2i2=0; int b1i2=0; int b2i2=0;
  int InNode=0; int OutNode=0;

  //vytvoření obovodových matic Y1, C12, C21, Y2
  MaxNodValueAllMod(Nodes, &ModAUCount, &ModAICount, &ModBUCount, &ModBICount);

  CRAdd = CompL->Count() + 2 * CompM->Count() + AOIdoz->Count();
  //  ShowMessage(IntToStr(ColIndex));   ShowMessage(IntToStr(ColIndexF));
  ic = ModAUCount;
  ir = ModAICount;

  FComplexCircuitArrayY1 = new TComplexArray( ModAUCount + CRAdd, ModAICount + CRAdd );
  FComplexCircuitArrayC12 = new TComplexArray( ModBUCount + CRAdd, ModAICount + CRAdd );
  FComplexCircuitArrayC21 = new TComplexArray( ModAUCount + CRAdd, ModBICount + CRAdd );
  FComplexCircuitArrayY2 = new TComplexArray( ModBUCount + CRAdd, ModBICount + CRAdd );
  if (ModAUCount != ModAICount) ShowMessage("Chyba v obvodu. Y1 není čtvercová matice");
  if (ModBUCount != ModBICount) ShowMessage("Chyba v obvodu. Y2 není čtvercová matice");

  // plnění matic součastkou R  ************************************************
  for ( int i=0; i< CompR->Count(); i++)
  {
     // vstupni uzel součástky
     InNode = Nodes->IndexOfID(CompR->GetItem(i).Input);
     a1u = Nodes->GetItem(InNode).ModAU;
     a1i = Nodes->GetItem(InNode).ModAI;
     a2u = Nodes->GetItem(InNode).ModBU;
     a2i = Nodes->GetItem(InNode).ModBI;
     // výstupní uzel součástky
     OutNode = Nodes->IndexOfID(CompR->GetItem(i).Output);
     b1u = Nodes->GetItem(OutNode).ModAU;
     b1i = Nodes->GetItem(OutNode).ModAI;
     b2u = Nodes->GetItem(OutNode).ModBU;
     b2i = Nodes->GetItem(OutNode).ModBI;

     // plneni Y1
     FComplexCircuitArrayY1->SetItemSumReIm(a1u,a1i,1/(ValueStrToDouble(CompR->GetItem(i).Value)),0);
     FComplexCircuitArrayY1->SetItemSumReIm(a1u,b1i,-1/ValueStrToDouble(CompR->GetItem(i).Value),0);
     FComplexCircuitArrayY1->SetItemSumReIm(b1u,a1i,-1/ValueStrToDouble(CompR->GetItem(i).Value),0);
     FComplexCircuitArrayY1->SetItemSumReIm(b1u,b1i,1/ValueStrToDouble(CompR->GetItem(i).Value),0);

     // plneni Y2
     FComplexCircuitArrayY2->SetItemSumReIm(a2u,a2i,1/ValueStrToDouble(CompR->GetItem(i).Value),0);
     FComplexCircuitArrayY2->SetItemSumReIm(a2u,b2i,-1/ValueStrToDouble(CompR->GetItem(i).Value),0);
     FComplexCircuitArrayY2->SetItemSumReIm(b2u,a2i,-1/ValueStrToDouble(CompR->GetItem(i).Value),0);
     FComplexCircuitArrayY2->SetItemSumReIm(b2u,b2i,1/ValueStrToDouble(CompR->GetItem(i).Value),0);
  }

  // plnění matic součastkou RS1  ************************************************
  for ( int i=0; i< CompRS1->Count(); i++)
  {
     // vstupni uzel součástky
     InNode = Nodes->IndexOfID(CompRS1->GetItem(i).Input);
     a1u = Nodes->GetItem(InNode).ModAU;
     a1i = Nodes->GetItem(InNode).ModAI;
     a2u = Nodes->GetItem(InNode).ModBU;
     a2i = Nodes->GetItem(InNode).ModBI;
     // výstupní uzel součástky
     OutNode = Nodes->IndexOfID(CompRS1->GetItem(i).Output);
     b1u = Nodes->GetItem(OutNode).ModAU;
     b1i = Nodes->GetItem(OutNode).ModAI;
     b2u = Nodes->GetItem(OutNode).ModBU;
     b2i = Nodes->GetItem(OutNode).ModBI;

     // plneni Y1
     FComplexCircuitArrayY1->SetItemSumReIm(a1u,a1i,1/ValueStrToDouble(CompRS1->GetItem(i).Value),0);
     FComplexCircuitArrayY1->SetItemSumReIm(a1u,b1i,-1/ValueStrToDouble(CompRS1->GetItem(i).Value),0);
     FComplexCircuitArrayY1->SetItemSumReIm(b1u,a1i,-1/ValueStrToDouble(CompRS1->GetItem(i).Value),0);
     FComplexCircuitArrayY1->SetItemSumReIm(b1u,b1i,1/ValueStrToDouble(CompRS1->GetItem(i).Value),0);
  }

  // plnění matic součastkou RS2  ************************************************
  for ( int i=0; i< CompRS2->Count(); i++)
  {
     // vstupni uzel součástky
     InNode = Nodes->IndexOfID(CompRS2->GetItem(i).Input);
     a1u = Nodes->GetItem(InNode).ModAU;
     a1i = Nodes->GetItem(InNode).ModAI;
     a2u = Nodes->GetItem(InNode).ModBU;
     a2i = Nodes->GetItem(InNode).ModBI;
     // výstupní uzel součástky
     OutNode = Nodes->IndexOfID(CompRS2->GetItem(i).Output);
     b1u = Nodes->GetItem(OutNode).ModAU;
     b1i = Nodes->GetItem(OutNode).ModAI;
     b2u = Nodes->GetItem(OutNode).ModBU;
     b2i = Nodes->GetItem(OutNode).ModBI;

     // plneni Y2
     FComplexCircuitArrayY2->SetItemSumReIm(a2u,a2i,1/ValueStrToDouble(CompRS2->GetItem(i).Value),0);
     FComplexCircuitArrayY2->SetItemSumReIm(a2u,b2i,-1/ValueStrToDouble(CompRS2->GetItem(i).Value),0);
     FComplexCircuitArrayY2->SetItemSumReIm(b2u,a2i,-1/ValueStrToDouble(CompRS2->GetItem(i).Value),0);
     FComplexCircuitArrayY2->SetItemSumReIm(b2u,b2i,1/ValueStrToDouble(CompRS2->GetItem(i).Value),0);
  }

  // plnění matic součastkou C  ************************************************
  for ( int i=0; i< CompC->Count(); i++)
  {
     // vstupni uzel součástky
     InNode = Nodes->IndexOfID(CompC->GetItem(i).Input);
     a1u = Nodes->GetItem(InNode).ModAU;
     a1i = Nodes->GetItem(InNode).ModAI;
     a2u = Nodes->GetItem(InNode).ModBU;
     a2i = Nodes->GetItem(InNode).ModBI;
     // výstupní uzel součástky
     OutNode = Nodes->IndexOfID(CompC->GetItem(i).Output);
     b1u = Nodes->GetItem(OutNode).ModAU;
     b1i = Nodes->GetItem(OutNode).ModAI;
     b2u = Nodes->GetItem(OutNode).ModBU;
     b2i = Nodes->GetItem(OutNode).ModBI;

     // plneni Y1
     FComplexCircuitArrayY1->SetItemSumReIm(a1u,a1i,0,ValueStrToDouble(CompC->GetItem(i).Value));
     FComplexCircuitArrayY1->SetItemSumReIm(a1u,b1i,0,-1*ValueStrToDouble(CompC->GetItem(i).Value));
     FComplexCircuitArrayY1->SetItemSumReIm(b1u,a1i,0,-1*ValueStrToDouble(CompC->GetItem(i).Value));
     FComplexCircuitArrayY1->SetItemSumReIm(b1u,b1i,0,ValueStrToDouble(CompC->GetItem(i).Value));

     // plneni C12
     FComplexCircuitArrayC12->SetItemSumReIm(a2u,a1i,ValueStrToDouble(CompC->GetItem(i).Value),0);
     FComplexCircuitArrayC12->SetItemSumReIm(a2u,b1i,-1*ValueStrToDouble(CompC->GetItem(i).Value),0);
     FComplexCircuitArrayC12->SetItemSumReIm(b2u,a1i,-1*ValueStrToDouble(CompC->GetItem(i).Value),0);
     FComplexCircuitArrayC12->SetItemSumReIm(b2u,b1i,ValueStrToDouble(CompC->GetItem(i).Value),0);

     // plneni C21
     FComplexCircuitArrayC21->SetItemSumReIm(a1u,a2i,ValueStrToDouble(CompC->GetItem(i).Value),0);
     FComplexCircuitArrayC21->SetItemSumReIm(a1u,b2i,-1*ValueStrToDouble(CompC->GetItem(i).Value),0);
     FComplexCircuitArrayC21->SetItemSumReIm(b1u,a2i,-1*ValueStrToDouble(CompC->GetItem(i).Value),0);
     FComplexCircuitArrayC21->SetItemSumReIm(b1u,b2i,ValueStrToDouble(CompC->GetItem(i).Value),0);

     // plneni Y2
     FComplexCircuitArrayY2->SetItemSumReIm(a2u,a2i,0,ValueStrToDouble(CompC->GetItem(i).Value));
     FComplexCircuitArrayY2->SetItemSumReIm(a2u,b2i,0,-1*ValueStrToDouble(CompC->GetItem(i).Value));
     FComplexCircuitArrayY2->SetItemSumReIm(b2u,a2i,0,-1*ValueStrToDouble(CompC->GetItem(i).Value));
     FComplexCircuitArrayY2->SetItemSumReIm(b2u,b2i,0,ValueStrToDouble(CompC->GetItem(i).Value));
  }

  // plnění matic součastkou L  ************************************************
  for ( int i=0; i< CompL->Count(); i++)
  {
     // vstupni uzel součástky
     InNode = Nodes->IndexOfID(CompL->GetItem(i).Input);
     a1u = Nodes->GetItem(InNode).ModAU;
     a1i = Nodes->GetItem(InNode).ModAI;
     a2u = Nodes->GetItem(InNode).ModBU;
     a2i = Nodes->GetItem(InNode).ModBI;
     // výstupní uzel součástky
     OutNode = Nodes->IndexOfID(CompL->GetItem(i).Output);
     b1u = Nodes->GetItem(OutNode).ModAU;
     b1i = Nodes->GetItem(OutNode).ModAI;
     b2u = Nodes->GetItem(OutNode).ModBU;
     b2i = Nodes->GetItem(OutNode).ModBI;

     // plneni Y1
     FComplexCircuitArrayY1->SetItemSumReIm(a1u,ir,-1,0);
     FComplexCircuitArrayY1->SetItemSumReIm(b1u,ir,1,0);
     FComplexCircuitArrayY1->SetItemSumReIm(ic,b1i,-1,0);
     FComplexCircuitArrayY1->SetItemSumReIm(ic,a1i,1,0);
     FComplexCircuitArrayY1->SetItemSumReIm(ic,ir,ValueStrToDouble(CompL->GetItem(i).ValueRs),ValueStrToDouble(CompL->GetItem(i).ValueLs));

     // plneni C12
     FComplexCircuitArrayC12->SetItemSumReIm(ic,ir,ValueStrToDouble(CompL->GetItem(i).ValueLs),0);

     // plneni C21
     FComplexCircuitArrayC21->SetItemSumReIm(ic,ir,ValueStrToDouble(CompL->GetItem(i).ValueLs),0);

     // plneni Y2
     FComplexCircuitArrayY2->SetItemSumReIm(a2u,ir,-1,0);
     FComplexCircuitArrayY2->SetItemSumReIm(b2u,ir,1,0);
     FComplexCircuitArrayY2->SetItemSumReIm(ic,b2i,-1,0);
     FComplexCircuitArrayY2->SetItemSumReIm(ic,a2i,1,0);
     FComplexCircuitArrayY2->SetItemSumReIm(ic,ir,ValueStrToDouble(CompL->GetItem(i).ValueRs),ValueStrToDouble(CompL->GetItem(i).ValueLs));

     ic++;
     ir++;
  }

  // plnění matic součastkou M  ************************************************
  for ( int i=0; i< CompM->Count(); i++)
  {
     // vstupni uzel součástky
     InNode = Nodes->IndexOfID(CompM->GetItem(i).InputA);
     a1u = Nodes->GetItem(InNode).ModAU;
     a1i = Nodes->GetItem(InNode).ModAI;
     a2u = Nodes->GetItem(InNode).ModBU;
     a2i = Nodes->GetItem(InNode).ModBI;
     // výstupní uzel součástky
     OutNode = Nodes->IndexOfID(CompM->GetItem(i).OutputB);
     b1u = Nodes->GetItem(OutNode).ModAU;
     b1i = Nodes->GetItem(OutNode).ModAI;
     b2u = Nodes->GetItem(OutNode).ModBU;
     b2i = Nodes->GetItem(OutNode).ModBI;

     // plneni Y1
     FComplexCircuitArrayY1->SetItemSumReIm(a1u,ir,-1,0);
     FComplexCircuitArrayY1->SetItemSumReIm(b1u,ir,1,0);
     FComplexCircuitArrayY1->SetItemSumReIm(ic,b1i,-1,0);
     FComplexCircuitArrayY1->SetItemSumReIm(ic,a1i,1,0);
     FComplexCircuitArrayY1->SetItemSumReIm(ic,ir,ValueStrToDouble(CompM->GetItem(i).ValueR1),ValueStrToDouble(CompM->GetItem(i).ValueL1));
     FComplexCircuitArrayY1->SetItemSumReIm(ic,ir+1,0,ValueStrToDouble(CompM->GetItem(i).ValueK));
     FComplexCircuitArrayY1->SetItemSumReIm(ic+1,ir,0,ValueStrToDouble(CompM->GetItem(i).ValueK));
     FComplexCircuitArrayY1->SetItemSumReIm(ic+1,ir+1,ValueStrToDouble(CompM->GetItem(i).ValueR2),ValueStrToDouble(CompM->GetItem(i).ValueL2));

     // plneni C12
     FComplexCircuitArrayC12->SetItemSumReIm(ic,ir,ValueStrToDouble(CompM->GetItem(i).ValueL1),0);
     FComplexCircuitArrayC12->SetItemSumReIm(ic,ir+1,ValueStrToDouble(CompM->GetItem(i).ValueK),0);
     FComplexCircuitArrayC12->SetItemSumReIm(ic+1,ir,ValueStrToDouble(CompM->GetItem(i).ValueK),0);
     FComplexCircuitArrayC12->SetItemSumReIm(ic+1,ir+1,ValueStrToDouble(CompM->GetItem(i).ValueL2),0);

     // plneni C21
     FComplexCircuitArrayC21->SetItemSumReIm(ic,ir,ValueStrToDouble(CompM->GetItem(i).ValueL1),0);
     FComplexCircuitArrayC21->SetItemSumReIm(ic,ir+1,ValueStrToDouble(CompM->GetItem(i).ValueK),0);
     FComplexCircuitArrayC21->SetItemSumReIm(ic+1,ir,ValueStrToDouble(CompM->GetItem(i).ValueK),0);
     FComplexCircuitArrayC21->SetItemSumReIm(ic+1,ir+1,ValueStrToDouble(CompM->GetItem(i).ValueL2),0);

     // plneni Y2
     FComplexCircuitArrayY1->SetItemSumReIm(a2u,ir,-1,0);
     FComplexCircuitArrayY1->SetItemSumReIm(b2u,ir,1,0);
     FComplexCircuitArrayY1->SetItemSumReIm(ic,b2i,-1,0);
     FComplexCircuitArrayY1->SetItemSumReIm(ic,a2i,1,0);
     FComplexCircuitArrayY1->SetItemSumReIm(ic,ir,ValueStrToDouble(CompM->GetItem(i).ValueR1),ValueStrToDouble(CompM->GetItem(i).ValueL1));
     FComplexCircuitArrayY1->SetItemSumReIm(ic,ir+1,0,ValueStrToDouble(CompM->GetItem(i).ValueK));
     FComplexCircuitArrayY1->SetItemSumReIm(ic+1,ir,0,ValueStrToDouble(CompM->GetItem(i).ValueK));
     FComplexCircuitArrayY1->SetItemSumReIm(ic+1,ir+1,ValueStrToDouble(CompM->GetItem(i).ValueR2),ValueStrToDouble(CompM->GetItem(i).ValueL2));

     ic++;
     ir++;

     // vstupni uzel součástky
     InNode = Nodes->IndexOfID(CompM->GetItem(i).InputC);
     a1u = Nodes->GetItem(InNode).ModAU;
     a1i = Nodes->GetItem(InNode).ModAI;
     a2u = Nodes->GetItem(InNode).ModBU;
     a2i = Nodes->GetItem(InNode).ModBI;
     // výstupní uzel součástky
     OutNode = Nodes->IndexOfID(CompM->GetItem(i).OutputD);
     b1u = Nodes->GetItem(OutNode).ModAU;
     b1i = Nodes->GetItem(OutNode).ModAI;
     b2u = Nodes->GetItem(OutNode).ModBU;
     b2i = Nodes->GetItem(OutNode).ModBI;

     // plneni Y1
     FComplexCircuitArrayY1->SetItemSumReIm(a1u,ir,-1,0);
     FComplexCircuitArrayY1->SetItemSumReIm(b1u,ir,1,0);
     FComplexCircuitArrayY1->SetItemSumReIm(ic,b1i,-1,0);
     FComplexCircuitArrayY1->SetItemSumReIm(ic,a1i,1,0);

     // plneni Y2
     FComplexCircuitArrayY1->SetItemSumReIm(a2u,ir,-1,0);
     FComplexCircuitArrayY1->SetItemSumReIm(b2u,ir,1,0);
     FComplexCircuitArrayY1->SetItemSumReIm(ic,b2i,-1,0);
     FComplexCircuitArrayY1->SetItemSumReIm(ic,a2i,1,0);

     ic++;
     ir++;
  }

/*  // plnění matic součastkou OA1p  ************************************************ // Chce to ještě upřesnit co a jak!
  for ( int i=0; i< AOIdoz->Count(); i++)
  {
     // vstupni uzel A součástky
     InNode = Nodes->IndexOfID(AOIdoz->GetItem(i).InputA);
     a1u = Nodes->GetItem(InNode).ModAU;
     a1i = Nodes->GetItem(InNode).ModAI;
     a2u = Nodes->GetItem(InNode).ModBU;
     a2i = Nodes->GetItem(InNode).ModBI;
     // vstupni uzel B součástky
     InNode = Nodes->IndexOfID(AOIdoz->GetItem(i).InputB);
     a1u2 = Nodes->GetItem(InNode).ModAU;
     a1i2 = Nodes->GetItem(InNode).ModAI;
     a2u2 = Nodes->GetItem(InNode).ModBU;
     a2i2 = Nodes->GetItem(InNode).ModBI;
     // výstupní uzel součástky
     OutNode = Nodes->IndexOfID(AOIdoz->GetItem(i).Output);
     b1u = Nodes->GetItem(OutNode).ModAU;
     b1i = Nodes->GetItem(OutNode).ModAI;
     b2u = Nodes->GetItem(OutNode).ModBU;
     b2i = Nodes->GetItem(OutNode).ModBI;


     // plneni Y1
     FComplexCircuitArrayY1->SetItem(a1u,a1i,Cmul(FComplexCircuitArrayY1->GetItem(a1u,a1i),Complex(ValueStrToDouble(AOIdoz->GetItem(i).ValueRout),0)));
     FComplexCircuitArrayY1->SetItem(a1u2,a1i,Cmul(FComplexCircuitArrayY1->GetItem(a1u2,a1i),Complex(ValueStrToDouble(AOIdoz->GetItem(i).ValueRout),0)));
     FComplexCircuitArrayY1->SetItem(b1u,a1i,Cmul(FComplexCircuitArrayY1->GetItem(b1u,a1i),Complex(ValueStrToDouble(AOIdoz->GetItem(i).ValueRout),0)));
       FComplexCircuitArrayY1->SetItemSumReIm(b1u,a1i,1,0);
     FComplexCircuitArrayY1->SetItemSumReIm(ic,a1i,-1,0);
     FComplexCircuitArrayY1->SetItemSumReIm(a1u,ir,-1,0);
     FComplexCircuitArrayY1->SetItemSumReIm(a1u2,ir,1,0);

     FComplexCircuitArrayY1->SetItemSumReIm(ic,ir,1/(AOIdoz->GetItem(i).ValueA),0);


     FComplexCircuitArrayY1->SetItemSumReIm(ic,ir,ValueStrToDouble(FComplexCircuitArrayY1->GetItem(ic,ir)) + 1 / ValueStrToDouble(AOIdoz->GetItem(i).ValueA));

     // plneni Y1s
     FComplexCircuitArrayY1s->SetItemSumReIm(a1u,a1i, ValueStrToDouble(FComplexCircuitArrayY1s->GetItem(a1u, a1i)) / ValueStrToDouble(AOIdoz->GetItem(i).ValueRout));
     FComplexCircuitArrayY1s->SetItemSumReIm(a1u2,a1i, ValueStrToDouble(FComplexCircuitArrayY1s->GetItem(a1u2, a1i)) / ValueStrToDouble(AOIdoz->GetItem(i).ValueRout));
     FComplexCircuitArrayY1s->SetItemSumReIm(b1u,a1i, ValueStrToDouble(FComplexCircuitArrayY1s->GetItem(b1u, a1i)) / ValueStrToDouble(AOIdoz->GetItem(i).ValueRout));
     FComplexCircuitArrayY1s->SetItemSumReIm(ic,ir,ValueStrToDouble(FComplexCircuitArrayY1s->GetItem(ic, ir)) + 1 / ( 2 * 3,1415 * ValueStrToDouble(AOIdoz->GetItem(i).ValueFt)));

     // plneni C12
     FComplexCircuitArrayC12->SetItemSumReIm(a2u,a1i,ValueStrToDouble(FComplexCircuitArrayC12->GetItem(a2u, a1i)) / ValueStrToDouble(AOIdoz->GetItem(i).ValueRout));
     FComplexCircuitArrayC12->SetItemSumReIm(a2u2,a1i,ValueStrToDouble(FComplexCircuitArrayC12->GetItem(a2u2, a1i)) / ValueStrToDouble(AOIdoz->GetItem(i).ValueRout));
     FComplexCircuitArrayC12->SetItemSumReIm(b2u,a1i,ValueStrToDouble(FComplexCircuitArrayC12->GetItem(b2u, a1i)) / ValueStrToDouble(AOIdoz->GetItem(i).ValueRout));
     FComplexCircuitArrayC12->SetItemSumReIm(ic,a1i,ValueStrToDouble(FComplexCircuitArrayC12->GetItem(ic, a1i)) / ValueStrToDouble(AOIdoz->GetItem(i).ValueRout));
     FComplexCircuitArrayC12->SetItemSumReIm(ic,ir,ValueStrToDouble(FComplexCircuitArrayC12->GetItem(ic, ir)) + 1 / ( 2 * 3,1415 * ValueStrToDouble(AOIdoz->GetItem(i).ValueFt)));

     // plneni C12s
     FComplexCircuitArrayC12s->SetItemSumReIm(a2u,a1i,ValueStrToDouble(FComplexCircuitArrayC12s->GetItem(a2u, a2i)) / ValueStrToDouble(AOIdoz->GetItem(i).ValueRout));
     FComplexCircuitArrayC12s->SetItemSumReIm(a2u2,a1i,ValueStrToDouble(FComplexCircuitArrayC12s->GetItem(a2u2, a1i)) / ValueStrToDouble(AOIdoz->GetItem(i).ValueRout));
     FComplexCircuitArrayC12s->SetItemSumReIm(b2u,a1i,ValueStrToDouble(FComplexCircuitArrayC12s->GetItem(b2u, a1i)) / ValueStrToDouble(AOIdoz->GetItem(i).ValueRout));
     FComplexCircuitArrayC12s->SetItemSumReIm(ic,a1i,ValueStrToDouble(FComplexCircuitArrayC12s->GetItem(ic, a1i)) / ValueStrToDouble(AOIdoz->GetItem(i).ValueRout));

     // plneni C21
     FComplexCircuitArrayC21->SetItemSumReIm(a1u,a2i,ValueStrToDouble(FComplexCircuitArrayC21->GetItem(a1u, a2i)) / ValueStrToDouble(AOIdoz->GetItem(i).ValueRout));
     FComplexCircuitArrayC21->SetItemSumReIm(a1u2,a2i,ValueStrToDouble(FComplexCircuitArrayC21->GetItem(a1u2, a2i)) / ValueStrToDouble(AOIdoz->GetItem(i).ValueRout));
     FComplexCircuitArrayC21->SetItemSumReIm(b1u,a2i,ValueStrToDouble(FComplexCircuitArrayC21->GetItem(b1u, a2i)) / ValueStrToDouble(AOIdoz->GetItem(i).ValueRout));
     FComplexCircuitArrayC21->SetItemSumReIm(ic,a2i,ValueStrToDouble(FComplexCircuitArrayC21->GetItem(ic, a2i)) / ValueStrToDouble(AOIdoz->GetItem(i).ValueRout));
     FComplexCircuitArrayC21->SetItemSumReIm(ic,ir,ValueStrToDouble(FComplexCircuitArrayC21->GetItem(ic, ir)) + 1 / ( 2 * 3,1415 * ValueStrToDouble(AOIdoz->GetItem(i).ValueFt)));

     // plneni C21s
     FComplexCircuitArrayC21s->SetItemSumReIm(a1u,a2i,ValueStrToDouble(FComplexCircuitArrayC21s->GetItem(a1u, a2i)) / ValueStrToDouble(AOIdoz->GetItem(i).ValueRout));
     FComplexCircuitArrayC21s->SetItemSumReIm(a1u2,a2i,ValueStrToDouble(FComplexCircuitArrayC21s->GetItem(a1u2, a2i)) / ValueStrToDouble(AOIdoz->GetItem(i).ValueRout));
     FComplexCircuitArrayC21s->SetItemSumReIm(b1u,a2i,ValueStrToDouble(FComplexCircuitArrayC21s->GetItem(b1u, a2i)) / ValueStrToDouble(AOIdoz->GetItem(i).ValueRout));
     FComplexCircuitArrayC21s->SetItemSumReIm(ic,a2i,ValueStrToDouble(FComplexCircuitArrayC21s->GetItem(b1u, a2i)) / ValueStrToDouble(AOIdoz->GetItem(i).ValueRout));

     // plneni Y2
     FComplexCircuitArrayY2->SetItemSumReIm(a2u,a2i,ValueStrToDouble(FComplexCircuitArrayY2->GetItem(a2u, a2i)) / ValueStrToDouble(AOIdoz->GetItem(i).ValueRout));
     FComplexCircuitArrayY2->SetItemSumReIm(a2u2,a2i,ValueStrToDouble(FComplexCircuitArrayY2->GetItem(a2u2, a2i)) / ValueStrToDouble(AOIdoz->GetItem(i).ValueRout));
     FComplexCircuitArrayY2->SetItemSumReIm(b2u,a2i,1 + ValueStrToDouble(FComplexCircuitArrayY2->GetItem(b2u, a2i)) / ValueStrToDouble(AOIdoz->GetItem(i).ValueRout));
     FComplexCircuitArrayY2->SetItemSumReIm(ic,a2i,ValueStrToDouble(FComplexCircuitArrayY2->GetItem(ic, a2i)) - 1);
     FComplexCircuitArrayY2->SetItemSumReIm(a2u,ir,ValueStrToDouble(FComplexCircuitArrayY2->GetItem(a2u, ir)) - 1);
     FComplexCircuitArrayY2->SetItemSumReIm(a2u2,ir,ValueStrToDouble(FComplexCircuitArrayY2->GetItem(a2u2, ir)) + 1);
     FComplexCircuitArrayY2->SetItemSumReIm(ic,ir,ValueStrToDouble(FComplexCircuitArrayY2->GetItem(ic,ir)) +  1 / ValueStrToDouble(AOIdoz->GetItem(i).ValueA));

     // plneni Y2s
     FComplexCircuitArrayY2s->SetItemSumReIm(a2u,a2i,ValueStrToDouble(FComplexCircuitArrayY2s->GetItem(a2u, a2i)) / ValueStrToDouble(AOIdoz->GetItem(i).ValueRout));
     FComplexCircuitArrayY2s->SetItemSumReIm(a2u2,a2i,ValueStrToDouble(FComplexCircuitArrayY2s->GetItem(a2u2, a2i)) / ValueStrToDouble(AOIdoz->GetItem(i).ValueRout));
     FComplexCircuitArrayY2s->SetItemSumReIm(b2u,a2i,ValueStrToDouble(FComplexCircuitArrayY2s->GetItem(b2u, a2i)) / ValueStrToDouble(AOIdoz->GetItem(i).ValueRout));
     FComplexCircuitArrayY2s->SetItemSumReIm(ic,ir,ValueStrToDouble(FComplexCircuitArrayY2s->GetItem(ic, ir)) + 1 / ( 2 * 3,1415 * ValueStrToDouble(AOIdoz->GetItem(i).ValueFt)));

     ic++;
     ir++;
  }
*/


/*
  WriteSubComplexArray(ListView9,FComplexCircuitArrayY1);
  WriteSubComplexArray(ListView10,FComplexCircuitArrayC12);
  WriteSubComplexArray(ListView11,FComplexCircuitArrayC21);
  WriteSubComplexArray(ListView12,FComplexCircuitArrayY2);
*/

// Tvorba M souboru
/*
   Memo3->Lines->Add(WriteSubComplexArrayToM(FComplexCircuitArrayY1,"Y1"));
   Memo3->Lines->Add(WriteSubComplexArrayToM(FComplexCircuitArrayC12,"C12"));
   Memo3->Lines->Add(WriteSubComplexArrayToM(FComplexCircuitArrayC21,"C21"));
   Memo3->Lines->Add(WriteSubComplexArrayToM(FComplexCircuitArrayY2,"Y2"));
*/

/* řešení přes m-file
  Memo3->Lines->Add("% JADRO_SPINU;");
  Memo3->Lines->Add("%    i,j,r:byte;");
  Memo3->Lines->Add("%    kj1,kj2:shortint;");
  Memo3->Lines->Add("%    G1,G2:mat;");
  Memo3->Lines->Add("%    F1,F2,en:matd;");
  Memo3->Lines->Add("%    G1vek,G2vek,G1int,G2int,nulyR,nulyI,pom1,pom2:vek;");
  Memo3->Lines->Add("%    ds,dl,koef,checksum1,checksum2:extended;");

  Memo3->Lines->Add("% Definice b1, b2, T1, T2, vstup a vystupuzel, sloupcovyvektor (G1, G2)!");

  Memo3->Lines->Add("[G1]=INVMAT(Y1, T1, 10);");
  Memo3->Lines->Add("[G2]=INVMAT(Y2, T2, 10);");

  Memo3->Lines->Add("% G1vek=imp.char,faze1       G1int=prech.char,faze1");
  Memo3->Lines->Add("if (b1>0)");
  Memo3->Lines->Add("   G1vek = G1(:,b1);");
  Memo3->Lines->Add("   [G1int] = INVLAPINT(Y1, vstup, b1, T1, 10);");
  Memo3->Lines->Add("end");

  Memo3->Lines->Add("% G2vek=imp.char,faze2       G2int=prech.char,faze2");
  Memo3->Lines->Add("if (b2>0)");
  Memo3->Lines->Add("   G2vek = G2(:,b2);");
  Memo3->Lines->Add("   [G2int] = INVLAPINT(Y2, vstup, b2, T2, 10);");
  Memo3->Lines->Add("end");

  Memo3->Lines->Add("G1 = G1 * C12;       % G1...g1shvezdickou(T1)");
  Memo3->Lines->Add("G2 = G2 * C21;       % G2...g2shvezdickou(T2)");
  Memo3->Lines->Add("F1 = G1 * (-1 * G2); % F1=-g1shvezdickou*g2shvezdickou");
  Memo3->Lines->Add("F2 = G2 * (-1 * G1); % F2=-g2shvezdickou*g1shvezdickou");

  Memo3->Lines->Add("% pom2=g2shvezdickou*G1int");
  Memo3->Lines->Add("if (b1>0)");
  Memo3->Lines->Add("   pom2 = G2 * G1int;");
  Memo3->Lines->Add("end");

  Memo3->Lines->Add("% pom1=g1shvezdickou*G2int");
  Memo3->Lines->Add("if (b2>0)");
  Memo3->Lines->Add("   pom1 = G1 * G2int;");
  Memo3->Lines->Add("end");

  Memo3->Lines->Add("% Vypocet nulovych bodu a polu");

  Memo3->Lines->Add("[m,n]=size(F1);");
  Memo3->Lines->Add("A=eye(n);");
  Memo3->Lines->Add("ak(n)=0;");
  Memo3->Lines->Add("Ak(n)=0;");
  Memo3->Lines->Add("ak(1)=1;");
  Memo3->Lines->Add("Ak(1)=A(vystupuzel);");


  Memo3->Lines->Add("for i=2:n");
  Memo3->Lines->Add("    a=-1/i*trace(F*A);");
  Memo3->Lines->Add("    A=F*A+a*eye(n);");
  Memo3->Lines->Add("    ak(i)=a;");
  Memo3->Lines->Add("    X=A*SloupcovyVektor;");
  Memo3->Lines->Add("    Ak(i,:)=X(vystupuzel,:);");
  Memo3->Lines->Add("end");
// Obvodové matice numericky pro metodu výpočtu přes submatice ***************KONEC******************
*/

// Obvodové matice numericky pro metodu výpočtu přes super matici     *******************ZAČÁTEK*******************
// vytvoří 6 matic Y1, C12, C21, Y2, Y1f, C12f, C21f, Y2f    ... f = matice, která se přičte k výsledné, nebude se nijak násobit
{
// user friendly
   Button2->Visible = false;
   Button8->Visible = true;
   Button8->Visible = true;
   Panelin->Visible = true;
   Panelout->Visible = true;
   Label39->Visible = true;
   Label40->Visible = true;
   Label43->Visible = true;
   EditFS->Visible = true;
   EditD->Visible = true;
   CBHodDek->Visible = true;
   CBXmin->Visible = true;
   CBXmax->Visible = true;


  Memo3->Clear();
  ListView9->Clear();
  ListView10->Clear();
  ListView11->Clear();
  ListView12->Clear();
  ListView13->Clear();
  ListView14->Clear();
  ListView15->Clear();
  ListView16->Clear();

  int ModAUCount=0; int ModAICount=0; int ModBUCount=0; int ModBICount=0;
  int CRAdd=0; //přídavný řádek a sloupec

  int icAU=0; int irAI=0; int icBU=0; int irBI=0; // číslo přídavného sloupce a řádku

  int a1u=0; int a2u=0; int b1u=0; int b2u=0;
  int a1u2=0; int a2u2=0; int b1u2=0; int b2u2=0;
  int a1i=0; int a2i=0; int b1i=0; int b2i=0;
  int a1i2=0; int a2i2=0; int b1i2=0; int b2i2=0;
  int InNode=0; int OutNode=0;

  //vytvoření obovodových matic Y1, C12, C21, Y2
  MaxNodValueAllMod(Nodes, &ModAUCount, &ModAICount, &ModBUCount, &ModBICount);

  CRAdd = CompL->Count()+ + 2 * CompM->Count() + AOIdoz->Count();
  //  ShowMessage(IntToStr(ColIndex));   ShowMessage(IntToStr(ColIndexF));
  icAU = ModAUCount;
  irAI = ModAICount;
  icBU = ModBUCount;
  irBI = ModBICount;

  FComplexCircuitArrayY1 = new TComplexArray( ModAUCount + CRAdd, ModAICount + CRAdd );
  FComplexCircuitArrayC12 = new TComplexArray( ModBUCount + CRAdd, ModAICount + CRAdd );
  FComplexCircuitArrayC21 = new TComplexArray( ModAUCount + CRAdd, ModBICount + CRAdd );
  FComplexCircuitArrayY2 = new TComplexArray( ModBUCount + CRAdd, ModBICount + CRAdd );
  FComplexCircuitArrayY1f = new TComplexArray( ModAUCount + CRAdd, ModAICount + CRAdd );
  FComplexCircuitArrayC12f = new TComplexArray( ModBUCount + CRAdd, ModAICount + CRAdd );
  FComplexCircuitArrayC21f = new TComplexArray( ModAUCount + CRAdd, ModBICount + CRAdd );
  FComplexCircuitArrayY2f = new TComplexArray( ModBUCount + CRAdd, ModBICount + CRAdd );
  if (ModAUCount != ModAICount) ShowMessage("Chyba v obvodu. Y1 není čtvercová matice");
  if (ModBUCount != ModBICount) ShowMessage("Chyba v obvodu. Y2 není čtvercová matice");

  // plnění matic součastkou R  ************************************************
  for ( int i=0; i< CompR->Count(); i++)
  {
     // vstupni uzel součástky
     InNode = Nodes->IndexOfID(CompR->GetItem(i).Input);
     a1u = Nodes->GetItem(InNode).ModAU;
     a1i = Nodes->GetItem(InNode).ModAI;
     a2u = Nodes->GetItem(InNode).ModBU;
     a2i = Nodes->GetItem(InNode).ModBI;
     // výstupní uzel součástky
     OutNode = Nodes->IndexOfID(CompR->GetItem(i).Output);
     b1u = Nodes->GetItem(OutNode).ModAU;
     b1i = Nodes->GetItem(OutNode).ModAI;
     b2u = Nodes->GetItem(OutNode).ModBU;
     b2i = Nodes->GetItem(OutNode).ModBI;

     // plneni Y1
     FComplexCircuitArrayY1->SetItemSumReIm(a1u,a1i,1.0/(ValueStrToDouble(CompR->GetItem(i).Value)),0);
     FComplexCircuitArrayY1->SetItemSumReIm(a1u,b1i,-1.0/ValueStrToDouble(CompR->GetItem(i).Value),0);
     FComplexCircuitArrayY1->SetItemSumReIm(b1u,a1i,-1.0/ValueStrToDouble(CompR->GetItem(i).Value),0);
     FComplexCircuitArrayY1->SetItemSumReIm(b1u,b1i,1.0/ValueStrToDouble(CompR->GetItem(i).Value),0);

     // plneni Y2
     FComplexCircuitArrayY2->SetItemSumReIm(a2u,a2i,1.0/ValueStrToDouble(CompR->GetItem(i).Value),0);
     FComplexCircuitArrayY2->SetItemSumReIm(a2u,b2i,-1.0/ValueStrToDouble(CompR->GetItem(i).Value),0);
     FComplexCircuitArrayY2->SetItemSumReIm(b2u,a2i,-1.0/ValueStrToDouble(CompR->GetItem(i).Value),0);
     FComplexCircuitArrayY2->SetItemSumReIm(b2u,b2i,1.0/ValueStrToDouble(CompR->GetItem(i).Value),0);
  }

  // plnění matic součastkou RS1  ************************************************
  for ( int i=0; i< CompRS1->Count(); i++)
  {
     // vstupni uzel součástky
     InNode = Nodes->IndexOfID(CompRS1->GetItem(i).Input);
     a1u = Nodes->GetItem(InNode).ModAU;
     a1i = Nodes->GetItem(InNode).ModAI;
     a2u = Nodes->GetItem(InNode).ModBU;
     a2i = Nodes->GetItem(InNode).ModBI;
     // výstupní uzel součástky
     OutNode = Nodes->IndexOfID(CompRS1->GetItem(i).Output);
     b1u = Nodes->GetItem(OutNode).ModAU;
     b1i = Nodes->GetItem(OutNode).ModAI;
     b2u = Nodes->GetItem(OutNode).ModBU;
     b2i = Nodes->GetItem(OutNode).ModBI;

     // plneni Y1
     FComplexCircuitArrayY1->SetItemSumReIm(a1u,a1i,1.0/ValueStrToDouble(CompRS1->GetItem(i).Value),0);
     FComplexCircuitArrayY1->SetItemSumReIm(a1u,b1i,-1.0/ValueStrToDouble(CompRS1->GetItem(i).Value),0);
     FComplexCircuitArrayY1->SetItemSumReIm(b1u,a1i,-1.0/ValueStrToDouble(CompRS1->GetItem(i).Value),0);
     FComplexCircuitArrayY1->SetItemSumReIm(b1u,b1i,1.0/ValueStrToDouble(CompRS1->GetItem(i).Value),0);
  }

  // plnění matic součastkou RS2  ************************************************
  for ( int i=0; i< CompRS2->Count(); i++)
  {
     // vstupni uzel součástky
     InNode = Nodes->IndexOfID(CompRS2->GetItem(i).Input);
     a1u = Nodes->GetItem(InNode).ModAU;
     a1i = Nodes->GetItem(InNode).ModAI;
     a2u = Nodes->GetItem(InNode).ModBU;
     a2i = Nodes->GetItem(InNode).ModBI;
     // výstupní uzel součástky
     OutNode = Nodes->IndexOfID(CompRS2->GetItem(i).Output);
     b1u = Nodes->GetItem(OutNode).ModAU;
     b1i = Nodes->GetItem(OutNode).ModAI;
     b2u = Nodes->GetItem(OutNode).ModBU;
     b2i = Nodes->GetItem(OutNode).ModBI;

     // plneni Y2
     FComplexCircuitArrayY2->SetItemSumReIm(a2u,a2i,1.0/ValueStrToDouble(CompRS2->GetItem(i).Value),0);
     FComplexCircuitArrayY2->SetItemSumReIm(a2u,b2i,-1.0/ValueStrToDouble(CompRS2->GetItem(i).Value),0);
     FComplexCircuitArrayY2->SetItemSumReIm(b2u,a2i,-1.0/ValueStrToDouble(CompRS2->GetItem(i).Value),0);
     FComplexCircuitArrayY2->SetItemSumReIm(b2u,b2i,1.0/ValueStrToDouble(CompRS2->GetItem(i).Value),0);
  }

  // plnění matic součastkou C  ************************************************
  for ( int i=0; i< CompC->Count(); i++)
  {
     // vstupni uzel součástky
     InNode = Nodes->IndexOfID(CompC->GetItem(i).Input);
     a1u = Nodes->GetItem(InNode).ModAU;
     a1i = Nodes->GetItem(InNode).ModAI;
     a2u = Nodes->GetItem(InNode).ModBU;
     a2i = Nodes->GetItem(InNode).ModBI;
     // výstupní uzel součástky
     OutNode = Nodes->IndexOfID(CompC->GetItem(i).Output);
     b1u = Nodes->GetItem(OutNode).ModAU;
     b1i = Nodes->GetItem(OutNode).ModAI;
     b2u = Nodes->GetItem(OutNode).ModBU;
     b2i = Nodes->GetItem(OutNode).ModBI;

     // plneni Y1
     FComplexCircuitArrayY1f->SetItemSumReIm(a1u,a1i,ValueStrToDouble(CompC->GetItem(i).Value),0);
     FComplexCircuitArrayY1f->SetItemSumReIm(a1u,b1i,-1.0*ValueStrToDouble(CompC->GetItem(i).Value),0);
     FComplexCircuitArrayY1f->SetItemSumReIm(b1u,a1i,-1.0*ValueStrToDouble(CompC->GetItem(i).Value),0);
     FComplexCircuitArrayY1f->SetItemSumReIm(b1u,b1i,ValueStrToDouble(CompC->GetItem(i).Value),0);

     // plneni C12
     FComplexCircuitArrayC12->SetItemSumReIm(a2u,a1i,-1.0*ValueStrToDouble(CompC->GetItem(i).Value),0);
     FComplexCircuitArrayC12->SetItemSumReIm(a2u,b1i,ValueStrToDouble(CompC->GetItem(i).Value),0);
     FComplexCircuitArrayC12->SetItemSumReIm(b2u,a1i,ValueStrToDouble(CompC->GetItem(i).Value),0);
     FComplexCircuitArrayC12->SetItemSumReIm(b2u,b1i,-1.0*ValueStrToDouble(CompC->GetItem(i).Value),0);

     // plneni C21
     FComplexCircuitArrayC21->SetItemSumReIm(a1u,a2i,-1.0*ValueStrToDouble(CompC->GetItem(i).Value),0);
     FComplexCircuitArrayC21->SetItemSumReIm(a1u,b2i,ValueStrToDouble(CompC->GetItem(i).Value),0);
     FComplexCircuitArrayC21->SetItemSumReIm(b1u,a2i,ValueStrToDouble(CompC->GetItem(i).Value),0);
     FComplexCircuitArrayC21->SetItemSumReIm(b1u,b2i,-1.0*ValueStrToDouble(CompC->GetItem(i).Value),0);

     // plneni Y2
     FComplexCircuitArrayY2f->SetItemSumReIm(a2u,a2i,ValueStrToDouble(CompC->GetItem(i).Value),0);
     FComplexCircuitArrayY2f->SetItemSumReIm(a2u,b2i,-1.0*ValueStrToDouble(CompC->GetItem(i).Value),0);
     FComplexCircuitArrayY2f->SetItemSumReIm(b2u,a2i,-1.0*ValueStrToDouble(CompC->GetItem(i).Value),0);
     FComplexCircuitArrayY2f->SetItemSumReIm(b2u,b2i,ValueStrToDouble(CompC->GetItem(i).Value),0);
  }

  // plnění matic součastkou L  ************************************************
  for ( int i=0; i< CompL->Count(); i++)
  {
     // vstupni uzel součástky
     InNode = Nodes->IndexOfID(CompL->GetItem(i).Input);
     a1u = Nodes->GetItem(InNode).ModAU;
     a1i = Nodes->GetItem(InNode).ModAI;
     a2u = Nodes->GetItem(InNode).ModBU;
     a2i = Nodes->GetItem(InNode).ModBI;
     // výstupní uzel součástky
     OutNode = Nodes->IndexOfID(CompL->GetItem(i).Output);
     b1u = Nodes->GetItem(OutNode).ModAU;
     b1i = Nodes->GetItem(OutNode).ModAI;
     b2u = Nodes->GetItem(OutNode).ModBU;
     b2i = Nodes->GetItem(OutNode).ModBI;


     // plneni Y1
     FComplexCircuitArrayY1->SetItemSumReIm(a1u,irAI,-1.0,0);
     FComplexCircuitArrayY1->SetItemSumReIm(b1u,irAI,1.0,0);
     FComplexCircuitArrayY1->SetItemSumReIm(icAU,b1i,-1.0,0);
     FComplexCircuitArrayY1->SetItemSumReIm(icAU,a1i,1.0,0);
     FComplexCircuitArrayY1->SetItemSumReIm(icAU,irAI,ValueStrToDouble(CompL->GetItem(i).ValueRs),0);
     FComplexCircuitArrayY1f->SetItemSumReIm(icAU,irAI,ValueStrToDouble(CompL->GetItem(i).ValueLs),0);

     // plneni C12
     FComplexCircuitArrayC12->SetItemSumReIm(icBU,irAI,-1.0*ValueStrToDouble(CompL->GetItem(i).ValueLs),0);

     // plneni C21
     FComplexCircuitArrayC21->SetItemSumReIm(icAU,irBI,-1.0*ValueStrToDouble(CompL->GetItem(i).ValueLs),0);

     // plneni Y2
     FComplexCircuitArrayY2->SetItemSumReIm(a2u,irBI,-1.0,0);
     FComplexCircuitArrayY2->SetItemSumReIm(b2u,irBI,1.0,0);
     FComplexCircuitArrayY2->SetItemSumReIm(icBU,b2i,-1.0,0);
     FComplexCircuitArrayY2->SetItemSumReIm(icBU,a2i,1.0,0);
     FComplexCircuitArrayY2->SetItemSumReIm(icBU,irBI,ValueStrToDouble(CompL->GetItem(i).ValueRs),0);
     FComplexCircuitArrayY2f->SetItemSumReIm(icBU,irBI,ValueStrToDouble(CompL->GetItem(i).ValueLs),0);
     icAU++;
     irAI++;
     icBU++;
     irBI++;
  }

  // plnění matic součastkou OA1p  *********************************************
  for ( int i=0; i< AOIdoz->Count(); i++)
  {
     // vstupni uzel A součástky
     InNode = Nodes->IndexOfID(AOIdoz->GetItem(i).InputA);
     a1u = Nodes->GetItem(InNode).ModAU;
     a1i = Nodes->GetItem(InNode).ModAI;
     a2u = Nodes->GetItem(InNode).ModBU;
     a2i = Nodes->GetItem(InNode).ModBI;
     // vstupni uzel B součástky
     InNode = Nodes->IndexOfID(AOIdoz->GetItem(i).InputB);
     a1u2 = Nodes->GetItem(InNode).ModAU;
     a1i2 = Nodes->GetItem(InNode).ModAI;
     a2u2 = Nodes->GetItem(InNode).ModBU;
     a2i2 = Nodes->GetItem(InNode).ModBI;
     // výstupní uzel součástky
     OutNode = Nodes->IndexOfID(AOIdoz->GetItem(i).Output);
     b1u = Nodes->GetItem(OutNode).ModAU;
     b1i = Nodes->GetItem(OutNode).ModAI;
     b2u = Nodes->GetItem(OutNode).ModBU;
     b2i = Nodes->GetItem(OutNode).ModBI;

     //hodnoty pro zapsání do matic
     double A = ValueStrToDouble(AOIdoz->GetItem(i).ValueA);
     double Ft = ValueStrToDouble(AOIdoz->GetItem(i).ValueFt);
     double Rout = ValueStrToDouble(AOIdoz->GetItem(i).ValueRout);
     const double pi = 3.14159265358979323846264338327950288419716939937510;
     double omegat = 2 * pi * Ft;

     // plneni Y1
     FComplexCircuitArrayY1->SetItemSumReIm(b1u,b1i,Rout,0);
     FComplexCircuitArrayY1->SetItemSumReIm(icAU,b1i,-Rout,0);
     FComplexCircuitArrayY1->SetItemSumReIm(a1u,irAI,-omegat,0);
     FComplexCircuitArrayY1->SetItemSumReIm(a1u2,irAI,omegat,0);
     FComplexCircuitArrayY1->SetItemSumReIm(icAU,irAI,omegat/A,0);
     FComplexCircuitArrayY1f->SetItemSumReIm(icAU,irAI,1.0,0);

     // plneni C12
     FComplexCircuitArrayC12->SetItemSumReIm(icBU,irAI,-1.0,0);

     // plneni C21
     FComplexCircuitArrayC21->SetItemSumReIm(icAU,irBI,-1.0,0);

     // plneni Y2
     FComplexCircuitArrayY2->SetItemSumReIm(b2u,b2i,Rout,0);
     FComplexCircuitArrayY2->SetItemSumReIm(icBU,b2i,-Rout,0);
     FComplexCircuitArrayY2->SetItemSumReIm(a2u,irBI,-omegat,0);
     FComplexCircuitArrayY2->SetItemSumReIm(a2u2,irBI,omegat,0);
     FComplexCircuitArrayY2->SetItemSumReIm(icBU,irBI,omegat/A,0);
     FComplexCircuitArrayY2f->SetItemSumReIm(icBU,irBI,1.0,0);

     icAU++;
     irAI++;
     icBU++;
     irBI++;
  }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button8Click(TObject *Sender)
{
  // vytvoření výsledných matic + vykreslení
  Memo3->Clear();
  boolean vypocet = true;   // overení jestli jsou zadané všechny hodnoty nutne pro vypocet
  int buzeni = 0;
  double fs= 0.0;
  double T1 = 0.0;
  double T2 = 0.0;
  double D = 0.0;
  double Xmin = 0.0;
  double Xmax = 0.0;
  int pocetdekad = 0;
  int hodnotnadekadu = 0;
  int sloupec = 0;
  int radek = 0;
  int sloupec2 = 0;
  int radek2 = 0;
  
  // test, jestli jsou vyplněné hodnoty v Editech
  if (EditIN->Text.Length()<=0){ vypocet = false; ShowMessage("Nesprávně zadaný vstupní uzel."); }
  if (EditOUT->Text.Length()<=0){ vypocet = false; ShowMessage("Nesprávně zadaný výstupní uzel."); }
  if (EditFS->Text.Length()>0) fs = ValueStrToDouble(ValidatingValue(EditFS->Text));
   else  {
    vypocet = false;
    ShowMessage("Nesprávně zadaná frekvence.");
   }
  if (EditD->Text.Length()>0) D = ValueStrToDouble(ValueWithDot(EditD->Text));
   else  {
    vypocet = false;
    ShowMessage("Nesprávně zadaná střída.");
   }
   if (CBXmin->Text.Length()>0) Xmin =  ValueStrToDouble(ValidatingValue(CBXmin->Text));
   else  {
    vypocet = false;
    ShowMessage("Nesprávně zadaná minimální frekvence osy X.");
   }
   if (CBXmax->Text.Length()>0) Xmax =  ValueStrToDouble(ValidatingValue(CBXmax->Text));
   else  {
    vypocet = false;
    ShowMessage("Nesprávně zadaná maximální frekvence osy X.");
   }
   if (Xmax <= Xmin)
    {
     vypocet = false;
     ShowMessage("Špatný rozsah frekvencí pro výpočet.");
    }
   if (vypocet) pocetdekad = log10(Xmax/Xmin);
   if (CBHodDek->Text.Length()>0) hodnotnadekadu =  StrToInt(CBHodDek->Text);
   else  {
    vypocet = false;
    ShowMessage("Nesprávně zadaný počet hodnot na dekádu.");
   }



  if (fs>0)
  {
    double T = (double) 1.0/ fs;
               
  
    if (D>0 && D<1)
    {
      T1 = T * D;
      T2 = T - T1;
    } else { vypocet = false; ShowMessage("Nesprávně zadaná střída."); }
  } else { vypocet = false; ShowMessage("Nesprávně zadaná frekvence."); }

  // transforamce uzlu vstupu a výstupu
  TEleNodeItem NodeInput;  // vstupní uzel obvodu
  TEleNodeItem NodeOutput; // výstupníuzel obvodu
  int NodeInputIndex = Nodes->IndexOfName(EditIN->Text);
  if(NodeInputIndex >= 0) {
    NodeInput = Nodes->GetItem(NodeInputIndex);
    //ShowMessage("Uzel vstup: "+ NodeInput.Name + " ("+NodeInput.ID+") ["+NodeInput.ModAU+","+NodeInput.ModAI+","+NodeInput.ModBU+","+NodeInput.ModBI+"]");
  } else {
    vypocet = false;
    ShowMessage("Nesmyslné zadání vstupního uzlu.");
  }
  int NodeOutputIndex = Nodes->IndexOfName(EditOUT->Text);
  if(NodeOutputIndex >= 0) {
    NodeOutput = Nodes->GetItem(NodeOutputIndex);
    //ShowMessage("Uzel vystup: "+NodeOutput.Name + " ("+NodeOutput.ID+") ["+NodeOutput.ModAU+","+NodeOutput.ModAI+","+NodeOutput.ModBU+","+NodeOutput.ModBI+"]");
  } else {
    vypocet = false;
    ShowMessage("Nesmyslné zadání výstupního uzlu.");
  }

 // buzeni = zjištění jak byl vybrán vstup a výstup
   if (ChBinfaze1->Checked && (ChBinfaze2->Checked == false))  // zaškrtnutá první fáze
   {
      if ( ChBoutfaze1->Checked ) buzeni = 11;
      if ( ChBoutfaze2->Checked ) buzeni = 12;
   }
   if ((ChBinfaze1->Checked == false) && ChBinfaze2->Checked) // zaškrtnutá druhá fáze
   {
      if ( ChBoutfaze1->Checked ) buzeni = 21;
      if ( ChBoutfaze2->Checked ) buzeni = 22;
   }
   if (ChBinfaze1->Checked && ChBinfaze2->Checked) // zaškrtnuté obě fáze
   {
      if ( ChBoutfaze1->Checked ) buzeni = 31;
      if ( ChBoutfaze2->Checked) buzeni = 32;
   }
 // ShowMessage(buzeni);
   if ( buzeni == 0 )
   {
     vypocet = false;
     ShowMessage("Nebyla vybrána fáze vstupního uzlu.");
   }

  // ošetření uzemněného vstupu nebo výstupu podle vybrané kombinace vstupu a výstupu
 if ( buzeni == 11 )
  if ( NodeInput.ModAU > 0 && NodeInput.ModAI > 0 && NodeOutput.ModAU > 0 )
  {}else{
    vypocet = false;
    ShowMessage("Nesprávné zadání uzlů, v dané fázi je některý uzemněn.");
  }
 if ( buzeni == 12 )
  if ( NodeInput.ModAU > 0 && NodeInput.ModAI > 0 && NodeOutput.ModBU > 0 )
  {}else{
    vypocet = false;
    ShowMessage("Nesprávné zadání uzlů, v dané fázi je některý uzemněn.");
  }
 if ( buzeni == 21 )
  if ( NodeInput.ModBU > 0 && NodeInput.ModBI > 0 && NodeOutput.ModAU > 0 )
  {}else{
    vypocet = false;
    ShowMessage("Nesprávné zadání uzlů, v dané fázi je některý uzemněn.");
  }
 if ( buzeni == 22 )
  if ( NodeInput.ModBU > 0 && NodeInput.ModBI > 0 && NodeOutput.ModBU > 0 )
  {}else{
    vypocet = false;
    ShowMessage("Nesprávné zadání uzlů, v dané fázi je některý uzemněn.");
  }
 if ( buzeni == 31 )
  if ( NodeInput.ModAU > 0 && NodeInput.ModAI > 0 && NodeOutput.ModAU > 0 && NodeInput.ModBU > 0 && NodeInput.ModBI > 0 && NodeOutput.ModAU > 0 )
  {}else{
    vypocet = false;
    ShowMessage("Nesprávné zadání uzlů, v dané fázi je některý uzemněn.");
  }
 if ( buzeni == 32 )
  if ( NodeInput.ModAU > 0 && NodeInput.ModAI > 0 && NodeOutput.ModBU > 0 && NodeInput.ModBU > 0 && NodeInput.ModBI > 0 && NodeOutput.ModBU > 0 )
  {}else{
    vypocet = false;
    ShowMessage("Nesprávné zadání uzlů, v dané fázi je některý uzemněn.");
  }


  const double pi = 3.14159265358979323846264338327950288419716939937510;
  const double e = 2.71828182845904523536028747135266249775724709369995;
  double fx = 1.0; // první počítaný kmitočet

  matfreq = new TComplexArray(pocetdekad*hodnotnadekadu+1,1);
  matexp = new TComplexArray(pocetdekad*hodnotnadekadu+1,1);
  matdb = new TComplexArray(pocetdekad*hodnotnadekadu+1,1);
  matfaze = new TComplexArray(pocetdekad*hodnotnadekadu+1,1);


  if (vypocet) {
  for (int n=0;n<=(pocetdekad*hodnotnadekadu);n++)
  {
  // int n=0;
    double mocnina = ((double)log10(Xmin)+(double) n / hodnotnadekadu);
    fx = pow(10,mocnina);
    matexp->SetItemReIm(n,0,mocnina,0); // pro vykreslení grafu Chart
    matfreq->SetItemReIm(n,0,fx,0); // hodnoty osy x logaritmické měřítko
    complex<double> jom(0,2*pi*fx);
    complex<double> exp1(0,0);
     exp1 = exp(-jom*T1);
    complex<double> exp2(0,0);
     exp2 = exp(-jom*T1);
    complex<double> f1(0,0);
     f1 = ((complex<double>) 1.0-exp1) /jom;
    complex<double> f2(0,0);
     f2 = ((complex<double>) 1.0-exp2) /jom;



  // ShowMessage(f1.real());   ShowMessage(f1.imag());

    TComplexArray* FComplexCircuitArrayY1big = new TComplexArray(FComplexCircuitArrayY1->GetColCount(),FComplexCircuitArrayY1->GetRowCount());
    TComplexArray* FComplexCircuitArrayC12big = new TComplexArray(FComplexCircuitArrayC12->GetColCount(),FComplexCircuitArrayC12->GetRowCount());
    TComplexArray* FComplexCircuitArrayC21big = new TComplexArray(FComplexCircuitArrayC21->GetColCount(),FComplexCircuitArrayC21->GetRowCount());
    TComplexArray* FComplexCircuitArrayY2big = new TComplexArray(FComplexCircuitArrayY2->GetColCount(),FComplexCircuitArrayY2->GetRowCount());

    //naplnění pomocných matic
    FComplexCircuitArrayY1big->PlusMat(FComplexCircuitArrayY1);
    FComplexCircuitArrayC12big->PlusMat(FComplexCircuitArrayC12);
    FComplexCircuitArrayC21big->PlusMat(FComplexCircuitArrayC21);
    FComplexCircuitArrayY2big->PlusMat(FComplexCircuitArrayY2);

   if (n==0)
   {
    WriteSubComplexArray(ListView9,FComplexCircuitArrayY1);
    WriteSubComplexArray(ListView10,FComplexCircuitArrayC12);
    WriteSubComplexArray(ListView11,FComplexCircuitArrayC21);
    WriteSubComplexArray(ListView12,FComplexCircuitArrayY2);

    WriteSubComplexArray(ListView13,FComplexCircuitArrayY1f);
    WriteSubComplexArray(ListView14,FComplexCircuitArrayC12f);
    WriteSubComplexArray(ListView15,FComplexCircuitArrayC21f);
    WriteSubComplexArray(ListView16,FComplexCircuitArrayY2f);
   }

    fcomplex MulY1 = Complex(f1.real(),f1.imag());
    FComplexCircuitArrayY1big->MulNum(MulY1);
    fcomplex MulC12 = Complex(exp1.real(),exp1.imag());
    FComplexCircuitArrayC12big->MulNum(MulC12);
    fcomplex MulC21 = Complex(exp2.real(),exp2.imag());
    FComplexCircuitArrayC21big->MulNum(MulC21);
    fcomplex MulY2 = Complex(f2.real(),f2.imag());
    FComplexCircuitArrayY2big->MulNum(MulY2);

    /*  //kontrola nasobitelu
    exp1 = 1;
    exp2 = 1;
    f1 = 1;
    f2 = 1;
    f->SetItem(n,2,MulC12);
    f->SetItem(n,3,MulC21);
    f->SetItem(n,4,MulY1);
    f->SetItem(n,5,MulY2);
    */

    FComplexCircuitArrayY1big->PlusMat(FComplexCircuitArrayY1f);
    FComplexCircuitArrayC12big->PlusMat(FComplexCircuitArrayC12f);
    FComplexCircuitArrayC21big->PlusMat(FComplexCircuitArrayC21f);
    FComplexCircuitArrayY2big->PlusMat(FComplexCircuitArrayY2f);


    // Ořezání matic o sloupec a řádek s nulovým indexem
    // nejprve si zkopíruji ukazatele na staré matice,
    // budu totiž přepisovat stávajicí ukazatele
    TComplexArray* FComplexCircuitArrayY1short = FComplexCircuitArrayY1big->SmallMatrix();
    TComplexArray* FComplexCircuitArrayC12short = FComplexCircuitArrayC12big->SmallMatrix();
    TComplexArray* FComplexCircuitArrayC21short = FComplexCircuitArrayC21big->SmallMatrix();
    TComplexArray* FComplexCircuitArrayY2short = FComplexCircuitArrayY2big->SmallMatrix();

    // zrušení starých matic, které jsou bez ořezu - ono se to totiž kopírovalo
    delete FComplexCircuitArrayY1big;
    delete FComplexCircuitArrayC12big;
    delete FComplexCircuitArrayC21big;
    delete FComplexCircuitArrayY2big;

    TComplexArray* smatrix = new TComplexArray(FComplexCircuitArrayY1short, FComplexCircuitArrayC12short, FComplexCircuitArrayC21short, FComplexCircuitArrayY2short);

    // nulování řádků v super matici
    if ((buzeni == 11) || (buzeni == 12))
    {
      smatrix->SetNullRowAndValueReIm(NodeInput.ModAU-1, NodeInput.ModAI-1,1.0,0.0);
      if ( NodeInput.ModBU > 0 && NodeInput.ModBI > 0 )
        smatrix->SetNullRowAndValueReIm(FComplexCircuitArrayY1short->GetColCount()+NodeInput.ModBU-1,FComplexCircuitArrayY1short->GetRowCount()+NodeInput.ModBI-1,1.0,0.0);
    }
    if ((buzeni == 21) || (buzeni == 22))
    {
      smatrix->SetNullRowAndValueReIm(FComplexCircuitArrayY1short->GetColCount()+NodeInput.ModBU-1,FComplexCircuitArrayY1short->GetRowCount()+NodeInput.ModBI-1,1.0,0.0);
      if ( NodeInput.ModAU > 0 && NodeInput.ModAI > 0 )
        smatrix->SetNullRowAndValueReIm(NodeInput.ModAU-1, NodeInput.ModAI-1,1.0,0.0);
    }

    if ((buzeni == 31) || (buzeni == 32))
    {
      smatrix->SetNullRowAndValueReIm(NodeInput.ModAU-1, NodeInput.ModAI-1,1.0,0.0);
      smatrix->SetNullRowAndValueReIm(FComplexCircuitArrayY1short->GetColCount()+NodeInput.ModBU-1,FComplexCircuitArrayY1short->GetRowCount()+NodeInput.ModBI-1,1.0,0.0);
    }

  if (n<1) WriteSubComplexArray(ListView17,smatrix);
   // Memo3->Lines->Add(WriteSubComplexArrayToM(smatrix,"smatrix"));

    TComplexArray* Invsmatrix = new TComplexArray(smatrix->GetColCount(),smatrix->GetRowCount());
    Invsmatrix->NullAll();
    for(int k = 0; k < Invsmatrix->GetColCount(); k++) {
      for(int i = 0; i < Invsmatrix->GetRowCount(); i++) {
        Invsmatrix->SetItem(k,i,smatrix->GetItem(k,i));
        }
      }
    Invsmatrix = InvMat(Invsmatrix,1.0,1.0);
   if (n<0) WriteSubComplexArray(ListView17,Invsmatrix);
   // Memo3->Lines->Add(WriteSubComplexArrayToM(Invsmatrix,"Invsmatrix"));

    //double absval = Invsmatrix->GetItem(StrToInt(Edit1->Text),StrToInt(Edit2->Text)).r;

   //pozice výstupu
   if ( buzeni == 11 )
    {
      sloupec = NodeInput.ModAI - 1;
      radek = NodeOutput.ModAU - 1;
    }
    if ( buzeni == 12 )
    {
      sloupec = NodeInput.ModAI - 1;
      radek =  FComplexCircuitArrayY1short->GetRowCount() + NodeOutput.ModBU - 1;
    }
    if ( buzeni == 21 )
    {
      sloupec = FComplexCircuitArrayY1short->GetColCount() + NodeInput.ModBI - 1;
      radek = NodeOutput.ModAU - 1;
    }
    if ( buzeni == 22 )
    {
      sloupec = FComplexCircuitArrayY1short->GetColCount() + NodeInput.ModBI - 1;
      radek =  FComplexCircuitArrayY1short->GetRowCount() + NodeOutput.ModBU - 1;
    }
    if ( buzeni == 31 )
    {
      sloupec = NodeInput.ModAI - 1;
      sloupec2 = FComplexCircuitArrayY1short->GetColCount() + NodeInput.ModBI - 1;
      radek = NodeOutput.ModAU - 1;
    }
    if ( buzeni == 32 )
    {
      sloupec = NodeInput.ModAI - 1;
      sloupec2 = FComplexCircuitArrayY1short->GetColCount() + NodeInput.ModBI - 1;
      radek =  FComplexCircuitArrayY1short->GetRowCount() + NodeOutput.ModBU - 1;
    }

  // amplitudová charakteristika
  if ( buzeni == 11 || buzeni == 12 || buzeni == 21 || buzeni == 22 )
  {
    double fdb = 20*log10(Cabs(Invsmatrix->GetItem(sloupec,radek)));
    matdb->SetItemReIm(n,0,fdb,0);
  }
  if ( buzeni == 31 || buzeni == 32 )
  {
    double fdb = 20*log10(Cabs(Invsmatrix->GetItem(sloupec,radek)));
    matdb->SetItemReIm(n,0,fdb,0);

    fdb = 20*log10(Cabs(Invsmatrix->GetItem(sloupec2,radek)));
    matdb->SetItemSumReIm(n,0,fdb,0);
  }

  // fázová charakteristika
  if ( buzeni == 11 || buzeni == 12 || buzeni == 21 || buzeni == 22 )
  {
    complex<double> faze(Invsmatrix->GetItem(sloupec,radek).r,Invsmatrix->GetItem(sloupec,radek).i);
    double ffaze = atan(faze.imag()/faze.real()); // v radiánech fázová charakteristika
    if (faze.real()>=0) matfaze->SetItemReIm(n,0,180/pi*ffaze,0);     // ve stupních fázová charakteristika
    else matfaze->SetItemReIm(n,0,(180/pi*ffaze)+180,0);   // korekce funkce atan, počítá o 180° menší hodnoty pokud je reálná část záporná
  }
  if ( buzeni == 31 || buzeni == 32 )
  {
    complex<double> faze(Invsmatrix->GetItem(sloupec,radek).r,Invsmatrix->GetItem(sloupec,radek).i);
    double ffaze = atan(faze.imag()/faze.real());
    if (faze.real()>=0) matfaze->SetItemReIm(n,0,180/pi*ffaze,0);
    else matfaze->SetItemReIm(n,0,(180/pi*ffaze)+180,0);

    complex<double> faze2(Invsmatrix->GetItem(sloupec2,radek).r,Invsmatrix->GetItem(sloupec2,radek).i);
    ffaze = atan(faze2.imag()/faze2.real());
    if (faze2.real()>=0) matfaze->SetItemReIm(n,0,180/pi*ffaze,0);
    else matfaze->SetItemSumReIm(n,0,(180/pi*ffaze)+180,0);
  }

 /* complex<double> faze(Invsmatrix->GetItem(vstup,vystup).r,Invsmatrix->GetItem(vstup,vystup).i);
  double ffaze = atan(faze.imag()/faze.real()); // v radiánech fázová charakteristika

    if (faze.real()>0) matfaze->SetItemReIm(n,0,180/pi*ffaze,0);     // ve stupních fázová charakteristika
  else matfaze->SetItemReIm(n,0,(180/pi*ffaze)+180,0);   // korekce funkce atan, počítá o 180° menší hodnoty pokud je reálná část záporná

  // matdb->SetItemReIm(n,0,180/pi*ffaze,0);
  //  if (n<10) ShowMessage(fdb);
  //  f->SetItemReIm(n,1,(Invsmatrix->GetItem(StrToInt(Edit2->Text),StrToInt(Edit1->Text)).r),Invsmatrix->GetItem(StrToInt(Edit2->Text),StrToInt(Edit1->Text)).i);     //   20*log
  //  WriteSubComplexArray(ListView17,f);

  */  delete smatrix;
    delete Invsmatrix;


}  //  konec for cyclu
   // ShowMessage("vstup = " +  EditIN->Text + "výstup = " + EditOUT->Text);
  if (vypocet) Memo3->Lines->Add(WriteSubComplexArrayToM(matfreq,"freq"));  // frekvence
  if (vypocet) Memo3->Lines->Add(WriteSubComplexArrayToM(matdb,"dB"));  // dB
  if (vypocet) Memo3->Lines->Add(WriteSubComplexArrayToM(matfaze,"ph"));  // faze
  //ShowMessage(matfreq->GetItem(0,0).r);
  //ShowMessage(matfreq->GetItem(pocetdekad*hodnotnadekadu,0).r);

//  FComplexCircuitArrayY1 = new TComplexArray( ModAUCount + CRAdd, ModAICount + CRAdd );


  //možna v budoucnu obecně... zatím rozděleno... první záložka modifikované matice a druhá záložka obsahuje jen matici kterou přičitám k té modifikované
  /*  WriteSubComplexArray(ListView9,FComplexCircuitArrayY1);
    WriteSubComplexArray(ListView10,FComplexCircuitArrayC12);
    WriteSubComplexArray(ListView11,FComplexCircuitArrayC21);
    WriteSubComplexArray(ListView12,FComplexCircuitArrayY2);

    WriteSubComplexArray(ListView13,FComplexCircuitArrayY1f);
    WriteSubComplexArray(ListView14,FComplexCircuitArrayC12f);
    WriteSubComplexArray(ListView15,FComplexCircuitArrayC21f);
    WriteSubComplexArray(ListView16,FComplexCircuitArrayY2f);
  */

  // tchart
  Chart1->Series[0]->Clear();
  Chart1->Series[1]->Clear();
/*  double maxosy_x = matfreq->GetItem(0,0).r; //prvni hodnota --> nalezení maxima
  double minosy_x = matfreq->GetItem(0,0).r;
   for(int d=1;d<(pocetdekad*hodnotnadekadu)+1;d++)
        {
        if (minosy_x>matfreq->GetItem(d,0).r)
                 minosy_x = matfreq->GetItem(d,0).r;
        if (maxosy_x<matfreq->GetItem(d,0).r)
                maxosy_x = matfreq->GetItem(d,0).r;
        }
  double maxosy_y = matdb->GetItem(0,0).r; //prvni hodnota --> nalezení maxima
  double minosy_y = matdb->GetItem(0,0).r;
   for(int d=1;d<(pocetdekad*hodnotnadekadu)+1;d++)
        {
        if (minosy_y>matdb->GetItem(d,0).r)
                 minosy_y = matdb->GetItem(d,0).r;
        if (maxosy_y<matdb->GetItem(d,0).r)
                maxosy_y = matdb->GetItem(d,0).r;
        }
     Chart1->LeftAxis->SetMinMax(1.1*minosy_y,1.1*maxosy_y);
  double maxosy_y2 = matfaze->GetItem(0,0).r; //prvni hodnota --> nalezení maxima
  double minosy_y2 = matfaze->GetItem(0,0).r;
   for(int d=1;d<(pocetdekad*hodnotnadekadu)+1;d++)
        {
        if (minosy_y2>matfaze->GetItem(d,0).r)
                 minosy_y2 = matfaze->GetItem(d,0).r;
        if (maxosy_y2<matfaze->GetItem(d,0).r)
                maxosy_y2 = matfaze->GetItem(d,0).r;
        }

     Chart1->LeftAxis->SetMinMax(1.1*minosy_y,1.1*maxosy_y);
     Chart1->RightAxis->SetMinMax(1.1*minosy_y2,1.1*maxosy_y2);

*/
  //      Chart1->BottomAxis->SetMinMax(minosy_x,maxosy_x);
     //   ShowMessage(minosy_x); ShowMessage(maxosy_x);

/*   odkomentovanim a vykreslenim grafu do záložky rozdrbu hodnoty leve osy Y
     for(int i = 0; i < matdb->GetColCount(); i++)
     {
       Chart1->Series[0]->Add(matdb->GetItem(i,0).r,(matfreq->GetItem(i,0).r), clBlue);
       Chart1->Series[1]->Add(matfaze->GetItem(i,0).r,(matfreq->GetItem(i,0).r), clRed);
     }
*/
     int poleCount =  matdb->GetColCount();
     double* poleMatfreq = new double[poleCount];
     double* poleMatfreq2 = new double[poleCount];
     double* poleMatdb = new double[poleCount];
     double* poleMatfaze = new double[poleCount];

    for(int i = 0; i < poleCount; i++)
     {
      poleMatfreq[i] = matfreq->GetItem(i,0).r;
      poleMatfreq2[i] = matfreq->GetItem(i,0).r;
      poleMatdb[i] = matdb->GetItem(i,0).r;
      poleMatfaze[i] = matfaze->GetItem(i,0).r;
     }


     TFormGraf* formGraf = new TFormGraf(this);
     formGraf->SetNazev("Graf " + IntToStr(1+Fx++));
     formGraf->SetValues(poleMatfreq, poleMatfreq2, poleMatdb, poleMatfaze, poleCount);
     formGraf->Show();

     delete [] poleMatfaze;
     delete [] poleMatdb;
     delete [] poleMatfreq;
     delete [] poleMatfreq2;
    // ShowMessage( Chart1->BottomAxis->IAxisSize );

     ///hahahaha





//---------------------------------------------------------------------------
//void rastr (void) //vykresli osy a pod. do image    { TComplexArray* f = new TComplexArray(400,1);
/*
Form1->Image->CleanupInstance();
Form1->Image->Canvas->Pen->Color = clBlack;
Form1->Image->Canvas->Pen->Width = 1;

float krok; //kolikrat se mi vleze jednotka stupnice(jedna osa na yose do cele osy y)

int xaxis = Form1->Image->Height - 60; // 40 + 10 pixels + 10 nalevo kvuli zvetseni min a max hodnot //802
int yaxis = Form1->Image->Width - 50; // 600
//smazani image
  double max_y = matdb->GetItem(0,0).r; //prvni hodnota --> nalezení maxima
  double min_y = matdb->GetItem(0,0).r;
   for(int d=1;d<(pocetdekad*hodnotnadekadu)+1;d++)
        {
        if (min_y>matdb->GetItem(d,0).r)
                 min_y = matdb->GetItem(d,0).r;
        if (max_y<matdb->GetItem(d,0).r)   // minimální hodnotu potřebuju
                max_y = matdb->GetItem(d,0).r;
        }
        // velikost osy y DATA
        double delkaosy_y = 0;
        float offsetosy_y = 0;
        if (max_y>0 && min_y>0) { delkaosy_y = max_y-min_y; offsetosy_y = min_y; }
        if (max_y>0 && min_y<0) { delkaosy_y = max_y-min_y; offsetosy_y = 0; }
        if (max_y<0 && min_y<0) { delkaosy_y = abs(min_y)-abs(max_y); offsetosy_y = max_y; }

      //  ShowMessage(max_y);   ShowMessage(min_y);

krok = 0.0001;  //kolikrat se mi vleze jednotka stupnice(jedna osa na yose do cele osy y)
       while (delkaosy_y > krok*2.5 ) //od dvou a pul nasobku kroku bude o dekadu vetsi rozliseni
        {
         krok=krok*10; //
        }
krok=krok*0.1;

int jednotka_stupnice = RoundTo(600/((1.1*delkaosy_y)/krok),0);
   yaxis=jednotka_stupnice*((1.1*delkaosy_y)/krok); //(1.1*delka_osy)/krok - kolikrat se mi vleze krok do osy

//   jednotka_stupnice = RoundTo((Form1->Image->Height-30)/11,0); //jednotka stupnice - rozestup popisku na ose v pixelech
//   yaxis=jednotka_stupnice*11;

   //max_e=RoundTo(max_e,-3)+0.001;
   //Form1->Image->Canvas->TextOutA(20,20,"test");
//---------------------------osa freq
//      while (x<10)//od kolika do kolika ma pocitat data na pocet dekad
//        {
//        if (Power(10,x)>min_f && Power(10,x)<max_f)
//                {
//                buff=x; //ulozim posledni dekadu
//                pocet_dekad++;    //sem ulozim kolik je dekad
//                }
//        x++;
//        }
    int pocet_dekad = 5;
    int buff = 5;
//y osa - zvetsit o cca 10procent
//max_e - maximum
//y_max = e_max*1.1; //kde y_max je delka osy (ne pixely ale eps)


  //nejdriv pocitame, potom kreslime :)
 int  delka_dekady = 200; // RoundTo((Form1->Image->Width - 40)/pocet_dekad,0) ;//zaokrouhlena delka dekady
 float konstanta=Power(10,1.0/delka_dekady);   //to cim nasobym pixely u xove osy
  xaxis=delka_dekady * pocet_dekad;   //delka xove osy
  freq_axis[0]=Power(10,buff-pocet_dekad+1); //Power(10,buff)/Power(10,pocet_dekad);
  for (int a=1; a<xaxis; a++)
        {
        freq_axis[a]=konstanta*freq_axis[a-1];
        }

  //mazani img
  Form1->Image->Canvas->Pen->Style = bsSolid;
  Form1->Image->Canvas->Brush->Style = bsSolid;
  Form1->Image->Canvas->Rectangle(0,0,Form1->Image->Width,Form1->Image->Height);


  Form1->Image->Canvas->Brush->Style = bsClear;
  for (int c=0;c<=pocet_dekad;c++)
        {
        Form1->Image->Canvas->Font->Size = 10;
        Form1->Image->Canvas->TextOutA(30+delka_dekady*c,Form1->Image->Height-15,"10");
        Form1->Image->Canvas->Font->Size = 8;
        Form1->Image->Canvas->TextOutA(43+delka_dekady*c,Form1->Image->Height-20,buff-pocet_dekad+c+1);    //jednicka jako korekce:(
        }

  Form1->Image->Canvas->MoveTo(40,Form1->Image->Height - 20);//x,y --- zacatek 0,0
  Form1->Image->Canvas->LineTo(pocet_dekad*delka_dekady-150,Form1->Image->Height - 20 );
  //Form1->Image->Canvas->LineTo(pocet_dekad*delka_dekady+40,Form1->Image->Height - 20 );

  //osa dB
  float ykonstanta;
  ykonstanta = yaxis/(delkaosy_y * 1.1);
  double offset = 0;
    int e = 0;
    for (int k=0;k <25; k++)
        if ( krok*k > abs(max_y) ) {offset = (krok*k - abs(max_y))*ykonstanta; e = k; k = 30;} //offset ... vzdálenost první horizontální osy od shora 0,0



  for( int k = 0;k<25;k++)      // e<11 puvodne
        {
        Form1->Image->Canvas->TextOutA(10,offset+50+jednotka_stupnice*k,-1*RoundTo((e+k)*krok,-2));     //-1* záporné hodnoty Y
      // Form1->Image->Canvas->TextOutA(1,(Form1->Image->Height-30)-jednotka_stupnice*e,-1*RoundTo(e*krok,-3));
        }
  Form1->Image->Canvas->MoveTo(40,Form1->Image->Height-20);
  Form1->Image->Canvas->LineTo(40,40);
  //Form1->Image->Canvas->LineTo(40,Form1->Image->Height-11*jednotka_stupnice);

 //rastr
 Form1->Image->Canvas->Pen->Style = psDot;
  for(int b=0;b<=pocet_dekad;b++)
        { //dekady
         Form1->Image->Canvas->MoveTo(b*delka_dekady+40,Form1->Image->Height-20);
         Form1->Image->Canvas->LineTo(b*delka_dekady+40,40);
         //Form1->Image->Canvas->LineTo(b*delka_dekady+40,Form1->Image->Height-11*jednotka_stupnice);
        }
    for(int b=0;b<25;b++)
        { //dB
         Form1->Image->Canvas->MoveTo(Form1->Image->Width+130-pocet_dekad*delka_dekady,offset+55+jednotka_stupnice*b);
         Form1->Image->Canvas->LineTo(Form1->Image->Width-50,offset+55+jednotka_stupnice*b);
         //Form1->Image->Canvas->MoveTo(Form1->Image->Width+130-pocet_dekad*delka_dekady,(Form1->Image->Height-20)-jednotka_stupnice*b);
         //Form1->Image->Canvas->LineTo(Form1->Image->Width-50,(Form1->Image->Height-20)-jednotka_stupnice*b);
        }
  Form1->Image->Canvas->Pen->Style = psSolid;



//---------------------------------------------------------------------------
//void vykresli_data(){

int xosa=0,yosa,b,c=0;

//int ykonstanta; //prozatim tady

//float ykonstanta=1000*jednotka_stupnice;
//float ykonstanta;
//ykonstanta = yaxis/(delkaosy_y * 1.1);
Form1->Image->Canvas->Pen->Color = clRed;
Form1->Image->Canvas->Pen->Width = 3;

 for (int a=0;a<401; a++)   //for (int a=0;a<delka_dekady*pocet_dekad; a++)
                 {
                  if(a==0)//je to zacatek
                        {                                         // - offsetosy protože je 0,0 od shora v image
                        Form1->Image->Canvas->MoveTo(a+40, 50-RoundTo((-offsetosy_y + matdb->GetItem(0,0).r)*ykonstanta,0));
                       // Form1->Image->Canvas->MoveTo(a+40,Form1->Image->Height - 20-RoundTo(f->GetItem(0,1).r*ykonstanta,0));
                        }
                  else
                        Form1->Image->Canvas->LineTo(2*a+41, 50-RoundTo((-offsetosy_y + matdb->GetItem(a,0).r)*ykonstanta,0));
                       // Form1->Image->Canvas->LineTo(2*a+41,Form1->Image->Height - 20-RoundTo(f->GetItem(a,1).r*ykonstanta,0));
                 }


for (int i=0; i<401; i++) dBvalue[i] = matdb->GetItem(i,0).r;
for (int i=0; i<401; i++) Pvalue[i] = matfaze->GetItem(i,0).r;


 */
// WriteSubComplexArray(ListView17,f);


}
}


//---------------------------------------------------------------------------

void __fastcall TForm1::ImageMouseMove(TObject *Sender, TShiftState Shift,
      int X, int Y)
{
  Form1->Panel1->Visible = 1;
  Form1->Panel1->Left = X+50;
  XLabel->Caption = "X = " + FloatToStr(RoundTo(freq_axis[X-40],0));
  if (X>=40 && X<=840) YLabel->Caption = "Y = " + FloatToStr( RoundTo(dBvalue[(X-40)/2],-2)); else YLabel->Caption = "Y = 0";


}
//---------------------------------------------------------------------------

void __fastcall TForm1::Chart1MouseMove(TObject *Sender, TShiftState Shift,
      int X, int Y)
{
    Label41->Visible = true;
    Label42->Visible = true;

  Label41->Caption = "X = " + IntToStr(X);
  Label42->Caption = "Y = " + IntToStr(Y);
  
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Memo1Exit(TObject *Sender)
{
 if (ZmenaNetList)
 {
   Memo2->Clear();
   Memo3->Clear();
   LV1->Clear();
   LV2->Clear();
   LV3->Clear();
   LV4->Clear();
   LV5->Clear();
   LV6->Clear();
   LV7->Clear();
   LV8->Clear();
   LV9->Clear();
   LV10->Clear();
   LV11->Clear();
   LV12->Clear();
   LV13->Clear();

   
   delete CompR;
   delete CompRS1;
   delete CompRS2;
   delete CompL;
   delete CompC;
   delete Switch;
   delete AOIdoz;
   delete CompBuf;
   delete CompM;
   delete Nodes;
   delete CompIdoz;
   CompR = new TElePart();
   CompRS1 = new TElePart();
   CompRS2 = new TElePart();
   CompL = new TElePartL();
   CompC = new TElePart();
   Switch = new TElePartS();
   AOIdoz = new TEleAO();
   CompBuf = new TElePartB();
   CompM = new TElePartM();
   Nodes = new TEleNodes();
   CompIdoz = new TEleIDOZ();
   delete FCircuitArrayY1;
   delete FCircuitArrayC12;
   delete FCircuitArrayC21;
   delete FCircuitArrayY2;
   delete FCircuitArrayY1s;
   delete FCircuitArrayC12s;
   delete FCircuitArrayC21s;
   delete FCircuitArrayY2s;
   delete FComplexCircuitArrayY1;
   delete FComplexCircuitArrayC12;
   delete FComplexCircuitArrayC21;
   delete FComplexCircuitArrayY2;
   delete FComplexCircuitArrayY1f;
   delete FComplexCircuitArrayC12f;
   delete FComplexCircuitArrayC21f;
   delete FComplexCircuitArrayY2f;
   delete FComplexCircuitArrayY1short;
   delete FComplexCircuitArrayC12short;
   delete FComplexCircuitArrayC21short;
   delete FComplexCircuitArrayY2short;

   delete matfreq;
   delete matexp;
   delete matdb;
   delete matfaze;
 }
 ZmenaNetList = false;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ChBoutfaze1Click(TObject *Sender)
{
 if (ChBoutfaze1->Checked == true)  ChBoutfaze2->Checked = false;
 else ChBoutfaze2->Checked = true;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ChBoutfaze2Click(TObject *Sender)
{
  if (ChBoutfaze2->Checked == true) ChBoutfaze1->Checked = false;
   else ChBoutfaze1->Checked = true;          
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Memo1Change(TObject *Sender)
{
   ZmenaNetList = true;
   Button1->Visible = true;
   Button2->Visible = false;
   Button3->Visible = false;
   Button8->Visible = false;
   Panelin->Visible = false;
   Panelout->Visible = false;
   Label39->Visible = false;
   Label40->Visible = false;
   Label43->Visible = false;
   EditFS->Visible = false;
   EditD->Visible = false;
   CBHodDek->Visible = false;
   CBXmin->Visible = false;
   CBXmax->Visible = false;
}
//---------------------------------------------------------------------------










void __fastcall TForm1::Ulozit2Click(TObject *Sender)
{
 AnsiString SaveName;
 if(SaveDialog2->Execute())
 {
   SaveName = SaveDialog2->FileName;
   if (SaveDialog2->FilterIndex == 1) SaveName = SaveName + ".cir";
   if (SaveDialog2->FilterIndex == 2) SaveName = SaveName + ".txt";
   Memo1->Lines->SaveToFile(SaveName);
 }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::secret1Click(TObject *Sender)
{
   TabSheet2->TabVisible = true;
   TabSheet3->TabVisible = true;
   TabSheet4->TabVisible = true;
   TabSheet5->TabVisible = true;
}
//---------------------------------------------------------------------------

