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

#include <vcl.h>
#pragma hdrstop


#include "Sect_Param_Weld.h"
#include "ListConst.h"
#include "Draw3D_beam.h"
#include "String_doubleUnit.h"


//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TSect_Param_WeldFrame *Sect_Param_WeldFrame;


extern SECT_DVUTAVR *sect_w;

#ifdef EMBRAC
static String text, header;
#else
static AnsiString text, header;
#endif

//---------------------------------------------------------------------------
__fastcall TSect_Param_WeldFrame::TSect_Param_WeldFrame(TComponent* Owner)
		: TFrame(Owner)
{
   kf_dvut = 8.0;
}
//---------------------------------------------------------------------------
//-------------------------------------------------------------------------
void __fastcall TSect_Param_WeldFrame::Write_sect(TObject *Sender) {
	 AnsiString* NameProfil;
	 int n_profil, i;

		Edit_b->Text="0";
		Edit_h_1->Text="0";
		Edit_b1->Text="0";
		Edit_h1->Text="0";
		Edit_b2->Text="0";
		Edit_h2->Text="0";
            // Заполнить поля
		Weld_Change(Sender);

		DrawSect(Sender, true);

}
//-------------------------------------------------------------------------
int __fastcall TSect_Param_WeldFrame::Read_sect() {
	int rc;
	double * ParamProfil;

	sect_w->flag_concl=false;
	sect_w->n_group = -1;
	  rc=String_double_zero_plus(Label_b1->Caption,Edit_b1->Text, &(sect_w->b1));
	  if (rc>0) return rc;
	  rc=String_double_zero_plus(Label_h1->Caption,Edit_h1->Text, &(sect_w->h1));
	  if (rc>0) return rc;
	  rc=String_double_zero_plus(Label_h_1->Caption,Edit_h_1->Text, &(sect_w->h));
	  if (rc>0) return rc;
	  rc=String_double_zero_plus(Label_b->Caption, Edit_b->Text, &(sect_w->b));
	  if (rc>0) return rc;
	  rc=String_double_zero_plus(Label_b2->Caption, Edit_b2->Text, &(sect_w->b2));
	  if (rc>0) return rc;
	  rc=String_double_zero_plus(Label_h2->Caption, Edit_h2->Text, &(sect_w->h2));
	  if (rc>0) return rc;
	  rc=String_double_zero_plus(Label_kf_dvut->Caption, Edit_kf_dvut->Text, &(kf_dvut));
	  if (rc>0) return rc;
	  sect_w->flag_concl=true;
	  sect_w->n_group = -1;
	  sect_w->n_profil = -1;
	  sect_w->index_orient = 0;
	  sect_w->b1 = MAX(10, sect_w->b1);
	  sect_w->b2 = MAX(10, sect_w->b2);
	  sect_w->h = MAX(10, sect_w->h);
	  sect_w->b = MAX(1, sect_w->b);
	  sect_w->h1 = MAX(1, sect_w->h1);
	  sect_w->h2 = MAX(1, sect_w->h2);
	return 0;
}
     
//--------------------------------------------------------------------
void __fastcall TSect_Param_WeldFrame::DrawSect(TObject *Sender, bool flag_read)
{
  // Если flag_read = false, не записывать сечение
  int rc;
  SECT_DVUTAVR sect_buf;

  memcpy((char*)&sect_buf, (char*)sect_w, sizeof(SECT_DVUTAVR));

  // Прочитать параметры сечения
  rc = Read_sect();
  if (rc>0) return;

  Draw_dvutavr_weld_plane(Image1, sect_w);


}
//---------------------------------------------------------------------------

void  __fastcall TSect_Param_WeldFrame::Check_Original() {

      //Label_Sect->Caption="";
      //Label_Side->Caption="";
      TRect NewRect = Rect(0, 0, Image1->Width,
                           Image1->Height);

      Image1->Canvas->Brush->Color = clWhite;
      Image1->Canvas->FillRect(NewRect);
      Image1->Canvas->Rectangle(0, 0, Image1->Width,
                           Image1->Height);

	  Edit_b->Text="0";
      Edit_h_1->Text="0";
      Edit_b1->Text="0";
	  Edit_h1->Text="0";
      Edit_h2->Text="0";
	  Edit_b2->Text="0";


}
//------------------------------------------------------------------
//  Проверить модификацию
void  __fastcall TSect_Param_WeldFrame::Check_Modify(TObject *Sender) {

	  TRect NewRect = Rect(0, 0, Image1->Width,
                           Image1->Height);

      Image1->Canvas->Brush->Color = clWhite;
      Image1->Canvas->FillRect(NewRect);
      Image1->Canvas->Rectangle(0, 0, Image1->Width,
                           Image1->Height);

	  // Нарисовать сечение
	  DrawSect(Sender, false);


}
//----------------------------------------------------------------------------------
//
//
// Изменение состояния CheckBox

void __fastcall TSect_Param_WeldFrame::CheckBox1MouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{
       Check_Modify(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TSect_Param_WeldFrame::Edit_b1KeyDown(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
     if (Key == VK_RETURN) Edit_h1->SetFocus();

}
//---------------------------------------------------------------------------

void __fastcall TSect_Param_WeldFrame::Edit_h1KeyDown(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
   if (Key == VK_RETURN) Edit_h_1->SetFocus();

}
//---------------------------------------------------------------------------


void __fastcall TSect_Param_WeldFrame::Edit_h_1KeyDown(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
   if (Key == VK_RETURN) Edit_b->SetFocus();

}
//---------------------------------------------------------------------------

void __fastcall TSect_Param_WeldFrame::Edit_bKeyDown(TObject *Sender, WORD &Key,
	  TShiftState Shift)
{
   if (Key == VK_RETURN) Edit_b2->SetFocus();

}
//---------------------------------------------------------------------------

void __fastcall TSect_Param_WeldFrame::Edit_b2KeyDown(TObject *Sender, WORD &Key,
	  TShiftState Shift)
{
   if (Key == VK_RETURN) Edit_h2->SetFocus();

}
//---------------------------------------------------------------------------

void __fastcall TSect_Param_WeldFrame::Edit_h2KeyDown(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
           if (Key == VK_RETURN) Edit_b1->SetFocus();

}
//---------------------------------------------------------------------------
void __fastcall TSect_Param_WeldFrame::Weld_Change(TObject *Sender)
{
	   Edit_b->Text = FloatToStrF(sect_w->b,ffFixed,15,1);
	   Edit_h_1->Text = FloatToStrF(sect_w->h,ffFixed,15,0);
	   Edit_b1->Text = FloatToStrF(sect_w->b1,ffFixed,15,0);
	   Edit_h1->Text = FloatToStrF(sect_w->h1,ffFixed,15,0);
	   Edit_b2->Text = FloatToStrF(sect_w->b2,ffFixed,15,0);
	   Edit_h2->Text = FloatToStrF(sect_w->h2,ffFixed,15,0);

	   Edit_kf_dvut->Text = FloatToStr(kf_dvut);
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------


void __fastcall TSect_Param_WeldFrame::Edit_h2Exit(TObject *Sender)
{
	  DrawSect(Sender, false);
	  BitBtn_Apply->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TSect_Param_WeldFrame::BitBtn_ApplyClick(TObject *Sender)
{
       // Прочитать параметры сечения
  int rc = Read_sect();
  if (rc>0) return;

  Draw_dvutavr_weld_plane(Image1, sect_w);

}
//---------------------------------------------------------------------------

