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.

158 lines
3.9 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2019 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. /**
  25. * @return False for schemes like 0,1...9,10
  26. * True for schemes like A,B..Z,AA (where the tens column starts with char 0)
  27. */
  28. static bool schemeNonUnitColsStartAt0( ARRAY_AXIS::NUMBERING_TYPE type )
  29. {
  30. return type == ARRAY_AXIS::NUMBERING_TYPE::NUMBERING_ALPHA_FULL
  31. || type == ARRAY_AXIS::NUMBERING_TYPE::NUMBERING_ALPHA_NO_IOSQXZ;
  32. }
  33. ARRAY_AXIS::ARRAY_AXIS() : m_type( NUMBERING_TYPE::NUMBERING_NUMERIC ), m_offset( 0 ), m_step( 1 )
  34. {
  35. }
  36. const wxString& ARRAY_AXIS::GetAlphabet() const
  37. {
  38. static const wxString alphaNumeric = "0123456789";
  39. static const wxString alphaHex = "0123456789ABCDEF";
  40. static const wxString alphaFull = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  41. static const wxString alphaNoIOSQXZ = "ABCDEFGHJKLMNPRTUVWY";
  42. switch( m_type )
  43. {
  44. default:
  45. case NUMBERING_NUMERIC:
  46. return alphaNumeric;
  47. case NUMBERING_HEX:
  48. return alphaHex;
  49. case NUMBERING_ALPHA_NO_IOSQXZ:
  50. return alphaNoIOSQXZ;
  51. case NUMBERING_ALPHA_FULL:
  52. return alphaFull;
  53. }
  54. }
  55. OPT<int> ARRAY_AXIS::getNumberingOffset( const wxString& str ) const
  56. {
  57. if( str.length() == 0 )
  58. return OPT<int>{};
  59. const wxString& alphabet = GetAlphabet();
  60. int offset = 0;
  61. const int radix = alphabet.length();
  62. for( unsigned i = 0; i < str.length(); i++ )
  63. {
  64. int chIndex = alphabet.Find( str[i], false );
  65. if( chIndex == wxNOT_FOUND )
  66. return OPT<int>{};
  67. const bool start0 = schemeNonUnitColsStartAt0( m_type );
  68. // eg "AA" is actually index 27, not 26
  69. if( start0 && i < str.length() - 1 )
  70. chIndex++;
  71. offset *= radix;
  72. offset += chIndex;
  73. }
  74. return OPT<int>{ offset };
  75. }
  76. void ARRAY_AXIS::SetAxisType( NUMBERING_TYPE aType )
  77. {
  78. m_type = aType;
  79. }
  80. bool ARRAY_AXIS::SetOffset( const wxString& aOffsetName )
  81. {
  82. OPT<int> offset = getNumberingOffset( aOffsetName );
  83. // The string does not decode to a valid offset
  84. if( !offset )
  85. return false;
  86. SetOffset( *offset );
  87. return true;
  88. }
  89. void ARRAY_AXIS::SetOffset( int aOffset )
  90. {
  91. m_offset = aOffset;
  92. }
  93. int ARRAY_AXIS::GetOffset() const
  94. {
  95. return m_offset;
  96. }
  97. void ARRAY_AXIS::SetStep( int aStep )
  98. {
  99. m_step = aStep;
  100. }
  101. wxString ARRAY_AXIS::GetItemNumber( int n ) const
  102. {
  103. wxString itemNum;
  104. const wxString& alphabet = GetAlphabet();
  105. const bool nonUnitColsStartAt0 = schemeNonUnitColsStartAt0( m_type );
  106. bool firstRound = true;
  107. int radix = alphabet.Length();
  108. n = m_offset + m_step * n;
  109. do
  110. {
  111. int modN = n % radix;
  112. if( nonUnitColsStartAt0 && !firstRound )
  113. modN--; // Start the "tens/hundreds/etc column" at "Ax", not "Bx"
  114. itemNum.insert( 0, 1, alphabet[modN] );
  115. n /= radix;
  116. firstRound = false;
  117. } while( n );
  118. return itemNum;
  119. }