unit U_ComponDesignWizard;

interface

uses
  Windows, U_LNG, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, U_BaseCommon, U_SCSComponent, ExtCtrls, RzPanel, RzTabs, RzButton, ActnList, ActnMan,
  cxControls, cxContainer, cxEdit, cxImage, StdCtrls, Buttons, PCPanel,
  PCDrawBox, PCDrawing, PowerCad, DrawObjects, RzSplit, Mask, RzEdit,
  siComp, siLngLnk, cxTextEdit, cxMaskEdit, cxDropDownEdit, ComCtrls,
  ToolWin, cxGraphics, cxLookAndFeels, cxLookAndFeelPainters,
  PlatformDefaultStyleActnCtrls;

const
  MinStep = 0;
  MaxStep = 1;

type
  TF_ComponDesignWizard = class(TForm)
    gbNavigator: TRzGroupBox;
    pcWizard: TRzPageControl;
    btBack: TRzBitBtn;
    btCancel: TRzBitBtn;
    tsStep1: TRzTabSheet;
    tsStep2: TRzTabSheet;
    btNext: TRzBitBtn;
    btPrepare: TRzBitBtn;
    ActionManager1: TActionManager;
    aBack: TAction;
    aNext: TAction;
    aPrepare: TAction;
    Label1: TLabel;
    ImgDesignIcon: TcxImage;
    btChoiceDesignImage: TSpeedButton;
    btClearDesignImage: TSpeedButton;
    aChoiceDesignImage: TAction;
    aClearDesignImage: TAction;
    PCad: TPowerCad;
    RzPanel1: TRzPanel;
    RzGroupBox1: TRzGroupBox;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    edLeftBound: TRzNumericEdit;
    edTopBound: TRzNumericEdit;
    edRightBound: TRzNumericEdit;
    edBottomBound: TRzNumericEdit;
    RzGroupBox2: TRzGroupBox;
    Label6: TLabel;
    Label7: TLabel;
    edHeight: TRzNumericEdit;
    edWidth: TRzNumericEdit;
    VerScroll: TScrollBar;
    HorScroll: TScrollBar;
    lng_Forms: TsiLangLinked;
    Label8: TLabel;
    Label9: TLabel;
    RzNumericEdit1: TRzNumericEdit;
    SpeedButton2: TSpeedButton;
    SpeedButton1: TSpeedButton;
    procedure aBackExecute(Sender: TObject);
    procedure aNextExecute(Sender: TObject);
    procedure aPrepareExecute(Sender: TObject);
    procedure aChoiceDesignImageExecute(Sender: TObject);
    procedure aClearDesignImageExecute(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure gbNavigatorResize(Sender: TObject);
    procedure PCadScaleChanged(Sender: TObject);
    procedure FormResize(Sender: TObject);
    procedure HorScrollScroll(Sender: TObject; ScrollCode: TScrollCode;
      var ScrollPos: Integer);
    procedure VerScrollScroll(Sender: TObject; ScrollCode: TScrollCode;
      var ScrollPos: Integer);
    procedure FormMouseWheelDown(Sender: TObject; Shift: TShiftState;
      MousePos: TPoint; var Handled: Boolean);
    procedure FormMouseWheelUp(Sender: TObject; Shift: TShiftState;
      MousePos: TPoint; var Handled: Boolean);
    procedure FormShow(Sender: TObject);
    procedure RzNumericEdit1DblClick(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
  private
    FBitmapStream: TMemoryStream;
    FBlkStream: TMemoryStream;
    FDesignWizardParams: TComponDesignWizardParams;
    FStepIndex: Integer;

    FForm: TForm;
    FSpravochnik: TSpravochnik;
    FUOM: Integer;

  public
    function ExecuteWizard(var AParams: TComponDesignWizardParams; AForm: TForm; ASpravochnik: TSpravochnik): Boolean;
    procedure LoadBoundsFromWiardToParams;
    procedure LoadImage;
    procedure LoadStreams;
    procedure DoStep(AIndex: Integer);
    procedure PutBlkStreamOnCAD;
    // === ScrollBars ===
    Function IfVisibleHorScrollBar: Boolean;
    Function ifVisibleVerScrollBar: Boolean;
    // максимальная позиция скроллов PCAD
    Function GetMaxScrollsPosition: TPoint;
    // получение размера скроллов PCAD
    Function GetPageSizesScrolls: TPoint;
    // установка позиции скроллов PCAD
    Procedure Set_PCad_HorScroll;
    Procedure Set_PCad_VerScroll;
    // установка позиции наших скроллов
    Procedure Set_SCS_HorScroll;
    Procedure Set_SCS_VerScroll;
    // изменение скролов при изменении размеров листа КАД
    Procedure ChangeScrollsOnChangeListSize;
  end;

var
  F_ComponDesignWizard: TF_ComponDesignWizard;

implementation
Uses U_Main, Unit_DM_SCS, PCTypesUtils, U_Common, U_Constants, U_ProtectionCommon;

{$R *.dfm}

{ TF_ComponDesignWizard }

function TF_ComponDesignWizard.ExecuteWizard(var AParams: TComponDesignWizardParams;
  AForm: TForm; ASpravochnik: TSpravochnik): Boolean;
begin
  Result := false;
  FDesignWizardParams := AParams;
  try
    FForm := AForm;
    FUOM := TF_Main(AForm).FUOM;
    FSpravochnik := ASpravochnik;

    LoadStreams;
    DoStep(0);
    if ShowModal = mrOk then
    begin
      LoadBoundsFromWiardToParams;
      AParams := FDesignWizardParams;
      Result := true;
    end;
  finally
    if Assigned(FBitmapStream) then
      FreeAndNil(FBitmapStream);
    if Assigned(FBlkStream) then
      FreeAndNil(FBlkStream);
  end;
end;

procedure TF_ComponDesignWizard.LoadBoundsFromWiardToParams;
begin
  try
    FDesignWizardParams.Height := FloatInUOM(edHeight.Value, FUOM, umM);
    FDesignWizardParams.Width := FloatInUOM(edWidth.Value, FUOM, umM);
    FDesignWizardParams.LeftBound := FloatInUOM(edLeftBound.Value, FUOM, umM);
    FDesignWizardParams.TopBound := FloatInUOM(edTopBound.Value, FUOM, umM);
    FDesignWizardParams.BottomBound := FloatInUOM(edBottomBound.Value, FUOM, umM);
    FDesignWizardParams.RightBound := FloatInUOM(edRightBound.Value, FUOM, umM);
  except
    on E: Exception do AddExceptionToLogEx('TF_ComponDesignWizard.LoadBoundsFromWiardToParams', E.Message);
  end;
end;

procedure TF_ComponDesignWizard.LoadImage;
begin
  try
    ClearcxImage(ImgDesignIcon);
    if Assigned(FBitmapStream) then
    begin
      ImgDesignIcon.Picture.Bitmap.LoadFromStream(FBitmapStream);
      FBitmapStream.Position := 0;
    end;
  except
    on E: Exception do AddExceptionToLogEx('TF_ComponDesignWizard.LoadImage', E.Message);
  end;
end;

procedure TF_ComponDesignWizard.LoadStreams;
begin
  try
    //loadObjectIconToCXImage(ImgDesignIcon, FDesignWizardParams.IDDesignIcon, oitProjectible);
    with F_NormBase.DM do
    begin
      if Assigned(FBitmapStream) then
        FreeAndNil(FBitmapStream);
      if Assigned(FBlkStream) then
        FreeAndNil(FBlkStream);

      if TF_Main(FForm).GDBMode = bkNormBase then
      begin
        FBitmapStream := GetComponIconByIconType(FDesignWizardParams.IDDesignIcon, FDesignWizardParams.ObjectSignType, ieBMP);
        FBlkStream := GetComponIconByIconType(FDesignWizardParams.IDDesignIcon, FDesignWizardParams.ObjectSignType, ieBLK);
      end
      else
      if (TF_Main(FForm).GDBMode = bkProjectManager) and (FSpravochnik <> nil) then
      begin
        FBitmapStream := FSpravochnik.GetObjectIconByIconType(FDesignWizardParams.GUIDDesignIcon, FDesignWizardParams.ObjectSignType, ieBMP);
        FBlkStream := FSpravochnik.GetObjectIconByIconType(FDesignWizardParams.GUIDDesignIcon, FDesignWizardParams.ObjectSignType, ieBLK);
      end;
    end;
  except
    on E: Exception do AddExceptionToLogEx('TF_ComponDesignWizard.LoadStreams', E.Message);
  end;
end;

procedure TF_ComponDesignWizard.DoStep(AIndex: Integer);
begin
  try
    FStepIndex := AIndex;
    pcWizard.ActivePageIndex := AIndex;

    case AIndex of
      0:
        LoadImage;
      1: begin
           edHeight.Value := FloatInUOM(FDesignWizardParams.Height, umM, FUOM);
           edWidth.Value := FloatInUOM(FDesignWizardParams.Width, umM, FUOM);
           edLeftBound.Value := FloatInUOM(FDesignWizardParams.LeftBound, umM, FUOM);
           edTopBound.Value := FloatInUOM(FDesignWizardParams.TopBound, umM, FUOM);
           edBottomBound.Value := FloatInUOM(FDesignWizardParams.BottomBound, umM, FUOM);
           edRightBound.Value := FloatInUOM(FDesignWizardParams.RightBound, umM, FUOM);
           PutBlkStreamOnCAD;
         end;
    end;

    //*** Навигаток
    aBack.Enabled := true;
    aNext.Visible := true;
    aPrepare.Visible := true;
    case AIndex of
      MinStep:
      begin
        aBack.Enabled := false;
        aPrepare.Visible := false;
      end;
      MaxStep:
      begin
        aNext.Visible := false;
        aPrepare.Visible := true;
      end;
    end;
    gbNavigatorResize(gbNavigator);
  except
    on E: Exception do AddExceptionToLogEx('TF_ComponDesignWizard.DoStep', E.Message);
  end;
end;

procedure TF_ComponDesignWizard.aBackExecute(Sender: TObject);
begin
  try
    LoadBoundsFromWiardToParams;
    DoStep(FStepIndex - 1);
  except
    on E: Exception do AddExceptionToLogEx('TF_ComponDesignWizard.aBackExecute', E.Message);
  end;
end;

procedure TF_ComponDesignWizard.aNextExecute(Sender: TObject);
begin
  try
    if FDesignWizardParams.IDDesignIcon > 0 then
      DoStep(FStepIndex + 1)
    else
      ShowMessageByType(Self.Handle, smtDisplay, cComponDesignWizard_Mes1, Application.Title, MB_ICONINFORMATION or MB_OK);
  except
    on E: Exception do AddExceptionToLogEx('TF_ComponDesignWizard.aNextExecute', E.Message);
  end;
end;

procedure TF_ComponDesignWizard.aPrepareExecute(Sender: TObject);
begin
    //
end;

procedure TF_ComponDesignWizard.aChoiceDesignImageExecute(Sender: TObject);
begin
  try
    if TF_Main(FForm).GDBMode = bkNormBase then
      FDesignWizardParams.IDDesignIcon := F_NormBase.DM.GetIDObjectIconFromGuide(FDesignWizardParams.IDDesignIcon)
    else
    if TF_Main(FForm).GDBMode = bkProjectManager then
    begin
      if ShowCurrProjectProperties(vkObjectIcons, FDesignWizardParams.GUIDDesignIcon) then
        if FSpravochnik.LastObjectIcon <> nil then
        begin
          FDesignWizardParams.GUIDDesignIcon := FSpravochnik.LastObjectIcon.GUID;
          FDesignWizardParams.IDDesignIcon := FSpravochnik.LastObjectIcon.ID;
        end;
    end;
    LoadStreams;
    LoadImage;

    {with F_NormBase do
    begin
      F_CaseForm.GFormMode := fmMake;
      F_CaseForm.GViewKind := vkObjectIcons;
      if F_CaseForm.ShowModal = mrOk then
      begin
        FDesignWizardParams.IDDesignIcon := DM.DataSet_OBJECT_ICONS.FN(fnID).AsInteger;
        LoadStreams;
        LoadImage;
      end;
    end;}
  except
    on E: Exception do AddExceptionToLogEx('TF_ComponDesignWizard.aChoiceDesignImageExecute', E.Message);
  end;
end;

procedure TF_ComponDesignWizard.aClearDesignImageExecute(Sender: TObject);
begin
  try
    ClearcxImage(ImgDesignIcon);
    FDesignWizardParams.IDDesignIcon := 0;
  except
    on E: Exception do AddExceptionToLogEx('TF_ComponDesignWizard.aClearDesignImageExecute', E.Message);
  end;
end;

procedure TF_ComponDesignWizard.FormCreate(Sender: TObject);
begin
  try
    FBitmapStream := nil;
    FBlkStream := nil;

    edHeight.DisplayFormat := GetDisplayFormatForFloat;
    edWidth.DisplayFormat := GetDisplayFormatForFloat;
    edLeftBound.DisplayFormat := GetDisplayFormatForFloat;
    edTopBound.DisplayFormat := GetDisplayFormatForFloat;
    edBottomBound.DisplayFormat := GetDisplayFormatForFloat;
    edRightBound.DisplayFormat := GetDisplayFormatForFloat;
    PCad.OnMouseWheel := nil;
  except
    on E: Exception do AddExceptionToLogEx('TF_ComponDesignWizard.FormCreate', E.Message);
  end;
end;

procedure TF_ComponDesignWizard.PutBlkStreamOnCAD;
var
  FileName: String;
  BlockFig: TBlock;
  aWidth, aHeight: Double;
begin
  try
    PCad.Clear(0);
    if FBlkStream <> nil then
    begin
      {$if Defined(ES_GRAPH_SC)}
        FileName := ExeDir + '\.blk\TempStream.pwb';
      {$else}
        FileName := ExtractFileDir(Application.ExeName) + '\.blk\TempStream.pwb';
      {$ifend}
      FBlkStream.SaveToFile(FileName);
      BlockFig := TBlock(PCad.InsertBlockwithFileName(1, FileName, 0, 0));
      aWidth := BlockFig.GetBoundRect.Right - BlockFig.GetBoundRect.Left;
      aHeight := BlockFig.GetBoundRect.Bottom - BlockFig.GetBoundRect.Top;
      PCad.WorkWidth := aWidth;
      PCad.WorkHeight := aHeight;
      BlockFig.move(aWidth / 2, aHeight / 2);
      if BlockFig.Selected then
        BlockFig.Deselect;
      BlockFig.LockModify := True;
      BlockFig.LockMove := True;
    end
    else
      Showmessage(cComponDesignWizard_Mes2);
  except
    on E: Exception do AddExceptionToLogEx('TF_ComponDesignWizard.PutBlkStreamOnCAD', E.Message);
  end;
end;

procedure TF_ComponDesignWizard.gbNavigatorResize(Sender: TObject);
begin
  try
    SetMiddleControlChilds(TControl(Sender), TControl(Self));
  except
    on E: Exception do AddExceptionToLogEx('TF_ComponDesignWizard.gbNavigatorResize', E.Message);
  end;
end;

function TF_ComponDesignWizard.IfVisibleHorScrollBar: Boolean;
var
  Client_Width: Integer;
  Page: TRect;
begin
  try
    Result := false;
    Client_Width := PCad.ClientWidth;
    Page := PCad.GetPageRect;
    if (Page.Left < 0) or (Page.Right > Client_Width) then
      Result := True
    else
      Result := False;
  except
    on E: Exception do AddExceptionToLogEx('TF_ComponDesignWizard.IfVisibleHorScrollBar', E.Message);
  end;
end;

function TF_ComponDesignWizard.IfVisibleVerScrollBar: Boolean;
var
  Client_Height: Integer;
  Page: TRect;
begin
  try
    Result := false;
    Client_Height := PCad.ClientHeight;
    Page := PCad.GetPageRect;
    if (Page.Top < 0) or (Page.Bottom > Client_Height) then
      Result := True
    else
      Result := False;
  except
    on E: Exception do AddExceptionToLogEx('TF_ComponDesignWizard.IfVisibleVerScrollBar', E.Message);
  end;
end;

function TF_ComponDesignWizard.GetMaxScrollsPosition: TPoint;
var
  Page: TRect;
  PageX, PageY: Integer;
  ClientX, ClientY: Integer;
begin
  try
    Result := Point(0, 0);
    Page := PCad.GetPageRect;
    PageX := abs(Page.Right - Page.Left);
    PageY := abs(Page.Bottom - page.Top);
    ClientX := PCad.ClientWidth;
    ClientY := PCad.ClientHeight;
    Result.x := PageX - ClientX + 59;
    Result.y := PageY - ClientY + 59;
  except
    on E: Exception do AddExceptionToLogEx('TF_ComponDesignWizard.GetMaxScrollsPosition', E.Message);
  end;
end;

function TF_ComponDesignWizard.GetPageSizesScrolls: TPoint;
var
  Page: TRect;
  PageX, PageY: Integer;
  ClientX, ClientY: Integer;
begin
  try
    Result := Point(0, 0);
    Page := PCad.GetPageRect;
    PageX := abs(Page.Right - Page.Left);
    PageY := abs(Page.Bottom - page.Top);
    ClientX := PCad.ClientWidth;
    ClientY := PCad.ClientHeight;
    Result.x := Round(ClientX / PageX * 100);
    Result.y := Round(ClientY / PageY * 100);
  except
    on E: Exception do AddExceptionToLogEx('TF_ComponDesignWizard.GetPageSizesScrolls', E.Message);
  end;
end;

procedure TF_ComponDesignWizard.Set_PCad_HorScroll;
var
  Koef_ScrollPos_X: Double;
  MaxCADScroll_X: Integer;
  SetScrollPos_X: Integer;
begin
  try
    // позиция CAD
    MaxCADScroll_X := GetMaxScrollsPosition.X;
    // Sets
    if (HorScroll.Max - HorScroll.PageSize) > 0 then
    begin
      Koef_ScrollPos_X := HorScroll.Position / (HorScroll.Max - HorScroll.PageSize);
      SetScrollPos_X := round(MaxCADScroll_X * Koef_ScrollPos_X);
      PCad.SetHScrollPosition(SetScrollPos_X, True);
    end;
  except
    on E: Exception do AddExceptionToLogEx('TF_ComponDesignWizard.Set_PCad_HorScroll', E.Message);
  end;
end;

procedure TF_ComponDesignWizard.Set_PCad_VerScroll;
var
  Koef_ScrollPos_Y: Double;
  MaxCADScroll_Y: Integer;
  SetScrollPos_Y: Integer;
begin
  try
    // позиция CAD
    MaxCADScroll_Y := GetMaxScrollsPosition.Y;
    // Sets
    if (VerScroll.Max - VerScroll.PageSize) > 0 then
    begin
      Koef_ScrollPos_Y := VerScroll.Position / (VerScroll.Max - VerScroll.PageSize);
      SetScrollPos_Y := round(MaxCADScroll_Y * Koef_ScrollPos_Y);
      PCad.SetVScrollPosition(SetScrollPos_Y, True);
    end;
  except
    on E: Exception do AddExceptionToLogEx('TF_ComponDesignWizard.Set_PCad_VerScroll', E.Message);
  end;
end;

procedure TF_ComponDesignWizard.Set_SCS_HorScroll;
var
  MaxCADScroll_X: Integer;
  CurScrollPos_X: Integer;
begin
  try
    // позиция CAD
    CurScrollPos_X := PCad.HSCBarPosition;
    MaxCADScroll_X := GetMaxScrollsPosition.X;
    // Sets
    HorScroll.PageSize := GetPageSizesScrolls.X;
    if MaxCADScroll_X > 0 then
    begin
      HorScroll.Position := round(CurScrollPos_X / MaxCADScroll_X * (HorScroll.Max - HorScroll.PageSize + 1));
      try
        if Self.Visible then
          PCad.SetFocus;
      except
      end;
    end;
  except
    on E: Exception do AddExceptionToLogEx('TF_ComponDesignWizard.Set_SCS_HorScroll', E.Message);
  end;
end;

procedure TF_ComponDesignWizard.Set_SCS_VerScroll;
var
  MaxCADScroll_Y: Integer;
  CurScrollPos_Y: Integer;
begin
  try
    // позиция CAD
    CurScrollPos_Y := PCad.VSCBarPosition;
    MaxCADScroll_Y := GetMaxScrollsPosition.Y;
    // Sets pos
    VerScroll.PageSize := GetPageSizesScrolls.Y;
    if MaxCADScroll_Y > 0 then
    begin
      VerScroll.Position := round(CurScrollPos_Y / MaxCADScroll_Y * (VerScroll.Max - VerScroll.PageSize + 1));
      try
        if self.Visible then
          PCad.SetFocus;
      except
      end;
    end;
  except
    on E: Exception do AddExceptionToLogEx('TF_ComponDesignWizard.Set_SCS_VerScroll', E.Message);
  end;
end;

procedure TF_ComponDesignWizard.ChangeScrollsOnChangeListSize;
begin
  try
    PCad.AutoRefresh := False;

    // Гориз. скролл есть
    if IfVisibleHorScrollBar then
    begin
      HorScroll.Visible := True;
      Set_SCS_HorScroll;
    end
    else
      HorScroll.Visible := False;
    // Вертик. скролл есть
    if ifVisibleVerScrollBar then
    begin
      VerScroll.Visible := True;
      Set_SCS_VerScroll;
    end
    else
      VerScroll.Visible := False;
    // подредактировать скролбары

    // только гориз.
    if IfVisibleHorScrollBar and not ifVisibleVerScrollBar then
      if HorScroll.Width <> (PCad.Width - 7) then
        HorScroll.Width := PCad.Width - 7;
    // только вертик.
    if not IfVisibleHorScrollBar and ifVisibleVerScrollBar then
      if VerScroll.Height <> (PCad.Height - 7) then
      VerScroll.Height := PCad.Height - 7;
    // оба
    if IfVisibleHorScrollBar and ifVisibleVerScrollBar then
    begin
      if HorScroll.Width <> (PCad.Width - 15 - 7) then
        HorScroll.Width := PCad.Width - 15 - 7;
      if VerScroll.Height <> (PCad.Height - 15 - 7) then
        VerScroll.Height := PCad.Height - 15 - 7;
    end;
    PCad.AutoRefresh := True;
    RefreshCAD_T(PCad);
  except
    on E: Exception do AddExceptionToLogEx('TF_ComponDesignWizard.ChangeScrollsOnChangeListSize', E.Message);
  end;
end;



procedure TF_ComponDesignWizard.PCadScaleChanged(Sender: TObject);
var
  NewScrollPosX: Double;
  NewScrollPosY: Double;
  SetScrollPosX: Integer;
  SetScrollPosY: Integer;
  NewZoomScale: Integer;
  CalcedZoomKoef: Double;
  CalcedZoomDelta: Double;
begin
  try
    // Гориз. скролл есть
    NewZoomScale := PCad.ZoomScale;
    CalcedZoomKoef := (GSavedZoomScale / NewZoomScale);
    CalcedZoomDelta := (NewZoomScale - GSavedZoomScale);
    if IfVisibleHorScrollBar then
    begin
      // CORRECT!!!
      if PCad.SelectedCount = 0 then
      begin
        SetScrollPosX := Round(GSavedScrollPosX / CalcedZoomKoef + 304 / GSavedZoomScale * CalcedZoomDelta);
        PCad.SetHScrollPosition(SetScrollPosX, PCad.AutoRefresh);
      end;
      HorScroll.Visible := True;
      Set_SCS_HorScroll;
    end
    else
      HorScroll.Visible := False;
    // Вертик. скролл есть
    if ifVisibleVerScrollBar then
    begin
      if PCad.SelectedCount = 0 then
      begin
        SetScrollPosY := Round(GSavedScrollPosY / CalcedZoomKoef + 268 / GSavedZoomScale * CalcedZoomDelta);
        PCad.SetVScrollPosition(SetScrollPosY, PCad.AutoRefresh);
      end;
      VerScroll.Visible := True;
      Set_SCS_VerScroll;
    end
    else
      VerScroll.Visible := False;
    // подредактировать скролбары
    // только гориз.
   if IfVisibleHorScrollBar and not ifVisibleVerScrollBar then
      if HorScroll.Width <> (PCad.Width - 7) then
        HorScroll.Width := PCad.Width - 7;
    // только вертик.
    if not IfVisibleHorScrollBar and ifVisibleVerScrollBar then
      if VerScroll.Height <> (PCad.Height - 7) then
      VerScroll.Height := PCad.Height - 7;
    // оба
    if IfVisibleHorScrollBar and ifVisibleVerScrollBar then
    begin
      if HorScroll.Width <> (PCad.Width - 15 - 7) then
        HorScroll.Width := PCad.Width - 15 - 7;
      if VerScroll.Height <> (PCad.Height - 15 - 7) then
        VerScroll.Height := PCad.Height - 15 - 7;
    end;
  except
    on E: Exception do AddExceptionToLogEx('TF_ComponDesignWizard.PCadScaleChanged', E.Message);
  end;
end;

procedure TF_ComponDesignWizard.FormResize(Sender: TObject);
begin
  try
    ChangeScrollsOnChangeListSize;
  except
    on E: Exception do AddExceptionToLogEx('TF_ComponDesignWizard.FormResize', E.Message);
  end;
end;

procedure TF_ComponDesignWizard.HorScrollScroll(Sender: TObject;
  ScrollCode: TScrollCode; var ScrollPos: Integer);
begin
  try
    if ScrollCode = scEndScroll then
      Set_PCad_HorScroll;
    PCad.SetFocus;
  except
    on E: Exception do AddExceptionToLogEx('TF_ComponDesignWizard.HorScrollScroll', E.Message);
  end;
end;

procedure TF_ComponDesignWizard.VerScrollScroll(Sender: TObject;
  ScrollCode: TScrollCode; var ScrollPos: Integer);
begin
  try
    if ScrollCode = scEndScroll then
      Set_PCad_VerScroll;
    PCad.SetFocus;
  except
    on E: Exception do AddExceptionToLogEx('TF_ComponDesignWizard.VerScrollScroll', E.Message);
  end;
end;

procedure TF_ComponDesignWizard.FormMouseWheelDown(Sender: TObject;
  Shift: TShiftState; MousePos: TPoint; var Handled: Boolean);
var
  X, Y: Integer;
begin
  try
    Handled := True;
    PCad.AutoRefresh := False;
    // Масштаб
    if ssCtrl in Shift then
    begin
      PCad.ZoomScale := PCad.ZoomScale - 5;
      if PCad.AutoRefresh then
        RefreshCAD(PCad);
    end
    else
    // Scrolls (Horiz)
    if (ssShift in Shift) and not (ssCtrl in Shift) then
    begin
      X := PCad.HSCBarPosition;
      PCad.SetHScrollPosition(X + 10, True);
    end
    else
    // Scrolls (Vert)
    if Shift = [] then
    begin
      Y := PCad.VSCBarPosition;
      PCad.SetVScrollPosition(Y + 10, True);
    end;
    // сколл
    Set_SCS_HorScroll;
    Set_SCS_VerScroll;
    PCad.AutoRefresh := True;
    RefreshCAD_T(PCad);
  except
    on E: Exception do AddExceptionToLogEx('TF_ComponDesignWizard.FormMouseWheelDown', E.Message);
  end;
end;

procedure TF_ComponDesignWizard.FormMouseWheelUp(Sender: TObject;
  Shift: TShiftState; MousePos: TPoint; var Handled: Boolean);
var
  X, Y: Integer;
begin
  try
    Handled := True;
    PCad.AutoRefresh := False;
    // Масштаб
    if ssCtrl in Shift then
    begin
      PCad.ZoomScale := PCad.ZoomScale + 5;
      if PCad.AutoRefresh then
        RefreshCAD(PCad);
    end
    else
    // Scrolls (Horiz)
    if (ssShift in Shift) and not (ssCtrl in Shift) then
    begin
      X := PCad.HSCBarPosition;
      PCad.SetHScrollPosition(X - 10, True);
    end
    else
    // Scrolls (Vert)
    if Shift = [] then
    begin
      Y := PCad.VSCBarPosition;
      PCad.SetVScrollPosition(Y - 10, True);
    end;
    // скролл
    Set_SCS_HorScroll;
    Set_SCS_VerScroll;
    PCad.AutoRefresh := True;
    RefreshCAD_T(PCad);
  except
    on E: Exception do AddExceptionToLogEx('TF_ComponDesignWizard.FormMouseWheelUp', E.Message);
  end;
end;

procedure TF_ComponDesignWizard.FormShow(Sender: TObject);
begin
  try
    if assigned(GCadForm) then
    begin
      SpeedButton1.Visible := True;
      SpeedButton2.Visible := True;
    end
    else
    begin
      SpeedButton1.Visible := False;
      SpeedButton2.Visible := False;
    end;

    // система измерений
    if (FUOM = umSM) or (FUOM = umM) then
      PCad.RulerSystem := rsMetric
    else
    if (FUOM = umIn) or (FUOM = umFt) then
      PCad.RulerSystem := rsWhitworth;

    if FUOM = umSM then
    begin
      Label8.Caption := cMetric_sm;
      Label9.Caption := cMetric_sm;
    end;
    if FUOM = umM then
    begin
      Label8.Caption := cMetric_m;
      Label9.Caption := cMetric_m;
    end;
    if FUOM = umIn then
    begin
      Label8.Caption := cWhitworth_in;
      Label9.Caption := cWhitworth_in;
    end;
    if FUOM = umFt then
    begin
      Label8.Caption := cWhitworth_ft;
      Label9.Caption := cWhitworth_ft;      
    end;
  except
    on E: Exception do AddExceptionToLogEx('', E.Message);
  end;
end;

procedure TF_ComponDesignWizard.RzNumericEdit1DblClick(Sender: TObject);
begin
  PCad.MapScale := RzNumericEdit1.Value;
end;

procedure TF_ComponDesignWizard.SpeedButton1Click(Sender: TObject);
begin
  PCad.SetTool(toFigure, 'TSCSHDimLine');
end;

procedure TF_ComponDesignWizard.SpeedButton2Click(Sender: TObject);
begin
  PCad.SetTool(toFigure, 'TSCSVDimLine');
end;

end.
