Files
Dario Sassi f54f1f8969 EgtExchange 2.6l1 :
- aggiunta importazione formato OFF (Object File Format).
2024-11-29 18:18:28 +01:00

297 lines
8.3 KiB
C++

//----------------------------------------------------------------------------
// EgalTech 2014-2014
//----------------------------------------------------------------------------
// File : ImportDxf.cpp Data : 16.04.14 Versione : 1.5d4
// Contenuto : Implementazione della classe per l'importazione di DXF.
//
//
//
// Modifiche : 16.04.14 DS Creazione modulo.
//
//
//----------------------------------------------------------------------------
//--------------------------- Include ----------------------------------------
#include "stdafx.h"
#include "ImportDxf.h"
#include "DxfConst.h"
#include "DllMain.h"
#include "/EgtDev/Include/EExDllMain.h"
#include "/EgtDev/Include/EGkGdbIterator.h"
#include "/EgtDev/Include/EGnStringUtils.h"
using namespace std ;
//----------------------------------------------------------------------------
IImportDxf*
CreateImportDxf( void)
{
// verifico la chiave e le opzioni
if ( ! VerifyKey( KEYOPT_EEX_INPBASE))
return nullptr ;
// creo l'oggetto
return static_cast<IImportDxf*> ( new(nothrow) ImportDxf) ;
}
//----------------------------------------------------------------------------
ImportDxf::ImportDxf( void)
{
m_pGDB = nullptr ;
m_nIdGroup = GDB_ID_NULL ;
m_dScaleFactor = 1 ;
m_bUnGet = false ;
m_nCurrCode = CODE_NULL ;
m_nIdBlocksParent = GDB_ID_NULL ;
m_nIdCurrBlock = GDB_ID_NULL ;
m_nCodePage = 1252 ;
}
//----------------------------------------------------------------------------
bool
ImportDxf::Import( const string& sFile, IGeomDB* pGDB, int nIdGroup, double dScaleFactor)
{
// inizializzo lo scanner
if ( ! m_theScanner.Init( sFile, nullptr, false)) {
LOG_ERROR( GetEExLogger(), "ImportDxf : Error on Init")
return false ;
}
// verifico il DB geometrico
if ( pGDB == nullptr) {
LOG_ERROR( GetEExLogger(), "ImportDxf : Error on GeomDB")
return false ;
}
m_pGDB = pGDB ;
// verifico l'Id di gruppo
if ( ! m_pGDB->ExistsObj( nIdGroup)) {
LOG_ERROR( GetEExLogger(), "ImportDxf : Error on IdGroup")
return false ;
}
m_nIdGroup = nIdGroup ;
// ricavo il riferimento del gruppo
if ( ! m_pGDB->GetGroupGlobFrame( m_nIdGroup, m_frGroup)) {
LOG_ERROR( GetEExLogger(), "ImportDxf : Error on Group Frame3d")
return false ;
}
// verifico il fattore di scala
if ( dScaleFactor < EPS_SMALL) {
LOG_ERROR( GetEExLogger(), "ImportDxf : Error on ScaleFactor too small (minimum 0.001).")
return false ;
}
m_dScaleFactor = dScaleFactor ;
// creo il gruppo in cui parcheggiare i blocchi
if ( ! CreateBlocksParent())
return false ;
// imposto la codepage di default ( 1252 = ANSI Latin 1)
m_nCodePage = 1252 ;
// ciclo di lettura
bool bOk = true ;
bool bFileEnd = false ;
string sSectName ;
do {
// leggo intestazione sezione, per avere il tipo
bool bOkLoc = GetSectionName( sSectName, bFileEnd) ;
if ( bOkLoc && ! bFileEnd) {
// se sezione header
if ( sSectName == "HEADER") {
if ( ! ReadHeader( bFileEnd))
bOkLoc = false ;
}
// se sezione tavole
else if ( sSectName == "TABLES") {
if ( ! ReadTables( bFileEnd))
bOk = false ;
}
// se sezione blocchi
else if ( sSectName == "BLOCKS") {
if ( ! ReadBlocks( bFileEnd))
bOk = false ;
}
// se sezione entità
else if ( sSectName == "ENTITIES") {
if ( ! ReadEntities( bFileEnd))
bOk = false ;
}
// altrimenti
else {
if ( ! SkipSection( bFileEnd))
bOkLoc = false ;
}
}
// gestisco eventuale segnalazione errore
if ( ! bOkLoc) {
bOk = false ;
string sOut = "ImportDxf : Error on line " + ToString( m_theScanner.GetCurrLineNbr()) ;
LOG_ERROR( GetEExLogger(), sOut.c_str())
}
} while ( ! bFileEnd) ;
// elimino gruppo dei blocchi
EraseBlocksParent() ;
// elimino layer rimasti vuoti
EraseEmptyLayers() ;
// pulisco map dei layer e dei blocchi
m_nmLayer.clear() ;
m_smStyle.clear() ;
m_nmBlock.clear() ;
return bOk ;
}
//----------------------------------------------------------------------------
int
ImportDxf::ReadNextItem( void)
{
// se unget attivo
if ( m_bUnGet) {
m_bUnGet = false ;
return m_nCurrCode ;
}
// pulisco Item corrente
m_nCurrCode = CODE_NULL ;
m_sCurrItem.clear() ;
// recupero la prima linea
string sCode ;
if ( ! m_theScanner.GetLine( sCode))
return m_nCurrCode ;
// recupero la seconda linea
if ( ! m_theScanner.GetLine( m_sCurrItem))
return m_nCurrCode ;
// interpreto il codice
if ( ! FromString( sCode, m_nCurrCode))
m_nCurrCode = CODE_NULL ;
// se linea di commento, la ignoro e rilancio lettura item
if ( m_nCurrCode == 999)
ReadNextItem() ;
return m_nCurrCode ;
}
//----------------------------------------------------------------------------
void
ImportDxf::UngetItem( void)
{
m_bUnGet = true ;
}
//----------------------------------------------------------------------------
bool
ImportDxf::GetSectionName( string& sSectName, bool& bFileEnd)
{
// inizializzo
sSectName.clear() ;
bFileEnd = false ;
// leggo il primo Item (mi aspetto 0 -> SECTION)
int nCode = ReadNextItem() ;
if ( nCode == CODE_NULL) {
bFileEnd = true ;
return true ;
}
if ( nCode == 0 && m_sCurrItem == "EOF") {
bFileEnd = true ;
return true ;
}
if ( nCode != 0 || m_sCurrItem != "SECTION")
return false ;
// leggo il secondo Item (mi aspetto 0 -> NomeSezione)
nCode = ReadNextItem() ;
if ( nCode == CODE_NULL) {
bFileEnd = true ;
return false ;
}
if ( nCode != 2)
return false ;
sSectName = m_sCurrItem ;
return true ;
}
//----------------------------------------------------------------------------
bool
ImportDxf::SkipSection( bool& bFileEnd)
{
bFileEnd = false ;
int nCode = CODE_NULL ;
do {
// leggo un Item ( cerco 0 -> ENDSEC)
nCode = ReadNextItem() ;
if ( nCode == CODE_NULL) {
bFileEnd = true ;
return false ;
}
} while ( nCode != 0 || m_sCurrItem != "ENDSEC") ;
return true ;
}
//----------------------------------------------------------------------------
bool
ImportDxf::CreateBlocksParent( void)
{
m_nIdBlocksParent = m_pGDB->AddGroup( GDB_ID_NULL, GDB_ID_ROOT, GLOB_FRM) ;
if ( m_nIdBlocksParent == GDB_ID_NULL)
return false ;
m_pGDB->SetName( m_nIdBlocksParent, "BLOCKS") ;
m_pGDB->SetLevel( m_nIdBlocksParent, GDB_LV_TEMP) ;
m_pGDB->SetStatus( m_nIdBlocksParent, GDB_ST_OFF) ;
return true ;
}
//----------------------------------------------------------------------------
bool
ImportDxf::EraseBlocksParent( void)
{
m_pGDB->Erase( m_nIdBlocksParent) ;
return true ;
}
//----------------------------------------------------------------------------
bool
ImportDxf::EraseEmptyLayers( void)
{
// creo un iteratore
IGdbIterator* pIter = CreateGdbIterator( m_pGDB) ;
if ( pIter == nullptr)
return false ;
// ciclo sui figli del gruppo di inserimento
bool bFound = pIter->GoToFirstInGroup( m_nIdGroup) ;
while ( bFound) {
// se inserito per DXF e non ha figli
string sInfo ;
if ( pIter->GetInfo( DXF_INFO, sInfo) && sInfo == "1" &&
pIter->GetGroupObjs() == 0)
bFound = pIter->EraseAndGoToNext() ;
else {
pIter->RemoveInfo( DXF_INFO) ;
bFound = pIter->GoToNext() ;
}
}
// elimino l'iteratore
delete pIter ;
return true ;
}
//----------------------------------------------------------------------------
int
ImportDxf::GetGroupId( const string& sName)
{
if ( m_nIdCurrBlock != GDB_ID_NULL)
return m_nIdCurrBlock ;
else
return GetLayerId( sName) ;
}
//----------------------------------------------------------------------------
bool
ImportDxf::BlockReading( void)
{
return ( m_nIdCurrBlock != GDB_ID_NULL) ;
}