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 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.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;
56 SCH_SHEET_LIST sheetList = m_sch->GetSheets();
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 int err_count = 0;
125 int warn_count = 0;
126 int total_count = 0;
127 SCH_SHEET_LIST sheetList = m_sch->GetSheets();
128 sheetList.FillItemMap( itemMap );
129
130 ERC_SETTINGS& settings = m_sch->ErcSettings();
131
132 for( unsigned i = 0; i < sheetList.size(); i++ )
133 {
134 RC_JSON::ERC_SHEET jsonSheet;
135 jsonSheet.path = sheetList[i].PathHumanReadable();
136 jsonSheet.uuid_path = sheetList[i].Path().AsString();
137
138 for( SCH_ITEM* aItem : sheetList[i].LastScreen()->Items().OfType( SCH_MARKER_T ) )
139 {
140 const SCH_MARKER* marker = static_cast<const SCH_MARKER*>( aItem );
141 RC_ITEM* item = marker->GetRCItem().get();
142 SEVERITY severity = settings.GetSeverity( item->GetErrorCode() );
143
144 if( marker->GetMarkerType() != MARKER_BASE::MARKER_ERC )
145 continue;
146
147 total_count++;
148
149 switch( severity )
150 {
151 case RPT_SEVERITY_ERROR: err_count++; break;
152 case RPT_SEVERITY_WARNING: warn_count++; break;
153 default: break;
154 }
155
156 RC_JSON::VIOLATION violation;
157 marker->GetRCItem()->GetJsonViolation( violation, &unitsProvider, severity, itemMap );
158
159 jsonSheet.violations.push_back( violation );
160 }
161
162 reportHead.sheets.push_back( jsonSheet );
163 }
164
165
166 nlohmann::json saveJson = nlohmann::json( reportHead );
167 jsonFileStream << std::setw( 4 ) << saveJson << std::endl;
168 jsonFileStream.flush();
169 jsonFileStream.close();
170
171 return true;
172}
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:120
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:75
wxString GetFileName() const override
Helper to retrieve the filename from the root sheet screen.
Definition: schematic.cpp:281
SCH_SHEET_LIST GetSheets() const override
Builds and returns an updated schematic hierarchy TODO: can this be cached?
Definition: schematic.h:100
ERC_SETTINGS & ErcSettings() const
Definition: schematic.cpp:294
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:174
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