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.

155 lines
4.0 KiB

  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_options.h>
  24. #include <trigo.h>
  25. int ARRAY_GRID_OPTIONS::GetArraySize() const
  26. {
  27. return m_nx * m_ny;
  28. }
  29. VECTOR2I ARRAY_GRID_OPTIONS::getGridCoords( int n ) const
  30. {
  31. const int axisSize = m_horizontalThenVertical ? m_nx : m_ny;
  32. int x = n % axisSize;
  33. int y = n / axisSize;
  34. // reverse on this row/col?
  35. if( m_reverseNumberingAlternate && ( y % 2 ) )
  36. x = axisSize - x - 1;
  37. VECTOR2I coords( x, y );
  38. return coords;
  39. }
  40. VECTOR2I ARRAY_GRID_OPTIONS::gtItemPosRelativeToItem0( int n ) const
  41. {
  42. VECTOR2I point;
  43. VECTOR2I coords = getGridCoords( n );
  44. // swap axes if needed
  45. if( !m_horizontalThenVertical )
  46. std::swap( coords.x, coords.y );
  47. point.x = coords.x * m_delta.x + coords.y * m_offset.x;
  48. point.y = coords.y * m_delta.y + coords.x * m_offset.y;
  49. if( std::abs( m_stagger ) > 1 )
  50. {
  51. const int stagger = std::abs( m_stagger );
  52. const bool sr = m_stagger_rows;
  53. const int stagger_idx = ( ( sr ? coords.y : coords.x ) % stagger );
  54. VECTOR2I stagger_delta( ( sr ? m_delta.x : m_offset.x ), ( sr ? m_offset.y : m_delta.y ) );
  55. // Stagger to the left/up if the sign of the stagger is negative
  56. point += stagger_delta * copysign( stagger_idx, m_stagger ) / stagger;
  57. }
  58. return point;
  59. }
  60. ARRAY_OPTIONS::TRANSFORM ARRAY_GRID_OPTIONS::GetTransform( int n, const VECTOR2I& aPos ) const
  61. {
  62. VECTOR2I point = gtItemPosRelativeToItem0( n );
  63. // Bump the item by half the array size
  64. if( m_centred )
  65. {
  66. // Get the array extents
  67. const int arrayExtentX = ( m_nx - 1 ) * m_delta.x + ( m_ny - 1 ) * m_offset.x;
  68. const int arrayExtentY = ( m_ny - 1 ) * m_delta.y + ( m_nx - 1 ) * m_offset.y;
  69. point -= VECTOR2I( arrayExtentX, arrayExtentY ) / 2;
  70. }
  71. return { point, ANGLE_0 };
  72. }
  73. wxString ARRAY_GRID_OPTIONS::GetItemNumber( int n ) const
  74. {
  75. wxString itemNum;
  76. if( m_2dArrayNumbering )
  77. {
  78. VECTOR2I coords = getGridCoords( n );
  79. itemNum << m_pri_axis.GetItemNumber( coords.x );
  80. itemNum << m_sec_axis.GetItemNumber( coords.y );
  81. }
  82. else
  83. {
  84. itemNum << m_pri_axis.GetItemNumber( n );
  85. }
  86. return itemNum;
  87. }
  88. int ARRAY_CIRCULAR_OPTIONS::GetArraySize() const
  89. {
  90. return m_nPts;
  91. }
  92. ARRAY_OPTIONS::TRANSFORM ARRAY_CIRCULAR_OPTIONS::GetTransform( int n, const VECTOR2I& aPos ) const
  93. {
  94. EDA_ANGLE angle;
  95. if( m_angle.IsZero() )
  96. // angle is zero, divide evenly into m_nPts
  97. angle = EDA_ANGLE( 360.0 * n / double( m_nPts ), DEGREES_T );
  98. else
  99. // n'th step
  100. angle = EDA_ANGLE( m_angle.AsDegrees() * n, DEGREES_T );
  101. angle += m_angleOffset;
  102. if( m_clockwise )
  103. angle = -angle;
  104. VECTOR2I new_pos = aPos;
  105. RotatePoint( new_pos, m_centre, angle );
  106. // take off the rotation (but not the translation) if needed
  107. if( !m_rotateItems )
  108. angle = ANGLE_0;
  109. return { new_pos - aPos, angle };
  110. }
  111. wxString ARRAY_CIRCULAR_OPTIONS::GetItemNumber( int aN ) const
  112. {
  113. return m_axis.GetItemNumber( aN );
  114. }