From e409b04538756f99af12cd1ff7ed9203d5ce1682 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 --- pcbnew/import_gfx/dxf_import_plugin.cpp | 32 +++++++++++++++++----- pcbnew/import_gfx/dxf_import_plugin.h | 6 ++-- 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, 32 insertions(+), 14 deletions(-) diff --git a/pcbnew/import_gfx/dxf_import_plugin.cpp b/pcbnew/import_gfx/dxf_import_plugin.cpp index 03c3062b8c..45db8008dc 100644 --- a/pcbnew/import_gfx/dxf_import_plugin.cpp +++ b/pcbnew/import_gfx/dxf_import_plugin.cpp @@ -186,7 +186,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; @@ -1311,8 +1311,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 ) @@ -1321,11 +1319,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 5b17e6d101..352ccb39b4 100644 --- a/pcbnew/import_gfx/dxf_import_plugin.h +++ b/pcbnew/import_gfx/dxf_import_plugin.h @@ -232,7 +232,7 @@ private: std::string m_codePage; // The code page, not used here bool m_importAsFPShapes; // Use footprint items instead of board items when true. // true when the items are imported in the footprint editor - std::string m_messages; // messages generated during dxf file parsing. + wxString m_messages; // messages generated during dxf file parsing. // Each message ends by '\n' DXF2BRD_ENTITY_DATA m_curr_entity; // the current entity parameters when parsing a DXF entity @@ -336,14 +336,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 42e8d0010d..90d12e75ce 100644 --- a/pcbnew/import_gfx/graphics_import_plugin.h +++ b/pcbnew/import_gfx/graphics_import_plugin.h @@ -119,7 +119,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 8545beff94..3925c561a1 100644 --- a/pcbnew/import_gfx/graphics_importer.h +++ b/pcbnew/import_gfx/graphics_importer.h @@ -84,7 +84,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' };