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 
29 {
30  static const wxString alphaNumeric = "0123456789";
31  static const wxString alphaHex = "0123456789ABCDEF";
32  static const wxString alphaFull = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
33  static const wxString alphaNoIOSQXZ = "ABCDEFGHJKLMNPRTUVWY";
34 
35  switch( type )
36  {
37  default:
38  case NUMBERING_NUMERIC: return alphaNumeric;
39  case NUMBERING_HEX: return alphaHex;
40  case NUMBERING_ALPHA_NO_IOSQXZ: return alphaNoIOSQXZ;
41  case NUMBERING_ALPHA_FULL: return alphaFull;
42  }
43 }
44 
45 
47 {
48  return type == NUMBERING_ALPHA_FULL || type == NUMBERING_ALPHA_NO_IOSQXZ;
49 }
50 
51 
53  const wxString& str, ARRAY_OPTIONS::NUMBERING_TYPE_T type, int& offsetToFill )
54 {
55  const wxString& alphabet = ARRAY_OPTIONS::AlphabetFromNumberingScheme( type );
56 
57  int offset = 0;
58  const int radix = alphabet.length();
59 
60  for( unsigned i = 0; i < str.length(); i++ )
61  {
62  int chIndex = alphabet.Find( str[i], false );
63 
64  if( chIndex == wxNOT_FOUND )
65  return false;
66 
67  const bool start0 = ARRAY_OPTIONS::SchemeNonUnitColsStartAt0( type );
68 
69  // eg "AA" is actually index 27, not 26
70  if( start0 && i < str.length() - 1 )
71  chIndex++;
72 
73  offset *= radix;
74  offset += chIndex;
75  }
76 
77  offsetToFill = offset;
78  return true;
79 }
80 
81 
83 {
84  wxString itemNum;
85  const wxString& alphabet = AlphabetFromNumberingScheme( type );
86 
87  const bool nonUnitColsStartAt0 = SchemeNonUnitColsStartAt0( type );
88 
89  bool firstRound = true;
90  int radix = alphabet.Length();
91 
92  do
93  {
94  int modN = n % radix;
95 
96  if( nonUnitColsStartAt0 && !firstRound )
97  modN--; // Start the "tens/hundreds/etc column" at "Ax", not "Bx"
98 
99  itemNum.insert( 0, 1, alphabet[modN] );
100 
101  n /= radix;
102  firstRound = false;
103  } while( n );
104 
105  return itemNum;
106 }
107 
108 
110 {
111  return m_nx * m_ny;
112 }
113 
114 
116 {
117  const int axisSize = m_horizontalThenVertical ? m_nx : m_ny;
118 
119  int x = n % axisSize;
120  int y = n / axisSize;
121 
122  // reverse on this row/col?
123  if( m_reverseNumberingAlternate && ( y % 2 ) )
124  x = axisSize - x - 1;
125 
126  wxPoint coords( x, y );
127 
128  return coords;
129 }
130 
131 
133 {
134  VECTOR2I point;
135 
136  VECTOR2I coords = getGridCoords( n );
137 
138  // swap axes if needed
140  std::swap( coords.x, coords.y );
141 
142  point.x = coords.x * m_delta.x + coords.y * m_offset.x;
143  point.y = coords.y * m_delta.y + coords.x * m_offset.y;
144 
145  if( std::abs( m_stagger ) > 1 )
146  {
147  const int stagger = std::abs( m_stagger );
148  const bool sr = m_stagger_rows;
149  const int stagger_idx = ( ( sr ? coords.y : coords.x ) % stagger );
150 
151  VECTOR2I stagger_delta( ( sr ? m_delta.x : m_offset.x ), ( sr ? m_offset.y : m_delta.y ) );
152 
153  // Stagger to the left/up if the sign of the stagger is negative
154  point += stagger_delta * copysign( stagger_idx, m_stagger ) / stagger;
155  }
156 
157  // this is already relative to the first array entry
158  return { point, 0.0 };
159 }
160 
161 
162 wxString ARRAY_GRID_OPTIONS::GetItemNumber( int n ) const
163 {
164  wxString itemNum;
165 
166  if( m_2dArrayNumbering )
167  {
168  VECTOR2I coords = getGridCoords( n );
169 
172  }
173  else
174  {
176  }
177 
178  return itemNum;
179 }
180 
181 
183 {
184  return m_nPts;
185 }
186 
187 
189 {
190  double angle;
191 
192  if( m_angle == 0 )
193  // angle is zero, divide evenly into m_nPts
194  angle = 10 * 360.0 * n / double( m_nPts );
195  else
196  // n'th step
197  angle = m_angle * n;
198 
199  VECTOR2I new_pos = aPos;
200  RotatePoint( new_pos, m_centre, angle );
201 
202  // take off the rotation (but not the translation) if needed
203  if( !m_rotateItems )
204  angle = 0;
205 
206  return { new_pos - aPos, angle / 10.0 };
207 }
208 
209 
211 {
213 }
Transform applied to an object by this array.
Definition: array_options.h:90
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.
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:216
int GetArraySize() const override
The number of points in this array.
#define abs(a)
Definition: auxiliary.h:84
NUMBERING_TYPE_T m_secAxisNumType
const string & str
Definition: json11.cpp:596
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
NUMBERING_TYPE_T m_numberingType
bool m_reverseNumberingAlternate
Arabic numerals: 0,1,2,3,4,5,6,7,8,9,10,11...
Definition: array_options.h:46
static bool SchemeNonUnitColsStartAt0(NUMBERING_TYPE_T type)
static wxString getCoordinateNumber(int n, NUMBERING_TYPE_T type)
VECTOR2I getGridCoords(int n) const
static const wxString & AlphabetFromNumberingScheme(NUMBERING_TYPE_T type)
Get the alphabet for a particular numbering scheme.
static bool GetNumberingOffset(const wxString &str, ARRAY_OPTIONS::NUMBERING_TYPE_T type, int &offsetToFill)
Get the numbering offset for a given numbering string.
static DIRECTION_45::AngleType angle(const VECTOR2I &a, const VECTOR2I &b)
size_t i
Definition: json11.cpp:597
NUMBERING_TYPE_T m_priAxisNumType
TRANSFORM GetTransform(int aN, const VECTOR2I &aPos) const override
Get the transform of the n-th point in the array.
Full 26-character alphabet.
Definition: array_options.h:54
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.