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

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

/////////////////////////////////////////////////////////////////////////////
// CStep7App

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

/////////////////////////////////////////////////////////////////////////////
// CStep7App construction

CStep7App::CStep7App()
{
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CStep7App object

CStep7App theApp;

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

void WalkFromView( ksDocument2D& doc );
void WalkFromMacro( ksDocument2D& doc );
void WalkFromDoc();
void WalkViewDoc( ksDocument2D& doc );
void WalkGroup( ksDocument2D& doc );
void WalkLayer( ksDocument2D& doc );
void WalkFromGroup( ksDocument2D& doc );
void WalkFromDocWithAttr( ksDocument2D& doc );
void WalkFromObjWithAttr( 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 ) {
		if ( comm == 3 || comm == 4 ) {
			ksDocument2D doc( kompas.Document2D() );
			if ( doc.m_lpDispatch ) {
				switch ( comm ) {
 					case 3  : ::WalkFromDoc();              break; //  
 					case 4  : ::WalkViewDoc( doc );         break; //  
				}
			}
		}
		else {
			ksDocument2D doc( kompas.ActiveDocument2D() );
			if ( doc.m_lpDispatch && doc.GetReference() ) {
				switch ( comm ) {
 				case 1  : ::WalkFromView( doc );        break; //  
  			case 2  : ::WalkFromMacro( doc );       break; //  
  			case 5  : ::WalkGroup( doc );           break; //     
  			case 6  : ::WalkLayer( doc );           break; //  
				case 7  : ::WalkFromGroup( doc );       break; //  
				case 8  : ::WalkFromDocWithAttr( doc ); break; //      
				case 9  : ::WalkFromObjWithAttr( doc ); break; //   
				}
			}
			else
				kompas.ksError( "    \n  /" );
		}
	}
}

static void UserLightObj( reference obj,char *c, int count, ksDocument2D& doc ) {
char buf[128];
   doc.ksLightObj( obj, 1 );
   ::sprintf(buf, c, count );
   kompas.ksMessage( buf );
   doc.ksLightObj( obj, 0 );
}

//-------------------------------------------------------------------------------
//
// ---
void WalkFromView( ksDocument2D& doc ) {
  //           
  reference obj;
  int count  = 0;
  char buf[128];
  ksIterator iter( kompas.GetIterator() );
	if ( !iter.m_lpDispatch )
		return;
	if ( iter.ksCreateIterator( ALL_OBJ, 0 ) ) {
    if ( doc.ksExistObj( obj = iter.ksMoveIterator( "F" )) ){
      do {
        doc.ksLightObj( obj, 1 );
        count ++;
        ::sprintf( buf, " = %d", count );
        kompas.ksMessage( buf );
        doc.ksLightObj( obj, 0 );
      } while( doc.ksExistObj ( obj = iter.ksMoveIterator( "N" )) );
    }
		iter.ksDeleteIterator();
  }
}

//-------------------------------------------------------------------------------
//
// ---
void WalkFromMacro( ksDocument2D& doc ) {
  //          
  reference obj, macro;
  char s[50],s1[50];
  int count  = 0, count1=0 ;
  ::strcpy(s," =%d");
  ::strcpy(s1," =%d");
  ksIterator iter( kompas.GetIterator() );
	if ( !iter.m_lpDispatch )
		return;
	if ( iter.ksCreateIterator( MACRO_OBJ, 0 ) ) {
    if ( doc.ksExistObj( macro = iter.ksMoveIterator( "F" )) ){
      do {
        count ++;
        UserLightObj( macro, s, count, doc ); //  
				ksIterator iter2( kompas.GetIterator() ); //      
        iter2.ksCreateIterator( ALL_OBJ, macro );
	      if ( iter2.GetReference() ) {
          if ( doc.ksExistObj( obj = iter2.ksMoveIterator( "F" )) ){
            do {
              count1++;
              UserLightObj( obj, s1, count1, doc ); //   
						}  while ( doc.ksExistObj ( obj = iter2.ksMoveIterator( "N" )) );
          }
        }
      } while( doc.ksExistObj ( macro = iter.ksMoveIterator( "N" )) );
		}
		iter.ksDeleteIterator();
  }
  if ( !count )
    kompas.ksError( "   " );
}

//-------------------------------------------------------------------------------
//
// ---
void WalkFromDoc() {
	ksDocument2D doc( kompas.Document2D() );
	ksDocumentParam docPar ( kompas.GetParamStruct(ko_DocumentParam) );
  char buf[128];
	if ( docPar.m_lpDispatch && doc.m_lpDispatch ) {
		docPar.Init();
		docPar.SetType( lt_DocSheetStandart );
		
		ksSheetPar sheetPar( docPar.GetLayoutParam() );
		if ( !sheetPar.m_lpDispatch ) 
			return;
		sheetPar.Init();
		sheetPar.SetShtType( 1 );
		sheetPar.SetLayoutName( CString("") );

		ksStandartSheet stSheet( sheetPar.GetSheetParam() );
		if ( !stSheet.m_lpDispatch ) 
			return;
		stSheet.Init();

		docPar.SetComment("create document");
		docPar.SetAuthor("Misha");
		docPar.SetRegime(0);
		sheetPar.SetLayoutName("");
		sheetPar.SetShtType(1);
		stSheet.SetFormat(3);
		stSheet.SetMultiply(1);
		stSheet.SetDirect(0);

		docPar.SetFileName("a.cdw");
		doc.ksCreateDocument( docPar );
		docPar.SetFileName("b.cdw");
		doc.ksCreateDocument( docPar );
		docPar.SetFileName("c.cdw");
		doc.ksCreateDocument( docPar );

		int count=0;
		ksIterator iter( kompas.GetIterator() );
		if ( !iter.m_lpDispatch )
			return;
		if ( iter.ksCreateIterator( DOCUMENT_OBJ, 0 ) ) { //     
			reference pDoc = iter.ksMoveIterator( "F" );
			if ( pDoc ){
				do {
					doc.SetReference( pDoc );
					if ( doc.ksSetObjParam( pDoc, 0, DOCUMENT_STATE ) ) { //  pDoc
						count ++;
						ksViewParam viewPar( kompas.GetParamStruct(ko_ViewParam) );
						if ( !viewPar.m_lpDispatch )
							return;

						viewPar.Init();

						long number = count;
						viewPar.SetX(10);
						viewPar.SetY(20);
						viewPar.SetScale_(1);
						viewPar.SetAngle(0);
						viewPar.SetColor(RGB(10,20,10));
						viewPar.SetState(stACTIVE);
						viewPar.SetName("user view");

						doc.ksCreateSheetView( viewPar, &number); //   
						doc.ksLayer( count ); //  
						switch ( count ) {
							case 1: doc.ksLineSeg( 20, 10, 40, 10, 1 );            break; //    
							case 2: doc.ksCircle ( 50, 50, 20, 1 );                break; //    
							case 3: doc.ksArcByAngle( 50, 50, 20, 45, 135, 1, 1 ); break; //     
						}
						::sprintf(buf, " %d ", count );
						kompas.ksMessage( buf );
					}
					pDoc = iter.ksMoveIterator( "N" );
				} while ( pDoc );
			}
  		iter.ksDeleteIterator();
		}
	}
}

//  
static void CreateDocument( ksDocument2D& doc ) {
	ksDocumentParam docPar ( kompas.GetParamStruct(ko_DocumentParam) );
	if ( !docPar.m_lpDispatch || !doc.m_lpDispatch ) 
		return;
	docPar.Init();
	docPar.SetType( lt_DocSheetStandart );
	
	ksSheetPar sheetPar( docPar.GetLayoutParam() );
	if ( !sheetPar.m_lpDispatch ) 
		return;
	sheetPar.Init();
	sheetPar.SetShtType( 1 );
	sheetPar.SetLayoutName( CString("") );

	ksStandartSheet stSheet( sheetPar.GetSheetParam() );
	if ( !stSheet.m_lpDispatch ) 
		return;
	stSheet.Init();

	docPar.SetComment("create document");
	docPar.SetAuthor("Misha");
	docPar.SetRegime(0);
	stSheet.SetFormat(3);
	stSheet.SetMultiply(1);
	stSheet.SetDirect(0);

  docPar.SetFileName("a.cdw");
  doc.ksCreateDocument( docPar );
}


//-------------------------------------------------------------------------------
//
// ---
void WalkViewDoc( ksDocument2D& doc ) {
   CreateDocument( doc ); //  

  // 5 
  for( int i = 0; i < 5; i++ ) {
		ksViewParam viewPar( kompas.GetParamStruct(ko_ViewParam) );
		if ( !viewPar.m_lpDispatch )
			return;

    viewPar.Init();

    long number = 0;
		viewPar.SetX(10);
		viewPar.SetY(20);
		viewPar.SetScale_(1);
		viewPar.SetAngle(0);
		viewPar.SetColor(RGB(10,20,10));
		viewPar.SetState(stACTIVE);
    viewPar.SetName("user view");

    doc.ksCreateSheetView( viewPar, &number); //   
  }

	reference  pView;
	int count =0;
  ksIterator iter( kompas.GetIterator() );
	if ( !iter.m_lpDispatch )
		return;
	if ( iter.ksCreateIterator( VIEW_OBJ, 0 ) ) { //       
    pView = iter.ksMoveIterator( "F" );
    if ( pView ){
      do  {
    		ksLtVariant var( kompas.GetParamStruct(ko_LtVariant) );
		    if ( !var.m_lpDispatch ) 
					return;
   			  
				var.Init();

				var.SetIntVal( stCURRENT );
        if ( doc.ksSetObjParam( pView, var, VIEW_LAYER_STATE )) {
          switch (count) {
            case 1: doc.ksLineSeg( 20, 20, 40, 20, 1 ); break;
            case 2: doc.ksCircle( 40, 20, 30, 1 );      break;
            case 3: doc.ksArcByAngle( 50, 50, 20, 45, 135, 1, 1 ); break;
            case 4:
              doc.ksMtr(40,0,0,1,1);
              doc.ksLineSeg( 10, 10, 30, 30, 1 );
              doc.ksLineSeg( 30, 30, 60, 10, 1 );
              doc.ksLineSeg( 60, 10, 10, 10, 1 );
              doc.ksDeleteMtr();
            break;
            case 5:
              doc.ksCircle( 30, 30, 20, 1 );
              doc.ksCircle( 30, 30, 10, 1 );
              doc.ksHatch( 0, 45, 2, 0, 0, 0);
                doc.ksCircle( 30, 30, 20, 1 );
                doc.ksCircle( 30, 30, 10, 1 );
              doc.ksEndObj();
            break;
          }
        }
        count++;
        pView = iter.ksMoveIterator ( "N" );
      } while ( pView );
    }
		iter.ksDeleteIterator();
  }
}

//-------------------------------------------------------------------------------
//
// ---
void WalkGroup( ksDocument2D& doc ) {
	reference  pNameGrp;
	int count =0;
  char buf[128];
  ksIterator iter( kompas.GetIterator() );
	if ( !iter.m_lpDispatch )
		return;
	if ( iter.ksCreateIterator( NAME_GROUP_OBJ, 0 ) ) { //        
    pNameGrp = iter.ksMoveIterator( "F" );
    if ( pNameGrp ){
      do  {
        doc.ksLightObj( pNameGrp, 1 );   // 
        count ++;
        ::sprintf(buf," = %d", count );
        kompas.ksMessage( buf );
        doc.ksLightObj( pNameGrp, 0 );   // 
      } while( (pNameGrp = iter.ksMoveIterator( "N" ))!=0 );
    }
		iter.ksDeleteIterator();
  }
  //       
  doc.ksNewGroup(0);
    doc.ksCircle( 30, 30, 20, 1 );
    doc.ksCircle( 30, 30, 10, 1 );
    doc.ksHatch( 0, 45, 2, 0, 0, 0);
      doc.ksCircle( 30, 30, 20, 1 );
      doc.ksCircle( 30, 30, 10, 1 );
    doc.ksEndObj();
  doc.ksEndGroup();

  //    
  count = 0;
  reference pWorkGrp;
	if ( iter.ksCreateIterator( WORK_GROUP_OBJ, 0 ) ) { //        
    pWorkGrp = iter.ksMoveIterator( "F" );
    if ( pWorkGrp ){
      do {
        doc.ksLightObj( pWorkGrp, 1 );   // 
        count ++;
        ::sprintf(buf," = %d", count );
        kompas.ksMessage( buf );
        doc.ksLightObj( pWorkGrp, 0 );   // 
      } while ( ( pWorkGrp = iter.ksMoveIterator( "N" ) )!=0  );
    }
		iter.ksDeleteIterator();
  }
}

//-------------------------------------------------------------------------------
//
// ---
void WalkLayer( ksDocument2D& doc ) {
  char buf[128];
//  K6   CreateDocument( doc ); //  

  // 5 
  for( int i = 0; i < 5; i++ ) {
    doc.ksLayer( i );
    doc.ksCircle( 30, 30, 5 + i * 10, 1 );
  }

  reference pLayer;
  ksIterator iter( kompas.GetIterator() );
	if ( !iter.m_lpDispatch )
		return;
	if ( iter.ksCreateIterator( LAYER_OBJ, 0 ) ) { //   
    pLayer = iter.ksMoveIterator( "F" );
		int count = 0;
    if ( pLayer ){
      do {
        doc.ksLightObj( pLayer, 1 );   // 
        ::sprintf(buf," = %d", count );
        kompas.ksMessage( buf );
        doc.ksLightObj( pLayer, 0 );   // 
        count ++;
      } while( (pLayer = iter.ksMoveIterator ( "N" )) !=0 );
    }
		iter.ksDeleteIterator();
  }
}

//-------------------------------------------------------------------------------
//
// ---
void WalkFromGroup( ksDocument2D& doc ) {
  char buf[128];
  doc.ksMtr( 20, 10, 0, 1, 1 );
  reference pGrp = doc.ksNewGroup(0);
    doc.ksLineSeg( 10, 50, 50, 50, 1 );
    doc.ksLineSeg( 10, 10, 50, 10, 1 );
    doc.ksLineSeg( 10, 10, 10, 50, 1 );
    doc.ksLineSeg( 50, 10, 50, 50, 1 );
    doc.ksCircle( 30, 30, 20, 1 );
    doc.ksCircle( 30, 30, 10, 1 );
    doc.ksHatch( 0, 45, 2, 0, 0, 0);
      doc.ksCircle( 30, 30, 20, 1 );
      doc.ksCircle( 30, 30, 10, 1 );
    doc.ksEndObj();
  doc.ksEndGroup();
  doc.ksDeleteMtr();
  
  reference obj;
  ksIterator iter( kompas.GetIterator() );
	if ( !iter.m_lpDispatch )
		return;
	if ( iter.ksCreateIterator( ALL_OBJ, pGrp ) ) { //     
    obj = iter.ksMoveIterator( "F" );
		int count = 0;
    if ( doc.ksExistObj(obj) ){
      do {
        doc.ksLightObj( obj, 1 );
        count ++;
        ::sprintf(buf," = %d", count );
        kompas.ksMessage( buf );
        doc.ksLightObj( obj, 0 );
      } while( doc.ksExistObj ( obj = iter.ksMoveIterator( "N" )) );
    }
		iter.ksDeleteIterator();
  }
}

//-------------------------------------------------------------------------------
//      
// ---
void WalkFromDocWithAttr( ksDocument2D& doc ) {
  
	ksAttributeObject attr( kompas.GetAttributeObject() );
	if ( !attr.m_lpDispatch )
		return;

  reference pObj, pAttr;
  char buf[128];
  ksIterator iter( kompas.GetIterator() );
	if ( !iter.m_lpDispatch )
		return;
	if ( iter.ksCreateAttrIterator( 0, 10, 0,0,0,0 ) ) { //       10
    pAttr = iter.ksMoveAttrIterator( "F", &pObj );
		int count = 0;
    long rowsCount, columnsCount;
    if ( pAttr && doc.ksExistObj(pObj) ){
      do {
        doc.ksLightObj( pObj, 1 );
        count ++;
        //    
        if(attr.ksGetAttrTabInfo( pAttr, &rowsCount, &columnsCount )) {
          ::sprintf(buf," = %d rowsCount=%d columnsCount=%d", count,rowsCount,columnsCount );
          kompas.ksMessage( buf );
				}
        else
          kompas.ksMessageBoxResult();  //   -     

        doc.ksLightObj( pObj, 0 );

        pAttr = iter.ksMoveAttrIterator( "N", &pObj );
			} while( pAttr && doc.ksExistObj(pObj));
		}
		iter.ksDeleteIterator();
  }
}

//-------------------------------------------------------------------------------
//   
// ---
void WalkFromObjWithAttr( ksDocument2D& doc ) {
  double x, y;
  int j;
  reference pObj;
 	ksRequestInfo info( kompas.GetParamStruct(ko_RequestInfo) );
	ksAttributeObject attr( kompas.GetAttributeObject() );
	if ( !info.m_lpDispatch || !attr.m_lpDispatch ) 
		return;

	info.Init();

	info.SetPrompt(" ");
  do {
    j = doc.ksCursor( info, &x ,&y, 0 );
    if ( j ) {
      if( doc.ksExistObj(pObj = doc.ksFindObj( x, y, 1e6 )) ){
        int count = 0;
        char buf[128];
        long rowsCount, columnsCount;
        doc.ksLightObj( pObj, 1 );

        reference pAttr;
        ksIterator iter( kompas.GetIterator() );
				if ( !iter.m_lpDispatch )
					return;
				if ( iter.ksCreateAttrIterator( pObj, 10, 0,0,0,0 ) ) { //       10
          pAttr = iter.ksMoveAttrIterator( "F", 0 );
          if ( pAttr ) {//doc.ksExistObj(pAttr) ){
            do {
              count ++;
              //    
              if( attr.ksGetAttrTabInfo ( pAttr, &rowsCount, &columnsCount ) ) {
                ::sprintf(buf," = %d rowsCount=%d columnsCount=%d", count,rowsCount,columnsCount );
                kompas.ksMessage( buf );
							}
              else
                kompas.ksMessageBoxResult();  //   -     
              pAttr = iter.ksMoveAttrIterator( "N", 0 );
						} while( pAttr );
					}
       		iter.ksDeleteIterator();
        }
        doc.ksLightObj( pObj, 0 );
      }
    }
  } while(j);
}
