KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcb_drill_chart.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 <set>
21
22#include <pcb_drill_chart.h>
23
24#include <board.h>
29#include <base_units.h>
30#include <string_utils.h>
31#include <eda_text.h>
32#include <i18n_utility.h>
34#include <api/api_enums.h>
36#include <api/api_utils.h>
37#include <api/api_pcb_utils.h>
38#include <api/board/board_types.pb.h>
39
40
42 m_Profile( aBoard.GetDesignSettings().GetDrillSymbolProfile() )
43{
44}
45
46
51
52
61
62
64 PCB_GENERATED_TABLE( aOther ),
65 m_filter( aOther.m_filter ),
66 m_showTotals( aOther.m_showTotals ),
68 m_rowShapes( aOther.m_rowShapes ),
71{
72 // PCB_TABLE's copy ctor propagates m_structType, so the clone reports PCB_DRILL_CHART_T
73 // and swapData's guard stays meaningful
74}
75
76
78{
79 m_columns = aTemplate.Columns();
80 m_units = aTemplate.GetUnits();
81 m_precision = aTemplate.GetPrecision();
82 m_showTotals = aTemplate.GetShowTotals();
83}
84
85
87{
88 wxCHECK_RET( aImage && aImage->Type() == Type(), wxT( "Cannot swap data with invalid chart." ) );
89
91
92 PCB_DRILL_CHART* other = static_cast<PCB_DRILL_CHART*>( aImage );
93
94 std::swap( m_filter, other->m_filter );
95 std::swap( m_showTotals, other->m_showTotals );
96 std::swap( m_laidOutShowTotals, other->m_laidOutShowTotals );
97 std::swap( m_builtGeneration, other->m_builtGeneration );
98 std::swap( m_rowShapes, other->m_rowShapes );
99 std::swap( m_symbolColumn, other->m_symbolColumn );
100}
101
102
104{
105 return static_cast<DRILL_CHART_COLUMN_ID>( m_columns[aCol].m_Id );
106}
107
108
109std::vector<DRILL_CHART_GROUP> PCB_DRILL_CHART::buildGroups( const BOARD& aBoard,
110 const DRILL_SYMBOL_PROFILE& aProfile ) const
111{
113 spec.m_Filter = m_filter;
114
115 // Every span on the board. What shares a row is the profile's grouping to decide
116 DRILL_CHART_MODEL model( aProfile );
117 model.Build( aBoard, EnumerateDrillSpans( aBoard ), spec );
118
119 return model.Groups();
120}
121
122
124{
125 wxCHECK_RET( aOther.Type() == Type(), wxT( "Cannot apply settings from another kind of table." ) );
126
128
129 const PCB_DRILL_CHART& other = static_cast<const PCB_DRILL_CHART&>( aOther );
130
131 m_filter = other.m_filter;
133}
134
135
136bool PCB_DRILL_CHART::IsStale( const BOARD& aBoard ) const
137{
139}
140
141
143{
144 if( aShow != m_showTotals && m_rowKeys.empty() && !m_laidOutShowTotals )
146
147 m_showTotals = aShow;
148}
149
150
151bool PCB_DRILL_CHART::IsDataRow( int aRow ) const
152{
153 if( !m_rowKeys.empty() )
154 return PCB_GENERATED_TABLE::IsDataRow( aRow );
155
156 // A chart written before the keys were recorded still has to answer this, and its rows are
157 // laid out the way RebuildCells lays them out
158 const int firstDataRow = 1;
159 const int lastDataRow = GetRowCount() - 1 - ( m_laidOutShowTotals.value_or( m_showTotals ) ? 1 : 0 );
160
161 return aRow >= firstDataRow && aRow <= lastDataRow;
162}
163
164
165wxString PCB_DRILL_CHART::GetItemDescription( UNITS_PROVIDER* aUnitsProvider, bool aFull ) const
166{
167 return wxString::Format( _( "Drill Chart (%d rows)" ), GetRowCount() );
168}
169
170
171bool PCB_DRILL_CHART::operator==( const BOARD_ITEM& aOther ) const
172{
173 if( aOther.Type() != Type() )
174 return false;
175
176 const PCB_DRILL_CHART& other = static_cast<const PCB_DRILL_CHART&>( aOther );
177
178 // Must cover everything swapData swaps. The git merge driver decides a change is a
179 // change from this, so an omitted member is a silently dropped edit
180 return m_filter == other.m_filter && m_showTotals == other.m_showTotals && m_rowShapes == other.m_rowShapes
181 && m_symbolColumn == other.m_symbolColumn && generatedEquals( other );
182}
183
184
185namespace
186{
187
188const wxString NO_VALUE( wxS( "\u2014" ) );
189
190
191wxString spanText( const BOARD& aBoard, const DRILL_CHART_GROUP& aGroup )
192{
193 // The board's own layer names, so a chart matches the stackup the fabricator was given
194 return wxString::Format( wxT( "%s / %s" ), aBoard.GetLayerName( aGroup.m_TopLayer ),
195 aBoard.GetLayerName( aGroup.m_BottomLayer ) );
196}
197
198
199wxString operationText( const DRILL_CHART_GROUP& aGroup )
200{
202 return wxT( "Backdrill" );
203
204 if( aGroup.m_IsSlot )
205 return wxT( "Slot" );
206
207 return wxT( "Drill" );
208}
209
210
211wxString protectionText( const DRILL_CHART_GROUP& aGroup )
212{
213 wxArrayString parts;
214
215 if( aGroup.m_Filled )
216 parts.Add( wxT( "filled" ) );
217
218 if( aGroup.m_Capped )
219 parts.Add( wxT( "capped" ) );
220
221 if( aGroup.m_TopPlugged || aGroup.m_BottomPlugged )
222 parts.Add( wxT( "plugged" ) );
223
224 if( aGroup.m_TopCovered || aGroup.m_BottomCovered )
225 parts.Add( wxT( "covered" ) );
226
227 if( aGroup.m_TopTented || aGroup.m_BottomTented )
228 parts.Add( wxT( "tented" ) );
229
230 return parts.IsEmpty() ? NO_VALUE : wxJoin( parts, ',' );
231}
232
233
234wxString symbolText( const DRILL_CHART_GROUP& aGroup, GENERATED_TABLE_UNITS aUnits, int aPrecision )
235{
236 switch( aGroup.m_Symbol.m_MarkMode )
237 {
239 return aGroup.m_Symbol.m_Letter;
240
242 return FormatGeneratedTableLength( aGroup.m_Diameter, aUnits, aPrecision );
243
245 default:
246 // The shape itself is drawn by the painter. The cell carries no text
247 return wxEmptyString;
248 }
249}
250
251} // namespace
252
253
254std::unique_ptr<GENERATED_TABLE_CONTENT> PCB_DRILL_CHART::generate( const BOARD& aBoard,
255 GENERATED_TABLE_REFRESH* aRefresh ) const
256{
257 // A refresh can carry grouping the board has not been given yet, as the properties dialog does
258 std::vector<DRILL_CHART_GROUP> groups =
259 buildGroups( aBoard, aRefresh ? aRefresh->Pending<DRILL_PROFILE_PENDING>().m_Profile
261
262 if( aRefresh )
263 {
264 // The refresh's copy, so a cancelled placement leaves nothing behind and a batch
265 // rebuild accumulates instead of keeping only the last chart's
267 }
268 else
269 {
270 // Whole board, not this chart's filtered subset, or a filtered chart prints a
271 // different symbol from the map for the same hole
272 const std::map<std::string, DRILL_SYMBOL_ASSIGNMENT> resolved =
273 ResolveDrillSymbols( aBoard );
274
275 for( DRILL_CHART_GROUP& group : groups )
276 {
277 const auto it = resolved.find( group.m_SymbolKey );
278
279 if( it != resolved.end() )
280 group.m_Symbol = it->second;
281 }
282 }
283
284 auto content = std::make_unique<DRILL_CHART_CONTENT>();
285 content->m_Rows.reserve( groups.size() );
286 content->m_RowShapes.reserve( groups.size() );
287
288 const int cols = static_cast<int>( m_columns.size() );
289
290 for( int col = 0; col < cols; ++col )
291 {
293 content->m_SymbolColumn = col;
294 }
295
296 for( const DRILL_CHART_GROUP& group : groups )
297 {
298 GENERATED_TABLE_ROW& row = content->m_Rows.emplace_back();
299 row.m_Key = group.m_Key;
300 row.m_Cells.reserve( cols );
301
302 if( group.m_Symbol.m_MarkMode == DRILL_MARK_MODE::SHAPE )
303 content->m_RowShapes.emplace_back( group.m_Symbol.m_ShapeIndex );
304 else
305 content->m_RowShapes.emplace_back();
306
307 for( int col = 0; col < cols; ++col )
308 {
309 wxString text;
310
311 switch( columnId( col ) )
312 {
314 text = symbolText( group, m_units, m_precision );
315 break;
316
319 break;
320
322 text = group.m_IsSlot ? wxString::Format( wxT( "%s x %s" ),
324 m_precision ),
326 m_precision ) )
327 : NO_VALUE;
328 break;
329
331 text = group.m_NotPlated ? wxT( "No" ) : wxT( "Yes" );
332 break;
333
335 text = wxString::Format( wxT( "%d" ), group.m_OperationCount );
336 break;
337
339 text = wxString::Format( wxT( "%d" ), group.m_SiteCount );
340 break;
341
343 text = spanText( aBoard, group );
344 break;
345
347 text = operationText( group );
348 break;
349
351 text = protectionText( group );
352 break;
353
355 text = group.m_StubLength.has_value()
357 : NO_VALUE;
358 break;
359
361 {
362 // Depth over diameter, the number a fabricator uses to judge plating
363 // difficulty. Needs a real stackup, so it stays an em dash without one
364 const int depth = aBoard.GetStackupOrDefault().GetLayerDistance( group.m_TopLayer,
365 group.m_BottomLayer );
366
367 const int diameter = group.m_Diameter;
368
369 text = depth > 0 && diameter > 0 ? wxString::Format( wxT( "%.1f:1" ), (double) depth / diameter )
370 : NO_VALUE;
371 break;
372 }
373
375 text = group.m_Symbol.m_Description;
376 break;
377 }
378
379 row.m_Cells.push_back( std::move( text ) );
380 }
381 }
382
383 if( m_showTotals )
384 {
385 int operations = 0;
386
387 // Sites are counted over distinct coordinates, not summed per group. A backdrilled
388 // via contributes several operations at one location and is still one site
389 std::set<std::pair<int, int>> sites;
390
391 for( const DRILL_CHART_GROUP& group : groups )
392 {
393 operations += group.m_OperationCount;
394
395 for( const VECTOR2I& site : group.m_Sites )
396 sites.emplace( site.x, site.y );
397 }
398
399 content->m_Trailer.push_back( { wxString::Format( wxT( "%d OPS / %zu SITES" ), operations, sites.size() ) } );
400 }
401
402 return content;
403}
404
405
407{
408 // The chart is not stale after a flip, so no rebuild would move the marks to the new column
409 if( m_symbolColumn >= 0 )
410 m_symbolColumn = static_cast<int>( m_columns.size() ) - 1 - m_symbolColumn;
411}
412
413
414void PCB_DRILL_CHART::onRebuilt( const BOARD& aBoard, const GENERATED_TABLE_CONTENT& aContent )
415{
416 const DRILL_CHART_CONTENT& content = static_cast<const DRILL_CHART_CONTENT&>( aContent );
417
418 m_rowShapes.clear();
419
420 for( size_t ii = 0; ii < content.m_RowShapes.size(); ++ii )
421 {
422 if( content.m_RowShapes[ii] )
423 m_rowShapes[1 + static_cast<int>( ii )] = *content.m_RowShapes[ii];
424 }
425
428}
429
430
431void PCB_DRILL_CHART::Serialize( google::protobuf::Any& aContainer ) const
432{
433 using namespace kiapi::board;
434 types::DrillChart chart;
435
436 PackGeneratedTable( *this, chart );
437
438 types::DrillChartFilter* filter = chart.mutable_filter();
439 filter->set_plated( m_filter.m_Plated );
440 filter->set_non_plated( m_filter.m_NonPlated );
441 filter->set_vias( m_filter.m_Vias );
442 filter->set_slots( m_filter.m_Slots );
443 filter->set_backdrills( m_filter.m_Backdrills );
444 filter->set_castellated( m_filter.m_Castellated );
445
446 chart.set_show_totals( m_showTotals );
447
448 chart.set_symbol_column( m_symbolColumn );
449
450 for( const auto& [row, shapeIndex] : m_rowShapes )
451 ( *chart.mutable_row_shapes() )[row] = shapeIndex;
452
453 if( !aContainer.PackFrom( chart ) )
454 wxFAIL_MSG( wxT( "Failed to pack PCB_DRILL_CHART" ) );
455}
456
457
458bool PCB_DRILL_CHART::Deserialize( const google::protobuf::Any& aContainer )
459{
460 using namespace kiapi::board;
461 types::DrillChart chart;
462
463 if( !aContainer.UnpackTo( &chart ) )
464 {
465 return false;
466 }
467
468 if( !UnpackGeneratedTable( chart, *this ) )
469 {
470 return false;
471 }
472
473 if( chart.has_filter() )
474 {
475 m_filter.m_Plated = chart.filter().plated();
476 m_filter.m_NonPlated = chart.filter().non_plated();
477 m_filter.m_Vias = chart.filter().vias();
478 m_filter.m_Slots = chart.filter().slots();
479 m_filter.m_Backdrills = chart.filter().backdrills();
480 m_filter.m_Castellated = chart.filter().castellated();
481 }
482
483 m_symbolColumn = chart.symbol_column();
484
485 m_rowShapes.clear();
486
487 for( const auto& [row, shapeIndex] : chart.row_shapes() )
488 m_rowShapes[row] = shapeIndex;
489
490 // The cells come with the message, laid out with its setting
491 RestoreShowTotals( chart.show_totals() );
492 m_laidOutShowTotals.reset();
493
494 return true;
495}
496
497
499{
501 {
504
510
511 const wxString chartProps = _( "Drill Chart Properties" );
512
513 propMgr.AddProperty( new PROPERTY<PCB_DRILL_CHART, bool>( _HKI( "Show Totals" ),
516 chartProps );
517
518 const wxString includeProps = _HKI( "Include Holes" );
519
520 propMgr.AddProperty( new PROPERTY<PCB_DRILL_CHART, bool>( _HKI( "Plated Holes" ),
523 includeProps );
524
525 propMgr.AddProperty( new PROPERTY<PCB_DRILL_CHART, bool>( _HKI( "Non-plated Holes" ),
528 includeProps );
529
532 includeProps );
533
536 includeProps );
537
538 propMgr.AddProperty( new PROPERTY<PCB_DRILL_CHART, bool>( _HKI( "Backdrills" ),
541 includeProps );
542
543 propMgr.AddProperty( new PROPERTY<PCB_DRILL_CHART, bool>( _HKI( "Castellated" ),
546 includeProps );
547 }
void PackGeneratedTable(const PCB_GENERATED_TABLE &aTable, MSG &aMsg)
Pack what every generated table shares into its message.
bool UnpackGeneratedTable(const MSG &aMsg, PCB_GENERATED_TABLE &aTable)
The reverse of PackGeneratedTable.
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:128
DRILL_SYMBOL_PROFILE & GetDrillSymbolProfile()
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:99
int GetLayerDistance(PCB_LAYER_ID aFirstLayer, PCB_LAYER_ID aSecondLayer) const
Calculate the distance (height) between the two given copper layers.
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:411
BOARD_STACKUP GetStackupOrDefault() const
Definition board.cpp:3506
uint64_t GetDrillModelGeneration() const
Definition board.h:597
const wxString GetLayerName(PCB_LAYER_ID aLayer) const
Return the name of a aLayer.
Definition board.cpp:960
BOARD_DESIGN_SETTINGS & GetDesignSettings()
Definition board.cpp:1318
The column set and formatting a new chart starts from.
GENERATED_TABLE_UNITS GetUnits() const
std::vector< GENERATED_TABLE_COLUMN > & Columns()
static DRILL_CHART_TEMPLATE MakeDefault()
Grouping rules and symbol assignments, shared by reference so a chart and its map can never disagree ...
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
One pass of rebuilding tables.
T & Pending()
The pass's T, created from the board on first use.
A drill chart placed on the board, kept in step with the holes.
bool GetIncludeNonPlated() const
bool GetIncludeBackdrills() const
bool GetIncludeCastellated() const
PCB_DRILL_CHART(BOARD_ITEM *aParent)
void Serialize(google::protobuf::Any &aContainer) const override
Serializes this object to the given Any message.
bool Deserialize(const google::protobuf::Any &aContainer) override
Deserializes the given protobuf message into this object.
void SetIncludePlated(bool aInclude)
void ApplyTemplate(const DRILL_CHART_TEMPLATE &aTemplate)
Copy a template's formatting.
void swapData(BOARD_ITEM *aImage) override
std::optional< bool > m_laidOutShowTotals
Set only between a change and the next rebuild.
void onRebuilt(const BOARD &aBoard, const GENERATED_TABLE_CONTENT &aContent) override
Called once the cells are laid out, with the content they were built from.
std::map< int, int > m_rowShapes
void onColumnsReversed() override
Called once the columns have been reversed, for any column index a subclass keeps.
void ApplySettings(const PCB_GENERATED_TABLE &aOther) override
Take aOther's settings and placement but keep this table's cells, so formatting applied to them survi...
uint64_t m_builtGeneration
void SetShowTotals(bool aShow)
A chart without row keys can only tell its totals row by position, so it remembers the setting its ro...
bool operator==(const BOARD_ITEM &aOther) const override
wxString GetItemDescription(UNITS_PROVIDER *aUnitsProvider, bool aFull) const override
Return a user-visible description string of this item.
std::unique_ptr< GENERATED_TABLE_CONTENT > generate(const BOARD &aBoard, GENERATED_TABLE_REFRESH *aRefresh) const override
The rows this table reports.
bool GetIncludeSlots() const
std::vector< DRILL_CHART_GROUP > buildGroups(const BOARD &aBoard, const DRILL_SYMBOL_PROFILE &aProfile) const
The board data this chart reports on, as chart rows grouped the way aProfile says.
bool IsDataRow(int aRow) const override
True for a row that reports a drill group, false for the title, heading and totals.
void SetIncludeVias(bool aInclude)
void SetIncludeCastellated(bool aInclude)
bool GetShowTotals() const
void SetIncludeNonPlated(bool aInclude)
bool GetIncludeVias() const
void SetIncludeSlots(bool aInclude)
bool GetIncludePlated() const
void RestoreShowTotals(bool aShow)
Set it without that, for a loader whose cells were laid out with this setting.
bool IsStale(const BOARD &aBoard) const override
One integer comparison, so a board with nothing drill related changed pays nothing.
DRILL_CHART_COLUMN_ID columnId(int aCol) const
The typed id a column at this index holds, so generate's switch stays exhaustive even though a stored...
DRILL_CHART_FILTER m_filter
void SetIncludeBackdrills(bool aInclude)
A table whose cell text is derived from the board and rebuilt whenever it goes stale.
std::vector< GENERATED_TABLE_COLUMN > m_columns
virtual void ApplySettings(const PCB_GENERATED_TABLE &aOther)
Take aOther's settings and placement but keep this table's cells, so formatting applied to them survi...
GENERATED_TABLE_UNITS m_units
virtual bool IsDataRow(int aRow) const
True for a row that reports on the board, false for the heading and trailer rows.
bool generatedEquals(const PCB_GENERATED_TABLE &aOther) const
Equality of the members this class owns and of the underlying PCB_TABLE.
void swapData(BOARD_ITEM *aImage) override
PCB_GENERATED_TABLE(BOARD_ITEM *aParent, KICAD_T aType, int aLineWidth)
std::map< int, std::string > m_rowKeys
int GetRowCount() const
Definition pcb_table.h:128
Provide class metadata.
void InheritsAfter(TYPE_ID aDerived, TYPE_ID aBase)
Declare an inheritance relationship between types.
static PROPERTY_MANAGER & Instance()
PROPERTY_BASE & AddProperty(PROPERTY_BASE *aProperty, const wxString &aGroup=wxEmptyString)
Register a property.
void AddTypeCast(TYPE_CAST_BASE *aCast)
Register a type converter.
DRILL_CHART_COLUMN_ID
std::vector< DRILL_SPAN > EnumerateDrillSpans(const BOARD &aBoard, bool aPadBackdrills)
Every drill span present on the board, through-holes first.
std::map< std::string, DRILL_SYMBOL_ASSIGNMENT > ResolveDrillSymbols(const BOARD &aBoard)
The symbol every hole should carry, without touching the board.
void AssignDrillSymbols(std::vector< DRILL_CHART_GROUP > &aGroups, DRILL_SYMBOL_PROFILE &aProfile)
Give every group a mark, keeping the ones the profile already records.
#define _(s)
wxString FormatGeneratedTableLength(int aValue, GENERATED_TABLE_UNITS aUnits, int aPrecision)
Render a length the way a generated table's cell shows it.
GENERATED_TABLE_UNITS
Some functions to handle hotkeys in KiCad.
#define _HKI(x)
Definition page_info.cpp:40
#define NO_VALUE
static struct PCB_DRILL_CHART_DESC _PCB_DRILL_CHART_DESC
#define TYPE_HASH(x)
Macro to generate unique identifier for a type.
Definition property.h:74
#define REGISTER_TYPE(x)
Helper macro to map type hashes to names.
A drill chart's rows, with the symbol each one is drawn with.
std::vector< std::optional< int > > m_RowShapes
Per m_Rows entry, empty when the mark is not a shape.
PCB_LAYER_ID m_TopLayer
DRILL_SYMBOL_ASSIGNMENT m_Symbol
PCB_LAYER_ID m_BottomLayer
Turns the board's drill operations into chart rows for one symbol profile.
DRILL_CHART_FILTER m_Filter
The board's drill symbol profile as a refresh assigns new symbols into it.
DRILL_SYMBOL_PROFILE m_Profile
void Commit(BOARD &aBoard) override
DRILL_PROFILE_PENDING(BOARD &aBoard)
What a generated table reports, one entry per cell below the heading row.
One data row of a generated table, and the identity its formatting follows across rebuilds.
std::vector< wxString > m_Cells
KIBIS_MODEL * model
@ PCB_DRILL_CHART_T
class PCB_DRILL_CHART, a live drill chart derived from PCB_TABLE
Definition typeinfo.h:239
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708