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


#pragma hdrstop

#include "SaveSelectProf.h"
#include "SelectProfilSect.h"
//#include "WRData_Dll.h"
#include "WRData_Check.h"
#include "Dvutavr_Choice_Alg.h"

#include "message.h"

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

#pragma package(smart_init)
extern MATER MaterProp[MAX_MATERIAL];
extern int Material_Number;

//---------------------------------------------------------------------------
// Запись проекта в архив
bool SaveSelectProf(FILE * BD_file) {
   int rc;

   if (!BD_WSelectProf(BD_file)) {
      InfMessage("Ошибка при записи проекта в архив ");
      return false;
   }
   if (SelectProfilSectForm->Data_select.index_loads<=1) {
	 if (!BD_WDataBeam(BD_file)) {
		InfMessage("Ошибка при записи данных о балке в архив ");
		return false;
	 }
   }
   else {
	 if (!BD_WDataRack(BD_file)) {
	   InfMessage("Ошибка при записи данных о стойке в архив ");
	   return false;
	 }
   }
   return true;
}
//-------------------------------------------------------------
// Открыть архив модели
bool OpenSelectProf(FILE * BD_file) {
   int rc;

   if (!BD_RSelectProf(BD_file)) {
	  InfMessage("Ошибка при чтении проекта из архива ");
	  return false;
   }
   if (SelectProfilSectForm->Data_select.index_loads<=1) {
	 if (!BD_RDataBeam(BD_file)) {
	   InfMessage("Ошибка при чтении данных о балке из архива ");
	   return false;
	 }
   }
   else {
	 if (!BD_RDataRack(BD_file)) {
	   InfMessage("Ошибка при чтении данных о стойке из архива ");
	   return false;
	 }
   }

   return true;
}
//---------------------------------
// Запись общих данных
bool BD_WSelectProf(FILE* BD_file) {

   FData * data1;
   int param[16];
   int i, j, rc;
   int n_material = 1;

   for (i=0; i<16; i++) {
	 param[i] = 0;
   }

   param[0] = n_material;         // число заданных материалов  = 1
   param[1] = Material_Number;    // номер установленного материала
   param[2] = SelectProfilSectForm->Data_select.form_profil;    // тип профиля
   param[3] = SelectProfilSectForm->Last_type_sect;
   param[4] = SelectProfilSectForm->Data_select.index_weld_param;   // 0 - параметры сварных швов не заданы

   data1 = BD_WOpenData(BD_file, "SELECTPROF", 0, param, &rc);
   if (rc>0) {
	 PrinCodOpenData(rc);
	 return false;
   }

   rc = BD_WriteData((char *)&(SelectProfilSectForm->Data_select), sizeof(DATA_SELECT ), 1, data1);
   if (rc!=1) return false;
   rc = BD_WriteData((char *)&(MaterProp[0]), sizeof(MATER), n_material+1, data1);
   if (rc!=n_material+1) return false;
   if (param[2]==DVUTAVR_WELD) {
	 rc = BD_WriteData((char *)&(SelectProfilSectForm->Param_chosen_sect), sizeof(PARAM_CHOSEN_SECT), 1, data1);
	 if (rc!=1) return false;
   }
   if (SelectProfilSectForm->Last_type_sect > 0) {
	 rc = BD_WriteData((char *)&(SelectProfilSectForm->Last_com_sect), sizeof(COMMON_SECT), 1, data1);
	 if (rc!=1) return false;
   }
   if (param[4]==1) {
	 rc = BD_WriteData((char *)&(SelectProfilSectForm->Weld_balka_unit), sizeof(WELD_PARAM_UNIT), 1, data1);
	 if (rc!=1) return false;
   }
   BD_CloseData(data1);

   return true;
}
//---------------------------------
// Запись данных по балке
bool BD_WDataBeam(FILE* BD_file) {

   FData * data1;
   int param[16];
   int i, j, rc;
   int Len_v = SelectProfilSectForm->Data_beam.Len_v;
   int n_zagr = SelectProfilSectForm->Data_beam.n_zagr;
   int n_comb = SelectProfilSectForm->Data_beam.n_comb;
   int constr_length = SelectProfilSectForm->Data_beam.constr_length;

   for (i=0; i<16; i++) {
	 param[i] = 0;
   }

   param[0] = n_zagr;   // число загружений
   param[1] = Len_v;    // размер депозитария
   param[2] = n_comb;   // число комбинаций
   param[3] = 1;        // признак записи index_place и dist_LR_v

   data1 = BD_WOpenData(BD_file, "DATABEAM", 0, param, &rc);
   if (rc>0) {
	 PrinCodOpenData(rc);
	 return false;
   }

   rc = BD_WriteData((char *)&(SelectProfilSectForm->Data_beam), sizeof(DATA_BEAM ), 1, data1);
   if (rc!=1) return false;
   if (n_zagr==0)
	 return true;
   // Запись регионов
   rc = BD_WriteData((char *)SelectProfilSectForm->Data_region_beam.EI_v, sizeof(BeamRegion), Len_v, data1);
   if (rc!=Len_v) return false;
   rc = BD_WriteData((char *)SelectProfilSectForm->Data_region_beam.q_v, sizeof(BeamRegion), Len_v, data1);
   if (rc!=Len_v) return false;
   rc = BD_WriteData((char *)SelectProfilSectForm->Data_region_beam.index_place_v, sizeof(int), Len_v, data1);
   if (rc!=Len_v) return false;
   rc = BD_WriteData((char *)SelectProfilSectForm->Data_region_beam.dist_LRv, sizeof(double), Len_v, data1);
   if (rc!=Len_v) return false;
   rc = BD_WriteData((char *)SelectProfilSectForm->Data_region_beam.N_v, sizeof(BeamRegion), Len_v, data1);
   if (rc!=Len_v) return false;
   rc = BD_WriteData((char *)SelectProfilSectForm->Data_region_beam.loads_v, sizeof(Load), Len_v, data1);
   if (rc!=Len_v) return false;
   if (constr_length>0) {
	 rc = BD_WriteData((char *)SelectProfilSectForm->Data_region_beam.constr, sizeof(Constraint), constr_length, data1);
	 if (rc!=constr_length) return false;
   }
   rc = BD_WriteData((char *)SelectProfilSectForm->Data_region_beam.type_zagr, sizeof(int), n_zagr, data1);
   if (rc!=n_zagr) return false;
   rc = BD_WriteData((char *)SelectProfilSectForm->Data_region_beam.flag_weight, sizeof(bool), n_zagr, data1);
   if (rc!=n_zagr) return false;
   rc = BD_WriteData((char *)SelectProfilSectForm->Data_region_beam.Koeff_gamma_f, sizeof(double), n_zagr, data1);
   if (rc!=n_zagr) return false;
   rc = BD_WriteData((char *)SelectProfilSectForm->Data_region_beam.Koeff_low, sizeof(double), n_zagr, data1);
   if (rc!=n_zagr) return false;
   rc = BD_WriteData((char *)SelectProfilSectForm->Data_region_beam.index_beam, sizeof(int), n_zagr*7, data1);
   if (rc!=n_zagr*7) return false;

   if (n_comb!=0) {
	 rc = BD_WriteData((char *)&(SelectProfilSectForm->Data_region_beam.Combin_zagr[0][0]), sizeof(double), n_zagr*n_comb, data1);
	 if (rc!=n_zagr*n_comb) return false;
   }

   BD_CloseData(data1);

   return true;
}
//---------------------------------
// Запись данных по стойке
bool BD_WDataRack(FILE* BD_file) {

   FData * data1;
   int param[16];
   int i, j, rc;
   int Len_v = SelectProfilSectForm->Data_rack.Len_v;
   int n_zagr = SelectProfilSectForm->Data_rack.n_zagr;
   int n_comb = SelectProfilSectForm->Data_rack.n_comb;
   int constr_length = SelectProfilSectForm->Data_rack.constr_length;

   for (i=0; i<16; i++) {
	 param[i] = 0;
   }

   param[0] = n_zagr;   // число загружений
   param[1] = Len_v;    // размер депозитария
   param[2] = n_comb;   // число комбинаций

   data1 = BD_WOpenData(BD_file, "DATARACK", 0, param, &rc);
   if (rc>0) {
	 PrinCodOpenData(rc);
	 return false;
   }

   rc = BD_WriteData((char *)&(SelectProfilSectForm->Data_rack), sizeof(DATA_RACK), 1, data1);
   if (rc!=1) return false;
   if (n_zagr==0)
	 return true;
   // Запись регионов
   rc = BD_WriteData((char *)SelectProfilSectForm->Data_region_rack.EI_v, sizeof(BeamRegion), Len_v, data1);
   if (rc!=Len_v) return false;
   rc = BD_WriteData((char *)SelectProfilSectForm->Data_region_rack.q_v, sizeof(BeamRegion), Len_v, data1);
   if (rc!=Len_v) return false;
   rc = BD_WriteData((char *)SelectProfilSectForm->Data_region_rack.dist_eLv, sizeof(double), Len_v, data1);
   if (rc!=Len_v) return false;
   rc = BD_WriteData((char *)SelectProfilSectForm->Data_region_rack.dist_eNv, sizeof(double), Len_v, data1);
   if (rc!=Len_v) return false;
   rc = BD_WriteData((char *)SelectProfilSectForm->Data_region_rack.N_v, sizeof(BeamRegion), Len_v, data1);
   if (rc!=Len_v) return false;
   rc = BD_WriteData((char *)SelectProfilSectForm->Data_region_rack.loads_v, sizeof(Load), Len_v, data1);
   if (rc!=Len_v) return false;
   if (constr_length>0) {
	 rc = BD_WriteData((char *)SelectProfilSectForm->Data_region_rack.constr, sizeof(Constraint), constr_length, data1);
	 if (rc!=constr_length) return false;
   }
   rc = BD_WriteData((char *)SelectProfilSectForm->Data_region_rack.type_zagr, sizeof(int), n_zagr, data1);
   if (rc!=n_zagr) return false;
   rc = BD_WriteData((char *)SelectProfilSectForm->Data_region_rack.flag_weight, sizeof(bool), n_zagr, data1);
   if (rc!=n_zagr) return false;
   rc = BD_WriteData((char *)SelectProfilSectForm->Data_region_rack.Koeff_gamma_f, sizeof(double), n_zagr, data1);
   if (rc!=n_zagr) return false;
   rc = BD_WriteData((char *)SelectProfilSectForm->Data_region_rack.Koeff_low, sizeof(double), n_zagr, data1);
   if (rc!=n_zagr) return false;
   rc = BD_WriteData((char *)SelectProfilSectForm->Data_region_rack.index_beam, sizeof(int), n_zagr*7, data1);
   if (rc!=n_zagr*7) return false;

   if (n_comb!=0) {
	 rc = BD_WriteData((char *)&(SelectProfilSectForm->Data_region_rack.Combin_zagr[0][0]), sizeof(double), n_zagr*n_comb, data1);
	 if (rc!=n_zagr*n_comb) return false;
   }

   BD_CloseData(data1);

   return true;
}
//---------------------------------------------------------------------------
bool BD_RSelectProf(FILE* BD_file) {
   FData * data1;
   int param[16];
   int i, j, rc;
   int n_material;


   data1 = BD_ROpenData(BD_file, "SELECTPROF", 0, param, &rc);
   if (rc>0) return false;
   n_material = param[0];         // число заданных материалов
   Material_Number = param[1];    // номер установленного материала
   SelectProfilSectForm->Last_type_sect = param[3];

   rc = BD_ReadData((char *)&(SelectProfilSectForm->Data_select), sizeof(DATA_SELECT ), 1, data1);
   if (rc!=1) return false;
   rc = BD_ReadData((char *)&(MaterProp[0]), sizeof(MATER), n_material+1, data1);
   if (rc!=n_material+1) return false;
   if (param[2]==DVUTAVR_WELD) {
	 rc = BD_ReadData((char *)&(SelectProfilSectForm->Param_chosen_sect), sizeof(PARAM_CHOSEN_SECT), 1, data1);
	 if (rc!=1) return false;
   }
   if (SelectProfilSectForm->Last_type_sect > 0) {
	 rc = BD_ReadData((char *)&(SelectProfilSectForm->Last_com_sect), sizeof(COMMON_SECT), 1, data1);
	 if (rc!=1) return false;
   }
   if (param[4]==1) {
	 rc = BD_ReadData((char *)&(SelectProfilSectForm->Weld_balka_unit), sizeof(WELD_PARAM_UNIT), 1, data1);
	 if (rc!=1) return false;
   }
   BD_CloseData(data1);
   return true;
}
//---------------------------------
// Чтение данных по балке
bool BD_RDataBeam(FILE* BD_file) {

   FData * data1;
   int param[16];
   int i, j, rc;

   data1 = BD_ROpenData(BD_file, "DATABEAM", 0, param, &rc);
   if (data1==NULL) {
	  SelectProfilSectForm->Data_beam.constr_length = 0;
	  SelectProfilSectForm->Data_region_beam.EI_v = 0;
	  SelectProfilSectForm->Data_region_beam.q_v = 0;
	  SelectProfilSectForm->Data_region_beam.N_v = 0;
	  SelectProfilSectForm->Data_region_beam.loads_v = 0;
	  return true;
   }
   if (rc>0) return false;

   int n_zagr = param[0];   // число загружений
   int Len_v = param[1];    // размер депозитария
   int n_comb = param[2];   // число комбинаций

   rc = BD_ReadData((char *)&(SelectProfilSectForm->Data_beam), sizeof(DATA_BEAM ), 1, data1);
   if (rc!=1) return false;

   int constr_length = SelectProfilSectForm->Data_beam.constr_length;
   if (n_zagr==0)
	 return true;
   // Чтение регионов
   rc = BD_ReadData((char *)SelectProfilSectForm->Data_region_beam.EI_v, sizeof(BeamRegion), Len_v, data1);
   if (rc!=Len_v) return false;
   rc = BD_ReadData((char *)SelectProfilSectForm->Data_region_beam.q_v, sizeof(BeamRegion), Len_v, data1);
   if (rc!=Len_v) return false;
   if (param[3]==1) {
	 rc = BD_ReadData((char *)SelectProfilSectForm->Data_region_beam.index_place_v, sizeof(int), Len_v, data1);
	 if (rc!=Len_v) return false;
	 rc = BD_ReadData((char *)SelectProfilSectForm->Data_region_beam.dist_LRv, sizeof(double), Len_v, data1);
	 if (rc!=Len_v) return false;
   }
   else {
	 for (i=0; i<Len_v; i++) {
	   SelectProfilSectForm->Data_region_beam.index_place_v[i] = 0;
	   SelectProfilSectForm->Data_region_beam.dist_LRv[i] = 0;
	 }
   }
   rc = BD_ReadData((char *)SelectProfilSectForm->Data_region_beam.N_v, sizeof(BeamRegion), Len_v, data1);
   if (rc!=Len_v) return false;
   rc = BD_ReadData((char *)SelectProfilSectForm->Data_region_beam.loads_v, sizeof(Load), Len_v, data1);
   if (rc!=Len_v) return false;
   if (constr_length>0) {
	 rc = BD_ReadData((char *)SelectProfilSectForm->Data_region_beam.constr, sizeof(Constraint), constr_length, data1);
	 if (rc!=constr_length) return false;
   }
   rc = BD_ReadData((char *)SelectProfilSectForm->Data_region_beam.type_zagr, sizeof(int), n_zagr, data1);
   if (rc!=n_zagr) return false;
   rc = BD_ReadData((char *)SelectProfilSectForm->Data_region_beam.flag_weight, sizeof(bool), n_zagr, data1);
   if (rc!=n_zagr) return false;
   rc = BD_ReadData((char *)SelectProfilSectForm->Data_region_beam.Koeff_gamma_f, sizeof(double), n_zagr, data1);
   if (rc!=n_zagr) return false;
   rc = BD_ReadData((char *)SelectProfilSectForm->Data_region_beam.Koeff_low, sizeof(double), n_zagr, data1);
   if (rc!=n_zagr) return false;
   rc = BD_ReadData((char *)SelectProfilSectForm->Data_region_beam.index_beam, sizeof(int), n_zagr*7, data1);
   if (rc!=n_zagr*7) return false;
   if (n_comb!=0) {
	 SelectProfilSectForm->Data_region_beam.Combin_zagr = (double**)almat((int)(n_comb),(int)(n_zagr),&rc);\
	 if (rc>0)
		return false;
	 rc = BD_ReadData((char *)&(SelectProfilSectForm->Data_region_beam.Combin_zagr[0][0]), sizeof(double), n_zagr*n_comb, data1);
	 if (rc!=n_zagr*n_comb) return false;
   }
   BD_CloseData(data1);

   return true;
}
//---------------------------------
// Чтение данных по стойке
bool BD_RDataRack(FILE* BD_file) {

   FData * data1;
   int param[16];
   int i, j, rc;

   data1 = BD_ROpenData(BD_file, "DATARACK", 0, param, &rc);
   if (data1==NULL) {
	  SelectProfilSectForm->Data_rack.constr_length = 0;
	  SelectProfilSectForm->Data_region_rack.EI_v = 0;
	  SelectProfilSectForm->Data_region_rack.q_v = 0;
	  SelectProfilSectForm->Data_region_rack.N_v = 0;
	  SelectProfilSectForm->Data_region_rack.loads_v = 0;
	  return true;
   }
   if (rc>0) return false;

   int n_zagr = param[0];   // число загружений
   int Len_v = param[1];    // размер депозитария
   int n_comb = param[2];   // число комбинаций

   rc = BD_ReadData((char *)&(SelectProfilSectForm->Data_rack), sizeof(DATA_RACK), 1, data1);
   if (rc!=1) return false;

   int constr_length = SelectProfilSectForm->Data_rack.constr_length;
   if (n_zagr==0)
	 return true;
   // Чтение регионов
   rc = BD_ReadData((char *)SelectProfilSectForm->Data_region_rack.EI_v, sizeof(BeamRegion), Len_v, data1);
   if (rc!=Len_v) return false;
   rc = BD_ReadData((char *)SelectProfilSectForm->Data_region_rack.q_v, sizeof(BeamRegion), Len_v, data1);
   if (rc!=Len_v) return false;
   rc = BD_ReadData((char *)SelectProfilSectForm->Data_region_rack.dist_eLv, sizeof(double), Len_v, data1);
   if (rc!=Len_v) return false;
   rc = BD_ReadData((char *)SelectProfilSectForm->Data_region_rack.dist_eNv, sizeof(double), Len_v, data1);
   if (rc!=Len_v) return false;
   rc = BD_ReadData((char *)SelectProfilSectForm->Data_region_rack.N_v, sizeof(BeamRegion), Len_v, data1);
   if (rc!=Len_v) return false;
   rc = BD_ReadData((char *)SelectProfilSectForm->Data_region_rack.loads_v, sizeof(Load), Len_v, data1);
   if (rc!=Len_v) return false;
   if (constr_length>0) {
	 rc = BD_ReadData((char *)SelectProfilSectForm->Data_region_rack.constr, sizeof(Constraint), constr_length, data1);
	 if (rc!=constr_length) return false;
   }
   rc = BD_ReadData((char *)SelectProfilSectForm->Data_region_rack.type_zagr, sizeof(int), n_zagr, data1);
   if (rc!=n_zagr) return false;
   rc = BD_ReadData((char *)SelectProfilSectForm->Data_region_rack.flag_weight, sizeof(bool), n_zagr, data1);
   if (rc!=n_zagr) return false;
   rc = BD_ReadData((char *)SelectProfilSectForm->Data_region_rack.Koeff_gamma_f, sizeof(double), n_zagr, data1);
   if (rc!=n_zagr) return false;
   rc = BD_ReadData((char *)SelectProfilSectForm->Data_region_rack.Koeff_low, sizeof(double), n_zagr, data1);
   if (rc!=n_zagr) return false;
   rc = BD_ReadData((char *)SelectProfilSectForm->Data_region_rack.index_beam, sizeof(int), n_zagr*7, data1);
   if (rc!=n_zagr*7) return false;
   if (n_comb!=0) {
	 SelectProfilSectForm->Data_region_rack.Combin_zagr = (double**)almat((int)(n_comb),(int)(n_zagr),&rc);\
	 if (rc>0)
		return false;
	 rc = BD_ReadData((char *)&(SelectProfilSectForm->Data_region_rack.Combin_zagr[0][0]), sizeof(double), n_zagr*n_comb, data1);
	 if (rc!=n_zagr*n_comb) return false;
   }
   BD_CloseData(data1);

   return true;
}

//------------------------------------------------------------
// Печать ошибки
void PrinCodOpenData(int rc) {
  char Message[6][100]={PRINT_ERROR_MESSAGE};

  ErrorMessage("Ошибка при открытии данных на запись: "+(AnsiString)Message[rc]);

  return;
}

