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.

133 lines
3.5 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. 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. wxPoint coords( x, y );
  38. return coords;
  39. }
  40. ARRAY_OPTIONS::TRANSFORM ARRAY_GRID_OPTIONS::GetTransform( int n, const VECTOR2I& aPos ) 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. // this is already relative to the first array entry
  59. return { point, 0.0 };
  60. }
  61. wxString ARRAY_GRID_OPTIONS::GetItemNumber( int n ) const
  62. {
  63. wxString itemNum;
  64. if( m_2dArrayNumbering )
  65. {
  66. VECTOR2I coords = getGridCoords( n );
  67. itemNum << m_pri_axis.GetItemNumber( coords.x );
  68. itemNum << m_sec_axis.GetItemNumber( coords.y );
  69. }
  70. else
  71. {
  72. itemNum << m_pri_axis.GetItemNumber( n );
  73. }
  74. return itemNum;
  75. }
  76. int ARRAY_CIRCULAR_OPTIONS::GetArraySize() const
  77. {
  78. return m_nPts;
  79. }
  80. ARRAY_OPTIONS::TRANSFORM ARRAY_CIRCULAR_OPTIONS::GetTransform( int n, const VECTOR2I& aPos ) const
  81. {
  82. double angle;
  83. if( m_angle == 0 )
  84. // angle is zero, divide evenly into m_nPts
  85. angle = 10 * 360.0 * n / double( m_nPts );
  86. else
  87. // n'th step
  88. angle = m_angle * n;
  89. VECTOR2I new_pos = aPos;
  90. RotatePoint( new_pos, m_centre, angle );
  91. // take off the rotation (but not the translation) if needed
  92. if( !m_rotateItems )
  93. angle = 0;
  94. return { new_pos - aPos, angle / 10.0 };
  95. }
  96. wxString ARRAY_CIRCULAR_OPTIONS::GetItemNumber( int aN ) const
  97. {
  98. return m_axis.GetItemNumber( aN );
  99. }