// step5.cpp : Defines the initialization routines for the DLL.
//

#include "stdafx.h"
#include <math.h>
#include "step5.h"
#include <ldefin2d.h>

#ifdef __LIGHT_VERSION__
#include <klAPI5.h>
#else
#include <kAPI5.h>
#endif

#include <ksConstants.h>

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

//
//	Note!
//
//		If this DLL is dynamically linked against the MFC
//		DLLs, any functions exported from this DLL which
//		call into MFC must have the AFX_MANAGE_STATE macro
//		added at the very beginning of the function.
//
//		For example:
//
//		extern "C" BOOL PASCAL EXPORT ExportedFunction()
//		{
//			AFX_MANAGE_STATE(AfxGetStaticModuleState());
//			// normal function body here
//		}
//
//		It is very important that this macro appear in each
//		function, prior to any calls into MFC.  This means that
//		it must appear as the first statement within the 
//		function, even before any object variable declarations
//		as their constructors may generate calls into the MFC
//		DLL.
//
//		Please see MFC Technical Notes 33 and 58 for additional
//		details.
//

/////////////////////////////////////////////////////////////////////////////
// CStep5App

BEGIN_MESSAGE_MAP(CStep5App, CWinApp)
	//{{AFX_MSG_MAP(CStep5App)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CStep5App construction

CStep5App::CStep5App()
{
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CStep5App object

CStep5App theApp;

//-------------------------------------------------------------------------------
//
// ---
unsigned int WINAPI LIBRARYID(){
	return IDS_LIBEDIT;
}

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 );

KompasObject kompas( NULL );

//-------------------------------------------------------------------------------
//
// ---
void GetKompas() {
	if ( !kompas.m_lpDispatch ) {
		CString filename;
		if( ::GetModuleFileName(NULL, filename.GetBuffer(255), 255) ) {
			filename.ReleaseBuffer( 255 );
			CString libname;

      #ifdef __LIGHT_VERSION__
        libname.LoadString( IDS_STRING5 );  //klAPI5.dll
      #else
        libname.LoadString( IDS_STRING4 );  //kAPI5.dll
      #endif

			filename.Replace( filename.Right(filename.GetLength() - (filename.ReverseFind('\\') + 1)), 
												libname );

			HINSTANCE hAppAuto = LoadLibrary( filename ); //  kAPI5.dll
			if(  hAppAuto ) {
				typedef LPDISPATCH ( WINAPI *FCreateKompasObject )(); 
				FCreateKompasObject pCreateKompasObject = 
					(FCreateKompasObject)GetProcAddress( hAppAuto, "CreateKompasObject" );	
				if ( pCreateKompasObject ) 
					kompas = pCreateKompasObject();
				FreeLibrary( hAppAuto );
			}
		}
	}
}

//-------------------------------------------------------------------------------
//
// ---
void WINAPI LIBRARYENTRY( unsigned int comm ) {
	//      .exe ,     
	//   dll   define
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	GetKompas();
	if ( kompas.m_lpDispatch ) {
		ksDocument2D doc( kompas.ActiveDocument2D() );
		if ( doc.m_lpDispatch && doc.GetReference() ) {
			switch ( comm ) {
			case 1  : ::DrawTransform( doc );         break; //   
			case 2  : ::DrawCopy( doc );              break; // 
			case 3  : ::DrawSymmetry( doc );          break; // 
			case 4  : ::EditTolerance( doc );         break; //  
			case 6  : ::EditTable( doc );             break; // 
			case 8  : ::EditStamp( doc );             break; //     
			case 9  : ::GetTextTT( doc );             break; //  
			case 10 : ::ChangeTechnicalDemand( doc ); break; // TT
			case 11 : ::ShowInsertFragment( doc );    break; // 
			case 12 : ::EditFragmentLibrary( doc );   break; //   
			case 13 : ::ShowInsertFragment1( doc );   break; //  
			}
		}
		else
			kompas.ksError( "    \n  /" );
	}
}

//-------------------------------------------------------------------------------
//
// ---
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("  20, 20, 45, 2");

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

  kompas.ksMessageBoxResult();
  kompas.ksMessage(" ");

  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( kompas.GetParamStruct( ko_ViewParam ) );

  if ( par.m_lpDispatch ) {
	  par.Init();
    par.SetX(20);
	  par.SetY(60);
	  par.SetScale_( 1 );
		par.SetColor( RGB(10,20,10) );
		par.SetState( stACTIVE );
		par.SetName( "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, "1"  );
}

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

 	ksRequestInfo info( kompas.GetParamStruct(ko_RequestInfo) );
	if ( !info.m_lpDispatch ) 
		return;
  double x, y;
  info.SetPrompt( "  " );
  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( kompas.GetParamStruct(ko_ToleranceParam) );
        //  
        doc.ksGetObjParam(  pObj,   //   
                    par,  //   
                    ALLPARAM ); //  

        ::sprintf( buf, " =%d =%d -%s\nx=%5.1f y=%5.1f  ",
                          par.GetTBase(), par.GetStyle(), par.GetType() ? "":"", par.GetX(), par.GetY() );
        kompas.ksMessage(buf);

        ksTextLineParam par1( kompas.GetParamStruct(ko_TextLineParam) );

				if ( !par1.m_lpDispatch )
					return;
				
				par1.Init();
        
				//      
        while ( doc.ksGetToleranceColumnText( &numb, par1 )!=0 ) {
          ::sprintf( buf, "numb =%d\nstyle=%d", numb, par1.GetStyle() );
          kompas.ksMessage(buf);

  				ksDynamicArray arrpTextItem( par1.GetTextItemArr() );
					ksTextItemParam item( kompas.GetParamStruct(ko_TextItemParam) );

					if ( !item.m_lpDispatch || !arrpTextItem.m_lpDispatch )
  					return;
          
					item.Init();

					for ( int j=0, count = arrpTextItem.ksGetArrayCount(); j < count; j++) {
            arrpTextItem.ksGetArrayItem( j, item );
  					ksTextItemFont textItemFont( item.GetItemFont() );
            if ( !item.GetType() )
              ::sprintf( buf, "=%d h=%5.1f\ns=%s\n fontName=%s\n =%d ",j, textItemFont.GetHeight(), 
    						         item.GetS(), textItemFont.GetFontName(), textItemFont.GetBitVector() );
            else
              ::sprintf( buf, "=%d  = %d  =%d ", j, item.GetType(), item.GetISNumb() );
						item.SetS(" ");
            kompas.ksMessage(buf);
          }
					arrpTextItem.ksClearArray();
					arrpTextItem.ksAddArrayItem( -1, item );
					doc.ksSetToleranceColumnText( numb, par1 );
          arrpTextItem.ksDeleteArray();  //  
        }
        // 
				par.SetX( par.GetX()+10 );
				par.SetY( par.GetY()+10 );
        doc.ksSetObjParam(  pObj,   //   
                      par,  //   
                      ALLPARAM ); //  
        doc.ksEndObj();//  " "
      }
      else
        kompas.ksError( "   ");
    }
    else
      kompas.ksError( " ");
  }
}

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

 	ksRequestInfo info( kompas.GetParamStruct(ko_RequestInfo) );
	if ( !info.m_lpDispatch ) 
		return;
  double x, y;
  info.SetPrompt( " " );
  //    
  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( kompas.GetParamStruct(ko_TextParam) );
				if ( !par.m_lpDispatch )
					return;
				par.Init();
        //      
        while (( doc.ksGetTableColumnText( &numb, par ))!=0) {
          ::sprintf( buf, "numb =%d   ", numb );
          kompas.ksMessage(buf);


  				ksDynamicArray arrpLineText( par.GetTextLineArr() );
					ksTextLineParam itemLineText( kompas.GetParamStruct(ko_TextLineParam) );
		  		if ( !itemLineText.m_lpDispatch )
			  		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.GetStyle() );
            kompas.ksMessage(buf);

    				ksDynamicArray arrpTextItem( itemLineText.GetTextItemArr() );
	  				ksTextItemParam item( kompas.GetParamStruct(ko_TextItemParam) );
			  		if ( !item.m_lpDispatch || !arrpTextItem.m_lpDispatch )
  			  		return;
		  			item.Init();
          
					  for ( int j=0, count2 = arrpTextItem.ksGetArrayCount(); j < count2; j++) {
              arrpTextItem.ksGetArrayItem( j, item );
    					ksTextItemFont textItemFont( item.GetItemFont() );

							if ( !item.GetType() )
                ::sprintf( buf, "=%d h=%5.1f\ns=%s\n fontName=%s\n =%d ",j, textItemFont.GetHeight(), 
								         item.GetS(), textItemFont.GetFontName(), textItemFont.GetBitVector() );
							else
                ::sprintf( buf, "=%d  = %d  =%d ",j, item.GetType(), item.GetISNumb() );
              kompas.ksMessage(buf);
            }
            arrpTextItem.ksDeleteArray();  //  
          }
          //   
          arrpLineText.ksDeleteArray();
        } // while ((ksGetTableColumnText( &numb, &par ))!=0) {

        //  2
        doc.ksColumnNumber( 2 );
        doc.ksText( 0, 0, 0, 5, 1 ,0," " );

        doc.ksDivideTableItem( 3, 1, 2 );
        doc.ksColumnNumber( 4 );
        doc.ksText( 0, 0, 0, 5, 1 ,0,"4" );

        doc.ksEndObj();//  ""
      }
      else
        kompas.ksError( "  ");
    }
    else
      kompas.ksError( " ");
  }
}


//-------------------------------------------------------------------------------
//
// ---
void EditStamp( ksDocument2D& doc ) {

  ksStamp stamp( doc.GetStamp() );
	
  if ( stamp.m_lpDispatch && stamp.ksOpenStamp() ) {

      stamp.ksColumnNumber(1);

      ksTextLineParam TLP(kompas.GetParamStruct(ko_TextLineParam));
      TLP.Init();
 

      ksDynamicArray TIArr(TLP.GetTextItemArr());

      ksTextItemParam TItem(kompas.GetParamStruct(ko_TextItemParam));
      TItem.Init();
 

      ksTextItemFont fnt(TItem.GetItemFont());
      fnt.Init();
      fnt.SetBitVector(NEW_LINE);

 

//  

      TItem.SetType(0);
      TItem.SetS("1");
      TIArr.ksAddArrayItem(-1, TItem);
      stamp.ksTextLine(TLP);
      TIArr.ksClearArray();
 

//  

      TItem.SetS("2");
      TIArr.ksAddArrayItem(-1, TItem);
      stamp.ksTextLine(TLP);
      stamp.ksCloseStamp();
  }
}


//-------------------------------------------------------------------------------
//
// ---
//  K8 void EditStamp( ksDocument2D& doc ) {
//  K8 	ksStamp stamp( doc.GetStamp() );
//  K8 	if ( stamp.m_lpDispatch && stamp.ksOpenStamp() ) {
//  K8     long numb;
//  K8     //      
//  K8 		ksDynamicArray arr( stamp.ksGetStampColumnText( &numb ) );
//  K8 		while ( numb && arr.m_lpDispatch ) {
//  K8       char buf[128];
//  K8       ::sprintf( buf, "numb =%d   ", numb );
//  K8       kompas.ksMessage(buf);
//  K8 
//  K8  		  ksDynamicArray arrpLineText( kompas.GetDynamicArray(TEXT_LINE_ARR) );
//  K8 			ksTextLineParam itemLineText( kompas.GetParamStruct(ko_TextLineParam) );
//  K8 		  if ( !itemLineText.m_lpDispatch )
//  K8 			 	return;
//  K8 			itemLineText.Init();
//  K8 
//  K8       for(int i = 0, count = arr.ksGetArrayCount(); i < count; i++ ) {
//  K8 			  arr.ksGetArrayItem( i, itemLineText );
//  K8         ::sprintf( buf, "i =%d style=%d", i, itemLineText.GetStyle() );
//  K8         kompas.ksMessage(buf);
//  K8 
//  K8 				ksDynamicArray arrpTextItem( itemLineText.GetTextItemArr() );
//  K8 	  		ksTextItemParam item( kompas.GetParamStruct(ko_TextItemParam) );
//  K8 
//  K8 				if ( !item.m_lpDispatch || !arrpTextItem.m_lpDispatch )
//  K8   			  return;
//  K8 		  	item.Init();
//  K8           
//  K8 				for ( int j=0, count2 = arrpTextItem.ksGetArrayCount(); j < count2; j++) {
//  K8           arrpTextItem.ksGetArrayItem( j, item );
//  K8 					ksTextItemFont textItemFont( item.GetItemFont() );
//  K8 				  ::sprintf( buf, "=%d h=%5.1f\ns=%s\nfontName=%s ",j, textItemFont.GetHeight(), item.GetS(),
//  K8 						         textItemFont.GetFontName() );
//  K8           kompas.ksMessage(buf);
//  K8         }
//  K8         arrpTextItem.ksDeleteArray();  //  
//  K8       }
//  K8       //   
//  K8       arrpLineText.ksDeleteArray();
//  K8 
//  K8 		  arr.ksDeleteArray();
//  K8 			arr = stamp.ksGetStampColumnText( &numb );
//  K8     }
//  K8     //   2
//  K8     doc.ksColumnNumber( 2 );
//  K8 		ksTextItemParam item( kompas.GetParamStruct(ko_TextItemParam) );
//  K8 		if ( !item.m_lpDispatch ) {
//  K8 			stamp.ksCloseStamp();
//  K8 			return;
//  K8 		}
//  K8 		item.Init();
//  K8 		ksTextItemFont itemFont( item.GetItemFont() );
//  K8 		if ( item.m_lpDispatch && itemFont.m_lpDispatch ) {
//  K8 		 	itemFont.SetBitVectorValue( NEW_LINE, true );
//  K8 			item.SetS( CString(" 2") );
//  K8 			doc.ksTextLine( item );
//  K8 		}
//  K8     stamp.ksCloseStamp();
//  K8   }
//  K8   else
//  K8     kompas.ksError ( "  " );
//  K8 }
//  K8 
//-------------------------------------------------------------------------------
//
// ---
void GetTextTT( ksDocument2D& doc ) {
  //    
  reference pTT = doc.ksGetReferenceDocumentPart( 1 );
	ksTechnicalDemandParam technicalDemandParam( kompas.GetParamStruct(ko_TechnicalDemandParam) );
  if ( pTT && technicalDemandParam.m_lpDispatch ) {
  	technicalDemandParam.Init();
    //   
    doc.ksGetObjParam( pTT, technicalDemandParam, TECHNICAL_DEMAND_PAR );
    char buf[128];
	  ksDynamicArray pGab( technicalDemandParam.GetPGab() );//kompas.GetDynamicArray(TEXT_LINE_ARR) );
    int count = pGab.ksGetArrayCount();//GetArrayCount( par.pGab );
    ::sprintf( buf, " =%d    TT =%d", technicalDemandParam.GetStyle(), count );
    kompas.ksMessage(buf);

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

			//      TextLineParam par2;
      for( int i1=0, count1 = pTextLine.ksGetArrayCount(); i1 < count1; i1++ ) {
				pTextLine.ksGetArrayItem( i1, itemLineText );
        ::sprintf( buf, " =%d style=%d   ", i1, itemLineText.GetStyle() );
        kompas.ksMessage(buf);

				ksDynamicArray arrpTextItem( itemLineText.GetTextItemArr() );
	  		ksTextItemParam item( kompas.GetParamStruct(ko_TextItemParam) );

				if ( !item.m_lpDispatch || !arrpTextItem.m_lpDispatch )
  			  return;
		  	item.Init();

        for ( int j=0, count2 = arrpTextItem.ksGetArrayCount(); j< count2; j++) {
					arrpTextItem.ksGetArrayItem( j, item );
					ksTextItemFont textItemFont( item.GetItemFont() );
          ::sprintf( buf, "=%d h=%5.1f\ns=%s\n fontName=%s ", j, textItemFont.GetHeight(),
						         item.GetS(), textItemFont.GetFontName() );
          kompas.ksMessage(buf);
        }
        arrpTextItem.ksDeleteArray(); //  
      }
    }
    pTextLine.ksDeleteArray(); //   
  }
}

//-------------------------------------------------------------------------------
//
// ---
void ShowInsertFragment( ksDocument2D& doc ) {
  char libName[250];
	libName[0] = '\0';
	long res;
  //   
	::strcpy( libName, kompas.ksChoiceFile( "*.lfr"," (*.lfr)|*.lfr|  (*.*)|*.*|", 1 ) );
  if ( ::strlen(libName) ) { 
    char buf[250]; 
    do {
      //    
			ksFragment fr( doc.GetFragment() );
// ABB K6    ksFragmentLibrary
      ksFragmentLibrary frLib( kompas.GetFragmentLibrary() ); 
			if  ( !fr.m_lpDispatch && !frLib.m_lpDispatch)
				return;
			::strcpy( buf, frLib.ksChoiceFragmentFromLib( libName, &res ) );
      if ( buf && res == 3 ) { // res = 3 -  
        //    
        char * insertName = ::strrchr ( buf, '|' );
        if ( insertName ) {
          double x, y;
          //      Placement
					ksPhantom rub( kompas.GetParamStruct( ko_Phantom ) );
					rub.SetPhantom(1);
					ksType1 type1( rub.GetPhantomParam() );
					if ( !rub.m_lpDispatch || !type1.m_lpDispatch )
						return;

					type1.Init(); rub.Init(); 

					type1.SetScale_(1);
					rub.SetPhantom(1);

          reference pDefFrg;
          //     
          pDefFrg = fr.ksFragmentDefinition( buf,         //  
                                        insertName+1,      // 
                                        1 );            //  -   
                                                          // 0-   , 1- 

          if( pDefFrg ) {
            //     ,     
						type1.SetGr( doc.ksNewGroup(1) );
   
            ksPlacementParam par( kompas.GetParamStruct( ko_PlacementParam ) );
	          if ( !par.m_lpDispatch )
		          return; 
	          par.Init();
	          par.SetScale_(1);
            reference  p = fr.ksInsertFragment( pDefFrg,              //    
                          0,                    //    0 -    1-   
                          par );               // 

            doc.ksEndGroup();
            int j;
            do {
							type1.SetAngle(0);
							double ang = type1.GetAngle();
	               if ( (j = doc.ksPlacement( NULL, &x, &y, &ang, rub ))!=0 ) {
  							type1.SetAngle(ang);
                doc.ksCopyObj( p,                  //    
                          0,0,     //   
                          x, y,     //   
                          1, type1.GetAngle()  );//      
              }
            } while ( j ); 
            doc.ksDeleteObj( type1.GetGr() );
          }
          else
            kompas.ksError ("    ");
        }
        else
          kompas.ksError( "   " );
      }
    } while( res );
  }
}

//-------------------------------------------------------------------------------
//
// ---
void ShowInsertFragment1( ksDocument2D& doc ) {
  char frwName[250];
	ksFragment fr( doc.GetFragment() );
	if ( !fr.m_lpDispatch )
		return;
  //  
  ::strcpy( frwName, kompas.ksChoiceFile( "*.frw","(*.frw)|*.frw|  (*.*)|*.*|", 1 ) );
  if( frwName ) { 
    double x, y;
    //      Placement
		ksPhantom rub( kompas.GetParamStruct( ko_Phantom ) );
		rub.SetPhantom(1);
		ksType1 type1( rub.GetPhantomParam() );
		if ( !rub.m_lpDispatch || !type1.m_lpDispatch )
			return;
		type1.Init(); rub.Init(); 

		type1.SetScale_(1);
		rub.SetPhantom(1);

		//     ,    
    ksPlacementParam par( kompas.GetParamStruct( ko_PlacementParam ) );
    if ( !par.m_lpDispatch )
      return; 
    par.Init();
    par.SetScale_(1);

    int j;
    do {
      //    ,    ,
      //       ,  , ,
      //   .       
      //.
      type1.SetGr( fr.ksReadFragmentToGroup( frwName,        //  
                                             0,          //    0 -    1-   
                                             par ) );     // 
      if ( type1.GetGr() ) {
  			double ang = type1.GetAngle();
  			if ((j = doc.ksPlacement( NULL, &x, &y, &ang, rub ))!=0 ) {
          // 
          doc.ksMoveObj( type1.GetGr(), x, y );
          // 
          if( ::fabs(ang) > 0.001 )
             doc.ksRotateObj( type1.GetGr(), x, y, ang );
          //   
          doc.ksStoreTmpGroup( type1.GetGr() );
          doc.ksClearGroup( type1.GetGr(), true );
          doc.ksDeleteObj( type1.GetGr() );
        }
      }
      else {
        if ( type1.GetGr() )
          doc.ksDeleteObj( type1.GetGr() );
        j = 0;
      }
    } while ( j );
  }
}

//-------------------------------------------------------------------------------
//
// ---
void EditFragmentLibrary( ksDocument2D& doc ) {
  char libName[250];
  char buf [250];
  //   
	::strcpy( libName, kompas.ksChoiceFile( "*.lfr"," (*.lfr)|*.lfr|  (*.*)|*.*|", 1 ) );
  if( libName ) { 
    ksRequestInfo info( kompas.GetParamStruct( ko_RequestInfo ) );
  	ksFragment fr( doc.GetFragment() );
//  6    ksFragmentLibrary
    ksFragmentLibrary  frLib( kompas.GetFragmentLibrary() ); 
		if  ( !info.m_lpDispatch || !fr.m_lpDispatch || !frLib.m_lpDispatch)
			return;
		info.Init();

		info.SetCommandsString( "!_ !_ !_ " );
    int j;
    int typeEdit;
    CString /*string*/ nameFrg;
    do {
      j = doc.ksCommandWindow( info );
      switch ( j ) {
        case 1:  //!_
					::strcpy( buf, kompas.ksReadString( "   ", "" ) );
					if ( buf ) {
            nameFrg = libName;
            if ( buf[0] != '|' )
              nameFrg += "|";
            nameFrg += buf;
            typeEdit = 2; //  
          }
          else
            typeEdit = 0;
          break;
        case 2 : //_
        case 3 : //_
          //   
					long res;
    			::strcpy( buf, frLib.ksChoiceFragmentFromLib( 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( /*(char*)*/nameFrg/*.c_str()*/, typeEdit ) ) {
          if ( typeEdit == 2 ) {
             frLib.ksFragmentLibraryOperation( nameFrg, 4 /*  */ );
            //   
            doc.ksText( 0, 100,     //  
                  0,            //  
                  5,            // 
                  1,            // 
                  0,	          // ,   .-.
                  "   "      );   // 

            doc.ksLineSeg ( 0, 100, 110, 100, 1 );
            //     
            //    ""  "  ", 
            //  
            kompas.ksSystemControlStop();
            //kompas.ksSystemControlStart( "  " );
            frLib.ksFragmentLibraryOperation( nameFrg, 0 /* c */ );
          }
        }  
        else
          kompas.ksMessageBoxResult();
      }

    } while ( j != -1 );
  }
}

//-------------------------------------------------------------------------------
//
// ---
void ChangeTechnicalDemand( ksDocument2D& doc ) {
  long ref = doc.ksGetReferenceDocumentPart( 1 );
  ksTechnicalDemandParam par( kompas.GetParamStruct(ko_TechnicalDemandParam) );
	if ( doc.ksGetObjParam( ref, par, TECHNICAL_DEMAND_PAR ) ) {
	  char buf[128];
    ::sprintf( buf, "  TT =%d", par.GetStrCount());
    kompas.ksMessage(buf);
	
		doc.ksOpenTechnicalDemand( par.GetPGab(), par.GetStyle() );

		ksTextLineParam parLine( kompas.GetParamStruct(ko_TextLineParam) );
		if ( parLine.m_lpDispatch ) {
  		parLine.Init();
	    for( int i = 0; i < par.GetStrCount(); i++ ) {
	      doc.ksGetObjParam( ref, parLine, TT_FIRST_STR+i );
		    ksTextItemParam parItem( kompas.GetParamStruct(ko_TextItemParam) );
				ksDynamicArray arr( parLine.GetTextItemArr() );
				if ( parItem.m_lpDispatch && arr.m_lpDispatch ) {
  				parItem.Init();
					for ( int j = 0, count1 = arr.ksGetArrayCount(); j < count1; j++ ) {
	  				arr.ksGetArrayItem( j, parItem );
			  		::strcpy( buf, parItem.GetS() );
				  	::strcat( buf, "!!!" );
						parItem.SetS( buf );  
						arr.ksSetArrayItem( j, parItem );
						kompas.ksMessage( parItem.GetS() );
					}
        }
  		  doc.ksSetObjParam( ref, parLine, TT_FIRST_STR+i );
			}
		}
		doc.ksCloseTechnicalDemand();
	}
}

