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-2022 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 = m_commit.GetStatus( aPcbFootprint ) ? nullptr
262 : (FOOTPRINT*) aPcbFootprint->Clone();
263 bool changed = false;
264
265 // Test for reference designator field change.
266 if( aPcbFootprint->GetReference() != aNetlistComponent->GetReference() )
267 {
268 if( m_isDryRun )
269 {
270 msg.Printf( _( "Change %s reference designator to %s." ),
271 aPcbFootprint->GetReference(),
272 aNetlistComponent->GetReference() );
273 }
274 else
275 {
276 msg.Printf( _( "Changed %s reference designator to %s." ),
277 aPcbFootprint->GetReference(),
278 aNetlistComponent->GetReference() );
279
280 changed = true;
281 aPcbFootprint->SetReference( aNetlistComponent->GetReference() );
282 }
283
285 }
286
287 // Test for value field change.
288 if( aPcbFootprint->GetValue() != aNetlistComponent->GetValue() )
289 {
290 if( m_isDryRun )
291 {
292 msg.Printf( _( "Change %s value from %s to %s." ),
293 aPcbFootprint->GetReference(),
294 aPcbFootprint->GetValue(),
295 aNetlistComponent->GetValue() );
296 }
297 else
298 {
299 msg.Printf( _( "Changed %s value from %s to %s." ),
300 aPcbFootprint->GetReference(),
301 aPcbFootprint->GetValue(),
302 aNetlistComponent->GetValue() );
303
304 changed = true;
305 aPcbFootprint->SetValue( aNetlistComponent->GetValue() );
306 }
307
309 }
310
311 // Test for time stamp change.
312 KIID_PATH new_path = aNetlistComponent->GetPath();
313
314 if( !aNetlistComponent->GetKIIDs().empty() )
315 new_path.push_back( aNetlistComponent->GetKIIDs().front() );
316
317 if( aPcbFootprint->GetPath() != new_path )
318 {
319 if( m_isDryRun )
320 {
321 msg.Printf( _( "Update %s symbol association from %s to %s." ),
322 aPcbFootprint->GetReference(),
323 aPcbFootprint->GetPath().AsString(),
324 new_path.AsString() );
325 }
326 else
327 {
328 msg.Printf( _( "Updated %s symbol association from %s to %s." ),
329 aPcbFootprint->GetReference(),
330 aPcbFootprint->GetPath().AsString(),
331 new_path.AsString() );
332
333 changed = true;
334 aPcbFootprint->SetPath( new_path );
335 }
336
338 }
339
340 if( aPcbFootprint->GetProperties() != aNetlistComponent->GetProperties() )
341 {
342 if( m_isDryRun )
343 {
344 msg.Printf( _( "Update %s properties." ),
345 aPcbFootprint->GetReference() );
346 }
347 else
348 {
349 msg.Printf( _( "Updated %s properties." ),
350 aPcbFootprint->GetReference() );
351
352 changed = true;
353 aPcbFootprint->SetProperties( aNetlistComponent->GetProperties() );
354 }
355
357 }
358
359 if( ( aNetlistComponent->GetProperties().count( wxT( "exclude_from_bom" ) ) > 0 )
360 != ( ( aPcbFootprint->GetAttributes() & FP_EXCLUDE_FROM_BOM ) > 0 ) )
361 {
362 if( m_isDryRun )
363 {
364 if( aNetlistComponent->GetProperties().count( wxT( "exclude_from_bom" ) ) )
365 {
366 msg.Printf( _( "Add %s 'exclude from BOM' fabrication attribute." ),
367 aPcbFootprint->GetReference() );
368 }
369 else
370 {
371 msg.Printf( _( "Remove %s 'exclude from BOM' fabrication attribute." ),
372 aPcbFootprint->GetReference() );
373 }
374 }
375 else
376 {
377 int attributes = aPcbFootprint->GetAttributes();
378
379 if( aNetlistComponent->GetProperties().count( wxT( "exclude_from_bom" ) ) )
380 {
381 attributes |= FP_EXCLUDE_FROM_BOM;
382 msg.Printf( _( "Added %s 'exclude from BOM' fabrication attribute." ),
383 aPcbFootprint->GetReference() );
384 }
385 else
386 {
387 attributes &= ~FP_EXCLUDE_FROM_BOM;
388 msg.Printf( _( "Removed %s 'exclude from BOM' fabrication attribute." ),
389 aPcbFootprint->GetReference() );
390 }
391
392 changed = true;
393 aPcbFootprint->SetAttributes( attributes );
394 }
395
397 }
398
399 if( ( aNetlistComponent->GetProperties().count( wxT( "dnp" ) ) > 0 )
400 != ( ( aPcbFootprint->GetAttributes() & FP_EXCLUDE_FROM_POS_FILES ) > 0 ) )
401 {
402 if( m_isDryRun )
403 {
404 if( aNetlistComponent->GetProperties().count( wxT( "dnp" ) ) )
405 {
406 msg.Printf( _( "Add %s 'exclude from position files' fabrication attribute." ),
407 aPcbFootprint->GetReference() );
408 }
409 else
410 {
411 msg.Printf( _( "Remove %s 'exclude from position files' fabrication attribute." ),
412 aPcbFootprint->GetReference() );
413 }
414 }
415 else
416 {
417 int attributes = aPcbFootprint->GetAttributes();
418
419 if( aNetlistComponent->GetProperties().count( wxT( "dnp" ) ) )
420 {
421 attributes |= FP_EXCLUDE_FROM_POS_FILES;
422 msg.Printf( _( "Added %s 'exclude from position files' fabrication attribute." ),
423 aPcbFootprint->GetReference() );
424 }
425 else
426 {
427 attributes &= ~FP_EXCLUDE_FROM_POS_FILES;
428 msg.Printf( _( "Removed %s 'exclude from position files' fabrication attribute." ),
429 aPcbFootprint->GetReference() );
430 }
431
432 changed = true;
433 aPcbFootprint->SetAttributes( attributes );
434 }
435
437 }
438
439 if( changed && copy )
440 m_commit.Modified( aPcbFootprint, copy );
441 else
442 delete copy;
443
444 return true;
445}
446
447
449 COMPONENT* aNewComponent )
450{
451 wxString msg;
452
453 // Create a copy only if the footprint has not been added during this update
454 FOOTPRINT* copy = m_commit.GetStatus( aFootprint ) ? nullptr : (FOOTPRINT*) aFootprint->Clone();
455 bool changed = false;
456
457 // At this point, the component footprint is updated. Now update the nets.
458 for( PAD* pad : aFootprint->Pads() )
459 {
460 const COMPONENT_NET& net = aNewComponent->GetNet( pad->GetNumber() );
461
462 wxString pinFunction;
463 wxString pinType;
464
465 if( net.IsValid() ) // i.e. the pad has a name
466 {
467 pinFunction = net.GetPinFunction();
468 pinType = net.GetPinType();
469 }
470
471 if( !m_isDryRun )
472 {
473 if( pad->GetPinFunction() != pinFunction )
474 {
475 changed = true;
476 pad->SetPinFunction( pinFunction );
477 }
478
479 if( pad->GetPinType() != pinType )
480 {
481 changed = true;
482 pad->SetPinType( pinType );
483 }
484 }
485 else
486 {
487 cachePinFunction( pad, pinFunction );
488 }
489
490 // Test if new footprint pad has no net (pads not on copper layers have no net).
491 if( !net.IsValid() || !pad->IsOnCopperLayer() )
492 {
493 if( !pad->GetNetname().IsEmpty() )
494 {
495 if( m_isDryRun )
496 {
497 msg.Printf( _( "Disconnect %s pin %s." ),
498 aFootprint->GetReference(),
499 pad->GetNumber() );
500 }
501 else
502 {
503 msg.Printf( _( "Disconnected %s pin %s." ),
504 aFootprint->GetReference(),
505 pad->GetNumber() );
506 }
507
509 }
510 else if( pad->IsOnCopperLayer() && !pad->GetNumber().IsEmpty() )
511 {
512 // pad is connectable but has no net found in netlist
513 msg.Printf( _( "No net found for symbol %s pin %s." ),
514 aFootprint->GetReference(),
515 pad->GetNumber() );
517 }
518
519 if( !m_isDryRun )
520 {
521 changed = true;
522 pad->SetNetCode( NETINFO_LIST::UNCONNECTED );
523
524 // If the pad has no net from netlist (i.e. not in netlist
525 // it cannot have a pin function
526 if( pad->GetNetname().IsEmpty() )
527 pad->SetPinFunction( wxEmptyString );
528
529 }
530 else
531 {
532 cacheNetname( pad, wxEmptyString );
533 }
534 }
535 else // New footprint pad has a net.
536 {
537 const wxString& netName = net.GetNetName();
538 NETINFO_ITEM* netinfo = m_board->FindNet( netName );
539
540 if( netinfo && !m_isDryRun )
541 netinfo->SetIsCurrent( true );
542
543 if( pad->GetNetname() != netName )
544 {
545
546 if( netinfo == nullptr )
547 {
548 // It might be a new net that has not been added to the board yet
549 if( m_addedNets.count( netName ) )
550 netinfo = m_addedNets[ netName ];
551 }
552
553 if( netinfo == nullptr )
554 {
555 netinfo = new NETINFO_ITEM( m_board, netName );
556
557 // It is a new net, we have to add it
558 if( !m_isDryRun )
559 {
560 changed = true;
561 m_commit.Add( netinfo );
562 }
563
564 m_addedNets[netName] = netinfo;
565 msg.Printf( _( "Add net %s." ), UnescapeString( netName ) );
567 }
568
569 if( !pad->GetNetname().IsEmpty() )
570 {
571 m_oldToNewNets[ pad->GetNetname() ] = netName;
572
573 if( m_isDryRun )
574 {
575 msg.Printf( _( "Reconnect %s pin %s from %s to %s."),
576 aFootprint->GetReference(),
577 pad->GetNumber(),
578 UnescapeString( pad->GetNetname() ),
579 UnescapeString( netName ) );
580 }
581 else
582 {
583 msg.Printf( _( "Reconnected %s pin %s from %s to %s."),
584 aFootprint->GetReference(),
585 pad->GetNumber(),
586 UnescapeString( pad->GetNetname() ),
587 UnescapeString( netName ) );
588 }
589 }
590 else
591 {
592 if( m_isDryRun )
593 {
594 msg.Printf( _( "Connect %s pin %s to %s."),
595 aFootprint->GetReference(),
596 pad->GetNumber(),
597 UnescapeString( netName ) );
598 }
599 else
600 {
601 msg.Printf( _( "Connected %s pin %s to %s."),
602 aFootprint->GetReference(),
603 pad->GetNumber(),
604 UnescapeString( netName ) );
605 }
606 }
607
609
610 if( !m_isDryRun )
611 {
612 changed = true;
613 pad->SetNet( netinfo );
614 }
615 else
616 {
617 cacheNetname( pad, netName );
618 }
619 }
620 }
621 }
622
623 if( changed && copy )
624 m_commit.Modified( aFootprint, copy );
625 else
626 delete copy;
627
628 return true;
629}
630
631
633{
634 for( ZONE* zone : m_board->Zones() )
635 {
636 if( !zone->IsOnCopperLayer() || zone->GetIsRuleArea() )
637 continue;
638
639 m_zoneConnectionsCache[ zone ] = m_board->GetConnectivity()->GetConnectedPads( zone );
640 }
641}
642
643
645{
646 wxString msg;
647 std::set<wxString> netlistNetnames;
648
649 for( int ii = 0; ii < (int) aNetlist.GetCount(); ii++ )
650 {
651 const COMPONENT* component = aNetlist.GetComponent( ii );
652
653 for( unsigned jj = 0; jj < component->GetNetCount(); jj++ )
654 {
655 const COMPONENT_NET& net = component->GetNet( jj );
656 netlistNetnames.insert( net.GetNetName() );
657 }
658 }
659
660 for( PCB_TRACK* via : m_board->Tracks() )
661 {
662 if( via->Type() != PCB_VIA_T )
663 continue;
664
665 if( netlistNetnames.count( via->GetNetname() ) == 0 )
666 {
667 wxString updatedNetname = wxEmptyString;
668
669 // Take via name from name change map if it didn't match to a new pad
670 // (this is useful for stitching vias that don't connect to tracks)
671 if( m_oldToNewNets.count( via->GetNetname() ) )
672 {
673 updatedNetname = m_oldToNewNets[via->GetNetname()];
674 }
675
676 if( !updatedNetname.IsEmpty() )
677 {
678 if( m_isDryRun )
679 {
680 msg.Printf( _( "Reconnect via from %s to %s." ),
681 UnescapeString( via->GetNetname() ),
682 UnescapeString( updatedNetname ) );
683
685 }
686 else
687 {
688 NETINFO_ITEM* netinfo = m_board->FindNet( updatedNetname );
689
690 if( !netinfo )
691 netinfo = m_addedNets[updatedNetname];
692
693 if( netinfo )
694 {
696 via->SetNet( netinfo );
697
698 msg.Printf( _( "Reconnected via from %s to %s." ),
699 UnescapeString( via->GetNetname() ),
700 UnescapeString( updatedNetname ) );
701
703 }
704 }
705 }
706 else
707 {
708 msg.Printf( _( "Via connected to unknown net (%s)." ),
709 UnescapeString( via->GetNetname() ) );
712 }
713 }
714 }
715
716 // Test copper zones to detect "dead" nets (nets without any pad):
717 for( ZONE* zone : m_board->Zones() )
718 {
719 if( !zone->IsOnCopperLayer() || zone->GetIsRuleArea() )
720 continue;
721
722 if( netlistNetnames.count( zone->GetNetname() ) == 0 )
723 {
724 // Look for a pad in the zone's connected-pad-cache which has been updated to
725 // a new net and use that. While this won't always be the right net, the dead
726 // net is guaranteed to be wrong.
727 wxString updatedNetname = wxEmptyString;
728
729 for( PAD* pad : m_zoneConnectionsCache[ zone ] )
730 {
731 if( getNetname( pad ) != zone->GetNetname() )
732 {
733 updatedNetname = getNetname( pad );
734 break;
735 }
736 }
737
738 // Take zone name from name change map if it didn't match to a new pad
739 // (this is useful for zones on internal layers)
740 if( updatedNetname.IsEmpty() && m_oldToNewNets.count( zone->GetNetname() ) )
741 {
742 updatedNetname = m_oldToNewNets[ zone->GetNetname() ];
743 }
744
745 if( !updatedNetname.IsEmpty() )
746 {
747 if( m_isDryRun )
748 {
749 if( !zone->GetZoneName().IsEmpty() )
750 {
751 msg.Printf( _( "Reconnect copper zone '%s' from %s to %s." ),
752 zone->GetZoneName(),
753 UnescapeString( zone->GetNetname() ),
754 UnescapeString( updatedNetname ) );
755 }
756 else
757 {
758 msg.Printf( _( "Reconnect copper zone from %s to %s." ),
759 UnescapeString( zone->GetNetname() ),
760 UnescapeString( updatedNetname ) );
761 }
762
764 }
765 else
766 {
767 NETINFO_ITEM* netinfo = m_board->FindNet( updatedNetname );
768
769 if( !netinfo )
770 netinfo = m_addedNets[ updatedNetname ];
771
772 if( netinfo )
773 {
774 m_commit.Modify( zone );
775 zone->SetNet( netinfo );
776
777 if( !zone->GetZoneName().IsEmpty() )
778 {
779 msg.Printf( _( "Reconnected copper zone '%s' from %s to %s." ),
780 zone->GetZoneName(),
781 UnescapeString( zone->GetNetname() ),
782 UnescapeString( updatedNetname ) );
783 }
784 else
785 {
786 msg.Printf( _( "Reconnected copper zone from %s to %s." ),
787 UnescapeString( zone->GetNetname() ),
788 UnescapeString( updatedNetname ) );
789 }
790
792 }
793 }
794 }
795 else
796 {
797 if( !zone->GetZoneName().IsEmpty() )
798 {
799 msg.Printf( _( "Copper zone '%s' has no pads connected." ),
800 zone->GetZoneName() );
801 }
802 else
803 {
804 PCB_LAYER_ID layer = zone->GetLayer();
805 VECTOR2I pos = zone->GetPosition();
806
807 msg.Printf( _( "Copper zone on layer %s at (%s, %s) has no pads connected." ),
808 m_board->GetLayerName( layer ),
811 }
812
815 }
816 }
817 }
818
819 return true;
820}
821
822
824 std::map<COMPONENT*, FOOTPRINT*>& aFootprintMap )
825{
826 // Verify that board contains all pads in netlist: if it doesn't then footprints are
827 // wrong or missing.
828
829 wxString msg;
830 wxString padNumber;
831
832 for( int i = 0; i < (int) aNetlist.GetCount(); i++ )
833 {
834 COMPONENT* component = aNetlist.GetComponent( i );
835 FOOTPRINT* footprint = aFootprintMap[component];
836
837 if( !footprint ) // It can be missing in partial designs
838 continue;
839
840 // Explore all pins/pads in component
841 for( unsigned jj = 0; jj < component->GetNetCount(); jj++ )
842 {
843 padNumber = component->GetNet( jj ).GetPinName();
844
845 if( footprint->FindPadByNumber( padNumber ) )
846 continue; // OK, pad found
847
848 // not found: bad footprint, report error
849 msg.Printf( _( "%s pad %s not found in %s." ),
850 component->GetReference(),
851 padNumber,
852 footprint->GetFPID().Format().wx_str() );
854 ++m_errorCount;
855 }
856 }
857
858 return true;
859}
860
861
863{
864 FOOTPRINT* lastPreexistingFootprint = nullptr;
865 COMPONENT* component = nullptr;
866 wxString msg;
867
868 m_errorCount = 0;
869 m_warningCount = 0;
871
872 std::map<COMPONENT*, FOOTPRINT*> footprintMap;
873
874 if( !m_board->Footprints().empty() )
875 lastPreexistingFootprint = m_board->Footprints().back();
876
878
879 // First mark all nets (except <no net>) as stale; we'll update those which are current
880 // in the following two loops.
881 //
882 if( !m_isDryRun )
883 {
884 m_board->SetStatus( 0 );
885
886 for( NETINFO_ITEM* net : m_board->GetNetInfo() )
887 net->SetIsCurrent( net->GetNetCode() == 0 );
888 }
889
890 // Next go through the netlist updating all board footprints which have matching component
891 // entries and adding new footprints for those that don't.
892 //
893 for( unsigned i = 0; i < aNetlist.GetCount(); i++ )
894 {
895 component = aNetlist.GetComponent( i );
896
897 if( component->GetProperties().count( wxT( "exclude_from_board" ) ) )
898 continue;
899
900 msg.Printf( _( "Processing symbol '%s:%s'." ),
901 component->GetReference(),
902 component->GetFPID().Format().wx_str() );
904
905 int matchCount = 0;
906
907 for( FOOTPRINT* footprint : m_board->Footprints() )
908 {
909 bool match = false;
910
912 {
913 for( const KIID& uuid : component->GetKIIDs() )
914 {
915 KIID_PATH base = component->GetPath();
916 base.push_back( uuid );
917
918 if( footprint->GetPath() == base )
919 {
920 match = true;
921 break;
922 }
923 }
924 }
925 else
926 {
927 match = footprint->GetReference().CmpNoCase( component->GetReference() ) == 0;
928 }
929
930 if( match )
931 {
932 FOOTPRINT* tmp = footprint;
933
934 if( m_replaceFootprints && component->GetFPID() != footprint->GetFPID() )
935 tmp = replaceFootprint( aNetlist, footprint, component );
936
937 if( tmp )
938 {
939 footprintMap[ component ] = tmp;
940
941 updateFootprintParameters( tmp, component );
942 updateComponentPadConnections( tmp, component );
943 }
944
945 matchCount++;
946 }
947
948 if( footprint == lastPreexistingFootprint )
949 {
950 // No sense going through the newly-created footprints: end of loop
951 break;
952 }
953 }
954
955 if( matchCount == 0 )
956 {
957 FOOTPRINT* footprint = addNewFootprint( component );
958
959 if( footprint )
960 {
961 footprintMap[ component ] = footprint;
962
963 updateFootprintParameters( footprint, component );
964 updateComponentPadConnections( footprint, component );
965 }
966 }
967 else if( matchCount > 1 )
968 {
969 msg.Printf( _( "Multiple footprints found for '%s'." ), component->GetReference() );
971 }
972 }
973
974 updateCopperZoneNets( aNetlist );
975
976 // Finally go through the board footprints and update all those that *don't* have matching
977 // component entries.
978 //
979 for( FOOTPRINT* footprint : m_board->Footprints() )
980 {
981 bool matched = false;
982 bool doDelete = m_deleteUnusedFootprints;
983
984 if( ( footprint->GetAttributes() & FP_BOARD_ONLY ) > 0 )
985 doDelete = false;
986
988 component = aNetlist.GetComponentByPath( footprint->GetPath() );
989 else
990 component = aNetlist.GetComponentByReference( footprint->GetReference() );
991
992 if( component && component->GetProperties().count( wxT( "exclude_from_board" ) ) == 0 )
993 matched = true;
994
995 if( doDelete && !matched && footprint->IsLocked() )
996 {
997 if( m_isDryRun )
998 {
999 msg.Printf( _( "Cannot remove unused footprint %s (locked)." ),
1000 footprint->GetReference() );
1001 }
1002 else
1003 {
1004 msg.Printf( _( "Could not remove unused footprint %s (locked)." ),
1005 footprint->GetReference() );
1006 }
1007
1009 doDelete = false;
1010 }
1011
1012 if( doDelete && !matched )
1013 {
1014 if( m_isDryRun )
1015 {
1016 msg.Printf( _( "Remove unused footprint %s." ), footprint->GetReference() );
1017 }
1018 else
1019 {
1020 m_commit.Remove( footprint );
1021 msg.Printf( _( "Removed unused footprint %s." ), footprint->GetReference() );
1022 }
1023
1025 }
1026 else if( !m_isDryRun )
1027 {
1028 if( !matched )
1029 footprint->SetPath( KIID_PATH() );
1030
1031 for( PAD* pad : footprint->Pads() )
1032 {
1033 if( pad->GetNet() )
1034 pad->GetNet()->SetIsCurrent( true );
1035 }
1036 }
1037 }
1038
1039 if( !m_isDryRun )
1040 {
1042 testConnectivity( aNetlist, footprintMap );
1043
1044 for( NETINFO_ITEM* net : m_board->GetNetInfo() )
1045 {
1046 if( !net->IsCurrent() )
1047 {
1048 msg.Printf( _( "Removed unused net %s." ), net->GetNetname() );
1050 m_commit.Removed( net );
1051 }
1052 }
1053
1056
1057 // When new footprints are added, the automatic zone refill is disabled because:
1058 // * it creates crashes when calculating dynamic ratsnests if auto refill is enabled.
1059 // (the auto refills rebuild the connectivity with incomplete data)
1060 // * it is useless because zones will be refilled after placing new footprints
1061 m_commit.Push( _( "Update netlist" ), m_newFootprintsCount ? ZONE_FILL_OP : 0 );
1062
1065 }
1066
1067 if( m_isDryRun )
1068 {
1069 for( const std::pair<const wxString, NETINFO_ITEM*>& addedNet : m_addedNets )
1070 delete addedNet.second;
1071
1072 m_addedNets.clear();
1073 }
1074
1075 // Update the ratsnest
1078
1079 msg.Printf( _( "Total warnings: %d, errors: %d." ), m_warningCount, m_errorCount );
1081
1082 return true;
1083}
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:109
#define ZONE_FILL_OP
Definition: board_commit.h:43
void SetResolveNetConflicts(bool aResolve=true)
Sets a flag that will cause Push() to resolve net conflicts on track/via clusters instead of the defa...
Definition: board_commit.h:73
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:763
const BOX2I GetBoardEdgesBoundingBox() const
Return the board bounding box calculated using exclusively the board edges (graphics on Edge....
Definition: board.h:821
const PAGE_INFO & GetPageSettings() const
Definition: board.h:624
ZONES & Zones()
Definition: board.h:313
NETINFO_ITEM * FindNet(int aNetcode) const
Search for a net with the given netcode.
Definition: board.cpp:1392
void SynchronizeNetsAndNetClasses()
Copy NETCLASS info to each NET, based on NET membership in a NETCLASS.
Definition: board.cpp:1460
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:400
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
void BuildConnectivity(PROGRESS_REPORTER *aReporter=nullptr)
Build or rebuild the board connectivity database for the board, especially the list of connected item...
Definition: board.cpp:155
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:131
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:140
virtual void SetParent(EDA_ITEM *aParent)
Definition: eda_item.h:100
void SetPosition(const VECTOR2I &aPos) override
Definition: footprint.cpp:1636
void SetPath(const KIID_PATH &aPath)
Definition: footprint.h:220
void SetAttributes(int aAttributes)
Definition: footprint.h:246
EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
Definition: footprint.cpp:1350
int GetAttributes() const
Definition: footprint.h:245
PADS & Pads()
Definition: footprint.h:174
const LIB_ID & GetFPID() const
Definition: footprint.h:207
void SetReference(const wxString &aReference)
Definition: footprint.h:528
void SetValue(const wxString &aValue)
Definition: footprint.h:549
const wxString & GetValue() const
Definition: footprint.h:541
const wxString & GetReference() const
Definition: footprint.h:519
void SetProperties(const std::map< wxString, wxString > &aProps)
Definition: footprint.h:563
const KIID_PATH & GetPath() const
Definition: footprint.h:219
PAD * FindPadByNumber(const wxString &aPadNumber, PAD *aSearchAfterMe=nullptr) const
Return a PAD with a matching number.
Definition: footprint.cpp:1151
const std::map< wxString, wxString > & GetProperties() const
Definition: footprint.h:562
wxString AsString() const
Definition: kiid.cpp:330
Definition: kiid.h:47
bool empty() const
Definition: lib_id.h:186
UTF8 Format() const
Definition: lib_id.cpp:117
Handle the data for a net.
Definition: netinfo.h:66
void SetIsCurrent(bool isCurrent)
Definition: netinfo.h:150
static const int UNCONNECTED
Constant that forces initialization of a netinfo item to the NETINFO_ITEM ORPHANED (typically -1) whe...
Definition: netinfo.h:373
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:58
const wxString & GetPinFunction() const
Definition: pad.h:145
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_EXCLUDE_FROM_POS_FILES
Definition: footprint.h:69
@ FP_BOARD_ONLY
Definition: footprint.h:71
@ FP_EXCLUDE_FROM_BOM
Definition: footprint.h:70
@ FP_JUST_ADDED
Definition: footprint.h:72
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