KiCad PCB EDA Suite
array_axis.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_axis.h>
25 
26 
32 {
33  return type == ARRAY_AXIS::NUMBERING_TYPE::NUMBERING_ALPHA_FULL
34  || type == ARRAY_AXIS::NUMBERING_TYPE::NUMBERING_ALPHA_NO_IOSQXZ;
35 }
36 
37 
38 ARRAY_AXIS::ARRAY_AXIS() : m_type( NUMBERING_TYPE::NUMBERING_NUMERIC ), m_offset( 0 ), m_step( 1 )
39 {
40 }
41 
42 
43 const wxString& ARRAY_AXIS::GetAlphabet() const
44 {
45  static const wxString alphaNumeric = "0123456789";
46  static const wxString alphaHex = "0123456789ABCDEF";
47  static const wxString alphaFull = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
48  static const wxString alphaNoIOSQXZ = "ABCDEFGHJKLMNPRTUVWY";
49 
50  switch( m_type )
51  {
52  default:
53  case NUMBERING_NUMERIC:
54  return alphaNumeric;
55  case NUMBERING_HEX:
56  return alphaHex;
58  return alphaNoIOSQXZ;
60  return alphaFull;
61  }
62 }
63 
64 
66 {
67  if( str.length() == 0 )
68  return OPT<int>{};
69 
70  const wxString& alphabet = GetAlphabet();
71 
72  int offset = 0;
73  const int radix = alphabet.length();
74 
75  for( unsigned i = 0; i < str.length(); i++ )
76  {
77  int chIndex = alphabet.Find( str[i], false );
78 
79  if( chIndex == wxNOT_FOUND )
80  return OPT<int>{};
81 
82  const bool start0 = schemeNonUnitColsStartAt0( m_type );
83 
84  // eg "AA" is actually index 27, not 26
85  if( start0 && i < str.length() - 1 )
86  chIndex++;
87 
88  offset *= radix;
89  offset += chIndex;
90  }
91 
92  return OPT<int>{ offset };
93 }
94 
95 
97 {
98  m_type = aType;
99 }
100 
101 
102 bool ARRAY_AXIS::SetOffset( const wxString& aOffsetName )
103 {
104  OPT<int> offset = getNumberingOffset( aOffsetName );
105 
106  // The string does not decode to a valid offset
107  if( !offset )
108  return false;
109 
110  SetOffset( *offset );
111  return true;
112 }
113 
114 
115 void ARRAY_AXIS::SetOffset( int aOffset )
116 {
117  m_offset = aOffset;
118 }
119 
120 
122 {
123  return m_offset;
124 }
125 
126 
127 void ARRAY_AXIS::SetStep( int aStep )
128 {
129  m_step = aStep;
130 }
131 
132 
133 wxString ARRAY_AXIS::GetItemNumber( int n ) const
134 {
135  wxString itemNum;
136  const wxString& alphabet = GetAlphabet();
137 
138  const bool nonUnitColsStartAt0 = schemeNonUnitColsStartAt0( m_type );
139 
140  bool firstRound = true;
141  int radix = alphabet.Length();
142 
143  n = m_offset + m_step * n;
144 
145  do
146  {
147  int modN = n % radix;
148 
149  if( nonUnitColsStartAt0 && !firstRound )
150  modN--; // Start the "tens/hundreds/etc column" at "Ax", not "Bx"
151 
152  itemNum.insert( 0, 1, alphabet[modN] );
153 
154  n /= radix;
155  firstRound = false;
156  } while( n );
157 
158  return itemNum;
159 }
wxString GetItemNumber(int n) const
Get the position number (name) for the n'th axis point.
Definition: array_axis.cpp:133
static bool schemeNonUnitColsStartAt0(ARRAY_AXIS::NUMBERING_TYPE type)
Definition: array_axis.cpp:31
Arabic numerals: 0,1,2,3,4,5,6,7,8,9,10,11...
Definition: array_axis.h:43
OPT< int > getNumberingOffset(const wxString &str) const
Get the numbering offset for a given numbering string.
Definition: array_axis.cpp:65
NUMBERING_TYPE m_type
Definition: array_axis.h:112
void SetAxisType(NUMBERING_TYPE aType)
Set the axis numbering type.
Definition: array_axis.cpp:96
bool SetOffset(const wxString &aOffsetName)
Set the axis start (as a string, which should decode to a valid index in the alphabet)
Definition: array_axis.cpp:102
const string & str
Definition: json11.cpp:596
void SetStep(int aStep)
Set the skip between consecutive numbers (useful when doing a partial array, e.g.
Definition: array_axis.cpp:127
int m_offset
Skip every 'n' numbers.
Definition: array_axis.h:113
Full 26-character alphabet.
Definition: array_axis.h:51
int GetOffset() const
Get the numbering offset for the axis.
Definition: array_axis.cpp:121
size_t i
Definition: json11.cpp:597
boost::optional< T > OPT
Definition: optional.h:7
const wxString & GetAlphabet() const
Get the alphabet for the current numbering scheme.
Definition: array_axis.cpp:43