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

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include "About.h"
#include "Register.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "optimal1dx_OCX"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormShow(TObject *Sender)
{
  fRegister->eComputerID->Text = Optimization1DX1->get_ComputerID();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Exit1Click(TObject *Sender)
{
  Application->Terminate();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Abut1Click(TObject *Sender)
{
  if (AboutBox->ShowModal()){
  }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Register1Click(TObject *Sender)
{
  if (fRegister->ShowModal() == mrOk){
    Optimization1DX1->set_UserName(WideString(fRegister->eName->Text));
    Optimization1DX1->set_SerialNumber(WideString(fRegister->eSerialNumber->Text));
    Optimization1DX1->RegisterOptimization1DX();
  }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::bStartClick(TObject *Sender)
{
  bStart->Enabled = false;

  Optimization1DX1->set_NumberOfRepositoryPieces(6);
  Optimization1DX1->SetRepositoryPiece(0, 10, 0);
  Optimization1DX1->SetRepositoryPiece(1, 10, 1);
  Optimization1DX1->SetRepositoryPiece(2, 10, 2);
  Optimization1DX1->SetRepositoryPiece(3, 10, 3);
  Optimization1DX1->SetRepositoryPiece(4, 10, 4);
  Optimization1DX1->SetRepositoryPiece(5, 10, 5);

  Optimization1DX1->set_NumberOfDemandPieces(10);
  Optimization1DX1->set_BladeWidth(0);
  Optimization1DX1->set_Width(10);
  Optimization1DX1->set_RandomSeed(1);
  Optimization1DX1->set_OptimizationLevel(50);

  Optimization1DX1->SetDemandPiece(0, 2, 90, 90, 0);
  Optimization1DX1->SetDemandPiece(1, 2, 90, 90, 1);
  Optimization1DX1->SetDemandPiece(2, 2, 90, 90, 2);
  Optimization1DX1->SetDemandPiece(3, 2, 90, 90, 3);
  Optimization1DX1->SetDemandPiece(4, 2, 90, 90, 4);
  Optimization1DX1->SetDemandPiece(5, 7, 90, 90, 5);
  Optimization1DX1->SetDemandPiece(6, 7, 90, 90, 6);
  Optimization1DX1->SetDemandPiece(7, 7, 90, 90, 7);
  Optimization1DX1->SetDemandPiece(8, 7, 90, 90, 8);
  Optimization1DX1->SetDemandPiece(9, 7, 90, 90, 9);

  lbUsedPieces->Clear();
  lbUnusedPieces->Clear();
  lbCuttingOrder->Clear();
  
  Optimization1DX1->Start();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Optimization1DX1Finish(TObject *Sender)
{
  bStart->Enabled = true;
  double waste;
  waste = Optimization1DX1->TotalWaste;
  eWaste->Text = FloatToStr(waste);

  long usedlayout, Length;
  long demand_ID, repository_ID, repository_Index, demand_Index;// repository, demand Index and External IDs

  for (int i = 0; i < Optimization1DX1->NumberOfDemandPieces; i++){
    Optimization1DX1->DemandPieceUtilized(i, &usedlayout, &demand_ID, &repository_Index, &repository_ID);
    double angle1, angle2;
    Optimization1DX1->GetDemandPiece(i, &Length, &angle1, &angle2, &demand_ID);
    if (usedlayout >= 0)
      lbUsedPieces->AddItem(IntToStr(Length), NULL);
    else
      lbUnusedPieces->AddItem(IntToStr(Length), NULL);
  }

  for (int BarIndex = 0; BarIndex < Optimization1DX1->TotalNumberOfUtilizedRepositoryPieces; BarIndex++){
    AnsiString tmp = "Bar " + IntToStr(BarIndex) + ":";
    long num_utilized;
    double angle1,angle2;
    long rotatedx, rotatedy, rotatedz;
    Optimization1DX1->GetUtilizedDemandPiece(BarIndex, 0, &repository_Index, &demand_Index, &rotatedx, &rotatedy, &rotatedz, &repository_ID, &demand_ID);

    Optimization1DX1->GetRepositoryPiece(repository_Index, &Length, &repository_ID);
    tmp += "(Length = " + IntToStr(Length) + ")";

    Optimization1DX1->GetNumberOfUtilizedDemandPieces(BarIndex, &num_utilized, &repository_Index, &repository_ID);
    
    for (int PieceIndex = 0; PieceIndex < num_utilized; PieceIndex++){
      Optimization1DX1->GetUtilizedDemandPiece(BarIndex, PieceIndex, &repository_Index, &demand_Index, &rotatedx, &rotatedy, &rotatedz, &repository_ID, &demand_ID);
      Optimization1DX1->GetDemandPiece(demand_Index, &Length, &angle1, &angle2, &demand_ID);
      if (rotatedx){
        angle1 = 180 - angle1;
        angle2 = 180 - angle2;
      }
      if (rotatedy){
        double tmp = angle1;
        angle1 = 180 - angle2;
        angle2 = 180 - tmp;
      }
      if (rotatedz){
        double tmp = angle1;
        angle1 = angle2;
        angle2 = tmp;
      }
      tmp += "(" + IntToStr(Length) + "," + FloatToStr(angle1) + "," +FloatToStr(angle2) + ")";
    }

    Optimization1DX1->GetWaste(BarIndex, &waste, &repository_Index, &repository_ID);
    tmp += "(Waste = " + FloatToStr(waste) + ")";
    lbCuttingOrder->AddItem(tmp, NULL);
  }
}
//---------------------------------------------------------------------------

