unit CSUnit;

{$OPTIMIZATION OFF}

interface

uses Windows, SysUtils, Classes, Graphics, Forms, Controls, StdCtrls,
  Buttons, ExtCtrls, Dialogs, Spin, MiscRoutines, PlugCommon;

type
  TCSDialog = class(TForm)
    OKBtn: TButton;
    CancelBtn: TButton;
    Bevel1: TBevel;
    SaveDialog: TSaveDialog;
    Label1: TLabel;
    SESteps: TSpinEdit;
    Label2: TLabel;
    EFileName: TEdit;
    BrowseButton: TButton;
    procedure SEStepsExit(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure BrowseButtonClick(Sender: TObject);
    procedure OKBtnClick(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  end;

var
  CSDialog : TCSDialog = nil;
  
implementation

{$R *.DFM}

procedure TCSDialog.SEStepsExit(Sender: TObject);
begin
  if(not CheckSpinContens(SESteps,'Делений')) then
    FocusControl(SESteps);
end;

procedure TCSDialog.FormShow(Sender: TObject);
begin
  EFileName.Text:=ExtractFileName(SaveDialog.FileName);
end;

procedure TCSDialog.BrowseButtonClick(Sender: TObject);
begin
  if(SaveDialog.Execute) then FormShow(nil);
end;

procedure TCSDialog.OKBtnClick(Sender: TObject);
var
  S : TFileStream;
  c1,c2,c3 : double;
  step : double;
  fn : string;
  X : array[0..9] of double;
  res : double;
begin
  try
    fn:=ExtractFilePath(SaveDialog.FileName)+EFileName.Text;
    S:=TFileStream.Create(fn,fmCreate);
    step:=1/SESteps.Value;
    c1:=1;
    while(c1>=0) do
    begin
      c2:=0;
      while((c1+c2)<=1) do
      begin
        c3:=1-(c1+c2);
        X[0]:=c1;
        X[1]:=c2;
        X[2]:=c3;
        GetMainServer.GetY(@X,@res);
        fPrint(TStream(S),Format('%f;%f;%f;%f'+#13,[c1,c2,c3,res]));
        c2:=c2+step;
      end;
      c1:=c1-step;
    end;
    S.Destroy;
  except
    on EFCreateError do
    begin
      Error('Ошибка создания файла "'+fn+'".');
      exit;
    end;
    on EReadError do
    begin
      Error('Ошибка записи файла "'+fn+'".');
      S.Destroy;
      exit;
    end;
  end;
end;

procedure TCSDialog.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if(Key=VK_ESCAPE) then Close;
end;

end.
