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>
31#include <macros.h>
32#include <json_common.h>
33#include <rc_json_schema.h>
34#include <string_utils.h>
36
37
38DRC_REPORT::DRC_REPORT( BOARD* aBoard, EDA_UNITS aReportUnits,
39 std::shared_ptr<RC_ITEMS_PROVIDER> aMarkersProvider,
40 std::shared_ptr<RC_ITEMS_PROVIDER> aRatsnestProvider,
41 std::shared_ptr<RC_ITEMS_PROVIDER> aFpWarningsProvider) :
42 m_board( aBoard ),
43 m_reportUnits( aReportUnits ),
44 m_markersProvider( std::move( aMarkersProvider ) ),
45 m_ratsnestProvider( std::move( aRatsnestProvider ) ),
46 m_fpWarningsProvider( std::move( aFpWarningsProvider ) ),
48{
50 m_reportedSeverities = m_markersProvider->GetSeverities();
51}
52
53
54bool DRC_REPORT::WriteTextReport( const wxString& aFullFileName )
55{
56 // We need the global LOCALE_IO here in order to
57 // write the report in the c-locale.
58 LOCALE_IO locale;
59 FILE* fp = wxFopen( aFullFileName, wxT( "w" ) );
60
61 if( fp == nullptr )
62 return false;
63
64 std::map<KIID, EDA_ITEM*> itemMap;
65 m_board->FillItemMap( itemMap );
66
67 UNITS_PROVIDER unitsProvider( pcbIUScale, m_reportUnits );
68 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
69 int count;
70
71 wxFileName fn( m_board->GetFileName() );
72 fprintf( fp, "** Drc report for %s **\n", TO_UTF8( fn.GetFullName() ) );
73
74 fprintf( fp, "** Created on %s **\n", TO_UTF8( GetISO8601CurrentDateTime() ) );
75
76 fprintf( fp, "** Report includes: %s **\n", TO_UTF8( formatSeverities( m_reportedSeverities ) ) );
77
78 count = m_markersProvider->GetCount();
79
80 fprintf( fp, "\n** Found %d DRC violations **\n", count );
81
82 for( int i = 0; i < count; ++i )
83 {
84 const std::shared_ptr<RC_ITEM>& item = m_markersProvider->GetItem( i );
85 SEVERITY severity = item->GetParent()->GetSeverity();
86
87 if( severity == RPT_SEVERITY_EXCLUSION )
88 severity = bds.GetSeverity( item->GetErrorCode() );
89
90 fprintf( fp, "%s", TO_UTF8( item->ShowReport( &unitsProvider, severity, itemMap ) ) );
91 }
92
93 count = m_ratsnestProvider->GetCount();
94
95 fprintf( fp, "\n** Found %d unconnected pads **\n", count );
96
97 for( int i = 0; i < count; ++i )
98 {
99 const std::shared_ptr<RC_ITEM>& item = m_ratsnestProvider->GetItem( i );
100 SEVERITY severity = bds.GetSeverity( item->GetErrorCode() );
101
102 fprintf( fp, "%s", TO_UTF8( item->ShowReport( &unitsProvider, severity, itemMap ) ) );
103 }
104
105 count = m_fpWarningsProvider->GetCount();
106
107 fprintf( fp, "\n** Found %d Footprint errors **\n", count );
108
109 for( int i = 0; i < count; ++i )
110 {
111 const std::shared_ptr<RC_ITEM>& item = m_fpWarningsProvider->GetItem( i );
112 SEVERITY severity = bds.GetSeverity( item->GetErrorCode() );
113
114 fprintf( fp, "%s", TO_UTF8( item->ShowReport( &unitsProvider, severity, itemMap ) ) );
115 }
116
117 fprintf( fp, "\n** Ignored checks **\n" );
118
119 bool hasIgnored = false;
120
121 for( const RC_ITEM& item : DRC_ITEM::GetItemsWithSeverities() )
122 {
123 int code = item.GetErrorCode();
124
125 if( code > 0 && bds.Ignore( code ) )
126 {
127 fprintf( fp, " - %s\n", TO_UTF8( item.GetErrorMessage( false ) ) );
128 hasIgnored = true;
129 }
130 }
131
132 if( !hasIgnored )
133 fprintf( fp, " - %s\n", TO_UTF8( _( "None" ) ) );
134
135 fprintf( fp, "\n** End of Report **\n" );
136
137 fclose( fp );
138
139 return true;
140}
141
142
143bool DRC_REPORT::WriteJsonReport( const wxString& aFullFileName )
144{
145 std::ofstream jsonFileStream( aFullFileName.fn_str() );
146
147 UNITS_PROVIDER unitsProvider( pcbIUScale, m_reportUnits );
148 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
149 std::map<KIID, EDA_ITEM*> itemMap;
150 m_board->FillItemMap( itemMap );
151
152 RC_JSON::DRC_REPORT reportHead;
153
154 wxFileName fn( m_board->GetFileName() );
155 reportHead.$schema = "https://schemas.kicad.org/drc.v1.json";
156 reportHead.source = fn.GetFullName();
157 reportHead.date = GetISO8601CurrentDateTime();
160
161 // Document which severities are included in this report
163 reportHead.included_severities.push_back( wxS( "error" ) );
164
166 reportHead.included_severities.push_back( wxS( "warning" ) );
167
169 reportHead.included_severities.push_back( wxS( "exclusion" ) );
170
171 for( int i = 0; i < m_markersProvider->GetCount(); ++i )
172 {
173 const std::shared_ptr<RC_ITEM>& item = m_markersProvider->GetItem( i );
174 SEVERITY severity = item->GetParent()->GetSeverity();
175
176 if( severity == RPT_SEVERITY_EXCLUSION )
177 severity = bds.GetSeverity( item->GetErrorCode() );
178
179 RC_JSON::VIOLATION violation;
180 item->GetJsonViolation( violation, &unitsProvider, severity, itemMap );
181
182 reportHead.violations.push_back( violation );
183 }
184
185 for( int i = 0; i < m_ratsnestProvider->GetCount(); ++i )
186 {
187 const std::shared_ptr<RC_ITEM>& item = m_ratsnestProvider->GetItem( i );
188 SEVERITY severity = bds.GetSeverity( item->GetErrorCode() );
189
190 RC_JSON::VIOLATION violation;
191 item->GetJsonViolation( violation, &unitsProvider, severity, itemMap );
192
193 reportHead.unconnected_items.push_back( violation );
194 }
195
196
197 for( int i = 0; i < m_fpWarningsProvider->GetCount(); ++i )
198 {
199 const std::shared_ptr<RC_ITEM>& item = m_fpWarningsProvider->GetItem( i );
200 SEVERITY severity = bds.GetSeverity( item->GetErrorCode() );
201
202 RC_JSON::VIOLATION violation;
203 item->GetJsonViolation( violation, &unitsProvider, severity, itemMap );
204
205 reportHead.schematic_parity.push_back( violation );
206 }
207
208 for( const RC_ITEM& item : DRC_ITEM::GetItemsWithSeverities() )
209 {
210 int code = item.GetErrorCode();
211
212 if( code > 0 && bds.Ignore( code ) )
213 {
214 RC_JSON::IGNORED_CHECK ignoredCheck;
215 ignoredCheck.key = item.GetSettingsKey();
216 ignoredCheck.description = item.GetErrorMessage( false );
217 reportHead.ignored_checks.push_back( ignoredCheck );
218 }
219 }
220
221 nlohmann::json saveJson = nlohmann::json( reportHead );
222 jsonFileStream << std::setw( 4 ) << saveJson << std::endl;
223 jsonFileStream.flush();
224 jsonFileStream.close();
225
226 return true;
227}
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 ...
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:322
static std::vector< std::reference_wrapper< RC_ITEM > > GetItemsWithSeverities()
Definition drc_item.h:141
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:81
#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