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 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 virtual bool Run() override;
60
61 virtual const wxString GetName() const override { return wxT( "schematic_parity" ); };
62
63private:
64 void testNetlist( NETLIST& aNetlist );
65};
66
67
69{
70 BOARD* board = m_drcEngine->GetBoard();
71
72 auto compare = []( const FOOTPRINT* x, const FOOTPRINT* y )
73 {
74 return x->GetReference().CmpNoCase( y->GetReference() ) < 0;
75 };
76
77 auto footprints = std::set<FOOTPRINT*, decltype( compare )>( compare );
78
79 // Search for duplicate footprints on the board
80 for( FOOTPRINT* footprint : board->Footprints() )
81 {
83 break;
84
85 auto ins = footprints.insert( footprint );
86
87 if( !ins.second && !( footprint->GetAttributes() & FP_BOARD_ONLY ) )
88 {
89 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_DUPLICATE_FOOTPRINT );
90 drcItem->SetItems( footprint, *ins.first );
91
92 reportViolation( drcItem, footprint->GetPosition(), UNDEFINED_LAYER );
93 }
94 }
95
96 // Search for component footprints in the netlist but not on the board.
97 for( unsigned ii = 0; ii < aNetlist.GetCount(); ii++ )
98 {
99 COMPONENT* component = aNetlist.GetComponent( ii );
100 FOOTPRINT* footprint = board->FindFootprintByReference( component->GetReference() );
101
102 if( footprint == nullptr )
103 {
105 {
106 wxString msg;
107 msg.Printf( _( "Missing footprint %s (%s)" ),
108 component->GetReference(),
109 component->GetValue() );
110
111 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_MISSING_FOOTPRINT );
112
113 drcItem->SetErrorMessage( msg );
115 }
116 }
117 else
118 {
119 if( component->GetValue() != footprint->GetValue()
121 {
122 wxString msg;
123 msg.Printf( _( "Value (%s) doesn't match symbol value (%s)." ),
124 footprint->GetReference(), footprint->GetValue(),
125 component->GetValue() );
126
127 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_SCHEMATIC_PARITY );
128 drcItem->SetErrorMessage( msg );
129 drcItem->SetItems( footprint );
130 reportViolation( drcItem, footprint->GetPosition(), UNDEFINED_LAYER );
131 }
132
133 if( component->GetFPID().GetUniStringLibId() != footprint->GetFPID().GetUniStringLibId()
135 {
136 wxString msg;
137 msg.Printf( _( "%s doesn't match footprint given by symbol (%s)." ),
138 footprint->GetFPID().GetUniStringLibId(),
139 component->GetFPID().GetUniStringLibId() );
140
141 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_SCHEMATIC_PARITY );
142 drcItem->SetErrorMessage( msg );
143 drcItem->SetItems( footprint );
144 reportViolation( drcItem, footprint->GetPosition(), UNDEFINED_LAYER );
145 }
146
148 {
149 wxString libIdLower = footprint->GetFPID().GetUniStringLibId().Lower();
150 wxString fpNameLower = footprint->GetFPID().GetUniStringLibItemName().Lower();
151 size_t filtercount = component->GetFootprintFilters().GetCount();
152 bool found = ( 0 == filtercount ); // if no entries, do not filter
153
154 for( size_t jj = 0; jj < filtercount && !found; jj++ )
155 {
156 wxString filterLower = component->GetFootprintFilters()[jj].Lower();
157
158 if( filterLower.Find( ':' ) == wxNOT_FOUND )
159 found = fpNameLower.Matches( filterLower );
160 else
161 found = libIdLower.Matches( filterLower );
162 }
163
164 if( !found )
165 {
166 wxString msg;
167 msg.Printf( _( "%s doesn't match symbol's footprint filters (%s)." ),
168 footprint->GetFPID().GetUniStringLibId(),
169 wxJoin( component->GetFootprintFilters(), ' ' ) );
170
171 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_FOOTPRINT_FILTERS );
172 drcItem->SetErrorMessage( msg );
173 drcItem->SetItems( footprint );
174 reportViolation( drcItem, footprint->GetPosition(), UNDEFINED_LAYER );
175 }
176 }
177
178 if( ( component->GetProperties().count( "dnp" ) > 0 )
179 != ( ( footprint->GetAttributes() & FP_DNP ) > 0 )
181 {
182 wxString msg;
183 msg.Printf( _( "'%s' settings differ." ), _( "Do not populate" ) );
184
185 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_SCHEMATIC_PARITY );
186 drcItem->SetErrorMessage( drcItem->GetErrorMessage() + wxS( ": " ) + msg );
187 drcItem->SetItems( footprint );
188 reportViolation( drcItem, footprint->GetPosition(), UNDEFINED_LAYER );
189 }
190
191 if( ( component->GetProperties().count( "exclude_from_bom" ) > 0 )
192 != ( (footprint->GetAttributes() & FP_EXCLUDE_FROM_BOM ) > 0 )
194 {
195 wxString msg;
196 msg.Printf( _( "'%s' settings differ." ), _( "Exclude from bill of materials" ) );
197
198 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_SCHEMATIC_PARITY );
199 drcItem->SetErrorMessage( drcItem->GetErrorMessage() + wxS( ": " ) + msg );
200 drcItem->SetItems( footprint );
201 reportViolation( drcItem, footprint->GetPosition(), UNDEFINED_LAYER );
202 }
203
204 for( PAD* pad : footprint->Pads() )
205 {
207 break;
208
209 if( !pad->CanHaveNumber() )
210 continue;
211
212 const COMPONENT_NET& sch_net = component->GetNet( pad->GetNumber() );
213 const wxString& pcb_netname = pad->GetNetname();
214
215 if( !pcb_netname.IsEmpty() && sch_net.GetPinName().IsEmpty() )
216 {
217 wxString msg;
218 msg.Printf( _( "No corresponding pin found in schematic." ) );
219
220 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_NET_CONFLICT );
221 drcItem->SetErrorMessage( msg );
222 drcItem->SetItems( pad );
223 reportViolation( drcItem, footprint->GetPosition(), UNDEFINED_LAYER );
224 }
225 else if( pcb_netname.IsEmpty() && !sch_net.GetNetName().IsEmpty() )
226 {
227 wxString msg;
228 msg.Printf( _( "Pad missing net given by schematic (%s)." ),
229 sch_net.GetNetName() );
230
231 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_NET_CONFLICT );
232 drcItem->SetErrorMessage( msg );
233 drcItem->SetItems( pad );
234 reportViolation( drcItem, footprint->GetPosition(), UNDEFINED_LAYER );
235 }
236 else if( pcb_netname != sch_net.GetNetName()
237 && !( pcb_netname.starts_with(
238 wxT( "unconnected-" ) )
239 && pcb_netname.starts_with( sch_net.GetNetName() ) ) )
240 {
241 wxString msg;
242 msg.Printf( _( "Pad net (%s) doesn't match net given by schematic (%s)." ),
243 pcb_netname,
244 sch_net.GetNetName() );
245
246 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_NET_CONFLICT );
247 drcItem->SetErrorMessage( msg );
248 drcItem->SetItems( pad );
249 reportViolation( drcItem, footprint->GetPosition(), UNDEFINED_LAYER );
250 }
251 }
252
253 for( unsigned jj = 0; jj < component->GetNetCount(); ++jj )
254 {
256 break;
257
258 const COMPONENT_NET& sch_net = component->GetNet( jj );
259
260 if( !footprint->FindPadByNumber( sch_net.GetPinName() ) )
261 {
262 wxString msg;
263
264 if( sch_net.GetNetName().StartsWith( wxT( "unconnected-" ) ) )
265 {
266 msg = sch_net.GetPinName();
267 }
268 else
269 {
270 msg = wxString::Format( wxT( "%s (%s)" ),
271 sch_net.GetPinName(),
272 sch_net.GetNetName() );
273 }
274
275 msg.Printf( _( "No pad found for pin %s in schematic." ), msg );
276
277 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_NET_CONFLICT );
278 drcItem->SetErrorMessage( msg );
279 drcItem->SetItems( footprint );
280 reportViolation( drcItem, footprint->GetPosition(), UNDEFINED_LAYER );
281 }
282 }
283 }
284 }
285
286 // Search for component footprints found on board but not in netlist.
287 for( FOOTPRINT* footprint : board->Footprints() )
288 {
290 break;
291
292 if( footprint->GetAttributes() & FP_BOARD_ONLY )
293 continue;
294
295 if( !aNetlist.GetComponentByReference( footprint->GetReference() ) )
296 {
297 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_EXTRA_FOOTPRINT );
298
299 drcItem->SetItems( footprint );
300 reportViolation( drcItem, footprint->GetPosition(), UNDEFINED_LAYER );
301 }
302 }
303}
304
305
307{
309 {
310 if( !reportPhase( _( "Checking PCB to schematic parity..." ) ) )
311 return false;
312
314
315 if( !netlist )
316 {
317 REPORT_AUX( wxT( "No netlist provided, skipping schematic parity tests." ) );
318 return true;
319 }
320
322 }
323
324 return !m_drcEngine->IsCancelled();
325}
326
327
328namespace detail
329{
331}
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:317
const FOOTPRINTS & Footprints() const
Definition: board.h:358
FOOTPRINT * FindFootprintByReference(const wxString &aReference) const
Search for a FOOTPRINT within this board with the given reference designator.
Definition: board.cpp:2100
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:100
const COMPONENT_NET & GetNet(unsigned aIndex) const
Definition: pcb_netlist.h:128
const wxString & GetReference() const
Definition: pcb_netlist.h:143
const wxString & GetValue() const
Definition: pcb_netlist.h:146
const std::map< wxString, wxString > & GetProperties() const
Definition: pcb_netlist.h:158
const wxArrayString & GetFootprintFilters() const
Definition: pcb_netlist.h:171
const LIB_ID & GetFPID() const
Definition: pcb_netlist.h:161
unsigned GetNetCount() const
Definition: pcb_netlist.h:126
bool GetTestFootprints() const
Definition: drc_engine.h:170
BOARD * GetBoard() const
Definition: drc_engine.h:95
bool IsErrorLimitExceeded(int error_code)
bool IsCancelled() const
NETLIST * GetSchematicNetlist() const
Definition: drc_engine.h:101
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 bool Run() override
Run this provider against the given PCB with configured options (if any).
virtual ~DRC_TEST_PROVIDER_SCHEMATIC_PARITY()=default
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
std::deque< PAD * > & Pads()
Definition: footprint.h:209
int GetAttributes() const
Definition: footprint.h:293
const LIB_ID & GetFPID() const
Definition: footprint.h:251
const wxString & GetValue() const
Definition: footprint.h:647
const wxString & GetReference() const
Definition: footprint.h:625
VECTOR2I GetPosition() const override
Definition: footprint.h:227
PAD * FindPadByNumber(const wxString &aPadNumber, PAD *aSearchAfterMe=nullptr) const
Return a PAD with a matching number.
Definition: footprint.cpp:1953
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:274
unsigned GetCount() const
Definition: pcb_netlist.h:295
COMPONENT * GetComponentByReference(const wxString &aReference)
Return a COMPONENT by aReference.
COMPONENT * GetComponent(unsigned aIndex)
Return the COMPONENT at aIndex.
Definition: pcb_netlist.h:303
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 REPORT_AUX(s)
#define _(s)
@ FP_DNP
Definition: footprint.h:86
@ 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