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

#include <vcl.h>
#pragma hdrstop

#include <windows.h>
#include <stdio.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 WorkContour( ksDocument2D* doc );
void TDemWork( ksDocument2D* doc );
void TableWork( ksDocument2D* doc );
void DrawViewPointer( ksDocument2D* doc );
void WorkStamp( ksDocument2D* doc );
void WorkTolerance( ksDocument2D* doc );
void DrawEquidistant( ksDocument2D* doc );
void DrawEllipse( ksDocument2D* doc );
void DrawPolyline( ksDocument2D* doc );
void DrawNurbs( ksDocument2D* doc );
void DrawInsFragment1( ksDocument2D* doc );
void DrawInsFragment2( ksDocument2D* doc );
void DrawSpecRough( ksDocument2D* doc );

//------------------------------------------------------------------------------
//
// ---
extern "C" void far __export __pascal  LibraryEntry( UINT Comm ) {
  kompas = (KompasObject*)::CreateKompasObject();
  if ( kompas ) {
    ksDocument2D* doc = (ksDocument2D*)kompas->ActiveDocument2D();
    switch ( Comm ) {
		case 1: //
			WorkContour( doc );
			break;
		case 2: // 
			TDemWork( doc );
			break;
		case 3: // 
      DrawViewPointer( doc );
			break;
		case 4: //  
      WorkStamp( doc );
			break;
    case 5: //
      TableWork( doc );
			break;
		case 6: //
      DrawEquidistant( doc );
			break;
		case 7: //
      DrawEllipse( doc );
			break;
		case 8: //
      DrawPolyline( doc );
			break;
		case 9: //nurbs
      DrawNurbs( doc );
			break;
		case 10: // 
      WorkTolerance( doc );
			break;
		case 11: // 
      DrawSpecRough( doc );
			break;
		case 12: //   
		  DrawInsFragment1( doc );
    	break;
		case 13: //  
		  DrawInsFragment2( doc  );
    	break;
/*      case 1: DrawTxtDB( doc );          break; //  
      case 2: DrawRectCallBack( doc );   break; //Placement, Cursor  (   step4_1.cpp )
      case 3: DrawRectNULL( doc );       break; //  
      case 4: WriteSlideStep( doc );     break; //   
      case 5: TestValidator( doc );      break; // 
      case 6: TestShowDialog( doc );     break; // 
      case 7: WorkRelativePath( doc );   break; //   (    step4_3.cpp )
      case 8: WorkSystemPath( doc );     break; //  (    step4_2.cpp )
*/
    }
    kompas->Release();
  }
}

//------------------------------------------------------------------------------
//  
// ---
void WorkContour( ksDocument2D* doc ) {
  if( doc && doc->ksContour( 1 ) ) {
    doc->ksLineSeg( 20, 30, 50, 30, 1 );
    doc->ksArcByPoint( 50, 20, 10, 50, 30, 50, 10, -1, 1 );
    //  
    doc->ksContour( 2 );
      doc->ksLineSeg( 50, 10, 20, 10, 1 );
      doc->ksArcByPoint( 20, 20, 10, 20, 10, 20, 30, -1, 1 );
    doc->ksEndObj();

    reference _contour = doc->ksEndObj();

    doc->ksLightObj( _contour, 1 );
    kompas->ksMessage( StringToOleStr("") );
    doc->ksLightObj( _contour, 1 );
    reference g = doc->ksNewGroup( 0 );
    doc->ksEndGroup();
    doc->ksAddObjGroup( g, _contour );
    doc->ksMoveObj( g, 10, 10 );
    kompas->ksMessage( StringToOleStr(" ") );
  }
}

//------------------------------------------------------------------------------
//   
// ---
void TDemWork( ksDocument2D* doc ) {
  if ( doc ) {
    ksDynamicArray* pGab = (ksDynamicArray*)kompas->GetDynamicArray( RECT_ARR );
    //       
    ksRectParam* par = (ksRectParam*)kompas->GetParamStruct( ko_RectParam );
    ksMathPointParam* mathPar = (ksMathPointParam*)kompas->GetParamStruct( ko_MathPointParam );
    if ( par && mathPar && pGab )
      mathPar->x = 230;
      mathPar->y = 65;
      par->SetpBot( mathPar );

      mathPar->x = 415;
      mathPar->y = 80;
      par->SetpTop( mathPar );

      pGab->ksAddArrayItem( -1, par );

      mathPar->x = 45;
      mathPar->y = 15;
      par->SetpBot( mathPar );

      mathPar->x = 230;
      mathPar->y = 60;
      par->SetpTop( mathPar );

      pGab->ksAddArrayItem( -1, par );

    reference p;
    if ( doc->ksOpenTechnicalDemand(pGab, 0) ) {
      ksTextItemParam* item = (ksTextItemParam*)kompas->GetParamStruct( ko_TextItemParam );
      if ( item ) {
        item->Init();
        ksTextItemFont* font = (ksTextItemFont*) item->GetItemFont();
        font->SetBitVectorValue( NEW_LINE, true );
        item->s = StringToOleStr( "1111111" );
        doc->ksTextLine( item );

        item->s = StringToOleStr( "2222222" );
        doc->ksTextLine( item );

        item->s = StringToOleStr( "3333333" );
        doc->ksTextLine( item );

        item->s = StringToOleStr( "4444444" );
        doc->ksTextLine( item );

        item->s = StringToOleStr( "5555555" );
        doc->ksTextLine( item );

        item->s = StringToOleStr( "6666666" );
        doc->ksTextLine( item );
      }

      p = doc->ksCloseTechnicalDemand();
    }

    kompas->ksMessage( StringToOleStr(" ") );
    doc->ksDeleteObj( p );
  }
}

//------------------------------------------------------------------------------
//  
// ---
void TableWork( ksDocument2D* doc ) {
  if ( doc ) {
    doc->ksTable();
      doc->ksLineSeg( 50, 50, 90, 50, 1 );
      doc->ksLineSeg( 50, 40, 90, 40, 1 );
      doc->ksLineSeg( 50, 30, 90, 30, 1 );
      doc->ksLineSeg( 50, 50, 50, 30, 1 );
      doc->ksLineSeg( 70, 50, 70, 30, 1 );
      doc->ksLineSeg( 90, 50, 90, 30, 1 );
      doc->ksText( 52, 48, 0, 5, 1, 0, StringToOleStr("1") );
      doc->ksText( 72, 48, 0, 5, 1, 0, StringToOleStr("2") );
      doc->ksText( 52, 38, 0, 5, 1, 0, StringToOleStr("3") );
      doc->ksText( 72, 38, 0, 5, 1, 0, StringToOleStr("4") );
    doc->ksEndObj();
  }
}

//------------------------------------------------------------------------------
//   
// ---
void DrawViewPointer( ksDocument2D* doc ) {
  if ( doc ) {
    ksViewPointerParam* par = (ksViewPointerParam*)kompas->GetParamStruct( ko_ViewPointerParam );
    if ( par ) {
      par->x1 = 55;
      par->y1 = 50; //  ( ) 
      par->x2 = 40;
      par->y2 = 50; //    
      par->xt = 40;
      par->yt = 52; //  
      par->type = 0; //    - 

      par->str = StringToOleStr( "B" );    // 

      reference  p = doc->ksViewPointer( par ); // " "
      if ( doc->ksExistObj(p) )
        doc->ksLightObj( p, 1 );
    }
  }
}


//------------------------------------------------------------------------------
//   
// ---
void WorkStamp( ksDocument2D* doc ) {
	ksStamp * stamp = (ksStamp*)( doc->GetStamp() );
	if ( stamp ) {
		if ( stamp->ksOpenStamp() ) {
			stamp->ksColumnNumber( 2 );

      ksTextItemParam * itemParam = (ksTextItemParam*)kompas->GetParamStruct( ko_TextItemParam );
		  if ( itemParam ) {
			  itemParam->Init();

        ksTextItemFont * itemFont = (ksTextItemFont*)itemParam->GetItemFont();
				if ( itemFont ) {
				 	itemFont->SetBitVectorValue( NEW_LINE, true );
					itemParam->set_s( StringToOleStr("1111111") );
					doc->ksTextLine( itemParam );
				}
			}

		  stamp->ksCloseStamp();
		}
  }
}

//------------------------------------------------------------------------------
//  
// ---
void WorkTolerance( ksDocument2D* doc ) {
  ksToleranceParam * par = (ksToleranceParam*)kompas->GetParamStruct( ko_ToleranceParam );
  ksMathPointParam * parPoint = (ksMathPointParam*)kompas->GetParamStruct( ko_MathPointParam );

	if ( par && parPoint ) {

		par->Init();
		parPoint->Init();

    ksToleranceBranch * tolBran = (ksToleranceBranch*)kompas->GetParamStruct( ko_ToleranceBranch );
    if ( tolBran ) {
      tolBran->Init();
      ksDynamicArray* arr = (ksDynamicArray*)tolBran->GetpMathPoint();
      ksDynamicArray* branArr = (ksDynamicArray*)par->GetBranchArr();
      if ( arr && branArr ) {

        //   1- 
        parPoint->set_x( 40 );
        parPoint->set_y( 10 );
        arr->ksAddArrayItem( -1, parPoint );
        tolBran->set_arrowType( 2 );
        tolBran->set_tCorner( 1 );
        branArr->ksAddArrayItem( -1, tolBran );

        //   2- 
        arr->ksClearArray();
        parPoint->set_x( 100 );
        parPoint->set_y( 50 );
        arr->ksAddArrayItem( -1, parPoint );
        parPoint->set_x( 100 );
        parPoint->set_y( 10 );
        arr->ksAddArrayItem( -1, parPoint );
        tolBran->set_arrowType( 1 );
        tolBran->set_tCorner( 5 );
        branArr->ksAddArrayItem( -1, tolBran );

        par->set_x( 40 );
        par->set_y( 40 );
        par->set_type( 0 );

        //    
        if ( doc->ksTolerance(par) ) {
          ksTextItemParam * itemParam = (ksTextItemParam*)kompas->GetParamStruct( ko_TextItemParam );
          if ( itemParam ) {
            itemParam->Init();

            ksTextItemFont* itemFont = (ksTextItemFont*)itemParam->GetItemFont();
            if ( itemFont ) {
              doc->ksColumnNumber( 1 );
              itemFont->SetBitVectorValue( SPECIAL_SYMBOL, true );
              itemParam->set_type( SPECIAL );
              itemParam->set_iSNumb( 26 );
              doc->ksTextLine( itemParam );

              itemParam->Init();
              doc->ksColumnNumber( 2 );
              itemFont->SetBitVectorValue( NEW_LINE, true );
              itemParam->set_s( StringToOleStr("111") );
              doc->ksTextLine( itemParam );

              doc->ksColumnNumber( 3 );
              itemFont->SetBitVectorValue( NEW_LINE, true );
              itemParam->set_s( StringToOleStr("222") );
              doc->ksTextLine( itemParam );

              itemParam->Init();
              doc->ksColumnNumber( 11 );
              itemFont->SetBitVectorValue( SPECIAL_SYMBOL, true );
              itemParam->set_type( SPECIAL );
              itemParam->set_iSNumb( 23 );
              doc->ksTextLine( itemParam );

              itemParam->Init();
              doc->ksColumnNumber( 12 );
              itemFont->SetBitVectorValue( NEW_LINE, true );
              itemParam->set_s( StringToOleStr("333") );
              doc->ksTextLine( itemParam );
              doc->ksColumnNumber( 13 );
              itemFont->SetBitVectorValue( NEW_LINE, true );
              itemParam->set_s( StringToOleStr("444") );
              doc->ksTextLine( itemParam );
            }
          }
        }
        reference p = doc->ksEndObj(); //    
        doc->ksLightObj( p, 1 );

        arr->ksDeleteArray();
        branArr->ksDeleteArray();
      }
    }
	}
}

//------------------------------------------------------------------------------
//  
// ---
void DrawEquidistant( ksDocument2D* doc ) {
  ksEquidistantParam * par = (ksEquidistantParam*)kompas->GetParamStruct(ko_EquidParam);
  ksRequestInfo * info = (ksRequestInfo*)kompas->GetParamStruct(ko_RequestInfo);

	if ( par && info ) {
		par->set_side( 2 );     // ,     
			                    // 0-  , 1-  , 2-  
		par->set_cutMode( 0 );  //    
			                    // 0- , 1-  
		par->set_degState( 0 ); //     
			                    // 0-  , 1-  
		par->set_radRight( 5 ); //  
		par->set_radLeft( 3 );  //  
		par->set_style( 1 );    //  
		info->set_commandsString( StringToOleStr(" ") );
		double x, y;
		//  
		int j;
		reference p1;
		do {
			j = doc->ksCursor( info, &x ,&y, NULL );
	    if ( j ) {
				par->set_geoObj( doc->ksFindObj(x, y, 1e6) );
		    if ( doc->ksExistObj(par->get_geoObj()) ) {
			    if ( (p1 = doc->ksEquidistant(par)) != 0 ) { //  

					  doc->ksLightObj( p1, 1 );
						kompas->ksMessage( StringToOleStr("") );
			      doc->ksLightObj( p1, 0 );
		      }
			    else
				    kompas->ksMessageBoxResult();
				}
				else
					kompas->ksError( StringToOleStr("  ") );
			}
		} while( j );
	}
}

//------------------------------------------------------------------------------
//   
// ---
void DrawEllipse( ksDocument2D* doc ) {
  ksEllipseParam * par = (ksEllipseParam*)kompas->GetParamStruct(ko_EllipseParam);
	if ( par ) {
		par->Init();
		par->set_xc( 50 );
		par->set_yc( 40 );
		par->set_A( 20 );
		par->set_B( 10 );
		par->set_style( 1 );
		reference p = doc->ksEllipse( par );
		doc->ksLightObj( p, 1 );
		kompas->ksMessage( StringToOleStr("") );
		doc->ksLightObj( p, 0 );
	}
}

//------------------------------------------------------------------------------
//  
// ---
void DrawPolyline( ksDocument2D* doc ) {
  //    
  ksPolylineParam * par = (ksPolylineParam*)kompas->GetParamStruct(ko_PolylineParam);
  ksMathPointParam * pr = (ksMathPointParam*)kompas->GetParamStruct(ko_MathPointParam);

	if ( par && pr ) {
		par->Init();
		pr->Init();

    ksDynamicArray* arr = (ksDynamicArray*)par->GetpMathPoint();
		if ( arr ) {
			pr->set_x( 10 );
			pr->set_y( 10 );
			arr->ksAddArrayItem( -1, pr );
			pr->set_x( 20 );
			pr->set_y( 20 );
			arr->ksAddArrayItem( -1, pr );
			pr->set_x( 30 );
			pr->set_y( 10 );
			arr->ksAddArrayItem( -1, pr );
			pr->set_x( 40 );
			pr->set_y( 20 );
			arr->ksAddArrayItem( -1, pr );

			par->set_style( 2 );
			reference p = doc->ksPolylineByParam( par );
			doc->ksLightObj( p, 1 );
			kompas->ksMessage( StringToOleStr("") );
			doc->ksLightObj( p, 0 );

			arr->ksDeleteArray();
		}
	}
}

//-------------------------------------------------------------------------------
//   Nurbs - 
// ---
void DrawNurbs( ksDocument2D* doc ) {
  ksNurbsPointParam * par = (ksNurbsPointParam*)kompas->GetParamStruct(ko_NurbsPointParam);
	if ( par ) {
		par->Init();
		// Nurbs     
		doc->ksNurbs( 3, 0, 1 );
			par->set_x( 0 );
			par->set_y( 0 );
			par->set_weight( 1 );
			doc->ksNurbsPoint( par );
			par->set_x( 20 );
			par->set_y( 20 );
			par->set_weight( 1 );
			doc->ksNurbsPoint( par );
			par->set_x( 50 );
			par->set_y( 10 );
			par->set_weight( 1 );
			doc->ksNurbsPoint( par );
			par->set_x( 70 );
			par->set_y( 20 );
			par->set_weight( 1 );
			doc->ksNurbsPoint( par );
			par->set_x( 100 );
			par->set_y( 0 );
			par->set_weight( 1 );
			doc->ksNurbsPoint( par );
			par->set_x( 50 );
      par->set_y( -50 );
			par->set_weight( 1 );
			doc->ksNurbsPoint( par );
		reference p = doc->ksEndObj();
		doc->ksLightObj( p, 1 );
		kompas->ksMessage( StringToOleStr("NURBS") );
		doc->ksLightObj( p, 0 );
	}
}

//-------------------------------------------------------------------------------
//   
// ---
void DrawInsFragment1( ksDocument2D* doc ) {
  ksFragment* frg = (ksFragment*)doc->GetFragment();
	if ( frg ) {
		//    
		reference pDefFrg = frg->ksFragmentDefinition( StringToOleStr("c:\\1.frw"), //   
																									 StringToOleStr("frw1"),      //  
																									1 );         //   1 -  
		if ( pDefFrg ) {
      ksPlacementParam * par = (ksPlacementParam*)kompas->GetParamStruct(ko_PlacementParam);
			if ( par ) {
				par->set_angle( 45 );
				par->set_scale_( 2 );
				par->set_xBase( 30 );
				par->set_yBase( 40 );
		    //   " "
				reference p = frg->ksInsertFragment( pDefFrg,     //    
											              				false,       //     false -   
															              par );       //  
		    doc->ksLightObj( p, 1 );
			  kompas->ksMessage( StringToOleStr("  ") );
				doc->ksLightObj( p, 0 );
			}
		}
	}
}

//-------------------------------------------------------------------------------
//   
// ---
void DrawInsFragment2( ksDocument2D* doc ) {
kompas->ksMessage( StringToOleStr("1") );
	//    
	ksFragment * frg = (ksFragment*)doc->GetFragment();
	if ( frg ) {
kompas->ksMessage( StringToOleStr("2") );
		reference pDefFrg;
		//   
	  if ( frg->ksLocalFragmentDefinition(StringToOleStr("local")) ) {
kompas->ksMessage( StringToOleStr("3") );
	    doc->ksLineSeg( 0, 0, 10, 0,  1 );
		  doc->ksLineSeg( 0, 0,  0, 10, 1 );
			doc->ksArcByPoint( 0, 0, 10, 10, 0, 0, 10, -1, 1 );
			pDefFrg = frg->ksCloseLocalFragmentDefinition();
		  if ( pDefFrg ) {
kompas->ksMessage( StringToOleStr("4") );
        ksPlacementParam * par = (ksPlacementParam*)kompas->GetParamStruct(ko_PlacementParam);
				if ( par ) {
kompas->ksMessage( StringToOleStr("5") );
					par->set_angle( 45 );
					par->set_scale_( 2 );
					par->set_xBase( 30 );
					par->set_yBase( 40 );
			    //   " "
					reference p = frg->ksInsertFragment( pDefFrg,     //    
												              				false,       //     false -   
																              par );       //  
			    doc->ksLightObj( p, 1 );
				  kompas->ksMessage( StringToOleStr("  ") );
					doc->ksLightObj( p, 0 );
				}
			}
		}
	}
}

//-------------------------------------------------------------------------------
//
// ---
void DrawSpecRough( ksDocument2D* doc ) {
  ksSpecRoughParam * par = (ksSpecRoughParam*)kompas->GetParamStruct(ko_SpecRoughParam);
	if ( par ) {
		par->Init();
		par->set_t( 1 );
		par->set_s( StringToOleStr("Rz40") );
		par->set_sign( 2 );
		par->set_style( 1 );
		doc->ksSpecRough( par );
	}
}


