KiCad PCB EDA Suite
Loading...
Searching...
No Matches
generated_table_schema.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
21
22#include <algorithm>
23#include <set>
24
25#include <base_units.h>
26#include <layer_ids.h>
27
28#include <wx/translation.h>
29
30
31GENERATED_TABLE_SCHEMA::GENERATED_TABLE_SCHEMA( std::vector<GENERATED_TABLE_COLUMN_DEF> aDefs,
32 GENERATED_TABLE_UNITS aDefaultUnits, int aDefaultPrecision ) :
33 m_defs( std::move( aDefs ) ),
34 m_defaultUnits( aDefaultUnits ),
35 m_defaultPrecision( aDefaultPrecision )
36{
37}
38
39
41{
42 for( const GENERATED_TABLE_COLUMN_DEF& def : m_defs )
43 {
44 if( def.m_Id == aId )
45 return &def;
46 }
47
48 return nullptr;
49}
50
51
53{
54 for( const GENERATED_TABLE_COLUMN_DEF& def : m_defs )
55 {
56 if( aToken.IsSameAs( wxString::FromUTF8( def.m_Token ) ) )
57 return &def;
58 }
59
60 return nullptr;
61}
62
63
64const char* GENERATED_TABLE_SCHEMA::Token( int aId ) const
65{
66 if( const GENERATED_TABLE_COLUMN_DEF* def = Find( aId ) )
67 return def->m_Token;
68
69 return m_defs.front().m_Token;
70}
71
72
74{
76 col.m_Id = aId;
77
78 if( const GENERATED_TABLE_COLUMN_DEF* def = Find( aId ) )
79 {
80 col.m_Heading = wxGetTranslation( wxString( def->m_Heading ) );
81 col.m_Align = def->m_Align;
82 }
83
84 return col;
85}
86
87
88std::vector<GENERATED_TABLE_COLUMN> GENERATED_TABLE_SCHEMA::Defaults() const
89{
90 std::vector<GENERATED_TABLE_COLUMN> cols;
91
92 for( const GENERATED_TABLE_COLUMN_DEF& def : m_defs )
93 {
94 if( def.m_DefaultShown )
95 cols.push_back( DefaultColumn( def.m_Id ) );
96 }
97
98 return cols;
99}
100
101
102bool GENERATED_TABLE_SCHEMA::Validate( std::vector<GENERATED_TABLE_COLUMN>& aColumns ) const
103{
104 std::vector<GENERATED_TABLE_COLUMN> kept;
105 std::set<int> seen;
106 int64_t total = 0;
107
108 for( GENERATED_TABLE_COLUMN& col : aColumns )
109 {
110 // Unknown to this schema, or a repeat that adds no information and multiplies the
111 // generated cell count either way
112 if( !Find( col.m_Id ) || !seen.insert( col.m_Id ).second )
113 continue;
114
115 col.m_Width = std::clamp( col.m_Width, 0, GENERATED_TABLE_MAX_COLUMN_WIDTH );
116 total += col.m_Width;
117 kept.push_back( col );
118 }
119
120 aColumns = std::move( kept );
121
122 return !aColumns.empty() && total <= GENERATED_TABLE_MAX_TOTAL_WIDTH;
123}
124
125
127{
128 // Drops mask, paste, adhesive and silk, which print on the finished board, plus the
129 // courtyard layers, which DRC reads as constraints
130 LSET layers = LSET::AllNonCuMask() & ~LSET::AllBoardTechMask();
131
132 layers.reset( Edge_Cuts );
133 layers.reset( Margin );
134 layers.reset( F_CrtYd );
135 layers.reset( B_CrtYd );
136 layers.reset( Rescue );
137
138 return layers;
139}
140
141
142wxString FormatGeneratedTableLength( int aValue, GENERATED_TABLE_UNITS aUnits, int aPrecision )
143{
144 const double mm = pcbIUScale.IUTomm( aValue );
145
146 switch( aUnits )
147 {
149 return wxString::Format( wxT( "%.*f″" ), aPrecision + 1, mm / 25.4 );
150
151 // A mil is three orders of magnitude finer than an inch, so its precision borrows from
152 // the same setting rather than needing one of its own
154 return wxString::Format( wxT( "%.*f mil" ), std::max( aPrecision - 2, 0 ), mm / 25.4 * 1000.0 );
155
157 default:
158 return wxString::Format( wxT( "%.*f" ), aPrecision, mm );
159 }
160}
161
162
163namespace
164{
165
166struct SCHEMA_TOKEN
167{
168 const char* token;
169 int value;
170};
171
172const SCHEMA_TOKEN unitsTokens[] = {
173 { "mm", (int) GENERATED_TABLE_UNITS::MM },
174 { "inch", (int) GENERATED_TABLE_UNITS::INCH },
175 { "mils", (int) GENERATED_TABLE_UNITS::MILS },
176};
177
178const SCHEMA_TOKEN alignTokens[] = {
179 { "left", (int) GENERATED_TABLE_ALIGN::LEFT },
180 { "center", (int) GENERATED_TABLE_ALIGN::CENTER },
181 { "right", (int) GENERATED_TABLE_ALIGN::RIGHT },
182};
183
184template <size_t N>
185const char* tokenFor( const SCHEMA_TOKEN ( &aMap )[N], int aValue )
186{
187 for( const SCHEMA_TOKEN& entry : aMap )
188 {
189 if( entry.value == aValue )
190 return entry.token;
191 }
192
193 return aMap[0].token;
194}
195
196template <size_t N>
197bool valueFor( const SCHEMA_TOKEN ( &aMap )[N], const wxString& aToken, int& aValue )
198{
199 for( const SCHEMA_TOKEN& entry : aMap )
200 {
201 if( aToken.IsSameAs( wxString::FromUTF8( entry.token ) ) )
202 {
203 aValue = entry.value;
204 return true;
205 }
206 }
207
208 return false;
209}
210
211} // namespace
212
213
215{
216 return tokenFor( unitsTokens, (int) aUnits );
217}
218
219
220bool GeneratedTableUnitsFromToken( const wxString& aToken, GENERATED_TABLE_UNITS& aUnits )
221{
222 int v = 0;
223
224 if( !valueFor( unitsTokens, aToken, v ) )
225 return false;
226
227 aUnits = (GENERATED_TABLE_UNITS) v;
228 return true;
229}
230
231
233{
234 return tokenFor( alignTokens, (int) aAlign );
235}
236
237
238bool GeneratedTableAlignFromToken( const wxString& aToken, GENERATED_TABLE_ALIGN& aAlign )
239{
240 int v = 0;
241
242 if( !valueFor( alignTokens, aToken, v ) )
243 return false;
244
245 aAlign = (GENERATED_TABLE_ALIGN) v;
246 return true;
247}
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:128
BASE_SET & reset(size_t pos)
Definition base_set.h:153
const char * Token(int aId) const
The file token for a known id, or a stable placeholder for one the schema does not recognize,...
GENERATED_TABLE_COLUMN DefaultColumn(int aId) const
std::vector< GENERATED_TABLE_COLUMN > Defaults() const
GENERATED_TABLE_UNITS m_defaultUnits
const GENERATED_TABLE_COLUMN_DEF * FindToken(const wxString &aToken) const
const GENERATED_TABLE_COLUMN_DEF * Find(int aId) const
GENERATED_TABLE_SCHEMA(std::vector< GENERATED_TABLE_COLUMN_DEF > aDefs, GENERATED_TABLE_UNITS aDefaultUnits, int aDefaultPrecision)
bool Validate(std::vector< GENERATED_TABLE_COLUMN > &aColumns) const
Drop a column whose id the schema does not know and a column that repeats an id already kept,...
std::vector< GENERATED_TABLE_COLUMN_DEF > m_defs
LSET is a set of PCB_LAYER_IDs.
Definition lset.h:37
static LSET AllNonCuMask()
Return a mask holding all layer minus CU layers.
Definition lset.cpp:623
bool GeneratedTableUnitsFromToken(const wxString &aToken, GENERATED_TABLE_UNITS &aUnits)
const char * GeneratedTableUnitsToken(GENERATED_TABLE_UNITS aUnits)
Stable file tokens, independent of enum ordering so inserting a value later cannot change what an exi...
LSET DocumentationLayers()
Layers a drill chart or drill map may live on.
wxString FormatGeneratedTableLength(int aValue, GENERATED_TABLE_UNITS aUnits, int aPrecision)
Render a length the way a generated table's cell shows it.
bool GeneratedTableAlignFromToken(const wxString &aToken, GENERATED_TABLE_ALIGN &aAlign)
const char * GeneratedTableAlignToken(GENERATED_TABLE_ALIGN aAlign)
GENERATED_TABLE_UNITS
constexpr int GENERATED_TABLE_MAX_COLUMN_WIDTH
A column can be no wider than this.
constexpr int64_t GENERATED_TABLE_MAX_TOTAL_WIDTH
A column set can be no wider than this in total, summed after each column is clamped to GENERATED_TAB...
GENERATED_TABLE_ALIGN
@ F_CrtYd
Definition layer_ids.h:112
@ Edge_Cuts
Definition layer_ids.h:108
@ Margin
Definition layer_ids.h:109
@ B_CrtYd
Definition layer_ids.h:111
@ Rescue
Definition layer_ids.h:117
STL namespace.
The column a schema knows how to build, independent of whether any particular table shows it.
A column as a generated table actually carries it: what a row's cell in that column holds and how it ...
GENERATED_TABLE_ALIGN m_Align