//---------------------------------------------------------------------------- // EgalTech 2014-2015 //---------------------------------------------------------------------------- // File : EXE_GeomDB.cpp Data : 04.05.15 Versione : 1.6e1 // Contenuto : Funzioni DB geometrico per EXE. // // // // Modifiche : 01.09.14 DS Creazione modulo. // // //---------------------------------------------------------------------------- //--------------------------- Include ---------------------------------------- #include "stdafx.h" #include "EXE.h" #include "EXE_Macro.h" #include "AuxTools.h" #include "/EgtDev/Include/EXeExecutor.h" #include "/EgtDev/Include/EGkStringUtils3d.h" #include "/EgtDev/Include/EGnFileUtils.h" #include "/EgtDev/Include/EgtStringConverter.h" #include "/EgtDev/Include/EgtPointerOwner.h" using namespace std ; //----------------------------------------------------------------------------- int ExeInitContext( void) { // creo e recupero un contesto libero int nGseCtx = CreateGseContext() ; if ( nGseCtx == 0) { ResetCurrGseContext() ; LOG_ERROR( GetLogger(), "Error in CreateGseContext (ExeInitContext)") return 0 ; } GseContext* pGseCtx = GetGseContext( nGseCtx) ; // inizializzazioni DB geometrico PtrOwner pGeomDB( CreateGeomDB()) ; VERIFY_NULL( Get( pGeomDB), "Error in CreateGeomDB", nGseCtx) // inserisco il GeomDB nel contesto pGseCtx->m_pGeomDB = Release( pGeomDB) ; pGseCtx->m_pGeomDB->Init() ; pGseCtx->m_pGeomDB->SetDefaultMaterial( pGseCtx->m_colDef) ; // rendo corrente il contesto SetCurrGseContext( nGseCtx) ; // log avvio Context (DB geometrico) string sLog = "Context " + ToString( nGseCtx) + " started" ; LOG_INFO( GetLogger(), sLog.c_str()) return nGseCtx ; } //----------------------------------------------------------------------------- bool ExeDeleteContext( int nGseCtx) { // se contesto corrente, lo resetto if ( nGseCtx == GetIndCurrGseContext()) ResetCurrGseContext() ; // lo cancello bool bOk = DeleteGseContext( nGseCtx) ; // log arresto Context (DB geometrico + eventuale Scene) string sLog = "Context " + ToString( nGseCtx) + ( bOk ? " deleted" : " not found") ; LOG_INFO( GetLogger(), sLog.c_str()) return bOk ; } //----------------------------------------------------------------------------- bool ExeSetCurrentContext( int nGseCtx) { return SetCurrGseContext( nGseCtx) ; } //----------------------------------------------------------------------------- bool ExeResetCurrentContext( void) { return ResetCurrGseContext() ; } //----------------------------------------------------------------------------- int ExeGetCurrentContext( void) { return GetIndCurrGseContext() ; } //----------------------------------------------------------------------------- bool ExeSetDefaultMaterial( Color ColDef) { GseContext* pGseCtx = GetCurrGseContext() ; VERIFY_CTX_GEOMDB( pGseCtx, false) // imposto il materiale di default pGseCtx->m_colDef = ColDef ; pGseCtx->m_pGeomDB->SetDefaultMaterial( pGseCtx->m_colDef) ; return true ; } //----------------------------------------------------------------------------- bool ExeSetGridFrame( const Frame3d& frFrame) { IGeomDB* pGeomDB = GetCurrGeomDB() ; VERIFY_GEOMDB( pGeomDB, false) // imposto il riferimento della griglia bool bOk = pGeomDB->SetGridFrame( frFrame) ; // se richiesto, salvo il comando Lua equivalente if ( IsCmdLog()) { string sLua = "EgtSetGridFrame({{" + ToString( frFrame.Orig()) + "},{" + ToString( frFrame.VersX()) + "},{" + ToString( frFrame.VersY()) + "},{" + ToString( frFrame.VersZ()) + "}})" + " -- Ok=" + ToString( bOk) ; LOG_INFO( GetCmdLogger(), sLua.c_str()) ; } return bOk ; } //----------------------------------------------------------------------------- bool ExeGetGridFrame( int nRefId, Frame3d& frFrame) { IGeomDB* pGeomDB = GetCurrGeomDB() ; VERIFY_GEOMDB( pGeomDB, false) // recupero il riferimento della griglia frFrame = pGeomDB->GetGridFrame() ; // se richiesto nel riferimento globale, esco subito if ( nRefId == GDB_ID_ROOT) return true ; // recupero il riferimento destinazione (nRefId può essere un gruppo o una entità) Frame3d frDest ; if ( ! pGeomDB->GetGroupGlobFrame( nRefId, frDest) && ! pGeomDB->GetGlobFrame( nRefId, frDest)) return false ; // eseguo la trasformazione return frFrame.ToLoc( frDest) ; } //----------------------------------------------------------------------------- bool ExeGetGridVersZ( int nRefId, Vector3d& vtVersZ) { IGeomDB* pGeomDB = GetCurrGeomDB() ; VERIFY_GEOMDB( pGeomDB, false) // recupero il versore Z della griglia vtVersZ = pGeomDB->GetGridFrame().VersZ() ; // se richiesto nel riferimento globale, esco subito if ( nRefId == GDB_ID_ROOT) return true ; // recupero il riferimento destinazione (nRefId può essere un gruppo o una entità) Frame3d frDest ; if ( ! pGeomDB->GetGroupGlobFrame( nRefId, frDest) && ! pGeomDB->GetGlobFrame( nRefId, frDest)) return false ; // eseguo la trasformazione return vtVersZ.ToLoc( frDest) ; } //----------------------------------------------------------------------------- bool ExeSetCurrFilePath( const string& sFilePath) { GseContext* pGseCtx = GetCurrGseContext() ; VERIFY_CTX_GEOMDB( pGseCtx, false) // assegno la path pGseCtx->m_sFilePath = sFilePath ; return true ; } //----------------------------------------------------------------------------- bool ExeGetCurrFilePath( string& sFilePath) { GseContext* pGseCtx = GetCurrGseContext() ; VERIFY_CTX_GEOMDB( pGseCtx, false) // restituisco la path sFilePath = pGseCtx->m_sFilePath ; return ( ! sFilePath.empty()) ; } //----------------------------------------------------------------------------- bool ExeEnableModified( void) { GseContext* pGseCtx = GetCurrGseContext() ; VERIFY_CTX_GEOMDB( pGseCtx, false) // abilito possibilità di alterare il flag di modifica pGseCtx->m_bEnableModified = true ; return true ; } //----------------------------------------------------------------------------- bool ExeDisableModified( void) { GseContext* pGseCtx = GetCurrGseContext() ; VERIFY_CTX_GEOMDB( pGseCtx, false) // disabilito possibilità di alterare il flag di modifica pGseCtx->m_bEnableModified = false ; return true ; } //----------------------------------------------------------------------------- bool ExeGetEnableModified( void) { GseContext* pGseCtx = GetCurrGseContext() ; VERIFY_CTX_GEOMDB( pGseCtx, false) // restituisco stato return pGseCtx->m_bEnableModified ; } //----------------------------------------------------------------------------- bool ExeSetModified( void) { GseContext* pGseCtx = GetCurrGseContext() ; VERIFY_CTX_GEOMDB( pGseCtx, false) // se consentito, imposto il flag if ( pGseCtx->m_bEnableModified) pGseCtx->m_bModified = true ; return true ; } //----------------------------------------------------------------------------- bool ExeSetModified( int nCtx) { GseContext* pGseCtx = GetGseContext( nCtx) ; VERIFY_CTX_GEOMDB( pGseCtx, false) // se consentito, imposto il flag if ( pGseCtx->m_bEnableModified) pGseCtx->m_bModified = true ; return true ; } //----------------------------------------------------------------------------- bool ExeResetModified( void) { GseContext* pGseCtx = GetCurrGseContext() ; VERIFY_CTX_GEOMDB( pGseCtx, false) // se consentito, cancello il flag if ( pGseCtx->m_bEnableModified) pGseCtx->m_bModified = false ; return true ; } //----------------------------------------------------------------------------- bool ExeGetModified( void) { GseContext* pGseCtx = GetCurrGseContext() ; VERIFY_CTX_GEOMDB( pGseCtx, false) // restituisco il flag return pGseCtx->m_bModified ; } //----------------------------------------------------------------------------- bool ExeNewFile( void) { GseContext* pGseCtx = GetCurrGseContext() ; VERIFY_CTX_GEOMDB( pGseCtx, false) bool bOk = true ; // reinizializzazione (con pulizia) del DB geometrico bOk = bOk && pGseCtx->m_pGeomDB->Init() ; bOk = bOk && pGseCtx->m_pGeomDB->SetDefaultMaterial( pGseCtx->m_colDef) ; // pulizia delle textures if ( pGseCtx->m_pScene != nullptr) bOk = bOk && pGseCtx->m_pScene->UnloadAllTextures() ; // aggiorno stato file corrente pGseCtx->m_sFilePath.clear() ; ExeResetModified() ; // aggiorno pezzo/layer correnti bool bPrevCmdLog = SetCmdLog( false) ; ExeResetCurrPartLayer() ; SetCmdLog( bPrevCmdLog) ; // aggiornamento gestore lavorazioni (se installato) if ( GetCurrMachMgr() != nullptr) ExeUpdateMachMgr() ; // se richiesto, salvo il comando Lua equivalente if ( IsCmdLog()) { string sLua = "EgtNewFile()" " -- Ok=" + ToString( bOk) ; LOG_INFO( GetCmdLogger(), sLua.c_str()) ; } // scrivo il log LOG_INFO( GetLogger(), "New File") ; // restituisco il risultato return bOk ; } //----------------------------------------------------------------------------- bool ExeOpenFile( const string& sFilePath) { GseContext* pGseCtx = GetCurrGseContext() ; VERIFY_CTX_GEOMDB( pGseCtx, false) bool bOk = true ; // reinizializzazione (con pulizia) del DB geometrico bOk = bOk && pGseCtx->m_pGeomDB->Init() ; bOk = bOk && pGseCtx->m_pGeomDB->SetDefaultMaterial( pGseCtx->m_colDef) ; // pulizia delle textures if ( pGseCtx->m_pScene != nullptr) bOk = bOk && pGseCtx->m_pScene->UnloadAllTextures() ; // pulizia gestore lavorazioni (necessaria qui) if ( GetCurrMachMgr() != nullptr) ExeUpdateMachMgr() ; // imposto path corrente del file pGseCtx->m_sFilePath = sFilePath ; // carico il file bOk = bOk && pGseCtx->m_pGeomDB->Load( sFilePath) ; // aggiorno stato file corrente ExeResetModified() ; // aggiorno pezzo/layer correnti bool bPrevCmdLog = SetCmdLog( false) ; ExeResetCurrPartLayer() ; SetCmdLog( bPrevCmdLog) ; // aggiornamento gestore lavorazioni if ( GetCurrMachMgr() != nullptr) ExeUpdateMachMgr() ; // se richiesto, salvo il comando Lua equivalente if ( IsCmdLog()) { string sLua = "EgtOpenFile('" + StringToLuaString( sFilePath) + "')" + " -- Ok=" + ToString( bOk) ; LOG_INFO( GetCmdLogger(), sLua.c_str()) ; } // scrivo il log { string sLog = "Open File " + sFilePath ; LOG_INFO( GetLogger(), sLog.c_str()) ; } // restituisco il risultato return bOk ; } //----------------------------------------------------------------------------- bool ExeInsertFile( const string& sFilePath) { GseContext* pGseCtx = GetCurrGseContext() ; VERIFY_CTX_GEOMDB( pGseCtx, false) IGeomDB* pGeomDB = pGseCtx->m_pGeomDB ; // creo gruppo temporaneo di parcheggio int nGrp = pGeomDB->InsertGroup( GDB_ID_NULL, GDB_ID_ROOT, GDB_FIRST_SON, GLOB_FRM) ; bool bOk = ( nGrp != GDB_ID_NULL) ; bOk = bOk && pGeomDB->SetLevel( nGrp, GDB_LV_TEMP) ; // carico il file bOk = bOk && pGeomDB->Load( sFilePath, nGrp) ; // sposto i pezzi sotto la radice int nId = pGeomDB->GetFirstGroupInGroup( nGrp) ; while ( bOk && nId != GDB_ID_NULL) { // prossimo gruppo int nNextId = pGeomDB->GetNextGroup( nId) ; // se il gruppo corrente è un pezzo, lo sposto int nLevel ; if ( ! pGeomDB->GetLevel( nId, nLevel) || nLevel == GDB_LV_USER) bOk = pGeomDB->Relocate( nId, GDB_ID_ROOT, GDB_LAST_SON) ; // passo al prossimo nId = nNextId ; } // se gestite, sistemo le lavorazioni IMachMgr* pMchMgr = GetCurrMachMgr() ; if ( pMchMgr != nullptr) bOk = bOk && ExeInsertMachMgr( nGrp) ; // sistemo eventuali pezzi duplicati bOk = bOk && ExeInsertDuplo( nGrp) ; // sposto i gruppi rimasti sotto la radice nId = pGeomDB->GetFirstGroupInGroup( nGrp) ; while ( bOk && nId != GDB_ID_NULL) { // prossimo gruppo int nNextId = pGeomDB->GetNextGroup( nId) ; // se non è base per lavorazioni, lo sposto if ( pMchMgr == nullptr || ! pMchMgr->IsMachBase( nId)) bOk = pGeomDB->Relocate( nId, GDB_ID_ROOT, GDB_LAST_SON) ; // passo al prossimo nId = nNextId ; } // cancello il gruppo temporaneo pGeomDB->Erase( nGrp) ; // aggiorno stato file corrente ExeSetModified() ; // se richiesto, salvo il comando Lua equivalente if ( IsCmdLog()) { string sLua = "EgtInsertFile('" + StringToLuaString( sFilePath) + "')" + " -- Ok=" + ToString( bOk) ; LOG_INFO( GetCmdLogger(), sLua.c_str()) ; } // restituisco il risultato return bOk ; } //----------------------------------------------------------------------------- class CamStatus { public : CamStatus( void) : m_nCurrMachGroup( GDB_ID_NULL), m_nCurrPhase( 0), m_nCurrMachining( GDB_ID_NULL) { IMachMgr* pMachMgr = GetCurrMachMgr() ; if ( pMachMgr == nullptr) return ; m_nCurrMachGroup = pMachMgr->GetCurrMachGroup() ; if ( m_nCurrMachGroup == GDB_ID_NULL) return ; m_nCurrPhase = pMachMgr->GetCurrPhase() ; if ( m_nCurrPhase == 0) return ; m_nCurrMachining = pMachMgr->GetCurrMachining() ; int nId = pMachMgr->GetPhaseDisposition( m_nCurrPhase) ; while ( nId != GDB_ID_NULL && pMachMgr->GetOperationPhase( nId) == m_nCurrPhase) { bool bShow = false ; pMachMgr->GetOperationStatus( nId, bShow) ; m_vIdStat.push_back( ( bShow ? nId : - nId)) ; nId = pMachMgr->GetNextOperation( nId) ; } pMachMgr->ResetCurrMachGroup() ; } ~CamStatus( void) { Restore() ; } void Restore( void) { if ( m_nCurrMachGroup == GDB_ID_NULL) return ; IMachMgr* pMachMgr = GetCurrMachMgr() ; if ( pMachMgr == nullptr) return ; pMachMgr->SetCurrMachGroup( m_nCurrMachGroup) ; m_nCurrMachGroup = GDB_ID_NULL ; if ( m_nCurrPhase == 0) return ; pMachMgr->SetCurrPhase( m_nCurrPhase, true) ; pMachMgr->SetCurrMachining( m_nCurrMachining) ; for ( auto nId : m_vIdStat) { pMachMgr->SetOperationStatus( abs( nId), ( nId > 0)) ; } } private : int m_nCurrMachGroup ; int m_nCurrPhase ; int m_nCurrMachining ; INTVECTOR m_vIdStat ; } ; //----------------------------------------------------------------------------- bool ExeSaveFile( const string& sFilePath, int nFlag) { GseContext* pGseCtx = GetCurrGseContext() ; VERIFY_CTX_GEOMDB( pGseCtx, false) // se ero in CAM, esco dopo averne salvato lo stato CamStatus CurrCamStatus ; // imposto path corrente del file pGseCtx->m_sFilePath = sFilePath ; // salvo il file bool bOk = pGseCtx->m_pGeomDB->Save( GDB_ID_ROOT, sFilePath, nFlag) ; // eventuale ripristino precedente CAM CurrCamStatus.Restore() ; // aggiorno stato file corrente if ( bOk) ExeResetModified() ; // se richiesto, salvo il comando Lua equivalente if ( IsCmdLog()) { string sLua = "EgtSaveFile('" + StringToLuaString( sFilePath) + "'," + NgeTypeToString( nFlag) + ")" + " -- Ok=" + ToString( bOk) ; LOG_INFO( GetCmdLogger(), sLua.c_str()) ; } // scrivo il log { string sLog = "Saved File " + sFilePath ; LOG_INFO( GetLogger(), sLog.c_str()) ; } // restituisco il risultato return bOk ; } //----------------------------------------------------------------------------- static bool IsMachGroupOrWithin( int nId, const IGeomDB* pGeomDB, const IMachMgr* pMachMgr, int& nMachGroupId) { // default non definito nMachGroupId = GDB_ID_NULL ; // verifico validità puntatori if ( pGeomDB == nullptr || pMachMgr == nullptr) return false ; // eseguo ricerca int nParentId = pGeomDB->GetParentId( nId) ; int nPrevParId = nId ; while ( nParentId != GDB_ID_NULL && nParentId != GDB_ID_ROOT) { if ( pMachMgr->IsMachBase( nParentId)) { nMachGroupId = nPrevParId ; return true ; } nPrevParId = nParentId ; nParentId = pGeomDB->GetParentId( nParentId) ; } return false ; } //----------------------------------------------------------------------------- bool ExeSaveObjToFile( const INTVECTOR& vId, const string& sFilePath, int nFlag) { IGeomDB* pGeomDB = GetCurrGeomDB() ; VERIFY_GEOMDB( pGeomDB, false) IMachMgr* pMachMgr = GetCurrMachMgr() ; // se uno degli oggetti è la base dei gruppi di lavoro, chiamo la funzione di salvataggio di tutto for ( int nId : vId) { if ( pMachMgr != nullptr && pMachMgr->IsMachBase( nId)) return ExeSaveFile( sFilePath, nFlag) ; } // se uno degli oggetti è un gruppo di lavoro o una sua parte, chiamo la appropriata funzione di salvataggio for ( int nId : vId) { int nMachGroupId ; if ( IsMachGroupOrWithin( nId, pGeomDB, pMachMgr, nMachGroupId)) { INTVECTOR vOthId ; vOthId.reserve( vId.size()) ; for ( int nSouId : vId) { if ( nSouId != nId) vOthId.push_back( nSouId) ; } return ExeSaveMachGroupToFile( nMachGroupId, vOthId, sFilePath, nFlag) ; } } // se ero in CAM, non esco <--- // copio l'oggetto nel file bool bOk = pGeomDB->Save( vId, sFilePath, nFlag) ; // se richiesto, salvo il comando Lua equivalente if ( IsCmdLog()) { string sLua = "EgtSaveObjToFile({" + ToString( vId) + "},'" + StringToLuaString( sFilePath) + "'," + NgeTypeToString( nFlag) + ")" + " -- Ok=" + ToString( bOk) ; LOG_INFO( GetCmdLogger(), sLua.c_str()) ; } // restituisco il risultato return bOk ; } //----------------------------------------------------------------------------- bool ExeSaveMachGroupToFile( int nMGroupId, const INTVECTOR& vPlusId, const string& sFilePath, int nFlag) { IGeomDB* pGeomDB = GetCurrGeomDB() ; VERIFY_GEOMDB( pGeomDB, false) IMachMgr* pMachMgr = GetCurrMachMgr() ; VERIFY_MACHMGR( pMachMgr, false) // disabilito registrazione dei comandi bool bPrevCmdLog = SetCmdLog( false) ; // disabilito possibilità di alterare il flag di modifica bool bOldEnabModif = ExeGetEnableModified() ; if ( bOldEnabModif) ExeDisableModified() ; // se ero in CAM, esco dopo averne salvato lo stato CamStatus CurrCamStatus ; // verifico che gli oggetti Plus non siano gruppi di lavoro o una loro parte bool bOk = true ; for ( int nPlusId : vPlusId) { int nMGrpPlusId ; if ( IsMachGroupOrWithin( nPlusId, pGeomDB, pMachMgr, nMGrpPlusId)) { bOk = false ; break ; } } // recupero eventuali pezzi e foto del gruppo di lavoro INTVECTOR vId ; int nPhotoId = GDB_ID_NULL ; string sPhotoOriPath = "" ; bOk = bOk && pMachMgr->SetCurrMachGroup( nMGroupId) ; if ( bOk) { // aggiungo gruppo di lavoro vId.emplace_back( nMGroupId) ; // aggiungo pezzi int nRawId = pMachMgr->GetFirstRawPart() ; while ( nRawId != GDB_ID_NULL) { int nPartId = pMachMgr->GetFirstPartInRawPart( nRawId) ; while ( nPartId != GDB_ID_NULL) { if ( find( vId.begin(), vId.end(), nPartId) == vId.end()) { vId.emplace_back( nPartId) ; // potrebbe essere un duplicato, recupero eventuale originale int nOrigId = GDB_ID_NULL ; pGeomDB->GetInfo( nPartId, GDB_SI_DUPSOU, nOrigId) ; if ( ExeIsPart( nOrigId) && find( vId.begin(), vId.end(), nOrigId) == vId.end()) vId.emplace_back( nOrigId) ; } nPartId = pMachMgr->GetNextPartInRawPart( nPartId) ; } nRawId = pMachMgr->GetNextRawPart( nRawId) ; } // aggiungo eventuale foto static const string PHOTO_GRP = "Photos" ; static const string PHOTO_NAME = "Raw" ; string sMGroupName = pMachMgr->GetMachGroupName( nMGroupId) ; nPhotoId = pGeomDB->GetFirstNameInGroup( pGeomDB->GetFirstNameInGroup( GDB_ID_ROOT, PHOTO_GRP), PHOTO_NAME + sMGroupName) ; if ( nPhotoId != GDB_ID_NULL) { vId.emplace_back( nPhotoId) ; ExeGetPhotoPath( nPhotoId, sPhotoOriPath) ; string sPhotoNewPath = ChangeFileExtension( sFilePath, GetFileExtension( sPhotoOriPath)) ; ExeChangePhotoPath( nPhotoId, sPhotoNewPath) ; CopyFileEgt( sPhotoOriPath, sPhotoNewPath) ; } // disattivo gruppo di lavoro pMachMgr->ResetCurrMachGroup() ; } // aggiungo gli oggetti plus if ( bOk) vId.insert( vId.end(), vPlusId.begin(), vPlusId.end()) ; // salvo gli oggetti appena identificati bOk = bOk && pGeomDB->Save( vId, sFilePath, nFlag) ; // eventuale ripristino dati fotografia if ( nPhotoId != GDB_ID_NULL) ExeChangePhotoPath( nPhotoId, sPhotoOriPath) ; // eventuale ripristino precedente CAM CurrCamStatus.Restore() ; // ripristino possibilità di alterare il flag di modifica if ( bOldEnabModif) ExeEnableModified() ; // ripristino registrazione dei comandi SetCmdLog( bPrevCmdLog) ; // se richiesto, salvo il comando Lua equivalente if ( IsCmdLog()) { string sLua = "EgtSaveMachGroupToFile(" + ToString( nMGroupId) + ",{" + ToString( vPlusId) + "},'" + StringToLuaString( sFilePath) + "'," + NgeTypeToString( nFlag) + ")" + " -- Ok=" + ToString( bOk) ; LOG_INFO( GetCmdLogger(), sLua.c_str()) ; } // restituisco il risultato return bOk ; }