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 <wx/string.h>
21
22#include <board.h>
24#include <build_version.h>
25#include "drc_report.h"
26#include <drc/drc_item.h>
27#include <locale_io.h>
28#include <macros.h>
29#include <json_common.h>
30#include <rc_json_schema.h>
31
32#include <fstream>
33
34DRC_REPORT::DRC_REPORT( BOARD* aBoard, EDA_UNITS aReportUnits,
35 std::shared_ptr<RC_ITEMS_PROVIDER> aMarkersProvider,
36 std::shared_ptr<RC_ITEMS_PROVIDER> aRatsnestProvider,
37 std::shared_ptr<RC_ITEMS_PROVIDER> aFpWarningsProvider) :
38 m_board( aBoard ),
39 m_reportUnits( aReportUnits ),
40 m_markersProvider( std::move( aMarkersProvider ) ),
41 m_ratsnestProvider( std::move( aRatsnestProvider ) ),
42 m_fpWarningsProvider( std::move( aFpWarningsProvider ) )
43{
44
45}
46
47
48bool DRC_REPORT::WriteTextReport( const wxString& aFullFileName )
49{
50 // We need the global LOCALE_IO here in order to
51 // write the report in the c-locale.
52 LOCALE_IO locale;
53 FILE* fp = wxFopen( aFullFileName, wxT( "w" ) );
54
55 if( fp == nullptr )
56 return false;
57
58 std::map<KIID, EDA_ITEM*> itemMap;
59 m_board->FillItemMap( itemMap );
60
61 UNITS_PROVIDER unitsProvider( pcbIUScale, m_reportUnits );
63 int count;
64
65 wxFileName fn( m_board->GetFileName() );
66 fprintf( fp, "** Drc report for %s **\n", TO_UTF8( fn.GetFullName() ) );
67
68 fprintf( fp, "** Created on %s **\n", TO_UTF8( GetISO8601CurrentDateTime() ) );
69
70 count = m_markersProvider->GetCount();
71
72 fprintf( fp, "\n** Found %d DRC violations **\n", count );
73
74 for( int i = 0; i < count; ++i )
75 {
76 const std::shared_ptr<RC_ITEM>& item = m_markersProvider->GetItem( i );
77 SEVERITY severity = item->GetParent()->GetSeverity();
78
79 if( severity == RPT_SEVERITY_EXCLUSION )
80 severity = bds.GetSeverity( item->GetErrorCode() );
81
82 fprintf( fp, "%s", TO_UTF8( item->ShowReport( &unitsProvider, severity, itemMap ) ) );
83 }
84
85 count = m_ratsnestProvider->GetCount();
86
87 fprintf( fp, "\n** Found %d unconnected pads **\n", count );
88
89 for( int i = 0; i < count; ++i )
90 {
91 const std::shared_ptr<RC_ITEM>& item = m_ratsnestProvider->GetItem( i );
92 SEVERITY severity = bds.GetSeverity( item->GetErrorCode() );
93
94 fprintf( fp, "%s", TO_UTF8( item->ShowReport( &unitsProvider, severity, itemMap ) ) );
95 }
96
97 count = m_fpWarningsProvider->GetCount();
98
99 fprintf( fp, "\n** Found %d Footprint errors **\n", count );
100
101 for( int i = 0; i < count; ++i )
102 {
103 const std::shared_ptr<RC_ITEM>& item = m_fpWarningsProvider->GetItem( i );
104 SEVERITY severity = bds.GetSeverity( item->GetErrorCode() );
105
106 fprintf( fp, "%s", TO_UTF8( item->ShowReport( &unitsProvider, severity, itemMap ) ) );
107 }
108
109
110 fprintf( fp, "\n** End of Report **\n" );
111
112 fclose( fp );
113
114 return true;
115}
116
117
118bool DRC_REPORT::WriteJsonReport( const wxString& aFullFileName )
119{
120 // We need the global LOCALE_IO here in order to
121 // write the report in the c-locale.
122 LOCALE_IO locale;
123 std::ofstream jsonFileStream( aFullFileName.fn_str() );
124
125 UNITS_PROVIDER unitsProvider( pcbIUScale, m_reportUnits );
127 std::map<KIID, EDA_ITEM*> itemMap;
128 m_board->FillItemMap( itemMap );
129
130 RC_JSON::DRC_REPORT reportHead;
131
132 wxFileName fn( m_board->GetFileName() );
133 reportHead.$schema = "https://schemas.kicad.org/drc.v1.json";
134 reportHead.source = fn.GetFullName();
135 reportHead.date = GetISO8601CurrentDateTime();
138
139 for( int i = 0; i < m_markersProvider->GetCount(); ++i )
140 {
141 const std::shared_ptr<RC_ITEM>& item = m_markersProvider->GetItem( i );
142 SEVERITY severity = item->GetParent()->GetSeverity();
143
144 if( severity == RPT_SEVERITY_EXCLUSION )
145 severity = bds.GetSeverity( item->GetErrorCode() );
146
147 RC_JSON::VIOLATION violation;
148 item->GetJsonViolation( violation, &unitsProvider, severity, itemMap );
149
150 reportHead.violations.push_back( violation );
151 }
152
153 for( int i = 0; i < m_ratsnestProvider->GetCount(); ++i )
154 {
155 const std::shared_ptr<RC_ITEM>& item = m_ratsnestProvider->GetItem( i );
156 SEVERITY severity = bds.GetSeverity( item->GetErrorCode() );
157
158 RC_JSON::VIOLATION violation;
159 item->GetJsonViolation( violation, &unitsProvider, severity, itemMap );
160
161 reportHead.unconnected_items.push_back( violation );
162 }
163
164
165 for( int i = 0; i < m_fpWarningsProvider->GetCount(); ++i )
166 {
167 const std::shared_ptr<RC_ITEM>& item = m_fpWarningsProvider->GetItem( i );
168 SEVERITY severity = bds.GetSeverity( item->GetErrorCode() );
169
170 RC_JSON::VIOLATION violation;
171 item->GetJsonViolation( violation, &unitsProvider, severity, itemMap );
172
173 reportHead.schematic_parity.push_back( violation );
174 }
175
176
177 nlohmann::json saveJson = nlohmann::json( reportHead );
178 jsonFileStream << std::setw( 4 ) << saveJson << std::endl;
179 jsonFileStream.flush();
180 jsonFileStream.close();
181
182 return true;
183}
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.
SEVERITY GetSeverity(int aDRCErrorCode)
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:317
const wxString & GetFileName() const
Definition: board.h:354
void FillItemMap(std::map< KIID, EDA_ITEM * > &aMap)
Definition: board.cpp:1693
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.cpp:1024
std::shared_ptr< RC_ITEMS_PROVIDER > m_ratsnestProvider
Definition: drc_report.h:46
bool WriteJsonReport(const wxString &aFullFileName)
Definition: drc_report.cpp:118
BOARD * m_board
Definition: drc_report.h:43
std::shared_ptr< RC_ITEMS_PROVIDER > m_markersProvider
Definition: drc_report.h:45
bool WriteTextReport(const wxString &aFullFileName)
Definition: drc_report.cpp:48
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)
Definition: drc_report.cpp:34
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
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.
Definition: eda_units.cpp:180
STL namespace.
SEVERITY
@ RPT_SEVERITY_EXCLUSION
wxString GetISO8601CurrentDateTime()
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: string_utils.h:429
std::vector< VIOLATION > unconnected_items
std::vector< VIOLATION > violations
std::vector< VIOLATION > schematic_parity