KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 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
24#include <array_axis.h>
25
26#include <increment.h>
27
28
38
39
47
48
49const wxString& ARRAY_AXIS::GetAlphabet() const
50{
51 static const wxString alphaNumeric = wxS( "0123456789" );
52 static const wxString alphaHex = wxS( "0123456789ABCDEF" );
53 static const wxString alphaFull = wxS( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
54 static const wxString alphaNoIOSQXZ = wxS( "ABCDEFGHJKLMNPRTUVWY" );
55
56 switch( m_type )
57 {
58 default:
60 return alphaNumeric;
61 case NUMBERING_HEX:
62 return alphaHex;
64 return alphaNoIOSQXZ;
66 return alphaFull;
67 }
68}
69
70
71std::optional<int> ARRAY_AXIS::getNumberingOffset( const wxString& str ) const
72{
73 if( str.length() == 0 )
74 return std::optional<int>{};
75
76 const wxString& alphabet = GetAlphabet();
77
78 int offset = 0;
79 const int radix = alphabet.length();
80
81 for( unsigned i = 0; i < str.length(); i++ )
82 {
83 wxUniChar ch = str[i];
84
85 // For alphabetic types, convert to uppercase for lookup since our alphabets
86 // are defined with uppercase letters. This allows users to enter lowercase.
87 if( !TypeIsNumeric( m_type ) && ch >= 'a' && ch <= 'z' )
88 ch = ch - 'a' + 'A';
89
90 int chIndex = alphabet.Find( ch, false );
91
92 if( chIndex == wxNOT_FOUND )
93 return std::optional<int>{};
94
95 const bool start0 = schemeNonUnitColsStartAt0( m_type );
96
97 // eg "AA" is actually index 27, not 26
98 if( start0 && i < str.length() - 1 )
99 chIndex++;
100
101 offset *= radix;
102 offset += chIndex;
103 }
104
105 return std::optional<int>{ offset };
106}
107
108
110{
111 m_type = aType;
112}
113
114
115bool ARRAY_AXIS::SetOffset( const wxString& aOffsetName )
116{
117 std::optional<int> offset = getNumberingOffset( aOffsetName );
118
119 // The string does not decode to a valid offset
120 if( !offset )
121 return false;
122
123 // For alphabetic types, check if the user entered lowercase letters.
124 // If so, we'll output lowercase letters as well.
125 if( !TypeIsNumeric( m_type ) && !aOffsetName.IsEmpty() )
126 {
127 wxUniChar firstChar = aOffsetName[0];
128 m_useLowercase = ( firstChar >= 'a' && firstChar <= 'z' );
129 }
130 else
131 {
132 m_useLowercase = false;
133 }
134
135 SetOffset( *offset );
136 return true;
137}
138
139
140void ARRAY_AXIS::SetOffset( int aOffset )
141{
142 m_offset = aOffset;
143}
144
145
147{
148 return m_offset;
149}
150
151
152void ARRAY_AXIS::SetStep( int aStep )
153{
154 m_step = aStep;
155}
156
157
158wxString ARRAY_AXIS::GetItemNumber( int n ) const
159{
160 const wxString& alphabet = GetAlphabet();
161 const bool nonUnitColsStartAt0 = schemeNonUnitColsStartAt0( m_type );
162
163 n = m_offset + m_step * n;
164
165 wxString result = AlphabeticFromIndex( n, alphabet, nonUnitColsStartAt0 );
166
167 // If the user entered a lowercase starting value, output lowercase letters
168 if( m_useLowercase )
169 result = result.Lower();
170
171 return result;
172}
static bool schemeNonUnitColsStartAt0(ARRAY_AXIS::NUMBERING_TYPE type)
bool SetOffset(const wxString &aOffsetName)
Set the axis start (as a string, which should decode to a valid index in the alphabet),...
int m_step
Skip every 'n' numbers.
Definition array_axis.h:127
NUMBERING_TYPE m_type
Definition array_axis.h:123
bool m_useLowercase
Output lowercase letters when true (for alphabetic types).
Definition array_axis.h:130
wxString GetItemNumber(int n) const
Get the position number (name) for the n'th axis point.
static bool TypeIsNumeric(NUMBERING_TYPE type)
Check if a numbering type is a numeric type.
Definition array_axis.h:60
const wxString & GetAlphabet() const
Get the alphabet for the current numbering scheme.
void SetAxisType(NUMBERING_TYPE aType)
Set the axis numbering type.
@ NUMBERING_NUMERIC
Arabic numerals: 0,1,2,3,4,5,6,7,8,9,10,11...
Definition array_axis.h:44
@ NUMBERING_ALPHA_NO_IOSQXZ
Alphabet, excluding IOSQXZ.
Definition array_axis.h:53
@ NUMBERING_ALPHA_FULL
Full 26-character alphabet.
Definition array_axis.h:54
std::optional< int > getNumberingOffset(const wxString &str) const
Get the numbering offset for a given numbering string.
void SetStep(int aStep)
Set the skip between consecutive numbers (useful when doing a partial array, e.g.
int GetOffset() const
Get the numbering offset for the axis.
wxString KICOMMON_API AlphabeticFromIndex(size_t aN, const wxString &aAlphabet, bool aZeroBasedNonUnitCols)
Get an alphabetic string like A, B, ... Z, AA, AB, ... ZZ, AAA, ...
wxString result
Test unit parsing edge cases and error handling.