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 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 <locale_io.h>
24#include <sch_screen.h>
25#include <sch_marker.h>
26#include <schematic.h>
27#include <string_utils.h>
28#include <build_version.h>
29#include "erc_report.h"
30#include <eda_units.h>
31#include <erc/erc.h>
32#include <fstream>
33#include <macros.h>
34#include <json_common.h>
35#include <rc_json_schema.h>
36
37
38ERC_REPORT::ERC_REPORT( SCHEMATIC* aSchematic, EDA_UNITS aReportUnits ) :
39 m_sch( aSchematic ),
40 m_reportUnits( aReportUnits )
41{
42}
43
44
46{
47 // We need the global LOCALE_IO here in order to
48 // write the report in the c-locale.
49 LOCALE_IO locale;
50 UNITS_PROVIDER unitsProvider( schIUScale, m_reportUnits );
51
52 wxString msg = wxString::Format( _( "ERC report (%s, Encoding UTF8)\n" ),
54
55 std::map<KIID, EDA_ITEM*> itemMap;
56
57 int err_count = 0;
58 int warn_count = 0;
59 int total_count = 0;
61
62 sheetList.FillItemMap( itemMap );
63
64 ERC_SETTINGS& settings = m_sch->ErcSettings();
65
68
69 std::map<SCH_SHEET_PATH, std::vector<ERC_ITEM*>> orderedItems;
70
71 for( int i = 0; i < errors.GetCount(); ++i )
72 {
73 if( auto item = dynamic_cast<ERC_ITEM*>( errors.GetItem( i ).get() ) )
74 {
75 if( item->MainItemHasSheetPath() )
76 orderedItems[item->GetMainItemSheetPath()].emplace_back( item );
77 else
78 orderedItems[sheetList[0]].emplace_back( item );
79 }
80 }
81
82 for( unsigned i = 0; i < sheetList.size(); i++ )
83 {
84 msg << wxString::Format( _( "\n***** Sheet %s\n" ), sheetList[i].PathHumanReadable() );
85
86 for( ERC_ITEM* item : orderedItems[sheetList[i]] )
87 {
88 SEVERITY severity = settings.GetSeverity( item->GetErrorCode() );
89
90 total_count++;
91
92 switch( severity )
93 {
94 case RPT_SEVERITY_ERROR: err_count++; break;
95 case RPT_SEVERITY_WARNING: warn_count++; break;
96 default: break;
97 }
98
99 msg << item->ShowReport( &unitsProvider, severity, itemMap );
100 }
101 }
102
103 msg << wxString::Format( _( "\n ** ERC messages: %d Errors %d Warnings %d\n" ), total_count,
104 err_count, warn_count );
105
106 return msg;
107}
108
109
110bool ERC_REPORT::WriteTextReport( const wxString& aFullFileName )
111{
112 wxFFile file( aFullFileName, wxT( "wt" ) );
113
114 if( !file.IsOpened() )
115 return false;
116
117 file.Write( GetTextReport() );
118
119 // wxFFile dtor will close the file.
120 return true;
121}
122
123
124bool ERC_REPORT::WriteJsonReport( const wxString& aFullFileName )
125{
126 // We need the global LOCALE_IO here in order to
127 // write the report in the c-locale.
128 LOCALE_IO locale;
129
130 std::ofstream jsonFileStream( aFullFileName.fn_str() );
131
132 UNITS_PROVIDER unitsProvider( pcbIUScale, m_reportUnits );
133 std::map<KIID, EDA_ITEM*> itemMap;
134
135 RC_JSON::ERC_REPORT reportHead;
136 wxFileName fn( m_sch->GetFileName() );
137 reportHead.$schema = "https://schemas.kicad.org/erc.v1.json";
138 reportHead.source = fn.GetFullName();
139 reportHead.date = GetISO8601CurrentDateTime();
142
143 SCH_SHEET_LIST sheetList = m_sch->Hierarchy();
144 sheetList.FillItemMap( itemMap );
145
146 ERC_SETTINGS& settings = m_sch->ErcSettings();
147
150
151 std::map<SCH_SHEET_PATH, std::vector<ERC_ITEM*>> orderedItems;
152
153 for( int i = 0; i < errors.GetCount(); ++i )
154 {
155 if( auto item = dynamic_cast<ERC_ITEM*>( errors.GetItem( i ).get() ) )
156 {
157 if( item->MainItemHasSheetPath() )
158 orderedItems[item->GetMainItemSheetPath()].emplace_back( item );
159 else
160 orderedItems[sheetList[0]].emplace_back( item );
161 }
162 }
163
164 for( unsigned i = 0; i < sheetList.size(); i++ )
165 {
166 RC_JSON::ERC_SHEET jsonSheet;
167 jsonSheet.path = sheetList[i].PathHumanReadable();
168 jsonSheet.uuid_path = sheetList[i].Path().AsString();
169
170 for( ERC_ITEM* item : orderedItems[sheetList[i]] )
171 {
172 SEVERITY severity = settings.GetSeverity( item->GetErrorCode() );
173
174 RC_JSON::VIOLATION violation;
175 item->GetJsonViolation( violation, &unitsProvider, severity, itemMap );
176
177 jsonSheet.violations.push_back( violation );
178 }
179
180 reportHead.sheets.push_back( jsonSheet );
181 }
182
183 nlohmann::json saveJson = nlohmann::json( reportHead );
184 jsonFileStream << std::setw( 4 ) << saveJson << std::endl;
185 jsonFileStream.flush();
186 jsonFileStream.close();
187
188 return true;
189}
constexpr EDA_IU_SCALE schIUScale
Definition: base_units.h:114
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:112
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:38
wxString GetTextReport()
Returns the ERC report in "text" (human readable) format in the C-locale.
Definition: erc_report.cpp:45
SCHEMATIC * m_sch
Definition: erc_report.h:57
bool WriteJsonReport(const wxString &aFullFileName)
Writes a JSON formatted ERC Report to the given file path in the c-locale.
Definition: erc_report.cpp:124
bool WriteTextReport(const wxString &aFullFileName)
Writes the text report also available via GetTextReport directly to a given file path.
Definition: erc_report.cpp:110
Container for ERC settings.
Definition: erc_settings.h:132
SEVERITY GetSeverity(int aErrorCode) const
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: locale_io.h:41
Holds all the data relating to one schematic.
Definition: schematic.h:88
SCH_SHEET_LIST BuildSheetListSortedByPageNumbers() const
Definition: schematic.h:108
wxString GetFileName() const
Helper to retrieve the filename from the root sheet screen.
Definition: schematic.cpp:350
SCH_SHEET_LIST Hierarchy() const
Return the full schematic flattened hierarchical sheet list.
Definition: schematic.cpp:258
ERC_SETTINGS & ErcSettings() const
Definition: schematic.cpp:363
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:241
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:48
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:180
SEVERITY
@ RPT_SEVERITY_WARNING
@ RPT_SEVERITY_ERROR
wxString GetISO8601CurrentDateTime()
std::vector< ERC_SHEET > sheets