//---------------------------------------------------------------------------- // 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 modifica macchina per lua m_LuaMgr.RegisterFunction( "EmtModifyAxisStroke", Machine::LuaEmtModifyAxisStroke) ; m_LuaMgr.RegisterFunction( "EmtModifyAxisHome", Machine::LuaEmtModifyAxisHome) ; // 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::LuaExistsFunction( 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 ; // verifico esistenza della funzione bool bOk = m_LuaMgr.ExistsFunction( sFun) ; // ripristino contesto originale if ( nOldCtx != m_pMchMgr->GetContextId()) ExeSetCurrentContext( nOldCtx) ; // reset dell'oggetto corrente per Lua m_pMchLua = nullptr ; return bOk ; } //---------------------------------------------------------------------------- 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) ; // reset dell'oggetto corrente per Lua m_pMchLua = nullptr ; 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) ; // reset dell'oggetto corrente per Lua m_pMchLua = nullptr ; 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) ; // reset dell'oggetto corrente per Lua m_pMchLua = nullptr ; 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) ; // reset dell'oggetto corrente per Lua m_pMchLua = nullptr ; 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) ; // lettura eventuale campo 'AxisMaxAdjust' dalla tabella (default EPS_SMALL) double dAxisMaxAdjust = EPS_SMALL ; LuaGetTabFieldParam( L, 1, "AxisMaxAdjust", dAxisMaxAdjust) ; // lettura eventuale campo 'ExitMaxAdjust' dalla tabella (default EPS_SMALL) double dExitMaxAdjust = EPS_SMALL ; LuaGetTabFieldParam( L, 1, "ExitMaxAdjust", dExitMaxAdjust) ; 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") } // imposto massimo spostamento assi e uscite tra definizione cinematica e geometria m_pMchLua->m_dAxisMaxAdjust = dAxisMaxAdjust ; m_pMchLua->m_dExitMaxAdjust = dExitMaxAdjust ; 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 eventuale campo 'ADir' dalla tabella Vector3d vtADir ; LuaGetTabFieldParam( L, 1, "ADir", vtADir) ; // lettura eventuale campo 'Rot1W' dalla tabella (default 1) double dRot1W = 1 ; LuaGetTabFieldParam( L, 1, "Rot1W", dRot1W) ; // lettura eventuale campo 'Rot2Stroke' dalla tabella STROKE Rot2Stroke = { - INFINITO, INFINITO} ; LuaGetTabFieldParam( L, 1, "Rot2Stroke", Rot2Stroke.v) ; // lettura eventuale campo 'SolCh' dalla tabella int nSolCh = MCH_SCC_NONE ; LuaGetTabFieldParam( L, 1, "SolCh", nSolCh) ; // 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, Rot2Stroke, nSolCh, 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 ; LuaGetTabFieldParam( L, 1, "ADir", vtADir) ; // lettura eventuale campo 'Rot1W' dalla tabella (default 1) double dRot1W = 1 ; LuaGetTabFieldParam( L, 1, "Rot1W", dRot1W) ; // lettura eventuale campo 'Rot2Stroke' dalla tabella STROKE Rot2Stroke = { - INFINITO, INFINITO} ; LuaGetTabFieldParam( L, 1, "Rot2Stroke", Rot2Stroke.v) ; // lettura eventuale campo 'SolCh' dalla tabella int nSolCh = MCH_SCC_NONE ; LuaGetTabFieldParam( L, 1, "SolCh", nSolCh) ; // 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, Rot2Stroke, nSolCh, sGeo)) return luaL_error( L, " Load Machine Standard Head failed") ; return 0 ; } //---------------------------------------------------------------------------- int Machine::LuaEmtModifyAxisStroke( lua_State* L) { // 2 parametri : sAxisName, {dMin, dMax} string sAxisName ; LuaCheckParam( L, 1, sAxisName) STROKE Stroke ; LuaCheckParam( L, 2, Stroke.v) LuaClearStack( L) ; // verifico ci sia una macchina attiva if ( m_pMchLua == nullptr) return luaL_error( L, " Unknown Machine") ; // modifico i limiti di corsa dell'asse bool bOk = m_pMchLua->ModifyMachineAxisStroke( sAxisName, Stroke) ; // assegno risultato LuaSetParam( L, bOk) ; return 1 ; } //---------------------------------------------------------------------------- int Machine::LuaEmtModifyAxisHome( lua_State* L) { // 2 parametri : sAxisName, dHome string sAxisName ; LuaCheckParam( L, 1, sAxisName) double dHome ; LuaCheckParam( L, 2, dHome) LuaClearStack( L) ; // verifico ci sia una macchina attiva if ( m_pMchLua == nullptr) return luaL_error( L, " Unknown Machine") ; // modifico la posizione home dell'asse bool bOk = m_pMchLua->ModifyMachineAxisHome( sAxisName, dHome) ; // assegno risultato LuaSetParam( L, bOk) ; return 1 ; } //---------------------------------------------------------------------------- int Machine::LuaEmtWrite( lua_State* L) { // 1 parametro : string string sOut ; LuaCheckParam( L, 1, sOut) LuaClearStack( L) ; // verifico ci sia una macchina attiva if ( m_pMchLua == nullptr) return luaL_error( L, " Unknown Machine") ; // emetto stringa bool bOk = m_pMchLua->WriterEmit( sOut) ; // assegno risultato LuaSetParam( L, bOk) ; return 1 ; }