diff --git a/common/plugins/altium/altium_parser.cpp b/common/plugins/altium/altium_parser.cpp index 52e504b301..4bd5568ae1 100644 --- a/common/plugins/altium/altium_parser.cpp +++ b/common/plugins/altium/altium_parser.cpp @@ -25,6 +25,7 @@ #include #include +#include #include #include #include @@ -149,6 +150,29 @@ std::map ALTIUM_PARSER::ReadProperties() return kv; } + +int32_t ALTIUM_PARSER::ConvertToKicadUnit( const double aValue ) +{ + const double int_limit = ( std::numeric_limits::max() - 1 ) / 2.54; + + int32_t iu = KiROUND( Clamp( -int_limit, aValue, int_limit ) * 2.54 ); + + // Altium stores metric units up to 0.001mm (1000nm) in accuracy. This code fixes rounding errors. + // Because imperial units > 0.01mil are always even, this workaround should never trigger for them. + switch( iu % 1000 ) + { + case 1: + case -999: + return iu - 1; + case 999: + case -1: + return iu + 1; + default: + return iu; + } +} + + int ALTIUM_PARSER::PropertiesReadInt( const std::map& aProperties, const wxString& aKey, int aDefault ) { @@ -156,6 +180,7 @@ int ALTIUM_PARSER::PropertiesReadInt( return value == aProperties.end() ? aDefault : wxAtoi( value->second ); } + double ALTIUM_PARSER::PropertiesReadDouble( const std::map& aProperties, const wxString& aKey, double aDefault ) { diff --git a/common/plugins/altium/altium_parser.h b/common/plugins/altium/altium_parser.h index 1424cc3fba..696553663c 100644 --- a/common/plugins/altium/altium_parser.h +++ b/common/plugins/altium/altium_parser.h @@ -28,7 +28,6 @@ #include #include -#include #include #include @@ -138,26 +137,7 @@ public: std::map ReadProperties(); - static int32_t ConvertToKicadUnit( const double aValue ) - { - const double int_limit = ( std::numeric_limits::max() - 1 ) / 2.54; - - int32_t iu = KiROUND( Clamp( -int_limit, aValue, int_limit ) * 2.54 ); - - // Altium stores metric units up to 0.001mm (1000nm) in accuracy. This code fixes rounding errors. - // Because imperial units > 0.01mil are always even, this workaround should never trigger for them. - switch( iu % 1000 ) - { - case 1: - case -999: - return iu - 1; - case 999: - case -1: - return iu + 1; - default: - return iu; - } - } + static int32_t ConvertToKicadUnit( const double aValue ); static int PropertiesReadInt( const std::map& aProperties, const wxString& aKey, int aDefault ); diff --git a/eeschema/lib_circle.cpp b/eeschema/lib_circle.cpp index 6f8bcf9336..b15155ff51 100644 --- a/eeschema/lib_circle.cpp +++ b/eeschema/lib_circle.cpp @@ -265,6 +265,12 @@ void LIB_CIRCLE::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, MSG_PANEL_ITEMS& aList } +int LIB_CIRCLE::GetRadius() const +{ + return KiROUND( GetLineLength( m_EndPos, m_Pos ) ); +} + + wxString LIB_CIRCLE::GetSelectMenuText( EDA_UNITS aUnits ) const { return wxString::Format( _( "Circle, radius %s" ), diff --git a/eeschema/lib_circle.h b/eeschema/lib_circle.h index 19ddb00dbe..cfc0e205f7 100644 --- a/eeschema/lib_circle.h +++ b/eeschema/lib_circle.h @@ -26,7 +26,6 @@ #define LIB_CIRCLE_H #include -#include // for KiROUND #include // for GetLineLength @@ -81,7 +80,7 @@ public: void SetWidth( int aWidth ) override { m_Width = aWidth; } void SetRadius( int aRadius ) { m_EndPos = wxPoint( m_Pos.x + aRadius, m_Pos.y ); } - int GetRadius() const { return KiROUND( GetLineLength( m_EndPos, m_Pos ) ); } + int GetRadius() const; wxString GetSelectMenuText( EDA_UNITS aUnits ) const override; diff --git a/eeschema/sch_no_connect.h b/eeschema/sch_no_connect.h index 62000810ce..d2374dc49f 100644 --- a/eeschema/sch_no_connect.h +++ b/eeschema/sch_no_connect.h @@ -31,7 +31,6 @@ #include -#include // for KiROUND class NETLIST_OBJECT_LIST; diff --git a/eeschema/sch_plugins/kicad/sch_sexpr_parser.cpp b/eeschema/sch_plugins/kicad/sch_sexpr_parser.cpp index afcdeb6e16..07f21dd43d 100644 --- a/eeschema/sch_plugins/kicad/sch_sexpr_parser.cpp +++ b/eeschema/sch_plugins/kicad/sch_sexpr_parser.cpp @@ -40,6 +40,7 @@ #include #include #include +#include // KiROUND, Clamp #include #include #include @@ -450,6 +451,29 @@ double SCH_SEXPR_PARSER::parseDouble() } +int SCH_SEXPR_PARSER::parseInternalUnits() +{ + auto retval = parseDouble() * IU_PER_MM; + + // Schematic internal units are represented as integers. Any values that are + // larger or smaller than the schematic units represent undefined behavior for + // the system. Limit values to the largest that can be displayed on the screen. + double int_limit = std::numeric_limits::max() * 0.7071; // 0.7071 = roughly 1/sqrt(2) + + return KiROUND( Clamp( -int_limit, retval, int_limit ) ); +} + + +int SCH_SEXPR_PARSER::parseInternalUnits( const char* aExpected ) +{ + auto retval = parseDouble( aExpected ) * IU_PER_MM; + + double int_limit = std::numeric_limits::max() * 0.7071; + + return KiROUND( Clamp( -int_limit, retval, int_limit ) ); +} + + void SCH_SEXPR_PARSER::parseStroke( STROKE_PARAMS& aStroke ) { wxCHECK_RET( CurTok() == T_stroke, diff --git a/eeschema/sch_plugins/kicad/sch_sexpr_parser.h b/eeschema/sch_plugins/kicad/sch_sexpr_parser.h index 970b1fdc1b..7674182066 100644 --- a/eeschema/sch_plugins/kicad/sch_sexpr_parser.h +++ b/eeschema/sch_plugins/kicad/sch_sexpr_parser.h @@ -32,7 +32,6 @@ #define __SCH_SEXPR_PARSER_H__ #include // IU_PER_MM -#include // KiROUND, Clamp #include #include @@ -123,26 +122,9 @@ class SCH_SEXPR_PARSER : public SCHEMATIC_LEXER return parseDouble( GetTokenText( aToken ) ); } - inline int parseInternalUnits() - { - auto retval = parseDouble() * IU_PER_MM; + int parseInternalUnits(); - // Schematic internal units are represented as integers. Any values that are - // larger or smaller than the schematic units represent undefined behavior for - // the system. Limit values to the largest that can be displayed on the screen. - double int_limit = std::numeric_limits::max() * 0.7071; // 0.7071 = roughly 1/sqrt(2) - - return KiROUND( Clamp( -int_limit, retval, int_limit ) ); - } - - inline int parseInternalUnits( const char* aExpected ) - { - auto retval = parseDouble( aExpected ) * IU_PER_MM; - - double int_limit = std::numeric_limits::max() * 0.7071; - - return KiROUND( Clamp( -int_limit, retval, int_limit ) ); - } + int parseInternalUnits( const char* aExpected ); inline int parseInternalUnits( TSCHEMATIC_T::T aToken ) { diff --git a/pcbnew/pcb_shape.cpp b/pcbnew/pcb_shape.cpp index efd8dc981a..a03505d70b 100644 --- a/pcbnew/pcb_shape.cpp +++ b/pcbnew/pcb_shape.cpp @@ -41,6 +41,7 @@ #include #include #include +#include // for KiROUND PCB_SHAPE::PCB_SHAPE( BOARD_ITEM* aParent, KICAD_T idtype ) : @@ -440,6 +441,15 @@ double PCB_SHAPE::GetArcAngleEnd() const } +int PCB_SHAPE::GetRadius() const +{ + double radius = GetLineLength( m_start, m_end ); + + // don't allow degenerate arcs + return std::max( 1, KiROUND( radius ) ); +} + + void PCB_SHAPE::SetArcGeometry( const wxPoint& aStart, const wxPoint& aMid, const wxPoint& aEnd ) { SetArcStart( aStart ); diff --git a/pcbnew/pcb_shape.h b/pcbnew/pcb_shape.h index 9c25ed259f..91a11643aa 100644 --- a/pcbnew/pcb_shape.h +++ b/pcbnew/pcb_shape.h @@ -28,7 +28,6 @@ #include #include #include -#include // for KiROUND #include #include #include @@ -198,13 +197,7 @@ public: * returns the radius of this item * Has meaning only for arc and circle */ - int GetRadius() const - { - double radius = GetLineLength( m_start, m_end ); - - // don't allow degenerate arcs - return std::max( 1, KiROUND( radius ) ); - } + int GetRadius() const; /** * Initialize the start arc point. can be used for circles diff --git a/pcbnew/pcbplot.h b/pcbnew/pcbplot.h index 04e3c9c261..2de7a71ad4 100644 --- a/pcbnew/pcbplot.h +++ b/pcbnew/pcbplot.h @@ -30,7 +30,6 @@ #define PCBPLOT_H_ #include -#include // for KiROUND #include #include #include diff --git a/pcbnew/plugins/kicad/pcb_parser.cpp b/pcbnew/plugins/kicad/pcb_parser.cpp index bc0d58c1d1..cce5924c3e 100644 --- a/pcbnew/plugins/kicad/pcb_parser.cpp +++ b/pcbnew/plugins/kicad/pcb_parser.cpp @@ -54,6 +54,7 @@ #include #include // for RECT_CHAMFER_POSITIONS definition #include +#include // KiROUND, Clamp #include using namespace PCB_KEYS_T; @@ -167,6 +168,40 @@ double PCB_PARSER::parseDouble() } +int PCB_PARSER::parseBoardUnits() +{ + // There should be no major rounding issues here, since the values in + // the file are in mm and get converted to nano-meters. + // See test program tools/test-nm-biu-to-ascii-mm-round-tripping.cpp + // to confirm or experiment. Use a similar strategy in both places, here + // and in the test program. Make that program with: + // $ make test-nm-biu-to-ascii-mm-round-tripping + auto retval = parseDouble() * IU_PER_MM; + + // N.B. we currently represent board units as integers. Any values that are + // larger or smaller than those board units represent undefined behavior for + // the system. We limit values to the largest that is visible on the screen + // This is the diagonal distance of the full screen ~1.5m + double int_limit = std::numeric_limits::max() * 0.7071; // 0.7071 = roughly 1/sqrt(2) + return KiROUND( Clamp( -int_limit, retval, int_limit ) ); +} + + +int PCB_PARSER::parseBoardUnits( const char* aExpected ) +{ + auto retval = parseDouble( aExpected ) * IU_PER_MM; + + // N.B. we currently represent board units as integers. Any values that are + // larger or smaller than those board units represent undefined behavior for + // the system. We limit values to the largest that is visible on the screen + double int_limit = std::numeric_limits::max() * 0.7071; + + // Use here #KiROUND, not EKIROUND (see comments about them) when having a function as + // argument, because it will be called twice with #KIROUND. + return KiROUND( Clamp( -int_limit, retval, int_limit ) ); +} + + bool PCB_PARSER::parseBool() { T token = NextTok(); diff --git a/pcbnew/plugins/kicad/pcb_parser.h b/pcbnew/plugins/kicad/pcb_parser.h index 812931b2cf..19711f6699 100644 --- a/pcbnew/plugins/kicad/pcb_parser.h +++ b/pcbnew/plugins/kicad/pcb_parser.h @@ -33,7 +33,6 @@ #include // IU_PER_MM #include #include // PCB_LAYER_ID -#include // KiROUND, Clamp #include #include @@ -284,37 +283,9 @@ private: return parseDouble( GetTokenText( aToken ) ); } - inline int parseBoardUnits() - { - // There should be no major rounding issues here, since the values in - // the file are in mm and get converted to nano-meters. - // See test program tools/test-nm-biu-to-ascii-mm-round-tripping.cpp - // to confirm or experiment. Use a similar strategy in both places, here - // and in the test program. Make that program with: - // $ make test-nm-biu-to-ascii-mm-round-tripping - auto retval = parseDouble() * IU_PER_MM; + int parseBoardUnits(); - // N.B. we currently represent board units as integers. Any values that are - // larger or smaller than those board units represent undefined behavior for - // the system. We limit values to the largest that is visible on the screen - // This is the diagonal distance of the full screen ~1.5m - double int_limit = std::numeric_limits::max() * 0.7071; // 0.7071 = roughly 1/sqrt(2) - return KiROUND( Clamp( -int_limit, retval, int_limit ) ); - } - - inline int parseBoardUnits( const char* aExpected ) - { - auto retval = parseDouble( aExpected ) * IU_PER_MM; - - // N.B. we currently represent board units as integers. Any values that are - // larger or smaller than those board units represent undefined behavior for - // the system. We limit values to the largest that is visible on the screen - double int_limit = std::numeric_limits::max() * 0.7071; - - // Use here #KiROUND, not EKIROUND (see comments about them) when having a function as - // argument, because it will be called twice with #KIROUND. - return KiROUND( Clamp( -int_limit, retval, int_limit ) ); - } + int parseBoardUnits( const char* aExpected ); inline int parseBoardUnits( PCB_KEYS_T::T aToken ) {