KiCad PCB EDA Suite
array_options.cpp
Go to the documentation of this file.
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 
24 #include <array_options.h>
25 
26 #include <trigo.h>
27 
28 
30 {
31  return m_nx * m_ny;
32 }
33 
34 
36 {
37  const int axisSize = m_horizontalThenVertical ? m_nx : m_ny;
38 
39  int x = n % axisSize;
40  int y = n / axisSize;
41 
42  // reverse on this row/col?
43  if( m_reverseNumberingAlternate && ( y % 2 ) )
44  x = axisSize - x - 1;
45 
46  wxPoint coords( x, y );
47 
48  return coords;
49 }
50 
51 
53 {
54  VECTOR2I point;
55 
56  VECTOR2I coords = getGridCoords( n );
57 
58  // swap axes if needed
60  std::swap( coords.x, coords.y );
61 
62  point.x = coords.x * m_delta.x + coords.y * m_offset.x;
63  point.y = coords.y * m_delta.y + coords.x * m_offset.y;
64 
65  if( std::abs( m_stagger ) > 1 )
66  {
67  const int stagger = std::abs( m_stagger );
68  const bool sr = m_stagger_rows;
69  const int stagger_idx = ( ( sr ? coords.y : coords.x ) % stagger );
70 
71  VECTOR2I stagger_delta( ( sr ? m_delta.x : m_offset.x ), ( sr ? m_offset.y : m_delta.y ) );
72 
73  // Stagger to the left/up if the sign of the stagger is negative
74  point += stagger_delta * copysign( stagger_idx, m_stagger ) / stagger;
75  }
76 
77  // this is already relative to the first array entry
78  return { point, 0.0 };
79 }
80 
81 
82 wxString ARRAY_GRID_OPTIONS::GetItemNumber( int n ) const
83 {
84  wxString itemNum;
85 
86  if( m_2dArrayNumbering )
87  {
88  VECTOR2I coords = getGridCoords( n );
89 
90  itemNum << m_pri_axis.GetItemNumber( coords.x );
91  itemNum << m_sec_axis.GetItemNumber( coords.y );
92  }
93  else
94  {
95  itemNum << m_pri_axis.GetItemNumber( n );
96  }
97 
98  return itemNum;
99 }
100 
101 
103 {
104  return m_nPts;
105 }
106 
107 
109 {
110  double angle;
111 
112  if( m_angle == 0 )
113  // angle is zero, divide evenly into m_nPts
114  angle = 10 * 360.0 * n / double( m_nPts );
115  else
116  // n'th step
117  angle = m_angle * n;
118 
119  VECTOR2I new_pos = aPos;
120  RotatePoint( new_pos, m_centre, angle );
121 
122  // take off the rotation (but not the translation) if needed
123  if( !m_rotateItems )
124  angle = 0;
125 
126  return { new_pos - aPos, angle / 10.0 };
127 }
128 
129 
131 {
132  return m_axis.GetItemNumber( aN );
133 }
wxString GetItemNumber(int n) const
Get the position number (name) for the n'th axis point.
Definition: array_axis.cpp:133
Transform applied to an object by this array.
Definition: array_options.h:55
int GetArraySize() const override
The number of points in this array.
TRANSFORM GetTransform(int aN, const VECTOR2I &aPos) const override
Get the transform of the n-th point in the array.
ARRAY_AXIS m_pri_axis
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:229
int GetArraySize() const override
The number of points in this array.
#define abs(a)
Definition: auxiliary.h:84
wxString GetItemNumber(int n) const override
Get the position number (name) for the n'th array point.
long m_nPts
number of point in the array
bool m_reverseNumberingAlternate
ARRAY_AXIS m_sec_axis
VECTOR2I getGridCoords(int n) const
static DIRECTION_45::AngleType angle(const VECTOR2I &a, const VECTOR2I &b)
TRANSFORM GetTransform(int aN, const VECTOR2I &aPos) const override
Get the transform of the n-th point in the array.
double m_angle
angle between points, or 0 for each point separated by this value (decideg)
wxString GetItemNumber(int n) const override
Get the position number (name) for the n'th array point.