//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Define_Material_ARSS.h"
#include "Steel_param_ARSS.h"
#include "TypeData.h"
#include "ProcExt.h"
#include "Data_Gen.h"
#include "ListConst.h"
#include "String_doubleUnit.h"

//#include "message.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TDefine_Material_ARSSForm *Define_Material_ARSSForm;

extern MATER MaterProp[MAX_MATERIAL];
extern int   Material_Number;


//---------------------------------------------------------------------------
__fastcall TDefine_Material_ARSSForm::TDefine_Material_ARSSForm(TComponent* Owner)
		: TForm(Owner)
{
   Index_profil = 0;
   Prefix="";
}
//---------------------------------------------------------------
void __fastcall TDefine_Material_ARSSForm::FormShow(TObject *Sender)
{
   int n_steel;

   Caption="Выбор материала";
// Заполнение ComboBox_steel
   ComboBox_steelChange(Sender);

   StringGrid_Prop->ColCount = 3;
   StringGrid_Prop->Cells[0][0] = "Толщина проката, мм";
   StringGrid_Prop->Cells[1][0] = "  Ryn";
   StringGrid_Prop->Cells[2][0] = "  Run";
}
//---------------------------------------------------------------------------
// Прочитать материал
void __fastcall TDefine_Material_ARSSForm::Read_Mater(TObject *Sender)
{
	 int i, j, rc;
	 double matG, matE, matNu;

	 //i = StrToInt(Edit_Num->Text);
	 i = 1;
	 Material_Number = i;
	 //---------------------------------------------------------
	 rc=String_double_plus(Label3->Caption, Edit_E->Text, &(MaterProp[i].E));
	 if (rc>0) return;
	 rc=String_double_zero_plus(Label4->Caption, Edit_G->Text, &(MaterProp[i].G));
	 if (rc>0) return;
	 rc=String_double_plus(Label5->Caption, Edit_nu->Text, &(MaterProp[i].nu));
	 if (rc>0) return;
	 rc=String_double_zero_plus(Label9->Caption, Edit_dens->Text, &(MaterProp[i].dens));
	 if (rc>0) return;
	 rc=String_double_zero_plus(Label_gamma_m->Caption, Edit_gamma_m->Text, &(MaterProp[i].gamma_m));
	 if (rc>0) return;
	 // Поиск толщины меньшей или равной 20 мм
	 for (j=0; j<=mater_param.n_row; j++) {
		 if (mater_param.thick_row[j] >=20 ) break;
	 }
	 if (i>mater_param.n_row)
		 j -=2;
	 else
		 j--;
	 MaterProp[i].Ru = mater_param.Ru_row[j];
	 MaterProp[i].Ry = mater_param.Ry_row[j];

	 strcpy(MaterProp[i].title, Name_steel.c_str());
	 AnsiString Astr = ComboBox_steel->Text;
	 strcpy(MaterProp[i].title, Astr.c_str());
     MaterProp[i].index_GOST = ComboBox_gost->ItemIndex;
	 Edit_Num->Text = IntToStr(i);

	 return;
}
//---------------------------------------------------------------------------
void __fastcall TDefine_Material_ARSSForm::BitBtn_OKClick(TObject *Sender)
{
	 Close();
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Получить параметры материала
void Get_Mater_Param_0(int i, double *E, double *G, double *nu) {


     *G = MaterProp[i].G;
     *nu = MaterProp[i].nu;
     *E = MaterProp[i].E;
	 if (MaterProp[i].E==0) {
      *E = 2*MaterProp[i].G*(1 + MaterProp[i].nu);
     }
	 if (MaterProp[i].G==0) {
	   *G = MaterProp[i].E/(2*(1 + MaterProp[i].nu));
     }
	 if (MaterProp[i].nu==0) {
	   *nu = MaterProp[i].E/MaterProp[i].G-2;
	 }
}
//---------------------------------------------------------------------------
// Заполнение ComboBox_steel
//----------------------------------------------------------------------
void __fastcall TDefine_Material_ARSSForm::ComboBox_steelChange(
	  TObject *Sender)
{
	   int i, rc;

	   Name_steel = ComboBox_steel->Text + Prefix;

	   rc = Get_Mater_param(Name_steel.c_str(), &mater_param);        //mater_param.Ry_row[0]
	   if (rc>0) {
		  //Edit_Sig_Run->Text=0;
		  //Edit_Sig_Ryn->Text=0;
		  //Label_thick->Caption="Ошибка при получении таблицы свойств стали "+
		  //                     ComboBox_steel->Text;
		  return;
       }
	   for (i=0; i<=mater_param.n_row; i++) {
		 if (mater_param.thick_row[i] >=20 ) break;
	   }
	   if (i>mater_param.n_row)
		 i -=2;
	   else
		 i--;
	   //---------------------------------------------------------
	   //Edit_Sig_Ryn->Text = FloatToStr(mater_param.Ryn_row[i]);
	   //Edit_Sig_Run->Text = FloatToStr(mater_param.Run_row[i]);
	   //if (mater_param.thick_row[mater_param.n_row]<1000)
	   //	 Edit_max_thick->Text = FloatToStr(mater_param.thick_row[mater_param.n_row]);
	   //else
	   //  Edit_max_thick->Text = ">"+FloatToStr(mater_param.thick_row[mater_param.n_row-1]);
	   //Label_thick->Caption="для толщины проката от "+FloatToStr(mater_param.thick_row[i])+
	   //						" до "+FloatToStr(mater_param.thick_row[i+1])+" мм";

	   Edit_gamma_m->Text = "1.025";
	   //--------------------------------
	   GroupBox_Prop->Caption = " Нормативные сопротивления стали "+ ComboBox_steel->Text + ", МПа";
	   StringGrid_Prop->RowCount = mater_param.n_row + 1;
	   for (i=0; i<mater_param.n_row; i++) {
		 if (i==0) {
		   if  (mater_param.thick_row[i] == 1.0)
			 StringGrid_Prop->Cells[0][i+1] = L" \u2264 " + FloatToStr(mater_param.thick_row[i+1]);
		   else
			 StringGrid_Prop->Cells[0][i+1] = "От " + FloatToStr(mater_param.thick_row[i]) + " до " +
												  FloatToStr(mater_param.thick_row[i+1])+" включ.";

		 }
		 else {
		   if (mater_param.thick_row[i+1] < 1000)
			 StringGrid_Prop->Cells[0][i+1] = "Св. " + FloatToStr(mater_param.thick_row[i]) + " до " +
												  FloatToStr(mater_param.thick_row[i+1])+" включ.";
		   else
			 StringGrid_Prop->Cells[0][i+1] = "Св. " + FloatToStr(mater_param.thick_row[i]);
		 }
		 StringGrid_Prop->Cells[1][i+1] = mater_param.Ryn_row[i];
		 StringGrid_Prop->Cells[2][i+1] = mater_param.Run_row[i];
	   }
	   Read_Mater(Sender);

}
//---------------------------------------------------------------------------
void __fastcall TDefine_Material_ARSSForm::ComboBox_gostChange(TObject *Sender)
{
	Set_Material(Index_profil, ComboBox_gost->ItemIndex);
	ComboBox_steelChange(Sender);
}
//---------------------------------------------------------------------------
// Установить список материалов, соответствующий прокату (фасон или труба) и материал в списке по имени
void __fastcall TDefine_Material_ARSSForm::Set_Material_name(int index_profil, int index_GOST, char *title_mater)
{
	int i;
	Set_Material(index_profil, index_GOST);

	for (i = 0; i < ComboBox_steel->Items->Count; i++) {
	  if (ComboBox_steel->Items->Strings[i]==(String)title_mater) {
		  ComboBox_steel->ItemIndex = i;
		  break;
	  }
	}
	if (i==ComboBox_steel->Items->Count) {
	  ComboBox_steel->ItemIndex = 0;
	}
}
//---------------------------------------------------------------------------
// Установить список материалов, соответствующий прокату (фасон или труба)
void __fastcall TDefine_Material_ARSSForm::Set_Material(int index_profil, int index_GOST)
{
   AnsiString Steel_Hot[N_STEEL_HOT] = {STEEL_HOT};
   AnsiString Steel_Hot_GOST[N_STEEL_HOT_GOST] = {STEEL_HOT_GOST};
   AnsiString Steel_HotB[N_STEEL_HOTB] = {STEEL_HOTB};
   AnsiString Steel_HotB_R57837[N_STEEL_HOTB_R57837] = {STEEL_HOTB_R57837};
   AnsiString Steel_HotB_GOST[N_STEEL_HOTB_GOST] = {STEEL_HOTB_GOST};

	int i;
	ComboBox_gost->Items->Clear();
	ComboBox_steel->Items->Clear();
	switch (index_profil) {
	//#define DVUTAVR_STAND      0
	//#define TRUBA              1
	//#define DVUTAVR_WELD       2
	//#define PERFOR_BEAM        3
	case 0: //DVUTAVR_STAND:
	case 3: //PERFOR_BEAM:
	case 4: //COMPOSED_BEAM:
	   ComboBox_gost->Items->Add(steel_STAND_GOST_27772);
	   ComboBox_gost->Items->Add(steel_STAND_GOST_57837);
	   ComboBox_gost->Items->Add(steel_STAND_SP_16);
	   switch (index_GOST) {
	   case 0:
		 for (i=0; i<N_STEEL_HOTB_GOST; i++) {
		   ComboBox_steel->Items->Add(Steel_HotB_GOST[i]);
		 }
		 Prefix ="BG";
	   break;
	   case 1:
		 for (i=0; i<N_STEEL_HOTB_R57837; i++) {
		   ComboBox_steel->Items->Add(Steel_HotB_R57837[i]);
		 }
		 Prefix ="R";
	   break;
	   case 2:
		 for (i=0; i<N_STEEL_HOTB; i++) {
		   ComboBox_steel->Items->Add(Steel_HotB[i]);
		 }
		 Prefix ="";
	   break;
	   }
	break;
	case 1: //TRUBA:
	   ComboBox_gost->Items->Add(steel_TRUBA_GOST_27772);
	   ComboBox_gost->Items->Add(steel_TRUBA_SP_16);
	   switch (index_GOST) {
	   case 0:
		 for (i=0; i<N_STEEL_HOT_GOST; i++) {
		   ComboBox_steel->Items->Add(Steel_Hot_GOST[i]);
		 }
		 Prefix ="G";
	   break;
	   case 1:
		 for (i=0; i<N_STEEL_HOT; i++) {
		   ComboBox_steel->Items->Add(Steel_Hot[i]);
		 }
		 Prefix ="";
	   break;
	   }
	break;
	case 2: //DVUTAVR_WELD:
	   ComboBox_gost->Items->Add(steel_WELD_GOST_27772);
	   ComboBox_gost->Items->Add(steel_WELD_SP_16);
	   switch (index_GOST) {
	   case 0:
		 for (i=0; i<N_STEEL_HOT_GOST; i++) {
		   ComboBox_steel->Items->Add(Steel_Hot_GOST[i]);
		 }
		 Prefix ="G";
	   break;
	   case 1:
		 for (i=0; i<N_STEEL_HOT; i++) {
		   ComboBox_steel->Items->Add(Steel_Hot[i]);
		 }
		 Prefix ="";
	   break;
	   }
	break;
	}
	ComboBox_gost->ItemIndex = index_GOST;
	ComboBox_steel->ItemIndex = 1;
	ComboBox_steel->ItemIndex = 0;
	Index_profil = index_profil;
	//ComboBox_steelChange(Sender);
}

void __fastcall TDefine_Material_ARSSForm::BitBtn_CancelClick(TObject *Sender)
{
	Close();
}
//---------------------------------------------------------------------------

