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 (C) 2023 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 <fstream>
28#include <macros.h>
29#include <nlohmann/json.hpp>
30#include <rc_json_schema.h>
31
32
33DRC_REPORT::DRC_REPORT( BOARD* aBoard, EDA_UNITS aReportUnits,
34 std::shared_ptr<RC_ITEMS_PROVIDER> aMarkersProvider,
35 std::shared_ptr<RC_ITEMS_PROVIDER> aRatsnestProvider,
36 std::shared_ptr<RC_ITEMS_PROVIDER> aFpWarningsProvider) :
37 m_board( aBoard ),
38 m_reportUnits( aReportUnits ),
39 m_markersProvider( std::move( aMarkersProvider ) ),
40 m_ratsnestProvider( std::move( aRatsnestProvider ) ),
41 m_fpWarningsProvider( std::move( aFpWarningsProvider ) )
42{
43
44}
45
46
47bool DRC_REPORT::WriteTextReport( const wxString& aFullFileName )
48{
49 FILE* fp = wxFopen( aFullFileName, wxT( "w" ) );
50
51 if( fp == nullptr )
52 return false;
53
54 std::map<KIID, EDA_ITEM*> itemMap;
55 m_board->FillItemMap( itemMap );
56
57 UNITS_PROVIDER unitsProvider( pcbIUScale, m_reportUnits );
59 int count;
60
61 wxFileName fn( m_board->GetFileName() );
62 fprintf( fp, "** Drc report for %s **\n", TO_UTF8( fn.GetFullName() ) );
63
64 fprintf( fp, "** Created on %s **\n", TO_UTF8( GetISO8601CurrentDateTime() ) );
65
66 count = m_markersProvider->GetCount();
67
68 fprintf( fp, "\n** Found %d DRC violations **\n", count );
69
70 for( int i = 0; i < count; ++i )
71 {
72 const std::shared_ptr<RC_ITEM>& item = m_markersProvider->GetItem( i );
73 SEVERITY severity = item->GetParent()->GetSeverity();
74
75 if( severity == RPT_SEVERITY_EXCLUSION )
76 severity = bds.GetSeverity( item->GetErrorCode() );
77
78 fprintf( fp, "%s", TO_UTF8( item->ShowReport( &unitsProvider, severity, itemMap ) ) );
79 }
80
81 count = m_ratsnestProvider->GetCount();
82
83 fprintf( fp, "\n** Found %d unconnected pads **\n", count );
84
85 for( int i = 0; i < count; ++i )
86 {
87 const std::shared_ptr<RC_ITEM>& item = m_ratsnestProvider->GetItem( i );
88 SEVERITY severity = bds.GetSeverity( item->GetErrorCode() );
89
90 fprintf( fp, "%s", TO_UTF8( item->ShowReport( &unitsProvider, severity, itemMap ) ) );
91 }
92
93 count = m_fpWarningsProvider->GetCount();
94
95 fprintf( fp, "\n** Found %d Footprint errors **\n", count );
96
97 for( int i = 0; i < count; ++i )
98 {
99 const std::shared_ptr<RC_ITEM>& item = m_fpWarningsProvider->GetItem( i );
100 SEVERITY severity = bds.GetSeverity( item->GetErrorCode() );
101
102 fprintf( fp, "%s", TO_UTF8( item->ShowReport( &unitsProvider, severity, itemMap ) ) );
103 }
104
105
106 fprintf( fp, "\n** End of Report **\n" );
107
108 fclose( fp );
109
110 return true;
111}
112
113
114bool DRC_REPORT::WriteJsonReport( const wxString& aFullFileName )
115{
116 std::ofstream jsonFileStream( aFullFileName.fn_str() );
117
118 UNITS_PROVIDER unitsProvider( pcbIUScale, m_reportUnits );
120 std::map<KIID, EDA_ITEM*> itemMap;
121 m_board->FillItemMap( itemMap );
122
123 RC_JSON::DRC_REPORT reportHead;
124
125 wxFileName fn( m_board->GetFileName() );
126 reportHead.$schema = "https://schemas.kicad.org/drc.v1.json";
127 reportHead.source = fn.GetFullName();
128 reportHead.date = GetISO8601CurrentDateTime();
131
132 for( int i = 0; i < m_markersProvider->GetCount(); ++i )
133 {
134 const std::shared_ptr<RC_ITEM>& item = m_markersProvider->GetItem( i );
135 SEVERITY severity = item->GetParent()->GetSeverity();
136
137 if( severity == RPT_SEVERITY_EXCLUSION )
138 severity = bds.GetSeverity( item->GetErrorCode() );
139
140 RC_JSON::VIOLATION violation;
141 item->GetJsonViolation( violation, &unitsProvider, severity, itemMap );
142
143 reportHead.violations.push_back( violation );
144 }
145
146 for( int i = 0; i < m_ratsnestProvider->GetCount(); ++i )
147 {
148 const std::shared_ptr<RC_ITEM>& item = m_ratsnestProvider->GetItem( i );
149 SEVERITY severity = bds.GetSeverity( item->GetErrorCode() );
150
151 RC_JSON::VIOLATION violation;
152 item->GetJsonViolation( violation, &unitsProvider, severity, itemMap );
153
154 reportHead.unconnected_items.push_back( violation );
155 }
156
157
158 for( int i = 0; i < m_fpWarningsProvider->GetCount(); ++i )
159 {
160 const std::shared_ptr<RC_ITEM>& item = m_fpWarningsProvider->GetItem( i );
161 SEVERITY severity = bds.GetSeverity( item->GetErrorCode() );
162
163 RC_JSON::VIOLATION violation;
164 item->GetJsonViolation( violation, &unitsProvider, severity, itemMap );
165
166 reportHead.schematic_parity.push_back( violation );
167 }
168
169
170 nlohmann::json saveJson = nlohmann::json( reportHead );
171 jsonFileStream << std::setw( 4 ) << saveJson << std::endl;
172 jsonFileStream.flush();
173 jsonFileStream.close();
174
175 return true;
176}
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:108
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:290
const wxString & GetFileName() const
Definition: board.h:327
void FillItemMap(std::map< KIID, EDA_ITEM * > &aMap)
Definition: board.cpp:1512
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.cpp:892
std::shared_ptr< RC_ITEMS_PROVIDER > m_ratsnestProvider
Definition: drc_report.h:46
bool WriteJsonReport(const wxString &aFullFileName)
Definition: drc_report.cpp:114
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:47
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:33
std::shared_ptr< RC_ITEMS_PROVIDER > m_fpWarningsProvider
Definition: drc_report.h:47
EDA_UNITS
Definition: eda_units.h:46
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:155
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:398
std::vector< VIOLATION > unconnected_items
std::vector< VIOLATION > violations
std::vector< VIOLATION > schematic_parity