unit U_MakeEditComponentType;

interface

uses
  Windows, U_LNG, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, RzPanel,

  U_BaseCommon, U_BaseConstants, cxTextEdit, cxControls, cxContainer, cxEdit, cxGroupBox,
  cxRadioGroup, StdCtrls, cxLookAndFeelPainters, cxButtons, ActnList,
  ActnMan, RzButton, Buttons, cxImage, cxMaskEdit, cxSpinEdit, kbmMemTable,
  cxDropDownEdit, DB, cxLookupEdit, cxDBLookupEdit, cxDBLookupComboBox, cxLookupDBGrid,
  siComp, siLngLnk, RzRadGrp, RzRadChk, cxGraphics, cxLookAndFeels, Menus,
  PlatformDefaultStyleActnCtrls;

type
  TF_MakeEditComponentType = class(TForm)
    RzGroupBox1: TRzGroupBox;
    Label_Name: TLabel;
    Edit_Name: TcxTextEdit;
    RzPanel1: TRzPanel;
    Button_OK: TcxButton;
    Button_Cancel: TcxButton;
    ActionManager1: TActionManager;
    Act_CancelByEsc: TAction;
    Label1: TLabel;
    Edit_NamePlural: TcxTextEdit;
    gbDesignIcon: TRzGroupBox;
    btChoiceDesignImage: TSpeedButton;
    btClearDesignImage: TSpeedButton;
    ImgDesignIcon: TcxImage;
    Label2: TLabel;
    RzGroupBox3: TRzGroupBox;
    Label_MarkMask: TLabel;
    lbCoordZ: TLabel;
    Edit_MarkMask: TcxTextEdit;
    Button_EditMarkMask: TcxButton;
    seCoordZ: TcxSpinEdit;
    Label4: TLabel;
    lbSysName: TcxLookupComboBox;
    mtSysNames: TkbmMemTable;
    dsSysNames: TDataSource;
    pnComponTemplate: TRzPanel;
    Label5: TLabel;
    btClearComponTemplate: TSpeedButton;
    edComponTemplate: TcxTextEdit;
    btChoiceComponTemplate: TcxButton;
    lbRoomHeight: TLabel;
    Label6: TLabel;
    lng_Forms: TsiLangLinked;
    RadioGroup_PortKind: TRzRadioGroup;
    RadioGroup_ActiveState: TRzRadioGroup;
    RadioGroup_IsLine: TRzRadioGroup;
    cbCanUseAsPoint: TRzCheckBox;
    RzRadioGroup1: TRzRadioGroup;
    seNewComponentIndex: TcxSpinEdit;
    ProjArea: TRzRadioButton;
    ListArea: TRzRadioButton;
    BeginFrom: TRzRadioButton;
    procedure Button_OKClick(Sender: TObject);
    procedure Edit_NamePropertiesChange(Sender: TObject);
    procedure RadioGroup_PortKindPropertiesChange(Sender: TObject);
    procedure RadioGroup_ActiveStateClick(Sender: TObject);
    procedure RadioGroup_IsLineClick(Sender: TObject);
    procedure Act_CancelByEscExecute(Sender: TObject);
    procedure Button_EditMarkMaskClick(Sender: TObject);
    procedure btChoiceDesignImageClick(Sender: TObject);
    procedure btClearDesignImageClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure btChoiceComponTemplateClick(Sender: TObject);
    procedure btClearComponTemplateClick(Sender: TObject);
    procedure BeginFromClick(Sender: TObject);
    procedure ListAreaClick(Sender: TObject);
    procedure ProjAreaClick(Sender: TObject);
  private
    GWasChanging: Boolean;

    FCompTypeID: Integer;
    FCompTypeGUID: String;
    FIDComponentTemplate: Integer;
    FOldIDComponentTemplate: Integer;
    FRoomHeight: Double;
    FDesignParams: TComponDesignWizardParams;
    procedure EnableControl(AEnable: Boolean);
    procedure SetControls;
  public
    GForm: TForm;
    GFormMode: TFormMode;
    GID: Integer;

    procedure LoadImage;
    function MakeEditComponentType(AFormMode: TFormMode; AMemTable: TkbmMemTable = nil; ARoomHeight: Double = -1): Boolean;

    constructor Create(AOwner: TComponent; AForm: TForm);
    destructor  Destroy; override;
  end;

//var
//  F_MakeEditComponentType: TF_MakeEditComponentType;

implementation
Uses U_Main, Unit_DM_SCS, U_Common, U_ComponDesignWizard, U_SCSComponent;

{$R *.dfm}

{ TF_MakeEditComponentType }

constructor TF_MakeEditComponentType.Create(AOwner: TComponent;
  AForm: TForm);
begin
  GForm := AForm;
  Inherited Create(AOwner);
end;

destructor TF_MakeEditComponentType.Destroy;
begin
  inherited;
end;


procedure TF_MakeEditComponentType.EnableControl(AEnable: Boolean);
begin
  RadioGroup_PortKind.Enabled := AEnable;
  RadioGroup_ActiveState.Enabled := AEnable;
  RadioGroup_IsLine.Enabled := AEnable;
  cbCanUseAsPoint.Enabled := AEnable;
end;

procedure TF_MakeEditComponentType.SetControls;
begin
  cbCanUseAsPoint.Visible := (RadioGroup_IsLine.ItemIndex = biTrue);
end;

function TF_MakeEditComponentType.MakeEditComponentType(
  AFormMode: TFormMode; AMemTable: TkbmMemTable = nil; ARoomHeight: Double = -1): Boolean;
var
  ModRes: TModalResult;
  ComponType: TComponentType;
  NBComponenType: TNBComponentType;
  MakeEdit: TMakeEdit;
begin
  Result := false;
  GID := -1;
  try
    GFormMode := AFormMode;
    MakeEdit := GetMakeEditByFormMode(AFormMode);


    GWasChanging := False;

    FDesignParams.BottomBound := 0;
    FDesignParams.LeftBound := 0;
    FDesignParams.RightBound := 0;
    FDesignParams.TopBound := 0;
    //ImgDesignIcon.Picture.Bitmap.FreeImage;


    FIDComponentTemplate := 0;
    FOldIDComponentTemplate := 0;
    edComponTemplate.Text := '';

    btClearDesignImageClick(btClearDesignImage);

    FCompTypeGUID := '';
    FCompTypeID := 0;
    FRoomHeight := ARoomHeight;

    lbCoordZ.Caption := cMakeEditComponentType_Msg9+', '+GetNameUOM(TF_Main(GForm).FUOM, true);
    //lbCoordZUOM.Caption := GetNameUOM(TF_Main(GForm).FUOM, true);
    lbRoomHeight.Visible := FRoomHeight <> -1;
    //lbRoomHeight.Caption := cMakeEditComponentType_Msg1_1+' '+FloatToStr(Round2(FRoomHeight))+' '+cMakeEditComponentType_Msg1_2;
    lbRoomHeight.Caption := cMakeEditComponentType_Msg1_1+' '+
                            FloatToStr(Round2(FloatInUOM(FRoomHeight, umMetr, TF_Main(GForm).FUOM)))+ ')';
    if FRoomHeight <> -1 then
      seCoordZ.Properties.MaxValue := FloatInUOM(FRoomHeight, umMetr, TF_Main(GForm).FUOM)
    else
      seCoordZ.Properties.MaxValue := 1E20;

    pnComponTemplate.Visible := (GFormMode = fmEdit) and (TF_Main(GForm).GDBMode = bkNormBase);
    pnComponTemplate.Enabled := GUseComponTemplates;

    seCoordZ.Value := -1;
    case GFormMode of
      fmMake:
        begin
          Caption := cMakeEditComponentType_Msg2_1;

          Edit_Name.Text := '';
          Edit_NamePlural.Text := '';
          lbSysName.Text := '';
          RadioGroup_PortKind.ItemIndex := -1;
          RadioGroup_ActiveState.ItemIndex := -1;
          RadioGroup_IsLine.ItemIndex := -1;
          cbCanUseAsPoint.Checked := false;
          Edit_MarkMask.Text := '#l.#c#s';
          seCoordZ.Value := -1;

          seNewComponentIndex.Value := 0;

          
          FDesignParams.IDDesignIcon := 0;
          FDesignParams.GUIDDesignIcon := '';
          EnableControl(True);
        end;
      fmEdit:
        with TF_Main(GForm).DM do
        begin
          Caption := cMakeEditComponentType_Msg2_2;

          if AMemTable <> nil then
          begin
            FCompTypeID := AMemTable.FieldByName(fnID).AsInteger;

            Edit_Name.Text := AMemTable.FieldByName(fnName).AsString;
            Edit_NamePlural.Text := AMemTable.FieldByName('NAME_PLURAL').AsString;
            lbSysName.Text := AMemTable.FieldByName(fnSysName).AsString;
            RadioGroup_PortKind.ItemIndex := AMemTable.FieldByName('PORT_KIND').AsInteger;
            RadioGroup_ActiveState.ItemIndex := AMemTable.FieldByName('ACTIVE_STATE').AsInteger;
            RadioGroup_IsLine.ItemIndex := AMemTable.FieldByName('ISLINE').AsInteger;
            Edit_MarkMask.Text := AMemTable.FieldByName('MARK_MASK').AsString;
            if AMemTable.FieldByName(fnCoordZ).AsFloat <> -1 then
              seCoordZ.Value := RoundCP(FloatInUOM(AMemTable.FieldByName(fnCoordZ).AsFloat, umMetr, TF_Main(GForm).FUOM));

            //  seNewComponentIndex.Value := AMemTable.FieldByName(fnComponentIndex).AsInteger;
            // Tolik
            seNewComponentIndex.ShowHint := AMemTable.FieldByName('ISLINE').AsInteger <> 1;
            seNewComponentIndex.Value := AMemTable.FieldByName(fnComponentIndex).AsInteger;
            // если смотрим линейную компоненту
            if AMemTable.FieldByName('ISLINE').AsInteger = 1 then
            begin
              if AMemTable.FieldByName(fnComponentIndex).AsInteger > -1 then
              begin
                ListArea.Enabled := true;
                ProjArea.Enabled := true;
                seNewComponentIndex.Enabled := true;
                BeginFrom.Checked := true;
              end
              else
              begin
                if AMemTable.FieldByName(fnComponentIndex).AsInteger = -1 then
                  begin
                    seNewComponentIndex.Value := 1;
                    ProjArea.Checked := true;
                    seNewComponentIndex.Enabled := false;
                  end;
                if AMemTable.FieldByName(fnComponentIndex).AsInteger = -2 then
                 begin
                   seNewComponentIndex.Value := 1;
                   seNewComponentIndex.Enabled := false;
                   ListArea.Checked := true;
                 end;
              end
            end
            // если смотрим точечную компоненту
            else
            begin
              ProjArea.Enabled := False;
              ListArea.Enabled := False;
              if F_ProjMan.GSCSBase.CurrProject.Setting.PointComonIndexingMode = cimInProject then
              begin
                seNewComponentIndex.Enabled := true;
                BeginFrom.Checked := true;
              end
              else
              begin
                seNewComponentIndex.Enabled := false;
                BeginFrom.Checked := false;
              end
            end;

            cbCanUseAsPoint.Checked := IntToBool(AMemTable.FieldByName(fnCanUseAsPoint).AsInteger);

            FDesignParams.IDDesignIcon := AMemTable.FieldByName(fnIDDesignIcon).AsInteger;
            FDesignParams.GUIDDesignIcon := AMemTable.FieldByName(fnGUIDDesignIcon).AsString;

            {
            FDesignParams.IDDesignIcon := AMemTable.FieldByName(fnIDDesignIcon).AsInteger;

            if FCompTypeID = ctCupBoard then
            begin
              FDesignParams.BottomBound := GetPropertyValueAsFloat(tkComponentTypes, FCompTypeID, pnBottomBound, qmPhisical, -1);
              FDesignParams.LeftBound := GetPropertyValueAsFloat(tkComponentTypes, FCompTypeID, pnLeftBound, qmPhisical, -1);
              FDesignParams.RightBound := GetPropertyValueAsFloat(tkComponentTypes, FCompTypeID, pnRightBound, qmPhisical, -1);
              FDesignParams.TopBound := GetPropertyValueAsFloat(tkComponentTypes, FCompTypeID, pnTopBound, qmPhisical, -1);
            end;
            if TF_Main(GForm).F_CaseForm.CheckNoUsingCurrCompType(meEdit, smtProtocol) then
              EnableControl(true)
            else
              EnableControl(false);

            LoadImage;}
          end
          else
          begin
            FCompTypeID := TF_Main(GForm).DM.DataSet_COMPONENT_TYPES.FN(fnID).AsInteger;
            FCompTypeGUID := TF_Main(GForm).DM.DataSet_COMPONENT_TYPES.FN(fnGuid).AsString;
            FIDComponentTemplate := DataSet_COMPONENT_TYPES.FN(fnIDComponTemplate).AsInteger;
            FOldIDComponentTemplate := FIDComponentTemplate;

            Edit_Name.Text := DataSet_COMPONENT_TYPES.FN('NAME').AsString;
            Edit_NamePlural.Text := DataSet_COMPONENT_TYPES.FN('NAME_PLURAL').AsString;
            lbSysName.Text := DataSet_COMPONENT_TYPES.FN(fnSysName).AsString;
            if lbSysName.Text = '' then
              if mtSysNames.Locate(fnDescription, Edit_Name.Text, []) then
                lbSysName.Text := mtSysNames.FieldByName(fnSysName).AsString;
            RadioGroup_PortKind.ItemIndex := DataSet_COMPONENT_TYPES.FN('PORT_KIND').AsInteger;
            RadioGroup_ActiveState.ItemIndex := DataSet_COMPONENT_TYPES.FN('ACTIVE_STATE').AsInteger;
            RadioGroup_IsLine.ItemIndex := DataSet_COMPONENT_TYPES.FN('ISLINE').AsInteger;
            Edit_MarkMask.Text := DataSet_COMPONENT_TYPES.FN('MARK_MASK').AsString;
            edComponTemplate.Text := GetComponFldValueAsString(FIDComponentTemplate, fnName);
            if DataSet_COMPONENT_TYPES.FN(fnCoordZ).AsFloat <> -1 then
              seCoordZ.Value := RoundCP(FloatInUOM(DataSet_COMPONENT_TYPES.FN(fnCoordZ).AsFloat, umMetr, TF_Main(GForm).FUOM));
            cbCanUseAsPoint.Checked := IntToBool(DataSet_COMPONENT_TYPES.FN(fnCanUseAsPoint).AsInteger);

            FDesignParams.IDDesignIcon := DataSet_COMPONENT_TYPES.FN(fnIDDesignIcon).AsInteger;
            //FDesignParams.GUIDDesignIcon := 

            if lbSysName.Text = ctsnCupBoard then
            begin
              FDesignParams.BottomBound := GetPropertyValueAsFloat(tkComponentTypes, FCompTypeID, pnBottomBound, qmPhisical, -1);
              FDesignParams.LeftBound := GetPropertyValueAsFloat(tkComponentTypes, FCompTypeID, pnLeftBound, qmPhisical, -1);
              FDesignParams.RightBound := GetPropertyValueAsFloat(tkComponentTypes, FCompTypeID, pnRightBound, qmPhisical, -1);
              FDesignParams.TopBound := GetPropertyValueAsFloat(tkComponentTypes, FCompTypeID, pnTopBound, qmPhisical, -1);
            end;
            if TF_Main(GForm).F_CaseForm.CheckNoUsingCurrCompType(meEdit, smtProtocol, FCompTypeID) then
              EnableControl(true)
            else
              EnableControl(false);

            LoadImage;
          end;
        end;
    end;
    GWasChanging := False;
    SetControls;


    if ShowModal = mrOk then
      with TF_Main(GForm).DM do
      begin
        if AMemTable <> nil then
        begin
          if GFormMode = fmMake then
            AMemTable.Append
          else
            AMemTable.Edit;
          AMemTable.FieldByName(fnName).AsString := Edit_Name.Text;
          AMemTable.FieldByName(fnNamePlural).AsString := Edit_NamePlural.Text;
          AMemTable.FieldByName(fnSysName).AsString := lbSysName.Text;
          AMemTable.FieldByName(fnPortKind).AsInteger := RadioGroup_PortKind.ItemIndex;
          AMemTable.FieldByName(fnActiveState).AsInteger := RadioGroup_ActiveState.ItemIndex;
          AMemTable.FieldByName(fnIsLine).AsInteger := RadioGroup_IsLine.ItemIndex;
          AMemTable.FieldByName(fnisStandart).AsInteger := biFalse;
          AMemTable.FieldByName(fnMarkMask).AsString := Edit_MarkMask.Text;
          if seCoordZ.Value <> -1 then
            AMemTable.FieldByName(fnCoordZ).AsFloat := FloatInUOM(seCoordZ.Value, TF_Main(GForm).FUOM, umMetr)
          else
            AMemTable.FieldByName(fnCoordZ).AsFloat := seCoordZ.Value;

          //AMemTable.FieldByName(fnComponentIndex).AsInteger := seNewComponentIndex.Value;
          // Tolik
            if seNewComponentIndex.enabled then
              AMemTable.FieldByName(fnComponentIndex).AsInteger := seNewComponentIndex.Value
            else
            begin
              if ProjArea.Checked then
                AMemTable.FieldByName(fnComponentIndex).AsInteger := -1;
              if ListArea.Checked then
                AMemTable.FieldByName(fnComponentIndex).AsInteger := -2;
            end;
          //
          AMemTable.FieldByName(fnCanUseAsPoint).AsInteger := BoolToInt(cbCanUseAsPoint.Checked);
          if FDesignParams.IDDesignIcon > 0 then
            AMemTable.FieldByName(fnIDDesignIcon).AsInteger := FDesignParams.IDDesignIcon
          else
            AMemTable.FieldByName(fnIDDesignIcon).AsVariant := Null;
          AMemTable.FieldByName(fnGUIDDesignIcon).AsString := FDesignParams.GUIDDesignIcon;
          AMemTable.FieldByName(fnIsModified).AsBoolean := true;
          AMemTable.Post;
          GID := AMemTable.FieldByName(fnID).AsInteger;

          {
          if FCompTypeID = ctCupBoard then
          begin
            SetPropertyValueAsFloat(tkComponentTypes, FCompTypeID, pnBottomBound, FDesignParams.BottomBound, qmPhisical, -1);
            SetPropertyValueAsFloat(tkComponentTypes, FCompTypeID, pnLeftBound, FDesignParams.LeftBound, qmPhisical, -1);
            SetPropertyValueAsFloat(tkComponentTypes, FCompTypeID, pnRightBound, FDesignParams.RightBound, qmPhisical, -1);
            SetPropertyValueAsFloat(tkComponentTypes, FCompTypeID, pnTopBound, FDesignParams.TopBound, qmPhisical, -1);

            RefreshDataSet(DataSet_comp_type_prop_relation);
          end;}
        end
        else
        begin
          ZeroMemory(@ComponType, SizeOf(TComponentType));
          ComponType.ID := FCompTypeID;
          ComponType.Name := Edit_Name.Text;
          ComponType.NamePlural := Edit_NamePlural.Text;
          ComponType.SysName := lbSysName.Text;
          ComponType.PortKind := RadioGroup_PortKind.ItemIndex;
          ComponType.ActiveState := RadioGroup_ActiveState.ItemIndex;
          ComponType.IsLine := RadioGroup_IsLine.ItemIndex;
          ComponType.IsStandart := biFalse;
          ComponType.MarkMask := Edit_MarkMask.Text;
          if seCoordZ.Value <> -1 then
            ComponType.CoordZ := FloatInUOM(seCoordZ.Value, TF_Main(GForm).FUOM, umMetr)
          else
            ComponType.CoordZ := seCoordZ.Value;
          ComponType.CanUseAsPoint := BoolToInt(cbCanUseAsPoint.Checked);

          ComponType.IDComponTemplate := FIDComponentTemplate;
          ComponType.IDDesignIcon := FDesignParams.IDDesignIcon;
          SaveComponentType(MakeEdit, @ComponType);
          GID := ComponType.ID;

          {if MakeEdit = meMake then
          begin
            DataSet_COMPONENT_TYPES.Active := false;
            DataSet_COMPONENT_TYPES.Active := true;
          end
          else}
          if MakeEdit = meEdit then
            DataSet_COMPONENT_TYPES.ReopenLocate(fnID); //DataSet_COMPONENT_TYPES.FullRefresh;

          {if GFormMode = fmMake then
            DataSet_COMPONENT_TYPES.Append;
          DataSet_COMPONENT_TYPES.Edit;
          DataSet_COMPONENT_TYPES.FN(fnName).AsString := Edit_Name.Text;
          DataSet_COMPONENT_TYPES.FN(fnNamePlural).AsString := Edit_NamePlural.Text;
          DataSet_COMPONENT_TYPES.FN(fnSysName).AsString := lbSysName.Text;
          DataSet_COMPONENT_TYPES.FN(fnPortKind).AsInteger := RadioGroup_PortKind.ItemIndex;
          DataSet_COMPONENT_TYPES.FN(fnActiveState).AsInteger := RadioGroup_ActiveState.ItemIndex;
          DataSet_COMPONENT_TYPES.FN(fnIsLine).AsInteger := RadioGroup_IsLine.ItemIndex;
          DataSet_COMPONENT_TYPES.FN(fnisStandart).AsInteger := biFalse;
          DataSet_COMPONENT_TYPES.FN(fnMarkMask).AsString := Edit_MarkMask.Text;
          DataSet_COMPONENT_TYPES.FN(fnCoordZ).AsFloat := seCoordZ.Value;
          SetDataSetIntValueAsZeroToNull(DataSet_COMPONENT_TYPES, fnIDComponTemplate, FIDComponentTemplate);
          SetDataSetIntValueAsZeroToNull(DataSet_COMPONENT_TYPES, fnIDDesignIcon, FDesignParams.IDDesignIcon);
          DataSet_COMPONENT_TYPES.Post;
          GID := DataSet_COMPONENT_TYPES.FN(fnID).AsInteger;}

          if lbSysName.Text = ctsnCupBoard then
          begin
            SetPropertyValueAsFloat(tkComponentTypes, FCompTypeID, pnBottomBound, FDesignParams.BottomBound, qmPhisical, -1);
            SetPropertyValueAsFloat(tkComponentTypes, FCompTypeID, pnLeftBound, FDesignParams.LeftBound, qmPhisical, -1);
            SetPropertyValueAsFloat(tkComponentTypes, FCompTypeID, pnRightBound, FDesignParams.RightBound, qmPhisical, -1);
            SetPropertyValueAsFloat(tkComponentTypes, FCompTypeID, pnTopBound, FDesignParams.TopBound, qmPhisical, -1);

            RefreshDataSet(DataSet_comp_type_prop_relation);
          end;

          if FOldIDComponentTemplate <> FIDComponentTemplate then
          begin
            NBComponenType := TF_Main(GForm).GSCSBase.NBSpravochnik.GetComponentTypeByGUID(DataSet_COMPONENT_TYPES.FN(fnGUID).AsString);
            if NBComponenType <> nil then
              NBComponenType.ComponentType.IDComponTemplate := FIDComponentTemplate;

            if FOldIDComponentTemplate > 0 then
              TF_Main(GForm).DefineImageIndexComponNode(FOldIDComponentTemplate);
            TF_Main(GForm).DefineImageIndexComponNode(FIDComponentTemplate);
            TF_Main(GForm).Tree_Catalog.Repaint;
          end;
        end;
        Result := true;
      end;
  except
    on E: Exception do AddExceptionToLog('TF_MakeEditComponentType.MakeEditComponentType: '+E.Message);
  end;
end;


procedure TF_MakeEditComponentType.LoadImage;
//var Bitmap: TBitmap;
begin
  LoadObjectIconToCXImage(ImgDesignIcon, FDesignParams.IDDesignIcon, oitProjectible, TF_Main(GForm).GDBMode);
    {
    //ImgDesignIcon.Picture.Bitmap.FreeImage;
    ClearcxImage(ImgDesignIcon);

    Bitmap := nil;
    Bitmap := GetObjIcon(FIDDesignIcon, oitProjectible);
    if Assigned(Bitmap) then
      begin
          //ImgDesignIcon.Picture.Bitmap.Free;
          ImgDesignIcon.Picture.Bitmap := Bitmap;
      end; }
end;



procedure TF_MakeEditComponentType.Button_OKClick(Sender: TObject);
var MessgError: String;
    CanSave: Boolean;

  procedure AddErrorMessg(AError: String);
  begin
      MessgError := MessgError + #13 + '  - ' + AError;
      CanSave := False;
  end;

begin
    try
      CanSave := True;
      MessgError := cMakeEditComponentType_Msg3_1+':'+#13+#13;
      if Edit_Name.Text = '' then
        AddErrorMessg(cMakeEditComponentType_Msg3_2);
      if Edit_NamePlural.Text = '' then
        AddErrorMessg(cMakeEditComponentType_Msg3_3);
      if lbSysName.Text = '' then
        AddErrorMessg(cMakeEditComponentType_Msg3_4);
      if RadioGroup_PortKind.ItemIndex = -1 then
        AddErrorMessg(cMakeEditComponentType_Msg3_5);
      if RadioGroup_ActiveState.ItemIndex = -1 then
        AddErrorMessg(cMakeEditComponentType_Msg3_6);
      if RadioGroup_IsLine.ItemIndex = -1 then
        AddErrorMessg(cMakeEditComponentType_Msg3_7);
      if (FRoomHeight > 0) and (seCoordZ.Value - FloatInUOM(FRoomHeight, umMetr, TF_Main(GForm).FUOM) > 0) then
        AddErrorMessg(cMakeEditComponentType_Msg3_8);

      if Not CanSave then
      begin
        MessageModal(MessgError, Caption, MB_OK or MB_ICONINFORMATION);
        ModalResult := mrNone;
        Exit; ///// EXIT /////
      end;
    except
      on E: Exception do AddExceptionToLog('TF_MakeEditComponentType.Button_OKClick: '+E.Message);
    end;
end;

procedure TF_MakeEditComponentType.Edit_NamePropertiesChange(
  Sender: TObject);
begin
    GWasChanging := true;
end;

procedure TF_MakeEditComponentType.RadioGroup_PortKindPropertiesChange(
  Sender: TObject);
begin
    GWasChanging := true;
end;

procedure TF_MakeEditComponentType.RadioGroup_ActiveStateClick(
  Sender: TObject);
begin
    GWasChanging := true;
end;

procedure TF_MakeEditComponentType.RadioGroup_IsLineClick(Sender: TObject);
begin
  GWasChanging := true;
  SetControls;
end;


// ##### Обработка нажатия на Esc #####
procedure TF_MakeEditComponentType.Act_CancelByEscExecute(Sender: TObject);
var MessRes: Integer;
begin
    try
      MessRes := ID_NO;
      if GWasChanging then
        MessRes := MessageModal(cMakeEditComponentType_Msg4,
                     Application.Title, MB_ICONQUESTION or MB_YESNOCANCEL);
      case MessRes of
        ID_YES: Button_OK.Click;
        ID_NO : Button_Cancel.Click;
      end;
    except
      on E: Exception do AddExceptionToLog('TF_MakeEditComponentType.Act_CancelByEscExecute: '+E.Message);
    end;
end;

procedure TF_MakeEditComponentType.Button_EditMarkMaskClick(
  Sender: TObject);
var NewMarkMask: String;
    ChangeMarkMask: Boolean;
begin
    NewMarkMask := Edit_MarkMask.Text;
    //if Edit_MarkMask.Text = '' then
    //  ChangeMarkMask := TF_Main(GForm).F_MarkMask.MakeEditMarkMask(fmMake, NewMarkMask)
    //else
    //  ChangeMarkMask := TF_Main(GForm).F_MarkMask.MakeEditMarkMask(fmEdit, NewMarkMask);
    //if ChangeMarkMask then
    if TF_Main(GForm).CreateFMarkMask.MakeEditMarkMask(fmEdit, NewMarkMask) then
      Edit_MarkMask.Text := NewMarkMask;
end;

procedure TF_MakeEditComponentType.btChoiceDesignImageClick(
  Sender: TObject);
var
    NewIDDesignIcon: Integer;
    NewGUIDDesignIcon: string;
begin
  with TF_Main(GForm).DM do
    //if (GFormMode = fmEdit) and (lbSysName.Text = ctsnCupBoard) then
//    begin {
//      FDesignParams.BottomBound := GetPropertyValueAsFloat(tkComponentTypes, CompTypeID, pnBottomBound, qmPhisical, -1);
//      FDesignParams.LeftBound := GetPropertyValueAsFloat(tkComponentTypes, CompTypeID, pnLeftBound, qmPhisical, -1);
//      FDesignParams.RightBound := GetPropertyValueAsFloat(tkComponentTypes, CompTypeID, pnRightBound, qmPhisical, -1);
//      FDesignParams.TopBound := GetPropertyValueAsFloat(tkComponentTypes, CompTypeID, pnTopBound, qmPhisical, -1);
//          }
//      if F_ComponDesignWizard.ExecuteWizard(FDesignParams) then
//      begin
//        LoadImage;
//        //LoadImageFromObjectIcons(GIDSymbolImage, Image_Symbol);
//      end;
//    end
//    else
    begin
      NewIDDesignIcon := TF_Main(GForm).DM.GetIDObjectIconFromGuide(FDesignParams.IDDesignIcon);
      NewGUIDDesignIcon := TF_Main(GForm).DM.DataSet_OBJECT_ICONS.FN(fnGUID).AsString;
      if NewIDDesignIcon <> FDesignParams.IDDesignIcon then
      begin
        FDesignParams.IDDesignIcon := NewIDDesignIcon;
        FDesignParams.GUIDDesignIcon := NewGUIDDesignIcon;
        LoadImage;
      end;
    end;
end;

procedure TF_MakeEditComponentType.btClearDesignImageClick(
  Sender: TObject);
begin
  //ImgDesignIcon.Picture.Bitmap.FreeImage;
  ClearcxImage(ImgDesignIcon);
  FDesignParams.IDDesignIcon := 0;
end;

procedure TF_MakeEditComponentType.FormCreate(Sender: TObject);

  procedure AddColumnToLookUp(const ACaption, AFieldName: String);
  var
    LookUpColumn: TcxLookupDBGridColumn;
  begin
    LookUpColumn := lbSysName.Properties.ListColumns.Add;
    LookUpColumn.Caption := ACaption;
    LookUpColumn.FieldName := AFieldName;
    LookUpColumn.Width := 200;
  end;

  procedure AddSysNameTYoTable(const ASysName, ADescription: String);
  begin
    mtSysNames.Append;
    //mtSysNames.FieldByName(fnSysName).AsString := ASysName;
    //mtSysNames.FieldByName(fnDescription).AsString := ADescription;
    mtSysNames.Fields[0].AsString := ASysName;
    mtSysNames.Fields[1].AsString := ADescription;
    mtSysNames.Post;
  end;

begin
  case TF_Main(GForm).GDBMode of
    bkNormBase:
      begin
       { lbNewComponentIndex.Visible := false;
        lbNewComponentIndexProjArea.Visible := false;
        seNewComponentIndex.Visible := false;}
        RzRadiogroup1.Visible := false;

      end;
    bkProjectManager:
      begin
        //lbSysName.Enabled := false;
        gbDesignIcon.Enabled := false;
      end;
  end;

  mtSysNames.FieldDefs.Add(fnSysName, ftString, 100);
  mtSysNames.FieldDefs.Add(fnDescription, ftString, 100);
  mtSysNames.Active := true;

  mtSysNames.DisableControls;
  try
    AddSysNameTYoTable('', '');
    AddSysNameTYoTable(ctsnNull, 'null');
    AddSysNameTYoTable(ctsn19InchAccessory, cMakeEditComponentType_Msg5_1);
    AddSysNameTYoTable(ctsnPCBFraming, cMakeEditComponentType_Msg5_2);
    AddSysNameTYoTable(ctsnAdapter, cMakeEditComponentType_Msg5_3);
    AddSysNameTYoTable(ctsnAdapterFraming, cMakeEditComponentType_Msg5_4);
    AddSysNameTYoTable(ctsnAccessory, cMakeEditComponentType_Msg5_5);
    AddSysNameTYoTable(ctsnCableChannelAccessory, cMakeEditComponentType_Msg5_6);
    AddSysNameTYoTable(ctsnAnalyser, cMakeEditComponentType_Msg5_7);
    AddSysNameTYoTable(ctsnBox, cMakeEditComponentType_Msg5_8);
    AddSysNameTYoTable(ctsnOFCable, cMakeEditComponentType_Msg5_9);
    AddSysNameTYoTable(ctsnOFConnector, cMakeEditComponentType_Msg5_10);
    AddSysNameTYoTable(ctsnOFModule, cMakeEditComponentType_Msg5_11);
    //AddSysNameTYoTable(ctsnTwoPinPlug, 'Вилка');
    AddSysNameTYoTable(ctsnPlugSwitch, cMakeEditComponentType_Msg5_12);
    AddSysNameTYoTable(ctsnOther, cMakeEditComponentType_Msg5_13);
    AddSysNameTYoTable(ctsnCork, cMakeEditComponentType_Msg5_14);
    AddSysNameTYoTable(ctsnProtectionIU, cMakeEditComponentType_Msg5_15);
    AddSysNameTYoTable(ctsnInstrument, cMakeEditComponentType_Msg5_16);
    AddSysNameTYoTable(ctsnInstrumentVSStandartAndVSModuler, cMakeEditComponentType_Msg5_17);
    AddSysNameTYoTable(ctsnCable, cMakeEditComponentType_Msg5_18);
    AddSysNameTYoTable(ctsnCableChannel, cMakeEditComponentType_Msg5_19);
    AddSysNameTYoTable(ctsnConnectorBlock, cMakeEditComponentType_Msg5_20);
    AddSysNameTYoTable(ctsnConnector, cMakeEditComponentType_Msg5_21);
    AddSysNameTYoTable(ctsnCase, cMakeEditComponentType_Msg5_22);
    AddSysNameTYoTable(ctsnCrossConnections, cMakeEditComponentType_Msg5_23);
    //AddSysNameTYoTable(ctsnCrossPointObject, 'Кроссо-точечный объект');
    AddSysNameTYoTable(ctsnMetalConstruction, cMakeEditComponentType_Msg5_24);
    AddSysNameTYoTable(ctsnModule, cMakeEditComponentType_Msg5_25);
    AddSysNameTYoTable(ctsnEquipment, cMakeEditComponentType_Msg5_26);
    AddSysNameTYoTable(ctsnOrganizer, cMakeEditComponentType_Msg5_27);
    AddSysNameTYoTable(ctsn19InchPanel, cMakeEditComponentType_Msg5_28);
    AddSysNameTYoTable(ctsnPatchCord, cMakeEditComponentType_Msg5_29);
    AddSysNameTYoTable(ctsnPatchPanel, cMakeEditComponentType_Msg5_30);
    AddSysNameTYoTable(ctsnTerminalBloc, cMakeEditComponentType_Msg5_31);
    AddSysNameTYoTable(ctsnWire, cMakeEditComponentType_Msg5_32);
    AddSysNameTYoTable(ctsnWorkPlace, cMakeEditComponentType_Msg5_33);
    AddSysNameTYoTable(ctsnFrame, cMakeEditComponentType_Msg5_34);
    AddSysNameTYoTable(ctsnSocket, cMakeEditComponentType_Msg5_35);
    AddSysNameTYoTable(ctsnJoiner, cMakeEditComponentType_Msg5_36);
    AddSysNameTYoTable(ctsnConnectingModule, cMakeEditComponentType_Msg5_37);
    AddSysNameTYoTable(ctsnTestingAccessory, cMakeEditComponentType_Msg5_38);
    AddSysNameTYoTable(ctsnTube, cMakeEditComponentType_Msg5_39);
    AddSysNameTYoTable(ctsnTubeElement,cMakeEditComponentType_Msg5_39_1);// Tolik 13/10/2018 -- 
    AddSysNameTYoTable(ctsnInstallBox, cMakeEditComponentType_Msg5_40);
    AddSysNameTYoTable(ctsnCupboard, cMakeEditComponentType_Msg5_41);
    AddSysNameTYoTable(ctsnJackPlug, cMakeEditComponentType_Msg5_42);
    AddSysNameTYoTable(ctsnShield, cMakeEditComponentType_Msg5_43);
    AddSysNameTYoTable(ctsnCableChannelElement,	cMakeEditComponentType_Msg5_44);

    AddSysNameTYoTable(ctsnCrossATS, cMakeEditComponentType_Msg5_45);
    AddSysNameTYoTable(ctsnDistributionCabinet, cMakeEditComponentType_Msg5_46);
    AddSysNameTYoTable(ctsnTerminalBox, cMakeEditComponentType_Msg5_47);
    AddSysNameTYoTable(ctsnLamp, cMakeEditComponentType_Msg5_48);
    AddSysNameTYoTable(ctsnAutoSwitch, cMakeEditComponentType_Msg5_49);

    AddSysNameTYoTable(ctsnHouse, cBaseCommon50_1);
    AddSysNameTYoTable(ctsnApproach, cBaseCommon50_3);

    AddSysNameTYoTable(ctsnArhRoom, cBaseCommon57);
    AddSysNameTYoTable(ctsnArhBrickWall, cBaseCommon72);
    AddSysNameTYoTable(ctsnArhWall, cBaseCommon58);
    AddSysNameTYoTable(ctsnArhWallDivision, cBaseCommon59);
    AddSysNameTYoTable(ctsnArhFloor, cBaseCommon60);
    AddSysNameTYoTable(ctsnArhCeiling, cBaseCommon61);
    AddSysNameTYoTable(ctsnArhEmbrasure, cBaseCommon62);
    AddSysNameTYoTable(ctsnArhWindow, cBaseCommon63);
    AddSysNameTYoTable(ctsnArhDoor, cBaseCommon64);
    AddSysNameTYoTable(ctsnArhArc, cBaseCommon70);
    AddSysNameTYoTable(ctsnArhBalcony, cBaseCommon71);
    AddSysNameTYoTable(ctsnArhNiche, cBaseCommon65);
    AddSysNameTYoTable(ctsnArhInnerSlope, cBaseCommon68);
    AddSysNameTYoTable(ctsnArhOuterSlope, cBaseCommon69);
    AddSysNameTYoTable(ctsnArhWallCorner, cBaseCommon76);

    AddSysNameTYoTable(ctsnArhRoof, cBaseCommon80);
    AddSysNameTYoTable(ctsnArhRoofSeg, cBaseCommon81);
    AddSysNameTYoTable(ctsnArhRoofHip, cBaseCommon82);
    AddSysNameTYoTable(ctsnArhRoofHipCorner, cBaseCommon83);
  finally
    mtSysNames.EnableControls;
  end;


  lbSysName.Properties.BeginUpdate;
  try
    dsSysNames.DataSet := mtSysNames;

    lbSysName.Properties.ListSource := dsSysNames;
    AddColumnToLookUp(cMakeEditComponentType_Msg6, fnSysName);
    AddColumnToLookUp(cMakeEditComponentType_Msg7, fnDescription);
    lbSysName.Properties.KeyFieldNames := fnSysName;
    //lbSysName.Properties.ListFieldNames := fnSysName;
  finally
    lbSysName.Properties.EndUpdate;
  end;
   //Tolik
  //seNewComponentIndex.Properties.MinValue := 1;
  seNewComponentIndex.Properties.MinValue := -2;

  seNewComponentIndex.Properties.MaxValue := MaxInt;
end;

procedure TF_MakeEditComponentType.btChoiceComponTemplateClick(
  Sender: TObject);
var
  ComponentTemplate: TSCSComponent;
begin
  if lbSysName.Text = '' then
  begin
    MessageModal(cMakeEditComponentType_Msg8, ApplicationName, MB_ICONINFORMATION or MB_OK);
    Exit; ///// EXIT /////
  end;

  ComponentTemplate := TF_Main(GForm).CreateFConnectComplWith.GetComponentFromNB(FIDComponentTemplate, FCompTypeGUID);
  if ComponentTemplate <> nil then
  begin
    edComponTemplate.Text := ComponentTemplate.Name;
    FIDComponentTemplate := ComponentTemplate.ID;
    FreeAndNil(ComponentTemplate);
  end;
end;

procedure TF_MakeEditComponentType.btClearComponTemplateClick(
  Sender: TObject);
begin
  edComponTemplate.Text := '';
  FIDComponentTemplate := 0;
end;

procedure TF_MakeEditComponentType.BeginFromClick(Sender: TObject);
begin
  seNewComponentIndex.Enabled := true;
end;

procedure TF_MakeEditComponentType.ListAreaClick(Sender: TObject);
begin
  seNewComponentIndex.Enabled := false;
end;

procedure TF_MakeEditComponentType.ProjAreaClick(Sender: TObject);
begin
  seNewComponentIndex.Enabled := false;
end;

end.
