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

#include <vcl.h>
#pragma hdrstop

#include <windows.h>
#include <stdio.h>
#include <math.h>

#include <ldefin2d.h>
#include <ksConstants.h>

#ifdef __LIGHT_VERSION__
#include <Kl_TLB.h>
#else
#include <Ks_TLB.h>
#endif

//---------------------------------------------------------------------------
//   Important note about DLL memory management when your DLL uses the
//   static version of the RunTime Library:
//
//   If your DLL exports any functions that pass String objects (or structs/
//   classes containing nested Strings) as parameter or function results,
//   you will need to add the library MEMMGR.LIB to both the DLL project and
//   any other projects that use the DLL.  You will also need to use MEMMGR.LIB
//   if any other projects which use the DLL will be performing new or delete
//   operations on any non-TObject-derived classes which are exported from the
//   DLL. Adding MEMMGR.LIB to your project will change the DLL and its calling
//   EXE's to use the BORLNDMM.DLL as their memory manager.  In these cases,
//   the file BORLNDMM.DLL should be deployed along with your DLL.
//
//   To avoid using BORLNDMM.DLL, pass string information using "char *" or
//   ShortString parameters.
//
//   If your DLL uses the dynamic version of the RTL, you do not need to
//   explicitly add MEMMGR.LIB as this will be done implicitly for you
//---------------------------------------------------------------------------

#pragma argsused
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
  return 1;
}
//------------------------------------------------------------------------------
//
// ---
extern "C" IDispatch* far __export WINAPI CreateKompasObject();

KompasObject* kompas = NULL;

extern "C" unsigned int far __export __pascal LibraryId(){
	return 100;
}

void DrawTransform( ksDocument2D * doc );
void DrawCopy( ksDocument2D * doc );
void DrawSymmetry( ksDocument2D * doc );
void EditTolerance( ksDocument2D * doc );
void EditTable( ksDocument2D * doc );
void EditStamp( ksDocument2D * doc );
void GetTextTT( ksDocument2D * doc );
void ShowInsertFragment( ksDocument2D * doc );
void ShowInsertFragment1( ksDocument2D * doc );
void EditFragmentLibrary( ksDocument2D * doc );
void ChangeTechnicalDemand( ksDocument2D * doc );

//------------------------------------------------------------------------------
//
// ---
extern "C" void far __export __pascal  LibraryEntry( UINT Comm ) {
  kompas = (KompasObject*)::CreateKompasObject();
  if ( kompas ) {
    ksDocument2D* doc = (ksDocument2D*)kompas->ActiveDocument2D();
    if ( doc && doc->get_reference() ) {
      switch ( Comm ) {
        case 1  : ::DrawTransform( doc );         break; //   
        case 2  : ::DrawCopy( doc );              break; // 
        case 3  : ::DrawSymmetry( doc );          break; // 
        case 4  : ::EditTolerance( doc );         break; //  
        case 5  : ::EditTable( doc );             break; // 
        case 6  : ::EditStamp( doc );             break; //     
        case 7  : ::GetTextTT( doc );             break; //  
        case 8 : ::ChangeTechnicalDemand( doc ); break; // TT
        case 9 : ::ShowInsertFragment( doc );    break; // 
        case 10 : ::EditFragmentLibrary( doc );   break; //   
        case 11 : ::ShowInsertFragment1( doc );   break; //  
      }
    }
    else
      kompas->ksError( StringToOleStr("    \n  /") );
    kompas->Release();
  }
}

//------------------------------------------------------------------------------
//    
// ---
void DrawTransform( ksDocument2D * doc ) {
  //   
	doc->ksMtr( -30, -30, 0, 1, 1 );
  reference ref = doc->ksNewGroup(0);
  doc->ksLineSeg( 30, 30, 60, 30, 1);
  doc->ksLineSeg( 60, 30, 60, 60, 1);
  doc->ksLineSeg( 60, 60, 30, 60, 1);
  doc->ksLineSeg( 30, 60, 30, 30, 1);
  doc->ksHatch( 0, 45, 2, 0, 0, 0 );
    doc->ksLineSeg( 30, 30, 60, 30, 1);
    doc->ksLineSeg( 60, 30, 60, 60, 1);
    doc->ksLineSeg( 60, 60, 30, 60, 1);
    doc->ksLineSeg( 30, 60, 30, 30, 1);
  doc->ksEndObj();
  doc->ksEndGroup();
  doc->ksDeleteMtr();

	kompas->ksMessage(StringToOleStr("C  20, 20, 45, 2"));

  doc->ksMtr(20, 20, 45, 2, 2 );
  doc->ksTransformObj( ref );
  doc->ksDeleteMtr();

  kompas->ksMessageBoxResult();
  kompas->ksMessage(StringToOleStr(" "));

  doc->ksMtr(-20, -20, 0, 1, 1 );
  doc->ksTransformObj( ref );
  doc->ksDeleteMtr();

  doc->ksMtr(0, 0, 0, 0.5, 0.5 );
  doc->ksTransformObj( ref );
  doc->ksDeleteMtr();

  doc->ksMtr(0, 0, -45, 1, 1 );
  doc->ksTransformObj( ref );
  doc->ksDeleteMtr();

  kompas->ksMessageBoxResult();
}

//------------------------------------------------------------------------------
//  
// ---
void DrawCopy( ksDocument2D * doc ) {
  ksViewParam * par = (ksViewParam*)kompas->GetParamStruct( ko_ViewParam );
  if ( par ) {
	  par->Init();
    par->set_x(20);
	  par->set_y(60);
	  par->set_scale_( 1 );
		par->set_color( RGB(10,20,10) );
		par->set_state( stACTIVE );
		par->set_name( StringToOleStr("user view") );

    long number = 5;
    // 
    reference v = doc->ksCreateSheetView( par, &number);
    // 
    doc->ksLayer( 5 );

		doc->ksLineSeg( 20, 10, 20, 30, 1 );
    doc->ksLineSeg( 20, 30, 40, 30, 1 );
    doc->ksLineSeg( 40, 30, 40, 10, 1 );
    doc->ksLineSeg( 40, 10, 20, 10, 1 );

    //  (        )
    doc->ksCopyObj( v, 20, 60, 40, 80, 1, 0  );
	}
}

//------------------------------------------------------------------------------
//  
// ---
void DrawSymmetry( ksDocument2D * doc ) {
  reference grp = doc->ksNewGroup( 0 );
    doc->ksLineSeg( 20, 10, 20, 30, 1 );
    doc->ksLineSeg( 20, 30, 40, 30, 1 );
    doc->ksLineSeg( 40, 30, 40, 10, 1 );
    doc->ksLineSeg( 40, 10, 20, 10, 1 );
  doc->ksEndGroup();

  doc->ksSymmetryObj(grp, 40, 10, 40, 20, StringToOleStr("1") );
}

//------------------------------------------------------------------------------
//   
// ---
void EditTolerance( ksDocument2D * doc ) {
  //   
  reference pObj;

 	ksRequestInfo * info = (ksRequestInfo*)kompas->GetParamStruct(ko_RequestInfo);
	if ( !info )
		return;
  double x, y;
  info->set_prompt( StringToOleStr("  ") );
  int j = doc->ksCursor( info, &x ,&y, 0 );
  if ( j ) {
    if( doc->ksExistObj( pObj = doc->ksFindObj( x, y, 1000000. )) ){
      //  
      int type = doc->ksGetObjParam( pObj,0,0);   //   
      if ( type == TOLERANCE_OBJ ) {
        long numb;
        char buf[128];
        //    
        doc->ksOpenTolerance( pObj );

        ksToleranceParam * par = (ksToleranceParam*)kompas->GetParamStruct(ko_ToleranceParam);
        //  
        doc->ksGetObjParam(  pObj,   //   
                    par,  //   
                    ALLPARAM ); //  

        ::sprintf( buf, " =%d =%d -%s\nx=%5.1f y=%5.1f  ",
                          par->get_tBase(), par->get_style(), par->get_type() ? "":"", par->get_x(), par->get_y() );
        kompas->ksMessage(StringToOleStr(buf));

        ksTextLineParam * par1 = (ksTextLineParam*)kompas->GetParamStruct(ko_TextLineParam);
				if ( !par1 )
					return;
				par1->Init();
        //      
        while ( doc->ksGetToleranceColumnText( &numb, par1 )!=0 ) {
          ::sprintf( buf, "numb =%d\nstyle=%d", numb, par1->get_style() );
          kompas->ksMessage(StringToOleStr(buf));

  				ksDynamicArray * arrpTextItem = (ksDynamicArray*)par1->GetTextItemArr();
					ksTextItemParam * item = (ksTextItemParam *)kompas->GetParamStruct(ko_TextItemParam);

					if ( !item || !arrpTextItem )
  					return;
					item->Init();

					for ( int j=0, count = arrpTextItem->ksGetArrayCount(); j < count; j++) {
            arrpTextItem->ksGetArrayItem( j, item );
  					ksTextItemFont * textItemFont = (ksTextItemFont*)item->GetItemFont();
            if ( !item->get_type() )
              ::sprintf( buf, "=%d h=%5.1f\ns=%s\n fontName=%s\n =%d ",j, textItemFont->get_height(),
    						         WideCharToString(item->get_s()), textItemFont->get_fontName(), textItemFont->get_bitVector() );
            else
              ::sprintf( buf, "=%d  = %d  =%d ", j, item->get_type(), item->get_iSNumb() );
						item->set_s(StringToOleStr(" "));
            kompas->ksMessage(StringToOleStr(buf));
          }
					arrpTextItem->ksClearArray();
					arrpTextItem->ksAddArrayItem( -1, item );
					doc->ksSetToleranceColumnText( numb, par1 );
          arrpTextItem->ksDeleteArray();  //  
        }
        // 
				par->set_x( par->get_x()+10 );
				par->set_y( par->get_y()+10 );
        doc->ksSetObjParam(  pObj,   //   
                      par,  //   
                      ALLPARAM ); //  
        doc->ksEndObj();//  " "
      }
      else
        kompas->ksError( StringToOleStr("   "));
    }
    else
      kompas->ksError( StringToOleStr(" "));
  }
}

//------------------------------------------------------------------------------
//  
// ---
void EditTable( ksDocument2D * doc ) {
  //  
  reference pObj;

 	ksRequestInfo * info = (ksRequestInfo*)kompas->GetParamStruct(ko_RequestInfo);
	if ( !info )
		return;
  double x, y;
  info->set_prompt( StringToOleStr(" ") );
  //    
  int j = doc->ksCursor( info, &x ,&y, 0 );
  if ( j ) {
    if( doc->ksExistObj( pObj = doc->ksFindObj( x, y, 100000 )) ){
      //  
      int type = doc->ksGetObjParam( pObj, 0, 0 );   //   
      //    - 
      if ( type == TABLE_OBJ ) {
        long numb;
        //reference p;
        char buf[128];
        //   
        doc->ksOpenTable( pObj );

        ksTextParam * par = (ksTextParam*)kompas->GetParamStruct(ko_TextParam);
				if ( !par )
					return;
				par->Init();
        //      
        while (( doc->ksGetTableColumnText( &numb, par ))!=0) {
          ::sprintf( buf, "numb =%d   ", numb );
          kompas->ksMessage(StringToOleStr(buf));

  				ksDynamicArray * arrpLineText = (ksDynamicArray*)par->GetTextLineArr();
					ksTextLineParam * itemLineText = (ksTextLineParam*)kompas->GetParamStruct(ko_TextLineParam);
		  		if ( !itemLineText )
			  		return;
					itemLineText->Init();

          for(int i = 0, count = arrpLineText->ksGetArrayCount(); i < count; i++ ) {
						arrpLineText->ksGetArrayItem( i, itemLineText );
            ::sprintf( buf, "i =%d style=%d   ", i, itemLineText->get_style() );
            kompas->ksMessage(StringToOleStr(buf));

    				ksDynamicArray * arrpTextItem = (ksDynamicArray*)itemLineText->GetTextItemArr();
	  				ksTextItemParam * item = (ksTextItemParam*)kompas->GetParamStruct(ko_TextItemParam);

			  		if ( !item || !arrpTextItem )
  			  		return;
		  			item->Init();

					  for ( int j=0, count2 = arrpTextItem->ksGetArrayCount(); j < count2; j++) {
              arrpTextItem->ksGetArrayItem( j, item );
    					ksTextItemFont * textItemFont = (ksTextItemFont*)item->GetItemFont();

							if ( !item->get_type() )
                ::sprintf( buf, "=%d h=%5.1f\ns=%s\n fontName=%s\n =%d ",j, textItemFont->get_height(),
								         WideCharToString(item->get_s()), textItemFont->get_fontName(), textItemFont->get_bitVector() );
							else
                ::sprintf( buf, "=%d  = %d  =%d ",j, item->get_type(), item->get_iSNumb() );
              kompas->ksMessage(StringToOleStr(buf));
            }
            arrpTextItem->ksDeleteArray();  //  
          }
          //   
          arrpLineText->ksDeleteArray();
        } // while ((ksGetTableColumnText( &numb, &par ))!=0) {

        //  2
        doc->ksColumnNumber( 2 );
        doc->ksText( 0, 0, 0, 5, 1 ,0,StringToOleStr(" ") );

        doc->ksDivideTableItem( 3, 1, 2 );
        doc->ksColumnNumber( 4 );
        doc->ksText( 0, 0, 0, 5, 1 ,0,StringToOleStr("4") );

        doc->ksEndObj();//  ""
      }
      else
        kompas->ksError(StringToOleStr("  "));
    }
    else
      kompas->ksError(StringToOleStr(" "));
  }
}

//------------------------------------------------------------------------------
//      
// ---
void EditStamp( ksDocument2D * doc ) {
	ksStamp * stamp = (ksStamp*)doc->GetStamp();
	if ( stamp && stamp->ksOpenStamp() ) {
    long numb;
    //      
		ksDynamicArray * arr = (ksDynamicArray*)stamp->ksGetStampColumnText( &numb );
		while ( numb && arr ) {
      char buf[128];
      ::sprintf( buf, "numb =%d   ", numb );
      kompas->ksMessage(StringToOleStr(buf));

 		  ksDynamicArray * arrpLineText = (ksDynamicArray*)kompas->GetDynamicArray(TEXT_LINE_ARR);
			ksTextLineParam * itemLineText = (ksTextLineParam*)kompas->GetParamStruct(ko_TextLineParam);
		  if ( !itemLineText )
			 	return;
			itemLineText->Init();

      for(int i = 0, count = arr->ksGetArrayCount(); i < count; i++ ) {
			  arr->ksGetArrayItem( i, itemLineText );
        ::sprintf( buf, "i =%d style=%d", i, itemLineText->get_style() );
        kompas->ksMessage(StringToOleStr(buf));

				ksDynamicArray * arrpTextItem = (ksDynamicArray*)itemLineText->GetTextItemArr();
	  		ksTextItemParam * item = (ksTextItemParam*)kompas->GetParamStruct(ko_TextItemParam);

				if ( !item || !arrpTextItem )
  			  return;
		  	item->Init();

				for ( int j=0, count2 = arrpTextItem->ksGetArrayCount(); j < count2; j++) {
          arrpTextItem->ksGetArrayItem( j, item );
					ksTextItemFont * textItemFont = (ksTextItemFont*)item->GetItemFont();
				  ::sprintf( buf, "=%d h=%5.1f\ns=%s\nfontName=%s ",j, textItemFont->get_height(), WideCharToString(item->get_s()),
						         textItemFont->get_fontName() );
          kompas->ksMessage(StringToOleStr(buf));
        }
        arrpTextItem->ksDeleteArray();  //  
      }
      //   
      arrpLineText->ksDeleteArray();

		  arr->ksDeleteArray();
			arr = (ksDynamicArray*)stamp->ksGetStampColumnText( &numb );
    }
    //   2
    doc->ksColumnNumber( 2 );
		ksTextItemParam * item = (ksTextItemParam*)kompas->GetParamStruct(ko_TextItemParam);
    if ( !item ) {
      stamp->ksCloseStamp();
      return;
    }
		item->Init();
		ksTextItemFont * itemFont = (ksTextItemFont*)item->GetItemFont();
		if ( itemFont ) {
		 	itemFont->SetBitVectorValue( NEW_LINE, true );
			item->set_s( StringToOleStr(" 2") );
			doc->ksTextLine( item );
		}
    stamp->ksCloseStamp();
  }
  else
    kompas->ksError (StringToOleStr("  "));
}

//------------------------------------------------------------------------------
//   
// ---
void GetTextTT( ksDocument2D * doc ) {
  //    
  reference pTT = doc->ksGetReferenceDocumentPart( 1 );
	ksTechnicalDemandParam * technicalDemandParam = (ksTechnicalDemandParam*)kompas->GetParamStruct(ko_TechnicalDemandParam);
  if ( pTT && technicalDemandParam ) {
  	technicalDemandParam->Init();
    //   
    doc->ksGetObjParam( pTT, technicalDemandParam, TECHNICAL_DEMAND_PAR );
    char buf[128];
	  ksDynamicArray * pGab = (ksDynamicArray*)technicalDemandParam->GetPGab();
    int count = pGab->ksGetArrayCount();
    ::sprintf( buf, " =%d    TT =%d", technicalDemandParam->get_style(), count );
    kompas->ksMessage(StringToOleStr(buf));

    //    
	  ksDynamicArray * pTextLine = (ksDynamicArray*)kompas->GetDynamicArray(TEXT_LINE_ARR);
    //      
    for( int i = 0; i < count; i++ ) {
      doc->ksGetObjParam( pTT, pTextLine, i );//ALLPARAM );
			ksTextLineParam * itemLineText = (ksTextLineParam*)kompas->GetParamStruct(ko_TextLineParam);
      if ( !itemLineText )
				return;
			itemLineText->Init();

      for( int i1=0, count1 = pTextLine->ksGetArrayCount(); i1 < count1; i1++ ) {
				pTextLine->ksGetArrayItem( i1, itemLineText );
        ::sprintf( buf, " =%d style=%d   ", i1, itemLineText->get_style() );
        kompas->ksMessage(StringToOleStr(buf));

				ksDynamicArray * arrpTextItem = (ksDynamicArray*)itemLineText->GetTextItemArr();
	  		ksTextItemParam * item = (ksTextItemParam*)kompas->GetParamStruct(ko_TextItemParam);

				if ( !item || !arrpTextItem )
  			  return;
		  	item->Init();

        for ( int j=0, count2 = arrpTextItem->ksGetArrayCount(); j< count2; j++) {
					arrpTextItem->ksGetArrayItem( j, item );
					ksTextItemFont * textItemFont = (ksTextItemFont*)item->GetItemFont();
          ::sprintf( buf, "=%d h=%5.1f\ns=%s\n fontName=%s ", j, textItemFont->get_height(),
						         WideCharToString(item->get_s()), textItemFont->get_fontName() );
          kompas->ksMessage(StringToOleStr(buf));
        }
        arrpTextItem->ksDeleteArray(); //  
      }
    }
    pTextLine->ksDeleteArray(); //   
  }
}

//------------------------------------------------------------------------------
//  
// ---
void ShowInsertFragment( ksDocument2D * doc ) {
  char libName[250];
	long res;
  //   
	::sprintf( libName, "%s", WideCharToString(kompas->ksChoiceFile( StringToOleStr("*.lfr"),StringToOleStr(" (*.lfr)|*.lfr|  (*.*)|*.*|"), 1 )) );
  if( libName ) {
    char buf[250];
    do {
      //    
			ksFragment * fr = (ksFragment*)doc->GetFragment();
			if  ( !fr )
				return;
      ksFragmentLibrary * frLib = (ksFragmentLibrary *) kompas->GetFragmentLibrary();
      if ( !frLib )
        return ;
			::sprintf( buf, "%s", WideCharToString(frLib->ksChoiceFragmentFromLib( StringToOleStr(libName), &res )) );
      if ( buf && res ) {
        //  
        char * insertName = ::strrchr ( buf, '|' );
        if ( insertName ) {
          double x, y;
          //      Placement
					ksPhantom * rub = (ksPhantom*)kompas->GetParamStruct( ko_Phantom );
					ksType1 * type1 = (ksType1*)rub->GetPhantomParam();
					if ( !rub || !type1 )
						return;

					type1->Init(); rub->Init();
					type1->set_scale_(1);
					rub->phantom = 1;

          reference pDefFrg;
          //     
          pDefFrg = fr->ksFragmentDefinition( StringToOleStr(buf),         //  
                                        StringToOleStr(" "),      // 
                                        1 );            //  -   
                                                          // 0-   , 1- 

          if( pDefFrg ) {
           //     ,    
						type1->set_gr( doc->ksNewGroup(1) );

              ksPlacementParam * par = (ksPlacementParam*)kompas->GetParamStruct( ko_PlacementParam );
	            if ( !par )
		            return;
	            par->Init();
	            par->set_scale_(1);
              reference  p = fr->ksInsertFragment( pDefFrg,              //    
                            0,                    //    0 -    1-   
                            par );               // 

            doc->ksEndGroup();
            int j;
            do {
							type1->set_angle(0);
							double ang = type1->get_angle();
              if ( (j = doc->ksPlacement( NULL, &x, &y, &ang, rub ))!=0 ) {
                doc->ksCopyObj( p,                  //    
                          0,0,     //   
                          x, y,     //   
                          1, type1->get_angle()  );//      
              }
            } while ( j );
            doc->ksDeleteObj( type1->get_gr() );
          }
          else
            kompas->ksError (StringToOleStr("    "));
        }
        else
          kompas->ksError(StringToOleStr("   "));
      }
    } while( res );
  }
}

//------------------------------------------------------------------------------
//   
// ---
void ShowInsertFragment1( ksDocument2D * doc ) {
  char frwName[250];
	ksFragment * fr = (ksFragment*)doc->GetFragment();
	if ( !fr )
		return;
  //  
  ::sprintf( frwName, "%s", WideCharToString(kompas->ksChoiceFile( StringToOleStr("*.frw"), StringToOleStr("(*.frw)|*.frw|  (*.*)|*.*|"), 1 )) );
  if( frwName ) { 
    double x, y;
    //      Placement
		ksPhantom * rub = (ksPhantom*)kompas->GetParamStruct( ko_Phantom );
		ksType1 * type1 = (ksType1*)rub->GetPhantomParam();
		if ( !rub || !type1 )
			return;
		type1->Init(); rub->Init();

		type1->set_scale_(1);
		rub->phantom = 1;

		//     ,    
    ksPlacementParam * par = (ksPlacementParam*)kompas->GetParamStruct( ko_PlacementParam );
    if ( !par )
      return;
    par->Init();
    par->set_scale_(1);

    int j;
    do {
      //    ,    ,
      //       ,  , ,
      //   .       
      //.
      type1->set_gr( fr->ksReadFragmentToGroup( StringToOleStr(frwName),        //  
                                             0,          //    0 -    1-   
                                             par ) );     // 
      if ( type1->get_gr() ) {
  			double ang = type1->get_angle();
  			if ((j = doc->ksPlacement( NULL, &x, &y, &ang, rub ))!=0 ) {
          // 
          doc->ksMoveObj( type1->get_gr(), x, y );
          //   
          doc->ksStoreTmpGroup( type1->get_gr() );
          doc->ksClearGroup( type1->get_gr(), true );
          doc->ksDeleteObj( type1->get_gr() );
        }
      }
      else {
        if ( type1->get_gr() )
          doc->ksDeleteObj( type1->get_gr() );
        j = 0;
      }
    } while ( j );
  }
}

//------------------------------------------------------------------------------
//    
// ---
void EditFragmentLibrary( ksDocument2D * doc ) {
  char libName[250];
  char buf [250];
  //   
	::sprintf( libName, "%s", WideCharToString(kompas->ksChoiceFile( StringToOleStr("*.lfr"),StringToOleStr(" (*.lfr)|*.lfr|  (*.*)|*.*|"), 1 )) );
//	::strcpy( libName, kompas.ksChoiceFile( "*.lfr"," (*.lfr)|*.lfr|  (*.*)|*.*|", 1 ) );
  if( libName ) {
    ksRequestInfo * info = (ksRequestInfo*)kompas->GetParamStruct( ko_RequestInfo );
  	ksFragment * fr = (ksFragment*)doc->GetFragment();
		if ( !info || !fr )
			return;
		info->Init();

		info->set_commandsString( StringToOleStr("!_ !_ !_ ") );
    int j;
    int typeEdit;
//    CString /*string*/
    char * nameFrg;
    ksFragmentLibrary * frLib = (ksFragmentLibrary *) kompas->GetFragmentLibrary();
    if ( !frLib )
      return ;

    do {
      j = doc->ksCommandWindow( info );
      switch ( j ) {
        case 1:  //!_
					::sprintf( buf, "%s", WideCharToString(kompas->ksReadString( StringToOleStr("   "), StringToOleStr("") )) );
					if ( buf ) {
            nameFrg = libName;
            if ( buf[0] != '|' )
//              nameFrg += "|";
            ::strcat ( nameFrg , buf );
            typeEdit = 2; //  
          }
          else
            typeEdit = 0;
          break;
        case 2 : //_
        case 3 : {//_
          //   
					long res;

    			::sprintf( buf, "%s", WideCharToString(frLib->ksChoiceFragmentFromLib( StringToOleStr(libName), &res )) );
          if ( res && buf && (j == 2 || j == 3) ) {
            nameFrg = buf;
            typeEdit = j; // 2-   , 3- ;
          }
          else
            typeEdit = 0;
          break;
        }
      }

      if ( j > 0 && typeEdit ) {
        if ( frLib->ksFragmentLibraryOperation( StringToOleStr(nameFrg), typeEdit ) ) {
          if ( typeEdit == 2 ) {
             frLib->ksFragmentLibraryOperation( StringToOleStr(nameFrg), 4 /*  */ );
            //   
            doc->ksText( 0, 100,     //  
                  0,            //  
                  5,            // 
                  1,            // 
                  0,	          // ,   .-.
                  StringToOleStr("   ") );   // 

            doc->ksLineSeg ( 0, 100, 110, 100, 1 );
            //    
            //    ""  "  ",
            //  
            kompas->ksSystemControlStart( StringToOleStr("  ") );
            frLib->ksFragmentLibraryOperation( StringToOleStr(nameFrg), 0 /* c */ );
          }
        }
        else
          kompas->ksMessageBoxResult();
      }
    } while ( j != -1 );
  }
}

//------------------------------------------------------------------------------
//  TT
// ---
void ChangeTechnicalDemand( ksDocument2D * doc ) {
  long ref = doc->ksGetReferenceDocumentPart( 1 );
  ksTechnicalDemandParam * par = (ksTechnicalDemandParam*)kompas->GetParamStruct(ko_TechnicalDemandParam);
	if ( doc->ksGetObjParam( ref, par, TECHNICAL_DEMAND_PAR ) ) {
	  char buf[128];
    ::sprintf( buf, "  TT =%d", par->get_strCount());
    kompas->ksMessage(StringToOleStr(buf));

		doc->ksOpenTechnicalDemand( par->GetPGab(), par->get_style() );

		ksTextLineParam * parLine = (ksTextLineParam*)kompas->GetParamStruct(ko_TextLineParam);
		if ( parLine ) {
  		parLine->Init();
	    for( int i = 0; i < par->get_strCount(); i++ ) {
	      doc->ksGetObjParam( ref, parLine, TT_FIRST_STR+i );
		    ksTextItemParam * parItem = (ksTextItemParam*)kompas->GetParamStruct(ko_TextItemParam);
				ksDynamicArray * arr = (ksDynamicArray*)parLine->GetTextItemArr();
				if ( parItem && arr ) {
  				parItem->Init();
					for ( int j = 0, count1 = arr->ksGetArrayCount(); j < count1; j++ ) {
	  				arr->ksGetArrayItem( j, parItem );
			  		::sprintf( buf, "%s", WideCharToString(parItem->get_s()) );
				  	::strcat( buf, "!!!" );
						parItem->set_s( StringToOleStr(buf) );
						arr->ksSetArrayItem( j, parItem );
						kompas->ksMessage( parItem->get_s() );
					}
        }
  		  doc->ksSetObjParam( ref, parLine, TT_FIRST_STR+i );
			}
		}
		doc->ksCloseTechnicalDemand();
	}
}

