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, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <array_axis.h>
21
22#include <increment.h>
23
24
34
35
43
44
45const wxString& ARRAY_AXIS::GetAlphabet() const
46{
47 static const wxString alphaNumeric = wxS( "0123456789" );
48 static const wxString alphaHex = wxS( "0123456789ABCDEF" );
49 static const wxString alphaFull = wxS( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
50 static const wxString alphaNoIOSQXZ = wxS( "ABCDEFGHJKLMNPRTUVWY" );
51
52 switch( m_type )
53 {
54 default:
56 return alphaNumeric;
57 case NUMBERING_HEX:
58 return alphaHex;
60 return alphaNoIOSQXZ;
62 return alphaFull;
63 }
64}
65
66
67std::optional<int> ARRAY_AXIS::getNumberingOffset( const wxString& str ) const
68{
69 if( str.length() == 0 )
70 return std::optional<int>{};
71
72 const wxString& alphabet = GetAlphabet();
73
74 int offset = 0;
75 const int radix = alphabet.length();
76
77 for( unsigned i = 0; i < str.length(); i++ )
78 {
79 wxUniChar ch = str[i];
80
81 // For alphabetic types, convert to uppercase for lookup since our alphabets
82 // are defined with uppercase letters. This allows users to enter lowercase.
83 if( !TypeIsNumeric( m_type ) && ch >= 'a' && ch <= 'z' )
84 ch = ch - 'a' + 'A';
85
86 int chIndex = alphabet.Find( ch, false );
87
88 if( chIndex == wxNOT_FOUND )
89 return std::optional<int>{};
90
91 const bool start0 = schemeNonUnitColsStartAt0( m_type );
92
93 // eg "AA" is actually index 27, not 26
94 if( start0 && i < str.length() - 1 )
95 chIndex++;
96
97 offset *= radix;
98 offset += chIndex;
99 }
100
101 return std::optional<int>{ offset };
102}
103
104
106{
107 m_type = aType;
108}
109
110
111bool ARRAY_AXIS::SetOffset( const wxString& aOffsetName )
112{
113 std::optional<int> offset = getNumberingOffset( aOffsetName );
114
115 // The string does not decode to a valid offset
116 if( !offset )
117 return false;
118
119 // For alphabetic types, check if the user entered lowercase letters.
120 // If so, we'll output lowercase letters as well.
121 if( !TypeIsNumeric( m_type ) && !aOffsetName.IsEmpty() )
122 {
123 wxUniChar firstChar = aOffsetName[0];
124 m_useLowercase = ( firstChar >= 'a' && firstChar <= 'z' );
125 }
126 else
127 {
128 m_useLowercase = false;
129 }
130
131 SetOffset( *offset );
132 return true;
133}
134
135
136void ARRAY_AXIS::SetOffset( int aOffset )
137{
138 m_offset = aOffset;
139}
140
141
143{
144 return m_offset;
145}
146
147
148void ARRAY_AXIS::SetStep( int aStep )
149{
150 m_step = aStep;
151}
152
153
154wxString ARRAY_AXIS::GetItemNumber( int n ) const
155{
156 const wxString& alphabet = GetAlphabet();
157 const bool nonUnitColsStartAt0 = schemeNonUnitColsStartAt0( m_type );
158
159 n = m_offset + m_step * n;
160
161 wxString result = AlphabeticFromIndex( n, alphabet, nonUnitColsStartAt0 );
162
163 // If the user entered a lowercase starting value, output lowercase letters
164 if( m_useLowercase )
165 result = result.Lower();
166
167 return result;
168}
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:123
NUMBERING_TYPE m_type
Definition array_axis.h:119
bool m_useLowercase
Output lowercase letters when true (for alphabetic types).
Definition array_axis.h:126
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:56
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:40
@ NUMBERING_ALPHA_NO_IOSQXZ
Alphabet, excluding IOSQXZ.
Definition array_axis.h:49
@ NUMBERING_ALPHA_FULL
Full 26-character alphabet.
Definition array_axis.h:50
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.