KiCad PCB EDA Suite
board_netlist_updater.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) 2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
5 * Copyright (C) 2015 CERN
6 * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <[email protected]>
7 * Copyright (C) 2011 Wayne Stambaugh <[email protected]>
8 *
9 * Copyright (C) 1992-2023 KiCad Developers, see AUTHORS.txt for contributors.
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, you may find one here:
23 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
24 * or you may search the http://www.gnu.org website for the version 2 license,
25 * or you may write to the Free Software Foundation, Inc.,
26 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
27 */
28
29
30#include <common.h> // for PAGE_INFO
31
32#include <base_units.h>
33#include <board.h>
34#include <netinfo.h>
35#include <footprint.h>
36#include <pad.h>
37#include <pcb_track.h>
38#include <zone.h>
39#include <string_utils.h>
40#include <pcbnew_settings.h>
41#include <pcb_edit_frame.h>
44#include <reporter.h>
45
47
48
50 m_frame( aFrame ),
51 m_commit( aFrame ),
52 m_board( aBoard )
53{
55
57 m_isDryRun = false;
59 m_lookupByTimestamp = false;
60
62 m_errorCount = 0;
64}
65
66
68{
69}
70
71
72// These functions allow inspection of pad nets during dry runs by keeping a cache of
73// current pad netnames indexed by pad.
74
75void BOARD_NETLIST_UPDATER::cacheNetname( PAD* aPad, const wxString& aNetname )
76{
77 m_padNets[ aPad ] = aNetname;
78}
79
80
82{
83 if( m_isDryRun && m_padNets.count( aPad ) )
84 return m_padNets[ aPad ];
85 else
86 return aPad->GetNetname();
87}
88
89
90void BOARD_NETLIST_UPDATER::cachePinFunction( PAD* aPad, const wxString& aPinFunction )
91{
92 m_padPinFunctions[ aPad ] = aPinFunction;
93}
94
95
97{
98 if( m_isDryRun && m_padPinFunctions.count( aPad ) )
99 return m_padPinFunctions[ aPad ];
100 else
101 return aPad->GetPinFunction();
102}
103
104
106{
107 VECTOR2I bestPosition;
108
109 if( !m_board->IsEmpty() )
110 {
111 // Position new components below any existing board features.
113
114 if( bbox.GetWidth() || bbox.GetHeight() )
115 {
116 bestPosition.x = bbox.Centre().x;
117 bestPosition.y = bbox.GetBottom() + pcbIUScale.mmToIU( 10 );
118 }
119 }
120 else
121 {
122 // Position new components in the center of the page when the board is empty.
124
125 bestPosition.x = pageSize.GetWidth() / 2;
126 bestPosition.y = pageSize.GetHeight() / 2;
127 }
128
129 return bestPosition;
130}
131
132
134{
135 wxString msg;
136
137 if( aComponent->GetFPID().empty() )
138 {
139 msg.Printf( _( "Cannot add %s (no footprint assigned)." ),
140 aComponent->GetReference(),
141 aComponent->GetFPID().Format().wx_str() );
143 ++m_errorCount;
144 return nullptr;
145 }
146
147 FOOTPRINT* footprint = m_frame->LoadFootprint( aComponent->GetFPID() );
148
149 if( footprint == nullptr )
150 {
151 msg.Printf( _( "Cannot add %s (footprint '%s' not found)." ),
152 aComponent->GetReference(),
153 aComponent->GetFPID().Format().wx_str() );
155 ++m_errorCount;
156 return nullptr;
157 }
158
159 if( m_isDryRun )
160 {
161 msg.Printf( _( "Add %s (footprint '%s')." ),
162 aComponent->GetReference(),
163 aComponent->GetFPID().Format().wx_str() );
164
165 delete footprint;
166 footprint = nullptr;
167 }
168 else
169 {
170 for( PAD* pad : footprint->Pads() )
171 {
172 // Set the pads ratsnest settings to the global settings
173 pad->SetLocalRatsnestVisible( m_frame->GetPcbNewSettings()->m_Display.m_ShowGlobalRatsnest );
174
175 // Pads in the library all have orphaned nets. Replace with Default.
176 pad->SetNetCode( 0 );
177 }
178
179 footprint->SetParent( m_board );
181
182 // This flag is used to prevent connectivity from considering the footprint during its
183 // initial build after the footprint is committed, because we're going to immediately start
184 // a move operation on the footprint and don't want its pads to drive nets onto vias/tracks
185 // it happens to land on at the initial position.
186 footprint->SetAttributes( footprint->GetAttributes() | FP_JUST_ADDED );
187
188 m_addedFootprints.push_back( footprint );
189 m_commit.Add( footprint );
190
191 msg.Printf( _( "Added %s (footprint '%s')." ),
192 aComponent->GetReference(),
193 aComponent->GetFPID().Format().wx_str() );
194 }
195
198 return footprint;
199}
200
201
203 COMPONENT* aNewComponent )
204{
205 wxString msg;
206
207 if( aNewComponent->GetFPID().empty() )
208 {
209 msg.Printf( _( "Cannot update %s (no footprint assigned)." ),
210 aNewComponent->GetReference(),
211 aNewComponent->GetFPID().Format().wx_str() );
213 ++m_errorCount;
214 return nullptr;
215 }
216
217 FOOTPRINT* newFootprint = m_frame->LoadFootprint( aNewComponent->GetFPID() );
218
219 if( newFootprint == nullptr )
220 {
221 msg.Printf( _( "Cannot update %s (footprint '%s' not found)." ),
222 aNewComponent->GetReference(),
223 aNewComponent->GetFPID().Format().wx_str() );
225 ++m_errorCount;
226 return nullptr;
227 }
228
229 if( m_isDryRun )
230 {
231 msg.Printf( _( "Change %s footprint from '%s' to '%s'."),
232 aFootprint->GetReference(),
233 aFootprint->GetFPID().Format().wx_str(),
234 aNewComponent->GetFPID().Format().wx_str() );
235
236 delete newFootprint;
237 newFootprint = nullptr;
238 }
239 else
240 {
241 m_frame->ExchangeFootprint( aFootprint, newFootprint, m_commit );
242
243 msg.Printf( _( "Changed %s footprint from '%s' to '%s'."),
244 aFootprint->GetReference(),
245 aFootprint->GetFPID().Format().wx_str(),
246 aNewComponent->GetFPID().Format().wx_str() );
247 }
248
251 return newFootprint;
252}
253
254
256 COMPONENT* aNetlistComponent )
257{
258 wxString msg;
259
260 // Create a copy only if the footprint has not been added during this update
261 FOOTPRINT* copy = nullptr;
262
263 if( !m_commit.GetStatus( aPcbFootprint ) )
264 {
265 copy = static_cast<FOOTPRINT*>( aPcbFootprint->Clone() );
266 copy->SetParentGroup( nullptr );
267 }
268
269 bool changed = false;
270
271 // Test for reference designator field change.
272 if( aPcbFootprint->GetReference() != aNetlistComponent->GetReference() )
273 {
274 if( m_isDryRun )
275 {
276 msg.Printf( _( "Change %s reference designator to %s." ),
277 aPcbFootprint->GetReference(),
278 aNetlistComponent->GetReference() );
279 }
280 else
281 {
282 msg.Printf( _( "Changed %s reference designator to %s." ),
283 aPcbFootprint->GetReference(),
284 aNetlistComponent->GetReference() );
285
286 changed = true;
287 aPcbFootprint->SetReference( aNetlistComponent->GetReference() );
288 }
289
291 }
292
293 // Test for value field change.
294 if( aPcbFootprint->GetValue() != aNetlistComponent->GetValue() )
295 {
296 if( m_isDryRun )
297 {
298 msg.Printf( _( "Change %s value from %s to %s." ),
299 aPcbFootprint->GetReference(),
300 aPcbFootprint->GetValue(),
301 aNetlistComponent->GetValue() );
302 }
303 else
304 {
305 msg.Printf( _( "Changed %s value from %s to %s." ),
306 aPcbFootprint->GetReference(),
307 aPcbFootprint->GetValue(),
308 aNetlistComponent->GetValue() );
309
310 changed = true;
311 aPcbFootprint->SetValue( aNetlistComponent->GetValue() );
312 }
313
315 }
316
317 // Test for time stamp change.
318 KIID_PATH new_path = aNetlistComponent->GetPath();
319
320 if( !aNetlistComponent->GetKIIDs().empty() )
321 new_path.push_back( aNetlistComponent->GetKIIDs().front() );
322
323 if( aPcbFootprint->GetPath() != new_path )
324 {
325 if( m_isDryRun )
326 {
327 msg.Printf( _( "Update %s symbol association from %s to %s." ),
328 aPcbFootprint->GetReference(),
329 aPcbFootprint->GetPath().AsString(),
330 new_path.AsString() );
331 }
332 else
333 {
334 msg.Printf( _( "Updated %s symbol association from %s to %s." ),
335 aPcbFootprint->GetReference(),
336 aPcbFootprint->GetPath().AsString(),
337 new_path.AsString() );
338
339 changed = true;
340 aPcbFootprint->SetPath( new_path );
341 }
342
344 }
345
346 if( aPcbFootprint->GetProperties() != aNetlistComponent->GetProperties() )
347 {
348 if( m_isDryRun )
349 {
350 msg.Printf( _( "Update %s properties." ),
351 aPcbFootprint->GetReference() );
352 }
353 else
354 {
355 msg.Printf( _( "Updated %s properties." ),
356 aPcbFootprint->GetReference() );
357
358 changed = true;
359 aPcbFootprint->SetProperties( aNetlistComponent->GetProperties() );
360 }
361
363 }
364
365 if( ( aNetlistComponent->GetProperties().count( wxT( "exclude_from_bom" ) ) > 0 )
366 != ( ( aPcbFootprint->GetAttributes() & FP_EXCLUDE_FROM_BOM ) > 0 ) )
367 {
368 if( m_isDryRun )
369 {
370 if( aNetlistComponent->GetProperties().count( wxT( "exclude_from_bom" ) ) )
371 {
372 msg.Printf( _( "Add %s 'exclude from BOM' fabrication attribute." ),
373 aPcbFootprint->GetReference() );
374 }
375 else
376 {
377 msg.Printf( _( "Remove %s 'exclude from BOM' fabrication attribute." ),
378 aPcbFootprint->GetReference() );
379 }
380 }
381 else
382 {
383 int attributes = aPcbFootprint->GetAttributes();
384
385 if( aNetlistComponent->GetProperties().count( wxT( "exclude_from_bom" ) ) )
386 {
387 attributes |= FP_EXCLUDE_FROM_BOM;
388 msg.Printf( _( "Added %s 'exclude from BOM' fabrication attribute." ),
389 aPcbFootprint->GetReference() );
390 }
391 else
392 {
393 attributes &= ~FP_EXCLUDE_FROM_BOM;
394 msg.Printf( _( "Removed %s 'exclude from BOM' fabrication attribute." ),
395 aPcbFootprint->GetReference() );
396 }
397
398 changed = true;
399 aPcbFootprint->SetAttributes( attributes );
400 }
401
403 }
404
405 if( changed && copy )
406 m_commit.Modified( aPcbFootprint, copy );
407 else if( copy )
408 delete copy;
409
410 return true;
411}
412
413
415 COMPONENT* aNewComponent )
416{
417 wxString msg;
418
419 // Create a copy only if the footprint has not been added during this update
420 FOOTPRINT* copy = nullptr;
421
422 if( m_commit.GetStatus( aFootprint ) )
423 {
424 copy = static_cast<FOOTPRINT*>( aFootprint->Clone() );
425 copy->SetParentGroup( nullptr );
426 }
427
428 bool changed = false;
429
430 // At this point, the component footprint is updated. Now update the nets.
431 for( PAD* pad : aFootprint->Pads() )
432 {
433 const COMPONENT_NET& net = aNewComponent->GetNet( pad->GetNumber() );
434
435 wxString pinFunction;
436 wxString pinType;
437
438 if( net.IsValid() ) // i.e. the pad has a name
439 {
440 pinFunction = net.GetPinFunction();
441 pinType = net.GetPinType();
442 }
443
444 if( !m_isDryRun )
445 {
446 if( pad->GetPinFunction() != pinFunction )
447 {
448 changed = true;
449 pad->SetPinFunction( pinFunction );
450 }
451
452 if( pad->GetPinType() != pinType )
453 {
454 changed = true;
455 pad->SetPinType( pinType );
456 }
457 }
458 else
459 {
460 cachePinFunction( pad, pinFunction );
461 }
462
463 // Test if new footprint pad has no net (pads not on copper layers have no net).
464 if( !net.IsValid() || !pad->IsOnCopperLayer() )
465 {
466 if( !pad->GetNetname().IsEmpty() )
467 {
468 if( m_isDryRun )
469 {
470 msg.Printf( _( "Disconnect %s pin %s." ),
471 aFootprint->GetReference(),
472 pad->GetNumber() );
473 }
474 else
475 {
476 msg.Printf( _( "Disconnected %s pin %s." ),
477 aFootprint->GetReference(),
478 pad->GetNumber() );
479 }
480
482 }
483 else if( pad->IsOnCopperLayer() && !pad->GetNumber().IsEmpty() )
484 {
485 // pad is connectable but has no net found in netlist
486 msg.Printf( _( "No net found for symbol %s pin %s." ),
487 aFootprint->GetReference(),
488 pad->GetNumber() );
490 }
491
492 if( !m_isDryRun )
493 {
494 changed = true;
495 pad->SetNetCode( NETINFO_LIST::UNCONNECTED );
496
497 // If the pad has no net from netlist (i.e. not in netlist
498 // it cannot have a pin function
499 if( pad->GetNetname().IsEmpty() )
500 pad->SetPinFunction( wxEmptyString );
501
502 }
503 else
504 {
505 cacheNetname( pad, wxEmptyString );
506 }
507 }
508 else // New footprint pad has a net.
509 {
510 const wxString& netName = net.GetNetName();
511 NETINFO_ITEM* netinfo = m_board->FindNet( netName );
512
513 if( netinfo && !m_isDryRun )
514 netinfo->SetIsCurrent( true );
515
516 if( pad->GetNetname() != netName )
517 {
518
519 if( netinfo == nullptr )
520 {
521 // It might be a new net that has not been added to the board yet
522 if( m_addedNets.count( netName ) )
523 netinfo = m_addedNets[ netName ];
524 }
525
526 if( netinfo == nullptr )
527 {
528 netinfo = new NETINFO_ITEM( m_board, netName );
529
530 // It is a new net, we have to add it
531 if( !m_isDryRun )
532 {
533 changed = true;
534 m_commit.Add( netinfo );
535 }
536
537 m_addedNets[netName] = netinfo;
538 msg.Printf( _( "Add net %s." ), UnescapeString( netName ) );
540 }
541
542 if( !pad->GetNetname().IsEmpty() )
543 {
544 m_oldToNewNets[ pad->GetNetname() ] = netName;
545
546 if( m_isDryRun )
547 {
548 msg.Printf( _( "Reconnect %s pin %s from %s to %s."),
549 aFootprint->GetReference(),
550 pad->GetNumber(),
551 UnescapeString( pad->GetNetname() ),
552 UnescapeString( netName ) );
553 }
554 else
555 {
556 msg.Printf( _( "Reconnected %s pin %s from %s to %s."),
557 aFootprint->GetReference(),
558 pad->GetNumber(),
559 UnescapeString( pad->GetNetname() ),
560 UnescapeString( netName ) );
561 }
562 }
563 else
564 {
565 if( m_isDryRun )
566 {
567 msg.Printf( _( "Connect %s pin %s to %s."),
568 aFootprint->GetReference(),
569 pad->GetNumber(),
570 UnescapeString( netName ) );
571 }
572 else
573 {
574 msg.Printf( _( "Connected %s pin %s to %s."),
575 aFootprint->GetReference(),
576 pad->GetNumber(),
577 UnescapeString( netName ) );
578 }
579 }
580
582
583 if( !m_isDryRun )
584 {
585 changed = true;
586 pad->SetNet( netinfo );
587 }
588 else
589 {
590 cacheNetname( pad, netName );
591 }
592 }
593 }
594 }
595
596 if( changed && copy )
597 m_commit.Modified( aFootprint, copy );
598 else if( copy )
599 delete copy;
600
601 return true;
602}
603
604
606{
607 for( ZONE* zone : m_board->Zones() )
608 {
609 if( !zone->IsOnCopperLayer() || zone->GetIsRuleArea() )
610 continue;
611
612 m_zoneConnectionsCache[ zone ] = m_board->GetConnectivity()->GetConnectedPads( zone );
613 }
614}
615
616
618{
619 wxString msg;
620 std::set<wxString> netlistNetnames;
621
622 for( int ii = 0; ii < (int) aNetlist.GetCount(); ii++ )
623 {
624 const COMPONENT* component = aNetlist.GetComponent( ii );
625
626 for( unsigned jj = 0; jj < component->GetNetCount(); jj++ )
627 {
628 const COMPONENT_NET& net = component->GetNet( jj );
629 netlistNetnames.insert( net.GetNetName() );
630 }
631 }
632
633 for( PCB_TRACK* via : m_board->Tracks() )
634 {
635 if( via->Type() != PCB_VIA_T )
636 continue;
637
638 if( netlistNetnames.count( via->GetNetname() ) == 0 )
639 {
640 wxString updatedNetname = wxEmptyString;
641
642 // Take via name from name change map if it didn't match to a new pad
643 // (this is useful for stitching vias that don't connect to tracks)
644 if( m_oldToNewNets.count( via->GetNetname() ) )
645 {
646 updatedNetname = m_oldToNewNets[via->GetNetname()];
647 }
648
649 if( !updatedNetname.IsEmpty() )
650 {
651 if( m_isDryRun )
652 {
653 msg.Printf( _( "Reconnect via from %s to %s." ),
654 UnescapeString( via->GetNetname() ),
655 UnescapeString( updatedNetname ) );
656
658 }
659 else
660 {
661 NETINFO_ITEM* netinfo = m_board->FindNet( updatedNetname );
662
663 if( !netinfo )
664 netinfo = m_addedNets[updatedNetname];
665
666 if( netinfo )
667 {
669 via->SetNet( netinfo );
670
671 msg.Printf( _( "Reconnected via from %s to %s." ),
672 UnescapeString( via->GetNetname() ),
673 UnescapeString( updatedNetname ) );
674
676 }
677 }
678 }
679 else
680 {
681 msg.Printf( _( "Via connected to unknown net (%s)." ),
682 UnescapeString( via->GetNetname() ) );
685 }
686 }
687 }
688
689 // Test copper zones to detect "dead" nets (nets without any pad):
690 for( ZONE* zone : m_board->Zones() )
691 {
692 if( !zone->IsOnCopperLayer() || zone->GetIsRuleArea() )
693 continue;
694
695 if( netlistNetnames.count( zone->GetNetname() ) == 0 )
696 {
697 // Look for a pad in the zone's connected-pad-cache which has been updated to
698 // a new net and use that. While this won't always be the right net, the dead
699 // net is guaranteed to be wrong.
700 wxString updatedNetname = wxEmptyString;
701
702 for( PAD* pad : m_zoneConnectionsCache[ zone ] )
703 {
704 if( getNetname( pad ) != zone->GetNetname() )
705 {
706 updatedNetname = getNetname( pad );
707 break;
708 }
709 }
710
711 // Take zone name from name change map if it didn't match to a new pad
712 // (this is useful for zones on internal layers)
713 if( updatedNetname.IsEmpty() && m_oldToNewNets.count( zone->GetNetname() ) )
714 {
715 updatedNetname = m_oldToNewNets[ zone->GetNetname() ];
716 }
717
718 if( !updatedNetname.IsEmpty() )
719 {
720 if( m_isDryRun )
721 {
722 if( !zone->GetZoneName().IsEmpty() )
723 {
724 msg.Printf( _( "Reconnect copper zone '%s' from %s to %s." ),
725 zone->GetZoneName(),
726 UnescapeString( zone->GetNetname() ),
727 UnescapeString( updatedNetname ) );
728 }
729 else
730 {
731 msg.Printf( _( "Reconnect copper zone from %s to %s." ),
732 UnescapeString( zone->GetNetname() ),
733 UnescapeString( updatedNetname ) );
734 }
735
737 }
738 else
739 {
740 NETINFO_ITEM* netinfo = m_board->FindNet( updatedNetname );
741
742 if( !netinfo )
743 netinfo = m_addedNets[ updatedNetname ];
744
745 if( netinfo )
746 {
747 m_commit.Modify( zone );
748 zone->SetNet( netinfo );
749
750 if( !zone->GetZoneName().IsEmpty() )
751 {
752 msg.Printf( _( "Reconnected copper zone '%s' from %s to %s." ),
753 zone->GetZoneName(),
754 UnescapeString( zone->GetNetname() ),
755 UnescapeString( updatedNetname ) );
756 }
757 else
758 {
759 msg.Printf( _( "Reconnected copper zone from %s to %s." ),
760 UnescapeString( zone->GetNetname() ),
761 UnescapeString( updatedNetname ) );
762 }
763
765 }
766 }
767 }
768 else
769 {
770 if( !zone->GetZoneName().IsEmpty() )
771 {
772 msg.Printf( _( "Copper zone '%s' has no pads connected." ),
773 zone->GetZoneName() );
774 }
775 else
776 {
777 PCB_LAYER_ID layer = zone->GetLayer();
778 VECTOR2I pos = zone->GetPosition();
779
780 msg.Printf( _( "Copper zone on layer %s at (%s, %s) has no pads connected." ),
781 m_board->GetLayerName( layer ),
784 }
785
788 }
789 }
790 }
791
792 return true;
793}
794
795
797 std::map<COMPONENT*, FOOTPRINT*>& aFootprintMap )
798{
799 // Verify that board contains all pads in netlist: if it doesn't then footprints are
800 // wrong or missing.
801
802 wxString msg;
803 wxString padNumber;
804
805 for( int i = 0; i < (int) aNetlist.GetCount(); i++ )
806 {
807 COMPONENT* component = aNetlist.GetComponent( i );
808 FOOTPRINT* footprint = aFootprintMap[component];
809
810 if( !footprint ) // It can be missing in partial designs
811 continue;
812
813 // Explore all pins/pads in component
814 for( unsigned jj = 0; jj < component->GetNetCount(); jj++ )
815 {
816 padNumber = component->GetNet( jj ).GetPinName();
817
818 if( padNumber.IsEmpty() )
819 {
820 // bad symbol, report error
821 msg.Printf( _( "Symbol %s has pins with no number. These pins can not be matched "
822 "to pads in %s." ),
823 component->GetReference(),
824 footprint->GetFPID().Format().wx_str() );
826 ++m_errorCount;
827 }
828 else if( !footprint->FindPadByNumber( padNumber ) )
829 {
830 // not found: bad footprint, report error
831 msg.Printf( _( "%s pad %s not found in %s." ),
832 component->GetReference(),
833 padNumber,
834 footprint->GetFPID().Format().wx_str() );
836 ++m_errorCount;
837 }
838 }
839 }
840
841 return true;
842}
843
844
846{
847 FOOTPRINT* lastPreexistingFootprint = nullptr;
848 COMPONENT* component = nullptr;
849 wxString msg;
850
851 m_errorCount = 0;
852 m_warningCount = 0;
854
855 std::map<COMPONENT*, FOOTPRINT*> footprintMap;
856
857 if( !m_board->Footprints().empty() )
858 lastPreexistingFootprint = m_board->Footprints().back();
859
861
862 // First mark all nets (except <no net>) as stale; we'll update those which are current
863 // in the following two loops.
864 //
865 if( !m_isDryRun )
866 {
867 m_board->SetStatus( 0 );
868
869 for( NETINFO_ITEM* net : m_board->GetNetInfo() )
870 net->SetIsCurrent( net->GetNetCode() == 0 );
871 }
872
873 // Next go through the netlist updating all board footprints which have matching component
874 // entries and adding new footprints for those that don't.
875 //
876 for( unsigned i = 0; i < aNetlist.GetCount(); i++ )
877 {
878 component = aNetlist.GetComponent( i );
879
880 if( component->GetProperties().count( wxT( "exclude_from_board" ) ) )
881 continue;
882
883 msg.Printf( _( "Processing symbol '%s:%s'." ),
884 component->GetReference(),
885 component->GetFPID().Format().wx_str() );
887
888 int matchCount = 0;
889
890 for( FOOTPRINT* footprint : m_board->Footprints() )
891 {
892 bool match = false;
893
895 {
896 for( const KIID& uuid : component->GetKIIDs() )
897 {
898 KIID_PATH base = component->GetPath();
899 base.push_back( uuid );
900
901 if( footprint->GetPath() == base )
902 {
903 match = true;
904 break;
905 }
906 }
907 }
908 else
909 {
910 match = footprint->GetReference().CmpNoCase( component->GetReference() ) == 0;
911 }
912
913 if( match )
914 {
915 FOOTPRINT* tmp = footprint;
916
917 if( m_replaceFootprints && component->GetFPID() != footprint->GetFPID() )
918 tmp = replaceFootprint( aNetlist, footprint, component );
919
920 if( tmp )
921 {
922 footprintMap[ component ] = tmp;
923
924 updateFootprintParameters( tmp, component );
925 updateComponentPadConnections( tmp, component );
926 }
927
928 matchCount++;
929 }
930
931 if( footprint == lastPreexistingFootprint )
932 {
933 // No sense going through the newly-created footprints: end of loop
934 break;
935 }
936 }
937
938 if( matchCount == 0 )
939 {
940 FOOTPRINT* footprint = addNewFootprint( component );
941
942 if( footprint )
943 {
944 footprintMap[ component ] = footprint;
945
946 updateFootprintParameters( footprint, component );
947 updateComponentPadConnections( footprint, component );
948 }
949 }
950 else if( matchCount > 1 )
951 {
952 msg.Printf( _( "Multiple footprints found for '%s'." ), component->GetReference() );
954 m_errorCount++;
955 }
956 }
957
958 updateCopperZoneNets( aNetlist );
959
960 // Finally go through the board footprints and update all those that *don't* have matching
961 // component entries.
962 //
963 for( FOOTPRINT* footprint : m_board->Footprints() )
964 {
965 bool matched = false;
966 bool doDelete = m_deleteUnusedFootprints;
967
968 if( ( footprint->GetAttributes() & FP_BOARD_ONLY ) > 0 )
969 doDelete = false;
970
972 component = aNetlist.GetComponentByPath( footprint->GetPath() );
973 else
974 component = aNetlist.GetComponentByReference( footprint->GetReference() );
975
976 if( component && component->GetProperties().count( wxT( "exclude_from_board" ) ) == 0 )
977 matched = true;
978
979 if( doDelete && !matched && footprint->IsLocked() )
980 {
981 if( m_isDryRun )
982 {
983 msg.Printf( _( "Cannot remove unused footprint %s (locked)." ),
984 footprint->GetReference() );
985 }
986 else
987 {
988 msg.Printf( _( "Could not remove unused footprint %s (locked)." ),
989 footprint->GetReference() );
990 }
991
993 m_errorCount++;
994 doDelete = false;
995 }
996
997 if( doDelete && !matched )
998 {
999 if( m_isDryRun )
1000 {
1001 msg.Printf( _( "Remove unused footprint %s." ), footprint->GetReference() );
1002 }
1003 else
1004 {
1005 m_commit.Remove( footprint );
1006 msg.Printf( _( "Removed unused footprint %s." ), footprint->GetReference() );
1007 }
1008
1010 }
1011 else if( !m_isDryRun )
1012 {
1013 if( !matched )
1014 footprint->SetPath( KIID_PATH() );
1015
1016 for( PAD* pad : footprint->Pads() )
1017 {
1018 if( pad->GetNet() )
1019 pad->GetNet()->SetIsCurrent( true );
1020 }
1021 }
1022 }
1023
1024 if( !m_isDryRun )
1025 {
1027 testConnectivity( aNetlist, footprintMap );
1028
1029 for( NETINFO_ITEM* net : m_board->GetNetInfo() )
1030 {
1031 if( !net->IsCurrent() )
1032 {
1033 msg.Printf( _( "Removed unused net %s." ), net->GetNetname() );
1035 m_commit.Removed( net );
1036 }
1037 }
1038
1040
1041 // When new footprints are added, the automatic zone refill is disabled because:
1042 // * it creates crashes when calculating dynamic ratsnests if auto refill is enabled.
1043 // (the auto refills rebuild the connectivity with incomplete data)
1044 // * it is useless because zones will be refilled after placing new footprints
1045 m_commit.Push( _( "Update netlist" ), m_newFootprintsCount ? ZONE_FILL_OP : 0 );
1046
1049 }
1050
1051 if( m_isDryRun )
1052 {
1053 for( const std::pair<const wxString, NETINFO_ITEM*>& addedNet : m_addedNets )
1054 delete addedNet.second;
1055
1056 m_addedNets.clear();
1057 }
1058
1059 // Update the ratsnest
1062
1063 msg.Printf( _( "Total warnings: %d, errors: %d." ), m_warningCount, m_errorCount );
1065
1066 return true;
1067}
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:109
#define ZONE_FILL_OP
Definition: board_commit.h:43
virtual void Push(const wxString &aMessage=wxT("A commit"), int aCommitFlags=0) override
Revert the commit by restoring the modified items state.
std::map< PAD *, wxString > m_padNets
void cacheNetname(PAD *aPad, const wxString &aNetname)
bool UpdateNetlist(NETLIST &aNetlist)
Update the board's components according to the new netlist.
wxString getNetname(PAD *aPad)
wxString getPinFunction(PAD *aPad)
std::vector< FOOTPRINT * > m_addedFootprints
std::map< wxString, wxString > m_oldToNewNets
bool updateFootprintParameters(FOOTPRINT *aPcbFootprint, COMPONENT *aNetlistComponent)
std::map< wxString, NETINFO_ITEM * > m_addedNets
bool testConnectivity(NETLIST &aNetlist, std::map< COMPONENT *, FOOTPRINT * > &aFootprintMap)
bool updateCopperZoneNets(NETLIST &aNetlist)
void cachePinFunction(PAD *aPad, const wxString &aPinFunction)
BOARD_NETLIST_UPDATER(PCB_EDIT_FRAME *aFrame, BOARD *aBoard)
bool updateComponentPadConnections(FOOTPRINT *aFootprint, COMPONENT *aNewComponent)
std::map< PAD *, wxString > m_padPinFunctions
std::map< ZONE *, std::vector< PAD * > > m_zoneConnectionsCache
FOOTPRINT * replaceFootprint(NETLIST &aNetlist, FOOTPRINT *aFootprint, COMPONENT *aNewComponent)
FOOTPRINT * addNewFootprint(COMPONENT *aComponent)
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:265
const NETINFO_LIST & GetNetInfo() const
Definition: board.h:765
const BOX2I GetBoardEdgesBoundingBox() const
Return the board bounding box calculated using exclusively the board edges (graphics on Edge....
Definition: board.h:823
const PAGE_INFO & GetPageSettings() const
Definition: board.h:626
ZONES & Zones()
Definition: board.h:313
NETINFO_ITEM * FindNet(int aNetcode) const
Search for a net with the given netcode.
Definition: board.cpp:1456
bool BuildConnectivity(PROGRESS_REPORTER *aReporter=nullptr)
Build or rebuild the board connectivity database for the board, especially the list of connected item...
Definition: board.cpp:162
void SynchronizeNetsAndNetClasses(bool aResetTrackAndViaSizes)
Copy NETCLASS info to each NET, based on NET membership in a NETCLASS.
Definition: board.cpp:1524
FOOTPRINTS & Footprints()
Definition: board.h:307
TRACKS & Tracks()
Definition: board.h:304
const wxString GetLayerName(PCB_LAYER_ID aLayer) const
Return the name of a aLayer.
Definition: board.cpp:452
bool IsEmpty() const
Definition: board.h:355
std::shared_ptr< CONNECTIVITY_DATA > GetConnectivity() const
Return a list of missing connections between components/tracks.
Definition: board.h:424
coord_type GetHeight() const
Definition: box2.h:188
coord_type GetWidth() const
Definition: box2.h:187
Vec Centre() const
Definition: box2.h:70
coord_type GetBottom() const
Definition: box2.h:190
COMMIT & Modify(EDA_ITEM *aItem)
Create an undo entry for an item that has been already modified.
Definition: commit.h:103
COMMIT & Modified(EDA_ITEM *aItem, EDA_ITEM *aCopy)
Definition: commit.h:110
COMMIT & Removed(EDA_ITEM *aItem)
Modify a given item in the model.
Definition: commit.h:96
COMMIT & Remove(EDA_ITEM *aItem)
Notify observers that aItem has been removed.
Definition: commit.h:90
int GetStatus(EDA_ITEM *aItem)
Definition: commit.cpp:123
COMMIT & Add(EDA_ITEM *aItem)
Notify observers that aItem has been added.
Definition: commit.h:78
Used to store the component pin name to net name (and pin function) associations stored in a netlist.
Definition: pcb_netlist.h:44
const wxString & GetNetName() const
Definition: pcb_netlist.h:58
bool IsValid() const
Definition: pcb_netlist.h:62
const wxString & GetPinFunction() const
Definition: pcb_netlist.h:59
const wxString & GetPinName() const
Definition: pcb_netlist.h:57
const wxString & GetPinType() const
Definition: pcb_netlist.h:60
Store all of the related footprint information found in a netlist.
Definition: pcb_netlist.h:85
const COMPONENT_NET & GetNet(unsigned aIndex) const
Definition: pcb_netlist.h:111
const KIID_PATH & GetPath() const
Definition: pcb_netlist.h:143
const wxString & GetReference() const
Definition: pcb_netlist.h:126
const wxString & GetValue() const
Definition: pcb_netlist.h:129
const std::map< wxString, wxString > & GetProperties() const
Definition: pcb_netlist.h:135
const std::vector< KIID > & GetKIIDs() const
Definition: pcb_netlist.h:145
const LIB_ID & GetFPID() const
Definition: pcb_netlist.h:138
unsigned GetNetCount() const
Definition: pcb_netlist.h:109
void SetStatus(EDA_ITEM_FLAGS aStatus)
Definition: eda_item.h:137
virtual void SetParent(EDA_ITEM *aParent)
Definition: eda_item.h:100
void SetPosition(const VECTOR2I &aPos) override
Definition: footprint.cpp:1670
void SetPath(const KIID_PATH &aPath)
Definition: footprint.h:225
void SetAttributes(int aAttributes)
Definition: footprint.h:251
EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
Definition: footprint.cpp:1373
int GetAttributes() const
Definition: footprint.h:250
PADS & Pads()
Definition: footprint.h:170
const LIB_ID & GetFPID() const
Definition: footprint.h:212
void SetReference(const wxString &aReference)
Definition: footprint.h:528
void SetValue(const wxString &aValue)
Definition: footprint.h:555
const wxString & GetValue() const
Definition: footprint.h:547
const wxString & GetReference() const
Definition: footprint.h:519
void SetProperties(const std::map< wxString, wxString > &aProps)
Definition: footprint.h:575
const KIID_PATH & GetPath() const
Definition: footprint.h:224
PAD * FindPadByNumber(const wxString &aPadNumber, PAD *aSearchAfterMe=nullptr) const
Return a PAD with a matching number.
Definition: footprint.cpp:1174
const std::map< wxString, wxString > & GetProperties() const
Definition: footprint.h:574
wxString AsString() const
Definition: kiid.cpp:359
Definition: kiid.h:48
bool empty() const
Definition: lib_id.h:193
UTF8 Format() const
Definition: lib_id.cpp:117
Handle the data for a net.
Definition: netinfo.h:67
void SetIsCurrent(bool isCurrent)
Definition: netinfo.h:158
static const int UNCONNECTED
Constant that forces initialization of a netinfo item to the NETINFO_ITEM ORPHANED (typically -1) whe...
Definition: netinfo.h:382
void RemoveUnusedNets()
Store information read from a netlist along with the flags used to update the NETLIST in the BOARD.
Definition: pcb_netlist.h:213
unsigned GetCount() const
Definition: pcb_netlist.h:234
COMPONENT * GetComponentByPath(const KIID_PATH &aPath)
Return a COMPONENT by aPath.
COMPONENT * GetComponentByReference(const wxString &aReference)
Return a COMPONENT by aReference.
COMPONENT * GetComponent(unsigned aIndex)
Return the COMPONENT at aIndex.
Definition: pcb_netlist.h:242
static REPORTER & GetInstance()
Definition: reporter.cpp:117
Definition: pad.h:59
const wxString & GetPinFunction() const
Definition: pad.h:146
const wxSize GetSizeIU(double aIUScale) const
Gets the page size in internal units.
Definition: page_info.h:162
DISPLAY_OPTIONS m_Display
PCBNEW_SETTINGS * GetPcbNewSettings() const
FOOTPRINT * LoadFootprint(const LIB_ID &aFootprintId)
Attempt to load aFootprintId from the footprint library table.
The main frame for Pcbnew.
void SaveProjectSettings() override
Save changes to the project settings to the project (.pro) file.
void ExchangeFootprint(FOOTPRINT *aExisting, FOOTPRINT *aNew, BOARD_COMMIT &aCommit, bool deleteExtraTexts=true, bool resetTextLayers=true, bool resetTextEffects=true, bool resetFabricationAttrs=true, bool reset3DModels=true, bool *aUpdated=nullptr)
Replace aExisting footprint by aNew footprint using the Existing footprint settings (position,...
virtual REPORTER & ReportTail(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Places the report at the end of the list, for objects that support report ordering.
Definition: reporter.h:99
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)=0
Report a string with a given severity.
wxString MessageTextFromValue(double aValue, bool aAddUnitLabel=true, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
A lower-precision version of StringFromValue().
wxString wx_str() const
Definition: utf8.cpp:46
Handle a list of polygons defining a copper zone.
Definition: zone.h:57
The common library.
#define _(s)
@ FP_BOARD_ONLY
Definition: footprint.h:72
@ FP_EXCLUDE_FROM_BOM
Definition: footprint.h:71
@ FP_JUST_ADDED
Definition: footprint.h:73
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:59
@ RPT_SEVERITY_WARNING
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_INFO
@ RPT_SEVERITY_ACTION
wxString UnescapeString(const wxString &aSource)
const double IU_PER_MILS
Definition: base_units.h:78
constexpr int mmToIU(double mm) const
Definition: base_units.h:89
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:102