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>
35
36
37DRC_REPORT::DRC_REPORT( BOARD* aBoard, EDA_UNITS aReportUnits,
38 std::shared_ptr<RC_ITEMS_PROVIDER> aMarkersProvider,
39 std::shared_ptr<RC_ITEMS_PROVIDER> aRatsnestProvider,
40 std::shared_ptr<RC_ITEMS_PROVIDER> aFpWarningsProvider) :
41 m_board( aBoard ),
42 m_reportUnits( aReportUnits ),
43 m_markersProvider( std::move( aMarkersProvider ) ),
44 m_ratsnestProvider( std::move( aRatsnestProvider ) ),
45 m_fpWarningsProvider( std::move( aFpWarningsProvider ) )
46{
47
48}
49
50
51bool DRC_REPORT::WriteTextReport( const wxString& aFullFileName )
52{
53 // We need the global LOCALE_IO here in order to
54 // write the report in the c-locale.
55 LOCALE_IO locale;
56 FILE* fp = wxFopen( aFullFileName, wxT( "w" ) );
57
58 if( fp == nullptr )
59 return false;
60
61 std::map<KIID, EDA_ITEM*> itemMap;
62 m_board->FillItemMap( itemMap );
63
64 UNITS_PROVIDER unitsProvider( pcbIUScale, m_reportUnits );
65 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
66 int count;
67
68 wxFileName fn( m_board->GetFileName() );
69 fprintf( fp, "** Drc report for %s **\n", TO_UTF8( fn.GetFullName() ) );
70
71 fprintf( fp, "** Created on %s **\n", TO_UTF8( GetISO8601CurrentDateTime() ) );
72
73 count = m_markersProvider->GetCount();
74
75 fprintf( fp, "\n** Found %d DRC violations **\n", count );
76
77 for( int i = 0; i < count; ++i )
78 {
79 const std::shared_ptr<RC_ITEM>& item = m_markersProvider->GetItem( i );
80 SEVERITY severity = item->GetParent()->GetSeverity();
81
82 if( severity == RPT_SEVERITY_EXCLUSION )
83 severity = bds.GetSeverity( item->GetErrorCode() );
84
85 fprintf( fp, "%s", TO_UTF8( item->ShowReport( &unitsProvider, severity, itemMap ) ) );
86 }
87
88 count = m_ratsnestProvider->GetCount();
89
90 fprintf( fp, "\n** Found %d unconnected pads **\n", count );
91
92 for( int i = 0; i < count; ++i )
93 {
94 const std::shared_ptr<RC_ITEM>& item = m_ratsnestProvider->GetItem( i );
95 SEVERITY severity = bds.GetSeverity( item->GetErrorCode() );
96
97 fprintf( fp, "%s", TO_UTF8( item->ShowReport( &unitsProvider, severity, itemMap ) ) );
98 }
99
100 count = m_fpWarningsProvider->GetCount();
101
102 fprintf( fp, "\n** Found %d Footprint errors **\n", count );
103
104 for( int i = 0; i < count; ++i )
105 {
106 const std::shared_ptr<RC_ITEM>& item = m_fpWarningsProvider->GetItem( i );
107 SEVERITY severity = bds.GetSeverity( item->GetErrorCode() );
108
109 fprintf( fp, "%s", TO_UTF8( item->ShowReport( &unitsProvider, severity, itemMap ) ) );
110 }
111
112
113 fprintf( fp, "\n** End of Report **\n" );
114
115 fclose( fp );
116
117 return true;
118}
119
120
121bool DRC_REPORT::WriteJsonReport( const wxString& aFullFileName )
122{
123 std::ofstream jsonFileStream( aFullFileName.fn_str() );
124
125 UNITS_PROVIDER unitsProvider( pcbIUScale, m_reportUnits );
126 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
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
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
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
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.
SEVERITY
@ 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< VIOLATION > unconnected_items
std::vector< VIOLATION > violations
std::vector< VIOLATION > schematic_parity