Files
EgtInterface/Interface.cpp
T
Dario Sassi 2c4de5c4f4 EgtInterface 1.5h1 :
- primo commit.
2014-08-31 10:24:17 +00:00

503 lines
14 KiB
C++

// Interface.cpp: definisce le funzioni esportate per l'applicazione DLL.
//
#include "stdafx.h"
#include "/EgtDev/Include/EInAPI.h"
#include "/EgtDev/Include/EInDllMain.h"
#include "/EgtDev/Include/EGkDllMain.h"
#include "/EgtDev/Include/ENkDllMain.h"
#include "/EgtDev/Include/EGnDllMain.h"
#include "/EgtDev/Include/EExDllMain.h"
#include "/EgtDev/Include/EGrDllMain.h"
#include "/EgtDev/Include/EGkGeomDB.h"
#include "/EgtDev/Include/EExImportStl.h"
#include "/EgtDev/Include/EExImportDxf.h"
#include "/EgtDev/Include/EExExportStl.h"
#include "/EgtDev/Include/EExExportDxf.h"
#include "/EgtDev/Include/EGrScene.h"
#include "/EgtDev/Include/EGnStringUtils.h"
#include "/EgtDev/Include/EGnStringConverter.h"
#include "/EgtDev/Include/EgtLogger.h"
#include "/EgtDev/Include/EgtPointerOwner.h"
#include <fstream>
using namespace std ;
using namespace egtlogger ;
//-----------------------------------------------------------------------------
static Logger* s_pLogGen = nullptr ;
static IGeomDB* s_pGeomDB = nullptr ;
static HWND s_hWnd = nullptr ;
static IEGrScene* s_pScene = nullptr ;
static Color s_colDef = BLACK ;
//-----------------------------------------------------------------------------
int
__stdcall EgtSetLog( int nDebug, const wchar_t* sLogFile)
{
// creo il logger generale
s_pLogGen = new Logger( ( nDebug > 0 ? LL_DEBUG : LL_INFO), "EgtInterface") ;
if ( s_pLogGen == nullptr)
return 0 ;
// assegno il file
s_pLogGen->AddOutputStream( new ofstream( sLogFile), true) ;
// lo passo alle DLL
SetEGnLogger( s_pLogGen) ;
SetENkLogger( s_pLogGen) ;
SetEGkLogger( s_pLogGen) ;
SetEExLogger( s_pLogGen) ;
SetEGrLogger( s_pLogGen) ;
return 1 ;
}
//-----------------------------------------------------------------------------
int
__stdcall EgtSetFont( const wchar_t* sNfeFontDir, const wchar_t* sDefaultFont)
{
// inizializzazioni gestore font Nfe
InitFontManager( LPSTR( WtoA( sNfeFontDir)), LPSTR( WtoA( sDefaultFont))) ;
return 1 ;
}
//-----------------------------------------------------------------------------
int
__stdcall EgtInit( HWND hWnd, int nDriver, int b2Buff, int nColorBits, int nDepthBits)
{
// inizio programma
LOG_DATETIME( s_pLogGen, " Init")
// versione dell'interfaccia
LOG_INFO( s_pLogGen, GetEInVersion())
// versione delle librerie
LOG_INFO( s_pLogGen, GetEGnVersion())
LOG_INFO( s_pLogGen, GetENkVersion())
LOG_INFO( s_pLogGen, GetEGkVersion())
LOG_INFO( s_pLogGen, GetEExVersion())
LOG_INFO( s_pLogGen, GetEGrVersion())
// inizializzazioni DB geometrico
s_pGeomDB = CreateGeomDB() ;
if ( s_pGeomDB == nullptr)
return 0 ;
s_pGeomDB->Init() ;
s_pGeomDB->SetDefaultMaterial( s_colDef) ;
// inizializzazione scena OpenGL
s_pScene = CreateEGrScene() ;
if ( s_pScene == nullptr)
return 0 ;
s_hWnd = hWnd ;
HDC hdc = GetDC( hWnd) ;
if ( ! s_pScene->CreateContext( hdc, nDriver, ( b2Buff != 0), nColorBits, nDepthBits))
return 0 ;
s_pScene->SetBackground( WHITE, WHITE) ;
s_pScene->SetShowMode( SM_SHADING) ;
s_pScene->SetShowCurveDirection( false) ;
s_pScene->SetCamera( CT_TOP) ;
s_pScene->ZoomAll() ;
s_pScene->SetWinRectAttribs( true, BLACK) ;
s_pScene->SetMark( YELLOW) ;
s_pScene->Init( s_pGeomDB) ;
// log con info sulla scena
string sSceneInfo = s_pScene->GetOpenGLInfo() + '\n' +
s_pScene->GetGLSLInfo() + '\n' +
s_pScene->GetPixelFormatInfo() ;
LOG_INFO( s_pLogGen, sSceneInfo.c_str())
return 1 ;
}
//-----------------------------------------------------------------------------
int
__stdcall EgtExit( void)
{
// fine programma
LOG_DATETIME( s_pLogGen, " Exit")
// scena
if ( s_pScene != nullptr) {
s_pScene->Destroy() ;
delete s_pScene ;
s_pScene = nullptr ;
}
s_hWnd = nullptr ;
// geomDB
if ( s_pGeomDB != nullptr) {
delete s_pGeomDB ;
s_pGeomDB = nullptr ;
}
// logger
if ( s_pLogGen != nullptr) {
delete s_pLogGen ;
s_pLogGen = nullptr ;
}
return 1 ;
}
//-----------------------------------------------------------------------------
int
__stdcall EgtSetDefaultMaterial( int nRed, int nGreen, int nBlue)
{
// verifico GeomDB
if ( s_pGeomDB == nullptr) {
LOG_ERROR( s_pLogGen, "GeomDB invalid (EgtSetDefaultMaterial)")
return 0 ;
}
// imposto il materiale di default
s_colDef.Set( nRed, nGreen, nBlue) ;
s_pGeomDB->SetDefaultMaterial( s_colDef) ;
return 1 ;
}
//-----------------------------------------------------------------------------
int
__stdcall EgtSetBackground( int nTopRed, int nTopGreen, int nTopBlue,
int nBottomRed, int nBottomGreen, int nBottomBlue)
{
// verifico GeomDB
if ( s_pGeomDB == nullptr) {
LOG_ERROR( s_pLogGen, "GeomDB invalid (EgtSetDefaultMaterial)")
return 0 ;
}
// imposto lo sfondo
s_pScene->SetBackground( Color( nTopRed, nTopGreen, nTopBlue), Color( nBottomRed, nBottomGreen, nBottomBlue)) ;
return 1 ;
}
//-----------------------------------------------------------------------------
int
__stdcall EgtNewFile( void)
{
// verifico GeomDB
if ( s_pGeomDB == nullptr) {
LOG_ERROR( s_pLogGen, "GeomDB invalid (EgtNewFile)")
return 0 ;
}
// reinizializzazione (con pulizia) del DB geometrico
s_pGeomDB->Init() ;
s_pGeomDB->SetDefaultMaterial( s_colDef) ;
return 1 ;
}
//-----------------------------------------------------------------------------
int
__stdcall EgtOpenFile( const wchar_t* sFilePath)
{
// reinizializzazione (con pulizia) del DB geometrico
if ( ! EgtNewFile())
return 0 ;
// carico il file
if ( s_pGeomDB->Load( LPSTR( WtoA( sFilePath))))
return 1 ;
else
return 0 ;
}
//-----------------------------------------------------------------------------
int
__stdcall EgtTestImportExt( const wchar_t* sFilePath)
{
// divido in nome e direttorio
string sFileDir, sFileName ;
SplitLast( LPSTR( WtoA( sFilePath)), "\\", sFileDir, sFileName) ;
// recupero l'estensione
string sFileTitle, sFileExt ;
SplitLast( sFileName, ".", sFileTitle, sFileExt) ;
ToUpper( sFileExt) ;
if ( sFileExt == "DXF")
return 1 ;
else if ( sFileExt == "STL")
return 2 ;
else {
// emetto info
string sInfo = "File type (" + sFileExt + ") not recognized" ;
LOG_INFO( s_pLogGen, sInfo.c_str())
return 0 ;
}
}
//-----------------------------------------------------------------------------
int
__stdcall EgtImportDxf( const wchar_t* sFilePath)
{
// verifico GeomDB
if ( s_pGeomDB == nullptr) {
LOG_ERROR( s_pLogGen, "GeomDB invalid (ImportFile)")
return 0 ;
}
// importo il file DXF
// aggiungo un gruppo pezzo
int nPartId = s_pGeomDB->AddGroup( GDB_ID_NULL, GDB_ID_ROOT, Frame3d()) ;
// preparo l'importatore
PtrOwner<IImportDxf> pImpDxf( CreateImportDxf()) ;
if ( IsNull( pImpDxf)) {
LOG_ERROR( s_pLogGen, "Error : CreateImportDxf")
return 0 ;
}
// eseguo l'importazione
if ( pImpDxf->Import( LPSTR( WtoA( sFilePath)), s_pGeomDB, nPartId))
return 1 ;
else
return 0 ;
}
//-----------------------------------------------------------------------------
int
__stdcall EgtImportStl( const wchar_t* sFilePath)
{
// verifico GeomDB
if ( s_pGeomDB == nullptr) {
LOG_ERROR( s_pLogGen, "GeomDB invalid (EgtImportStl)")
return 0 ;
}
// importo il file STL
// aggiungo un gruppo pezzo e un gruppo layer
int nPartId = s_pGeomDB->AddGroup( GDB_ID_NULL, GDB_ID_ROOT, Frame3d()) ;
int nLayerId = s_pGeomDB->AddGroup( GDB_ID_NULL, nPartId, Frame3d()) ;
// preparo l'importatore
PtrOwner<IImportStl> pImpStl( CreateImportStl()) ;
if ( IsNull( pImpStl)) {
LOG_ERROR( s_pLogGen, "Error : CreateImportStl")
return 0 ;
}
// eseguo l'importazione
if ( pImpStl->Import( LPSTR( WtoA( sFilePath)), s_pGeomDB, nLayerId))
return 1 ;
else
return 0 ;
}
//-----------------------------------------------------------------------------
int
__stdcall EgtDraw( void)
{
// verifico Scena
if ( s_pScene == nullptr) {
LOG_ERROR( s_pLogGen, "Scene invalid (EgtDraw)")
return 0 ;
}
// eseguo disegno
s_pScene->Draw() ;
// valido la finestra disegnata
ValidateRgn( s_hWnd, NULL) ;
return 1 ;
}
//-----------------------------------------------------------------------------
int
__stdcall EgtResize( int nW, int nH)
{
// verifico Scena
if ( s_pScene == nullptr) {
LOG_ERROR( s_pLogGen, "Scene invalid (EgtResize)")
return 0 ;
}
s_pScene->Resize( nW, nH) ;
return 1 ;
}
//-----------------------------------------------------------------------------
int
__stdcall EgtSetShowMode( int nShowMode)
{
// verifico Scena
if ( s_pScene == nullptr) {
LOG_ERROR( s_pLogGen, "Scene invalid (EgtSetShowMode)")
return 0 ;
}
if ( nShowMode >= SM_WIREFRAME && nShowMode <= SM_SHADING) {
s_pScene->SetShowMode( nShowMode) ;
return 1 ;
}
else
return 0 ;
}
//-----------------------------------------------------------------------------
int
__stdcall EgtSetShowCurveDirection( int nShow)
{
// verifico Scena
if ( s_pScene == nullptr) {
LOG_ERROR( s_pLogGen, "Scene invalid (EgtZoom)")
return 0 ;
}
s_pScene->SetShowCurveDirection( nShow != 0) ;
return 1 ;
}
//-----------------------------------------------------------------------------
int
__stdcall EgtZoom( int nZoom)
{
// verifico Scena
if ( s_pScene == nullptr) {
LOG_ERROR( s_pLogGen, "Scene invalid (EgtZoom)")
return 0 ;
}
const double COEFF_IN = 0.9 ;
const double COEFF_OUT = 1 / COEFF_IN ;
switch ( nZoom) {
case 1 :
s_pScene->ZoomAll() ;
return 1 ;
break ;
case 2 :
s_pScene->ZoomChange( COEFF_IN) ;
return 1 ;
break ;
case 3 :
s_pScene->ZoomChange( COEFF_OUT) ;
return 1 ;
break ;
}
return 0 ;
}
//-----------------------------------------------------------------------------
int
__stdcall EgtZoomOnPoint( int nWinX, int nWinY, double dCoeff)
{
// verifico Scena
if ( s_pScene == nullptr) {
LOG_ERROR( s_pLogGen, "Scene invalid (EgtZoomOnPoint)")
return 0 ;
}
s_pScene->ZoomOnPoint( Point3d( nWinX, nWinY), dCoeff) ;
return 1 ;
}
//-----------------------------------------------------------------------------
int
__stdcall EgtSetWinRect( int nPrevX, int nPrevY, int nCurrX, int nCurrY)
{
// verifico Scena
if ( s_pScene == nullptr) {
LOG_ERROR( s_pLogGen, "Scene invalid (EgtSetWinRect)")
return 0 ;
}
// eseguo panoramica
s_pScene->SetWinRect( Point3d( nPrevX, nPrevY), Point3d( nCurrX, nCurrY)) ;
return 1 ;
}
//-----------------------------------------------------------------------------
int
__stdcall EgtResetWinRect( void)
{
// verifico Scena
if ( s_pScene == nullptr) {
LOG_ERROR( s_pLogGen, "Scene invalid (EgtResetWinRect)")
return 0 ;
}
// eseguo panoramica
s_pScene->ResetWinRect() ;
return 1 ;
}
//-----------------------------------------------------------------------------
int
__stdcall EgtZoomWin( int nPrevX, int nPrevY, int nCurrX, int nCurrY)
{
// verifico Scena
if ( s_pScene == nullptr) {
LOG_ERROR( s_pLogGen, "Scene invalid (EgtZoomWin)")
return 0 ;
}
// eseguo panoramica
s_pScene->ZoomWin( Point3d( nPrevX, nPrevY), Point3d( nCurrX, nCurrY)) ;
return 1 ;
}
//-----------------------------------------------------------------------------
int
__stdcall EgtSetView( int nView)
{
// verifico Scena
if ( s_pScene == nullptr) {
LOG_ERROR( s_pLogGen, "Scene invalid (EgtSetView)")
return 0 ;
}
if ( nView >= CT_TOP && nView <= CT_ISO_NW) {
s_pScene->SetCamera( nView) ;
return 1 ;
}
else
return 0 ;
}
//-----------------------------------------------------------------------------
int
__stdcall EgtPanCamera( int nPrevX, int nPrevY, int nCurrX, int nCurrY)
{
// verifico Scena
if ( s_pScene == nullptr) {
LOG_ERROR( s_pLogGen, "Scene invalid (EgtPanCamera)")
return 0 ;
}
// eseguo panoramica
s_pScene->PanCamera( Point3d( nPrevX, nPrevY), Point3d( nCurrX, nCurrY)) ;
return 1 ;
}
//-----------------------------------------------------------------------------
int
__stdcall EgtRotateCamera( int nPrevX, int nPrevY, int nCurrX, int nCurrY)
{
// verifico Scena
if ( s_pScene == nullptr) {
LOG_ERROR( s_pLogGen, "Scene invalid (EgtRotateCamera)")
return 0 ;
}
// eseguo panoramica
s_pScene->RotateCamera( Point3d( nPrevX, nPrevY), Point3d( nCurrX, nCurrY)) ;
return 1 ;
}
//-----------------------------------------------------------------------------
int
__stdcall EgtGetCameraDir( int* pnDir)
{
// verifico Scena
if ( s_pScene == nullptr) {
LOG_ERROR( s_pLogGen, "Scene invalid (EgtGetCameraDir)")
return 0 ;
}
// recupero direzione di vista
if ( pnDir != nullptr)
*pnDir = s_pScene->GetCameraDir() ;
return 1 ;
}
//-----------------------------------------------------------------------------
int
__stdcall EgtUnProject( int nWinX, int nWinY, double ptP[3])
{
// verifico Scena
if ( s_pScene == nullptr) {
LOG_ERROR( s_pLogGen, "Scene invalid (EgtGetCameraDir)")
return 0 ;
}
// eseguo l'inverso della proiezione (considero Z punto su centro)
Point3d ptView( nWinX, nWinY, s_pScene->GetProjectedCenter().z) ;
Point3d ptWorld ;
if ( ! s_pScene->UnProject( ptView, ptWorld))
return 0 ;
ptP[0] = ptWorld.x ;
ptP[1] = ptWorld.y ;
ptP[2] = ptWorld.z ;
return 1 ;
}