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
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#include "drc_report.h"
21#include "eda_item.h"
22
23#include <fstream>
24
25#include <wx/string.h>
26
27#include <board.h>
29#include <build_version.h>
30#include <drc/drc_item.h>
31#include <locale_io.h>
33#include <macros.h>
34#include <json_common.h>
35#include <pcb_marker.h>
36#include <rc_json_schema.h>
37#include <string_utils.h>
39
40
41DRC_REPORT::DRC_REPORT( BOARD* aBoard, EDA_UNITS aReportUnits,
42 std::shared_ptr<RC_ITEMS_PROVIDER> aMarkersProvider,
43 std::shared_ptr<RC_ITEMS_PROVIDER> aRatsnestProvider,
44 std::shared_ptr<RC_ITEMS_PROVIDER> aFpWarningsProvider) :
45 m_board( aBoard ),
46 m_reportUnits( aReportUnits ),
47 m_markersProvider( std::move( aMarkersProvider ) ),
48 m_ratsnestProvider( std::move( aRatsnestProvider ) ),
49 m_fpWarningsProvider( std::move( aFpWarningsProvider ) ),
51{
53 m_reportedSeverities = m_markersProvider->GetSeverities();
54}
55
56
57bool DRC_REPORT::WriteTextReport( const wxString& aFullFileName )
58{
59 // We need the global LOCALE_IO here in order to
60 // write the report in the c-locale.
61 LOCALE_IO locale;
62 FILE* fp = wxFopen( aFullFileName, wxT( "w" ) );
63
64 if( fp == nullptr )
65 return false;
66
67 UNITS_PROVIDER unitsProvider( pcbIUScale, m_reportUnits );
68 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
69 BOARD_RC_ITEM_RESOLVER itemResolver( *m_board );
70 int count = 0;
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, itemResolver ) ) );
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, itemResolver ) ) );
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, itemResolver ) ) );
116 }
117
118 fprintf( fp, "\n** Ignored checks **\n" );
119
120 bool hasIgnored = false;
121
122 for( const std::reference_wrapper<RC_ITEM>& item : DRC_ITEM::GetItemsWithSeverities() )
123 {
124 int code = item.get().GetErrorCode();
125
126 if( code > 0 && bds.Ignore( code ) )
127 {
128 fprintf( fp, " - %s\n", TO_UTF8( HYPERLINK_DV_RENDERER::StripMarkup( item.get().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 BOARD_RC_ITEM_RESOLVER itemResolver( *m_board );
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 auto setMarkerLocation = [&]( const std::shared_ptr<RC_ITEM>& aItem, RC_JSON::VIOLATION& aViolation )
172 {
173 const PCB_MARKER* marker = dynamic_cast<const PCB_MARKER*>( aItem->GetParent() );
174
175 if( !marker )
176 return;
177
178 if( IsValidLayer( marker->GetLayer() ) )
179 aViolation.layer = m_board->GetLayerName( marker->GetLayer() );
180 else if( marker->GetPosition() == VECTOR2I() )
181 return;
182
184 pos.x = EDA_UNIT_UTILS::UI::ToUserUnit( unitsProvider.GetIuScale(), unitsProvider.GetUserUnits(),
185 marker->GetPosition().x );
186 pos.y = EDA_UNIT_UTILS::UI::ToUserUnit( unitsProvider.GetIuScale(), unitsProvider.GetUserUnits(),
187 marker->GetPosition().y );
188 aViolation.pos = pos;
189 };
190
191 for( int i = 0; i < m_markersProvider->GetCount(); ++i )
192 {
193 const std::shared_ptr<RC_ITEM>& item = m_markersProvider->GetItem( i );
194 SEVERITY severity = item->GetParent()->GetSeverity();
195
196 if( severity == RPT_SEVERITY_EXCLUSION )
197 severity = bds.GetSeverity( item->GetErrorCode() );
198
199 RC_JSON::VIOLATION violation;
200 item->GetJsonViolation( violation, &unitsProvider, severity, itemResolver );
201 setMarkerLocation( item, violation );
202
203 reportHead.violations.push_back( violation );
204 }
205
206 for( int i = 0; i < m_ratsnestProvider->GetCount(); ++i )
207 {
208 const std::shared_ptr<RC_ITEM>& item = m_ratsnestProvider->GetItem( i );
209 SEVERITY severity = bds.GetSeverity( item->GetErrorCode() );
210
211 RC_JSON::VIOLATION violation;
212 item->GetJsonViolation( violation, &unitsProvider, severity, itemResolver );
213 setMarkerLocation( item, violation );
214
215 reportHead.unconnected_items.push_back( violation );
216 }
217
218
219 for( int i = 0; i < m_fpWarningsProvider->GetCount(); ++i )
220 {
221 const std::shared_ptr<RC_ITEM>& item = m_fpWarningsProvider->GetItem( i );
222 SEVERITY severity = bds.GetSeverity( item->GetErrorCode() );
223
224 RC_JSON::VIOLATION violation;
225 item->GetJsonViolation( violation, &unitsProvider, severity, itemResolver );
226 setMarkerLocation( item, violation );
227
228 reportHead.schematic_parity.push_back( violation );
229 }
230
231 for( const std::reference_wrapper<RC_ITEM>& item : DRC_ITEM::GetItemsWithSeverities() )
232 {
233 int code = item.get().GetErrorCode();
234
235 if( code > 0 && bds.Ignore( code ) )
236 {
237 RC_JSON::IGNORED_CHECK ignoredCheck;
238 ignoredCheck.key = item.get().GetSettingsKey();
239 ignoredCheck.description = HYPERLINK_DV_RENDERER::StripMarkup( item.get().GetErrorMessage( false ) );
240 reportHead.ignored_checks.push_back( ignoredCheck );
241 }
242 }
243
244 nlohmann::json saveJson = nlohmann::json( reportHead );
245 jsonFileStream << std::setw( 4 ) << saveJson << std::endl;
246 jsonFileStream.flush();
247 jsonFileStream.close();
248
249 return true;
250}
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:128
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) const
Return true if the DRC error code's severity is RPT_SEVERITY_ERROR.
SEVERITY GetSeverity(int aDRCErrorCode) const
Return the severity of a given DRC error code.
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Implements the RC_ITEM_RESOLVER interface to resolve PCB items by their KIID.
Definition drc_item.h:351
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:411
static std::vector< std::reference_wrapper< RC_ITEM > > GetItemsWithSeverities()
Definition drc_item.h:173
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:37
VECTOR2I GetPosition() const override
Definition pcb_marker.h:70
const EDA_IU_SCALE & GetIuScale() const
EDA_UNITS GetUserUnits() const
#define _(s)
EDA_UNITS
Definition eda_units.h:45
bool IsValidLayer(int aLayerId)
Test whether a given integer is a valid layer index, i.e.
Definition layer_ids.h:722
This file contains miscellaneous commonly used macros and functions.
KICOMMON_API double ToUserUnit(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnit, double aValue)
Convert aValue in internal units to the appropriate user units defined by aUnit.
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
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708