geometry: Parse() method for SHAPE_LINE_CHAIN, working on Read() in SHAPE_FILE_IO

This commit is contained in:
Tomasz Wlostowski 2015-08-03 20:29:44 +02:00 committed by Maciej Suminski
parent 9c2bcb2f3c
commit 07f5516e59
5 changed files with 120 additions and 33 deletions

View File

@ -28,62 +28,119 @@
#include <geometry/shape.h>
#include <geometry/shape_file_io.h>
SHAPE_FILE_IO::SHAPE_FILE_IO( const std::string& aFilename, bool aAppend )
SHAPE_FILE_IO::SHAPE_FILE_IO( const std::string& aFilename, SHAPE_FILE_IO::IO_MODE aMode )
{
m_groupActive = false;
if( aFilename.length() )
m_file = fopen ( aFilename.c_str(), aAppend ? "ab" : "wb" );
{
switch( aMode )
{
case IOM_READ: m_file = fopen( aFilename.c_str(), "rb" ); break;
case IOM_WRITE: m_file = fopen( aFilename.c_str(), "wb" ); break;
case IOM_APPEND: m_file = fopen( aFilename.c_str(), "ab" ); break;
default:
return;
}
}
else
{
m_file = NULL;
}
m_mode = aMode;
// fixme: exceptions
}
SHAPE_FILE_IO::~SHAPE_FILE_IO()
{
if(!m_file)
if( !m_file )
return;
if (m_groupActive)
fprintf(m_file,"endgroup\n");
if( m_groupActive && m_mode != IOM_READ )
fprintf( m_file, "endgroup\n" );
fclose(m_file);
fclose( m_file );
}
SHAPE *SHAPE_FILE_IO::Read()
SHAPE* SHAPE_FILE_IO::Read()
{
assert(false);
/* char tmp[1024];
do {
if (fscanf(m_file, "%s", tmp) != 1)
return NULL;
if( !strcmp( tmp, "shape" )
break;
}
int type;
SHAPE *rv = NULL;
fscanf(m_file,"%d %s", &type, tmp);
printf("create shape %d\n", type);
switch(type)
{
case SHAPE::LINE_CHAIN:
rv = new SHAPE_LINE_CHAIN;
break;
}
if(!rv)
return NULL;
rv.Parse ( )
fprintf(m_file,"shape %d %s %s\n", aShape->Type(), aName.c_str(), sh.c_str() );
*/
assert( false );
return NULL;
}
void SHAPE_FILE_IO::BeginGroup( const std::string aName )
{
if(!m_file)
assert( m_mode != IOM_READ );
if( !m_file )
return;
fprintf(m_file, "group %s\n", aName.c_str());
fprintf( m_file, "group %s\n", aName.c_str() );
m_groupActive = true;
}
void SHAPE_FILE_IO::EndGroup()
{
if(!m_file || !m_groupActive)
assert( m_mode != IOM_READ );
if( !m_file || !m_groupActive )
return;
fprintf(m_file, "endgroup\n");
fprintf( m_file, "endgroup\n" );
m_groupActive = false;
}
void SHAPE_FILE_IO::Write( const SHAPE *aShape, const std::string aName )
void SHAPE_FILE_IO::Write( const SHAPE* aShape, const std::string aName )
{
if(!m_file)
assert( m_mode != IOM_READ );
if( !m_file )
return;
if(!m_groupActive)
fprintf(m_file,"group default\n");
if( !m_groupActive )
fprintf( m_file,"group default\n" );
std::string sh = aShape->Format();
fprintf(m_file,"shape %d %s %s\n", aShape->Type(), aName.c_str(), sh.c_str() );
fflush(m_file);
}
fprintf( m_file, "shape %d %s %s\n", aShape->Type(), aName.c_str(), sh.c_str() );
fflush( m_file );
}

View File

@ -559,3 +559,22 @@ SHAPE* SHAPE_LINE_CHAIN::Clone() const
{
return new SHAPE_LINE_CHAIN( *this );
}
bool SHAPE_LINE_CHAIN::Parse( std::stringstream& aStream )
{
int n_pts;
m_points.clear();
aStream >> n_pts;
aStream >> m_closed;
for( int i = 0; i < n_pts; i++ )
{
int x, y;
aStream >> x;
aStream >> y;
m_points.push_back( VECTOR2I( x, y ) );
}
return true;
}

View File

@ -38,24 +38,32 @@ class SHAPE;
class SHAPE_FILE_IO
{
public:
SHAPE_FILE_IO ( const std::string& aFilename, bool aAppend = false );
~SHAPE_FILE_IO ( );
void BeginGroup( const std::string aName = "<noname>");
void EndGroup( );
SHAPE *Read();
void Write ( const SHAPE *aShape, const std::string aName = "<noname>");
void Write ( const SHAPE &aShape, const std::string aName = "<noname>")
enum IO_MODE
{
Write( &aShape, aName );
IOM_READ = 0,
IOM_APPEND,
IOM_WRITE
};
SHAPE_FILE_IO( const std::string& aFilename, IO_MODE aMode = IOM_READ );
~SHAPE_FILE_IO();
void BeginGroup( const std::string aName = "<noname>");
void EndGroup();
SHAPE* Read();
void Write( const SHAPE* aShape, const std::string aName = "<noname>" );
void Write( const SHAPE& aShape, const std::string aName = "<noname>" )
{
Write( &aShape, aName );
}
private:
FILE *m_file;
FILE* m_file;
bool m_groupActive;
IO_MODE m_mode;
};
#endif

View File

@ -554,6 +554,9 @@ public:
/// @copydoc SHAPE::Format()
const std::string Format() const;
/// @copydoc SHAPE::Parse()
bool Parse( std::stringstream& aStream );
bool operator!=( const SHAPE_LINE_CHAIN& aRhs ) const
{
if( PointCount() != aRhs.PointCount() )

View File

@ -406,7 +406,7 @@ void ZONE_CONTAINER::AddClearanceAreasPolygonsToPolysList_NG( BOARD* aPcb )
std::auto_ptr<SHAPE_FILE_IO> dumper( new SHAPE_FILE_IO(
g_DumpZonesWhenFilling ? "zones_dump.txt" : "", true ) );
g_DumpZonesWhenFilling ? "zones_dump.txt" : "", SHAPE_FILE_IO::IOM_APPEND ) );
// Set the number of segments in arc approximations
if( m_ArcToSegmentsCount == ARC_APPROX_SEGMENTS_COUNT_HIGHT_DEF )