Files
EgtMachKernel/MachMgrPhases.cpp
Dario Sassi c2d2db28cd EgtMachKernel :
- correzione a RemoveLastPhase per casi "strani".
2019-07-10 17:27:55 +00:00

266 lines
9.3 KiB
C++

//----------------------------------------------------------------------------
// EgalTech 2016-2016
//----------------------------------------------------------------------------
// File : MachMgrPhases.cpp Data : 09.02.16 Versione : 1.6n3
// Contenuto : Implementazione gestione fasi della classe MachMgr.
//
//
//
// Modifiche : 09.02.16 DS Creazione modulo.
//
//
//----------------------------------------------------------------------------
//--------------------------- Include ----------------------------------------
#include "stdafx.h"
#include "DllMain.h"
#include "MachMgr.h"
#include "Disposition.h"
#include "Machining.h"
#include "MachConst.h"
#include "/EgtDev/Include/EGkGdbIterator.h"
#include "/EgtDev/Include/EgtPointerOwner.h"
using namespace std ;
//----------------------------------------------------------------------------
int
MachMgr::AddPhase( void)
{
// recupero la tavola della fase precedente
string sTable ;
int nDispId = GetFirstOperation() ;
while ( nDispId != GDB_ID_NULL) {
const Disposition* pDisp = ::GetDisposition( m_pGeomDB->GetUserObj( nDispId)) ;
if ( pDisp != nullptr)
pDisp->GetTable( sTable) ;
nDispId = GetNextOperation( nDispId) ;
}
// incremento il numero totale delle fasi
++ m_nPhasesCount ;
// imposto la nuova fase corrente
PrepareCurrPhase( m_nPhasesCount, false, true) ;
// inserisco la prima operazione di fase, sempre una disposizione
string sName = "Disp" + ToString( m_nCurrPhase, 2) ;
m_nCurrDispId = AddDisposition( sName) ;
if ( m_nCurrDispId == GDB_ID_NULL)
return 0 ;
// assegno la tavola della disposizione precedente
if ( ! sTable.empty()) {
Disposition* pDisp = ::GetDisposition( m_pGeomDB->GetUserObj( m_nCurrDispId)) ;
pDisp->SetTable( sTable) ;
}
return m_nCurrPhase ;
}
//----------------------------------------------------------------------------
bool
MachMgr::SetCurrPhase( int nPhase, bool bForced)
{
return PrepareCurrPhase( nPhase, true, bForced) ;
}
//----------------------------------------------------------------------------
bool
MachMgr::PrepareCurrPhase( int nPhase, bool bDoDisp, bool bForced)
{
// verifico esistenza macchina
Machine* pMach = GetCurrMachine() ;
if ( pMach == nullptr)
return false ;
// verifico esistenza fase
if ( nPhase <= 0 || nPhase > m_nPhasesCount)
return false ;
// se non forzato e coincide con la vecchia fase, non devo fare alcunché
if ( ! bForced && nPhase == m_nCurrPhase)
return true ;
// se c'è lavorazione corrente e non appartiene a questa fase, reset
const Machining* pMch = GetMachining( m_pGeomDB->GetUserObj( GetCurrMachining())) ;
if ( pMch != nullptr && pMch->GetPhase() != nPhase)
ResetCurrMachining() ;
// riporto tutti i bloccaggi linkati nel gruppo dei bloccaggi
pMach->UnlinkAllFixturesFromGroups() ;
// nascondo tutti i bloccaggi
int nFxtId = m_pGeomDB->GetFirstGroupInGroup( GetCurrFixtGroupId()) ;
while ( nFxtId != GDB_ID_NULL) {
m_pGeomDB->SetStatus( nFxtId, GDB_ST_OFF) ;
nFxtId = m_pGeomDB->GetNextGroup( nFxtId) ;
}
// riporto tutti i pezzi linkati nel loro grezzo
pMach->UnlinkAllPartsFromGroups() ;
// riporto tutti i grezzi linkati nel gruppo dei grezzi
pMach->UnlinkAllRawPartsFromGroups() ;
// tolgo i pezzi dai grezzi che non appartengono alla fase
int nRawId = GetFirstRawPart() ;
while ( nRawId != GDB_ID_NULL) {
// se non appartiene alla fase
if ( ! VerifyRawPartPhase( nRawId, nPhase))
SwapRawPartParts( nRawId, false) ;
// passo al prossimo
nRawId = GetNextRawPart( nRawId) ;
}
// inserisco i pezzi nei grezzi che appartengono alla fase
nRawId = GetFirstRawPart() ;
while ( nRawId != GDB_ID_NULL) {
// se appartiene alla fase
if ( VerifyRawPartPhase( nRawId, nPhase))
SwapRawPartParts( nRawId, true) ;
// passo al prossimo
nRawId = GetNextRawPart( nRawId) ;
}
// aggiorno stato grezzi
nRawId = GetFirstRawPart() ;
while ( nRawId != GDB_ID_NULL) {
// verifico se appartiene alla fase
bool bOn = VerifyRawPartPhase( nRawId, nPhase) ;
m_pGeomDB->SetStatus( nRawId, ( bOn ? GDB_ST_ON : GDB_ST_OFF)) ;
// passo al prossimo
nRawId = GetNextRawPart( nRawId) ;
}
// imposto disposizione corrente
int nDispId = GDB_ID_NULL ;
if ( bDoDisp) {
Disposition* pDisp = nullptr ;
nDispId = GetFirstOperation() ;
while ( nDispId != GDB_ID_NULL) {
pDisp = ::GetDisposition( m_pGeomDB->GetUserObj( nDispId)) ;
if ( pDisp != nullptr && pDisp->GetPhase() == nPhase)
break ;
else
pDisp = nullptr ;
nDispId = GetNextOperation( nDispId) ;
}
if ( pDisp != nullptr) {
pDisp->Init( this) ;
// se impostazione fase per impostazione gruppo di lavoro forzo verifica tavola
bool bVerifyTable = ( m_nCurrPhase == 0) ;
if ( pDisp->IsSetTableName() && ! pDisp->Apply( bVerifyTable))
return false ;
}
else
return false ;
}
// aggiorno stato lavorazioni
int nMchId = GetFirstOperation() ;
while ( nMchId != GDB_ID_NULL) {
const Machining* pMch = GetMachining( m_pGeomDB->GetUserObj( nMchId)) ;
if ( pMch != nullptr) {
m_pGeomDB->SetStatus( nMchId, ( pMch->GetPhase() == nPhase ? GDB_ST_ON : GDB_ST_OFF)) ;
}
nMchId = GetNextOperation( nMchId) ;
}
// assegno l'indice della nuova fase corrente
m_nCurrPhase = nPhase ;
m_nCurrDispId = nDispId ;
return true ;
}
//----------------------------------------------------------------------------
int
MachMgr::GetCurrPhase( void) const
{
return m_nCurrPhase ;
}
//----------------------------------------------------------------------------
bool
MachMgr::RemoveLastPhase( void)
{
if ( m_pGeomDB == nullptr)
return false ;
// non è possibile rimuovere la prima e unica fase
if ( m_nPhasesCount <= 1)
return false ;
// cancello le eventuali lavorazioni della fase
int nOperId = GetLastOperation() ;
while ( nOperId != GDB_ID_NULL) {
InitOperation( nOperId) ;
if ( GetOperationType( nOperId) != OPER_DISP && GetOperationPhase( nOperId) == m_nPhasesCount) {
if ( m_nCurrMachiningId == nOperId)
m_nCurrMachiningId = GDB_ID_NULL ;
m_pGeomDB->Erase( nOperId) ;
}
else
break ;
int nPrevId = GetLastOperation() ;
if ( nPrevId == nOperId)
break ;
nOperId = nPrevId ;
}
// aggiorno i grezzi ed elimino quelli appartenenti a questa sola fase
int nRawId = GetFirstRawPart() ;
while ( nRawId != GDB_ID_NULL) {
// prossimo grezzo
int nNextRawId = GetNextRawPart( nRawId) ;
// recupero le fasi in cui è presente il grezzo
INTVECTOR vPhase ;
GetRawPartPhases( nRawId, vPhase) ;
// se appartiene alla fase
auto iIter = find( vPhase.begin(), vPhase.end(), m_nPhasesCount) ;
if ( iIter != vPhase.end()) {
// se appartiene solo a questa fase, lo elimino
if ( vPhase.size() == 1)
RemoveRawPart( nRawId) ;
// altrimenti
else {
// aggiorno l'elenco delle fasi di appartenenza
vPhase.erase( iIter) ;
m_pGeomDB->SetInfo( nRawId, MACH_RAW_PHASE, vPhase) ;
// se questa fase è la corrente, tolgo i pezzi dal grezzo e lo nascondo
if ( m_nCurrPhase == m_nPhasesCount) {
SwapRawPartParts( nRawId, false) ;
m_pGeomDB->SetStatus( nRawId, GDB_ST_OFF) ;
}
}
}
// passo al prossimo
nRawId = nNextRawId ;
}
// elimino la disposizione di fase
int nDispId = GetLastOperation() ;
if ( GetOperationType( nDispId) == OPER_DISP && GetOperationPhase( nDispId) == m_nPhasesCount) {
// elimino i bloccaggi della fase
Disposition* pDisp = ::GetDisposition( m_pGeomDB->GetUserObj( nDispId)) ;
if ( pDisp != nullptr) {
int nFxtId = pDisp->GetFirstFixture() ;
while ( nFxtId != GDB_ID_NULL) {
if ( ! pDisp->RemoveFixture( nFxtId))
break ;
nFxtId = pDisp->GetFirstFixture() ;
}
}
// eliminazione dell'operazione
m_pGeomDB->Erase( nDispId) ;
}
// se era corrente, rendo corrente la precedente
if ( m_nCurrPhase == m_nPhasesCount)
SetCurrPhase( m_nCurrPhase - 1) ;
// ricalcolo il numero delle fasi (per maggior robustezza)
m_nPhasesCount = CalcPhaseCount() ;
return true ;
}
//----------------------------------------------------------------------------
int
MachMgr::GetPhaseCount( void) const
{
return m_nPhasesCount ;
}
//----------------------------------------------------------------------------
int
MachMgr::CalcPhaseCount( void) const
{
// ciclo sulle disposizioni
int nPhases = 0 ;
int nDispId = GetFirstOperation() ;
while ( nDispId != GDB_ID_NULL) {
Disposition* pDisp = ::GetDisposition( m_pGeomDB->GetUserObj( nDispId)) ;
if ( pDisp != nullptr)
nPhases = max( nPhases, pDisp->GetPhase()) ;
nDispId = GetNextOperation( nDispId) ;
}
return nPhases ;
}