Files
EgtMachKernel/MachMgrBasic.cpp
T
DarioS 156f0f8ef8 EgtMachKernel 2.4e3 :
- modifiche per prima versione chiave di rete.
2022-05-17 08:26:38 +02:00

316 lines
10 KiB
C++

//----------------------------------------------------------------------------
// EgalTech 2015-2015
//----------------------------------------------------------------------------
// File : MachMgr.cpp Data : 16.04.15 Versione : 1.6d3
// Contenuto : Implementazione delle funzionalità base della classe MachMgr.
//
//
//
// Modifiche : 16.04.15 DS Creazione modulo.
//
//
//----------------------------------------------------------------------------
//--------------------------- Include ----------------------------------------
#include "stdafx.h"
#include "MachMgr.h"
#include "MachConst.h"
#include "DllMain.h"
#include "/EgtDev/Include/EMkDllMain.h"
#include "/EgtDev/Include/EGnStringUtils.h"
#include "/EgtDev/Include/EGnFileUtils.h"
#include "/EgtDev/Include/EgtKeyCodes.h"
#include "/EgtDev/Include/SELkKeyProc.h"
#include <new>
using namespace std ;
//----------------------------------------------------------------------------
IMachMgr*
CreateMachMgr( void)
{
// verifico la chiave e le opzioni
if ( ! GetEMkNetHwKey() && ! TestKeyForEMk( GetEMkKey(), 0, GetEMkLogger()))
return nullptr ;
// creo il MachMgr
return static_cast<IMachMgr*> ( new(nothrow) MachMgr) ;
}
//----------------------------------------------------------------------------
// MachMgr
//----------------------------------------------------------------------------
MachMgr::MachMgr( void)
{
m_nContextId = 0 ;
m_pGeomDB = nullptr ;
m_nLastError = 0 ;
m_nMachBaseId = GDB_ID_NULL ;
m_nMachAuxId = GDB_ID_NULL ;
m_nCurrMGrpId = GDB_ID_NULL ;
m_nCurrMch = - 1 ;
m_nPhasesCount = 0 ;
m_nCurrPhase = 0 ;
m_nCurrDispId = GDB_ID_NULL ;
m_nCurrMachiningId = GDB_ID_NULL ;
m_pSimul = nullptr ;
}
//----------------------------------------------------------------------------
MachMgr::~MachMgr( void)
{
Clear() ;
}
//----------------------------------------------------------------------------
void
MachMgr::Clear( void)
{
// cancello simulatore, se necessario
if ( m_pSimul != nullptr) {
delete m_pSimul ;
m_pSimul = nullptr ;
}
// pulisco le macchine (ma ne lascio intatto l'elencazione con il nome)
for ( size_t i = 0 ; i < m_vMachines.size() ; ++ i) {
if ( m_vMachines[i].pMsMgr != nullptr) {
delete m_vMachines[i].pMsMgr ;
m_vMachines[i].pMsMgr = nullptr ;
}
if ( m_vMachines[i].pTsMgr != nullptr) {
delete m_vMachines[i].pTsMgr ;
m_vMachines[i].pTsMgr = nullptr ;
}
if ( m_vMachines[i].pMachine != nullptr) {
delete m_vMachines[i].pMachine ;
m_vMachines[i].pMachine = nullptr ;
}
}
}
//----------------------------------------------------------------------------
bool
MachMgr::Init( const string& sMachinesDir, const string& sToolMakersDir, IGeomDB* pGeomDB,
int nContextId, const string& sLuaLibsDir, const string& sLuaLastRequire)
{
m_nContextId = nContextId ;
m_pGeomDB = pGeomDB ;
m_sMachinesDir = sMachinesDir ;
m_sToolMakersDir = sToolMakersDir ;
m_sLuaLibsDir = sLuaLibsDir ;
m_sLuaLastRequire = sLuaLastRequire ;
m_nMachBaseId = GDB_ID_NULL ;
m_nMachAuxId = GDB_ID_NULL ;
m_nCurrMGrpId = GDB_ID_NULL ;
m_nCurrMch = - 1 ;
m_stuMgr.Reset() ;
return ( m_nContextId > 0 && m_pGeomDB != nullptr && ExistsDirectory( m_sMachinesDir)) ;
}
//----------------------------------------------------------------------------
bool
MachMgr::Update( void)
{
// pulizia
Clear() ;
// ricerca del gruppo base per le lavorazioni
m_nMachBaseId = FindMachBase( GDB_ID_ROOT) ;
// imposto gruppo di lavorazione corrente a nessuno
m_nCurrMGrpId = GDB_ID_NULL ;
// recupero indice di gruppo dove spostare i gruppi di lavoro con errori
int nMachErrId = m_pGeomDB->GetFirstNameInGroup( GDB_ID_ROOT, MACH_ERR) ;
// verifico i gruppi di lavorazione
int nId = m_pGeomDB->GetFirstGroupInGroup( m_nMachBaseId) ;
while ( nId != GDB_ID_NULL) {
// recupero successivo
int nNextId = m_pGeomDB->GetNextGroup( nId) ;
// se non è gruppo di lavoro, lo sposto in apposito gruppo per gli errori
MachGrp mgData ;
if ( ! VerifyMachGroup( nId, mgData)) {
// se non esiste gruppo per gli errori, lo creo
if ( nMachErrId == GDB_ID_NULL) {
nMachErrId = m_pGeomDB->AddGroup( GDB_ID_NULL, GDB_ID_ROOT, Frame3d()) ;
m_pGeomDB->SetName( nMachErrId, MACH_ERR) ;
m_pGeomDB->SetLevel( nMachErrId, GDB_LV_SYSTEM) ;
}
// se non riesco a rilocare il gruppo errato, lo cancello
if ( ! m_pGeomDB->Relocate( nId, nMachErrId))
m_pGeomDB->Erase( nId) ;
}
// passo al successivo
nId = nNextId ;
}
return true ;
}
//----------------------------------------------------------------------------
bool
MachMgr::Insert( int nInsGrp)
{
// cerco MachBase nel gruppo di inserimento
int nInsMachBaseId = FindMachBase( nInsGrp) ;
if ( nInsMachBaseId == GDB_ID_NULL)
return true ;
// verifica ed eventuale creazione del gruppo base per le lavorazioni
if ( ! CreateMachBase())
return false ;
// ciclo sui suoi gruppi
int nId = m_pGeomDB->GetFirstGroupInGroup( nInsMachBaseId) ;
while ( nId != GDB_ID_NULL) {
// prossimo gruppo
int nNextId = m_pGeomDB->GetNextGroup( nId) ;
// se gruppo di lavoro
MachGrp mgData ;
if ( VerifyMachGroup( nId, mgData)) {
// recupero il nome e se già presente lo modifico
string sName ;
if ( ! m_pGeomDB->GetName( nId, sName) ||
! GetMachGroupNewName( sName) ||
! m_pGeomDB->SetName( nId, sName))
return false ;
// sposto il gruppo
if ( ! m_pGeomDB->Relocate( nId, m_nMachBaseId, GDB_LAST_SON))
return false ;
}
// passo al prossimo
nId = nNextId ;
}
return true ;
}
//----------------------------------------------------------------------------
bool
MachMgr::IsMachBase( int nId) const
{
if ( m_pGeomDB == nullptr)
return false ;
int nLevel ;
string sName ;
return ( m_pGeomDB != nullptr &&
m_pGeomDB->GetLevel( nId, nLevel) && nLevel == GDB_LV_SYSTEM &&
m_pGeomDB->GetName( nId, sName) && sName == MACH_BASE) ;
}
//----------------------------------------------------------------------------
int
MachMgr::FindMachBase( int nGroup) const
{
// verifica collegamento a DB geometrico
if ( m_pGeomDB == nullptr)
return GDB_ID_NULL ;
// cerco il gruppo di base delle macchinate nel gruppo passato
int nId = m_pGeomDB->GetFirstGroupInGroup( nGroup) ;
while ( nId != GDB_ID_NULL) {
// verifico se gruppo cercato
if ( IsMachBase( nId))
return nId ;
// passo al successivo
nId = m_pGeomDB->GetNextGroup( nId) ;
}
return GDB_ID_NULL ;
}
//----------------------------------------------------------------------------
bool
MachMgr::VerifyMachBase( void) const
{
// verifica collegamento a DB geometrico
if ( m_pGeomDB == nullptr)
return false ;
// verifica validità indice del gruppo base
if ( m_nMachBaseId == GDB_ID_NULL)
return false ;
// verifica esistenza gruppo di base per macchinate
if ( ! IsMachBase( m_nMachBaseId)) {
const_cast<int&>( m_nMachBaseId) = GDB_ID_NULL ;
return false ;
}
return true ;
}
//----------------------------------------------------------------------------
bool
MachMgr::CreateMachBase( void)
{
// se già esiste, esco con successo
if ( VerifyMachBase())
return true ;
// non trovato, devo creare il gruppo di base delle lavorazioni
m_nMachBaseId = m_pGeomDB->InsertGroup( GDB_ID_NULL, GDB_ID_ROOT, GDB_FIRST_SON, GLOB_FRM) ;
if ( m_nMachBaseId == GDB_ID_NULL)
return false ;
// imposto nome del gruppo
m_pGeomDB->SetName( m_nMachBaseId, MACH_BASE) ;
// imposto livello del gruppo a System
m_pGeomDB->SetLevel( m_nMachBaseId, GDB_LV_SYSTEM) ;
return true ;
}
//----------------------------------------------------------------------------
bool
MachMgr::IsMachAux( int nId) const
{
int nLevel ;
string sName ;
return ( m_pGeomDB != nullptr &&
m_pGeomDB->GetLevel( nId, nLevel) && nLevel == GDB_LV_TEMP &&
m_pGeomDB->GetName( nId, sName) && sName == MACH_AUX) ;
}
//----------------------------------------------------------------------------
bool
MachMgr::VerifyMachAux( void) const
{
// verifica collegamento a DB geometrico
if ( m_pGeomDB == nullptr)
return false ;
// verifica validità indice del gruppo base
if ( m_nMachAuxId == GDB_ID_NULL)
return false ;
// verifica esistenza gruppo di base per macchinate
if ( ! IsMachAux( m_nMachAuxId)) {
const_cast<int&>( m_nMachAuxId) = GDB_ID_NULL ;
return false ;
}
return true ;
}
//----------------------------------------------------------------------------
bool
MachMgr::CreateMachAux( void)
{
// se già esiste, esco con successo
if ( VerifyMachAux())
return true ;
// non trovato, devo creare il gruppo di base delle lavorazioni
m_nMachAuxId = m_pGeomDB->InsertGroup( GDB_ID_NULL, GDB_ID_ROOT, GDB_FIRST_SON, GLOB_FRM) ;
if ( m_nMachAuxId == GDB_ID_NULL)
return false ;
// imposto nome del gruppo
m_pGeomDB->SetName( m_nMachAuxId, MACH_AUX) ;
// imposto livello del gruppo a Temp
m_pGeomDB->SetLevel( m_nMachAuxId, GDB_LV_TEMP) ;
return true ;
}
//----------------------------------------------------------------------------
bool
MachMgr::SetLastError( int nErr, const string& sErr)
{
m_nLastError = nErr ;
m_sLastError = sErr ;
string sInfo = m_sLastError + " (" + ToString( nErr) + ")" ;
LOG_ERROR( GetEMkLogger(), sInfo.c_str()) ;
return true ;
}
//----------------------------------------------------------------------------
bool
MachMgr::SetWarning( int nWarn, const string& sWarn)
{
m_Warnings.emplace_back( nWarn, sWarn) ;
string sInfo = sWarn + " (" + ToString( nWarn) + ")" ;
LOG_WARN( GetEMkLogger(), sInfo.c_str()) ;
return true ;
}