////////////////////////////////////////////////////////////////////////////////
//
// Step4_API7_2D.cpp -   Visual C++
//
////////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <afxdllx.h>
#include "Step4_API7_2D.h"

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


//-------------------------------------------------------------------------------
//       DLL
// ---
static AFX_EXTENSION_MODULE Step4_API7_2DDLL = { NULL, NULL };
HINSTANCE g_hInstance = NULL;
ksAPI7::IApplicationPtr newKompasAPI( NULL );               

void OnProcessDetach();                            //  



//-------------------------------------------------------------------------------
//   
//    DLL
// ---
extern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
  g_hInstance = hInstance;

	if (dwReason == DLL_PROCESS_ATTACH)
	{
		TRACE0("STEP4_API7_2D.AWX Initializing!\n");

		AfxInitExtensionModule(Step4_API7_2DDLL, hInstance);

		new CDynLinkLibrary(Step4_API7_2DDLL);
	}
	else if (dwReason == DLL_PROCESS_DETACH)
	{
		TRACE0("STEP4_API7_2D.AWX Terminating!\n");
    OnProcessDetach();
		AfxTermExtensionModule(Step4_API7_2DDLL);
	}
	return 1;   // ok
}


//-------------------------------------------------------------------------------
//     API
// ---
void GetNewKompasAPI() 
{
  if ( !( ksAPI7::IApplication * )newKompasAPI ) 
  {
    CString filename;
    
    if( ::GetModuleFileName(NULL, filename.GetBuffer(255), 255) ) 
    {
      filename.ReleaseBuffer( 255 );
      CString libname;
      
      libname = LoadStr( IDR_API7 );    // kAPI7.dll
      filename.Replace( filename.Right(filename.GetLength() - (filename.ReverseFind('\\') + 1)), 
        libname );
      
      HINSTANCE hAppAuto = LoadLibrary( filename ); //  kAPI7.dll
      
      if(  hAppAuto ) 
      {
        //      KompasApplication  
        typedef LPDISPATCH ( WINAPI * FCreateKompasApplication )(); 
        
        FCreateKompasApplication pCreateKompasApplication = 
          (FCreateKompasApplication)GetProcAddress( hAppAuto, "CreateKompasApplication" );	
        
        if ( pCreateKompasApplication )
          newKompasAPI = IDispatchPtr ( pCreateKompasApplication(), false /*AddRef*/); //   Application
        FreeLibrary( hAppAuto );  
      }
    }
  }
}


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


//-------------------------------------------------------------------------------
//   
// ---
void PrintSample();			//       


//-------------------------------------------------------------------------------
//   
// ---
void WINAPI LIBRARYENTRY( unsigned int comm )
{
  switch ( comm )
  {
    case 1: 
    {
      PrintSample();
      break;
    }
  }
}


//-------------------------------------------------------------------------------
//   SAFEARRAY  long
// ---
_variant_t NewSafeArrayLong( long * dv, int count )
{
  _variant_t res;
  SAFEARRAYBOUND sabNewArray;
	
  sabNewArray.cElements = count;
  sabNewArray.lLbound = 0;
	
  SAFEARRAY * pSafe = ::SafeArrayCreate( VT_I4, 1, &sabNewArray );
	
  if( pSafe )
  {
    for ( long i = 0; i < count; i++ )
    {
      HRESULT  hr = ::SafeArrayPutElement( pSafe, (long*)&i, (void*)&dv[i] );
			
      if ( FAILED(hr) ) 
        break;
    }
		
    V_VT(&res) = VT_ARRAY | VT_I4;
    V_ARRAY(&res) = pSafe;
  }
  return res;
}


//-------------------------------------------------------------------------------
//    
// ---
void CalcScale( ksAPI7::IPrintJobPtr & print )
{
	if ( print )
	{
		double pageWidth, pageHeight;
		//     
		print->GetPageGabarites( &pageWidth, &pageHeight );

		long count = print->SheetsCount;

		for ( long i = 0; i < count; i++ )
		{
			//   
			ksAPI7::IPrintJob_SheetPtr sheet( print->GetSheet(i) );

			if ( sheet )
			{
				double w, h;
				//   
				sheet->GetGabarites( &w, &h );

				//   
				if ( pageWidth > w )
					sheet->Scale = 1;
				else
					sheet->Scale = pageWidth/w;

				//     
				sheet->X = pageWidth * i;
			}
		}
	}
}


//-------------------------------------------------------------------------------
//       
// ---
void PrintSample()
{
	GetNewKompasAPI();

	if ( newKompasAPI )
	{
		//     
		ksAPI7::IPrintJobPtr print( newKompasAPI->PrintJob );
		
		if ( print )
		{
			_variant_t sheets;
			sheets.vt = VT_EMPTY;
			
			//    ( )
			print->AddSheets( _T(".cdw"), sheets, ksAllSheets );
			print->AddSheets( _T(".cdw"), sheets, ksAllSheets );

			//   
			long index[4];
			index[0] = 0;
			index[1] = 1;
			index[2] = 4;
			index[3] = 5;
			_variant_t delIndex = NewSafeArrayLong( index, 4 );

			//  
			print->RemoveSheets( delIndex );

			//  
			CalcScale( print );

			//    
			print->ShowPreviewWindow();
		}
	}
}


//-------------------------------------------------------------------------------
//  
// ---
void OnProcessDetach()
{
  newKompasAPI = NULL;
}


//------------------------------------------------------------------------------
//    
// ---
CString LoadStr( int strID ) 
{
	TCHAR temp[_MAX_PATH];
	LoadString( Step4_API7_2DDLL.hModule, strID, temp, _MAX_PATH ); 
	return temp;
}


