Files
EgtMachKernel/Machining.cpp
T
Dario Sassi c5795df8d0 EgtMachKernel :
- aggiunta funzione SetParam alle diverse lavorazioni
- aggiunto calcolo elevazione da grezzo
- estesa lavorazione di taglio con lama.
2015-06-30 14:06:31 +00:00

331 lines
11 KiB
C++

//----------------------------------------------------------------------------
// EgalTech 2015-2015
//----------------------------------------------------------------------------
// File : Machining.cpp Data : 10.06.15 Versione : 1.6f2
// Contenuto : Implementazione gestione base lavorazioni.
//
//
//
// Modifiche : 10.06.15 DS Creazione modulo.
//
//
//----------------------------------------------------------------------------
//--------------------------- Include ----------------------------------------
#include "stdafx.h"
#include "MachMgr.h"
#include "Machining.h"
#include "CamData.h"
#include "/EgtDev/Include/EGkGeoPoint3d.h"
#include "/EgtDev/Include/EGkCurveLine.h"
#include "/EgtDev/Include/EGkIntersLineSurfTm.h"
#include "/EgtDev/Include/EGkGeomDB.h"
#include "/EgtDev/Include/EgtPointerOwner.h"
using namespace std ;
//----------------------------------------------------------------------------
bool
Machining::SetOwner( int nId, IGeomDB* pGDB)
{
m_nOwnerId = nId ;
m_pGeomDB = pGDB ;
return ( m_nOwnerId != GDB_ID_NULL && m_pGeomDB != nullptr) ;
}
//----------------------------------------------------------------------------
int
Machining::GetOwner( void) const
{
return m_nOwnerId ;
}
//----------------------------------------------------------------------------
IGeomDB*
Machining::GetGeomDB( void) const
{
return m_pGeomDB ;
}
//----------------------------------------------------------------------------
bool
Machining::Init( MachMgr* pMchMgr)
{
m_pMchMgr = pMchMgr ;
if ( m_pMchMgr == nullptr)
return false ;
if ( m_pMchMgr->GetGeomDB() != m_pGeomDB)
return false ;
return true ;
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
Machining::Machining( void)
: m_nOwnerId( GDB_ID_NULL), m_pGeomDB( nullptr), m_pMchMgr( nullptr),
m_nPathId( GDB_ID_NULL), m_bCurr( false), m_ptCurr(),
m_vtTool(), m_vtCorr(), m_vtAux(), m_dFeed( 0), m_nFlag( 0)
{
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
bool
Machining::GetElevation( const Point3d& ptP, const Vector3d& vtDir, double& dElev)
{
if ( m_pMchMgr == nullptr || m_pGeomDB == nullptr)
return false ;
// inizializzo elevazione
dElev = INFINITO ;
// ciclo sui grezzi
bool bFound = false ;
int nRawId = m_pMchMgr->GetFirstRawPart() ;
while ( nRawId != GDB_ID_NULL) {
// intersezione del raggio dal punto alla trimesh del grezzo
const double RAY_LEN = 10000 ;
int nStmId = m_pGeomDB->GetFirstNameInGroup( nRawId, MACH_RAW_SOLID) ;
ISurfTriMesh* pStm = GetSurfTriMesh( m_pGeomDB->GetGeoObj( nStmId)) ;
if ( pStm != nullptr) {
bFound = true ;
// recupero il riferimento della trimesh
Frame3d frStm ;
m_pGeomDB->GetGlobFrame( nStmId, frStm) ;
// porto il raggio in questo riferimento
Point3d ptPL = ptP ;
ptPL.ToLoc( frStm) ;
Vector3d vtDirL = vtDir ;
vtDirL.ToLoc( frStm) ;
ILSIVECTOR vInfo ;
if ( IntersLineSurfTm( ptPL, vtDirL, RAY_LEN, *pStm, vInfo)) {
for ( const auto& Info : vInfo) {
// se tratto di intersezione coincidente, considero la posizione più lontana, ovvero 2
if ( Info.nILTT == ILTT_SEGM || Info.nILTT == ILTT_SEGM_ON_EDGE) {
// se prosegue un tratto precedente, non devo controllare sia minimo
if ( fabs( dElev - Info.dU) < EPS_SMALL)
dElev = Info.dU2 ;
else
dElev = min( dElev, Info.dU2) ;
}
// altrimenti intersezione puntuale, verifico che esca (coseno >= 0)
else if ( Info.dCosDN > - COS_ORTO_ANG_ZERO) {
dElev = min( dElev, Info.dU) ;
}
}
}
}
// passo al grezzo successivo
nRawId = m_pMchMgr->GetNextRawPart( nRawId) ;
}
// se non trovate intersezioni, elevazione nulla
if ( dElev > INFINITO - 1)
dElev = 0 ;
return bFound ;
}
//----------------------------------------------------------------------------
bool
Machining::GetElevation( const Point3d& ptP1, const Point3d& ptP2, const Vector3d& vtDir, double& dElev)
{
if ( m_pMchMgr == nullptr || m_pGeomDB == nullptr)
return false ;
// inizializzo elevazione
dElev = INFINITO ;
// ciclo sui grezzi
bool bFound = false ;
int nRawId = m_pMchMgr->GetFirstRawPart() ;
while ( nRawId != GDB_ID_NULL) {
// intersezione del raggio dal punto alla trimesh del grezzo
const double RAY_LEN = 10000 ;
int nStmId = m_pGeomDB->GetFirstNameInGroup( nRawId, MACH_RAW_SOLID) ;
ISurfTriMesh* pStm = GetSurfTriMesh( m_pGeomDB->GetGeoObj( nStmId)) ;
if ( pStm != nullptr) {
bFound = true ;
// recupero il riferimento della trimesh
Frame3d frStm ;
m_pGeomDB->GetGlobFrame( nStmId, frStm) ;
// porto il segmento e la direzione in questo riferimento
Point3d ptP1L = ptP1 ;
ptP1L.ToLoc( frStm) ;
Point3d ptP2L = ptP2 ;
ptP2L.ToLoc( frStm) ;
Vector3d vtDirL = vtDir ;
vtDirL.ToLoc( frStm) ;
ILSIVECTOR vInfo ;
// inizializzo elevazione della superficie
double dElevS = 0 ;
// faccio test con un insieme di punti ( !!! sostituire con intersezione tra rettangolo e trimesh !!!)
const double STEP = 50 ;
int nStep = max( (int) ceil( ApproxDist( ptP1L, ptP2L) / STEP), 3) ;
for ( int i = 0 ; i <= nStep ; ++ i) {
// calcolo punto di test
double dFraz = i / (double) nStep ;
Point3d ptPL = Media( ptP1L, ptP2L, dFraz) ;
// inizializzo elevazione del punto
double dElevP = INFINITO ;
// calcolo elevazione sul punto
if ( IntersLineSurfTm( ptPL, vtDirL, RAY_LEN, *pStm, vInfo)) {
for ( const auto& Info : vInfo) {
// se tratto di intersezione coincidente, considero la posizione più lontana, ovvero 2
if ( Info.nILTT == ILTT_SEGM || Info.nILTT == ILTT_SEGM_ON_EDGE) {
// se prosegue un tratto precedente, non devo controllare sia minimo
if ( fabs( dElevP - Info.dU) < EPS_SMALL)
dElevP = Info.dU2 ;
else
dElevP = min( dElevP, Info.dU2) ;
}
// altrimenti intersezione puntuale, verifico che esca (coseno >= 0)
else if ( Info.dCosDN > - COS_ORTO_ANG_ZERO) {
dElevP = min( dElevP, Info.dU) ;
}
}
}
// se elevazione calcolata, aggiorno quella della superficie
if ( dElevP < INFINITO - 1)
dElevS = max( dElevS, dElevP) ;
}
// aggiorno elevazione complessiva
if ( dElevS > 0)
dElev = min( dElev, dElevS) ;
}
// passo al grezzo successivo
nRawId = m_pMchMgr->GetNextRawPart( nRawId) ;
}
// se non trovate intersezioni, elevazione nulla
if ( dElev > INFINITO - 1)
dElev = 0 ;
return bFound ;
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
bool
Machining::SetPathId( int nPathId)
{
m_nPathId = nPathId ;
return true ;
}
//----------------------------------------------------------------------------
bool
Machining::SetToolDir( const Vector3d& vtDir)
{
m_vtTool = vtDir ;
return ( m_vtTool.Normalize()) ;
}
//----------------------------------------------------------------------------
bool
Machining::SetCorrDir( const Vector3d& vtDir)
{
m_vtCorr = vtDir ;
return ( ! m_vtCorr.IsSmall()) ;
}
//----------------------------------------------------------------------------
bool
Machining::SetAuxDir( const Vector3d& vtDir)
{
m_vtAux = vtDir ;
return true ;
}
//----------------------------------------------------------------------------
bool
Machining::SetFeed( double dFeed)
{
m_dFeed = dFeed ;
return true ;
}
//----------------------------------------------------------------------------
bool
Machining::SetFlag( int nFlag)
{
m_nFlag = nFlag ;
return true ;
}
//----------------------------------------------------------------------------
int
Machining::AddStart( const Point3d& ptP)
{
// verifico di essere in uno stato valido per inizio
if ( m_vtTool.IsSmall())
return GDB_ID_NULL ;
// creo oggetto punto per DB geometrico
PtrOwner<IGeoPoint3d> pGP( CreateGeoPoint3d()) ;
if ( IsNull( pGP))
return GDB_ID_NULL ;
// assegno le coordinate del punto
pGP->Set( ptP) ;
// inserisco l'oggetto nel DB geometrico
int nId = m_pGeomDB->AddGeoObj( GDB_ID_NULL, m_nPathId, Release( pGP)) ;
if ( nId == GDB_ID_NULL)
return GDB_ID_NULL ;
// creo oggetto dati Cam
PtrOwner<CamData> pCam( new( nothrow) CamData) ;
if ( IsNull( pCam))
return GDB_ID_NULL ;
// assegno valori
pCam->SetToolDir( m_vtTool) ;
pCam->SetCorrDir( m_vtCorr) ;
pCam->SetAuxDir( m_vtAux) ;
pCam->SetBasePoint( ptP) ;
// associo questo oggetto a quello geometrico
m_pGeomDB->SetUserObj( nId, Release( pCam)) ;
// salvo la posizione corrente
m_bCurr = true ;
m_ptCurr = ptP ;
return nId ;
}
//----------------------------------------------------------------------------
int
Machining::AddLinearMove( const Point3d& ptP)
{
// verifico di essere in uno stato valido per un movimento lineare
if ( ! m_bCurr || m_vtTool.IsSmall() || m_dFeed < EPS_SMALL)
return GDB_ID_NULL ;
// creo oggetto linea per DB geometrico
PtrOwner<ICurveLine> pLine( CreateCurveLine()) ;
if ( IsNull( pLine))
return GDB_ID_NULL ;
// assegno le coordinate degli estremi
if ( ! pLine->Set( m_ptCurr, ptP))
return GDB_ID_NULL ;
// inserisco l'oggetto nel DB geometrico
int nId = m_pGeomDB->AddGeoObj( GDB_ID_NULL, m_nPathId, Release( pLine)) ;
if ( nId == GDB_ID_NULL)
return GDB_ID_NULL ;
// creo oggetto dati Cam
PtrOwner<CamData> pCam( new( nothrow) CamData) ;
if ( IsNull( pCam))
return GDB_ID_NULL ;
// assegno valori
pCam->SetToolDir( m_vtTool) ;
pCam->SetCorrDir( m_vtCorr) ;
pCam->SetAuxDir( m_vtAux) ;
pCam->SetBasePoint( ptP) ;
pCam->SetFeed( m_dFeed) ;
pCam->SetFlag( m_nFlag) ;
// associo questo oggetto a quello geometrico
m_pGeomDB->SetUserObj( nId, Release( pCam)) ;
// salvo la posizione corrente
m_ptCurr = ptP ;
return nId ;
}
//----------------------------------------------------------------------------
bool
Machining::ResetMoveData( void)
{
m_bCurr = false ;
m_ptCurr = ORIG ;
m_vtTool = V_NULL ;
m_vtCorr = V_NULL ;
m_vtAux = V_NULL ;
m_dFeed = 0 ;
m_nFlag = 0 ;
return true ;
}