32#include <nlohmann/json.hpp>
39#include <wx/datetime.h>
169 switch( footprint->GetSide() )
192 auto trackShapeA = track->GetEffectiveShape( layer );
196 if( layer != otherTrack->GetLayer() )
199 if( track->GetNetCode() == otherTrack->GetNetCode() )
206 auto trackShapeB = otherTrack->GetEffectiveShape( layer );
229 std::vector<DRILL_LINE_ITEM> drills;
260 for(
int j = 0; j < polySet.
HoleCount( i ); ++j )
265 for(
PAD*
pad : footprint->Pads() )
267 if( !
pad->HasHole() )
270 std::shared_ptr<SHAPE_SEGMENT> hole =
pad->GetEffectiveHoleShape();
275 const SEG& seg = hole->GetSeg();
276 double width = hole->GetWidth();
277 double area = seg.
Length() * width;
279 area +=
M_PI * 0.25 * width * width;
289 double drill =
via->GetDrillValue();
310 int minPadClearanceOuter = netSettings->GetDefaultNetclass()->GetClearance();
317 frontShapesForArea.
Append( frontA );
320 backShapesForArea.
Append( backA );
324 for(
PAD*
pad : fp->Pads() )
326 if( !
pad->HasHole() )
331 pad->TransformShapeToPolygon( frontShapesForArea,
F_Cu,
332 std::min( minPadClearanceOuter,
pad->GetOwnClearance(
F_Cu ) ),
334 pad->TransformShapeToPolygon( backShapesForArea,
B_Cu,
335 std::min( minPadClearanceOuter,
pad->GetOwnClearance(
B_Cu ) ),
420 return wxString::Format( wxT(
"%i" ), aCount );
424static void appendTable(
const std::vector<std::vector<wxString>>& aRows,
bool aUseFirstColAsLabel, wxString& aOut )
429 size_t columnCount = 0;
431 for(
const std::vector<wxString>& row : aRows )
433 if( row.size() > columnCount )
434 columnCount = row.size();
437 if( columnCount == 0 )
440 std::vector<int> widths( columnCount, 0 );
442 for(
const std::vector<wxString>& row : aRows )
444 for(
size_t col = 0; col < columnCount; ++col )
446 if( col >= row.size() )
449 int cellWidth =
static_cast<int>( row[col].length() );
451 if( cellWidth > widths[col] )
452 widths[col] = cellWidth;
457 [&](
const std::vector<wxString>& row,
bool treatFirstAsLabel )
459 if( treatFirstAsLabel && aUseFirstColAsLabel )
462 wxString firstColumn;
465 firstColumn = row[0];
467 formatted.Printf( wxS(
"|%-*s |" ), widths[0], firstColumn );
470 for(
size_t col = 1; col < columnCount; ++col )
473 if( col < row.size() )
476 formatted.Printf( wxS(
" %*s |" ), widths[col], value );
484 for(
size_t col = 0; col < columnCount; ++col )
487 if( col < row.size() )
491 formatted.Printf( wxS(
" %*s |" ), widths[col], value );
499 appendDataRow( aRows.front(),
false );
503 for(
size_t col = 0; col < columnCount; ++col )
505 int dashCount = widths[col] + 2;
512 for(
int i = 0; i < dashCount; ++i )
513 dashes << wxS(
"-" );
515 aOut << dashes << wxS(
"|" );
520 for(
size_t rowIdx = 1; rowIdx < aRows.size(); ++rowIdx )
521 appendDataRow( aRows[rowIdx],
true );
526 const UNITS_PROVIDER& aUnitsProvider,
const wxString& aProjectName,
527 const wxString& aBoardName )
531 report <<
_(
"PCB statistics report\n=====================" ) << wxS(
"\n" );
532 report << wxS(
"- " ) <<
_(
"Date" ) << wxS(
": " ) << wxDateTime::Now().Format() << wxS(
"\n" );
533 report << wxS(
"- " ) <<
_(
"Project" ) << wxS(
": " ) << aProjectName << wxS(
"\n" );
534 report << wxS(
"- " ) <<
_(
"Board name" ) << wxS(
": " ) << aBoardName << wxS(
"\n\n" );
536 report <<
_(
"Board" ) << wxS(
"\n-----\n" );
542 report << wxS(
"- " ) <<
_(
"Height" ) << wxS(
": " )
544 report << wxS(
"- " ) <<
_(
"Area" ) << wxS(
": " )
549 report << wxS(
"- " ) <<
_(
"Dimensions" ) << wxS(
": " ) <<
_(
"unknown" ) << wxS(
"\n" );
550 report << wxS(
"- " ) <<
_(
"Area" ) << wxS(
": " ) <<
_(
"unknown" ) << wxS(
"\n" );
553 report << wxS(
"- " ) <<
_(
"Front copper area" ) << wxS(
": " )
555 report << wxS(
"- " ) <<
_(
"Back copper area" ) << wxS(
": " )
558 report << wxS(
"- " ) <<
_(
"Min track clearance" ) << wxS(
": " )
562 report << wxS(
"- " ) <<
_(
"Min track width" ) << wxS(
": " )
567 report << wxS(
"- " ) <<
_(
"Min drill diameter" ) << wxS(
": " )
570 report << wxS(
"- " ) <<
_(
"Board stackup thickness" ) << wxS(
": " )
574 report << wxS(
"- " ) <<
_(
"Front footprint area" ) << wxS(
": " )
576 report << wxS(
"- " ) <<
_(
"Back footprint area" ) << wxS(
": " )
579 report << wxS(
"- " ) <<
_(
"Front component density" ) << wxS(
": " );
586 report <<
_(
"unknown" );
588 report << wxS(
"\n" );
590 report << wxS(
"- " ) <<
_(
"Back component density" ) << wxS(
": " );
597 report <<
_(
"unknown" );
599 report << wxS(
"\n" );
601 report <<
_(
"Pads" ) << wxS(
"\n----\n" );
604 report << wxS(
"- " ) << padEntry.
title << wxS(
" " ) << padEntry.
quantity << wxS(
"\n" );
607 report << wxS(
"- " ) << propEntry.
title << wxS(
" " ) << propEntry.
quantity << wxS(
"\n" );
609 report << wxS(
"\n" );
610 report <<
_(
"Vias" ) << wxS(
"\n----\n" );
613 report << wxS(
"- " ) << viaEntry.
title << wxS(
" " ) << viaEntry.
quantity << wxS(
"\n" );
615 report << wxS(
"\n" );
616 report <<
_(
"Components" ) << wxS(
"\n----------\n\n" );
618 std::vector<std::vector<wxString>> componentRows;
619 std::vector<wxString> header;
620 header.push_back( wxString() );
621 header.push_back(
_(
"Front Side" ) );
622 header.push_back(
_(
"Back Side" ) );
623 header.push_back(
_(
"Total" ) );
624 componentRows.push_back( std::move( header ) );
631 std::vector<wxString> row;
632 row.push_back( fpEntry.
title );
636 componentRows.push_back( std::move( row ) );
642 std::vector<wxString> totalRow;
643 totalRow.push_back(
_(
"Total:" ) );
646 totalRow.push_back(
formatCount( frontTotal + backTotal ) );
647 componentRows.push_back( std::move( totalRow ) );
651 report << wxS(
"\n" );
652 report <<
_(
"Drill holes" ) << wxS(
"\n-----------\n\n" );
654 std::vector<std::vector<wxString>> drillRows;
655 std::vector<wxString> drillHeader;
656 drillHeader.push_back(
_(
"Count" ) );
657 drillHeader.push_back(
_(
"Shape" ) );
658 drillHeader.push_back(
_(
"X Size" ) );
659 drillHeader.push_back(
_(
"Y Size" ) );
660 drillHeader.push_back(
_(
"Plated" ) );
661 drillHeader.push_back(
_(
"Via/Pad" ) );
662 drillHeader.push_back(
_(
"Start Layer" ) );
663 drillHeader.push_back(
_(
"Stop Layer" ) );
664 drillRows.push_back( std::move( drillHeader ) );
670 switch( drill.
shape )
674 default: shapeStr =
_(
"???" );
break;
677 wxString platedStr = drill.
isPlated ?
_(
"PTH" ) :
_(
"NPTH" );
678 wxString itemStr = drill.
isPad ?
_(
"Pad" ) :
_(
"Via" );
680 wxString startLayerStr;
681 wxString stopLayerStr;
684 startLayerStr =
_(
"N/A" );
688 startLayerStr =
_(
"N/A" );
691 stopLayerStr =
_(
"N/A" );
695 stopLayerStr =
_(
"N/A" );
697 std::vector<wxString> row;
699 row.push_back( shapeStr );
702 row.push_back( platedStr );
703 row.push_back( itemStr );
704 row.push_back( startLayerStr );
705 row.push_back( stopLayerStr );
706 drillRows.push_back( std::move( row ) );
716 const UNITS_PROVIDER& aUnitsProvider,
const wxString& aProjectName,
717 const wxString& aBoardName )
719 nlohmann::ordered_json root;
721 nlohmann::ordered_json metadata;
724 metadata[
"project"] = aProjectName;
725 metadata[
"board_name"] = aBoardName;
726 root[
"metadata"] = metadata;
728 nlohmann::ordered_json board;
741 board[
"width"] = nlohmann::json();
742 board[
"height"] = nlohmann::json();
743 board[
"area"] = nlohmann::json();
744 board[
"front_component_density"] = nlohmann::json();
745 board[
"back_component_density"] = nlohmann::json();
748 board[
"front_copper_area"] =
755 board[
"front_footprint_area"] =
757 board[
"back_footprint_area"] =
767 board[
"front_footprint_density"] = nlohmann::json();
768 board[
"back_footprint_density"] = nlohmann::json();
771 root[
"board"] = board;
776 [](
const wxString& title,
bool removeSuffix ) -> wxString
778 wxString
json = title;
783 if(
json.EndsWith( wxS(
":" ) ) )
786 json.Replace( wxS(
" " ), wxS(
"_" ) );
787 json.Replace( wxS(
"-" ), wxS(
"_" ) );
789 return json.MakeLower();
792 nlohmann::ordered_json pads = nlohmann::ordered_json::object();
798 pads[jsonize( propEntry.
title,
false )] = propEntry.
quantity;
802 nlohmann::ordered_json vias = nlohmann::ordered_json::object();
811 nlohmann::ordered_json components = nlohmann::ordered_json::object();
815 nlohmann::ordered_json component;
819 components[jsonize( fpEntry.
title,
false )] = component;
825 nlohmann::ordered_json totals;
826 totals[
"front"] = frontTotal;
827 totals[
"back"] = backTotal;
828 totals[
"total"] = frontTotal + backTotal;
829 components[
"total"] = totals;
831 root[
"components"] = components;
833 nlohmann::ordered_json drillHoles = nlohmann::ordered_json::array();
837 nlohmann::ordered_json drillJson;
841 switch( drill.
shape )
845 default: shapeStr =
_(
"???" );
break;
848 wxString sourceStr = drill.
isPad ?
_(
"Pad" ) :
_(
"Via" );
849 wxString startLayerStr =
_(
"N/A" );
850 wxString stopLayerStr =
_(
"N/A" );
858 drillJson[
"count"] = drill.
m_Qty;
859 drillJson[
"shape"] = shapeStr;
862 drillJson[
"plated"] = drill.
isPlated;
863 drillJson[
"source"] = sourceStr;
864 drillJson[
"start_layer"] = startLayerStr;
865 drillJson[
"stop_layer"] = stopLayerStr;
866 drillHoles.push_back( std::move( drillJson ) );
869 root[
"drill_holes"] = drillHoles;
871 std::string jsonText = root.dump( 2 );
872 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.
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...
virtual bool IsOnLayer(PCB_LAYER_ID aLayer) const
Test to see if this object is on the given layer.
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.
int BuildBoardThicknessFromStackup() const
Information pertinent to a Pcbnew printed circuit board.
BOARD_STACKUP GetStackupOrDefault() const
const FOOTPRINTS & Footprints() const
const TRACKS & Tracks() const
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...
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.
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
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.
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.
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
double backFootprintCourtyardArea
double frontFootprintDensity
std::vector< BOARD_STATISTICS_FP_ENTRY > footprintEntries
std::vector< BOARD_STATISTICS_INFO_ENTRY< VIATYPE > > viaEntries
double backFootprintDensity
double frontFootprintCourtyardArea
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