unit U_Ruler;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, cxControls, cxContainer, cxEdit, cxTextEdit,
  cxMaskEdit, cxButtonEdit, cxLookAndFeelPainters, cxButtons, cxRadioGroup,
  DrawObjects, RzPanel;

type
  TF_Ruler = class(TForm)
    bOK: TcxButton;
    bCancel: TcxButton;
    RzGroupBox1: TRzGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    rbmm1: TcxRadioButton;
    rbsm1: TcxRadioButton;
    rbm1: TcxRadioButton;
    edSizeAs: TcxMaskEdit;
    Label3: TLabel;
    edSizeAll: TcxMaskEdit;
    rbmm2: TcxRadioButton;
    rbsm2: TcxRadioButton;
    rbm2: TcxRadioButton;
    Label4: TLabel;
    Bevel1: TBevel;
    procedure bCancelClick(Sender: TObject);
    procedure bOKClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure edSizeAsKeyPress(Sender: TObject; var Key: Char);
    procedure edSizeAllKeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  F_Ruler: TF_Ruler;

implementation
uses USCS_Main, U_CAD, U_ESCadClasess, U_Common;

{$R *.dfm}

procedure TF_Ruler.bCancelClick(Sender: TObject);
begin
  Close;
end;

procedure TF_Ruler.bOKClick(Sender: TObject);
var
  i: integer;
  ScaleAs: double;
  ScaleAll: double;
  KoefAs: double;
  KoefAll: double;
  PageKoef: double;
begin
  try
    ScaleAs := 0;
    ScaleAll := 0;
    KoefAs := 0;
    KoefAll := 0;
    PageKoef := 0;

    // для показа отдельного отрезка на экране
    if rbm1.Checked then
      KoefAs := 100;
    if rbsm1.Checked then
      KoefAs := 1;
    if rbmm1.Checked then
      KoefAs := 0.1;
    if (rbm1.Checked) Or (rbsm1.Checked) Or (rbmm1.Checked) then
    begin
      if edSizeAs.Text <> '' then
      begin
        ScaleAs := StrToFloat_My(edSizeAs.Text) * KoefAs;
        GCadForm.PCad.MapScale := round(ScaleAs);
      end;
    end;
    // для отображения общей длинны на странице
    if rbm2.Checked then
      KoefAll := 1;
    if rbsm2.Checked then
      KoefAll := 100;
    if rbmm2.Checked then
      KoefAll := 1000;
    if (rbm2.Checked) Or (rbsm2.Checked) Or (rbmm2.Checked) then
    begin
      if edSizeAll.Text <> '' then
      begin
        PageKoef := GCadForm.PCad.WorkWidth / 1000;
        ScaleAll := StrToFloat_My(edSizeAll.Text) / PageKoef / KoefAll;
        GCadForm.PCad.MapScale := round(ScaleAll);
      end;
    end;
    // определить перемасштабирование обьектов
    for i := 0 to GCadForm.PCad.Figures.Count - 1 do
    begin
      if TFigure(GCadForm.PCad.Figures[i]) is TOrthoLine then
        TOrthoLine(GCadForm.PCad.Figures[i]).Move(0, 0);
    end;
    try
      GCadForm.PCad.Refresh;
    except
    end;    
  except
    ShowMessage('EXCEPTION: TF_Ruler.bOKClick');
  end;
end;

procedure TF_Ruler.FormShow(Sender: TObject);
begin
  bOK.SetFocus;
end;

procedure TF_Ruler.FormKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #27 then
    Close; 
end;

procedure TF_Ruler.edSizeAsKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    bOK.SetFocus;
    bOK.Click;
  end;
end;

procedure TF_Ruler.edSizeAllKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    bOK.SetFocus;
    bOK.Click;
  end;
end;

end.
