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
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( aMarkersProvider ),
40 m_ratsnestProvider( aRatsnestProvider ),
41 m_fpWarningsProvider( 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 fprintf( fp, "** Drc report for %s **\n", TO_UTF8( m_board->GetFileName() ) );
62
63 fprintf( fp, "** Created on %s **\n", TO_UTF8( GetISO8601CurrentDateTime() ) );
64
65 count = m_markersProvider->GetCount();
66
67 fprintf( fp, "\n** Found %d DRC violations **\n", count );
68
69 for( int i = 0; i < count; ++i )
70 {
71 const std::shared_ptr<RC_ITEM>& item = m_markersProvider->GetItem( i );
72 SEVERITY severity = item->GetParent()->GetSeverity();
73
74 if( severity == RPT_SEVERITY_EXCLUSION )
75 severity = bds.GetSeverity( item->GetErrorCode() );
76
77 fprintf( fp, "%s", TO_UTF8( item->ShowReport( &unitsProvider, severity, itemMap ) ) );
78 }
79
80 count = m_ratsnestProvider->GetCount();
81
82 fprintf( fp, "\n** Found %d unconnected pads **\n", count );
83
84 for( int i = 0; i < count; ++i )
85 {
86 const std::shared_ptr<RC_ITEM>& item = m_ratsnestProvider->GetItem( i );
87 SEVERITY severity = bds.GetSeverity( item->GetErrorCode() );
88
89 fprintf( fp, "%s", TO_UTF8( item->ShowReport( &unitsProvider, severity, itemMap ) ) );
90 }
91
92 count = m_fpWarningsProvider->GetCount();
93
94 fprintf( fp, "\n** Found %d Footprint errors **\n", count );
95
96 for( int i = 0; i < count; ++i )
97 {
98 const std::shared_ptr<RC_ITEM>& item = m_fpWarningsProvider->GetItem( i );
99 SEVERITY severity = bds.GetSeverity( item->GetErrorCode() );
100
101 fprintf( fp, "%s", TO_UTF8( item->ShowReport( &unitsProvider, severity, itemMap ) ) );
102 }
103
104
105 fprintf( fp, "\n** End of Report **\n" );
106
107 fclose( fp );
108
109 return true;
110}
111
112
113bool DRC_REPORT::WriteJsonReport( const wxString& aFullFileName )
114{
115 std::ofstream jsonFileStream( aFullFileName.fn_str() );
116
117 UNITS_PROVIDER unitsProvider( pcbIUScale, m_reportUnits );
119 std::map<KIID, EDA_ITEM*> itemMap;
120 m_board->FillItemMap( itemMap );
121
122 RC_JSON::DRC_REPORT reportHead;
123 reportHead.source = m_board->GetFileName();
124 reportHead.date = GetISO8601CurrentDateTime();
127
128 for( int i = 0; i < m_markersProvider->GetCount(); ++i )
129 {
130 const std::shared_ptr<RC_ITEM>& item = m_markersProvider->GetItem( i );
131 SEVERITY severity = item->GetParent()->GetSeverity();
132
133 if( severity == RPT_SEVERITY_EXCLUSION )
134 severity = bds.GetSeverity( item->GetErrorCode() );
135
136 RC_JSON::VIOLATION violation;
137 item->GetJsonViolation( violation, &unitsProvider, severity, itemMap );
138
139 reportHead.violations.push_back( violation );
140 }
141
142 for( int i = 0; i < m_ratsnestProvider->GetCount(); ++i )
143 {
144 const std::shared_ptr<RC_ITEM>& item = m_ratsnestProvider->GetItem( i );
145 SEVERITY severity = bds.GetSeverity( item->GetErrorCode() );
146
147 RC_JSON::VIOLATION violation;
148 item->GetJsonViolation( violation, &unitsProvider, severity, itemMap );
149
150 reportHead.unconnected_items.push_back( violation );
151 }
152
153
154 for( int i = 0; i < m_fpWarningsProvider->GetCount(); ++i )
155 {
156 const std::shared_ptr<RC_ITEM>& item = m_ratsnestProvider->GetItem( i );
157 SEVERITY severity = bds.GetSeverity( item->GetErrorCode() );
158
159 RC_JSON::VIOLATION violation;
160 item->GetJsonViolation( violation, &unitsProvider, severity, itemMap );
161
162 reportHead.schematic_parity.push_back( violation );
163 }
164
165
166 nlohmann::json saveJson = nlohmann::json( reportHead );
167 jsonFileStream << std::setw( 4 ) << saveJson << std::endl;
168 jsonFileStream.flush();
169 jsonFileStream.close();
170
171 return true;
172}
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:109
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:276
const wxString & GetFileName() const
Definition: board.h:313
void FillItemMap(std::map< KIID, EDA_ITEM * > &aMap)
Definition: board.cpp:1231
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.cpp:766
std::shared_ptr< RC_ITEMS_PROVIDER > m_ratsnestProvider
Definition: drc_report.h:46
bool WriteJsonReport(const wxString &aFullFileName)
Definition: drc_report.cpp:113
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:139
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:391
std::vector< VIOLATION > unconnected_items
std::vector< VIOLATION > violations
std::vector< VIOLATION > schematic_parity