You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
3.7 KiB

3 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you may find one here:
  18. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. * or you may search the http://www.gnu.org website for the version 2 license,
  20. * or you may write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. #include <array_axis.h>
  24. #include <increment.h>
  25. /**
  26. * @return False for schemes like 0,1...9,10
  27. * True for schemes like A,B..Z,AA (where the tens column starts with char 0)
  28. */
  29. static bool schemeNonUnitColsStartAt0( ARRAY_AXIS::NUMBERING_TYPE type )
  30. {
  31. return type == ARRAY_AXIS::NUMBERING_TYPE::NUMBERING_ALPHA_FULL
  32. || type == ARRAY_AXIS::NUMBERING_TYPE::NUMBERING_ALPHA_NO_IOSQXZ;
  33. }
  34. ARRAY_AXIS::ARRAY_AXIS() : m_type( NUMBERING_TYPE::NUMBERING_NUMERIC ), m_offset( 0 ), m_step( 1 )
  35. {
  36. }
  37. const wxString& ARRAY_AXIS::GetAlphabet() const
  38. {
  39. static const wxString alphaNumeric = wxS( "0123456789" );
  40. static const wxString alphaHex = wxS( "0123456789ABCDEF" );
  41. static const wxString alphaFull = wxS( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
  42. static const wxString alphaNoIOSQXZ = wxS( "ABCDEFGHJKLMNPRTUVWY" );
  43. switch( m_type )
  44. {
  45. default:
  46. case NUMBERING_NUMERIC:
  47. return alphaNumeric;
  48. case NUMBERING_HEX:
  49. return alphaHex;
  50. case NUMBERING_ALPHA_NO_IOSQXZ:
  51. return alphaNoIOSQXZ;
  52. case NUMBERING_ALPHA_FULL:
  53. return alphaFull;
  54. }
  55. }
  56. std::optional<int> ARRAY_AXIS::getNumberingOffset( const wxString& str ) const
  57. {
  58. if( str.length() == 0 )
  59. return std::optional<int>{};
  60. const wxString& alphabet = GetAlphabet();
  61. int offset = 0;
  62. const int radix = alphabet.length();
  63. for( unsigned i = 0; i < str.length(); i++ )
  64. {
  65. int chIndex = alphabet.Find( str[i], false );
  66. if( chIndex == wxNOT_FOUND )
  67. return std::optional<int>{};
  68. const bool start0 = schemeNonUnitColsStartAt0( m_type );
  69. // eg "AA" is actually index 27, not 26
  70. if( start0 && i < str.length() - 1 )
  71. chIndex++;
  72. offset *= radix;
  73. offset += chIndex;
  74. }
  75. return std::optional<int>{ offset };
  76. }
  77. void ARRAY_AXIS::SetAxisType( NUMBERING_TYPE aType )
  78. {
  79. m_type = aType;
  80. }
  81. bool ARRAY_AXIS::SetOffset( const wxString& aOffsetName )
  82. {
  83. std::optional<int> offset = getNumberingOffset( aOffsetName );
  84. // The string does not decode to a valid offset
  85. if( !offset )
  86. return false;
  87. SetOffset( *offset );
  88. return true;
  89. }
  90. void ARRAY_AXIS::SetOffset( int aOffset )
  91. {
  92. m_offset = aOffset;
  93. }
  94. int ARRAY_AXIS::GetOffset() const
  95. {
  96. return m_offset;
  97. }
  98. void ARRAY_AXIS::SetStep( int aStep )
  99. {
  100. m_step = aStep;
  101. }
  102. wxString ARRAY_AXIS::GetItemNumber( int n ) const
  103. {
  104. wxString itemNum;
  105. const wxString& alphabet = GetAlphabet();
  106. const bool nonUnitColsStartAt0 = schemeNonUnitColsStartAt0( m_type );
  107. n = m_offset + m_step * n;
  108. return AlphabeticFromIndex( n, alphabet, nonUnitColsStartAt0 );
  109. }