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

#include <vcl.h>
#include <math.h>
#pragma hdrstop

#include "DrawRSU.h"
#include "SelectProfilSect.h"
#include "ListConst.h"
#include "pCanvas_func.h"
#include "StandartProfil.h"
#include "Draw3D_beam.h"
#include "Point_dvutavr.h"


//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TDrawRSUForm *DrawRSUForm;

extern  bool flag_image;
static TStandartProfil StandartProfil;


//---------------------------------------------------------------------------
__fastcall TDrawRSUForm::TDrawRSUForm(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TDrawRSUForm::FormShow(TObject *Sender)
{
   if (index_draw==0)
	 Draw_All(Sender);
   else
	 Draw_Epur(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TDrawRSUForm::Draw_Epur(TObject *Sender) {

	double sigma_max = MAX((ABS(sigma_ext)), (ABS(sigma_int)));
	sigma_max = MAX(1.0, sigma_max);
	double scale = (0.5*Image_DRsu->Height)/(2*sigma_max);
	int zero_px, zero_py;
	TPoint points[10];

	Caption = "Эпюра нормальных напряжений";
	Height = BitBtn_Close->Top + 60;
	GroupBox_q_bal->Visible = false;
	CheckBox_SignLoad->Visible = false;
	CheckBox_sysc->Visible = false;

	TRect NewRect = Rect(0, 0, Image_DRsu->Width, Image_DRsu->Height);
	Image_DRsu->Canvas->Pen->Color = clBlack;
	Image_DRsu->Canvas->Brush->Color = clWhite;
	Image_DRsu->Canvas->FillRect(NewRect);
	Image_DRsu->Canvas->Rectangle(0, 0, Image_DRsu->Width,Image_DRsu->Height);

	zero_px = Image_DRsu->Width/2;     // точка левого края фланца
	zero_py = 40;    // точка верха фланца

	pCanvas_TextOut(Image_DRsu, 10 , Image_DRsu->Height - 20, "Нормальные напряжения (МПа)");
	points[0] = Point(zero_px, zero_py);
	points[1] = Point(zero_px, Image_DRsu->Height - 60);
	points[2] = Point(zero_px + sigma_int*scale, points[1].y);
	points[3] = Point(zero_px + sigma_ext*scale, points[0].y);
	points[4] = points[0];
	pCanvas_Polyline(Image_DRsu, points, 4);
	pCanvas_Dim(Image_DRsu, points[4], points[3], orHORIZ, sideUP, sigma_ext, 1);
	pCanvas_Dim(Image_DRsu, points[2], points[1], orHORIZ, sideDOWN, sigma_int, 1);
}
//---------------------------------------------------------------------------
void __fastcall TDrawRSUForm::Draw_All(TObject *Sender) {
	 //flag_image = true;
	 q_bal = loads[5];

	Caption = "РСУ на участке конструктивного элемента";
	CheckBox_SignLoad->Visible = true;
	CheckBox_sysc->Visible = true;


	 if (q_bal == 0) {
	   GroupBox_q_bal->Visible = false;
	   Height = BitBtn_Close->Top + 60;
	 }
	 else {
	   GroupBox_q_bal->Visible = true;
	   Height = GroupBox_q_bal->Top + 80;
	 }

	 CheckBox_SignLoadClick(Sender);
	 CheckBox_CentrClick(Sender);
	 CheckBox_SideClick(Sender);
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Нарисовать профиль и оси координат
void __fastcall TDrawRSUForm::Draw_beam(bool flag_sign, bool flag_sysc)
{
   int rc;
   pBrush_Color(Image_DRsu, clBlack);
   pPen_Color(Image_DRsu, clBlack);

   switch (form_profil) {
   case DVUTAVR_STAND:
   // Сварной двутавр
   // Установить тип стандартного сечения
	 type_profil = index_profil + index_GOST_or_TU;
	 StandartProfil.SetProfil(type_profil);
   // Получить вектор имен профилей и длину вектора
	 //NameProfil = StandartProfil.GetVectorNameProfil(&n_profil);
	 rc = StandartProfil.SetParamProfil(&(com_sect.dvutavr), 0);
	 if (rc>0) return;
	 Draw_dvutavr(Image_DRsu, &com_sect.dvutavr, vISOMETR, 4.0, 70, flag_sysc, false, 0, 0, 0);
	 H_beam = com_sect.dvutavr.h + 2*com_sect.dvutavr.h1;
   break;
   case PERFOR_BEAM:
   // Перфорированный двутавр
   // Установить тип стандартного сечения
	 type_profil = index_profil + index_GOST_or_TU;
	 StandartProfil.SetProfil(type_profil);
   // Получить вектор имен профилей и длину вектора
	 //NameProfil = StandartProfil.GetVectorNameProfil(&n_profil);
	 rc = StandartProfil.SetParamProfil(&(com_sect.dvutavr), 0);

	 com_sect.dvutavr.h *= attit;
	 if (rc>0) return;
	 Draw_dvutavr(Image_DRsu, &com_sect.dvutavr, vISOMETR, 4.0, 70, flag_sysc, true, attit, alfa, 0);

	 H_beam = com_sect.dvutavr.h + 2*com_sect.dvutavr.h1;
   break;
   case TRUBA:                                  // Труба
	 StandartProfil.SetProfil_Tube(type_profil);
	 rc = StandartProfil.SetParamProfil_Tube(&(com_sect), type_profil, 0, 0);
	 if (rc>0) return;
	 Draw_tube(Image_DRsu, &com_sect, type_profil, vISOMETR, 4.0, 70, flag_sysc, 0);
	 H_beam = com_sect.rect.h + 2*com_sect.rect.h1;
   break;
   case DVUTAVR_WELD:
   // Установить тип стандартного сечения, необходим для рисования сечения
	 type_profil = typeGOST_G57837_B;
	 StandartProfil.SetProfil(type_profil);
   // Получить вектор имен профилей и длину вектора
	 //NameProfil = StandartProfil.GetVectorNameProfil(&n_profil);
	 rc = StandartProfil.SetParamProfil(&(com_sect.dvutavr), 0);
	 if (rc>0) return;
	 Draw_dvutavr_weld(Image_DRsu, &com_sect.dvutavr, vISOMETR, 4.0, 70, flag_sysc, 0);
	 H_beam = com_sect.dvutavr.h + com_sect.dvutavr.h1 + com_sect.dvutavr.h2;
   break;
   case COMPOSED_BEAM:
   // Тавр или сварной двутавр
   /* Установить тип стандартного сечения
	 type_profil = index_profil + typeGOST_G57837_B;
	 StandartProfil.SetProfil(type_profil);
   // Получить вектор имен профилей и длину вектора
	 //NameProfil = StandartProfil.GetVectorNameProfil(&n_profil);
	 rc = StandartProfil.SetParamProfil(&(com_sect.dvutavr), 0);
	 if (rc>0) return;
	 Draw_tavr(Image_DRsu, &com_sect.tavr, vISOMETR, 4.0, 70, flag_sysc, 0, 0, 0);
	 H_beam = com_sect.dvutavr.h + 2*com_sect.dvutavr.h1;
	*/
	 switch (index_composed) {
	  case TAVR_PLATE_DOWN:
	  case TAVR_PLATE_UP:
	   type_profil = index_profil + index_TAVR_GOST_or_TU;
	   StandartProfil.SetProfil(type_profil);

	   rc = StandartProfil.SetParamProfil(&(com_sect.dvutavr), n_profil);
	   if (rc>0) return;
	 //Draw_dvutavr(Image_prof, &com_sect.dvutavr, vDIMETR, 1.2, 20, true);
	 //Draw_dvutavr_oblic(Image_prof, &com_sect.dvutavr);
	   Draw_tavr_plate(Image_DRsu, &com_sect.tavr, vISOMETR, 4.0, 70, flag_sysc, 0, 0, 0,
					   plate_thick, plate_width, index_composed);
	   H_beam = com_sect.tavr.h + com_sect.dvutavr.h1 + plate_thick;
	 break;
	 case DVUTAVR_PLATE_DOWN:
	   type_profil = index_profil + index_GOST_or_TU;
	   StandartProfil.SetProfil(type_profil);

	   rc = StandartProfil.SetParamProfil(&(com_sect.dvutavr), n_profil);
	   if (rc>0) return;
	 //Draw_dvutavr(Image_prof, &com_sect.dvutavr, vDIMETR, 1.2, 20, true);
	 //Draw_dvutavr_oblic(Image_prof, &com_sect.dvutavr);
	   Draw_dvutavr_plate(Image_DRsu, &com_sect.dvutavr, vISOMETR, 4.0, 70, flag_sysc, false, 0, 0, 0,
						  plate_thick, plate_width);
	   H_beam = com_sect.tavr.h + 2*com_sect.dvutavr.h1 + plate_thick;
	 break;
	 }

   break;
   }
   // Рисование нагрузок
   Draw_load(Image_DRsu, flag_sign);
}
//------------------------------------------------------------------------------------------------------
// Рисование нагрузок
void __fastcall TDrawRSUForm::Draw_load(TImage *Image_DRsu, bool flag_sign) {
  TPoint points[10];
  TPoint vert[10];
  TPoint point_0;
  int X1, X2, X3, X4, Y1, Y2, Y3, Y4;
  float shift;
  int k, n;
  float scale_load;
  double Value;
  double coor_g[30][3];
  double xf0[20], xf1[20], xf2[2];
  double coor_load[3];
  double direction[3], Sign;
  double t_ar, l_ar;

  double x0, y0, length, scale, X_shift, Y_shift;

  Get_XY_Len(&x0, &y0, &length);
  Get_scale(&scale, &X_shift, &Y_shift);

  //---------------------------------------------------
  Value = 0;
  Value = MAX(Value, (ABS(loads[0])));
  Value = MAX(Value, (ABS(loads[2] + Qy_plus)));
  Value = MAX(Value, (ABS(loads[4])));
  pPen_Width(Image_DRsu, 1);
  //pBrush_Color(Image_DRsu, clMaroon);
  pBrush_Style(Image_DRsu, bsClear);
  l_ar = length/6;
  t_ar = l_ar/5;

	if (loads[5]!=0) { // равномерно распределенная нагрузка
	  float scale_force, delta_x, delta_y;

	  scale_force = length*scale/(8*(ABS(loads[5])));
	  pPen_Color(Image_DRsu, clPurple);
	  pBrush_Style(Image_DRsu, bsClear);

	  coor_g[0][0] = x0;    coor_g[0][2] = 0;
	  coor_g[1][0] = x0;    coor_g[1][2] = length;

	  if (loads[5] > 0) {
		coor_g[0][1] = y0 - H_beam*0.5;
		coor_g[1][1] = y0 - H_beam*0.5;
	  }
	  else  {
		coor_g[0][1] = y0 + H_beam*0.5;
		coor_g[1][1] = y0 + H_beam*0.5;
	  }
	  get_point_q(coor_g);

	  n = (coor_g[0][0] - coor_g[1][0])/20;
	  n = ABS(n);
	  delta_x = (coor_g[0][0] - coor_g[1][0])/n;
	  delta_y = (coor_g[0][1] - coor_g[1][1])/n;

	  points[0] = Point(coor_g[0][0], coor_g[0][1]);
	  points[1] = Point(points[0].x, points[0].y - loads[5]*scale_force);
	  for (k=0; k <=n ; k++) {

		vert[1] = Point(points[0].x - k*delta_x, points[0].y - k*delta_y);
		vert[0] = Point(vert[1].x, vert[1].y - loads[5]*scale_force);
		CoorAx.PutArrow(vert, 0, MAX_PIC);
		CoorAx.SetArrow();

		pPen_Width(Image_DRsu, 1);
		pCanvas_Polyline(Image_DRsu, CoorAx.LArrow, 2);
		pCanvas_Polyline(Image_DRsu, CoorAx.Ar, 3);
	  }
	  points[2] = vert[0];
	  pCanvas_Polyline(Image_DRsu, points+1, 1);
	  points[1] = Point(coor_g[1][0], coor_g[1][1]);
	  pCanvas_Polyline(Image_DRsu, points, 1);

	  //pFont_Color(Image_DRsu, clPurple);
	  pBrush_Style(Image_DRsu, bsSolid);
	  if (flag_sign) {
		 pCanvas_TextOut(Image_DRsu, (points[0].x + points[2].x)/2 + 20 , (points[0].y  + points[2].y)/2 - 10, "qy ="+FloatToStr(loads[5])+" кН/м");
	  }

	}

	//--------------------------------------------------------
	// Сила N
	if (loads[0]!=0) {
	  pPen_Color(Image_DRsu, clRed);
	  // N на левом конце
	   direction[0] = direction[1] = 0; direction[2] = 1;
	   coor_load[0] = x0;   coor_load[1] = y0;    coor_load[2] = length;
	   scale_load = ABS(loads[0]/Value);
	   // получить экранные координаты точек для рисования нагрузок на геометрии
	   get_point_load_ar(loads[0], direction, scale_load, coor_load,
					  t_ar, l_ar, coor_g, &Sign);
	   draw_load_ar(Image_DRsu, coor_g, loads[0], flag_sign, 0, 0);   // изображение силы
	  // N на правом конце
	   direction[0] = direction[1] = 0; direction[2] = -1;
	   coor_load[0] = x0;   coor_load[1] = y0;    coor_load[2] = 0;
	   get_point_load_ar(loads[0], direction, scale_load, coor_load,
					  t_ar, l_ar, coor_g, &Sign);
	   draw_load_ar(Image_DRsu, coor_g, loads[0], flag_sign, 0, 0);   // изображение силы

	}

	if (loads[2] + Qy_plus!=0 ) { // Сила Qy
	   pPen_Color(Image_DRsu, clGreen);
	  // Qy на левом конце
	   scale_load = ABS((loads[2]  + Qy_plus)/Value);
	   direction[0] = 0; direction[1] = 1; direction[2] = 0;
	   coor_load[0] = x0;   coor_load[1] = y0;    coor_load[2] = length;
	   get_point_load_ar(loads[2]  + Qy_plus, direction, scale_load, coor_load,
					  t_ar, l_ar, coor_g, &Sign);
	   draw_load_ar(Image_DRsu, coor_g, loads[2] + Qy_plus, flag_sign, 1, 0);   // изображение силы
	  // Qy на правом конце
	   direction[0] =0;  direction[1] = -1; direction[2] = 0;
	   coor_load[0] = x0;   coor_load[1] = y0;    coor_load[2] = 0;
	   get_point_load_ar(loads[2]  + Qy_plus, direction, scale_load, coor_load,
					  t_ar, l_ar, coor_g, &Sign);
	   draw_load_ar(Image_DRsu, coor_g, loads[2] + Qy_plus, flag_sign, 1, 0);   // изображение силы
	}

	if (loads[4]!=0) { // Сила Qx
	   pPen_Color(Image_DRsu, clBlue);
	  // Qy на левом конце
	   scale_load = ABS(loads[4]/Value);
	   direction[0] = 1; direction[1] = 0; direction[2] = 0;
	   coor_load[0] = x0;   coor_load[1] = y0;    coor_load[2] = length;
	   get_point_load_ar(loads[4], direction, scale_load, coor_load,
					  t_ar, l_ar, coor_g, &Sign);
	   draw_load_ar(Image_DRsu, coor_g, loads[4], flag_sign, 2, 0);   // изображение силы
	  // Qy на правом конце
	   direction[0] =-1;  direction[1] = 0; direction[2] = 0;
	   coor_load[0] = x0;   coor_load[1] = y0;    coor_load[2] = 0;
	   get_point_load_ar(loads[4], direction, scale_load, coor_load,
					  t_ar, l_ar, coor_g, &Sign);
	   draw_load_ar(Image_DRsu, coor_g, loads[4], flag_sign, 2, 0);   // изображение силы
	}

	//Image_DRsu->Canvas->Pen->Width = 1;
	pPen_Width(Image_DRsu, 1);
	if (loads[1] + Mx_plus!=0) {  // Момент Mx
	  pPen_Color(Image_DRsu, clBlue);
	  // Mx на левом конце
	   direction[0] = 1; direction[1] = 0; direction[2] = 0;
	   coor_load[0] = x0;   coor_load[1] = y0;    coor_load[2] = length;
	   get_point_moment(loads[1] + Mx_plus, direction, coor_load, coor_g, length);
	   draw_moment(Image_DRsu, coor_g, loads[1] + Mx_plus, flag_sign);   // изображение момента
	  // Mx на правом конце
	   direction[0] = -1; direction[1] = 0; direction[2] = 0;
	   coor_load[0] = x0;   coor_load[1] = y0;    coor_load[2] = 0;
	   get_point_moment(loads[1] + Mx_plus, direction, coor_load, coor_g, length);
	   draw_moment(Image_DRsu, coor_g, loads[1] + Mx_plus, flag_sign);   // изображение момента
	}

	if (loads[3]!=0) {  // Момент My
	  pPen_Color(Image_DRsu, clGreen);
	  // My на левом конце
	   direction[0] = 0; direction[1] = 1; direction[2] = 0;
	   coor_load[0] = x0;   coor_load[1] = y0;    coor_load[2] = length;
	   get_point_moment(loads[3], direction, coor_load, coor_g, length);
	   draw_moment(Image_DRsu, coor_g, loads[3], flag_sign);   // изображение момента
	  // My на правом конце
	   direction[0] = 0; direction[1] = -1; direction[2] = 0;
	   coor_load[0] = x0;   coor_load[1] = y0;    coor_load[2] = 0;
	   get_point_moment(loads[3], direction, coor_load, coor_g, length);
	   draw_moment(Image_DRsu, coor_g, loads[3], flag_sign);   // изображение момента
	}
	pPen_Width(Image_DRsu, 1);

}
//------------------------------------------------------------------------------------------------------
// Рисование осей
// Штриховка
void __fastcall TDrawRSUForm::Shtrih(TPoint *points, float s_s, float mx, float my) {
	// points - точки линии
	// s_s - шаг штриховки
	// mx, my: +1 или -1  - множители, определяющие направление штриховки
	float delx, dely;
	int k;
	TPoint vert[2];
	float l_s;

	delx = points[1].x - points[0].x;
	dely = points[1].y - points[0].y;
    l_s = sqrt(delx*delx + dely*dely);

    for (k=0; k < l_s; k += s_s) {
      vert[0] = Point(points[0].x + delx*k/l_s, points[0].y + dely*k/l_s);
      vert[1] = Point(vert[0].x + mx*s_s, vert[0].y +my*s_s);
      pCanvas_Polygon(Image_DRsu, vert, 1);
    }
}

void __fastcall TDrawRSUForm::BitBtn_CloseClick(TObject *Sender)
{
	Close();
}
//---------------------------------------------------------------------------
void __fastcall TDrawRSUForm::CheckBox_SideClick(TObject *Sender)
{
	if (CheckBox_Side->Checked) {
	   CheckBox_Centr->Checked = false;
	   Mx_plus = 0;
	   Qy_plus = loads[5]*L_beam*0.5*1e-3;
	   loads[5] = 0;
	}
	else {
	   Mx_plus = 0;
	   Qy_plus = 0;
	   loads[5] = q_bal;
	}
	Draw_beam(CheckBox_SignLoad->Checked, CheckBox_sysc->Checked);

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

void __fastcall TDrawRSUForm::CheckBox_CentrClick(TObject *Sender)
{
	if (CheckBox_Centr->Checked) {
	   CheckBox_Side->Checked = false;
	   Mx_plus = loads[5]*L_beam*L_beam*0.125*1e-6;
	   Qy_plus = 0;
	   loads[5] = 0;
	}
	else {
	   Mx_plus = 0;
	   Qy_plus = 0;
	   loads[5] = q_bal;
	}
	Draw_beam(CheckBox_SignLoad->Checked, CheckBox_sysc->Checked);

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

void __fastcall TDrawRSUForm::CheckBox_SignLoadClick(TObject *Sender)
{

	Draw_beam(CheckBox_SignLoad->Checked, CheckBox_sysc->Checked);
}
//---------------------------------------------------------------------------

void __fastcall TDrawRSUForm::CheckBox_syscClick(TObject *Sender)
{

	Draw_beam(CheckBox_SignLoad->Checked, CheckBox_sysc->Checked);
}
//---------------------------------------------------------------------------


