KiCad PCB EDA Suite
Loading...
Searching...
No Matches
board_statistics_report.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 modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * 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#include "build_version.h"
22
23#include <board.h>
26#include <footprint.h>
27#include <pad.h>
28#include <pcb_track.h>
32#include <i18n_utility.h>
33#include <json_common.h>
34#include <string_utils.h>
35
36#include <algorithm>
37#include <cmath>
38#include <limits>
39#include <memory>
40#include <wx/datetime.h>
41
43 hasOutline( false ),
44 boardWidth( 0 ),
45 boardHeight( 0 ),
46 boardArea( 0.0 ),
47 frontCopperArea( 0.0 ),
48 backCopperArea( 0.0 ),
53 minClearanceTrackToTrack( std::numeric_limits<int>::max() ),
54 minTrackWidth( std::numeric_limits<int>::max() ),
55 minDrillSize( std::numeric_limits<int>::max() ),
56 boardThickness( 0 ),
58 padEntries(),
60 viaEntries(),
62{
63}
64
65
67{
68 hasOutline = false;
69 boardWidth = 0;
70 boardHeight = 0;
71 boardArea = 0.0;
72 frontCopperArea = 0.0;
73 backCopperArea = 0.0;
76 minClearanceTrackToTrack = std::numeric_limits<int>::max();
77 minTrackWidth = std::numeric_limits<int>::max();
78 minDrillSize = std::numeric_limits<int>::max();
80
82 {
83 fp.frontCount = 0;
84 fp.backCount = 0;
85 }
86
88 pad.quantity = 0;
89
91 prop.quantity = 0;
92
94 via.quantity = 0;
95
96 drillEntries.clear();
97}
98
99
101{
102 aData.footprintEntries.clear();
103 aData.padEntries.clear();
104 aData.padPropertyEntries.clear();
105 aData.viaEntries.clear();
106 aData.drillEntries.clear();
107
108 aData.footprintEntries.push_back(
110 aData.footprintEntries.push_back( BOARD_STATISTICS_FP_ENTRY( FP_SMD, FP_SMD, _( "SMD:" ), "smd" ) );
111 aData.footprintEntries.push_back(
112 BOARD_STATISTICS_FP_ENTRY( FP_THROUGH_HOLE | FP_SMD, 0, _( "Unspecified:" ), "unspecified" ) );
113
114 aData.padEntries.push_back(
115 BOARD_STATISTICS_INFO_ENTRY<PAD_ATTRIB>( PAD_ATTRIB::PTH, _( "Through hole:" ), "through_hole" ) );
116 aData.padEntries.push_back( BOARD_STATISTICS_INFO_ENTRY<PAD_ATTRIB>( PAD_ATTRIB::SMD, _( "SMD:" ), "smd" ) );
117 aData.padEntries.push_back(
118 BOARD_STATISTICS_INFO_ENTRY<PAD_ATTRIB>( PAD_ATTRIB::CONN, _( "Connector:" ), "connector" ) );
119 aData.padEntries.push_back( BOARD_STATISTICS_INFO_ENTRY<PAD_ATTRIB>( PAD_ATTRIB::NPTH, _( "NPTH:" ), "npth" ) );
120 aData.padEntries.push_back(
121 BOARD_STATISTICS_INFO_ENTRY<PAD_ATTRIB>( PAD_ATTRIB::NON_COMPONENT, _( "Non-component:" ), "non_component" ) );
122
123 aData.padPropertyEntries.push_back(
124 BOARD_STATISTICS_INFO_ENTRY<PAD_PROP>( PAD_PROP::CASTELLATED, _( "Castellated:" ), "castellated" ) );
125 aData.padPropertyEntries.push_back(
126 BOARD_STATISTICS_INFO_ENTRY<PAD_PROP>( PAD_PROP::PRESSFIT, _( "Press-fit:" ), "press_fit" ) );
127
128 aData.viaEntries.push_back(
129 BOARD_STATISTICS_INFO_ENTRY<VIATYPE>( VIATYPE::THROUGH, _( "Through vias:" ), "through" ) );
130 aData.viaEntries.push_back( BOARD_STATISTICS_INFO_ENTRY<VIATYPE>( VIATYPE::BLIND, _( "Blind vias:" ), "blind" ) );
131 aData.viaEntries.push_back(
132 BOARD_STATISTICS_INFO_ENTRY<VIATYPE>( VIATYPE::BURIED, _( "Buried vias:" ), "buried" ) );
133 aData.viaEntries.push_back(
134 BOARD_STATISTICS_INFO_ENTRY<VIATYPE>( VIATYPE::MICROVIA, _( "Micro vias:" ), "micro" ) );
135
136 aData.ResetCounts();
137}
138
139
140static void updatePadCounts( FOOTPRINT* aFootprint, BOARD_STATISTICS_DATA& aData )
141{
142 for( PAD* pad : aFootprint->Pads() )
143 {
145 {
146 if( pad->GetAttribute() == padEntry.attribute )
147 {
148 padEntry.quantity++;
149 break;
150 }
151 }
152
154 {
155 if( pad->GetProperty() == propEntry.attribute )
156 {
157 propEntry.quantity++;
158 break;
159 }
160 }
161 }
162}
163
164
166{
167 aData.ResetCounts();
168
169 if( !aBoard )
170 return;
171
172 static const std::vector<KICAD_T> trackTypes = { PCB_TRACE_T, PCB_ARC_T, PCB_VIA_T };
173
174 for( FOOTPRINT* footprint : aBoard->Footprints() )
175 {
176 if( aOptions.excludeFootprintsWithoutPads && footprint->Pads().empty() )
177 continue;
178
179 for( BOARD_STATISTICS_FP_ENTRY& entry : aData.footprintEntries )
180 {
181 if( ( footprint->GetAttributes() & entry.attributeMask ) == entry.attributeValue )
182 {
183 switch( footprint->GetSide() )
184 {
185 case F_Cu: entry.frontCount++; break;
186 case B_Cu: entry.backCount++; break;
187 default: break;
188 }
189
190 break;
191 }
192 }
193
194 updatePadCounts( footprint, aData );
195 }
196
197 for( PCB_TRACK* track : aBoard->Tracks() )
198 {
199 if( track->Type() == PCB_TRACE_T || track->Type() == PCB_ARC_T )
200 aData.minTrackWidth = std::min( aData.minTrackWidth, track->GetWidth() );
201
202 if( !track->IsType( trackTypes ) )
203 continue;
204
205 for( PCB_TRACK* otherTrack : aBoard->Tracks() )
206 {
207 if( track->GetNetCode() == otherTrack->GetNetCode() )
208 continue;
209
210 if( !otherTrack->IsType( trackTypes ) )
211 continue;
212
213 LSET commonLayers = track->GetLayerSet() & otherTrack->GetLayerSet() & LSET::AllCuMask();
214
215 for( PCB_LAYER_ID layer : commonLayers.CuStack() )
216 {
217 int actual = 0;
218 auto trackShapeA = track->GetEffectiveShape( layer );
219 auto trackShapeB = otherTrack->GetEffectiveShape( layer );
220 bool collide = trackShapeA->Collide( trackShapeB.get(), aData.minClearanceTrackToTrack, &actual );
221
222 if( collide )
224 }
225 }
226
227 if( track->Type() == PCB_VIA_T )
228 {
229 PCB_VIA* via = static_cast<PCB_VIA*>( track );
230
232 {
233 if( via->GetViaType() == entry.attribute )
234 {
235 entry.quantity++;
236 break;
237 }
238 }
239 }
240 }
241
242 {
243 std::vector<DRILL_LINE_ITEM> drills;
244 CollectDrillLineItems( aBoard, drills );
245
246 aData.drillEntries = std::move( drills );
247 }
248
249 std::sort( aData.drillEntries.begin(), aData.drillEntries.end(),
251
252 aData.minDrillSize = std::numeric_limits<int>::max();
253
254 for( const DRILL_LINE_ITEM& drill : aData.drillEntries )
255 {
256 if( drill.shape == PAD_DRILL_SHAPE::CIRCLE )
257 aData.minDrillSize = std::min( aData.minDrillSize, drill.xSize );
258 }
259
260 SHAPE_POLY_SET polySet;
261 aData.hasOutline = aBoard->GetBoardPolygonOutlines( polySet, false );
262
263 if( aData.hasOutline )
264 {
265 aData.boardArea = 0.0;
266
267 for( int i = 0; i < polySet.OutlineCount(); ++i )
268 {
269 SHAPE_LINE_CHAIN& outline = polySet.Outline( i );
270 aData.boardArea += outline.Area();
271
272 if( aOptions.subtractHolesFromBoardArea )
273 {
274 for( int j = 0; j < polySet.HoleCount( i ); ++j )
275 aData.boardArea -= polySet.Hole( i, j ).Area();
276 }
277 }
278
279 if( aOptions.subtractHolesFromBoardArea )
280 {
281 for( FOOTPRINT* footprint : aBoard->Footprints() )
282 {
283 for( PAD* pad : footprint->Pads() )
284 {
285 if( !pad->HasHole() )
286 continue;
287
288 std::shared_ptr<SHAPE_SEGMENT> hole = pad->GetEffectiveHoleShape();
289
290 if( !hole )
291 continue;
292
293 const SEG& seg = hole->GetSeg();
294 double width = hole->GetWidth();
295 double area = seg.Length() * width;
296
297 area += M_PI * 0.25 * width * width;
298 aData.boardArea -= area;
299 }
300 }
301
302 for( PCB_TRACK* track : aBoard->Tracks() )
303 {
304 if( track->Type() == PCB_VIA_T )
305 {
306 PCB_VIA* via = static_cast<PCB_VIA*>( track );
307 double drill = via->GetDrillValue();
308 aData.boardArea -= M_PI * 0.25 * drill * drill;
309 }
310 }
311 }
312
313 BOX2I bbox = polySet.BBox();
314
315 aData.boardWidth = static_cast<int>( bbox.GetWidth() );
316 aData.boardHeight = static_cast<int>( bbox.GetHeight() );
317
318 }
319
320 // Now determine the courtyard areas which reflects how much space is occupied by components
321 // This will always assume all components are populated as its intended for
322 // layout purposes and arguing with people saying theres not enough space.
323 SHAPE_POLY_SET frontShapesForArea;
324 SHAPE_POLY_SET backShapesForArea;
325
326 std::shared_ptr<NET_SETTINGS>& netSettings = aBoard->GetDesignSettings().m_NetSettings;
327 int minPadClearanceOuter = netSettings->GetDefaultNetclass()->GetClearance();
328
329 for( FOOTPRINT* fp : aBoard->Footprints() )
330 {
331 const SHAPE_POLY_SET& frontA = fp->GetCourtyard( F_CrtYd );
332 const SHAPE_POLY_SET& backA = fp->GetCourtyard( B_CrtYd );
333
334 if( frontA.OutlineCount() != 0 )
335 frontShapesForArea.Append( frontA );
336
337 if( backA.OutlineCount() != 0 )
338 backShapesForArea.Append( backA );
339
340 // PTH/NPTH holes in footprints can be outside the main courtyard and also
341 // consume space on the other side of the board but without a courtyard
342 for( PAD* pad : fp->Pads() )
343 {
344 if( !pad->HasHole() )
345 continue;
346
347 if( pad->GetAttribute() != PAD_ATTRIB::NPTH )
348 {
349 pad->TransformShapeToPolygon( frontShapesForArea, F_Cu,
350 std::min( minPadClearanceOuter, pad->GetOwnClearance( F_Cu ) ),
352 pad->TransformShapeToPolygon( backShapesForArea, B_Cu,
353 std::min( minPadClearanceOuter, pad->GetOwnClearance( B_Cu ) ),
355 }
356 else
357 {
358 pad->TransformHoleToPolygon( frontShapesForArea, 0, ARC_LOW_DEF, ERROR_INSIDE );
359 pad->TransformHoleToPolygon( backShapesForArea, 0, ARC_LOW_DEF, ERROR_INSIDE );
360 }
361 }
362 }
363
364 // deal with overlapping courtyards (if people are ignoring DRC or something)
365 // and such through simplify
366 frontShapesForArea.Simplify();
367 backShapesForArea.Simplify();
368
369 aData.frontFootprintCourtyardArea = frontShapesForArea.Area();
370 aData.backFootprintCourtyardArea = backShapesForArea.Area();
371
372 if( aData.hasOutline )
373 {
376 }
377
378 SHAPE_POLY_SET frontCopper;
379 SHAPE_POLY_SET backCopper;
380 SHAPE_POLY_SET frontHoles;
381 SHAPE_POLY_SET backHoles;
382
383 aBoard->RunOnChildren(
384 [&]( BOARD_ITEM* child )
385 {
386 if( child->Type() == PCB_FOOTPRINT_T
387 || child->Type() == PCB_GROUP_T
388 || child->Type() == PCB_GENERATOR_T
389 || child->Type() == PCB_REFERENCE_IMAGE_T )
390 {
391 return;
392 }
393
394 if( child->IsOnLayer( F_Cu ) )
395 child->TransformShapeToPolySet( frontCopper, F_Cu, 0, ARC_LOW_DEF, ERROR_INSIDE );
396
397 if( child->IsOnLayer( B_Cu ) )
398 child->TransformShapeToPolySet( backCopper, B_Cu, 0, ARC_LOW_DEF, ERROR_INSIDE );
399
400 if( child->Type() == PCB_PAD_T )
401 {
402 PAD* pad = static_cast<PAD*>( child );
403
404 if( pad->HasHole() )
405 {
406 pad->TransformHoleToPolygon( frontHoles, 0, ARC_LOW_DEF, ERROR_OUTSIDE );
407 pad->TransformHoleToPolygon( backHoles, 0, ARC_LOW_DEF, ERROR_OUTSIDE );
408 }
409 }
410 else if( child->Type() == PCB_VIA_T )
411 {
412 PCB_VIA* via = static_cast<PCB_VIA*>( child );
413 VECTOR2I center = via->GetPosition();
414 int radius = via->GetDrillValue() / 2;
415
416 if( via->IsOnLayer( F_Cu ) )
418
419 if( via->IsOnLayer( B_Cu ) )
421 }
422 },
424
425 frontCopper.Simplify();
426 backCopper.Simplify();
427
428 if( aOptions.subtractHolesFromCopperAreas )
429 {
430 frontCopper.BooleanSubtract( frontHoles );
431 backCopper.BooleanSubtract( backHoles );
432 }
433
434 aData.frontCopperArea = frontCopper.Area();
435 aData.backCopperArea = backCopper.Area();
436
438}
439
440
441static wxString formatCount( int aCount )
442{
443 return wxString::Format( wxT( "%i" ), aCount );
444}
445
446
447static void appendTable( const std::vector<std::vector<wxString>>& aRows, bool aUseFirstColAsLabel, wxString& aOut )
448{
449 if( aRows.empty() )
450 return;
451
452 size_t columnCount = 0;
453
454 for( const std::vector<wxString>& row : aRows )
455 {
456 if( row.size() > columnCount )
457 columnCount = row.size();
458 }
459
460 if( columnCount == 0 )
461 return;
462
463 std::vector<int> widths( columnCount, 0 );
464
465 for( const std::vector<wxString>& row : aRows )
466 {
467 for( size_t col = 0; col < columnCount; ++col )
468 {
469 if( col >= row.size() )
470 continue;
471
472 int cellWidth = static_cast<int>( row[col].length() );
473
474 if( cellWidth > widths[col] )
475 widths[col] = cellWidth;
476 }
477 }
478
479 auto appendDataRow =
480 [&]( const std::vector<wxString>& row, bool treatFirstAsLabel )
481 {
482 if( treatFirstAsLabel && aUseFirstColAsLabel )
483 {
484 wxString formatted;
485 wxString firstColumn;
486
487 if( !row.empty() )
488 firstColumn = row[0];
489
490 formatted.Printf( wxS( "|%-*s |" ), widths[0], firstColumn );
491 aOut << formatted;
492
493 for( size_t col = 1; col < columnCount; ++col )
494 {
495 wxString value;
496 if( col < row.size() )
497 value = row[col];
498
499 formatted.Printf( wxS( " %*s |" ), widths[col], value );
500 aOut << formatted;
501 }
502 }
503 else
504 {
505 aOut << wxS( "|" );
506
507 for( size_t col = 0; col < columnCount; ++col )
508 {
509 wxString value;
510 if( col < row.size() )
511 value = row[col];
512
513 wxString formatted;
514 formatted.Printf( wxS( " %*s |" ), widths[col], value );
515 aOut << formatted;
516 }
517 }
518
519 aOut << wxS( "\n" );
520 };
521
522 appendDataRow( aRows.front(), false );
523
524 aOut << wxS( "|" );
525
526 for( size_t col = 0; col < columnCount; ++col )
527 {
528 int dashCount = widths[col] + 2;
529
530 if( dashCount < 3 )
531 dashCount = 3;
532
533 wxString dashes;
534
535 for( int i = 0; i < dashCount; ++i )
536 dashes << wxS( "-" );
537
538 aOut << dashes << wxS( "|" );
539 }
540
541 aOut << wxS( "\n" );
542
543 for( size_t rowIdx = 1; rowIdx < aRows.size(); ++rowIdx )
544 appendDataRow( aRows[rowIdx], true );
545}
546
547
548wxString FormatBoardStatisticsMinimum( int aValue, const UNITS_PROVIDER& aUnitsProvider )
549{
550 if( aValue == std::numeric_limits<int>::max() )
551 return _( "unknown" );
552
553 return aUnitsProvider.MessageTextFromValue( aValue, true, EDA_DATA_TYPE::DISTANCE );
554}
555
556
558 const UNITS_PROVIDER& aUnitsProvider, const wxString& aProjectName,
559 const wxString& aBoardName )
560{
561 wxString report;
562
563 report << _( "PCB statistics report\n=====================" ) << wxS( "\n" );
564 report << wxS( "- " ) << _( "Date" ) << wxS( ": " ) << wxDateTime::Now().Format() << wxS( "\n" );
565 report << wxS( "- " ) << _( "Project" ) << wxS( ": " ) << aProjectName << wxS( "\n" );
566 report << wxS( "- " ) << _( "Board name" ) << wxS( ": " ) << aBoardName << wxS( "\n\n" );
567
568 report << _( "Board" ) << wxS( "\n-----\n" );
569
570 if( aData.hasOutline )
571 {
572 report << wxS( "- " ) << _( "Width" ) << wxS( ": " ) << aUnitsProvider.MessageTextFromValue( aData.boardWidth )
573 << wxS( "\n" );
574 report << wxS( "- " ) << _( "Height" ) << wxS( ": " )
575 << aUnitsProvider.MessageTextFromValue( aData.boardHeight ) << wxS( "\n" );
576 report << wxS( "- " ) << _( "Area" ) << wxS( ": " )
577 << aUnitsProvider.MessageTextFromValue( aData.boardArea, true, EDA_DATA_TYPE::AREA ) << wxS( "\n" );
578 }
579 else
580 {
581 report << wxS( "- " ) << _( "Dimensions" ) << wxS( ": " ) << _( "unknown" ) << wxS( "\n" );
582 report << wxS( "- " ) << _( "Area" ) << wxS( ": " ) << _( "unknown" ) << wxS( "\n" );
583 }
584
585 report << wxS( "- " ) << _( "Front copper area" ) << wxS( ": " )
586 << aUnitsProvider.MessageTextFromValue( aData.frontCopperArea, true, EDA_DATA_TYPE::AREA ) << wxS( "\n" );
587 report << wxS( "- " ) << _( "Back copper area" ) << wxS( ": " )
588 << aUnitsProvider.MessageTextFromValue( aData.backCopperArea, true, EDA_DATA_TYPE::AREA ) << wxS( "\n" );
589
590 report << wxS( "- " ) << _( "Min track clearance" ) << wxS( ": " )
591 << FormatBoardStatisticsMinimum( aData.minClearanceTrackToTrack, aUnitsProvider ) << wxS( "\n" );
592
593 report << wxS( "- " ) << _( "Min track width" ) << wxS( ": " )
594 << FormatBoardStatisticsMinimum( aData.minTrackWidth, aUnitsProvider ) << wxS( "\n" );
595
596 report << wxS( "- " ) << _( "Min drill diameter" ) << wxS( ": " )
597 << FormatBoardStatisticsMinimum( aData.minDrillSize, aUnitsProvider ) << wxS( "\n" );
598
599 report << wxS( "- " ) << _( "Board stackup thickness" ) << wxS( ": " )
600 << aUnitsProvider.MessageTextFromValue( aData.boardThickness, true, EDA_DATA_TYPE::DISTANCE )
601 << wxS( "\n\n" );
602
603 report << wxS( "- " ) << _( "Front footprint area" ) << wxS( ": " )
604 << aUnitsProvider.MessageTextFromValue( aData.frontFootprintCourtyardArea, true, EDA_DATA_TYPE::AREA ) << wxS( "\n" );
605 report << wxS( "- " ) << _( "Back footprint area" ) << wxS( ": " )
606 << aUnitsProvider.MessageTextFromValue( aData.backFootprintCourtyardArea, true, EDA_DATA_TYPE::AREA ) << wxS( "\n" );
607
608 report << wxS( "- " ) << _( "Front component density" ) << wxS( ": " );
609
610 if( aData.hasOutline )
611 report << wxString::Format( "%.2f %%", aData.frontFootprintDensity );
612 else
613 report << _( "unknown" );
614
615 report << wxS( "\n" );
616
617 report << wxS( "- " ) << _( "Back component density" ) << wxS( ": " );
618
619 if( aData.hasOutline )
620 report << wxString::Format( "%.2f %%", aData.backFootprintDensity );
621 else
622 report << _( "unknown" );
623
624 report << wxS( "\n" );
625
626 report << _( "Pads" ) << wxS( "\n----\n" );
627
628 for( const BOARD_STATISTICS_INFO_ENTRY<PAD_ATTRIB>& padEntry : aData.padEntries )
629 report << wxS( "- " ) << padEntry.title << wxS( " " ) << padEntry.quantity << wxS( "\n" );
630
631 for( const BOARD_STATISTICS_INFO_ENTRY<PAD_PROP>& propEntry : aData.padPropertyEntries )
632 report << wxS( "- " ) << propEntry.title << wxS( " " ) << propEntry.quantity << wxS( "\n" );
633
634 report << wxS( "\n" );
635 report << _( "Vias" ) << wxS( "\n----\n" );
636
637 for( const BOARD_STATISTICS_INFO_ENTRY<VIATYPE>& viaEntry : aData.viaEntries )
638 report << wxS( "- " ) << viaEntry.title << wxS( " " ) << viaEntry.quantity << wxS( "\n" );
639
640 report << wxS( "\n" );
641 report << _( "Components" ) << wxS( "\n----------\n\n" );
642
643 std::vector<std::vector<wxString>> componentRows;
644 std::vector<wxString> header;
645 header.push_back( wxString() );
646 header.push_back( _( "Front Side" ) );
647 header.push_back( _( "Back Side" ) );
648 header.push_back( _( "Total" ) );
649 componentRows.push_back( std::move( header ) );
650
651 int frontTotal = 0;
652 int backTotal = 0;
653
654 for( const BOARD_STATISTICS_FP_ENTRY& fpEntry : aData.footprintEntries )
655 {
656 std::vector<wxString> row;
657 row.push_back( fpEntry.title );
658 row.push_back( formatCount( fpEntry.frontCount ) );
659 row.push_back( formatCount( fpEntry.backCount ) );
660 row.push_back( formatCount( fpEntry.frontCount + fpEntry.backCount ) );
661 componentRows.push_back( std::move( row ) );
662
663 frontTotal += fpEntry.frontCount;
664 backTotal += fpEntry.backCount;
665 }
666
667 std::vector<wxString> totalRow;
668 totalRow.push_back( _( "Total:" ) );
669 totalRow.push_back( formatCount( frontTotal ) );
670 totalRow.push_back( formatCount( backTotal ) );
671 totalRow.push_back( formatCount( frontTotal + backTotal ) );
672 componentRows.push_back( std::move( totalRow ) );
673
674 appendTable( componentRows, true, report );
675
676 report << wxS( "\n" );
677 report << _( "Drill holes" ) << wxS( "\n-----------\n\n" );
678
679 std::vector<std::vector<wxString>> drillRows;
680 std::vector<wxString> drillHeader;
681 drillHeader.push_back( _( "Count" ) );
682 drillHeader.push_back( _( "Shape" ) );
683 drillHeader.push_back( _( "X Size" ) );
684 drillHeader.push_back( _( "Y Size" ) );
685 drillHeader.push_back( _( "Plated" ) );
686 drillHeader.push_back( _( "Via/Pad" ) );
687 drillHeader.push_back( _( "Start Layer" ) );
688 drillHeader.push_back( _( "Stop Layer" ) );
689 drillRows.push_back( std::move( drillHeader ) );
690
691 for( const DRILL_LINE_ITEM& drill : aData.drillEntries )
692 {
693 wxString shapeStr;
694
695 switch( drill.shape )
696 {
697 case PAD_DRILL_SHAPE::CIRCLE: shapeStr = _( "Round" ); break;
698 case PAD_DRILL_SHAPE::OBLONG: shapeStr = _( "Slot" ); break;
699 default: shapeStr = _( "???" ); break;
700 }
701
702 wxString platedStr = drill.isPlated ? _( "PTH" ) : _( "NPTH" );
703 wxString itemStr = drill.isPad ? _( "Pad" ) : _( "Via" );
704
705 wxString startLayerStr;
706 wxString stopLayerStr;
707
708 if( drill.startLayer == UNDEFINED_LAYER )
709 startLayerStr = _( "N/A" );
710 else if( aBoard )
711 startLayerStr = aBoard->GetLayerName( drill.startLayer );
712 else
713 startLayerStr = _( "N/A" );
714
715 if( drill.stopLayer == UNDEFINED_LAYER )
716 stopLayerStr = _( "N/A" );
717 else if( aBoard )
718 stopLayerStr = aBoard->GetLayerName( drill.stopLayer );
719 else
720 stopLayerStr = _( "N/A" );
721
722 std::vector<wxString> row;
723 row.push_back( formatCount( drill.m_Qty ) );
724 row.push_back( shapeStr );
725 row.push_back( aUnitsProvider.MessageTextFromValue( drill.xSize ) );
726 row.push_back( aUnitsProvider.MessageTextFromValue( drill.ySize ) );
727 row.push_back( platedStr );
728 row.push_back( itemStr );
729 row.push_back( startLayerStr );
730 row.push_back( stopLayerStr );
731 drillRows.push_back( std::move( row ) );
732 }
733
734 appendTable( drillRows, false, report );
735
736 return report;
737}
738
739
741 const UNITS_PROVIDER& aUnitsProvider, const wxString& aProjectName,
742 const wxString& aBoardName )
743{
744 const auto formatMinimum = [&aUnitsProvider]( int aValue ) -> nlohmann::json
745 {
746 if( aValue == std::numeric_limits<int>::max() )
747 return nlohmann::json();
748
749 return aUnitsProvider.MessageTextFromValue( aValue );
750 };
751
752 nlohmann::ordered_json root;
753
754 nlohmann::ordered_json metadata;
755 metadata["date"] = GetISO8601CurrentDateTime();
756 metadata["generator"] = "KiCad " + GetBuildVersion();
757 metadata["project"] = aProjectName;
758 metadata["board_name"] = aBoardName;
759 root["metadata"] = metadata;
760
761 nlohmann::ordered_json board;
762 board["has_outline"] = aData.hasOutline;
763
764 if( aData.hasOutline )
765 {
766 board["width"] = aUnitsProvider.MessageTextFromValue( aData.boardWidth );
767 board["height"] = aUnitsProvider.MessageTextFromValue( aData.boardHeight );
768 board["area"] = aUnitsProvider.MessageTextFromValue( aData.boardArea, true, EDA_DATA_TYPE::AREA );
769 board["front_component_density"] = wxString::Format( "%.2f", aData.frontFootprintDensity );
770 board["back_component_density"] = wxString::Format( "%.2f", aData.backFootprintDensity );
771 }
772 else
773 {
774 board["width"] = nlohmann::json();
775 board["height"] = nlohmann::json();
776 board["area"] = nlohmann::json();
777 board["front_component_density"] = nlohmann::json();
778 board["back_component_density"] = nlohmann::json();
779 }
780
781 board["front_copper_area"] = aUnitsProvider.MessageTextFromValue( aData.frontCopperArea, true,
783 board["back_copper_area"] = aUnitsProvider.MessageTextFromValue( aData.backCopperArea, true,
785 board["min_track_clearance"] = formatMinimum( aData.minClearanceTrackToTrack );
786 board["min_track_width"] = formatMinimum( aData.minTrackWidth );
787 board["min_drill_diameter"] = formatMinimum( aData.minDrillSize );
788 board["board_thickness"] = aUnitsProvider.MessageTextFromValue( aData.boardThickness );
789 board["front_footprint_area"] = aUnitsProvider.MessageTextFromValue( aData.frontFootprintCourtyardArea, true,
791 board["back_footprint_area"] = aUnitsProvider.MessageTextFromValue( aData.backFootprintCourtyardArea, true,
793
794 if( aData.hasOutline )
795 {
796 board["front_footprint_density"] = wxString::Format( "%.2f", aData.frontFootprintDensity );
797 board["back_footprint_density"] = wxString::Format( "%.2f", aData.backFootprintDensity );
798 }
799 else
800 {
801 board["front_footprint_density"] = nlohmann::json();
802 board["back_footprint_density"] = nlohmann::json();
803 }
804
805 root["board"] = board;
806
807 nlohmann::ordered_json pads = nlohmann::ordered_json::object();
808
809 for( const BOARD_STATISTICS_INFO_ENTRY<PAD_ATTRIB>& padEntry : aData.padEntries )
810 pads[padEntry.jsonKey] = padEntry.quantity;
811
812 for( const BOARD_STATISTICS_INFO_ENTRY<PAD_PROP>& propEntry : aData.padPropertyEntries )
813 pads[propEntry.jsonKey] = propEntry.quantity;
814
815 root["pads"] = pads;
816
817 nlohmann::ordered_json vias = nlohmann::ordered_json::object();
818
819 for( const BOARD_STATISTICS_INFO_ENTRY<VIATYPE>& viaEntry : aData.viaEntries )
820 vias[viaEntry.jsonKey] = viaEntry.quantity;
821
822 root["vias"] = vias;
823
824 int frontTotal = 0;
825 int backTotal = 0;
826 nlohmann::ordered_json components = nlohmann::ordered_json::object();
827
828 for( const BOARD_STATISTICS_FP_ENTRY& fpEntry : aData.footprintEntries )
829 {
830 nlohmann::ordered_json component;
831 component["front"] = fpEntry.frontCount;
832 component["back"] = fpEntry.backCount;
833 component["total"] = fpEntry.frontCount + fpEntry.backCount;
834 components[fpEntry.jsonKey] = component;
835
836 frontTotal += fpEntry.frontCount;
837 backTotal += fpEntry.backCount;
838 }
839
840 nlohmann::ordered_json totals;
841 totals["front"] = frontTotal;
842 totals["back"] = backTotal;
843 totals["total"] = frontTotal + backTotal;
844 components["total"] = totals;
845
846 root["components"] = components;
847
848 nlohmann::ordered_json drillHoles = nlohmann::ordered_json::array();
849
850 for( const DRILL_LINE_ITEM& drill : aData.drillEntries )
851 {
852 nlohmann::ordered_json drillJson;
853
854 wxString shapeStr;
855
856 switch( drill.shape )
857 {
858 case PAD_DRILL_SHAPE::CIRCLE: shapeStr = _( "Round" ); break;
859 case PAD_DRILL_SHAPE::OBLONG: shapeStr = _( "Slot" ); break;
860 default: shapeStr = _( "???" ); break;
861 }
862
863 wxString sourceStr = drill.isPad ? _( "Pad" ) : _( "Via" );
864 wxString startLayerStr = _( "N/A" );
865 wxString stopLayerStr = _( "N/A" );
866
867 if( aBoard && drill.startLayer != UNDEFINED_LAYER )
868 startLayerStr = aBoard->GetLayerName( drill.startLayer );
869
870 if( aBoard && drill.stopLayer != UNDEFINED_LAYER )
871 stopLayerStr = aBoard->GetLayerName( drill.stopLayer );
872
873 drillJson["count"] = drill.m_Qty;
874 drillJson["shape"] = shapeStr;
875 drillJson["x_size"] = aUnitsProvider.MessageTextFromValue( drill.xSize );
876 drillJson["y_size"] = aUnitsProvider.MessageTextFromValue( drill.ySize );
877 drillJson["plated"] = drill.isPlated;
878 drillJson["source"] = sourceStr;
879 drillJson["start_layer"] = startLayerStr;
880 drillJson["stop_layer"] = stopLayerStr;
881 drillHoles.push_back( std::move( drillJson ) );
882 }
883
884 root["drill_holes"] = drillHoles;
885
886 std::string jsonText = root.dump( 2 );
887 return wxString::FromUTF8( jsonText.c_str() );
888}
@ ERROR_OUTSIDE
@ ERROR_INSIDE
constexpr int ARC_LOW_DEF
Definition base_units.h:143
void CollectDrillLineItems(BOARD *board, std::vector< DRILL_LINE_ITEM > &out)
static void updatePadCounts(FOOTPRINT *aFootprint, BOARD_STATISTICS_DATA &aData)
wxString FormatBoardStatisticsJson(const BOARD_STATISTICS_DATA &aData, BOARD *aBoard, const UNITS_PROVIDER &aUnitsProvider, const wxString &aProjectName, const wxString &aBoardName)
void ComputeBoardStatistics(BOARD *aBoard, const BOARD_STATISTICS_OPTIONS &aOptions, BOARD_STATISTICS_DATA &aData)
static wxString formatCount(int aCount)
wxString FormatBoardStatisticsReport(const BOARD_STATISTICS_DATA &aData, BOARD *aBoard, const UNITS_PROVIDER &aUnitsProvider, const wxString &aProjectName, const wxString &aBoardName)
wxString FormatBoardStatisticsMinimum(int aValue, const UNITS_PROVIDER &aUnitsProvider)
Format a minimum distance, or "unknown" when no qualifying measurement was found.
void InitializeBoardStatisticsData(BOARD_STATISTICS_DATA &aData)
static void appendTable(const std::vector< std::vector< wxString > > &aRows, bool aUseFirstColAsLabel, wxString &aOut)
BOX2< VECTOR2I > BOX2I
Definition box2.h:917
wxString GetBuildVersion()
Get the full KiCad version string.
std::shared_ptr< NET_SETTINGS > m_NetSettings
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:99
virtual bool IsOnLayer(PCB_LAYER_ID aLayer) const
Test to see if this object is on the given layer.
Definition board_item.h:432
virtual void TransformShapeToPolySet(SHAPE_POLY_SET &aBuffer, PCB_LAYER_ID aLayer, int aClearance, int aError, ERROR_LOC aErrorLoc, KIGFX::RENDER_SETTINGS *aRenderSettings=nullptr) const
Convert the item shape to a polyset.
Definition board_item.h:582
int BuildBoardThicknessFromStackup() const
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:411
BOARD_STACKUP GetStackupOrDefault() const
Definition board.cpp:3506
const FOOTPRINTS & Footprints() const
Definition board.h:465
const TRACKS & Tracks() const
Definition board.h:463
bool GetBoardPolygonOutlines(SHAPE_POLY_SET &aOutlines, bool aInferOutlineIfNecessary, OUTLINE_ERROR_HANDLER *aErrorHandler=nullptr, bool aAllowUseArcsInPolygons=false, bool aIncludeNPTHAsOutlines=false)
Extract the board outlines and build a closed polygon from lines, arcs and circle items on edge cut l...
Definition board.cpp:3651
const wxString GetLayerName(PCB_LAYER_ID aLayer) const
Return the name of a aLayer.
Definition board.cpp:960
void RunOnChildren(const std::function< void(BOARD_ITEM *)> &aFunction, RECURSE_MODE aMode) const override
Invoke a function on all children.
Definition board.cpp:865
BOARD_DESIGN_SETTINGS & GetDesignSettings()
Definition board.cpp:1318
constexpr size_type GetWidth() const
Definition box2.h:217
constexpr size_type GetHeight() const
Definition box2.h:218
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
std::deque< PAD * > & Pads()
Definition footprint.h:355
LSET is a set of PCB_LAYER_IDs.
Definition lset.h:37
static const LSET & AllCuMask()
return AllCuMask( MAX_CU_LAYERS );
Definition lset.cpp:604
LSEQ CuStack() const
Return a sequence of copper layers in starting from the front/top and extending to the back/bottom.
Definition lset.cpp:259
Definition pad.h:61
Definition seg.h:38
int Length() const
Return the length (this).
Definition seg.h:361
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
double Area(bool aAbsolute=true) const
Return the area of this chain.
Represent a set of closed polygons.
double Area()
Return the area of this poly set.
int HoleCount(int aOutline) const
Returns the number of holes in a given outline.
int Append(int x, int y, int aOutline=-1, int aHole=-1, bool aAllowDuplication=false)
Appends a vertex at the end of the given outline/hole (default: the last outline)
void Simplify()
Simplify the polyset (merges overlapping polys, eliminates degeneracy/self-intersections)
SHAPE_LINE_CHAIN & Outline(int aIndex)
Return the reference to aIndex-th outline in the set.
SHAPE_LINE_CHAIN & Hole(int aOutline, int aHole)
Return the reference to aHole-th hole in the aIndex-th outline.
int OutlineCount() const
Return the number of outlines in the set.
void BooleanSubtract(const SHAPE_POLY_SET &b)
Perform boolean polyset difference.
const BOX2I BBox(int aClearance=0) const override
Compute a bounding box of the shape, with a margin of aClearance a collision.
wxString MessageTextFromValue(double aValue, bool aAddUnitLabel=true, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE) const
A lower-precision version of StringFromValue().
void TransformCircleToPolygon(SHAPE_LINE_CHAIN &aBuffer, const VECTOR2I &aCenter, int aRadius, int aError, ERROR_LOC aErrorLoc, int aMinSegCount=0)
Convert a circle to a polygon, using multiple straight lines.
#define _(s)
@ RECURSE
Definition eda_item.h:51
static const std::vector< KICAD_T > trackTypes
@ FP_SMD
Definition footprint.h:87
@ FP_THROUGH_HOLE
Definition footprint.h:86
Some functions to handle hotkeys in KiCad.
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
@ F_CrtYd
Definition layer_ids.h:112
@ B_Cu
Definition layer_ids.h:61
@ B_CrtYd
Definition layer_ids.h:111
@ UNDEFINED_LAYER
Definition layer_ids.h:57
@ F_Cu
Definition layer_ids.h:60
STL namespace.
@ NPTH
like PAD_PTH, but not plated mechanical use only, no connection allowed
Definition padstack.h:104
@ NON_COMPONENT
Pad with no hole that does not connect to a physical component, but rather provides a connection poin...
Definition padstack.h:106
@ SMD
Smd pad, appears on the solder paste layer (default)
Definition padstack.h:100
@ PTH
Plated through hole pad.
Definition padstack.h:99
@ CONN
Like smd, does not appear on the solder paste layer (default) Note: also has a special attribute in G...
Definition padstack.h:101
@ PRESSFIT
a PTH with a hole diameter with tight tolerances for press fit pin
Definition padstack.h:127
@ CASTELLATED
a pad with a castellated through hole
Definition padstack.h:125
bool collide(T aObject, U aAnotherObject, int aLayer, int aMinDistance)
Used by SHAPE_INDEX to implement Query().
wxString GetISO8601CurrentDateTime()
std::vector< BOARD_STATISTICS_INFO_ENTRY< PAD_ATTRIB > > padEntries
std::vector< BOARD_STATISTICS_FP_ENTRY > footprintEntries
std::vector< BOARD_STATISTICS_INFO_ENTRY< VIATYPE > > viaEntries
std::vector< BOARD_STATISTICS_INFO_ENTRY< PAD_PROP > > padPropertyEntries
std::vector< DRILL_LINE_ITEM > drillEntries
int attributeMask
int attributeValue
int backCount
int frontCount
std::string jsonKey
wxString title
std::string jsonKey
int quantity
wxString title
T attribute
PCB_LAYER_ID stopLayer
PAD_DRILL_SHAPE shape
PCB_LAYER_ID startLayer
static wxString formatted(LIB_SYMBOL &aSymbol)
Serialize one symbol the way the library writer would.
VECTOR2I center
int radius
int actual
#define M_PI
@ PCB_GENERATOR_T
class PCB_GENERATOR, generator on a layer
Definition typeinfo.h:83
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition typeinfo.h:89
@ PCB_GROUP_T
class PCB_GROUP, a set of BOARD_ITEMs
Definition typeinfo.h:103
@ PCB_REFERENCE_IMAGE_T
class PCB_REFERENCE_IMAGE, bitmap on a layer
Definition typeinfo.h:81
@ PCB_FOOTPRINT_T
class FOOTPRINT, a footprint
Definition typeinfo.h:78
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition typeinfo.h:79
@ PCB_ARC_T
class PCB_ARC, an arc track segment on a copper layer
Definition typeinfo.h:90
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition typeinfo.h:88
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708