unit U_MakeEditPropRel;

interface

uses
  Windows, U_LNG, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, RzButton, ExtCtrls, RzPanel, StdCtrls, RzCmboBx, RzRadChk, Mask,
  RzEdit, kbmMemTable,

  U_BaseCommon, U_Common, U_BaseConstants, U_SCSComponent, U_SCSLists,{Tolik 03/10/2018 -- }U_ProtectionCommon, RzSpnEdt, DB, siComp, siLngLnk, cxControls,
  cxContainer, cxEdit, cxTextEdit, cxMaskEdit, Buttons,{Tolik} form3d;

type

  TOnCheckPropRelValue = function (ASender: TObject; AProp: PProperty; aDataSetProps: TDataSet): Boolean of Object;

  TF_MakeEditPropRel = class(TForm)
    RzGroupBox1: TRzGroupBox;
    RzGroupBox2: TRzGroupBox;
    btOk: TRzBitBtn;
    btCancel: TRzBitBtn;
    Label_Value: TLabel;
    lbValue: TLabel;
    cbName: TRzComboBox;
    Label1: TLabel;
    RzGroupBox3: TRzGroupBox;
    cbTakeIntoJoin: TRzCheckBox;
    cbTakeIntoComplect: TRzCheckBox;
    deValue: TRzDateTimeEdit;
    colorbValue: TRzColorComboBox;
    cbValue: TRzCheckBox;
    cbCompStateType: TRzComboBox;
    edValue: TRzEdit;
    cbIsTakeJoinForPoints: TRzCheckBox;
    pnCrossControl: TRzPanel;
    cbIsCrossControl: TRzCheckBox;
    cbCrossProperty: TRzComboBox;
    cbCableCanalElemntTypes: TRzComboBox;
    seValue: TRzSpinEdit;
    RzPanel1: TRzPanel;
    Label_SysName: TLabel;
    Label4: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label10: TLabel;
    edSysName: TRzEdit;
    edIzm: TRzEdit;
    cbDataType: TRzComboBox;
    cbValueReq: TRzCheckBox;
    edDescription: TRzEdit;
    seMinValue: TRzSpinEdit;
    seMaxValue: TRzSpinEdit;
    deMinValue: TRzDateTimeEdit;
    deMaxValue: TRzDateTimeEdit;
    Label2: TLabel;
    pnIsForWholeComponent: TRzPanel;
    cbIsForWholeComponent: TRzCheckBox;
    lng_Forms: TsiLangLinked;
    pnSectionSideValue: TRzPanel;
    cbSectionSideValue: TRzComboBox;
    btSectionSideValue: TSpeedButton;
    btChoiceProperty: TSpeedButton;
    btChoiceCrossProperty: TSpeedButton;
    cbItemList: TRzComboBox;
    meStringListDef: TRzMemo;
    BtnLoad3DSModel: TBitBtn;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure cbNameChange(Sender: TObject);
    procedure btOkClick(Sender: TObject);
    procedure cbIsCrossControlClick(Sender: TObject);
    procedure cbTakeIntoJoinClick(Sender: TObject);
    procedure cbTakeIntoComplectClick(Sender: TObject);
    procedure btChoicePropertyClick(Sender: TObject);
    procedure btChoiceCrossPropertyClick(Sender: TObject);
    procedure cbValueReqMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure cbValueReqKeyUp(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure seValueKeyPress(Sender: TObject; var Key: Char);
    procedure btSectionSideValueClick(Sender: TObject);
    procedure cbItemListChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure seValueChange(Sender: TObject);
    procedure BtnLoad3DSModelClick(Sender: TObject);
  private
    GForm: TForm;

    FDataSet: TDataSet;
    FProperty: PProperty;
    FPropertyData: TPropertyData;
    FMakeEdit: TMakeEdit;
    FTableKind: TTableKind;

    FItemFilter: String;
    FItemType: Integer;
    FMemTableProps: TkbmMemTable;
    FSpravochnik: TSpravochnik;

    FLastTubeConnectcKind: Integer;
    FLastBasementType: Integer;
    FLastTypeItem: TIntList;

    FValueControl: TWinControl;

    FOnCheckPropRelValue: TOnCheckPropRelValue;

    function ChoiceProperty(AComboBox, AOtherComboBox: TRzComboBox): Boolean;
    function DefineItemFilter(AItemType: Integer): String;
    procedure SetControls;
  public
    property PropertyData: TPropertyData read FPropertyData;

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

    function Execute(AMakeEdit: TMakeEdit; ATableKind: TTableKind;
                AProperty: PProperty; ADataSet: TDataSet; AItemType: TItemType;
                AMemTableProps: TkbmMemTable; ASpravochnik: TSpravochnik;
                AOnCheckPropRelValue: TOnCheckPropRelValue=nil): Boolean;
    procedure FormDataToProperty(var aProp: TProperty);
    function GetPropertValue: String;
    function MinMaxControl(AValue: String; var AMessages: String; APropData: TPropertyData): Boolean;
    function SetPropertyParams(AIDProperty: Integer; AGUIDProperty: String; var APropertyData: TPropertyData): Boolean;
    procedure SetPropertyValue(AValue, ASysName: String);
  end;

//var
//  F_MakeEditPropRel: TF_MakeEditPropRel;

implementation
Uses U_Main, Unit_DM_SCS;
{$R *.dfm}

{ TF_MakeEditPropRel }

constructor TF_MakeEditPropRel.Create(AOwner: TComponent; AForm: TForm);
begin
  GForm := AForm;
  inherited Create(AOwner);
end;

destructor TF_MakeEditPropRel.Destroy;
begin
  inherited;
end;

function TF_MakeEditPropRel.Execute(AMakeEdit: TMakeEdit;
  ATableKind: TTableKind; AProperty: PProperty; ADataSet: TDataSet; AItemType: TItemType;
  AMemTableProps: TkbmMemTable; ASpravochnik: TSpravochnik; AOnCheckPropRelValue: TOnCheckPropRelValue=nil): Boolean;
var
  SavedOnChange: TNotifyEvent;
  //OldTick, CurrTick: Cardinal;
begin
  //OldTick := GetTickCount;
  // Tolik 03/10/2018 - -
  BtnLoad3DSModel.Enabled := False;
  //
  Result := false;
  Caption := '';
  FProperty := AProperty;
  ZeroMemory(@FPropertyData, SizeOf(FPropertyData));

  FMakeEdit := AMakeEdit;
  FDataSet := ADataSet;
  FTableKind := ATableKind;
  FItemType := AItemType;
  FItemFilter := DefineItemFilter(AItemType);

  FMemTableProps := AMemTableProps;
  FSpravochnik := ASpravochnik;
  FValueControl := nil;
  FOnCheckPropRelValue := AOnCheckPropRelValue;

  FLastTubeConnectcKind := -1;
  FLastBasementType := -1;
  FLastTypeItem.Fill(-1, dtLast);

  SavedOnChange := cbName.OnChange;
  cbName.OnChange := nil;
  cbName.Items.BeginUpdate;
  cbCrossProperty.Items.BeginUpdate;
  cbSectionSideValue.Items.BeginUpdate;
  try
    TF_Main(GForm).FNormBase.DM.LoadInputStringsToTStrings(cbSectionSideValue.Items, stDimensions);

    cbName.Sorted := true; //30.09.2011
    cbCrossProperty.Sorted := true; //30.09.2011
    if ASpravochnik <> nil then
    begin
      //30.09.2011 ASpravochnik.LoadPropertiesToStrings(cbName.Items, true);
      //30.09.2011 ASpravochnik.LoadPropertiesToStrings(cbCrossProperty.Items, true);
      ASpravochnik.LoadPropertiesToStrings(cbName.Items, true, false);
      ASpravochnik.LoadPropertiesToStrings(cbCrossProperty.Items, true, false);
    end
    else
    if AMemTableProps = nil then
    begin
      FillComboBoxRz(cbName, F_NormBase, true, tnProperties, fnID, fnName, '', 0, '', FItemFilter);
      FillComboBoxRz(cbCrossProperty, F_NormBase, true, tnProperties, fnID, fnName, '', 0, '', FItemFilter);
    end
    else
    begin
      FillComboBoxRzFromMT(cbName, AMemTableProps, true, fnID, fnName, '', 0, '');
      FillComboBoxRzFromMT(cbCrossProperty, AMemTableProps, true, fnID, fnName, '', 0, '');
    end;
    btChoiceProperty.Visible := (AMemTableProps = nil);
    case AMakeEdit of
      meMake:
        begin
          Caption := cMakeEditPropRel_Msg1_1;
          cbTakeIntoComplect.Checked := false;
          cbTakeIntoJoin.Checked := false;
          cbIsTakeJoinForPoints.Checked := false;
          cbIsCrossControl.Checked := false;

          SelectItemByIDinComboRz(cbName, 0);
          SelectItemByIDinComboRz(cbCrossProperty, 0);

          //SetPropertyParams(0, '', AProperty);
        end;
      meEdit:
        begin
          Caption := cMakeEditPropRel_Msg1_2;
          if TF_Main(GForm).GDBMode = bkNormBase then
          begin
            SelectItemByIDinComboRz(cbName, AProperty.ID_Property);
            SelectItemByIDinComboRz(cbCrossProperty, AProperty.IDCrossProperty);
          end
          else
          begin
            SelectItemByGUIDinComboRz(cbName, AProperty.GUIDProperty);
            SelectItemByGUIDinComboRz(cbCrossProperty, AProperty.GUIDCrossProperty);
          end;

          cbTakeIntoComplect.Checked := IntToBool(AProperty.TakeIntoConnect);
          cbTakeIntoJoin.Checked := IntToBool(AProperty.TakeIntoJoin);
          cbIsTakeJoinForPoints.Checked := IntToBool(AProperty.IsTakeJoinforPoint);
          cbIsCrossControl.Checked := IntToBool(AProperty.IsCrossControl);
          //SetPropertyParams(AProperty.ID_Property, AProperty.GUIDProperty, FPropertyData);
          //SetPropertyValue(AProperty.Value);
        end;
    end;
    SetPropertyParams(AProperty.ID_Property, AProperty.GUIDProperty, FPropertyData);
    SetPropertyValue(AProperty.Value, AProperty.SysName);
  finally
    cbName.OnChange := SavedOnChange;
    cbName.Items.EndUpdate;
    cbCrossProperty.Items.EndUpdate;
    cbSectionSideValue.Items.EndUpdate;
  end;
  cbIsCrossControlClick(cbIsCrossControl);

  SetControls;

  //CurrTick := GetTickCount - OldTick;
  //CurrTick := GetTickCount - OldTick;

  if ShowModal = mrOk then
  begin
    Result := true;
    {//01.10.2012
    AProperty.Value := GetPropertValue;
    AProperty.ID_Property := GetIDFromComboBoxRz(cbName);
    AProperty.IDDataType := FPropertyData.IDDataType;
    AProperty.TakeIntoConnect := BoolToInt(cbTakeIntoComplect.Checked);
    AProperty.TakeIntoJoin := BoolToInt(cbTakeIntoJoin.Checked);
    AProperty.IsTakeJoinforPoint := BoolToInt(cbIsTakeJoinForPoints.Checked);
    AProperty.IsCrossControl := BoolToInt(cbIsCrossControl.Checked);
    AProperty.IDCrossProperty := GetIDFromComboBoxRz(cbCrossProperty);

    AProperty.GUIDProperty := GetGUIDFromComboBoxRz(cbName); //F_NormBase.DM.GetStringFromTableByID(tnProperties, fnGuid, AProperty.ID_Property, qmPhisical);
    AProperty.GUIDCrossProperty := GetGUIDFromComboBoxRz(cbCrossProperty); //F_NormBase.DM.GetStringFromTableByID(tnProperties, fnGuid, AProperty.IDCrossProperty, qmPhisical);

    AProperty.Name_ := FPropertyData.Name;
    AProperty.SysName := FPropertyData.SysName;
    AProperty.IsForWholeComponent := FPropertyData.IsForWholeComponent;}
    FormDataToProperty(AProperty^);
  end;
end;

procedure TF_MakeEditPropRel.FormDataToProperty(var aProp: TProperty);
begin
  aProp.Value := GetPropertValue;
  aProp.ID_Property := GetIDFromComboBoxRz(cbName);
  aProp.IDDataType := FPropertyData.IDDataType;
  aProp.TakeIntoConnect := BoolToInt(cbTakeIntoComplect.Checked);
  aProp.TakeIntoJoin := BoolToInt(cbTakeIntoJoin.Checked);
  aProp.IsTakeJoinforPoint := BoolToInt(cbIsTakeJoinForPoints.Checked);
  aProp.IsCrossControl := BoolToInt(cbIsCrossControl.Checked);
  aProp.IDCrossProperty := GetIDFromComboBoxRz(cbCrossProperty);

  aProp.GUIDProperty := GetGUIDFromComboBoxRz(cbName); //F_NormBase.DM.GetStringFromTableByID(tnProperties, fnGuid, AProperty.ID_Property, qmPhisical);
  aProp.GUIDCrossProperty := GetGUIDFromComboBoxRz(cbCrossProperty); //F_NormBase.DM.GetStringFromTableByID(tnProperties, fnGuid, AProperty.IDCrossProperty, qmPhisical);

  aProp.Name_ := FPropertyData.Name;
  aProp.SysName := FPropertyData.SysName;
  aProp.IsForWholeComponent := FPropertyData.IsForWholeComponent;
end;

function TF_MakeEditPropRel.GetPropertValue: String;
var
  DataType: Integer;
  ValueFloat: Double;
begin
  Result := '';
  DataType := GetIDFromComboBoxRz(cbDataType);
  try
    Case DataType of
      dtFloat:
        begin
          ValueFloat := seValue.Value;
          PropValueInUOM(ValueFloat, FPropertyData.SysName, TF_Main(GForm).FUOM, umMetr);
          Result := FloatToStrU(ValueFloat);
        end;
      dtInteger:
        begin
          ValueFloat := Int(seValue.Value);
          PropValueInUOM(ValueFloat, FPropertyData.SysName, TF_Main(GForm).FUOM, umMetr);
          Result := FloatToStrU(Int(ValueFloat)); //IntToStr(Round(seValue.Value));
        end;
      dtBoolean:
        begin
          Result := IntToStr(BoolToInt(cbValue.Checked));
        end;
      dtString:
        begin
          Result := edValue.Text;
        end;
      dtDate:
        begin
          Result := DateToStrU(deValue.Date);
        end;
      dtCompStateType:
        begin
          Result := IntTostr(GetIDFromComboBoxRz(cbCompStateType));
        end;
      dtColor:
        begin
          Result := IntToStr(colorbValue.SelectedColor);
        end;
      dtCableCanalElementType:
        begin
          Result := IntTostr(GetIDFromComboBoxRz(cbCableCanalElemntTypes));
          //CheckBox_PNotNull.Enabled := false;
          //CheckBox_PNotNull.Checked := false;
        end;
      dtDimensions:
        Result := cbSectionSideValue.Text; //meSectionSideValue.Text;
      dtConnectionKind, dtBasementType, dtPlaneMaterialType, dtRoofHipType, dtRoofHipApexType, dtRoofHipValleyType:
        Result := IntToStr(GetIDFromComboBoxRz(cbItemList));
      dtStringList:
        Result := meStringListDef.Lines.Text;
    end;
  except
  end;
end;

function TF_MakeEditPropRel.MinMaxControl(AValue: String; var AMessages: String; APropData: TPropertyData): Boolean;
begin
  Result := True;
  //if (seMinValue.Enabled) and (seMaxValue.Enabled) then
  case APropData.IDDataType of
    dtFloat, dtInteger: // 0 or 1
       begin
         if APropData.MinValue <> 0 then
           if StrToFloatU(AValue) < APropData.MinValue then
           begin
             AMessages := cMakeEditPropRel_Msg2_1;
             Result := false;
           end;
          if seMaxValue.Value <> 0 then
            if StrToFloatU(AValue) > APropData.MaxValue then
            begin
              AMessages := cMakeEditPropRel_Msg2_2;
              Result := false;
            end;
        end;
      dtDate:
        begin
         if APropData.MinValue <> 0 then
           if StrToDateU(AValue) < APropData.MinValue then
           begin
             AMessages := cMakeEditPropRel_Msg2_3;
             Result := false;
           end;
           if deMaxValue.Date <> 0 then
            if StrToDateU(AValue) > APropData.MaxValue then
            begin
              AMessages := cMakeEditPropRel_Msg2_4;
              Result := false;
            end;
        end;
  end;

  {case APropData.IDDataType of
    dtFloat, dtInteger: // 0 or 1
       begin
         if seMinValue.Value <> 0 then
           if StrToFloatU(AValue) < seMinValue.Value then
           begin
             AMessages := cMakeEditPropRel_Msg2_1;
             Result := false;
           end;
          if seMaxValue.Value <> 0 then
            if StrToFloatU(AValue) > seMaxValue.Value then
            begin
              AMessages := cMakeEditPropRel_Msg2_2;
              Result := false;
            end;
        end;
      dtDate:
        begin
         if deMinValue.Date <> 0 then
           if StrToDateU(AValue) < deMinValue.Date then
           begin
             AMessages := cMakeEditPropRel_Msg2_3;
             Result := false;
           end;
           if deMaxValue.Date <> 0 then
            if StrToDateU(AValue) > deMaxValue.Date then
            begin
              AMessages := cMakeEditPropRel_Msg2_4;
              Result := false;
            end;
        end;
  end;}
end;

function TF_MakeEditPropRel.SetPropertyParams(AIDProperty: Integer; AGUIDProperty: String; var APropertyData: TPropertyData): Boolean;
var
  PropertyData: TPropertyData;
  SpinEditDecimals: Integer;
  ValueFloat: Double;
begin
  Result := false;
  ZeroMemory(@PropertyData, SizeOf(TPropertyData));

  PropertyData := TF_Main(GForm).DM.GetPropertyData(AIDProperty, AGUIDProperty, FSpravochnik);
  if (PropertyData.ID = 0) and (PropertyData.GUID = '') then //11.01.2011
    PropertyData := TF_Main(GForm).FNormBase.DM.GetPropertyData(AIDProperty, AGUIDProperty, FSpravochnik); //11.01.2011

  if (PropertyData.ID <> 0) or (PropertyData.GUID <> '') then
  begin
    Result := true;
    APropertyData := PropertyData;
  end;

  if Result then
    BtnLoad3DSModel.Enabled := (PropertyData.SysName = '3DS_MODEL');

  edSysName.Text := PropertyData.SysName;
  if PropertyData.Izm <> '' then
    edIzm.Text := PropertyData.Izm
  else
    edIzm.Text := GetNameUOMForProperty('', PropertyData.SysName, TF_Main(GForm).FUOM);
  cbValueReq.Checked := IntToBool(PropertyData.ValueReq);
  if PropertyData.IDDataType > 0 then
    SelectItemByIDinComboRz(cbDataType, PropertyData.IDDataType)
  else
    cbDataType.ItemIndex := -1;

  edDescription.Text := PropertyData.Description;
  cbIsForWholeComponent.Checked := PropertyData.IsForWholeComponent = biTrue;

  seMaxValue.Visible := false;
  seMinValue.Visible := false;
  deMaxValue.Visible := false;
  deMinValue.Visible := false;
  case PropertyData.IDDataType of
    dtDate:
      begin
        deMaxValue.Visible := true;
        deMinValue.Visible := true;

        deMinValue.Date := PropertyData.MinValue;
        deMaxValue.Date := PropertyData.MaxValue;
      end;
    dtInteger, dtFloat:
      begin
        SpinEditDecimals := 0;
        if PropertyData.IDDataType = dtFloat then
          SpinEditDecimals := FloatPrecision;

        seMaxValue.Visible := true;
        seMinValue.Visible := true;

        seMaxValue.IntegersOnly := (PropertyData.IDDataType = dtInteger);
        seMinValue.IntegersOnly := (PropertyData.IDDataType = dtInteger);
        seMaxValue.Decimals := SpinEditDecimals;
        seMinValue.Decimals := SpinEditDecimals;

        ValueFloat := PropertyData.MaxValue;
        PropValueInUOM(ValueFloat, PropertyData.SysName, umMetr, TF_Main(GForm).FUOM);
        seMaxValue.Value := ValueFloat;

        ValueFloat := PropertyData.MinValue;
        PropValueInUOM(ValueFloat, PropertyData.SysName, umMetr, TF_Main(GForm).FUOM);
        seMinValue.Value := ValueFloat;
      end;
    else
    begin
      seMaxValue.Visible := true;
      seMinValue.Visible := true;
      seMaxValue.Value := 0;
      seMinValue.Value := 0;
    end;
  end;
end;

procedure TF_MakeEditPropRel.SetPropertyValue(AValue, ASysName: String);
var
  DataType: Integer;
  ValueFloat: Double;
  LastItem: Integer;
begin
  edValue.Visible := false;
  cbCompStateType.Visible := false;
  cbValue.Visible := false;
  seValue.Visible := false;
  colorbValue.Visible := false;
  deValue.Visible := false;
  cbCableCanalElemntTypes.Visible := false;
  pnSectionSideValue.Visible := false;  //meSectionSideValue.Visible := false;
  cbItemList.Visible := false;
  meStringListDef.Visible := false;

  seValue.Value := 0;

  DataType := GetIDFromComboBoxRz(cbDataType);

  FValueControl := nil;
  Case DataType of
    dtFloat:
      begin
        seValue.Visible := true;
        seValue.IntegersOnly := false;
        if ( (edSysName.Text = 'WIDTH') or (edSysName.Text = 'HEIGHT') or (edSysName.Text = 'DEPTH') ) and ( (EdIzm.Text = 'м.') or (EdIzm.Text = 'm.') or (EdIzm.Text = 'м') or (EdIzm.Text = 'm') )  then
          seValue.Decimals := 4
        else
          seValue.Decimals := FloatPrecision;
        if AValue <> '' then
          try
            ValueFloat := StrToFloatU(CorrectStrToFloat(AValue));
            PropValueInUOM(ValueFloat, ASysName, umMetr, TF_Main(GForm).FUOM);
            seValue.Value := ValueFloat;
          except
          end;
        FValueControl := seValue;
      end;
    dtInteger:
      begin
        seValue.Visible := true;
        seValue.IntegersOnly := true;
        seValue.Decimals := 0;
        if AValue <> '' then
          try
            ValueFloat := StrToFloatU(AValue);
            PropValueInUOM(ValueFloat, ASysName, umMetr, TF_Main(GForm).FUOM);
            seValue.Value := ValueFloat;
          except
          end;
        FValueControl := seValue;
      end;
    dtBoolean:
      begin
        cbValue.Visible := true;
        cbValue.Checked := true;
        try
          cbValue.Checked := IntToBool(StrToInt(AVAlue));
        except
        end;
        FValueControl := cbValue;
      end;
    dtString:
      begin
        edValue.Visible := true;
        edValue.Text := AValue;
        FValueControl := edValue;
      end;
    dtDate:
      begin
        deValue.Visible := true;
        if AValue <> '' then
          deValue.Date := StrToDateU(AValue);
        FValueControl := deValue;
      end;
    dtCompStateType:
      begin
        cbCompStateType.Visible := true;
        if AValue <> '' then
          SelectItemByIDinComboRz(cbCompStateType, StrToInt(AValue));
        FValueControl := cbCompStateType;
      end;
    dtColor:
      begin
        colorbValue.Visible := true;
        if AValue <> '' then
          colorbValue.SelectedColor := StrToInt(AValue);
        FValueControl := colorbValue;
      end;
    dtCableCanalElementType:
      begin
        cbCableCanalElemntTypes.Visible := true;
        if AValue <> '' then
          SelectItemByIDinComboRz(cbCableCanalElemntTypes, StrToInt(AValue));
        //CheckBox_PNotNull.Enabled := false;
        //CheckBox_PNotNull.Checked := false;
        FValueControl := cbCableCanalElemntTypes;
      end;
    dtDimensions:
      begin
        pnSectionSideValue.Visible := true;
        cbSectionSideValue.Text := AValue;
        if cbSectionSideValue.Text = '' then
          if FTableKind = tkComponent then
            cbSectionSideValue.Text := cSectionSideDefValue;
        FValueControl := cbSectionSideValue;

        {meSectionSideValue.Visible := true;
        meSectionSideValue.Text := AValue;
        if meSectionSideValue.Text = '' then
          meSectionSideValue.Text := cSectionSideDefValue;}
      end;
    dtConnectionKind:
      begin
        ClearComboBoxRz(cbItemList);
        AddIDToComboRz(-1, '', cbItemList);
        AddIDToComboRz(tckHubOfPipe, tcknHubOfPipe, cbItemList);
        AddIDToComboRz(tckCapillarySoldering, tcknCapillarySoldering, cbItemList);
        AddIDToComboRz(tckMechanicalCompressive, tcknMechanicalCompressive, cbItemList);
        AddIDToComboRz(tckMechanicalPress, tcknMechanicalPress, cbItemList);
        AddIDToComboRz(tckMechanicalTread, tcknMechanicalTread, cbItemList);
        AddIDToComboRz(tckPress, tcknPress, cbItemList);
        AddIDToComboRz(tckWeldingConnection, tcknWeldingConnection, cbItemList);
        AddIDToComboRz(tckWeldingButt, tcknWeldingButt, cbItemList);
        AddIDToComboRz(tckWeldHubOfPipe, tcknWeldHubOfPipe, cbItemList);
        AddIDToComboRz(tckWeldElectric, tcknWeldElectric, cbItemList);

        cbItemList.Visible := true;

        if FLastTubeConnectcKind <> -1 then
          SelectItemByIDinComboRz(cbItemList, FLastTubeConnectcKind)
        else
        if AValue <> '' then
          SelectItemByIDinComboRz(cbItemList, StrToInt(AValue))
        else
          SelectItemByIDinComboRz(cbItemList, FLastTubeConnectcKind);

        FValueControl := cbItemList;
      end;
    dtStringList:
      begin
        meStringListDef.Visible := true;
        meStringListDef.BringToFront;
        meStringListDef.Lines.Text := AValue;
      end;
    //dtBasementType:
//      begin
//        ClearComboBoxRz(cbItemList);
//        AddIDToComboRz(-1, '', cbItemList);
//        AddIDToComboRz(btnStrip, btnStrip, cbItemList);
//        AddIDToComboRz(btnColumnar, btnColumnar, cbItemList);
//
//        if FLastBasementType <> -1 then
//          SelectItemByIDinComboRz(cbItemList, FLastBasementType)
//        else
//        if GValue <> '' then
//          SelectItemByIDinComboRz(cbItemList, StrToInt(GValue))
//        else
//          SelectItemByIDinComboRz(cbItemList, FLastBasementType);
//        cbItemList.Visible := true;
//      end;
    dtPlaneMaterialType, dtRoofHipType, dtRoofHipApexType, dtRoofHipValleyType:
      begin
        FillPropValuesByDataType(cbItemList.Items, DataType);
        cbItemList.Visible := true;
        
        LastItem := FLastTypeItem[DataType];
        if LastItem <> -1 then
          SelectItemByIDinComboRz(cbItemList, LastItem)
        else if AValue <> '' then
          SelectItemByIDinComboRz(cbItemList, StrToInt(AValue));
        FLastTypeItem[DataType] := GetIDFromComboBoxRz(cbItemList);
      end;
  end;
end;


procedure TF_MakeEditPropRel.FormCreate(Sender: TObject);
//var
//  i: Integer;
begin
  cbDataType.Items.BeginUpdate;
  try
    AddIDToComboRz(dtFloat, cMakeEditPropRel_Msg3_1, cbDataType);
    AddIDToComboRz(dtInteger, cMakeEditPropRel_Msg3_2, cbDataType);
    AddIDToComboRz(dtBoolean, cMakeEditPropRel_Msg3_3, cbDataType);
    AddIDToComboRz(dtString, cMakeEditPropRel_Msg3_4, cbDataType);
    AddIDToComboRz(dtDate, cMakeEditPropRel_Msg3_5, cbDataType);
    AddIDToComboRz(dtCompStateType, cMakeEditPropRel_Msg3_6, cbDataType);
    AddIDToComboRz(dtColor, cMakeEditPropRel_Msg3_7, cbDataType);
    AddIDToComboRz(dtCableCanalElementType, cMakeEditPropRel_Msg3_8, cbDataType);
    AddIDToComboRz(dtDimensions, cDimensionsB, cbDataType);
    AddIDToComboRz(dtConnectionKind, cNameConnectionKind, cbDataType);
    AddIDToComboRz(dtStringList, cNameStringListB, cbDataType);
    //AddIDToComboRz(dtBasementType, cNameBasementTypeB, cbDataType);
    AddIDToComboRz(dtPlaneMaterialType, cNamePlaneMaterialTypeB, cbDataType);
    AddIDToComboRz(dtRoofHipType, cNameRoofHipTypeB, cbDataType);
    AddIDToComboRz(dtRoofHipApexType, cNameRoofHipApexTypeB, cbDataType);
    AddIDToComboRz(dtRoofHipValleyType, cNameRoofHipValleyTypeB, cbDataType);

    AddIDToComboRz(oitProjectible, cMakeEditPropRel_Msg3_9, cbCompStateType);
    AddIDToComboRz(oitActive, cMakeEditPropRel_Msg3_10, cbCompStateType);

    AddIDToComboRz(contNone, '', cbCableCanalElemntTypes);
    AddIDToComboRz(contCork, ctnCork, cbCableCanalElemntTypes);
    AddIDToComboRz(contAnglePlane, ctnAnglePlane, cbCableCanalElemntTypes);
    AddIDToComboRz(contTjoin, ctnTjoin, cbCableCanalElemntTypes);
    AddIDToComboRz(contCross, ctnCross, cbCableCanalElemntTypes);
    AddIDToComboRz(contAngleIn, ctnAngleIn, cbCableCanalElemntTypes);
    AddIDToComboRz(contAngleOut, ctnAngleOut, cbCableCanalElemntTypes);
    AddIDToComboRz(contADapter, ctnADapter, cbCableCanalElemntTypes);
    AddIDToComboRz(contConnector, ctnConnector, cbCableCanalElemntTypes);
    AddIDToComboRz(contWallCork, ctnWallCork, cbCableCanalElemntTypes);
  finally
    cbDataType.Items.EndUpdate;
  end;


  seValue.Max := 0;
  //seMinValue.Max := 0;
  //seMaxValue.Max := 0;

  meStringListDef.MaxLength := cnstPropValMaxLen;

  lbValue.Caption := '';
  btSectionSideValue.Hint := cSendValueToDimensionsSprav;
  //SetCableChannelSectionMaskEditProps(meSectionSideValue.Properties);

  FLastTypeItem := TIntList.Create;
  //for i := 0 to dtLast do
  //  FLastTypeItem.Add(-1);
end;

procedure TF_MakeEditPropRel.FormDestroy(Sender: TObject);
begin
  ClearComboBoxRz(cbName);
  ClearComboBoxRz(cbDataType);
  ClearComboBoxRz(cbCrossProperty);

  FLastTypeItem.Free;
end;

procedure TF_MakeEditPropRel.cbNameChange(Sender: TObject);
var
  IDProperty: Integer;
  GUIDProperty: String;
begin
  IDProperty := GetIDFromComboBoxRz(cbName);
  GUIDProperty := GetGUIDFromComboBoxRz(cbName);

  SetPropertyParams(IDProperty, GUIDProperty, FPropertyData);
  SetPropertyValue(FPropertyData.DefValue, FPropertyData.SysName);
end;

procedure TF_MakeEditPropRel.btOkClick(Sender: TObject);
var
  Messg: String;
  PropValue: String;
  MinMaxControlMessg: String;
  Prop: TProperty;

  procedure AddToMessg(AText: String);
  begin
    Messg := Messg + #10+#13+'-'+AText;
  end;

begin
  Messg := '';
  if GetGUIDFromComboBoxRz(cbName) = '' then
    AddToMessg(cMakeEditPropRel_Msg4);

  PropValue := GetPropertValue;
  //Tolik 22/07/2021 -- для 3Д моделек приписать относительные пути, чтобы можно было
  //                    поставлять вместе с базой (новой версией приложения)
  if FPropertyData.SysName = '3DS_MODEL' then
  begin
    if pos('\\', PropValue) = 0 then // если не сетевой путь
    begin
      if pos(UpperCase(ExeDir), UpperCase(PropValue)) = 1 then // если начинается с пути установленного приложения
      begin
        PropValue := StringReplace(PropValue, ExeDir, '', [rfIgnoreCase]); // удалить путь к установленному приложению
        edValue.Text := PropValue;
      end;
    end;
  end;
  //
  MinMaxControlMessg := '';
  MinMaxControl(PropValue, MinMaxControlMessg, FPropertyData);
  if MinMaxControlMessg <> '' then
    AddToMessg(MinMaxControlMessg);

  if FMakeEdit = meMake then
    if CheckLocate(FDataSet, fnIDProperty, FPropertyData.ID) then
      AddToMessg(cMakeEditPropRel_Msg5_1+' "'+FPropertyData.Name+'" '+cMakeEditPropRel_Msg5_2);

  if Messg <> '' then
  begin
    ModalResult := mrNone;
    MessageModal(Messg, ApplicationName, MB_ICONINFORMATION or MB_OK);
  end
  else
  if Assigned(FOnCheckPropRelValue) then
  begin
    ZeroMemory(@Prop, SizeOf(Prop));
    FormDataToProperty(Prop);
    //FOnCheckPropRelValue;
    if Not FOnCheckPropRelValue(Self, @Prop, FDataSet) then
      ModalResult := mrNone;
  end;
end;

procedure TF_MakeEditPropRel.cbIsCrossControlClick(Sender: TObject);
var
  Checked: Boolean;
begin
  Checked := TRzCheckBox(Sender).Checked;
  cbCrossProperty.Enabled := Checked;
  btChoiceCrossProperty.Enabled := Checked;
end;

function TF_MakeEditPropRel.ChoiceProperty(AComboBox, AOtherComboBox: TRzComboBox): Boolean;
var
  NewPropertyID: Integer;
  CurrID: Integer;
  CurrGUID: string;

  OtherComboGUID: string;
begin
  Result := false;
  NewPropertyID := -1;
  CurrID := GetIDFromComboBoxRz(AComboBox);
  CurrGUID := GetGUIDFromComboBoxRz(AComboBox);

  OtherComboGUID := GetGUIDFromComboBoxRz(AOtherComboBox);

  if TF_Main(GForm).GDBMode = bkNormBase then
  begin
    NewPropertyID := TF_Main(GForm).FNormBase.DM.GetPropertyIDFromGuide(CurrID, fmEdit, FItemType);
    FillComboBoxRz(AComboBox, F_NormBase, true, tnProperties, fnID, fnName, '', NewPropertyID, '',  FItemFilter);
    FillComboBoxRz(AOtherComboBox, F_NormBase, true, tnProperties, fnID, fnName, '', -1, '', FItemFilter);
  end
  else
  if TF_Main(GForm).GDBMode = bkProjectManager then
  begin
    if ShowCurrProjectProperties(vkProperty, CurrGUID) then
      if TF_Main(GForm).GSCSBase.CurrProject.Spravochnik.LastProperty <> nil then
      begin
        NewPropertyID := TF_Main(GForm).GSCSBase.CurrProject.Spravochnik.LastProperty.PropertyData.ID;
        TF_Main(GForm).GSCSBase.CurrProject.Spravochnik.LoadPropertiesToStrings(AComboBox.Items, true);
        TF_Main(GForm).GSCSBase.CurrProject.Spravochnik.LoadPropertiesToStrings(AOtherComboBox.Items, true);

        SelectItemByGUIDinComboRz(AComboBox, TF_Main(GForm).GSCSBase.CurrProject.Spravochnik.LastProperty.PropertyData.GUID);
        SelectItemByGUIDinComboRz(AOtherComboBox, OtherComboGUID);
      end;
  end;

  TF_Main(GForm).FNormBase.DM.LoadInputStringsToTStrings(cbSectionSideValue.Items, stDimensions);

  if NewPropertyID > 0 then
    Result := true;
end;

function TF_MakeEditPropRel.DefineItemFilter(AItemType: Integer): String;
var
  ItemFieldName: String;
begin
  Result := '';
  ItemFieldName := ItemTypeToIsOwnerFieldName(AItemType);
  if ItemFieldName <> '' then
    Result := ItemFieldName +' = '''+IntToStr(biTrue)+'''';
end;

procedure TF_MakeEditPropRel.SetControls;
begin
  cbName.ReadOnly := (FProperty.IsDefault = biTrue);
  if cbName.ReadOnly then
    cbName.SelLength := 0;
  cbName.Enabled := (FProperty.IsDefault = biFalse);

  //*** отображаемые контролы
  cbTakeIntoComplect.Visible := ((FTableKind = tkComponent) or (FTableKind = tkCompTypePropRelation));
  cbTakeIntoJoin.Visible := ((FTableKind = tkComponent) or (FTableKind = tkCompTypePropRelation));
  cbIsTakeJoinForPoints.Visible := ((FTableKind = tkComponent) and (FItemType = itComponCon));
  pnCrossControl.Visible := (FTableKind = tkComponent);

  pnIsForWholeComponent.Visible := (FTableKind = tkComponent) and (FItemType = itComponLine);

  //cbIsCrossControl.Visible := (FTableKind = tkComponent);
  //cbCrossProperty.Visible := (FTableKind = tkComponent);
  //btChoiceCrossProperty.Visible := (FTableKind = tkComponent);

  //*** Enable/Disable controls
  pnCrossControl.Enabled := (cbTakeIntoComplect.Checked or cbTakeIntoJoin.Checked);
  cbIsTakeJoinForPoints.Enabled := (cbTakeIntoJoin.Checked);

  btChoiceProperty.Visible := (FMemTableProps = nil);
  btChoiceCrossProperty.Visible := (FMemTableProps = nil);

  if GUseArhOnlyMode then
  begin
    cbName.ReadOnly := FMakeEdit <> meMake;

    btChoiceProperty.Visible := false;
    cbTakeIntoJoin.Visible := false;
    cbTakeIntoComplect.Visible := false;
    cbIsTakeJoinForPoints.Visible := false;
    cbIsCrossControl.Visible := false;
    cbCrossProperty.Visible := false;
    btChoiceCrossProperty.Visible := false;
  end;
end;

procedure TF_MakeEditPropRel.cbTakeIntoJoinClick(Sender: TObject);
begin
  SetControls;
end;

procedure TF_MakeEditPropRel.cbTakeIntoComplectClick(Sender: TObject);
begin
  SetControls;
end;

procedure TF_MakeEditPropRel.btChoicePropertyClick(Sender: TObject);
begin
  if FProperty.IsDefault = biTrue then
    MessageModal(cMakeEditPropRel_Msg6, ApplicationName,
      MB_ICONINFORMATION or MB_OK)
  else
  if ChoiceProperty(cbName, cbCrossProperty) then
    cbNameChange(cbName);
end;

procedure TF_MakeEditPropRel.btChoiceCrossPropertyClick(Sender: TObject);
begin
  ChoiceProperty(cbCrossProperty, cbName);
end;

procedure TF_MakeEditPropRel.cbValueReqMouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  TRzCheckBox(Sender).Checked := Not TRzCheckBox(Sender).Checked;
end;

procedure TF_MakeEditPropRel.cbValueReqKeyUp(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
  Key := 0;
end;

procedure TF_MakeEditPropRel.seValueKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key = '-' then
    Key := #0;
end;

procedure TF_MakeEditPropRel.btSectionSideValueClick(Sender: TObject);
begin
  TF_Main(GForm).FNormBase.DM.SendTextToInputStringsWithAddToTStrings(cbSectionSideValue.Text, stDimensions, true, cbSectionSideValue.Items);
end;

procedure TF_MakeEditPropRel.cbItemListChange(Sender: TObject);
var
  DataType: Integer;
begin
  DataType := GetIDFromComboBoxRz(cbDataType);
  if DataType = dtConnectionKind then
    FLastTubeConnectcKind := GetIDFromComboBoxRz(cbItemList)
  else
  if DataType = dtBasementType then
    FLastBasementType := GetIDFromComboBoxRz(cbItemList);

  FLastTypeItem[DataType] := GetIDFromComboBoxRz(cbItemList);
end;

procedure TF_MakeEditPropRel.FormShow(Sender: TObject);
begin
  try
    if FMakeEdit = meMake then
      cbName.SetFocus
    else
    if FMakeEdit = meEdit then
      if FValueControl <> nil then
        if FValueControl.Showing then
        begin
          FValueControl.SetFocus;
          if FValueControl is TCustomEdit then
            TCustomEdit(FValueControl).SelectAll;
        end;
  except
    on E: Exception do AddExceptionToLogEx('TF_MakeEditPropRel.FormShow', E.Message);
  end;
end;

procedure TF_MakeEditPropRel.seValueChange(Sender: TObject);
begin
  //
end;

procedure TF_MakeEditPropRel.BtnLoad3DSModelClick(Sender: TObject);
var dlg: TOpen3dsModelDialog;
    currVal: string; // Tolik 23/07/2021 --
begin
  //dlg := TOpen3dsModelDialog.create(Self);//TOpenDialog.Create(Self);
  dlg := TOpen3dsModelDialog.create(nil);//TOpenDialog.Create(Self);
  //dlg.Filter := 'Model files (*.3DS)|*.3DS';
  dlg.Title := 'Sel3dModFile';
  dlg.Options := [ofFileMustExist];
  dlg.InitialDir := ExeDir + '\' + '3DModels';

  // Tolik 23/07/2021 --
  currVal := edValue.Text;
  if (Pos('\\', currVal) = 0) or (Pos(':', currVal) = 0) then
    currVal := ExeDir + currVal;
  if FileExists(currVal) then
    dlg.FileName := currVal;
  //dlg.FindComponent(currVal);
  //dlg.Files.Add(currVal);
  //Application.ProcessMessages;
  //
  if dlg.Execute then
  begin
    if dlg.FileName <> '' then
    //Tolik 22/07/2021 --
    //edValue.Text := dlg.FileName;
    begin // учесть относительный путь к моделькам в папке программы
      edValue.Text := dlg.FileName;
      if Pos('\\', edValue.Text) = 0 then
      begin
        if Pos(ExeDir, dlg.FileName) = 1 then
          edValue.Text := StringReplace(edValue.Text, ExeDir,'',[rfIgnoreCase]);
      end;
    end;
  end;
  dlg.free;
end;

end.
