//---------------------------------------------------------------------------- // EgalTech 2017-2022 //---------------------------------------------------------------------------- // File : Pocketing.cpp Data : 24.08.22 Versione : 2.4h2 // Contenuto : Implementazione gestione svuotature. // // // // Modifiche : 04.02.17 DS Creazione modulo. // 24.02.22 DS Corretta ed estesa VerifyPathFromBottom. // // //---------------------------------------------------------------------------- //--------------------------- Include ---------------------------------------- #include "stdafx.h" #include "MachMgr.h" #include "DllMain.h" #include "Pocketing.h" #include "OperationConst.h" #include "MachiningConst.h" #include "GeoConst.h" #include "/EgtDev/Include/EGkCurveLine.h" #include "/EgtDev/Include/EGkCurveArc.h" #include "/EgtDev/Include/EGkBiArcs.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/EGkCurveLocal.h" #include "/EgtDev/Include/EGkDistPointCurve.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" #include "/EgtDev/Include/EGkLinePntMinDistCurve.h" #include "/EgtDev/Include/EGkDistPointCurve.h" #include "/EgtDev/Include/EGkMedialAxis.h" #include "/EgtDev/Include/EGkFilletChamfer.h" #include "/EgtDev/Include/EGkCurveBezier.h" #include "/EgtDev/Include/EGkGeoPoint3d.h" #include "/EgtDev/Include/EGkIntersCurves.h" #include "/EgtDev/Include/EGkLinePntTgCurve.h" #include "/EgtDev/Include/EGkSfrCreate.h" #include 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 Not Perpendicular to Flat Area" // 2405 = "Error in Pocketing : Empty RawBox" // 2406 = "Error in Pocketing : Depth not computable" // 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" // 2428 = "Error in Pocketing : Tool loading failed" // 2429 = "Error in Pocketing : machining depth (xxx) bigger than MaxDepth (yyy)" // 2430 = "Error in Pocketing : adjust open edges failed" // 2431 = "Error in Pocketing : LeadIn with Mill NoTip in material" // 2451 = "Warning in Pocketing : Skipped entity (xx)" // 2452 = "Warning in Pocketing : No machinable pocket" // 2453 = "Warning in Pocketing : Tool name changed (xx)" // 2454 = "Warning in Pocketing : Tool data changed (xx)" // 2455 = "Warning in Pocketing : skipped Path too short" // 2456 = "Warning in Pocketing : machining step too small (xx)" // 2457 = "Warning in Pocketing : machining step (xxx) bigger than MaxMaterial (yyy)" // 2458 = "Warning in Pocketing : machining depth (xxx) bigger than MaxMaterial (yyy)" //---------------------------------------------------------------------------- static string KEY_OPEN = "OPEN" ; static int LINK_CURVE_PROP = -3 ; bool GetClosestOffsetToAPoint( ICRVCOMPOPOVECTOR& vCurves, Point3d& ptFocus, Point3d& ptCL, ICurveComposite* pCrv1, ICurveComposite* pCrv2, int& nIndex, bool bFirst = true) ; bool GetClosestLinkToAPoint( ICURVEPOVECTOR& vCurves, ICRVCOMPOPOVECTOR& vOffs, Point3d& ptFocus, Point3d& ptCL, ICurveComposite* pCrv1, ICurveComposite* pCrv2, bool& bfound, int& nIndex) ; bool RemoveFirstLoopFromSrfFlatRgn( ISurfFlatRegion* pSrfOrig, ISurfFlatRegion* pSrfRes) ; bool RemoveSmallParts( ICURVEPOVECTOR& vCurve, double dToll) ; //---------------------------------------------------------------------------- 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_vId = m_vId ; pPock->m_pMchMgr = m_pMchMgr ; pPock->m_nPhase = m_nPhase ; pPock->m_Params = m_Params ; pPock->m_TParams = m_TParams ; pPock->m_dTHoldBase = m_dTHoldBase ; pPock->m_dTHoldLen = m_dTHoldLen ; pPock->m_dTHoldDiam = m_dTHoldDiam ; pPock->m_nStatus = m_nStatus ; pPock->m_nPockets = m_nPockets ; pPock->m_bTiltingTab = m_bTiltingTab ; pPock->m_bAboveHead = m_bAboveHead ; pPock->m_bAggrBottom = m_bAggrBottom ; pPock->m_bOpenOutRaw = m_bOpenOutRaw ; } 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 ; sOut += KEY_STAT + EQUAL + ToString( m_nStatus) + szNewLine ; return true ; } //---------------------------------------------------------------------------- bool Pocketing::Save( int nBaseId, STRVECTOR& vString) const { try { int nSize = 1 + m_Params.GetSize() + m_TParams.GetSize() + 3 ; 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) { string sParam = m_Params.ToString( i) ; if ( ! sParam.empty()) vString[++k] = sParam ; } 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 ; if ( ! SetVal( KEY_STAT, m_nStatus, vString[++k])) return false ; vString.resize( k + 1) ; } 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 ; } else if ( sKey == KEY_STAT) { if ( ! FromString( sVal, m_nStatus)) return false ; } } return true ; } //---------------------------------------------------------------------------- //---------------------------------------------------------------------------- Pocketing::Pocketing( void) { m_Params.m_sName = "*" ; m_Params.m_sToolName = "*" ; m_TParams.m_sName = "*" ; m_TParams.m_sHead = "*" ; m_dTHoldBase = 0 ; m_dTHoldLen = 0 ; m_dTHoldDiam = 0 ; m_dMaxHelixRad = INFINITO ; m_nStatus = MCH_ST_TO_VERIFY ; m_nPockets = 0 ; m_bTiltingTab = false ; m_bAboveHead = true ; m_bAggrBottom = false ; m_bOpenOutRaw = false ; } //---------------------------------------------------------------------------- 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 : if ( bVal != m_Params.m_bInvert) m_nStatus |= MCH_ST_PARAM_MODIF ; m_Params.m_bInvert = bVal ; return true ; case MPA_TOOLINVERT : if ( bVal != m_Params.m_bToolInvert) m_nStatus |= MCH_ST_PARAM_MODIF ; 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 ; if ( nVal != m_Params.m_nLeadInType) m_nStatus |= MCH_ST_PARAM_MODIF ; m_Params.m_nLeadInType = nVal ; return true ; case MPA_LEADOUTTYPE : if ( ! m_Params.VerifyLeadOutType( nVal)) return false ; if ( nVal != m_Params.m_nLeadOutType) m_nStatus |= MCH_ST_PARAM_MODIF ; m_Params.m_nLeadOutType = nVal ; return true ; case MPA_SCC : if ( ! m_Params.VerifySolCh( nVal)) return false ; if ( nVal != m_Params.m_nSolCh) m_nStatus |= MCH_ST_PARAM_MODIF ; m_Params.m_nSolCh = nVal ; return true ; case MPA_SUBTYPE : if ( ! m_Params.VerifySubType( nVal)) return false ; if ( nVal != m_Params.m_nSubType) m_nStatus |= MCH_ST_PARAM_MODIF ; 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) dVal = 0 ; if ( abs( dVal - m_Params.m_dSpeed) > EPS_MACH_ANG_PAR) m_nStatus |= MCH_ST_PARAM_MODIF ; m_Params.m_dSpeed = dVal ; return true ; case MPA_FEED : if ( abs( m_TParams.m_dFeed - dVal) < EPS_MACH_LEN_PAR) dVal = 0 ; if ( abs( dVal - m_Params.m_dFeed) > EPS_MACH_LEN_PAR) m_nStatus |= MCH_ST_PARAM_MODIF ; m_Params.m_dFeed = dVal ; return true ; case MPA_STARTFEED : if ( abs( m_TParams.m_dStartFeed - dVal) < EPS_MACH_LEN_PAR) dVal = 0 ; if ( abs( dVal - m_Params.m_dStartFeed) > EPS_MACH_LEN_PAR) m_nStatus |= MCH_ST_PARAM_MODIF ; m_Params.m_dStartFeed = dVal ; return true ; case MPA_ENDFEED : if ( abs( m_TParams.m_dEndFeed - dVal) < EPS_MACH_LEN_PAR) dVal = 0 ; if ( abs( dVal - m_Params.m_dEndFeed) > EPS_MACH_LEN_PAR) m_nStatus |= MCH_ST_PARAM_MODIF ; m_Params.m_dEndFeed = dVal ; return true ; case MPA_TIPFEED : if ( abs( m_TParams.m_dTipFeed - dVal) < EPS_MACH_LEN_PAR) dVal = 0 ; if ( abs( dVal - m_Params.m_dTipFeed) > EPS_MACH_LEN_PAR) m_nStatus |= MCH_ST_PARAM_MODIF ; m_Params.m_dTipFeed = dVal ; return true ; case MPA_OFFSR : if ( abs( m_TParams.m_dOffsR - dVal) < EPS_MACH_LEN_PAR) dVal = UNKNOWN_PAR ; if ( abs( dVal - m_Params.m_dOffsR) > EPS_MACH_LEN_PAR) m_nStatus |= MCH_ST_PARAM_MODIF ; m_Params.m_dOffsR = dVal ; return true ; case MPA_OFFSL : if ( abs( m_TParams.m_dOffsL - dVal) < EPS_MACH_LEN_PAR) dVal = UNKNOWN_PAR ; if ( abs( dVal - m_Params.m_dOffsL) > EPS_MACH_LEN_PAR) m_nStatus |= MCH_ST_PARAM_MODIF ; m_Params.m_dOffsL = dVal ; return true ; case MPA_DEPTH : { string sVal = ToString( dVal) ; if ( sVal != m_Params.m_sDepth) m_nStatus |= MCH_ST_PARAM_MODIF ; m_Params.m_sDepth = sVal ; } return true ; case MPA_STARTPOS : if ( abs( dVal - m_Params.m_dStartPos) > EPS_MACH_LEN_PAR) m_nStatus |= MCH_ST_PARAM_MODIF ; m_Params.m_dStartPos = dVal ; return true ; case MPA_STEP : if ( abs( dVal - m_Params.m_dStep) > EPS_MACH_LEN_PAR) m_nStatus |= MCH_ST_PARAM_MODIF ; m_Params.m_dStep = dVal ; return true ; case MPA_SIDESTEP : if ( abs( dVal - m_Params.m_dSideStep) > EPS_MACH_LEN_PAR) m_nStatus |= MCH_ST_PARAM_MODIF ; m_Params.m_dSideStep = dVal ; return true ; case MPA_SIDEANGLE : if ( abs( dVal - m_Params.m_dSideAngle) > EPS_MACH_LEN_PAR) m_nStatus |= MCH_ST_PARAM_MODIF ; m_Params.m_dSideAngle = dVal ; return true ; case MPA_LITANG : if ( abs( dVal - m_Params.m_dLiTang) > EPS_MACH_LEN_PAR) m_nStatus |= MCH_ST_PARAM_MODIF ; m_Params.m_dLiTang = dVal ; return true ; case MPA_LIELEV : if ( abs( dVal - m_Params.m_dLiElev) > EPS_MACH_LEN_PAR) m_nStatus |= MCH_ST_PARAM_MODIF ; m_Params.m_dLiElev = dVal ; return true ; case MPA_LOTANG : if ( abs( dVal - m_Params.m_dLoTang) > EPS_MACH_LEN_PAR) m_nStatus |= MCH_ST_PARAM_MODIF ; m_Params.m_dLoTang = dVal ; return true ; case MPA_EPICYCLESRAD : if ( abs( dVal - m_Params.m_dEpicyclesRad) > EPS_MACH_LEN_PAR) m_nStatus |= MCH_ST_PARAM_MODIF ; m_Params.m_dEpicyclesRad = dVal ; return true ; case MPA_EPICYCLESDIST : if ( abs( dVal - m_Params.m_dEpicyclesDist) > EPS_MACH_LEN_PAR) m_nStatus |= MCH_ST_PARAM_MODIF ; m_Params.m_dEpicyclesDist = 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 ; if ( ! SameTool( m_TParams, *pTdata)) m_nStatus |= MCH_ST_PARAM_MODIF ; m_Params.m_sToolName = sVal ; m_Params.m_ToolUuid = pTdata->m_Uuid ; m_TParams = *pTdata ; } return true ; case MPA_DEPTH_STR : if ( sVal != m_Params.m_sDepth) m_nStatus |= MCH_ST_PARAM_MODIF ; m_Params.m_sDepth = sVal ; return true ; case MPA_SYSNOTES : if ( sVal != m_Params.m_sSysNotes) m_nStatus |= MCH_ST_PARAM_MODIF ; m_Params.m_sSysNotes = sVal ; return true ; case MPA_USERNOTES : if ( sVal != m_Params.m_sUserNotes) m_nStatus |= MCH_ST_PARAM_MODIF ; m_Params.m_sUserNotes = sVal ; return true ; case MPA_INITANGS : if ( sVal != m_Params.m_sInitAngs) m_nStatus |= MCH_ST_PARAM_MODIF ; m_Params.m_sInitAngs = sVal ; return true ; case MPA_BLOCKEDAXIS : if ( sVal != m_Params.m_sBlockedAxis) m_nStatus |= MCH_ST_PARAM_MODIF ; m_Params.m_sBlockedAxis = sVal ; return true ; } return false ; } //---------------------------------------------------------------------------- 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 sInfo = "Warning in Pocketing : Skipped entity " + ToString( Id) ; m_pMchMgr->SetWarning( 2451, sInfo) ; continue ; } // posso aggiungere alla lista m_vId.emplace_back( Id) ; } // aggiorno lo stato m_nStatus |= MCH_ST_GEO_MODIF ; // restituisco presenza geometria da lavorare 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 ; } // rendo corrente l'utensile usato nella lavorazione if ( ! m_pMchMgr->SetCalcTool( m_TParams.m_sName, m_TParams.m_sHead, m_TParams.m_nExit)) { m_pMchMgr->SetLastError( 2428, "Error in Pocketing : Tool loading failed") ; return false ; } // recupero i dati del portautensile int nToolId = m_pMchMgr->GetCalcTool() ; m_dTHoldBase = 0 ; m_pGeomDB->GetInfo( nToolId, TTH_BASE, m_dTHoldBase) ; m_dTHoldLen = 0 ; m_pGeomDB->GetInfo( nToolId, TTH_LEN, m_dTHoldLen) ; m_dTHoldDiam = 0 ; m_pGeomDB->GetInfo( nToolId, TTH_DIAM, m_dTHoldDiam) ; // 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 int nCurrPockets = m_nPockets ; 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 ; // aggiorno dati geometrici dell'utensile bool bToolChanged = true ; if ( ! UpdateToolData( &bToolChanged)) { m_pMchMgr->SetLastError( 2401, "Error in Pocketing : UpdateToolData failed") ; return false ; } // verifico se necessario continuare nell'aggiornamento if ( !bRecalc && !bToolChanged && ( m_nStatus == MCH_ST_OK || ( !bPostApply && m_nStatus == MCH_ST_NO_POSTAPPL))) { // confermo i percorsi di lavorazione m_nPockets = nCurrPockets ; LOG_DBG_INFO( GetEMkLogger(), "Pocketing apply skipped : status already ok") ; // eseguo aggiornamento assi macchina e collegamento con operazione precedente if ( ! Update( bPostApply)) return false ; LOG_DBG_INFO( GetEMkLogger(), "Update done") ; // esco con successo return true ; } m_nStatus = MCH_ST_TO_VERIFY ; // 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 ; } // rendo corrente l'utensile usato nella lavorazione if ( ! m_pMchMgr->SetCalcTool( m_TParams.m_sName, m_TParams.m_sHead, m_TParams.m_nExit)) { m_pMchMgr->SetLastError( 2428, "Error in Pocketing : Tool loading failed") ; return false ; } // recupero i dati del portautensile int nToolId = m_pMchMgr->GetCalcTool() ; m_dTHoldBase = 0 ; m_pGeomDB->GetInfo( nToolId, TTH_BASE, m_dTHoldBase) ; m_dTHoldLen = 0 ; m_pGeomDB->GetInfo( nToolId, TTH_LEN, m_dTHoldLen) ; m_dTHoldDiam = 0 ; m_pGeomDB->GetInfo( nToolId, TTH_DIAM, m_dTHoldDiam) ; // 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( nAuxId, GDB_ID_NULL, nClId)) //nPathId 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 ; // aggiorno stato della lavorazione m_nStatus = ( bPostApply ? MCH_ST_OK : MCH_ST_NO_POSTAPPL) ; // dichiaro successiva da aggiornare UpdateFollowingOperationsStatus( MCH_ST_OTH_MODIF) ; LOG_DBG_INFO( GetEMkLogger(), "Pocketing apply done") ; 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) { m_pMchMgr->SetWarning( 2452, "Warning in Pocketing : No machinable pocket") ; return true ; } // imposto eventuale asse bloccato da lavorazione SetBlockedRotAxis( m_Params.m_sBlockedAxis) ; // 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 ; } // assegno estremi degli assi dei vari percorsi di lavorazione e della lavorazione nel suo complesso CalcAndSetAxesBBox() ; // esecuzione eventuali personalizzazioni string sErr ; if ( bPostApply && ! PostApply( sErr)) { if ( ! IsEmptyOrSpaces( sErr)) m_pMchMgr->SetLastError( 2427, sErr) ; else 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 ; case MPA_EPICYCLESRAD : dVal = m_Params.m_dEpicyclesRad ; return true ; case MPA_EPICYCLESDIST : dVal = m_Params.m_dEpicyclesDist ; 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 sInfo = "Warning in Pocketing : tool name changed (" + m_Params.m_sToolName + "->" + m_TParams.m_sName + ")" ; m_pMchMgr->SetWarning( 2453, sInfo) ; m_Params.m_sToolName = m_TParams.m_sName ; } if ( bChanged) { string sInfo = "Warning in Pocketing : tool data changed (" + m_Params.m_sToolName + ")" ; m_pMchMgr->SetWarning( 2454, sInfo) ; } // 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 : curve, testi, facce di trimesh o regioni 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) return false ; if ( pCurve->GetType() == CRV_COMPO) nSubs = ::GetCurveComposite( pCurve)->GetCurveCount() ; else nSubs = 0 ; } // altrimenti sottocurva di composita else { const ICurveComposite* pCompo = GetCurveComposite( pGObj) ; pCurve = ( pCompo != nullptr ? pCompo->GetCurve( Id.nSub) : nullptr) ; if ( pCurve == nullptr) return false ; nSubs = 0 ; } return true ; } // 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 ; 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) { // deve avere una sola faccia if ( pSurf->GetFacetCount() != 1) return false ; nSubs = 1 ; } // altrimenti faccia di superficie trimesh else { // se faccia non esistente if ( Id.nSub >= pSurf->GetFacetCount()) return false ; nSubs = 0 ; } return true ; } // se altrimenti ammesse regioni ed è tale else if ( ( nType == GEO_NONE || nType == SRF_FLATRGN) && pGObj->GetType() == SRF_FLATRGN) { nType = SRF_FLATRGN ; const ISurfFlatRegion* pReg = ::GetSurfFlatRegion( pGObj) ; if ( pReg == nullptr) return false ; // se direttamente la regione if ( Id.nSub == SEL_SUB_ALL) { nSubs = pReg->GetChunkCount() ; } // altrimenti chunk di regione else { // se chunk non esistente if ( Id.nSub >= pReg->GetChunkCount()) return false ; // tutto bene nSubs = 0 ; } return true ; } // altrimenti errore else return false ; } //---------------------------------------------------------------------------- bool Pocketing::GetCurves( SelData Id, ICURVEPLIST& lstPC) { // ammessi : curve, testi, facce di trimesh o regioni 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()) ; // recupero eventuali informazioni per lati aperti SetCurveAllTempProp( Id.nId, pCurve) ; // 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()) ; // reset proprietà temporanee ResetCurveAllTempProp( pCurve) ; // 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 ; // reset proprietà temporanee for ( auto pCrv : lstPC) ResetCurveAllTempProp( pCrv) ; // porto le curve in globale for ( auto pCrv : lstPC) pCrv->ToGlob( frGlob) ; // ritorno return true ; } // se altrimenti superficie else if ( pGObj->GetType() == SRF_TRIMESH) { // 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 ; // recupero la normale esterna della faccia Vector3d vtN ; if ( ! pSurf->GetFacetNormal( nFacet, vtN)) return false ; // creo la curva a partire dei loop for ( int i = 0 ; i < int( vPL.size()) ; i++) { PtrOwner pCrvCompo( CreateCurveComposite()) ; pCrvCompo->FromPolyLine( vPL[i]) ; if ( ! pCrvCompo->IsValid()) return false ; // reset delle proprietà temporanee ResetCurveAllTempProp( pCrvCompo) ; // determino eventuali lati aperti e aggiorno proprietà del contorno int nInd = 0 ; double dPar ; bool bFound = vPL[i].GetFirstU( dPar, true) ; while ( bFound) { // recupero il flag int nFlag = int( dPar) ; // se non c'è nulla di adiacente, lato aperto if (nFlag == SVT_NULL) pCrvCompo->SetCurveTempProp( nInd, 1) ; // altrimenti verifico se la faccia adiacente forma diedro convesso o concavo else { bool bAdjac ; Point3d ptP1, ptP2 ; double dAng ; if ( ! pSurf->GetFacetsContact( nFacet, nFlag, bAdjac, ptP1, ptP2, dAng)) dAng = - ANG_RIGHT ; if ( dAng > - EPS_ANG_SMALL) pCrvCompo->SetCurveTempProp( nInd, 1) ; } // passo al successivo ++nInd ; bFound = vPL[i].GetNextU( dPar, true) ; } // 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 ; /* // creo la curva a partire da quello esterno PtrOwner pCrvCompo( CreateCurveComposite()) ; pCrvCompo->FromPolyLine( vPL[0]) ; if ( ! pCrvCompo->IsValid()) return false ; // reset proprietà temporanee ResetCurveAllTempProp( pCrvCompo) ; // determino eventuali lati aperti e aggiorno proprietà del contorno int nInd = 0 ; double dPar ; bool bFound = vPL[0].GetFirstU( dPar, true) ; while ( bFound) { // recupero il flag int nFlag = int( dPar) ; // se non c'è nulla di adiacente, lato aperto if ( nFlag == SVT_NULL) pCrvCompo->SetCurveTempProp( nInd, 1) ; // altrimenti verifico se la faccia adiacente forma diedro convesso o concavo else { bool bAdjac ; Point3d ptP1, ptP2 ; double dAng ; if ( ! pSurf->GetFacetsContact( nFacet, nFlag, bAdjac, ptP1, ptP2, dAng)) dAng = - ANG_RIGHT ; if ( dAng > - EPS_ANG_SMALL) pCrvCompo->SetCurveTempProp( nInd, 1) ; } // passo al successivo ++ nInd ; bFound = vPL[0].GetNextU( dPar, true) ; } // 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 ; */ } // se altrimenti regione else if ( pGObj->GetType() == SRF_FLATRGN) { // recupero la regione const ISurfFlatRegion* pReg = ::GetSurfFlatRegion( pGObj) ; if ( pReg == nullptr) return false ; // recupero la normale della regione Vector3d vtN = pReg->GetNormVersor() ; if ( vtN.IsSmall()) return false ; // determino intervallo di chunk int nCstart = 0 ; int nCend = pReg->GetChunkCount() ; if ( Id.nSub != SEL_SUB_ALL) { nCstart = Id.nSub ; nCend = nCstart + 1 ; } // ciclo sui chunk for ( int nC = nCstart ; nC < nCend ; ++ nC) { // recupero i contorni del chunk for ( int nL = 0 ; nL < pReg->GetLoopCount( nC) ; ++ nL) { PtrOwner pCrvCompo( CreateCurveComposite()) ; if ( IsNull( pCrvCompo) || ! pCrvCompo->AddCurve( pReg->GetLoop( nC, nL))) return false ; // reset proprietà temporanee ResetCurveAllTempProp( pCrvCompo) ; // assegno l'estrusione dalla normale alla regione 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::SetCurveAllTempProp( int nCrvId, ICurve* pCurve) { if ( pCurve == nullptr) return false ; // reset proprietà temporanee ResetCurveAllTempProp( pCurve) ; // verifico se presenti info per lati aperti if ( ! m_pGeomDB->ExistsInfo( nCrvId, KEY_OPEN)) return true ; // recupero info sui lati aperti INTVECTOR vOpen ; m_pGeomDB->GetInfo( nCrvId, KEY_OPEN, vOpen) ; // se curva composita ICurveComposite* pCC = GetCurveComposite( pCurve) ; if ( pCC != nullptr) { for ( int j : vOpen) pCC->SetCurveTempProp( j, 1) ; } // altrimenti else { if ( ! vOpen.empty() && vOpen[0] == 0) pCurve->SetTempProp( 1) ; } return true ; } //---------------------------------------------------------------------------- bool Pocketing::ResetCurveAllTempProp( ICurve* pCurve) { if ( pCurve == nullptr) return false ; pCurve->SetTempProp( 0) ; ICurveComposite* pCC = GetCurveComposite( pCurve) ; if ( pCC != nullptr) { for ( int i = 0 ; i < pCC->GetCurveCount() ; ++ i) pCC->SetCurveTempProp( i, 0) ; } return true ; } //---------------------------------------------------------------------------- bool Pocketing::Chain( int nGrpDestId) //AUX { // vettore puntatori alle curve ICURVEPOVECTOR vpCrvs ; vpCrvs.reserve( m_vId.size()) ; // OGGETTI CLICCATI PRIMA DELL'APPLY // 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 sInfo = "Warning in Pocketing : Skipped entity " + ToString( Id) ; m_pMchMgr->SetWarning( 2451, sInfo) ; } 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) && abs( dTemp) > abs( 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, true)) return false ; // recupero eventuali lati aperti INTVECTOR vOpen ; for ( int i = 0 ; i < int( pCrvCompo->GetCurveCount()) ; ++ i) { int nProp = 0 ; if ( pCrvCompo->GetCurveTempProp( i, nProp) && nProp == 1) vOpen.emplace_back( i) ; } // 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 ; // salvo info con eventuali lati aperti if ( ! vOpen.empty()) m_pGeomDB->SetInfo( nNewId, KEY_OPEN, vOpen) ; } return true ; } //---------------------------------------------------------------------------- bool Pocketing::ProcessPath(int nAuxId, int nPvId, int nClId) { // ========= GRUPPO TMP =================== // recupero gruppo per geometria temporanea ( Gruppo TMP ) 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) ; // ======================================= // ========= STEP ======================== // verifico che lo step dell'utensile sia sensato double dOkStep = ( m_Params.m_dStep > EPS_SMALL ? m_Params.m_dStep + EPS_SMALL : 0); const double MIN_ZSTEP = 1.0 ; if ( dOkStep >= EPS_SMALL && dOkStep < MIN_ZSTEP) { dOkStep = MIN_ZSTEP + EPS_SMALL ; string sInfo = "Warning in Pocketing : machining step too small (" + ToString(m_Params.m_dStep, 2) + ")" ; m_pMchMgr->SetWarning( 2456, sInfo) ; } // verifico di non superare il massimo materiale if ( m_Params.m_dStep > m_TParams.m_dMaxMat + EPS_SMALL) { dOkStep = m_TParams.m_dMaxMat + EPS_SMALL ; string sInfo = "Warning in Pocketing : machining step (" + ToString(m_Params.m_dStep, 1) + ") bigger than MaxMaterial (" + ToString(m_TParams.m_dMaxMat, 1) + ")" ; m_pMchMgr->SetWarning( 2457, sInfo) ; } // ======================================= // recupero eventuale flag di lato aperto forzato fuori dal grezzo int nOpenOutRaw ; m_bOpenOutRaw = ( FromString( ExtractInfo(m_Params.m_sUserNotes, "OpenOutRaw="), nOpenOutRaw) && nOpenOutRaw != 0) ; SurfFlatRegionByContours SrfByC ; // superificie totale generata da tutte le curve concatenate Plane3d plAux ; bool bFirstPlane = true ; // piano Ausiliario per verificare che le curve siano complanari Vector3d vtExtr ; double dThick = -INFINITO ; // spessore più grande tra le curve int nPathId = m_pGeomDB->GetFirstGroupInGroup( nAuxId) ; while ( nPathId != GDB_ID_NULL) { // Scorro tutte le curve concatenate per creare la superficie da lavorare int nPi = m_pGeomDB->GetFirstInGroup( nPathId) ; const IGeoObj* pGObj = m_pGeomDB->GetGeoObj( nPi) ; if ( pGObj == nullptr) return false ; const ICurve* pOriCurve = ::GetCurve( pGObj) ; if ( pOriCurve == nullptr) return false ; PtrOwner pCompoOriCrv( GetCurveComposite( pOriCurve->Clone())) ; if ( IsNull( pCompoOriCrv)) return false ; // unisco le parti allineate (tranne inizio-fine se chiusa) if ( ! pCompoOriCrv->MergeCurves( 10 * EPS_SMALL, 10 * EPS_ANG_SMALL, false, true)) return false ; // controllo se trovo dei Flag per lati aperti bool bSomeOpen = m_pGeomDB->ExistsInfo( nPi, KEY_OPEN) ; if ( bSomeOpen) { int nOpen ; if ( FromString( ExtractInfo( m_Params.m_sUserNotes, "Open="), nOpen) && nOpen == 0) bSomeOpen = false ; } // annullo i flag di tratto aperto for ( int i = 0; i < int( pCompoOriCrv->GetCurveCount()) ; ++i) pCompoOriCrv->SetCurveTempProp( i, 0, 0) ; // alla sottocurva // aggiorno flag per lati aperti if ( bSomeOpen) { INTVECTOR vOpen ; m_pGeomDB->GetInfo( nPi, KEY_OPEN, vOpen) ; for (int j : vOpen) pCompoOriCrv->SetCurveTempProp( j, 1, 0) ; // alla sottocurva } // recupero estrusione e spessore Vector3d vtExtr_p = Z_AX ; pCompoOriCrv->GetExtrusion( vtExtr_p) ; double dThick_p ; pCompoOriCrv->GetThickness( dThick_p) ; // verifico sia piana e se necessario la appiattisco PtrOwner pFlatCrv( FlattenCurve( *pCompoOriCrv, 50 * EPS_SMALL, 50 * EPS_ANG_SMALL, FLTCRV_USE_EXTR)) ; if ( IsNull( pFlatCrv)) { Plane3d plPlane ; if ( ! pCompoOriCrv->IsFlat( plPlane, true, 50 * EPS_SMALL)) m_pMchMgr->SetLastError( 2403, "Error in Pocketing : Contour Not Flat") ; else m_pMchMgr->SetLastError( 2404, "Error in Pocketing : Tool Not Perpendicular to Flat Area") ; return false; } pFlatCrv->GetExtrusion( vtExtr_p) ; pCompoOriCrv->Clear() ; pCompoOriCrv->AddCurve( Release( pFlatCrv)) ; pCompoOriCrv->SetExtrusion( vtExtr_p) ; pCompoOriCrv->SetThickness( dThick_p) ; // setto come proprietà temperanea il suo Id ( per riprenderlo dopo per casi ottimizzati) pCompoOriCrv->SetTempProp( nPi, 0) ; // a tutta la curva // controllo che le curve siano complanari e con stessa estrusione (la prima come riferimento) Plane3d plAct ; pCompoOriCrv->IsFlat( plAct) ; if ( bFirstPlane) { plAux = plAct ; bFirstPlane = false ; pCompoOriCrv->GetThickness( dThick_p) ; dThick = dThick_p ; // prima come riferimento vtExtr = vtExtr_p ; // prima come riferimento SrfByC.AddCurve( pCompoOriCrv->Clone()) ; } else { Vector3d vtN1, vtN2 ; if ( abs( plAct.GetDist() - plAux.GetDist()) < EPS_SMALL && AreSameOrOppositeVectorApprox( plAct.GetVersN(), plAux.GetVersN()) // se stessa normale && AreSameVectorApprox( vtExtr, vtExtr_p)) { // se stessa estrusione SrfByC.AddCurve( pCompoOriCrv->Clone()) ; // prendo Thickness Massima tra tutte le curve dei chunks double dCurrThink ; pCompoOriCrv->GetThickness( dCurrThink) ; if ( dCurrThink > dThick) dThick = dCurrThink ; } } nPathId = m_pGeomDB->GetNextGroup( nPathId) ; // aggiorno il PathId con il successivo nel gruppo } // creazione della superficie da lavorare completa (dall'unione di tutte le curve concatenate) PtrOwner pSrfByC( SrfByC.GetSurf()) ; if ( pSrfByC == nullptr) return false ; // creo la superificie finale ( quella con le curve modificate se contengono dei lati aperti ) PtrOwner pSrfPock( CreateSurfFlatRegion()) ; if ( IsNull( pSrfPock)) return false ; // vettore per i punti iniziali delle curve che definiscono i contorni dei Chunks PNTVECTOR vPtStart( pSrfByC->GetChunkCount()) ; // vettori controllo per lati aperti BOOLVECTOR vbMidOpen( pSrfByC->GetChunkCount()) ; PNTVECTOR vPtMidOpen( vbMidOpen.size()) ; VCT3DVECTOR vVtMidOut( vPtMidOpen.size()) ; for ( int c = 0 ; c < pSrfByC->GetChunkCount() ; c++) { // ciclo sui chunk ottenuti // riferimento della curva esterna del chunk (c)-esimo (bordo esterno) PtrOwner pCrvExt( GetCurveComposite( pSrfByC->GetLoop( c, 0))) ; bool bSomeOpen = m_pGeomDB->ExistsInfo( pCrvExt->GetTempProp( 0), KEY_OPEN) ; // se trovo dei ==| LATI APERTI |== allora modifico il bordo esterno bool bMidOpen = false ; Point3d ptMidOpen ; Vector3d vtMidOut ; if ( bSomeOpen) { // ricerca del punto medio del lato aperto più lungo bMidOpen = GetMidOfLongestOpenSide( pCrvExt, ptMidOpen, vtMidOut) ; // sistemazioni per eventuali lati aperti if ( ! AdjustContourWithOpenEdges( pCrvExt)) { m_pMchMgr->SetLastError( 2430, "Error in Pocketing : adjust open edges failed") ; return false; } } // sposto l'inizio a metà del tratto più lungo ( o aperto più lungo) della curva esterna if ( bMidOpen) { const double LEN_OUT = 5 ; double dPar ; int nFlag ; bMidOpen = ( DistPointCurve( ptMidOpen + LEN_OUT * vtMidOut, *pCrvExt).GetParamAtMinDistPoint( 0, dPar, nFlag) && pCrvExt->ChangeStartPoint( dPar)) ; } if ( ! bMidOpen) AdjustContourStart( pCrvExt) ; // recupero il punto di inizio (per poi salvarlo nelle info di CL path) Point3d ptStart ; pCrvExt->GetStartPoint( ptStart) ; vPtStart[c] = ptStart ; // salvataggio del punto nel vettore // inserisco il Loop esterno if ( ! pSrfPock->AddExtLoop( pCrvExt->Clone())) return false ; // aggiorno i vettori di controllo vbMidOpen.push_back( bMidOpen) ; vPtMidOpen.push_back( ptMidOpen) ; vVtMidOut.push_back( vtMidOut) ; for ( int l = 1 ; l < pSrfByC->GetLoopCount( c) ; l++) { // scorro il loop interni del Chunk ( bordi delle isole ) PtrOwner pCrvInt(( ICurveComposite*)pSrfByC->GetLoop( c, l)) ; if ( IsNull( pCrvInt)) return false ; bool bSomeOpen_i = m_pGeomDB->ExistsInfo( pCrvInt->GetTempProp( 0), KEY_OPEN) ; // se trovo dei ==| LATI APERTI |== allora modifico il bordo esterno bool bMidOpen_i = false ; Point3d ptMidOpen_i ; Vector3d vtMidOut_i ; if ( bSomeOpen_i) { // ricerca del punto medio del lato aperto più lungo bMidOpen_i = GetMidOfLongestOpenSide( pCrvInt, ptMidOpen_i, vtMidOut_i) ; // sistemazioni per eventuali lati aperti if ( ! AdjustContourWithOpenEdges( pCrvInt)) { m_pMchMgr->SetLastError( 2430, "Error in Pocketing : adjust open edges failed") ; return false; } } if ( bMidOpen_i) { const double LEN_OUT = 5 ; double dPar_i; int nFlag_i ; bMidOpen_i = ( DistPointCurve( ptMidOpen_i + LEN_OUT * vtMidOut_i, *pCrvInt).GetParamAtMinDistPoint( 0, dPar_i, nFlag_i) && pCrvInt->ChangeStartPoint( dPar_i)) ; } if ( ! bMidOpen_i) AdjustContourStart( pCrvInt) ; // aggiungo curve interne if ( ! pSrfPock->AddIntLoop( pCrvInt->Clone())) return false ; } } // normale della superifice finale || extrusione prima curva selezionata if ( AreOppositeVectorApprox( vtExtr, pSrfPock->GetNormVersor())) pSrfPock->Invert() ; if ( m_Params.m_bToolInvert) { // eventuale inversione direzione utensile vtExtr.Invert() ; pSrfPock->Invert() ; dThick = -dThick ; } // recupero il box del grezzo in globale BBox3d b3Raw ; BBox3d b3Chunk ; pSrfPock->GetBBox( GLOB_FRM, b3Chunk) ; if ( ! GetRawGlobBox( m_nPhase, b3Chunk, 0.5 * m_TParams.m_dTDiam, 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 ; double dAllRbDist = 0 ; if ( AreSameVectorApprox( vtExtr, Z_AX)) { if ( ! GetDistanceFromRawBottom( m_nPhase, b3Chunk, m_TParams.m_dTDiam, dRbDist, dAllRbDist)) 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() ; // assegno il versore fresa Vector3d vtTool = vtExtr ; m_bAggrBottom = false; // calcolo l'elevazione massima double dElev = -INFINITO ; for ( int c = 0; c < pSrfPock->GetChunkCount() ; c++) { double dCurrElev ; if ( CalcRegionElevation( GetCurveComposite( pSrfPock->GetLoop( c,0)), vtTool, dDepth, 0.5 * m_TParams.m_dDiam, dCurrElev)) { if ( dCurrElev < EPS_SMALL && AreSameVectorApprox( vtExtr, Z_AX)) { BBox3d b3Crv ; pSrfPock->GetLoop( c, 0)->GetLocalBBox( b3Crv) ; dCurrElev = max( 0., b3Raw.GetMax().z - b3Crv.GetMin().z + min(0., dThick) + dDepth) ; } if ( dCurrElev > dElev) dElev = dCurrElev ; } 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 ; } // se lavorazione singola if ( dOkStep < EPS_SMALL || dOkStep > dElev) { // se l'elevazione supera la capacità dell'utensile if ( dElev > m_TParams.m_dMaxMat + EPS_SMALL ) { string sInfo = "Warning in Pocketing : machining depth (" + ToString(dElev, 1) + ") bigger than MaxMaterial (" + ToString(m_TParams.m_dMaxMat, 1) + ")" ; m_pMchMgr->SetWarning(2458, sInfo) ; dDepth -= dElev - m_TParams.m_dMaxMat ; dElev = m_TParams.m_dMaxMat ; } } // altrimenti lavorazione a step else { // se l'elevazione supera il massimo affondamento dell'utensile double dSafe = m_pMchMgr->GetCurrMachiningsMgr()->GetMaxDepthSafe() ; double dMaxDepth = m_TParams.m_dLen - ( m_TParams.m_dDiam > m_dTHoldDiam ? m_dTHoldBase : m_dTHoldLen) - dSafe ; if ( dElev > dMaxDepth + EPS_SMALL) { // segnalo, riduco e continuo string sInfo = "Warning in Pocketing : machining depth (" + ToString( dElev, 1) + ") bigger than MaxDepth (" + ToString(dMaxDepth, 1) + ")" ; m_pMchMgr->SetWarning( 2458, sInfo) ; dDepth -= dElev - dMaxDepth ; dElev = dMaxDepth ; } } // verifico se tavola basculante bool bTiltTab = false ; m_bTiltingTab = ( m_pMchMgr->GetCurrMachine()->GetCurrTableIsTilting( bTiltTab) && bTiltTab) ; // verifico se testa da sopra (Z+) m_bAboveHead = m_pMchMgr->GetHeadAbove( m_TParams.m_sHead) ; // verifiche per svuotature dal basso m_bAggrBottom = false ; if ( ! VerifyPathFromBottom( pSrfPock, vtTool)) { return false; } string sPathName = "P1" ; // 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 ( ! GeneratePocketingPv( nPxId, pSrfPock)) 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) ; // 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, vPtStart[0]); //<-------- m_pGeomDB->SetInfo( nPxId, KEY_END, vPtStart[0]); //<-------- // assegno l'elevazione massima m_pGeomDB->SetInfo( nPxId, KEY_ELEV, dElev) ; // Imposto dati comuni SetPathId( nPxId) ; SetToolDir( vtTool) ; // Eseguo la lavorazione del chunk (c)-esimo modificato per lati aperti a seconda del tipo di lavorazione richiesto switch ( m_Params.m_nSubType) { case POCKET_SUB_ZIGZAG: if ( ! AddZigZag( pSrfPock, vtTool, vtExtr, dDepth, dElev, dOkStep, bSplitArcs, nPathId)) return false ; break ; case POCKET_SUB_ONEWAY: if ( ! AddOneWay( pSrfPock, vtTool, vtExtr, dDepth, dElev, dOkStep, bSplitArcs)) return false ; break ; case POCKET_SUB_SPIRALIN: if ( ! AddSpiralIn( pSrfPock, vPtStart, vtTool, vtExtr, dDepth, dElev, dOkStep, bSplitArcs, vbMidOpen, vPtMidOpen, vVtMidOut, nPathId)) return false ; break ; case POCKET_SUB_SPIRALOUT: if ( ! AddSpiralOut( pSrfPock, vPtStart, vtTool, vtExtr, dDepth, dElev, dOkStep, bSplitArcs, nPathId)) return false ; break ; } } m_nPockets++ ; return true ; } //---------------------------------------------------------------------------- bool Pocketing::CalcRegionElevation( const ICurveComposite* pCompo, const Vector3d& vtTool, double dDepth, double dRad, double& dElev) const { // inizializzo l'elevazione dElev = 0 ; // affondamento come vettore Vector3d vtDepth = vtTool * dDepth ; // Campiono il contorno int nMaxInd = pCompo->GetCurveCount() - 1 ; for ( int i = 0 ; i <= nMaxInd ; ++ i) { // curva corrente const ICurve* pCrvC = pCompo->GetCurve( i) ; Point3d ptStart ; pCrvC->GetStartPoint( ptStart) ; Point3d ptMid ; pCrvC->GetMidPoint( ptMid) ; Point3d ptEnd ; pCrvC->GetEndPoint( ptEnd) ; // elevazione della curva double dCurrElev ; 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 ; } } // Campiono l'interno con una griglia (uso linee parallele a X) // determino il riferimento di base Frame3d frPocket ; Point3d ptCen ; pCompo->GetCentroid( ptCen) ; frPocket.Set( ptCen, vtTool) ; // copio il contorno e lo porto nel riferimento PtrOwner pCompoL( pCompo->Clone()) ; if ( IsNull( pCompoL) || ! pCompoL->ToLoc( frPocket)) { m_pMchMgr->SetLastError( 2408, "Error in Pocketing : Entity GetElevation") ; return false ; } // ingombro del contorno in locale BBox3d b3Pocket ; pCompoL->GetLocalBBox( b3Pocket) ; Point3d ptMin ; double dDimX, dDimY, dDimZ ; b3Pocket.GetMinDim( ptMin, dDimX, dDimY, dDimZ) ; // passi in Y const double STEP = 50 ; int nYStep = max( int( ceil( ( dDimY - 20 * EPS_SMALL) / STEP)), 2) ; double dYStep = ( nYStep > 0 ? ( dDimY - 20 * EPS_SMALL) / nYStep : 0) ; // calcolo le linee di svuotatura int nCount = 0 ; for ( int i = 1 ; i < nYStep ; ++ i) { // 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( 2408, "Error in Pocketing : Entity GetElevation") ; return false ; } // calcolo la classificazione della curva rispetto al contorno IntersCurveCurve intCC( *pLine, *pCompoL) ; CRVCVECTOR ccClass ; if ( intCC.GetCurveClassification( 0, EPS_SMALL, ccClass)) { // determino gli intervalli di curva interni Intervals inOk ; for ( auto& ccOne : ccClass) { if ( ccOne.nClass == CRVC_IN) { Point3d ptStart ; pLine->GetPointD1D2( ccOne.dParS, ICurve::FROM_PLUS, ptStart) ; ptStart.ToGlob( frPocket) ; Point3d ptEnd ; pLine->GetPointD1D2( ccOne.dParE, ICurve::FROM_MINUS, ptEnd) ; ptEnd.ToGlob( frPocket) ; // elevazione della curva double dCurrElev ; if ( GetElevation( m_nPhase, ptStart - vtDepth, ptEnd - vtDepth, vtTool, dCurrElev)) { if ( dCurrElev > dElev) dElev = dCurrElev ; } else { m_pMchMgr->SetLastError( 2408, "Error in Pocketing : Entity GetElevation") ; return false ; } } } } // altrimenti uso tutta la curva else { Point3d ptStart ; pLine->GetStartPoint( ptStart) ; ptStart.ToGlob( frPocket) ; Point3d ptEnd ; pLine->GetEndPoint( ptEnd) ; ptEnd.ToGlob( frPocket) ; // elevazione della curva double dCurrElev ; if ( GetElevation( m_nPhase, ptStart - vtDepth, ptEnd - 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 ISurfFlatRegion* pSrfPock, const Vector3d& vtTool) { // se non è svuotatura dal basso in alto, esco if ( vtTool.z > MIN_ZDIR_TOP_TOOL) return true ; // se c'è testa non dall'alto o tavola basculante, esco if ( ! m_bAboveHead || m_bTiltingTab) 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 il minimo della massima distanza del percorso dal contorno del grezzo double dMinDist = INFINITO ; Vector3d vtMinDir ; VCT3DVECTOR vDir ; for ( int c = 0 ; c < pSrfPock->GetChunkCount() ; c++) { for ( int l = 0 ; l < pSrfPock->GetLoopCount( c) ; l++) { PtrOwner pCompo( GetCurveComposite( pSrfPock->GetLoop( c, l))) ; if ( IsNull( pCompo)) return false ; double dParS, dParE ; pCompo->GetDomain( dParS, dParE) ; for ( double dPar = dParS ; dPar < dParE + EPS_PARAM ; dPar += 0.5) { // distanza minima del punto e relativa direzione dal contorno del grezzo Point3d ptP ; double dCurrDist = INFINITO ; Vector3d vtCurrDir ; if ( pCompo->GetPointD1D2( dPar, ICurve::FROM_MINUS, ptP) && GetMinDistanceFromRawSide( m_nPhase, ptP, 0, m_AggrBottom.vtMDir, MCH_AGB_DELTAMAX_MDIR, dCurrDist, vtCurrDir)) { if ( dCurrDist < dMinDist - 10 * EPS_SMALL && find_if( vDir.begin(), vDir.end(), [&]( const Vector3d& vtV) { return vtCurrDir * vtV > cos(15 * DEGTORAD); }) == vDir.end()) { // inserisco la direzione tra quelle già esplorate vDir.emplace_back( vtCurrDir) ; // determino la distanza di tutti gli altri punti dal contorno del grezzo lungo questa direzione for ( double dPar2 = dParS; dPar2 < dParE + EPS_PARAM; dPar2 += 0.5) { if ( abs( dPar2 - dPar) > EPS_PARAM) { Point3d ptQ ; double dQDist ; if ( pCompo->GetPointD1D2( dPar2, ICurve::FROM_MINUS, ptQ) && GetDistanceFromRawSide( m_nPhase, ptQ, vtCurrDir, dQDist) && dQDist > dCurrDist) dCurrDist = dQDist ; } } // se la massima distanza trovata è inferiore al minimo, lo aggiorno if ( dCurrDist < dMinDist) { dMinDist = dCurrDist ; vtMinDir = vtCurrDir ; } } } } } } // se supera il limite, errore if ( dMinDist > 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 = vtMinDir ; m_bAggrBottom = true ; return true ; } //---------------------------------------------------------------------------- bool Pocketing::GeneratePocketingPv( int nPathId, const ISurfFlatRegion* pSrfPock) { // creo copia della curva composita PtrOwner pSfr( CloneSurfFlatRegion( pSrfPock)) ; 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, RED) ; // 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, RED) ; } // 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, Color( 255, 0, 0, 60)) ; // 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 ; } //---------------------------------------------------------------------------- static double GetCurveRadius( const ICurve* pCrv) { if ( pCrv == nullptr) return 0.0 ; BBox3d b3Loc ; if ( ! pCrv->GetLocalBBox( b3Loc, BBF_EXACT)) return 0.0 ; double dRad ; if ( ! b3Loc.GetRadius( dRad)) return 0.0 ; return dRad ; } //---------------------------------------------------------------------------- bool Pocketing::AddZigZag( const ISurfFlatRegion* pSrfPock, const Vector3d& vtTool, const Vector3d& vtExtr, double dDepth, double dElev, double dOkStep, bool bSplitArcs, int nPathId) { // 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 ; double dTRad = m_TParams.m_dDiam / 2 ; double dOffs = dTRad + GetOffsR() ; double dExtra = (( m_TParams.m_nType != TT_MILL_POLISHING) ? min(0.1 * m_TParams.m_dDiam, 2.0) : 0) ; // vettore delle curve esterne Offsettate (per attacchi) e numero di Chunks ICRVCOMPOPOVECTOR vCrvExt ; int nChunks = 0 ; // copia della mia superificie PtrOwner pSrfToPock( CloneSurfFlatRegion( pSrfPock)) ; if ( IsNull( pSrfToPock)) return false ; // scorro tutti i Chunks della regione per svuotarli for ( int c = 0 ; c < pSrfToPock->GetChunkCount() ; c++) { // copio il Chunk (c)-esimo PtrOwner pSrfChunk( CloneSurfFlatRegion( pSrfToPock->CloneChunk( c))) ; if ( IsNull( pSrfChunk)) return false ; // superficie per ingressi ed uscite PtrOwner pSrfLeanInOut( CloneSurfFlatRegion( pSrfChunk)) ; // determino il riferimento in base alla svuotatura Frame3d frPocket ; Point3d ptCen ; pSrfToPock->GetCentroid( ptCen) ; frPocket.Set( ptCen, vtExtr) ; frPocket.Rotate( ptCen, vtExtr, m_Params.m_dSideAngle) ; // porto la superificie nel nuovo sistema di riferimento pSrfChunk->ToLoc( frPocket) ; // verifico se si tratta di caso ottimizzato bool bOptimizedZigZag = false ; bool bOutRawLeadIn = false ; ICRVCOMPOPOVECTOR vpCrvs ; // vettore con i paths per il Chunk (c)-esimo if ( ! OptimizedZigZag( pSrfChunk, vtTool, dDepth, dSafeZ, frPocket, bOptimizedZigZag, vpCrvs)) return false ; if ( bOptimizedZigZag && ! vpCrvs.empty()) { nChunks = 1; // verifico se attacco fuori dal grezzo Point3d ptStart ; vpCrvs[0]->GetStartPoint( ptStart) ; Vector3d vtDir ; vpCrvs[0]->GetStartDir( vtDir) ; ptStart += -vtDir * ( m_TParams.m_dDiam / 2 + dSafeZ) ; ptStart.ToGlob( frPocket) ; ptStart += -vtTool * dDepth ; double dTestElev ; if ( ! GetElevation( m_nPhase, ptStart, vtTool, m_TParams.m_dDiam / 2, vtTool, dTestElev) || dTestElev < EPS_SMALL) bOutRawLeadIn = true ; // sistemo attacco (per essere completamente fuori dal grezzo) if ( bOutRawLeadIn || m_bOpenOutRaw) vpCrvs[0]->ExtendStartByLen( m_TParams.m_dDiam / 2 + dSafeZ) ; } // se non ottimizzato e utensile che non lavora di testa poichè ingresso non fuori dal pezzo, errore if (( ! bOptimizedZigZag || ! ( bOutRawLeadIn || m_bOpenOutRaw)) && m_TParams.m_nType == TT_MILL_NOTIP) { if ( ! LeadInIsOk()) { m_pMchMgr->SetLastError( 2431, "Error in Pocketing : LeadIn with Mill NoTip in material") ; return false; } } // se non sono nel caso ottimizzato, effettuo il primo Offset della regione e controllo quanti Chunks ottengo... if ( ! bOptimizedZigZag) { if ( ! pSrfChunk->Offset( - dOffs - dExtra, ICurve::OFF_FILLET)) { m_pMchMgr->SetLastError( 2412, "Error in Pocketing : Offset not computable") ; return false ; } nChunks = pSrfChunk->GetChunkCount() ; // facendo il primo Offset il Chunk (c)-esimo può generare altri Chunks... } // tratto ogni Chunk ottenuto come se fosse una superficie singola, quindi con la propria svuotatura bool bStart = true ; // controllo se prima curva for ( int cc = 0 ; cc < nChunks ; cc++) { // per ognuno di questi Chunks... PtrOwner pSrfZigZag( GetSurfFlatRegion( pSrfChunk->CloneChunk( cc))) ; if ( IsNull( pSrfZigZag)) return false ; if ( ! bOptimizedZigZag) { vpCrvs.clear() ; if ( ! CalcZigZag( pSrfZigZag, vpCrvs)) return false ; } // se lucidatura if ( m_TParams.m_nType == TT_MILL_POLISHING) { // ciclo sui percorsi for (int k = 0; k < int( vpCrvs.size()); ++k) { // se attacco a scivolo if ( GetLeadInType() == POCKET_LI_GLIDE) { double dU; vpCrvs[k]->GetParamAtLength(m_Params.m_dLiTang, dU); vpCrvs[k]->AddJoint(dU); Point3d ptStart; vpCrvs[k]->GetStartPoint(ptStart); vpCrvs[k]->ModifyStart(ptStart + vtTool * m_Params.m_dLiElev); } // se uscita a scivolo if ( GetLeadOutType() == POCKET_LO_GLIDE) { double dLen, dU; vpCrvs[k]->GetLength(dLen); vpCrvs[k]->GetParamAtLength(dLen - m_Params.m_dLoTang, dU); vpCrvs[k]->AddJoint(dU); Point3d ptEnd; vpCrvs[k]->GetEndPoint(ptEnd); vpCrvs[k]->ModifyEnd(ptEnd + vtTool * m_Params.m_dLiElev); } } } // inserisco i movimenti di svuotatura for ( int j = 1 ; j <= nStep ; ++j) { // ciclo sui percorsi int nPath = int( vpCrvs.size()); for ( int k = 0 ; k < nPath ; ++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 ENTITA' --- if ( i == 0) { // dati inizio entità Point3d ptStart ; pCurve->GetStartPoint( ptStart) ; Vector3d vtStart ; pCurve->GetStartDir( vtStart) ; // determino inizio attacco Point3d ptP1 ; if ( ! CalcLeadInStart( ptStart, vtStart, vtExtr, nullptr, ptP1)) return false; // determino elevazione su inizio attacco double dStElev ; GetElevation( m_nPhase, ptStart - 10 * EPS_SMALL * vtTool, vtTool, GetRadiusForStartEndElevation(), vtTool, dStElev) ; dStElev = max(dStElev, j * dStep) ; bool bUnderRaw = m_bAboveHead && !m_bAggrBottom && !m_bTiltingTab && GetPointUnderRaw( ptP1, vtTool, 0, GetRadiusForStartEndElevation(), m_TParams.m_dLen, false, dSafeZ, vtTool, dStElev); if ( bUnderRaw) dStElev = max( dStElev, j * dStep) ; dStElev -= ( ptP1 - ptStart) * vtExtr ; // se ottimizzata e attacco nel grezzo if ( bOptimizedZigZag && !( bOutRawLeadIn || m_bOpenOutRaw)) { // se richiesto attacco a zigzag o a spirale, l'elevazione va nell'attacco if ( GetLeadInType() == POCKET_LI_ZIGZAG || GetLeadInType() == POCKET_LI_HELIX) { ptP1 += vtExtr * dStElev ; dStElev = 0 ; } } // se inizio, approccio globale al punto iniziale if ( bStart) { if ( ! AddApproach( ptP1, vtTool, dSafeZ, dSafeAggrBottZ, dStElev, dAppr, bOutRawLeadIn || m_bOpenOutRaw)) { 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, bOutRawLeadIn || m_bOpenOutRaw)) { m_pMchMgr->SetLastError( 2418, "Error in Pocketing : Link not computable") ; return false; } } // aggiungo attacco SetFeed( GetStartFeed()) ; if ( ! AddLeadIn( ptP1, ptStart, vtStart, vtExtr, pSrfLeanInOut, nullptr, !m_Params.m_bInvert, bSplitArcs, bOutRawLeadIn || m_bOpenOutRaw)) { 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 ENTITA' --- if ( i == nMaxInd) { // dati fine entità Point3d ptEnd; pCurve->GetEndPoint(ptEnd); Vector3d vtEnd; pCurve->GetEndDir(vtEnd); // aggiungo uscita double dEndElev = dElev; Point3d ptP1; SetFeed( GetEndFeed()) ; if ( ! AddLeadOut( ptEnd, vtEnd, vtExtr, nullptr, bSplitArcs, ptP1, dEndElev, true)) { m_pMchMgr->SetLastError(2416, "Error in Pocketing : LeadOut not computable") ; return false; } dEndElev = max( dEndElev, j * dStep) ; // se lucidatura o caso ottimizzato e ultimo percorso di ultimo step, aggiungo retrazione if (( m_TParams.m_nType == TT_MILL_POLISHING || bOptimizedZigZag) && k == nPath - 1 && j == nStep) { if ( ! AddRetract( ptP1, vtTool, dSafeZ, dSafeAggrBottZ, dEndElev, dAppr)) { m_pMchMgr->SetLastError( 2418, "Error in Pocketing : Retract not computable") ; return false; } } // altrimenti, aggiungo retrazione di collegamento else { if ( ! AddLinkRetract( ptP1, vtTool, dSafeZ, dSafeAggrBottZ, dEndElev, dAppr)) { m_pMchMgr->SetLastError(2418, "Error in Pocketing : Link not computable"); return false; } } } } } } } // Curva esterna (per passare vicino ai bordi) // se lucidatura o caso ottimizzato, non aggiungo contorno if ( m_TParams.m_nType == TT_MILL_POLISHING /* || bOptimizedZigZag*/) return true; bool bChangeFirst = true; Point3d ptLastEnd; // Copio il Chunk c-esimo e lo porto nel sistema di riferimento della svuotatura pSrfChunk.Set( CloneSurfFlatRegion( pSrfPock->CloneChunk( c))) ; if ( IsNull( pSrfChunk)) return false ; pSrfChunk->ToLoc( frPocket) ; // creo l'Offset per le curve estene if ( ! pSrfChunk->Offset( -dOffs, ICurve::OFF_FILLET)) { m_pMchMgr->SetLastError( 2412, "Error in Pocketing : Offset not computable") ; return false ; } // creo un vettore con tutti i loop formati, aggiustando i loro punti iniziali ICRVCOMPOPOVECTOR vCrvLoops ; for ( int cc = 0 ; cc < pSrfChunk->GetChunkCount() ; cc++) { for ( int l = 0 ; l < pSrfChunk->GetLoopCount( cc) ; l++) { PtrOwner pOffs( CreateCurveComposite()) ; if ( IsNull( pOffs) || ! pOffs->AddCurve( pSrfChunk->GetLoop( cc, l))) { m_pMchMgr->SetLastError( 2413, "Error in Pocketing : Toolpath not computable") ; return false ; } // se richiesto, la inverto if ( m_Params.m_bInvert) pOffs->Invert() ; // resetto le proprietà per attacchi ResetCurveAllTempProp( pOffs) ; // aggiusto il punto iniziale AdjustContourStart( pOffs) ; // verifico archi VerifyArcs( pOffs) ; vCrvLoops.emplace_back( Release( pOffs)) ; } } // se sono nel caso ottimizzato devo togliere il loop esterno if ( bOptimizedZigZag) { double dMaxArea = -INFINITO ; int nJumpInd = 0 ; for ( int i = 0 ; i < int( vCrvLoops.size()) ; i++) { double dArea ; vCrvLoops[i]->GetAreaXY( dArea) ; if ( dArea > dMaxArea) { dMaxArea = dArea ; nJumpInd = i ; } } ICRVCOMPOPOVECTOR vCrvHelp ; for ( int i = 0 ; i < int( vCrvLoops.size()) ; i++) { if ( i != nJumpInd) vCrvHelp.emplace_back( vCrvLoops[i]->Clone()) ; } vCrvLoops.clear() ; vCrvLoops.resize( vCrvHelp.size()) ; for ( int i = 0 ; i < int( vCrvHelp.size()) ; i++) vCrvLoops[i].Set( Release( vCrvHelp[i])) ; } // guardo il punto finale in cui si trova la fresa Point3d ptEnd ; if( vpCrvs.size() > 0) vpCrvs.back()->GetEndPoint( ptEnd) ; // ordino le curve a partire dal punto finale raggiunto della fresa Point3d ptRef = ptEnd ; double dMinDist = INFINITO ; int nIndexSwitch = -1 ; for ( int i = -1 ; i < int( vCrvLoops.size()) - 1 ; i++) { for ( int j = i + 1 ; j < int( vCrvLoops.size()) ; j++) { Point3d ptS ; vCrvLoops[j]->GetStartPoint( ptS) ; if ( Dist( ptS, ptRef) < dMinDist) { dMinDist = Dist( ptS, ptRef) ; nIndexSwitch = j ; } } if ( nIndexSwitch != i + 1) { PtrOwner pCrvClosest( vCrvLoops[nIndexSwitch]->Clone()) ; vCrvLoops[nIndexSwitch].Set(Release( vCrvLoops[i+1])) ; vCrvLoops[i+1].Set( Release( pCrvClosest)) ; } vCrvLoops[i+1]->GetEndPoint( ptRef) ; dMinDist = INFINITO ; } for( int u = 0 ; u < int( vCrvLoops.size()) ; u++) { // recupero la prima curva di offset disponibile PtrOwner pOffs( CreateCurveComposite()); if ( IsNull( pOffs) || ! pOffs->AddCurve( vCrvLoops[u]->Clone())) { m_pMchMgr->SetLastError( 2413, "Error in Pocketing : Toolpath not computable") ; return false; } // 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 ; pCurve->ToGlob( frPocket) ; // aggiungo affondamento pCurve->Translate( -vtTool * ( dDepth - dElev + j * dStep)) ; // --- se PRIMA ENTITA' --- 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 = dStep; bool bUnderRaw = m_bAboveHead && !m_bAggrBottom && !m_bTiltingTab && GetPointUnderRaw( ptP1, vtTool, 0, GetRadiusForStartEndElevation(), m_TParams.m_dLen, false, dSafeZ, vtTool, dStElev) ; if ( bUnderRaw) dStElev = max( dStElev, dStep) ; 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, pSrfLeanInOut, nullptr, !m_Params.m_bInvert, bSplitArcs)) { m_pMchMgr->SetLastError(2415, "Error in Pocketing : LeadIn not computable"); return false; } } // altrimenti solo collegamento else { SetFeed( GetStartFeed()) ; GetCurrPos( ptP1) ; if ( ! AddLeadIn( ptP1, ptStart, vtStart, vtExtr, pSrfLeanInOut, 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 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 ; } // se ci sono ancora curve, aggiungo retrazione di collegamento if ( pOffs->GetCurveCount() > 0) { if ( ! AddLinkRetract( ptP1, vtTool, dSafeZ, dSafeAggrBottZ, dEndElev, dAppr)) { m_pMchMgr->SetLastError( 2418, "Error in Pocketing : Link not computable") ; return false ; } } // altrimenti retrazione finale else { 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 ISurfFlatRegion* pSrfZigZag, ICRVCOMPOPOVECTOR& vpCrvs) { // check parametri if ( pSrfZigZag == nullptr) return true ; // per tagli delle curve non sul secondo offset per zone non svuotate double dExtra = (( m_TParams.m_nType != TT_MILL_POLISHING) ? min(0.1 * m_TParams.m_dDiam, 2.0) : 0) ; // creo il vettore dei contorni ICRVCOMPOPOVECTOR vFirstOff ; for ( int c = 0 ; c < pSrfZigZag->GetChunkCount(); c++) { for ( int l = 0 ; l < pSrfZigZag->GetLoopCount(c); l++) { vFirstOff.emplace_back( GetCurveComposite( pSrfZigZag->GetLoop( c, l)->Clone())) ; } } // ingombro del contorno offsettato BBox3d b3Pocket ; vFirstOff[0]->GetLocalBBox( b3Pocket) ; Point3d ptMin ; double dDimX, dDimY, dDimZ ; b3Pocket.GetMinDim( ptMin, dDimX, dDimY, dDimZ) ; // lunghezza del contorno offsettato double dLen ; vFirstOff[0]->GetLength( dLen) ; // passi in Y int nYStep = static_cast< int >( ceil(( dDimY - 30 * EPS_SMALL) / GetSideStep())) ; double dYStep = ( nYStep > 0 ? ( dDimY - 30 * EPS_SMALL) / nYStep : 0) ; int nRef = (( nYStep + ( m_Params.m_bInvert ? 0 : 1)) % 2) ; // tratto valido struct Section { bool bActive ; Point3d ptS ; Point3d ptE ; double dOs ; double dOe ; int nOffIndS ; int nOffIndE ; } ; struct ParIsland { double dU ; int nInd ; } ; // raccolta di tratti typedef 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) == nRef) ; // 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 ; } if ( ! bPlus) pLine->Invert() ; // calcolo la classificazione della linea rispetto alla superificie CRVCVECTOR ccClass ; pSrfZigZag->GetCurveClassification( *pLine, EPS_SMALL, ccClass) ; for ( int j = 0 ; j < int( ccClass.size()) ; j++) { if ( ccClass[j].nClass == CRVC_IN) { // memorizzo il segmento Section currSec; currSec.bActive = true; PtrOwner pSeg( GetCurveLine( pLine->CopyParamRange( ccClass[j].dParS, ccClass[j].dParE))) ; Point3d ptS, ptE ; pSeg->GetStartPoint( ptS) ; pSeg->GetEndPoint( ptE) ; currSec.ptS = ptS ; currSec.ptE = ptE ; for ( int k = 0 ; k < int( vFirstOff.size()) ; k++) { if ( vFirstOff[k]->IsPointOn( ptS)) { currSec.nOffIndS = k ; double dUOS; vFirstOff[k]->GetParamAtPoint( ptS, dUOS) ; currSec.dOs = dUOS ; } if ( vFirstOff[k]->IsPointOn( ptE)){ currSec.nOffIndE = k ; double dUOE; vFirstOff[k]->GetParamAtPoint( ptE, dUOE) ; currSec.dOe = dUOE ; } } vvSec[i].emplace_back( currSec) ; ++nCount ; // numero di segmenti inseriti } } } int nStatus = 0 ; // 0 -> inizio oppure ho inserito una parte di isola | 2 -> sto inserendo un segmento Point3d ptS_ref ; PtrOwner plastLine( CreateCurveLine()) ; // 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 ( il primo segmento con bActive messo a true) 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()) ; nStatus = 0 ; // aggiungo punto iniziale vpCrvs.back()->AddPoint( vvSec[nI][nJ].ptS) ; } // altrimenti, esco else break ; } // determino senso bool bPlus = (( nI % 2) == nRef) ; // aggiungo la sezione alla curva Section& Sec = vvSec[nI][nJ] ; Sec.bActive = false ; vpCrvs.back()->AddLine( vvSec[nI][nJ].ptE) ; if ( nStatus == 0) { // primo segmento per il raccordo smussato plastLine.Set( GetCurveLine( vpCrvs.back()->GetLastCurve()->Clone())) ; plastLine->GetStartPoint( ptS_ref) ; } if ( nStatus == 2) { // ho precedentemente aggiunto il bordo di un'isola, quindi mi salvo il secondo segmento PtrOwner pSeg1( GetCurveLine( vpCrvs.back()->GetLastCurve()->Clone())) ; PtrOwner pCrvLink( CreateCurveComposite()) ; PtrOwner pSeg2( GetCurveLine( plastLine->Clone())) ; if ( CalcBoundedZigZagLink( pSeg2, pSeg1, vFirstOff, pCrvLink)) { // se riesco a creare una semicirconferenza ... double dUE ; // paramtro finale della curva totale fino ad ora vpCrvs[vpCrvs.size() - 1]->GetParamAtPoint( ptS_ref, dUE) ; // punto finale della curva totale fino ad ora PtrOwner pCrvToSave( CreateCurveComposite()) ; // tratto di curva precedente a pSeg1, pCrvLink, pSeg2 if( dUE != 0) // se il tratto di curva precedente esiste ... pCrvToSave.Set(GetCurveComposite(vpCrvs[vpCrvs.size() - 1]->CopyParamRange( 0, dUE))) ; // ... lo salvo pCrvToSave->AddCurve( Release( pSeg2)) ; // aggiungo al tratto precedente il pSeg1 (trimmed alla fine) pCrvToSave->AddCurve( Release( pCrvLink)) ; // aggiungo la semicirconferenza pCrvToSave->AddCurve( Release( pSeg1)) ; // aggiungo il pSeg2 (trimmed all'inizio) vpCrvs[vpCrvs.size() - 1]->Clear() ; vpCrvs[vpCrvs.size() - 1]->AddCurve( Release( pCrvToSave)) ; // sistemo il percorso } else { // ... se non ho creato una semicirconferenza cerco di smussare pSeg1 - pCrvLink - pSeg2 (pCrvlink è il tratto di isola) double dUS, dUE, dUS_I, dUE_I ; vpCrvs[vpCrvs.size() - 1]->GetDomain( dUS, dUE) ; // dominio del percorso vpCrvs[vpCrvs.size() - 1]->GetParamAtPoint( ptS_ref, dUS) ; // dUS -> parametro della curva totale nel punto iniziale di pSeg1 PtrOwner pCrvToSmooth( CreateCurveComposite()) ; // cuvra smussata pSeg1 - pCrvLink - pSeg2 pCrvToSmooth->AddCurve(vpCrvs[vpCrvs.size() - 1]->CopyParamRange( dUS, dUE)); double dLen1, dLenI_s, dLenI_e, dLen2 ; PtrOwner pCrv1( CreateCurveLine()) ; // segmento pSeg1 Point3d pt1, pt2; pCrvToSmooth->GetFirstCurve()->GetStartPoint( pt1) ; pCrvToSmooth->GetFirstCurve()->GetEndPoint( pt2) ; pCrv1->Set( pt1, pt2) ; PtrOwner pCrv2( CreateCurveLine()); // segmento pSeg2 pCrvToSmooth->GetLastCurve()->GetStartPoint( pt1) ; pCrvToSmooth->GetLastCurve()->GetEndPoint( pt2) ; pCrv2->Set(pt1, pt2) ; Point3d ptSH, ptEH ; // punto iniziale sul pSeg1 pCrv1->GetEndPoint( ptSH) ; vpCrvs[vpCrvs.size() - 1]->GetParamAtPoint( ptSH, dUS_I) ; // parametro sulla curva globale di inizio di pCrvLink pCrv2->GetStartPoint( ptEH) ; vpCrvs[vpCrvs.size() - 1]->GetParamAtPoint( ptEH, dUE_I) ; // paramtro sulla curva globale di fine di pCrvLink PtrOwner pCrvIsl( CreateCurveComposite()) ; // segmento pCrvLink pCrvIsl->AddCurve(vpCrvs[vpCrvs.size() - 1]->CopyParamRange( dUS_I, dUE_I)) ; pCrv1->GetLength( dLen1) ; // lunghezza di pSeg1 pCrv2->GetLength( dLen2) ; // lunghezza di pSeg2 pCrvIsl->GetFirstCurve()->GetLength( dLenI_s); // lunghezza prima curva di PCrvLink pCrvIsl->GetLastCurve()->GetLength( dLenI_e) ; // lunghezza ultima curva di pCrvLink // raccordo tra pSeg1 e pCrvLink double dTollLeft = 1 - ( dLen1 - ( m_TParams.m_dTDiam / 16)) / dLen1 ; // % parametro sinistro per smusso double dTollRight = ( m_TParams.m_dTDiam / 16) / dLenI_s ; // % parametro destro di smusso PtrOwner pCrv_1I( CreateCurveComposite()) ; // curva unione di pSeg1 e pCrvLink smussata pCrv_1I->AddCurve( pCrv1->Clone()); pCrv_1I->AddCurve( pCrvIsl->Clone()) ; ModifyCurveToSmooted( pCrv_1I, dTollLeft, dTollRight) ; double dUS_1IH, dUE_1IH, dToTLen ; pCrv_1I->GetDomain( dUS_1IH, dUE_1IH) ; // dominio della curva smussata tra pSeg1 e PCrvLink pCrv_1I->GetLength( dToTLen) ; // nuova lunghezza della curva smussata tra pSeg1 e pCrvLink dTollLeft = 1 - ( dLenI_e - (m_TParams.m_dTDiam / 16)) / dLenI_e; // % paramtro sinistro per smusso dTollRight = ( m_TParams.m_dTDiam / 16) / dLen2 ; // % parametro destro per smusso PtrOwner pCrv_I2( CreateCurveComposite()) ; // curva unione del primo smusso con pSeg2 (il tutto smussato) pCrv_I2->AddCurve( pCrv_1I->Clone()) ; pCrv_I2->AddCurve( pCrv2->Clone()) ; ModifyCurveToSmooted( pCrv_I2, dTollLeft, dTollRight) ; PtrOwner pCrvToSave( CreateCurveComposite()) ; if ( dUS != 0) pCrvToSave->AddCurve(( vpCrvs[vpCrvs.size() - 1]->CopyParamRange( 0, dUS))) ; pCrvToSave->AddCurve( Release( pCrv_I2)) ; vpCrvs[vpCrvs.size() - 1]->Clear() ; vpCrvs[vpCrvs.size() - 1]->AddCurve(Release( pCrvToSave)) ; } // pSeg2 ora diventa pSeg1 e il procedimento si itera per tutto il percorso plastLine.Set( GetCurveLine( vpCrvs.back()->GetLastCurve()->Clone())) ; plastLine->GetStartPoint( ptS_ref) ; } // cerco nella stessa fila o in quella successiva sezione successiva raccordabile tramite il contorno double dUstart = Sec.dOe ; int nIndexIslandE = Sec.nOffIndE ; // isola di arrivo double dUmin, dUmax ; vFirstOff[nIndexIslandE]->GetDomain( dUmin, dUmax) ; double dUspan = dUmax - dUmin ; 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) // se sezione non attiva... non la considero continue ; if ( vvSec[k][l].nOffIndS != nIndexIslandE) // se isola diversa... non la considero 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, controllo il contorno dell'isola if ( nNextI != -1) { PtrOwner pCopy ; if ( bPlus) { if ( dUref > dUmax) dUref -= dUspan ; pCopy.Set( vFirstOff[nIndexIslandE]->CopyParamRange( dUstart, dUref)) ; if ( ! IsNull( pCopy)) { double dCLen ; pCopy->GetLength( dCLen) ; if ( dCLen > 0.5 * dLen) { pCopy.Set( vFirstOff[nIndexIslandE]->CopyParamRange( dUref, dUstart)) ; if ( ! IsNull( pCopy)) pCopy->Invert() ; } } } else { if ( dUref < dUmin) dUref += dUspan ; pCopy.Set( vFirstOff[nIndexIslandE]->CopyParamRange( dUref, dUstart)) ; if ( ! IsNull( pCopy)) { pCopy->Invert() ; double dCLen; pCopy->GetLength( dCLen) ; if ( dCLen > 0.5 * dLen) pCopy.Set( vFirstOff[nIndexIslandE]->CopyParamRange( dUstart, dUref)) ; } } // controllo che nel percorso scelto non ritorni indietro superando la precendete passata BBox3d b3Copy ; if ( ! IsNull( pCopy)) pCopy->GetLocalBBox( b3Copy) ; if ( ! b3Copy.IsEmpty() && ( b3Copy.GetMax().y - b3Copy.GetMin().y) < dYStep + 10 * EPS_SMALL) { // tengo la curva (non ritorno indietro più dello step) Point3d ptS, ptE ; vpCrvs.back()->AddCurve( Release( pCopy)) ; nStatus = 2 ; // aggiungo il Link nI = nNextI ; nJ = nNextJ ; } else { nI = -1 ; nJ = -1 ; } } else { nI = -1 ; nJ = -1 ; } } return true ; } //---------------------------------------------------------- bool Pocketing::GetUnclearedRegion( ICRVCOMPOPOVECTOR& vFirstOffs, ICRVCOMPOPOVECTOR& vCrvs, ICURVEPOVECTOR& vLinks, ISurfFlatRegion* pSrfToCut) { // controllo dei parametri if ( vFirstOffs.size() == 0 || vCrvs.size() < vFirstOffs.size() ) return false ; pSrfToCut->Clear() ; // 1) creo la regione esterna PtrOwner pSrfExtern( CreateSurfFlatRegion()) ; if ( IsNull( pSrfExtern)) return false ; for ( int i = 0 ; i < int( vFirstOffs.size()) ; i++) { if ( i == 0) pSrfExtern->AddExtLoop( vFirstOffs[i]->Clone()) ; else { ICurve* pCrvIntLoop( vFirstOffs[i]->Clone()) ; if ( ! pCrvIntLoop->Invert() || ! pSrfExtern->AddIntLoop( pCrvIntLoop)) return false ; } } // 2) Creo la regione svuotata dal Tool PtrOwner pSrfTool( CreateSurfFlatRegion()) ; if ( IsNull( pSrfTool)) return false ; for ( int i = 0 ; i < int( vCrvs.size()) ; i++) { PtrOwner pSrfToolRegOffi( GetSurfFlatRegionFromFatCurve( vCrvs[i]->Clone(), m_TParams.m_dDiam / 2 + 5 * EPS_SMALL, false, false)) ; if ( i == 0) pSrfTool.Set( Release( pSrfToolRegOffi)) ; else pSrfTool->Add( *Release( pSrfToolRegOffi)) ; } for ( int i = 0; i < int( vLinks.size()) ; i++) { if ( ! IsNull( vLinks[i])) { PtrOwner pSrfToolRegLinki( GetSurfFlatRegionFromFatCurve( vLinks[i]->Clone(), m_TParams.m_dDiam / 2 + 5 * EPS_SMALL, false, false)) ; pSrfTool->Add( *Release( pSrfToolRegLinki)) ; } } // 3) Creo la regione contenente tutte le parti non svuotate pSrfToCut->CopyFrom( pSrfExtern->Clone()) ; if ( ! pSrfToCut->Subtract( *pSrfTool)) return false ; return true ; } //---------------------------------------------------------- bool Pocketing::CalcBoundedZigZagLink( ICurveLine* pCrv1, ICurveLine* pCrv2, ICRVCOMPOPOVECTOR& vOffIslands, ICurveComposite* pCrvLink, int nIndexCut) { // controllo dei parametri if ( pCrv1 == nullptr || pCrv2 == nullptr) return false ; if ( ! ( nIndexCut == 1 || nIndexCut == 2 || nIndexCut == 3 || nIndexCut == 4)) return false ; pCrvLink->Clear() ; // prendo i parametri per il raccordo Vector3d vtS, vtE, vtH ; pCrv1->GetEndDir( vtS) ; pCrv2->GetStartDir( vtE) ; Point3d ptS, ptE, ptE1, ptS2 ; pCrv1->GetEndPoint( ptS) ; pCrv2->GetStartPoint( ptE) ; vtH = ptE - ptS ; double dLen1, dLen2 ; pCrv1->GetLength( dLen1) ; pCrv2->GetLength( dLen2) ; double dUS1, dUE1, dUS2, dUE2 ; pCrv1->GetDomain( dUS1, dUE1) ; pCrv2->GetDomain( dUS2, dUE2) ; double dAngle, dAngleH ; double dDist = Dist( ptS, ptE) ; PtrOwner pCrvH1( CreateCurveLine()) ; PtrOwner pCrvH2( CreateCurveLine()) ; PtrOwner pCrvTempLink( CreateCurveComposite()) ; PtrOwner pCrvTest( CreateCurveComposite()) ; if ( pCrvH1 == nullptr || pCrvH2 == nullptr || pCrvTempLink == nullptr || pCrvTest == nullptr) return false ; if ( vtS.GetAngle( vtE, dAngle) && abs( dAngle) < 180 + 5 * EPS_SMALL && abs( dAngle) > 180 - 5 * EPS_SMALL && vtH.GetAngle( vtE, dAngleH) && abs( dAngleH) < 90 + 5 * EPS_SMALL && abs( dAngleH) > 90 - 5 * EPS_SMALL && dLen1 > dDist && dLen2 > dDist) { // creo una semi circonferenza double dRad = dDist / 2 ; ICurveArc* pSemiCir( CreateCurveArc()) ; Point3d ptNS, ptNE ; switch ( nIndexCut) { case 1: // taglio solo la prima pCrv1->GetParamAtLength( dLen1 - dRad, dUE1) ; break ; case 2: // taglio solo la seconda pCrv2->GetParamAtLength( dRad, dUS2) ; break ; case 3: // taglio entrambe pCrv1->GetParamAtLength( dLen1 - dRad, dUE1) ; pCrv2->GetParamAtLength( dRad, dUS2) ; break ; default: break ; } pCrv1->GetPointD1D2( dUE1, ICurve::FROM_MINUS, ptNS); pCrv2->GetPointD1D2( dUS2, ICurve::FROM_PLUS, ptNE); if (pSemiCir->Set2PVN(ptNS, ptNE, vtS, Z_AX)) { pCrvH1.Set(GetCurveLine(pCrv1->CopyParamRange(dUS1, dUE1))); pCrvH2.Set(GetCurveLine(pCrv2->CopyParamRange(dUS2, dUE2))); pCrvTempLink->AddCurve(pSemiCir->Clone()); } } else if( dLen1 - m_TParams.m_dDiam / 4 > 50 * EPS_SMALL && dLen2 - m_TParams.m_dDiam / 4 > 50 * EPS_SMALL) { // se non riesco collego con un segmento ? no, esco dalla funzione return false ; switch ( nIndexCut) { case 1: // taglio solo la prima pCrv1->GetParamAtLength( dLen1 - m_TParams.m_dDiam / 4, dUE1) ; break ; case 2: // taglio solo la seconda pCrv2->GetParamAtLength( m_TParams.m_dDiam / 4, dUS2) ; break ; case 3: // taglio entrambe pCrv1->GetParamAtLength( dLen1 - m_TParams.m_dDiam / 4, dUE1) ; pCrv2->GetParamAtLength( m_TParams.m_dDiam / 4, dUS2) ; break ; default: break; } pCrvH1.Set( GetCurveLine( pCrv1->CopyParamRange( dUS1, dUE1))) ; pCrvH2.Set( GetCurveLine( pCrv2->CopyParamRange( dUS2, dUE2))) ; pCrvH1->GetEndPoint( ptS) ; pCrvH1->GetEndDir( vtS) ; pCrvH2->GetStartPoint( ptE) ; pCrvH2->GetStartDir( vtE) ; if ( ! CalcBoundedSmootedLink( ptS, vtS, ptE, vtE, 0.5, vOffIslands, pCrvTempLink)) return false ; } else { return false ; } // controllo finale sui raccordi ammissibili ... if ( pCrvTest->AddCurve( pCrvH1->Clone()) && pCrvTest->AddCurve( pCrvTempLink->Clone()) && pCrvTest->AddCurve( pCrvH2->Clone())) { //pCrv1->Clear() ; //pCrv1->AddCurve( Release( pCrvH1)) ; Point3d pt1, pt2; pCrvH1->GetStartPoint(pt1); pCrvH1->GetEndPoint(pt2); pCrv1->Set(pt1, pt2); pCrvLink->Clear() ; //m_pGeomDB->AddGeoObj(GDB_ID_NULL, GDB_ID_ROOT, pCrvTempLink->Clone()); pCrvLink->AddCurve( pCrvTempLink->Clone()) ; pCrvH2->GetStartPoint(pt1); pCrvH2->GetEndPoint(pt2); pCrv2->Set(pt1, pt2); //pCrv2->Clear() ; //pCrv2->AddCurve( pCrvH2->Clone()) ; return true ; } return false ; } //---------------------------------------------------------- bool Pocketing::OptimizedZigZag( ISurfFlatRegion* pSrf, const Vector3d& vtTool, double dDepth, double dSafeZ, Frame3d& frPocket, bool& bOptimizedZigZag, ICRVCOMPOPOVECTOR& vpCrvs) { // clono la superficie (chunk c-esimo dell'originale) PtrOwner pSrfPocket( CloneSurfFlatRegion( pSrf)) ; if ( IsNull( pSrfPocket)) return false ; // recupero la curva ORIGINALE che delimita la svuotatura (questa curva non risente dell'offset fatto dalla superificie creata dopo) int nCrvId = pSrfPocket->GetLoop( 0, 0)->GetTempProp( 0) ; PtrOwner pCrvPocket( CloneCurveComposite( m_pGeomDB->GetGeoObj( nCrvId))) ; if ( IsNull( pCrvPocket)) return true ; // porto la curva nel sistema di riferimento locale pCrvPocket->ToLoc( frPocket) ; // setto proprietà sulle curve SetCurveAllTempProp( nCrvId, pCrvPocket) ; pCrvPocket->MergeCurves( 10 * EPS_SMALL, 10 * EPS_ANG_SMALL) ; // sistemo senso antiorario visto dalla direzione di estrusione Vector3d vtPocket; pCrvPocket->GetExtrusion( vtPocket) ; Plane3d plPlane ; double dArea ; pCrvPocket->GetArea( plPlane, dArea) ; if ( plPlane.GetVersN() * vtPocket * dArea < 0) pCrvPocket->Invert() ; // recupero gli id dei lati chiusi INTVECTOR vnInfoClosed ; for( int i = 0 ; i < pCrvPocket->GetCurveCount() ; i++) { int nProp ; if ( pCrvPocket->GetCurveTempProp( i, nProp) && nProp == 0) vnInfoClosed.push_back( i) ; } int nClosedSides = vnInfoClosed.size() ; // modifico pCrvPocket per poterla passare a CalcZigZag bool bTwoOpposite ; Vector3d vtDir ; // direzione principale del segmento più lungo (linea) switch ( nClosedSides) { case 0 : // 0 lati CHIUSI -> tutti APERTI ZigZagOptimizedNoClosedEdges( pCrvPocket, bOptimizedZigZag, vtDir) ; break ; case 1 : // 1 lato CHIUSO ZigZagOptimizedOneClosedEdge( pCrvPocket, vnInfoClosed[0], bOptimizedZigZag, vtDir) ; break ; case 2 : // 2 lati CHIUSI ZigZagOptimizedTwoClosedEdges( pCrvPocket, vnInfoClosed, bOptimizedZigZag, bTwoOpposite, vtDir) ; break ; case 3 : // 3 lati CHIUSI ZigZagOptimizedThreeClosedEdges( pCrvPocket, vnInfoClosed, bOptimizedZigZag, bTwoOpposite, vtDir) ; break ; default : // nessuna ottimizzazione... bOptimizedZigZag = false ; break ; } if ( ! bOptimizedZigZag) return true ; // oriento il frame della svuotatura allineando asse X con vtDir if ( vtDir.IsZero()) pCrvPocket->GetStartDir( vtDir) ; double dAng ; vtDir.GetAngleXY( X_AX, dAng) ; if ( nClosedSides == 0) dAng += m_Params.m_dSideAngle ; pCrvPocket->ToGlob( frPocket) ; Frame3d frameH( frPocket) ; Point3d ptCen = frPocket.Orig() ; Vector3d vtExtr = frPocket.VersZ() ; frPocket.Rotate( ptCen, vtExtr, -dAng) ; pCrvPocket->ToLoc( frPocket) ; // vettore contenente le curve di primo Offset ottimizzate ( bordo esterno Optimized e isole ) ICRVCOMPOPOVECTOR vFirstOffsets ;; vFirstOffsets.emplace_back( Release( pCrvPocket)) ; // la curva esterna modificata double dTRad = m_TParams.m_dDiam / 2 ; double dOffs = dTRad + GetOffsR() ; double dExtra = (( m_TParams.m_nType != TT_MILL_POLISHING) ? min( 0.1 * m_TParams.m_dDiam, 2.0) : 0) ; PtrOwner pSrfPocketClone( CloneSurfFlatRegion( pSrfPocket)) ; if ( IsNull( pSrfPocketClone)) return false ; if ( ! pSrfPocketClone->Offset( - dOffs - dExtra, ICurve::OFF_FILLET)) { // le isole interne vanno Offsettate m_pMchMgr->SetLastError( 2412, "Error in Pocketing : Offset not computable") ; return false ; } ICRVCOMPOPOVECTOR vCrvAllLoops ; double dMaxArea = -INFINITO; int nIndex = -1; int p = 0 ; // p è il # di loop totali for ( int cu = 0 ; cu < pSrfPocketClone->GetChunkCount(); cu++) { for ( int l = 0 ; l < pSrfPocketClone->GetLoopCount( cu); l++) { // inserisco i loops portandoli nel nuovo sistema di riferimento PtrOwner pCrvLoop( GetCurveComposite( pSrfPocketClone->GetLoop( cu, l))) ; if ( IsNull( pCrvLoop)) return false ; pCrvLoop->ToGlob( frameH) ; pCrvLoop->ToLoc( frPocket) ; double dArea; pCrvLoop->GetAreaXY( dArea) ; if ( dArea > dMaxArea) { dMaxArea = dArea ; nIndex = p ; } vCrvAllLoops.emplace_back( Release( pCrvLoop)) ; p++ ; } } for ( int i = 0 ; i < int( vCrvAllLoops.size()) ; i++) { if ( i != nIndex) { vFirstOffsets.emplace_back( vCrvAllLoops[i]->Clone()) ; } } // creo la superficie da svuotare ottimizzata SurfFlatRegionByContours pSrfMod ; for ( int i = 0 ; i < int( vFirstOffsets.size()) ; i++) pSrfMod.AddCurve( vFirstOffsets[i]->Clone()) ; PtrOwner pSrfZigZag( GetSurfFlatRegion( pSrfMod.GetSurf())) ; if ( IsNull( pSrfZigZag)) return false ; if ( AreOppositeVectorApprox( vtExtr, pSrfZigZag->GetNormVersor())) pSrfZigZag->Invert() ; // calcolo il percorso di svuotatura if ( ! CalcZigZag( pSrfZigZag, vpCrvs)) return false ; // se un lato chiuso if ( nClosedSides == 1) { // inverto il percorso vpCrvs[0]->Invert() ; // verifico se attacco fuori dal grezzo Point3d ptStart ; vpCrvs[0]->GetStartPoint( ptStart) ; Vector3d vtDir ; vpCrvs[0]->GetStartDir( vtDir) ; Point3d ptTest = ptStart + ( - vtDir) * ( m_TParams.m_dDiam / 2 + dSafeZ) ; ptTest.ToGlob( frPocket) ; ptTest += - vtTool * dDepth ; double dTestElev ; // se è nel grezzo provo a ruotare di 90 gradi if ( GetElevation( m_nPhase, ptTest, vtTool, m_TParams.m_dDiam / 2, vtTool, dTestElev) && dTestElev > EPS_SMALL) { Vector3d vtDirO = vtDir ; vtDirO.Rotate( Z_AX, ( m_Params.m_bInvert ? -90 : 90)) ; Point3d ptTestO = ptStart + vtDirO * ( m_TParams.m_dDiam / 2 + dSafeZ) ; ptTestO.ToGlob( frPocket) ; ptTestO += - vtTool * dDepth ; double dTestElevO ; // se è fuori dal grezzo uso inizio ruotato if ( ! GetElevation( m_nPhase, ptTestO, vtTool, m_TParams.m_dDiam / 2, vtTool, dTestElevO) || dTestElevO < EPS_SMALL) { Point3d ptNewStart = ptStart + vtDirO ; vpCrvs[0]->AddLine( ptNewStart, false) ; } } } // se due lati chiusi consecutivi o tre lati chiusi... else if (( nClosedSides == 2 || nClosedSides == 3) && ! bTwoOpposite) { // inverto il percorso vpCrvs[0]->Invert() ; // allungo opportunamente inizio e fine Point3d ptStart ; vpCrvs[0]->GetStartPoint( ptStart) ; Vector3d vtDir ; vpCrvs[0]->GetStartDir( vtDir) ; vtDir.Rotate( Z_AX, ( m_Params.m_bInvert ? -90 : 90)) ; ptStart += vtDir ; Point3d ptEnd ; vpCrvs[0]->GetEndPoint( ptEnd) ; ptEnd += OrthoCompo( ptStart - ptEnd, X_AX) ; vpCrvs[0]->AddLine( ptEnd, true) ; vpCrvs[0]->AddLine( ptStart, false) ; } // estendo la fine del percorso if ( ! ( nClosedSides == 3 && bTwoOpposite)) vpCrvs[0]->ExtendEndByLen( m_TParams.m_dDiam / 4) ; return true ; } //------------------------------------------------------------------ bool Pocketing::ZigZagOptimizedNoClosedEdges( ICurveComposite* pCrvPocket, bool& bOptimizedZigZag, Vector3d& vtDir) { // individuo il segmento di retta più lungo int nMax = -1 ; double dMaxLen = 0 ; for ( int i = 0 ; i < int( pCrvPocket->GetCurveCount()) ; ++ i) { const ICurve* pCrv = pCrvPocket->GetCurve( i) ; if ( pCrv->GetType() == CRV_LINE) { double dLen = 0 ; pCrv->GetLength( dLen) ; if ( dLen > dMaxLen) { dMaxLen = dLen ; nMax = i ; } } } if ( nMax == -1) return true ; pCrvPocket->GetCurve( nMax)->GetStartDir( vtDir) ; // aggiorno pCrvPocket con eventuale offset per regioni residue if ( ! ZigZagOptimizedComputeOffset( pCrvPocket, vtDir, 0, {})) return true ; bOptimizedZigZag = true ; return true ; } //------------------------------------------------------------------ bool Pocketing::ZigZagOptimizedOneClosedEdge( ICurveComposite* pCrvPocket, int nClosedId, bool& bOptimizedZigZag, Vector3d& vtDir) { // verifico che il lato chiuso sia una linea PtrOwner pCrv( CloneCurveLine( pCrvPocket->GetCurve( nClosedId))) ; if ( IsNull( pCrv)) return true ; // aggiorno pCrvPocket con eventuale offset per regioni residue pCrv->GetStartDir( vtDir) ; if ( ! ZigZagOptimizedComputeOffset( pCrvPocket, vtDir, 1, {nClosedId})) return true ; // setto il vettore estrusione per eseguire correttamente offset Vector3d vtExtr ; pCrvPocket->GetExtrusion( vtExtr) ; pCrv->SetExtrusion( vtExtr) ; pCrv->SimpleOffset( -m_TParams.m_dDiam / 2 - GetOffsR() + 10 * EPS_SMALL) ; pCrv->ExtendStartByLen( 300) ; pCrv->ExtendEndByLen( 300) ; // sarà la prima curva del percorso if ( ! CutCurveWithLine( pCrvPocket, pCrv)) return false ; bOptimizedZigZag = true ; return true ; } //------------------------------------------------------------------ bool Pocketing::ZigZagOptimizedTwoClosedEdges( ICurveComposite* pCrvPocket, const INTVECTOR& vnClosedIds, bool& bOptimizedZigZag, bool& bOpposite, Vector3d& vtDir) { // verifico che i lati chiusi siano linee PtrOwner pCrv1( CloneCurveLine( pCrvPocket->GetCurve( vnClosedIds[0]))) ; PtrOwner pCrv2( CloneCurveLine( pCrvPocket->GetCurve( vnClosedIds[1]))) ; if ( IsNull( pCrv1) || IsNull( pCrv2)) return true ; // verifico abbiano direzioni opposte Vector3d vtDir1, vtDir2 ; pCrv1->GetStartDir( vtDir1) ; pCrv2->GetStartDir( vtDir2) ; if ( AreOppositeVectorApprox( vtDir1, vtDir2)) bOpposite = true ; else { // verifico siano consecutive Point3d ptStart1 ; pCrv1->GetStartPoint( ptStart1) ; Point3d ptEnd1 ; pCrv1->GetEndPoint( ptEnd1) ; Point3d ptStart2 ; pCrv2->GetStartPoint( ptStart2) ; Point3d ptEnd2 ; pCrv2->GetEndPoint( ptEnd2) ; if ( AreSamePointApprox( ptEnd1, ptStart2) || AreSamePointApprox( ptEnd2, ptStart1)) bOpposite = false ; else return true ; } // setto il vettore estrusione per eseguire correttamente offset Vector3d vtExtr ; pCrvPocket->GetExtrusion( vtExtr) ; pCrv1->SetExtrusion( vtExtr) ; pCrv2->SetExtrusion( vtExtr) ; // determino la curva chiusa più lunga double dLen1 ; pCrv1->GetLength( dLen1) ; double dLen2 ; pCrv2->GetLength( dLen2) ; // se non opposti, verifico che almeno una delle due sia corta if ( ! bOpposite && dLen1 > 1.5 * m_TParams.m_dDiam && dLen2 > 1.5 * m_TParams.m_dDiam) return true ; // trovo la direzione principale della svuotatura if ( dLen1 > dLen2) pCrv1->GetStartDir( vtDir) ; else pCrv2->GetStartDir( vtDir) ; // aggiorno pCrvPocket con eventuale offset per regioni residue if ( ! ZigZagOptimizedComputeOffset( pCrvPocket, vtDir, bOpposite ? 2 : 1, vnClosedIds)) return true ; double dOffs = m_TParams.m_dDiam / 2 + GetOffsR() - 10 * EPS_SMALL ; pCrv1->SimpleOffset( -dOffs) ; pCrv1->ExtendStartByLen( 300) ; pCrv1->ExtendEndByLen( 300) ; if ( ! CutCurveWithLine( pCrvPocket, pCrv1)) return false ; // sarà la prima curva del percorso pCrv2->SimpleOffset( -dOffs) ; pCrv2->ExtendStartByLen( 300) ; pCrv2->ExtendEndByLen( 300) ; if ( ! CutCurveWithLine( pCrvPocket, pCrv2)) return false ; bOptimizedZigZag = true ; return true ; } //------------------------------------------------------------------ bool Pocketing::ZigZagOptimizedThreeClosedEdges( ICurveComposite* pCrvPocket, const INTVECTOR& vnClosedIds, bool& bOptimizedZigZag, bool& bOpposite, Vector3d& vtDir) { // verifico che i lati chiusi siano linee PtrOwner pCrv1( CloneCurveLine( pCrvPocket->GetCurve( vnClosedIds[0]))) ; PtrOwner pCrv2( CloneCurveLine( pCrvPocket->GetCurve( vnClosedIds[1]))) ; PtrOwner pCrv3( CloneCurveLine( pCrvPocket->GetCurve( vnClosedIds[2]))) ; if ( IsNull( pCrv1) || IsNull( pCrv2) || IsNull( pCrv3)) return true ; // verifico siano consecutivi Point3d ptStart1 ; pCrv1->GetStartPoint( ptStart1) ; Point3d ptEnd1 ; pCrv1->GetEndPoint( ptEnd1) ; Point3d ptStart2 ; pCrv2->GetStartPoint( ptStart2) ; Point3d ptEnd2 ; pCrv2->GetEndPoint( ptEnd2) ; Point3d ptStart3 ; pCrv3->GetStartPoint( ptStart3) ; Point3d ptEnd3 ; pCrv3->GetEndPoint( ptEnd3) ; if ( AreSamePointApprox( ptEnd1, ptStart2) && AreSamePointApprox( ptEnd2, ptStart3)) ; else if ( AreSamePointApprox( ptEnd2, ptStart3) && AreSamePointApprox( ptEnd3, ptStart1)) { swap( pCrv1, pCrv2) ; swap( pCrv2, pCrv3) ; } else if ( AreSamePointApprox( ptEnd3, ptStart1) && AreSamePointApprox( ptEnd1, ptStart2)) { swap( pCrv1, pCrv3) ; swap( pCrv3, pCrv2) ; } else return true ; // calcolo la direzione della svuotatura e verifico che lati non siano troppo lunghi double dLen1 ; pCrv1->GetLength( dLen1) ; double dLen2 ; pCrv2->GetLength( dLen2) ; double dLen3 ; pCrv3->GetLength( dLen3) ; if ( dLen2 > dLen1 && dLen2 > dLen3) { if ( dLen1 > 1.5 * m_TParams.m_dDiam || dLen3 > 1.5 * m_TParams.m_dDiam) return true ; pCrv2->GetStartDir( vtDir) ; bOpposite = false ; } else { if ( dLen2 > 1.5 * m_TParams.m_dDiam) return true ; pCrv3->GetStartDir( vtDir) ; bOpposite = true ; } // aggiorno pCrvPocket con eventuale offset per regioni residue if ( ! ZigZagOptimizedComputeOffset( pCrvPocket, vtDir, 1, vnClosedIds)) return true ; // setto il vettore estrusione per eseguire correttamente offset Vector3d vtExtr ; pCrvPocket->GetExtrusion( vtExtr) ; pCrv1->SetExtrusion( vtExtr) ; pCrv2->SetExtrusion( vtExtr) ; pCrv3->SetExtrusion( vtExtr) ; double dOffs = m_TParams.m_dDiam / 2 + GetOffsR() - 10 * EPS_SMALL ; pCrv1->SimpleOffset( -dOffs) ; pCrv1->ExtendStartByLen( 300) ; pCrv1->ExtendEndByLen( 300) ; if ( ! CutCurveWithLine( pCrvPocket, pCrv1)) return false ; pCrv3->SimpleOffset( -dOffs) ; pCrv3->ExtendStartByLen( 300) ; pCrv3->ExtendEndByLen( 300) ; if ( ! CutCurveWithLine( pCrvPocket, pCrv3)) return false ; // sarà la prima curva del percorso pCrv2->SimpleOffset( -dOffs) ; pCrv2->ExtendStartByLen( 300) ; pCrv2->ExtendEndByLen( 300) ; if ( ! CutCurveWithLine( pCrvPocket, pCrv2)) return false ; bOptimizedZigZag = true ; return true ; } //------------------------------------------------------------------ bool Pocketing::ZigZagOptimizedComputeOffset( ICurveComposite* pCrvPocket, const Vector3d& vtMainDir, int nOffsettedEdgesOnY, const INTVECTOR& vnClosedIds) { // calcolo il side step che verrà utilizzato in CalcZigZag /*Frame3d frLoc; if ( ! frLoc.Set( ORIG, Z_AX, vtMainDir)) return true ; BBox3d b3Loc ; pCrvPocket->ToLoc( frLoc) ; pCrvPocket->GetLocalBBox( b3Loc) ; pCrvPocket->ToGlob( frLoc) ; Point3d pt ; double dDimX, dDimY, dDimZ ; b3Loc.GetMinDim( pt, dDimX, dDimY, dDimZ) ; // riduco la DimY della svuotatura in base al numero di lati chiusi che saranno offsettati lungo quella direzione dDimY -= nOffsettedEdgesOnY * ( 0.5 * m_TParams.m_dDiam + GetOffsR()) ; int nYStep = static_cast( ceil( ( dDimY - 30 * EPS_SMALL) / GetSideStep())) ; double dYSideStep = ( nYStep > 0 ? ( dDimY - 30 * EPS_SMALL) / nYStep : 0) ; // individuo i lati vicini a quelli closed for ( int i = 0 ; i < ( int)vnClosedIds.size() ; i ++) { int nNext = vnClosedIds[i] == pCrvPocket->GetCurveCount() - 1 ? 0 : vnClosedIds[i] + 1 ; int nPrev = vnClosedIds[i] == 0 ? pCrvPocket->GetCurveCount() - 1 : vnClosedIds[i] - 1 ; pCrvPocket->SetCurveTempProp( nNext, 1, 1) ; pCrvPocket->SetCurveTempProp( nPrev, 1, 1) ; } // verifico se resteranno aree residue e calcolo eventuale offset per pCrvPocket double dOffs = 0 ; for (int i = 0; i < pCrvPocket->GetCurveCount(); i++) { double dOffsTmp = 0 ; double dVal = 0 ; if ( pCrvPocket->GetCurve( i)->GetType() == CRV_LINE) { Vector3d vtDir ; if ( ! pCrvPocket->GetCurve( i)->GetStartDir( vtDir)) return false ; int nProp ; pCrvPocket->GetCurveTempProp( i, nProp, 1) ; double dLen ; pCrvPocket->GetCurve( i)->GetLength( dLen) ; if ( nProp == 1 && dLen > 1000 * EPS_SMALL) { // gestione speciale se vicino al lato closed double dCosAlpha = vtMainDir * vtDir ; if ( abs( dCosAlpha) > EPS_SMALL && abs( dCosAlpha) < 1 - EPS_SMALL) // se non quasi paralleli o quasi perpendicolari dOffsTmp = abs( 0.5 * m_TParams.m_dDiam * dCosAlpha) ; } else { double dSinAlpha = ( vtMainDir ^ vtDir).Len() ; if ( abs( dSinAlpha) > EPS_SMALL) // se sono quasi perpendicolari dVal = ( dYSideStep - 0.5 * m_TParams.m_dDiam) / dSinAlpha - 0.5 * m_TParams.m_dDiam ; if ( dVal > EPS_SMALL) { double dCosAlpha = vtMainDir * vtDir ; dOffsTmp = abs( ( dYSideStep - 0.5 * m_TParams.m_dDiam) * dCosAlpha) ; } } } if ( dOffsTmp > dOffs) dOffs = dOffsTmp ; }*/ double dOffs = m_TParams.m_dDiam / 2 + 5 * EPS_SMALL; if ( dOffs > EPS_SMALL) { // calcolo offset OffsetCurve OffsCrv ; if ( ! OffsCrv.Make( pCrvPocket, dOffs, ICurve::OFF_EXTEND)) { m_pMchMgr->SetLastError( 2412, "Error in Pocketing : Offset not computable") ; return false ; } if ( OffsCrv.GetCurveCount() > 1) return false ; // aggiorno pCrvPocket pCrvPocket->Clear() ; pCrvPocket->AddCurve( OffsCrv.GetCurve()) ; } return true ; } //------------------------------------------------------------------ bool Pocketing::CutCurveWithLine( ICurveComposite* pCrvA, const ICurveLine* pCrvB) { IntersCurveCurve IntersCC( *pCrvA, *pCrvB) ; CRVCVECTOR ccClass ; IntersCC.GetCurveClassification( 1, EPS_SMALL, ccClass) ; if ( ccClass.size() != 3 || ccClass[0].nClass != CRVC_OUT || ccClass[1].nClass == CRVC_OUT || ccClass[2].nClass != CRVC_OUT) return false ; Point3d ptS, ptE ; pCrvB->GetPointD1D2( ccClass[1].dParS, ICurve::FROM_MINUS, ptS) ; pCrvB->GetPointD1D2( ccClass[1].dParE, ICurve::FROM_MINUS, ptE) ; double dParS, dParE ; pCrvA->GetParamAtPoint( ptS, dParS) ; pCrvA->GetParamAtPoint( ptE, dParE) ; PtrOwner pCrvTmp( CloneCurveComposite( pCrvA)) ; if ( IsNull( pCrvTmp)) return false ; pCrvA->Clear() ; pCrvA->AddCurve( pCrvB->CopyParamRange( ccClass[1].dParS, ccClass[1].dParE)) ; pCrvA->AddCurve( pCrvTmp->CopyParamRange( dParE, dParS)) ; return true ; } //---------------------------------------------------------------------------- bool Pocketing::AddOneWay( const ISurfFlatRegion* pSrfPock, 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 ; // se utensile che non lavora di testa poichè ingresso non fuori dal pezzo, errore if ( m_TParams.m_nType == TT_MILL_NOTIP) { if ( ! LeadInIsOk()) { m_pMchMgr->SetLastError( 2431, "Error in Pocketing : LeadIn with Mill NoTip in material") ; return false; } } // determino numero e affondamento degli step int nStep = 1 ; nStep = max( 1, static_cast( ceil(dElev / dOkStep))) ; double dStep = dElev / nStep ; double dTRad = 0.5 * m_TParams.m_dDiam ; double dOffs = dTRad + GetOffsR() ; double dExtra = min( 0.1 * m_TParams.m_dDiam, 1.0) ; // copio la regione da svuotare, il Chunk (c)-esimo PtrOwner pSrfToPock( pSrfPock->Clone()) ; if ( IsNull( pSrfToPock)) return false ; // classifico i chunks in modo da creare delle regioni ideali; una regione ideale è formata da un chunk principale // con tutti gli altri contenuti in esso ( in questo modo ottimizzo i percorsi per i bordi) typedef vector> SRFFLTVECTOR ; SRFFLTVECTOR vSrfFlat ; INTVECTOR vChunksAvailable( pSrfToPock->GetChunkCount(), 1) ; for ( int c = 0 ; c < pSrfToPock->GetChunkCount() ; c++) { PtrOwner pSrfIdeal( CreateSurfFlatRegion()) ; if ( IsNull( pSrfIdeal)) return false ; if ( vChunksAvailable[c] == 1) { // se Chunk valido... PtrOwner pCrvExt( GetCurveComposite( pSrfToPock->GetLoop( c, 0))) ; if ( IsNull( pCrvExt)) return false ; pSrfIdeal.Set( pSrfToPock->CloneChunk( c)) ; vChunksAvailable[c] = -1 ; for ( int i = 0; i < int( vChunksAvailable.size()) ; i++) { if ( vChunksAvailable[i] == 1) { // se chunk valido... PtrOwner pCrvExtC( GetCurveComposite( pSrfToPock->GetLoop( i, 0))) ; if ( IsNull( pCrvExtC)) return false ; IntersCurveCurve intCC( *pCrvExtC, *pCrvExt) ; CRVCVECTOR ccClass ; intCC.GetCurveClassification( 0, EPS_SMALL, ccClass) ; if ( ccClass.size() == 1 && ccClass[0].nClass == CRVC_IN) { pSrfIdeal->Add( *pSrfToPock->CloneChunk( i)) ; vChunksAvailable[i] = -1 ; } } } } if ( pSrfIdeal->GetChunkCount() > 0) { vSrfFlat.emplace_back( Release( pSrfIdeal)) ; } } // === per ogni superificie ideale creo 2 Offsets ===, // - dOffs con le curve da percorrere all'inizio ( bordo da percorrere, senza mai superarlo) // - dOffs - dExtra per definire le curve interne della svuotatura ( bordo immaginario per il OneWay ) for ( int nIdealSurf = 0 ; nIdealSurf < int( vSrfFlat.size()) ; nIdealSurf++) { // === PRIMO OFFSET === PtrOwner pSrfIdeal( CloneSurfFlatRegion( vSrfFlat[nIdealSurf])->Clone()) ; if ( IsNull( pSrfIdeal)) return false ; // per entrate ed uscite PtrOwner pSrfLeanInOut( CloneSurfFlatRegion( pSrfIdeal)) ; // effettuo il primo Offset della regione e controllo quanti Chunks ottengo if ( ! pSrfIdeal->Offset( - dOffs, ICurve::OFF_FILLET)) { m_pMchMgr->SetLastError( 2412, "Error in Pocketing : Offset not computable") ; return false ; } // salvo tutte le curve di Offset in un vettore per poi ordinarle ICRVCOMPOPOVECTOR vAllCrv ; for ( int cc = 0 ; cc < pSrfIdeal->GetChunkCount() ; cc++) { for ( int l = 0 ; l < pSrfIdeal->GetLoopCount( cc) ; l++) { vAllCrv.emplace_back( GetCurveComposite( pSrfIdeal->GetLoop( cc, l))) ; } } int nClosestInd = -1 ; int nFlag ; int nFocus = 0 ; double dDist = INFINITO ; Point3d ptHelp ; // scorro tutte le curve for ( int i = 0 ; i < int( vAllCrv.size()) ; i++) { //se richiesto, la inverto if ( m_Params.m_bInvert) vAllCrv[i]->Invert() ; // verificare casi ottimizzati per OneWay ? // ... // resetto le proprietà per casi ottimizzati ResetCurveAllTempProp( vAllCrv[i]) ; if ( i == 0) { // sposto l'inizio a metà del tratto più lungo AdjustContourStart( vAllCrv[i]) ; } else if ( i < int( vAllCrv.size()) - 1) { // sposto l'inizio della curva più vicina al punto finale precendente Point3d ptS ; vAllCrv[i]->GetStartPoint( ptS) ; // setto i default delle variabili if ( ! DistPointCurve( ptS, *vAllCrv[i + 1]).GetMinDistPoint( EPS_SMALL, ptHelp, nFlag)) return false ; dDist = INFINITO ; for ( int j = i + 1 ; j <= int( vAllCrv.size()) - 1 ; j++) { // cerco il punto più vicino della curva Point3d ptE ; if ( ! DistPointCurve( ptS, *vAllCrv[j]).GetMinDistPoint( EPS_SMALL, ptE, nFlag)) return false ; if ( dDist > Dist(ptS, ptE)) { dDist = Dist( ptS, ptE) ; nClosestInd = j ; ptHelp.Set( ptE.x, ptE.y, ptE.z) ; } } // avendo la curva più vicina ... if ( nClosestInd != i + 1) { PtrOwner ptoCCHelp( GetCurveComposite( Release(vAllCrv[i + 1]))); vAllCrv[i + 1].Set( Release( vAllCrv[nClosestInd])) ; vAllCrv[nClosestInd].Set(Release( ptoCCHelp)) ; } // cambio il suo punto iniziale ... double dU ; Point3d ptNE( ptHelp.x, ptHelp.y, ptHelp.z) ; if ( ! vAllCrv[i + 1]->GetParamAtPoint( ptNE, dU)) return false ; vAllCrv[i + 1]->ChangeStartPoint( dU) ; } // verifico archi VerifyArcs( vAllCrv[i]) ; PtrOwner pOffs( CreateCurveComposite()); if ( IsNull( pOffs) || ! pOffs->AddCurve( vAllCrv[i]->Clone())) { m_pMchMgr->SetLastError( 2413, "Error in Pocketing : Toolpath not computable") ; return false ; } // 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 ENTITA' 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 = dStep ; bool bUnderRaw = m_bAboveHead && !m_bAggrBottom && !m_bTiltingTab && GetPointUnderRaw( ptP1, vtTool, 0, GetRadiusForStartEndElevation(), m_TParams.m_dLen, false, dSafeZ, vtTool, dStElev) ; if ( bUnderRaw) dStElev = max( dStElev, dStep) ; 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, false)) { m_pMchMgr->SetLastError( 2414, "Error in Pocketing : Approach not computable") ; return false ; } // aggiungo attacco SetFeed( GetStartFeed()) ; if ( ! AddLeadIn( ptP1, ptStart, vtStart, vtExtr, pSrfLeanInOut, nullptr, !m_Params.m_bInvert, bSplitArcs)) { m_pMchMgr->SetLastError( 2415, "Error in Pocketing : LeadIn not computable") ; return false; } } // altrimenti solo collegamento else { SetFeed( GetStartFeed()) ; GetCurrPos( ptP1) ; if ( ! AddLeadIn( ptP1, ptStart, vtStart, vtExtr, pSrfLeanInOut, nullptr, !m_Params.m_bInvert, bSplitArcs)) { m_pMchMgr->SetLastError( 2418, "Error in Pocketing : Link not computable") ; return false ; } } } // elaborazioni sulla curva corrente (aggiungo movimenti dell'utensile : LINEAR o ARC) 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 ENTITA' 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 ; } } } } } } // determino il riferimento in base alla svuotatura Frame3d frPocket ; Point3d ptCen ; pSrfIdeal->GetCentroid( ptCen) ; frPocket.Set( ptCen, vtExtr) ; frPocket.Rotate ( ptCen, vtExtr, m_Params.m_dSideAngle) ; pSrfIdeal->ToLoc( frPocket) ; BBox3d b3Pocket ; pSrfIdeal->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 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 ; } // riempio il vettore di segmenti CRVCVECTOR ccClassSeg ; pSrfIdeal->GetCurveClassification( *pLine, EPS_SMALL, ccClassSeg) ; for ( int w = 0 ; w < int( ccClassSeg.size()) ; w++) { if ( ccClassSeg[w].nClass == CRVC_IN) { PtrOwner pCrvSeg( GetCurveLine( pLine->CopyParamRange( ccClassSeg[w].dParS, ccClassSeg[w].dParE))) ; double duF, dLen ; // accorcio leggermente il segmento per non toccare la prima curva di Offset if ( ! pCrvSeg->GetLength( dLen) || dLen < 2 * dExtra || ! pCrvSeg->GetParamAtLength( dLen - dExtra, duF) || ! pCrvSeg->TrimStartAtLen( dExtra) || ! pCrvSeg->TrimEndAtParam( duF)) pCrvSeg.Set( GetCurveLine( pLine->CopyParamRange( ccClassSeg[w].dParS, ccClassSeg[w].dParE))) ; Point3d ptS, ptE ; pCrvSeg->GetStartPoint( ptS) ; pCrvSeg->GetEndPoint( ptE) ; // ricavo la curva su cui si trova il punto iniziale PtrOwner pCrvPtStartOn( CreateCurveComposite()) ; if ( IsNull( pCrvPtStartOn)) return false ; for ( int v = 0; v < pSrfIdeal->GetChunkCount(); v++) { for ( int b = 0; b < pSrfIdeal->GetLoopCount( v); b++) { PtrOwner pCrvTest( GetCurveComposite( pSrfIdeal->GetLoop( v, b))) ; if ( IsNull( pCrvTest)) return false ; if ( pCrvTest->IsPointOn( ptS)) { pCrvPtStartOn.Set( Release( pCrvTest)) ; v = pSrfIdeal->GetChunkCount() ; break ; } } } // INIZIO 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 = j * dStep ; bool bUnderRaw = m_bAboveHead && !m_bAggrBottom && !m_bTiltingTab && GetPointUnderRaw( ptP, vtTool, 0, GetRadiusForStartEndElevation(), m_TParams.m_dLen, false, dSafeZ, vtTool, dStElev); if ( bUnderRaw) dStElev = max( dStElev, j * dStep) ; 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 (forzato ad essere lineare) SetFeed( GetStartFeed()) ; if ( ! AddLeadIn( ptP, ptS, frPocket.VersX(), vtExtr, pSrfLeanInOut, nullptr, !m_Params.m_bInvert, bSplitArcs, true)) { m_pMchMgr->SetLastError( 2415, "Error in Pocketing : LeadIn not computable"); return false ; } ptE.ToGlob( frPocket) ; ptE.Translate( - vtTool * (dDepth - dElev + j * dStep)) ; // movimento al punto finale SetFeed( GetFeed()) ; if ( AddLinearMove( ptE) == GDB_ID_NULL) return false ; // FINE 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; } // se ultimo movimento di ultima area, aggiungo retrazione globale if ( j == nStep && i == nYStep) { 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 ISurfFlatRegion* pSrfPock, const PNTVECTOR vPtStart, const Vector3d& vtTool, const Vector3d& vtExtr, double dDepth, double dElev, double dOkStep, bool bSplitArcs, BOOLVECTOR vbMidOpen, PNTVECTOR vPtMidOpen, VCT3DVECTOR vVtMidOut, int nPathId) { // 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 ; // ciclo sui Chunks for ( int c = 0 ; c < pSrfPock->GetChunkCount() ; c++) { // copio il chunk PtrOwner pSrfChunk( GetSurfFlatRegion( pSrfPock->CloneChunk( c))) ; if ( IsNull( pSrfChunk)) return false ; const int MAX_REGS = 50 ; int nReg = 0 ; // porto il Chunk nel sistema di riferimento locale Point3d ptCen ; pSrfChunk->GetCentroid(ptCen) ; Frame3d frPocket ; frPocket.Set( ptCen, vtExtr) ; pSrfChunk->ToLoc( frPocket) ; while ( nReg < MAX_REGS) { // superifice per ingressi ed uscite PtrOwner pSrfLeanInOut( CloneSurfFlatRegion( pSrfChunk)); // 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; } // se lucidatura con epicicli if ( m_TParams.m_nType == TT_MILL_POLISHING && m_Params.m_dEpicyclesRad > EPS_SMALL) { // verifico che i parametri lucidatura siano sensati if ( m_Params.m_dEpicyclesDist < 100 * EPS_SMALL) { m_pMchMgr->SetLastError( 2413, "Error in Pocketing : Toolpath not computable") ; return false; } // modifico il diametro dell'utensile per tenere conto anche del diametro degli epicicli m_TParams.m_dDiam += 2 * m_Params.m_dEpicyclesRad ; } bool bOptimizedTrap = false ; if ( ! CalcSpiral( pSrfChunk, nReg, vPtStart[c], bSplitArcs, pMCrv, pRCrv, nPathId, bOptimizedTrap)) return false; // se terminate le regioni, esco if ( pMCrv->GetCurveCount() == 0) break ; // passo al chunk originale successivo ++nReg ; // riporto le curve nel sistema di riferimento della svuotatura if ( ! bOptimizedTrap) { pMCrv->ToGlob( frPocket) ; pRCrv->ToGlob( frPocket) ; } if ( m_TParams.m_nType == TT_MILL_POLISHING && m_Params.m_dEpicyclesRad > EPS_SMALL) { // riporto il diametro dell'utensile al valore originale m_TParams.m_dDiam -= 2 * m_Params.m_dEpicyclesRad ; // aggiorno i percorsi di svuotatura con epicicli if ( ! ComputePolishingPath( pMCrv, pRCrv, bSplitArcs)) { m_pMchMgr->SetLastError( 2413, "Error in Pocketing : Toolpath not computable") ; return false ; } } // se prima regione e gestione lato aperto ( caso non ottimizzato) bool bOutStart = ( nReg == 1 && vbMidOpen[c]); if ( bOutStart && !bOptimizedTrap) { // calcolo il punto fuori Point3d ptOut = vPtMidOpen[c] + vVtMidOut[c] * (0.5 * m_TParams.m_dDiam + dSafeZ); // verifico che il punto sia veramente fuori dal grezzo double dStElev; bOutStart = ( ! GetElevation( m_nPhase, ptOut, vtTool, 0.5 * m_TParams.m_dDiam, vtTool, dStElev) || dStElev < EPS_SMALL); if ( bOutStart || m_bOpenOutRaw) { // aggiungo al ritorno l'uscita if (pRCrv->GetCurveCount() == 0) { Point3d ptStart; pMCrv->GetStartPoint(ptStart); pRCrv->AddPoint(ptStart); } pRCrv->AddLine(ptOut, true); // premetto alla spirale la partenza da fuori pMCrv->AddLine(ptOut, false); } } // calcolo gli eventuali punti fuori dal grezzo nel caso ottimizzato int nOutsideRaw = 0; if ( bOptimizedTrap) { AdjustTrapezoidSpiralForLeadInLeadOut( pMCrv, pRCrv, vtTool, dDepth, nOutsideRaw) ; bOutStart = ( nOutsideRaw > 0) ; } // verifico se ingresso da considerare fuori grezzo anche se dentro bool bForcedOutStart = ( vbMidOpen[c] && m_bOpenOutRaw); // se utensile che non lavora di testa e ingresso non fuori dal pezzo, errore if ( m_TParams.m_nType == TT_MILL_NOTIP && !bOutStart && !bForcedOutStart) { if ( ! LeadInIsOk()) { m_pMchMgr->SetLastError( 2431, "Error in Pocketing : LeadIn with Mill NoTip in material") ; 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) { // se sono nel caso ottimizzato e ho attacco e uscita entrambi dentro/fuori dal grezzo, ad ogni step inverto la direzione della curva if ( bOptimizedTrap && nOutsideRaw != 1 && j > 1) pMCrv->Invert() ; // 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 (se non trovata, elevazione è step) double dStElev ; if ( ! GetElevation( m_nPhase, ptStart - 10 * EPS_SMALL * vtTool, vtTool, GetRadiusForStartEndElevation(), vtTool, dStElev)) dStElev = dStep ; bool bUnderRaw = m_bAboveHead && !m_bAggrBottom && !m_bTiltingTab && GetPointUnderRaw( ptP1, vtTool, 0, GetRadiusForStartEndElevation(), m_TParams.m_dLen, false, dSafeZ, vtTool, dStElev); if ( bUnderRaw || ( bOutStart && !m_bAboveHead)) dStElev = max( dStElev, dStep) ; 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, bOutStart || bForcedOutStart)) { m_pMchMgr->SetLastError( 2414, "Error in Pocketing : Approach not computable") ; return false ; } // aggiungo attacco SetFeed( GetStartFeed()) ; if ( ! AddLeadIn( ptP1, ptStart, vtStart, vtExtr, pSrfLeanInOut, pRCrv, !m_Params.m_bInvert, bSplitArcs, bOutStart || bForcedOutStart)) { m_pMchMgr->SetLastError( 2415, "Error in Pocketing : LeadIn not computable") ; return false ; } } // altrimenti solo collegamento else { SetFeed( GetStartFeed()) ; GetCurrPos( ptP1) ; if ( ! AddLeadIn( ptP1, ptStart, vtStart, vtExtr, pSrfLeanInOut, pRCrv, !m_Params.m_bInvert, bSplitArcs, bOutStart || bForcedOutStart)) { 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 ; } if ( !m_bAboveHead) dEndElev = max( dEndElev, j * dStep) ; // 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 ISurfFlatRegion* pSrfPock, const PNTVECTOR vPtStart, const Vector3d& vtTool, const Vector3d& vtExtr, double dDepth, double dElev, double dOkStep, bool bSplitArcs, int nPathId) { // 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 ; // ciclo sulle regioni for ( int c = 0 ; c < pSrfPock->GetChunkCount() ; c++) { // copio il chunk PtrOwner pSrfChunk( GetSurfFlatRegion( pSrfPock->CloneChunk( c))) ; if ( IsNull( pSrfChunk)) return false ; // porto il Chunk nel sistema di riferimento locale Point3d ptCen ; pSrfChunk->GetCentroid( ptCen) ; Frame3d frPocket ; frPocket.Set( ptCen, vtExtr) ; pSrfChunk->ToLoc( frPocket) ; int nReg = 0; int MAX_REGS = 50 ; while ( nReg < MAX_REGS) { // superificie per ingressi ed uscite PtrOwner pSrfLeanInOut(CloneSurfFlatRegion(pSrfChunk)); // 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 ; } // se lucidatura con epicicli if ( m_TParams.m_nType == TT_MILL_POLISHING && m_Params.m_dEpicyclesRad > EPS_SMALL) { // verifico che parametri lucidatura siano sensati if ( m_Params.m_dEpicyclesDist < 100 * EPS_SMALL) { m_pMchMgr->SetLastError(2413, "Error in Pocketing : Toolpath not computable") ; return false ; } // modifico il diametro dell'utensile per tenere conto anche del raggio degli epicicli m_TParams.m_dDiam += 2 * m_Params.m_dEpicyclesRad ; } bool bOptimizedTrap = false; if ( ! CalcSpiral( pSrfChunk, nReg, vPtStart[c], bSplitArcs, pMCrv, pRCrv, nPathId, bOptimizedTrap)) return false ; // se terminate le regioni, esco if ( pMCrv->GetCurveCount() == 0) break ; ++nReg ; // riporto le curve nel sistema di riferimento della svuotatura pMCrv->ToGlob( frPocket) ; pRCrv->ToGlob( frPocket) ; if ( m_TParams.m_nType == TT_MILL_POLISHING && m_Params.m_dEpicyclesRad > EPS_SMALL) { // riporto il diametro dell'utensile al valore originale m_TParams.m_dDiam -= 2 * m_Params.m_dEpicyclesRad ; // aggiorno i percorsi di svuotatura con epicicli if (!ComputePolishingPath( pMCrv, pRCrv, bSplitArcs)) { m_pMchMgr->SetLastError( 2413, "Error in Pocketing : Toolpath not computable") ; return false ; } } // nel caso ottimizzato verifico se posso entrare e uscire fuori dal grezzo bool bOutStart = false ; int nOutsideRaw = 0 ; if ( bOptimizedTrap) { AdjustTrapezoidSpiralForLeadInLeadOut( pMCrv, pRCrv, vtTool, dDepth, nOutsideRaw) ; bOutStart = ( nOutsideRaw > 0) ; } // se utensile che non lavora di testa e ingresso non fuori dal pezzo, errore if ( m_TParams.m_nType == TT_MILL_NOTIP && !bOutStart) { if ( ! LeadInIsOk()) { m_pMchMgr->SetLastError( 2431, "Error in Pocketing : LeadIn with Mill NoTip in material") ; return false ; } } // inverto i percorsi, perchè sono calcolati dall'esterno all'interno (solo nel caso non ottimizzato) if ( ! bOptimizedTrap) { 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) { // se sono nel caso ottimizzato e ho attacco e uscita entrambi dentro/fuori dal grezzo, ad ogni step inverto la direzione della curva if ( bOptimizedTrap && nOutsideRaw != 1 && j > 1) pMCrv->Invert() ; // 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 = dStep ; bool bUnderRaw = m_bAboveHead && !m_bAggrBottom && !m_bTiltingTab && GetPointUnderRaw( ptP1, vtTool, 0, GetRadiusForStartEndElevation(), m_TParams.m_dLen, false, dSafeZ, vtTool, dStElev); if ( bUnderRaw || ( bOutStart && !m_bAboveHead)) dStElev = max( dStElev, dStep); 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, false)) { m_pMchMgr->SetLastError( 2414, "Error in Pocketing : Approach not computable") ; return false ; } // aggiungo attacco SetFeed( GetStartFeed()) ; if ( ! AddLeadIn( ptP1, ptStart, vtStart, vtExtr, pSrfLeanInOut, pRCrv, m_Params.m_bInvert, bSplitArcs, bOutStart)) { m_pMchMgr->SetLastError( 2415, "Error in Pocketing : LeadIn not computable") ; return false ; } } // altrimenti solo collegamento else { SetFeed( GetStartFeed()) ; GetCurrPos( ptP1) ; if ( ! AddLeadIn( ptP1, ptStart, vtStart, vtExtr, pSrfLeanInOut, pRCrv, m_Params.m_bInvert, bSplitArcs, bOutStart)) { 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 ; } if ( !m_bAboveHead) dEndElev = max( dEndElev, j * dStep) ; // 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 ISurfFlatRegion* pSrfPock, int nReg, Point3d ptStart, bool bSplitArcs, ICurveComposite* pMCrv, ICurveComposite* pRCrv, int nPathId, bool& bOptimizedTrap) { // inizializzo i risultati pMCrv->Clear() ; pRCrv->Clear() ; // primo offset pari al raggio utensile + sovramateriale double dTRad = 0.5 * m_TParams.m_dDiam ; double dOffs = dTRad + GetOffsR() ; // recupero il versore normale della superificie, ruotandola nel piano XY PtrOwner pSrfToWork( pSrfPock->Clone()) ; if ( IsNull( pSrfToWork) || pSrfToWork->GetChunkCount() == 0) return false; // controllo se ho isole per i casi ottimizzati bool bHasIsland = false ; for ( int j = 1 ; j < pSrfPock->GetLoopCount( 0) ; j++) { PtrOwner pCrvBorder( GetCurveComposite( pSrfPock->GetLoop( 0, j))) ; if( ! IsNull( pCrvBorder)) bHasIsland = true ; } // se non ho isole allora controllo casi ottimizzati if ( ! bHasIsland) { // caso spirale PtrOwner pCrvBorder( CreateCurveComposite()) ; pCrvBorder->AddCurve( pSrfPock->GetLoop( 0, 0)->Clone()) ; Point3d ptCen ; Vector3d vtN ; double dRad ; bool bCCW ; if ( pCrvBorder->IsACircle( 100 * 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) ; } // caso trapezoide int nCrvId = pSrfPock->GetLoop( 0, 0)->GetTempProp( 0) ; PtrOwner pCrvPocket( CloneCurveComposite( m_pGeomDB->GetGeoObj( nCrvId))) ; if ( IsNull( pCrvPocket)) return true ; SetCurveAllTempProp( nCrvId, pCrvPocket) ; pCrvPocket->MergeCurves( 10 * EPS_SMALL, 10 * EPS_ANG_SMALL) ; Point3d pt ; Vector3d vtB1, vtL1, vtB2 ; if ( pCrvPocket->IsATrapezoid( 100 * EPS_SMALL, pt, vtB1, vtL1, vtB2)) { Vector3d vtDir( vtB1), vtOtherDir( vtL1) ; // se parallelogramma scelgo come base i lati lunghi Vector3d vtL2( - vtB1 + vtL1 + vtB2) ; if ( AreSameOrOppositeVectorApprox( vtL1, vtL2)) { if ( vtL1.Len() > vtB1.Len()) swap( vtDir, vtOtherDir) ; } vtDir.Normalize(); Vector3d vtOrtho = OrthoCompo( vtOtherDir, vtDir); double dPocketSize = vtOrtho.Len() ; if ( dPocketSize < m_TParams.m_dDiam + EPS_SMALL) { CalcTrapezoidSpiral( pCrvPocket, vtDir, dPocketSize, pMCrv, pRCrv, bOptimizedTrap); ModifyCurveToSmooted( pCrvPocket, 0.002, 0.002) ; if ( bOptimizedTrap) return true ; } } } // ciclo di offset verso l'interno const int MAX_ITER = 1000 ; int nIter = 0 ; ICRVCOMPOPOVECTOR vOffs ; // vettore delle curve di offset ICRVCOMPOPOVECTOR vOffsFirstCurve ; // curve di primo offset PtrOwner pSrfAct( CloneSurfFlatRegion( pSrfToWork)) ; // regione attuale PtrOwner pSrfPrec( CloneSurfFlatRegion( pSrfToWork)) ; // regione precedente if( IsNull( pSrfAct) || IsNull( pSrfPrec) || pSrfAct->GetChunkCount() == 0 || pSrfPrec->GetChunkCount() == 0) return false ; PtrOwner pCutRegion( CreateSurfFlatRegion()) ; // regione lavorata dall'utensile while ( nIter < MAX_ITER) { // salvo la regione pSrfPrec.Set( pSrfAct->Clone()) ; // calcolo offset della flatRegion if ( ! pSrfAct->Offset( - dOffs, ICurve::OFF_FILLET)) return false ; // estraggo il contorno della FlatRegion (ciclo i Chunks e prendo i rispettivi Loops) if ( nIter == 0) { PtrOwner pSrfChunknReg( pSrfAct->CloneChunk( nReg)) ; if ( IsNull( pSrfChunknReg)) // se supero i chunk ottenuti return true ; pSrfAct.Set( Release( pSrfChunknReg)) ; } int nChunks = pSrfAct->GetChunkCount(); for ( int i = 0 ; i < nChunks; i++ ) { // per ogni chunk... int nLoops = pSrfAct->GetLoopCount( i) ; for ( int j = 0 ; j < nLoops ; j++ ) { // per ogni loop... PtrOwner ptoCCBorder( GetCurveComposite( pSrfAct->GetLoop( i, j))) ; if ( j > 0 ) // inverto l'orientamento delle curve interne (offset delle isole trovate) ptoCCBorder->Invert() ; // controllo quali regioni di Offset possono essere sostituite bool bInsert = true ; ICurveArc* pCrvToolShape( CreateCurveArc()) ; // superifice di ingombro del tool if ( pCrvToolShape == nullptr) return false ; // prendo il centroide dell'Offset Point3d ptC ; ptoCCBorder->GetCentroid( ptC) ; // creo la curva che rappresenta il mio Tool centrata nel centroide dell'Offset pCrvToolShape->SetXY( ptC, m_TParams.m_dDiam / 2 + 5 * EPS_SMALL) ; // guardo se l'Offset è contenuto nella curva del Tool CRVCVECTOR ccClass ; IntersCurveCurve intCC( *ptoCCBorder, *pCrvToolShape) ; intCC.GetCurveClassification( 0, EPS_SMALL, ccClass) ; if ( ccClass.size() == 1 && ccClass[0].nClass == CRVC_IN && vOffs.size() > 0) bInsert = false ; if( bInsert) vOffs.emplace_back( Release( ptoCCBorder)) ; PtrOwner ptoCCExtBorder( GetCurveComposite( pSrfAct->GetLoop( i, j))) ; if ( nIter == 0 ) { // salvo il bordo per i link (non invertiti) vOffsFirstCurve.emplace_back( Release( ptoCCExtBorder)) ; } } } bool bSmallRad = ( nIter == 0 ? dOffs < dTRad + GetOffsR() + EPS_ZERO : dOffs < dTRad + EPS_ZERO) ; if ( nChunks > 0) { dOffs = GetSideStep() ; } else if ( ! bSmallRad) { pSrfAct.Set( Release( pSrfPrec)) ; dOffs = ( nIter == 0 ? dTRad + GetOffsR() : dTRad ) ; } else break ; ++ nIter ; } // se non ho trovato curve di Offset allora esco if ( vOffs.size() == 0) return true ; // cambio il punto iniziale della prima Curva di Offset Point3d ptNewStart ; if ( vOffs[0]->IsPointOn( ptStart)) { double duS ; vOffs[0]->GetParamAtPoint( ptStart, duS) ; vOffs[0]->ChangeStartPoint( duS) ; } else { // se non dovesse essere sulla curva, allora cerco il più vicino int nFlag ; if ( DistPointCurve( ptStart, *vOffs[0]).GetMinDistPoint( EPS_SMALL, ptNewStart, nFlag)) { double duS ; vOffs[0]->GetParamAtPoint( ptNewStart, duS) ; vOffs[0]->ChangeStartPoint( duS) ; } } // smusso le curve di offset ( ad eccezione della prima) ICRVCOMPOPOVECTOR vOffsClosedCurves( vOffs.size()) ; // vettore con tutte le curve di Offset Chiuse for ( int i = 0; i < int( vOffs.size()) ; i++ ) { if( i != 0) ModifyCurveToSmooted( vOffs[i], 0.01, 0.01) ; vOffs[i]->MergeCurves( 10 * EPS_SMALL, 10 * EPS_ANG_SMALL) ; vOffsClosedCurves[i].Set( vOffs[i]->Clone()) ; } for ( int i = 0 ; i < int( vOffsFirstCurve.size()) ; i++ ) { if( i != 0 ) ModifyCurveToSmooted( vOffsFirstCurve[i], 0.01, 0.01) ; vOffsFirstCurve[i]->MergeCurves( 10 * EPS_SMALL, 10 * EPS_ANG_SMALL) ; } // setto il punto iniziale della svuotatura double dNewUS ; vOffs[0]->GetParamAtPoint( ptNewStart, dNewUS) ; vOffs[0]->ChangeStartPoint( dNewUS) ; // riordino le curve cambiando il loro punto di inizio e creando poi i collegamenti int nClosestInd = -1 ; int nFlag ; int nFocus = 0 ; double dDist = INFINITO ; Point3d ptHelp ; ICURVEPOVECTOR vLinks( vOffs.size()) ; for ( int i = 0 ; i < int( vOffs.size()) - 1 ; i++) { Point3d ptS ; if ( ! vOffs[i]->GetStartPoint( ptS)) return false ; // setto i default delle variabili if ( ! DistPointCurve( ptS, *vOffs[i+1]).GetMinDistPoint( EPS_SMALL, ptHelp, nFlag)) return false ; dDist = INFINITO ; for ( int j = i + 1 ; j <= int( vOffs.size()) - 1 ; j++) { // cerco il punto più vicino della curva Point3d ptE ; if ( ! DistPointCurve( ptS, *vOffs[j]).GetMinDistPoint( EPS_SMALL, ptE, nFlag)) return false ; if ( dDist > Dist( ptS, ptE) ) { dDist = Dist( ptS, ptE) ; nClosestInd = j ; ptHelp.Set( ptE.x, ptE.y, ptE.z) ; } } // avendo la curva più vicina ... // 1) scambio la curva i con la curva nClosestInd if ( nClosestInd != i + 1) { PtrOwner ptoCCHelp( GetCurveComposite( Release( vOffs[i + 1]))) ; vOffs[i + 1].Set( Release( vOffs[nClosestInd])) ; vOffs[nClosestInd].Set( Release( ptoCCHelp)) ; } // 2) cambio il suo punto iniziale ... double dU ; Point3d ptNE( ptHelp.x, ptHelp.y, ptHelp.z) ; if ( ! vOffs[i + 1]->GetParamAtPoint( ptNE, dU)) return false ; vOffs[i + 1]->ChangeStartPoint( dU) ; // 2.1) Accorcio la curva per velocizzare ... if( i == 0 ) { PtrOwner pCrvExtraOff0( CreateCurveComposite()) ; if ( IsNull( pCrvExtraOff0)) return false ; double dUExtra0 ; vOffs[0]->GetParamAtLength( m_TParams.m_dDiam/4, dUExtra0) ; pCrvExtraOff0->AddCurve( GetCurve( vOffs[0]->CopyParamRange( 0, dUExtra0))) ; //vOffs[0]->AddCurve( Release( pCrvExtraOff0)) ; PtrOwner pCrvExtraOff1( CreateCurveComposite()) ; if ( IsNull( pCrvExtraOff1)) return false ; double dUExtra1, dLen1, duS, duE ; if( vOffs.size() > 0) { vOffs[1]->GetLength( dLen1) ; if ( dLen1 > m_TParams.m_dDiam/4) { vOffs[1]->GetParamAtLength( dLen1 - m_TParams.m_dDiam / 4, dUExtra1) ; vOffs[1]->GetDomain( duS, duE) ; pCrvExtraOff1->AddCurve( GetCurve( vOffs[1]->CopyParamRange( dUExtra1, duE))) ; } PtrOwner pCrvLink( CreateCurveComposite()) ; Vector3d vS, vE ; if ( ! pCrvExtraOff0->GetEndDir( vS) || ! pCrvExtraOff0->GetEndPoint( ptS) || ! pCrvExtraOff1->GetStartDir( vE) || ! pCrvExtraOff1->GetStartPoint( ptNE)) return false ; if ( CalcBoundedLink( ptS, ptNE, vOffsFirstCurve, pCrvLink)) { PtrOwner pCrvFirstLink( Release( pCrvExtraOff0)) ; pCrvFirstLink->AddCurve( Release( pCrvLink)) ; pCrvFirstLink->AddCurve( Release( pCrvExtraOff1)) ; ModifyCurveToSmooted( pCrvFirstLink, 0.025, 0.025) ; vLinks[1].Set( Release( pCrvFirstLink)) ; } } else { m_pMchMgr->SetLastError( 2413, "Error in Pocketing : Toolpath not computable") ; return false ; } } else { double dUNS, dUNE ; PtrOwner pCrvTest( CreateCurveComposite()) ; PtrOwner pOff_i0(CloneCurveComposite( vOffs[i])) ; PtrOwner pOff_i1(CloneCurveComposite( vOffs[i + 1])) ; if ( ! CutCurveToConnect( vOffs[i], vOffs[i+1], vOffsClosedCurves, vOffsFirstCurve, pCrvTest)) { PtrOwner pCrvLink( CreateCurveComposite()) ; Vector3d vS, vE ; vOffs[i].Set( Release( pOff_i0)) ; vOffs[i+1].Set( Release(pOff_i1)) ; if ( ! vOffs[i]->GetStartDir( vS) || ! vOffs[i+1]->GetStartDir( vE)) return false ; if ( CalcBoundedSmootedLink( ptS, vS, ptNE, vE, 0.5, vOffsFirstCurve, pCrvLink)) vLinks[i + 1].Set( Release( pCrvLink)) ; else { m_pMchMgr->SetLastError( 2413, "Error in Pocketing : Toolpath not computable") ; return false ; } continue ; } if ( ! pCrvTest->GetStartPoint( ptS) || ! pCrvTest->GetEndPoint( ptNE) || ! vOffs[i]->GetParamAtPoint( ptS, dUNS) || ! vOffs[i+1]->GetParamAtPoint( ptNE, dUNE) ) return false ; vOffs[i+1]->ChangeStartPoint( dUNE) ; PtrOwner pCrvNewOffS( GetCurveComposite( vOffs[i]->CopyParamRange( 0, dUNS))) ; vOffs[i]->Clear() ; vOffs[i].Set( Release( pCrvNewOffS)) ; vLinks[i + 1].Set( Release( pCrvTest)) ; } } // 3) controllo eventuali parti non svuotate... PtrOwner pSrfToCut( CreateSurfFlatRegion()) ; if ( GetUnclearedRegion( vOffsFirstCurve, vOffs, vLinks, pSrfToCut)) { // 4) Modifico i percorsi if ( ! RemoveExtraParts( pSrfToCut, vOffs, vOffsClosedCurves, vOffsFirstCurve, vLinks)) { return false ; } } // calcolo il percorso di ritorno if ( vOffs.size() >= 2) { pRCrv->Clear() ; // punto inziale e finale | vettore iniziale e finale Point3d ptStart ; vOffs.back()->GetEndPoint( ptStart) ; Point3d ptEnd ; vOffs.front()->GetStartPoint( ptEnd) ; Vector3d vtStart ; vOffs.back()->GetEndDir( vtStart) ; Vector3d vtEnd ; vOffs.front()->GetStartDir( vtEnd) ; // calcolo il ritorno (garantendo che non esca dalla svuotatura) PtrOwner pCrvLink( CreateCurveComposite()) ; if ( CalcBoundedSmootedLink( ptStart, vtStart, ptEnd, vtEnd, 0.5, vOffsFirstCurve, pCrvLink)) { pRCrv->AddCurve( Release( pCrvLink)) ; pRCrv->MergeCurves( 10 * EPS_SMALL, 10 * EPS_ANG_SMALL, false) ; // se necessario, approssimo archi con rette if ( bSplitArcs && ! ApproxWithLines( pRCrv)) { m_pMchMgr->SetLastError( 2421, "Error in Pocketing : Linear Approx not computable") ; return false ; } VerifyArcs( pRCrv) ; } else { m_pMchMgr->SetLastError( 2413, "Error in Pocketing : Toolpath not computable") ; return false ; } } // creo il percorso di lavoro a partire dalla raccolta degli offset e dei collegamenti for ( int i = 0 ; i < int( vOffs.size()) ; ++ i) { // se collegamento da aggiungere if ( ! IsNull( vLinks[i])) { int nCrvsCount0 = pMCrv->GetCurveCount() ; // accodo nel percorso di lavorazione if ( ! pMCrv->AddCurve( vLinks[i]->Clone())) return false ; // nel caso di lucidatura setto proprietà alle curve di collegamento per poterle identificare if ( m_TParams.m_nType == TT_MILL_POLISHING) { for ( int j = nCrvsCount0 ; j < pMCrv->GetCurveCount() ; j ++) pMCrv->SetCurveTempProp( j, LINK_CURVE_PROP) ; } } // se richiesta percorrenza invertita if ( m_Params.m_bInvert) vOffs[i]->Invert() ; // aggiungo la curva pMCrv->AddCurve( vOffs[i]->Clone()) ; } // 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) ; // setto estrusione Vector3d vtExtr ; if( vOffs.size() > 0) if ( vOffs[0]->GetExtrusion( vtExtr)) pMCrv->SetExtrusion( vtExtr) ; return true ; } //---------------------------------------------------------------------------- bool Pocketing::ModifyCurveToSmooted( ICurveComposite* pCrv, double dRightPer, double dLeftPer) { // controllo parametri if ( pCrv == nullptr) return false ; ICURVEPOVECTOR vCrvStepsToFill; // vettore delle Curve da raccordare (curve della composita) ICURVEPOVECTOR vCrvArcs ; // vettore contenente gli Archi (tra le curve della composita) INTVECTOR vArcsToJump ; // vettore di indici per gli archi saltati (nel caso non si riesca a raccordare ...) // se nella composita ho meno di due curve allora non c'è nulla da raccordare const ICurve* pMyCrv = pCrv->GetFirstCurve() ; while ( pMyCrv != nullptr) { vCrvStepsToFill.emplace_back( pMyCrv->Clone()) ; pMyCrv = pCrv->GetNextCurve() ; } if ( vCrvStepsToFill.size() < 2) return false ; // controllo se la curva è chiusa (nel caso inserisco nel vettore la prima curva due volte, all'inizio e alla fine) if ( pCrv->IsClosed()) { const ICurve* pMyCrvFirst = pCrv->GetFirstCurve() ; vCrvStepsToFill.emplace_back( pMyCrvFirst->Clone()) ; } double dUE_ref, dUS_ref, dRadius, dPar1, dPar2 ; // riempio il vettore degli archi, scorro tutte le curve da raccordare ( la i-esima e la (i+1)-esima ) for ( int i = 0; i < int( vCrvStepsToFill.size()) - 1 ; i++) { // controllo che le curve non siano già tangenti Vector3d vtS, vtE ; if ( ! vCrvStepsToFill[i]->GetEndDir( vtS) || ! vCrvStepsToFill[i+1]->GetStartDir( vtE)) continue ; if ( AreSameVectorApprox( vtS, vtE)) { vCrvArcs.emplace_back( CreateCurveComposite()) ; // arco nullo vArcsToJump.push_back( i) ; // da scartare continue ; } // cerco i paramentri al dRightPer% e dLeftPer% della lunghezza della prima e della seconda curva ( per raccordarli ) double dU_cm_S = 0 ; double dULast1 = 1; double dULast2 = 1; vCrvStepsToFill[i]->GetDomain( dU_cm_S, dULast1) ; dUE_ref = ( 1 - dRightPer) * dULast1 ; vCrvStepsToFill[i+1]->GetDomain( dU_cm_S, dULast2) ; dUS_ref = dLeftPer * dULast2 ; // prendo i punti sulle due curve rispetto a tali parametri Point3d ptS, ptE ; if ( ! vCrvStepsToFill[i]->GetPointD1D2( dUE_ref, ICurve::FROM_PLUS, ptS) || ! vCrvStepsToFill[i+1]->GetPointD1D2( dUS_ref, ICurve::FROM_MINUS, ptE)) return false ; dRadius = Dist( ptS, ptE) ; // uso come raggio la distanza tra i due punti int nMaxTestForArcs = 3 ; // tentativi per creare l'arco int nIterForArcs = 0 ; bool IntersBTWArcs = false ; // creo l'arco di raccordo PtrOwner pCrvArc( CreateFillet( *vCrvStepsToFill[i]->Clone(), ptS, *vCrvStepsToFill[i+1]->Clone(), ptE, Z_AX, dRadius, dPar1, dPar2)) ; // controllo che l'arco creato non sia troppo piccolo double dArcLen ; if ( pCrvArc != nullptr && (! pCrvArc->GetLength( dArcLen) || dArcLen < 5 * EPS_SMALL)) { vCrvArcs.emplace_back( Release( pCrvArc)) ; // arco nullo vArcsToJump.push_back( i) ; // da scartare continue ; } if ( i != 0 && vArcsToJump[i-1] == -1 && pCrvArc != nullptr) { // dal secondo arco in poi controllo che non ci siano intersezioni tra essi IntersCurveCurve intCCH( *pCrvArc, *vCrvArcs[i-1]) ; if( intCCH.GetIntersCount() > 0 ) IntersBTWArcs = true ; } // se ho intersezioni tra archi o l'arco creato non è valido, allora provo altre nMaxTestForArcs volte a ricrearlo avvicinando i punti while (( pCrvArc == nullptr || IntersBTWArcs) && nIterForArcs < nMaxTestForArcs) { dUE_ref = ( dULast1 + dUE_ref ) * 0.5 ; dUS_ref = dUS_ref * 0.5 ; if ( ! vCrvStepsToFill[i]->GetPointD1D2( dUE_ref, ICurve::FROM_PLUS, ptS) || ! vCrvStepsToFill[i+1]->GetPointD1D2( dUS_ref, ICurve::FROM_MINUS, ptE)) return false ; dRadius = Dist( ptS, ptE) ; pCrvArc.Set( CreateFillet( *vCrvStepsToFill[i]->Clone(), ptS, *vCrvStepsToFill[i+1]->Clone(), ptE, Z_AX, dRadius, dPar1, dPar2)) ; nIterForArcs++ ; IntersBTWArcs = false ; if ( i != 0 && vArcsToJump[i-1] == -1 && pCrvArc != nullptr) { // dal secondo arco in poi controllo che non ci siano intersezioni tra essi IntersCurveCurve intCCH( *pCrvArc, *vCrvArcs[i-1]) ; if ( intCCH.GetIntersCount() > 0) IntersBTWArcs = true ; } } if ( pCrvArc == nullptr) { // se ancora non riesco... salto l'arco vCrvArcs.emplace_back( Release( pCrvArc)) ; // arco nullo vArcsToJump.push_back( i) ; // da scartare continue ; } // se ho creato l'arco lo memorizzo vCrvArcs.emplace_back( Release( pCrvArc)) ; // arco valido vArcsToJump.push_back( -1) ; // da considerare } // creo la curva che restituirò PtrOwner pCrvCO_temp( CreateCurveComposite()) ; if ( pCrvCO_temp == nullptr) return false ; Point3d ptArcHelp, ptFirstPoint ; // unisco la curva i-esima con l'arco i-esimo (non guardo l'ultima curva nel vettore, controllo dopo il caso di curva chiusa) for ( int i = 0 ; i < int( vCrvStepsToFill.size()) - 1 ; i++) { if ( vArcsToJump[i] == -1) { // se esiste l'arco ... Point3d ptArcS, ptArcE ; if ( ! vCrvArcs[i]->GetStartPoint( ptArcS) || ! vCrvArcs[i]->GetEndPoint( ptArcE) || ! vCrvStepsToFill[i]->GetParamAtPoint( ptArcS, dUE_ref) || ! vCrvStepsToFill[i+1]->GetParamAtPoint( ptArcE, dUS_ref)) return false ; if ( i == 0) { // ... e sono nella prima iterazione ... if ( ! pCrv->IsClosed()) { // ... e se la curva è aperta -> la inserisco nel nuovo percorso if ( ! pCrvCO_temp->AddCurve( GetCurve( vCrvStepsToFill[0]->CopyParamRange( 0, dUE_ref)))) return false ; } ptFirstPoint = ptArcS ; // ... e se la curva è chiusa -> non la inserisco nel nuovo percorso } else { // ... e non sono nella prima iterazione (non ha importanza se la curva è chiusa o aperta...) if ( ! vCrvStepsToFill[i]->GetParamAtPoint( ptArcHelp, dUS_ref) || ! pCrvCO_temp->AddCurve( GetCurve( vCrvStepsToFill[i]->CopyParamRange( dUS_ref, dUE_ref)))) return false ; // aggiungo la curva 'tagliata per il raccordo' } if ( ! pCrvCO_temp->AddCurve( vCrvArcs[i]->Clone())) // aggiungo l'arco di raccordo return false ; ptArcHelp = ptArcE ; } else { // se non esiste l'arco ... if ( i == 0 ) { // e sono nella prima iterazione... if ( ! vCrvStepsToFill[0]->GetEndPoint( ptArcHelp)) return false ; if ( ! pCrv->IsClosed()) { // ...e se aperta // aggiungo la prima curva per intero if ( ! vCrvStepsToFill[0]->GetParamAtPoint( ptArcHelp, dUE_ref) || ! pCrvCO_temp->AddCurve( GetCurve( vCrvStepsToFill[0]->CopyParamRange( 0, dUE_ref)))) return false ; } ptFirstPoint = ptArcHelp ; } else { // ... e non sono nella prima iterazione (non ha importanza se la curva è chiusa o aperta...) double dUS_cm ; if ( ! vCrvStepsToFill[i]->GetParamAtPoint( ptArcHelp, dUS_ref) || ! vCrvStepsToFill[i]->GetDomain( dUS_cm, dUE_ref) || ! pCrvCO_temp->AddCurve( GetCurve( vCrvStepsToFill[i]->CopyParamRange( dUS_ref, dUE_ref))) || ! vCrvStepsToFill[i]->GetEndPoint( ptArcHelp)) return false ; } } } // ultima curva... if ( pCrv->IsClosed()) { // se curva chiusa... if ( ! vCrvStepsToFill[0]->GetParamAtPoint( ptArcHelp, dUS_ref) || ! vCrvStepsToFill[0]->GetParamAtPoint( ptFirstPoint, dUE_ref) || ! pCrvCO_temp->AddCurve( GetCurve( vCrvStepsToFill[0]->CopyParamRange( dUS_ref, dUE_ref)))) return false ; } else { // se curva aperta... ( non ha importanza l'esistenza o meno degli archi...) double dUS_cm ; if ( ! vCrvStepsToFill[vCrvStepsToFill.size() - 1]->GetParamAtPoint( ptArcHelp, dUS_ref) || ! vCrvStepsToFill[vCrvStepsToFill.size() - 1]->GetDomain( dUS_cm, dUE_ref) || ! pCrvCO_temp->AddCurve( GetCurve( vCrvStepsToFill[vCrvStepsToFill.size() - 1]->CopyParamRange( dUS_ref, dUE_ref)))) return false ; } // ripristino il punto inziale se la curva è chiusa double dNewDU ; if ( ! pCrv->IsClosed()) { if( ! pCrvCO_temp->GetParamAtPoint( ptArcHelp, dNewDU)) return false ; pCrvCO_temp->ChangeStartPoint( dNewDU) ; } pCrv->Clear() ; pCrv->AddCurve( Release( pCrvCO_temp)) ; return true ; } //---------------------------------------------------------------------------- bool Pocketing::CutCurveToConnect( ICurveComposite* pCrvS, ICurveComposite* pCrvE, ICRVCOMPOPOVECTOR& vOffsCL, ICRVCOMPOPOVECTOR& vFirstOffset, ICurveComposite* pCrvLink, double dLenPercS, double dLenPercE, int nMaxIter) { // controllo i parametri if ( pCrvS == nullptr || pCrvE == nullptr ) return false ; pCrvLink->Clear() ; PtrOwner ptCrvFinal( CreateCurveComposite()) ; if ( ptCrvFinal == nullptr) return false ; // Prendo i punti, i vettori tangenti e i parametri di essi per le curve Point3d ptSS, ptSE, ptES, ptEE ; Vector3d vS, vE; double dUSS, dUSE, dUES, dUEE, dLenS, dLenE ; dUSS = 0; dUSE = 0; pCrvS->GetEndPoint( ptSE) ; pCrvS->GetStartPoint( ptSS); pCrvE->GetStartPoint( ptES); pCrvE->GetEndPoint( ptEE) ; pCrvS->GetEndDir( vS) ; pCrvE->GetStartDir( vE) ; pCrvS->GetDomain( dUSS, dUSE) ; pCrvE->GetDomain( dUES, dUEE) ; pCrvS->GetLength( dLenS) ; pCrvE->GetLength( dLenE) ; // se ho una curva di primo Offset allora non devo accorciarla ... bool bCut = true ; for ( int i = 0 ; i < int( vFirstOffset.size()) ; i++ ) { if ( vFirstOffset[i]->IsPointOn( ptSS) && vFirstOffset[i]->IsPointOn( ptSE)) dLenPercS = 0 ; if ( vFirstOffset[i]->IsPointOn( ptES) && vFirstOffset[i]->IsPointOn( ptEE)) dLenPercE = 0 ; } double dLStepS = dLenPercS * dLenS ; double dLStepE = dLenPercE * dLenE ; dLenE = 0 ; // calcolo i possibili BiArchi tra le due curve int nIter = 0; while ( nIter < nMaxIter) { // calcolo il BiArco PtrOwner ptBiArc( CreateCurveComposite()) ; if ( ! CalcBoundedSmootedLink( ptSE, vS, ptES, vE, 0.5, vFirstOffset, ptBiArc) || ptBiArc->GetCurveCount() == 0) return false ; ptCrvFinal->Clear() ; ptCrvFinal.Set( ptBiArc->Clone()) ; if ( dLenPercE == 0 && dLenPercS == 0 ) break ; // se il BiArco creato interseca le altre curve di Offset allora mi fermo... bool bInterr = false ; for ( int i = 0 ; i < int( vOffsCL.size()) && ! bInterr ; i++) { if ( vOffsCL[i]->IsPointOn( ptSE) || vOffsCL[i]->IsPointOn( ptES)) continue ; CRVCVECTOR ccClass ; IntersCurveCurve intCC( *ptBiArc, *vOffsCL[i]) ; intCC.GetCurveClassification( 0, EPS_SMALL, ccClass) ; if ( ccClass.size() > 1) // se intersezione bInterr = true ; } if ( bInterr) break ; // così come ultimo arco ho quello precedente (valido) // se ho trovato un BiArco valido, allora accorcio le due curve di Offset dLenS -= dLStepS ; dLenE += dLStepE ; if ( ! pCrvS->GetParamAtLength( dLenS, dUSE) || ! pCrvS->GetPointD1D2( dUSE, ICurve::FROM_MINUS, ptSE) || ! pCrvE->GetParamAtLength( dLenE, dUES) || ! pCrvE->GetPointD1D2( dUES, ICurve::FROM_PLUS, ptES)) return false ; ICurveComposite* pCrvS_clone( pCrvS->Clone()) ; // curva di Offset iniziale accorciata ICurveComposite* pCrvE_clone( pCrvE->Clone()) ; // curva di Offset finale accorciata pCrvS_clone->ChangeStartPoint( dUSE) ; pCrvE_clone->ChangeStartPoint( dUES) ; if ( ! pCrvS_clone->GetStartDir( vS) || ! pCrvE_clone->GetStartDir( vE)) return false ; nIter++ ; } pCrvLink->AddCurve( ptCrvFinal->Clone()) ; // ultimo arco valido trovato return true ; } //---------------------------------------------------------------------------- bool Pocketing::RemoveExtraParts( ISurfFlatRegion* pSrfToCut, ICRVCOMPOPOVECTOR& vOffs, ICRVCOMPOPOVECTOR& vOffsClosedCurves, ICRVCOMPOPOVECTOR& vOffsFirstCurve, ICURVEPOVECTOR& vLinks) { if ( pSrfToCut == nullptr || vOffs.size() == 0) return true ; // ciclo tutti i chunk della regione da tagliare for ( int i = 0 ; i < pSrfToCut->GetChunkCount() ; i++ ) { // prendo il bordo del chunk PtrOwner pCrvChunkBorder( pSrfToCut->GetLoop( i, 0)) ; if ( IsNull( pCrvChunkBorder)) return false ; // 1) regione i-esima da rimuovere PtrOwner pSrfChunkToCut( CreateSurfFlatRegion()) ; if ( IsNull( pSrfChunkToCut)) return false ; if ( ! pSrfChunkToCut->AddExtLoop( pCrvChunkBorder->Clone())) return false ; // 2) Curva nel caso la regione si svuoti con MedialAxis PtrOwner pCrvPath( CreateCurveComposite()) ; if ( IsNull( pCrvPath)) return false ; // 3) Punto nel caso la regione si svuoti con un centroide Point3d ptGoTo, ptGoToI ; int nOptFlag ; if ( ! RemoveExtraPartByMedialAxis( pSrfChunkToCut, vOffsFirstCurve, nOptFlag, ptGoTo, pCrvPath) || nOptFlag == 0) continue ; if ( nOptFlag == 2 ) if ( ! pCrvPath->GetStartPoint( ptGoTo) || ! pCrvPath->GetEndPoint( ptGoToI)) continue ; // 1) cerco la curva più vicina al punto trovato (ptGoTo) PtrOwner pCrvH1( CreateCurveComposite()) ; PtrOwner pCrvH2( CreateCurveComposite()) ; PtrOwner pCrvH1I( CreateCurveComposite()) ; PtrOwner pCrvH2I( CreateCurveComposite()) ; if ( pCrvH1 == nullptr || pCrvH2 == nullptr) return false ; int nIndexO = -1 ; int nIndexOI = -1 ; Point3d ptS, ptSI ; if ( ! GetClosestOffsetToAPoint( vOffs, ptGoTo, ptS, pCrvH1, pCrvH2, nIndexO)) continue ; if( nOptFlag == 2) if ( ! GetClosestOffsetToAPoint( vOffs, ptGoToI, ptSI, pCrvH1I, pCrvH2I, nIndexOI)) continue ; // A) SE SONO SU UN ESTREMO DELLA CURVA DI OFFSET -> Cerco un punto sui collegamenti if ( pCrvH1 == nullptr || pCrvH2 == nullptr) { bool bFound, bFoundI ; int nIndexL, nIndexLI ; if ( ! GetClosestLinkToAPoint( vLinks, vOffs, ptGoTo, ptS, pCrvH1, pCrvH2, bFound, nIndexL)) continue ; if( nOptFlag == 2) if ( ! GetClosestLinkToAPoint( vLinks, vOffs, ptGoToI, ptSI, pCrvH1I, pCrvH2I, bFoundI, nIndexLI)) continue ; PtrOwner ptCrvNewLink( CreateCurveComposite()) ; if ( ptCrvNewLink == nullptr) return false ; if ( nOptFlag == 1) { if ( ! GetNewCurvetWithCentroid( pCrvH1, pCrvH2, ptGoTo, bFound, vOffsFirstCurve, ptCrvNewLink)) continue ; } else if ( nOptFlag == 2) { bool bSucc = true; bool bSuccI = true ; if ( ! GetNewCurvetWithPath( pCrvH1, pCrvH2, pCrvPath, vOffsFirstCurve, vOffsClosedCurves, ptCrvNewLink)) bSucc = false; PtrOwner pCrvPathI( pCrvPath->Clone()) ; pCrvPathI->Invert() ; PtrOwner ptCrvNewLinkI( CreateCurveComposite()) ; if ( ptCrvNewLinkI == nullptr) return false ; if ( ! GetNewCurvetWithPath( pCrvH1I, pCrvH2I, pCrvPathI, vOffsFirstCurve, vOffsClosedCurves, ptCrvNewLinkI)) bSuccI = false; int nC = 0 ; if ( bSucc && bSuccI) { if ( ChoosePath( ptCrvNewLink, ptCrvNewLinkI, 0, 0.25, 5 * 1000 * EPS_SMALL, nC) && nC == 1) { ptCrvNewLink.Set( Release( ptCrvNewLinkI)) ; nIndexL = nIndexLI ; } } else if ( ! bSucc) { ptCrvNewLink.Set( Release( ptCrvNewLinkI)) ; nIndexL = nIndexLI ; } } vLinks[nIndexL].Set( Release( ptCrvNewLink)) ; } else { // B) SE NON SONO AD UN ESTREMO DELLA CURVA DI OFFSET PtrOwner ptCrvNewOffs( CreateCurveComposite()) ; if ( ptCrvNewOffs == nullptr) return false ; if ( nOptFlag == 1) { if ( ! GetNewCurvetWithCentroid( pCrvH1, pCrvH2, ptGoTo, true, vOffsFirstCurve, ptCrvNewOffs)) continue ; } else if ( nOptFlag == 2) { // nel caso aggiungo un medial Axis non agli estremi di un Offset bool bSucc = true; bool bSuccI = true; if ( ! GetNewCurvetWithPath( pCrvH1, pCrvH2, pCrvPath, vOffsFirstCurve, vOffsClosedCurves, ptCrvNewOffs)) bSucc = false ; PtrOwner pCrvPathI( pCrvPath->Clone()) ; pCrvPathI->Invert() ; PtrOwner ptCrvNewOffsI( CreateCurveComposite()) ; if ( ptCrvNewOffsI == nullptr) return false ; if ( ! GetNewCurvetWithPath( pCrvH1I, pCrvH2I, pCrvPathI, vOffsFirstCurve, vOffsClosedCurves, ptCrvNewOffsI)) bSuccI = false ; int nC = 0 ; // se entrambi i percorsi sono validi allora li confronto if ( bSucc && bSuccI) { if ( ChoosePath( ptCrvNewOffs, ptCrvNewOffsI, 0, 0.5, 5 * 1000 * EPS_SMALL, nC) && nC == 1) { ptCrvNewOffs.Set( Release( ptCrvNewOffsI)) ; nIndexO = nIndexOI ; } } else if ( ! bSucc) { ptCrvNewOffs.Set( Release( ptCrvNewOffsI)) ; nIndexO = nIndexOI ; } } vOffs[nIndexO].Set( Release( ptCrvNewOffs)) ; } } return true ; } //--------------------------------------------------------------------------- bool Pocketing::GetCurveWeightInfo( ICurveComposite* pCrvCompo, double dMaxLen, double& dToTRot, int& nSmallArcs, int& nSmallLines) { dToTRot = 0 ; nSmallArcs = 0 ; nSmallLines = 0 ; if ( pCrvCompo == nullptr) return true ; int nLines = 0 ; PtrOwner pCrvLineOld( CreateCurveLine()) ; // scorro tutte le curve trovate const ICurve* pMyCrv = pCrvCompo->GetFirstCurve() ; while ( pMyCrv != nullptr) { GeoObjType GeoType = pMyCrv->GetType() ; if ( GeoType == CRV_ARC) { nLines = 0 ; PtrOwner pCrvArc( GetCurveArc(pMyCrv->Clone())) ; double dAngCenter = abs( pCrvArc->GetAngCenter()) ; dToTRot += dAngCenter ; double dLen = 0 ; if ( pCrvArc->GetLength( dLen) && dLen < dMaxLen) nSmallArcs++ ; } else if ( GeoType == CRV_LINE) { // check nel caso non sia riuscito a smussare... nLines++ ; if ( nLines == 1) pCrvLineOld.Set( GetCurveLine( pMyCrv->Clone())) ; else { PtrOwner pNewMyCrv( GetCurveLine( pMyCrv->Clone())) ; Vector3d vtNew, vtOld ; pCrvLineOld->GetEndDir( vtOld) ; pNewMyCrv->GetStartDir( vtNew) ; double dAngCorner ; vtOld.GetAngle( vtNew, dAngCorner) ; dToTRot += abs( dAngCorner) ; pCrvLineOld.Set( GetCurveLine( Release( pNewMyCrv))) ; } double dLen = 0 ; if ( pMyCrv->GetLength( dLen) && dLen < dMaxLen) nSmallLines++ ; } pMyCrv = pCrvCompo->GetNextCurve() ; } return true ; } //--------------------------------------------------------------------------- bool Pocketing::ChoosePath( ICurveComposite* pCrv1, ICurveComposite* pCrv2, int nP, double dPerP, double dMaxLen, int& nC) { // controllo dei parametri if ( pCrv1 == nullptr || pCrv2 == nullptr || ! ( nP == 0 || nP == 1) || dPerP > 1 || dPerP < 0 || dMaxLen < EPS_SMALL) return false ; // lunghezze... double dLen1 = 0 ; double dLen2 = 0 ; pCrv1->GetLength( dLen1) ; pCrv2->GetLength( dLen2) ; double CL1, CL2 ; if ( dLen1 < EPS_SMALL && dLen2 < EPS_SMALL) { CL1 = 0 ; CL2 = 0 ; } else { CL1 = dLen1 / max( dLen1, dLen2) ; CL2 = dLen2 / max( dLen1, dLen2) ; } // rotazioni... double dRot1 = 0; double dRot2 = 0 ; int dSmallArcs1 = 0 ; int dSmallArcs2 = 0 ; int dSmallLines1 = 0 ; int dSmallLines2 = 0 ; if ( ! GetCurveWeightInfo( pCrv1, dMaxLen, dRot1, dSmallArcs1, dSmallLines1) || ! GetCurveWeightInfo( pCrv2, dMaxLen, dRot2, dSmallArcs2, dSmallLines2)) return false ; double CR1, CR2 ; if ( dRot1 == 0 && dRot2 == 0) { CR1 = 0 ; CR2 = 0 ; } else { CR1 = dRot1 / max( dRot1, dRot2) ; CR2 = dRot2 / max( dRot1, dRot2) ; } double Fp1 = ( nP == 0 ? dPerP : ( 1 - dPerP)) * (CL1 + dSmallLines1) + ( nP == 0 ? ( 1 - dPerP) : dPerP) * ( CR1 + dSmallArcs1) ; double Fp2 = ( nP == 0 ? dPerP : ( 1 - dPerP)) * (CL2 + dSmallLines2) + ( nP == 0 ? ( 1 - dPerP) : dPerP) * ( CR2 + dSmallArcs2) ; nC = ( Fp1 > Fp2 ? 1 : 0) ; return true ; } //--------------------------------------------------------------------------- bool GetClosestOffsetToAPoint( ICRVCOMPOPOVECTOR& vCurves, Point3d& ptFocus, Point3d& ptCL, ICurveComposite* pCrv1, ICurveComposite* pCrv2, int& nIndex, bool bFirst) { // check paramtri if ( vCurves.size() == 0) return false ; // cerco il punto più vicino a ptFocus su ogni curva e scelgo quello a distanza minimia (da ptFocus) Point3d ptCl_H ; double dDistance = INFINITO ; int nFlag ; pCrv1->Clear() ; pCrv2->Clear() ; if (vCurves.size() == 1) { nIndex = 0 ; if ( ! DistPointCurve( ptFocus, *vCurves[0]).GetMinDistPoint( EPS_SMALL, ptCL, nFlag)) return false ; } for ( int j = 1; j < int( vCurves.size()) ; ++ j) { if ( ! DistPointCurve( ptFocus, *vCurves[j]).GetMinDistPoint( EPS_SMALL, ptCl_H, nFlag)) return false ; if ( dDistance > Dist( ptCl_H, ptFocus)) { dDistance = Dist( ptCl_H, ptFocus) ; ptCL = ptCl_H ; nIndex = j ; } } if ( ! bFirst && vCurves.size() > 1) { int nindexOld = nIndex ; dDistance = INFINITO ; for ( int j = 1; j < int( vCurves.size()) ; ++ j) { if ( j != nindexOld) { if ( ! DistPointCurve( ptFocus, *vCurves[j]).GetMinDistPoint( EPS_SMALL, ptCl_H, nFlag)) return false ; if ( dDistance > Dist( ptCl_H, ptFocus) ) { dDistance = Dist( ptCl_H, ptFocus) ; ptCL = ptCl_H ; nIndex = j ; } } } } if ( nIndex < 0) return false ; PtrOwner pCrv_H( vCurves[nIndex]->Clone()) ; // 2) Sulle curve di Offset ricavo: vettore iniziale, finale | punto iniziale, finale Vector3d vtTanS, vtTanE, vtHelp ; double dUTan, dUS, dUE ; bool bMustUseBiArcs = false ; if ( ! pCrv_H->GetParamAtPoint( ptCL, dUTan) || ! pCrv_H->GetDomain( dUS, dUE)) return false ; pCrv1->AddCurve( GetCurveComposite( pCrv_H->CopyParamRange( dUS, dUTan))) ; pCrv2->AddCurve( GetCurveComposite( pCrv_H->CopyParamRange( dUTan, dUE))) ; return true ; } //--------------------------------------------------------------------------- bool GetClosestLinkToAPoint( ICURVEPOVECTOR& vCurves, ICRVCOMPOPOVECTOR& vOffs, Point3d& ptFocus, Point3d& ptCL, ICurveComposite* pCrv1, ICurveComposite* pCrv2, bool& bfound, int& nIndex) { double dDistance = INFINITO; Point3d ptCl_H ; int nFlag ; pCrv1->Clear() ; pCrv2->Clear() ; bfound = false ; for ( int j = 1; j < int( vCurves.size()) ; ++ j) { if ( ! DistPointCurve( ptFocus, *vCurves[j]).GetMinDistPoint( EPS_SMALL, ptCl_H, nFlag)) return false ; if ( dDistance > Dist( ptCl_H, ptFocus) && ! ( vOffs[j-1]->IsPointOn( ptCl_H) || vOffs[j]->IsPointOn( ptCl_H))) { dDistance = Dist( ptCl_H, ptFocus) ; ptCL = ptCl_H ; nIndex = j ; bfound = true ; } } double dUS, dUE, dUTan ; if ( ! vCurves[nIndex]->GetDomain( dUS, dUE) || ! vCurves[nIndex]->GetParamAtPoint( ptCL, dUTan)) return false ; pCrv1->AddCurve( GetCurve( vCurves[nIndex]->CopyParamRange( dUS, dUTan))) ; pCrv2->AddCurve( GetCurve( vCurves[nIndex]->CopyParamRange( dUTan, dUE))) ; return true ; } //---------------------------------------------------------------------------- bool Pocketing::GetNewCurvetWithCentroid( ICurveComposite* pCrvH1, ICurveComposite* pCrvH2, Point3d& ptC, bool bCir, ICRVCOMPOPOVECTOR& VFirstOff, ICurveComposite* pCrvNewCurve) { ICurveComposite* pPath1( CreateCurveComposite()) ; ICurveComposite* pPath2( CreateCurveComposite()) ; if ( pPath1 == nullptr || pPath2 == nullptr) return false ; pCrvNewCurve->Clear() ; Point3d ptS, ptE = ptC ; Vector3d vtTanS, vtTanE ; if ( bCir) { // creo una circonferenza // prendo il punto iniziale if ( pCrvH1 == nullptr || pCrvH1->GetFirstCurve() == nullptr) { if ( ! pCrvH2->GetStartPoint( ptS)) return false ; } else { if ( ! pCrvH1->GetEndPoint( ptS)) return false ; } // creo la circonferenza if ( ! CalcBoundedSmootedLink( ptS, vtTanS, ptE, vtTanE, 0, VFirstOff, pPath1)) return false ; // controllo che la circonferenza sia ammissibile ( solo che non esca dalle prime curve di Offset ) Point3d ptCrvS ; pPath1->GetStartPoint( ptCrvS) ; if ( ! CutCurveByOffsets( pPath1, VFirstOff)) { // taglio la curva sugli Offsets return false ; } double dUCrvS ; ModifyCurveToSmooted( pPath1, 0.075, 0.075) ; if ( pPath1->IsClosed()) { pPath1->GetParamAtPoint( ptCrvS, dUCrvS) ; pPath1->ChangeStartPoint( dUCrvS) ; } // creo la nuova curva con la circonferenza if ( pCrvH1 != nullptr && pCrvH1->GetFirstCurve() != nullptr) if ( ! pCrvNewCurve->AddCurve( pCrvH1->Clone())) // aggiungo inizio return false ; if ( ! pCrvNewCurve->AddCurve( pPath1->Clone())) // aggiungo la circonferenza return false ; if ( pCrvH2 != nullptr && pCrvH2->GetFirstCurve() != nullptr) if ( !pCrvNewCurve->AddCurve(( pCrvH2->Clone()))) // aggiungo la fine return false ; } else { // creo un BiArco // prendo il vettore tangente e il punto iniziale if ( pCrvH1 == nullptr || pCrvH1->GetFirstCurve() == nullptr) { if ( ! pCrvH2->GetStartDir( vtTanS) || ! pCrvH2->GetStartPoint( ptS)) return false ; } else { if ( ! pCrvH1->GetEndDir( vtTanS) || ! pCrvH1->GetEndPoint( ptS)) return false ; } // creo i due Biarchi if ( ! CalcBoundedSmootedLink( ptS, vtTanS, ptE, vtTanS, 0.5, VFirstOff, pPath1) || ! CalcBoundedSmootedLink( ptE, vtTanS, ptS, vtTanS, 0.5, VFirstOff, pPath2)) return false ; // creo la nuova curva con il doppio Biarco if ( pCrvH1 != nullptr && pCrvH1->GetFirstCurve() != nullptr) if ( ! pCrvNewCurve->AddCurve( pCrvH1->Clone())) // aggiungo inizio return false ; if ( ! pCrvNewCurve->AddCurve( pPath1->Clone()) || ! pCrvNewCurve->AddCurve( pPath2->Clone())) // aggiungo i due BiArchi return false ; if ( pCrvH2 != nullptr && pCrvH2->GetFirstCurve() != nullptr) if ( ! pCrvNewCurve->AddCurve(( pCrvH2->Clone()))) // aggiungo la fine return false ; } return true ; } //---------------------------------------------------------------------------- bool Pocketing::GetNewCurvetWithPath( ICurveComposite* pCrvH1, ICurveComposite* pCrvH2, ICurveComposite* pCrvPath, ICRVCOMPOPOVECTOR& VFirstOff, ICRVCOMPOPOVECTOR& VoffsCl, ICurveComposite* pCrvNewCurve) { PtrOwner pPath1( CreateCurveComposite()) ; PtrOwner pPath2( CreateCurveComposite()) ; if ( pPath1 == nullptr || pPath2 == nullptr || pCrvPath == nullptr) return false ; pCrvNewCurve->Clear() ; Point3d ptS, ptE, ptH ; Vector3d vtTanS, vtTanE, vtH ; if ( ! pCrvPath->GetStartPoint( ptE) || ! pCrvPath->GetStartDir( vtTanE) || ! pCrvPath->GetEndPoint( ptH) || ! pCrvPath->GetEndDir( vtH)) return false ; if ( pCrvH1 == nullptr || pCrvH1->GetFirstCurve() == nullptr) { if ( ! pCrvH2->GetStartPoint( ptS) || ! pCrvH2->GetStartDir( vtTanS)) return false ; } else { if ( ! pCrvH1->GetEndPoint( ptS) || ! pCrvH1->GetEndDir( vtTanS)) return false ; } // creo i due Biarchi if ( ! CalcBoundedSmootedLink( ptS, vtTanS, ptE, vtTanE, 0.5, VFirstOff, pPath1)) if( ! CalcBoundedLink( ptS, ptE, VFirstOff, pPath1)) return false ; if( ! CalcBoundedSmootedLink( ptH, vtH, ptS, vtTanS, 0.5, VFirstOff, pPath2)) if( ! CalcBoundedLink( ptH, ptS, VFirstOff, pPath2)) return false ; // creo la curva formata da BiArco1 - Path - BiArco 2 PtrOwner pCrvToAdd( CreateCurveComposite()) ; if ( pCrvToAdd == nullptr || ! pCrvToAdd->AddCurve( pPath1->Clone()) || ! pCrvToAdd->AddCurve( pCrvPath->Clone()) || ! pCrvToAdd->AddCurve( pPath2->Clone())) return false ; if ( ! ManageSmoothAndAutoInters( pCrvToAdd, pCrvPath, pPath1, pPath2, VoffsCl)) return false ; // curva finale if ( pCrvH1 != nullptr && pCrvH1->GetFirstCurve() != nullptr) if ( ! pCrvNewCurve->AddCurve( pCrvH1->Clone())) // aggiungo inizio return false ; if ( ! pCrvNewCurve->AddCurve( pCrvToAdd->Clone())) { // aggiungo BiArco - Path - BiArco Vector3d vtHS, vtHE ; Point3d ptHS, ptHE ; pCrvNewCurve->GetEndPoint( ptHS) ; pCrvNewCurve->GetEndDir( vtHS) ; pCrvToAdd->GetStartPoint( ptHE) ; pCrvToAdd->GetStartDir( vtHE) ; PtrOwner pCrvBiArcHelper( CreateCurveComposite()) ; if ( ! CalcBoundedSmootedLink( ptHS, vtHS, ptHE, vtHE, 0.5, VFirstOff, pCrvBiArcHelper)) if ( ! CalcBoundedLink( ptHS, ptHE, VFirstOff, pCrvBiArcHelper)) return false ; if ( ! pCrvNewCurve->AddCurve( Release( pCrvBiArcHelper)) || ! pCrvNewCurve->AddCurve( pCrvToAdd->Clone())) return false ; } if ( pCrvH2 != nullptr && pCrvH2->GetFirstCurve() != nullptr) { if ( ! pCrvNewCurve->AddCurve( pCrvH2->Clone())) { // aggiungo fine int nTry = 1; int nMaxTry = 10 ; bool bFound = false; while ( nTry < nMaxTry && ! bFound) { Vector3d vtHS, vtHE ; Point3d ptHS, ptHE ; pCrvNewCurve->GetEndPoint( ptHS) ; pCrvNewCurve->GetEndDir( vtHS) ; pCrvH2->GetStartPoint( ptHE) ; pCrvH2->GetStartDir( vtHE) ; PtrOwner pCrvBiArcHelper( CreateCurveComposite()) ; if ( ! CalcBoundedSmootedLink( ptHS, vtHS, ptHE, vtHE, 0.5, VFirstOff, pCrvBiArcHelper)) if ( ! CalcBoundedLink( ptHS, ptHE, VFirstOff, pCrvBiArcHelper) ) return false ; if ( ! pCrvNewCurve->AddCurve( pCrvBiArcHelper->Clone()) || ! pCrvNewCurve->AddCurve( pCrvH2->Clone())) { nTry++; } else { bFound = true ; } } if ( ! bFound) return false ; } } return true ; } //---------------------------------------------------------------------------- bool Pocketing::ManageSmoothAndAutoInters( ICurveComposite* pCrv, ICurveComposite* pCrvPath, ICurveComposite* pPath1, ICurveComposite* pPath2, ICRVCOMPOPOVECTOR& vOffsCL) { // controllo parametri if ( pCrv == nullptr) return false ; // check AutoIntersezioni... SelfIntersCurve SICrv( *pCrv) ; if ( SICrv.GetCrossOrOverlapIntersCount() > 0) { // se ci sono autointersezioni Vector3d vTanE ; Point3d ptHS ; pCrvPath->GetEndDir( vTanE) ; pCrvPath->GetEndPoint( ptHS) ; bool bFound = false ; int nIter = -45 ; while ( ! bFound && nIter < 45) { vTanE.Rotate( Z_AX, 90 - nIter) ; // vettore perpendicolare PtrOwner pLine( CreateCurveLine()) ; pLine->SetPVL( ptHS, vTanE, m_TParams.m_dDiam / 3 - 5 * EPS_SMALL) ; // segmento uscente CRVCVECTOR ccClass ; IntersCurveCurve intCC( *pLine, *pCrv) ; intCC.GetCurveClassification( 0, EPS_SMALL, ccClass) ; if ( ccClass.size() > 1) // se intersezione con parte precedente inverto la direzione pLine->SetPVL( ptHS, - vTanE, m_TParams.m_dDiam / 3 - 5 * EPS_SMALL) ; if ( pLine == nullptr) break ; // punto finale segmento uscente Point3d ptEndLine ; pLine->GetEndPoint( ptEndLine) ; Point3d ptNear ; double dUS, dUE ; pPath2->GetDomain( dUS, dUE) ; PtrOwner pCrvHelper( GetCurve( pPath2->CopyParamRange( dUS, dUE - 0.5))); pCrvHelper->GetEndPoint( ptNear) ; PtrOwner pLine1( GetLinePointTgCurve( ptEndLine, *pPath2, ptNear)) ; // segmento tangente if ( pLine1 == nullptr) break ; // creo la nuova curva formata da BiArco 1 - Path - Segmento uscente - Segmento tangente - parte restante di Biarco 2 Point3d ptELine1 ; pLine1->GetEndPoint( ptELine1) ; pPath2->GetParamAtPoint( ptELine1, dUS) ; PtrOwner pNewPath2(( ICurveComposite*) GetCurve( pPath2->CopyParamRange( dUS, dUE))) ; // creo la curva formata dai due segmenti ma smussati tra loro... PtrOwner pCrvTwoSeg( CreateCurveComposite()) ; if ( ! pCrvTwoSeg->AddCurve( pCrvPath->Clone()) || ! pCrvTwoSeg->AddCurve( pLine->Clone()) || ! pCrvTwoSeg->AddCurve( pLine1->Clone())) return false ; ModifyCurveToSmooted( pCrvTwoSeg, 0.2, 0.2) ; PtrOwner ptNewCrv( CreateCurveComposite()); if ( ptNewCrv->AddCurve( pPath1->Clone()) && ptNewCrv->AddCurve( pCrvTwoSeg->Clone()) && ptNewCrv->AddCurve( pNewPath2->Clone())) { SelfIntersCurve SICrvLoop( *ptNewCrv) ; if ( SICrvLoop.GetCrossOrOverlapIntersCount() == 0) { pCrv->Clear() ; pCrv->AddCurve( Release( ptNewCrv)) ; bFound = true ; } else { // se trovo autointersezioni, ripeto cambiando l'angolo del segmento uscente nIter++ ; } } else break ; } } // smusso questa curva sulle varie curve di offsets Point3d ptCheck1, ptCheck2 ; PtrOwner pCrvH( pCrv->Clone()) ; // copia della curva if ( ! CutCurveByOffsets( pCrv, vOffsCL)) { // taglio la curva sugli Offsets pCrv->Clear() ; pCrv->AddCurve( Release( pCrvH)) ; } else { // controllo che i punti iniziali coincidano pCrv->GetStartPoint( ptCheck1) ; pCrvH->GetStartPoint( ptCheck2) ; if ( ! AreSamePointApprox( ptCheck1, ptCheck2)) { // se i punti iniziali della curva prima e dopo lo smusso non coincidono ... pCrv->Clear() ; pCrv->AddCurve ( Release( pCrvH)) ; } } return true ; } //---------------------------------------------------------------------------- bool RemoveFirstLoopFromSrfFlatRgn( ISurfFlatRegion* pSrfOrig) { if ( pSrfOrig == nullptr) return true ; PtrOwner pSrfRes( CreateSurfFlatRegion()) ; if ( pSrfRes == nullptr) return false ; for ( int c = 1; c < pSrfOrig->GetChunkCount(); c++) { PtrOwner pSrfHelp( CreateSurfFlatRegion()) ; if ( pSrfHelp == nullptr) return false ; PtrOwner pCrvMiniChunkBorder( GetCurve( pSrfOrig->GetLoop(c, 0)->Clone())) ; pSrfHelp->AddExtLoop( pCrvMiniChunkBorder->Clone()) ; if ( c == 1) pSrfRes.Set( pSrfHelp->Clone()) ; else pSrfRes->Add(*( pSrfHelp->Clone())) ; } pSrfOrig->Clear() ; pSrfOrig->CopyFrom( Release( pSrfRes)) ; return true ; } //---------------------------------------------------------------------------- bool Pocketing::RemoveExtraPartByMedialAxis( ISurfFlatRegion* pChunkToCut, ICRVCOMPOPOVECTOR& vOffsFirstCurve, int& nOptFlag, Point3d& ptCentroid, ICurveComposite* pCrvPath) { nOptFlag = 2 ; bool bForceCentroid = false ; if ( pChunkToCut == nullptr) return false ; ICurveComposite* pCrvStepByStepPath( CreateCurveComposite()) ; // curva che l'utensile dovrà seguire per svuotare la regione ICurveArc* pCrvToolShape( CreateCurveArc()) ; // superifice di ingobro del tool if ( pCrvToolShape == nullptr || pCrvStepByStepPath == nullptr) return false ; // copie del chunk che devo svuotare ISurfFlatRegion* pSrfChunkToCutClone( pChunkToCut->Clone()) ; double dArea = INFINITO ; ICRVCOMPOPVECTOR vCrvCoMedAxi ; // vettore dei medial Axis PNTVECTOR vPtCentroid ; // vettore di centroidi int nIter = 0 ; // numero di iterazioni while ( pSrfChunkToCutClone != nullptr && pSrfChunkToCutClone->GetChunkCount() != 0 && pSrfChunkToCutClone->GetArea( dArea) && dArea > 10 * EPS_SMALL) { // finchè restano parti non svuotate la cui area totale è suffcientemente grande... if ( nIter > 25) return false ; nIter++ ; ISurfFlatRegion* pSrfBiggerChunk( CreateSurfFlatRegion()) ; if ( pSrfBiggerChunk == nullptr || ! pSrfBiggerChunk->AddExtLoop( pSrfChunkToCutClone->GetLoop(0, 0))) return false ; double dAreaExt ; if ( pSrfBiggerChunk->GetArea( dAreaExt) && dAreaExt < 10 * EPS_SMALL) // se l'area del chunk è piccola... rimuovo il chunk dalla superficie da svuotare { if ( ! RemoveFirstLoopFromSrfFlatRgn( pSrfChunkToCutClone)) return false ; continue ; } // prendo il centroide del chunk Point3d ptC ; if ( ! pSrfBiggerChunk->GetCentroid( ptC)) return false ; // creo la superificie che racchiude il mio tool pCrvToolShape->SetXY( ptC, m_TParams.m_dDiam / 2 + 5 * EPS_SMALL) ; ISurfFlatRegion* pSrfTool( CreateSurfFlatRegion()) ; if ( pSrfTool == nullptr || ! pSrfTool->AddExtLoop( pCrvToolShape)) return false ; ISurfFlatRegion* pSrfChunkToCutCloneTest( pSrfBiggerChunk->Clone()) ; if ( bForceCentroid || ( pSrfChunkToCutCloneTest->Subtract( *pSrfTool) && ! pSrfChunkToCutCloneTest->GetArea( dArea) && dArea < 10 * EPS_SMALL)) { if ( nIter == 1) { // se alla prima iterazione svuoto tutto con il centroide ... nOptFlag = 1 ; ptCentroid = ptC; return true ; } else { // ... altrimenti devo prendere il centroide (che poi unirò al medil axis più vicino...) for ( int cen = 0 ; cen < int( vPtCentroid.size()) ; ++ cen) { if ( AreSamePointApprox( vPtCentroid[cen], ptC)) { pSrfChunkToCutClone->Clear() ; break ; } } vPtCentroid.push_back( ptC) ; if ( bForceCentroid) pSrfChunkToCutClone->Subtract( *pSrfTool) ; if ( ! pSrfChunkToCutClone->Subtract( *pSrfTool)) // o la pSrfBiggerChunk return false ; continue ; } } bForceCentroid = false ; // 1) ricavo il bordo della regione ICurve* pCrvBorder( pSrfBiggerChunk->GetLoop( 0, 0)) ; if ( pCrvBorder == nullptr) return false ; // 2) ricavo il medial Axis del bordo PolyLine PlMedAx ; if ( ! CurveSimpleMedialAxis( pCrvBorder, PlMedAx)) return false ; ICurveComposite* pCrvMedAx( CreateCurveComposite()) ; if ( pCrvMedAx == nullptr ) return false; pCrvMedAx->Clear() ; if ( ! pCrvMedAx->FromPolyLine( PlMedAx)) { // se non riesco ad aggiungere il medial axis non faccio nulla //if ( ! RemoveFirstLoopFromSrfFlatRgn( pSrfChunkToCutClone)) // return false ; //continue ; bForceCentroid = true ; nIter --; continue ; } pCrvMedAx->MergeCurves( 100 * EPS_SMALL, 100 * EPS_ANG_SMALL, false) ; PolyArc PlMedAxArc ; pCrvMedAx->ApproxWithArcsEx( 500 * EPS_SMALL, ANG_TOL_STD_DEG, LIN_FEA_STD, PlMedAxArc) ; pCrvMedAx->Clear(); pCrvMedAx->FromPolyArc( PlMedAxArc) ; // smusso la curva ModifyCurveToSmooted( pCrvMedAx, 0.025, 0.025) ; // la inserisco nel vettore vCrvCoMedAxi.push_back( pCrvMedAx->Clone()) ; // 3) guardo quale regione svuoto con questa curva ISurfFlatRegion* pSrfRemoved = GetSurfFlatRegionFromFatCurve( pCrvMedAx->Clone(), m_TParams.m_dDiam / 2 + 5 * EPS_SMALL, false, false) ; if ( pSrfRemoved == nullptr ) break ; // 4) aggiorno la regione togliendo la parte percorsa dal tool if ( ! pSrfChunkToCutClone->Subtract( *pSrfRemoved)) break ; if ( pSrfChunkToCutClone == nullptr) break ; } // Se il vettore è dei medial axis è vuoto allora l'area originaria era più piccola di 10 * EPS_SMALL if ( vCrvCoMedAxi.size() == 0 && vPtCentroid.size() == 0) { nOptFlag = 0 ; return true ; } // ora collego la varie curve medial Axis trovate tra loro (ottenendo quindi una curva che svuota tutta la regione) ICurveComposite* pCrvCoBackLink( CreateCurveComposite()) ; if ( pCrvCoBackLink == nullptr) return false ; Point3d ptSOriginal, ptEOriginal ; Vector3d vtSOriginal, vtEOriginal ; if ( ! vCrvCoMedAxi[0]->GetStartPoint( ptSOriginal) // parametri iniziali del primo e ultimo medial Axis trovato || ! vCrvCoMedAxi[vCrvCoMedAxi.size() -1]->GetEndPoint( ptEOriginal) || ! vCrvCoMedAxi[0]->GetStartDir( vtEOriginal) || ! vCrvCoMedAxi[vCrvCoMedAxi.size() - 1]->GetEndDir( vtSOriginal)) return false ; pCrvPath->AddCurve( vCrvCoMedAxi[0]->Clone()) ; // inserisco il primo medial Axis nel percorso finale for ( int i = 1; i < int( vCrvCoMedAxi.size()) ; ++ i) { // scorro i restanti Point3d ptS, ptE ; Vector3d vtS, vtE ; // parametri iniziali del medialAxis iesimo e (i-1)esimo if ( ! vCrvCoMedAxi[i]->GetStartPoint( ptE) || ! vCrvCoMedAxi[i-1]->GetEndPoint( ptS) || ! vCrvCoMedAxi[i]->GetStartDir( vtE) || ! vCrvCoMedAxi[i-1]->GetEndDir( vtS)) return false ; ICurveComposite* pCrvCoLink( CreateCurveComposite()) ; if ( pCrvCoLink == nullptr) return false ; if ( ! CalcBoundedSmootedLink( ptS, vtS, ptE, vtE, 0.5, vOffsFirstCurve, pCrvCoLink)) if ( ! CalcBoundedLink( ptS, ptE, vOffsFirstCurve, pCrvCoLink)) return false ; if ( ! pCrvPath->AddCurve( pCrvCoLink) || ! pCrvPath->AddCurve( vCrvCoMedAxi[i]->Clone())) return false ; } if ( ! AreSamePointEpsilon( ptEOriginal, ptSOriginal, EPS_SMALL)) if ( ! CalcBoundedSmootedLink( ptEOriginal, vtSOriginal, ptSOriginal, vtEOriginal, 0.5, vOffsFirstCurve, pCrvCoBackLink)) if ( ! CalcBoundedLink( ptEOriginal, ptSOriginal, vOffsFirstCurve, pCrvCoBackLink)) return false ; pCrvPath->AddCurve( pCrvCoBackLink->Clone()) ; // percorso con tutti i medial Axis Collegati // questa curva è chiusa e ha come punto iniziale/finale il punto finale del link della la curva di offset più vicina a questa regione svuotata // la tengo chiusa in modo da unire facilmente le circonferenze che collegheranno i vari centroidi a questa curva // se ho trovato dei centroidi li unisco nei punti più vicino a questo percorso for ( int i = 0; i < int( vPtCentroid.size()) ; ++ i) { // 1) cerco il punto sulla curva più vicino al centroide i Point3d ptClosestOnPath ; int nFlag ; if ( ! DistPointCurve( vPtCentroid[i], *pCrvPath).GetMinDistPoint( EPS_SMALL, ptClosestOnPath, nFlag)) return false ; // 2) imposto come punto iniziale della curva chiusa quest'ultimo punto double dU; if ( ! pCrvPath->GetParamAtPoint( ptClosestOnPath, dU)) return false ; pCrvPath->ChangeStartPoint( dU) ; // 3) prendo il vettore tangente al percorso nel punto PtClosestOnPath Vector3d vtTanCpt ; ICurveComposite* pCrvCoHelpToTan( pCrvPath->Clone()) ; if ( ! pCrvCoHelpToTan->GetStartDir( vtTanCpt)) return false ; // 4) collego i due punti (centroide e punto più vicino alla curva) ICurveComposite * pCrvPath1(CreateCurveComposite()) ; if ( pCrvPath1 == nullptr) return false ; if( ! CalcBoundedSmootedLink( ptClosestOnPath, vtTanCpt, vPtCentroid[i], vtTanCpt, 0, vOffsFirstCurve, pCrvPath1)) if( ! CalcBoundedLink( ptClosestOnPath, vPtCentroid[i], vOffsFirstCurve, pCrvPath1)) return false ; if ( ! pCrvPath->AddCurve( pCrvPath1)) { return false ; } } // riprendo il punto iniziale double dU ; if ( ! pCrvPath->GetParamAtPoint( ptSOriginal, dU)) return false ; pCrvPath->ChangeStartPoint( dU) ; // spezzo la curva nel punto PTEORIGINAL if ( ! pCrvPath->GetParamAtPoint( ptEOriginal, dU)) return false ; PtrOwner ptCrvPathWithCut( GetCurve( pCrvPath->CopyParamRange(0, dU))) ; if ( ptCrvPathWithCut == nullptr) return false ; pCrvPath->Clear(); pCrvPath->AddCurve( Release( ptCrvPathWithCut)) ; // la curva resitituita è una curva aperta che ha come primo punto il punto iniziale del primo medialAxis // la curva restituita collega tutti i medial Axis tra di loro // la curva restitutita collega tutti i centroidi con delle circonferenze // la curva restituita ha come punto finale l'ultimo punto dell'ultimo medial Axis return true ; } //---------------------------------------------------------------------------- bool Pocketing::CalcBoundedLink( const Point3d& ptStart, const Point3d& ptEnd, ICRVCOMPOPOVECTOR& vOffIslands, ICurveComposite* pCrvLink) { pCrvLink->Clear() ; // recupero il vettore estrusione dal bordo esterno offsettato della superficie Vector3d vtExtr ; vOffIslands[0]->GetExtrusion( vtExtr) ; // determino il riferimento naturale della svuotatura (OCS con il vettore estrusione come asse Z) Frame3d frLoc ; frLoc.Set( ORIG, vtExtr) ; // non serve collegare (può capitare nel tagliare percorsi con isole complesse) if ( AreSamePointApprox( ptStart, ptEnd)) return true ; // porto la curva di contenimento in locale a questo riferimento vector vOffsExtr ; for ( int i = 0 ; i < int( vOffIslands.size()) ; ++ i) { CurveLocal CrvOutLoc( vOffIslands[i], GLOB_FRM, frLoc) ; vOffsExtr.push_back( CrvOutLoc) ; } // creo la retta che li unisce PtrOwner pLine( CreateCurveComposite()) ; if ( ! pLine->AddPoint( ptStart) || ! pLine->AddLine( ptEnd)) return false ; pLine->SetExtrusion( vtExtr) ; // la porto in locale al riferimento della svuotatura CurveLocal LineLoc( pLine, GLOB_FRM, frLoc) ; // ... per le intersezioni // creo la nuova curva formata dai tratti di linee INTERNI alle isole e dai tratti sul bordo degli offset PtrOwner pCompo( pLine->Clone()) ; if ( IsNull( pCompo) ) return false ; if ( ! pCompo->LocToLoc( GLOB_FRM, frLoc) ) return false; PtrOwner pCompoHelp( pLine->Clone()) ; if ( IsNull( pCompoHelp)) return false ; if ( ! pCompoHelp->LocToLoc( GLOB_FRM, frLoc)) return false ; // scorro il vettore degli indici degli offset delle isole intersecati for ( int i = 0 ; i < int( vOffIslands.size()) ; i++) { CRVCVECTOR ccClass ; IntersCurveCurve intCC( *pCompo, *vOffIslands[i]) ; intCC.GetCurveClassification( 0, EPS_SMALL, ccClass) ; if ( ! pCompoHelp->Clear()) return false ; for ( int j = 0 ; j < int( ccClass.size()) ; ++j ) { // per ogni intersezione j con l'offset dell'isola i if ( ccClass[j].nClass == CRVC_OUT) { // se ho intersezione spezzo il segmento Point3d ptS ; pCompo->GetPointD1D2( ccClass[j].dParS, ICurve::FROM_PLUS, ptS) ; double dOffS ; vOffIslands[i]->GetParamAtPoint( ptS, dOffS) ; Point3d ptE ; pCompo->GetPointD1D2( ccClass[j].dParE, ICurve::FROM_MINUS, ptE) ; double dOffE ; vOffIslands[i]->GetParamAtPoint( ptE, dOffE) ; // recupero i due possibili percorsi e uso il più corto PtrOwner pCrvA( GetCurve( vOffIslands[i]->CopyParamRange( dOffS, dOffE))) ; PtrOwner pCrvB( GetCurve( vOffIslands[i]->CopyParamRange( dOffE, dOffS))) ; if ( IsNull( pCrvA) || IsNull( pCrvB) ) return false ; double dLenA ; pCrvA->GetLength( dLenA) ; double dLenB ; pCrvB->GetLength( dLenB) ; if ( dLenA < dLenB ) { pCompoHelp->AddCurve( Release( pCrvA)) ; } else { pCrvB->Invert() ; pCompoHelp->AddCurve( Release( pCrvB)) ; } } else { // se non interseco pCompoHelp->AddCurve( pCompo->Clone()->CopyParamRange( ccClass[j].dParS, ccClass[j].dParE)) ; } } pCompo->Clear() ; pCompo->AddCurve( pCompoHelp->Clone()) ; } // riporto pCompo nel sistema di riferimento originale if ( ! pCompo->LocToLoc( frLoc, GLOB_FRM)) return false ; pCrvLink->AddCurve( Release( pCompo)) ; return true ; } //------------------------------------------------------------------------------ bool Pocketing::CalcBoundedSmootedLink( const Point3d& ptStart, const Vector3d& vtStart, const Point3d& ptEnd, const Vector3d& vtEnd, double dParMeet, ICRVCOMPOPOVECTOR& vOffIslands, ICurveComposite* pCrvLink) { // creo il BiArc che unisce i due punti double dAngStart, dAngEnd ; vtStart.GetAngleXY( X_AX, dAngStart) ; vtEnd.GetAngleXY( X_AX, dAngEnd) ; PtrOwner pBiArcLink ; if ( dParMeet != 0) pBiArcLink.Set( GetBiArc( ptStart, -dAngStart, ptEnd, -dAngEnd, dParMeet)) ; else { ICurveArc* pCrvCir( CreateCurveArc()) ; if ( pCrvCir == nullptr) return false ; pCrvCir->SetCPAN( Media( ptStart, ptEnd), ptStart, 360, 0, Z_AX) ; pBiArcLink.Set( pCrvCir->Clone()) ; pCrvLink->AddCurve( pBiArcLink->Clone()) ; return true ; } if ( IsNull( pBiArcLink)) return CalcBoundedLink( ptStart, ptEnd, vOffIslands, pCrvLink) ; // se BiArco troppo grande allora lo modifico double dLenBiArc ; pBiArcLink->GetLength( dLenBiArc) ; if ( dLenBiArc > 200 * 1000 * EPS_SMALL) { PtrOwner pCrvNewBiArcS( CreateCurveComposite()) ; ModifyBiArc( pBiArcLink, 0.2, pCrvNewBiArcS) ; pBiArcLink.Set( Release( pCrvNewBiArcS)) ; } // creo la nuova curva formata inizialmente dai tratti di archi PtrOwner pCompo( GetCurveComposite( pBiArcLink->Clone())) ; if ( IsNull( pCompo)) return false ; PtrOwner pCompoHelp( GetCurveComposite( pBiArcLink->Clone())) ; if ( IsNull( pCompoHelp)) return false ; // scorro tutte le curve per controllare le intersezioni ... for ( int i = 0 ; i < int( vOffIslands.size()) ; i++) { CRVCVECTOR ccClass ; IntersCurveCurve intCC( *pCompo, *vOffIslands[i]) ; intCC.GetCurveClassification( 0, EPS_SMALL, ccClass) ; if ( ! pCompoHelp->Clear()) return false ; for ( int j = 0 ; j < int( ccClass.size()) ; ++j ) { // per ogni intersezione j con l'offset dell'isola i if ( ccClass[j].nClass == CRVC_OUT && ccClass.size() > 1) { // se ho intersezione spezzo il segmento Point3d ptS ; pCompo->GetPointD1D2( ccClass[j].dParS, ICurve::FROM_PLUS, ptS) ; double dOffS ; vOffIslands[i]->GetParamAtPoint( ptS, dOffS) ; Point3d ptE ; pCompo->GetPointD1D2( ccClass[j].dParE, ICurve::FROM_MINUS, ptE) ; double dOffE ; vOffIslands[i]->GetParamAtPoint( ptE, dOffE) ; // recupero i due possibili percorsi e uso il più corto PtrOwner pCrvA( GetCurve( vOffIslands[i]->CopyParamRange( dOffS, dOffE))) ; PtrOwner pCrvB( GetCurve( vOffIslands[i]->CopyParamRange( dOffE, dOffS))) ; if (IsNull(pCrvA) || IsNull(pCrvB)) { m_pGeomDB->AddGeoObj(GDB_ID_NULL, GDB_ID_ROOT, vOffIslands[i]->Clone()); m_pGeomDB->AddGeoObj(GDB_ID_NULL, GDB_ID_ROOT, pCompo->Clone()); IGeoPoint3d* PTS(CreateGeoPoint3d()); IGeoPoint3d* PTE(CreateGeoPoint3d()); PTS->Set( ptS) ; PTE->Set( ptE) ; m_pGeomDB->AddGeoObj(GDB_ID_NULL, GDB_ID_ROOT, PTS); m_pGeomDB->AddGeoObj(GDB_ID_NULL, GDB_ID_ROOT, PTE); return false; } double dLenA ; pCrvA->GetLength( dLenA) ; double dLenB ; pCrvB->GetLength( dLenB) ; if ( j != 0) { if ( dLenA < dLenB) { if ( ! pCompoHelp->AddCurve( Release( pCrvA))) return false ; } else { pCrvB->Invert() ; if ( ! pCompoHelp->AddCurve( Release( pCrvB))) return false ; } } else { pCrvB->Invert() ; if ( ! pCompoHelp->AddCurve( Release( pCrvB))) return false ; } } else { // se non interseco if ( ! pCompoHelp->AddCurve( pCompo->Clone()->CopyParamRange( ccClass[j].dParS, ccClass[j].dParE))) return false ; } } pCompo->Clear() ; if ( ! pCompo->AddCurve( pCompoHelp->Clone())) return false ; } // se il BiArco è troppo piccolo allora lo approssimo con un segmento BBox3d bBox3 ; if ( pCompo->GetLocalBBox( bBox3)) { double dRadBB ; if ( bBox3.GetRadius( dRadBB) && dRadBB < 500 * EPS_SMALL) { if ( ! CalcBoundedLink( ptStart, ptEnd, vOffIslands, pCrvLink)) return false ; return true ; } } ModifyCurveToSmooted( pCompo, 0.05, 0.05) ; pCrvLink->AddCurve( Release( pCompo)) ; return true ; } //----------------------------------------------------------------------------- bool Pocketing::ModifyBiArc( ICurve* pBiArcLink, double dToll, ICurveComposite* pNewBiArc) { if ( pBiArcLink == nullptr) return false ; if ( dToll > 0.99 || dToll < 0.01) return false ; pNewBiArc->Clear() ; double dUS, dUE ; pBiArcLink->GetDomain( dUS, dUE) ; PtrOwner pArc1( GetCurve( pBiArcLink->CopyParamRange( dUS, (dUS + dUE) / 2))) ; PtrOwner pArc2( GetCurve( pBiArcLink->CopyParamRange(( dUS + dUE ) / 2, dUE))) ; if ( pArc1 == nullptr || pArc2 == nullptr) return false ; // primo pezzo pArc1->GetDomain( dUS, dUE) ; PtrOwner pArc1A( GetCurve( pArc1->CopyParamRange( dUS, dUS + ( dUE - dUS ) * dToll))) ; // Arc1 PtrOwner pArc1B( GetCurve( pArc1->CopyParamRange( dUE - ( dUE - dUS ) * dToll, dUE))) ; // Arc2 Point3d pt1A, pt1B ; pArc1A->GetEndPoint( pt1A) ; pArc1B->GetStartPoint( pt1B) ; PtrOwner pLine1( CreateCurveLine()) ; pLine1->Set( pt1A, pt1B) ; // Linea // secondo pezzo pArc2->GetDomain(dUS, dUE) ; PtrOwner pArc2A( GetCurve( pArc2->CopyParamRange( dUS, dUS + ( dUE - dUS ) * dToll))) ; // Arc1 PtrOwner pArc2B( GetCurve( pArc2->CopyParamRange( dUE - ( dUE - dUS ) * dToll, dUE))) ; // Arc2 Point3d pt2A, pt2B ; pArc2A->GetEndPoint( pt2A) ; pArc2B->GetStartPoint( pt2B) ; PtrOwner pLine2( CreateCurveLine()) ; pLine2->Set( pt2A, pt2B) ; // Linea if ( ! pNewBiArc->AddCurve( Release( pArc1A)) || ! pNewBiArc->AddCurve( Release( pLine1)) || ! pNewBiArc->AddCurve( Release( pArc1B)) || ! pNewBiArc->AddCurve( Release( pArc2A)) || ! pNewBiArc->AddCurve( Release( pLine2)) || ! pNewBiArc->AddCurve( Release( pArc2B))) return false ; return true ; } //------------------------------------------------------------------------------ bool Pocketing::CutCurveByOffsets( ICurveComposite* pCurve, ICRVCOMPOPOVECTOR& vOffs) { // controllo parametri ingresso if ( vOffs.size() == 0) return true ; ICRVCOMPOPOVECTOR vOffOrig( vOffs.size()) ; for ( int i = 0; i < int( vOffs.size()) ; i++) { vOffOrig[i].Set(vOffs[i]->Clone()) ; } Point3d ptStart ; if ( ! pCurve->GetStartPoint( ptStart)) return false ; PtrOwner pCompo(( ICurveComposite* ) pCurve->Clone()) ; PtrOwner pCompoHelp(( ICurveComposite* ) pCurve->Clone()) ; PtrOwner pOffCheck( CreateCurveComposite()) ; if ( pCompoHelp == nullptr || pCompo == nullptr || pOffCheck == nullptr) return false ; int nTypeOfCut = -1 ; bool bAttention = false ; for ( int i = 0; i < int( vOffOrig.size()) ; i++) { if ( vOffOrig[i]->IsPointOn( ptStart)) pOffCheck.Set( vOffOrig[i]->Clone()) ; CRVCVECTOR ccClass ; IntersCurveCurve intCC( *pCompo, *vOffOrig[i]) ; intCC.GetCurveClassification( 0, 10 * EPS_SMALL, ccClass) ; if ( ! pCompoHelp->Clear()) return false ; if ( ccClass.size() > 1) { if ( ccClass[0].nClass == CRVC_IN) nTypeOfCut = CRVC_OUT ; else nTypeOfCut = CRVC_IN ; for ( int j = 0 ; j < int( ccClass.size()) ; ++j) { if ( ccClass[j].nClass == nTypeOfCut) { Point3d ptS ; pCompo->GetPointD1D2( ccClass[j].dParS, ICurve::FROM_PLUS, ptS) ; double dOffS ; vOffOrig[i]->GetParamAtPoint( ptS, dOffS) ; Point3d ptE ; pCompo->GetPointD1D2( ccClass[j].dParE, ICurve::FROM_MINUS, ptE) ; double dOffE ; vOffOrig[i]->GetParamAtPoint( ptE, dOffE) ; // recupero i due possibili percorsi e uso il più corto PtrOwner pCrvA( GetCurve( vOffOrig[i]->CopyParamRange( dOffS, dOffE))) ; PtrOwner pCrvB( GetCurve( vOffOrig[i]->CopyParamRange( dOffE, dOffS))) ; if ( IsNull( pCrvA) || IsNull( pCrvB)) return false ; double dLenA ; pCrvA->GetLength( dLenA) ; double dLenB ; pCrvB->GetLength( dLenB) ; if ( dLenA < dLenB) { CRVCVECTOR ccClassA ; IntersCurveCurve intCCA( *pCrvA, *pOffCheck) ; intCCA.GetCurveClassification( 0, 10 * EPS_SMALL, ccClassA) ; if ( ccClassA.size() > 0 && i == vOffOrig.size() - 1 && ccClassA[0].nClass == CRVC_ON_M) { if ( ! pCompoHelp->AddCurve( pCompo->Clone()->CopyParamRange( ccClass[j].dParS, ccClass[j].dParE))) return false ; continue ; } if ( ! pCompoHelp->AddCurve( Release( pCrvA))) return false ; } else { pCrvB->Invert() ; CRVCVECTOR ccClassB ; IntersCurveCurve intCCB( *pCrvB, *pOffCheck) ; intCCB.GetCurveClassification( 0, 10 * EPS_SMALL, ccClassB) ; if ( ccClassB.size() > 0 && i == vOffOrig.size() - 1 && ccClassB[0].nClass == CRVC_ON_M) { if ( ! pCompoHelp->AddCurve( pCompo->Clone()->CopyParamRange( ccClass[j].dParS, ccClass[j].dParE))) return false ; continue ; } if ( ! pCompoHelp->AddCurve( Release( pCrvB))) return false ; } } else { if ( ! pCompoHelp->AddCurve( pCompo->Clone()->CopyParamRange( ccClass[j].dParS, ccClass[j].dParE))) return false ; } } pCompo->Clear() ; pCompo->AddCurve( pCompoHelp->Clone()) ; } } pCurve->Clear() ; pCurve->AddCurve( Release( pCompo)) ; return true ; } //------------------------------------------------------------------------------ bool Pocketing::CalcBoundedLinkWithBiArcs( const Point3d& ptStart, const Vector3d& vtStart, const Point3d& ptEnd, const Vector3d& vtEnd, const ICurve* pCrvBound, ICurveComposite* pCrvLink) { double dAngStart, dAngEnd ; vtStart.GetAngleXY( X_AX, dAngStart) ; vtEnd.GetAngleXY( X_AX, dAngEnd) ; PtrOwner pBiArcLink( GetBiArc( ptStart, -dAngStart, ptEnd, -dAngEnd, 0.5)) ; if ( IsNull( pBiArcLink)) return false ; // verifico se esce dalla svuotatura CRVCVECTOR ccClass ; IntersCurveCurve intCC( *pBiArcLink, *pCrvBound) ; intCC.GetCurveClassification( 0, EPS_SMALL, ccClass) ; // se nessuno o un solo tratto e interno, il biarco è il collegamento if ( ccClass.empty() || ( ccClass.size() == 1 && ccClass[0].nClass == CRVC_IN)) { pCrvLink->AddCurve( Release( pBiArcLink)) ; } // altrimenti creo un percorso con biarchi e opportuni tratti della curva di contenimento else { PtrOwner pCompo( CreateCurveComposite()) ; if ( IsNull( pCompo)) return false ; double dPar1, dPar2 ; Point3d ptMinDist1, ptMinDist2 ; Vector3d vtDir1, vtDir2 ; double dAng1, dAng2 ; int nFlag ; DistPointCurve distPtSCrv( ptStart, *pCrvBound) ; distPtSCrv.GetParamAtMinDistPoint( 0, dPar1, nFlag) ; pCrvBound->GetPointTang( dPar1, ICurve::FROM_MINUS, ptMinDist1, vtDir1) ; vtDir1.GetAngleXY( X_AX, dAng1) ; DistPointCurve distPtECrv( ptEnd, *pCrvBound) ; distPtECrv.GetParamAtMinDistPoint( 0, dPar2, nFlag) ; pCrvBound->GetPointTang( dPar2, ICurve::FROM_MINUS, ptMinDist2, vtDir2) ; vtDir2.GetAngleXY( X_AX, dAng2) ; pCompo->AddCurve( GetBiArc( ptStart, -dAngStart, ptMinDist1, -dAng1, 0.5)) ; // primo biarco pCompo->AddCurve( pCrvBound->CopyParamRange( dPar1, dPar2)) ; // tratto di pCrvBound pCompo->AddCurve( GetBiArc( ptMinDist2, -dAng2, ptEnd, -dAngEnd, 0.5)) ; // secondo biarco pCrvLink->AddCurve( Release( pCompo)) ; } 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::CalcTrapezoidSpiral( ICurveComposite* pCrvPocket, const Vector3d& vtDir, double dPocketSize, ICurveComposite* pMCrv, ICurveComposite* pRCrv, bool& bOptimizedTrap) { bOptimizedTrap = false ; Vector3d vtExtr ; pCrvPocket->GetExtrusion( vtExtr) ; // eventuale approssimazione della curva con polyline per ottenere la stessa curva calcolata in ICurveComposite::IsATrapezoid if ( pCrvPocket->GetCurveCount() > 4) { PolyLine PL ; if ( ! pCrvPocket->ApproxWithLines( 100 * EPS_SMALL, ANG_TOL_STD_DEG, ICurve::APL_STD, PL)) return false ; pCrvPocket->Clear() ; pCrvPocket->FromPolyLine( PL) ; pCrvPocket->SetExtrusion( vtExtr) ; } // sistemo senso antiorario visto dalla direzione di estrusione Plane3d plPlane ; double dArea ; pCrvPocket->GetArea( plPlane, dArea) ; if ( plPlane.GetVersN() * vtExtr * dArea < 0) pCrvPocket->Invert() ; // passo in un sistema di riferimento locale avente asse X allineato con uno dei due lati paralleli (possibilmente aperto) e centro nel // punto iniziale del lato Frame3d frLoc ; if ( ! CalcTrapezoidSpiralLocalFrame( pCrvPocket, vtDir, frLoc)) return false ; pCrvPocket->ToLoc( frLoc) ; // recupero flag aperto/chiuso dei lati (0=chiuso, 1=aperto) INTVECTOR vnProp( 4, 0) ; for ( int i = 0 ; i < 4 ; i++) pCrvPocket->GetCurveTempProp( i, vnProp[i]) ; // verifico le dimensioni della svuotatura double dLen0 = 0, dLen2 = 0 ; pCrvPocket->GetCurve( 0)->GetLength( dLen0) ; pCrvPocket->GetCurve( 2)->GetLength( dLen2) ; if ( vnProp[0] == 0 && abs( dPocketSize - m_TParams.m_dDiam) > EPS_SMALL) return false ; if ( vnProp[1] == 0 && vnProp[3] == 0) { if ( dLen0 < m_TParams.m_dDiam - EPS_SMALL || dLen2 < m_TParams.m_dDiam - EPS_SMALL) return false ; } // calcolo la larghezza massima della svuotatura (riferimento con X parallelo a primo lato chiuso) Point3d ptOrig ; pCrvPocket->GetCurve( 1)->GetStartPoint( ptOrig) ; Vector3d vtX ; pCrvPocket->GetCurve( 1)->GetStartDir( vtX) ; Frame3d frDim ; frDim.Set( ptOrig, Z_AX, vtX) ; frDim.Invert() ; BBox3d b3Dim ; pCrvPocket->GetBBox( frDim, b3Dim, BBF_EXACT) ; double dMaxLarg = b3Dim.GetDimY() ; // calcolo percorso di svuotatura // se lati obliqui sono entrambi chiusi e dimensione svuotatura è maggiore di diametro fresa e minore del doppio gestione speciale if ( vnProp[0] != 0 && vnProp[2] != 0 && vnProp[3] == 0 && vnProp[1] == 0 && dMaxLarg > m_TParams.m_dDiam + 10 * EPS_SMALL && max( dLen0, dLen2) < 2 * m_TParams.m_dDiam + EPS_SMALL) { if ( ! SpecialAdjustTrapezoidSpiralForAngles( pMCrv, pCrvPocket)) { pMCrv->Clear() ; return false ; } } else { double dYCoord = dPocketSize - 0.5 * m_TParams.m_dDiam ; if ( vnProp[0] != 0) dYCoord -= GetOffsR() ; if ( vnProp[0] != 0 && vnProp[2] != 0) dYCoord = 0.5 * dPocketSize ; // se entrambi i lati paralleli sono aperti mi posiziono a metà della svuotatura double dXCoordStart, dXCoordEnd ; if ( ! CalcTrapezoidSpiralXCoord( pCrvPocket, true, dYCoord, dXCoordStart, dPocketSize)) return false ; if ( ! CalcTrapezoidSpiralXCoord( pCrvPocket, false, dYCoord, dXCoordEnd, dPocketSize)) return false ; if ( dXCoordStart > dXCoordEnd + 500 * EPS_SMALL) return false ; Point3d ptStart( dXCoordStart, dYCoord) ; Point3d ptEnd( dXCoordEnd, dYCoord) ; if ( AreSamePointEpsilon( ptStart, ptEnd, 500 * EPS_SMALL) && vnProp[0] != 0) { Vector3d vtDir1, vtDir3 ; pCrvPocket->GetCurve( 1)->GetStartDir( vtDir1) ; pCrvPocket->GetCurve( 3)->GetStartDir( vtDir3) ; // gestisco il caso speciale di un parallelogramma in cui anche l'altra dimensione della svuotatura è pari al diametro utensile if ( AreOppositeVectorApprox( vtDir1, vtDir3)) { PtrOwner pLine1( GetCurveLine( pCrvPocket->GetCurve( 1)->Clone())) ; PtrOwner pLine3( GetCurveLine( pCrvPocket->GetCurve( 3)->Clone())) ; if ( IsNull( pLine1) || IsNull( pLine3)) return true ; if ( ! pLine1->SimpleOffset( - 0.5 * m_TParams.m_dDiam - GetOffsR()) || ! pLine3->SimpleOffset( - 0.5 * m_TParams.m_dDiam - GetOffsR())) return true ; Point3d ptS, ptE ; if ( vtDir3 * X_AX > EPS_SMALL) { pLine1->GetStartPoint( ptS) ; pLine3->GetStartPoint( ptE) ; } else { pLine1->GetEndPoint( ptE) ; pLine3->GetEndPoint( ptS) ; } pMCrv->AddPoint( ptS) ; if ( vnProp[2] != 0) pMCrv->AddLine( ptE) ; else pMCrv->AddLine( ptStart) ; pMCrv->SetCurveTempProp( 0, 1) ; } } else { if ( ! pMCrv->AddPoint( ptStart)) return true ; if ( ! pMCrv->AddLine( ptEnd)) return true ; // aggiustamenti al percorso per rimuovere materiale residuo negli angoli if ( vnProp[0] != 0) { if ( vnProp[3] == 0 && ! AdjustTrapezoidSpiralForAngles( pMCrv, pCrvPocket, true)) { pMCrv->Clear() ; return false ; } if ( vnProp[1] == 0 && ! AdjustTrapezoidSpiralForAngles( pMCrv, pCrvPocket, false)) { pMCrv->Clear() ; return false ; } } } } if ( pMCrv->GetCurveCount() == 0) return true ; pMCrv->ToGlob( frLoc) ; if ( ! m_Params.m_bInvert) { pMCrv->Invert() ; // inverto le proprietà in modo che nProp3 sia sempre legata al punto iniziale e nProp1 a quello finale swap( vnProp[1], vnProp[3]) ; } // segno i lati aperti come temp prop della curva int nOpenEdges = vnProp[0] + vnProp[1] * 2 + vnProp[3] * 8 ; pMCrv->SetTempProp( nOpenEdges, 0) ; pMCrv->SetExtrusion( vtExtr) ; bOptimizedTrap = true ; return true ; } //---------------------------------------------------- bool Pocketing::CalcTrapezoidSpiralLocalFrame( ICurveComposite* pCrvPocket, const Vector3d& vtDir, Frame3d& frLoc) { // cerco i lati paralleli a vtDir int nBaseId = -1 ; for ( int i = 0 ; i < pCrvPocket->GetCurveCount() ; i ++) { Vector3d vtEdge ; pCrvPocket->GetCurve( i)->GetStartDir( vtEdge) ; if ( AreSameOrOppositeVectorApprox( vtEdge, vtDir)) { nBaseId = i ; break ; } } if ( nBaseId != 0 && nBaseId != 1) return false ; // imposto come lato iniziale per la curva uno dei lati paralleli a vtDir (possibilmente aperto) int nProp0, nProp2 ; pCrvPocket->GetCurveTempProp( nBaseId, nProp0) ; pCrvPocket->GetCurveTempProp( nBaseId + 2, nProp2) ; if ( nProp0 == 0 && nProp2 != 0) pCrvPocket->ChangeStartPoint( nBaseId + 2) ; else pCrvPocket->ChangeStartPoint( nBaseId) ; Point3d ptOrig ; pCrvPocket->GetStartPoint( ptOrig) ; Vector3d vtExtr ; pCrvPocket->GetExtrusion( vtExtr) ; Vector3d vtX ; pCrvPocket->GetStartDir( vtX) ; return frLoc.Set( ptOrig, vtExtr, vtX) ; } //------------------------------------------------------ bool Pocketing::CalcTrapezoidSpiralXCoord( const ICurveComposite* pCrvPocket, bool bStart, double dYCoord, double& dXCoord, double dPocketSize) { // se open int nCrvId = ( bStart ? 3 : 1) ; int nProp ; if ( pCrvPocket->GetCurveTempProp( nCrvId, nProp) && nProp != 0) { Point3d pt1, pt2 ; pCrvPocket->GetCurve( nCrvId)->GetStartPoint( pt1) ; pCrvPocket->GetCurve( nCrvId)->GetEndPoint( pt2) ; if ( bStart) dXCoord = min( pt1.x, pt2.x) ; else dXCoord = max( pt1.x, pt2.x) ; } // se closed else { double dRad = 0.5 * m_TParams.m_dDiam + GetOffsR() ; double dVal ; Vector3d vtRef ; pCrvPocket->GetCurve( nCrvId)->GetStartDir( vtRef) ; double dCosAlpha = vtRef * X_AX ; if ( dRad * dCosAlpha < dYCoord && dYCoord < dPocketSize + dRad * dCosAlpha) { double dSinAlpha = ( vtRef ^ X_AX).Len() ; if ( abs( dSinAlpha) < EPS_SMALL) return false ; dVal = 1.0 / dSinAlpha * ( dRad - dYCoord * dCosAlpha) ; } else if ( dYCoord < dRad * dCosAlpha) dVal = sqrt( dRad * dRad - dYCoord * dYCoord) ; else { double dLen ; pCrvPocket->GetCurve( nCrvId)->GetLength( dLen) ; dVal = - dLen * dCosAlpha + sqrt( dRad * dRad - dYCoord * dYCoord) ; } Point3d ptRef ; if ( bStart) { pCrvPocket->GetCurve( nCrvId)->GetEndPoint( ptRef) ; dXCoord = ptRef.x + dVal ; } else { pCrvPocket->GetCurve( nCrvId)->GetStartPoint( ptRef) ; dXCoord = ptRef.x - dVal ; } } return true ; } //---------------------------------------------------- bool Pocketing::AdjustTrapezoidSpiralForAngles( ICurveComposite* pMCrv, const ICurveComposite* pCrvPocket, bool bStart) { PtrOwner pCompo( CreateCurveComposite()) ; if ( ! bStart) pMCrv->Invert() ; Point3d ptTmp ; pMCrv->GetStartPoint( ptTmp) ; double dYCoord = ptTmp.y ; // quota verticale del percorso di svuotatura pCrvPocket->GetCurve( 2)->GetStartPoint( ptTmp) ; double dPocketSize = ptTmp.y ; int nCrvId = ( bStart ? 3 : 1) ; PtrOwner pLine( GetCurveLine( pCrvPocket->GetCurve( nCrvId)->Clone())) ; pLine->SimpleOffset( - 0.5 * m_TParams.m_dDiam - GetOffsR()) ; Point3d ptP1, ptP2 ; pLine->GetStartPoint( ptP1) ; pLine->GetEndPoint( ptP2) ; if ( ! bStart) swap( ptP1, ptP2) ; int nProp2 ; // lato opposto a quello di riferimento aperto if ( pCrvPocket->GetCurveTempProp( 2, nProp2) && nProp2 != 0) { // caso 1 : pLine ha un estremo sopra e uno sotto il percorso di svuotatura pMCrv if ( ptP2.y < dYCoord && dYCoord < ptP1.y) { // creo tratto da ptP2 a ptP1 pCompo->AddPoint( ptP2) ; pCompo->AddLine( ptP1) ; // trovo il punto di pMCrv da cui ripartire per non lasciare aree residue pLine->SimpleOffset( - 0.5 * m_TParams.m_dDiam) ; pLine->ExtendStartByLen( EPS_SMALL) ; pLine->ExtendEndByLen( EPS_SMALL) ; IntersCurveCurve intCC( *pLine, *pCrvPocket) ; if ( intCC.GetIntersCount() == 0) return false ; IntCrvCrvInfo aInfo ; intCC.GetIntCrvCrvInfo( 0, aInfo) ; double dDeltaX = sqrt( m_TParams.m_dDiam * m_TParams.m_dDiam / 4 - dYCoord * dYCoord) ; if ( ! bStart) dDeltaX *= -1 ; Point3d ptCrv( aInfo.IciA[0].ptI.x + dDeltaX, dYCoord) ; double dPar = 0 ; if ( ! pMCrv->GetParamAtPoint( ptCrv, dPar)) { dPar = 0.5 ; pMCrv->GetPointD1D2( dPar, ICurve::FROM_MINUS, ptCrv) ; } pMCrv->TrimStartAtParam( dPar) ; if ( ptP1.y > dPocketSize) { // se ptP1 è esterno alla svuotatura scambio i punti in modo da usare ptP2 per il biarco ( così da non farlo fuoriuscire troppo) swap( ptP1, ptP2) ; pCompo->Invert() ; } // creo biarco fra ptP1 e ptCrv Vector3d vtDir ; pCompo->GetStartDir( vtDir) ; double dAng ; vtDir.GetAngleXY( X_AX, dAng) ; PtrOwner pBiArc( GetBiArc( ptP1, - dAng, ptCrv, bStart ? 0 : 180, 0.8)) ; bool bUseBiArc = false ; if ( ! IsNull( pBiArc)) { // verifico che con il biarco non si oltrepassi il lato obliquo chiuso della svuotatura IntersCurveCurve intCC2( *pBiArc, *pCompo) ; if ( intCC2.GetIntersCount() == 1) bUseBiArc = true ; } if ( bUseBiArc) pCompo->AddCurve( Release( pBiArc)) ; else { double dParLine = ( dYCoord - ptP2.y) / ( ptP1.y - ptP2.y) ; Point3d ptOnLine = Media( ptP2, ptP1, dParLine) ; pCompo->AddLine( ptOnLine) ; pCompo->AddLine( ptCrv) ; } } // caso 2 : pLine è completamente sopra/sotto la linea di svuotatura else { // se è sopra modifiche per ricondurmi al caso in cui è sotto if ( ptP2.y > dYCoord) { pLine->Invert() ; swap( ptP1, ptP2) ; } // trovo l'intersezione fra il prolungamento della linea e pMCrv if ( bStart) pLine->ExtendStartByLen( 1000) ; else pLine->ExtendEndByLen( 1000) ; IntersCurveCurve intCC ( *pLine, *pMCrv) ; if ( intCC.GetIntersCount() == 0) return false ; IntCrvCrvInfo aInfo ; intCC.GetIntCrvCrvInfo( 0, aInfo) ; Point3d ptInt ; ptInt = aInfo.IciA[0].ptI ; double dPar = 0 ; pMCrv->GetParamAtPoint( ptInt, dPar) ; pMCrv->TrimStartAtParam( dPar) ; pCompo->AddPoint( ptP2) ; pCompo->AddLine( ptInt) ; } } // se il lato opposto a quello di riferimento è chiuso ha senso aggiungere solo se il punto è sotto la linea di svuotatura else { if ( ptP2.y < dYCoord) { pCompo->AddPoint( ptP2) ; Point3d ptCrv ; pMCrv->GetStartPoint( ptCrv) ; pCompo->AddLine( ptCrv) ; } } // setto temp prop per ricordare che è curva aggiuntiva per pulire angoli for ( int i = 0 ; i < pCompo->GetCurveCount() ; i++) pCompo->SetCurveTempProp( i, 1) ; pMCrv->AddCurve( Release( pCompo), false) ; if ( ! bStart) pMCrv->Invert() ; // ripristino la direzione originaria return true ; } //---------------------------------------------------- bool Pocketing::SpecialAdjustTrapezoidSpiralForAngles( ICurveComposite* pMCrv, const ICurveComposite* pCrvPocket) { // calcolo gli offset dei lati obliqui PtrOwner pLineS( GetCurveLine( pCrvPocket->GetCurve( 3)->Clone())) ; pLineS->SimpleOffset( - 0.5 * m_TParams.m_dDiam - GetOffsR()) ; pLineS->Invert() ; PtrOwner pLineE( GetCurveLine( pCrvPocket->GetCurve( 1)->Clone())) ; pLineE->SimpleOffset( - 0.5 * m_TParams.m_dDiam - GetOffsR()) ; pLineE->Invert() ; Point3d ptS, ptE ; pLineS->GetEndPoint( ptS) ; pLineE->GetStartPoint( ptE) ; Vector3d vtS, vtE ; pLineS->GetStartDir( vtS) ; pLineE->GetStartDir( vtE) ; PtrOwner pLineLink( CreateCurveLine()) ; pLineLink->Set( ptS, ptE) ; pMCrv->Clear() ; if ( ! pMCrv->AddCurve( Release( pLineS))) return false ; // creo raccordi bool bUseBiArcs = false ; Vector3d vtDir, vtDir2 ; pLineLink->GetStartDir( vtDir) ; pCrvPocket->GetStartDir( vtDir2) ; if ( Dist( ptS, ptE) > 500 * EPS_SMALL && AreSameOrOppositeVectorApprox( vtDir, vtDir2)) { Point3d ptCrv1, ptCrv2 ; pLineLink->GetPointD1D2( 0.3, ICurve::FROM_MINUS, ptCrv1) ; pLineLink->GetPointD1D2( 0.7, ICurve::FROM_MINUS, ptCrv2) ; // primo raccordo double dAng ; vtS.GetAngleXY( X_AX, dAng) ; PtrOwner pBiArc1( GetBiArc( ptS, - dAng, ptCrv1, 0, 0.5)) ; // secondo raccordo vtE.GetAngleXY( X_AX, dAng) ; PtrOwner pBiArc2( GetBiArc( ptE, dAng, ptCrv2, 180, 0.5)) ; pBiArc2->Invert() ; if ( ! IsNull( pBiArc1) && ! IsNull( pBiArc2)) { bUseBiArcs = true ; pMCrv->AddCurve( Release( pBiArc1)) ; pMCrv->AddLine( ptCrv2) ; pMCrv->AddCurve( Release( pBiArc2)) ; } } // se non è stato possibile creare raccordo, unisco linearmente if ( ! bUseBiArcs) pMCrv->AddCurve( Release( pLineLink)) ; if ( ! pMCrv->AddCurve( Release( pLineE))) return false ; // setto temp prop per ricordare curve aggiuntive per pulire angoli pMCrv->SetCurveTempProp( 0, 1) ; pMCrv->SetCurveTempProp( pMCrv->GetCurveCount() - 1, 1) ; return true ; } //---------------------------------------------------- bool Pocketing::AdjustTrapezoidSpiralForLeadInLeadOut( ICurveComposite* pCompo, ICurveComposite* pRCrv, const Vector3d& vtTool, double dDepth, int& nOutsideRaw) { // recupero la direzione principale della svuotatura Vector3d vtMainDir ; for ( int i = 0 ; i < pCompo->GetCurveCount() ; i++) { int nProp ; if ( pCompo->GetCurveTempProp( i, nProp) && nProp == 0) { // se non è lato aggiuntivo per la pulitura angoli recupero la sua direzione pCompo->GetCurve( i)->GetStartDir( vtMainDir) ; break ; } } // start point bool bStartOutside = false ; ComputeTrapezoidSpiralLeadInLeadOut( pCompo, vtMainDir, true, vtTool, dDepth, bStartOutside) ; // end point bool bEndOutside = false ; ComputeTrapezoidSpiralLeadInLeadOut( pCompo, vtMainDir, false, vtTool, dDepth, bEndOutside) ; // eventuale inversione della curva per partire sempre dall'esterno del grezzo if ( bEndOutside && ! bStartOutside) pCompo->Invert() ; nOutsideRaw = 0 ; if ( bStartOutside && bEndOutside) nOutsideRaw = 2 ; else if ( bStartOutside || bEndOutside) { nOutsideRaw = 1 ; // calcolo percorso di ritorno pRCrv->Clear() ; pRCrv->AddCurve( pCompo->Clone()) ; pRCrv->Invert() ; } return true ; } //---------------------------------------------------- bool Pocketing::ComputeTrapezoidSpiralLeadInLeadOut( ICurveComposite* pCompo, const Vector3d& vtMainDir, bool bLeadIn, const Vector3d& vtTool, double dDepth, bool& bIsOutsideRaw) { bIsOutsideRaw = false ; Point3d ptP ; Vector3d vtDir ; if ( bLeadIn) { pCompo->GetStartPoint( ptP) ; pCompo->GetStartDir( vtDir) ; } else { pCompo->GetEndPoint( ptP) ; pCompo->GetEndDir( vtDir) ; } Vector3d vtExtr ; pCompo->GetExtrusion( vtExtr) ; // recupero info sui lati aperti int nPropOpen = pCompo->GetTempProp( 0) ; bool bEdgeOpen = (( nPropOpen & ( bLeadIn ? 8 : 2)) > 0) ; bool bBaseOpen = (( nPropOpen & 1) > 0) ; // recupero info per capire se sto considerando un lato aggiuntivo per pulire angoli int nIdCrv = ( bLeadIn ? 0 : pCompo->GetCurveCount() - 1) ; int nExtraEdge ; pCompo->GetCurveTempProp( nIdCrv, nExtraEdge) ; double dSafeZ = m_pMchMgr->GetCurrMachiningsMgr()->GetSafeZ() ; // tento con allungamento se lato inclinato è aperto oppure se sto considerando un lato aggiuntivo per pulire angoli if ( bEdgeOpen || nExtraEdge == 1) { Vector3d vtDirP = ( bLeadIn ? -vtDir : vtDir) ; // se forzato come fuori dal grezzo if ( m_bOpenOutRaw) { Point3d ptNewStart = ptP + vtDirP * ( m_TParams.m_dDiam / 2 + dSafeZ) ; pCompo->AddLine( ptNewStart, ! bLeadIn) ; bIsOutsideRaw = true ; return true ; } // recupero la distanza dal bordo del grezzo lungo la direzione di allungamento double dDist ; Vector3d vtNorm ; if ( ! CalcDistanceFromRawSurface( m_nPhase, ptP, vtDirP, dDist, vtNorm)) return false ; // calcolo eventuali fattori correttivi double dCorr = 1 ; double dDistRef = dDist ; double dDistMin ; Vector3d vtNormMin ; if ( CalcDistanceFromRawSurface( m_nPhase, ptP, vtNorm, dDistMin, vtNormMin)) { if ( abs( dDistMin) < abs( dDist) && abs( dDistMin) > EPS_SMALL) { dDistRef = dDistMin ; dCorr = dDist / dDistMin ; } } // se vicino al bordo del grezzo if ( dDistRef < m_TParams.m_dDiam / 2 + EPS_SMALL) { Point3d ptTest = ptP + vtDirP * ( dDist + ( m_TParams.m_dDiam / 2 + dSafeZ) * dCorr) ; ptTest += - vtTool * dDepth ; double dTestElev ; // se è fuori dal grezzo if ( ! GetElevation( m_nPhase, ptTest, vtTool, m_TParams.m_dDiam / 2, vtTool, dTestElev) || dTestElev < EPS_SMALL) { Point3d ptNewStart = ptP + vtDirP * ( dDist + ( m_TParams.m_dDiam / 2 + dSafeZ) * dCorr) ; pCompo->AddLine( ptNewStart, ! bLeadIn) ; bIsOutsideRaw = true ; } } } // tento con attacco ruotato di 90° se non sto considerando un tratto aggiuntivo per pulire angoli if ( bBaseOpen && ! bIsOutsideRaw && nExtraEdge == 0) { Vector3d vtDirO = vtDir ; vtDirO.Rotate( vtExtr, ( m_Params.m_bInvert ? -90 : 90)) ; // calcolo distanza dal bordo del grezzo lungo vtDirO double dDist ; Vector3d vtNorm ; if ( ! CalcDistanceFromRawSurface( m_nPhase, ptP, vtDirO, dDist, vtNorm)) return false ; // se vicino al bordo del grezzo if ( dDist < m_TParams.m_dDiam / 2 + EPS_SMALL) { Point3d ptTestO = ptP + vtDirO * ( dDist + m_TParams.m_dDiam / 2 + dSafeZ) ; ptTestO += - vtTool * dDepth ; double dTestElevO ; // se è fuori dal grezzo uso inizio ruotato if ( ! GetElevation( m_nPhase, ptTestO, vtTool, m_TParams.m_dDiam / 2, vtTool, dTestElevO) || dTestElevO < EPS_SMALL) { Point3d ptNewStart = ptP + vtDirO * ( dDist + m_TParams.m_dDiam / 2 + dSafeZ) ; pCompo->AddLine( ptNewStart, ! bLeadIn) ; bIsOutsideRaw = true ; } } } return true ; } //---------------------------------------------------------------------------- bool Pocketing::ComputePolishingPath( ICurveComposite* pMCrv, ICurveComposite* pRCrv, bool bSplitArcs) { PtrOwner pCompo( CreateCurveComposite()) ; PtrOwner pCrvBound( CreateCurveComposite()) ; // curva bound da usare per CalcBoundedLink ICRVCOMPOPOVECTOR vpCrvsEp ; Frame3d frLoc ; Vector3d vtExtr ; pMCrv->GetExtrusion( vtExtr) ; frLoc.Set( ORIG, vtExtr) ; pMCrv->ToLoc( frLoc) ; for ( int i = 0 ; i < pMCrv->GetCurveCount() ; i ++) { int nProp = 0 ; if ( ! pMCrv->GetCurveTempProp( i, nProp)) return false ; // se è un tratto di collegamento ho concluso percorso su cui aggiungere epicicli if ( nProp == LINK_CURVE_PROP) { if ( pCompo->IsValid()) { PtrOwner pCrvEp( CreateCurveComposite()) ; // la curva di bound è l'offset che calcolo in AddEpicycles per la prima curva compo trovata in pMCrv bool bAddEp = ( ! pCrvBound->IsValid()) ? AddEpicycles( pCompo, pCrvEp, pCrvBound) : AddEpicycles( pCompo, pCrvEp) ; if ( ! bAddEp) return false ; vpCrvsEp.emplace_back( Release( pCrvEp)) ; pCompo.Set( CreateCurveComposite()) ; } } // se non è tratto di collegamento lo aggiungo alla curva else { if ( ! pCompo->AddCurve( pMCrv->GetCurve(i)->Clone())) return false ; } } // ultima curva if ( ! IsNull( pCompo)) { PtrOwner pCrvEp( CreateCurveComposite()) ; if ( ! AddEpicycles( pCompo, pCrvEp)) return false ; vpCrvsEp.emplace_back( Release( pCrvEp)) ; } // calcolo i collegamenti ICURVEPOVECTOR vLinks( vpCrvsEp.size()) ; for ( int i = 1 ; i < int( vpCrvsEp.size()) ; ++ i) { // punti e direzioni di inizio e fine Point3d ptStart ; Vector3d vtStart ; vpCrvsEp[i-1]->GetEndPoint( ptStart) ; vpCrvsEp[i-1]->GetEndDir( vtStart) ; Point3d ptEnd ; Vector3d vtEnd ; vpCrvsEp[i]->GetStartPoint( ptEnd) ; vpCrvsEp[i]->GetStartDir( vtEnd) ; // calcolo il collegamento con biarchi (garantendo che non esca dalla svuotatura) PtrOwner pCrvLink( CreateCurveComposite()) ; if ( CalcBoundedLinkWithBiArcs( ptStart, vtStart, ptEnd, vtEnd, pCrvBound, pCrvLink)) { vLinks[i].Set( Release( pCrvLink)) ; } else { m_pMchMgr->SetLastError( 2413, "Error in Pocketing : Toolpath not computable") ; return false ; } } // calcolo il percorso di ritorno pRCrv->Clear() ; if ( vpCrvsEp.size() >= 2) { // punti di inizio e fine Point3d ptStart ; Vector3d vtStart ; vpCrvsEp.back()->GetEndPoint( ptStart) ; vpCrvsEp.back()->GetEndDir( vtStart) ; Point3d ptEnd ; Vector3d vtEnd ; vpCrvsEp.front()->GetStartPoint( ptEnd) ; vpCrvsEp.front()->GetStartDir( vtEnd) ; // calcolo il ritorno con biarchi (garantendo che non esca dalla svuotatura) PtrOwner pCrvLink( CreateCurveComposite()) ; if ( CalcBoundedLinkWithBiArcs( ptStart, vtStart, ptEnd, vtEnd, pCrvBound, pCrvLink)) { pRCrv->AddCurve( Release( pCrvLink)) ; pRCrv->MergeCurves( 10 * EPS_SMALL, 10 * EPS_ANG_SMALL, false) ; // se necessario, approssimo archi con rette if ( bSplitArcs && ! ApproxWithLines( pRCrv)) { m_pMchMgr->SetLastError( 2421, "Error in Pocketing : Linear Approx not computable") ; return false ; } VerifyArcs( pRCrv) ; } else { m_pMchMgr->SetLastError( 2413, "Error in Pocketing : Toolpath not computable") ; return false ; } } // creo il percorso di lavoro a partire dalla raccolta delle curve con epicicli e dei collegamenti pMCrv->Clear() ; for ( int i = 0 ; i < int( vpCrvsEp.size()) ; ++ i) { // se collegamento da aggiungere if ( ! IsNull( vLinks[i])) { // accodo nel percorso di lavorazione pMCrv->AddCurve( Release( vLinks[i])) ; } // aggiungo la curva pMCrv->AddCurve( Release( vpCrvsEp[i])) ; } // se necessario, approssimo archi con rette if ( bSplitArcs && ! ApproxWithLines( pMCrv)) { m_pMchMgr->SetLastError( 2421, "Error in Pocketing : Linear Approx not computable") ; return false ; } VerifyArcs( pMCrv) ; pMCrv->ToGlob( frLoc) ; pRCrv->ToGlob( frLoc) ; return true ; } //---------------------------------------------------------------------------- bool Pocketing::AddEpicycles( ICurveComposite* pCompo, ICurveComposite * pCrv, ICurveComposite * pCrvBound) { if ( m_Params.m_bInvert) pCompo->Invert() ; // oriento la curva in senso antiorario OffsetCurve OffsCrv ; double dOffs = m_Params.m_dEpicyclesRad ; if ( ! OffsCrv.Make( pCompo, dOffs, ICurve::OFF_FILLET)) { m_pMchMgr->SetLastError( 2412, "Error in Pocketing : Offset not computable") ; return false ; } if ( OffsCrv.GetCurveCount() > 1) return false ; PtrOwner pCrvOffs( GetCurveComposite( OffsCrv.GetCurve())) ; if ( IsNull( pCrvOffs)) return false ; // verifico se devo resitituire la curva offsettata if ( pCrvBound) pCrvBound->AddCurve( pCrvOffs->Clone()) ; pCrv->Clear() ; double dParPrec = 0 ; for ( int i = 0 ; i < pCompo->GetCurveCount() ; i++) { // calcolo distanza epicili specifica per quel tratto double dLen ; pCompo->GetCurve( i)->GetLength( dLen) ; int nStep = max( 1, static_cast( ceil( ( dLen) / m_Params.m_dEpicyclesDist))) ; double dStep = 1.0 / nStep ; for ( int k = 1 ; k <= nStep ; k ++) { // creo epiciclo PtrOwner pCrvArc( CreateCurveArc()) ; Point3d ptCen ; Vector3d vtDir ; pCompo->GetCurve( i)->GetPointD1D2( k * dStep, ICurve::FROM_MINUS, ptCen, &vtDir) ; vtDir.Normalize() ; vtDir.Rotate( Z_AX, - 90) ; Point3d pt = ptCen + vtDir * m_Params.m_dEpicyclesRad ; pCrvArc->Set( ptCen, Z_AX, m_Params.m_dEpicyclesRad) ; double dU ; pCrvArc->GetParamAtPoint( pt, dU) ; pCrvArc->ChangeStartPoint( dU) ; // aggiungo tratto della curva offsettata double dPar ; pCrvOffs->GetParamAtPoint( pt, dPar) ; bool bAdd = pCrv->AddCurve( pCrvOffs->CopyParamRange( dParPrec, dPar)) ; // aggiungo epiciclo if ( ! pCrv->AddCurve( Release( pCrvArc))) { // se fallisco nell'aggiungere l'epiciclo tento nuovamente spostandolo di EPS_SMALL if ( bAdd) PtrOwner pCrvErased( pCrv->RemoveFirstOrLastCurve( true)) ; k -- ; dStep -= EPS_SMALL ; if ( dStep < EPS_SMALL) return false ; } else dParPrec = dPar ; } } // se necessario ripristino orientamento originale if ( m_Params.m_bInvert) pCrv->Invert() ; return true ; } //---------------------------------------------------------------------------- bool Pocketing::AddApproach( const Point3d& ptP, const Vector3d& vtTool, double dSafeZ, double dSafeAggrBottZ, double dElev, double dAppr, bool bOutStart) { SetFlag( 1) ; // se con aggregato da sotto o equivalente (rinvio a 90 gradi su testa 5 assi) bool bBottomOutStart = false ; if ( m_bAggrBottom) { // distanza dal bordo del pezzo (se negativa il punto è fuori dal grezzo) double dDistBottom ; if ( ! GetDistanceFromRawSide( m_nPhase, ptP, m_vtAggrBottom, dDistBottom)) dDistBottom = 0 ; bBottomOutStart = ( dDistBottom < - 10 * EPS_SMALL) ; // aggiuntivo in Z double dAggZ = ( bBottomOutStart ? 0. : max( dElev + max( dSafeAggrBottZ, dAppr), 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, 0, 1) ; 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 ( ! bBottomOutStart && 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( bOutStart ? GetStartFeed() : 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, bool bOutStart) { // 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( bOutStart ? GetStartFeed() : 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 con aggregato da sotto o equivalente (rinvio a 90 gradi su testa 5 assi) bool bBottomOutStart = false ; double dDistBottom ; if ( m_bAggrBottom) { // distanza dal bordo del pezzo if ( ! GetDistanceFromRawSide( m_nPhase, ptP, m_vtAggrBottom, dDistBottom)) dDistBottom = 0 ; bBottomOutStart = ( dDistBottom < - 10 * EPS_SMALL) ; } // se sopra uscita c'è spazio per sicurezza o approccio double dSafeDist = ( m_bAggrBottom ? dSafeAggrBottZ : dSafeZ) ; if ( ! bBottomOutStart && 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 o equivalente (rinvio a 90 gradi su testa 5 assi) if ( m_bAggrBottom) { // aggiuntivo in Z double dAggZ = ( bBottomOutStart ? 0. : max( dElev + max( dSafeAggrBottZ, dAppr), 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, 0, 1) ; 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) const { // 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 : case POCKET_LI_ZIGZAG : case POCKET_LI_HELIX : 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 ; } default : return false ; } } //---------------------------------------------------------------------------- bool Pocketing::AddLeadIn( const Point3d& ptP1, const Point3d& ptStart, const Vector3d& vtStart, const Vector3d& vtN, ISurfFlatRegion* pSrfChunk, const ICurveComposite* pRCrv, bool bAtLeft, bool bSplitArcs, bool bNoneForced, bool bSkipControl) { // Assegno il tipo int nType = GetLeadInType() ; if ( bNoneForced || AreSamePointEpsilon( ptP1, ptStart, 10 * EPS_SMALL) || ( nType == POCKET_LI_GLIDE && ( pRCrv == nullptr || pRCrv->GetCurveCount() == 0))) nType = POCKET_LI_NONE ; // Se elica e fattibile lo creo if ( nType == POCKET_LI_HELIX) { // vettore dal punto al centro elica Vector3d vtCen = vtStart ; vtCen.Rotate( vtN, 0, ( bAtLeft ? 1 : - 1)) ; // 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 + 20 * EPS_SMALL)) * ( bAtLeft ? ANG_FULL : - ANG_FULL) ; // verifico se fattibile if ( bSkipControl || VerifyLeadInHelix( pSrfChunk, ptCen, dRad)) { // 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) ; } } // altrimenti zigzag else nType = POCKET_LI_ZIGZAG ; } // Se zigzag e fattibile lo creo if ( nType == POCKET_LI_ZIGZAG) { // dati dello zigzag double dDeltaN = ( ptStart - ptP1) * vtN ; int nStep = int( ceil( - dDeltaN / ( m_Params.m_dLiElev + 20 * 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) ; // verifico se fattibile if ( bSkipControl || VerifyLeadInZigZag( pSrfChunk, ptPa, ptPb)) { 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) ; } // altrimenti diretto else nType = POCKET_LI_NONE ; } // Se a scivolo e fattibile if ( nType == POCKET_LI_GLIDE) { if ( pRCrv != nullptr) { // 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) ; } // altrimenti diretto else nType = POCKET_LI_NONE ; } // Se diretto if ( nType == 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 ; } // Altrimenti errore 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, ptP1 - 10 * EPS_SMALL * vtN, vtN, GetRadiusForStartEndElevation(), vtN, dEndElev)) dElev = dEndElev ; // correzione per punto sotto il grezzo con testa normale da sopra double dSafeZ = m_pMchMgr->GetCurrMachiningsMgr()->GetSafeZ() ; bool bUnderEnd = m_bAboveHead && ! m_bAggrBottom && ! m_bTiltingTab && GetPointUnderRaw( ptP1, vtN, 0, GetRadiusForStartEndElevation(), m_TParams.m_dLen, false, dSafeZ, vtN, dElev) ; 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 ; // correzione per punto sotto il grezzo con testa normale da sopra double dSafeZ = m_pMchMgr->GetCurrMachiningsMgr()->GetSafeZ() ; bool bUnderEnd = m_bAboveHead && ! m_bAggrBottom && ! m_bTiltingTab && GetPointUnderRaw( ptP1, vtN, 0, GetRadiusForStartEndElevation(), m_TParams.m_dLen, false, dSafeZ, vtN, dElev) ; return true ; } default : return false ; } } //---------------------------------------------------------------------------- double Pocketing::GetRadiusForStartEndElevation( void) const { const double DELTA_ELEV_RAD = 20.0 ; return ( 0.5 * m_TParams.m_dTDiam + min( 0.25 * m_TParams.m_dTDiam, DELTA_ELEV_RAD)) ; } //---------------------------------------------------------------------------- bool Pocketing::GetMidOfLongestOpenSide( const ICurveComposite* pCompo, Point3d& ptMid, Vector3d& vtMidOrt) { // recupero il vettore estrusione Vector3d vtExtr = Z_AX ; pCompo->GetExtrusion( vtExtr) ; // verifico se tutti i lati sono aperti bool bAllOpen = true ; const ICurve* pMyCrv = pCompo->GetFirstCurve() ; while ( pMyCrv != nullptr) { if ( pMyCrv->GetTempProp() != 1) { bAllOpen = false ; break ; } pMyCrv = pCompo->GetNextCurve() ; } // richiedo lunghezza superiore a diametro utensile più doppio offset radiale double dRefLen = ( bAllOpen ? 0 : m_TParams.m_dDiam + 2 * GetOffsR() - EPS_SMALL) ; double dMaxLen = dRefLen ; // ciclo sulle singole curve bool bFound = false ; const ICurve* pPrevCrv = pCompo->GetLastCurve() ; double dLenPrev = 0 ; if ( pPrevCrv != nullptr && pPrevCrv->GetTempProp() == 1) pPrevCrv->GetLength( dLenPrev) ; const ICurve* pCrv = pCompo->GetFirstCurve() ; while ( pCrv != nullptr) { // analizzo la curva successiva const ICurve* pNextCrv = pCompo->GetNextCurve() ; bool bNextOk = ( pNextCrv != nullptr) ; if ( ! bNextOk) pNextCrv = pCompo->GetFirstCurve() ; double dLenNext = 0 ; if ( pNextCrv != nullptr && pNextCrv->GetTempProp() == 1) pNextCrv->GetLength( dLenNext) ; // verifico la curva corrente if ( pCrv->GetTempProp() == 1) { // contributo dalle entità adiacenti (se non tutte aperte) double dLenAgg = 0 ; if ( ! bAllOpen) { if ( pPrevCrv != nullptr && pPrevCrv->GetTempProp() == 1) { Vector3d vtPrevEnd ; pPrevCrv->GetEndDir( vtPrevEnd) ; Vector3d vtStart ; pCrv->GetStartDir( vtStart) ; dLenAgg += max( 0.4, vtPrevEnd * vtStart) * dLenPrev ; } if ( pNextCrv != nullptr && pNextCrv->GetTempProp() == 1) { Vector3d vtEnd ; pCrv->GetEndDir( vtEnd) ; Vector3d vtNextStart ; pNextCrv->GetStartDir( vtNextStart) ; dLenAgg += max( 0.4, vtEnd * vtNextStart) * dLenNext ; } } // entità corrente double dLen = 0 ; if (pCrv->GetLength(dLen)) { const double LEN_TOL = 1; // se di lunghezza praticamente uguale if (bFound && dLen + dLenAgg > dRefLen && abs(dLen + dLenAgg - dMaxLen) < LEN_TOL) { Point3d ptTest; pCrv->GetMidPoint(ptTest); if ((m_bAboveHead && ptTest.z > ptMid.z + 100 * EPS_SMALL) || (!m_bAboveHead && ptTest.z < ptMid.z - 100 * EPS_SMALL) || (abs(ptTest.z - ptMid.z) < 100 * EPS_SMALL && ptTest.y < ptMid.y - 100 * EPS_SMALL)) { dMaxLen = max(dMaxLen, dLen + dLenAgg); ptMid = ptTest; // vettore ortogonale verso l'esterno (ruotato -90deg attorno a estrusione) pCrv->GetMidDir(vtMidOrt); vtMidOrt.Rotate(vtExtr, 0, -1); } } // se pi� lunga else if (dLen + dLenAgg > dMaxLen) { dMaxLen = dLen + dLenAgg; pCrv->GetMidPoint(ptMid); // vettore ortogonale verso l'esterno (ruotato -90deg attorno a estrusione) pCrv->GetMidDir(vtMidOrt); vtMidOrt.Rotate(vtExtr, 0, -1); bFound = true; } dLenPrev = dLen; } } else dLenPrev = 0 ; // vado alla successiva pPrevCrv = pCrv ; pCrv = ( bNextOk ? pNextCrv : nullptr) ; } return bFound ; } //---------------------------------------------------------------------------- bool Pocketing::AdjustContourWithOpenEdges( ICurveComposite* pCompo) { // vettore estrusione Vector3d vtExtr ; pCompo->GetExtrusion( vtExtr) ; // calcolo riferimento nel piano della svuotatura Frame3d frPocket ; Point3d ptStart ; pCompo->GetStartPoint( ptStart) ; frPocket.Set( ptStart, vtExtr) ; // sposto l'inizio a metà del tratto più lungo AdjustContourStart( pCompo) ; // raggio di riferimento per offset double dRad = 0.5 * m_TParams.m_dDiam + GetOffsR() ; // estraggo tutte le curve in un vettore ICURVEPOVECTOR vpCrvs ; vpCrvs.reserve( pCompo->GetCurveCount()) ; while ( pCompo->GetCurveCount() > 0) vpCrvs.emplace_back( pCompo->RemoveFirstOrLastCurve( false)) ; // elimino le curve troppo corte (10 epsilon) for ( int i = 0 ; i < int( vpCrvs.size()) ;) { double dLen = 0 ; vpCrvs[i]->GetLength( dLen) ; if ( dLen < 10 * EPS_SMALL) vpCrvs.erase( vpCrvs.begin() + i) ; else ++ i ; } // offsetto del raggio le curve aperte for ( int i = 0 ; i < int( vpCrvs.size()) ; ++ i) { if ( vpCrvs[i]->GetTempProp() == 1) vpCrvs[i]->SimpleOffset( dRad) ; } // reinserisco le curve, chiudendo eventuali gap bool bOpenCurr = false ; double dDiam = 1.05 * m_TParams.m_dDiam + 2 * GetOffsR() ; for ( int i = 0 ; i < int( vpCrvs.size()) ; ++ i) { // stato curve bool bOpenPrev = bOpenCurr ; bOpenCurr = ( vpCrvs[i]->GetTempProp() != 0) ; // chiudo eventuale gap if ( i > 0) { Point3d ptEnd ; pCompo->GetEndPoint( ptEnd) ; Point3d ptStart ; vpCrvs[i]->GetStartPoint( ptStart) ; if ( ! AreSamePointEpsilon( ptEnd, ptStart, 10 * EPS_SMALL)) { // se passo da chiuso ad aperto if ( ! bOpenPrev && bOpenCurr) { // determino la curva ad amo Vector3d vtTg ; pCompo->GetEndDir( vtTg) ; Vector3d vtOrt = vtTg ; vtOrt.Rotate( vtExtr, 0, 1) ; Point3d ptArc = ptEnd + dDiam * vtOrt ; Point3d ptLine = ptArc - 5 * dDiam * vtTg ; PtrOwner pJCrv( CreateCurveComposite()) ; if ( IsNull( pJCrv)) return false ; pJCrv->SetExtrusion( vtExtr) ; pJCrv->AddPoint( ptLine) ; pJCrv->AddLine( ptArc, false) ; pJCrv->AddArcTg( ptEnd, false) ; // calcolo l'intersezione nel piano della svuotatura dell'amo con la curva aperta pJCrv->ToLoc( frPocket) ; vpCrvs[i]->ToLoc( frPocket) ; IntersCurveCurve intCC( *pJCrv, *vpCrvs[i]) ; pJCrv->ToGlob( frPocket) ; vpCrvs[i]->ToGlob( frPocket) ; // taglio opportunamente le curve IntCrvCrvInfo aInfo ; if ( intCC.GetIntCrvCrvInfo( intCC.GetIntersCount() - 1, aInfo)) { pJCrv->TrimEndAtParam( aInfo.IciA[0].dU) ; vpCrvs[i]->TrimStartAtParam( aInfo.IciB[0].dU) ; pCompo->AddCurve( ::Release( pJCrv), true, 10 * EPS_SMALL) ; } else pCompo->AddLine( ptStart) ; } // se passo da aperto a chiuso else if ( bOpenPrev && ! bOpenCurr) { // determino la curva ad amo Vector3d vtTg ; vpCrvs[i]->GetStartDir( vtTg) ; Vector3d vtOrt = vtTg ; vtOrt.Rotate( vtExtr, 0, 1) ; Point3d ptArc = ptStart + dDiam * vtOrt ; Point3d ptLine = ptArc + 5 * dDiam * vtTg ; PtrOwner pJCrv( CreateCurveComposite()) ; if ( IsNull( pJCrv)) return false ; pJCrv->SetExtrusion( vtExtr) ; pJCrv->AddPoint( ptLine) ; pJCrv->AddLine( ptArc) ; pJCrv->AddArcTg( ptStart) ; // calcolo l'intersezione nel piano della svuotatura dell'amo con la curva aperta PtrOwner pLCrv( CreateCurveComposite()) ; if ( IsNull( pLCrv)) return false ; pLCrv->AddCurve( pCompo->GetLastCurve()->Clone()) ; if ( pCompo->GetCurveCount() >= 2) pLCrv->AddCurve( pCompo->GetPrevCurve()->Clone(), false) ; double dUL = pLCrv->GetCurveCount() ; pJCrv->ToLoc( frPocket) ; pLCrv->ToLoc( frPocket) ; IntersCurveCurve intCC( *pJCrv, *pLCrv) ; pJCrv->ToGlob( frPocket) ; pLCrv->ToGlob( frPocket) ; // taglio opportunamente le curve IntCrvCrvInfo aInfo ; if ( intCC.GetIntCrvCrvInfo( 0, aInfo)) { double dUs, dUe ; pCompo->GetDomain( dUs, dUe) ; pCompo->TrimEndAtParam( dUe - dUL + aInfo.IciB[0].dU) ; pJCrv->TrimStartAtParam( aInfo.IciA[0].dU) ; pCompo->AddCurve( ::Release( pJCrv), true, 10 * EPS_SMALL) ; } else pCompo->AddLine( ptStart) ; } else pCompo->AddLine( ptStart) ; } } // aggiungo la curva pCompo->AddCurve( ::Release( vpCrvs[i]), true, 10 * EPS_SMALL) ; } // non dovrebbe esserci un gap, ma meglio prevenire problemi pCompo->Close() ; return true ; } //---------------------------------------------------------------------------- bool Pocketing::AdjustContourStart( ICurveComposite* pCompo) { // cerco il tratto lineare più lungo che non sia aperto int i = 0 ; int nMax = - 1 ; double dLenMax = 0 ; const ICurve* pCrv = pCompo->GetFirstCurve() ; while ( pCrv != nullptr) { double dLen ; if ( pCrv->GetType() == CRV_LINE && pCrv->GetTempProp() == 0 && pCrv->GetLength( dLen) && dLen > dLenMax) { dLenMax = dLen ; nMax = i ; } ++ i ; pCrv = pCompo->GetNextCurve() ; } // se non trovato o troppo corto, cerco il tratto generico più lungo if ( nMax < 0 || dLenMax < 2 * m_TParams.m_dDiam) { i = 0 ; pCrv = pCompo->GetFirstCurve() ; while ( pCrv != nullptr) { double dLen ; if ( pCrv->GetType() != CRV_LINE && pCrv->GetTempProp() == 0 && 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 ; } //---------------------------------------------------------------------------- bool Pocketing::VerifyLeadInHelix( ISurfFlatRegion* pSrfChunk, const Point3d& ptCen, double dRad) const { // controllo della superifice if ( pSrfChunk == nullptr) return false ; // vettore di tutte le curve della superficie ICRVCOMPOPOVECTOR vCrv ; for( int c = 0 ; c < pSrfChunk->GetChunkCount() ; c++) { for ( int l = 0 ; l < pSrfChunk->GetLoopCount( c) ; l++) { vCrv.emplace_back( CloneCurveComposite( pSrfChunk->GetLoop( c, l))) ; } } if ( vCrv.size() == 0) return false ; // estraggo il bordo esterno PtrOwner pCompo( CloneCurveComposite( vCrv[0])) ; if ( IsNull( pCompo)) return false ; // recupero il piano della curva di contorno Point3d ptStart ; Vector3d vtN ; if ( ! pCompo->GetStartPoint( ptStart) || ! pCompo->GetExtrusion( vtN)) return false ; // porto il centro sullo stesso piano del contorno Point3d ptCenL = ptCen - ( ptCen - ptStart) * vtN * vtN ; // calcolo la distanza del centro dal contorno double dMinDist ; bool bOk = DistPointCurve( ptCenL, *pCompo).GetDist( dMinDist) && dMinDist > dRad + 0.5 * m_TParams.m_dDiam - 10 * EPS_SMALL ; // se la curva per l'attacco è valida per tale, controllo che l'elica non intersechi eventuali isole for ( int i = 1 ; bOk && i < int( vCrv.size()) ; i++) bOk = DistPointCurve( ptCenL, *vCrv[i]).GetDist( dMinDist) && dMinDist > dRad + 0.5 * m_TParams.m_dDiam - 10 * EPS_SMALL ; return bOk ; } //---------------------------------------------------------------------------- bool Pocketing::VerifyLeadInZigZag( ISurfFlatRegion* pSrfChunk, const Point3d& ptPa, const Point3d& ptPb) const { // controllo della superificie if ( pSrfChunk == nullptr) return false ; // vettore di tutte le curve della superficie ICRVCOMPOPOVECTOR vCrv ; for ( int c = 0 ; c < pSrfChunk->GetChunkCount() ; c++) { for ( int l = 0 ; l < pSrfChunk->GetLoopCount( c) ; l++) { vCrv.emplace_back( CloneCurveComposite( pSrfChunk->GetLoop( c, l))) ; } } if ( vCrv.size() == 0) return false ; // estraggo il bordo esterno PtrOwner pCompo( CloneCurveComposite( vCrv[0])) ; if ( IsNull( pCompo)) return false ; // recupero il piano della curva di contorno Point3d ptStart ; Vector3d vtN ; if ( ! pCompo->GetStartPoint( ptStart) || ! pCompo->GetExtrusion( vtN)) return false ; // porto i punti sullo stesso piano del contorno Point3d ptPaL = ptPa - ( ptPa - ptStart) * vtN * vtN ; Point3d ptPbL = ptPb - ( ptPb - ptStart) * vtN * vtN ; // calcolo la distanza dei due punti dal contorno double dMinDistPa ; if ( ! DistPointCurve( ptPaL, *pCompo).GetDist( dMinDistPa)) return false ; double dMinDistPb ; if ( ! DistPointCurve( ptPbL, *pCompo).GetDist( dMinDistPb)) return false ; bool bOk = dMinDistPa > 0.5 * m_TParams.m_dDiam - 10 * EPS_SMALL && dMinDistPb > 0.5 * m_TParams.m_dDiam - 10 * EPS_SMALL ; // se la curva per l'attacco è valida per tale, controllo che lo ZigZag non intersechi eventuali isole for ( int i = 1 ; bOk && i < int( vCrv.size()) ; i++) { if ( ! DistPointCurve( ptPaL, *vCrv[i]).GetDist( dMinDistPa)) return false ; if ( ! DistPointCurve( ptPbL, *vCrv[i]).GetDist( dMinDistPb)) return false ; bOk = dMinDistPa > 0.5 * m_TParams.m_dDiam - 10 * EPS_SMALL && dMinDistPb > 0.5 * m_TParams.m_dDiam - 10 * EPS_SMALL ; } return bOk ; } //---------------------------------------------------------------------------- bool Pocketing::CalcDistanceFromRawSurface( int nPhase, const Point3d& ptP, const Vector3d& vtDir, double& dDist, Vector3d& vtNorm) { if ( ! GetElevation( nPhase, ptP, vtDir, dDist, vtNorm)) return false ; // se punto esterno al grezzo if ( abs( dDist) < EPS_SMALL) { double dDist1, dDist2 ; if ( ! GetElevation( nPhase, ptP, -vtDir, dDist1)) return false ; if ( ! GetElevation( nPhase, ptP - vtDir * ( dDist1), vtDir, dDist2, vtNorm)) return false ; if ( abs( dDist2) > EPS_SMALL && abs( dDist1) > EPS_SMALL) dDist = dDist2 - dDist1 ; } return true ; }