//////////////////////////////////////////////////////////////////////////////// 
// 
// 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
// 
//////////////////////////////////////////////////////////////////////////////// 
#include "stdafx.h"
#include <afxdllx.h> 
#include "resource.h"

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

#ifndef __LIBTOOL_H
#include <libtool.h>
#endif

#ifndef __LDEFIN2D_H
#include <ldefin2d.h>
#endif

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


//-------------------------------------------------------------------------------
//       DLL
// ---
static AFX_EXTENSION_MODULE StepDLL = { NULL, NULL };


//-------------------------------------------------------------------------------
//   
//    DLL
// ---
extern "C" int APIENTRY
DllMain( HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved )
{
  UNREFERENCED_PARAMETER( lpReserved );

  if ( dwReason == DLL_PROCESS_ATTACH )
  {
    TRACE0( "DLL Initializing!" );
 
    if ( !AfxInitExtensionModule( StepDLL, hInstance ) )
      return 0;

    new CDynLinkLibrary( StepDLL );
  }
  else if ( dwReason == DLL_PROCESS_DETACH )
  {
    TRACE0( "DLL Terminating!" );
    AfxTermExtensionModule( StepDLL );
  }
  return 1;
}


//-------------------------------------------------------------------------------
//   
// ---
unsigned int WINAPI 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();


//-------------------------------------------------------------------------------
//   
// ---
void WINAPI 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
    ErrorT( _T("   \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;
  */
 
  MessageT( _T("C  20, 20, 45, 2") );
  
  Mtr( 20, 20, 45, 2 );    //    ( 20   OX, 20   OY,   45 ,  2:1 )
  TransformObj( rObject ); //     
  DeleteMtr();             //    

  MessageT( _T(" ") );

  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()
{
  ViewParamT parView; //     
 
  parView.x     = 20;                  //   
  parView.y     = 60;
  parView.scale = 1;                   //  
  parView.ang   = 0;                   //   
  parView.color = RGB( 10, 20, 10 );   //     
  parView.state = stACTIVE;            //  
  _tcscpy( parView.name, _T("User view") ); //  
  //     5,  0.5,   45 .
  int number = 5;
  reference rView = CreateSheetViewT( &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()
{
  //    

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

  //      
  info.prompt = _T("  ");
  
  //     
  double x, y; 

  //     
  if ( CursorExT( &info, &x, &y, 0, NULL ) )
  {
    //       
    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 );             //   
                   
        TCHAR buf[255];
        _stprintf( buf, _T("tBase = %d, style = %d,  - %s,\nx = %4.2f, y = %4.2f"), 
                 parTolerance.tBase, parTolerance.style, parTolerance.type ? _T("") : _T(""), parTolerance.x, parTolerance.y );
        MessageT( buf );

        TextLineParam parTextLine; //    
        TextItemParamT parTextItem; //     
          
        //       
        unsigned int number; //  
        //         
        while ( ksGetToleranceColumnText( &number, &parTextLine ) ) 
        {
          _stprintf( buf, _T("number = %d, style = %d"), number, parTextLine.style );
          MessageT( 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 )
              _stprintf( buf, _T("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
              _stprintf( buf, _T("i = %d, tip = %d, iSNumb = %d"), i, parTextItem.tip, parTextItem.iSNumb );
            MessageT( 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 );
          TextLineT( NEW_LINE, 0, 0, _T(" ") );
        ColumnNumber( 4 );
          TextLineT( NEW_LINE, 0, 0, _T("4") );
  
        EndObj(); //     
      }
      else
        ErrorT( _T("   ") );
    }
    else
      ErrorT( _T(" ") );
  }
}


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

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

  //      
  info.prompt = _T("  ");
  
  //     
  double x, y; 

  //     
  if ( CursorExT( &info, &x, &y, 0, NULL ) )
  {
    //       
    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; //    
        TextItemParamT 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 ( _tcslen( parTextItem.s ) ) 
            {
              _tcscat( parTextItem.s, _T("!!!") );
              SetArrayItem( parTextLine.pTextItem, i, &parTextItem, sizeof( parTextItem ) );
            }
          }  
          //              
          ksSetToleranceColumnText( number, &parTextLine ); 
        }
        EndObj(); //     
      }
      else
        ErrorT( _T("   ") );
    }
    else
      ErrorT( _T(" ") );
  }
}


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

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

  //    
  info.prompt = _T(" ");
 
  //    
  double x, y;
  
  //     
  if ( CursorExT( &info, &x, &y, 0, NULL ) )
  {
    //       
    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; //    
        TextItemParamT parTextItem; //     

        //       
        unsigned int number; //  
        //                 
        while ( ksGetTableColumnText( &number, &parText ) ) 
        {
          TCHAR buf[255];
          _stprintf( buf, _T("number = %d"), number );
          MessageT( buf );
          
          //    
          int count = GetArrayCount( parText.pTextLine );
     
          //   
          for ( int i = 0; i < count; i++ )
          {
            //    
            GetArrayItem( parText.pTextLine, i, &parTextLine, sizeof( parTextLine ) );
            
            _stprintf( buf, _T("i = %d style = %d"), i, parTextLine.style );
            MessageT( 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 )
                _stprintf( buf, _T("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
                _stprintf( buf, _T("j = %d, tip = %d, iSNumb = %d"), j, parTextItem.tip, parTextItem.iSNumb );
              MessageT( 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 );
          TextT( 0, 0, 0, 5, 1 ,0, _T(" ") );
        ColumnNumber( 4 );
          TextT( 0, 0, 0, 5, 1 ,0, _T("4") );
  
        EndObj(); //     
      }
      else
        ErrorT( _T("  ") );
    }
    else
      ErrorT( _T(" ") );
  }
}


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

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

  //    
  info.prompt = _T(" ");
    
  //     
  double x, y;
 
  //     
  if ( CursorExT( &info, &x, &y, 0, NULL ) ) 
  {
    //       
    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; //    
        TextItemParamT 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 ( _tcslen( parTextItem.s ) ) 
              {
                _tcscat( parTextItem.s, _T("!!!") );
                SetArrayItem( parTextLine.pTextItem, j, &parTextItem, sizeof( parTextItem ) );
              }
            }  
            SetArrayItem( parText.pTextLine, i, &parTextLine, sizeof( parTextLine ) );
          }
          //             
          ksSetTableColumnText( number, &parText ); 
        }  
        EndObj(); //     
      }
      else
        ErrorT( _T("  ") );
    }
    else
      ErrorT( _T(" ") );
  }
}


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

    //       
    unsigned int number;   //  
    reference rColumnText; //      
    //           
    while ( rColumnText = GetStampColumnText( &number ) ) 
    {
      TCHAR buf[255];
      _stprintf( buf, _T("number = %d"), number );
      MessageT( buf );

      //    
      int count = GetArrayCount( rColumnText );
     
      //   
      for ( int i = 0; i < count; i++ )
      {
        //    
        GetArrayItem( rColumnText, i, &parTextLine, sizeof( parTextLine ) );
            
        _stprintf( buf, _T("i = %d style = %d"), i, parTextLine.style );
        MessageT( 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 )
            _stprintf( buf, _T("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
            _stprintf( buf, _T("j = %d, tip = %d, iSNumb = %d"), j, parTextItem.tip, parTextItem.iSNumb );
          MessageT( buf );
        }  
      }
    }     
    //    /   

    //  2- 
    ColumnNumber( 2 );
      TextLineT( NEW_LINE, 0, 0, _T(" 2") );

    ClearStamp( 2 ); //  ,  0   

    CloseStamp(); //   / 
  }
  else
    ErrorT( _T("  ") );
}


//-------------------------------------------------------------------------------
//    
// ---
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 );
    
    TCHAR buf[255];
    _stprintf( buf, _T("style = %d,   = %d"), parTechnicalDemand.style, count );
    MessageT( buf );
    
    // C   
    reference rArTextLine = CreateArray( TEXT_LINE_ARR, 0 ) ;
    
    TextLineParam parTextLine; //    
    TextItemParamT 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 ) );
            
        _stprintf( buf, _T("j = %d style = %d"), j, parTextLine.style );
        MessageT( 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 )
            _stprintf( buf, _T("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
            _stprintf( buf, _T("k = %d, tip = %d, iSNumb = %d"), k, parTextItem.tip, parTextItem.iSNumb );
          MessageT( buf );
        }  
      }
    }  
    //    
    DeleteArray( rArTextLine );
  }
  else
    ErrorT( _T("  ") );
}


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

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

    TCHAR buf[255];
    _stprintf( buf, _T("  = %d"), parTechnicalDemand.strCount );
    MessageT( buf );
      
    //     ,     ,   
    OpenTechnicalDemand( parTechnicalDemand.pGab,    //     ,  0 
                                                     //      
                         parTechnicalDemand.style ); //     ,  0  
      
    TextLineParam  parTextLine; //    
    TextItemParamT 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 ( _tcslen( parTextItem.s ) ) 
        {
          _tcscat( parTextItem.s, _T("!!!") );
          SetArrayItem( parTextLine.pTextItem, j, &parTextItem, sizeof( parTextItem ) );
        }
      }
      SetObjParam( rTechnicalDemand, &parTextLine, sizeof( parTextLine ), TT_FIRST_STR + i );  
    }      
    CloseTechnicalDemand(); //   
  }
  else
    ErrorT( _T("  ") );
}


//-------------------------------------------------------------------------------
//           
// ---
void ShowInsertFragment()
{
  TCHAR libName[255];
  //   
  if ( ksChoiceFileT( _T("*.lfr"), //    
                      //   (   ,    ) 
                      _T(" ( *.lfr )|*.lfr|  ( *.* )|*.*|"), 
                     libName, //  
                     255, 0 ) )  //   
  {
    TCHAR buf[500];
    //     
    while ( ksChoiceFragmentFromLibT( libName, buf, 500 ) ) 
    {
      //   
      TCHAR * insertName = _tcsrchr( buf, _T('|') );
      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 = FragmentDefinitionT( 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 = PlacementExT( NULL,               //       	
                               &x, &y,             //   	
                               &phantom.type1.ang, //  
                               &phantom, NULL );   //     ,  	  
          
          if ( comm != 0 ) //   
            CopyObj ( rInsertFragment,        //    
                      0, 0,                   //   
                      x, y,                   //   
                      1, phantom.type1.ang ); //      
        }
        //   
        ClearGroup( phantom.type1.gr );
      }
      else
        ErrorT( _T("   ") );
    }
  }
}


//-------------------------------------------------------------------------------
//     
// ---
void  EditFragmentLibrary()
{
  TCHAR libName[255];
  
  //   
  if ( ksChoiceFileT( _T("*.lfr"), //    
                      //   (   ,    )   
                      _T(" ( *.lfr )|*.lfr|  ( *.* )|*.*|"), 
                      libName, //  
                      255, 0 ) )  //   
  {    
    //     
    RequestInfoT info;
    memset( &info, 0, sizeof( info ) );
    
    //       
    info.commands = _T("!_ !_ !_");
  
    int comm = 1; //  
    while ( comm != -1 )
    {
      CString strNameFragment;
      TCHAR buf[255];
    
      //     
      comm = CommandWindowT( &info );
      switch ( comm ) 
      {
        case 1:  //  
          _tcscpy( buf, _T("") );
          if ( ReadStringT( _T("   "), //  
                            buf,                           //  
                            255 ) )                        //   
          {  
            strNameFragment = libName;
            if ( buf[0] != _T('|') )
              strNameFragment += _T("|");
            strNameFragment += buf;
            comm = 2;
          }
          else
            comm = 0;            
          break;
        case 2 : //  
        case 3 : //  
          //   
          if ( ksChoiceFragmentFromLibT( libName, buf, 255 ) ) 
            strNameFragment = buf;
          else
            comm = 0;  
          break;
      }

      if ( comm > 0 )
      {
        //   
        if ( ksFragmentLibraryT( (LPTSTR)(LPCTSTR)strNameFragment, //    
                                 comm ) )                          //     ( -1 -   ,                    
                                                                   // 0 -   , 1 - , 2 - 
                                                                   // , 3 -    , 4 - 
                                                                   //  , 5 -    .
        {
          if ( comm == 2 ) 
          {
            ksFragmentLibraryT( (LPTSTR)( LPCTSTR )strNameFragment, 4 ); //   
            //    
            TextT( 0, 100, //   
                   0,      //   
                   5,      //  
                   1,      //  
                   0,      //  ,   /
                  _T("   ") ); //  

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

            //      -     ""
            //  "  "   
            SystemControlStartT( _T("  ") );
            ksFragmentLibraryT( (LPTSTR)( LPCTSTR )strNameFragment, 0 ); //  c 
          }
        }
        else
          MessageBoxResult(); //       
      }
    } 
  }
}


//-------------------------------------------------------------------------------
//    
// ---
void ShowInsertFragment1()
{
  TCHAR frwName[255];
  
  //   
  while(  ksChoiceFileT( _T("*.frw"), //    
                         //   (   ,    ) 
                         _T("( *.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 = ksReadFragmentToGroupExT( frwName,            //  
                                                   0,                  //     ( 0 -   , 1 -    )
                                                   &parPlacement, 1 ); //  
     
      //     
      double x =0, y = 0; 
  
      //  ,   
      if ( phantom.type1.gr && ( comm = PlacementExT( NULL,               //       	
                                                      &x, &y,             //   	
                                                      &phantom.type1.ang, //  
                                                      &phantom, NULL ) ) != 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;
      }
    }
  }
}