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 (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#include <increment.h>
27
28
34{
37}
38
39
40ARRAY_AXIS::ARRAY_AXIS() : m_type( NUMBERING_TYPE::NUMBERING_NUMERIC ), m_offset( 0 ), m_step( 1 )
41{
42}
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 int chIndex = alphabet.Find( str[i], false );
80
81 if( chIndex == wxNOT_FOUND )
82 return std::optional<int>{};
83
84 const bool start0 = schemeNonUnitColsStartAt0( m_type );
85
86 // eg "AA" is actually index 27, not 26
87 if( start0 && i < str.length() - 1 )
88 chIndex++;
89
90 offset *= radix;
91 offset += chIndex;
92 }
93
94 return std::optional<int>{ offset };
95}
96
97
99{
100 m_type = aType;
101}
102
103
104bool ARRAY_AXIS::SetOffset( const wxString& aOffsetName )
105{
106 std::optional<int> offset = getNumberingOffset( aOffsetName );
107
108 // The string does not decode to a valid offset
109 if( !offset )
110 return false;
111
112 SetOffset( *offset );
113 return true;
114}
115
116
117void ARRAY_AXIS::SetOffset( int aOffset )
118{
119 m_offset = aOffset;
120}
121
122
124{
125 return m_offset;
126}
127
128
129void ARRAY_AXIS::SetStep( int aStep )
130{
131 m_step = aStep;
132}
133
134
135wxString ARRAY_AXIS::GetItemNumber( int n ) const
136{
137 wxString itemNum;
138 const wxString& alphabet = GetAlphabet();
139 const bool nonUnitColsStartAt0 = schemeNonUnitColsStartAt0( m_type );
140
141 n = m_offset + m_step * n;
142
143 return AlphabeticFromIndex( n, alphabet, nonUnitColsStartAt0 );
144}
static bool schemeNonUnitColsStartAt0(ARRAY_AXIS::NUMBERING_TYPE type)
Definition: array_axis.cpp:33
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:104
int m_offset
Skip every 'n' numbers.
Definition: array_axis.h:114
NUMBERING_TYPE m_type
Definition: array_axis.h:113
wxString GetItemNumber(int n) const
Get the position number (name) for the n'th axis point.
Definition: array_axis.cpp:135
const wxString & GetAlphabet() const
Get the alphabet for the current numbering scheme.
Definition: array_axis.cpp:45
void SetAxisType(NUMBERING_TYPE aType)
Set the axis numbering type.
Definition: array_axis.cpp:98
@ NUMBERING_NUMERIC
Arabic numerals: 0,1,2,3,4,5,6,7,8,9,10,11...
Definition: array_axis.h:44
@ NUMBERING_HEX
Definition: array_axis.h:45
@ NUMBERING_ALPHA_NO_IOSQXZ
Definition: array_axis.h:46
@ NUMBERING_ALPHA_FULL
Full 26-character alphabet.
Definition: array_axis.h:52
std::optional< int > getNumberingOffset(const wxString &str) const
Get the numbering offset for a given numbering string.
Definition: array_axis.cpp:67
void SetStep(int aStep)
Set the skip between consecutive numbers (useful when doing a partial array, e.g.
Definition: array_axis.cpp:129
int GetOffset() const
Get the numbering offset for the axis.
Definition: array_axis.cpp:123
wxString KICOMMON_API AlphabeticFromIndex(size_t aN, const wxString &aAlphabet, bool aZeroBasedNonUnitCols)
Get an alphabetic string like A, B, ... Z, AA, AB, ... ZZ, AAA, ...
Definition: increment.cpp:258