//////////////////////////////////////////////////////////////////////////////// 
// 
// step3a.cpp -  - a
// 
// 1.                              - WorkContour
// 2.               - TDemWork
// 3.                         - DrawViewPointer
// 4.                     - WorkStamp
// 5.                             - TableWork
// 6.                        - DrawEquidistant
// 7.                              - DrawEllipse
// 8.                           - DrawPolyline
// 9.  Nurbs                             - DrawNurbs
// 10.                        - WorkTolerance
// 11.            - DrawSpecRough
// 12.     - DrawInsFragment1
// 13.         - DrawInsFragment2
// 
//////////////////////////////////////////////////////////////////////////////// 
#ifndef __WINDOWS_H  
#include <windows.h>
#endif

#ifndef __STDIO_H
#include <stdio.h>
#endif

#ifndef __STRING_H
#include <string.h>
#endif

#ifndef __STDLIB_H
#include <stdlib.h>
#endif

#ifndef __LIBTOOL_H
#include "libtool.h"
#endif

#ifndef __STEP3A_RH
#include "step3a.rh"
#endif 


//-------------------------------------------------------------------------------
//   
// ---
extern "C" unsigned int __export __pascal LIBRARYID()
{
  return IDR_LIBID;
}


//-------------------------------------------------------------------------------
//   
// ---
void WorkContour();
void TDemWork();
void TableWork();
void DrawViewPointer();
void WorkStamp();
void WorkTolerance();
void DrawEquidistant();
void DrawEllipse();
void DrawPolyline();
void DrawNurbs();
void DrawInsFragment1();
void DrawInsFragment2();
void DrawSpecRough();


//-------------------------------------------------------------------------------
//   
// ---
extern "C" void __export __pascal LIBRARYENTRY( unsigned int comm )
{
  if ( ksGetCurrentDocument( 1 ) )
  {
    switch ( comm )
    {
      case 1  : WorkContour();      break; // 
      case 2  : TDemWork();         break; //  
      case 3  : DrawViewPointer();  break; //  
      case 4  : WorkStamp();        break; //   
      case 5  : TableWork();        break; // 
      case 6  : DrawEquidistant();  break; // 
      case 7  : DrawEllipse();      break; // 
      case 8  : DrawPolyline();     break; // 
      case 9  : DrawNurbs();        break; // Nurbs
      case 10 : WorkTolerance();    break; //  
      case 11 : DrawSpecRough();    break; //  
      case 12 : DrawInsFragment1(); break; //    
      case 13 : DrawInsFragment2(); break; //   
    }
  }
  else
    Message( "   \n  /" );  
}


//-------------------------------------------------------------------------------
//  
// ---
void WorkContour()
{
  Contour( 1 );  //  , style -	  ( 1 - , 2 - ,
                 // 3 - , 4 - ,  5 - , 6 - ,
                 // 7 -    ,  8 -  ,
                 // 9 -  , 10 -  ,
                 // 11 - ,    )
    LineSeg( 20, 30, 50, 30, 1 );
    ArcByPoint( 50, 20, 10, 50, 30, 50, 10, -1, 1 );
    //  
    Contour( 2 );
      LineSeg( 50, 10, 20, 10, 1 );
      ArcByPoint( 20, 20, 10, 20, 10, 20, 30, -1, 1 );
    EndObj();
  reference rContour = EndObj(); //  EndObj      
  
  //  
  LightObj( rContour, 1 );
  Message( "  " );
  //    
  LightObj( rContour, 0 );
  
  //   , type -   ( 0 -  , 1 -  )
  reference g = NewGroup( 0 );
  EndGroup(); //    
  
  //     
  AddObjGroup( g, rContour );
  MoveObj( g, 10, 10 ); //    10   OX,  10   OY
}


//-------------------------------------------------------------------------------
//   
// ---
void TDemWork()
{
  //    
  reference pArRect = CreateArray( RECT_ARR, 0 ) ;
  
  //      
  
  RectParam par; //      
  
  //    
  par.pBot.x = 230; //     
  par.pBot.y = 65;
  par.pTop.x = 415; //     
  par.pTop.y = 80;
  //    ,     
  AddArrayItem( pArRect, -1, &par, sizeof( par ) );
  par.pBot.x = 45;  //     
  par.pBot.y = 15;
  par.pTop.x = 230; //     
  par.pTop.y = 60;
  //    ,     
  AddArrayItem( pArRect, -1, &par, sizeof( par ) );
 
  //   
  OpenTechnicalDemand( pArRect, 0 ); //    ,  0,
                                     //        
    //    
    TextLine( NEW_LINE,    //  ,    
              0,           //  
              0,           //    
              "1111111" ); // 
    TextLine( NEW_LINE, 0, 0, "2222222" );
    TextLine( NEW_LINE, 0, 0, "3333333" );
    TextLine( NEW_LINE, 0, 0, "4444444" );
    TextLine( NEW_LINE, 0, 0, "5555555" );
    TextLine( NEW_LINE, 0, 0, "6666666" );
  //   
  reference rTechnicalDemand = CloseTechnicalDemand(); 
 
  Message( "  " );
  //   
  DeleteObj( rTechnicalDemand );

/*
  //       4- 
  create_tdem(20, 0, 0 ,20, 0,0, 27,0,0,27,0,0);

    string_obj("111$d2&4522;33&463$1111111111");
    string_obj("2222&46222222222");
    string_obj("33$333;3333$3333");
    string_obj("4444444444444");
    string_obj("5555555555555");
      
  close_tdem();
*/ 
  
}


//-------------------------------------------------------------------------------
//  
// ---
void TableWork()
{
  Table(); //  ,      
    //  
    LineSeg( 50, 50, 90, 50, 1 ); 
    LineSeg( 50, 40, 90, 40, 1 );
    LineSeg( 50, 30, 90, 30, 1 );
    LineSeg( 50, 50, 50, 30, 1 );
    LineSeg( 70, 50, 70, 30, 1 );
    LineSeg( 90, 50, 90, 30, 1 );

	  Text( 52, 48, 0, 5, 1,0,"1" );
	  Text( 72, 48, 0, 5, 1,0,"2" );
	  Text( 52, 38, 0, 5, 1,0,"3" );
	  Text( 72, 38, 0, 5, 1,0,"4" );    
    //     
/*  Text( 52, 48,            //  
          0,                 //   
          5,                 //  
          1,                 //  
          0,                 //  ,    
          " " ); // 
    //  
    
    ParagraphParam parParagraph; //   

    //   
    parParagraph.style   = 0;  //    (0 -   ,
                               // 1 -  , 2 -    ,
                               // 3 -   , 4 -    ,
                               // 5 -    -, 6 -  \  -,
                               // 7 -    -, 8 -     ,
                               // 9 -    , 10 -    ,
                               // 11 -    , 12 -     ,
                               // 13 -     ,
                               // 14 -     )
    parParagraph.x       = 72; //    
    parParagraph.y       = 48; 
    parParagraph.ang     = 0;  //   
    parParagraph.hFormat = 0;  //    ( 0 -  ,
                               // 1 -  , 2 -     )
    parParagraph.vFormat = 0;  //    ( 0 -  ,
                               // 1-   )
    parParagraph.height  = 20; //   
    parParagraph.width   = 20; //   
  
    //      
    Paragraph( &parParagraph );
      //    
      TextLine( NEW_LINE,    //  ,    
                0,           //  
                0,           //    
                "" ); //     
      TextLine( NEW_LINE|BOLD_ON, 0, 0," " );
      TextLine( NEW_LINE|BOLD_OFF | ITALIC_ON, 0, 0, " " );
    //     EndObj
    EndObj(); 
*/  //     EndObj  
  EndObj();
}


//-------------------------------------------------------------------------------
//   
// ---
void DrawViewPointer()
{
  ViewPointerParam parViewPointer; //     
  
  parViewPointer.style = 0;	 //    (0 -   ,
                             // 1 -  , 2 -    ,
                             // 3 -   , 4 -    ,
                             // 5 -    -, 6 -  \  -,
                             // 7 -    -, 8 -     ,
                             // 9 -    , 10 -    ,
                             // 11 -    , 12 -     ,
                             // 13 -     ,
                             // 14 -     )
  parViewPointer.x1    = 55; //    (  )
  parViewPointer.y1    = 50; 
  parViewPointer.x2    = 40; //    
  parViewPointer.y2    = 50; 
  parViewPointer.xt    = 40; //  
  parViewPointer.yt    = 52;
  parViewPointer.type	 = 0;  //      ( 0 -    ,
                             // 1 -     )
  strcpy( parViewPointer.str, "A" ); // 
 
  //    
  reference rViewPointer = ViewPointer( &parViewPointer ); 
  
  //  
  LightObj( rViewPointer, 1 );
  Message( "  " );
  //    
  LightObj( rViewPointer, 0 );  
}


//-------------------------------------------------------------------------------
//   
// ---
void WorkStamp()
{
  OpenStamp(); //   / 
    ColumnNumber( 2 ); //    ,      
    //    
    TextLine( NEW_LINE,    //  ,    
              0,           //  
              0,           //    
              "1111111" ); // 
  CloseStamp(); //   / 
}


//-------------------------------------------------------------------------------
//  
// ---
void WorkTolerance()
{
  ToleranceParam parTolerance; //          
  MathPointParam parPoint;     //    
 
  //   1- 
  //      1- 
  parTolerance.branch1.pMathPoint = CreateArray( POINT_ARR,  0 );
  parTolerance.branch1.arrowType  = 2; //   ( 0 -  , 1 - , 2 -  )
  parTolerance.branch1.tCorner    = 1; //   1  8,    ""  
  
  //   
  parPoint.x = 40; 
  parPoint.y = 10;
  //     ,     
  AddArrayItem( parTolerance.branch1.pMathPoint, -1, &parPoint, sizeof( parPoint ) );
  
  //   2- 
  //      2- 
  parTolerance.branch2.pMathPoint = CreateArray( POINT_ARR,  0 );
  parTolerance.branch2.arrowType  = 1; //   ( 0 -  , 1 - , 2 -  )
  parTolerance.branch2.tCorner    = 5; //   1  8,    ""  
  
  //    
  parPoint.x = 100; 
  parPoint.y = 50;   
  //  1-    ,     
  AddArrayItem( parTolerance.branch2.pMathPoint, -1, &parPoint, sizeof( parPoint ) );  
  
  //    
  parPoint.x = 100; 
  parPoint.y = 10;  
  //  2-    ,     
  AddArrayItem( parTolerance.branch2.pMathPoint, -1, &parPoint, sizeof( parPoint ) );
 
  //   
  parTolerance.x    = 40; 
  parTolerance.y    = 40; 
  parTolerance.type = 0;  //    ( 0 - , 1 -  ) 
  //    ,    (    1010 )    
  Tolerance( &parTolerance );
    //     ( 1-  ) 
    ColumnNumber( 1 );
    //    
    //   
    int symbol = 26;
    TextLine ( SPECIAL_SYMBOL, //  ,    
               SPECIAL,        //  
               &symbol,        //    
               "" );           // 
    
    // 2- 
    ColumnNumber( 2 );
    TextLine ( NEW_LINE, 0, 0, "111" );
    
    // 3- 
    ColumnNumber( 3 );
    TextLine ( NEW_LINE, 0, 0, "222" );
    
    // 11- 
    ColumnNumber( 11 );
    //  
    symbol = 23;
    TextLine ( SPECIAL_SYMBOL, SPECIAL, &symbol, "" );
    
    // 12- 
    ColumnNumber( 12 );
    TextLine ( NEW_LINE, 0, 0, "333" );
    
    // 13- 
    ColumnNumber( 13 );
    TextLine ( NEW_LINE, 0, 0, "444" );
  reference rTolerance = EndObj(); //  EndObj        

  //   
  LightObj( rTolerance, 1 );
  Message( "   " );
  //     
  LightObj( rTolerance, 0 ); 
  
  DeleteArray( parTolerance.branch2.pMathPoint ); //     2- 
  DeleteArray( parTolerance.branch1.pMathPoint ); //     1- 
}


//-------------------------------------------------------------------------------
//  
// ---
void DrawEquidistant()
{
  EquidistantParam parEquidistant; //   
  
  parEquidistant.side     = 2; // ,      ( 0 -   ,
                               // 1 -   , 2 -    )
  parEquidistant.cutMode  = 0; //     ( 0 -  , 1 -   )
  parEquidistant.degState = 0; //      ( 0 - 
                               //  , 1 -    )
  parEquidistant.radRight = 5; //  
  parEquidistant.radLeft  = 3; //  
  parEquidistant.style    = 1; // C  ( 1 - , 2 - , 3 - , 4 - , 
                               // 5 - , 6 - , 7 -    , 
                               // 8 -  , 9 -  , 10 -  ,
                               // 11 - ,    )
 
  //     
  RequestInfo info;
  memset( &info, 0, sizeof( info ) );
  
  //       
  info.prompt = " ";
  
  //   
  double x, y;  
  
  //     
  while( Cursor( &info, &x, &y, 0 ) )
  {
    //       
    parEquidistant.geoObj = FindObj( x, y,  //  
                                     1e6 ); //   -     x,y
    //   
    if ( ExistObj( parEquidistant.geoObj ) )
    { 
      //  
      reference rEquidistant = Equidistant( &parEquidistant );
      
      //  
      LightObj( rEquidistant, 1 );
      Message( "  " );
      //    
      LightObj( rEquidistant, 0 ); 
    }
    else
      Error( "  " );
  }
}


//-------------------------------------------------------------------------------
//  
// ---
void DrawEllipse()
{
  EllipseParam parEllipse; //   

  parEllipse.xc    = 50; //   
  parEllipse.yc    = 40; 
  parEllipse.a     = 20; //   
  parEllipse.b     = 10; 
  parEllipse.ang   = 0;  //        X
  parEllipse.style = 1;  // C  ( 1 - , 2 - , 3 - , 4 - , 
                         // 5 - , 6 - , 7 -    , 
                         // 8 -  , 9 -  , 10 -  ,
                         // 11 - ,    )
  
  //  
  reference rEllipse = ksEllipse( &parEllipse );
  
  //  
  LightObj( rEllipse, 1 );
  Message( "  " );
  //    
  LightObj( rEllipse, 0 ); 
}


//-------------------------------------------------------------------------------
//  
// ---
void DrawPolyline()
{
  //    
  MathPointParam arPoint[] = { {10, 10}, {20, 20}, {30, 10}, {40, 20} }; 

  /*
  //      
  ksPolyline( 1 ); //  , style - c  ( 1 - ,
  // 2 - , 3 - , 4 - , 5 - , 6 - ,
  // 7 -    , 8 -  , 9 -  ,
  // 10 -  , 11 - ,    )
  for ( int i = 0; i < 4; i++ )
    Point( arPoint[i].x, arPoint[i].y, 1 ); //    
  reference rPolyline = EndObj(); //  EndObj       
  */
  
  //     
  PolylineParam parPolyline; //    
  
  //      
  parPolyline.pMathPoint = CreateArray( POINT_ARR,  0 );
  //      
  for ( int i = 0; i < 4; i++ )
    AddArrayItem( parPolyline.pMathPoint, -1, &arPoint[i], sizeof( arPoint[i] ) );
  parPolyline.style = 2; // C  ( 1 - , 2 - , 3 - , 4 - , 
                         // 5 - , 6 - , 7 -    , 
                         // 8 -  , 9 -  , 10 -  ,
                         // 11 - ,    )
  reference rPolyline = _ksPolyline( &parPolyline ); //  
  
  DeleteArray( parPolyline.pMathPoint );  
  
  //  
  LightObj( rPolyline, 1 );
  Message( "  " );
  //    
  LightObj( rPolyline, 0 );  
}


//-------------------------------------------------------------------------------
//  Nurbs - 
// ---
void DrawNurbs()
{
  //    NURBS
  // struct NurbsPointParam 
  // {
  //   double x, y;   //    
  //   double weight; //   (   )
  // };

  //    
  NurbsPointParam arPoint[] = { {0, 0, 1}, {20, 20, 1}, {50, 10, 1}, {70, 20, 1}, {100, 0, 1},  {50, -50, 1} };

  //  Nurbs    
  Nurbs( 3, 0, 1 );
  for ( int i = 0; i < 6; i++ )
    NurbsPoint( &arPoint[i] ); //    
  reference rNurbs = EndObj(); //  EndObj       

  //  
  LightObj( rNurbs, 1 );
  Message( "  " );
  //    
  LightObj( rNurbs, 0 );  
}


//-------------------------------------------------------------------------------
//   
// ---
void DrawInsFragment1()
{
  char file[255];
  if( ChoiceFile("*.frw", NULL, file, 255) )
  {
    //     
    reference rDefinitionFragment = FragmentDefinition( file,       //   
                                                        "Fragment", //  
                                                        1 );        //   ( 0 - 
                                                                    //  , 1 -   )
    PlacementParam parPlacement; //  
    
    parPlacement.xBase = 30; //       
    parPlacement.yBase = 40;
    parPlacement.ang   = 45; //      
    parPlacement.scale = 2 ; // 
    
    //    
    reference rInsertFragment = InsertFragment( rDefinitionFragment, //   
                                                0,                   //     ( 0 -    1 -    )
                                                &parPlacement );     //  
      
    //  
    LightObj( rInsertFragment, 1 );
    Message( "  " );
    //    
    LightObj( rInsertFragment, 0 ); 
  }
}


//-------------------------------------------------------------------------------
//   
// ---
void DrawInsFragment2()
{
  //   
  LocalFragmentDefinition( "Local" ); //    
    LineSeg( 0, 0, 10, 0, 1 );
    LineSeg( 0, 0, 0, 10, 1 );
    ArcByPoint( 0, 0, 10, 10, 0, 0, 10, -1, 1 );
  //      
  //       
  reference rDefinitionFragment = CloseLocalFragmentDefinition(); 
 
  PlacementParam parPlacement; //  
  
  parPlacement.xBase = 30; //       
  parPlacement.yBase = 40;
  parPlacement.ang   = 45; //      
  parPlacement.scale = 2 ; // 

  //    
  reference rInsertFragment = InsertFragment( rDefinitionFragment, //   
                                              0,                   //     ( 0 -    1 -    )
                                              &parPlacement );     //  
  
  //  
  LightObj( rInsertFragment, 1 );
  Message( "  " );
  //    
  LightObj( rInsertFragment, 0 ); 
}


//-------------------------------------------------------------------------------
// 
// ---
void DrawSpecRough()
{
  //    
  SpecRough( 2,        //   ( 0 -    , 1 -  
                       //  , 2 -     
             1,        //     ( 1 - , 0 -  )
             "Rz40" ); //  
}