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
44bool ERC_REPORT::WriteTextReport( const wxString& aFullFileName )
45{
46 wxFFile file( aFullFileName, wxT( "wt" ) );
47
48 if( !file.IsOpened() )
49 return false;
50
51 UNITS_PROVIDER unitsProvider( schIUScale, m_reportUnits );
52
53 wxString msg = wxString::Format( _( "ERC report (%s, Encoding UTF8)\n" ),
55
56 std::map<KIID, EDA_ITEM*> itemMap;
57
58 int err_count = 0;
59 int warn_count = 0;
60 int total_count = 0;
61 SCH_SHEET_LIST sheetList = m_sch->GetSheets();
62
63 sheetList.FillItemMap( itemMap );
64
65 ERC_SETTINGS& settings = m_sch->ErcSettings();
66
67 for( unsigned i = 0; i < sheetList.size(); i++ )
68 {
69 msg << wxString::Format( _( "\n***** Sheet %s\n" ), sheetList[i].PathHumanReadable() );
70
71 for( SCH_ITEM* aItem : sheetList[i].LastScreen()->Items().OfType( SCH_MARKER_T ) )
72 {
73 const SCH_MARKER* marker = static_cast<const SCH_MARKER*>( aItem );
74 RC_ITEM* item = marker->GetRCItem().get();
75 SEVERITY severity = settings.GetSeverity( item->GetErrorCode() );
76
77 if( marker->GetMarkerType() != MARKER_BASE::MARKER_ERC )
78 continue;
79
80 total_count++;
81
82 switch( severity )
83 {
84 case RPT_SEVERITY_ERROR: err_count++; break;
85 case RPT_SEVERITY_WARNING: warn_count++; break;
86 default: break;
87 }
88
89 msg << marker->GetRCItem()->ShowReport( &unitsProvider, severity, itemMap );
90 }
91 }
92
93 msg << wxString::Format( _( "\n ** ERC messages: %d Errors %d Warnings %d\n" ), total_count,
94 err_count, warn_count );
95
96 // Currently: write report using UTF8 (as usual in Kicad).
97 // TODO: see if we can use the current encoding page (mainly for Windows users),
98 // Or other format (HTML?)
99 file.Write( msg );
100
101 // wxFFile dtor will close the file.
102
103 return true;
104}
105
106
107bool ERC_REPORT::WriteJsonReport( const wxString& aFullFileName )
108{
109 std::ofstream jsonFileStream( aFullFileName.fn_str() );
110
111 UNITS_PROVIDER unitsProvider( pcbIUScale, m_reportUnits );
112 std::map<KIID, EDA_ITEM*> itemMap;
113
114 RC_JSON::ERC_REPORT reportHead;
115 reportHead.source = m_sch->GetFileName();
116 reportHead.date = GetISO8601CurrentDateTime();
119
120 int err_count = 0;
121 int warn_count = 0;
122 int total_count = 0;
123 SCH_SHEET_LIST sheetList = m_sch->GetSheets();
124 sheetList.FillItemMap( itemMap );
125
126 ERC_SETTINGS& settings = m_sch->ErcSettings();
127
128 for( unsigned i = 0; i < sheetList.size(); i++ )
129 {
130 RC_JSON::ERC_SHEET jsonSheet;
131 jsonSheet.path = sheetList[i].PathHumanReadable();
132 jsonSheet.uuid_path = sheetList[i].Path().AsString();
133
134 for( SCH_ITEM* aItem : sheetList[i].LastScreen()->Items().OfType( SCH_MARKER_T ) )
135 {
136 const SCH_MARKER* marker = static_cast<const SCH_MARKER*>( aItem );
137 RC_ITEM* item = marker->GetRCItem().get();
138 SEVERITY severity = settings.GetSeverity( item->GetErrorCode() );
139
140 if( marker->GetMarkerType() != MARKER_BASE::MARKER_ERC )
141 continue;
142
143 total_count++;
144
145 switch( severity )
146 {
147 case RPT_SEVERITY_ERROR: err_count++; break;
148 case RPT_SEVERITY_WARNING: warn_count++; break;
149 default: break;
150 }
151
152 RC_JSON::VIOLATION violation;
153 marker->GetRCItem()->GetJsonViolation( violation, &unitsProvider, severity, itemMap );
154
155 jsonSheet.violations.push_back( violation );
156 }
157
158 reportHead.sheets.push_back( jsonSheet );
159 }
160
161
162 nlohmann::json saveJson = nlohmann::json( reportHead );
163 jsonFileStream << std::setw( 4 ) << saveJson << std::endl;
164 jsonFileStream.flush();
165 jsonFileStream.close();
166
167 return true;
168}
constexpr EDA_IU_SCALE schIUScale
Definition: base_units.h:111
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:109
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:40
ERC_REPORT(SCHEMATIC *aSchematic, EDA_UNITS aReportUnits)
Definition: erc_report.cpp:37
SCHEMATIC * m_sch
Definition: erc_report.h:39
bool WriteJsonReport(const wxString &aFullFileName)
Definition: erc_report.cpp:107
bool WriteTextReport(const wxString &aFullFileName)
Definition: erc_report.cpp:44
Container for ERC settings.
Definition: erc_settings.h:115
SEVERITY GetSeverity(int aErrorCode) const
std::shared_ptr< RC_ITEM > GetRCItem() const
Definition: marker_base.h:105
enum TYPEMARKER GetMarkerType() const
Definition: marker_base.h:95
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:151
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:139
SEVERITY
@ RPT_SEVERITY_WARNING
@ RPT_SEVERITY_ERROR
wxString GetISO8601CurrentDateTime()
std::vector< ERC_SHEET > sheets
@ SCH_MARKER_T
Definition: typeinfo.h:141