Files
DxfConverter/DxfConverter.cpp
Dario Sassi 2acfbafb48 DxfConverter 1.6w4 :
- primo commit.
2016-11-21 09:11:58 +00:00

180 lines
6.2 KiB
C++

//----------------------------------------------------------------------------
// EgalTech 2016-2016
//----------------------------------------------------------------------------
// File : DxfConverter.cpp Data : 20.11.16 Versione : 1.6w4
// Contenuto : Programma per conversione file DXF.
//
//
//
// Modifiche : 20.11.16 DS Creazione modulo.
//
//
//----------------------------------------------------------------------------
//--------------------------- Include ----------------------------------------
#include "stdafx.h"
#include "/EgtDev/Include/EExDllMain.h"
#include "/EgtDev/Include/EGkDllMain.h"
#include "/EgtDev/Include/ENkDllMain.h"
#include "/EgtDev/Include/EGnDllMain.h"
#include "/EgtDev/Include/EExImportDxf.h"
#include "/EgtDev/Include/EExExportDxf.h"
#include "/EgtDev/Include/EGkGeomDB.h"
#include "/EgtDev/Include/EGkCurveComposite.h"
#include "/EgtDev/Include/EGnGetModuleVer.h"
#include "/EgtDev/Include/EGnStringUtils.h"
#include "/EgtDev/Include/EgtStringConverter.h"
#include "/EgtDev/Include/EGnFileUtils.h"
#include "/EgtDev/Include/EgtLogger.h"
#include "/EgtDev/Include/EgtPointerOwner.h"
#include <fstream>
//--------------------------- Define -----------------------------------------
#if defined( NDEBUG)
#define STR_EXE "DxfConverterR32.exe"
#else
#define STR_EXE "DxfConverterD32.exe"
#endif
//--------------------------- Consts -----------------------------------------
const double LIN_TOL_STD = 0.01 ;
const double LIN_TOL_MIN = 0.001 ;
const double ANG_TOL_STD_DEG = 30 ;
using namespace std ;
using namespace egtlogger ;
//----------------------------------------------------------------------------
int
wmain( int argc, wchar_t* argv[])
{
// Recupero la versione
string sVer ;
GetModuleVersion( NULL, sVer) ;
// Controllo presenza dei parametri
if ( argc < 3) {
wcerr << "Usage: " << argv[0] << " <input_file> <output_file> <lin_toler>, where:" << endl ;
wcerr << " <input_file> is the DXF file to be read" << endl ;
wcerr << " <output_file> is the DXF file to be written" << endl ;
wcerr << " <lin_toler> is the approximation linear tolerance." << endl ;
wcerr << STR_EXE " v." << sVer.c_str() << endl ;
return 1 ;
}
// Interpretazione dei parametri
string sInFile = wstrztoA( argv[1]) ;
string sOutFile = wstrztoA( argv[2]) ;
double dLinTol = LIN_TOL_STD ;
if ( argc >= 4)
FromString( wstrztoA( argv[3]), dLinTol) ;
dLinTol = max( dLinTol, LIN_TOL_MIN) ;
// Imposto il direttorio dell'eseguibile come direttorio corrente
string sExeDir ;
GetModuleDirectory( NULL, sExeDir) ;
SetCurrentDirectory( sExeDir) ;
// Inizializzazioni del logger
string sLog = ChangeFileExtension( sInFile, ".log") ;
Logger logger( LL_INFO, STR_EXE) ;
logger.AddOutputStream( new(nothrow) ofstream( stringtoW( sLog)), true) ;
// Informazioni di log
LOG_DATETIME( &logger, "")
LOG_INFO( &logger, string( STR_EXE " v." + sVer).c_str())
// versione delle librerie
LOG_INFO( &logger, GetEGnVersion())
LOG_INFO( &logger, GetENkVersion())
LOG_INFO( &logger, GetEGkVersion())
LOG_INFO( &logger, GetEExVersion())
// parametri del programma
string sPar1 = string( "Input file = ") + sInFile ;
LOG_INFO( &logger, sPar1.c_str())
string sPar2 = string( "Output file = ") + sOutFile ;
LOG_INFO( &logger, sPar2.c_str())
string sPar3 = string( "Linear tolerance = ") + ToString( dLinTol, 3) ;
LOG_INFO( &logger, sPar3.c_str())
// Passo logger alle librerie
SetEGnLogger( &logger) ;
SetENkLogger( &logger) ;
SetEGkLogger( &logger) ;
SetEExLogger( &logger) ;
// Imposto la chiave di protezione
SetEGkKey( "EGkBase") ;
SetEExKey( "EExBase") ;
// Salto inizializzazione del font manager
//InitFontManager( sNfeFontDir, sDefaultFont) ;
// Preparazione degli oggetti di gestione della conversione
PtrOwner<IGeomDB> pGdb( CreateGeomDB()) ;
if ( IsNull( pGdb)) {
LOG_ERROR( &logger, "Error in CreateGeomDB")
return 2 ;
}
PtrOwner<IImportDxf> pImpDxf( CreateImportDxf()) ;
if ( IsNull( pImpDxf)) {
LOG_ERROR( &logger, "Error in CreateImportDxf")
return 2 ;
}
PtrOwner<IExportDxf> pExpDxf( CreateExportDxf()) ;
if ( IsNull( pExpDxf)) {
LOG_ERROR( &logger, "Error in CreateExportDxf")
return 2 ;
}
// Eseguo importazione
int nPartId = pGdb->AddGroup( GDB_ID_NULL, GDB_ID_ROOT, Frame3d()) ;
if ( ! pImpDxf->Import( sInFile, pGdb, nPartId, 1.0)) {
LOG_ERROR( &logger, "Error in Dxf Import")
return 3 ;
}
// Eseguo conversione
bool bOk = true ;
int nLayId = pGdb->GetFirstInGroup( nPartId) ;
while ( nLayId != GDB_ID_NULL) {
int nId = pGdb->GetFirstInGroup( nLayId) ;
while ( nId != GDB_ID_NULL) {
ICurve* pCurve = GetCurve( pGdb->GetGeoObj( nId)) ;
if ( pCurve != nullptr) {
PtrOwner<ICurveComposite> pCC( CreateCurveComposite()) ;
bOk = bOk && ! IsNull( pCC) ;
PolyArc PA ;
bOk = bOk && pCurve->ApproxWithArcs( dLinTol, ANG_TOL_STD_DEG, PA) && pCC->FromPolyArc( PA) ;
// merge di archi identici di biarchi
bOk = bOk && pCC->MergeCurves( 0.5 * dLinTol, ANG_TOL_STD_DEG) ;
// copio estrusione e spessore
Vector3d vtExtr ;
if ( bOk && pCurve->GetExtrusion( vtExtr))
pCC->SetExtrusion( vtExtr) ;
double dThick ;
if ( bOk && pCurve->GetThickness( dThick))
pCC->SetThickness(dThick) ;
// sostituisco la vecchia curva con la nuova
bOk = bOk && pGdb->ReplaceGeoObj( nId, Release( pCC)) ;
}
nId = pGdb->GetFirstInGroup( nId) ;
}
nLayId = pGdb->GetNext( nLayId) ;
}
if ( ! bOk) {
LOG_ERROR( &logger, "Error in Geometric Conversion")
return 4 ;
}
// Eseguo esportazione
if ( ! pExpDxf->Export( pGdb, GDB_ID_ROOT, sOutFile)) {
LOG_ERROR( &logger, "Error in Dxf Export")
return 5 ;
}
LOG_INFO( &logger, "Conversion completed")
return 0 ;
}