From 82f2e8c344ce04691f5353a97aa8b015daf87894 Mon Sep 17 00:00:00 2001 From: Marek Roszko Date: Wed, 24 Mar 2021 00:29:00 -0400 Subject: [PATCH] More gracefully handle tinyspline not liking a spline definition Partial fix for #6634 (cherry picked from commit e409b04538756f99af12cd1ff7ed9203d5ce1682) --- pcbnew/import_gfx/dxf_import_plugin.cpp | 32 +++++++++++++++++----- pcbnew/import_gfx/dxf_import_plugin.h | 4 +-- pcbnew/import_gfx/graphics_import_plugin.h | 2 +- pcbnew/import_gfx/graphics_importer.h | 2 +- pcbnew/import_gfx/svg_import_plugin.h | 4 +-- 5 files changed, 31 insertions(+), 13 deletions(-) diff --git a/pcbnew/import_gfx/dxf_import_plugin.cpp b/pcbnew/import_gfx/dxf_import_plugin.cpp index 9e540a8745..73f7ccf312 100644 --- a/pcbnew/import_gfx/dxf_import_plugin.cpp +++ b/pcbnew/import_gfx/dxf_import_plugin.cpp @@ -161,7 +161,7 @@ bool DXF_IMPORT_PLUGIN::ImportDxfFile( const wxString& aFile ) } -void DXF_IMPORT_PLUGIN::reportMsg( const char* aMessage ) +void DXF_IMPORT_PLUGIN::reportMsg( const wxString& aMessage ) { // Add message to keep trace of not handled dxf entities m_messages += aMessage; @@ -1050,8 +1050,6 @@ void DXF_IMPORT_PLUGIN::insertSpline( int aWidth ) } } #else // Use bezier curves, supported by pcbnew, to approximate the spline - tinyspline::BSpline dxfspline( m_curr_entity.m_SplineControlPointList.size(), - /* coord dim */ 2, m_curr_entity.m_SplineDegree ); std::vector ctrlp; for( unsigned ii = 0; ii < imax; ++ii ) @@ -1060,11 +1058,31 @@ void DXF_IMPORT_PLUGIN::insertSpline( int aWidth ) ctrlp.push_back( m_curr_entity.m_SplineControlPointList[ii].m_y ); } - dxfspline.setCtrlp( ctrlp ); - dxfspline.setKnots( m_curr_entity.m_SplineKnotsList ); - tinyspline::BSpline beziers( dxfspline.toBeziers() ); + std::vector coords; + try + { + tinyspline::BSpline dxfspline( m_curr_entity.m_SplineControlPointList.size(), + /* coord dim */ 2, m_curr_entity.m_SplineDegree ); - std::vector coords = beziers.ctrlp(); + dxfspline.setCtrlp( ctrlp ); + dxfspline.setKnots( m_curr_entity.m_SplineKnotsList ); + tinyspline::BSpline beziers( dxfspline.toBeziers() ); + + coords = beziers.ctrlp(); + } + catch( const std::runtime_error& ) //tinyspline throws everything including data validation as runtime errors + { + // invalid spline definition, drop this block + reportMsg( _( "Invalid spline definition encountered" ) ); + return; + } + + if( coords.size() % 8 != 0 ) + { + // somehow we generated a bad Bezier curve + reportMsg( _( "Invalid Bezier curve created" ) ); + return; + } // Each Bezier curve uses 4 vertices (a start point, 2 control points and a end point). // So we can have more than one Bezier curve ( there are one curve each four vertices) diff --git a/pcbnew/import_gfx/dxf_import_plugin.h b/pcbnew/import_gfx/dxf_import_plugin.h index fce2d3bdef..d4f8a74929 100644 --- a/pcbnew/import_gfx/dxf_import_plugin.h +++ b/pcbnew/import_gfx/dxf_import_plugin.h @@ -217,14 +217,14 @@ public: /** * @return the list of messages in one string. Each message ends by '\n' */ - const std::string& GetMessages() const override + const wxString& GetMessages() const override { return m_messages; } private: // report message to keep trace of not supported dxf entities: - void reportMsg( const char* aMessage ); + void reportMsg( const wxString& aMessage ); // coordinate conversions from dxf file to mm double mapX( double aDxfCoordX ); diff --git a/pcbnew/import_gfx/graphics_import_plugin.h b/pcbnew/import_gfx/graphics_import_plugin.h index 2ef00c5eeb..ff21fc8f74 100644 --- a/pcbnew/import_gfx/graphics_import_plugin.h +++ b/pcbnew/import_gfx/graphics_import_plugin.h @@ -118,7 +118,7 @@ public: * * @return the list of messages in one string. Each message ends by '\n' */ - const virtual std::string& GetMessages() const = 0; + const virtual wxString& GetMessages() const = 0; protected: ///> Importer used to create objects representing the imported shapes. diff --git a/pcbnew/import_gfx/graphics_importer.h b/pcbnew/import_gfx/graphics_importer.h index 308a69b3ad..a9b8d7a8c0 100644 --- a/pcbnew/import_gfx/graphics_importer.h +++ b/pcbnew/import_gfx/graphics_importer.h @@ -83,7 +83,7 @@ public: * * @return the list of messages in one string. Each message ends by '\n' */ - const std::string& GetMessages() const + const wxString& GetMessages() const { return m_plugin->GetMessages(); } diff --git a/pcbnew/import_gfx/svg_import_plugin.h b/pcbnew/import_gfx/svg_import_plugin.h index 708f6aecaa..b1371b61ee 100644 --- a/pcbnew/import_gfx/svg_import_plugin.h +++ b/pcbnew/import_gfx/svg_import_plugin.h @@ -52,7 +52,7 @@ public: /** * @return the list of messages in one string. Each message ends by '\n' */ - const std::string& GetMessages() const override + const wxString& GetMessages() const override { return m_messages; } @@ -77,7 +77,7 @@ private: struct NSVGimage* m_parsedImage; - std::string m_messages; // messages generated during svg file parsing. + wxString m_messages; // messages generated during svg file parsing. // Each message ends by '\n' };