KiCad PCB EDA Suite
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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 The 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 libIdLower = footprint->GetFPID().GetUniStringLibId().Lower();
160 wxString fpNameLower = footprint->GetFPID().GetUniStringLibItemName().Lower();
161 size_t filtercount = component->GetFootprintFilters().GetCount();
162 bool found = ( 0 == filtercount ); // if no entries, do not filter
163
164 for( size_t jj = 0; jj < filtercount && !found; jj++ )
165 {
166 wxString filterLower = component->GetFootprintFilters()[jj].Lower();
167
168 if( filterLower.Find( ':' ) == wxNOT_FOUND )
169 found = fpNameLower.Matches( filterLower );
170 else
171 found = libIdLower.Matches( filterLower );
172 }
173
174 if( !found )
175 {
176 wxString msg;
177 msg.Printf( _( "%s doesn't match symbol's footprint filters (%s)." ),
178 footprint->GetFPID().GetUniStringLibId(),
179 wxJoin( component->GetFootprintFilters(), ' ' ) );
180
181 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_FOOTPRINT_FILTERS );
182 drcItem->SetErrorMessage( msg );
183 drcItem->SetItems( footprint );
184 reportViolation( drcItem, footprint->GetPosition(), UNDEFINED_LAYER );
185 }
186 }
187
188 if( ( component->GetProperties().count( "dnp" ) > 0 )
189 != ( ( footprint->GetAttributes() & FP_DNP ) > 0 )
191 {
192 wxString msg;
193 msg.Printf( _( "'%s' settings differ." ), _( "Do not populate" ) );
194
195 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_SCHEMATIC_PARITY );
196 drcItem->SetErrorMessage( drcItem->GetErrorMessage() + wxS( ": " ) + msg );
197 drcItem->SetItems( footprint );
198 reportViolation( drcItem, footprint->GetPosition(), UNDEFINED_LAYER );
199 }
200
201 if( ( component->GetProperties().count( "exclude_from_bom" ) > 0 )
202 != ( (footprint->GetAttributes() & FP_EXCLUDE_FROM_BOM ) > 0 )
204 {
205 wxString msg;
206 msg.Printf( _( "'%s' settings differ." ), _( "Exclude from bill of materials" ) );
207
208 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_SCHEMATIC_PARITY );
209 drcItem->SetErrorMessage( drcItem->GetErrorMessage() + wxS( ": " ) + msg );
210 drcItem->SetItems( footprint );
211 reportViolation( drcItem, footprint->GetPosition(), UNDEFINED_LAYER );
212 }
213
214 for( PAD* pad : footprint->Pads() )
215 {
217 break;
218
219 if( !pad->CanHaveNumber() )
220 continue;
221
222 const COMPONENT_NET& sch_net = component->GetNet( pad->GetNumber() );
223 const wxString& pcb_netname = pad->GetNetname();
224
225 if( !pcb_netname.IsEmpty() && sch_net.GetPinName().IsEmpty() )
226 {
227 wxString msg;
228 msg.Printf( _( "No corresponding pin found in schematic." ) );
229
230 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_NET_CONFLICT );
231 drcItem->SetErrorMessage( msg );
232 drcItem->SetItems( pad );
233 reportViolation( drcItem, footprint->GetPosition(), UNDEFINED_LAYER );
234 }
235 else if( pcb_netname.IsEmpty() && !sch_net.GetNetName().IsEmpty() )
236 {
237 wxString msg;
238 msg.Printf( _( "Pad missing net given by schematic (%s)." ),
239 sch_net.GetNetName() );
240
241 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_NET_CONFLICT );
242 drcItem->SetErrorMessage( msg );
243 drcItem->SetItems( pad );
244 reportViolation( drcItem, footprint->GetPosition(), UNDEFINED_LAYER );
245 }
246 else if( pcb_netname != sch_net.GetNetName()
247 && !( pcb_netname.starts_with(
248 wxT( "unconnected-" ) )
249 && pcb_netname.starts_with( sch_net.GetNetName() ) ) )
250 {
251 wxString msg;
252 msg.Printf( _( "Pad net (%s) doesn't match net given by schematic (%s)." ),
253 pcb_netname,
254 sch_net.GetNetName() );
255
256 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_NET_CONFLICT );
257 drcItem->SetErrorMessage( msg );
258 drcItem->SetItems( pad );
259 reportViolation( drcItem, footprint->GetPosition(), UNDEFINED_LAYER );
260 }
261 }
262
263 for( unsigned jj = 0; jj < component->GetNetCount(); ++jj )
264 {
266 break;
267
268 const COMPONENT_NET& sch_net = component->GetNet( jj );
269
270 if( !footprint->FindPadByNumber( sch_net.GetPinName() ) )
271 {
272 wxString msg;
273
274 if( sch_net.GetNetName().StartsWith( wxT( "unconnected-" ) ) )
275 {
276 msg = sch_net.GetPinName();
277 }
278 else
279 {
280 msg = wxString::Format( wxT( "%s (%s)" ),
281 sch_net.GetPinName(),
282 sch_net.GetNetName() );
283 }
284
285 msg.Printf( _( "No pad found for pin %s in schematic." ), msg );
286
287 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_NET_CONFLICT );
288 drcItem->SetErrorMessage( msg );
289 drcItem->SetItems( footprint );
290 reportViolation( drcItem, footprint->GetPosition(), UNDEFINED_LAYER );
291 }
292 }
293 }
294 }
295
296 // Search for component footprints found on board but not in netlist.
297 for( FOOTPRINT* footprint : board->Footprints() )
298 {
300 break;
301
302 if( footprint->GetAttributes() & FP_BOARD_ONLY )
303 continue;
304
305 if( !aNetlist.GetComponentByReference( footprint->GetReference() ) )
306 {
307 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_EXTRA_FOOTPRINT );
308
309 drcItem->SetItems( footprint );
310 reportViolation( drcItem, footprint->GetPosition(), UNDEFINED_LAYER );
311 }
312 }
313}
314
315
317{
319 {
320 if( !reportPhase( _( "Checking PCB to schematic parity..." ) ) )
321 return false;
322
324
325 if( !netlist )
326 {
327 reportAux( wxT( "No netlist provided, skipping schematic parity tests." ) );
328 return true;
329 }
330
332
334 }
335
336 return !m_drcEngine->IsCancelled();
337}
338
339
340namespace detail
341{
343}
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:297
const FOOTPRINTS & Footprints() const
Definition: board.h:338
FOOTPRINT * FindFootprintByReference(const wxString &aReference) const
Search for a FOOTPRINT within this board with the given reference designator.
Definition: board.cpp:2097
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:115
const wxString & GetReference() const
Definition: pcb_netlist.h:130
const wxString & GetValue() const
Definition: pcb_netlist.h:133
const std::map< wxString, wxString > & GetProperties() const
Definition: pcb_netlist.h:145
const wxArrayString & GetFootprintFilters() const
Definition: pcb_netlist.h:158
const LIB_ID & GetFPID() const
Definition: pcb_netlist.h:148
unsigned GetNetCount() const
Definition: pcb_netlist.h:113
bool GetTestFootprints() const
Definition: drc_engine.h:171
BOARD * GetBoard() const
Definition: drc_engine.h:96
bool IsErrorLimitExceeded(int error_code)
bool IsCancelled() const
NETLIST * GetSchematicNetlist() const
Definition: drc_engine.h:102
static std::shared_ptr< DRC_ITEM > Create(int aErrorCode)
Constructs a DRC_ITEM for the given error code.
Definition: drc_item.cpp:393
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_CUSTOM_MARKER_HANDLER *aCustomHandler=nullptr)
DRC_ENGINE * m_drcEngine
void reportAux(const wxString &aMsg)
virtual void reportRuleStatistics()
std::deque< PAD * > & Pads()
Definition: footprint.h:211
int GetAttributes() const
Definition: footprint.h:295
const LIB_ID & GetFPID() const
Definition: footprint.h:253
const wxString & GetValue() const
Definition: footprint.h:641
const wxString & GetReference() const
Definition: footprint.h:619
VECTOR2I GetPosition() const override
Definition: footprint.h:229
PAD * FindPadByNumber(const wxString &aPadNumber, PAD *aSearchAfterMe=nullptr) const
Return a PAD with a matching number.
Definition: footprint.cpp:1955
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:255
unsigned GetCount() const
Definition: pcb_netlist.h:276
COMPONENT * GetComponentByReference(const wxString &aReference)
Return a COMPONENT by aReference.
COMPONENT * GetComponent(unsigned aIndex)
Return the COMPONENT at aIndex.
Definition: pcb_netlist.h:284
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:88
@ FP_BOARD_ONLY
Definition: footprint.h:84
@ FP_EXCLUDE_FROM_BOM
Definition: footprint.h:83
@ 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:695