{-----------------------------------------------------------------------------
 Unit Name: NumSpheres
 Author:    kaetzel
 Purpose:   This unit contains the procedures for reading the agglomerate
            configuration and the handling of the three different forms
-----------------------------------------------------------------------------}

unit NumSpheres;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, JvEdit,JvDSADialogs;

procedure Delay(msec:LongInt);

type
  TNumSpheresWindow = class(TForm)
    OkButton: TBitBtn;
    InputLabel: TLabel;
    NumberInput: TJvEdit;
    procedure OkButtonClick(Sender: TObject);
    procedure OnActivate(Sender: TObject);

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  NumSpheresWindow: TNumSpheresWindow;



implementation

uses Coordinates, Main, Functions;

{$R *.dfm}


{-----------------------------------------------------------------------------
  Procedure: Delay
  Purpose:   Stops the program for a defined time
  Comments:  Introduced to make process messages readable
-----------------------------------------------------------------------------}

procedure Delay(msec: LongInt);
var start, stop: LongInt;
begin
  start := GetTickCount;
  repeat
    stop := GetTickCount;
    Application.ProcessMessages;
  until (stop-start)>=msec;
end;


{-----------------------------------------------------------------------------
  Procedure: ReadValues
  Purpose:   Reads the coordinate values of each sphere, the diameter and the
             refractive index and stores them
  Comments:
-----------------------------------------------------------------------------}

procedure ReadValues;
var
  i:integer;
begin
  Delay(3);
        //opens the third form which contains the coordinates of each sphere
  CoordinateWindow.Show;
  i:=1;
  Repeat
    begin
      case i of
        1: CoordinateWindow.NumberLabel.Caption:='1st';
        2: CoordinateWindow.NumberLabel.Caption:='2nd';
        3: CoordinateWindow.NumberLabel.Caption:='3rd'
      else
        CoordinateWindow.NumberLabel.Caption:=IntToStr(i)+'th';
      end;
      CoordinateWindow.DiameterInput.SetFocus;
                //Wait for the Ok-Button to be clicked
      Repeat
        Application.ProcessMessages;
        if CloseClicked then
          exit;
      Until ButtonClicked;
      try
                //Reading the values d,coordinates(x,y,z) and np of each sphere
        dia[i-1]:=StrToFloat(CoordinateWindow.DiameterInput.Text);
                //d must be >0
        if dia[i-1]<=0 then abort;
        Xcoord[i-1]:=StrToFloat(CoordinateWindow.XcoordInput.Text);
        Ycoord[i-1]:=StrToFloat(CoordinateWindow.YcoordInput.Text);
        Zcoord[i-1]:=StrToFloat(CoordinateWindow.ZcoordInput.Text);
        RefrIndexReal[i-1]:=StrToFloat(CoordinateWindow.RealPartInput.Text);
        RefrIndexImag[i-1]:=StrToFloat(CoordinateWindow.ImagPartInput.Text);
                //Refractive index real part must be >1
        if RefrIndexReal[i-1]<1 then abort;
                //Refractive index  imaginary part must be negative to ensure
                //energy conservation
        if RefrIndexImag[i-1]>0 then abort;
        ButtonClicked:=False;
        i:=i+1;
        CoordinateWindow.XcoordInput.Text:='';
        CoordinateWindow.YcoordInput.Text:='';
        CoordinateWindow.ZcoordInput.Text:='';
      except
        ShowMessage(Format(' This is not an allowed value!'+
        '%s All values must be floating points, %s '+
        'diameter must be positive,'+
        ' %s real part of the refractive index must be greater than 1 %s'
        +' and the imaginary part must be negative! %s',
        [#10#13,#10#13,#10#13,#10#13,#10#13,#10#13] ),dckActiveForm);
        CoordinateWindow.DiameterInput.SetFocus;
        ButtonClicked:=False;
      end;
    end;
  Until i=Nl+1;
  CoordinateWindow.Close;
  NumSpheresWindow.Close;
        //Set properties for further calculation
  MainWindow.AggrConfigButton.Enabled:=False;
  MainWindow.ResetButton.Enabled:=True;
  MainWindow.RefrIndexInput.Enabled:=True;
  MainWindow.WavelengthInput.Enabled:=True;
  if Nl<>1 then
    MainWindow.CbOrientations.Enabled:=True;
  MainWindow.StartButton.Enabled:=True;
        //"Agglomerate configuration......"
  MainWindow.Label22.Hide;
        //"Agglomerate specified!"
  MainWindow.Label23.Show;
  MainWindow.RefrIndexInput.SetFocus;
end;


{-----------------------------------------------------------------------------
  Procedure: TNumSpheresWindow.OkButtonClick
  Purpose:   Reads the number of spheres in the agglomerate and calls ReadValues
             with immediate check of overlapping
  Comments:
-----------------------------------------------------------------------------}

procedure TNumSpheresWindow.OkButtonClick(Sender: TObject);
begin
  try
        //Reads the number of spheres
    Nl:=StrToInt(NumberInput.Text);
    if Nl<=0 then abort;
        //Initialize all global arrays which depend on Nl
    SetLength(dia,Nl);
    SetLength(Xcoord,Nl);
    SetLength(Ycoord,Nl);
    SetLength(Zcoord,Nl);
    SetLength(RefrIndexReal,Nl);
    SetLength(RefrIndexImag,Nl);
    SetLength(Rcoord,Nl);
    SetLength(Thetacoord,Nl);
    SetLength(Phicoord,Nl);
    SetLength(x,Nl);
    SetLength(WiscombeNumber,Nl);
    SetLength(RelRefrIndex,Nl,2);
    SetLength(djl,Nl,Nl);
    SetLength(phijl,Nl,Nl);
    SetLength(thetajl,Nl,Nl);
    NumSpheresWindow.Hide;
    repeat
      ReadValues;
      if Overlap then
        begin
          MainWindow.ResetButton.Enabled:=False;
          MainWindow.RefrIndexInput.Enabled:=False;
          MainWindow.WavelengthInput.Enabled:=False;
          MainWindow.StartButton.Enabled:=False;
          MainWindow.Label23.Hide;
          ShowMessage(Format('WARNING!! SPHERES OVERLAPPED!!%s'+
                              'This programm can'+
                              ' only calculate the scattering %s'+
                              ' parameters for non-intersecting sphe'+
                              'res!%s'+' Please redefine Agglomerate!',
                              [#10#13#10#13,#10#13,#10#13#10#13])
                              ,dckActiveForm);
        end
    until (not Overlap);
    CoordinateWindow.Close;
    NumSpheresWindow.Close;
    MainWindow.Show;
  except
    ShowMessage('Nl must be an postive integer!',dckActiveForm);
    NumberInput.SetFocus;
  end;
end;


{-----------------------------------------------------------------------------
  Procedure: TNumSpheresWindow.OnActivate
  Purpose:   Initializes the second form
  Comments:
-----------------------------------------------------------------------------}

procedure TNumSpheresWindow.OnActivate(Sender: TObject);
begin
  NumberInput.Text:='';
  NumberInput.SetFocus;
end;

end.
