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
42 - DRCE_FOOTPRINT_FILTERS
43
44 TODO:
45 - cross-check PCB netlist against SCH netlist
46 - cross-check PCB fields against SCH fields
47*/
48
50{
51public:
53 {
54 m_isRuleDriven = false;
55 }
56
58 {
59 }
60
61 virtual bool Run() override;
62
63 virtual const wxString GetName() const override
64 {
65 return wxT( "schematic_parity" );
66 };
67
68 virtual const wxString GetDescription() const override
69 {
70 return wxT( "Performs layout-vs-schematics integity check" );
71 }
72
73private:
74 void testNetlist( NETLIST& aNetlist );
75};
76
77
79{
80 BOARD* board = m_drcEngine->GetBoard();
81
82 auto compare = []( const FOOTPRINT* x, const FOOTPRINT* y )
83 {
84 return x->GetReference().CmpNoCase( y->GetReference() ) < 0;
85 };
86
87 auto footprints = std::set<FOOTPRINT*, decltype( compare )>( compare );
88
89 // Search for duplicate footprints on the board
90 for( FOOTPRINT* footprint : board->Footprints() )
91 {
93 break;
94
95 auto ins = footprints.insert( footprint );
96
97 if( !ins.second && !( footprint->GetAttributes() & FP_BOARD_ONLY ) )
98 {
99 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_DUPLICATE_FOOTPRINT );
100 drcItem->SetItems( footprint, *ins.first );
101
102 reportViolation( drcItem, footprint->GetPosition(), UNDEFINED_LAYER );
103 }
104 }
105
106 // Search for component footprints in the netlist but not on the board.
107 for( unsigned ii = 0; ii < aNetlist.GetCount(); ii++ )
108 {
109 COMPONENT* component = aNetlist.GetComponent( ii );
110 FOOTPRINT* footprint = board->FindFootprintByReference( component->GetReference() );
111
112 if( footprint == nullptr )
113 {
115 {
116 wxString msg;
117 msg.Printf( _( "Missing footprint %s (%s)" ),
118 component->GetReference(),
119 component->GetValue() );
120
121 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_MISSING_FOOTPRINT );
122
123 drcItem->SetErrorMessage( msg );
125 }
126 }
127 else
128 {
129 if( component->GetValue() != footprint->GetValue()
131 {
132 wxString msg;
133 msg.Printf( _( "Value (%s) doesn't match symbol value (%s)." ),
134 footprint->GetReference(), footprint->GetValue(),
135 component->GetValue() );
136
137 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_SCHEMATIC_PARITY );
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 doesn't match footprint given by symbol (%s)." ),
148 footprint->GetFPID().GetUniStringLibId(),
149 component->GetFPID().GetUniStringLibId() );
150
151 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_SCHEMATIC_PARITY );
152 drcItem->SetErrorMessage( msg );
153 drcItem->SetItems( footprint );
154 reportViolation( drcItem, footprint->GetPosition(), UNDEFINED_LAYER );
155 }
156
158 {
159 wxString fpName = footprint->GetFPID().GetUniStringLibItemName();
160 size_t filtercount = component->GetFootprintFilters().GetCount();
161 bool found = ( 0 == filtercount ); // if no entries, do not filter
162
163 for( size_t jj = 0; jj < filtercount && !found; jj++ )
164 found = fpName.Matches( component->GetFootprintFilters()[jj] );
165
166 if( !found )
167 {
168 wxString msg;
169 msg.Printf( _( "%s doesn't match symbol's footprint filters (%s)." ),
170 fpName,
171 wxJoin( component->GetFootprintFilters(), ' ' ) );
172
173 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_FOOTPRINT_FILTERS );
174 drcItem->SetErrorMessage( msg );
175 drcItem->SetItems( footprint );
176 reportViolation( drcItem, footprint->GetPosition(), UNDEFINED_LAYER );
177 }
178 }
179
180 if( ( component->GetProperties().count( "dnp" ) > 0 )
181 != ( ( footprint->GetAttributes() & FP_DNP ) > 0 )
183 {
184 wxString msg;
185 msg.Printf( _( "'%s' settings differ." ), _( "Do not populate" ) );
186
187 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_SCHEMATIC_PARITY );
188 drcItem->SetErrorMessage( drcItem->GetErrorMessage() + wxS( ": " ) + msg );
189 drcItem->SetItems( footprint );
190 reportViolation( drcItem, footprint->GetPosition(), UNDEFINED_LAYER );
191 }
192
193 if( ( component->GetProperties().count( "exclude_from_bom" ) > 0 )
194 != ( (footprint->GetAttributes() & FP_EXCLUDE_FROM_BOM ) > 0 )
196 {
197 wxString msg;
198 msg.Printf( _( "'%s' settings differ." ), _( "Exclude from bill of materials" ) );
199
200 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_SCHEMATIC_PARITY );
201 drcItem->SetErrorMessage( drcItem->GetErrorMessage() + wxS( ": " ) + msg );
202 drcItem->SetItems( footprint );
203 reportViolation( drcItem, footprint->GetPosition(), UNDEFINED_LAYER );
204 }
205
206 for( PAD* pad : footprint->Pads() )
207 {
209 break;
210
211 if( !pad->CanHaveNumber() )
212 continue;
213
214 const COMPONENT_NET& sch_net = component->GetNet( pad->GetNumber() );
215 const wxString& pcb_netname = pad->GetNetname();
216
217 if( !pcb_netname.IsEmpty() && sch_net.GetPinName().IsEmpty() )
218 {
219 wxString msg;
220 msg.Printf( _( "No corresponding pin found in schematic." ) );
221
222 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_NET_CONFLICT );
223 drcItem->SetErrorMessage( msg );
224 drcItem->SetItems( pad );
225 reportViolation( drcItem, footprint->GetPosition(), UNDEFINED_LAYER );
226 }
227 else if( pcb_netname.IsEmpty() && !sch_net.GetNetName().IsEmpty() )
228 {
229 wxString msg;
230 msg.Printf( _( "Pad missing net given by schematic (%s)." ),
231 sch_net.GetNetName() );
232
233 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_NET_CONFLICT );
234 drcItem->SetErrorMessage( msg );
235 drcItem->SetItems( pad );
236 reportViolation( drcItem, footprint->GetPosition(), UNDEFINED_LAYER );
237 }
238 else if( pcb_netname != sch_net.GetNetName()
239 && !( pcb_netname.starts_with(
240 wxT( "unconnected-" ) )
241 && pcb_netname.starts_with( sch_net.GetNetName() ) ) )
242 {
243 wxString msg;
244 msg.Printf( _( "Pad net (%s) doesn't match net given by schematic (%s)." ),
245 pcb_netname,
246 sch_net.GetNetName() );
247
248 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_NET_CONFLICT );
249 drcItem->SetErrorMessage( msg );
250 drcItem->SetItems( pad );
251 reportViolation( drcItem, footprint->GetPosition(), UNDEFINED_LAYER );
252 }
253 }
254
255 for( unsigned jj = 0; jj < component->GetNetCount(); ++jj )
256 {
258 break;
259
260 const COMPONENT_NET& sch_net = component->GetNet( jj );
261
262 if( !footprint->FindPadByNumber( sch_net.GetPinName() ) )
263 {
264 wxString msg;
265
266 if( sch_net.GetNetName().StartsWith( wxT( "unconnected-" ) ) )
267 {
268 msg = sch_net.GetPinName();
269 }
270 else
271 {
272 msg = wxString::Format( wxT( "%s (%s)" ),
273 sch_net.GetPinName(),
274 sch_net.GetNetName() );
275 }
276
277 msg.Printf( _( "No pad found for pin %s in schematic." ), msg );
278
279 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_NET_CONFLICT );
280 drcItem->SetErrorMessage( msg );
281 drcItem->SetItems( footprint );
282 reportViolation( drcItem, footprint->GetPosition(), UNDEFINED_LAYER );
283 }
284 }
285 }
286 }
287
288 // Search for component footprints found on board but not in netlist.
289 for( FOOTPRINT* footprint : board->Footprints() )
290 {
292 break;
293
294 if( footprint->GetAttributes() & FP_BOARD_ONLY )
295 continue;
296
297 if( !aNetlist.GetComponentByReference( footprint->GetReference() ) )
298 {
299 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_EXTRA_FOOTPRINT );
300
301 drcItem->SetItems( footprint );
302 reportViolation( drcItem, footprint->GetPosition(), UNDEFINED_LAYER );
303 }
304 }
305}
306
307
309{
311 {
312 if( !reportPhase( _( "Checking PCB to schematic parity..." ) ) )
313 return false;
314
316
317 if( !netlist )
318 {
319 reportAux( wxT( "No netlist provided, skipping schematic parity tests." ) );
320 return true;
321 }
322
324
326 }
327
328 return !m_drcEngine->IsCancelled();
329}
330
331
332namespace detail
333{
335}
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:290
const FOOTPRINTS & Footprints() const
Definition: board.h:331
FOOTPRINT * FindFootprintByReference(const wxString &aReference) const
Search for a FOOTPRINT within this board with the given reference designator.
Definition: board.cpp:2004
Used to store the component pin name to net name (and pin function) associations stored in a netlist.
Definition: pcb_netlist.h:47
const wxString & GetNetName() const
Definition: pcb_netlist.h:61
const wxString & GetPinName() const
Definition: pcb_netlist.h:60
Store all of the related footprint information found in a netlist.
Definition: pcb_netlist.h:88
const COMPONENT_NET & GetNet(unsigned aIndex) const
Definition: pcb_netlist.h:114
const wxString & GetReference() const
Definition: pcb_netlist.h:129
const wxString & GetValue() const
Definition: pcb_netlist.h:132
const std::map< wxString, wxString > & GetProperties() const
Definition: pcb_netlist.h:144
const wxArrayString & GetFootprintFilters() const
Definition: pcb_netlist.h:157
const LIB_ID & GetFPID() const
Definition: pcb_netlist.h:147
unsigned GetNetCount() const
Definition: pcb_netlist.h:112
bool GetTestFootprints() const
Definition: drc_engine.h:191
BOARD * GetBoard() const
Definition: drc_engine.h:99
bool IsErrorLimitExceeded(int error_code)
bool IsCancelled() const
NETLIST * GetSchematicNetlist() const
Definition: drc_engine.h:105
static std::shared_ptr< DRC_ITEM > Create(int aErrorCode)
Constructs a DRC_ITEM for the given error code.
Definition: drc_item.cpp:372
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()
std::deque< PAD * > & Pads()
Definition: footprint.h:206
int GetAttributes() const
Definition: footprint.h:290
const LIB_ID & GetFPID() const
Definition: footprint.h:248
const wxString & GetValue() const
Definition: footprint.h:624
const wxString & GetReference() const
Definition: footprint.h:602
VECTOR2I GetPosition() const override
Definition: footprint.h:224
PAD * FindPadByNumber(const wxString &aPadNumber, PAD *aSearchAfterMe=nullptr) const
Return a PAD with a matching number.
Definition: footprint.cpp:1809
wxString GetUniStringLibId() const
Definition: lib_id.h:148
const wxString GetUniStringLibItemName() const
Get strings for display messages in dialogs.
Definition: lib_id.h:112
Store information read from a netlist along with the flags used to update the NETLIST in the BOARD.
Definition: pcb_netlist.h:241
unsigned GetCount() const
Definition: pcb_netlist.h:262
COMPONENT * GetComponentByReference(const wxString &aReference)
Return a COMPONENT by aReference.
COMPONENT * GetComponent(unsigned aIndex)
Return the COMPONENT at aIndex.
Definition: pcb_netlist.h:270
Definition: pad.h:54
@ DRCE_FOOTPRINT_FILTERS
Definition: drc_item.h:79
@ DRCE_DUPLICATE_FOOTPRINT
Definition: drc_item.h:75
@ DRCE_EXTRA_FOOTPRINT
Definition: drc_item.h:76
@ DRCE_NET_CONFLICT
Definition: drc_item.h:77
@ DRCE_MISSING_FOOTPRINT
Definition: drc_item.h:74
@ DRCE_SCHEMATIC_PARITY
Definition: drc_item.h:78
#define _(s)
@ FP_DNP
Definition: footprint.h:83
@ FP_BOARD_ONLY
Definition: footprint.h:79
@ FP_EXCLUDE_FROM_BOM
Definition: footprint.h:78
@ UNDEFINED_LAYER
Definition: layer_ids.h:61
static DRC_REGISTER_TEST_PROVIDER< DRC_TEST_PROVIDER_ANNULAR_WIDTH > dummy
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:691