////////////////////////////////////////////////////////////////////////////////
//
// gayka.cpp -    Visual C++ - 
//
////////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"

#include "resource.h"
   
#ifndef __LIBHPPAR_H
#include <Libhppar.h>
#endif 

#ifndef  _INC_MATH
#include "math.h"
#endif             

#ifndef __GAYKA_H
#include "Gayka.h"
#endif

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

bool messCommand = false;
bool messPrepare = false;
bool messSelect = false;
bool commandChange = false;

//-------------------------------------------------------------------------------
//    (  )
// ---
void Gayka_K( float ls, float l, float d1, float s, 
              float l1, float H, short j,  int j1, float d2, int j2, bool apl ) 
{
  double x[9], y[9];
  double c, h1, rb;
  double xc2, yc2, xcbl, ycbl, xcbp, ycbp, ycml;
  //   _
  // j1=1 -  , j1=0 -   
  // j2=1 -  1, j2=2 -  2

  double D  = s / CosD( 30 );
  double D2 = D  * 0.5;
  double d22 = d2 * 0.5;
  
  c  = ( D2 - d22 ) * TanD( 30 );
  h1 = D2 * SinD( 30 );

  rb = ( h1 * h1 + c * c ) * 0.5 / c;

  x[1] = ls; y[1] = 0;
  if ( j2 == 1 )
  {
    x[2] = ls;         y[2] = j * d22;
    x[3] = ( ls + c ); y[3] = j * D2;
    x[7] = ls + c;     y[7] = j * h1;
  }
  else 
  {
    x[2] = ls; y[2] = j * D2;
    x[7] = ls; y[7] = j * h1;
  }
  x[4] = ls + H - c; y[4] = j * D2;
  x[5] = ls + H;     y[5] = j * d22;
  x[6] = ls + H;     y[6] = 0;

  x[8] = ls + H - c; y[8] = j * h1;
  xc2  = ls + l;     yc2 =  j * ( D2 - l1 );

  xcbl = ls + rb; ycbl = 0;
  xcbp = ls + H - rb; ycbp = 0;
  ycml = j * ( ( D2 - h1 ) * 0.5 + h1 );

  if ( j2 == 1) 
  {
    LineSeg( x[1], y[1], x[2], y[2], 1 );
    LineSeg( x[2], y[2], x[3], y[3], 1 );
    LineSeg( x[3], y[3], x[4], y[4], 1 );
    LineSeg( x[4], y[4], x[5], y[5], 1 );
    LineSeg( x[5], y[5], x[6], y[6], 1 );
    if ( !apl ) 
    {    
      LineSeg( x[7], y[7], x[8], y[8], 1 );
      ArcByPoint( xcbl, ycbl, rb, x[1], y[1], x[7], y[7],(short) -j, 1 );
      ArcByPoint( xcbp, ycbp, rb, x[6], y[6], x[8], y[8], j, 1 );
      ArcBy3Points( ls + c * 0.5, (D2 - (D2 - d22) * 0.5 ) * j,
                    ls, ycml, x[7], y[7], 1 );
      ArcBy3Points( ls + H - c * 0.5, (D2 - (D2 - d22) * 0.5 ) * j, ls + H,
                    ycml, x[8], y[8], 1 );
    }
  }
  else 
  {
    LineSeg( x[1], y[1], x[2], y[2], 1 );
    LineSeg( x[2], y[2], x[4], y[4], 1 );
    LineSeg( x[4], y[4], x[5], y[5], 1 );
    LineSeg( x[5], y[5], x[6], y[6], 1 );
    if ( !apl ) 
    {
      LineSeg( x[7], y[7], x[8], y[8], 1 );
      ArcByPoint( xcbp, ycbp, rb, x[6], y[6], x[8], y[8], j, 1 );
      ArcBy3Points( ls + H - c * 0.5, (D2 - (D2 - d22) * 0.5 ) * j,
                    ls + H, ycml, x[8], y[8], 1 );
    }
  }

  if ( !apl && ( j1 == 1 ) ) 
  {
    Circle(  xc2, yc2, d1 * 0.5, 1 );
    LineSeg( xc2  - 2, yc2, xc2 + 2, yc2, 2 );
    LineSeg( xc2, yc2 - 2, xc2, yc2 + 2, 2 );
  }
} 


//-------------------------------------------------------------------------------
//    (   )
// ---
void Gayka_K_Side( float ls, float s, float D, float d2, float H, int j, int j2, bool appl ) 
{
  float x, y;
  float x2, y2;
  // j2=1 -  1, j2=2 - 

  float c = (float)( ( D - d2 ) / 2 * TanD( 30 ) );

  y = (float)( j * s * 0.5 );
  if ( j2 == 1 )  
  {
    x = ls + c;
    LineSeg( ls, 0, ls, j * d2 * 0.5, 1 );
    LineSeg( ls, j * d2 * 0.5, x, y, 1 );
    if ( !appl )
      ArcBy3Points( ls + c, j * (s * 0.5), ls, s * 0.25 * j, ls + c, 0, 1 );
  }
  else 
  {
    x = ls;
    LineSeg( x, 0, x, y, 1 );
  }

  if ( j2 == 3 ) 
  {
    x2 =ls + H;
    y2 = y;
  }
  else 
  {
    x2 = ls + H - c;
    y2 = (float)( j * d2 * 0.5 );
  }
  LineSeg( x, y, x2, y, 1 );
  if ( j2 != 3 )  
  {
    LineSeg( x2, y, ls + H, y2 , 1 );
    if ( !appl )
      ArcBy3Points( ls + H - c, j * (s * 0.5), ls + H,s * 0.25 * j,
                    ls + H - c, 0, 1 );
  }
  LineSeg( ls + H, y2, ls + H, 0, 1 );
  if ( !appl && j > 0 )
    LineSeg( x, 0, x2, 0, 1 );
}


//-------------------------------------------------------------------------------
//    (  -  )
// ---
void Gayka_K_Y( float ls, GAYKA5915 *tmp, int j, bool appl ) 
{
  float h1;
  tmp->D = (float)( tmp->s / CosD( 30 ) );

  h1 = (float)( tmp->D * 0.5 * SinD( 30 ) );

  LineSeg( ls, 0, ls, j * (tmp->D * 0.5 ), 1 );
  LineSeg( ls, j * ( tmp->D * 0.5), ls + tmp->h, j * (tmp->D * 0.5), 1 );
  LineSeg( ls + tmp->h, j * (tmp->D * 0.5), ls + tmp->h, 0, 1 );
  if ( !appl )
    LineSeg( ls, j * h1, ls + tmp->h, j * h1, 1 );
} 


//-------------------------------------------------------------------------------
//    ( -/- )
// ---
void Gayka5915::Gayka_P_K( int j, bool appl ) 
{
  double  c, dd, c1 = 0, c2 = 0;
  double  y, x = 0, x1, x2, x3, y3, y2;
  double D2 = m_paramGayka.D  * 0.5;
  double d2 = m_paramGayka.d2 * 0.5;
  c = ( D2 - d2) * TanD( 30 );

  if ( !m_paramGayka.flags.isp && !m_baseParam.flags.flagSimp )
    y = j > 0 ? d2 : -d2;
  else
    y = j > 0 ? D2 : -D2;

  LineSeg( 0, 0, 0, y, 1 );

  if ( !m_paramGayka.flags.isp && !m_baseParam.flags.flagSimp ) 
  {
    x = c;
    LineSeg( 0, y, x, j * D2, 1 );
  }
  dd = ( m_paramGayka.dr - 2 * MODSTEP_REAL * m_paramGayka.p );

  if ( !m_baseParam.flags.flagSimp ) 
  {
    x3 = m_paramGayka.h - c ;
    y3 = j * d2;
    y2 = j * m_paramGayka.da * 0.5;
  }
  else 
  {
    x3 = m_paramGayka.h;
    y3 = j * D2;
    y2 = j * dd * 0.5;
  }

  LineSeg( x, j * D2, x3, j * D2, 1 );
  
  if ( !m_baseParam.flags.flagSimp )
    LineSeg( x3, j * D2, m_paramGayka.h, y3, 1 );

  LineSeg( m_paramGayka.h, y3, m_paramGayka.h, 0, 1 );

  x1 = m_paramGayka.h;
  x2 = x1;
  if ( !(m_baseParam.flags.flagSimp) ) 
  {
    c1 = (m_paramGayka.da - dd) * 0.5;
    c2 = ( m_paramGayka.da - m_paramGayka.dr ) * 0.5;
    if ( !m_paramGayka.flags.isp )
      x2 = x2 - c2;
  }

  
  if ( !appl ) 
  {
    if ( !m_paramGayka.flags.isp && !m_baseParam.flags.flagSimp ) 
    {
      x1 = x1 - c1;
      LineSeg ( m_paramGayka.h, j * m_paramGayka.da * 0.5, x1, j * 0.5 * dd, 1 ); //+
      LineSeg( x1, j * dd * 0.5, x1, 0, 1 );
    }

    if ( !m_baseParam.flags.flagSimp ) 
    {
      LineSeg( c1, j * 0.5 * dd, 0, j * 0.5 * m_paramGayka.da, 1); //+
      LineSeg( c1, j * dd * 0.5, c1, 0, 1 );                       //+ 
    }
 
    LineSeg( x1, j * 0.5 * dd, c1, j * 0.5 * dd, 1 ); //+
    
    Hatch( 0, m_paramGayka.hatchAngle, m_paramGayka.hatchStep, 0, 0, 0 );
      LineSeg( 0, y2, 0, y, 1 );

      if ( x != x3 )
        LineSeg( x,  j * D2, x3, j * D2, 1 );
      if ( !(m_baseParam.flags.flagSimp) )
        LineSeg( x3, j * D2, m_paramGayka.h, y3, 1 );

      if ( !m_paramGayka.flags.isp && !m_baseParam.flags.flagSimp )
      {
        LineSeg( 0, y, x, j * D2, 1 );
        LineSeg( m_paramGayka.h, y3, m_paramGayka.h, j * m_paramGayka.da * 0.5, 1 );
        LineSeg( m_paramGayka.h, j * m_paramGayka.da * 0.5, x1, j * 0.5 * dd, 1 );
      }
      else
        LineSeg( m_paramGayka.h, y3, m_paramGayka.h, j * dd * 0.5, 1 );

      LineSeg( x1, j * 0.5 * dd, c1, j * 0.5 * dd, 1 );
      if ( !m_baseParam.flags.flagSimp )
        LineSeg( c1, j * 0.5 * dd, 0, j * 0.5 * m_paramGayka.da, 1 );
    EndObj();

    LineSeg( c2, j * 0.5 * m_paramGayka.dr, x2, j * 0.5 * m_paramGayka.dr, 2 );
  } 
} 


//-------------------------------------------------------------------------------
//    ()
// ---
void Gayka5915::Gayka_Sverhu( bool appl ) 
{
 
  float D, s;
  float h1, dd;

  s  = (float)( m_paramGayka.s * 0.5 );
  D  = (float)( s / CosD( 30 ) );
  dd = (float)( m_paramGayka.dr - 2 * MODSTEP_REAL * m_paramGayka.p );

  h1 = (float)( D * SinD( 30 ) ); 

  LineSeg( -s, h1, 0, D, 1 );
  LineSeg( 0, D, s, h1, 1 );
  LineSeg( s, h1, s, -h1, 1 );
  LineSeg( s, -h1, 0, -D, 1 );
  LineSeg( 0, -D, -s, -h1, 1 );
  LineSeg( -s, -h1, -s, h1, 1 );

  if( !appl && !(m_baseParam.flags.flagSimp) )
    Circle( 0, 0, m_paramGayka.d2 * 0.5, 1 );
  Circle( 0, 0, dd * 0.5, 1 );
  if ( !appl ) 
  {
    ArcByPoint( 0, 0, m_paramGayka.dr * 0.5, m_paramGayka.dr * 0.5 * SinD(15),
         m_paramGayka.dr * 0.5 * CosD(15), m_paramGayka.dr * 0.5 * CosD(15),
        -m_paramGayka.dr * 0.5 * SinD(15), 1, 2 );
  }
  
  if ( !appl && m_baseParam.flags.flagAxis ) 
  {
    if ( D >= 6 )  
    {
      LineSeg( -AXIS_END - s, 0, s + AXIS_END, 0, AXIS_LINE );
      LineSeg( 0, -AXIS_END - D, 0, AXIS_END + D, AXIS_LINE );
    }
    else  
    {
      LineSeg( -AXIS_END - s, 0, s + AXIS_END, 0, AXIS_LINE );
      LineSeg( 0, -AXIS_END - D, 0, AXIS_END + D, AXIS_LINE );
    }
  }
}


//-------------------------------------------------------------------------------
//   
// ---
bool Gayka5915::OpenBase() 
{
  return OpenGaykaBase( base );
}


//-------------------------------------------------------------------------------
//   
// ---
bool OpenGaykaBase( SimpleBase & base ) 
{
  bool res = false;
  base.bg = CreateDBT(_T("TXT_DB"));

  if( _ConnectDB(base.bg, _T("5915.loa")) ) 
  {
    //  
    base.rg1 = Relation( base.bg );
      RChar( "", 20, 0 );
      RFloatT(_T("dr"));
    EndRelation(); 
    if ( DoStatementT(base.bg, base.rg1, _T("1 1") ) ) 
    {
      //  
      base.rg2 = Relation( base.bg );
        RFloatT(_T("dr"));
        RFloatT(_T(""));
        RFloatT(_T(""));
        RFloatT(_T(""));
        RFloatT(_T(""));
        RFloatT(_T(""));
        RFloatT(_T(""));
        RFloatT(_T(""));
        RFloatT(_T(""));
        RFloatT(_T(""));
        RFloatT(_T(""));
        RFloatT(_T(""));
      EndRelation();
      if ( DoStatementT( base.bg, base.rg2, _T("")) ) 
        res = true;
    }
  }
  return res;
}


//--------------------------------------------------------------------------
//      
// ---
bool ReadGaykaBase( float d, SimpleBase & base, GAYKA5915 * pGayka ) 
{
  bool res = false;

  struct { float dr; 
           float p1; 
           float p2; 
           float s; 
           float D; 
           float da; 
           float h; 
           float d2; 
           float m; 
           float s1; 
           float D1; 
           float m1; } tmpGayka;

  TCHAR s[256];
  _stprintf( s, _T("dr=%.1f"), d );
  if ( ConditionT(base.bg, base.rg2, s) ) 
  {   
    bool read = !!ReadRecord( base.bg, base.rg2, &tmpGayka );
    if ( read )
    {
      pGayka->dr = tmpGayka.dr;
      pGayka->da = tmpGayka.da;
      pGayka->h  = tmpGayka.h;
      pGayka->d2 = tmpGayka.d2;

      if ( pGayka->flags.pitch )
        pGayka->p = tmpGayka.p2;
      else
        pGayka->p = tmpGayka.p1;
      
      pGayka->flags.pitch_on = fabs( tmpGayka.p2 - tmpGayka.p1 ) > 0.001;
      pGayka->flags.key_s_on = fabs( tmpGayka.s1 ) > 0.001;
      
      if ( !pGayka->flags.key_s_on )
        pGayka->flags.key_s = false;
     
      if ( pGayka->flags.key_s ) 
      {
        pGayka->s     = tmpGayka.s1; //   
        pGayka->D     = tmpGayka.D1; //   
        pGayka->massa = tmpGayka.m1; // 
      }
      else 
      {
        pGayka->s     = tmpGayka.s; //   
        pGayka->D     = tmpGayka.D; //   
        pGayka->massa = tmpGayka.m; // 
      }

      res = true;
    }
  }
  return res;
}

// HOT_POINTS ################################################################## 

//------------------------------------------------------------------------------
//   Hot 
// ---
bool Gayka5915::GetHotPoints( HotPointDescription* point, int index ) 
{
  bool res = false;
	switch ( index ) 
  {
  	case 0 : //  
    {  
      point->x    =  0;
      point->y    =  0;
			point->text = "0";
      res = true;
      break;
    }
    
    case 1 : //   
    {  
      static char textHP[255]; //      static
      sprintf( textHP, "A = %.2f", fmod(angle, 360.0) ); 
      point->x          = m_baseParam.drawType == ID_TOP ? 
                         (m_paramGayka.s * 0.5 ) : m_paramGayka.h;
      point->y          = 0;
      point->cursorInst = StepDLL.hModule;
      point->cursorId   = CURSOR_ROTATE;
      point->text       = textHP;
      res = true;
      break;
    }

    case 2 : //    
    {  
      point->x          = 0;
      point->y          = m_paramGayka.dr / 2.0;
      point->cursorInst = StepDLL.hModule;
      point->cursorId   = CURSOR_DIAMETER;
      point->text       = "Dr";
      res = true;
      break;
    }
  }
  return res;
}

//-------------------------------------------------------------------------------
//   Hot .   
// ---
bool Gayka5915::GetHotPointsEx( HotPointDescription1* point, int index ) 
{ 
  bool res = false;

  if ( point ) {
    CString name;

	  switch ( index ) 
    {
  	  case 0 : //  
      {  
        name = _T("0");
			  point->text = name.AllocSysString();
        res = true;
        if ( !commandChange ) {
          point->bitmapInst = 0;
          point->bitmapId   = 1;
          point->bitmapIdMove   = 0;
          point->bitmapIdSelect = 0;
        }
        else {
          point->bitmapInst     = StepDLL.hModule;
          point->bitmapId       = IDP_HOTPNT3_BMP;
          point->bitmapIdMove   = IDP_HOTPNT3C_BMP;
          point->bitmapIdSelect = IDP_HOTPNT3S_BMP;
        }
        break;
      }
    
      case 1 : //   
      {  
        _TCHAR textHP[255]; //      static
        _stprintf( textHP, _T("A1 = %.2f"), fmod(angle, 360.0) ); 
        name = textHP;

        point->bitmapInst     = StepDLL.hModule;
        if ( !commandChange ) {
          point->bitmapId       = IDP_HOTPNT1_BMP;
          point->bitmapIdMove   = IDP_HOTPNT1C_BMP;
          point->bitmapIdSelect = IDP_HOTPNT1S_BMP;
        }
        else {
          point->bitmapId       = IDP_HOTPNT4_BMP;
          point->bitmapIdMove   = IDP_HOTPNT4C_BMP;
          point->bitmapIdSelect = IDP_HOTPNT4S_BMP;

        }
        point->text       = name.AllocSysString();;
        res = true;
        break;
      }

      case 2 : //    
      {  
        point->bitmapInst = StepDLL.hModule;
        if ( !commandChange ) {
          point->bitmapId       = IDP_HOTPNT2_BMP;
          point->bitmapIdMove   = IDP_HOTPNT2C_BMP;
          point->bitmapIdSelect = IDP_HOTPNT2S_BMP;
        }
        else {
          point->bitmapInst = 0;
          point->bitmapId   = 2;
          point->bitmapIdMove   = 0;
          point->bitmapIdSelect = 0;

        }
        name = _T("Dr1");
        point->text = name.AllocSysString();
        res = true;
        break;
      }
    }
  }
  return res;
} 


//-------------------------------------------------------------------------------
//   Hot 
// ---
bool Gayka5915::GetCursorText( int index, char ** text ) 
{
  bool res = false;
  static char cursorText[255]; //      static
  switch ( index ) 
  {
    case 1 :
    {
			sprintf( cursorText, "A = %.2f", fmod(angle, 360.0) );
      *text = cursorText;
      res = true;
      break;
    }
    case 2 :
    {
			sprintf( cursorText, "D = %.2f\nH = %.2f", m_paramGayka.dr, m_paramGayka.h );
      *text = cursorText;
      res = true;
      break;
    }
  }   
  return res;
}


//-------------------------------------------------------------------------------
//   Hot 
// ---
bool Gayka5915::ChangeHotPointParam( double d ) 
{
  bool res = false;

  if ( OpenBase() ) 
  {
    struct { char s[20]; float dr; } record;

    //   dR  d
    float dr    = 0.0;
    float drOld = m_paramGayka.dr;

    while( true ) 
    {
      if ( ReadRecord(  base.bg, base.rg1, &record ) ) 
      {
        if ( dr <= d )
          dr = record.dr;
        else
          break;
      }
      else
        break;
    }

    //   
    if ( fabs( dr - drOld ) > 0.01 )
      res = ReadGaykaBase( dr, base, &m_paramGayka );

    CloseBase();
  } 
  return res;
}


//------------------------------------------------------------------------------
//   Hot 
// ---
bool Gayka5915::SetHotPoint( HotPointDescription * point, int index )
{ 
  switch ( index ) 
  {
    case 0 : //   
    case 1 : 
      CPar::SetHotPoint( point, index );
      break;
    case 2 :
    {
      if ( ChangeHotPointParam( fabs(point->y * 2) ) ) 
      {  
        reference group = 0;
        DrawGroup( group );
        SetParam();
        SetPlacement( 0., 0., 0. );
        StoreTmpGroup( group );
        ClearGroup( group );
      }
      break;
    }
    
  }
  return TRUE;
}


//------------------------------------------------------------------------------
//     ,    
// ---
bool Gayka5915::EditComplete( int index, BOOL success ) 
{
  if ( index == 2 && success ) 
  {
    DrawSpcObj(0);
  }
  if (messPrepare) {
    MessageT(_T("EditComplete"));
  }
  
  return true;
}

//-------------------------------------------------------------------------------
// 
// ---
int Gayka5915::GetMenu( int index ) {

  int res = 0;
  if (index == -1 )
    res = (int)::LoadMenu( StepDLL.hModule, (TCHAR*)MENU_G5915_1 );
  else
    switch ( index ) 
    {
      case 1 :
      {
        res = (int)::LoadMenu( StepDLL.hModule, (TCHAR*)MENU_G5915_2 );
        break;
      }
      case 2 :
      {
        res = (int)::LoadMenu( StepDLL.hModule, (TCHAR*)MENU_G5915_3 );
        break;
      }
    }   

    return res;
}

//-------------------------------------------------------------------------------
//  
// ---
bool Gayka5915::ExecuteCommand( int id )
{
  if (messCommand) {
    TCHAR buf[128];
    _stprintf( buf, _T("  = %d"), id  );
    MessageT(buf);
  }

  switch (id) {
    case 1: messCommand = !messCommand; break;
    case 2: messPrepare = !messPrepare; break;
    case 3: messSelect = !messSelect; break;
    case 4: messSelect =  messCommand = messPrepare = false; break;
    case 7: commandChange =  !commandChange; break;
  }



  return true;
}


//-------------------------------------------------------------------------------
//
// ---
LPOLESTR Gayka5915::GetCursorTextEx( int index ) { 
  
  CString name;
    
  wchar_t cursorText[255]; //      static

	switch ( index ) 
  {
  	case 0 : //  
    {  
      break;
    }
  
    case 1 : //   
    {  
      
      swprintf( cursorText, L"A = %.2f", fmod(angle, 360.0) );
      name = cursorText;
      break;
    }

    case 2 : //    
    {  
			swprintf( cursorText, L"D = %.2f\nH = %.2f", m_paramGayka.dr, m_paramGayka.h );
      name = cursorText;
      break;
    }
  }

  return name.AllocSysString();
};


//-------------------------------------------------------------------------------
//
// ---
bool Gayka5915::UpdateCommand( int id ){
  bool res = false;
//    if ( id == 2 || id == 3  || id == 5 || id == 30 ) 
//    {
    res = id < 5 || id == 7;
//    }   
  return res;
}

//-------------------------------------------------------------------------------
//
// ---
bool Gayka5915::Prepare() {
  if (messPrepare) {
    MessageT(_T("Prepare"));
  }
  return true;

}

//-------------------------------------------------------------------------------
//
// ---
bool Gayka5915::SelectHotPoint( int index )
{ 
  if (messSelect) {
    TCHAR buf[128];
    _stprintf( buf, _T("Select  HotPoint = %d"), index  );
    MessageT(buf);
  }
  return true;

} 

//-------------------------------------------------------------------------------
//
// ---
bool Gayka5915::UnselectHotPoint( int index )
{ 
  if (messSelect) {
    TCHAR buf[128];
    _stprintf( buf, _T("Unselect  HotPoint = %d"), index  );
    MessageT(buf);
  }
  return true;
} 

// HOT_POINTS ################################################################## 

//-------------------------------------------------------------------------------
// 
// ---
Gayka5915::Gayka5915( reference ref ) 
  : CPar( CM_GAYKA5915, ref )
{

  if( !GetMacroParam(0, *this, Size()) ) 
  {
    m_paramGayka.hatchAngle      = 45;
    m_paramGayka.hatchStep       = 2;     
    InitParam();
    m_paramGayka.iFlags          = 0;        
  }
  else 
  {
    if ( !m_baseParam.version ) 
    {
      m_baseParam.version        = 1;
    }
  }
}


//-------------------------------------------------------------------------------
//  
// ---
void Gayka5915::InitParam() 
{
  m_paramGayka.dr              = 20;  
  m_paramGayka.p               = (float)2.5;    
  m_paramGayka.version         = 1;
  m_paramGayka.indexMassa      = 0;
  m_paramGayka.s               = 30; 
  m_paramGayka.D               = 33;  
  m_paramGayka.da              = (float)21.6;  
  m_paramGayka.h               = 16;
  m_paramGayka.d2              = (float)27.7;  
  m_paramGayka.classAccuracy   = 2; 
  m_paramGayka.massa           = (float)71.44;
}


//-------------------------------------------------------------------------------
// 
// ---
void Gayka5915::Assign( Gayka5915& other ) 
{         
  memcpy( &m_baseParam,  &other.m_baseParam,  sizeof(m_baseParam)  ); //   
  memcpy( &m_paramGayka, &other.m_paramGayka, sizeof(m_paramGayka) ); //    5915
  flagMode = other.flagMode;
}


//-------------------------------------------------------------------------------
//  
// ---
Gayka5915::Gayka5915( Gayka5915& other ) 
  : CPar( CM_GAYKA5915 )
{
	Assign( other );
}


//-------------------------------------------------------------------------------
//  
// ---
double Gayka5915::AttrNumber() 
{
  return 297327484710.0;
}


//-------------------------------------------------------------------------------
//   
// ---
bool Gayka5915::_EditSpcObj() 
{
  int spcValue;
    // 
  if( !m_paramGayka.flags.isp )
    ksSpcVisible( SPC_NAME, 2, 0 );
  else 
  {
    spcValue = 2;
    ksSpcVisible( SPC_NAME, 2, 1 );
    ksSpcChangeValue( SPC_NAME , 2, &spcValue, UINT_ATTR_TYPE  );
  }

  // 
  ksSpcChangeValue( SPC_NAME , 4, &m_paramGayka.dr, FLOAT_ATTR_TYPE  );

  //   
  if( !m_paramGayka.flags.pitch )
  {
    //     
    ksSpcVisible( SPC_NAME, 5, 0 );
    ksSpcVisible( SPC_NAME, 6, 0 ); // 
  }
  else 
  {
    ksSpcVisible( SPC_NAME, 5, 1 );
    ksSpcVisible( SPC_NAME, 6, 1 ); // 
    ksSpcChangeValue( SPC_NAME , 6, &m_paramGayka.p, FLOAT_ATTR_TYPE );
  }

  //   
  ksSpcVisible( SPC_NAME, 7, 0 );

  //   
  ksSpcVisible( SPC_NAME, 8, 0 );

  //  
  ksSpcVisible( SPC_NAME, 9, 0 );

  //  
  ksSpcVisible( SPC_NAME, 10, 0 );

  // 
  spcValue = 5915;
  ksSpcChangeValue( SPC_NAME, 12, &spcValue, UINT_ATTR_TYPE  );
   
  //  
  double massa = m_paramGayka.massa * ( !m_paramGayka.indexMassa      ? 1 : 
                                         m_paramGayka.indexMassa == 1 ?
                                         0.356 : 1.08 ) / COUNT_MASSA;
  char buf[256];
  sprintf( buf, "%f", massa );
  ksSpcMassa( buf ); 
  return true;
}


//-------------------------------------------------------------------------------
//  
// ---
void Gayka5915::GetGroup()
{
  int k2 = int(m_paramGayka.flags.isp) + 1;

  switch (m_baseParam.drawType) 
  {
    case ID_VIEW :  // 
      if ( !(m_baseParam.flags.flagSimp) ) 
      {
        //   
        ksColouring( WORKWINDOWCOLOR );
          Gayka_K( 0, 0, 0, m_paramGayka.s, 0, m_paramGayka.h,  1, 0, m_paramGayka.d2, k2, true );
          Gayka_K( 0, 0, 0, m_paramGayka.s, 0, m_paramGayka.h, -1, 0, m_paramGayka.d2, k2, true );
        EndObj();
        //   
        Gayka_K( 0, 0, 0, m_paramGayka.s, 0, m_paramGayka.h,  1, 0, m_paramGayka.d2, k2, false );
        Gayka_K( 0, 0, 0, m_paramGayka.s, 0, m_paramGayka.h, -1, 0, m_paramGayka.d2, k2, false );
        
      }
      else 
      {
        //   
        ksColouring( WORKWINDOWCOLOR );
          Gayka_K_Y( 0, &m_paramGayka,  1, true );
          Gayka_K_Y( 0, &m_paramGayka, -1, true );
        EndObj();
        //   
        Gayka_K_Y( 0, &m_paramGayka,  1, false );
        Gayka_K_Y( 0, &m_paramGayka, -1, false );
      }
      if ( m_baseParam.flags.flagAxis )
        LineSeg( -AXIS_END, 0, m_paramGayka.h + AXIS_END, 0, AXIS_LINE );
    break;
    case ID_LEFT: //  
      if ( m_baseParam.flags.flagAxis )
        LineSeg( -AXIS_END, 0, m_paramGayka.h + AXIS_END, 0, AXIS_LINE );
      if ( m_baseParam.flags.flagSimp )
        k2 = 3;
      //   
      ksColouring( WORKWINDOWCOLOR );
        Gayka_K_Side( 0, m_paramGayka.s, m_paramGayka.D, m_paramGayka.d2, m_paramGayka.h,  1, k2, true );
        Gayka_K_Side( 0, m_paramGayka.s, m_paramGayka.D, m_paramGayka.d2, m_paramGayka.h, -1, k2, true );
      EndObj();
      //   
      Gayka_K_Side( 0, m_paramGayka.s, m_paramGayka.D, m_paramGayka.d2, m_paramGayka.h,  1, k2, false );
      Gayka_K_Side( 0, m_paramGayka.s, m_paramGayka.D, m_paramGayka.d2, m_paramGayka.h, -1, k2, false );
    break;
    case ID_TOP : //  
      //   
      ksColouring( WORKWINDOWCOLOR );
        Gayka_Sverhu( true );
      EndObj();
      //   
      Gayka_Sverhu( false );
    break;
    case ID_SEC : // -/-
      if( !(m_baseParam.flags.flagSimp) ) 
      {
        //   
        ksColouring( WORKWINDOWCOLOR );
          Gayka_K( 0, 0, 0, m_paramGayka.s, 0, m_paramGayka.h, 1, 0, m_paramGayka.d2, k2, true );
          Gayka_P_K( -1, true );
        EndObj();
        //   
        Gayka_K( 0, 0, 0, m_paramGayka.s, 0, m_paramGayka.h, 1, 0, m_paramGayka.d2, k2, false );
        Gayka_P_K( -1, false );
      }
      else 
      {
        //   
        ksColouring( WORKWINDOWCOLOR );
          Gayka_K_Y( 0, &m_paramGayka, 1, true );
          Gayka_P_K( -1, true );
        EndObj();
        //   
        Gayka_K_Y( 0, &m_paramGayka, 1, false );
        Gayka_P_K( -1, false );
      }
      if ( m_baseParam.flags.flagAxis )
        LineSeg( -AXIS_END, 0, m_paramGayka.h + AXIS_END, 0, AXIS_LINE );
    break;
  }
}


//-------------------------------------------------------------------------------
//      
// ---
void Gayka5915::RedrawPreview() 
{
  Gayka5915 tmp(*this);
  tmp.InitParam();
  tmp.GetGroup();
}


//-------------------------------------------------------------------------------
//   
// ---
void Gayka5915::ShowControls() 
{
  
  //   
  IPropertyListPtr diamList( CreateList() );
  if ( diamList ) 
  {
    InitPropertyControl( diamList, IDP_DIAM, IDS_DIAM_HINT/*hint*/, IDS_DIAM_TIPS/*tips*/ );
    bool flagRepeat = true;
    struct { char s[20]; float dr; } record;
	  while ( flagRepeat ) 
    {
		  flagRepeat = !!ReadRecord( base.bg, base.rg1, &record );
		  if ( flagRepeat ) 
      {
			  diamList->Add( _variant_t(record.dr, VT_R8 ) );
      }
    }
	  diamList->Value = _variant_t(m_paramGayka.dr);
  }

  // 
  IPropertySeparatorPtr sep( CreateSeparator(ksSeparatorDownName) );
  if ( sep ) 
  {
    sep->Name = "";
  }

  IPropertyMultiButtonPtr buttons( CreateMultiButton(ksRadioButton) );
  if ( buttons ) 
  {
    InitPropertyControl( buttons, IDP_REFLECTION, IDS_REFLECTION_HINT/*hint*/, IDS_REFLECTION_TIPS/*tips*/, TRUE, ksNameVerticalVisible );
    //  
    AddButton2( buttons, ID_VIEW, IDP_BMP_G_VIEW, m_baseParam.drawType == ID_VIEW );
    //   
    AddButton2( buttons, ID_LEFT, IDP_BMP_G_LEFT, m_baseParam.drawType == ID_LEFT );
    //   
    AddButton2( buttons, ID_TOP,  IDP_BMP_G_TOP,  m_baseParam.drawType == ID_TOP  );
    //  / 
    AddButton2( buttons, ID_SEC,  IDP_BMP_G_SEC,  m_baseParam.drawType == ID_SEC  );
  }
  
  // 
  IPropertyMultiButtonPtr ispButtons( CreateMultiButton(ksRadioButton) );
  if ( ispButtons ) 
  {
    InitPropertyControl( ispButtons, IDP_ISP, IDS_ISP_HINT/*hint*/, IDS_ISP_TIPS/*tips*/, TRUE, ksNameVerticalVisible );
    //   1
    AddButton( ispButtons, IDP_BMP_G_I1,  m_paramGayka.flags.isp );
    //   2
    AddButton( ispButtons, IDP_BMP_G_I2, !m_paramGayka.flags.isp );
  }
  
  // 
  IPropertyMultiButtonPtr simpleButtons( CreateMultiButton(ksCheckButton) );
  if ( simpleButtons ) 
  {
    InitPropertyControl( simpleButtons, IDP_SIMPLE, IDS_SIMPLE_HINT/*hint*/, IDS_SIMPLE_TIPS/*tips*/, TRUE, ksNameVerticalVisible );
    AddButton2( simpleButtons, ID_SIMPLE, IDP_BMP_G_SIMPLE, m_baseParam.flags.flagSimp );
    AddButton( simpleButtons,  ID_AXIS, m_baseParam.flags.flagAxis );
  }
  
  sep = CreateSeparator( ksSeparatorDownName );

  //  
  IPropertyMultiButtonPtr paramButtons( CreateMultiButton(ksCheckButton) );
  if ( paramButtons ) 
  {
    InitPropertyControl( paramButtons, IDP_FLAGS, IDS_FLAGS_TIPS/*hint*/, IDS_FLAGS_TIPS/*tips*/, TRUE, ksNameVerticalVisible );
    //  
    AddButton( paramButtons, IDP_BMP_G_STEP,  m_paramGayka.flags.pitch, m_paramGayka.flags.pitch_on );
    //   
    AddButton( paramButtons, IDP_BMP_KEY,    !m_paramGayka.flags.key_s, m_paramGayka.flags.pitch_on );
  }

  //  
  CPar::ShowControls();
} 


//---------------------------------------------------------------------------------------------------- 
//       
// ---
bool Gayka5915::OnChangeControlValue( /*IPropertyControl* cntrl,*/ 
                                       long               ctrlID, 
                                       const VARIANT&     newVal ) 
{

  bool res = CPar::OnChangeControlValue( ctrlID, newVal );
  if ( !res ) 
  {
    
    switch ( ctrlID ) 
    {
      case IDP_DIAM: //  
      { 
        float diam = (float)newVal.dblVal; 
      	if ( ::fabs(diam - m_paramGayka.dr ) > 0.001 ) 
        {
		      m_paramGayka.dr = diam;  
          if ( ReadGaykaBase ( m_paramGayka.dr, base, &m_paramGayka ) ) 
          {
            IPropertyMultiButtonPtr control( GetPropertyControl(IDP_FLAGS) ); 
            if ( control ) 
            {
              control->ButtonEnable [ IDP_BMP_G_STEP] = m_paramGayka.flags.pitch_on;
              control->ButtonEnable [ IDP_BMP_KEY   ] = m_paramGayka.flags.key_s_on;
              control->ButtonChecked[ IDP_BMP_KEY   ] = m_paramGayka.flags.key_s; 
              res = true;
            }
          }
        }
        break; 
      }
    }
    if ( res ) 
      changed = true; //  
  }
  return res;  
} 


//---------------------------------------------------------------------------------------------------- 
//   
// ---
bool Gayka5915::OnButtonClick( long buttonID ) 
{ 

  switch ( buttonID ) { 

    case IDP_BMP_G_I1: //  1
      m_paramGayka.flags.isp = 0;
      changed = true;  //     
      break;
       
    case IDP_BMP_G_I2: //  2
      m_paramGayka.flags.isp = 1;
      changed = true;  //     
      break;
 
    case IDP_BMP_G_STEP: //  
      m_paramGayka.flags.pitch = !m_paramGayka.flags.pitch;
      changed = true;  //     
      break; 

    case IDP_BMP_KEY:  //    
      m_paramGayka.flags.key_s = !m_paramGayka.flags.key_s;
      changed = true;  //      
      break;
  }
  return changed ? 
         changed :                        //  ButtonClick
         CPar::OnButtonClick( buttonID ); //     
}


//---------------------------------------------------------------------------------------------------- 
//   
// ---
void Gayka5915::ShowParam() 
{
  AddDoubleToGrig( IDS_PITCH,      m_paramGayka.p     ); // " "
  AddDoubleToGrig( IDS_DIM_KEY,    m_paramGayka.s     ); // "  "
  AddDoubleToGrig( IDS_HEAD_H,     m_paramGayka.h     ); // " "
  AddDoubleToGrig( IDS_MASSA_1000, m_paramGayka.massa ); // " 1000 "
}


//---------------------------------------------------------------------------------------------------- 
//  
// ---
CPar * NewGayka5915( reference obj ) 
{
  return new Gayka5915( obj );
}

