KiCad PCB EDA Suite
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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 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 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
64
65 std::map<SCH_SHEET_PATH, std::vector<ERC_ITEM*>> orderedItems;
66
67 for( int i = 0; i < errors.GetCount(); ++i )
68 {
69 if( auto item = dynamic_cast<ERC_ITEM*>( errors.GetItem( i ).get() ) )
70 {
71 if( item->MainItemHasSheetPath() )
72 orderedItems[item->GetMainItemSheetPath()].emplace_back( item );
73 else
74 orderedItems[sheetList[0]].emplace_back( item );
75 }
76 }
77
78 for( unsigned i = 0; i < sheetList.size(); i++ )
79 {
80 msg << wxString::Format( _( "\n***** Sheet %s\n" ), sheetList[i].PathHumanReadable() );
81
82 for( ERC_ITEM* item : orderedItems[sheetList[i]] )
83 {
84 SEVERITY severity = settings.GetSeverity( item->GetErrorCode() );
85
86 total_count++;
87
88 switch( severity )
89 {
90 case RPT_SEVERITY_ERROR: err_count++; break;
91 case RPT_SEVERITY_WARNING: warn_count++; break;
92 default: break;
93 }
94
95 msg << item->ShowReport( &unitsProvider, severity, itemMap );
96 }
97 }
98
99 msg << wxString::Format( _( "\n ** ERC messages: %d Errors %d Warnings %d\n" ), total_count,
100 err_count, warn_count );
101
102 return msg;
103}
104
105
106bool ERC_REPORT::WriteTextReport( const wxString& aFullFileName )
107{
108 wxFFile file( aFullFileName, wxT( "wt" ) );
109
110 if( !file.IsOpened() )
111 return false;
112
113 file.Write( GetTextReport() );
114
115 // wxFFile dtor will close the file.
116 return true;
117}
118
119
120bool ERC_REPORT::WriteJsonReport( const wxString& aFullFileName )
121{
122 std::ofstream jsonFileStream( aFullFileName.fn_str() );
123
124 UNITS_PROVIDER unitsProvider( pcbIUScale, m_reportUnits );
125 std::map<KIID, EDA_ITEM*> itemMap;
126
127 RC_JSON::ERC_REPORT reportHead;
128 wxFileName fn( m_sch->GetFileName() );
129 reportHead.$schema = "https://schemas.kicad.org/erc.v1.json";
130 reportHead.source = fn.GetFullName();
131 reportHead.date = GetISO8601CurrentDateTime();
134
135 SCH_SHEET_LIST sheetList = m_sch->Hierarchy();
136 sheetList.FillItemMap( itemMap );
137
138 ERC_SETTINGS& settings = m_sch->ErcSettings();
139
142
143 std::map<SCH_SHEET_PATH, std::vector<ERC_ITEM*>> orderedItems;
144
145 for( int i = 0; i < errors.GetCount(); ++i )
146 {
147 if( auto item = dynamic_cast<ERC_ITEM*>( errors.GetItem( i ).get() ) )
148 {
149 if( item->MainItemHasSheetPath() )
150 orderedItems[item->GetMainItemSheetPath()].emplace_back( item );
151 else
152 orderedItems[sheetList[0]].emplace_back( item );
153 }
154 }
155
156 for( unsigned i = 0; i < sheetList.size(); i++ )
157 {
158 RC_JSON::ERC_SHEET jsonSheet;
159 jsonSheet.path = sheetList[i].PathHumanReadable();
160 jsonSheet.uuid_path = sheetList[i].Path().AsString();
161
162 for( ERC_ITEM* item : orderedItems[sheetList[i]] )
163 {
164 SEVERITY severity = settings.GetSeverity( item->GetErrorCode() );
165
166 RC_JSON::VIOLATION violation;
167 item->GetJsonViolation( violation, &unitsProvider, severity, itemMap );
168
169 jsonSheet.violations.push_back( violation );
170 }
171
172 reportHead.sheets.push_back( jsonSheet );
173 }
174
175 nlohmann::json saveJson = nlohmann::json( reportHead );
176 jsonFileStream << std::setw( 4 ) << saveJson << std::endl;
177 jsonFileStream.flush();
178 jsonFileStream.close();
179
180 return true;
181}
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:120
bool WriteTextReport(const wxString &aFullFileName)
Writes the text report also available via GetTextReport directly to a given file path.
Definition: erc_report.cpp:106
Container for ERC settings.
Definition: erc_settings.h:135
SEVERITY GetSeverity(int aErrorCode) const
Holds all the data relating to one schematic.
Definition: schematic.h:69
SCH_SHEET_LIST BuildSheetListSortedByPageNumbers() const
Definition: schematic.h:89
wxString GetFileName() const
Helper to retrieve the filename from the root sheet screen.
Definition: schematic.cpp:300
SCH_SHEET_LIST Hierarchy() const
Return the full schematic flattened hierarchical sheet list.
Definition: schematic.cpp:208
ERC_SETTINGS & ErcSettings() const
Definition: schematic.cpp:313
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.
An implementation of the RC_ITEM_LIST interface which uses the global SHEETLIST to fulfill the contra...
Definition: erc_settings.h:244
int GetCount(int aSeverity=-1) const override
void SetSeverities(int aSeverities) override
std::shared_ptr< RC_ITEM > GetItem(int aIndex) const override
Retrieve a RC_ITEM by index.
#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:156
SEVERITY
@ RPT_SEVERITY_WARNING
@ RPT_SEVERITY_ERROR
wxString GetISO8601CurrentDateTime()
std::vector< ERC_SHEET > sheets