//---------------------------------------------------------------------------- // EgalTech 2017-2017 //---------------------------------------------------------------------------- // File : Pocketing.cpp Data : 04.02.17 Versione : 1.8b1 // Contenuto : Implementazione gestione svuotature. // // // // Modifiche : 04.02.17 DS Creazione modulo. // // //---------------------------------------------------------------------------- //--------------------------- Include ---------------------------------------- #include "stdafx.h" #include "MachMgr.h" #include "DllMain.h" #include "Pocketing.h" #include "OperationConst.h" #include "MachiningConst.h" #include "/EgtDev/Include/EGkCurveLine.h" #include "/EgtDev/Include/EGkCurveArc.h" #include "/EgtDev/Include/EGkArcSpecial.h" #include "/EgtDev/Include/EGkChainCurves.h" #include "/EgtDev/Include/EGkOffsetCurve.h" #include "/EgtDev/Include/EGkCurveAux.h" #include "/EgtDev/Include/EGkSfrCreate.h" #include "/EgtDev/Include/EGkSurfTriMesh.h" #include "/EgtDev/Include/EGkExtText.h" #include "/EgtDev/Include/EGkUserObjFactory.h" #include "/EgtDev/Include/EGkIntervals.h" #include "/EgtDev/Include/EGkStringUtils3d.h" #include "/EgtDev/Include/EGnStringKeyVal.h" #include "/EgtDev/Include/EgtPointerOwner.h" #include "/EgtDev/Include/EgtNumUtils.h" using namespace std ; //------------------------------ Errors -------------------------------------- // 2401 = "Error in Pocketing : UpdateToolData failed" // 2402 = "Error in Pocketing : Open Contour" // 2403 = "Error in Pocketing : Contour Not Flat" // 2404 = "Error in Pocketing : Tool Dir not perpendicular to Flat Area" // 2405 = "Error in Pocketing : Empty RawBox" // 2406 = "Error in Pocketing : Depth not computable" // 2407 = "Error in Pocketing : machining depth (xxx) bigger than MaxMaterial (yyy)" // 2408 = "Error in Pocketing : Entity GetElevation" // 2409 = "Error in Pocketing : missing aggregate from bottom" // 2410 = "Error in Pocketing : path too far from part sides" // 2411 = "Error in Pocketing : toolpath allocation failed" // 2412 = "Error in Pocketing : Offset not computable" // 2413 = "Error in Pocketing : Toolpath not computable" // 2414 = "Error in Pocketing : Approach not computable" // 2415 = "Error in Pocketing : LeadIn not computable" // 2416 = "Error in Pocketing : LeadOut not computable" // 2417 = "Error in Pocketing : Retract not computable" // 2418 = "Error in Pocketing : Link not computable" // 2419 = "Error in Pocketing : Linear Approx not computable" // 2420 = "Error in Pocketing : Return toolpath not computable" // 2421 = "Error in Pocketing : Chaining failed" // 2422 = "Error in Pocketing : Tool MaxMaterial too small (xxx)" // 2423 = "Error in Pocketing : axes values not calculable" // 2424 = "Error in Pocketing : outstroke xxx" // 2425 = "Error in Pocketing : link movements not calculable" // 2426 = "Error in Pocketing : link outstroke xxx" // 2427 = "Error in Pocketing : post apply not calculable" //---------------------------------------------------------------------------- USEROBJ_REGISTER( GetOperationClass( OPER_POCKETING), Pocketing) ; //---------------------------------------------------------------------------- const string& Pocketing::GetClassName( void) const { return USEROBJ_GETNAME( Pocketing) ; } //---------------------------------------------------------------------------- Pocketing* Pocketing::Clone( void) const { // alloco oggetto Pocketing* pPock = new(nothrow) Pocketing ; // eseguo copia dei dati if ( pPock != nullptr) { try { pPock->m_Params = m_Params ; pPock->m_TParams = m_TParams ; } catch( ...) { delete pPock ; return nullptr ; } } // ritorno l'oggetto return pPock ; } //---------------------------------------------------------------------------- bool Pocketing::Dump( string& sOut, bool bMM, const char* szNewLine) const { sOut += GetClassName() + "[mm]" + szNewLine ; sOut += KEY_PHASE + EQUAL + ToString( m_nPhase) + szNewLine ; sOut += KEY_IDS + EQUAL + ToString( m_vId) + szNewLine ; for ( int i = 0 ; i < m_Params.GetSize() ; ++ i) sOut += m_Params.ToString( i) + szNewLine ; for ( int i = 0 ; i < m_TParams.GetSize() ; ++ i) sOut += m_TParams.ToString( i) + szNewLine ; sOut += KEY_NUM + EQUAL + ToString( m_nPockets) + szNewLine ; return true ; } //---------------------------------------------------------------------------- bool Pocketing::Save( int nBaseId, STRVECTOR& vString) const { try { int nSize = 1 + m_Params.GetSize() + m_TParams.GetSize() + 2 ; vString.insert( vString.begin(), nSize, "") ; int k = - 1 ; if ( ! SetVal( KEY_IDS, m_vId, vString[++k])) return false ; for ( int i = 0 ; i < m_Params.GetSize() ; ++ i) vString[++k] = m_Params.ToString( i) ; for ( int i = 0 ; i < m_TParams.GetSize() ; ++ i) vString[++k] = m_TParams.ToString( i) ; if ( ! SetVal( KEY_PHASE, m_nPhase, vString[++k])) return false ; if ( ! SetVal( KEY_NUM, m_nPockets, vString[++k])) return false ; } catch( ...) { return false ; } return true ; } //---------------------------------------------------------------------------- bool Pocketing::Load( const STRVECTOR& vString, int nBaseGdbId) { int nSize = int( vString.size()) ; // lista identificativi geometrie da lavorare int k = - 1 ; if ( k >= nSize - 1 || ! GetVal( vString[++k], KEY_IDS, m_vId)) return false ; for ( auto& Sel : m_vId) Sel.nId += nBaseGdbId ; // parametri lavorazione for ( int i = 0 ; i < m_Params.GetSize() ; ++ i) { int nKey ; if ( k >= nSize - 1 || ! m_Params.FromString( vString[++k], nKey) || nKey != i) { if ( m_Params.IsOptional( i)) -- k ; else return false ; } } // parametri utensile for ( int i = 0 ; i < m_TParams.GetSize() ; ++ i) { int nKey ; if ( k >= nSize - 1 || ! m_TParams.FromString( vString[++k], nKey) || nKey != i) return false ; } // parametri di stato while ( k < nSize - 1) { // separo chiave da valore string sKey, sVal ; SplitFirst( vString[++k], "=", sKey, sVal) ; // leggo if ( sKey == KEY_PHASE){ if ( ! FromString( sVal, m_nPhase)) return false ; } else if ( sKey == KEY_NUM) { if ( ! FromString( sVal, m_nPockets)) return false ; } } return true ; } //---------------------------------------------------------------------------- //---------------------------------------------------------------------------- Pocketing::Pocketing( void) { m_Params.m_sName = "*" ; m_Params.m_sToolName = "*" ; m_TParams.m_sName = "*" ; m_TParams.m_sHead = "*" ; m_dMaxHelixRad = INFINITO ; m_nPockets = 0 ; } //---------------------------------------------------------------------------- bool Pocketing::Prepare( const string& sMillName) { // verifico il gestore lavorazioni if ( m_pMchMgr == nullptr) return false ; // recupero il gestore DB utensili della macchina corrente ToolsMgr* pTMgr = m_pMchMgr->GetCurrToolsMgr() ; if ( pTMgr == nullptr) return false ; // recupero il gestore DB lavorazioni della macchina corrente MachiningsMgr* pMMgr = m_pMchMgr->GetCurrMachiningsMgr() ; if ( pMMgr == nullptr) return false ; // ricerca della lavorazione di libreria con il nome indicato const PocketingData* pDdata = GetPocketingData( pMMgr->GetMachining( sMillName)) ; if ( pDdata == nullptr) return false ; m_Params = *pDdata ; // ricerca dell'utensile usato dalla lavorazione const ToolData* pTdata = pTMgr->GetTool( m_Params.m_ToolUuid) ; if ( pTdata == nullptr) return false ; m_TParams = *pTdata ; m_Params.m_sToolName = m_TParams.m_sName ; return true ; } //---------------------------------------------------------------------------- bool Pocketing::SetParam( int nType, bool bVal) { switch ( nType) { case MPA_INVERT : m_Params.m_bInvert = bVal ; return true ; case MPA_TOOLINVERT : m_Params.m_bToolInvert = bVal ; return true ; } return false ; } //---------------------------------------------------------------------------- bool Pocketing::SetParam( int nType, int nVal) { switch ( nType) { case MPA_LEADINTYPE : if ( ! m_Params.VerifyLeadInType( nVal)) return false ; m_Params.m_nLeadInType = nVal ; return true ; case MPA_LEADOUTTYPE : if ( ! m_Params.VerifyLeadOutType( nVal)) return false ; m_Params.m_nLeadOutType = nVal ; return true ; case MPA_SCC : if ( ! m_Params.VerifySolCh( nVal)) return false ; m_Params.m_nSolCh = nVal ; return true ; case MPA_SUBTYPE : if ( ! m_Params.VerifySubType( nVal)) return false ; m_Params.m_nSubType = nVal ; return true ; } return false ; } //---------------------------------------------------------------------------- bool Pocketing::SetParam( int nType, double dVal) { switch ( nType) { case MPA_SPEED : if ( ! m_TParams.VerifySpeed( dVal)) return false ; if ( abs( m_TParams.m_dSpeed - dVal) < EPS_MACH_ANG_PAR) m_Params.m_dSpeed = 0 ; else m_Params.m_dSpeed = dVal ; return true ; case MPA_FEED : if ( abs( m_TParams.m_dFeed - dVal) < EPS_MACH_LEN_PAR) m_Params.m_dFeed = 0 ; else m_Params.m_dFeed = dVal ; return true ; case MPA_STARTFEED : if ( abs( m_TParams.m_dStartFeed - dVal) < EPS_MACH_LEN_PAR) m_Params.m_dStartFeed = 0 ; else m_Params.m_dStartFeed = dVal ; return true ; case MPA_ENDFEED : if ( abs( m_TParams.m_dEndFeed - dVal) < EPS_MACH_LEN_PAR) m_Params.m_dEndFeed = 0 ; else m_Params.m_dEndFeed = dVal ; return true ; case MPA_TIPFEED : if ( abs( m_TParams.m_dTipFeed - dVal) < EPS_MACH_LEN_PAR) m_Params.m_dTipFeed = 0 ; else m_Params.m_dTipFeed = dVal ; return true ; case MPA_OFFSR : if ( abs( m_TParams.m_dOffsR - dVal) < EPS_MACH_LEN_PAR) m_Params.m_dOffsR = UNKNOWN_PAR ; else m_Params.m_dOffsR = dVal ; return true ; case MPA_OFFSL : if ( abs( m_TParams.m_dOffsL - dVal) < EPS_MACH_LEN_PAR) m_Params.m_dOffsL = UNKNOWN_PAR ; else m_Params.m_dOffsL = dVal ; return true ; case MPA_DEPTH : m_Params.m_sDepth = ToString( dVal) ; return true ; case MPA_STARTPOS : m_Params.m_dStartPos = dVal ; return true ; case MPA_STEP : m_Params.m_dStep = dVal ; return true ; case MPA_SIDESTEP : m_Params.m_dSideStep = dVal ; return true ; case MPA_SIDEANGLE : m_Params.m_dSideAngle = dVal ; return true ; case MPA_LITANG : m_Params.m_dLiTang = dVal ; return true ; case MPA_LIELEV : m_Params.m_dLiElev = dVal ; return true ; case MPA_LOTANG : m_Params.m_dLoTang = dVal ; return true ; } return false ; } //---------------------------------------------------------------------------- bool Pocketing::SetParam( int nType, const string& sVal) { switch ( nType) { case MPA_TOOL : { const ToolData* pTdata ; if ( ! m_Params.VerifyTool( m_pMchMgr->GetCurrToolsMgr(), sVal, pTdata)) return false ; m_Params.m_sToolName = sVal ; m_Params.m_ToolUuid = pTdata->m_Uuid ; m_TParams = *pTdata ; } break ; case MPA_DEPTH_STR : m_Params.m_sDepth = sVal ; return true ; case MPA_SYSNOTES : m_Params.m_sSysNotes = sVal ; break ; case MPA_USERNOTES : m_Params.m_sUserNotes = sVal ; break ; case MPA_INITANGS : m_Params.m_sInitAngs = sVal ; break ; case MPA_BLOCKEDAXIS : m_Params.m_sBlockedAxis = sVal ; break ; default : return false ; } return true ; } //---------------------------------------------------------------------------- bool Pocketing::SetGeometry( const SELVECTOR& vIds) { // verifico validità gestore DB geometrico if ( m_pGeomDB == nullptr) return false ; // reset della geometria corrente m_vId.clear() ; // verifico che gli identificativi rappresentino delle entità ammissibili (tutte curve o tutte facce) int nType = GEO_NONE ; for ( const auto& Id : vIds) { // test sull'entità int nSubs ; if ( ! VerifyGeometry( Id, nSubs, nType)) { string sOut = "Entity " + ToString( Id) + " skipped by Milling" ; LOG_INFO( GetEMkLogger(), sOut.c_str()) ; continue ; } // posso aggiungere alla lista m_vId.emplace_back( Id) ; } return ( ! m_vId.empty() || vIds.empty()) ; } //---------------------------------------------------------------------------- bool Pocketing::Preview( bool bRecalc) { // reset numero percorsi di svuotatura generati m_nPockets = 0 ; // verifico validità gestore DB geometrico e Id del gruppo if ( m_pGeomDB == nullptr || ! m_pGeomDB->ExistsObj( m_nOwnerId)) return false ; // recupero gruppo per geometria ausiliaria int nAuxId = m_pGeomDB->GetFirstNameInGroup( m_nOwnerId, MCH_AUX) ; bool bChain = false ; // se non c'è, lo aggiungo if ( nAuxId == GDB_ID_NULL) { nAuxId = m_pGeomDB->AddGroup( GDB_ID_NULL, m_nOwnerId, Frame3d()) ; if ( nAuxId == GDB_ID_NULL) return false ; m_pGeomDB->SetName( nAuxId, MCH_AUX) ; m_pGeomDB->SetStatus( nAuxId, GDB_ST_OFF) ; bChain = true ; } // altrimenti, se chiesto ricalcolo, lo svuoto else if ( bRecalc) { m_pGeomDB->EmptyGroup( nAuxId) ; bChain = true ; } // aggiorno dati geometrici dell'utensile if ( ! UpdateToolData()) { m_pMchMgr->SetLastError( 2401, "Error in Pocketing : UpdateToolData failed") ; return false ; } // se necessario, eseguo concatenamento ed inserisco i percorsi sotto la geometria ausiliaria if ( bChain && ! Chain( nAuxId)) { m_pMchMgr->SetLastError( 2421, "Error in Pocketing : Chaining failed") ; return false ; } // recupero gruppo per geometria di Preview int nPvId = m_pGeomDB->GetFirstNameInGroup( m_nOwnerId, MCH_PV) ; // se non c'è, lo aggiungo if ( nPvId == GDB_ID_NULL) { nPvId = m_pGeomDB->AddGroup( GDB_ID_NULL, m_nOwnerId, Frame3d()) ; if ( nPvId == GDB_ID_NULL) return false ; m_pGeomDB->SetName( nPvId, MCH_PV) ; } // altrimenti lo svuoto else m_pGeomDB->EmptyGroup( nPvId) ; // lavoro ogni singola catena int nPathId = m_pGeomDB->GetFirstGroupInGroup( nAuxId) ; while ( nPathId != GDB_ID_NULL) { if ( ! ProcessPath( nPathId, nPvId, GDB_ID_NULL)) return false ; nPathId = m_pGeomDB->GetNextGroup( nPathId) ; } return true ; } //---------------------------------------------------------------------------- bool Pocketing::Apply( bool bRecalc, bool bPostApply) { // reset numero percorsi di svuotatura generati m_nPockets = 0 ; // reset raggio massimo attacco ad elica nel caso di cerchi m_dMaxHelixRad = INFINITO ; // verifico validità gestore DB geometrico e Id del gruppo if ( m_pGeomDB == nullptr || ! m_pGeomDB->ExistsObj( m_nOwnerId)) return false ; // recupero gruppo per geometria ausiliaria int nAuxId = m_pGeomDB->GetFirstNameInGroup( m_nOwnerId, MCH_AUX) ; bool bChain = false ; // se non c'è, lo aggiungo if ( nAuxId == GDB_ID_NULL) { nAuxId = m_pGeomDB->AddGroup( GDB_ID_NULL, m_nOwnerId, Frame3d()) ; if ( nAuxId == GDB_ID_NULL) return false ; m_pGeomDB->SetName( nAuxId, MCH_AUX) ; m_pGeomDB->SetStatus( nAuxId, GDB_ST_OFF) ; bChain = true ; } // altrimenti, se chiesto ricalcolo, lo svuoto else if ( bRecalc) { m_pGeomDB->EmptyGroup( nAuxId) ; bChain = true ; } // aggiorno dati geometrici dell'utensile if ( ! UpdateToolData()) { m_pMchMgr->SetLastError( 2401, "Error in Pocketing : UpdateToolData failed") ; return false ; } // se necessario, eseguo concatenamento ed inserisco i percorsi sotto la geometria ausiliaria if ( bChain && ! Chain( nAuxId)) { m_pMchMgr->SetLastError( 2421, "Error in Pocketing : Chaining failed") ; return false ; } // recupero gruppo per geometria di lavorazione (Cutter Location) int nClId = m_pGeomDB->GetFirstNameInGroup( m_nOwnerId, MCH_CL) ; // se non c'è, lo aggiungo if ( nClId == GDB_ID_NULL) { nClId = m_pGeomDB->AddGroup( GDB_ID_NULL, m_nOwnerId, Frame3d()) ; if ( nClId == GDB_ID_NULL) return false ; m_pGeomDB->SetName( nClId, MCH_CL) ; } // altrimenti lo svuoto else m_pGeomDB->EmptyGroup( nClId) ; // lavoro ogni singola catena bool bOk = true ; int nPathId = m_pGeomDB->GetFirstGroupInGroup( nAuxId) ; while ( nPathId != GDB_ID_NULL) { if ( ! ProcessPath( nPathId, GDB_ID_NULL, nClId)) bOk = false ; nPathId = m_pGeomDB->GetNextGroup( nPathId) ; } if ( ! bOk) return false ; // assegno ingombri dei vari percorsi di lavorazione e della lavorazione nel suo complesso CalcAndSetBBox( nClId) ; // eseguo aggiornamento assi macchina e collegamento con operazione precedente if ( ! Update( bPostApply)) return false ; return true ; } //---------------------------------------------------------------------------- bool Pocketing::Update( bool bPostApply) { // verifico validità gestore DB geometrico e Id del gruppo if ( m_pGeomDB == nullptr || ! m_pGeomDB->ExistsObj( m_nOwnerId)) return false ; // se lavorazione vuota, esco if ( m_nPockets == 0) { LOG_INFO( GetEMkLogger(), "Warning in Pocketing : No machinable pocket") return true ; } // imposto eventuale asse bloccato da lavorazione if ( ! m_Params.m_sBlockedAxis.empty()) { string sAxis, sVal ; Split( m_Params.m_sBlockedAxis, "=", true, sAxis, sVal) ; double dVal = 0 ; FromString( sVal, dVal) ; m_pMchMgr->ClearRotAxisBlock() ; m_pMchMgr->SetRotAxisBlock( sAxis, dVal) ; } // calcolo gli assi macchina string sHint = ExtractHint( m_Params.m_sUserNotes) ; if ( ! m_Params.m_sInitAngs.empty()) sHint = m_Params.m_sInitAngs ; if ( ! CalculateAxesValues( sHint)) { string sInfo = m_pMchMgr->GetOutstrokeInfo() ; if ( sInfo.empty()) m_pMchMgr->SetLastError( 2423, "Error in Pocketing : axes values not calculable") ; else m_pMchMgr->SetLastError( 2424, "Error in Pocketing : outstroke ") ; return false ; } // gestione movimenti all'inizio di ogni singolo percorso di lavorazione e alla fine della lavorazione if ( ! AdjustStartEndMovements()) { string sInfo = m_pMchMgr->GetOutstrokeInfo() ; if ( sInfo.empty()) m_pMchMgr->SetLastError( 2425, "Error in Pocketing : link movements not calculable") ; else m_pMchMgr->SetLastError( 2426, "Error in Pocketing : link outstroke ") ; return false ; } // esecuzione eventuali personalizzazioni if ( bPostApply && ! PostApply()) { m_pMchMgr->SetLastError( 2427, "Error in Pocketing : post apply not calculable") ; return false ; } return true ; } //---------------------------------------------------------------------------- bool Pocketing::GetParam( int nType, bool& bVal) const { switch ( nType) { case MPA_INVERT : bVal = m_Params.m_bInvert ; return true ; case MPA_TOOLINVERT : bVal = m_Params.m_bToolInvert ; return true ; } bVal = false ; return false ; } //---------------------------------------------------------------------------- bool Pocketing::GetParam( int nType, int& nVal) const { switch ( nType) { case MPA_TYPE : nVal = MT_POCKETING ; return true ; case MPA_LEADINTYPE : nVal = m_Params.m_nLeadInType ; return true ; case MPA_LEADOUTTYPE : nVal = m_Params.m_nLeadOutType ; return true ; case MPA_SCC : nVal = m_Params.m_nSolCh ; return true ; case MPA_SUBTYPE : nVal = m_Params.m_nSubType ; return true ; } nVal = 0 ; return false ; } //---------------------------------------------------------------------------- bool Pocketing::GetParam( int nType, double& dVal) const { switch ( nType) { case MPA_SPEED : dVal = GetSpeed() ; return true ; case MPA_FEED : dVal = GetFeed() ; return true ; case MPA_STARTFEED : dVal = GetStartFeed() ; return true ; case MPA_ENDFEED : dVal = GetEndFeed() ; return true ; case MPA_TIPFEED : dVal = GetTipFeed() ; return true ; case MPA_OFFSR : dVal = GetOffsR() ; return true ; case MPA_OFFSL : dVal = GetOffsL() ; return true ; case MPA_STARTPOS : dVal = m_Params.m_dStartPos ; return true ; case MPA_STEP : dVal = m_Params.m_dStep ; return true ; case MPA_SIDESTEP : dVal = m_Params.m_dSideStep ; return true ; case MPA_SIDEANGLE : dVal = m_Params.m_dSideAngle ; return true ; case MPA_LITANG : dVal = m_Params.m_dLiTang ; return true ; case MPA_LIELEV : dVal = m_Params.m_dLiElev ; return true ; case MPA_LOTANG : dVal = m_Params.m_dLoTang ; return true ; } dVal = 0 ; return false ; } //---------------------------------------------------------------------------- bool Pocketing::GetParam( int nType, string& sVal) const { switch ( nType) { case MPA_NAME : sVal = m_Params.m_sName ; return true ; case MPA_TOOL : sVal = m_Params.m_sToolName ; return true ; case MPA_DEPTH_STR : sVal = m_Params.m_sDepth ; return true ; case MPA_TUUID : sVal = ToString( m_Params.m_ToolUuid) ; return true ; case MPA_UUID : sVal = ToString( m_Params.m_Uuid) ; return true ; case MPA_SYSNOTES : sVal = m_Params.m_sSysNotes ; return true ; case MPA_USERNOTES : sVal = m_Params.m_sUserNotes ; return true ; case MPA_INITANGS : sVal = m_Params.m_sInitAngs ; return true ; case MPA_BLOCKEDAXIS : sVal = m_Params.m_sBlockedAxis ; return true ; } sVal = "" ; return false ; } //---------------------------------------------------------------------------- const ToolData& Pocketing::GetToolData( void) const { return m_TParams ; } //---------------------------------------------------------------------------- bool Pocketing::UpdateToolData( bool* pbChanged) { // recupero il gestore DB utensili della macchina corrente ToolsMgr* pTMgr = m_pMchMgr->GetCurrToolsMgr() ; if ( pTMgr == nullptr) return false ; // recupero l'utensile nel DB utensili const ToolData* pTdata = pTMgr->GetTool( m_Params.m_ToolUuid) ; if ( pTdata == nullptr) return false ; // verifico se sono diversi (ad esclusione del nome) m_TParams.m_sName = pTdata->m_sName ; bool bChanged = ! SameTool( m_TParams, *pTdata) ; // aggiorno comunque i parametri m_TParams = *pTdata ; // eventuali segnalazioni if ( ! EqualNoCase( m_Params.m_sToolName, m_TParams.m_sName)) { string sLog = "Warning in Pocketing : tool name changed (" + m_Params.m_sToolName + "->" + m_TParams.m_sName + ")" ; LOG_INFO( GetEMkLogger(), sLog.c_str()) ; m_Params.m_sToolName = m_TParams.m_sName ; } if ( bChanged) { string sLog = "Warning in Pocketing : tool data changed (" + m_Params.m_sToolName + ")" ; LOG_INFO( GetEMkLogger(), sLog.c_str()) ; } // se definito parametro di ritorno, lo assegno if ( pbChanged != nullptr) *pbChanged = bChanged ; return true ; } //---------------------------------------------------------------------------- bool Pocketing::GetGeometry( SELVECTOR& vIds) const { // restituisco l'elenco delle entità vIds = m_vId ; return true ; } //---------------------------------------------------------------------------- bool Pocketing::VerifyGeometry( SelData Id, int& nSubs, int& nType) { // ammessi : o curve o testi o facce di trimesh const IGeoObj* pGObj = m_pGeomDB->GetGeoObj( Id.nId) ; if ( pGObj == nullptr) return false ; // se ammesse curve ed è tale if ( ( nType == GEO_NONE || nType == GEO_CURVE) && ( pGObj->GetType() & GEO_CURVE) != 0) { nType = GEO_CURVE ; const ICurve* pCurve = nullptr ; // se direttamente la curva if ( Id.nSub == SEL_SUB_ALL) { pCurve = ::GetCurve( pGObj) ; if ( pCurve != nullptr) { if ( pCurve->GetType() == CRV_COMPO) nSubs = ::GetCurveComposite( pCurve)->GetCurveCount() ; else nSubs = 0 ; } } // altrimenti sottocurva di composita else { const ICurveComposite* pCompo = GetCurveComposite( pGObj) ; if ( pCompo != nullptr) pCurve = pCompo->GetCurve( Id.nSub) ; nSubs = 0 ; } return ( pCurve != nullptr) ; } // se altrimenti ammessi testi ed è tale else if ( ( nType == GEO_NONE || nType == EXT_TEXT) && pGObj->GetType() == EXT_TEXT) { nType = EXT_TEXT ; const IExtText* pText = ::GetExtText( pGObj) ; if ( pText == nullptr) return false ; // tutto bene nSubs = 0 ; return true ; } // se altrimenti ammesse superfici trimesh ed è tale else if ( ( nType == GEO_NONE || nType == SRF_TRIMESH) && pGObj->GetType() == SRF_TRIMESH) { nType = SRF_TRIMESH ; const ISurfTriMesh* pSurf = ::GetSurfTriMesh( pGObj) ; if ( pSurf == nullptr) return false ; // se direttamente la superficie if ( Id.nSub == SEL_SUB_ALL) { nSubs = pSurf->GetFacetCount() ; return true ; } // altrimenti faccia di superficie trimesh else { // se faccia non esistente if ( Id.nSub > pSurf->GetFacetCount()) return false ; // tutto bene nSubs = 0 ; return true ; } } // altrimenti errore else return false ; } //---------------------------------------------------------------------------- bool Pocketing::GetCurves( SelData Id, ICURVEPLIST& lstPC) { // ammessi : curve, testi o facce di polymesh const IGeoObj* pGObj = m_pGeomDB->GetGeoObj( Id.nId) ; if ( pGObj == nullptr) return false ; // ne recupero il riferimento globale Frame3d frGlob ; if ( ! m_pGeomDB->GetGlobFrame( Id.nId, frGlob)) return false ; // se curva if ( ( pGObj->GetType() & GEO_CURVE) != 0) { PtrOwner pCurve ; // se direttamente curva if ( Id.nSub == SEL_SUB_ALL) { // recupero la curva const ICurve* pOriCurve = ::GetCurve( pGObj) ; if ( pOriCurve == nullptr) return false ; // la duplico pCurve.Set( pOriCurve->Clone()) ; // se estrusione mancante, imposto default Vector3d vtExtr ; if ( ! pCurve->GetExtrusion( vtExtr) || vtExtr.IsSmall()) pCurve->SetExtrusion( Z_AX) ; } // altrimenti sottocurva di composita else { // recupero la composita const ICurveComposite* pCompo = GetCurveComposite( pGObj) ; if ( pCompo == nullptr) return false ; // recupero la curva semplice const ICurve* pOriCurve = ::GetCurve( pCompo->GetCurve( Id.nSub)) ; if ( pOriCurve == nullptr) return false ; // la duplico pCurve.Set( pOriCurve->Clone()) ; // recupero estrusione e spessore Vector3d vtExtr ; if ( ! pCompo->GetExtrusion( vtExtr) || vtExtr.IsSmall()) vtExtr = Z_AX ; pCurve->SetExtrusion( vtExtr) ; double dThick ; if ( pCompo->GetThickness( dThick)) pCurve->SetThickness( dThick) ; } if ( IsNull( pCurve)) return false ; // la porto in globale pCurve->ToGlob( frGlob) ; // la restituisco lstPC.emplace_back( Release( pCurve)) ; return true ; } // se altrimenti testo else if ( pGObj->GetType() == EXT_TEXT) { // recupero il testo const IExtText* pText = ::GetExtText( pGObj) ; if ( pText == nullptr) return false ; // recupero l'outline del testo if ( ! pText->GetOutline( lstPC)) return false ; // porto le curve in globale for ( auto pCrv : lstPC) pCrv->ToGlob( frGlob) ; // ritorno return true ; } // se altrimenti superficie else if ( ( pGObj->GetType() & GEO_SURF) != 0) { // recupero la trimesh const ISurfTriMesh* pSurf = ::GetSurfTriMesh( pGObj) ; if ( pSurf == nullptr) return false ; // recupero l'indice della faccia int nFacet = ( ( Id.nSub == SEL_SUB_ALL) ? 0 : Id.nSub) ; // recupero i contorni della faccia POLYLINEVECTOR vPL ; pSurf->GetFacetLoops( nFacet, vPL) ; if ( vPL.empty()) return false ; // creo la curva a partire da quello esterno PtrOwner pCrvCompo( CreateCurveComposite()) ; pCrvCompo->FromPolyLine( vPL[0]) ; if ( ! pCrvCompo->IsValid()) return false ; // recupero la normale esterna della faccia Vector3d vtN ; if ( ! pSurf->GetFacetNormal( nFacet, vtN)) return false ; // assegno l'estrusione dalla normale alla faccia pCrvCompo->SetExtrusion( vtN) ; // unisco le eventuali parti allineate pCrvCompo->MergeCurves( 10 * EPS_SMALL, 10 * EPS_ANG_SMALL) ; // la porto in globale pCrvCompo->ToGlob( frGlob) ; // sistemazioni varie AdjustCurveFromSurf( pCrvCompo, TOOL_ORTHO, FACE_CONT, 0) ; // la restituisco lstPC.emplace_back( Release( pCrvCompo)) ; return true ; } // altrimenti errore else return false ; } //---------------------------------------------------------------------------- bool Pocketing::Chain( int nGrpDestId) { // vettore puntatori alle curve ICURVEPOVECTOR vpCrvs ; vpCrvs.reserve( m_vId.size()) ; // vettore selettori delle curve originali SELVECTOR vInds ; // recupero tutte le curve e le porto in globale for ( const auto& Id : m_vId) { // prendo le curve ICURVEPLIST lstPC ; if ( ! GetCurves( Id, lstPC)) { string sOut = "Entity " + ToString( Id) + " skipped by Pocketing" ; LOG_INFO( GetEMkLogger(), sOut.c_str()) ; } for ( auto pCrv : lstPC) { vpCrvs.emplace_back( pCrv) ; vInds.emplace_back( Id) ; } } // preparo i dati per il concatenamento bool bFirst = true ; Point3d ptNear = ORIG ; double dToler = 10 * EPS_SMALL ; ChainCurves chainC ; chainC.Init( true, dToler, int( vpCrvs.size())) ; for ( size_t i = 0 ; i < vpCrvs.size() ; ++ i) { // recupero la curva e il suo riferimento ICurve* pCrv = vpCrvs[i] ; if ( pCrv == nullptr) continue ; // recupero i dati della curva necessari al concatenamento e li assegno Point3d ptStart, ptEnd ; Vector3d vtStart, vtEnd ; if ( ! pCrv->GetStartPoint( ptStart) || ! pCrv->GetStartDir( vtStart) || ! pCrv->GetEndPoint( ptEnd) || ! pCrv->GetEndDir( vtEnd)) return false ; if ( ! chainC.AddCurve( int( i + 1), ptStart, vtStart, ptEnd, vtEnd)) return false ; // se prima curva, assegno inizio della ricerca if ( bFirst) { ptNear = ptStart + 10 * EPS_SMALL * vtStart ; bFirst = false ; } } // recupero i percorsi concatenati int nCount = 0 ; INTVECTOR vnId2 ; while ( chainC.GetChainFromNear( ptNear, false, vnId2)) { // creo una curva composita PtrOwner pCrvCompo( CreateCurveComposite()) ; if ( IsNull( pCrvCompo)) return false ; // estrusione e spessore Vector3d vtExtr = Z_AX ; double dThick = 0 ; // vettore Id originali SELVECTOR vId2 ; vId2.reserve( vnId2.size()) ; // recupero le curve semplici e le inserisco nella curva composita for ( size_t i = 0 ; i < vnId2.size() ; ++ i) { int nId = abs( vnId2[i]) - 1 ; bool bInvert = ( vnId2[i] < 0) ; vId2.emplace_back( vInds[nId]) ; // recupero la curva ICurve* pCrv = vpCrvs[nId] ; // se necessario, la inverto if ( bInvert) pCrv->Invert() ; // recupero eventuali estrusione e spessore Vector3d vtTemp ; if ( pCrv->GetExtrusion( vtTemp)) { vtExtr = vtTemp ; double dTemp ; if ( pCrv->GetThickness( dTemp) && fabs( dTemp) > fabs( dThick)) dThick = dTemp ; } // la aggiungo alla curva composta if ( ! pCrvCompo->AddCurve( ::Release( vpCrvs[nId]), true, dToler)) return false ; } // se non sono state inserite curve, vado oltre if ( pCrvCompo->GetCurveCount() == 0) continue ; // imposto estrusione e spessore pCrvCompo->SetExtrusion( vtExtr) ; pCrvCompo->SetThickness( dThick) ; // aggiorno il nuovo punto vicino pCrvCompo->GetEndPoint( ptNear) ; // se utile, approssimo con archi if ( ! ApproxWithArcsIfUseful( pCrvCompo)) return false ; // creo nuovo gruppo int nPathId = m_pGeomDB->AddGroup( GDB_ID_NULL, nGrpDestId, Frame3d()) ; if ( nPathId == GDB_ID_NULL) return false ; m_pGeomDB->SetName( nPathId, MCH_PATH + ToString( ++ nCount)) ; m_pGeomDB->SetInfo( nPathId, KEY_IDS, ToString( vId2)) ; // inserisco la curva composita nel gruppo destinazione int nNewId = m_pGeomDB->AddGeoObj( GDB_ID_NULL, nPathId, ::Release( pCrvCompo)) ; if ( nNewId == GDB_ID_NULL) return false ; } return true ; } //---------------------------------------------------------------------------- bool Pocketing::ProcessPath( int nPathId, int nPvId, int nClId) { // recupero gruppo per geometria temporanea const string GRP_TEMP = "Temp" ; int nTempId = m_pGeomDB->GetFirstNameInGroup( m_nOwnerId, GRP_TEMP) ; // se non c'è, lo aggiungo if ( nTempId == GDB_ID_NULL) { nTempId = m_pGeomDB->AddGroup( GDB_ID_NULL, m_nOwnerId, Frame3d()) ; if ( nTempId == GDB_ID_NULL) return false ; m_pGeomDB->SetName( nTempId, GRP_TEMP) ; } // altrimenti lo svuoto else m_pGeomDB->EmptyGroup( nTempId) ; // in ogni caso lo dichiaro temporaneo e non visibile m_pGeomDB->SetLevel( nTempId, GDB_LV_TEMP) ; m_pGeomDB->SetStatus( nTempId, GDB_ST_OFF) ; // verifico sia una curva chiusa (deve delimitare l'area da svuotare) int nCrvId = m_pGeomDB->GetFirstInGroup( nPathId) ; if ( m_pGeomDB->GetGeoType( nCrvId) != CRV_COMPO) return false ; ICurve* pCrv = ::GetCurve( m_pGeomDB->GetGeoObj( nCrvId)) ; if ( pCrv == nullptr || ! pCrv->IsClosed()) { m_pMchMgr->SetLastError( 2402, "Error in Pocketing : Open Contour") ; return false ; } // copio la curva composita da elaborare int nCopyId = m_pGeomDB->CopyGlob( nCrvId, GDB_ID_NULL, nTempId) ; if ( nCopyId == GDB_ID_NULL) return false ; ICurveComposite* pCompo = GetCurveComposite( m_pGeomDB->GetGeoObj( nCopyId)) ; // recupero estrusione e spessore Vector3d vtExtr = Z_AX ; pCompo->GetExtrusion( vtExtr) ; double dThick ; pCompo->GetThickness( dThick) ; // eventuale inversione direzione utensile if ( m_Params.m_bToolInvert) { vtExtr.Invert() ; pCompo->SetExtrusion( vtExtr) ; dThick = - dThick ; pCompo->SetThickness( dThick) ; } // verifico sia piana e sistemo senso antiorario visto dalla direzione di estrusione Plane3d plPlane ; double dArea ; if ( ! pCompo->GetArea( plPlane, dArea)) { m_pMchMgr->SetLastError( 2403, "Error in Pocketing : Contour Not Flat") ; return false ; } if ( abs( plPlane.GetVersN() * vtExtr) < cos( 10 * EPS_ANG_SMALL)) { m_pMchMgr->SetLastError( 2404, "Error in Pocketing : Tool Dir not perpendicular to Flat Area") ; return false ; } if ( plPlane.GetVersN() * vtExtr * dArea < 0) pCompo->Invert() ; // unisco le parti allineate (tranne inizio-fine se chiusa) if ( ! pCompo->MergeCurves( 10 * EPS_SMALL, 10 * EPS_ANG_SMALL, false)) return false ; // recupero il punto di inizio (per poi salvarlo nelle info di CL path) Point3d ptStart ; pCompo->GetStartPoint( ptStart) ; // recupero il box del grezzo in globale BBox3d b3Raw ; if ( ! GetRawGlobBox( m_nPhase, nPathId, 0.5 * m_TParams.m_dDiam, b3Raw) || b3Raw.IsEmpty()) { m_pMchMgr->SetLastError( 2405, "Error in Pocketing : Empty RawBox") ; return false ; } // recupero distanza da fondo dei grezzi interessati dal percorso double dRbDist = 0 ; if ( AreSameVectorApprox( vtExtr, Z_AX)) { if ( ! GetDistanceFromRawBottom( m_nPhase, nCopyId, m_TParams.m_dDiam, dRbDist)) return false ; } // valuto l'espressione dell'affondamento ExeLuaSetGlobNumVar( "TH", abs( dThick)) ; ExeLuaSetGlobNumVar( "RB", dRbDist) ; double dDepth ; string sMyDepth = m_Params.m_sDepth ; if ( ! ExeLuaEvalNumExpr( ToUpper( sMyDepth), &dDepth)) { m_pMchMgr->SetLastError( 2406, "Error in Pocketing : Depth not computable") ; return false ; } // se spessore positivo, lo sottraggo dal risultato if ( dThick > 0) dDepth -= dThick ; // sottraggo eventuale offset longitudinale dDepth -= GetOffsL() ; // recupero nome del path string sPathName ; m_pGeomDB->GetName( nPathId, sPathName) ; // assegno il versore fresa Vector3d vtTool = vtExtr ; // calcolo l'elevazione massima double dElev ; if ( CalcPathElevation( pCompo, vtTool, dDepth, 0.5 * m_TParams.m_dDiam, dElev)) { if ( dElev < EPS_SMALL && AreSameVectorApprox( vtExtr, Z_AX)) { BBox3d b3Crv ; pCompo->GetLocalBBox( b3Crv) ; dElev = max( 0., b3Raw.GetMax().z - b3Crv.GetMin().z + min( 0., dThick) + dDepth) ; } } else return false ; // eventuale imposizione massima elevazione da note utente double dMaxElev ; if ( FromString( ExtractInfo( m_Params.m_sUserNotes, "MaxElev="), dMaxElev) && dElev > dMaxElev) dElev = dMaxElev ; // verifico che il massimo materiale dell'utensile sia sensato const double MIN_MAXMAT = 1.0 ; if ( m_TParams.m_dMaxMat < dElev && m_TParams.m_dMaxMat < MIN_MAXMAT) { string sInfo = "Error in Pocketing : Tool MaxMaterial too small (" + ToString( m_TParams.m_dMaxMat, 2) + ")" ; m_pMchMgr->SetLastError( 2422, sInfo) ; return false ; } // verifico che lo step dell'utensile sia sensato double dOkStep = m_Params.m_dStep ; const double MIN_ZSTEP = 1.0 ; if ( dOkStep >= EPS_SMALL && dOkStep < MIN_ZSTEP) { dOkStep = MIN_ZSTEP ; string sInfo = "Warning in Pocketing : machining step too small (" + ToString( m_Params.m_dStep, 2) + ")" ; LOG_INFO( GetEMkLogger(), sInfo.c_str()) ; } // verifico di non superare il massimo materiale // se lo step supera la capacità dell'utensile if ( m_Params.m_dStep > m_TParams.m_dMaxMat + EPS_SMALL) { dOkStep = m_TParams.m_dMaxMat ; string sInfo = "Warning in Pocketing : machining step (" + ToString( m_Params.m_dStep, 1) + ") bigger than MaxMaterial (" + ToString( m_TParams.m_dMaxMat, 1) + ")" ; LOG_INFO( GetEMkLogger(), sInfo.c_str()) ; } // se lavorazione singola e l'elevazione supera la capacità dell'utensile if ( ( dOkStep < EPS_SMALL || dOkStep > dElev) && dElev > m_TParams.m_dMaxMat + EPS_SMALL) { if ( dDepth + max( dThick, 0.0) > m_TParams.m_dMaxMat) { dDepth = m_TParams.m_dMaxMat - max( dThick, 0.0) ; string sInfo = "Warning in Pocketing : machining depth (" + ToString( dElev, 1) + ") bigger than MaxMaterial (" + ToString( m_TParams.m_dMaxMat, 1) + ")" ; LOG_INFO( GetEMkLogger(), sInfo.c_str()) ; } else { string sInfo = "Error in Pocketing : machining depth (" + ToString( dElev, 1) + ") bigger than MaxMaterial (" + ToString( m_TParams.m_dMaxMat, 1) + ")" ; m_pMchMgr->SetLastError( 2407, sInfo) ; return false ; } } // verifiche per svuotature dal basso m_bAggrBottom = false ; if ( ! VerifyPathFromBottom( pCompo, vtTool)) { return false ; } // se richiesta anteprima if ( nPvId != GDB_ID_NULL) { // creo gruppo per geometria di lavorazione del percorso int nPxId = m_pGeomDB->AddGroup( GDB_ID_NULL, nPvId, Frame3d()) ; if ( nPxId == GDB_ID_NULL) return false ; m_pGeomDB->SetName( nPxId, sPathName) ; m_pGeomDB->SetMaterial( nPxId, GREEN) ; // creo l'anteprima del percorso if ( ! GenerateMillingPv( nPxId, pCompo)) return false ; } // se richiesta lavorazione if ( nClId != GDB_ID_NULL) { // creo gruppo per geometria di lavorazione del percorso int nPxId = m_pGeomDB->AddGroup( GDB_ID_NULL, nClId, Frame3d()) ; if ( nPxId == GDB_ID_NULL) return false ; m_pGeomDB->SetName( nPxId, sPathName) ; m_pGeomDB->SetMaterial( nPxId, BLUE) ; // sposto l'inizio a metà del tratto più lungo AdjustContourStart( pCompo) ; // verifico se archi vanno approssimati con segmenti di retta int nSplitArcs = m_pMchMgr->GetCurrMachiningsMgr()->GetSplitArcs() ; bool bSplitArcs = ( nSplitArcs == SPLAR_ALWAYS || ( nSplitArcs == SPLAR_NO_XY_PLANE && ! vtExtr.IsZplus()) || ( nSplitArcs == SPLAR_GEN_PLANE && vtExtr.IsGeneric())) ; // assegno il vettore estrazione al gruppo del percorso m_pGeomDB->SetInfo( nPxId, KEY_EXTR, vtTool) ; // assegno i punti di inizio e fine al gruppo del percorso m_pGeomDB->SetInfo( nPxId, KEY_START, ptStart) ; m_pGeomDB->SetInfo( nPxId, KEY_END, ptStart) ; // assegno l'elevazione massima m_pGeomDB->SetInfo( nPxId, KEY_ELEV, dElev) ; // Imposto dati comuni SetPathId( nPxId) ; SetToolDir( vtTool) ; // Eseguo la lavorazione a seconda del tipo switch ( m_Params.m_nSubType) { case POCKET_SUB_ZIGZAG : if ( ! AddZigZag( pCompo, vtTool, vtExtr, dDepth, dElev, dOkStep, bSplitArcs)) return false ; break ; case POCKET_SUB_ONEWAY : if ( ! AddOneWay( pCompo, vtTool, vtExtr, dDepth, dElev, dOkStep, bSplitArcs)) return false ; break ; case POCKET_SUB_SPIRALIN : if ( ! AddSpiralIn( pCompo, vtTool, vtExtr, dDepth, dElev, dOkStep, bSplitArcs)) return false ; break ; case POCKET_SUB_SPIRALOUT : if ( ! AddSpiralOut( pCompo, vtTool, vtExtr, dDepth, dElev, dOkStep, bSplitArcs)) return false ; break ; } } // incremento numero di svuotature ++ m_nPockets ; return true ; } //---------------------------------------------------------------------------- bool Pocketing::CalcPathElevation( const ICurveComposite* pCompo, const Vector3d& vtTool, double dDepth, double dRad, double& dElev) { dElev = 0 ; int nMaxInd = pCompo->GetCurveCount() - 1 ; for ( int i = 0 ; i <= nMaxInd ; ++ i) { // curva corrente const ICurve* pCrvC = pCompo->GetCurve( i) ; // calcolo elevazione double dCurrElev ; Point3d ptStart, ptMid, ptEnd ; pCrvC->GetStartPoint( ptStart) ; pCrvC->GetMidPoint( ptMid) ; pCrvC->GetEndPoint( ptEnd) ; Vector3d vtStartPerp, vtMidPerp, vtEndPerp, vtTg ; pCrvC->GetStartDir( vtTg) ; vtStartPerp = vtTg ^ vtTool ; vtStartPerp.Normalize() ; vtStartPerp *= dRad ; pCrvC->GetMidDir( vtTg) ; vtMidPerp = vtTg ^ vtTool ; vtMidPerp.Normalize() ; vtMidPerp *= dRad ; pCrvC->GetEndDir( vtTg) ; vtEndPerp = vtTg ^ vtTool ; vtEndPerp.Normalize() ; vtEndPerp *= dRad ; Vector3d vtDepth = vtTool * dDepth ; // linea centro utensile if ( GetElevation( m_nPhase, ptStart - vtDepth, ptMid - vtDepth, ptEnd - vtDepth, vtTool, dCurrElev)) { if ( dCurrElev > dElev) dElev = dCurrElev ; } else { m_pMchMgr->SetLastError( 2408, "Error in Pocketing : Entity GetElevation") ; return false ; } // da una parte if ( GetElevation( m_nPhase, ptStart + vtStartPerp - vtDepth, ptMid + vtMidPerp - vtDepth, ptEnd + vtEndPerp - vtDepth, vtTool, dCurrElev)) { if ( dCurrElev > dElev) dElev = dCurrElev ; } else { m_pMchMgr->SetLastError( 2408, "Error in Pocketing : Entity GetElevation") ; return false ; } // dall'altra parte if ( GetElevation( m_nPhase, ptStart - vtStartPerp - vtDepth, ptMid - vtMidPerp - vtDepth, ptEnd - vtEndPerp - vtDepth, vtTool, dCurrElev)) { if ( dCurrElev > dElev) dElev = dCurrElev ; } else { m_pMchMgr->SetLastError( 2408, "Error in Pocketing : Entity GetElevation") ; return false ; } } return true ; } //---------------------------------------------------------------------------- bool Pocketing::VerifyPathFromBottom( const ICurveComposite* pCompo, const Vector3d& vtTool) { // se non è fresatura dal basso in alto, esco if ( vtTool.z > - 0.5) return true ; // se c'è tavola basculante, esco bool bTabTilting = false ; if ( m_pMchMgr->GetCurrMachine()->GetCurrTableIsTilting( bTabTilting) && bTabTilting) return true ; // recupero dati di eventuale rinvio da sotto if ( ! GetAggrBottomData( m_TParams.m_sHead, m_AggrBottom) || m_AggrBottom.nType == 0) { m_pMchMgr->SetLastError( 2409, "Error in Pocketing : missing aggregate from bottom") ; return false ; } // calcolo la massima distanza minima del percorso dal contorno del grezzo double dDist = 0 ; Vector3d vtDir ; int nMaxInd = pCompo->GetCurveCount() - 1 ; for ( int i = 0 ; i <= nMaxInd ; ++ i) { // curva corrente const ICurve* pCrvC = pCompo->GetCurve( i) ; // considero inizio della prima curva, punto medio e fine di tutte Point3d ptP ; double dCurrDist = 0 ; Vector3d vtCurrDir ; if ( i == 0) { pCrvC->GetStartPoint( ptP) ; GetMinDistanceFromRawSide( m_nPhase, ptP, dCurrDist, vtCurrDir) ; if ( dCurrDist > dDist) { dDist = dCurrDist ; vtDir = vtCurrDir ; } } pCrvC->GetMidPoint( ptP) ; GetMinDistanceFromRawSide( m_nPhase, ptP, dCurrDist, vtCurrDir) ; if ( dCurrDist > dDist) { dDist = dCurrDist ; vtDir = vtCurrDir ; } pCrvC->GetEndPoint( ptP) ; GetMinDistanceFromRawSide( m_nPhase, ptP, dCurrDist, vtCurrDir) ; if ( dCurrDist > dDist) { dDist = dCurrDist ; vtDir = vtCurrDir ; } } // se supera il limite, errore if ( dDist > m_AggrBottom.dDMax) { m_pMchMgr->SetLastError( 2410, "Error in Pocketing : path too far from part sides") ; return false ; } // assegno direzione di accesso e segnalo utilizzo aggregato da sotto m_vtAggrBottom = vtDir ; m_bAggrBottom = true ; return true ; } //---------------------------------------------------------------------------- bool Pocketing::GenerateMillingPv( int nPathId, const ICurveComposite* pCompo) { // creo copia della curva composita PtrOwner< ICurve> pCrv( pCompo->Clone()) ; if ( IsNull( pCrv)) return false ; // calcolo la regione PtrOwner pSfr ; pSfr.Set( GetSurfFlatRegionFromFatCurve( Release( pCrv), 0.5 * m_TParams.m_dDiam, false, false)) ; if ( IsNull( pSfr)) return false ; // ne recupero il contorno PtrOwner< ICurve> pCrv2 ; pCrv2.Set( pSfr->GetLoop( 0, 0)) ; if ( IsNull( pCrv2)) return false ; // inserisco la curva nel DB int nC2Id = m_pGeomDB->AddGeoObj( GDB_ID_NULL, nPathId, Release( pCrv2)) ; if ( nC2Id == GDB_ID_NULL) return false ; // assegno nome e colore m_pGeomDB->SetName( nC2Id, MCH_PV_CUT) ; m_pGeomDB->SetMaterial( nC2Id, LIME) ; // eventuali altri contorni ( interni di contornatura chiusa) const int MAX_INT_LOOP = 1000 ; for ( int i = 1 ; i <= MAX_INT_LOOP ; ++i) { PtrOwner< ICurve> pCrv3 ; pCrv3.Set( pSfr->GetLoop( 0, i)) ; if ( IsNull( pCrv3)) break ; // inserisco la curva nel DB int nC3Id = m_pGeomDB->AddGeoObj( GDB_ID_NULL, nPathId, Release( pCrv3)) ; if ( nC3Id == GDB_ID_NULL) return false ; // assegno nome e colore m_pGeomDB->SetName( nC3Id, MCH_PV_CUT) ; m_pGeomDB->SetMaterial( nC3Id, LIME) ; } // inserisco la regione nel DB int nRId = m_pGeomDB->AddGeoObj( GDB_ID_NULL, nPathId, Release( pSfr)) ; if ( nRId == GDB_ID_NULL) return false ; m_pGeomDB->SetName( nRId, MCH_PV_RCUT) ; m_pGeomDB->SetMaterial( nRId, INVISIBLE) ; // la copio anche come regione ridotta int nRrId = m_pGeomDB->Copy( nRId, GDB_ID_NULL, nPathId) ; if ( nRrId == GDB_ID_NULL) return false ; m_pGeomDB->SetName( nRrId, MCH_PV_RRCUT) ; m_pGeomDB->SetMaterial( nRrId, INVISIBLE) ; return true ; } //---------------------------------------------------------------------------- double GetCurveRadius( const ICurve* pCrv) { if ( pCrv == nullptr) return 0.0 ; BBox3d b3Loc ; if ( ! pCrv->GetLocalBBox( b3Loc)) return 0.0 ; double dRad ; if ( ! b3Loc.GetRadius( dRad)) return 0.0 ; return dRad ; } //---------------------------------------------------------------------------- bool Pocketing::AddZigZag( const ICurveComposite* pCompo, const Vector3d& vtTool, const Vector3d& vtExtr, double dDepth, double dElev, double dOkStep, bool bSplitArcs) { // recupero distanze di sicurezza double dSafeZ = m_pMchMgr->GetCurrMachiningsMgr()->GetSafeZ() ; double dSafeAggrBottZ = m_pMchMgr->GetCurrMachiningsMgr()->GetSafeAggrBottZ() ; // lunghezza di approccio/retrazione double dAppr = m_Params.m_dStartPos ; // determino numero e affondamento degli step int nStep = 1 ; nStep = max( 1, static_cast( ceil( dElev / dOkStep))) ; double dStep = dElev / nStep ; // calcolo curva offsettata del raggio utensile + sovramateriale double dTRad = 0.5 * m_TParams.m_dDiam ; double dOffs = dTRad + GetOffsR() ; double dExtra = min( 0.1 * m_TParams.m_dDiam, 2.0) ; OffsetCurve OffsCrv ; if ( ! OffsCrv.Make( pCompo, - ( dOffs + dExtra), ICurve::OFF_FILLET)) { m_pMchMgr->SetLastError( 2412, "Error in Pocketing : Offset not computable") ; return false ; } // recupero la prima curva di offset PtrOwner pOffs( CreateCurveComposite()) ; if ( IsNull( pOffs) || ! pOffs->AddCurve( OffsCrv.GetLongerCurve())) { m_pMchMgr->SetLastError( 2413, "Error in Pocketing : Toolpath not computable") ; return false ; } // determino il riferimento di base e il box della svuotatura Frame3d frPocket ; Point3d ptCen ; pCompo->GetCentroid( ptCen) ; frPocket.Set( ptCen, vtExtr) ; frPocket.Rotate( ptCen, vtExtr, m_Params.m_dSideAngle) ; pOffs->ToLoc( frPocket) ; // calcolo i percorsi di svuotatura ICRVCOMPOPOVECTOR vpCrvs ; if ( ! CalcZigZag( pOffs, vpCrvs)) return false ; // inserisco i movimenti di svuotatura bool bStart = true ; Point3d ptP1 ; for ( int j = 1 ; j <= nStep ; ++ j) { // ciclo sui percorsi for ( int k = 0 ; k < int( vpCrvs.size()) ; ++ k) { // ciclo sulle curve elementari int nMaxInd = vpCrvs[k]->GetCurveCount() - 1 ; for ( int i = 0 ; i <= nMaxInd ; ++ i) { // curva corrente const ICurve* pCrvC = vpCrvs[k]->GetCurve( i) ; // copio la curva PtrOwner pCurve( pCrvC->Clone()) ; if ( IsNull( pCurve)) return false ; pCurve->ToGlob( frPocket) ; // aggiungo affondamento pCurve->Translate( - vtTool * ( dDepth - dElev + j * dStep)) ; // se prima entità if ( i == 0 ) { // dati inizio entità Point3d ptStart ; pCurve->GetStartPoint( ptStart) ; Vector3d vtStart ; pCurve->GetStartDir( vtStart) ; // determino inizio attacco if ( ! CalcLeadInStart( ptStart, vtStart, vtExtr, nullptr, ptP1)) return false ; // determino elevazione su inizio attacco double dStElev ; if ( ! GetElevation( m_nPhase, ptStart - 10 * EPS_SMALL * vtTool, vtTool, GetRadiusForStartEndElevation(), vtTool, dStElev)) dStElev = dElev ; dStElev -= ( ptP1 - ptStart) * vtExtr ; // se inizio, approccio globale al punto iniziale if ( bStart) { if ( ! AddApproach( ptP1, vtTool, dSafeZ, dSafeAggrBottZ, dStElev, dAppr)) { m_pMchMgr->SetLastError( 2414, "Error in Pocketing : Approach not computable") ; return false ; } bStart = false ; } // altrimenti, approccio di collegamento else { if ( ! AddLinkApproach( ptP1, vtTool, dSafeZ, dSafeAggrBottZ, dStElev, dAppr)) { m_pMchMgr->SetLastError( 2418, "Error in Pocketing : Link not computable") ; return false ; } } // aggiungo attacco SetFeed( GetStartFeed()) ; if ( ! AddLeadIn( ptP1, ptStart, vtStart, vtExtr, nullptr, ! m_Params.m_bInvert, bSplitArcs, true)) { m_pMchMgr->SetLastError( 2415, "Error in Pocketing : LeadIn not computable") ; return false ; } } // elaborazioni sulla curva corrente if ( pCurve->GetType() == CRV_LINE) { ICurveLine* pLine = GetCurveLine( pCurve) ; Point3d ptP3 = pLine->GetEnd() ; SetFeed( GetFeed()) ; if ( AddLinearMove( ptP3) == GDB_ID_NULL) return false ; } else if ( pCurve->GetType() == CRV_ARC) { ICurveArc* pArc = GetCurveArc( pCurve) ; Point3d ptCen = pArc->GetCenter() ; double dAngCen = pArc->GetAngCenter() ; Vector3d vtN = pArc->GetNormVersor() ; Point3d ptP3 ; pArc->GetEndPoint( ptP3) ; SetFeed( GetFeed()) ; if ( AddArcMove( ptP3, ptCen, dAngCen, vtN) == GDB_ID_NULL) return false ; } // se ultima entità if ( i == nMaxInd) { // dati fine entità Point3d ptEnd ; pCurve->GetEndPoint( ptEnd) ; Vector3d vtEnd ; pCurve->GetEndDir( vtEnd) ; // aggiungo uscita Point3d ptP1 ; double dEndElev = dElev ; SetFeed( GetEndFeed()) ; if ( ! AddLeadOut( ptEnd, vtEnd, vtExtr, nullptr, bSplitArcs, ptP1, dEndElev, true)) { m_pMchMgr->SetLastError( 2416, "Error in Pocketing : LeadOut not computable") ; return false ; } // aggiungo retrazione di collegamento if ( ! AddLinkRetract( ptP1, vtTool, dSafeZ, dSafeAggrBottZ, dEndElev, dAppr)) { m_pMchMgr->SetLastError( 2418, "Error in Pocketing : Link not computable") ; return false ; } } } } } // calcolo curva offsettata del raggio utensile + sovramateriale OffsetCurve OffsCrv2 ; if ( ! OffsCrv2.Make( pCompo, - dOffs, ICurve::OFF_FILLET)) { m_pMchMgr->SetLastError( 2412, "Error in Pocketing : Offset not computable") ; return false ; } // recupero la prima curva di offset PtrOwner pOffs2( CreateCurveComposite()) ; if ( IsNull( pOffs2) || ! pOffs2->AddCurve( OffsCrv2.GetLongerCurve())) { m_pMchMgr->SetLastError( 2413, "Error in Pocketing : Toolpath not computable") ; return false ; } // se richiesto, la inverto if ( m_Params.m_bInvert) pOffs->Invert() ; // aggiungo la lavorazione di questa curva for ( int j = 1 ; j <= nStep ; ++ j) { // ciclo sulle curve elementari int nMaxInd = pOffs2->GetCurveCount() - 1 ; for ( int i = 0 ; i <= nMaxInd ; ++ i) { // curva corrente const ICurve* pCrvC = pOffs2->GetCurve( i) ; // copio la curva PtrOwner pCurve( pCrvC->Clone()) ; if ( IsNull( pCurve)) return false ; // aggiungo affondamento pCurve->Translate( - vtTool * ( dDepth - dElev + j * dStep)) ; // se prima entità if ( i == 0 ) { // dati inizio entità Point3d ptStart ; pCurve->GetStartPoint( ptStart) ; Vector3d vtStart ; pCurve->GetStartDir( vtStart) ; // se primo step, approccio e affondo if ( j == 1) { // determino inizio attacco if ( ! CalcLeadInStart( ptStart, vtStart, vtExtr, nullptr, ptP1)) return false ; // determino elevazione su inizio attacco double dStElev ; if ( ! GetElevation( m_nPhase, ptStart - 10 * EPS_SMALL * vtTool, vtTool, GetRadiusForStartEndElevation(), vtTool, dStElev)) dStElev = dElev ; dStElev -= ( ptP1 - ptStart) * vtExtr ; // se attacco a zigzag o a spirale o a scivolo, l'elevazione va nell'attacco if ( GetLeadInType() == POCKET_LI_ZIGZAG || GetLeadInType() == POCKET_LI_HELIX || GetLeadInType() == POCKET_LI_GLIDE) { ptP1 += vtExtr * dStElev ; dStElev = 0 ; } // approccio al punto iniziale if ( ! AddLinkApproach( ptP1, vtTool, dSafeZ, dSafeAggrBottZ, dStElev, dAppr)) { m_pMchMgr->SetLastError( 2418, "Error in Pocketing : Link not computable") ; return false ; } // aggiungo attacco SetFeed( GetStartFeed()) ; if ( ! AddLeadIn( ptP1, ptStart, vtStart, vtExtr, nullptr, ! m_Params.m_bInvert, bSplitArcs)) { m_pMchMgr->SetLastError( 2415, "Error in Pocketing : LeadIn not computable") ; return false ; } } // altrimenti solo collegamento else { SetFeed( GetStartFeed()) ; ptP1 -= vtExtr * dStep ; if ( ! AddLeadIn( ptP1, ptStart, vtStart, vtExtr, nullptr, ! m_Params.m_bInvert, bSplitArcs)) { m_pMchMgr->SetLastError( 2418, "Error in Pocketing : Link not computable") ; return false ; } } } // elaborazioni sulla curva corrente if ( pCurve->GetType() == CRV_LINE) { ICurveLine* pLine = GetCurveLine( pCurve) ; Point3d ptP3 = pLine->GetEnd() ; SetFeed( GetFeed()) ; if ( AddLinearMove( ptP3) == GDB_ID_NULL) return false ; } else if ( pCurve->GetType() == CRV_ARC) { ICurveArc* pArc = GetCurveArc( pCurve) ; Point3d ptCen = pArc->GetCenter() ; double dAngCen = pArc->GetAngCenter() ; Vector3d vtN = pArc->GetNormVersor() ; Point3d ptP3 ; pArc->GetEndPoint( ptP3) ; SetFeed( GetFeed()) ; if ( AddArcMove( ptP3, ptCen, dAngCen, vtN) == GDB_ID_NULL) return false ; } // se ultima entità if ( i == nMaxInd) { // se ultimo step, uscita e retrazione di collegamento if ( j == nStep) { // dati fine entità Point3d ptEnd ; pCurve->GetEndPoint( ptEnd) ; Vector3d vtEnd ; pCurve->GetEndDir( vtEnd) ; // aggiungo uscita Point3d ptP1 ; double dEndElev = dElev ; SetFeed( GetEndFeed()) ; if ( ! AddLeadOut( ptEnd, vtEnd, vtExtr, nullptr, bSplitArcs, ptP1, dEndElev)) { m_pMchMgr->SetLastError( 2416, "Error in Pocketing : LeadOut not computable") ; return false ; } // aggiungo retrazione if ( ! AddRetract( ptP1, vtTool, dSafeZ, dSafeAggrBottZ, dEndElev, dAppr)) { m_pMchMgr->SetLastError( 2418, "Error in Pocketing : Retract not computable") ; return false ; } } } } } return true ; } //---------------------------------------------------------------------------- bool Pocketing::CalcZigZag( const ICurveComposite* pOffs, ICRVCOMPOPOVECTOR& vpCrvs) { // ingombro del contorno offsettato BBox3d b3Pocket ; pOffs->GetLocalBBox( b3Pocket) ; Point3d ptMin ; double dDimX, dDimY, dDimZ ; b3Pocket.GetMinDim( ptMin, dDimX, dDimY, dDimZ) ; // lunghezza del contorno offsettato double dLen ; pOffs->GetLength( dLen) ; // passi in Y int nYStep = static_cast( ceil( ( dDimY - 20 * EPS_SMALL) / GetSideStep())) ; double dYStep = ( nYStep > 0 ? ( dDimY - 20 * EPS_SMALL) / nYStep : 0) ; // tratto valido struct Section { bool bActive ; Point3d ptS ; Point3d ptE ; double dOs ; double dOe ; } ; // raccolta di tratti typedef std::vector> VECVECSECT ; VECVECSECT vvSec ; vvSec.resize( nYStep + 1) ; // calcolo le linee di svuotatura int nCount = 0 ; for ( int i = 0 ; i <= nYStep ; ++ i) { // determino senso bool bPlus = (( i % 2) == 0) ; // definisco la linea PtrOwner pLine( CreateCurveLine()) ; const double EXP_LEN = 1.0 ; Point3d ptStart( ptMin.x - EXP_LEN, ptMin.y + 10 * EPS_SMALL + i * dYStep, ptMin.z + dDimZ) ; if ( IsNull( pLine) || ! pLine->SetPVL( ptStart, X_AX, dDimX + 2 * EXP_LEN)) { m_pMchMgr->SetLastError( 2413, "Error in Pocketing : Toolpath not computable") ; return false ; } // calcolo la classificazione della curva rispetto al contorno esterno offsettato IntersCurveCurve intCC( *pLine, *pOffs) ; CRVCVECTOR ccClass ; if ( ! intCC.GetCurveClassification( 0, ccClass)) { m_pMchMgr->SetLastError( 2413, "Error in Pocketing : Toolpath not computable") ; return false ; } // determino gli intervalli di curva da conservare Intervals inOk ; for ( auto& ccOne : ccClass) { if ( ccOne.nClass == CRVC_IN || ccOne.nClass == CRVC_ON_P || ccOne.nClass == CRVC_ON_M) inOk.Add( ccOne.dParS, ccOne.dParE) ; } // inserisco i tratti validi (secondo X+ i pari, secondo X- i dispari) double dParS, dParE ; bool bFound = ( bPlus ? inOk.GetFirst( dParS, dParE) : inOk.GetLast( dParE, dParS)) ; while ( bFound) { // determino i dati della sezione Section Sect ; Sect.bActive = true ; pLine->GetPointD1D2( dParS, ICurve::FROM_PLUS, Sect.ptS) ; pLine->GetPointD1D2( dParE, ICurve::FROM_MINUS, Sect.ptE) ; pOffs->GetParamAtPoint( Sect.ptS, Sect.dOs, 10 * EPS_SMALL) ; pOffs->GetParamAtPoint( Sect.ptE, Sect.dOe, 10 * EPS_SMALL) ; // inserisco nel contenitore vvSec[i].emplace_back( Sect) ; ++ nCount ; // recupero successivo intervallo bFound = ( bPlus ? inOk.GetNext( dParS, dParE) : inOk.GetPrev( dParE, dParS)) ; } } // dominio del contorno double dUmin, dUmax ; pOffs->GetDomain( dUmin, dUmax) ; double dUspan = dUmax - dUmin ; // creo i percorsi di svuotatura vpCrvs.reserve( nCount) ; int nI = -1, nJ = -1 ; while ( true) { // se sezione non valida if ( nI < 0 || nJ < 0) { // ricerco la prima valida for ( int k = 0 ; k < int( vvSec.size()) && nI < 0 ; ++ k) { for ( int l = 0 ; l < int( vvSec[k].size()) && nJ < 0 ; ++ l) { if ( vvSec[k][l].bActive) { nI = k ; nJ = l ; } } } // se trovata, creo nuova curva composita if ( nI >= 0 && nJ >= 0) { // creo la curva vpCrvs.emplace_back( CreateCurveComposite()) ; // aggiungo punto iniziale vpCrvs.back()->AddPoint( vvSec[nI][nJ].ptS) ; } // altrimenti, esco else break ; } // determino senso bool bPlus = (( nI % 2) == 0) ; // aggiungo la sezione alla curva Section& Sec = vvSec[nI][nJ] ; Sec.bActive = false ; vpCrvs.back()->AddLine( vvSec[nI][nJ].ptE) ; // cerco nella stessa fila o in quella successiva sezione successiva raccordabile tramite il contorno double dUstart = Sec.dOe ; double dUref = ( bPlus ? INFINITO : - INFINITO) ; int nNextI = -1 ; int nNextJ = -1 ; int li = nJ + 1 ; for ( int k = nI ; k <= nI + 1 && k < int( vvSec.size()) ; ++ k) { for ( int l = li ; l < int( vvSec[k].size()) ; ++ l) { if ( ! vvSec[k][l].bActive) continue ; double dU = vvSec[k][l].dOs ; if ( bPlus) { if ( dU < dUstart) dU += dUspan ; if ( dU < dUref) { dUref = dU ; nNextI = k ; nNextJ = l ; } } else { if ( dU > dUstart) dU -= dUspan ; if ( dU > dUref) { dUref = dU ; nNextI = k ; nNextJ = l ; } } } li = 0 ; } // se trovato, aggiungo il tratto di contorno e continuo if ( nNextI != -1) { PtrOwner pCopy ; if ( bPlus) { if ( dUref > dUmax) dUref -= dUspan ; pCopy.Set( pOffs->CopyParamRange( dUstart, dUref)) ; if ( ! IsNull( pCopy)) { double dCLen ; pCopy->GetLength( dCLen) ; if ( dCLen > 0.5 * dLen) { pCopy.Set( pOffs->CopyParamRange( dUref, dUstart)) ; if ( ! IsNull( pCopy)) pCopy->Invert() ; } } } else { if ( dUref < dUmin) dUref += dUspan ; pCopy.Set( pOffs->CopyParamRange( dUref, dUstart)) ; if ( ! IsNull( pCopy)) { pCopy->Invert() ; double dCLen ; pCopy->GetLength( dCLen) ; if ( dCLen > 0.5 * dLen) pCopy.Set( pOffs->CopyParamRange( dUstart, dUref)) ; } } BBox3d b3Copy ; if ( ! IsNull( pCopy)) pCopy->GetLocalBBox( b3Copy) ; if ( ! b3Copy.IsEmpty() && ( b3Copy.GetMax().y - b3Copy.GetMin().y) < dYStep + 10 * EPS_SMALL) { vpCrvs.back()->AddCurve( Release( pCopy)) ; nI = nNextI ; nJ = nNextJ ; } else { nI = -1 ; nJ = -1 ; } } else { nI = -1 ; nJ = -1 ; } } return true ; } //---------------------------------------------------------------------------- bool Pocketing::AddOneWay( const ICurveComposite* pCompo, const Vector3d& vtTool, const Vector3d& vtExtr, double dDepth, double dElev, double dOkStep, bool bSplitArcs) { // recupero distanze di sicurezza double dSafeZ = m_pMchMgr->GetCurrMachiningsMgr()->GetSafeZ() ; double dSafeAggrBottZ = m_pMchMgr->GetCurrMachiningsMgr()->GetSafeAggrBottZ() ; // lunghezza di approccio/retrazione double dAppr = m_Params.m_dStartPos ; // determino numero e affondamento degli step int nStep = 1 ; nStep = max( 1, static_cast( ceil( dElev / dOkStep))) ; double dStep = dElev / nStep ; // calcolo curva offsettata del raggio utensile + sovramateriale double dTRad = 0.5 * m_TParams.m_dDiam ; double dOffs = dTRad + GetOffsR() ; OffsetCurve OffsCrv ; if ( ! OffsCrv.Make( pCompo, - dOffs, ICurve::OFF_FILLET)) { m_pMchMgr->SetLastError( 2412, "Error in Pocketing : Offset not computable") ; return false ; } // recupero la prima curva di offset PtrOwner pOffs( CreateCurveComposite()) ; if ( IsNull( pOffs) || ! pOffs->AddCurve( OffsCrv.GetLongerCurve())) { m_pMchMgr->SetLastError( 2413, "Error in Pocketing : Toolpath not computable") ; return false ; } // se richiesto, la inverto if ( m_Params.m_bInvert) pOffs->Invert() ; // coefficiente di riduzione feed di lavorazione di questa curva double dFeedRid = min( GetSideStep() / m_TParams.m_dDiam, 1.0) ; // aggiungo la lavorazione di questa curva Point3d ptP1 ; for ( int j = 1 ; j <= nStep ; ++ j) { // ciclo sulle curve elementari int nMaxInd = pOffs->GetCurveCount() - 1 ; for ( int i = 0 ; i <= nMaxInd ; ++ i) { // curva corrente const ICurve* pCrvC = pOffs->GetCurve( i) ; // copio la curva PtrOwner pCurve( pCrvC->Clone()) ; if ( IsNull( pCurve)) return false ; // aggiungo affondamento pCurve->Translate( - vtTool * ( dDepth - dElev + j * dStep)) ; // se prima entità if ( i == 0 ) { // dati inizio entità Point3d ptStart ; pCurve->GetStartPoint( ptStart) ; Vector3d vtStart ; pCurve->GetStartDir( vtStart) ; // se primo step, approccio e affondo if ( j == 1) { // determino inizio attacco if ( ! CalcLeadInStart( ptStart, vtStart, vtExtr, nullptr, ptP1)) return false ; // determino elevazione su inizio attacco double dStElev ; if ( ! GetElevation( m_nPhase, ptStart - 10 * EPS_SMALL * vtTool, vtTool, GetRadiusForStartEndElevation(), vtTool, dStElev)) dStElev = dElev ; dStElev -= ( ptP1 - ptStart) * vtExtr ; // se attacco a zigzag o a spirale o a scivolo, l'elevazione va nell'attacco if ( GetLeadInType() == POCKET_LI_ZIGZAG || GetLeadInType() == POCKET_LI_HELIX || GetLeadInType() == POCKET_LI_GLIDE) { ptP1 += vtExtr * dStElev ; dStElev = 0 ; } // approccio al punto iniziale if ( ! AddApproach( ptP1, vtTool, dSafeZ, dSafeAggrBottZ, dStElev, dAppr)) { m_pMchMgr->SetLastError( 2414, "Error in Pocketing : Approach not computable") ; return false ; } // aggiungo attacco SetFeed( GetStartFeed()) ; if ( ! AddLeadIn( ptP1, ptStart, vtStart, vtExtr, nullptr, ! m_Params.m_bInvert, bSplitArcs)) { m_pMchMgr->SetLastError( 2415, "Error in Pocketing : LeadIn not computable") ; return false ; } } // altrimenti solo collegamento else { SetFeed( GetStartFeed()) ; ptP1 -= vtExtr * dStep ; if ( ! AddLeadIn( ptP1, ptStart, vtStart, vtExtr, nullptr, ! m_Params.m_bInvert, bSplitArcs)) { m_pMchMgr->SetLastError( 2418, "Error in Pocketing : Link not computable") ; return false ; } } } // elaborazioni sulla curva corrente if ( pCurve->GetType() == CRV_LINE) { ICurveLine* pLine = GetCurveLine( pCurve) ; Point3d ptP3 = pLine->GetEnd() ; SetFeed( dFeedRid * GetFeed()) ; if ( AddLinearMove( ptP3) == GDB_ID_NULL) return false ; } else if ( pCurve->GetType() == CRV_ARC) { ICurveArc* pArc = GetCurveArc( pCurve) ; Point3d ptCen = pArc->GetCenter() ; double dAngCen = pArc->GetAngCenter() ; Vector3d vtN = pArc->GetNormVersor() ; Point3d ptP3 ; pArc->GetEndPoint( ptP3) ; SetFeed( dFeedRid * GetFeed()) ; if ( AddArcMove( ptP3, ptCen, dAngCen, vtN) == GDB_ID_NULL) return false ; } // se ultima entità if ( i == nMaxInd) { // se ultimo step, uscita e retrazione di collegamento if ( j == nStep) { // dati fine entità Point3d ptEnd ; pCurve->GetEndPoint( ptEnd) ; Vector3d vtEnd ; pCurve->GetEndDir( vtEnd) ; // aggiungo uscita Point3d ptP1 ; double dEndElev = dElev ; SetFeed( GetEndFeed()) ; if ( ! AddLeadOut( ptEnd, vtEnd, vtExtr, nullptr, bSplitArcs, ptP1, dEndElev)) { m_pMchMgr->SetLastError( 2416, "Error in Pocketing : LeadOut not computable") ; return false ; } // aggiungo retrazione if ( ! AddLinkRetract( ptP1, vtTool, dSafeZ, dSafeAggrBottZ, dEndElev, dAppr)) { m_pMchMgr->SetLastError( 2418, "Error in Pocketing : Link not computable") ; return false ; } } } } } // calcolo seconda curva di offset OffsetCurve OffsCrv2 ; double dExtra = min( 0.1 * m_TParams.m_dDiam, 1.0) ; if ( ! OffsCrv2.Make( pCompo, - ( dOffs + dExtra), ICurve::OFF_FILLET)) { m_pMchMgr->SetLastError( 2412, "Error in Pocketing : Offset not computable") ; return false ; } PtrOwner pOffs2( CreateCurveComposite()) ; if ( IsNull( pOffs2) || ! pOffs2->AddCurve( OffsCrv2.GetLongerCurve())) { m_pMchMgr->SetLastError( 2413, "Error in Pocketing : Toolpath not computable") ; return false ; } // determino il riferimento di base e il box della svuotatura Frame3d frPocket ; Point3d ptCen ; pCompo->GetCentroid( ptCen) ; frPocket.Set( ptCen, vtExtr) ; frPocket.Rotate( ptCen, vtExtr, m_Params.m_dSideAngle) ; pOffs2->ToLoc( frPocket) ; BBox3d b3Pocket ; pOffs2->GetLocalBBox( b3Pocket) ; Point3d ptMin ; double dDimX, dDimY, dDimZ ; b3Pocket.GetMinDim( ptMin, dDimX, dDimY, dDimZ) ; // passi in Y int nYStep = static_cast( ceil( ( dDimY + 2 * dExtra) / GetSideStep())) ; double dYStep = ( nYStep > 0 ? ( dDimY + 2 * dExtra) / nYStep : 0) ; -- nYStep ; // calcolo le linee di svuotatura bool bStart = true ; const double EXP_LEN = 1.0 ; for ( int j = 1 ; j <= nStep ; ++ j) { for ( int i = 1 ; i <= nYStep ; ++ i) { // definisco la linea PtrOwner pLine( CreateCurveLine()) ; Point3d ptStart( ptMin.x - EXP_LEN, ptMin.y + ( - dExtra + i * dYStep), ptMin.z + dDimZ) ; if ( IsNull( pLine) || ! pLine->SetPVL( ptStart, X_AX, dDimX + 2 * EXP_LEN)) { m_pMchMgr->SetLastError( 2413, "Error in Pocketing : Toolpath not computable") ; return false ; } // calcolo la classificazione della curva rispetto al contorno esterno offsettato IntersCurveCurve intCC( *pLine, *pOffs2) ; CRVCVECTOR ccClass ; if ( ! intCC.GetCurveClassification( 0, ccClass)) { m_pMchMgr->SetLastError( 2413, "Error in Pocketing : Toolpath not computable") ; return false ; } // determino gli intervalli di curva da conservare Intervals inOk ; for ( auto& ccOne : ccClass) { if ( ccOne.nClass == CRVC_IN || ccOne.nClass == CRVC_ON_P || ccOne.nClass == CRVC_ON_M) inOk.Add( ccOne.dParS, ccOne.dParE) ; } // inserisco i tratti validi double dParS, dParE ; bool bFound = inOk.GetFirst( dParS, dParE) ; while ( bFound) { // calcolo inizio con affondamento Point3d ptS ; pLine->GetPointD1D2( dParS, ICurve::FROM_PLUS, ptS) ; ptS.ToGlob( frPocket) ; ptS.Translate( - vtTool * ( dDepth - dElev + j * dStep)) ; // determino inizio attacco Point3d ptP ; if ( ! CalcLeadInStart( ptS, frPocket.VersX(), vtExtr, nullptr, ptP)) return false ; // determino elevazione su inizio attacco double dStElev ; if ( ! GetElevation( m_nPhase, ptS - 10 * EPS_SMALL * vtTool, vtTool, GetRadiusForStartEndElevation(), vtTool, dStElev)) dStElev = dElev ; dStElev -= ( ptP - ptS) * vtExtr ; // sempre approccio di collegamento if ( ! AddLinkApproach( ptP, vtTool, dSafeZ, dSafeAggrBottZ, dStElev, dAppr)) { m_pMchMgr->SetLastError( 2418, "Error in Pocketing : Link not computable") ; return false ; } // aggiungo attacco SetFeed( GetStartFeed()) ; if ( ! AddLeadIn( ptP, ptS, frPocket.VersX(), vtExtr, nullptr, ! m_Params.m_bInvert, bSplitArcs, true)) { m_pMchMgr->SetLastError( 2415, "Error in Pocketing : LeadIn not computable") ; return false ; } // calcolo fine con affondamento Point3d ptE ; pLine->GetPointD1D2( dParE, ICurve::FROM_MINUS, ptE) ; ptE.ToGlob( frPocket) ; ptE.Translate( - vtTool * ( dDepth - dElev + j * dStep)) ; // movimento al punto finale SetFeed( GetFeed()) ; if ( AddLinearMove( ptE) == GDB_ID_NULL) return false ; // risalita Point3d ptQ ; double dEndElev = dElev ; SetFeed( GetEndFeed()) ; if ( ! AddLeadOut( ptE, frPocket.VersX(), vtExtr, nullptr, bSplitArcs, ptQ, dEndElev, true)) { m_pMchMgr->SetLastError( 2416, "Error in Pocketing : LeadOut not computable") ; return false ; } // recupero successivo intervallo bFound = inOk.GetNext( dParS, dParE) ; // se ultimo movimento, aggiungo retrazione globale if ( j == nStep && i == nYStep && ! bFound) { if ( ! AddRetract( ptQ, vtTool, dSafeZ, dSafeAggrBottZ, dEndElev, dAppr)) { m_pMchMgr->SetLastError( 2417, "Error in Pocketing : Retract not computable") ; return false ; } } // altrimenti aggiungo retrazione di collegamento else { if ( ! AddLinkRetract( ptQ, vtTool, dSafeZ, dSafeAggrBottZ, dEndElev, dAppr)) { m_pMchMgr->SetLastError( 2418, "Error in Pocketing : Link not computable") ; return false ; } } } } } return true ; } //---------------------------------------------------------------------------- bool Pocketing::AddSpiralIn( const ICurveComposite* pCompo, const Vector3d& vtTool, const Vector3d& vtExtr, double dDepth, double dElev, double dOkStep, bool bSplitArcs) { // recupero distanze di sicurezza double dSafeZ = m_pMchMgr->GetCurrMachiningsMgr()->GetSafeZ() ; double dSafeAggrBottZ = m_pMchMgr->GetCurrMachiningsMgr()->GetSafeAggrBottZ() ; // lunghezza di approccio/retrazione double dAppr = m_Params.m_dStartPos ; // calcolo la spirale dall'esterno all'interno e la curva che unisce inizio e fine PtrOwner pMCrv( CreateCurveComposite()) ; PtrOwner pRCrv( CreateCurveComposite()) ; if ( IsNull( pMCrv) || IsNull( pRCrv)) { m_pMchMgr->SetLastError( 2411, "Error in Pocketing : toolpath allocation failed") ; return false ; } if ( ! CalcSpiral( pCompo, bSplitArcs, pMCrv, pRCrv)) return false ; // determino numero e affondamento degli step int nStep = 1 ; nStep = max( 1, static_cast( ceil( dElev / dOkStep))) ; double dStep = dElev / nStep ; int nMaxInd = pMCrv->GetCurveCount() - 1 ; int nMaxRInd = pRCrv->GetCurveCount() - 1 ; // ciclo sugli step Point3d ptP1 ; for ( int j = 1 ; j <= nStep ; ++ j) { // ciclo sulle curve elementari for ( int i = 0 ; i <= nMaxInd ; ++ i) { // curva corrente const ICurve* pCrvC = pMCrv->GetCurve( i) ; // copio la curva PtrOwner pCurve( pCrvC->Clone()) ; if ( IsNull( pCurve)) return false ; // aggiungo affondamento pCurve->Translate( - vtTool * ( dDepth - dElev + j * dStep)) ; // se prima entità if ( i == 0 ) { // dati inizio entità Point3d ptStart ; pCurve->GetStartPoint( ptStart) ; Vector3d vtStart ; pCurve->GetStartDir( vtStart) ; // se primo step, approccio e affondo if ( j == 1) { // determino inizio attacco if ( ! CalcLeadInStart( ptStart, vtStart, vtExtr, pRCrv, ptP1)) return false ; // determino elevazione su inizio attacco double dStElev ; if ( ! GetElevation( m_nPhase, ptStart - 10 * EPS_SMALL * vtTool, vtTool, GetRadiusForStartEndElevation(), vtTool, dStElev)) dStElev = dElev ; dStElev -= ( ptP1 - ptStart) * vtExtr ; // se attacco a zigzag o a spirale o a scivolo, l'elevazione va nell'attacco if ( GetLeadInType() == POCKET_LI_ZIGZAG || GetLeadInType() == POCKET_LI_HELIX || GetLeadInType() == POCKET_LI_GLIDE) { ptP1 += vtExtr * dStElev ; dStElev = 0 ; } // approccio al punto iniziale if ( ! AddApproach( ptP1, vtTool, dSafeZ, dSafeAggrBottZ, dStElev, dAppr)) { m_pMchMgr->SetLastError( 2414, "Error in Pocketing : Approach not computable") ; return false ; } // aggiungo attacco SetFeed( GetStartFeed()) ; if ( ! AddLeadIn( ptP1, ptStart, vtStart, vtExtr, pRCrv, ! m_Params.m_bInvert, bSplitArcs)) { m_pMchMgr->SetLastError( 2415, "Error in Pocketing : LeadIn not computable") ; return false ; } } // altrimenti solo collegamento else { SetFeed( GetStartFeed()) ; ptP1 -= vtExtr * dStep ; if ( ! AddLeadIn( ptP1, ptStart, vtStart, vtExtr, pRCrv, ! m_Params.m_bInvert, bSplitArcs)) { m_pMchMgr->SetLastError( 2418, "Error in Pocketing : Link not computable") ; return false ; } } } // elaborazioni sulla curva corrente if ( pCurve->GetType() == CRV_LINE) { ICurveLine* pLine = GetCurveLine( pCurve) ; Point3d ptP3 = pLine->GetEnd() ; SetFeed( GetFeed()) ; if ( AddLinearMove( ptP3) == GDB_ID_NULL) return false ; } else if ( pCurve->GetType() == CRV_ARC) { ICurveArc* pArc = GetCurveArc( pCurve) ; Point3d ptCen = pArc->GetCenter() ; double dAngCen = pArc->GetAngCenter() ; Vector3d vtN = pArc->GetNormVersor() ; Point3d ptP3 ; pArc->GetEndPoint( ptP3) ; SetFeed( GetFeed()) ; if ( AddArcMove( ptP3, ptCen, dAngCen, vtN) == GDB_ID_NULL) return false ; } // se ultima entità if ( i == nMaxInd) { // se step intermedio, ritorno all'inizio direttamente if ( j < nStep) { // se necessario ritorno all'inizio if ( nMaxRInd >= 0) { // copio la curva di ritorno PtrOwner pRet( pRCrv->Clone()) ; if ( IsNull( pRet)) return false ; // aggiungo affondamento pRet->Translate( - vtTool * ( dDepth - dElev + j * dStep)) ; // se attacco a scivolo, accorcio della lunghezza dell'attacco if ( GetLeadInType() == POCKET_LI_GLIDE) { double dLen ; pRet->GetLength( dLen) ; if ( dLen > m_Params.m_dLiTang + 10 * EPS_SMALL) pRet->TrimEndAtLen( dLen - m_Params.m_dLiTang) ; else pRet->Clear() ; } // emetto SetFeed( GetFeed()) ; if ( pRet->GetCurveCount() > 0 && AddCurveMove( pRet) == GDB_ID_NULL) return false ; } } // atrimenti ultimo step, uscita e retrazione else { // dati fine entità Point3d ptEnd ; pCurve->GetEndPoint( ptEnd) ; Vector3d vtEnd ; pCurve->GetEndDir( vtEnd) ; // aggiungo uscita Point3d ptP1 ; double dEndElev = dElev ; SetFeed( GetEndFeed()) ; if ( ! AddLeadOut( ptEnd, vtEnd, vtExtr, pRCrv, bSplitArcs, ptP1, dEndElev)) { m_pMchMgr->SetLastError( 2416, "Error in Pocketing : LeadOut not computable") ; return false ; } // aggiungo retrazione if ( ! AddRetract( ptP1, vtTool, dSafeZ, dSafeAggrBottZ, dEndElev, dAppr)) { m_pMchMgr->SetLastError( 2417, "Error in Pocketing : Retract not computable") ; return false ; } } } } } return true ; } //---------------------------------------------------------------------------- bool Pocketing::AddSpiralOut( const ICurveComposite* pCompo, const Vector3d& vtTool, const Vector3d& vtExtr, double dDepth, double dElev, double dOkStep, bool bSplitArcs) { // recupero distanze di sicurezza double dSafeZ = m_pMchMgr->GetCurrMachiningsMgr()->GetSafeZ() ; double dSafeAggrBottZ = m_pMchMgr->GetCurrMachiningsMgr()->GetSafeAggrBottZ() ; // lunghezza di approccio/retrazione double dAppr = m_Params.m_dStartPos ; // calcolo la spirale dall'interno all'esterno PtrOwner pMCrv( CreateCurveComposite()) ; PtrOwner pRCrv( CreateCurveComposite()) ; if ( IsNull( pMCrv) || IsNull( pRCrv)) { m_pMchMgr->SetLastError( 2411, "Error in Pocketing : toolpath allocation failed") ; return false ; } if ( ! CalcSpiral( pCompo, bSplitArcs, pMCrv, pRCrv)) return false ; pMCrv->Invert() ; pRCrv->Invert() ; // determino numero e affondamento degli step int nStep = 1 ; nStep = max( 1, static_cast( ceil( dElev / dOkStep))) ; double dStep = dElev / nStep ; int nMaxInd = pMCrv->GetCurveCount() - 1 ; int nMaxRInd = pRCrv->GetCurveCount() - 1 ; // ciclo sugli step Point3d ptP1 ; for ( int j = 1 ; j <= nStep ; ++ j) { // ciclo sulle curve elementari for ( int i = 0 ; i <= nMaxInd ; ++ i) { // curva corrente const ICurve* pCrvC = pMCrv->GetCurve( i) ; // copio la curva PtrOwner pCurve( pCrvC->Clone()) ; if ( IsNull( pCurve)) return false ; // aggiungo affondamento pCurve->Translate( - vtTool * ( dDepth - dElev + j * dStep)) ; // se prima entità if ( i == 0 ) { // dati inizio entità Point3d ptStart ; pCurve->GetStartPoint( ptStart) ; Vector3d vtStart ; pCurve->GetStartDir( vtStart) ; // se primo step, approccio e affondo if ( j == 1) { // determino inizio attacco if ( ! CalcLeadInStart( ptStart, vtStart, vtExtr, pRCrv, ptP1)) { m_pMchMgr->SetLastError( 2415, "Error in Pocketing : LeadIn not computable") ; return false ; } // determino elevazione su inizio attacco double dStElev ; if ( ! GetElevation( m_nPhase, ptStart - 10 * EPS_SMALL * vtTool, vtTool, GetRadiusForStartEndElevation(), vtTool, dStElev)) dStElev = dElev ; dStElev -= ( ptP1 - ptStart) * vtExtr ; // se attacco a zigzag o a spirale o a scivolo, l'elevazione va nell'attacco if ( GetLeadInType() == POCKET_LI_ZIGZAG || GetLeadInType() == POCKET_LI_HELIX || GetLeadInType() == POCKET_LI_GLIDE) { ptP1 += vtExtr * dStElev ; dStElev = 0 ; } // approccio al punto iniziale if ( ! AddApproach( ptP1, vtTool, dSafeZ, dSafeAggrBottZ, dStElev, dAppr)) { m_pMchMgr->SetLastError( 2414, "Error in Pocketing : Approach not computable") ; return false ; } // aggiungo attacco SetFeed( GetStartFeed()) ; if ( ! AddLeadIn( ptP1, ptStart, vtStart, vtExtr, pRCrv, m_Params.m_bInvert, bSplitArcs)) { m_pMchMgr->SetLastError( 2415, "Error in Pocketing : LeadIn not computable") ; return false ; } } // altrimenti solo collegamento else { SetFeed( GetStartFeed()) ; ptP1 -= vtExtr * dStep ; if ( ! AddLeadIn( ptP1, ptStart, vtStart, vtExtr, pRCrv, m_Params.m_bInvert, bSplitArcs)) { m_pMchMgr->SetLastError( 2418, "Error in Pocketing : Link not computable") ; return false ; } } } // elaborazioni sulla curva corrente if ( pCurve->GetType() == CRV_LINE) { ICurveLine* pLine = GetCurveLine( pCurve) ; Point3d ptP3 = pLine->GetEnd() ; SetFeed( GetFeed()) ; if ( AddLinearMove( ptP3) == GDB_ID_NULL) return false ; } else if ( pCurve->GetType() == CRV_ARC) { ICurveArc* pArc = GetCurveArc( pCurve) ; Point3d ptCen = pArc->GetCenter() ; double dAngCen = pArc->GetAngCenter() ; Vector3d vtN = pArc->GetNormVersor() ; Point3d ptP3 ; pArc->GetEndPoint( ptP3) ; SetFeed( GetFeed()) ; if ( AddArcMove( ptP3, ptCen, dAngCen, vtN) == GDB_ID_NULL) return false ; } // se ultima entità if ( i == nMaxInd) { // se step intermedio if ( j < nStep) { // se necessario ritorno all'inizio if ( nMaxRInd >= 0) { // copio la curva di ritorno PtrOwner pRet( pRCrv->Clone()) ; if ( IsNull( pRet)) return false ; // aggiungo affondamento pRet->Translate( - vtTool * ( dDepth - dElev + j * dStep)) ; // se attacco a scivolo, accorcio della lunghezza dell'attacco if ( GetLeadInType() == POCKET_LI_GLIDE) { double dLen ; pRet->GetLength( dLen) ; if ( dLen > m_Params.m_dLiTang + 10 * EPS_SMALL) pRet->TrimEndAtLen( dLen - m_Params.m_dLiTang) ; else pRet->Clear() ; } // emetto SetFeed( GetFeed()) ; if ( pRet->GetCurveCount() > 0 && AddCurveMove( pRet) == GDB_ID_NULL) return false ; } } // atrimenti ultimo step, uscita e retrazione else { // dati fine entità Point3d ptEnd ; pCurve->GetEndPoint( ptEnd) ; Vector3d vtEnd ; pCurve->GetEndDir( vtEnd) ; // aggiungo uscita Point3d ptQ ; double dEndElev = dElev ; SetFeed( GetEndFeed()) ; if ( ! AddLeadOut( ptEnd, vtEnd, vtExtr, pRCrv, bSplitArcs, ptQ, dEndElev)) { m_pMchMgr->SetLastError( 2416, "Error in Pocketing : LeadOut not computable") ; return false ; } // aggiungo retrazione if ( ! AddRetract( ptQ, vtTool, dSafeZ, dSafeAggrBottZ, dEndElev, dAppr)) { m_pMchMgr->SetLastError( 2417, "Error in Pocketing : Retract not computable") ; return false ; } } } } } return true ; } //---------------------------------------------------------------------------- bool Pocketing::CalcSpiral( const ICurveComposite* pCompo, bool bSplitArcs, ICurveComposite* pMCrv, ICurveComposite* pRCrv) { // primo offset pari al raggio utensile + sovramateriale double dTRad = 0.5 * m_TParams.m_dDiam ; double dOffs = dTRad + GetOffsR() ; // se circonferenza, chiamo la funzione specializzata Point3d ptCen ; Vector3d vtN ; double dRad ; bool bCCW ; if ( pCompo->IsACircle( 10 * EPS_SMALL, ptCen, vtN, dRad, bCCW)) { double dIntRad = 0 ; if ( m_Params.m_nSubType == POCKET_SUB_SPIRALOUT && GetLeadInType() == POCKET_LI_HELIX) { dIntRad = min( 0.5 * min( m_Params.m_dLiTang, m_TParams.m_dDiam), dRad - dOffs) ; m_dMaxHelixRad = dIntRad ; } return CalcCircleSpiral( ptCen, vtN, dRad - dOffs, dIntRad, bSplitArcs, pMCrv, pRCrv) ; } // ciclo di offset verso l'interno const int MAX_ITER = 100 ; int nIter = 0 ; PtrOwner pOffs ; const ICurve* pCurr = pCompo ; double dCurrRad = GetCurveRadius( pCurr) ; while ( nIter < MAX_ITER) { // calcolo OffsetCurve OffsCrv ; if ( ! OffsCrv.Make( pCurr, - dOffs, ICurve::OFF_FILLET)) { m_pMchMgr->SetLastError( 2412, "Error in Pocketing : Offset not computable") ; return false ; } // recupero la prima curva di offset PtrOwner pNextOffs( OffsCrv.GetLongerCurve()) ; double dRad = GetCurveRadius( pNextOffs) ; bool bNextOk = ( dRad > EPS_ZERO && dRad < dCurrRad) ; bool bSmallRad = ( nIter == 0 ? dOffs < dTRad + GetOffsR() + EPS_ZERO : dOffs < dTRad + EPS_ZERO) ; // se completato step di offset, accodo la curva offsettata al percorso di lavoro if ( ! IsNull( pOffs) && ( bNextOk || bSmallRad)) { // verifico se aggiungere linea di congiunzione if ( pMCrv->GetCurveCount() > 0) { Point3d ptStart ; pOffs->GetStartPoint( ptStart) ; // aggiungo al percorso principale pMCrv->AddLine( ptStart) ; // copio nel percorso di ritorno pRCrv->AddCurve( *pMCrv->GetLastCurve()) ; } // se richiesta percorrenza invertita if ( m_Params.m_bInvert) pOffs->Invert() ; // aggiungo la curva pMCrv->AddCurve( Release( pOffs)) ; } // se offset va bene if ( bNextOk) { // sistemo per prossimo step dCurrRad = dRad ; pOffs.Set( Release( pNextOffs)) ; pCurr = Get( pOffs) ; // nuovo valore pari allo step dOffs = GetSideStep() ; } // se altrimenti riducibile, provo con offset ridotto al raggio utensile else if ( ! bSmallRad) { // nuovo valore pari al raggio dOffs = ( nIter == 0 ? dTRad + GetOffsR() : dTRad) ; } // altrimenti esco else break ; // incremento contatore iterazioni ++ nIter ; } // verifico il percorso di lavoro if ( pMCrv->GetCurveCount() == 0) { m_pMchMgr->SetLastError( 2413, "Error in Pocketing : Toolpath not computable") ; return false ; } // se necessario, approssimo archi con rette if ( bSplitArcs && ! ApproxWithLines( pMCrv)) { m_pMchMgr->SetLastError( 2421, "Error in Pocketing : Linear Approx not computable") ; return false ; } // eventuale sistemazione archi VerifyArcs( pMCrv) ; // compatto e inverto il percorso di ritorno pRCrv->MergeCurves( 10 * EPS_SMALL, 10 * EPS_ANG_SMALL, false) ; pRCrv->Invert() ; return true ; } //---------------------------------------------------------------------------- bool Pocketing::CalcCircleSpiral( const Point3d& ptCen, const Vector3d& vtN, double dOutRad, double dIntRad, bool bSplitArcs, ICurveComposite* pMCrv, ICurveComposite* pRCrv) { // raggio della circonferenza esterna if ( dOutRad < 10 * EPS_SMALL) { m_pMchMgr->SetLastError( 2413, "Error in Pocketing : Toolpath not computable") ; return false ; } // imposto versore estrusione sulle curve composite pMCrv->SetExtrusion( vtN) ; pRCrv->SetExtrusion( vtN) ; // creo e inserisco la circonferenza esterna PtrOwner pArc( CreateCurveArc()) ; if ( IsNull( pArc) || ! pArc->Set( ptCen, vtN, dOutRad)) { m_pMchMgr->SetLastError( 2413, "Error in Pocketing : Toolpath not computable") ; return false ; } Vector3d vtDir = pArc->GetStartVersor() ; pMCrv->AddCurve( Release( pArc)) ; // se richiesta percorrenza invertita if ( m_Params.m_bInvert) pMCrv->Invert() ; // se raggio esterno maggiore dell'interno if ( dOutRad > dIntRad + 10 * EPS_SMALL) { // aggiungo le semicirconferenze della spirale ( devono essere in numero dispari) int nStep = int( ceil( ( dOutRad - dIntRad) / ( 0.5 * GetSideStep()))) ; if ( IsEven( nStep)) nStep += 1 ; double dStep = ( dOutRad - dIntRad) / nStep ; for ( int i = 1 ; i <= nStep ; ++ i) { if ( ! IsEven( i)) pMCrv->AddArcTg( ptCen - vtDir * ( dOutRad - i * dStep)) ; else pMCrv->AddArcTg( ptCen + vtDir * ( dOutRad - i * dStep)) ; } // aggiungo la circonferenza interna pMCrv->AddArcTg( ptCen + vtDir * dIntRad) ; pMCrv->AddArcTg( ptCen - vtDir * dIntRad) ; } // verifico il percorso di lavoro if ( pMCrv->GetCurveCount() == 0) { m_pMchMgr->SetLastError( 2413, "Error in Pocketing : Toolpath not computable") ; return false ; } // se necessario, approssimo con rette if ( bSplitArcs && ! ApproxWithLines( pMCrv)) { m_pMchMgr->SetLastError( 2421, "Error in Pocketing : Linear Approx not computable") ; return false ; } // eventuale sistemazione archi VerifyArcs( pMCrv) ; // calcolo l'eventuale percorso di ritorno Point3d ptStart ; pMCrv->GetStartPoint( ptStart) ; Point3d ptEnd ; pMCrv->GetEndPoint( ptEnd) ; Vector3d vtStart ; pMCrv->GetStartDir( vtStart) ; if ( ! AreSamePointApprox( ptStart, ptEnd)) { PtrOwner pArc2( CreateCurveArc()) ; if ( IsNull( pArc2) || ! pArc2->Set2PVN( ptStart, ptEnd, - vtStart, vtN)) { m_pMchMgr->SetLastError( 2420, "Error in Pocketing : Return toolpath not computable") ; return false ; } pRCrv->AddCurve( Release( pArc2)) ; // inverto e eventualmente sistemo archi pRCrv->Invert() ; VerifyArcs( pRCrv) ; } return true ; } //---------------------------------------------------------------------------- bool Pocketing::AddApproach( const Point3d& ptP, const Vector3d& vtTool, double dSafeZ, double dSafeAggrBottZ, double dElev, double dAppr) { SetFlag( 1) ; // se con aggregato da sotto if ( m_bAggrBottom) { // aggiuntivo in Z double dAggZ = max( dElev + max( dSafeAggrBottZ, dAppr), 0.) ; // distanza dal bordo del pezzo double dDistBottom ; if ( ! GetDistanceFromRawSide( m_nPhase, ptP, m_vtAggrBottom, dDistBottom)) dDistBottom = 0 ; // pre-approccio Point3d ptP0 = ptP - Z_AX * dAggZ + m_vtAggrBottom * ( dDistBottom + m_AggrBottom.dEncH + dSafeZ) ; Point3d ptP00 = ptP0 + Z_AX * ( m_AggrBottom.dEncV + m_TParams.m_dLen + dAggZ - dElev) ; // se rinvio da sotto che richiede speciale rotazione if ( m_AggrBottom.nType == 1) { Vector3d vtAux = m_vtAggrBottom ; vtAux.Rotate( Z_AX, 90) ; SetAuxDir( vtAux) ; if ( AddRapidStart( ptP00, MCH_CL_AGB_DWN) == GDB_ID_NULL) return false ; SetAuxDir( m_vtAggrBottom) ; SetFlag( 0) ; if ( AddRapidMove( ptP0, MCH_CL_AGB_IN) == GDB_ID_NULL) return false ; } // altrimenti rinvio normale else { SetAuxDir( m_vtAggrBottom) ; if ( AddRapidStart( ptP0, MCH_CL_AGB_IN) == GDB_ID_NULL) return false ; SetFlag( 0) ; } } // se sopra attacco c'è spazio per sicurezza o approccio double dSafeDist = ( m_bAggrBottom ? dSafeAggrBottZ : dSafeZ) ; if ( dElev + max( dSafeDist, dAppr) > 10 * EPS_SMALL) { // se distanza di sicurezza minore di distanza di inizio if ( dSafeDist < dAppr + 10 * EPS_SMALL) { // 1 -> punto sopra inizio Point3d ptP1 = ptP + vtTool * ( dElev + dAppr) ; if ( ( ! m_bAggrBottom && AddRapidStart( ptP1) == GDB_ID_NULL) || ( m_bAggrBottom && AddRapidMove( ptP1) == GDB_ID_NULL)) return false ; } else { // 1a -> punto sopra inizio Point3d ptP1b = ptP + vtTool * ( dElev + dAppr) ; Point3d ptP1a = ptP1b + vtTool * ( dSafeDist - dAppr) ; if ( ( ! m_bAggrBottom && AddRapidStart( ptP1a) == GDB_ID_NULL) || ( m_bAggrBottom && AddRapidMove( ptP1a) == GDB_ID_NULL)) return false ; // 1b -> punto appena sopra inizio if ( ( dElev + dAppr) > EPS_SMALL) { SetFlag( 0) ; if ( AddRapidMove( ptP1b) == GDB_ID_NULL) return false ; } } // affondo al punto iniziale SetFlag( 0) ; SetFeed( GetTipFeed()) ; if ( AddLinearMove( ptP) == GDB_ID_NULL) return false ; } else { // affondo diretto al punto iniziale SetFlag( 0) ; if ( ( ! m_bAggrBottom && AddRapidStart( ptP) == GDB_ID_NULL) || ( m_bAggrBottom && AddRapidMove( ptP) == GDB_ID_NULL)) return false ; } return true ; } //---------------------------------------------------------------------------- bool Pocketing::AddLinkApproach( const Point3d& ptP, const Vector3d& vtTool, double dSafeZ, double dSafeAggrBottZ, double dElev, double dAppr) { // se sopra attacco c'è spazio per approccio if ( ( dElev + dAppr) > 10 * EPS_SMALL) { // 1b -> punto appena sopra inizio Point3d ptP1b = ptP + vtTool * ( dElev + dAppr) ; if ( ( dElev + dAppr) > EPS_SMALL) { SetFlag( 0) ; if ( AddRapidMove( ptP1b) == GDB_ID_NULL) return false ; } // affondo al punto iniziale SetFlag( 0) ; SetFeed( GetTipFeed()) ; if ( AddLinearMove( ptP) == GDB_ID_NULL) return false ; } else { // affondo diretto al punto iniziale SetFlag( 0) ; if ( AddRapidMove( ptP) == GDB_ID_NULL) return false ; } return true ; } //---------------------------------------------------------------------------- bool Pocketing::AddLinkRetract( const Point3d& ptP, const Vector3d& vtTool, double dSafeZ, double dSafeAggrBottZ, double dElev, double dAppr) { // se sopra uscita c'è spazio per approccio if ( ( dElev + dAppr) > 10 * EPS_SMALL) { // 4 -> movimento di risalita sopra il punto finale SetFeed( GetEndFeed()) ; Point3d ptP4 = ptP + vtTool * ( dElev + dAppr) ; if ( AddLinearMove( ptP4) == GDB_ID_NULL) return false ; } return true ; } //---------------------------------------------------------------------------- bool Pocketing::AddRetract( const Point3d& ptP, const Vector3d& vtTool, double dSafeZ, double dSafeAggrBottZ, double dElev, double dAppr) { // se sopra uscita c'è spazio per sicurezza o approccio double dSafeDist = ( m_bAggrBottom ? dSafeAggrBottZ : dSafeZ) ; if ( dElev + max( dSafeDist, dAppr) > 10 * EPS_SMALL) { if ( dSafeDist < dAppr + 10 * EPS_SMALL) { // 4 -> movimento di risalita sopra il punto finale SetFeed( GetEndFeed()) ; Point3d ptP4 = ptP + vtTool * ( dElev + dAppr) ; if ( AddLinearMove( ptP4) == GDB_ID_NULL) return false ; } else { // 4a -> movimento di risalita appena sopra il punto finale Point3d ptP4a = ptP + vtTool * ( dElev + dAppr) ; if ( dElev + dAppr > EPS_SMALL) { SetFeed( GetEndFeed()) ; if ( AddLinearMove( ptP4a) == GDB_ID_NULL) return false ; } // 4b -> movimento di risalita sopra il punto finale Point3d ptP4b = ptP4a + vtTool * ( dSafeDist - dAppr) ; if ( AddRapidMove( ptP4b) == GDB_ID_NULL) return false ; } } // se con aggregato da sotto if ( m_bAggrBottom) { // aggiuntivo in Z double dAggZ = max( dElev + max( dSafeAggrBottZ, dAppr), 0.) ; // distanza dal bordo del pezzo double dDistBottom ; if ( ! GetDistanceFromRawSide( m_nPhase, ptP, m_vtAggrBottom, dDistBottom)) dDistBottom = 0 ; // post-retract Point3d ptP0 = ptP - Z_AX * dAggZ + m_vtAggrBottom * ( dDistBottom + m_AggrBottom.dEncH + dSafeZ) ; Point3d ptP00 = ptP0 + Z_AX * ( m_AggrBottom.dEncV + m_TParams.m_dLen + dAggZ - dElev) ; if ( AddRapidMove( ptP0, MCH_CL_AGB_OUT) == GDB_ID_NULL) return false ; // se rinvio da sotto che richiede speciale rotazione if ( m_AggrBottom.nType == 1) { Vector3d vtAux = m_vtAggrBottom ; vtAux.Rotate( Z_AX, 90) ; SetAuxDir( vtAux) ; if ( AddRapidMove( ptP00, MCH_CL_AGB_UP) == GDB_ID_NULL) return false ; } } return true ; } //---------------------------------------------------------------------------- bool Pocketing::CalcLeadInStart( const Point3d& ptStart, const Vector3d& vtStart, const Vector3d& vtN, const ICurveComposite* pRCrv, Point3d& ptP1) { // Assegno tipo e parametri int nType = GetLeadInType() ; if ( nType == POCKET_LI_GLIDE && ( pRCrv == nullptr || pRCrv->GetCurveCount() == 0)) nType = POCKET_LI_NONE ; // senso di rotazione da dir tg a dir esterna bool bCcwRot = true ; // Calcolo punto iniziale switch ( nType) { case POCKET_LI_NONE : ptP1 = ptStart ; return true ; case POCKET_LI_GLIDE : { double dLen, dU ; if ( ! pRCrv->GetLength( dLen) || ! pRCrv->GetParamAtLength( dLen - m_Params.m_dLiTang, dU) || ! pRCrv->GetPointD1D2( dU, ICurve::FROM_MINUS, ptP1)) { if ( ! pRCrv->GetStartPoint( ptP1)) return false ; } ptP1 += vtN * ( vtN * ( ptStart - ptP1)) ; return true ; } case POCKET_LI_ZIGZAG : case POCKET_LI_HELIX : ptP1 = ptStart ; return true ; default : return false ; } } //---------------------------------------------------------------------------- bool Pocketing::AddLeadIn( const Point3d& ptP1, const Point3d& ptStart, const Vector3d& vtStart, const Vector3d& vtN, const ICurveComposite* pRCrv, bool bAtLeft, bool bSplitArcs, bool bNoneForced) { // Assegno il tipo int nType = GetLeadInType() ; if ( bNoneForced || ( nType == POCKET_LI_GLIDE && ( pRCrv == nullptr || pRCrv->GetCurveCount() == 0))) nType = POCKET_LI_NONE ; // Eseguo a seconda del tipo switch ( nType) { case POCKET_LI_NONE : { Point3d ptCurr = ptP1 ; GetCurrPos( ptCurr) ; if ( ! AreSamePointApprox( ptCurr, ptStart)) { if ( AddLinearMove( ptStart, MCH_CL_LEADIN) == GDB_ID_NULL) return false ; } return true ; } case POCKET_LI_GLIDE : { // recupero la parte richiesta della curva di ritorno PtrOwner pCrv ; double dLen, dU ; if ( pRCrv->GetLength( dLen) && pRCrv->GetParamAtLength( dLen - m_Params.m_dLiTang, dU)) { double dParS, dParE ; pRCrv->GetDomain( dParS, dParE) ; if ( ! pCrv.Set( CreateCurveComposite()) || ! pCrv->AddCurve( pRCrv->CopyParamRange( dU, dParE))) return false ; } else { if ( ! pCrv.Set( pRCrv->Clone())) return false ; } pCrv->SetExtrusion( vtN) ; // la porto alla giusta quota Point3d ptFin ; pCrv->GetEndPoint( ptFin) ; Vector3d vtMove = ptStart - ptFin ; pCrv->Translate( vtMove) ; // assegno la corretta pendenza double dNini = ( ptP1 - ORIG) * vtN ; double dNfin = ( ptStart - ORIG) * vtN ; AdjustCurveSlope( pCrv, dNini, dNfin) ; // eventuale spezzatura if ( bSplitArcs && ! ApproxWithLines( pCrv)) return false ; // emetto return ( AddCurveMove( pCrv) != GDB_ID_NULL) ; } case POCKET_LI_ZIGZAG : { double dDeltaN = ( ptStart - ptP1) * vtN ; int nStep = int( ceil( - dDeltaN / ( m_Params.m_dLiElev + 10 * EPS_SMALL))) ; double dStep = - dDeltaN / nStep ; Point3d ptPa = ptP1 + vtStart * 0.5 * min( m_Params.m_dLiTang, m_TParams.m_dDiam) ; Point3d ptPb = ptP1 - vtStart * 0.5 * min( m_Params.m_dLiTang, m_TParams.m_dDiam) ; for ( int i = 1 ; i <= nStep ; ++ i) { if ( AddLinearMove( ptPa - vtN * ( i - 0.75) * dStep, MCH_CL_LEADIN) == GDB_ID_NULL) return false ; if ( AddLinearMove( ptPb - vtN * ( i - 0.25) * dStep, MCH_CL_LEADIN) == GDB_ID_NULL) return false ; } return ( AddLinearMove( ptStart, MCH_CL_LEADIN) != GDB_ID_NULL) ; } case POCKET_LI_HELIX : { // vettore dal punto al centro elica Vector3d vtCen = vtStart ; vtCen.Rotate( vtN, ( bAtLeft ? ANG_RIGHT : - ANG_RIGHT)) ; // dati dell'elica double dRad = min( 0.5 * min( m_Params.m_dLiTang, m_TParams.m_dDiam), m_dMaxHelixRad) ; Point3d ptCen = ptP1 + vtCen * dRad ; double dDeltaN = ( ptStart - ptP1) * vtN ; double dAngCen = ceil( - dDeltaN / ( m_Params.m_dLiElev + 10 * EPS_SMALL)) * ( bAtLeft ? ANG_FULL : - ANG_FULL) ; // creo l'elica PtrOwner pArc( CreateCurveArc()) ; if ( IsNull( pArc) || ! pArc->Set( ptCen, vtN, dRad, - vtCen, dAngCen, dDeltaN)) return false ; // eventuale spezzatura if ( bSplitArcs) { PtrOwner pCompo( CreateCurveComposite()) ; if ( IsNull( pCompo) || ! pCompo->AddCurve( Release( pArc)) || ! ApproxWithLines( pCompo)) return false ; return ( AddCurveMove( pCompo, MCH_CL_LEADIN) != GDB_ID_NULL) ; } else { // emetto l'elica return ( AddCurveMove( pArc, MCH_CL_LEADIN) != GDB_ID_NULL) ; } } default : return false ; } } //---------------------------------------------------------------------------- bool Pocketing::AddLeadOut( const Point3d& ptEnd, const Vector3d& vtEnd, const Vector3d& vtN, const ICurveComposite* pRCrv, bool bSplitArcs, Point3d& ptP1, double& dElev, bool bNoneForced) { // assegno i parametri int nType = GetLeadOutType() ; if ( bNoneForced || ( nType == POCKET_LO_GLIDE && ( pRCrv == nullptr || pRCrv->GetCurveCount() == 0))) nType = POCKET_LO_NONE ; // eseguo a seconda del tipo switch ( nType) { case POCKET_LO_NONE : // nessuna uscita ptP1 = ptEnd ; // determino elevazione su fine uscita double dEndElev ; if ( GetElevation( m_nPhase, ptEnd - 10 * EPS_SMALL * vtN, vtN, GetRadiusForStartEndElevation(), vtN, dEndElev)) dElev = dEndElev ; return true ; case POCKET_LO_GLIDE : { // recupero la parte richiesta della curva di ritorno PtrOwner pCrv ; double dU ; if ( pRCrv->GetParamAtLength( m_Params.m_dLoTang, dU)) { if ( ! pCrv.Set( CreateCurveComposite()) || ! pCrv->AddCurve( pRCrv->CopyParamRange( 0, dU))) return false ; } else { if ( ! pCrv.Set( pRCrv->Clone())) return false ; } // la porto alla giusta quota Point3d ptIni ; pCrv->GetStartPoint( ptIni) ; Vector3d vtMove = ptEnd - ptIni ; pCrv->Translate( vtMove) ; Point3d ptFin ; pCrv->GetEndPoint( ptFin) ; ptFin += vtN * 1.0 ; pCrv->ModifyEnd( ptFin) ; // eventuale spezzatura if ( bSplitArcs && ! ApproxWithLines( pCrv)) return false ; // emetto AddCurveMove( pCrv) ; // determino elevazione su fine uscita ptP1 = ptFin ; double dEndElev ; if ( GetElevation( m_nPhase, ptP1 - 10 * EPS_SMALL * vtN, vtN, GetRadiusForStartEndElevation(), vtN, dEndElev)) dElev = dEndElev ; return true ; } default : return false ; } } //---------------------------------------------------------------------------- double Pocketing::GetRadiusForStartEndElevation( void) { const double DELTA_ELEV_RAD = 20.0 ; return ( 0.5 * m_TParams.m_dTDiam + DELTA_ELEV_RAD) ; } //---------------------------------------------------------------------------- bool Pocketing::AdjustContourStart( ICurveComposite* pCompo) { // cerco il tratto più lungo int i = 0 ; int nMax = - 1 ; double dLenMax = 0 ; const ICurve* pCrv = pCompo->GetFirstCurve() ; while ( pCrv != nullptr) { double dLen ; if ( pCrv->GetLength( dLen) && dLen > dLenMax) { dLenMax = dLen ; nMax = i ; } ++ i ; pCrv = pCompo->GetNextCurve() ; } // sposto inizio if ( nMax >= 0) pCompo->ChangeStartPoint( nMax + 0.5) ; return true ; }