KiCad PCB EDA Suite
Loading...
Searching...
No Matches
drc_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 "drc_report.h"
21
22#include <fstream>
23
24#include <wx/string.h>
25
26#include <board.h>
28#include <build_version.h>
29#include <drc/drc_item.h>
30#include <locale_io.h>
32#include <macros.h>
33#include <json_common.h>
34#include <rc_json_schema.h>
35#include <string_utils.h>
37
38
39DRC_REPORT::DRC_REPORT( BOARD* aBoard, EDA_UNITS aReportUnits,
40 std::shared_ptr<RC_ITEMS_PROVIDER> aMarkersProvider,
41 std::shared_ptr<RC_ITEMS_PROVIDER> aRatsnestProvider,
42 std::shared_ptr<RC_ITEMS_PROVIDER> aFpWarningsProvider) :
43 m_board( aBoard ),
44 m_reportUnits( aReportUnits ),
45 m_markersProvider( std::move( aMarkersProvider ) ),
46 m_ratsnestProvider( std::move( aRatsnestProvider ) ),
47 m_fpWarningsProvider( std::move( aFpWarningsProvider ) ),
49{
51 m_reportedSeverities = m_markersProvider->GetSeverities();
52}
53
54
55bool DRC_REPORT::WriteTextReport( const wxString& aFullFileName )
56{
57 // We need the global LOCALE_IO here in order to
58 // write the report in the c-locale.
59 LOCALE_IO locale;
60 FILE* fp = wxFopen( aFullFileName, wxT( "w" ) );
61
62 if( fp == nullptr )
63 return false;
64
65 std::map<KIID, EDA_ITEM*> itemMap;
66 m_board->FillItemMap( itemMap );
67
68 UNITS_PROVIDER unitsProvider( pcbIUScale, m_reportUnits );
69 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
70 int count;
71
72 wxFileName fn( m_board->GetFileName() );
73 fprintf( fp, "** Drc report for %s **\n", TO_UTF8( fn.GetFullName() ) );
74
75 fprintf( fp, "** Created on %s **\n", TO_UTF8( GetISO8601CurrentDateTime() ) );
76
77 fprintf( fp, "** Report includes: %s **\n", TO_UTF8( formatSeverities( m_reportedSeverities ) ) );
78
79 count = m_markersProvider->GetCount();
80
81 fprintf( fp, "\n** Found %d DRC violations **\n", count );
82
83 for( int i = 0; i < count; ++i )
84 {
85 const std::shared_ptr<RC_ITEM>& item = m_markersProvider->GetItem( i );
86 SEVERITY severity = item->GetParent()->GetSeverity();
87
88 if( severity == RPT_SEVERITY_EXCLUSION )
89 severity = bds.GetSeverity( item->GetErrorCode() );
90
91 fprintf( fp, "%s", TO_UTF8( item->ShowReport( &unitsProvider, severity, itemMap ) ) );
92 }
93
94 count = m_ratsnestProvider->GetCount();
95
96 fprintf( fp, "\n** Found %d unconnected pads **\n", count );
97
98 for( int i = 0; i < count; ++i )
99 {
100 const std::shared_ptr<RC_ITEM>& item = m_ratsnestProvider->GetItem( i );
101 SEVERITY severity = bds.GetSeverity( item->GetErrorCode() );
102
103 fprintf( fp, "%s", TO_UTF8( item->ShowReport( &unitsProvider, severity, itemMap ) ) );
104 }
105
106 count = m_fpWarningsProvider->GetCount();
107
108 fprintf( fp, "\n** Found %d Footprint errors **\n", count );
109
110 for( int i = 0; i < count; ++i )
111 {
112 const std::shared_ptr<RC_ITEM>& item = m_fpWarningsProvider->GetItem( i );
113 SEVERITY severity = bds.GetSeverity( item->GetErrorCode() );
114
115 fprintf( fp, "%s", TO_UTF8( item->ShowReport( &unitsProvider, severity, itemMap ) ) );
116 }
117
118 fprintf( fp, "\n** Ignored checks **\n" );
119
120 bool hasIgnored = false;
121
122 for( const RC_ITEM& item : DRC_ITEM::GetItemsWithSeverities() )
123 {
124 int code = item.GetErrorCode();
125
126 if( code > 0 && bds.Ignore( code ) )
127 {
128 fprintf( fp, " - %s\n", TO_UTF8( HYPERLINK_DV_RENDERER::StripMarkup( item.GetErrorMessage( false ) ) ) );
129 hasIgnored = true;
130 }
131 }
132
133 if( !hasIgnored )
134 fprintf( fp, " - %s\n", TO_UTF8( _( "None" ) ) );
135
136 fprintf( fp, "\n** End of Report **\n" );
137
138 fclose( fp );
139
140 return true;
141}
142
143
144bool DRC_REPORT::WriteJsonReport( const wxString& aFullFileName )
145{
146 std::ofstream jsonFileStream( aFullFileName.fn_str() );
147
148 UNITS_PROVIDER unitsProvider( pcbIUScale, m_reportUnits );
149 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
150 std::map<KIID, EDA_ITEM*> itemMap;
151 m_board->FillItemMap( itemMap );
152
153 RC_JSON::DRC_REPORT reportHead;
154
155 wxFileName fn( m_board->GetFileName() );
156 reportHead.$schema = "https://schemas.kicad.org/drc.v1.json";
157 reportHead.source = fn.GetFullName();
158 reportHead.date = GetISO8601CurrentDateTime();
161
162 // Document which severities are included in this report
164 reportHead.included_severities.push_back( wxS( "error" ) );
165
167 reportHead.included_severities.push_back( wxS( "warning" ) );
168
170 reportHead.included_severities.push_back( wxS( "exclusion" ) );
171
172 for( int i = 0; i < m_markersProvider->GetCount(); ++i )
173 {
174 const std::shared_ptr<RC_ITEM>& item = m_markersProvider->GetItem( i );
175 SEVERITY severity = item->GetParent()->GetSeverity();
176
177 if( severity == RPT_SEVERITY_EXCLUSION )
178 severity = bds.GetSeverity( item->GetErrorCode() );
179
180 RC_JSON::VIOLATION violation;
181 item->GetJsonViolation( violation, &unitsProvider, severity, itemMap );
182
183 reportHead.violations.push_back( violation );
184 }
185
186 for( int i = 0; i < m_ratsnestProvider->GetCount(); ++i )
187 {
188 const std::shared_ptr<RC_ITEM>& item = m_ratsnestProvider->GetItem( i );
189 SEVERITY severity = bds.GetSeverity( item->GetErrorCode() );
190
191 RC_JSON::VIOLATION violation;
192 item->GetJsonViolation( violation, &unitsProvider, severity, itemMap );
193
194 reportHead.unconnected_items.push_back( violation );
195 }
196
197
198 for( int i = 0; i < m_fpWarningsProvider->GetCount(); ++i )
199 {
200 const std::shared_ptr<RC_ITEM>& item = m_fpWarningsProvider->GetItem( i );
201 SEVERITY severity = bds.GetSeverity( item->GetErrorCode() );
202
203 RC_JSON::VIOLATION violation;
204 item->GetJsonViolation( violation, &unitsProvider, severity, itemMap );
205
206 reportHead.schematic_parity.push_back( violation );
207 }
208
209 for( const RC_ITEM& item : DRC_ITEM::GetItemsWithSeverities() )
210 {
211 int code = item.GetErrorCode();
212
213 if( code > 0 && bds.Ignore( code ) )
214 {
215 RC_JSON::IGNORED_CHECK ignoredCheck;
216 ignoredCheck.key = item.GetSettingsKey();
217 ignoredCheck.description = HYPERLINK_DV_RENDERER::StripMarkup( item.GetErrorMessage( false ) );
218 reportHead.ignored_checks.push_back( ignoredCheck );
219 }
220 }
221
222 nlohmann::json saveJson = nlohmann::json( reportHead );
223 jsonFileStream << std::setw( 4 ) << saveJson << std::endl;
224 jsonFileStream.flush();
225 jsonFileStream.close();
226
227 return true;
228}
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:125
wxString GetMajorMinorPatchVersion()
Get the major, minor and patch version in a string major.minor.patch This is extracted by CMake from ...
Container for design settings for a BOARD object.
bool Ignore(int aDRCErrorCode)
Return true if the DRC error code's severity is SEVERITY_IGNORE.
SEVERITY GetSeverity(int aDRCErrorCode)
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:323
static std::vector< std::reference_wrapper< RC_ITEM > > GetItemsWithSeverities()
Definition drc_item.h:145
std::shared_ptr< RC_ITEMS_PROVIDER > m_ratsnestProvider
Definition drc_report.h:46
bool WriteJsonReport(const wxString &aFullFileName)
BOARD * m_board
Definition drc_report.h:43
int m_reportedSeverities
Definition drc_report.h:48
std::shared_ptr< RC_ITEMS_PROVIDER > m_markersProvider
Definition drc_report.h:45
bool WriteTextReport(const wxString &aFullFileName)
EDA_UNITS m_reportUnits
Definition drc_report.h:44
DRC_REPORT(BOARD *aBoard, EDA_UNITS aReportUnits, std::shared_ptr< RC_ITEMS_PROVIDER > aMarkersProvider, std::shared_ptr< RC_ITEMS_PROVIDER > aRatsnestProvider, std::shared_ptr< RC_ITEMS_PROVIDER > aFpWarningsProvider)
std::shared_ptr< RC_ITEMS_PROVIDER > m_fpWarningsProvider
Definition drc_report.h:47
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition locale_io.h:41
A holder for a rule check item, DRC in Pcbnew or ERC in Eeschema.
Definition rc_item.h:84
#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.
STL namespace.
wxString formatSeverities(int aSeverities)
Convert a severity mask to a human-readable comma-separated string.
SEVERITY
@ RPT_SEVERITY_WARNING
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_EXCLUSION
wxString GetISO8601CurrentDateTime()
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
std::vector< wxString > included_severities
std::vector< VIOLATION > unconnected_items
std::vector< IGNORED_CHECK > ignored_checks
std::vector< VIOLATION > violations
std::vector< VIOLATION > schematic_parity