KiCad PCB EDA Suite
Loading...
Searching...
No Matches
erc_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 (C) 2023, 2024 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 GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <wx/ffile.h>
21#include <wx/string.h>
22
23#include <sch_screen.h>
24#include <sch_marker.h>
25#include <schematic.h>
26#include <string_utils.h>
27#include <build_version.h>
28#include "erc_report.h"
29#include <eda_units.h>
30#include <erc/erc.h>
31#include <fstream>
32#include <macros.h>
33#include <nlohmann/json.hpp>
34#include <rc_json_schema.h>
35
36
37ERC_REPORT::ERC_REPORT( SCHEMATIC* aSchematic, EDA_UNITS aReportUnits ) :
38 m_sch( aSchematic ),
39 m_reportUnits( aReportUnits )
40{
41}
42
43
45{
46 UNITS_PROVIDER unitsProvider( schIUScale, m_reportUnits );
47
48 wxString msg = wxString::Format( _( "ERC report (%s, Encoding UTF8)\n" ),
50
51 std::map<KIID, EDA_ITEM*> itemMap;
52
53 int err_count = 0;
54 int warn_count = 0;
55 int total_count = 0;
57
58 sheetList.FillItemMap( itemMap );
59
60 ERC_SETTINGS& settings = m_sch->ErcSettings();
61
62 for( unsigned i = 0; i < sheetList.size(); i++ )
63 {
64 msg << wxString::Format( _( "\n***** Sheet %s\n" ), sheetList[i].PathHumanReadable() );
65
66 for( SCH_ITEM* aItem : sheetList[i].LastScreen()->Items().OfType( SCH_MARKER_T ) )
67 {
68 const SCH_MARKER* marker = static_cast<const SCH_MARKER*>( aItem );
69 RC_ITEM* item = marker->GetRCItem().get();
70 SEVERITY severity = settings.GetSeverity( item->GetErrorCode() );
71
72 if( marker->GetMarkerType() != MARKER_BASE::MARKER_ERC )
73 continue;
74
75 total_count++;
76
77 switch( severity )
78 {
79 case RPT_SEVERITY_ERROR: err_count++; break;
80 case RPT_SEVERITY_WARNING: warn_count++; break;
81 default: break;
82 }
83
84 msg << marker->GetRCItem()->ShowReport( &unitsProvider, severity, itemMap );
85 }
86 }
87
88 msg << wxString::Format( _( "\n ** ERC messages: %d Errors %d Warnings %d\n" ), total_count,
89 err_count, warn_count );
90
91 return msg;
92}
93
94
95bool ERC_REPORT::WriteTextReport( const wxString& aFullFileName )
96{
97 wxFFile file( aFullFileName, wxT( "wt" ) );
98
99 if( !file.IsOpened() )
100 return false;
101
102 file.Write( GetTextReport() );
103
104 // wxFFile dtor will close the file.
105 return true;
106}
107
108
109bool ERC_REPORT::WriteJsonReport( const wxString& aFullFileName )
110{
111 std::ofstream jsonFileStream( aFullFileName.fn_str() );
112
113 UNITS_PROVIDER unitsProvider( pcbIUScale, m_reportUnits );
114 std::map<KIID, EDA_ITEM*> itemMap;
115
116 RC_JSON::ERC_REPORT reportHead;
117 wxFileName fn( m_sch->GetFileName() );
118 reportHead.$schema = "https://schemas.kicad.org/erc.v1.json";
119 reportHead.source = fn.GetFullName();
120 reportHead.date = GetISO8601CurrentDateTime();
123
124 SCH_SHEET_LIST sheetList = m_sch->Hierarchy();
125 sheetList.FillItemMap( itemMap );
126
127 ERC_SETTINGS& settings = m_sch->ErcSettings();
128
129 for( unsigned i = 0; i < sheetList.size(); i++ )
130 {
131 RC_JSON::ERC_SHEET jsonSheet;
132 jsonSheet.path = sheetList[i].PathHumanReadable();
133 jsonSheet.uuid_path = sheetList[i].Path().AsString();
134
135 for( SCH_ITEM* aItem : sheetList[i].LastScreen()->Items().OfType( SCH_MARKER_T ) )
136 {
137 const SCH_MARKER* marker = static_cast<const SCH_MARKER*>( aItem );
138 RC_ITEM* item = marker->GetRCItem().get();
139 SEVERITY severity = settings.GetSeverity( item->GetErrorCode() );
140
141 if( marker->GetMarkerType() != MARKER_BASE::MARKER_ERC )
142 continue;
143
144 RC_JSON::VIOLATION violation;
145 marker->GetRCItem()->GetJsonViolation( violation, &unitsProvider, severity, itemMap );
146
147 jsonSheet.violations.push_back( violation );
148 }
149
150 reportHead.sheets.push_back( jsonSheet );
151 }
152
153 nlohmann::json saveJson = nlohmann::json( reportHead );
154 jsonFileStream << std::setw( 4 ) << saveJson << std::endl;
155 jsonFileStream.flush();
156 jsonFileStream.close();
157
158 return true;
159}
constexpr EDA_IU_SCALE schIUScale
Definition: base_units.h:110
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:108
wxString GetMajorMinorPatchVersion()
Get the major, minor and patch version in a string major.minor.patch This is extracted by CMake from ...
EDA_UNITS m_reportUnits
Definition: erc_report.h:58
ERC_REPORT(SCHEMATIC *aSchematic, EDA_UNITS aReportUnits)
Definition: erc_report.cpp:37
wxString GetTextReport()
Returns the ERC report in "text" (human readable) format.
Definition: erc_report.cpp:44
SCHEMATIC * m_sch
Definition: erc_report.h:57
bool WriteJsonReport(const wxString &aFullFileName)
Writes a JSON formatted ERC Report to the given file path.
Definition: erc_report.cpp:109
bool WriteTextReport(const wxString &aFullFileName)
Writes the text report also available via GetTextReport directly to a given file path.
Definition: erc_report.cpp:95
Container for ERC settings.
Definition: erc_settings.h:128
SEVERITY GetSeverity(int aErrorCode) const
std::shared_ptr< RC_ITEM > GetRCItem() const
Definition: marker_base.h:112
enum MARKER_T GetMarkerType() const
Definition: marker_base.h:96
A holder for a rule check item, DRC in Pcbnew or ERC in Eeschema.
Definition: rc_item.h:79
int GetErrorCode() const
Definition: rc_item.h:153
Holds all the data relating to one schematic.
Definition: schematic.h:77
wxString GetFileName() const override
Helper to retrieve the filename from the root sheet screen.
Definition: schematic.cpp:308
SCH_SHEET_LIST Hierarchy() const override
Return the full schematic flattened hierarchical sheet list.
Definition: schematic.cpp:214
SCH_SHEET_LIST BuildSheetListSortedByPageNumbers() const override
Definition: schematic.h:97
ERC_SETTINGS & ErcSettings() const
Definition: schematic.cpp:321
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:166
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
void FillItemMap(std::map< KIID, EDA_ITEM * > &aMap)
Fill an item cache for temporary use when many items need to be fetched.
#define _(s)
EDA_UNITS
Definition: eda_units.h:46
This file contains miscellaneous commonly used macros and functions.
KICOMMON_API wxString GetLabel(EDA_UNITS aUnits, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
Get the units string for a given units type.
Definition: eda_units.cpp:155
SEVERITY
@ RPT_SEVERITY_WARNING
@ RPT_SEVERITY_ERROR
wxString GetISO8601CurrentDateTime()
std::vector< ERC_SHEET > sheets
@ SCH_MARKER_T
Definition: typeinfo.h:158