KiCad PCB EDA Suite
Loading...
Searching...
No Matches
drc_test_provider_schematic_parity.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) 2004-2022 KiCad Developers.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU 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, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include <board.h>
25#include <pad.h>
26#include <drc/drc_engine.h>
27#include <drc/drc_item.h>
28#include <drc/drc_rule.h>
30
31#include <kiway.h>
33
34/*
35 Schematic parity test.
36
37 Errors generated:
38 - DRCE_MISSING_FOOTPRINT
39 - DRCE_DUPLICATE_FOOTPRINT
40 - DRCE_EXTRA_FOOTPRINT
41 - DRCE_SCHEMATIC_PARITY_ISSUES
42
43 TODO:
44 - cross-check PCB netlist against SCH netlist
45 - cross-check PCB fields against SCH fields
46*/
47
49{
50public:
52 {
53 m_isRuleDriven = false;
54 }
55
57 {
58 }
59
60 virtual bool Run() override;
61
62 virtual const wxString GetName() const override
63 {
64 return wxT( "schematic_parity" );
65 };
66
67 virtual const wxString GetDescription() const override
68 {
69 return wxT( "Performs layout-vs-schematics integity check" );
70 }
71
72private:
73 void testNetlist( NETLIST& aNetlist );
74};
75
76
78{
79 BOARD* board = m_drcEngine->GetBoard();
80
81 auto compare = []( const FOOTPRINT* x, const FOOTPRINT* y )
82 {
83 return x->GetReference().CmpNoCase( y->GetReference() ) < 0;
84 };
85
86 auto footprints = std::set<FOOTPRINT*, decltype( compare )>( compare );
87
88 // Search for duplicate footprints on the board
89 for( FOOTPRINT* footprint : board->Footprints() )
90 {
92 break;
93
94 auto ins = footprints.insert( footprint );
95
96 if( !ins.second && !( footprint->GetAttributes() & FP_BOARD_ONLY ) )
97 {
98 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_DUPLICATE_FOOTPRINT );
99 drcItem->SetItems( footprint, *ins.first );
100
101 reportViolation( drcItem, footprint->GetPosition(), UNDEFINED_LAYER );
102 }
103 }
104
105 // Search for component footprints in the netlist but not on the board.
106 for( unsigned ii = 0; ii < aNetlist.GetCount(); ii++ )
107 {
108 COMPONENT* component = aNetlist.GetComponent( ii );
109 FOOTPRINT* footprint = board->FindFootprintByReference( component->GetReference() );
110
111 if( footprint == nullptr )
112 {
114 {
115 wxString msg;
116 msg.Printf( _( "Missing footprint %s (%s)" ),
117 component->GetReference(),
118 component->GetValue() );
119
120 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_MISSING_FOOTPRINT );
121
122 drcItem->SetErrorMessage( msg );
124 }
125 }
126 else
127 {
128 if( component->GetValue() != footprint->GetValue()
130 {
131 wxString msg;
132 msg.Printf( _( "Footprint %s value (%s) doesn't match symbol value (%s)." ),
133 footprint->GetReference(), footprint->GetValue(),
134 component->GetValue() );
135
136 std::shared_ptr<DRC_ITEM> drcItem =
138 drcItem->SetErrorMessage( msg );
139 drcItem->SetItems( footprint );
140 reportViolation( drcItem, footprint->GetPosition(), UNDEFINED_LAYER );
141 }
142
143 if( component->GetFPID().GetUniStringLibId() != footprint->GetFPID().GetUniStringLibId()
145 {
146 wxString msg;
147 msg.Printf( _( "%s footprint (%s) doesn't match that given by symbol (%s)." ),
148 footprint->GetReference(), footprint->GetFPID().GetUniStringLibId(),
149 component->GetFPID().GetUniStringLibId() );
150
151 std::shared_ptr<DRC_ITEM> drcItem =
153 drcItem->SetErrorMessage( msg );
154 drcItem->SetItems( footprint );
155 reportViolation( drcItem, footprint->GetPosition(), UNDEFINED_LAYER );
156 }
157
158 if( ( component->GetProperties().count( "dnp" ) > 0 )
159 != ( ( footprint->GetAttributes() & FP_DNP ) > 0 )
161 {
162 wxString msg;
163 msg.Printf( _( "'%s' settings differ." ), _( "Do not populate" ) );
164
165 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_SCHEMATIC_PARITY_ISSUES );
166 drcItem->SetErrorMessage( drcItem->GetErrorMessage() + wxS( ": " ) + msg );
167 drcItem->SetItems( footprint );
168 reportViolation( drcItem, footprint->GetPosition(), UNDEFINED_LAYER );
169 }
170
171 if( ( component->GetProperties().count( "exclude_from_bom" ) > 0 )
172 != ( (footprint->GetAttributes() & FP_EXCLUDE_FROM_BOM ) > 0 )
174 {
175 wxString msg;
176 msg.Printf( _( "'%s' settings differ." ), _( "Exclude from bill of materials" ) );
177
178 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_SCHEMATIC_PARITY_ISSUES );
179 drcItem->SetErrorMessage( drcItem->GetErrorMessage() + wxS( ": " ) + msg );
180 drcItem->SetItems( footprint );
181 reportViolation( drcItem, footprint->GetPosition(), UNDEFINED_LAYER );
182 }
183
184 for( PAD* pad : footprint->Pads() )
185 {
187 break;
188
189 if( !pad->CanHaveNumber() )
190 continue;
191
192 const COMPONENT_NET& sch_net = component->GetNet( pad->GetNumber() );
193 const wxString& pcb_netname = pad->GetNetname();
194
195 if( !pcb_netname.IsEmpty() && sch_net.GetPinName().IsEmpty() )
196 {
197 wxString msg;
198 msg.Printf( _( "No corresponding pin found in schematic." ) );
199
200 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_NET_CONFLICT );
201 drcItem->SetErrorMessage( msg );
202 drcItem->SetItems( pad );
203 reportViolation( drcItem, footprint->GetPosition(), UNDEFINED_LAYER );
204 }
205 else if( pcb_netname.IsEmpty() && !sch_net.GetNetName().IsEmpty() )
206 {
207 wxString msg;
208 msg.Printf( _( "Pad missing net given by schematic (%s)." ),
209 sch_net.GetNetName() );
210
211 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_NET_CONFLICT );
212 drcItem->SetErrorMessage( msg );
213 drcItem->SetItems( pad );
214 reportViolation( drcItem, footprint->GetPosition(), UNDEFINED_LAYER );
215 }
216 else if( pcb_netname != sch_net.GetNetName()
217 && !( pcb_netname.starts_with(
218 wxT( "unconnected-" ) )
219 && pcb_netname.starts_with( sch_net.GetNetName() ) ) )
220 {
221 wxString msg;
222 msg.Printf( _( "Pad net (%s) doesn't match net given by schematic (%s)." ),
223 pcb_netname,
224 sch_net.GetNetName() );
225
226 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_NET_CONFLICT );
227 drcItem->SetErrorMessage( msg );
228 drcItem->SetItems( pad );
229 reportViolation( drcItem, footprint->GetPosition(), UNDEFINED_LAYER );
230 }
231 }
232
233 for( unsigned jj = 0; jj < component->GetNetCount(); ++jj )
234 {
236 break;
237
238 const COMPONENT_NET& sch_net = component->GetNet( jj );
239
240 if( !footprint->FindPadByNumber( sch_net.GetPinName() ) )
241 {
242 wxString msg;
243
244 if( sch_net.GetNetName().StartsWith( wxT( "unconnected-" ) ) )
245 {
246 msg = sch_net.GetPinName();
247 }
248 else
249 {
250 msg = wxString::Format( wxT( "%s (%s)" ),
251 sch_net.GetPinName(),
252 sch_net.GetNetName() );
253 }
254
255 msg.Printf( _( "No pad found for pin %s in schematic." ), msg );
256
257 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_NET_CONFLICT );
258 drcItem->SetErrorMessage( msg );
259 drcItem->SetItems( footprint );
260 reportViolation( drcItem, footprint->GetPosition(), UNDEFINED_LAYER );
261 }
262 }
263 }
264 }
265
266 // Search for component footprints found on board but not in netlist.
267 for( FOOTPRINT* footprint : board->Footprints() )
268 {
270 break;
271
272 if( footprint->GetAttributes() & FP_BOARD_ONLY )
273 continue;
274
275 if( !aNetlist.GetComponentByReference( footprint->GetReference() ) )
276 {
277 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_EXTRA_FOOTPRINT );
278
279 drcItem->SetItems( footprint );
280 reportViolation( drcItem, footprint->GetPosition(), UNDEFINED_LAYER );
281 }
282 }
283}
284
285
287{
289 {
290 if( !reportPhase( _( "Checking PCB to schematic parity..." ) ) )
291 return false;
292
294
295 if( !netlist )
296 {
297 reportAux( wxT( "No netlist provided, skipping schematic parity tests." ) );
298 return true;
299 }
300
302
304 }
305
306 return !m_drcEngine->IsCancelled();
307}
308
309
310namespace detail
311{
313}
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:281
const FOOTPRINTS & Footprints() const
Definition: board.h:322
FOOTPRINT * FindFootprintByReference(const wxString &aReference) const
Search for a FOOTPRINT within this board with the given reference designator.
Definition: board.cpp:1896
Used to store the component pin name to net name (and pin function) associations stored in a netlist.
Definition: pcb_netlist.h:45
const wxString & GetNetName() const
Definition: pcb_netlist.h:59
const wxString & GetPinName() const
Definition: pcb_netlist.h:58
Store all of the related footprint information found in a netlist.
Definition: pcb_netlist.h:86
const COMPONENT_NET & GetNet(unsigned aIndex) const
Definition: pcb_netlist.h:112
const wxString & GetReference() const
Definition: pcb_netlist.h:127
const wxString & GetValue() const
Definition: pcb_netlist.h:130
const std::map< wxString, wxString > & GetProperties() const
Definition: pcb_netlist.h:142
const LIB_ID & GetFPID() const
Definition: pcb_netlist.h:145
unsigned GetNetCount() const
Definition: pcb_netlist.h:110
bool GetTestFootprints() const
Definition: drc_engine.h:163
BOARD * GetBoard() const
Definition: drc_engine.h:89
bool IsErrorLimitExceeded(int error_code)
bool IsCancelled() const
NETLIST * GetSchematicNetlist() const
Definition: drc_engine.h:95
static std::shared_ptr< DRC_ITEM > Create(int aErrorCode)
Constructs a DRC_ITEM for the given error code.
Definition: drc_item.cpp:331
virtual const wxString GetName() const override
virtual const wxString GetDescription() const override
virtual bool Run() override
Run this provider against the given PCB with configured options (if any).
Represent a DRC "provider" which runs some DRC functions over a BOARD and spits out DRC_ITEM and posi...
virtual bool reportPhase(const wxString &aStageName)
virtual void reportViolation(std::shared_ptr< DRC_ITEM > &item, const VECTOR2I &aMarkerPos, int aMarkerLayer)
DRC_ENGINE * m_drcEngine
void reportAux(const wxString &aMsg)
virtual void reportRuleStatistics()
int GetAttributes() const
Definition: footprint.h:276
PADS & Pads()
Definition: footprint.h:191
const LIB_ID & GetFPID() const
Definition: footprint.h:233
const wxString & GetValue() const
Definition: footprint.h:610
const wxString & GetReference() const
Definition: footprint.h:588
VECTOR2I GetPosition() const override
Definition: footprint.h:209
PAD * FindPadByNumber(const wxString &aPadNumber, PAD *aSearchAfterMe=nullptr) const
Return a PAD with a matching number.
Definition: footprint.cpp:1779
wxString GetUniStringLibId() const
Definition: lib_id.h:148
Store information read from a netlist along with the flags used to update the NETLIST in the BOARD.
Definition: pcb_netlist.h:223
unsigned GetCount() const
Definition: pcb_netlist.h:244
COMPONENT * GetComponentByReference(const wxString &aReference)
Return a COMPONENT by aReference.
COMPONENT * GetComponent(unsigned aIndex)
Return the COMPONENT at aIndex.
Definition: pcb_netlist.h:252
Definition: pad.h:59
@ DRCE_DUPLICATE_FOOTPRINT
Definition: drc_item.h:71
@ DRCE_EXTRA_FOOTPRINT
Definition: drc_item.h:72
@ DRCE_NET_CONFLICT
Definition: drc_item.h:73
@ DRCE_MISSING_FOOTPRINT
Definition: drc_item.h:70
@ DRCE_SCHEMATIC_PARITY_ISSUES
Definition: drc_item.h:74
#define _(s)
@ FP_DNP
Definition: footprint.h:80
@ FP_BOARD_ONLY
Definition: footprint.h:76
@ FP_EXCLUDE_FROM_BOM
Definition: footprint.h:75
@ UNDEFINED_LAYER
Definition: layer_ids.h:61
static DRC_REGISTER_TEST_PROVIDER< DRC_TEST_PROVIDER_ANNULAR_WIDTH > dummy
VECTOR2< int > VECTOR2I
Definition: vector2d.h:588