diff --git a/common/drawing_sheet/ds_data_model_io.cpp b/common/drawing_sheet/ds_data_model_io.cpp index 3c6f77c2a2..2742043777 100644 --- a/common/drawing_sheet/ds_data_model_io.cpp +++ b/common/drawing_sheet/ds_data_model_io.cpp @@ -39,8 +39,6 @@ using namespace DRAWINGSHEET_T; -#define double2Str Double2Str - // A helper function to write tokens: static const char* getTokenName( T aTok ) { @@ -210,17 +208,17 @@ void DS_DATA_MODEL_IO::Format( DS_DATA_MODEL* aSheet ) const // Write default values: m_out->Print( nestLevel, "(setup " ); m_out->Print( 0, "(textsize %s %s)", - double2Str( aSheet->m_DefaultTextSize.x ).c_str(), - double2Str( aSheet->m_DefaultTextSize.y ).c_str() ); - m_out->Print( 0, "(linewidth %s)", double2Str( aSheet->m_DefaultLineWidth ).c_str() ); - m_out->Print( 0, "(textlinewidth %s)", double2Str( aSheet->m_DefaultTextThickness ).c_str() ); + FormatDouble2Str( aSheet->m_DefaultTextSize.x ).c_str(), + FormatDouble2Str( aSheet->m_DefaultTextSize.y ).c_str() ); + m_out->Print( 0, "(linewidth %s)", FormatDouble2Str( aSheet->m_DefaultLineWidth ).c_str() ); + m_out->Print( 0, "(textlinewidth %s)", FormatDouble2Str( aSheet->m_DefaultTextThickness ).c_str() ); m_out->Print( 0, "\n" ); // Write margin values - m_out->Print( nestLevel, "(left_margin %s)", double2Str( aSheet->GetLeftMargin() ).c_str() ); - m_out->Print( 0, "(right_margin %s)", double2Str( aSheet->GetRightMargin() ).c_str() ); - m_out->Print( 0, "(top_margin %s)", double2Str( aSheet->GetTopMargin() ).c_str() ); - m_out->Print( 0, "(bottom_margin %s)", double2Str( aSheet->GetBottomMargin() ).c_str() ); + m_out->Print( nestLevel, "(left_margin %s)", FormatDouble2Str( aSheet->GetLeftMargin() ).c_str() ); + m_out->Print( 0, "(right_margin %s)", FormatDouble2Str( aSheet->GetRightMargin() ).c_str() ); + m_out->Print( 0, "(top_margin %s)", FormatDouble2Str( aSheet->GetTopMargin() ).c_str() ); + m_out->Print( 0, "(bottom_margin %s)", FormatDouble2Str( aSheet->GetBottomMargin() ).c_str() ); m_out->Print( 0, ")\n" ); // Save the graphical items on the drawing sheet @@ -244,7 +242,7 @@ void DS_DATA_MODEL_IO::format( DS_DATA_ITEM_TEXT* aItem, int aNestLevel ) const formatOptions( aItem ); if( aItem->m_Orient ) - m_out->Print( 0, " (rotate %s)", double2Str(aItem->m_Orient ).c_str() ); + m_out->Print( 0, " (rotate %s)", FormatDouble2Str( aItem->m_Orient ).c_str() ); // Write font info, only if it is not the default setup bool write_size = aItem->m_TextSize.x != 0.0 || aItem->m_TextSize.y != 0.0; @@ -258,13 +256,13 @@ void DS_DATA_MODEL_IO::format( DS_DATA_ITEM_TEXT* aItem, int aNestLevel ) const m_out->Print( 0, " (face \"%s\")", aItem->m_Font->NameAsToken() ); if( write_thickness ) - m_out->Print( 0, " (linewidth %s)", double2Str( aItem->m_LineWidth ).c_str() ); + m_out->Print( 0, " (linewidth %s)", FormatDouble2Str( aItem->m_LineWidth ).c_str() ); if( write_size ) { m_out->Print( 0, " (size %s %s)", - double2Str( aItem->m_TextSize.x ).c_str(), - double2Str( aItem->m_TextSize.y ).c_str() ); + FormatDouble2Str( aItem->m_TextSize.x ).c_str(), + FormatDouble2Str( aItem->m_TextSize.y ).c_str() ); } if( aItem->m_Bold ) @@ -279,7 +277,7 @@ void DS_DATA_MODEL_IO::format( DS_DATA_ITEM_TEXT* aItem, int aNestLevel ) const KiROUND( aItem->m_TextColor.r * 255.0 ), KiROUND( aItem->m_TextColor.g * 255.0 ), KiROUND( aItem->m_TextColor.b * 255.0 ), - Double2Str( aItem->m_TextColor.a ).c_str() ); + FormatDouble2Str( aItem->m_TextColor.a ).c_str() ); } m_out->Print( 0, ")" ); @@ -307,10 +305,10 @@ void DS_DATA_MODEL_IO::format( DS_DATA_ITEM_TEXT* aItem, int aNestLevel ) const // write constraints if( aItem->m_BoundingBoxSize.x ) - m_out->Print( 0, " (maxlen %s)", double2Str(aItem->m_BoundingBoxSize.x ).c_str() ); + m_out->Print( 0, " (maxlen %s)", FormatDouble2Str( aItem->m_BoundingBoxSize.x ).c_str() ); if( aItem->m_BoundingBoxSize.y ) - m_out->Print( 0, " (maxheight %s)", double2Str(aItem->m_BoundingBoxSize.y ).c_str() ); + m_out->Print( 0, " (maxheight %s)", FormatDouble2Str(aItem->m_BoundingBoxSize.y ).c_str() ); formatRepeatParameters( aItem ); @@ -335,7 +333,7 @@ void DS_DATA_MODEL_IO::format( DS_DATA_MODEL* aModel, DS_DATA_ITEM* aItem, int a formatOptions( aItem ); if( aItem->m_LineWidth && aItem->m_LineWidth != aModel->m_DefaultLineWidth ) - m_out->Print( 0, " (linewidth %s)", double2Str( aItem->m_LineWidth ).c_str() ); + m_out->Print( 0, " (linewidth %s)", FormatDouble2Str( aItem->m_LineWidth ).c_str() ); formatRepeatParameters( aItem ); @@ -356,10 +354,10 @@ void DS_DATA_MODEL_IO::format( DS_DATA_ITEM_POLYGONS* aItem, int aNestLevel ) co formatRepeatParameters( aItem ); if( !aItem->m_Orient.IsZero() ) - m_out->Print( 0, " (rotate %s)", double2Str( aItem->m_Orient.AsDegrees() ).c_str() ); + m_out->Print( 0, " (rotate %s)", FormatDouble2Str( aItem->m_Orient.AsDegrees() ).c_str() ); if( aItem->m_LineWidth ) - m_out->Print( 0, " (linewidth %s)\n", double2Str( aItem->m_LineWidth ).c_str() ); + m_out->Print( 0, " (linewidth %s)\n", FormatDouble2Str( aItem->m_LineWidth ).c_str() ); if( !aItem->m_Info.IsEmpty() ) m_out->Print( 0, " (comment %s)\n", m_out->Quotew( aItem->m_Info ).c_str() ); @@ -386,8 +384,8 @@ void DS_DATA_MODEL_IO::format( DS_DATA_ITEM_POLYGONS* aItem, int aNestLevel ) co } m_out->Print( nestLevel, " (xy %s %s)", - double2Str( pos.x ).c_str(), - double2Str( pos.y ).c_str() ); + FormatDouble2Str( pos.x ).c_str(), + FormatDouble2Str( pos.y ).c_str() ); } m_out->Print( 0, ")\n" ); @@ -404,7 +402,7 @@ void DS_DATA_MODEL_IO::format( DS_DATA_ITEM_BITMAP* aItem, int aNestLevel ) cons formatCoordinate( "pos", aItem->m_Pos ); formatOptions( aItem ); - m_out->Print( 0, " (scale %s)", double2Str( aItem->m_ImageBitmap->GetScale() ).c_str() ); + m_out->Print( 0, " (scale %s)", FormatDouble2Str( aItem->m_ImageBitmap->GetScale() ).c_str() ); formatRepeatParameters( aItem ); m_out->Print( 0,"\n"); @@ -429,8 +427,8 @@ void DS_DATA_MODEL_IO::format( DS_DATA_ITEM_BITMAP* aItem, int aNestLevel ) cons void DS_DATA_MODEL_IO::formatCoordinate( const char * aToken, POINT_COORD & aCoord ) const { m_out->Print( 0, " (%s %s %s", aToken, - double2Str( aCoord.m_Pos.x ).c_str(), - double2Str( aCoord.m_Pos.y ).c_str() ); + FormatDouble2Str( aCoord.m_Pos.x ).c_str(), + FormatDouble2Str( aCoord.m_Pos.y ).c_str() ); switch( aCoord.m_Anchor ) { @@ -452,10 +450,10 @@ void DS_DATA_MODEL_IO::formatRepeatParameters( DS_DATA_ITEM* aItem ) const m_out->Print( 0, " (repeat %d)", aItem->m_RepeatCount ); if( aItem->m_IncrementVector.x ) - m_out->Print( 0, " (incrx %s)", double2Str(aItem-> m_IncrementVector.x ).c_str() ); + m_out->Print( 0, " (incrx %s)", FormatDouble2Str( aItem->m_IncrementVector.x ).c_str() ); if( aItem->m_IncrementVector.y ) - m_out->Print( 0, " (incry %s)", double2Str( aItem->m_IncrementVector.y ).c_str() ); + m_out->Print( 0, " (incry %s)", FormatDouble2Str( aItem->m_IncrementVector.y ).c_str() ); if( aItem->m_IncrementLabel != 1 && aItem->GetType() == DS_DATA_ITEM::DS_TEXT ) m_out->Print( 0, " (incrlabel %d)", aItem->m_IncrementLabel ); diff --git a/common/eda_text.cpp b/common/eda_text.cpp index 3b54e9e928..a1849f78af 100644 --- a/common/eda_text.cpp +++ b/common/eda_text.cpp @@ -789,7 +789,7 @@ void EDA_TEXT::Format( OUTPUTFORMATTER* aFormatter, int aNestLevel, int aControl if( GetLineSpacing() != 1.0 ) { aFormatter->Print( 0, " (line_spacing %s)", - Double2Str( GetLineSpacing() ).c_str() ); + FormatDouble2Str( GetLineSpacing() ).c_str() ); } if( GetTextThickness() ) @@ -810,7 +810,7 @@ void EDA_TEXT::Format( OUTPUTFORMATTER* aFormatter, int aNestLevel, int aControl KiROUND( GetTextColor().r * 255.0 ), KiROUND( GetTextColor().g * 255.0 ), KiROUND( GetTextColor().b * 255.0 ), - Double2Str( GetTextColor().a ).c_str() ); + FormatDouble2Str( GetTextColor().a ).c_str() ); } aFormatter->Print( 0, ")"); // (font diff --git a/common/eda_units.cpp b/common/eda_units.cpp index f846dfd7ea..1596baa2e5 100644 --- a/common/eda_units.cpp +++ b/common/eda_units.cpp @@ -128,12 +128,9 @@ wxString EDA_UNIT_UTILS::GetAbbreviatedUnitsLabel( EDA_UNITS aUnits, EDA_DATA_TY std::string EDA_UNIT_UTILS::FormatAngle( const EDA_ANGLE& aAngle ) { - char temp[50]; - int len; + std::string temp = fmt::format( "{:.10g}", aAngle.AsDegrees() ); - len = snprintf( temp, sizeof( temp ), "%.10g", aAngle.AsDegrees() ); - - return std::string( temp, len ); + return temp; } diff --git a/common/libeval/numeric_evaluator.cpp b/common/libeval/numeric_evaluator.cpp index 8e81bcdde4..3e82a997c5 100644 --- a/common/libeval/numeric_evaluator.cpp +++ b/common/libeval/numeric_evaluator.cpp @@ -110,8 +110,8 @@ void NUMERIC_EVALUATOR::parseSetResult( double val ) // Can be printed as a floating point // Warning: DO NOT use a format like %f or %g, because they can create issues. // especially %g can generate an exponent, incompatible with UNIT_BINDER - // Use the optimized Double2Str - snprintf( m_token.token, m_token.OutLen, "%s", Double2Str( val ).c_str() ); + // Use the optimized UIDouble2Str + snprintf( m_token.token, m_token.OutLen, "%s", UIDouble2Str( val ).c_str() ); } } diff --git a/common/string_utils.cpp b/common/string_utils.cpp index 1a4562f337..29b72c7264 100644 --- a/common/string_utils.cpp +++ b/common/string_utils.cpp @@ -28,6 +28,7 @@ #include #include +#include #include #include // StrPrintf #include @@ -1088,7 +1089,30 @@ void StripTrailingZeros( wxString& aStringValue, unsigned aTrailingZeroAllowed ) } -std::string Double2Str( double aValue ) +std::string FormatDouble2Str( double aValue ) +{ + std::string buf; + + if( aValue != 0.0 && std::fabs( aValue ) <= 0.0001 ) + { + buf = fmt::format( "{:.16f}", aValue ); + + // remove trailing zeros + while( !buf.empty() && buf[buf.size() - 1] == '0' ) + { + buf.pop_back(); + } + } + else + { + buf = fmt::format( "{:.10g}", aValue ); + } + + return buf; +} + + +std::string UIDouble2Str( double aValue ) { char buf[50]; int len; diff --git a/common/stroke_params.cpp b/common/stroke_params.cpp index 2e67bf11af..b94921a4c7 100644 --- a/common/stroke_params.cpp +++ b/common/stroke_params.cpp @@ -236,7 +236,7 @@ void STROKE_PARAMS::Format( OUTPUTFORMATTER* aFormatter, const EDA_IU_SCALE& aIu KiROUND( GetColor().r * 255.0 ), KiROUND( GetColor().g * 255.0 ), KiROUND( GetColor().b * 255.0 ), - Double2Str( GetColor().a ).c_str() ); + FormatDouble2Str( GetColor().a ).c_str() ); } } diff --git a/eeschema/sch_plugins/kicad/sch_sexpr_plugin.cpp b/eeschema/sch_plugins/kicad/sch_sexpr_plugin.cpp index 56b9b33586..01048769d5 100644 --- a/eeschema/sch_plugins/kicad/sch_sexpr_plugin.cpp +++ b/eeschema/sch_plugins/kicad/sch_sexpr_plugin.cpp @@ -972,7 +972,7 @@ void SCH_SEXPR_PLUGIN::saveJunction( SCH_JUNCTION* aJunction, int aNestLevel ) KiROUND( aJunction->GetColor().r * 255.0 ), KiROUND( aJunction->GetColor().g * 255.0 ), KiROUND( aJunction->GetColor().b * 255.0 ), - Double2Str( aJunction->GetColor().a ).c_str() ); + FormatDouble2Str( aJunction->GetColor().a ).c_str() ); m_out->Print( aNestLevel + 1, "(uuid %s)\n", TO_UTF8( aJunction->m_Uuid.AsString() ) ); diff --git a/eeschema/sch_plugins/kicad/sch_sexpr_plugin_common.cpp b/eeschema/sch_plugins/kicad/sch_sexpr_plugin_common.cpp index e08b222f1b..b3cc44beab 100644 --- a/eeschema/sch_plugins/kicad/sch_sexpr_plugin_common.cpp +++ b/eeschema/sch_plugins/kicad/sch_sexpr_plugin_common.cpp @@ -52,7 +52,7 @@ void formatFill( OUTPUTFORMATTER* aFormatter, int aNestLevel, FILL_T aFillMode, KiROUND( aFillColor.r * 255.0 ), KiROUND( aFillColor.g * 255.0 ), KiROUND( aFillColor.b * 255.0 ), - Double2Str( aFillColor.a ).c_str() ); + FormatDouble2Str( aFillColor.a ).c_str() ); } else { diff --git a/gerbview/export_to_pcbnew.cpp b/gerbview/export_to_pcbnew.cpp index 31c128664f..1f80b4e674 100644 --- a/gerbview/export_to_pcbnew.cpp +++ b/gerbview/export_to_pcbnew.cpp @@ -195,23 +195,23 @@ void GBR_TO_PCB_EXPORTER::export_non_copper_item( const GERBER_DRAW_ITEM* aGbrIt if( angle == 360.0 || angle == 0 ) { fprintf( m_fp, "(gr_circle (center %s %s) (end %s %s) (layer %s) (width %s))\n", - Double2Str( MapToPcbUnits(seg_start.x) ).c_str(), - Double2Str( MapToPcbUnits(seg_start.y) ).c_str(), - Double2Str( MapToPcbUnits(seg_end.x) ).c_str(), - Double2Str( MapToPcbUnits(seg_end.y) ).c_str(), + FormatDouble2Str( MapToPcbUnits( seg_start.x ) ).c_str(), + FormatDouble2Str( MapToPcbUnits( seg_start.y ) ).c_str(), + FormatDouble2Str( MapToPcbUnits( seg_end.x ) ).c_str(), + FormatDouble2Str( MapToPcbUnits( seg_end.y ) ).c_str(), TO_UTF8( GetPCBDefaultLayerName( aLayer ) ), - Double2Str( MapToPcbUnits( aGbrItem->m_Size.x ) ).c_str() ); + FormatDouble2Str( MapToPcbUnits( aGbrItem->m_Size.x ) ).c_str() ); } else { fprintf( m_fp, "(gr_arc (start %s %s) (end %s %s) (angle %s) (layer %s) (width %s))\n", - Double2Str( MapToPcbUnits(seg_start.x) ).c_str(), - Double2Str( MapToPcbUnits(seg_start.y) ).c_str(), - Double2Str( MapToPcbUnits(seg_end.x) ).c_str(), - Double2Str( MapToPcbUnits(seg_end.y) ).c_str(), - Double2Str( angle ).c_str(), + FormatDouble2Str( MapToPcbUnits( seg_start.x ) ).c_str(), + FormatDouble2Str( MapToPcbUnits( seg_start.y ) ).c_str(), + FormatDouble2Str( MapToPcbUnits( seg_end.x ) ).c_str(), + FormatDouble2Str( MapToPcbUnits( seg_end.y ) ).c_str(), + FormatDouble2Str( angle ).c_str(), TO_UTF8( GetPCBDefaultLayerName( aLayer ) ), - Double2Str( MapToPcbUnits( aGbrItem->m_Size.x ) ).c_str() ); + FormatDouble2Str( MapToPcbUnits( aGbrItem->m_Size.x ) ).c_str() ); } } break; @@ -222,12 +222,12 @@ void GBR_TO_PCB_EXPORTER::export_non_copper_item( const GERBER_DRAW_ITEM* aGbrIt seg_end.y = -seg_end.y; fprintf( m_fp, "(gr_circle (start %s %s) (end %s %s) (layer %s) (width %s))\n", - Double2Str( MapToPcbUnits( seg_start.x ) ).c_str(), - Double2Str( MapToPcbUnits( seg_start.y ) ).c_str(), - Double2Str( MapToPcbUnits( seg_end.x ) ).c_str(), - Double2Str( MapToPcbUnits( seg_end.y ) ).c_str(), + FormatDouble2Str( MapToPcbUnits( seg_start.x ) ).c_str(), + FormatDouble2Str( MapToPcbUnits( seg_start.y ) ).c_str(), + FormatDouble2Str( MapToPcbUnits( seg_end.x ) ).c_str(), + FormatDouble2Str( MapToPcbUnits( seg_end.y ) ).c_str(), TO_UTF8( GetPCBDefaultLayerName( aLayer ) ), - Double2Str( MapToPcbUnits( aGbrItem->m_Size.x ) ).c_str() ); + FormatDouble2Str( MapToPcbUnits( aGbrItem->m_Size.x ) ).c_str() ); break; case GBR_SEGMENT: @@ -236,12 +236,12 @@ void GBR_TO_PCB_EXPORTER::export_non_copper_item( const GERBER_DRAW_ITEM* aGbrIt seg_end.y = -seg_end.y; fprintf( m_fp, "(gr_line (start %s %s) (end %s %s) (layer %s) (width %s))\n", - Double2Str( MapToPcbUnits( seg_start.x ) ).c_str(), - Double2Str( MapToPcbUnits( seg_start.y ) ).c_str(), - Double2Str( MapToPcbUnits( seg_end.x ) ).c_str(), - Double2Str( MapToPcbUnits( seg_end.y ) ).c_str(), + FormatDouble2Str( MapToPcbUnits( seg_start.x ) ).c_str(), + FormatDouble2Str( MapToPcbUnits( seg_start.y ) ).c_str(), + FormatDouble2Str( MapToPcbUnits( seg_end.x ) ).c_str(), + FormatDouble2Str( MapToPcbUnits( seg_end.y ) ).c_str(), TO_UTF8( GetPCBDefaultLayerName( aLayer ) ), - Double2Str( MapToPcbUnits( aGbrItem->m_Size.x ) ).c_str() ); + FormatDouble2Str( MapToPcbUnits( aGbrItem->m_Size.x ) ).c_str() ); break; } } @@ -263,10 +263,10 @@ void GBR_TO_PCB_EXPORTER::export_via( const EXPORT_VIA& aVia ) // Layers are Front to Back fprintf( m_fp, " (via (at %s %s) (size %s) (drill %s)", - Double2Str( MapToPcbUnits( via_pos.x ) ).c_str(), - Double2Str( MapToPcbUnits( via_pos.y ) ).c_str(), - Double2Str( MapToPcbUnits( aVia.m_Size ) ).c_str(), - Double2Str( MapToPcbUnits( aVia.m_Drill ) ).c_str() ); + FormatDouble2Str( MapToPcbUnits( via_pos.x ) ).c_str(), + FormatDouble2Str( MapToPcbUnits( via_pos.y ) ).c_str(), + FormatDouble2Str( MapToPcbUnits( aVia.m_Size ) ).c_str(), + FormatDouble2Str( MapToPcbUnits( aVia.m_Drill ) ).c_str() ); fprintf( m_fp, " (layers %s %s))\n", TO_UTF8( GetPCBDefaultLayerName( F_Cu ) ), @@ -329,11 +329,11 @@ void GBR_TO_PCB_EXPORTER::writeCopperLineItem( const VECTOR2I& aStart, const VEC int aWidth, int aLayer ) { fprintf( m_fp, "(segment (start %s %s) (end %s %s) (width %s) (layer %s) (net 0))\n", - Double2Str( MapToPcbUnits(aStart.x) ).c_str(), - Double2Str( MapToPcbUnits(aStart.y) ).c_str(), - Double2Str( MapToPcbUnits(aEnd.x) ).c_str(), - Double2Str( MapToPcbUnits(aEnd.y) ).c_str(), - Double2Str( MapToPcbUnits( aWidth ) ).c_str(), + FormatDouble2Str( MapToPcbUnits(aStart.x) ).c_str(), + FormatDouble2Str( MapToPcbUnits(aStart.y) ).c_str(), + FormatDouble2Str( MapToPcbUnits(aEnd.x) ).c_str(), + FormatDouble2Str( MapToPcbUnits(aEnd.y) ).c_str(), + FormatDouble2Str( MapToPcbUnits( aWidth ) ).c_str(), TO_UTF8( GetPCBDefaultLayerName( aLayer ) ) ); } @@ -435,10 +435,10 @@ void GBR_TO_PCB_EXPORTER::writePcbFilledCircle( const VECTOR2I& aCenterPosition, { fprintf( m_fp, "(gr_circle (center %s %s) (end %s %s)", - Double2Str( MapToPcbUnits( aCenterPosition.x ) ).c_str(), - Double2Str( MapToPcbUnits( aCenterPosition.y ) ).c_str(), - Double2Str( MapToPcbUnits( aCenterPosition.x + aRadius ) ).c_str(), - Double2Str( MapToPcbUnits( aCenterPosition.y ) ).c_str() ); + FormatDouble2Str( MapToPcbUnits( aCenterPosition.x ) ).c_str(), + FormatDouble2Str( MapToPcbUnits( aCenterPosition.y ) ).c_str(), + FormatDouble2Str( MapToPcbUnits( aCenterPosition.x + aRadius ) ).c_str(), + FormatDouble2Str( MapToPcbUnits( aCenterPosition.y ) ).c_str() ); fprintf( m_fp, "(layer %s) (width 0) (fill solid) )\n", @@ -505,8 +505,8 @@ void GBR_TO_PCB_EXPORTER::writePcbPolygon( const SHAPE_POLY_SET& aPolys, int aLa } fprintf( m_fp, " (xy %s %s)", - Double2Str( MapToPcbUnits( poly.CPoint( ii ).x + aOffset.x ) ).c_str(), - Double2Str( MapToPcbUnits( -poly.CPoint( ii ).y + aOffset.y ) ).c_str() ); + FormatDouble2Str( MapToPcbUnits( poly.CPoint( ii ).x + aOffset.x ) ).c_str(), + FormatDouble2Str( MapToPcbUnits( -poly.CPoint( ii ).y + aOffset.y ) ).c_str() ); } fprintf( m_fp, ")" ); @@ -560,8 +560,8 @@ void GBR_TO_PCB_EXPORTER::writePcbZoneItem( const GERBER_DRAW_ITEM* aGbrItem, in fprintf( m_fp, "\n " ); } - fprintf( m_fp, " (xy %s %s)", Double2Str( MapToPcbUnits( poly.CPoint( ii ).x ) ).c_str(), - Double2Str( MapToPcbUnits( -poly.CPoint( ii ).y ) ).c_str() ); + fprintf( m_fp, " (xy %s %s)", FormatDouble2Str( MapToPcbUnits( poly.CPoint( ii ).x ) ).c_str(), + FormatDouble2Str( MapToPcbUnits( -poly.CPoint( ii ).y ) ).c_str() ); } fprintf( m_fp, ")\n" ); diff --git a/include/eda_units.h b/include/eda_units.h index 5e66c4447f..8284141b5e 100644 --- a/include/eda_units.h +++ b/include/eda_units.h @@ -82,6 +82,8 @@ namespace EDA_UNIT_UTILS /** * Converts \a aAngle from board units to a string appropriate for writing to file. * + * This should only be used for writing to files as it ignores locale + * * @note Internal angles for board items can be either degrees or tenths of degree * on how KiCad is built. * @param aAngle A angle value to convert. @@ -93,6 +95,8 @@ namespace EDA_UNIT_UTILS * Converts \a aValue from internal units to a string appropriate for writing * to file. * + * This should only be used for writing to files as it ignores locale + * * @note Internal units for board items can be either deci-mils or nanometers depending * on how KiCad is built. * diff --git a/include/string_utils.h b/include/string_utils.h index 71823618dc..e18b1dc6b2 100644 --- a/include/string_utils.h +++ b/include/string_utils.h @@ -353,6 +353,16 @@ void StripTrailingZeros( wxString& aStringValue, unsigned aTrailingZeroAllowed = * this helper function uses the %f format when needed, or %g when %f is * not well working and then removes trailing 0 */ -std::string Double2Str( double aValue ); +std::string UIDouble2Str( double aValue ); + +/** + * Print a float number without using scientific notation and no trailing 0 + * This function is intended in uses to write to file, it ignores locale + * + * We cannot always just use the %g or the %f format to print a fp number + * this helper function uses the %f format when needed, or %g when %f is + * not well working and then removes trailing 0 + */ +std::string FormatDouble2Str( double aValue ); #endif // STRING_UTILS_H diff --git a/pcbnew/board_stackup_manager/board_stackup.cpp b/pcbnew/board_stackup_manager/board_stackup.cpp index fcdf499891..4d4c3c09c6 100644 --- a/pcbnew/board_stackup_manager/board_stackup.cpp +++ b/pcbnew/board_stackup_manager/board_stackup.cpp @@ -266,7 +266,7 @@ wxString BOARD_STACKUP_ITEM::FormatEpsilonR( int aDielectricSubLayer ) const { // return a wxString to print/display Epsilon R // note: we do not want scientific notation - wxString txt = Double2Str( GetEpsilonR( aDielectricSubLayer ) ); + wxString txt = UIDouble2Str( GetEpsilonR( aDielectricSubLayer ) ); return txt; } @@ -275,7 +275,7 @@ wxString BOARD_STACKUP_ITEM::FormatLossTangent( int aDielectricSubLayer ) const { // return a wxString to print/display Loss Tangent // note: we do not want scientific notation - wxString txt = Double2Str( GetLossTangent( aDielectricSubLayer ) ); + wxString txt = UIDouble2Str( GetLossTangent( aDielectricSubLayer ) ); return txt; } @@ -666,7 +666,7 @@ void BOARD_STACKUP::FormatBoardStackup( OUTPUTFORMATTER* aFormatter, if( item->HasLossTangentValue() && item->HasMaterialValue( idx ) ) aFormatter->Print( 0, " (loss_tangent %s)", - Double2Str(item->GetLossTangent( idx ) ).c_str() ); + FormatDouble2Str( item->GetLossTangent( idx ) ).c_str() ); } aFormatter->Print( 0, ")\n" ); diff --git a/pcbnew/board_stackup_manager/dielectric_material.cpp b/pcbnew/board_stackup_manager/dielectric_material.cpp index 7ad7ef3c19..cdcfa7b1a5 100644 --- a/pcbnew/board_stackup_manager/dielectric_material.cpp +++ b/pcbnew/board_stackup_manager/dielectric_material.cpp @@ -26,7 +26,7 @@ #include "stackup_predefined_prms.h" #include "dielectric_material.h" #include -#include // for Double2Str() +#include // for UIDouble2Str() // A list of available substrate material @@ -70,7 +70,7 @@ wxString DIELECTRIC_SUBSTRATE::FormatEpsilonR() { // return a wxString to print/display Epsilon R // note: we do not want scientific notation - wxString txt = Double2Str( m_EpsilonR ); + wxString txt = UIDouble2Str( m_EpsilonR ); return txt; } @@ -79,7 +79,7 @@ wxString DIELECTRIC_SUBSTRATE::FormatLossTangent() { // return a wxString to print/display Loss Tangent // note: we do not want scientific notation - wxString txt = Double2Str( m_LossTangent ); + wxString txt = UIDouble2Str( m_LossTangent ); return txt; } diff --git a/pcbnew/board_stackup_manager/panel_board_stackup.cpp b/pcbnew/board_stackup_manager/panel_board_stackup.cpp index b3ad40218e..5df4e8d6a1 100644 --- a/pcbnew/board_stackup_manager/panel_board_stackup.cpp +++ b/pcbnew/board_stackup_manager/panel_board_stackup.cpp @@ -48,7 +48,7 @@ #include #include -#include // for Double2Str() +#include // for UIDouble2Str() // Some wx widget ID to know what widget has fired a event: @@ -573,7 +573,7 @@ void PANEL_SETUP_BOARD_STACKUP::synchronizeWithBoard( bool aFullSync ) if( item->HasEpsilonRValue() ) { - wxString txt = Double2Str( item->GetEpsilonR( sub_item ) ); + wxString txt = UIDouble2Str( item->GetEpsilonR( sub_item ) ); wxTextCtrl* textCtrl = dynamic_cast( ui_row_item.m_EpsilonCtrl ); if( textCtrl ) @@ -582,7 +582,7 @@ void PANEL_SETUP_BOARD_STACKUP::synchronizeWithBoard( bool aFullSync ) if( item->HasLossTangentValue() ) { - wxString txt = Double2Str( item->GetLossTangent( sub_item ) ); + wxString txt = UIDouble2Str( item->GetLossTangent( sub_item ) ); wxTextCtrl* textCtrl = dynamic_cast( ui_row_item.m_LossTgCtrl ); if( textCtrl ) @@ -832,7 +832,7 @@ BOARD_STACKUP_ROW_UI_ITEM PANEL_SETUP_BOARD_STACKUP::createRowData( int aRow, if( item->HasEpsilonRValue() ) { - wxString txt = Double2Str( item->GetEpsilonR( aSublayerIdx ) ); + wxString txt = UIDouble2Str( item->GetEpsilonR( aSublayerIdx ) ); wxTextCtrl* textCtrl = new wxTextCtrl( m_scGridWin, wxID_ANY, wxEmptyString, wxDefaultPosition, m_numericFieldsSize ); textCtrl->ChangeValue( txt ); @@ -846,7 +846,7 @@ BOARD_STACKUP_ROW_UI_ITEM PANEL_SETUP_BOARD_STACKUP::createRowData( int aRow, if( item->HasLossTangentValue() ) { - wxString txt = Double2Str( item->GetLossTangent( aSublayerIdx ) );; + wxString txt = UIDouble2Str( item->GetLossTangent( aSublayerIdx ) );; wxTextCtrl* textCtrl = new wxTextCtrl( m_scGridWin, wxID_ANY, wxEmptyString, wxDefaultPosition, m_numericFieldsSize ); textCtrl->ChangeValue( txt ); diff --git a/pcbnew/plugins/kicad/pcb_plugin.cpp b/pcbnew/plugins/kicad/pcb_plugin.cpp index 05f20a16c6..b8ed8c4479 100644 --- a/pcbnew/plugins/kicad/pcb_plugin.cpp +++ b/pcbnew/plugins/kicad/pcb_plugin.cpp @@ -608,7 +608,7 @@ void PCB_PLUGIN::formatSetup( const BOARD* aBoard, int aNestLevel ) const if( dsnSettings.m_SolderPasteMarginRatio != 0 ) { m_out->Print( aNestLevel+1, "(pad_to_paste_clearance_ratio %s)\n", - Double2Str( dsnSettings.m_SolderPasteMarginRatio ).c_str() ); + FormatDouble2Str( dsnSettings.m_SolderPasteMarginRatio ).c_str() ); } if( dsnSettings.m_AllowSoldermaskBridgesInFPs ) @@ -1268,7 +1268,7 @@ void PCB_PLUGIN::format( const FOOTPRINT* aFootprint, int aNestLevel ) const if( aFootprint->GetLocalSolderPasteMarginRatio() != 0 ) { m_out->Print( aNestLevel+1, "(solder_paste_ratio %s)\n", - Double2Str( aFootprint->GetLocalSolderPasteMarginRatio() ).c_str() ); + FormatDouble2Str( aFootprint->GetLocalSolderPasteMarginRatio() ).c_str() ); } if( aFootprint->GetLocalClearance() != 0 ) @@ -1384,19 +1384,19 @@ void PCB_PLUGIN::format( const FOOTPRINT* aFootprint, int aNestLevel ) const m_out->Print( aNestLevel+2, "(opacity %0.4f)", bs3D->m_Opacity ); m_out->Print( aNestLevel+2, "(offset (xyz %s %s %s))\n", - Double2Str( bs3D->m_Offset.x ).c_str(), - Double2Str( bs3D->m_Offset.y ).c_str(), - Double2Str( bs3D->m_Offset.z ).c_str() ); + FormatDouble2Str( bs3D->m_Offset.x ).c_str(), + FormatDouble2Str( bs3D->m_Offset.y ).c_str(), + FormatDouble2Str( bs3D->m_Offset.z ).c_str() ); m_out->Print( aNestLevel+2, "(scale (xyz %s %s %s))\n", - Double2Str( bs3D->m_Scale.x ).c_str(), - Double2Str( bs3D->m_Scale.y ).c_str(), - Double2Str( bs3D->m_Scale.z ).c_str() ); + FormatDouble2Str( bs3D->m_Scale.x ).c_str(), + FormatDouble2Str( bs3D->m_Scale.y ).c_str(), + FormatDouble2Str( bs3D->m_Scale.z ).c_str() ); m_out->Print( aNestLevel+2, "(rotate (xyz %s %s %s))\n", - Double2Str( bs3D->m_Rotation.x ).c_str(), - Double2Str( bs3D->m_Rotation.y ).c_str(), - Double2Str( bs3D->m_Rotation.z ).c_str() ); + FormatDouble2Str( bs3D->m_Rotation.x ).c_str(), + FormatDouble2Str( bs3D->m_Rotation.y ).c_str(), + FormatDouble2Str( bs3D->m_Rotation.z ).c_str() ); m_out->Print( aNestLevel+1, ")\n" ); } @@ -1607,7 +1607,7 @@ void PCB_PLUGIN::format( const PAD* aPad, int aNestLevel ) const if( aPad->GetShape() == PAD_SHAPE::ROUNDRECT || aPad->GetShape() == PAD_SHAPE::CHAMFERED_RECT) { m_out->Print( 0, " (roundrect_rratio %s)", - Double2Str( aPad->GetRoundRectRadiusRatio() ).c_str() ); + FormatDouble2Str( aPad->GetRoundRectRadiusRatio() ).c_str() ); } // Output the chamfer corners for chamfered rect pads @@ -1616,7 +1616,7 @@ void PCB_PLUGIN::format( const PAD* aPad, int aNestLevel ) const m_out->Print( 0, "\n" ); m_out->Print( aNestLevel+1, "(chamfer_ratio %s)", - Double2Str( aPad->GetChamferRectRatio() ).c_str() ); + FormatDouble2Str( aPad->GetChamferRectRatio() ).c_str() ); m_out->Print( 0, " (chamfer" ); @@ -1682,7 +1682,7 @@ void PCB_PLUGIN::format( const PAD* aPad, int aNestLevel ) const if( aPad->GetLocalSolderPasteMarginRatio() != 0 ) { StrPrintf( &output, " (solder_paste_margin_ratio %s)", - Double2Str( aPad->GetLocalSolderPasteMarginRatio() ).c_str() ); + FormatDouble2Str( aPad->GetLocalSolderPasteMarginRatio() ).c_str() ); } if( aPad->GetLocalClearance() != 0 ) @@ -2331,20 +2331,20 @@ void PCB_PLUGIN::format( const ZONE* aZone, int aNestLevel ) const m_out->Print( aNestLevel+2, "(hatch_thickness %s) (hatch_gap %s) (hatch_orientation %s)", EDA_UNIT_UTILS::FormatInternalUnits( pcbIUScale, aZone->GetHatchThickness() ).c_str(), EDA_UNIT_UTILS::FormatInternalUnits( pcbIUScale, aZone->GetHatchGap() ).c_str(), - Double2Str( aZone->GetHatchOrientation().AsDegrees() ).c_str() ); + FormatDouble2Str( aZone->GetHatchOrientation().AsDegrees() ).c_str() ); if( aZone->GetHatchSmoothingLevel() > 0 ) { m_out->Print( 0, "\n" ); m_out->Print( aNestLevel+2, "(hatch_smoothing_level %d) (hatch_smoothing_value %s)", aZone->GetHatchSmoothingLevel(), - Double2Str( aZone->GetHatchSmoothingValue() ).c_str() ); + FormatDouble2Str( aZone->GetHatchSmoothingValue() ).c_str() ); } m_out->Print( 0, "\n" ); m_out->Print( aNestLevel+2, "(hatch_border_algorithm %s) (hatch_min_hole_area %s)", aZone->GetHatchBorderAlgorithm() ? "hatch_thickness" : "min_thickness", - Double2Str( aZone->GetHatchHoleMinArea() ).c_str() ); + FormatDouble2Str( aZone->GetHatchHoleMinArea() ).c_str() ); } m_out->Print( 0, ")\n" );