Files
EgtGeomKernel/OutScl.cpp
T

394 lines
10 KiB
C++

//----------------------------------------------------------------------------
// EgalTech 2013-2013
//----------------------------------------------------------------------------
// File : OutScl.cpp Data : 31.12.12 Versione : 1.1a1
// Contenuto : Implementazione della classe output SCL.
//
//
//
// Modifiche : 31.12.12 DS Creazione modulo.
//
//
//----------------------------------------------------------------------------
//--------------------------- Include ----------------------------------------
#include "stdafx.h"
#include <algorithm>
#include <string>
#include <sstream>
#include <iomanip>
#include "/EgtDev/Include/EgnStringUtils.h"
//#include "GeoObjTypeFun.h"
#include "OutScl.h"
using namespace std ;
//----------------------------------------------------------------------------
OutScl::OutScl( void)
{
}
//----------------------------------------------------------------------------
OutScl::~OutScl( void)
{
Close() ;
}
//----------------------------------------------------------------------------
bool
OutScl::Open( const wstring& sOutScl)
{
// apro il file
m_ofFile.open( sOutScl) ;
if ( ! m_ofFile.good())
return false ;
// scrivo linee iniziali
Start() ;
New() ;
return true ;
}
//----------------------------------------------------------------------------
bool
OutScl::Close( void)
{
// verifico sia aperto
if ( ! m_ofFile.is_open())
return false ;
// scrivo linee finali
End() ;
// chiudo il file
m_ofFile.close() ;
return true ;
}
//----------------------------------------------------------------------------
bool
OutScl::Start( void)
{
// verifico sia aperto
if ( ! m_ofFile.is_open())
return false ;
// emetto stringa
m_ofFile << "{" << endl ;
return true ;
}
//----------------------------------------------------------------------------
bool
OutScl::End( void)
{
// verifico sia aperto
if ( ! m_ofFile.is_open())
return false ;
// emetto stringa
m_ofFile << "}" << endl ;
return true ;
}
//----------------------------------------------------------------------------
bool
OutScl::New( void)
{
// verifico sia aperto
if ( ! m_ofFile.is_open())
return false ;
// emetto stringa
m_ofFile << "NewFile() ;" << endl ;
return true ;
}
//----------------------------------------------------------------------------
bool
OutScl::Remark( string sRemark)
{
// verifico sia aperto
if ( ! m_ofFile.is_open())
return false ;
// emetto stringa
m_ofFile << "// " << sRemark << endl ;
return true ;
}
//----------------------------------------------------------------------------
bool
OutScl::SetMaterial( double dRed, double dGreen, double dBlue)
{
int nRed ;
int nGreen ;
int nBlue ;
string sMat ;
// componenti colore nell'intervallo 0-255
nRed = max( 0, min( int( 255 * dRed), 255)) ;
nGreen = max( 0, min( int( 255 * dGreen), 255)) ;
nBlue = max( 0, min( int( 255 * dBlue), 255)) ;
// calcolo nome materiale
sMat = "RGB" + ToString( nRed, 3) + ToString( nGreen, 3) + ToString( nBlue, 3) ;
// definizione materiale
return SetMaterial( sMat, ( nRed / 255.), ( nGreen / 255.), ( nBlue / 255.)) ;
}
//----------------------------------------------------------------------------
bool
OutScl::SetMaterial( string sMaterial, double dRed, double dGreen, double dBlue)
{
// verifico sia aperto
if ( ! m_ofFile.is_open())
return false ;
// salvo nome materiale
m_sMaterial = sMaterial ;
// emetto comando di creazione materiale
m_ofFile << "AddMaterial( \"" << sMaterial << "\", " <<
ToString( dRed, 3) << ", " << ToString( dGreen, 3) << ", " << ToString( dBlue, 3) << ") ;" << endl ;
return true ;
}
//----------------------------------------------------------------------------
bool
OutScl::SetPartLay( string sPart, string sLay)
{
// verifico sia aperto
if ( ! m_ofFile.is_open())
return false ;
m_sPartLay = sPart + "\\" + sLay ;
// emetto comando di creazione pezzo
m_ofFile << "CreatePart( \"" << sPart << "\") ;" << endl ;
// emetto comando di creazione layer
m_ofFile << "CreateLayer( \"" << sLay << "\", \"" << sPart << "\") ;" << endl ;
return true ;
}
//----------------------------------------------------------------------------
bool
OutScl::Line2P( Point3d ptP1, Point3d ptP2)
{
// verifico sia aperto
if ( ! m_ofFile.is_open())
return false ;
// emetto linea
m_ofFile << "Line3D( \"" << m_sPartLay << "\", \"" << m_sMaterial << "\", " ;
m_ofFile << ToString( ptP1) << ", " << ToString( ptP2) ;
m_ofFile << ") ;" << endl ;
return true ;
}
//----------------------------------------------------------------------------
bool
OutScl::Arc3P( Point3d ptP1, Point3d ptP2, Point3d ptP3)
{
// verifico sia aperto
if ( ! m_ofFile.is_open())
return false ;
// emetto arco
m_ofFile << "Arc3P( \"" << m_sPartLay << "\", \"" << m_sMaterial << "\", " ;
m_ofFile << ToString( ptP1) << ", " << ToString( ptP2) << ", " << ToString( ptP3) ;
m_ofFile << ") ;" << endl ;
return true ;
}
//----------------------------------------------------------------------------
bool
OutScl::ArcCPA( Point3d ptCen, Point3d ptMed, double dAngCenDeg)
{
Point3d ptP1 ;
Point3d ptP3 ;
// verifico sia aperto
if ( ! m_ofFile.is_open())
return false ;
// calcolo i due punti estremi
ptP1 = ptMed ;
ptP1.Rotate( ptCen, Vector3d( 0, 0, 1), - 0.5 * dAngCenDeg * DEGTORAD) ;
ptP3 = ptMed ;
ptP3.Rotate( ptCen, Vector3d( 0, 0, 1), 0.5 * dAngCenDeg * DEGTORAD) ;
// emetto arco
m_ofFile << "Arc3P( \"" << m_sPartLay << "\", \"" << m_sMaterial << "\", " ;
m_ofFile << ToString( ptP1, 8) << ", " << ToString( ptMed, 8) << ", " << ToString( ptP3, 8) ;
m_ofFile << ") ;" << endl ;
return true ;
}
//----------------------------------------------------------------------------
bool
OutScl::PutCurveLine( const ICurveLine& CrvLine)
{
// scrittura comandi SCL
Remark( "CurveLine") ;
Line2P( CrvLine.GetStart(), CrvLine.GetEnd()) ;
return true ;
}
//----------------------------------------------------------------------------
bool
OutScl::PutCurveArc( const ICurveArc& CrvArc)
{
const double DELTA_ANG = 15 ;
int nNumSeg ;
int i ;
double dU ;
double dCurv ;
Point3d ptIni ;
Point3d ptFin ;
Vector3d vtT ;
Vector3d vtN ;
// numero segmenti con cui approssimare
nNumSeg = (int) ( fabs( CrvArc.GetAngCenter()) / DELTA_ANG) + 1 ;
// ciclo sui segmenti
Remark( "CurveArc") ;
for ( i = 0 ; i <= nNumSeg ; ++ i) {
// ricavo il punto
dU = i / (double) nNumSeg ;
CrvArc.GetPoint( dU, ptFin) ;
// dopo il primo, disegno
if ( i > 0)
Line2P( ptIni, ptFin) ;
// nuovo iniziale prende i valori del finale
ptIni = ptFin ;
}
// ciclo per disegnare le derivate
Remark( "ArcTangents+Der2") ;
for ( i = 0 ; i <= nNumSeg ; ++ i) {
// ricavo il punto
dU = i / (double) nNumSeg ;
CrvArc.GetPointTangNormCurv( dU, ptFin, vtT, vtN, dCurv) ;
// tangente
Line2P( ptFin - vtT, ptFin + vtT) ;
// derivata
if ( fabs( dCurv) > EPS_ZERO)
Line2P( ptFin, ptFin + vtN * ( 1 / dCurv)) ;
}
return true ;
}
//----------------------------------------------------------------------------
bool
OutScl::PutCurveBez( const ICurveBezier& CrvBez)
{
const int NUM_SEG = 20 ;
bool bCCW ;
int i ;
double dU ;
double dCurv ;
double dAngCenDeg ;
Point3d ptIni ;
Point3d ptFin ;
Point3d ptCen ;
Vector3d vtT ;
Vector3d vtN ;
// ciclo per disegnare i segmenti
Remark( "BezierCurve") ;
for ( i = 0 ; i <= NUM_SEG ; ++ i) {
// ricavo il punto
dU = i / (double) NUM_SEG ;
CrvBez.GetPoint( dU, ptFin) ;
// dopo il primo, disegno
if ( i > 0)
Line2P( ptIni, ptFin) ;
// nuovo iniziale prende i valori del finale
ptIni = ptFin ;
}
// ciclo per disegnare le derivate
Remark( "BezierTangents+Der2") ;
for ( i = 0 ; i <= NUM_SEG ; ++ i) {
// ricavo il punto
dU = i / (double) NUM_SEG ;
CrvBez.GetPointTangNormCurv( dU, ptFin, vtT, vtN, dCurv) ;
// curvatura
if ( fabs( dCurv) > EPS_ZERO) {
// tratto di arco
ptCen = ptFin + vtN * ( 1 / dCurv) ;
bCCW = ( vtT ^ vtN).z > 0 ;
dAngCenDeg = ( bCCW ? 1 : -1) * 4 * dCurv * RADTODEG ;
ArcCPA( ptCen, ptFin, dAngCenDeg) ;
// raggio
Line2P( ptFin, ptCen) ;
}
// altrimenti, tangente
else if ( ! vtT.IsSmall())
Line2P( ptFin - vtT, ptFin + vtT) ;
}
return true ;
}
//----------------------------------------------------------------------------
bool
OutScl::PutPolygBez( const ICurveBezier& CrvBez)
{
int i ;
Point3d ptIni ;
Point3d ptFin ;
// ciclo per disegnare il poligono di controllo
Remark( "BezierPolygon") ;
for ( i = 0 ; i <= CrvBez.GetDegree() ; ++ i) {
ptFin = CrvBez.GetControlPoint( i) ;
// dopo il primo, disegno
if ( i > 0)
Line2P( ptIni, ptFin) ;
// nuovo iniziale prende i valori del finale
ptIni = ptFin ;
}
return true ;
}
//----------------------------------------------------------------------------
bool
OutScl::PutCurveCompo( const ICurveComposite& CrvCompo)
{
const ICurve* pCrvSmpl ;
pCrvSmpl = CrvCompo.GetFirstCurve() ;
while ( pCrvSmpl != nullptr) {
// scrittura comandi SCL
Remark( "CurveComposite") ;
// secondo il tipo
switch ( pCrvSmpl->GetType()) {
case CRV_LINE :
PutCurveLine( *::GetCurveLine( pCrvSmpl)) ;
break ;
case CRV_ARC :
PutCurveArc( *::GetCurveArc( pCrvSmpl)) ;
break ;
case CRV_BEZ :
PutCurveBez( *::GetCurveBezier( pCrvSmpl)) ;
break ;
}
// passo alla successiva
pCrvSmpl = CrvCompo.GetNextCurve() ;
}
return true ;
}