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.

213 lines
5.7 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_options.h>
  24. #include <trigo.h>
  25. const wxString& ARRAY_OPTIONS::AlphabetFromNumberingScheme( NUMBERING_TYPE_T type )
  26. {
  27. static const wxString alphaNumeric = "0123456789";
  28. static const wxString alphaHex = "0123456789ABCDEF";
  29. static const wxString alphaFull = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  30. static const wxString alphaNoIOSQXZ = "ABCDEFGHJKLMNPRTUVWY";
  31. switch( type )
  32. {
  33. default:
  34. case NUMBERING_NUMERIC: return alphaNumeric;
  35. case NUMBERING_HEX: return alphaHex;
  36. case NUMBERING_ALPHA_NO_IOSQXZ: return alphaNoIOSQXZ;
  37. case NUMBERING_ALPHA_FULL: return alphaFull;
  38. }
  39. }
  40. bool ARRAY_OPTIONS::SchemeNonUnitColsStartAt0( NUMBERING_TYPE_T type )
  41. {
  42. return type == NUMBERING_ALPHA_FULL || type == NUMBERING_ALPHA_NO_IOSQXZ;
  43. }
  44. bool ARRAY_OPTIONS::GetNumberingOffset(
  45. const wxString& str, ARRAY_OPTIONS::NUMBERING_TYPE_T type, int& offsetToFill )
  46. {
  47. const wxString& alphabet = ARRAY_OPTIONS::AlphabetFromNumberingScheme( type );
  48. int offset = 0;
  49. const int radix = alphabet.length();
  50. for( unsigned i = 0; i < str.length(); i++ )
  51. {
  52. int chIndex = alphabet.Find( str[i], false );
  53. if( chIndex == wxNOT_FOUND )
  54. return false;
  55. const bool start0 = ARRAY_OPTIONS::SchemeNonUnitColsStartAt0( type );
  56. // eg "AA" is actually index 27, not 26
  57. if( start0 && i < str.length() - 1 )
  58. chIndex++;
  59. offset *= radix;
  60. offset += chIndex;
  61. }
  62. offsetToFill = offset;
  63. return true;
  64. }
  65. wxString ARRAY_OPTIONS::getCoordinateNumber( int n, NUMBERING_TYPE_T type )
  66. {
  67. wxString itemNum;
  68. const wxString& alphabet = AlphabetFromNumberingScheme( type );
  69. const bool nonUnitColsStartAt0 = SchemeNonUnitColsStartAt0( type );
  70. bool firstRound = true;
  71. int radix = alphabet.Length();
  72. do
  73. {
  74. int modN = n % radix;
  75. if( nonUnitColsStartAt0 && !firstRound )
  76. modN--; // Start the "tens/hundreds/etc column" at "Ax", not "Bx"
  77. itemNum.insert( 0, 1, alphabet[modN] );
  78. n /= radix;
  79. firstRound = false;
  80. } while( n );
  81. return itemNum;
  82. }
  83. int ARRAY_GRID_OPTIONS::GetArraySize() const
  84. {
  85. return m_nx * m_ny;
  86. }
  87. VECTOR2I ARRAY_GRID_OPTIONS::getGridCoords( int n ) const
  88. {
  89. const int axisSize = m_horizontalThenVertical ? m_nx : m_ny;
  90. int x = n % axisSize;
  91. int y = n / axisSize;
  92. // reverse on this row/col?
  93. if( m_reverseNumberingAlternate && ( y % 2 ) )
  94. x = axisSize - x - 1;
  95. wxPoint coords( x, y );
  96. return coords;
  97. }
  98. ARRAY_OPTIONS::TRANSFORM ARRAY_GRID_OPTIONS::GetTransform( int n, const VECTOR2I& aPos ) const
  99. {
  100. VECTOR2I point;
  101. VECTOR2I coords = getGridCoords( n );
  102. // swap axes if needed
  103. if( !m_horizontalThenVertical )
  104. std::swap( coords.x, coords.y );
  105. point.x = coords.x * m_delta.x + coords.y * m_offset.x;
  106. point.y = coords.y * m_delta.y + coords.x * m_offset.y;
  107. if( std::abs( m_stagger ) > 1 )
  108. {
  109. const int stagger = std::abs( m_stagger );
  110. const bool sr = m_stagger_rows;
  111. const int stagger_idx = ( ( sr ? coords.y : coords.x ) % stagger );
  112. VECTOR2I stagger_delta( ( sr ? m_delta.x : m_offset.x ), ( sr ? m_offset.y : m_delta.y ) );
  113. // Stagger to the left/up if the sign of the stagger is negative
  114. point += stagger_delta * copysign( stagger_idx, m_stagger ) / stagger;
  115. }
  116. // this is already relative to the first array entry
  117. return { point, 0.0 };
  118. }
  119. wxString ARRAY_GRID_OPTIONS::GetItemNumber( int n ) const
  120. {
  121. wxString itemNum;
  122. if( m_2dArrayNumbering )
  123. {
  124. VECTOR2I coords = getGridCoords( n );
  125. itemNum += getCoordinateNumber( coords.x + m_numberingOffsetX, m_priAxisNumType );
  126. itemNum += getCoordinateNumber( coords.y + m_numberingOffsetY, m_secAxisNumType );
  127. }
  128. else
  129. {
  130. itemNum += getCoordinateNumber( n + m_numberingOffsetX, m_priAxisNumType );
  131. }
  132. return itemNum;
  133. }
  134. int ARRAY_CIRCULAR_OPTIONS::GetArraySize() const
  135. {
  136. return m_nPts;
  137. }
  138. ARRAY_OPTIONS::TRANSFORM ARRAY_CIRCULAR_OPTIONS::GetTransform( int n, const VECTOR2I& aPos ) const
  139. {
  140. double angle;
  141. if( m_angle == 0 )
  142. // angle is zero, divide evenly into m_nPts
  143. angle = 10 * 360.0 * n / double( m_nPts );
  144. else
  145. // n'th step
  146. angle = m_angle * n;
  147. VECTOR2I new_pos = aPos;
  148. RotatePoint( new_pos, m_centre, angle );
  149. // take off the rotation (but not the translation) if needed
  150. if( !m_rotateItems )
  151. angle = 0;
  152. return { new_pos - aPos, angle / 10.0 };
  153. }
  154. wxString ARRAY_CIRCULAR_OPTIONS::GetItemNumber( int aN ) const
  155. {
  156. return getCoordinateNumber( aN + m_numberingOffset, m_numberingType );
  157. }