31#include <nlohmann/json.hpp>
38#include <wx/datetime.h>
166 switch( footprint->GetSide() )
189 auto trackShapeA = track->GetEffectiveShape( layer );
193 if( layer != otherTrack->GetLayer() )
196 if( track->GetNetCode() == otherTrack->GetNetCode() )
203 auto trackShapeB = otherTrack->GetEffectiveShape( layer );
225 std::vector<DRILL_LINE_ITEM> drills;
255 for(
int j = 0; j < polySet.
HoleCount( i ); ++j )
260 for(
PAD*
pad : footprint->Pads() )
262 if( !
pad->HasHole() )
265 std::shared_ptr<SHAPE_SEGMENT> hole =
pad->GetEffectiveHoleShape();
270 const SEG& seg = hole->GetSeg();
271 double width = hole->GetWidth();
272 double area = seg.
Length() * width;
274 area +=
M_PI * 0.25 * width * width;
284 double drill =
via->GetDrillValue();
357 return wxString::Format( wxT(
"%i" ), aCount );
361static void appendTable(
const std::vector<std::vector<wxString>>& aRows,
bool aUseFirstColAsLabel, wxString& aOut )
366 size_t columnCount = 0;
368 for(
const std::vector<wxString>& row : aRows )
370 if( row.size() > columnCount )
371 columnCount = row.size();
374 if( columnCount == 0 )
377 std::vector<int> widths( columnCount, 0 );
379 for(
const std::vector<wxString>& row : aRows )
381 for(
size_t col = 0; col < columnCount; ++col )
383 if( col >= row.size() )
386 int cellWidth =
static_cast<int>( row[col].length() );
388 if( cellWidth > widths[col] )
389 widths[col] = cellWidth;
393 auto appendDataRow = [&](
const std::vector<wxString>& row,
bool treatFirstAsLabel )
395 if( treatFirstAsLabel && aUseFirstColAsLabel )
398 wxString firstColumn;
401 firstColumn = row[0];
403 formatted.Printf( wxS(
"|%-*s |" ), widths[0], firstColumn );
406 for(
size_t col = 1; col < columnCount; ++col )
409 if( col < row.size() )
412 formatted.Printf( wxS(
" %*s |" ), widths[col], value );
420 for(
size_t col = 0; col < columnCount; ++col )
423 if( col < row.size() )
427 formatted.Printf( wxS(
" %*s |" ), widths[col], value );
435 appendDataRow( aRows.front(),
false );
439 for(
size_t col = 0; col < columnCount; ++col )
441 int dashCount = widths[col] + 2;
448 for(
int i = 0; i < dashCount; ++i )
449 dashes << wxS(
"-" );
451 aOut << dashes << wxS(
"|" );
456 for(
size_t rowIdx = 1; rowIdx < aRows.size(); ++rowIdx )
457 appendDataRow( aRows[rowIdx],
true );
462 const UNITS_PROVIDER& aUnitsProvider,
const wxString& aProjectName,
463 const wxString& aBoardName )
467 report <<
_(
"PCB statistics report\n=====================" ) << wxS(
"\n" );
468 report << wxS(
"- " ) <<
_(
"Date" ) << wxS(
": " ) << wxDateTime::Now().Format() << wxS(
"\n" );
469 report << wxS(
"- " ) <<
_(
"Project" ) << wxS(
": " ) << aProjectName << wxS(
"\n" );
470 report << wxS(
"- " ) <<
_(
"Board name" ) << wxS(
": " ) << aBoardName << wxS(
"\n\n" );
472 report <<
_(
"Board" ) << wxS(
"\n-----\n" );
478 report << wxS(
"- " ) <<
_(
"Height" ) << wxS(
": " )
480 report << wxS(
"- " ) <<
_(
"Area" ) << wxS(
": " )
485 report << wxS(
"- " ) <<
_(
"Dimensions" ) << wxS(
": " ) <<
_(
"unknown" ) << wxS(
"\n" );
486 report << wxS(
"- " ) <<
_(
"Area" ) << wxS(
": " ) <<
_(
"unknown" ) << wxS(
"\n" );
489 report << wxS(
"- " ) <<
_(
"Front copper area" ) << wxS(
": " )
491 report << wxS(
"- " ) <<
_(
"Back copper area" ) << wxS(
": " )
494 report << wxS(
"- " ) <<
_(
"Min track clearance" ) << wxS(
": " )
498 report << wxS(
"- " ) <<
_(
"Min track width" ) << wxS(
": " )
503 report << wxS(
"- " ) <<
_(
"Min drill diameter" ) << wxS(
": " )
506 report << wxS(
"- " ) <<
_(
"Board stackup thickness" ) << wxS(
": " )
510 report <<
_(
"Pads" ) << wxS(
"\n----\n" );
513 report << wxS(
"- " ) << padEntry.
title << wxS(
" " ) << padEntry.
quantity << wxS(
"\n" );
516 report << wxS(
"- " ) << propEntry.
title << wxS(
" " ) << propEntry.
quantity << wxS(
"\n" );
518 report << wxS(
"\n" );
519 report <<
_(
"Vias" ) << wxS(
"\n----\n" );
522 report << wxS(
"- " ) << viaEntry.
title << wxS(
" " ) << viaEntry.
quantity << wxS(
"\n" );
524 report << wxS(
"\n" );
525 report <<
_(
"Components" ) << wxS(
"\n----------\n\n" );
527 std::vector<std::vector<wxString>> componentRows;
528 std::vector<wxString> header;
529 header.push_back( wxString() );
530 header.push_back(
_(
"Front Side" ) );
531 header.push_back(
_(
"Back Side" ) );
532 header.push_back(
_(
"Total" ) );
533 componentRows.push_back( header );
540 std::vector<wxString> row;
541 row.push_back( fpEntry.
title );
545 componentRows.push_back( row );
551 std::vector<wxString> totalRow;
552 totalRow.push_back(
_(
"Total:" ) );
555 totalRow.push_back(
formatCount( frontTotal + backTotal ) );
556 componentRows.push_back( totalRow );
560 report << wxS(
"\n" );
561 report <<
_(
"Drill holes" ) << wxS(
"\n-----------\n\n" );
563 std::vector<std::vector<wxString>> drillRows;
564 std::vector<wxString> drillHeader;
565 drillHeader.push_back(
_(
"Count" ) );
566 drillHeader.push_back(
_(
"Shape" ) );
567 drillHeader.push_back(
_(
"X Size" ) );
568 drillHeader.push_back(
_(
"Y Size" ) );
569 drillHeader.push_back(
_(
"Plated" ) );
570 drillHeader.push_back(
_(
"Via/Pad" ) );
571 drillHeader.push_back(
_(
"Start Layer" ) );
572 drillHeader.push_back(
_(
"Stop Layer" ) );
573 drillRows.push_back( drillHeader );
579 switch( drill.
shape )
583 default: shapeStr =
_(
"???" );
break;
586 wxString platedStr = drill.
isPlated ?
_(
"PTH" ) :
_(
"NPTH" );
587 wxString itemStr = drill.
isPad ?
_(
"Pad" ) :
_(
"Via" );
589 wxString startLayerStr;
590 wxString stopLayerStr;
593 startLayerStr =
_(
"N/A" );
597 startLayerStr =
_(
"N/A" );
600 stopLayerStr =
_(
"N/A" );
604 stopLayerStr =
_(
"N/A" );
606 std::vector<wxString> row;
608 row.push_back( shapeStr );
611 row.push_back( platedStr );
612 row.push_back( itemStr );
613 row.push_back( startLayerStr );
614 row.push_back( stopLayerStr );
615 drillRows.push_back( row );
625 const UNITS_PROVIDER& aUnitsProvider,
const wxString& aProjectName,
626 const wxString& aBoardName )
628 nlohmann::ordered_json root;
630 nlohmann::ordered_json metadata;
633 metadata[
"project"] = aProjectName;
634 metadata[
"board_name"] = aBoardName;
635 root[
"metadata"] = metadata;
637 nlohmann::ordered_json board;
648 board[
"width"] = nlohmann::json();
649 board[
"height"] = nlohmann::json();
650 board[
"area"] = nlohmann::json();
653 board[
"front_copper_area"] =
660 root[
"board"] = board;
664 auto jsonize = [](
const wxString& title,
bool removeSuffix )
666 wxString
json = title;
671 if(
json.EndsWith( wxS(
":" ) ) )
674 json.Replace( wxS(
" " ), wxS(
"_" ) );
675 json.Replace( wxS(
"-" ), wxS(
"_" ) );
677 return json.MakeLower();
680 nlohmann::ordered_json pads = nlohmann::ordered_json::object();
686 pads[jsonize( propEntry.
title,
false )] = propEntry.
quantity;
690 nlohmann::ordered_json vias = nlohmann::ordered_json::object();
699 nlohmann::ordered_json components = nlohmann::ordered_json::object();
703 nlohmann::ordered_json component;
707 components[jsonize( fpEntry.
title,
false )] = component;
713 nlohmann::ordered_json totals;
714 totals[
"front"] = frontTotal;
715 totals[
"back"] = backTotal;
716 totals[
"total"] = frontTotal + backTotal;
717 components[
"total"] = totals;
719 root[
"components"] = components;
721 nlohmann::ordered_json drillHoles = nlohmann::ordered_json::array();
725 nlohmann::ordered_json drillJson;
729 switch( drill.
shape )
733 default: shapeStr =
_(
"???" );
break;
736 wxString sourceStr = drill.
isPad ?
_(
"Pad" ) :
_(
"Via" );
737 wxString startLayerStr =
_(
"N/A" );
738 wxString stopLayerStr =
_(
"N/A" );
746 drillJson[
"count"] = drill.
m_Qty;
747 drillJson[
"shape"] = shapeStr;
750 drillJson[
"plated"] = drill.
isPlated;
751 drillJson[
"source"] = sourceStr;
752 drillJson[
"start_layer"] = startLayerStr;
753 drillJson[
"stop_layer"] = stopLayerStr;
754 drillHoles.push_back( drillJson );
757 root[
"drill_holes"] = drillHoles;
759 std::string jsonText = root.dump( 2 );
760 return wxString::FromUTF8( jsonText.c_str() );
constexpr int ARC_LOW_DEF
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)
void InitializeBoardStatisticsData(BOARD_STATISTICS_DATA &aData)
static void appendTable(const std::vector< std::vector< wxString > > &aRows, bool aUseFirstColAsLabel, wxString &aOut)
wxString GetBuildVersion()
Get the full KiCad version string.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
virtual void TransformShapeToPolySet(SHAPE_POLY_SET &aBuffer, PCB_LAYER_ID aLayer, int aClearance, int aError, ERROR_LOC aErrorLoc) const
Convert the item shape to a polyset.
virtual bool IsOnLayer(PCB_LAYER_ID aLayer) const
Test to see if this object is on the given layer.
int BuildBoardThicknessFromStackup() const
Information pertinent to a Pcbnew printed circuit board.
BOARD_STACKUP GetStackupOrDefault() const
bool GetBoardPolygonOutlines(SHAPE_POLY_SET &aOutlines, 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...
const FOOTPRINTS & Footprints() const
const TRACKS & Tracks() const
const wxString GetLayerName(PCB_LAYER_ID aLayer) const
Return the name of a aLayer.
void RunOnChildren(const std::function< void(BOARD_ITEM *)> &aFunction, RECURSE_MODE aMode) const override
Invoke a function on all children.
constexpr size_type GetWidth() const
constexpr size_type GetHeight() const
KICAD_T Type() const
Returns the type of object.
int Length() const
Return the length (this).
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.
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.
Some functions to handle hotkeys in KiCad.
PCB_LAYER_ID
A quick note on layer IDs:
@ NPTH
like PAD_PTH, but not plated mechanical use only, no connection allowed
@ SMD
Smd pad, appears on the solder paste layer (default)
@ PTH
Plated through hole pad.
@ CONN
Like smd, does not appear on the solder paste layer (default) Note: also has a special attribute in G...
@ PRESSFIT
a PTH with a hole diameter with tight tolerances for press fit pin
@ CASTELLATED
a pad with a castellated through hole
bool collide(T aObject, U aAnotherObject, int aLayer, int aMinDistance)
Used by SHAPE_INDEX to implement Query().
wxString GetISO8601CurrentDateTime()
int minClearanceTrackToTrack
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
bool subtractHolesFromCopperAreas
bool subtractHolesFromBoardArea
bool excludeFootprintsWithoutPads
@ PCB_GENERATOR_T
class PCB_GENERATOR, generator on a layer
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
@ PCB_GROUP_T
class PCB_GROUP, a set of BOARD_ITEMs
@ PCB_FOOTPRINT_T
class FOOTPRINT, a footprint
@ PCB_PAD_T
class PAD, a pad in a footprint
@ PCB_ARC_T
class PCB_ARC, an arc track segment on a copper layer
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
VECTOR2< int32_t > VECTOR2I