//---------------------------------------------------------------------------- // EgalTech 2015-2015 //---------------------------------------------------------------------------- // File : MachMgr.cpp Data : 23.03.15 Versione : 1.6c6 // Contenuto : Implementazione della classe MachMgr. // // // // Modifiche : 23.03.15 DS Creazione modulo. // // //---------------------------------------------------------------------------- //--------------------------- Include ---------------------------------------- #include "stdafx.h" #include "DllMain.h" #include "MachMgr.h" #include "MachConst.h" #include "/EgtDev/Include/EGnStringUtils.h" #include "/EgtDev/Include/SELkKeyProc.h" #include "/EgtDev/Include/EgtKeyCodes.h" #include "/EgtDev/Include/EGkStmStandard.h" #include "/EgtDev/Include/EgtPointerOwner.h" #include using namespace std ; //---------------------------------------------------------------------------- IMachMgr* CreateMachMgr( void) { // verifico la chiave e le opzioni unsigned int nOpt1, nOpt2 ; int nOptExpDays ; int nRet = GetKeyOptions( GetEMkKey(), KEY_BASELIB_PROD, KEY_BASELIB_VER, KEY_BASELIB_LEV, nOpt1, nOpt2, nOptExpDays) ; if ( nRet != KEY_OK) { string sErr = "Error on Key (MKC/" + ToString( nRet) + ")" ; LOG_ERROR( GetEMkLogger(), sErr.c_str()) ; return false ; } if ( (nOpt1 & KEYOPT_EMK_BASE) == 0 || nOptExpDays < GetCurrDay()) { string sErr = "Error on Key (MKC/OPT)" ; LOG_ERROR( GetEMkLogger(), sErr.c_str()) ; return false ; } // creo il MachMgr return static_cast ( new(nothrow) MachMgr) ; } //---------------------------------------------------------------------------- // MachMgr //---------------------------------------------------------------------------- MachMgr::MachMgr( void) { m_pGeomDB = nullptr ; m_nMachBaseId = GDB_ID_NULL ; m_nMGroupCurr = 0 ; } //---------------------------------------------------------------------------- bool MachMgr::Init( IGeomDB* pGeomDB) { m_pGeomDB = pGeomDB ; m_nMachBaseId = GDB_ID_NULL ; m_vMGroupId.clear() ; m_nMGroupCurr = 0 ; return ( m_pGeomDB != nullptr) ; } //---------------------------------------------------------------------------- bool MachMgr::Update( void) { // reset m_nMachBaseId = GDB_ID_NULL ; m_nMGroupCurr = 0 ; m_vMGroupId.clear() ; // ricerca del gruppo base per le lavorazioni if ( ! FindMachBase( GDB_ID_ROOT, m_nMachBaseId)) return true ; // aggiornamento altri dati int nId = m_pGeomDB->GetFirstGroupInGroup( m_nMachBaseId) ; while ( nId != GDB_ID_NULL) { // inserisco id del gruppo nel vettore dei gruppi di lavorazione m_vMGroupId.push_back( nId) ; // passo al successivo nId = m_pGeomDB->GetNextGroup( nId) ; } return true ; } //---------------------------------------------------------------------------- bool MachMgr::Insert( int nInsGrp) { // verifica ed eventuale creazione del gruppo base per le lavorazioni if ( ! CreateMachBase()) return false ; // cerco MachBase nel gruppo di inserimento int nInsMachBaseId ; if ( ! FindMachBase( nInsGrp, nInsMachBaseId)) return true ; // ciclo sui suoi gruppi int nId = m_pGeomDB->GetFirstGroupInGroup( nInsMachBaseId) ; while ( nId != GDB_ID_NULL) { // prossimo gruppo int nNextId = m_pGeomDB->GetNextGroup( nId) ; // 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 { 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) ; } //---------------------------------------------------------------------------- bool MachMgr::FindMachBase( int nGroup, int& nMachBase) const { // verifica collegamento a DB geometrico if ( m_pGeomDB == nullptr) return false ; // 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)) { if ( &nMachBase != nullptr) nMachBase = nId ; return true ; } // passo al successivo nId = m_pGeomDB->GetNextGroup( nId) ; } return false ; } //---------------------------------------------------------------------------- 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( 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 ; } //---------------------------------------------------------------------------- int MachMgr::GetMachGroupNbr( void) const { // verifica del gruppo base per le lavorazioni if ( ! VerifyMachBase()) return 0 ; // ritorno numero di gruppi di lavorazioni return ( int( m_vMGroupId.size())) ; } //---------------------------------------------------------------------------- bool MachMgr::GetMachGroupNewName( string& sName) const { // il parametro nome deve essere valido if ( &sName == nullptr) return false ; // se gruppo base per le lavorazioni assente if ( ! VerifyMachBase()) { if ( sName.empty()) sName = "Mach01" ; return true ; } // verifico che il nome sia unico int nId ; int nCount = 1 ; string sOrigName = sName ; while ( GetMachGroupInd( sName, nId)) { ++ nCount ; sName = sOrigName + "_" + ToString( nCount) ; } return true ; } //---------------------------------------------------------------------------- int MachMgr::AddMachGroup( const string& sName, const string& sMachineName) { // verifica ed eventuale creazione del gruppo base per le lavorazioni if ( ! CreateMachBase()) return 0 ; // verifico nome non vuoto e non esista già un gruppo con lo stesso nome int nMGroup ; if ( sName.empty() || GetMachGroupInd( sName, nMGroup)) return 0 ; // verifico esistenza della macchina // !!!! TO DO !!!! // inserisco il gruppo in coda int nNewId = m_pGeomDB->AddGroup( GDB_ID_NULL, m_nMachBaseId, GLOB_FRM) ; // assegno il nome m_pGeomDB->SetName( nNewId, sName) ; // assegno la macchina m_pGeomDB->SetInfo( nNewId, MACH_KEY_MACHINE, sMachineName) ; // aggiungo il gruppo al vettore degli indici m_vMGroupId.push_back( nNewId) ; m_nMGroupCurr = int( m_vMGroupId.size()) ; // restituisco l'indice nel vettore ( +1) return m_nMGroupCurr ; } //---------------------------------------------------------------------------- bool MachMgr::RemoveMachGroup( int nMGroup) { // verifica del gruppo base per le lavorazioni if ( ! VerifyMachBase()) return false ; // verifico validità indice gruppo da rimuovere if ( nMGroup <= 0 || nMGroup > int( m_vMGroupId.size())) return false ; // eseguo rimozione m_pGeomDB->Erase( m_vMGroupId[nMGroup-1]) ; // aggiorno vettore indici for ( size_t i = nMGroup ; i < m_vMGroupId.size() ; ++i) m_vMGroupId[i-1] = m_vMGroupId[i] ; m_vMGroupId.pop_back() ; // eventuale aggiornamento gruppo corrente if ( m_nMGroupCurr == nMGroup) m_nMGroupCurr = 0 ; else if ( m_nMGroupCurr > nMGroup) -- m_nMGroupCurr ; return true ; } //---------------------------------------------------------------------------- bool MachMgr::GetMachGroupName( int nMGroup, string& sName) const { // verifica del parametro di ritorno if ( &sName == nullptr) return false ; // verifica del gruppo base per le lavorazioni if ( ! VerifyMachBase()) return false ; // recupero il nome del gruppo riferito return ( nMGroup > 0 && nMGroup <= int( m_vMGroupId.size()) && m_pGeomDB->GetName( m_vMGroupId[nMGroup-1], sName)) ; } //---------------------------------------------------------------------------- bool MachMgr::GetMachGroupInd( const std::string& sName, int& nMGroup) const { // verifica dei parametri if ( &sName == nullptr || &nMGroup == nullptr) return false ; // verifica del gruppo base per le lavorazioni if ( ! VerifyMachBase()) return false ; // recupero l'indice del gruppo riferito for ( size_t i = 0 ; i < m_vMGroupId.size() ; ++ i) { // verifico il nome string sMGroupName ; if ( m_pGeomDB->GetName( m_vMGroupId[i], sMGroupName) && sMGroupName == sName) { nMGroup = int( i + 1) ; return true ; } } return false ; } //---------------------------------------------------------------------------- bool MachMgr::SetCurrMachGroup( int nMGroup) { // verifica del gruppo base per le lavorazioni if ( ! VerifyMachBase()) return false ; // verifica validità indice if ( nMGroup <= 0 || nMGroup > int( m_vMGroupId.size())) return false ; // eseguo m_nMGroupCurr = nMGroup ; return true ; } //---------------------------------------------------------------------------- bool MachMgr::ResetCurrMachGroup( void) { m_nMGroupCurr = 0 ; return VerifyMachBase() ; } //---------------------------------------------------------------------------- bool MachMgr::GetCurrMachGroup( int& nMGroup) const { // verifica del parametro di ritorno if ( &nMGroup == nullptr) return false ; // verifica del gruppo base per le lavorazioni if ( ! VerifyMachBase()) return false ; // recupero l'indice del gruppo corrente nMGroup = m_nMGroupCurr ; return ( m_nMGroupCurr != 0) ; } //---------------------------------------------------------------------------- int MachMgr::AddRawPart( const Point3d& ptOrig, double dWidth, double dLen, double dHeight, Color cCol) { // verifica della macchinata if ( ! VerifyMachBase() || m_nMGroupCurr == 0) return false ; // inserisco il gruppo del grezzo nella macchinata int nRawId = m_pGeomDB->AddGroup( GDB_ID_NULL, m_vMGroupId[ m_nMGroupCurr - 1], Frame3d( ptOrig)) ; if ( nRawId == GDB_ID_NULL) return GDB_ID_NULL ; // assegno il nome m_pGeomDB->SetName( nRawId, MACH_RAW_PART) ; // assegno il colore m_pGeomDB->SetMaterial( nRawId, cCol) ; // creo il solido PtrOwner pStm( GetSurfTriMeshBox( dWidth, dLen, dHeight)) ; if ( IsNull( pStm)) return GDB_ID_NULL ; // lo inserisco nel gruppo int nId = m_pGeomDB->AddGeoObj( GDB_ID_NULL, nRawId, Release( pStm)) ; if ( nId == GDB_ID_NULL) return GDB_ID_NULL ; return nRawId ; }