843282c234
- aggiunta prima versione del postprocessore - corretto salvataggio angolo rotazione per fixture - aggiunta GetParam di lavorazioni applicate - migliorata gestione DB lavorazioni con versioni e size nel file - corretto Clone di SawingData.
453 lines
15 KiB
C++
453 lines
15 KiB
C++
//----------------------------------------------------------------------------
|
|
// EgalTech 2015-2015
|
|
//----------------------------------------------------------------------------
|
|
// File : MachineLua.cpp Data : 06.05.15 Versione : 1.6e3
|
|
// Contenuto : Implementazione gestione macchina : funzioni Lua.
|
|
//
|
|
//
|
|
//
|
|
// Modifiche : 06.05.15 DS Creazione modulo.
|
|
//
|
|
//
|
|
//----------------------------------------------------------------------------
|
|
|
|
//--------------------------- Include ----------------------------------------
|
|
#include "stdafx.h"
|
|
#include "MachMgr.h"
|
|
#include "DllMain.h"
|
|
#include "/EgtDev/Include/EXeExecutor.h"
|
|
#include "/EgtDev/Include/EGkGeomDB.h"
|
|
#include "/EgtDev/Include/EGkLuaAux.h"
|
|
#include "/EgtDev/Include/EGnStringUtils.h"
|
|
#include "/EgtDev/Include/EGnFileUtils.h"
|
|
|
|
using namespace std ;
|
|
|
|
//----------------------------------------------------------------------------
|
|
Machine* Machine::m_pMchLua = nullptr ;
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
Machine::LuaInit( const string& sMachineName)
|
|
{
|
|
// inizializzo l'interprete lua
|
|
if ( ! m_LuaMgr.Init())
|
|
return false ;
|
|
// carico le funzioni speciali
|
|
if ( ! LuaInstallEgtFunctions( m_LuaMgr))
|
|
return false ;
|
|
// recupero la versione di Lua
|
|
string sLua ;
|
|
if ( m_LuaMgr.GetVersion( sLua))
|
|
sLua += " machine interpreter started" ;
|
|
else
|
|
sLua = "Lua *.* machine interpreter started" ;
|
|
sLua += " (" + sMachineName + ")" ;
|
|
LOG_INFO( GetEMkLogger(), sLua.c_str())
|
|
// imposto il direttorio delle librerie
|
|
m_LuaMgr.SetLuaLibsDir( m_pMchMgr->GetLuaLibsDir()) ;
|
|
// carico la libreria standard
|
|
m_LuaMgr.Require( m_pMchMgr->GetLuaLastRequire()) ;
|
|
// registro le funzioni di lettura macchina per lua
|
|
m_LuaMgr.RegisterFunction( "EmtGeneral", Machine::LuaEmtGeneral) ;
|
|
m_LuaMgr.RegisterFunction( "EmtBase", Machine::LuaEmtBase) ;
|
|
m_LuaMgr.RegisterFunction( "EmtTable", Machine::LuaEmtTable) ;
|
|
m_LuaMgr.RegisterFunction( "EmtAxis", Machine::LuaEmtAxis) ;
|
|
m_LuaMgr.RegisterFunction( "EmtHead", Machine::LuaEmtHead) ;
|
|
// registro le funzioni di scrittura part program per lua
|
|
m_LuaMgr.RegisterFunction( "EmtWrite", Machine::LuaEmtWrite) ;
|
|
|
|
return true ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
Machine::LuaExit( void)
|
|
{
|
|
// chiudo lua ( se era aperto)
|
|
if ( m_LuaMgr.Exit()) {
|
|
string sLua = "Lua machine interpreter closed (" + m_sName + ")" ;
|
|
LOG_INFO( GetEMkLogger(), sLua.c_str())
|
|
}
|
|
return true ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
Machine::LuaCallFunction( const string& sFun)
|
|
{
|
|
// imposto contesto corretto
|
|
int nOldCtx = ExeGetCurrentContext() ;
|
|
if ( nOldCtx != m_pMchMgr->GetContextId())
|
|
ExeSetCurrentContext( m_pMchMgr->GetContextId()) ;
|
|
// imposto l'oggetto corrente per Lua
|
|
m_pMchLua = this ;
|
|
// eseguo la funzione
|
|
bool bOk = m_LuaMgr.CallFunction( sFun, 0) ;
|
|
// ripristino contesto originale
|
|
if ( nOldCtx != m_pMchMgr->GetContextId())
|
|
ExeSetCurrentContext( nOldCtx) ;
|
|
return bOk ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
Machine::LuaCreateGlobTable( const string& sName)
|
|
{
|
|
// imposto contesto corretto
|
|
int nOldCtx = ExeGetCurrentContext() ;
|
|
if ( nOldCtx != m_pMchMgr->GetContextId())
|
|
ExeSetCurrentContext( m_pMchMgr->GetContextId()) ;
|
|
// imposto l'oggetto corrente per Lua
|
|
m_pMchLua = this ;
|
|
// eseguo la funzione
|
|
bool bOk = ::LuaCreateGlobTable( m_LuaMgr.GetLuaState(), sName) ;
|
|
// ripristino contesto originale
|
|
if ( nOldCtx != m_pMchMgr->GetContextId())
|
|
ExeSetCurrentContext( nOldCtx) ;
|
|
return bOk ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
Machine::LuaResetGlobVar( const string& sName)
|
|
{
|
|
// imposto contesto corretto
|
|
int nOldCtx = ExeGetCurrentContext() ;
|
|
if ( nOldCtx != m_pMchMgr->GetContextId())
|
|
ExeSetCurrentContext( m_pMchMgr->GetContextId()) ;
|
|
// imposto l'oggetto corrente per Lua
|
|
m_pMchLua = this ;
|
|
// eseguo la funzione
|
|
bool bOk = ::LuaResetGlobVar( m_LuaMgr.GetLuaState(), sName) ;
|
|
// ripristino contesto originale
|
|
if ( nOldCtx != m_pMchMgr->GetContextId())
|
|
ExeSetCurrentContext( nOldCtx) ;
|
|
return bOk ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
Machine::LuaLoadMachine( const string& sFile)
|
|
{
|
|
// info
|
|
string sOut = "LuaLoadMachine : " + sFile ;
|
|
LOG_INFO( GetEMkLogger(), sOut.c_str())
|
|
// imposto contesto corretto
|
|
int nOldCtx = ExeGetCurrentContext() ;
|
|
if ( nOldCtx != m_pMchMgr->GetContextId())
|
|
ExeSetCurrentContext( m_pMchMgr->GetContextId()) ;
|
|
// imposto l'oggetto corrente per Lua
|
|
m_pMchLua = this ;
|
|
// carico la macchina
|
|
bool bOk = m_LuaMgr.ExecFile( sFile) ;
|
|
// ripristino contesto originale
|
|
if ( nOldCtx != m_pMchMgr->GetContextId())
|
|
ExeSetCurrentContext( nOldCtx) ;
|
|
return bOk ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
int
|
|
Machine::LuaEmtGeneral( lua_State* L)
|
|
{
|
|
// Il parametro 1 deve essere una tabella
|
|
if ( ! lua_istable( L, 1))
|
|
return luaL_error( L, " Invalid Parameter, required a table") ;
|
|
// lettura campo 'File' dalla tabella
|
|
string sFile ;
|
|
LuaCheckTabFieldParam( L, 1, "File", sFile)
|
|
// lettura eventuale campo 'Offset' dalla tabella (default 0,0,0)
|
|
Vector3d vtOffset ;
|
|
LuaGetTabFieldParam( L, 1, "Offset", vtOffset) ;
|
|
// lettura eventuale campo 'Processor' dalla tabella
|
|
string sProcessor ;
|
|
LuaGetTabFieldParam( L, 1, "Processor", sProcessor) ;
|
|
LuaClearStack( L) ;
|
|
|
|
// info
|
|
string sOut = "LuaEmtGeneral : " + sFile ;
|
|
if ( ! sProcessor.empty())
|
|
sOut += ", " + sProcessor ;
|
|
LOG_INFO( GetEMkLogger(), sOut.c_str())
|
|
|
|
// verifico ci sia una macchina attiva
|
|
if ( m_pMchLua == nullptr)
|
|
return luaL_error( L, " Unknown Machine") ;
|
|
|
|
// carico la geometria della macchina
|
|
if ( ! m_pMchLua->LoadMachineGeometry( sFile, vtOffset))
|
|
return luaL_error( L, " Load Machine failed") ;
|
|
|
|
// carico il processore della macchina
|
|
if ( ! sProcessor.empty()) {
|
|
// carico il file lua del processore
|
|
string sProcPath = m_pMchLua->m_sMachineDir + "\\" + sProcessor ;
|
|
if ( ! m_pMchLua->m_LuaMgr.ExecFile( sProcPath))
|
|
return luaL_error( L, " Load Processor failed") ;
|
|
}
|
|
else {
|
|
LOG_INFO( GetEMkLogger(), " Processor file not specified")
|
|
}
|
|
|
|
return 0 ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
int
|
|
Machine::LuaEmtBase( lua_State* L)
|
|
{
|
|
// Il parametro 1 deve essere una tabella
|
|
if ( ! lua_istable( L, 1))
|
|
return luaL_error( L, " Invalid Parameter, required a table") ;
|
|
// lettura campo 'Name' dalla tabella
|
|
string sName ;
|
|
LuaCheckTabFieldParam( L, 1, "Name", sName)
|
|
// lettura campo 'Geo' dalla tabella
|
|
string sGeo ;
|
|
LuaCheckTabFieldParam( L, 1, "Geo", sGeo)
|
|
LuaClearStack( L) ;
|
|
|
|
// info
|
|
string sOut = "LuaEmtBase : " + sName ;
|
|
LOG_INFO( GetEMkLogger(), sOut.c_str())
|
|
|
|
// verifico ci sia una macchina attiva
|
|
if ( m_pMchLua == nullptr)
|
|
return luaL_error( L, " Unknown Machine") ;
|
|
|
|
// carico i dati della base
|
|
if ( ! m_pMchLua->LoadMachineBase( sName, sGeo))
|
|
return luaL_error( L, " Load Machine Base failed") ;
|
|
|
|
return 0 ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
int
|
|
Machine::LuaEmtTable( lua_State* L)
|
|
{
|
|
// Il parametro 1 deve essere una tabella
|
|
if ( ! lua_istable( L, 1))
|
|
return luaL_error( L, " Invalid Parameter, required a table") ;
|
|
// lettura campo 'Name' dalla tabella
|
|
string sName ;
|
|
LuaCheckTabFieldParam( L, 1, "Name", sName)
|
|
// lettura campo 'Parent' dalla tabella
|
|
string sParent ;
|
|
LuaCheckTabFieldParam( L, 1, "Parent", sParent)
|
|
// lettura campo 'Type' dalla tabella
|
|
int nType ;
|
|
LuaCheckTabFieldParam( L, 1, "Type", nType)
|
|
// lettura campo 'Ref1' dalla tabella
|
|
Point3d Ref1 ;
|
|
LuaCheckTabFieldParam( L, 1, "Ref1", Ref1)
|
|
// lettura campo 'Geo' dalla tabella
|
|
string sGeo ;
|
|
LuaCheckTabFieldParam( L, 1, "Geo", sGeo)
|
|
LuaClearStack( L) ;
|
|
|
|
// info
|
|
string sOut = "LuaEmtTable : " + sName ;
|
|
LOG_INFO( GetEMkLogger(), sOut.c_str())
|
|
|
|
// verifico ci sia una macchina attiva
|
|
if ( m_pMchLua == nullptr)
|
|
return luaL_error( L, " Unknown Machine") ;
|
|
|
|
// carico i dati della tavola
|
|
if ( ! m_pMchLua->LoadMachineTable( sName, sParent, nType, Ref1, sGeo))
|
|
return luaL_error( L, " Load Machine Table failed") ;
|
|
|
|
return 0 ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
int
|
|
Machine::LuaEmtAxis( lua_State* L)
|
|
{
|
|
// Il parametro 1 deve essere una tabella
|
|
if ( ! lua_istable( L, 1))
|
|
return luaL_error( L, " Invalid Parameter, required a table") ;
|
|
// lettura campo 'Name' dalla tabella
|
|
string sName ;
|
|
LuaCheckTabFieldParam( L, 1, "Name", sName)
|
|
// lettura campo 'Parent' dalla tabella
|
|
string sParent ;
|
|
LuaCheckTabFieldParam( L, 1, "Parent", sParent)
|
|
// lettura campo 'Type' dalla tabella
|
|
int nType ;
|
|
LuaCheckTabFieldParam( L, 1, "Type", nType)
|
|
// lettura campo 'Pos' dalla tabella
|
|
Point3d ptPos ;
|
|
LuaCheckTabFieldParam( L, 1, "Pos", ptPos)
|
|
// lettura campo 'Dir' dalla tabella
|
|
Vector3d vtDir ;
|
|
LuaCheckTabFieldParam( L, 1, "Dir", vtDir)
|
|
// lettura campo 'Stroke' dalla tabella
|
|
STROKE Stroke ;
|
|
LuaCheckTabFieldParam( L, 1, "Stroke", Stroke.v)
|
|
// lettura campo 'Geo' dalla tabella
|
|
string sGeo ;
|
|
LuaCheckTabFieldParam( L, 1, "Geo", sGeo)
|
|
// lettura eventuale campo 'Home' dalla tabella (default 0)
|
|
double dHome = 0 ;
|
|
LuaGetTabFieldParam( L, 1, "Home", dHome) ;
|
|
LuaClearStack( L) ;
|
|
|
|
// info
|
|
string sOut = "LuaEmtAxis : " + sName ;
|
|
LOG_INFO( GetEMkLogger(), sOut.c_str())
|
|
|
|
// verifico ci sia una macchina attiva
|
|
if ( m_pMchLua == nullptr)
|
|
return luaL_error( L, " Unknown Machine") ;
|
|
|
|
// carico i dati dell'asse
|
|
if ( ! m_pMchLua->LoadMachineAxis( sName, sParent, nType, ptPos, vtDir, Stroke, sGeo, dHome))
|
|
return luaL_error( L, " Load Machine Axis failed") ;
|
|
|
|
return 0 ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
int
|
|
Machine::LuaEmtHead( lua_State* L)
|
|
{
|
|
// Il parametro 1 deve essere una tabella
|
|
if ( ! lua_istable( L, 1))
|
|
return luaL_error( L, " Invalid Parameter, required a table") ;
|
|
// lettura campo 'Type' dalla tabella
|
|
int nType ;
|
|
LuaCheckTabFieldParam( L, 1, "Type", nType)
|
|
|
|
// Procedo alla lettura a seconda del tipo
|
|
if ( nType == MCH_HT_STD)
|
|
return LuaEmtStdHead( L) ;
|
|
else if ( nType == MCH_HT_MULTI)
|
|
return LuaEmtMultiHead( L) ;
|
|
else
|
|
return luaL_error( L, " Head type unknown") ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
int
|
|
Machine::LuaEmtStdHead( lua_State* L)
|
|
{
|
|
// Il parametro 1 deve essere una tabella
|
|
if ( ! lua_istable( L, 1))
|
|
return luaL_error( L, " Invalid Parameter, required a table") ;
|
|
// lettura campo 'Name' dalla tabella
|
|
string sName ;
|
|
LuaCheckTabFieldParam( L, 1, "Name", sName)
|
|
// lettura campo 'Parent' dalla tabella
|
|
string sParent ;
|
|
LuaCheckTabFieldParam( L, 1, "Parent", sParent)
|
|
// lettura campo 'HSet' dalla tabella
|
|
string sHSet ;
|
|
LuaCheckTabFieldParam( L, 1, "HSet", sHSet)
|
|
// lettura campo 'Pos' dalla tabella
|
|
Point3d ptPos ;
|
|
LuaCheckTabFieldParam( L, 1, "Pos", ptPos)
|
|
// lettura campo 'TDir' dalla tabella
|
|
Vector3d vtTDir ;
|
|
LuaCheckTabFieldParam( L, 1, "TDir", vtTDir)
|
|
// lettura campo 'ADir' dalla tabella
|
|
Vector3d vtADir ;
|
|
LuaCheckTabFieldParam( L, 1, "ADir", vtADir)
|
|
// lettura eventuale campo 'Rot1W' dalla tabella (default 1)
|
|
double dRot1W = 1 ;
|
|
LuaGetTabFieldParam( L, 1, "Rot1W", dRot1W) ;
|
|
// lettura campo 'Geo' dalla tabella
|
|
string sGeo ;
|
|
LuaCheckTabFieldParam( L, 1, "Geo", sGeo)
|
|
LuaClearStack( L) ;
|
|
|
|
// info
|
|
string sOut = "LuaEmtStdHead : " + sName ;
|
|
LOG_INFO( GetEMkLogger(), sOut.c_str())
|
|
|
|
// verifico ci sia una macchina attiva
|
|
if ( m_pMchLua == nullptr)
|
|
return luaL_error( L, " Unknown Machine") ;
|
|
|
|
// carico i dati della testa standard
|
|
if ( ! m_pMchLua->LoadMachineStdHead( sName, sParent, sHSet, ptPos, vtTDir, vtADir, dRot1W, sGeo))
|
|
return luaL_error( L, " Load Machine Standard Head failed") ;
|
|
|
|
return 0 ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
int
|
|
Machine::LuaEmtMultiHead( lua_State* L)
|
|
{
|
|
// Il parametro 1 deve essere una tabella
|
|
if ( ! lua_istable( L, 1))
|
|
return luaL_error( L, " Invalid Parameter, required a table") ;
|
|
// lettura campo 'Name' dalla tabella
|
|
string sName ;
|
|
LuaCheckTabFieldParam( L, 1, "Name", sName)
|
|
// lettura campo 'Parent' dalla tabella
|
|
string sParent ;
|
|
LuaCheckTabFieldParam( L, 1, "Parent", sParent)
|
|
// lettura campo 'HSet' dalla tabella
|
|
string sHSet ;
|
|
LuaCheckTabFieldParam( L, 1, "HSet", sHSet)
|
|
// lettura campo 'ExitNbr' dalla tabella
|
|
int nExitNbr ;
|
|
LuaCheckTabFieldParam( L, 1, "ExitNbr", nExitNbr)
|
|
// lettura campi 'PosN' e 'TDirN' per ogni uscita dalla tabella
|
|
MUEXITVECTOR vMuExit ;
|
|
vMuExit.reserve( nExitNbr) ;
|
|
for ( int i = 0 ; i < nExitNbr ; ++ i) {
|
|
// lettura
|
|
string sPos = "Pos" + ToString( i + 1) ;
|
|
Point3d ptPos ;
|
|
LuaCheckTabFieldParam( L, 1, sPos.c_str(), ptPos)
|
|
string sTDir = "TDir" + ToString( i + 1) ;
|
|
Vector3d vtTDir ;
|
|
LuaCheckTabFieldParam( L, 1, sTDir.c_str(), vtTDir)
|
|
// inserimento nell'array
|
|
vMuExit.emplace_back( ptPos, vtTDir) ;
|
|
}
|
|
// lettura campo 'ADir' dalla tabella
|
|
Vector3d vtADir ;
|
|
LuaCheckTabFieldParam( L, 1, "ADir", vtADir)
|
|
// lettura eventuale campo 'Rot1W' dalla tabella (default 1)
|
|
double dRot1W = 1 ;
|
|
LuaGetTabFieldParam( L, 1, "Rot1W", dRot1W) ;
|
|
// lettura campo 'Geo' dalla tabella
|
|
string sGeo ;
|
|
LuaCheckTabFieldParam( L, 1, "Geo", sGeo)
|
|
LuaClearStack( L) ;
|
|
|
|
// info
|
|
string sOut = "LuaEmtMultiHead : " + sName ;
|
|
LOG_INFO( GetEMkLogger(), sOut.c_str())
|
|
|
|
// verifico ci sia una macchina attiva
|
|
if ( m_pMchLua == nullptr)
|
|
return luaL_error( L, " Unknown Machine") ;
|
|
|
|
// carico i dati della testa multipla
|
|
if ( ! m_pMchLua->LoadMachineMultiHead( sName, sParent, sHSet, vMuExit, vtADir, dRot1W, sGeo))
|
|
return luaL_error( L, " Load Machine Standard Head failed") ;
|
|
|
|
return 0 ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
int
|
|
Machine::LuaEmtWrite( lua_State* L)
|
|
{
|
|
// 1 parametro : string
|
|
string sOut ;
|
|
LuaCheckParam( L, 1, sOut)
|
|
LuaClearStack( L) ;
|
|
// emetto stringa
|
|
bool bOk = m_pMchLua->WriterEmit( sOut) ;
|
|
// assegno risultato
|
|
LuaSetParam( L, bOk) ;
|
|
return 1 ;
|
|
} |