//////////////////////////////////////////////////////////////////////////////// 
// 
// step5.cpp - 
// 
// 1.             - DrawTransform
// 2.                        - DrawCopy
// 3.                          - DrawSymmetry
// 4.                - EditTolerance
// 5.                     - EditTable
// 6.        - EditStamp
// 7.                           - GetTextTT
// 8.     - ChangeTechnicalDemand
// 9.                          - ShowInsertFragment
// 10.             - EditFragmentLibrary
// 11.                 - ShowInsertFragment1
// 12.                     - EditTable1
// 13.               - EditTolerance1
// 
//////////////////////////////////////////////////////////////////////////////// 
#ifndef __WINDOWS_H 
#include <windows.h>
#endif

#ifndef  __MATH_H
#include <math.h>
#endif

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

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

#ifndef __CSTRING_H
#include "cstring.h"
#endif

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

#ifndef __STEP5_RH
#include "step5.rh"
#endif 


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


//-------------------------------------------------------------------------------
//   
// ---
void DrawTransform();
void DrawCopy();
void DrawSymmetry();
void EditTolerance();
void EditTable();
void EditStamp();
void GetTextTT();
void ShowInsertFragment ();
void ShowInsertFragment1 ();
void EditFragmentLibrary();
void ChangeTechnicalDemand ();
void EditTable1();
void EditTolerance1();


//-------------------------------------------------------------------------------
//   
// ---
extern "C" void __export __pascal LIBRARYENTRY( unsigned int comm )
{
  if ( ksGetCurrentDocument( 1 ) )
  {
    switch ( comm )
    {
      case 1  : DrawTransform();         break; //    
      case 2  : DrawCopy();              break; //  
      case 3  : DrawSymmetry();          break; //  
      case 4  : EditTolerance();         break; //   
      case 5  : EditTable();             break; //  
      case 6  : EditStamp();             break; //      
      case 7  : GetTextTT();             break; //   
      case 8  : ChangeTechnicalDemand(); break; //   
      case 9  : ShowInsertFragment();    break; //  
      case 10 : EditFragmentLibrary();   break; //    
      case 11 : ShowInsertFragment1();   break; //   
      case 12 : EditTable1();            break; //  
      case 13 : EditTolerance1();        break; //   
    }
  }
  else
    Error( "   \n  /" );
}


//-------------------------------------------------------------------------------
//  
// ---
void DrawTransform()
{
  //    
  reference rObject;
  
  //   , type -   ( 0 -  , 1 -  )
  rObject = NewGroup( 0 );
    //  
    LineSeg( 30, 30, 60, 30, 1 );
    LineSeg( 60, 30, 60, 60, 1 );
    LineSeg( 60, 60, 30, 60, 1 );
    LineSeg( 30, 60, 30, 30, 1 );
    //  
    Hatch( 0, 45, 2, 0, 0, 0 );
      LineSeg( 30, 30, 60, 30, 1 );
      LineSeg( 60, 30, 60, 60, 1 );
      LineSeg( 60, 60, 30, 60, 1 );
      LineSeg( 30, 60, 30, 30, 1 );
    EndObj(); //   
  EndGroup(); //    
 
  /*
  //    -  
  rObject = ArcBy3Points( 20, 60, 40, 70, 20, 80, 1 );
  */

  /*
  //   1 -    
  if ( !( rObject = GetViewReference( 1 ) ) )
  {
    MessageBoxResult(); //       
    return;
  }
  */
 
  /*
  //   1    
  if ( !( rObject = GetLayerReference( 1 ) ) )
  {
    MessageBoxResult(); //       
    return;
  }
  */
 
  /*
  //   
  rObject = NULL;
  */
 
  Message( "C  20, 20, 45, 2" );
  
  Mtr( 20, 20, 45, 2 );    //    ( 20   OX, 20   OY,   45 ,  2:1 )
  TransformObj( rObject ); //     
  DeleteMtr();             //    

  Message( " " );

  Mtr( -20, -20, 0, 1 );   //    ( -20   OX, -20   OY )
  TransformObj( rObject ); //     
  DeleteMtr();             //    

  Mtr( 0, 0, 0, 0.5 );     //    (  1:2 )
  TransformObj( rObject ); //     
  DeleteMtr();             //    

  Mtr( 0, 0, -45, 1 );     //    (   -45  )
  TransformObj( rObject ); //     
  DeleteMtr();             //    
}


//-------------------------------------------------------------------------------
//    
// ---
void DrawSymmetry()
{
  //     .     
  //    ,       
  // .       8 
  
  //   , type -   ( 0 -  , 1 -  )
  reference rGroup = NewGroup( 0 );
    LineSeg( 20, 10, 20, 30, 1 );
    LineSeg( 20, 30, 40, 30, 1 );
    LineSeg( 40, 30, 40, 10, 1 );
    LineSeg( 40, 10, 20, 10, 1 );
    //   
    ksSymmetryObj( rGroup, //   
                   80, 10, //   
                   80, 20, //   
                   1 );    //   ( 0 -   , 1 -    )
  EndGroup(); //    
}


//-------------------------------------------------------------------------------
//    
// ---
void DrawCopy()
{
  ViewParam parView; //     
 
  parView.x     = 20;                  //   
  parView.y     = 60;
  parView.scale = 1;                   //  
  parView.ang   = 0;                   //   
  parView.color = RGB( 10, 20, 10 );   //     
  parView.state = stACTIVE;            //  
  strcpy( parView.name, "User view" ); //  
  //     5,  0.5,   45 .
  int number = 5;
  reference rView = CreateSheetView( &parView, &number );

  LineSeg( 20, 10, 20, 30, 1 );
  LineSeg( 20, 30, 40, 30, 1 );
  LineSeg( 40, 30, 40, 10, 1 );
  LineSeg( 40, 10, 20, 10, 1 );

  //   (        )
  CopyObj( rView,  //   
           20, 60, //  
           40, 80, //    
           1,      // 
           0 );    //     
}


//-------------------------------------------------------------------------------
//    
// ---
void EditTolerance()
{
  //    

  //     
  RequestInfo info;
  memset( &info, 0, sizeof( info ) );

  //      
  info.prompt = "  ";
  
  //     
  double x, y; 

  //     
  if ( Cursor( &info, &x, &y, 0 ) )
  {
    //       
    reference rObject = FindObj( x, y,  //  
                                 1e6 ); //   -     x,y
    if ( rObject )
    {
      //     
      int type = GetObjParam( rObject, 0, 0, 0 );   
      if ( type == TOLERANCE_OBJ ) 
      {
        //     
        ksOpenTolerance( rObject );

        ToleranceParam parTolerance; //   
        
        //    
        GetObjParam( rObject,                //    
                     &parTolerance,          //    
                     sizeof( parTolerance ), //   
                     ALLPARAM );             //   
                   
        char buf[255];
        sprintf( buf, "tBase = %d, style = %d,  - %s,\nx = %4.2f, y = %4.2f", 
                 parTolerance.tBase, parTolerance.style, parTolerance.type ? "" : "", parTolerance.x, parTolerance.y );
        Message( buf );

        TextLineParam parTextLine; //    
        TextItemParam parTextItem; //     

        //       
        unsigned int number; //  
        //         
        while ( ksGetToleranceColumnText( &number, &parTextLine ) )
        {
          sprintf( buf, "number = %d, style = %d", number, parTextLine.style );
          Message( buf );

          //    
          int count = GetArrayCount( parTextLine.pTextItem );

          //    
          for ( int i = 0; i < count; i++ )
          {
            //    
            GetArrayItem( parTextLine.pTextItem, i, &parTextItem, sizeof( parTextItem ) );

            //    ( SPECIAL_SYMBOL, FONT_SYMBOL, FRACTION_TYPE, SUM_TYPE  0 )
            if ( !parTextItem.tip )
              sprintf( buf, "i = %d, font.height = %4.2f,\ns = %s,\nfont.fontName = %s,\nfont.bitVector = %d",
                       i, parTextItem.font.height, parTextItem.s, parTextItem.font.fontName, parTextItem.font.bitVector );
            else
              sprintf( buf, "i = %d, tip = %d, iSNumb = %d", i, parTextItem.tip, parTextItem.iSNumb );
            Message( buf );
          }
        }

        //    
        parTolerance.x += 10;
        parTolerance.y += 10;
        SetObjParam(  rObject,                 //    
                      &parTolerance,           //    
                      sizeof( parTolerance  ), //   
                      ALLPARAM );              //   

        //      
        ksDivideTableItem( 3,   //  
                           1,   //  ( 1 -  0 -  )
                           2 ); //   ( 0 - , 1 - , 2 - ,	7 -  )
        //       
        ksSetTableBorderStyle( 1,   //  
                               2,   //   ( 0 - ,	1 - ,	2 - ,	3 -  )
                               1 ); //   ( 0 - , 1 - , 2 - ,	7 -  )
        //      
        ksClearTableColumnText( 0 ); //  ,  0   
        //   
        ColumnNumber( 2 );
          TextLine ( NEW_LINE, 0, 0, " " );
        ColumnNumber( 4 );
          TextLine ( NEW_LINE, 0, 0, "4" );
  
        EndObj(); //     
      }
      else
        Error( "   " );
    }
    else
      Error( " " );
  }
}


//-------------------------------------------------------------------------------
//    
// ---
void EditTolerance1()
{
  //    

  //     
  RequestInfo info;
  memset( &info, 0, sizeof( info ) );

  //      
  info.prompt = "  ";
  
  //     
  double x, y; 

  //     
  if ( Cursor( &info, &x, &y, 0 ) )
  {
    //       
    reference rObject = FindObj( x, y,  //  
                                 1e6 ); //   -     x,y
    if ( rObject )
    {
      //     
      int type = GetObjParam( rObject, 0, 0, 0 );   
      if ( type == TOLERANCE_OBJ ) 
      {
        //     
        ksOpenTolerance( rObject );       
        
        TextLineParam parTextLine; //    
        TextItemParam parTextItem; //     
                  
        //       
        unsigned int number; //  
        //         
        while ( ksGetToleranceColumnText( &number, &parTextLine ) ) 
        {
          //    
          int count = GetArrayCount( parTextLine.pTextItem );
          
          //    
          for ( int i = 0; i < count; i++ ) 
          {
            //    
            GetArrayItem( parTextLine.pTextItem, i, &parTextItem, sizeof( parTextItem ) );
            
            //    - ,     "!!!"
            if ( strlen( parTextItem.s ) ) 
            {
              strcat( parTextItem.s, "!!!" );
              SetArrayItem( parTextLine.pTextItem, i, &parTextItem, sizeof( parTextItem ) );
            }
          }  
          //              
          ksSetToleranceColumnText( number, &parTextLine ); 
        }
        EndObj(); //     
      }
      else
        Error( "   " );
    }
    else
      Error( " " );
  }
}


//-------------------------------------------------------------------------------
//   
// ---
void EditTable()
{
  //    

  //     
  RequestInfo info;
  memset( &info, 0, sizeof( info ) );

  //    
  info.prompt = " ";
 
  //    
  double x, y;
  
  //     
  if ( Cursor( &info, &x, &y, 0 ) )
  {
    //       
    reference rObject = FindObj( x, y,  //  
                                 1e6 ); //   -     x,y
    if ( rObject )
    {
      //     
      int type = GetObjParam( rObject, 0, 0, 0 );   
      if ( type == TABLE_OBJ )
      {
        //    
        ksOpenTable( rObject );

        TextParam parText;         //   
        TextLineParam parTextLine; //    
        TextItemParam parTextItem; //     

        //       
        unsigned int number; //  
        //                 
        while ( ksGetTableColumnText( &number, &parText ) ) 
        {
          char buf[255];
          sprintf( buf, "number = %d", number );
          Message( buf );
          
          //    
          int count = GetArrayCount( parText.pTextLine );
     
          //   
          for ( int i = 0; i < count; i++ )
          {
            //    
            GetArrayItem( parText.pTextLine, i, &parTextLine, sizeof( parTextLine ) );
            
            sprintf( buf, "i = %d style = %d", i, parTextLine.style );
            Message( buf );

            //    
            int count = GetArrayCount( parTextLine.pTextItem );
          
            //    
            for ( int j = 0; j < count; j++ ) 
            {
              //    
              GetArrayItem( parTextLine.pTextItem, j, &parTextItem, sizeof( parTextItem ) );
            
              //    ( SPECIAL_SYMBOL, FONT_SYMBOL, FRACTION_TYPE, SUM_TYPE  0 )
              if ( !parTextItem.tip )
                sprintf( buf, "j = %d, font.height = %4.2f,\ns = %s,\nfont.fontName = %s,\nfont.bitVector = %d",
                         j, parTextItem.font.height, parTextItem.s, parTextItem.font.fontName, parTextItem.font.bitVector );
              else
                sprintf( buf, "j = %d, tip = %d, iSNumb = %d", j, parTextItem.tip, parTextItem.iSNumb );
              Message( buf );
            }  
          }
        } 
        
        //      
        ksDivideTableItem( 3,   //   
                           1,   //  ( 1 -  0 -  )
                           2 ); //   ( 0 - , 1 - , 2 - ,	7 -  )                
        //         
        ksSetTableBorderStyle( 1,   //    
                               2,   //   ( 0 - ,	1 - ,	2 - ,	3 -  )
                               1 ); //   ( 0 - , 1 - , 2 - ,	7 -  ) 
        //                             
        ksClearTableColumnText( 0 ); //  ,  0   
        //   
        ColumnNumber( 2 );
          Text( 0, 0, 0, 5, 1 ,0, " " );
        ColumnNumber( 4 );
          Text ( 0, 0, 0, 5, 1 ,0, "4" );
  
        EndObj(); //     
      }
      else
        Error( "  " );
    }
    else
      Error( " " );
  }
}


//-------------------------------------------------------------------------------
//   
// ---
void EditTable1()
{
  //    

  //     
  RequestInfo info;
  memset( &info, 0, sizeof( info ) );

  //    
  info.prompt = " ";
    
  //     
  double x, y;
 
  //     
  if ( Cursor( &info, &x, &y, 0 ) ) 
  {
    //       
    reference rObject = FindObj( x, y,  //  
                                 1e6 ); //   -     x,y
    if ( rObject )
    {
      //     
      int type = GetObjParam( rObject, 0, 0, 0 );  
      if ( type == TABLE_OBJ )
      {
        //    
        ksOpenTable( rObject );

        TextParam parText;         //   
        TextLineParam parTextLine; //    
        TextItemParam parTextItem; //     
        
        //       
        unsigned int number; //  
        //                 
        while ( ksGetTableColumnText( &number, &parText ) ) 
        {
          //    
          int count = GetArrayCount( parText.pTextLine );
     
          //   
          for ( int i = 0; i < count; i++ )
          {
            //    
            GetArrayItem( parText.pTextLine, i, &parTextLine, sizeof( parTextLine ) );

            //    
            int count = GetArrayCount( parTextLine.pTextItem );
          
            //    
            for ( int j = 0; j < count; j++ ) 
            {
              //    
              GetArrayItem( parTextLine.pTextItem, j, &parTextItem, sizeof( parTextItem ) );

              //    - ,     "!!!"
              if ( strlen( parTextItem.s ) ) 
              {
                strcat( parTextItem.s, "!!!" );
                SetArrayItem( parTextLine.pTextItem, j, &parTextItem, sizeof( parTextItem ) );
              }
            }  
            SetArrayItem( parText.pTextLine, i, &parTextLine, sizeof( parTextLine ) );
          }
          //             
          ksSetTableColumnText( number, &parText ); 
        }  
        EndObj(); //     
      }
      else
        Error( "  " );
    }
    else
      Error( " " );
  }
}


//-------------------------------------------------------------------------------
//       
// ---
void EditStamp()
{
  if( OpenStamp() ) //   / 
  {
    TextLineParam parTextLine; //    
    TextItemParam parTextItem; //     

    //       
    unsigned int number;   //  
    reference rColumnText; //  
    //         
    while ( 0 != ( rColumnText = GetStampColumnText( &number ) ) )
    {
      char buf[255];
      sprintf( buf, "number = %d", number );
      Message( buf );

      //    
      int count = GetArrayCount( rColumnText );

      //   
      for ( int i = 0; i < count; i++ )
      {
        //    
        GetArrayItem( rColumnText, i, &parTextLine, sizeof( parTextLine ) );

        sprintf( buf, "i = %d style = %d", i, parTextLine.style );
        Message( buf );

        //    
        int count = GetArrayCount( parTextLine.pTextItem );

        //    
        for ( int j = 0; j < count; j++ )
        {
          //    
          GetArrayItem( parTextLine.pTextItem, j, &parTextItem, sizeof( parTextItem ) );

          //    ( SPECIAL_SYMBOL, FONT_SYMBOL, FRACTION_TYPE, SUM_TYPE  0 )
          if ( !parTextItem.tip )
            sprintf( buf, "j = %d, font.height = %4.2f,\ns = %s,\nfont.fontName = %s,\nfont.bitVector = %d",
                     j, parTextItem.font.height, parTextItem.s, parTextItem.font.fontName, parTextItem.font.bitVector );
          else
            sprintf( buf, "j = %d, tip = %d, iSNumb = %d", j, parTextItem.tip, parTextItem.iSNumb );
          Message( buf );
        }
      }
    }
    //    / 
    ClearStamp( 0 ); //  ,  0   
    //  2- 
    ColumnNumber( 2 );
      TextLine ( NEW_LINE, 0, 0, " 2" );

    CloseStamp(); //   / 
  }
  else
    Error( "  " );
}


//-------------------------------------------------------------------------------
//    
// ---
void GetTextTT()
{
  //         
  //         
  reference rTechnicalDemand = GetReferenceDocumentPart( 1 ); //      
                                                              // ( 0 -  , 1 -   ,
                                                              // 2 -   , 3 -   ,
                                                              // 4 -     )
  if ( rTechnicalDemand ) 
  {
    TechnicalDemandParam parTechnicalDemand; //    
    
    //     
    GetObjParam( rTechnicalDemand, &parTechnicalDemand, sizeof( parTechnicalDemand ), TECHNICAL_DEMAND_PAR );
    
    //    
    int count = GetArrayCount( parTechnicalDemand.pGab );
    
    char buf[255];
    sprintf( buf, "style = %d,   = %d", parTechnicalDemand.style, count );
    Message( buf );
    
    // C   
    reference rArTextLine = CreateArray( TEXT_LINE_ARR, 0 ) ;
    
    TextLineParam parTextLine; //    
    TextItemParam parTextItem; //     
    
    //        
    for ( int i = 0; i < count; i++ ) 
    {
      GetObjParam( rTechnicalDemand, &rArTextLine, sizeof( rArTextLine ), i );
 
      //    
      int count = GetArrayCount( rArTextLine );
     
      //   
      for ( int j = 0; j < count; j++ )
      {
        //    
        GetArrayItem( rArTextLine, j, &parTextLine, sizeof( parTextLine ) );
            
        sprintf( buf, "j = %d style = %d", j, parTextLine.style );
        Message( buf );

        //    
        int count = GetArrayCount( parTextLine.pTextItem );
          
        //    
        for ( int k = 0; k < count; k++ ) 
        {
          //    
          GetArrayItem( parTextLine.pTextItem, k, &parTextItem, sizeof( parTextItem ) );
            
          //    ( SPECIAL_SYMBOL, FONT_SYMBOL, FRACTION_TYPE, SUM_TYPE  0 )
          if ( !parTextItem.tip )
            sprintf( buf, "k = %d, font.height = %4.2f,\ns = %s,\nfont.fontName = %s,\nfont.bitVector = %d",
                     k, parTextItem.font.height, parTextItem.s, parTextItem.font.fontName, parTextItem.font.bitVector );
          else
            sprintf( buf, "k = %d, tip = %d, iSNumb = %d", k, parTextItem.tip, parTextItem.iSNumb );
          Message( buf );
        }  
      }
    }  
    //    
    DeleteArray( rArTextLine );
  }
  else
    Error( "  " );
}


//-------------------------------------------------------------------------------
//   
// ---
void ChangeTechnicalDemand()
{
  //         
  //         
  reference rTechnicalDemand = GetReferenceDocumentPart( 1 ); //      
                                                              // ( 0 -  , 1 -   ,
                                                              // 2 -   , 3 -   ,
                                                              // 4 -     )
  if ( rTechnicalDemand ) 
  {
    TechnicalDemandParam parTechnicalDemand; //    

    //     
    GetObjParam( rTechnicalDemand, &parTechnicalDemand, sizeof( parTechnicalDemand ), TECHNICAL_DEMAND_PAR );

    char buf[255];
    sprintf( buf, "  = %d", parTechnicalDemand.strCount );
    Message( buf );
      
    //     ,     ,   
    OpenTechnicalDemand( parTechnicalDemand.pGab,    //     ,  0 
                                                     //      
                         parTechnicalDemand.style ); //     ,  0  
      
    TextLineParam parTextLine; //    
    TextItemParam parTextItem; //     
    
    //        
    for ( int i = 0; i < parTechnicalDemand.strCount; i++ ) 
    {
      GetObjParam( rTechnicalDemand, &parTextLine, sizeof( parTextLine ), TT_FIRST_STR + i );
 
      //    
      int count = GetArrayCount( parTextLine.pTextItem );
          
      //    
      for ( int j = 0; j < count; j++ ) 
      {
        //    
        GetArrayItem( parTextLine.pTextItem, j, &parTextItem, sizeof( parTextItem ) );
        
        //    - ,     "!!!"
        if ( strlen( parTextItem.s ) ) 
        {
          strcat( parTextItem.s, "!!!" );
          SetArrayItem( parTextLine.pTextItem, j, &parTextItem, sizeof( parTextItem ) );
        }
      }
      SetObjParam( rTechnicalDemand, &parTextLine, sizeof( parTextLine ), TT_FIRST_STR + i );  
    }      
    CloseTechnicalDemand(); //   
  }
  else
    Error( "  " );
}


//-------------------------------------------------------------------------------
//           
// ---
void ShowInsertFragment()
{
  char libName[255];
  //   
  if ( ChoiceFile( "*.lfr", //    
                   //   (   ,    ) 
                   " ( *.lfr )|*.lfr|  ( *.* )|*.*|", 
                   libName, //  
                   255 ) )  //   
  {
    char buf[500];
    //     
    while ( ksChoiceFragmentFromLib( libName, buf, 500 ) ) 
    {
      //   
      char * insertName = strrchr( buf, '|' );
      if ( insertName )
      {
        //       Placement
        
        Phantom phantom; //   
  
        phantom.phType      = 1; //   ( type1, type2, ... type7 )
        phantom.type1.xBase = 0; //    
        phantom.type1.yBase = 0;
        phantom.type1.scale = 1; // 
        phantom.type1.gr    = 0; //   
        phantom.type1.ang   = 0; //    

        //     
        reference rDefinitionFragment = FragmentDefinition( buf,          //   
                                                            insertName+1, //  
                                                            1 );          //   ( 0 - 
                                                                          //  , 1 -   )

        //      ,    
        PlacementParam parPlacement; //  
  
        parPlacement.xBase = 0; //       
        parPlacement.yBase = 0;
        parPlacement.ang   = 0; //      
        parPlacement.scale = 1; // 
        
        //   , type -   ( 0 -  , 1 -  )
        phantom.type1.gr = NewGroup( 1 );
          //    
          reference rInsertFragment = InsertFragment( rDefinitionFragment, //   
                                                      0,                   //     ( 0 -    1 -    )
                                                      &parPlacement );     //  
        EndGroup(); //    
            
        //  
        double x = 0, y = 0;

        int comm = 1; //  
        while ( comm )
        {
          phantom.type1.ang = 0;
          
          //  ,   
          comm = Placement( NULL,               //       	
                            &x, &y,             //   	
                            &phantom.type1.ang, //  
                            &phantom );         //     ,  	  
          
          if ( comm != 0 ) //   
            CopyObj ( rInsertFragment,        //    
                      0, 0,                   //   
                      x, y,                   //   
                      1, phantom.type1.ang ); //      
        }
        //   
        ClearGroup( phantom.type1.gr );
      }
      else
        Error( "   " );
    }
  }
}


//-------------------------------------------------------------------------------
//     
// ---
void  EditFragmentLibrary()
{
  char libName[255];
  
  //   
  if ( ChoiceFile( "*.lfr", //    
                   //   (   ,    )   
                   " ( *.lfr )|*.lfr|  ( *.* )|*.*|", 
                   libName, //  
                   255 ) )  //   
  {    
    //     
    RequestInfo info;
    memset( &info, 0, sizeof( info ) );
    
    //       
    info.commands = "!_ !_ !_";
  
    int comm = 1; //  
    while ( comm != -1 )
    {
      string strNameFragment;
      char buf[255];
    
      //     
      comm = CommandWindow( &info );
      switch ( comm )
      {
        case 1:  //  
          strcpy( buf, "" );
          if ( ReadString( "   ", //  
                            buf,                           //  
                            255 ) )                        //   
          {  
            strNameFragment = libName;
            if ( buf[0] != '|' )
              strNameFragment += "|";
            strNameFragment += buf;
            comm = 2;
          }
          else
            comm = 0;            
          break;
        case 2 : //  
        case 3 : //  
          //   
          if ( ksChoiceFragmentFromLib( libName, buf, 255 ) ) 
            strNameFragment = buf;
          else
            comm = 0;  
          break;
      }

      if ( comm > 0 )
      {
        //   
        if ( ksFragmentLibrary( ( char* )strNameFragment.c_str(), //    
                                comm ) )                          //     ( -1 -   ,
                                                                  // 0 -   , 1 - , 2 - 
                                                                  // , 3 -    , 4 - 
                                                                  //  , 5 -    .
        {
          if ( comm == 2 ) 
          {
            ksFragmentLibrary( ( char * )strNameFragment.c_str(), 4 ); //   
            //    
            Text( 0, 100, //   
                  0,      //   
                  5,      //  
                  1,      //  
                  0,      //  ,   /
                  "   " ); //  

            LineSeg ( 0, 0, 100, 100, 1 );

            //      -     ""
            //  "  "   
            SystemControlStart( "  " );
            ksFragmentLibrary( ( char * )strNameFragment.c_str(), 0 ); //  c 
          }
        }
        else
          MessageBoxResult(); //       
      }
    } 
  }
}


//-------------------------------------------------------------------------------
//    
// ---
void ShowInsertFragment1()
{
  char frwName[255];
  
  //   
  while(  ksChoiceFile( "*.frw", //    
                        //   (   ,    ) 
                        "( *.frw )|*.frw|  ( *.* )|*.*|",
                        frwName, //  
                        255,     //   
                        1 ) )    //    ( - 1 -  
                                 // , 0 -      )
  {
    //       Placement
        
    Phantom phantom; //   
  
    phantom.phType      = 1; //   ( type1, type2, ... type7 )
    phantom.type1.xBase = 0; //    
    phantom.type1.yBase = 0;
    phantom.type1.scale = 1; // 
    phantom.type1.gr    = 0; //   
    phantom.type1.ang   = 0; //    
    
    //      ,    
    PlacementParam parPlacement; //  
  
    parPlacement.xBase = 0; //       
    parPlacement.yBase = 0;
    parPlacement.ang   = 0; //      
    parPlacement.scale = 1; // 
    
    int comm = 1; //  
    while ( comm )
    {
      //     ,    ,    
      //    ,  , ,    
      phantom.type1.gr = ksReadFragmentToGroup( frwName,         //  
                                                0,               //     ( 0 -   , 1 -    )
                                                &parPlacement ); //  
     
      //     
      double x =0, y = 0; 
  
      //  ,   
      if ( phantom.type1.gr && ( comm = Placement( NULL,               //       	
                                                   &x, &y,             //   	
                                                   &phantom.type1.ang, //  
                                                   &phantom ) ) != 0 ) //     ,  	  
      {
        // C 
        MoveObj( phantom.type1.gr, x, y );
        //  
        if ( fabs( phantom.type1.ang ) > 0.001 )
          RotateObj( phantom.type1.gr, x, y, phantom.type1.ang );
      
        //     
        StoreTmpGroup( phantom.type1.gr );
        //   
        ClearGroup( phantom.type1.gr );
      }
      else 
      {
        //    
        if ( phantom.type1.gr )
          ClearGroup( phantom.type1.gr );
        comm = 0;
      }
    }
  }
}