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

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

/////////////////////////////////////////////////////////////////////////////
// CStep3App

BEGIN_MESSAGE_MAP(CStep3App, CWinApp)
	//{{AFX_MSG_MAP(CStep3App)
		// 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()

/////////////////////////////////////////////////////////////////////////////
// CStep3App construction

CStep3App::CStep3App()
{
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CStep3App object

CStep3App theApp;

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

KompasObject kompas( NULL );

void WorkDocument();
void DrawView( ksDocument2D& doc );
void DrawLayer( ksDocument2D& doc );
void DrawGroup( ksDocument2D& doc );
void WorkNameGroup( ksDocument2D& doc );
void DrawLineSeg( ksDocument2D& doc );
void DrawArc( ksDocument2D& doc );
void DrawLine( ksDocument2D& doc );
void DrawCircle( ksDocument2D& doc );
void DrawPoint( ksDocument2D& doc );
void DrawBezier( ksDocument2D& doc );
void DrawHatch( ksDocument2D& doc );
void DrawText( ksDocument2D& doc );

//-------------------------------------------------------------------------------
//
// ---
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 ) {
		if ( comm == 1 ) 
			::WorkDocument();  //  
		else {
			ksDocument2D doc( kompas.ActiveDocument2D() );
			if ( doc.m_lpDispatch ) {
				switch ( comm ) {
					case 2  : ::DrawView( doc );      break; // 
					case 3  : ::DrawLayer( doc );     break; // 
					case 4  : ::DrawGroup( doc );     break; // 
					case 5  : ::WorkNameGroup( doc ); break; //  
					case 6  : ::DrawLineSeg( doc );   break; // 
					case 7  : ::DrawArc( doc );       break; // 
					case 8  : ::DrawLine( doc );      break; // 
					case 9  : ::DrawCircle( doc );    break; // 
					case 10 : ::DrawPoint( doc );			break; // 
					case 11 : ::DrawBezier( doc );    break; // Bezier-
					case 12 : ::DrawHatch( doc );     break; // 
					case 13 : ::DrawText( doc );			break; // 
				}
			}
		}
	}
}

//-------------------------------------------------------------------------------
//    
//-------------------------------------------------------------------------------
void WorkDocument() {
	char buf[ 128 ];
	ksDocument2D doc( kompas.Document2D() );
	ksDocumentParam docPar ( kompas.GetParamStruct(ko_DocumentParam) );
	ksDocumentParam docPar1( kompas.GetParamStruct(ko_DocumentParam) );
	if ( docPar.m_lpDispatch && docPar1.m_lpDispatch ) {
		docPar.SetFileName( CString("c:\\2.cdw") );
		docPar.SetComment( CString("create document") );
		docPar.SetAuthor( CString("user") );
		docPar.SetRegime( 0 );
		docPar.SetType( lt_DocSheetStandart );
		ksSheetPar shPar( docPar.GetLayoutParam() );
		if ( shPar.m_lpDispatch ) {
			shPar.SetShtType( 1 );
			shPar.SetLayoutName( CString("") );
			ksStandartSheet stPar( shPar.GetSheetParam() );
			if ( stPar.m_lpDispatch ) {
				stPar.SetFormat( 3 );
				stPar.SetMultiply( 1 );
				stPar.SetDirect( false );
			}
		}
		//  : ,  3,  
		//     1
		doc.ksCreateDocument( docPar );

		ksViewParam par( kompas.GetParamStruct(ko_ViewParam) );
		if ( par.m_lpDispatch ) {
			long number = 2;
			par.Init();
			par.SetX( 10 );
			par.SetY(20 );
			par.SetScale_( 0.5 );
			par.SetAngle( 45 );
			par.SetColor( RGB(10, 20, 10) );
			par.SetState( stACTIVE );
			par.SetName( CString("user view") );
		  //       2,  0.5,   45 
			doc.ksCreateSheetView( par, &number );

			//     5
			doc.ksLayer( 5 );

			doc.ksLineSeg( 20, 10, 40, 10, 1 );
			doc.ksLineSeg( 40, 10, 40, 30, 1 );
			doc.ksLineSeg( 40, 30, 20, 30, 1 );
			doc.ksLineSeg( 20, 30, 20, 10, 1 );

			kompas.ksMessage( "" );

		  //   
			doc.ksGetObjParam( doc.GetReference(), docPar1, ALLPARAM );
			ksSheetPar shPar1( docPar1.GetLayoutParam() );
			if ( shPar1.m_lpDispatch ) {
				ksStandartSheet stPar( shPar1.GetSheetParam() );
				if ( stPar.m_lpDispatch ) {
					::sprintf( buf, "type = %d, f = %d, m = %d, d = %d ", docPar1.GetType(), stPar.GetFormat(),
					           stPar.GetMultiply(), stPar.GetDirect() );
					kompas.ksMessage( buf );
				}
			}

		  kompas.ksMessage( docPar1.GetFileName() );
			kompas.ksMessage( docPar1.GetComment() );
			kompas.ksMessage( docPar1.GetAuthor() );

			//  
			doc.ksSaveDocument( CString("\0") );
		}
	}
}

//-------------------------------------------------------------------------------
//  
//-------------------------------------------------------------------------------
void DrawView( ksDocument2D& doc )  {
	char buf[128];
	ksViewParam par( kompas.GetParamStruct(ko_ViewParam) );
	if ( par.m_lpDispatch ) {
		long number = 5;
		par.Init();
		par.SetX( 10 );
		par.SetY(20 );
		par.SetScale_( 0.5 );
		par.SetAngle( 45 );
		par.SetColor( RGB(10, 20, 10) );
		par.SetState( stACTIVE );
		par.SetName( CString("user view") );

		reference gr = 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();

	  //  
		reference v = doc.ksCreateSheetView( par, &number );
		number = doc.ksGetViewNumber( v );
		::sprintf( buf, " : ref = %d, number = %d", v, number );
		kompas.ksMessage( buf );

		doc.ksAddObjGroup( gr, v );
		kompas.ksMessage( "   " );
		kompas.ksMessageBoxResult();
		
		reference p = doc.ksLineSeg( 10, 10, 30, 30, 0 );
		doc.ksAddObjGroup( gr, p );

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

		doc.ksRotateObj( gr, 0, 0, -45 );

		//   
		par.Init();
		doc.ksGetObjParam( v, par, ALLPARAM );

		::sprintf( buf, "x = %4.1f, y = %4.1f, angl = %4.1f, name = %s, st = %d", par.GetX(), 
							 par.GetY(), par.GetAngle(), par.GetName(), par.GetState() );
		kompas.ksMessage( buf );

	  //    (  0 )
		doc.ksOpenView( 0 );
		//     :  
		ksLtVariant var( kompas.GetParamStruct(ko_LtVariant) );
		if ( var.m_lpDispatch ) {
			var.Init();
			var.SetIntVal( stREADONLY );
			doc.ksSetObjParam( v, var, VIEW_LAYER_STATE );
		}
	}
}

//-------------------------------------------------------------------------------
//  
//-------------------------------------------------------------------------------
void DrawLayer( ksDocument2D& doc ) {
  long n;
  if ( !kompas.ksReadInt("  ", 1, 0, 255, &n) )
    return;
  
	//  ,   
  reference lay = doc.ksLayer( n );
  doc.ksMtr( 20, 15, 0, 1, 1 );
		doc.ksLineSeg( -10, 0,  10,  0,  1 );
		doc.ksLineSeg( 10,  0,  10,  20, 1 );
		doc.ksLineSeg( 10,  20, -10, 20, 1 );
		doc.ksLineSeg( -10, 20, -10, 0,  1 );
  doc.ksDeleteMtr();

  //  
  doc.ksLightObj( lay, 1 );

  //         
  int n1 = doc.ksGetLayerNumber( lay );
  reference l = doc.ksGetLayerReference( n1 );

  char buf[128];
  ::sprintf( buf, "n = %d, n1 = %d, layer = %d, l = %d", n, n1, lay, l );
  kompas.ksMessage( buf );

  //      
  ksLayerParam par ( kompas.GetParamStruct(ko_LayerParam) );
  ksLayerParam par1( kompas.GetParamStruct(ko_LayerParam) );
	if ( par.m_lpDispatch && par1.m_lpDispatch ) {
		par.Init();
		par1.Init();
		par.SetColor( RGB(0, 255, 0) );
		par.SetState( stACTIVE );
		par.SetName( "" );
		doc.ksLayer( 0 );

		if ( !doc.ksSetObjParam(l, par, ALLPARAM) )
			kompas.ksMessageBoxResult();
		else {
			doc.ksGetObjParam( l, par1, ALLPARAM );
			::sprintf( buf, "col = %d, col1 = %d, name = %s, name1 = %s", par.GetColor(),
		             par1.GetColor(), par.GetName(), par1.GetName() );
			kompas.ksMessage( buf );
		}
		doc.ksLightObj( lay, 0 );

		//   
		ksLtVariant var( kompas.GetParamStruct(ko_LtVariant) );
		if ( var.m_lpDispatch ) {
			var.Init();
			var.SetIntVal( stACTIVE );
			doc.ksSetObjParam( l, var, VIEW_LAYER_STATE );
		}
	}
}

//-------------------------------------------------------------------------------
//   
//-------------------------------------------------------------------------------
void DrawGroup( ksDocument2D& doc ) {
	reference p1 = doc.ksLineSeg( 10, 10, 20, 10, 0 );
	reference p2 = doc.ksLineSeg( 10, 10, 10, 20, 0 );

	//      1
	reference gr1 = doc.ksNewGroup(0);
	doc.ksEndGroup();

	//      2
	reference gr2 = doc.ksNewGroup(0);
	doc.ksEndGroup();

	doc.ksAddObjGroup( gr1, p1 );
	doc.ksAddObjGroup( gr1, p2 );

	doc.ksAddObjGroup( gr2, p1 );
	doc.ksAddObjGroup( gr2, p2 );

	kompas.ksMessage( " " );

	doc.ksMoveObj( gr1, 10, 0 );
  kompas.ksMessage( "   10 " );

  doc.ksRotateObj( gr2, 20, 10, 45 );
  kompas.ksMessage( "   45 " );

  doc.ksRotateObj( gr2, 20, 10, -45 );
  kompas.ksMessage( "   -45 " );

  doc.ksMoveObj( gr1, -10, 0 );
  kompas.ksMessage( "   -10 " );

  //   2 (     )
  doc.ksClearGroup( gr2, false );
  //    2
  doc.ksDeleteObj( gr2 );

	kompas.ksMessage( " gr" );
	doc.ksLightObj( gr1, 1 );

	kompas.ksMessage( " gr" );
	doc.ksLightObj( gr1, 0 );

	kompas.ksMessage( " el" );
	doc.ksLightObj( p1, 1 );
	kompas.ksMessage( " el" );
	doc.ksLightObj( p1, 0 );

	//   1(    )
	doc.ksDeleteObj( gr1 );
	kompas.ksMessageBoxResult();
}

//-------------------------------------------------------------------------------
//     
//-------------------------------------------------------------------------------
void WorkNameGroup( ksDocument2D& doc ) {
 //        
 reference gr = doc.ksNewGroup( 0 );
   reference p = doc.ksLineSeg( 20, 20, 40, 20, 1 );
   doc.ksLineSeg( 40, 20, 40, 40, 1 );
   doc.ksLineSeg( 40, 40, 20, 40, 1 );
   doc.ksLineSeg( 20, 40, 20, 20, 1 );
 doc.ksEndGroup();

 //    
 //     
 if ( !doc.ksSaveGroup(gr, "group1") )
   exit( EXIT_SUCCESS );
 reference gr1 = doc.ksGetGroup( "group1" );
 if( !gr1 )
   exit( EXIT_SUCCESS );

 reference c = doc.ksCircle( 30, 30, 10, 1 );
 doc.ksAddObjGroup( gr1, c );

 doc.ksLightObj( gr1, 1 );
 kompas.ksMessage( "    " );
 doc.ksLightObj( gr1, 0 );

 doc.ksExcludeObjGroup( gr1, p );

 doc.ksLightObj( gr1, 1 );
 kompas.ksMessage( "    " );
 doc.ksLightObj( gr1, 0 );
}

//-------------------------------------------------------------------------------
//  
//--------------------------------------------------------------------------------
void DrawLineSeg( ksDocument2D& doc ) {
  char buf [128];
	// 
  //   
  doc.ksMtr( 30, 20, 45, 1, 1 );

//	ksDocument2D doc1( kompas.Document2D() );                          // 
//	ksDocumentParam pardoc( kompas.GetParamStruct(ko_DocumentParam) ); //  
//	if ( pardoc.m_lpDispatch && doc1.m_lpDispatch ) {
//		pardoc.Init();
//		pardoc.SetType( lt_DocFragment ); // 
//		pardoc.SetRegime( 0 );            //  
//		doc1.ksCreateDocument( pardoc );  //  
//	}

  reference p = doc.ksLineSeg( 30, 20, 60, 20, 1 );

  //  
  ksLineSegParam par( kompas.GetParamStruct(ko_LineSegParam) );
	if ( par.m_lpDispatch ) {
	  int t = doc.ksGetObjParam( p, par, ALLPARAM );

		::sprintf( buf, "t = %d, x1 = %4.1f, y1 = %4.1f, x2 = %4.1f, y2 = %4.1f, tl = %d", t,
               par.GetX1(), par.GetY1(), par.GetX2(), par.GetY2(), par.GetStyle() );

	  kompas.ksMessage( buf );

		//   
		par.SetX2( 30 );
		par.SetY2( 60 );
		par.SetStyle( 2 );

		if ( doc.ksSetObjParam(p, par, ALLPARAM) )
			kompas.ksMessage( " " );
		else
			kompas.ksMessageBoxResult();
	}
	//    
  doc.ksDeleteMtr();
}

//-------------------------------------------------------------------------------
//  
//--------------------------------------------------------------------------------
void DrawArc( ksDocument2D& doc )  {
  char buf [128];
  // 
  doc.ksMtr( 10, 10, 0, 1, 1 );
  reference p = doc.ksArcByAngle( 30, 20, 20, 45, 135, 1, 1 );

  //     
  ksArcByAngleParam par ( kompas.GetParamStruct(ko_ArcByAngleParam) );
  ksArcByPointParam par1( kompas.GetParamStruct(ko_ArcByPointParam) );
	if ( par.m_lpDispatch && par1.m_lpDispatch ) {
		int t = doc.ksGetObjParam( p, par, ALLPARAM );

		::sprintf( buf, "t = %d, xc = %4.1f, yc = %4.1f, rad = %4.1f, \n\n a1 = %4.1f, a2 = %4.1f, napr = %d , tl = %d",
               t, par.GetXc(), par.GetYc(), par.GetRad(), par.GetAng1(), par.GetAng2(), 
							 par.GetDir(), par.GetStyle() );
		kompas.ksMessage( buf );

		//     
		par1.SetXc( 40 );
		par1.SetYc( 30 );
		par1.SetRad( 10 );
		par1.SetDir( 1 );
		par1.SetStyle( 2 );
		par1.SetX1( 50 );
		par1.SetY1( 30 );
		par1.SetX2( 40 );
		par1.SetY2( 20 );

		if( doc.ksSetObjParam(p, par1, 1) )
			kompas.ksMessage( " " );
		else
			kompas.ksMessageBoxResult();
	}
  doc.ksDeleteMtr();
}

//-------------------------------------------------------------------------------
//   
//--------------------------------------------------------------------------------
void DrawLine( ksDocument2D& doc )  {
  doc.ksMtr( 0, 0, 45, 1, 1 );
  char buf [128];
  //  
  reference p = doc.ksLine( 30, 20, 0 );

  //   
  ksLineParam par( kompas.GetParamStruct(ko_LineParam) );
	if ( par.m_lpDispatch ) {
		par.Init();
		int t = doc.ksGetObjParam( p, par, ALLPARAM );
		::sprintf( buf, "t = %d, x = %4.1f, y = %4.1f, alf = %4.1f", t,
			         par.GetX(), par.GetY(), par.GetAngle() );
		kompas.ksMessage( buf );

		//    
		par.SetAngle( 90 );

		if( doc.ksSetObjParam(p, par, ALLPARAM) )
	    kompas.ksMessage( " " );
		else
			kompas.ksMessageBoxResult();
	}
  doc.ksDeleteMtr();
}

//-------------------------------------------------------------------------------
//  
//--------------------------------------------------------------------------------
void DrawCircle( ksDocument2D& doc ) {
  doc.ksMtr( 0, 0, 0, 2, 2 );
  char buf[128];
  // 
  reference p = doc.ksCircle( 30, 20, 10, 1 );

  //  
  ksCircleParam par( kompas.GetParamStruct(ko_CircleParam) );
	if ( par.m_lpDispatch ) {
	  int t = doc.ksGetObjParam( p, par, ALLPARAM );
		::sprintf( buf, "t = %d, xc = %4.1f, yc = %4.1f, rad = %4.1f, tl = %d", t,
               par.GetXc(), par.GetYc(), par.GetRad(), par.GetStyle() );
		kompas.ksMessage( buf );

		//  
		par.SetRad( 20 );
		par.SetStyle( 2 );
		if ( doc.ksSetObjParam(p, par, ALLPARAM) )
			kompas.ksMessage( " " );
		else
			kompas.ksMessageBoxResult();
	}
  doc.ksDeleteMtr();
}

//-------------------------------------------------------------------------------
//  
//--------------------------------------------------------------------------------
void DrawPoint( ksDocument2D& doc ) {
  // 0-, 1-, 2--, 3-, 4-, 5-, 6-,
	// 7- 

	doc.ksMtr( 10, 10, 0, 1, 1 );
	char buf [128];
	// 
	reference p = doc.ksPoint( 30, 40, 0 );
	doc.ksPoint( 40,  40, 1 );
	doc.ksPoint( 50,  40, 2 );
	doc.ksPoint( 60,  40, 3 );
	doc.ksPoint( 70,  40, 4 );
	doc.ksPoint( 80,  40, 5 );
  doc.ksPoint( 90,  40, 6 );
  doc.ksPoint( 100, 40, 7 );

  //  
	ksPointParam par( kompas.GetParamStruct(ko_PointParam) );
	if ( par.m_lpDispatch ) {
		int t = doc.ksGetObjParam( p, par, ALLPARAM );
		::sprintf( buf, "t = %d, x = %4.1f, y = %4.1f, style = %d", t,
							 par.GetX(), par.GetY(), par.GetStyle() );
		kompas.ksMessage( buf );

		//  
		par.SetX( 20 );
		par.SetY( 30 );
		par.SetStyle( 7 );

		if ( doc.ksSetObjParam(p, par, ALLPARAM) )
			kompas.ksMessage( " " );
		else
			kompas.ksMessageBoxResult();
	}
	doc.ksDeleteMtr();
}

//-------------------------------------------------------------------------------
//  Bezier 
//--------------------------------------------------------------------------------
void DrawBezier( ksDocument2D& doc ) {
  char buf [128];
	double x[] = { 0, 20, 50, 70, 100, 50  };
  double y[] = { 0, 20, 10, 20, 0,   -50 };

  // Bezier 
  doc.ksBezier( 0, 1 );
    for ( int i = 0; i < 5; i++ )
      doc.ksPoint( x[i], y[i], 0 );
  reference p = doc.ksEndObj();
  
	//  Bezier 
  ksMathPointParam pPar( kompas.GetParamStruct(ko_MathPointParam) );
  ksBezierParam    par ( kompas.GetParamStruct(ko_BezierParam)    );
	if ( pPar.m_lpDispatch && par.m_lpDispatch ) {
		par.Init();
		ksDynamicArray arr( par.GetMathPointArr() );
		if ( arr.m_lpDispatch ) {
			int t = doc.ksGetObjParam( p, par, ALLPARAM );

			int count = arr.ksGetArrayCount();
			::sprintf( buf, "t = %d, count = %d, close = %d, tl = %d", t,
							   count, par.GetClosed(), par.GetStyle() );
			kompas.ksMessage( buf );

			for ( int i = 0; i < count; i++ ) {
				arr.ksGetArrayItem( i, pPar );
				::sprintf( buf, "x[%d] = %4.1f, y[%d] = %4.1f",
									 i, pPar.GetX(), i, pPar.GetY() );
				kompas.ksMessage( buf );
			}


			//	  Bezier 
			arr.ksClearArray();
			//   
			for ( i = 0; i < 6; i++ ) {
				pPar.SetX( x[i] );
				pPar.SetY( y[i] );
				arr.ksAddArrayItem( -1, pPar );
		  }

			par.SetStyle( 2 );
			par.SetClosed( 1 );

			if ( doc.ksSetObjParam(p, par, ALLPARAM) )
				kompas.ksMessage( " " );
			else
				kompas.ksMessageBoxResult();
			arr.ksDeleteArray();
		}
	}
}

//-------------------------------------------------------------------------------
//  
//--------------------------------------------------------------------------------
void DrawHatch( ksDocument2D& doc )  {
	doc.ksMtr( 30, 20, 0, 0.5, 0.5 );
	char buf [128];

	//  
	doc.ksLineSeg( 20, 30, 70, 30, 2 );
	doc.ksLineSeg( 70, 30, 70, 80, 2 );
	doc.ksLineSeg( 70, 80, 20, 80, 2 );
	doc.ksLineSeg( 20, 80, 20, 30, 2 );

  if( doc.ksHatch(0, 45, 2, 0, 0, 0) ) {
		doc.ksLineSeg( 20, 30, 70, 30, 2 );
		doc.ksLineSeg( 70, 30, 70, 80, 2 );
		doc.ksLineSeg( 70, 80, 20, 80, 2 );
		doc.ksLineSeg( 20, 80, 20, 30, 2 );
    reference p = doc.ksEndObj();

    //  
    ksHatchParam par( kompas.GetParamStruct(ko_HatchParam) );
		if ( par.m_lpDispatch ) {
			par.Init();
			int t = doc.ksGetObjParam( p, par, ALLPARAM );
			::sprintf( buf, "t = %d, tip = %d, angl = %4.1f, shag = %4.1f, \n\n width = %4.1f, x0 = %4.1f, y0 = %4.1f",
						     t, par.GetStyle(), par.GetAng(), par.GetStep(), par.GetWidth(), par.GetX(), par.GetY() );
			kompas.ksMessage( buf );
			doc.ksDeleteMtr();

	    doc.ksMtr( 0, 0, 0, 2, 2 );

			//  
			par.SetX( 0.8 );

			if	( doc.ksSetObjParam(p, par, ALLPARAM) )
				kompas.ksMessage( " " );
			else
				kompas.ksMessageBoxResult();
	    doc.ksDeleteMtr();
		}
  }
  else
    kompas.ksMessageBoxResult();
}

//-------------------------------------------------------------------------------
//
// ---
static void PrintPar1( ksTextLineParam& par2, ksTextItemParam& par3, ksDynamicArray& arr2 ) {
	char buf[128];
	::sprintf( buf, "style = %d", par2.GetStyle() );
	kompas.ksMessage( buf );

	int count = arr2.ksGetArrayCount();
	for ( int j = 0; j < count; j++ ) {
	  arr2.ksGetArrayItem( j, par3 );
  	ksTextItemFont font( par3.GetItemFont() );
		if ( font.m_lpDispatch ) {
			::sprintf( buf, "j = %d, h = %5.1f, s = %s \n fontName = %s", j, font.GetHeight(), par3.GetS(),
							   font.GetFontName() );
			kompas.ksMessage( buf );
		}
	}
	kompas.ksMessageBoxResult();
}

//-------------------------------------------------------------------------------
//
// ---
void DrawText( ksDocument2D& doc ) {
	ksParagraphParam par( kompas.GetParamStruct(ko_ParagraphParam) );
	ksTextParam      par1( kompas.GetParamStruct(ko_TextParam)     );
	ksTextLineParam  par2( kompas.GetParamStruct(ko_TextLineParam) );
	ksTextItemParam  par3( kompas.GetParamStruct(ko_TextItemParam) );
	if ( par.m_lpDispatch && par1.m_lpDispatch && par2.m_lpDispatch && par3.m_lpDispatch ) {
		par1.Init();
		par2.Init();
		par3.Init();
		par.Init();
		par.SetX( 30 );
		par.SetY( 30 );
		par.SetHeight( 25 );
		par.SetWidth( 20 );

		doc.ksParagraph( par );

		// 4        
    ksTextItemParam itemParam( kompas.GetParamStruct(ko_TextItemParam) );
    if ( itemParam.m_lpDispatch ) {
		  itemParam.Init(); 
		        
		  ksTextItemFont itemFont( itemParam.GetItemFont() );
			if ( itemFont.m_lpDispatch ) {
				itemFont.Init();
			 	itemFont.SetBitVectorValue( NEW_LINE, true );
				itemParam.SetS( CString("111") );
				doc.ksTextLine( itemParam );

				itemFont.Init();
			 	itemFont.SetBitVectorValue( NUMERATOR, true );
				itemFont.SetBitVectorValue( ITALIC_ON, true );
				itemParam.SetS( CString("55") );
				doc.ksTextLine( itemParam );

				itemFont.Init();
			 	itemFont.SetBitVectorValue( DENOMINATOR, true );
				itemParam.SetS( CString("77") );
				doc.ksTextLine( itemParam );

				itemFont.Init();
			 	itemFont.SetBitVectorValue( END_FRACTION, true );
				itemFont.SetBitVectorValue( BOLD_OFF, true );
				itemFont.SetBitVectorValue( ITALIC_OFF, true );
				itemParam.SetS( CString("4444") );
				doc.ksTextLine( itemParam );
			}
		}

		reference p = doc.ksEndObj();

		//         :
		ksDynamicArray arr1( par1.GetTextLineArr() );   //   
		ksDynamicArray arr2( par2.GetTextItemArr() );   //    
		if ( arr1.m_lpDispatch && arr2.m_lpDispatch ) {
			//   1 -  (  0 )
			doc.ksGetObjParam( p, par2, 0 );

			::PrintPar1( par2, par3, arr2 );

			if ( kompas.ksYesNo("   ?") ) {
				//     ITALIC  BOLD   
				arr2.ksGetArrayItem( 0, par3 );
			  ksTextItemFont font( par3.GetItemFont() );
				if ( font.m_lpDispatch ) {
					font.SetBitVectorValue( BOLD_OFF, true );
					font.SetBitVectorValue( ITALIC_OFF, true );
					font.SetColor( RGB(0, 255, 0) );
					arr2.ksSetArrayItem( 0, par3 );
					//     
					doc.ksSetObjParam( p, par2, 0 );
					//  1 -  (  0 )   
  				doc.ksGetObjParam( p, par2, 0 );
					::PrintPar1( par2, par3, arr2 );
				}
			}
		}
	}
}

