unit U_SizePos;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxEdit,
  DB, cxDBData, cxGridLevel, cxClasses, cxControls, cxGridCustomView,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxGrid,
  StdCtrls, cxContainer, cxTextEdit,
  /// PowerCad
  PCPanel, PCDrawBox, PCDrawing, PowerCad, PCTypesUtils,
  DrawObjects, ExtDlgs, PCLayerDlg, OleCtnrs, PCgui, GuiStrings,
  DrawEngine, u_lng, U_ESCadClasess, ExtCtrls, siComp, siLngLnk;


type
  TF_SizePos = class(TForm)
    lb_X: TLabel;
    lb_Y: TLabel;
    lbWidth: TLabel;
    lbHeight: TLabel;
    lbAngle: TLabel;
    edX: TcxTextEdit;
    edY: TcxTextEdit;
    edWidth: TcxTextEdit;
    edHeight: TcxTextEdit;
    edAngle: TcxTextEdit;
    lbX_m: TLabel;
    lbY_m: TLabel;
    lbW_m: TLabel;
    lbH_m: TLabel;
    lbA_o: TLabel;
    Bevel1: TBevel;
    lb_Z: TLabel;
    lbZ_m: TLabel;
    edZ: TcxTextEdit;
    Label1: TLabel;
    Bevel2: TBevel;
    edObjectName: TcxTextEdit;
    lng_Forms: TsiLangLinked;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure eXKeyPress(Sender: TObject; var Key: Char);
    procedure edAngleKeyPress(Sender: TObject; var Key: Char);
    procedure edWidthKeyPress(Sender: TObject; var Key: Char);
    procedure edZKeyPress(Sender: TObject; var Key: Char);
    procedure edObjectNameKeyPress(Sender: TObject; var Key: Char);
    procedure edObjectNameExit(Sender: TObject);
    procedure edWidthExit(Sender: TObject);
    procedure edAngleExit(Sender: TObject);
    procedure edXExit(Sender: TObject);
    procedure edYExit(Sender: TObject);
    procedure edZExit(Sender: TObject);
    procedure edHeightExit(Sender: TObject);
  private
    { Private declarations }
  public
    ClickFigure: TFigure;
    { Public declarations }
    Figure_Name: string;
    Figure_Width: string;
    Figure_Height: string;
    Figure_Angle: string;
    Figure_X: string;
    Figure_Y: string;
    Figure_Z: string;
    procedure DefineObjectSizePos;
  end;

var
  F_SizePos: TF_SizePos;

implementation
uses USCS_Main, U_CAD, U_Common, U_BaseCommon, U_Constants;
{$R *.dfm}

procedure TF_SizePos.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  FSCS_Main.aViewCADObjectsProp.Checked := False;
  GObjectProperty := False;
end;


procedure TF_SizePos.FormShow(Sender: TObject);
begin
  try
    if GCadForm.PCad.SelectedCount = 1 then
      ClickFigure := TFigure(GCadForm.PCad.Selection[0]);
    DefineObjectSizePos;
    // Name
    if ClickFigure <> nil then
    begin
      edObjectName.Text := ClickFigure.Name;
      Figure_Name := edObjectName.Text;
      // Width
      Figure_Width := edWidth.Text;
      // Height
      Figure_Height := edHeight.Text;
      // Angle
      Figure_Angle := edAngle.Text;
      // X
      Figure_X := edX.Text;
      // Y
      Figure_Y := edY.Text;
      // Z
      Figure_Z := edZ.Text;
    end;
    TForm(Sender).SetFocus;
  except
    on E: Exception do AddExceptionToLogEx('TF_SizePos.FormShow', E.Message);
  end;
end;


procedure TF_SizePos.DefineObjectSizePos;
var
  MapScale: Double;
  Points: TDoublePoint;
begin
  try
    if GCadForm.PCad.Selection.Count = 1 then
    begin
      if ClickFigure = nil then
        ClickFigure := TFigure(GCadForm.PCad.Selection[0]);
      if CheckFigureByClassName(ClickFigure, cTConnectorObject) or CheckFigureByClassName(ClickFigure, cTOrthoLine) then
      begin
        F_SizePos.Height := 305;
        lb_Z.Visible := True;
        lbZ_m.Visible := True;
        edZ.Visible := True;
      end
      else
      begin
        F_SizePos.Height := 270;
        lb_Z.Visible := False;
        lbZ_m.Visible := False;
        edZ.Visible := False;
      end;

      MapScale := GCadForm.PCad.MapScale;
      edX.Text := FormatFloat('0.00', ClickFigure.ActualPoints[1].x * Mapscale / 1000);
      edY.Text := FormatFloat('0.00', ClickFigure.ActualPoints[1].y * Mapscale / 1000);
      // Z координата для СКС-обьектов
      if CheckFigureByClassName(ClickFigure, cTConnectorObject) then
        edZ.Text := FormatFloat('0.00', TConnectorObject(ClickFigure).ActualZOrder[1])
      else
      if CheckFigureByClassName(ClickFigure, cTOrthoLine) then
        edZ.Text := FormatFloat('0.00', TOrthoLine(ClickFigure).ActualZOrder[1]);

      edAngle.Text := FormatFloat('0.00', ClickFigure.AngletoPoint / pi * 180);
      if CheckFigureByClassName(ClickFigure, cTConnectorObject) then
      begin
        edWidth.Text := FormatFloat('0.00', TConnectorObject(ClickFigure).GrpSizeX * MapScale / 1000);
        edHeight.Text := FormatFloat('0.00', TConnectorObject(ClickFigure).GrpSizeY * MapScale / 1000);
      end
      else
      begin
        edWidth.Text := FormatFloat('0.00', abs(ClickFigure.CenterPoint.x - ClickFigure.ActualPoints[1].x) * 2 * MapScale / 1000);
        edHeight.Text := FormatFloat('0.00', abs(ClickFigure.CenterPoint.y - ClickFigure.ActualPoints[1].y) * 2 * MapScale / 1000);
      end;
    end
    else
    begin
      edWidth.Clear;
      edHeight.Clear;
      edAngle.Clear;
      edX.Clear;
      edY.Clear;
      edZ.Clear;
    end;
  except
    on E: Exception do AddExceptionToLogEx('TF_SizePos.DefineObjectSizePos', E.Message);
  end;
end;

procedure TF_SizePos.eXKeyPress(Sender: TObject; var Key: Char);
var
  OldPoints: TDoublePoint;
  NewPoints: TDoublePoint;
  MapScale: Double;
begin
  try
    if GCadForm.PCad.Selection.Count = 1 then
    begin
      if Key = #13 then
      begin
        MapScale := GCadForm.PCad.MapScale;
        OldPoints := ClickFigure.ActualPoints[1];
        NewPoints.x := StrToFloat_My(edX.Text) / MapScale * 1000;
        NewPoints.y := StrToFloat_My(edY.Text) / MapScale * 1000;
        ClickFigure.Move(NewPoints.x - OldPoints.x, NewPoints.y - OldPoints.y);
        RefreshCAD(GCadForm.PCad);
      end;
    end;
  except
    on E: Exception do AddExceptionToLogEx('TF_SizePos.eXKeyPress', E.Message);
  end;
end;

procedure TF_SizePos.edAngleKeyPress(Sender: TObject; var Key: Char);
var
  AngleRad: double;
begin
  try
    if GCadForm.PCad.Selection.Count = 1 then
    begin
      if Key = #13 then
      begin
        AngleRad := StrToFloat_My(edAngle.Text) / 180 * pi;
        ClickFigure.AngletoPoint := AngleRad;
        // перепросчет Width, Height
        RefreshCAD(GCadForm.PCad);
      end;
    end;
  except
    on E: Exception do AddExceptionToLogEx('TF_SizePos.edAngleKeyPress', E.Message);
  end;
end;

procedure TF_SizePos.edWidthKeyPress(Sender: TObject; var Key: Char);
var
  Mapscale: Double;
  OldSize: TDoublePoint;
  NewSize: TDoublePoint;
  AngleRad: Double;
begin
  try
    if GCadForm.PCad.Selection.Count = 1 then
    begin
      if Key = #13 then
      begin
        MapScale := GCadForm.PCad.MapScale;
        // определить старые размеры обьекта
        if CheckFigureByClassName(ClickFigure, cTConnectorObject) then
        begin
          OldSize.x := TConnectorObject(ClickFigure).GrpSizeX;
          OldSize.y := TConnectorObject(ClickFigure).GrpSizeY;
        end
        else
        begin
          OldSize.x := abs(ClickFigure.CenterPoint.x - ClickFigure.ActualPoints[1].x) * 2;
          OldSize.y := abs(ClickFigure.CenterPoint.y - ClickFigure.ActualPoints[1].y) * 2;
        end;
        // определить новые размеры обьекта
        if (OldSize.x <> 0) OR (OldSize.y <> 0) then
        begin
          NewSize.x := StrToFloat_My(edWidth.Text) / Mapscale * 1000;
          NewSize.y := StrToFloat_My(edHeight.Text) / Mapscale * 1000;
        end
        else
        begin
          NewSize.x := 0;
          NewSize.y := 0;
          edWidth.Text := '0';
          edHeight.Text := '0';
        end;
        // изменить размер обьекта
        if (OldSize.x <> 0) OR (OldSize.y <> 0) OR (NewSize.x <> 0) AND (NewSize.y <> 0) then
        begin
          if OldSize.x = 0 then
            OldSize.x := 0.001;
          if OldSize.y = 0 then
            OldSize.y := 0.001;
          ClickFigure.Scale(NewSize.x / OldSize.x, NewSize.y / OldSize.y, ClickFigure.ActualPoints[1]);
        end;
        // перепросчет Angle
        RefreshCAD(GCadForm.PCad);
      end;
    end;
  except
    on E: Exception do AddExceptionToLogEx('TF_SizePos.edWidthKeyPress', E.Message);
  end;
end;

procedure TF_SizePos.edZKeyPress(Sender: TObject; var Key: Char);
begin
  try
    if GCadForm.PCad.Selection.Count = 1 then
    begin
      if Key = #13 then
      begin
        if CheckFigureByClassName(ClickFigure, cTConnectorObject) then
          TConnectorObject(ClickFigure).ActualZOrder[1] := StrToFloat_My(edZ.Text)
        else
        if CheckFigureByClassName(ClickFigure, cTOrthoLine) then
          TOrthoLine(ClickFigure).ActualZOrder[1] := StrToFloat_My(edZ.Text);
        RefreshCAD(GCadForm.PCad);
      end;
    end;
  except
    on E: Exception do AddExceptionToLogEx('TF_SizePos.edZKeyPress', E.Message);
  end;
end;

procedure TF_SizePos.edObjectNameKeyPress(Sender: TObject; var Key: Char);
begin
  try
    if GCadForm.PCad.Selection.Count = 1 then
    begin
      if Key = #13 then
      begin
        ClickFigure.Name := edObjectName.Text;
        // Изменить в менеджере проектов
        SetNewObjectNameInPM(ClickFigure.ID, ClickFigure.Name);
      end;
    end;
  except
    on E: Exception do AddExceptionToLogEx('TF_SizePos.edObjectNameKeyPress', E.Message);
  end;
end;

procedure TF_SizePos.edObjectNameExit(Sender: TObject);
begin
  if edObjectName.Text = '' then
    edObjectName.Text := Figure_Name;
end;

procedure TF_SizePos.edWidthExit(Sender: TObject);
begin
  try
    StrToFloat_My(edWidth.Text);
  except
    ShowMessage(cSizePos_Mes1);
    edWidth.Text := Figure_Width;
    Exit;
  end;
  if StrToFloat_My(edWidth.Text) < 0 then
  begin
    ShowMessage(cSizePos_Mes2);
    edWidth.Text := Figure_Width;
  end;
end;

procedure TF_SizePos.edHeightExit(Sender: TObject);
begin
  try
    StrToFloat_My(edHeight.Text);
  except
    ShowMessage(cSizePos_Mes1);
    edHeight.Text := Figure_Height;
    Exit;
  end;
  if StrToFloat_My(edHeight.Text) < 0 then
  begin
    ShowMessage(cSizePos_Mes2);
    edHeight.Text := Figure_Height;
  end;
end;

procedure TF_SizePos.edAngleExit(Sender: TObject);
begin
  try
    StrToFloat_My(edAngle.Text);
  except
    ShowMessage(cSizePos_Mes1);
    edAngle.Text := Figure_Angle;
    Exit;
  end;
  if StrToFloat_My(edAngle.Text) < 0 then
  begin
    ShowMessage(cSizePos_Mes2);
    edAngle.Text := Figure_Angle;
  end;
end;

procedure TF_SizePos.edXExit(Sender: TObject);
begin
  try
    StrToFloat_My(edX.Text);
  except
    ShowMessage(cSizePos_Mes1);
    edX.Text := Figure_X;
    Exit;
  end;
  if StrToFloat_My(edX.Text) < 0 then
  begin
    ShowMessage(cSizePos_Mes2);
    edX.Text := Figure_X;
  end;
end;

procedure TF_SizePos.edYExit(Sender: TObject);
begin
  try
    StrToFloat_My(edY.Text);
  except
    ShowMessage(cSizePos_Mes1);
    edY.Text := Figure_Y;
    Exit;
  end;
  if StrToFloat_My(edY.Text) < 0 then
  begin
    ShowMessage(cSizePos_Mes2);
    edY.Text := Figure_Y;
  end;
end;

procedure TF_SizePos.edZExit(Sender: TObject);
begin
  try
    StrToFloat_My(edZ.Text);
  except
    ShowMessage(cSizePos_Mes1);
    edZ.Text := Figure_Z;
    Exit;
  end;
  if StrToFloat_My(edZ.Text) < 0 then
  begin
    ShowMessage(cSizePos_Mes2);
    edZ.Text := Figure_Z;
  end;
end;

end.
