EgtMachKernel 2.3c1 :
- aggiunte funzioni per importazione ed esportazione utensili.
This commit is contained in:
+361
@@ -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 <cassert>
|
||||
#include <bitset>
|
||||
|
||||
@@ -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<string>::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<DIM_BS> 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 ;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user