Files
EgtExecutor/EXE_GeomDB.cpp
T
Dario Sassi c0fb5b2010 EgtExecutor :
- migliorata gestione aggiornamento dati lavorazioni su nuovo e apri file.
2017-01-04 07:38:01 +00:00

461 lines
15 KiB
C++

//----------------------------------------------------------------------------
// 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/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 (" __FUNCTION__ ")")
return 0 ;
}
GseContext* pGseCtx = GetGseContext( nGseCtx) ;
// inizializzazioni DB geometrico
PtrOwner<IGeomDB> 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
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)
// imposto 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()) ;
}
// 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()) ;
}
// 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
if ( GetCurrMachMgr() != nullptr)
bOk = bOk && ExeInsertMachMgr( nGrp) ;
// 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) {
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 ;
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) ;
}
}
void Restore( void) {
IMachMgr* pMachMgr = GetCurrMachMgr() ;
if ( pMachMgr == nullptr)
return ;
if ( m_nCurrMachGroup == GDB_ID_NULL)
return ;
pMachMgr->SetCurrMachGroup( m_nCurrMachGroup) ;
if ( m_nCurrPhase == 0)
return ;
pMachMgr->SetCurrPhase( m_nCurrPhase, true) ;
for ( auto nId : m_vIdStat) {
pMachMgr->SetOperationStatus( abs( nId), ( nId > 0)) ;
}
}
private :
int m_nCurrMachGroup ;
int m_nCurrPhase ;
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 ;
ExeResetCurrMachGroup() ;
// 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()) ;
}
// restituisco il risultato
return bOk ;
}
//-----------------------------------------------------------------------------
bool
ExeSaveObjToFile( int nId, const string& sFilePath, int nFlag)
{
IGeomDB* pGeomDB = GetCurrGeomDB() ;
VERIFY_GEOMDB( pGeomDB, false)
// se ero in CAM, esco
int nCurrMachGroup = ExeGetCurrMachGroup() ;
ExeResetCurrMachGroup() ;
// copio l'oggetto nel file
bool bOk = pGeomDB->Save( nId, sFilePath, nFlag) ;
// ripristino eventuale precedente CAM
if ( nCurrMachGroup != GDB_ID_NULL)
ExeSetCurrMachGroup( nCurrMachGroup) ;
// se richiesto, salvo il comando Lua equivalente
if ( IsCmdLog()) {
string sLua = "EgtSaveObjToFile('" + StringToLuaString( sFilePath) + "'," +
ToString( nId) + "," +
NgeTypeToString( nFlag) + ")" +
" -- Ok=" + ToString( bOk) ;
LOG_INFO( GetCmdLogger(), sLua.c_str()) ;
}
// restituisco il risultato
return bOk ;
}