From 0893cae18c79624d8629c84d48725e2f1509d73b Mon Sep 17 00:00:00 2001 From: Sara Perdoncin Date: Wed, 17 Mar 2021 07:44:44 +0000 Subject: [PATCH] EgtMachKernel 2.3c1 : - aggiunte funzioni per importazione ed esportazione utensili. --- EgtMachKernel.rc | Bin 11774 -> 11774 bytes MachMgr.h | 3 + MachMgrDBTools.cpp | 59 ++++++++ ToolsMgr.cpp | 361 +++++++++++++++++++++++++++++++++++++++++++++ ToolsMgr.h | 6 + 5 files changed, 429 insertions(+) diff --git a/EgtMachKernel.rc b/EgtMachKernel.rc index c8b3656930f2c1d9fce9017f013bdc4f1f3a9316..668242fe19a1b08bb07ec88f840c55dabcc568d4 100644 GIT binary patch delta 265 zcmewt{V#gMI%aiGhCGIJh8%`ehGHOAV9;O)W+-7u1ma8}FBB-731Uz7R&U)L$1;P7 z)tEtt!Ep0$Hfv_)WCp{@4<&Usr*ZYLz{M{zO+F!*hmaFOl0(ts!MJ&qv delta 260 zcmewt{V#gMI%aKGhCGIJh8%`WhGK>c1_cHUhESkLCPN8BDnkm89}HwAf_cT0IklxX ze_&=}W;J5aVKCkNUhX2(Export( vsToolsNames, sOutFile) ; +} + +//---------------------------------------------------------------------------- +STRVECTOR +MachMgr::TdbToBeImported( string& sFile) +{ + if ( ! ExistsFile( sFile)){ + LOG_ERROR( GetEMkLogger(), "TdbToBeImported Error : File does not exist") ; + return STRVECTOR() ; + } + + // recupero il gestore di utensili della macchina corrente + ToolsMgr* pTsMgr = GetCurrToolsMgr() ; + if ( pTsMgr == nullptr){ + LOG_ERROR( GetEMkLogger(), "TdbToBeImported Error : no current ToolsMgr") ; + return STRVECTOR() ; + } + + return pTsMgr->ToBeImported( sFile) ; +} + + +//---------------------------------------------------------------------------- +STRVECTOR +MachMgr::TdbImport( string& sFile, STRVECTOR& vsToolsToImport, STRVECTOR& vsToolsNames) +{ + if ( vsToolsToImport.empty()) + return STRVECTOR() ; + + if ( ! ExistsFile( sFile)){ + LOG_ERROR( GetEMkLogger(), "TdbImport Error : File does not exist") ; + return STRVECTOR() ; + } + + if ( vsToolsToImport.size() != vsToolsNames.size()) + return STRVECTOR() ; + + ToolsMgr* pTsMgr = GetCurrToolsMgr() ; + if ( pTsMgr == nullptr){ + LOG_ERROR( GetEMkLogger(), "TdbImport Error : no current ToolsMgr") ; + return STRVECTOR() ; + } + + return pTsMgr->Import( sFile, vsToolsToImport, vsToolsNames) ; +} diff --git a/ToolsMgr.cpp b/ToolsMgr.cpp index cde8266..b065aac 100644 --- a/ToolsMgr.cpp +++ b/ToolsMgr.cpp @@ -24,6 +24,7 @@ #include "/EgtDEv/Include/EGnScanner.h" #include "/EgtDEv/Include/EGnWriter.h" #include "/EgtDev/Include/EgtPointerOwner.h" +#include "/EgtDev/Include/EXeExecutor.h" #include #include @@ -786,3 +787,363 @@ ToolsMgr::GetCurrToolThLength( double& dThLen) const return false ; } + + + +//---------------------------------------------------------------------------- +bool +ToolsMgr::Export( STRVECTOR& vsToolsNames, string& sOutFile, bool bCompressed) +{ + LOG_ERROR( GetEMkLogger(), ( "Export Tools " + sOutFile).c_str()) ; + + // Out Directory + size_t pos = sOutFile.rfind( "\\") ; + if ( pos == string::npos) + return false ; + string sOutDir = sOutFile.substr( 0, pos) ; + + // Verifico che i tools da esportare esistano + for ( STRVECTOR::iterator it = vsToolsNames.begin() ; it != vsToolsNames.end() ; it ++) { + if ( m_suData.find( *it) == m_suData.end()){ + // se non lo trovo fra i tools lo elimino dal vettore dei ToolsNames + string sOut = "Warning Export Tool : " + *it + " not found. Tool is ignored" ; + LOG_ERROR( GetEMkLogger(), sOut.c_str()) ; + vsToolsNames.erase( it) ; + it -- ; + } + } + + // Numero tools da salvare + size_t nTools = vsToolsNames.size() ; + + // inizializzo il writer + Writer TheWriter ; + if ( ! TheWriter.Init( sOutFile, bCompressed)) { + LOG_ERROR( GetEMkLogger(), "Export Tools : Error on Init") ; + return false ; + } + + // scrivo linea di inizio file + string sOut = "; --- " + sOutFile + " " + CurrDateTime() + " ---" ; + if ( ! TheWriter.OutText( sOut)) { + LOG_ERROR( GetEMkLogger(), "Export Tools : Error on Start") ; + return false ; + } + + // scrivo l'intestazione + bool bOk = TheWriter.OutText( TF_HEADER) ; + bOk = bOk && TheWriter.OutText( TF_VERSION + "=" + ToString( TF_CURR_VER)) ; + bOk = bOk && TheWriter.OutText( TF_TOOLSIZE + "=" + ToString( ToolData::GetSize())) ; + bOk = bOk && TheWriter.OutText( TF_TOTAL + "=" + to_string( nTools)) ; + + if ( ! bOk) { + LOG_ERROR( GetEMkLogger(), "Export Tools : Error on Header") ; + return false ; + } + + // ciclo su tutti i nomi degli utensili da esportare + int nCounter = 1 ; + for ( size_t i = 0 ; i < vsToolsNames.size() ; i++) { + if ( ! ExportOneTool( vsToolsNames[i], TheWriter, nCounter, sOutDir)) + return false ; + nCounter ++ ; + } + + // scrivo linea di fine file + if ( ! TheWriter.OutText( "; --- End ---")) { + LOG_ERROR( GetEMkLogger(), "Export Tools : Error on End") ; + return false ; + } + + // chiudo la scrittura + TheWriter.Close() ; + + return true ; +} + +//---------------------------------------------------------------------------- +bool +ToolsMgr::ExportOneTool( string& sToolName, Writer& TheWriter, int& nCounter, string& sOutDir) +{ + const ToolData * tData = GetTool( sToolName) ; + string sOut ; + if ( tData == nullptr){ + sOut = "Export Tools : Error exporting " + sToolName ; + LOG_ERROR( GetEMkLogger(), sOut.c_str()) ; + return false ; + } + + // Tool draw + string sDraw = m_sToolsDir + "\\" + tData->m_sDraw ; + string sOutDraw = sOutDir + "\\" + tData->m_sDraw ; + if ( ! CopyToolDraw( sDraw, sOutDraw, sToolName)) + return false ; + + // Scrittura del file + sOut = "[TOOL_" + ToString( nCounter, 3) + "]" ; + bool bOk = TheWriter.OutText( sOut) ; + for ( int i = 0 ; bOk && i < tData->GetSize() ; ++ i) { + sOut = tData->ToString( i) ; + bOk = ! sOut.empty() && TheWriter.OutText( sOut) ; + } + + if ( ! bOk){ + sOut = "Export Tools : Error exporting " + sToolName ; + LOG_ERROR( GetEMkLogger(), sOut.c_str()) ; + // visto che non ho salvato correttamente il tool elimino il suo disegno + EraseFile( sOutDraw) ; + return false ; + } + + return true ; +} + + +//---------------------------------------------------------------------------- +STRVECTOR +ToolsMgr::ToBeImported( string& sFile) +{ + // Vettore con i nomi di tutti i tools che posso importare da sFile. + STRVECTOR vsToolsNames ; + + Scanner TheScanner ; + if ( ! TheScanner.Init( sFile, ";")) { + LOG_ERROR( GetEMkLogger(), "Tools ToBeImported : Error on Init") ; + return STRVECTOR() ; + } + + // leggo l'intestazione + int nVersion = 0 ; + int nToolSize = 0 ; + int nTotal = 0 ; + bool bEnd = false ; + if ( ! LoadHeader( TheScanner, nVersion, nToolSize, nTotal, bEnd)){ + LOG_ERROR( GetEMkLogger(), "Tools ToBeImported : Error on Header") ; + } + + string sLine ; + if ( ! TheScanner.GetLine( sLine)) + return STRVECTOR() ; + if ( sLine.front() != '[' || sLine.back() != ']') + return STRVECTOR() ; + + bool bToolName = false ; + bool bRemoved = false ; + // cerco i nomi di tutti i tool del file + while ( TheScanner.GetLine( sLine)) { + if ( sLine.front() == '[' && sLine.back() == ']') { // se è intestazione + bToolName = false ; + bRemoved = false ; + } + else { + string sKey, sVal ; + SplitFirst( sLine, "=", sKey, sVal) ; + if( ToUpper( sKey) == "NAME") { + if ( ! bToolName){ + bToolName = true ; + vsToolsNames.push_back( sVal) ; + } + else if ( ! bRemoved) { // se tool ha più nomi è errore, lo elimino dal vettore + vsToolsNames.pop_back() ; + bRemoved = true ; + } + } + } + } + + return vsToolsNames ; +} + +//---------------------------------------------------------------------------- +STRVECTOR +ToolsMgr::Import( string& sFile, STRVECTOR& vsToolsToImport, STRVECTOR& vsToolsNames) +{ + LOG_ERROR( GetEMkLogger(), ( "Import Tools : " + sFile).c_str()) ; + STRVECTOR vsImported ; + + // Verifico validità dei ToolsNames + for ( size_t i = 0 ; i < vsToolsNames.size() ; i ++) { + if ( m_suData.find( vsToolsNames[i]) != m_suData.end()){ + LOG_ERROR( GetEMkLogger(), ( " Error : name \"" + vsToolsNames[i] + "\" is already used").c_str()) ; + return STRVECTOR() ; + } + if ( i != vsToolsNames.size() - 1 && find( vsToolsNames.begin() + i + 1, vsToolsNames.end(), vsToolsNames[i]) != vsToolsNames.end()){ + LOG_ERROR( GetEMkLogger(), ( " Error : name \"" + vsToolsNames[i] + "\" is already used").c_str()) ; + return STRVECTOR() ; + } + } + + // sFile directory + size_t pos = sFile.rfind( "\\") ; + if ( pos == string::npos) + return STRVECTOR() ; + string sFileDir = sFile.substr( 0, pos) ; + + // Inizializzo lo scanner + Scanner TheScanner ; + if ( ! TheScanner.Init( sFile, ";")) { + LOG_ERROR( GetEMkLogger(), " Import Tools : Error on Init") ; + return STRVECTOR() ; + } + // leggo l'intestazione + int nVersion = 0 ; + int nToolSize = 0 ; + int nTotal = 0 ; + bool bEnd ; + bool bOk = true ; + if ( ! LoadHeader( TheScanner, nVersion, nToolSize, nTotal, bEnd)) { + bOk = false ; + LOG_ERROR( GetEMkLogger(), " Import Tools : Error on Header") ; + } + + // leggo i tool + string sLine ; + if( ! TheScanner.GetLine( sLine)) + return STRVECTOR() ; + if ( sLine.front() != '[' || sLine.back() != ']') + return STRVECTOR() ; + TheScanner.UngetLine( sLine) ; + + while ( TheScanner.GetLine( sLine)) { + if ( sLine.front() == '[' && sLine.back() == ']') { // nuovo tool + TheScanner.UngetLine( sLine) ; + ToolData tData ; + bOk = ReadTool( TheScanner, tData, nToolSize) ; + vector::iterator it = find( vsToolsToImport.begin(), vsToolsToImport.end(), tData.m_sName) ; + + // Se il tool non deve essere importato proseguo con il successivo + if ( it == vsToolsToImport.end()) + continue ; + + // Se errore nel leggere il tool, proseguo con il successivo + if ( ! bOk) { + string sOut = " Error : " + vsToolsNames[ it - vsToolsToImport.begin()] + " could not be imported" ; + LOG_ERROR( GetEMkLogger(), sOut.c_str()) ; + continue ; + } + + // Assegno il nuovo nome + tData.m_sName = vsToolsNames[ it - vsToolsToImport.begin()] ; + + string sDraw = sFileDir + "\\" + tData.m_sDraw ; + // Se il suo UUID esiste già nel DB lo modifico + if ( m_utData.find( tData.m_Uuid) != m_utData.end()) { + LOG_ERROR( GetEMkLogger(), ( " Warning : " + tData.m_sName + " UUID changed").c_str()) ; + CreateEgtUUID( tData.m_Uuid) ; + // se il nome del disegno è legato all'UUID, devo aggiornarlo con il nuovo valore + if ( IsUUID( GetFileTitleEgt( sDraw))) + tData.m_sDraw = ToString( tData.m_Uuid) + ".nge" ; + } + + // Aggiungo il tool + bOk = m_utData.emplace( tData.m_Uuid, tData).second ; + bOk = bOk && m_suData.emplace( tData.m_sName, tData.m_Uuid).second ; + if ( ! bOk){ + LOG_ERROR( GetEMkLogger(), ( " Error adding " + tData.m_sName).c_str()) ; + continue ; + } + + string sOutDraw = m_sToolsDir + "\\" + tData.m_sDraw ; + // se fallisco nel copiare il draw elimino il tool dal DB + if ( ! CopyToolDraw( sDraw, sOutDraw, tData.m_sName)){ + m_utData.erase( tData.m_Uuid) ; + m_suData.erase( tData.m_sName) ; + } + else + vsImported.push_back( tData.m_sName) ; + } + } + + if ( ! vsImported.empty()) + m_bModified = true ; + + return vsImported ; +} + + +//-------------------------------------------------------------------------- +bool +ToolsMgr::ReadTool( Scanner& TheScanner, ToolData& tData, int& nToolSize) +{ + string sLine ; + TheScanner.GetLine( sLine) ; + // deve essere intestazione + if ( sLine.front() != '[' || sLine.back() != ']') + return false ; + + // uso temporaneamente come nome utensile + tData.m_sName = sLine.substr( 1, sLine.length() - 2) ; + bool bOk = ! tData.m_sName.empty() ; + // dati successivi + const int DIM_BS = 64 ; + bitset Flag ; + assert( nToolSize <= DIM_BS) ; + while ( bOk && TheScanner.GetLine( sLine)) { + // se utensile successivo + if ( sLine.front() == '[' && sLine.back() == ']') { + TheScanner.UngetLine( sLine) ; + break ; + } + // interpreto la linea + int nKey = - 1 ; + bOk = tData.FromString( sLine, nKey) ; + // se tutto bene, dichiaro letto il campo + if ( bOk) + Flag.set( nKey) ; + } + + // verifico di aver letto tutti i campi + bOk = bOk && ( Flag.count() == nToolSize) ; + + return bOk ; +} + + +//---------------------------------------------------------------------------- +bool +ToolsMgr::CopyToolDraw( string& sDraw, string& sOutDraw, string& sToolName) +{ + // se il file del disegno esiste lo copio nella nuova cartella + if ( ExistsFile( sDraw)){ + if ( ! CopyFileEgt( sDraw, sOutDraw)) { + string sOut = " Error in copying " + sToolName + " draw" ; + LOG_ERROR( GetEMkLogger(), sOut.c_str()) ; + return false ; + } + } + else { + // se il file non esiste e non è standard restituisco errore + if ( ! IsUUID( GetFileTitleEgt( sDraw))) { + string sOut = " Error : " + sToolName + " draw not found" ; + LOG_ERROR( GetEMkLogger(), sOut.c_str()) ; + return false ; + } + // se il file non esiste ma è standard lo ricreo + else { + int nGenCtx = ExeGetCurrentContext() ; + int nToolCtx = ExeInitContext() ; + + bool bOk = SetCurrTool( sToolName) ; // setto il tool corrente per farne il disegno + if ( ! bOk){ + string sOut = " Error in " + sToolName + " draw" ; + LOG_ERROR( GetEMkLogger(), sOut.c_str()) ; + return false ; + } + + if ( ExeTdbCurrToolDraw( nGenCtx, nToolCtx)) { + string sOut = " Error : " + sToolName + " CurrToolDraw" ; + LOG_ERROR( GetEMkLogger(), sOut.c_str()) ; + return false ; + } + if ( ! ExeSaveFile( sOutDraw, GDB_SV_CMPTXT)) { + string sOut = " Error : saving " + sToolName + " tool draw" ; + LOG_ERROR( GetEMkLogger(), sOut.c_str()) ; + return false ; + } + ExeSetCurrentContext( nGenCtx) ; + ExeDeleteContext( nToolCtx) ; + } + } + return true ; +} + diff --git a/ToolsMgr.h b/ToolsMgr.h index b8aa1e6..d63b51e 100644 --- a/ToolsMgr.h +++ b/ToolsMgr.h @@ -61,6 +61,9 @@ class ToolsMgr bool GetCurrToolThLength( double& dThLen) const ; const ToolData* GetCurrTool(void) const { return (m_bCurrTool ? &m_tdCurrTool : nullptr) ; } + bool Export( STRVECTOR& vsToolsNames, std::string& sOutFile, bool bCompressed = true) ; + STRVECTOR ToBeImported( std::string& sFile) ; + STRVECTOR Import( std::string& sFile, STRVECTOR& vsToolsToImport, STRVECTOR& vsToolsNames) ; private : bool Clear( void) ; @@ -70,6 +73,9 @@ class ToolsMgr bool SaveOneTool( const EgtUUID& Uuid, int& nCounter, Writer& TheWriter) const ; bool VerifyTool( int nFamily, std::string& sName, int& nType) const ; bool UpdateCurrToolHolderData( void) ; + bool ExportOneTool( std::string& sToolName, Writer& TheWriter, int& nCounter, std::string& sOutDir) ; + bool ReadTool( Scanner& TheScanner, ToolData& tData, int& nToolSize) ; + bool CopyToolDraw( std::string& sDraw, std::string& sOutDraw, std::string& sToolName) ; private : typedef std::unordered_map< EgtUUID, ToolData> UUIDTDATA_UMAP ;