unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, OleServer, ComCtrls, Menus, RegisterUnit, About, optimal1dx_TLB;

type
  TForm1 = class(TForm)
    bStart: TButton;
    eWaste: TEdit;
    Label1: TLabel;
    lbUsedPieces: TListBox;
    lbUnusedPieces: TListBox;
    Label5: TLabel;
    Label6: TLabel;
    lCuttingOrder: TLabel;
    sbIteration: TStatusBar;
    lbCuttingOrder: TListBox;
    eRepetitions: TEdit;
    lRepetitions: TLabel;
    MainMenu1: TMainMenu;
    File1: TMenuItem;
    Exit1: TMenuItem;
    Help1: TMenuItem;
    Register1: TMenuItem;
    About1: TMenuItem;
    Optimization1DX1: TOptimization1DX;
    procedure bStartClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure About1Click(Sender: TObject);
    procedure Exit1Click(Sender: TObject);
    procedure Register1Click(Sender: TObject);
    procedure Optimization1DX1Finish(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.bStartClick(Sender: TObject);
begin
  bStart.Enabled := false;

  Optimization1DX1.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.NumberOfDemandPieces := 10;
  Optimization1DX1.BladeWidth := 0;
  Optimization1DX1.Width := 10;
  Optimization1DX1.RandomSeed := 1;
  Optimization1DX1.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();
  
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  RegisterForm.eComputerID.Text := Optimization1DX1.ComputerID;
end;

procedure TForm1.About1Click(Sender: TObject);
begin
  AboutBox.ShowModal;
end;

procedure TForm1.Exit1Click(Sender: TObject);
begin
  Application.Terminate;
end;

procedure TForm1.Register1Click(Sender: TObject);
begin
  if RegisterForm.ShowModal = mrOk
  then begin
    Optimization1DX1.RegisterOptimization1DX2(RegisterForm.eName.Text, RegisterForm.eSerialNumber.Text);
  end;
end;

procedure TForm1.Optimization1DX1Finish(Sender: TObject);
  var
    usedlayout, Length, rotatedOx, rotatedOy, rotatedOz : longint;
    num_utilized : longint;
    demand_ID, repository_ID, demand_Index, repository_Index : longint;
    i, k : integer;
    angle1, angle2, tmp, w : double;
    tmp_str : AnsiString ;
begin
  bStart.Enabled := true;
  eWaste.Text := FloatToStr(Optimization1DX1.TotalWaste);

  for i := 0 to Optimization1DX1.NumberOfDemandPieces - 1 do
  begin
    Optimization1DX1.DemandPieceUtilized(i, usedlayout, demand_ID, repository_Index, repository_ID);
    Optimization1DX1.GetDemandPiece(i, Length, angle1, angle2, demand_ID);
    if usedlayout >= 0
    then
      lbUsedPieces.AddItem(IntToStr(Length), nil)
    else
      lbUnusedPieces.AddItem(IntToStr(Length), nil);
  end;

  for k := 0 to Optimization1DX1.TotalNumberOfUtilizedRepositoryPieces - 1 do
  begin
    tmp_str := 'Bar ' + IntToStr(k) + ': ';
    Optimization1DX1.GetUtilizedDemandPiece(k, 0, repository_Index, demand_Index, rotatedOx, rotatedOy, rotatedOz, repository_ID, demand_ID);
    Optimization1DX1.GetRepositoryPiece(repository_Index, Length, repository_ID);
    tmp_str := tmp_str + '(Length = ' + IntToStr(Length) + ')';
    Optimization1DX1.GetNumberOfUtilizedDemandPieces(k, num_utilized, repository_Index, repository_ID);

    for i := 0 to num_utilized -1 do
    begin
      Optimization1DX1.GetUtilizedDemandPiece(k, i, repository_Index, demand_Index, rotatedOx, rotatedOy, rotatedOz, repository_ID, demand_ID);
      Optimization1DX1.GetDemandPiece(demand_Index, Length, angle1, angle2, demand_ID);
      if rotatedOx = 1
      then begin
        angle1 := 180 - angle1;
        angle2 := 180 - angle2;
      end;
      if rotatedOy = 1
      then begin
        tmp := angle1;
        angle1 := 180 - angle2;
        angle2 := 180 - tmp;
      end;
      if rotatedOz = 1
      then begin
        tmp := angle1;
        angle1 := angle2;
        angle2 := tmp;
      end;
      tmp_str := tmp_str + '(' + IntToStr(Length) + ',' + FloatToStr(angle1) + ',' +FloatToStr(angle2) + ')';

    end;

    Optimization1DX1.GetWaste(k, w, repository_Index, repository_ID);
    tmp_str := tmp_str + '(Waste = ' + FloatToStr(w) + ')';

    lbCuttingOrder.AddItem(tmp_str, nil);
  end;
end;

end.
