KiCad PCB EDA Suite
Loading...
Searching...
No Matches
backannotate.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) 2019 Alexander Shuklin <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21
22#include <backannotate.h>
23#include <boost/property_tree/ptree.hpp>
24#include <confirm.h>
25#include <common.h>
26#include <dsnlexer.h>
27#include <ptree.h>
28#include <reporter.h>
29#include <sch_edit_frame.h>
30#include <sch_sheet_path.h>
31#include <sch_label.h>
32#include <lib_symbol.h>
33#include <schematic.h>
34#include <sch_commit.h>
35#include <string_utils.h>
36#include <kiface_base.h>
38#include <connection_graph.h>
39#include <limits>
40#include <set>
41#include <tool/tool_manager.h>
43#include <tools/sch_selection.h>
45#include <wx/log.h>
46#include <fmt.h>
47#include <fmt/ranges.h>
48#include <fmt/xchar.h>
49
50// Compare nets in the deterministic library order so diode arrays and similar map cleanly.
51static std::vector<wxString> netsInUnitOrder( const std::vector<wxString>& aPins,
52 const std::map<wxString, wxString>& aNetByPin )
53{
54 std::vector<wxString> nets;
55
56 if( !aPins.empty() )
57 {
58 for( const wxString& pinNum : aPins )
59 {
60 auto it = aNetByPin.find( pinNum );
61 nets.push_back( it != aNetByPin.end() ? it->second : wxString() );
62 }
63 }
64 else
65 {
66 for( const std::pair<const wxString, wxString>& kv : aNetByPin )
67 nets.push_back( kv.second );
68 }
69
70 return nets;
71}
72
73
75 const std::vector<BACKANNOTATE_UNIT_SWAP_CANDIDATE>& aCandidates,
76 const std::map<wxString, wxString>& aPcbPinMap )
77{
79
80 if( aCandidates.size() < 2 )
81 {
82 plan.m_mappingOk = true;
83 return plan;
84 }
85
86 std::map<size_t, size_t> desiredTarget;
87 std::set<size_t> usedTargets;
88
89 for( size_t candidateIdx = 0; candidateIdx < aCandidates.size(); ++candidateIdx )
90 {
91 const BACKANNOTATE_UNIT_SWAP_CANDIDATE& candidate = aCandidates[candidateIdx];
92 std::map<wxString, wxString> pcbNetsByPin;
93
94 for( const wxString& pinNum : candidate.m_unitPinNumbers )
95 {
96 auto it = aPcbPinMap.find( pinNum );
97 pcbNetsByPin[pinNum] = ( it != aPcbPinMap.end() ) ? it->second : wxString();
98 }
99
100 std::vector<wxString> pcbNets = netsInUnitOrder( candidate.m_unitPinNumbers, pcbNetsByPin );
101 bool matched = false;
102
103 for( size_t matchIdx = 0; matchIdx < aCandidates.size(); ++matchIdx )
104 {
105 if( usedTargets.count( matchIdx ) )
106 continue;
107
108 const BACKANNOTATE_UNIT_SWAP_CANDIDATE& potentialMatch = aCandidates[matchIdx];
109 std::vector<wxString> schNets =
110 netsInUnitOrder( potentialMatch.m_unitPinNumbers, potentialMatch.m_schNetsByPin );
111
112 if( pcbNets == schNets )
113 {
114 desiredTarget[candidateIdx] = matchIdx;
115 usedTargets.insert( matchIdx );
116 matched = true;
117 break;
118 }
119 }
120
121 if( !matched )
122 return plan;
123 }
124
125 plan.m_mappingOk = true;
126 plan.m_identity = true;
127
128 for( size_t candidateIdx = 0; candidateIdx < aCandidates.size(); ++candidateIdx )
129 {
130 auto it = desiredTarget.find( candidateIdx );
131
132 if( it == desiredTarget.end() || it->second != candidateIdx )
133 {
134 plan.m_identity = false;
135 break;
136 }
137 }
138
139 if( plan.m_identity )
140 return plan;
141
142 std::set<size_t> visited;
143
144 for( size_t startIdx = 0; startIdx < aCandidates.size(); ++startIdx )
145 {
146 if( visited.count( startIdx ) )
147 continue;
148
149 std::vector<size_t> cycle;
150 size_t curIdx = startIdx;
151
152 while( !visited.count( curIdx ) )
153 {
154 visited.insert( curIdx );
155 cycle.push_back( curIdx );
156
157 auto nextIt = desiredTarget.find( curIdx );
158
159 if( nextIt == desiredTarget.end() || nextIt->second == curIdx )
160 break;
161
162 curIdx = nextIt->second;
163 }
164
165 if( cycle.size() < 2 )
166 continue;
167
168 for( size_t i = cycle.size() - 1; i > 0; --i )
169 {
170 size_t firstIdx = cycle[i - 1];
171 size_t secondIdx = cycle[i];
172
173 plan.m_steps.push_back( { firstIdx, secondIdx,
174 aCandidates[firstIdx].m_currentUnit,
175 aCandidates[secondIdx].m_currentUnit } );
176 plan.m_swappedCandidateIndices.insert( firstIdx );
177 plan.m_swappedCandidateIndices.insert( secondIdx );
178 }
179 }
180
181 return plan;
182}
183
184
185BACK_ANNOTATE::BACK_ANNOTATE( SCH_EDIT_FRAME* aFrame, REPORTER& aReporter, bool aRelinkFootprints,
186 bool aProcessFootprints, bool aProcessValues, bool aProcessReferences,
187 bool aProcessNetNames, bool aProcessAttributes, bool aProcessOtherFields,
188 bool aPreferUnitSwaps, bool aPreferPinSwaps, bool aDryRun ) :
189 m_reporter( aReporter ),
190 m_matchByReference( aRelinkFootprints ),
191 m_processFootprints( aProcessFootprints ),
192 m_processValues( aProcessValues ),
193 m_processReferences( aProcessReferences ),
194 m_processNetNames( aProcessNetNames ),
195 m_processAttributes( aProcessAttributes ),
196 m_processOtherFields( aProcessOtherFields ),
197 m_preferUnitSwaps( aPreferUnitSwaps ),
198 m_preferPinSwaps( aPreferPinSwaps ),
199 m_dryRun( aDryRun ),
200 m_frame( aFrame ),
201 m_changesCount( 0 )
202{ }
203
204
205bool BACK_ANNOTATE::BackAnnotateSymbols( const std::string& aNetlist )
206{
207 m_changesCount = 0;
208
211 {
212 m_reporter.ReportTail( _( "Select at least one property to back annotate." ), RPT_SEVERITY_ERROR );
213 return false;
214 }
215
216 getPcbModulesFromString( aNetlist );
217
218 SCH_SHEET_LIST sheets = m_frame->Schematic().Hierarchy();
221
224
226 return true;
227}
228
229
230bool BACK_ANNOTATE::FetchNetlistFromPCB( std::string& aNetlist )
231{
232 if( Kiface().IsSingle() )
233 {
234 DisplayErrorMessage( m_frame, _( "Cannot fetch PCB netlist because Schematic Editor is opened in "
235 "stand-alone mode.\n"
236 "You must launch the KiCad project manager and create a project." ) );
237 return false;
238 }
239
240 KIWAY_PLAYER* frame = m_frame->Kiway().Player( FRAME_PCB_EDITOR, false );
241
242 if( !frame )
243 {
244 wxFileName fn( m_frame->Prj().GetProjectFullName() );
245 fn.SetExt( FILEEXT::PcbFileExtension );
246
247 frame = m_frame->Kiway().Player( FRAME_PCB_EDITOR, true );
248 frame->OpenProjectFiles( std::vector<wxString>( 1, fn.GetFullPath() ) );
249 }
250
251 m_frame->Kiway().ExpressMail( FRAME_PCB_EDITOR, MAIL_PCB_GET_NETLIST, aNetlist );
252 return true;
253}
254
255
257{
258 std::string nullPayload;
259
260 m_frame->Kiway().ExpressMail( FRAME_PCB_EDITOR, MAIL_PCB_UPDATE_LINKS, nullPayload );
261}
262
263
264void BACK_ANNOTATE::getPcbModulesFromString( const std::string& aPayload )
265{
266 auto getStr = []( const PTREE& pt ) -> wxString
267 {
268 return UTF8( pt.front().first );
269 };
270
271 DSNLEXER lexer( aPayload, From_UTF8( __func__ ) );
272 PTREE doc;
273
274 // NOTE: KiCad's PTREE scanner constructs a property *name* tree, not a property tree.
275 // Every token in the s-expr is stored as a property name; the property's value is then
276 // either the nested s-exprs or an empty PTREE; there are *no* literal property values.
277
278 Scan( &doc, &lexer );
279
280 PTREE& tree = doc.get_child( "pcb_netlist" );
281 wxString msg;
282 m_pcbFootprints.clear();
283
284 for( const std::pair<const std::string, PTREE>& item : tree )
285 {
286 wxString path, value, footprint;
287 bool dnp = false, exBOM = false, exPosFiles = false;
288 std::map<wxString, wxString> pinNetMap, fieldsMap;
289 wxASSERT( item.first == "ref" );
290 wxString ref = getStr( item.second );
291
292 try
293 {
295 path = ref;
296 else
297 path = getStr( item.second.get_child( "timestamp" ) );
298
299 if( path == "" )
300 {
301 msg.Printf( _( "Footprint '%s' has no assigned symbol." ), DescribeRef( ref ) );
302 m_reporter.ReportHead( msg, RPT_SEVERITY_WARNING );
303 continue;
304 }
305
306 footprint = getStr( item.second.get_child( "fpid" ) );
307 value = getStr( item.second.get_child( "value" ) );
308
309 // Get child PTREE of fields
310 boost::optional<const PTREE&> fields = item.second.get_child_optional( "fields" );
311
312 // Parse each field out of the fields string
313 if( fields )
314 {
315 for( const std::pair<const std::string, PTREE>& field : fields.get() )
316 {
317 if( field.first != "field" )
318 continue;
319
320 // Fields are of the format "(field (name "name") "12345")
321 const auto& fieldName = field.second.get_child_optional( "name" );
322 const std::string& fieldValue = field.second.back().first;
323
324 if( !fieldName )
325 continue;
326
327 fieldsMap[getStr( fieldName.get() )] = wxString::FromUTF8( fieldValue );
328 }
329 }
330
331
332 // Get DNP, Exclude from BOM, and Exclude from Position Files out of the
333 // properties if they exist
334 for( const auto& child : item.second )
335 {
336 if( child.first != "property" )
337 continue;
338
339 auto property = child.second;
340 auto name = property.get_child_optional( "name" );
341
342 if( !name )
343 continue;
344
345 if( name.get().front().first == "dnp" )
346 dnp = true;
347 else if( name.get().front().first == "exclude_from_bom" )
348 exBOM = true;
349 else if( name.get().front().first == "exclude_from_pos_files" )
350 exPosFiles = true;
351 }
352
353 boost::optional<const PTREE&> nets = item.second.get_child_optional( "nets" );
354
355 if( nets )
356 {
357 for( const std::pair<const std::string, PTREE>& pin_net : nets.get() )
358 {
359 wxASSERT( pin_net.first == "pin_net" );
360 wxString pinNumber = UTF8( pin_net.second.front().first );
361 wxString netName = UTF8( pin_net.second.back().first );
362 pinNetMap[ pinNumber ] = netName;
363 }
364 }
365 }
366 catch( ... )
367 {
368 wxLogWarning( "Cannot parse PCB netlist for back-annotation." );
369 }
370
371 // Use lower_bound for not to iterate over map twice
372 auto nearestItem = m_pcbFootprints.lower_bound( path );
373
374 if( nearestItem != m_pcbFootprints.end() && nearestItem->first == path )
375 {
376 // Module with this path already exists - generate error
377 msg.Printf( _( "Footprints '%s' and '%s' linked to same symbol." ),
378 DescribeRef( nearestItem->second->m_ref ),
379 DescribeRef( ref ) );
380 m_reporter.ReportHead( msg, RPT_SEVERITY_ERROR );
381 }
382 else
383 {
384 // Add footprint to the map
385 std::shared_ptr<PCB_FP_DATA> data = std::make_shared<PCB_FP_DATA>( ref, footprint, value, dnp, exBOM,
386 exPosFiles, pinNetMap, fieldsMap );
387 m_pcbFootprints.insert( nearestItem, std::make_pair( path, data ) );
388 }
389 }
390}
391
392
394{
395 for( const auto& [pcbPath, pcbData] : m_pcbFootprints )
396 {
397 int refIndex;
398 bool foundInMultiunit = false;
399
400 for( const auto& [_, refList] : m_multiUnitsRefs )
401 {
403 refIndex = refList.FindRef( pcbPath );
404 else
405 refIndex = refList.FindRefByFullPath( pcbPath );
406
407 if( refIndex >= 0 )
408 {
409 // If footprint linked to multi unit symbol, we add all symbol's units to
410 // the change list
411 foundInMultiunit = true;
412
413 for( size_t i = 0; i < refList.GetCount(); ++i )
414 {
415 refList[ i ].GetSymbol()->ClearFlags(SKIP_STRUCT );
416 m_changelist.emplace_back( CHANGELIST_ITEM( refList[i], pcbData ) );
417 }
418
419 break;
420 }
421 }
422
423 if( foundInMultiunit )
424 continue;
425
427 refIndex = m_refs.FindRef( pcbPath );
428 else
429 refIndex = m_refs.FindRefByFullPath( pcbPath );
430
431 if( refIndex >= 0 )
432 {
433 m_refs[ refIndex ].GetSymbol()->ClearFlags( SKIP_STRUCT );
434 m_changelist.emplace_back( CHANGELIST_ITEM( m_refs[refIndex], pcbData ) );
435 }
436 else
437 {
438 // Haven't found linked symbol in multiunits or common refs. Generate error
439 m_reporter.ReportTail( wxString::Format( _( "Cannot find symbol for footprint '%s'." ),
440 DescribeRef( pcbData->m_ref ) ),
442 }
443 }
444}
445
447{
448 m_refs.SortByTimeStamp();
449
450 std::sort( m_changelist.begin(), m_changelist.end(),
451 []( const CHANGELIST_ITEM& a, const CHANGELIST_ITEM& b )
452 {
453 return SCH_REFERENCE_LIST::sortByTimeStamp( a.first, b.first );
454 } );
455
456 size_t i = 0;
457
458 for( const std::pair<SCH_REFERENCE, std::shared_ptr<PCB_FP_DATA>>& item : m_changelist )
459 {
460 // Refs and changelist are both sorted by paths, so we just go over m_refs and
461 // generate errors before we will find m_refs member to which item linked
462 while( i < m_refs.GetCount() && m_refs[i].GetPath() != item.first.GetPath() )
463 {
464 const SCH_REFERENCE& ref = m_refs[i];
465
466 if( ref.GetSymbol()->GetExcludedFromBoard() )
467 {
468 m_reporter.ReportTail( wxString::Format( _( "Footprint '%s' is not present on PCB. "
469 "Corresponding symbols in schematic must be "
470 "manually deleted (if desired)." ),
471 DescribeRef( m_refs[i].GetRef() ) ),
473 }
474
475 ++i;
476 }
477
478 ++i;
479 }
480
481 if( m_matchByReference && !m_frame->ReadyToNetlist( _( "Re-linking footprints requires a fully "
482 "annotated schematic." ) ) )
483 {
484 m_reporter.ReportTail( _( "Footprint re-linking canceled by user." ), RPT_SEVERITY_ERROR );
485 }
486}
487
488
490{
491 SCH_COMMIT commit( m_frame );
492 wxString msg;
493
494 std::set<CHANGELIST_ITEM*> unitSwapItems;
495
496 // First, optionally handle unit swaps across multi-unit symbols where possible
497 // This needs to happen before the rest of the normal changelist processing,
498 // because the swaps will modify the changelist
500 {
501 REPORTER& debugReporter = NULL_REPORTER::GetInstance(); // Change to m_reporter for debugging
502
503 // Group changelist items by shared PCB footprint data pointer
504 std::map<std::shared_ptr<PCB_FP_DATA>, std::vector<CHANGELIST_ITEM*>> changesPerFp;
505
506 msg.Printf( wxT( "DEBUG(unit-swap): grouped changelist into %zu footprint(s) for unit-swap processing:" ),
507 changesPerFp.size() );
508
509 // The changelist will have multiple entries for multi-unit symbols, each unit ref, e.g. U1A, U1B
510 // will point to the same PCB_FP_DATA. Grouping by pointer allows us to handle all units of a symbol
511 // together by working on all changes to the same PCB footprint at once.
512 for( CHANGELIST_ITEM& item : m_changelist )
513 {
514 changesPerFp[item.second].push_back( &item );
515 msg += wxT( " " ) + item.first.GetRef();
516 }
517
518 debugReporter.ReportHead( msg, RPT_SEVERITY_INFO );
519
520 // Handle all changes per footprint
521 for( auto& fpChangelistPair : changesPerFp )
522 {
523 std::set<SCH_SYMBOL*> swappedSymbols;
524 std::set<CHANGELIST_ITEM*> swappedItems;
525 std::shared_ptr<PCB_FP_DATA> fp = fpChangelistPair.first;
526 auto& changedFpItems = fpChangelistPair.second;
527
528 // Build symbol unit list for this footprint (multi-unit candidates)
529 // Snapshot of one schematic unit instance (ref + sheet path + nets) for matching
530 struct SYM_UNIT
531 {
532 SCH_SYMBOL* sym = nullptr;
533 SCH_SCREEN* screen = nullptr;
534 const SCH_SHEET_PATH* sheetPath = nullptr;
535 wxString ref;
536 int currentUnit = 0;
537 // Track these so we avoid re-applying label/field changes to swapped units
538 CHANGELIST_ITEM* changeItem = nullptr;
539 std::map<wxString, wxString> schNetsByPin; // pinNumber -> net (schematic)
540 std::map<wxString, wxString> pcbNetsByPin; // pinNumber -> net (from PCB pin map)
541 std::vector<wxString> unitPinNumbers; // library-defined pin numbers per unit
542 std::vector<wxString> schNetsInUnitOrder;
543 std::vector<wxString> pcbNetsInUnitOrder;
544 };
545
546 // All symbol units for this footprint
547 std::vector<SYM_UNIT> symbolUnits;
548
549 std::map<LIB_SYMBOL*, std::vector<LIB_SYMBOL::UNIT_PIN_INFO>> unitPinsByLibSymbol;
550
551 auto getUnitPins =
552 [&]( SCH_SYMBOL* symbol, int unitNumber ) -> std::vector<wxString>
553 {
554 if( unitNumber <= 0 )
555 return {};
556
557 if( !symbol )
558 return {};
559
560 LIB_SYMBOL* libSymbol = symbol->GetLibSymbolRef().get();
561
562 if( !libSymbol )
563 return {};
564
565 auto found = unitPinsByLibSymbol.find( libSymbol );
566
567 if( found == unitPinsByLibSymbol.end() )
568 found = unitPinsByLibSymbol.emplace( libSymbol, libSymbol->GetUnitPinInfo() ).first;
569
570 const std::vector<LIB_SYMBOL::UNIT_PIN_INFO>& unitInfos = found->second;
571
572 if( unitNumber > static_cast<int>( unitInfos.size() ) )
573 return {};
574
575 return unitInfos[unitNumber - 1].m_pinNumbers;
576 };
577
578 for( CHANGELIST_ITEM* changedItem : changedFpItems )
579 {
580 SCH_REFERENCE& ref = changedItem->first;
581 SCH_SYMBOL* symbol = ref.GetSymbol();
582 SCH_SCREEN* screen = ref.GetSheetPath().LastScreen();
583
584 if( !symbol )
585 continue;
586
587 // Collect nets keyed by pin number. Ordering by XY is intentionally avoided
588 // here; see comment in SYM_UNIT above.
589 SYM_UNIT symbolUnit;
590 symbolUnit.sym = symbol;
591 symbolUnit.screen = screen;
592 symbolUnit.ref = symbol->GetRef( &ref.GetSheetPath(), true );
593 symbolUnit.sheetPath = &ref.GetSheetPath();
594 symbolUnit.changeItem = changedItem;
595
596 int currentUnit = ref.GetUnit();
597
598 if( currentUnit <= 0 )
599 currentUnit = symbol->GetUnitSelection( &ref.GetSheetPath() );
600
601 if( currentUnit <= 0 )
602 currentUnit = symbol->GetUnit();
603
604 symbolUnit.currentUnit = currentUnit;
605 symbolUnit.unitPinNumbers = getUnitPins( symbol, symbolUnit.currentUnit );
606
607 const SCH_SHEET_PATH& sheetPath = ref.GetSheetPath();
608
609 for( SCH_PIN* pin : symbol->GetPins( &ref.GetSheetPath() ) )
610 {
611 const wxString& pinNum = pin->GetNumber();
612
613 // PCB nets from footprint pin map
614 auto it = fp->m_pinMap.find( pinNum );
615 symbolUnit.pcbNetsByPin[pinNum] = ( it != fp->m_pinMap.end() ) ? it->second : wxString();
616
617 // Schematic nets from connections
618 if( SCH_PIN* p = symbol->GetPin( pinNum ) )
619 {
620 if( SCH_CONNECTION* connection = p->Connection( &sheetPath ) )
621 symbolUnit.schNetsByPin[pinNum] = connection->Name( true );
622 else
623 symbolUnit.schNetsByPin[pinNum] = wxString();
624 }
625 else
626 symbolUnit.schNetsByPin[pinNum] = wxString();
627 }
628
629 symbolUnit.pcbNetsInUnitOrder = netsInUnitOrder( symbolUnit.unitPinNumbers, symbolUnit.pcbNetsByPin );
630 symbolUnit.schNetsInUnitOrder = netsInUnitOrder( symbolUnit.unitPinNumbers, symbolUnit.schNetsByPin );
631
632 symbolUnits.push_back( symbolUnit );
633 }
634
635 auto vectorToString =
636 []( const std::vector<wxString>& values ) -> wxString
637 {
638 return fmt::format( L"{}", fmt::join( values, L", " ) );
639 };
640
641 auto mapToString =
642 [vectorToString]( const std::map<wxString, wxString>& pinMap ) -> wxString
643 {
644 std::vector<wxString> entries;
645
646 for( const std::pair<const wxString, wxString>& pin : pinMap )
647 entries.push_back( pin.first + '=' + pin.second );
648
649 return vectorToString( entries );
650 };
651
652 msg.Printf( wxT( "DEBUG(unit-swap): footprint %s processed (%zu units, dryRun=%d)." ), fp->m_ref,
653 symbolUnits.size(), m_dryRun ? 1 : 0 );
654 debugReporter.ReportHead( msg, RPT_SEVERITY_INFO );
655
656 // For debugging, sort the symbol units by ref
657 std::sort( symbolUnits.begin(), symbolUnits.end(),
658 []( const SYM_UNIT& a, const SYM_UNIT& b )
659 {
660 return a.ref < b.ref;
661 } );
662
663 for( const SYM_UNIT& su : symbolUnits )
664 {
665 wxString pcbPins = mapToString( su.pcbNetsByPin );
666 wxString schPins = mapToString( su.schNetsByPin );
667 wxString unitPins = vectorToString( su.unitPinNumbers );
668 wxString pcbUnitNetSeq = vectorToString( su.pcbNetsInUnitOrder );
669 wxString schUnitNetSeq = vectorToString( su.schNetsInUnitOrder );
670
671 msg.Printf( wxT( "DEBUG(unit-swap): unit %d: %s pcbPins[%s] schPins[%s] unitPins[%s] pcbUnitNets[%s] "
672 "schUnitNets[%s]." ),
673 su.currentUnit, su.ref, pcbPins, schPins, unitPins, pcbUnitNetSeq, schUnitNetSeq );
674 debugReporter.ReportHead( msg, RPT_SEVERITY_INFO );
675 }
676
677 if( symbolUnits.size() < 2 )
678 continue;
679
680 std::vector<BACKANNOTATE_UNIT_SWAP_CANDIDATE> candidates;
681
682 for( const SYM_UNIT& symbolUnit : symbolUnits )
683 {
685 candidate.m_ref = symbolUnit.ref;
686 candidate.m_currentUnit = symbolUnit.currentUnit;
687 candidate.m_schNetsByPin = symbolUnit.schNetsByPin;
688 candidate.m_unitPinNumbers = symbolUnit.unitPinNumbers;
689 candidates.push_back( candidate );
690 }
691
692 BACKANNOTATE_UNIT_SWAP_PLAN plan = PlanBackannotateUnitSwaps( candidates, fp->m_pinMap );
693
694 if( !plan.m_mappingOk )
695 {
696 msg.Printf( wxT( "DEBUG(unit-swap): mapping failed for footprint %s." ), fp->m_ref );
697 debugReporter.ReportHead( msg, RPT_SEVERITY_INFO );
698 continue;
699 }
700
701 if( plan.m_identity )
702 {
703 msg.Printf( wxT( "DEBUG(unit-swap): footprint %s already aligned (identity mapping)." ), fp->m_ref );
704 debugReporter.ReportHead( msg, RPT_SEVERITY_INFO );
705 continue;
706 }
707
708 if( !m_dryRun )
709 {
710 for( size_t candidateIdx : plan.m_swappedCandidateIndices )
711 commit.Modify( symbolUnits[candidateIdx].sym, symbolUnits[candidateIdx].screen );
712 }
713
714 for( const BACKANNOTATE_UNIT_SWAP_STEP& step : plan.m_steps )
715 {
716 SYM_UNIT& a = symbolUnits[step.m_firstIndex];
717 SYM_UNIT& b = symbolUnits[step.m_secondIndex];
718 int aUnit = a.currentUnit;
719 int bUnit = b.currentUnit;
720
721 if( !m_dryRun )
722 {
723 a.sym->SetUnit( bUnit );
724 b.sym->SetUnit( aUnit );
725
726 if( const SCH_SHEET_PATH* sheet = a.sheetPath )
727 a.sym->SetUnitSelection( sheet, bUnit );
728
729 if( const SCH_SHEET_PATH* sheet = b.sheetPath )
730 b.sym->SetUnitSelection( sheet, aUnit );
731 }
732
733 a.currentUnit = bUnit;
734 b.currentUnit = aUnit;
735
736 swappedSymbols.insert( a.sym );
737 swappedSymbols.insert( b.sym );
738
739 if( a.changeItem )
740 {
741 swappedItems.insert( a.changeItem );
742 unitSwapItems.insert( a.changeItem );
743 }
744
745 if( b.changeItem )
746 {
747 swappedItems.insert( b.changeItem );
748 unitSwapItems.insert( b.changeItem );
749 }
750
751 wxString baseRef = a.sym->GetRef( a.sheetPath, false );
752 wxString unitAString = a.sym->SubReference( aUnit, false );
753 wxString unitBString = b.sym->SubReference( bUnit, false );
754
755 if( unitAString.IsEmpty() )
756 unitAString.Printf( wxT( "%d" ), aUnit );
757
758 if( unitBString.IsEmpty() )
759 unitBString.Printf( wxT( "%d" ), bUnit );
760
761 msg.Printf( _( "Swap %s unit %s with unit %s." ),
762 DescribeRef( baseRef ),
763 unitAString,
764 unitBString );
765 m_reporter.ReportHead( msg, RPT_SEVERITY_ACTION );
767 }
768
769 msg.Printf( wxT( "DEBUG(unit-swap): applied %zu swap steps for footprint %s." ),
770 plan.m_steps.size(), fp->m_ref );
771 debugReporter.ReportHead( msg, RPT_SEVERITY_INFO );
772
773 // Remove label updates for swapped symbols by marking their SKIP_STRUCT flag
774 if( !m_dryRun )
775 {
776 for( CHANGELIST_ITEM* changedItem : changedFpItems )
777 {
778 SCH_SYMBOL* symbol = changedItem->first.GetSymbol();
779
780 if( !symbol )
781 continue;
782
783 if( swappedItems.count( changedItem ) || swappedSymbols.count( symbol ) )
784 symbol->SetFlags( SKIP_STRUCT );
785
786 int updatedUnit = symbol->GetUnitSelection( &changedItem->first.GetSheetPath() );
787 changedItem->first.SetUnit( updatedUnit );
788 }
789 }
790 }
791 }
792
793 // Apply changes from change list
794 for( CHANGELIST_ITEM& item : m_changelist )
795 {
796 SCH_REFERENCE& ref = item.first;
797 PCB_FP_DATA& fpData = *item.second;
798 SCH_SYMBOL* symbol = ref.GetSymbol();
799 SCH_SCREEN* screen = ref.GetSheetPath().LastScreen();
800 wxString oldFootprint = ref.GetFootprint();
801 wxString oldValue = ref.GetValue();
802 bool oldDNP = ref.GetSymbol()->GetDNP();
803 bool oldExBOM = ref.GetSymbol()->GetExcludedFromBOM();
804 bool oldExPosFiles = ref.GetSymbol()->GetExcludedFromPosFiles();
805 // Skip prevents us from re-applying label/field changes to units we just swapped
806 bool skip = ( ref.GetSymbol()->GetFlags() & SKIP_STRUCT ) > 0 || unitSwapItems.count( &item ) > 0;
807
808 auto boolString =
809 []( bool b ) -> wxString
810 {
811 return b ? _( "true" ) : _( "false" );
812 };
813
814 if( !m_dryRun )
815 commit.Modify( symbol, screen, RECURSE_MODE::NO_RECURSE );
816
817 if( m_processReferences && ref.GetRef() != fpData.m_ref && !skip
818 && !symbol->GetField( FIELD_T::REFERENCE )->HasTextVars() )
819 {
821 msg.Printf( _( "Change %s reference designator to '%s'." ),
822 DescribeRef( ref.GetRef() ),
823 fpData.m_ref );
824
825 if( !m_dryRun )
826 symbol->SetRef( &ref.GetSheetPath(), fpData.m_ref );
827
828 m_reporter.ReportHead( msg, RPT_SEVERITY_ACTION );
829 }
830
831 if( m_processFootprints && oldFootprint != fpData.m_footprint && !skip
832 && !symbol->GetField( FIELD_T::FOOTPRINT )->HasTextVars() )
833 {
835 msg.Printf( _( "Change %s footprint assignment from '%s' to '%s'." ),
836 DescribeRef( ref.GetRef() ),
837 EscapeHTML( oldFootprint ),
838 EscapeHTML( fpData.m_footprint ) );
839
840 if( !m_dryRun )
841 symbol->SetFootprintFieldText( fpData.m_footprint );
842
843 m_reporter.ReportHead( msg, RPT_SEVERITY_ACTION );
844 }
845
846 if( m_processValues && oldValue != fpData.m_value && !skip
847 && !symbol->GetField( FIELD_T::VALUE )->HasTextVars() )
848 {
850 msg.Printf( _( "Change %s value from '%s' to '%s'." ),
851 DescribeRef( ref.GetRef() ),
852 EscapeHTML( oldValue ),
853 EscapeHTML( fpData.m_value ) );
854
855 if( !m_dryRun )
856 symbol->SetValueFieldText( fpData.m_value );
857
858 m_reporter.ReportHead( msg, RPT_SEVERITY_ACTION );
859 }
860
861 if( m_processAttributes && oldDNP != fpData.m_DNP && !skip )
862 {
864 msg.Printf( _( "Change %s 'Do not populate' from '%s' to '%s'." ),
865 DescribeRef( ref.GetRef() ),
866 boolString( oldDNP ),
867 boolString( fpData.m_DNP ) );
868
869 if( !m_dryRun )
870 symbol->SetDNP( fpData.m_DNP );
871
872 m_reporter.ReportHead( msg, RPT_SEVERITY_ACTION );
873 }
874
875 if( m_processAttributes && oldExBOM != fpData.m_excludeFromBOM && !skip )
876 {
878 msg.Printf( _( "Change %s 'Exclude from bill of materials' from '%s' to '%s'." ),
879 DescribeRef( ref.GetRef() ),
880 boolString( oldExBOM ),
881 boolString( fpData.m_excludeFromBOM ) );
882
883 if( !m_dryRun )
884 symbol->SetExcludedFromBOM( fpData.m_excludeFromBOM );
885
886 m_reporter.ReportHead( msg, RPT_SEVERITY_ACTION );
887 }
888
889 if( m_processAttributes && oldExPosFiles != fpData.m_excludeFromPosFiles && !skip )
890 {
892 msg.Printf( _( "Change %s 'Exclude from position files' from '%s' to '%s'." ), DescribeRef( ref.GetRef() ),
893 boolString( oldExPosFiles ), boolString( fpData.m_excludeFromPosFiles ) );
894
895 if( !m_dryRun )
897
898 m_reporter.ReportHead( msg, RPT_SEVERITY_ACTION );
899 }
900
901 std::set<wxString> swappedPins;
902
903 // Try to satisfy footprint pad net swaps by moving symbol pins before falling back to labels.
904 if( m_preferPinSwaps && m_processNetNames && !skip )
905 swappedPins = applyPinSwaps( symbol, ref, fpData, &commit );
906
907 if( m_processNetNames && !skip )
908 {
909 for( const std::pair<const wxString, wxString>& entry : fpData.m_pinMap )
910 {
911 const wxString& pinNumber = entry.first;
912 const wxString& shortNetName = entry.second;
913 SCH_PIN* pin = symbol->GetPin( pinNumber );
914
915 // Skip pins that the user preferred to handle with pin swaps in applyPreferredPinSwaps()
916 if( swappedPins.count( pinNumber ) > 0 )
917 continue;
918
919 if( !pin )
920 {
921 msg.Printf( _( "Cannot find %s pin '%s'." ),
922 DescribeRef( ref.GetRef() ),
923 EscapeHTML( pinNumber ) );
924 m_reporter.ReportHead( msg, RPT_SEVERITY_ERROR );
925
926 continue;
927 }
928
929 SCH_CONNECTION* connection = pin->Connection( &ref.GetSheetPath() );
930
931 if( connection && connection->Name( true ) != shortNetName )
932 {
933 processNetNameChange( &commit, ref.GetRef(), pin, connection,
934 connection->Name( true ), shortNetName );
935 }
936 }
937 }
938
940 {
941 // Need to handle three cases: existing field, new field, deleted field
942 for( const std::pair<const wxString, wxString>& field : fpData.m_fieldsMap )
943 {
944 const wxString& fpFieldName = field.first;
945 const wxString& fpFieldValue = field.second;
946 SCH_FIELD* symField = symbol->GetField( fpFieldName );
947
948 // Skip fields that are individually controlled
949 if( fpFieldName == GetCanonicalFieldName( FIELD_T::REFERENCE )
950 || fpFieldName == GetCanonicalFieldName( FIELD_T::VALUE ) )
951 {
952 continue;
953 }
954
955 // 1. Existing fields has changed value
956 // PCB Field value is checked against the shown text because this is the value
957 // with all the variables resolved. The footprints field value gets the symbol's
958 // resolved value when the PCB is updated from the schematic.
959 if( symField
960 && !symField->HasTextVars()
961 && symField->GetShownText( &ref.GetSheetPath(), false ) != fpFieldValue )
962 {
964 msg.Printf( _( "Change %s field '%s' value to '%s'." ),
965 DescribeRef( ref.GetRef() ),
966 EscapeHTML( symField->GetCanonicalName() ),
967 EscapeHTML( fpFieldValue ) );
968
969 if( !m_dryRun )
970 symField->SetText( fpFieldValue );
971
972 m_reporter.ReportHead( msg, RPT_SEVERITY_ACTION );
973 }
974
975 // 2. New field has been added to footprint and needs to be added to symbol
976 if( symField == nullptr )
977 {
979 msg.Printf( _( "Add %s field '%s' with value '%s'." ),
980 DescribeRef( ref.GetRef() ),
981 EscapeHTML( fpFieldName ),
982 EscapeHTML( fpFieldValue ) );
983
984 if( !m_dryRun )
985 {
986 SCH_FIELD newField( symbol, FIELD_T::USER, fpFieldName );
987 newField.SetText( fpFieldValue );
988 newField.SetTextPos( symbol->GetPosition() );
989 newField.SetVisible( false ); // Don't clutter up the schematic
990 symbol->AddField( newField );
991 }
992
993 m_reporter.ReportHead( msg, RPT_SEVERITY_ACTION );
994 }
995 }
996
997 // 3. Existing field has been deleted from footprint and needs to be deleted from symbol
998 // Check all symbol fields for existence in the footprint field map.
999 // Collect names first to avoid iterator invalidation when removing fields.
1000 std::vector<wxString> fieldsToDelete;
1001
1002 for( SCH_FIELD& field : symbol->GetFields() )
1003 {
1004 if( field.IsMandatory() )
1005 continue;
1006
1007 if( fpData.m_fieldsMap.find( field.GetCanonicalName() ) == fpData.m_fieldsMap.end() )
1008 {
1010 msg.Printf( _( "Delete %s field '%s.'" ),
1011 DescribeRef( ref.GetRef() ),
1012 EscapeHTML( field.GetName() ) );
1013
1014 fieldsToDelete.push_back( field.GetName() );
1015 m_reporter.ReportHead( msg, RPT_SEVERITY_ACTION );
1016 }
1017 }
1018
1019 if( !m_dryRun )
1020 {
1021 for( const wxString& fieldName : fieldsToDelete )
1022 symbol->RemoveField( fieldName );
1023 }
1024 }
1025
1026 if( symbol->GetFlags() & SKIP_STRUCT )
1027 symbol->ClearFlags( SKIP_STRUCT );
1028
1029 unitSwapItems.erase( &item );
1030
1031 // TODO: back-annotate netclass changes?
1032 }
1033
1034 if( !m_dryRun )
1035 {
1036 m_frame->RecalculateConnections( &commit, NO_CLEANUP );
1037 m_frame->UpdateNetHighlightStatus();
1038
1039 commit.Push( _( "Update Schematic from PCB" ) );
1040 }
1041}
1042
1043
1045{
1047
1048 // Initial orientation from the pin
1049 switch( aPin->GetLibPin()->GetOrientation() )
1050 {
1051 default:
1052 case PIN_ORIENTATION::PIN_RIGHT: spin = SPIN_STYLE::LEFT; break;
1053 case PIN_ORIENTATION::PIN_UP: spin = SPIN_STYLE::BOTTOM; break;
1054 case PIN_ORIENTATION::PIN_DOWN: spin = SPIN_STYLE::UP; break;
1055 case PIN_ORIENTATION::PIN_LEFT: spin = SPIN_STYLE::RIGHT; break;
1056 }
1057
1058 // Reorient based on the actual symbol orientation now
1059 struct ORIENT
1060 {
1061 int flag;
1062 int n_rots;
1063 int mirror_x;
1064 int mirror_y;
1065 }
1066 orientations[] =
1067 {
1068 { SYM_ORIENT_0, 0, 0, 0 },
1069 { SYM_ORIENT_90, 1, 0, 0 },
1070 { SYM_ORIENT_180, 2, 0, 0 },
1071 { SYM_ORIENT_270, 3, 0, 0 },
1072 { SYM_MIRROR_X + SYM_ORIENT_0, 0, 1, 0 },
1073 { SYM_MIRROR_X + SYM_ORIENT_90, 1, 1, 0 },
1074 { SYM_MIRROR_Y, 0, 0, 1 },
1075 { SYM_MIRROR_X + SYM_ORIENT_270, 3, 1, 0 },
1076 { SYM_MIRROR_Y + SYM_ORIENT_0, 0, 0, 1 },
1077 { SYM_MIRROR_Y + SYM_ORIENT_90, 1, 0, 1 },
1078 { SYM_MIRROR_Y + SYM_ORIENT_180, 2, 0, 1 },
1079 { SYM_MIRROR_Y + SYM_ORIENT_270, 3, 0, 1 }
1080 };
1081
1082 ORIENT o = orientations[ 0 ];
1083
1084 const SCH_SYMBOL* parentSymbol = static_cast<const SCH_SYMBOL*>( aPin->GetParentSymbol() );
1085
1086 if( !parentSymbol )
1087 return spin;
1088
1089 int symbolOrientation = parentSymbol->GetOrientation();
1090
1091 for( const ORIENT& i : orientations )
1092 {
1093 if( i.flag == symbolOrientation )
1094 {
1095 o = i;
1096 break;
1097 }
1098 }
1099
1100 for( int i = 0; i < o.n_rots; i++ )
1101 spin = spin.RotateCCW();
1102
1103 if( o.mirror_x )
1104 spin = spin.MirrorX();
1105
1106 if( o.mirror_y )
1107 spin = spin.MirrorY();
1108
1109 return spin;
1110}
1111
1112
1113void addConnections( SCH_ITEM* aItem, const SCH_SHEET_PATH& aSheetPath, std::set<SCH_ITEM*>& connectedItems )
1114{
1115 if( connectedItems.insert( aItem ).second )
1116 {
1117 for( SCH_ITEM* connectedItem : aItem->ConnectedItems( aSheetPath ) )
1118 addConnections( connectedItem, aSheetPath, connectedItems );
1119 }
1120}
1121
1122
1123std::set<wxString> BACK_ANNOTATE::applyPinSwaps( SCH_SYMBOL* aSymbol, const SCH_REFERENCE& aReference,
1124 const PCB_FP_DATA& aFpData, SCH_COMMIT* aCommit )
1125{
1126 // Tracks pin numbers that we end up swapping so that the caller can skip any
1127 // duplicate label-only handling for those pins.
1128 std::set<wxString> swappedPins;
1129
1130 if( !aSymbol )
1131 return swappedPins;
1132
1133 SCH_SCREEN* screen = aReference.GetSheetPath().LastScreen();
1134
1135 if( !screen )
1136 return swappedPins;
1137
1138 wxCHECK( m_frame, swappedPins );
1139
1140 // Used to build the list of schematic pins whose current net assignment does not match the PCB.
1141 struct PIN_CHANGE
1142 {
1143 SCH_PIN* pin;
1144 wxString pinNumber;
1145 wxString currentNet;
1146 wxString targetNet;
1147 };
1148
1149 std::vector<PIN_CHANGE> mismatches;
1150
1151 // Helper map that lets us find all pins currently on a given net.
1152 std::map<wxString, std::vector<size_t>> pinsByCurrentNet;
1153
1154 // Build the mismatch list by inspecting each footprint pin that differs from the schematic.
1155 for( const std::pair<const wxString, wxString>& entry : aFpData.m_pinMap )
1156 {
1157 const wxString& pinNumber = entry.first;
1158 const wxString& desiredNet = entry.second;
1159
1160 SCH_PIN* pin = aSymbol->GetPin( pinNumber );
1161
1162 // Ignore power pins and anything marked as non-connectable. Power pins can map to
1163 // hidden/global references, e.g. implicit power connections on logic symbols.
1164 // KiCad pins are currently always connectable, but the extra guard keeps the
1165 // logic robust if alternate pin types (e.g. explicit mechanical/NC pins)
1166 // ever start reporting false.
1167 if( !pin || pin->IsPower() || !pin->IsConnectable() )
1168 continue;
1169
1170 SCH_CONNECTION* connection = pin->Connection( &aReference.GetSheetPath() );
1171 wxString currentNet = connection ? connection->Name( true ) : wxString();
1172
1173 if( desiredNet.IsEmpty() || currentNet.IsEmpty() )
1174 continue;
1175
1176 if( desiredNet == currentNet )
1177 continue;
1178
1179 size_t idx = mismatches.size();
1180 mismatches.push_back( { pin, pinNumber, currentNet, desiredNet } );
1181 pinsByCurrentNet[currentNet].push_back( idx );
1182 }
1183
1184 if( mismatches.size() < 2 )
1185 return swappedPins;
1186
1187 // Track which mismatch entries we have already consumed, and the underlying pin objects
1188 // we still need to clean up wiring around once the geometry swap is done.
1189 std::vector<bool> handled( mismatches.size(), false );
1190 std::vector<SCH_PIN*> swappedPinObjects;
1191 bool swappedLibPins = false;
1192 wxString msg;
1193
1194 bool allowPinSwaps = false;
1195 wxString currentProjectName = m_frame->Prj().GetProjectName();
1196
1197 if( m_frame->eeconfig() )
1198 allowPinSwaps = m_frame->eeconfig()->m_Input.allow_unconstrained_pin_swaps;
1199
1200 std::set<wxString> sharedSheetPaths;
1201 std::set<wxString> sharedProjectNames;
1202 bool sharedSheetSymbol = SymbolHasSheetInstances( *aSymbol, currentProjectName,
1203 &sharedSheetPaths, &sharedProjectNames );
1204
1205 std::set<wxString> friendlySheetNames;
1206
1207 if( sharedSheetSymbol && !sharedSheetPaths.empty() )
1208 friendlySheetNames = GetSheetNamesFromPaths( sharedSheetPaths, m_frame->Schematic() );
1209
1210 // Check each mismatch and try to find a partner whose desired net matches our current net
1211 // (i.e. the two pins have been swapped on the PCB).
1212 for( size_t i = 0; i < mismatches.size(); ++i )
1213 {
1214 // Skip entries already handled by a previous successful swap.
1215 if( handled[i] )
1216 continue;
1217
1218 PIN_CHANGE& change = mismatches[i];
1219
1220 // Find candidate pins whose current net equals the net we want to move this pin to.
1221 auto range = pinsByCurrentNet.find( change.targetNet );
1222
1223 // Nobody currently on the desired net, so there’s no reciprocal swap to apply.
1224 if( range == pinsByCurrentNet.end() )
1225 continue;
1226
1227 // Track the best partner index in case we discover a reciprocal mismatch below.
1228 size_t partnerIdx = std::numeric_limits<size_t>::max();
1229
1230 // Examine every pin that presently lives on the net we want to move to.
1231 for( size_t candidateIdx : range->second )
1232 {
1233 if( candidateIdx == i || handled[candidateIdx] )
1234 continue;
1235
1236 PIN_CHANGE& candidate = mismatches[candidateIdx];
1237
1238 // Potential swap partner must want to move to our current net, i.e. the PCB swapped the
1239 // two nets between these pins.
1240 if( candidate.targetNet == change.currentNet )
1241 {
1242 partnerIdx = candidateIdx;
1243 break;
1244 }
1245 }
1246
1247 // No viable partner found; either there was no reciprocal net mismatch or it was already
1248 // consumed. Leave this entry for label-based handling.
1249 if( partnerIdx == std::numeric_limits<size_t>::max() )
1250 continue;
1251
1252 PIN_CHANGE& partner = mismatches[partnerIdx];
1253
1254 // Sanity check: both pins must belong to the same schematic symbol before we swap geometry;
1255 // this prevents us from moving pin outlines between different units of a multi-unit symbol.
1256 if( change.pin->GetParentSymbol() != partner.pin->GetParentSymbol() )
1257 continue;
1258
1259 if( !allowPinSwaps || sharedSheetSymbol )
1260 {
1261 if( !sharedProjectNames.empty() )
1262 {
1263 std::vector<wxString> otherProjects;
1264
1265 for( const wxString& name : sharedProjectNames )
1266 {
1267 if( !currentProjectName.IsEmpty() && name.IsSameAs( currentProjectName ) )
1268 continue;
1269
1270 otherProjects.push_back( name );
1271 }
1272
1273 wxString projects = AccumulateDescriptions( otherProjects );
1274
1275 if( projects.IsEmpty() )
1276 {
1277 msg.Printf( _( "Would swap %s pins %s and %s to match PCB, but the symbol is shared across other projects." ),
1278 DescribeRef( aReference.GetRef() ),
1279 EscapeHTML( change.pin->GetShownNumber() ),
1280 EscapeHTML( partner.pin->GetShownNumber() ) );
1281 }
1282 else
1283 {
1284 msg.Printf( _( "Would swap %s pins %s and %s to match PCB, but the symbol is shared across other "
1285 "projects (%s)." ),
1286 aReference.GetRef(), EscapeHTML( change.pin->GetShownNumber() ),
1287 EscapeHTML( partner.pin->GetShownNumber() ), projects );
1288 }
1289 }
1290 else if( !friendlySheetNames.empty() )
1291 {
1292 wxString sheets = AccumulateDescriptions( friendlySheetNames );
1293
1294 msg.Printf( _( "Would swap %s pins %s and %s to match PCB, but the symbol is used by multiple sheet "
1295 "instances (%s)." ),
1296 DescribeRef( aReference.GetRef() ),
1297 EscapeHTML( change.pin->GetShownNumber() ),
1298 EscapeHTML( partner.pin->GetShownNumber() ), sheets );
1299 }
1300 else if( sharedSheetSymbol )
1301 {
1302 msg.Printf( _( "Would swap %s pins %s and %s to match PCB, but the symbol is shared." ),
1303 DescribeRef( aReference.GetRef() ),
1304 EscapeHTML( change.pin->GetShownNumber() ),
1305 EscapeHTML( partner.pin->GetShownNumber() ) );
1306 }
1307 else
1308 {
1309 msg.Printf( _( "Would swap %s pins %s and %s to match PCB, but unconstrained pin swaps are disabled in "
1310 "the schematic preferences." ),
1311 DescribeRef( aReference.GetRef() ),
1312 EscapeHTML( change.pin->GetShownNumber() ),
1313 EscapeHTML( partner.pin->GetShownNumber() ) );
1314 }
1315 m_reporter.ReportHead( msg, RPT_SEVERITY_INFO );
1316
1317 handled[i] = true;
1318 handled[partnerIdx] = true;
1319 continue;
1320 }
1321
1322 if( !m_dryRun )
1323 {
1324 wxCHECK2( aCommit, continue );
1325
1326 // Record the two pins in the commit and physically swap their local geometry.
1327 aCommit->Modify( change.pin, screen, RECURSE_MODE::RECURSE );
1328 aCommit->Modify( partner.pin, screen, RECURSE_MODE::RECURSE );
1329
1330 swappedLibPins |= SwapPinGeometry( change.pin, partner.pin );
1331 }
1332
1333 // Don’t pick either entry again for another pairing.
1334 handled[i] = true;
1335 handled[partnerIdx] = true;
1336
1337 // Remember which pin numbers we touched so the caller can suppress duplicate work.
1338 swappedPins.insert( change.pinNumber );
1339 swappedPins.insert( partner.pinNumber );
1340 swappedPinObjects.push_back( change.pin );
1341 swappedPinObjects.push_back( partner.pin );
1342
1344
1345 msg.Printf( _( "Swap %s pins %s and %s to match PCB." ),
1346 DescribeRef( aReference.GetRef() ),
1347 EscapeHTML( change.pin->GetShownNumber() ),
1348 EscapeHTML( partner.pin->GetShownNumber() ) );
1349 m_reporter.ReportHead( msg, RPT_SEVERITY_ACTION );
1350 }
1351
1352 // Nothing left to do if we didn't find a valid swap pair when we were in dry-run mode.
1353 if( swappedPinObjects.empty() )
1354 return swappedPins;
1355
1356 if( !m_dryRun )
1357 {
1358 if( swappedLibPins )
1359 aSymbol->UpdatePins();
1360
1361 m_frame->UpdateItem( aSymbol, false, true );
1362
1363 if( TOOL_MANAGER* toolMgr = m_frame->GetToolManager() )
1364 {
1365 if( SCH_LINE_WIRE_BUS_TOOL* lwbTool = toolMgr->GetTool<SCH_LINE_WIRE_BUS_TOOL>() )
1366 {
1367 SCH_SELECTION cleanupSelection( screen );
1368
1369 // Make sure we tidy up any wires connected to the pins whose geometry just moved.
1370 for( SCH_PIN* swappedPin : swappedPinObjects )
1371 cleanupSelection.Add( swappedPin );
1372
1373 lwbTool->TrimOverLappingWires( aCommit, &cleanupSelection );
1374 lwbTool->AddJunctionsIfNeeded( aCommit, &cleanupSelection );
1375 }
1376 }
1377
1378 m_frame->Schematic().CleanUp( aCommit );
1379 }
1380
1381 return swappedPins;
1382}
1383
1384
1385void BACK_ANNOTATE::processNetNameChange( SCH_COMMIT* aCommit, const wxString& aRef, SCH_PIN* aPin,
1386 const SCH_CONNECTION* aConnection,
1387 const wxString& aOldName, const wxString& aNewName )
1388{
1389 wxString msg;
1390
1391 // Find a physically-connected driver. We can't use the SCH_CONNECTION's m_driver because
1392 // it has already been resolved by merging subgraphs with the same label, etc., and our
1393 // name change may cause that resolution to change.
1394
1395 std::set<SCH_ITEM*> connectedItems;
1396 SCH_ITEM* driver = nullptr;
1398
1399 addConnections( aPin, aConnection->Sheet(), connectedItems );
1400
1401 for( SCH_ITEM* item : connectedItems )
1402 {
1404
1405 if( priority > driverPriority )
1406 {
1407 driver = item;
1408 driverPriority = priority;
1409 }
1410 }
1411
1412 switch( driver->Type() )
1413 {
1414 case SCH_LABEL_T:
1415 case SCH_GLOBAL_LABEL_T:
1416 case SCH_HIER_LABEL_T:
1417 case SCH_SHEET_PIN_T:
1419
1420 msg.Printf( _( "Change %s pin %s net label from '%s' to '%s'." ),
1421 DescribeRef( aRef ),
1422 EscapeHTML( aPin->GetShownNumber() ),
1423 EscapeHTML( aOldName ),
1424 EscapeHTML( aNewName ) );
1425
1426 if( !m_dryRun )
1427 {
1428 aCommit->Modify( driver, aConnection->Sheet().LastScreen() );
1429 static_cast<SCH_LABEL_BASE*>( driver )->SetText( aNewName );
1430 }
1431
1432 m_reporter.ReportHead( msg, RPT_SEVERITY_ACTION );
1433 break;
1434
1435 case SCH_PIN_T:
1436 if( static_cast<SCH_PIN*>( driver )->IsPower() )
1437 {
1438 msg.Printf( _( "Net %s cannot be changed to %s because it is driven by a power pin." ),
1439 EscapeHTML( aOldName ),
1440 EscapeHTML( aNewName ) );
1441
1442 m_reporter.ReportHead( msg, RPT_SEVERITY_ERROR );
1443 break;
1444 }
1445
1446 // The physical connection walk could not find a label driver. This can happen when
1447 // a label sits in the middle of an unsplit wire. Fall back to the connection graph's
1448 // resolved driver which handles this case through subgraph merging.
1449 {
1450 CONNECTION_GRAPH* connGraph = m_frame->Schematic().ConnectionGraph();
1451 CONNECTION_SUBGRAPH* sg = connGraph ? connGraph->GetSubgraphForItem( aPin ) : nullptr;
1452
1454 {
1455 SCH_ITEM* resolvedDriver = const_cast<SCH_ITEM*>( sg->GetDriver() );
1456
1458 msg.Printf( _( "Change %s pin %s net label from '%s' to '%s'." ), DescribeRef( aRef ),
1459 EscapeHTML( aPin->GetShownNumber() ), EscapeHTML( aOldName ), EscapeHTML( aNewName ) );
1460
1461 if( !m_dryRun )
1462 {
1463 if( resolvedDriver->Type() == SCH_PIN_T )
1464 {
1465 SCH_PIN* powerPin = static_cast<SCH_PIN*>( resolvedDriver );
1466 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( powerPin->GetParentSymbol() );
1467
1468 aCommit->Modify( symbol, sg->GetSheet().LastScreen() );
1469 symbol->GetField( FIELD_T::VALUE )->SetText( aNewName );
1470 }
1471 else
1472 {
1473 aCommit->Modify( resolvedDriver, sg->GetSheet().LastScreen() );
1474 static_cast<SCH_LABEL_BASE*>( resolvedDriver )->SetText( aNewName );
1475 }
1476 }
1477
1478 m_reporter.ReportHead( msg, RPT_SEVERITY_ACTION );
1479 break;
1480 }
1481 }
1482
1484 msg.Printf( _( "Add label '%s' to %s pin %s net." ),
1485 EscapeHTML( aNewName ),
1486 DescribeRef( aRef ),
1487 EscapeHTML( aPin->GetShownNumber() ) );
1488
1489 if( !m_dryRun )
1490 {
1491 SCHEMATIC_SETTINGS& settings = m_frame->Schematic().Settings();
1492 SCH_LABEL* label = new SCH_LABEL( driver->GetPosition(), aNewName );
1493 label->SetParent( &m_frame->Schematic() );
1494 label->SetTextSize( VECTOR2I( settings.m_DefaultTextSize, settings.m_DefaultTextSize ) );
1495 label->SetSpinStyle( orientLabel( static_cast<SCH_PIN*>( driver ) ) );
1496 label->SetFlags( IS_NEW );
1497
1498 SCH_SCREEN* screen = aConnection->Sheet().LastScreen();
1499 aCommit->Add( label, screen );
1500 }
1501
1502 m_reporter.ReportHead( msg, RPT_SEVERITY_ACTION );
1503 break;
1504
1505 default:
1506 break;
1507 }
1508}
const char * name
static std::vector< wxString > netsInUnitOrder(const std::vector< wxString > &aPins, const std::map< wxString, wxString > &aNetByPin)
void addConnections(SCH_ITEM *aItem, const SCH_SHEET_PATH &aSheetPath, std::set< SCH_ITEM * > &connectedItems)
static SPIN_STYLE orientLabel(SCH_PIN *aPin)
BACKANNOTATE_UNIT_SWAP_PLAN PlanBackannotateUnitSwaps(const std::vector< BACKANNOTATE_UNIT_SWAP_CANDIDATE > &aCandidates, const std::map< wxString, wxString > &aPcbPinMap)
Compute a pure unit-swap plan from schematic-side unit definitions and the final PCB pin map.
BACKANNOTATE_UNIT_SWAP_PLAN PlanBackannotateUnitSwaps(const std::vector< BACKANNOTATE_UNIT_SWAP_CANDIDATE > &aCandidates, const std::map< wxString, wxString > &aPcbPinMap)
Compute a pure unit-swap plan from schematic-side unit definitions and the final PCB pin map.
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
bool BackAnnotateSymbols(const std::string &aNetlist)
Run back annotation algorithm.
SCH_MULTI_UNIT_REFERENCE_MAP m_multiUnitsRefs
std::deque< CHANGELIST_ITEM > m_changelist
std::pair< SCH_REFERENCE, std::shared_ptr< PCB_FP_DATA > > CHANGELIST_ITEM
bool m_processReferences
bool m_processFootprints
bool m_processOtherFields
void getPcbModulesFromString(const std::string &aPayload)
Parse netlist sent over KiWay express mail interface and fill m_pcbModules.
SCH_EDIT_FRAME * m_frame
void checkForUnusedSymbols()
Check if some symbols are not represented in PCB footprints and vice versa.
SCH_REFERENCE_LIST m_refs
bool FetchNetlistFromPCB(std::string &aNetlist)
Get netlist from the Pcbnew.
PCB_FOOTPRINTS_MAP m_pcbFootprints
void processNetNameChange(SCH_COMMIT *aCommit, const wxString &aRef, SCH_PIN *aPin, const SCH_CONNECTION *aConnection, const wxString &aOldName, const wxString &aNewName)
REPORTER & m_reporter
bool m_processAttributes
BACK_ANNOTATE(SCH_EDIT_FRAME *aFrame, REPORTER &aReporter, bool aRelinkFootprints, bool aProcessFootprints, bool aProcessValues, bool aProcessReferences, bool aProcessNetNames, bool aProcessAttributes, bool aProcessOtherFields, bool aPreferUnitSwaps, bool aPreferPinSwaps, bool aDryRun)
std::set< wxString > applyPinSwaps(SCH_SYMBOL *aSymbol, const SCH_REFERENCE &aReference, const PCB_FP_DATA &aFpData, SCH_COMMIT *aCommit)
Handle footprint pad net swaps with symbol pin swaps where possible.
bool m_matchByReference
void PushNewLinksToPCB()
void applyChangelist()
Apply changelist to the schematic.
COMMIT & Modify(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr, RECURSE_MODE aRecurse=RECURSE_MODE::NO_RECURSE)
Modify a given item in the model.
Definition commit.h:102
COMMIT & Add(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Add a new item to the model.
Definition commit.h:74
Calculate the connectivity of a schematic and generates netlists.
CONNECTION_SUBGRAPH * GetSubgraphForItem(SCH_ITEM *aItem) const
A subgraph is a set of items that are electrically connected on a single sheet.
const SCH_ITEM * GetDriver() const
static PRIORITY GetDriverPriority(SCH_ITEM *aDriver)
Return the priority (higher is more important) of a candidate driver.
const SCH_SHEET_PATH & GetSheet() const
Implement a lexical analyzer for the SPECCTRA DSN file format.
Definition dsnlexer.h:75
virtual VECTOR2I GetPosition() const
Definition eda_item.h:282
void SetFlags(EDA_ITEM_FLAGS aMask)
Definition eda_item.h:152
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:108
void ClearFlags(EDA_ITEM_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition eda_item.h:154
virtual void SetParent(EDA_ITEM *aParent)
Definition eda_item.cpp:89
EDA_ITEM_FLAGS GetFlags() const
Definition eda_item.h:155
virtual void SetTextSize(VECTOR2I aNewSize, bool aEnforceMinTextSize=true)
Definition eda_text.cpp:532
virtual void SetTextPos(const VECTOR2I &aPoint)
Definition eda_text.cpp:576
bool HasTextVars() const
Indicates the ShownText has text var references which need to be processed.
Definition eda_text.h:129
virtual void SetVisible(bool aVisible)
Definition eda_text.cpp:381
A wxFrame capable of the OpenProjectFiles function, meaning it can load a portion of a KiCad project.
virtual bool OpenProjectFiles(const std::vector< wxString > &aFileList, int aCtl=0)
Open a project or set of files given by aFileList.
Define a library symbol object.
Definition lib_symbol.h:79
std::vector< UNIT_PIN_INFO > GetUnitPinInfo() const
Return pin-number lists for each unit, ordered consistently for gate swapping.
static REPORTER & GetInstance()
Definition reporter.cpp:120
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:71
virtual REPORTER & ReportHead(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Places the report at the beginning of the list for objects that support ordering.
Definition reporter.h:119
These are loaded from Eeschema settings but then overwritten by the project settings.
virtual void Push(const wxString &aMessage=wxT("A commit"), int aCommitFlags=0) override
Execute the changes.
Each graphical item can have a SCH_CONNECTION describing its logical connection (to a bus or net).
SCH_SHEET_PATH Sheet() const
wxString Name(bool aIgnoreSheet=false) const
Schematic editor (Eeschema) main window.
wxString GetCanonicalName() const
Get a non-language-specific name for a field which can be used for storage, variable look-up,...
wxString GetShownText(const SCH_SHEET_PATH *aPath, bool aAllowExtraText, int aDepth=0, const wxString &aVariantName=wxEmptyString) const
void SetText(const wxString &aText) override
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:162
const SYMBOL * GetParentSymbol() const
Definition sch_item.cpp:274
int GetUnit() const
Definition sch_item.h:233
const std::vector< SCH_ITEM * > & ConnectedItems(const SCH_SHEET_PATH &aPath)
Retrieve the set of items connected to this item on the given sheet.
Definition sch_item.cpp:558
virtual void SetSpinStyle(SPIN_STYLE aSpinStyle)
Tool responsible for drawing/placing items (symbols, wires, buses, labels, etc.)
SCH_PIN * GetLibPin() const
Definition sch_pin.h:92
PIN_ORIENTATION GetOrientation() const
Definition sch_pin.cpp:353
const wxString & GetShownNumber() const
Definition sch_pin.cpp:682
A helper to define a symbol's reference designator in a schematic.
const SCH_SHEET_PATH & GetSheetPath() const
const wxString GetFootprint() const
SCH_SYMBOL * GetSymbol() const
wxString GetRef() const
const wxString GetValue() const
int GetUnit() const
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
void GetMultiUnitSymbols(SCH_MULTI_UNIT_REFERENCE_MAP &aRefList, SYMBOL_FILTER aSymbolFilter) const
Add a SCH_REFERENCE_LIST object to aRefList for each same-reference set of multi-unit parts in the li...
void GetSymbols(SCH_REFERENCE_LIST &aReferences, SYMBOL_FILTER aSymbolFilter, bool aForceIncludeOrphanSymbols=false) const
Add a SCH_REFERENCE object to aReferences for each symbol in the list of sheets.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
SCH_SCREEN * LastScreen()
Schematic symbol object.
Definition sch_symbol.h:69
virtual void SetDNP(bool aEnable, const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) override
void RemoveField(const wxString &aFieldName)
Remove a user field from the symbol.
std::vector< const SCH_PIN * > GetPins(const SCH_SHEET_PATH *aSheet) const
Retrieve a list of the SCH_PINs for the given sheet path.
bool GetExcludedFromBOM(const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) const override
void UpdatePins()
Updates the cache of SCH_PIN objects for each pin.
void SetRef(const SCH_SHEET_PATH *aSheet, const wxString &aReference)
Set the reference for the given sheet path for this symbol.
void GetFields(std::vector< SCH_FIELD * > &aVector, bool aVisibleOnly) const override
Populate a std::vector with SCH_FIELDs, sorted in ordinal order.
void SetFootprintFieldText(const wxString &aFootprint)
VECTOR2I GetPosition() const override
Definition sch_symbol.h:885
void SetExcludedFromPosFiles(bool aEnable, const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) override
void SetValueFieldText(const wxString &aValue, const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString)
bool GetExcludedFromPosFiles(const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) const override
SCH_FIELD * AddField(const SCH_FIELD &aField)
Add a field to the symbol.
bool GetExcludedFromBoard(const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) const override
int GetUnitSelection(const SCH_SHEET_PATH *aSheet) const
Return the instance-specific unit selection for the given sheet path.
SCH_PIN * GetPin(const wxString &number) const
Find a symbol pin by number.
int GetOrientation() const override
Get the display symbol orientation.
void SetExcludedFromBOM(bool aEnable, const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) override
Set or clear the exclude from schematic bill of materials flag.
std::unique_ptr< LIB_SYMBOL > & GetLibSymbolRef()
Definition sch_symbol.h:177
virtual bool GetDNP(const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) const override
Set or clear the 'Do Not Populate' flag.
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const override
SCH_FIELD * GetField(FIELD_T aFieldType)
Return a mandatory field in this symbol.
virtual void Add(EDA_ITEM *aItem)
Definition selection.cpp:38
SPIN_STYLE MirrorX()
Mirror the label spin style across the X axis or simply swaps up and bottom.
SPIN_STYLE MirrorY()
Mirror the label spin style across the Y axis or simply swaps left and right.
SPIN_STYLE RotateCCW()
Master controller class:
An 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to and fro...
Definition utf8.h:67
wxString DescribeRef(const wxString &aRef)
Returns a user-visible HTML string describing a footprint reference designator.
Definition common.cpp:464
The common library.
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition confirm.cpp:217
This file is part of the common library.
#define _(s)
@ RECURSE
Definition eda_item.h:49
@ NO_RECURSE
Definition eda_item.h:50
#define IS_NEW
New item, just created.
#define SKIP_STRUCT
flag indicating that the structure should be ignored
@ FRAME_PCB_EDITOR
Definition frame_type.h:38
@ MAIL_PCB_UPDATE_LINKS
Definition mail_type.h:49
@ MAIL_PCB_GET_NETLIST
Definition mail_type.h:48
@ PIN_UP
The pin extends upwards from the connection point: Probably on the bottom side of the symbol.
Definition pin_type.h:123
@ PIN_RIGHT
The pin extends rightwards from the connection point.
Definition pin_type.h:107
@ PIN_LEFT
The pin extends leftwards from the connection point: Probably on the right side of the symbol.
Definition pin_type.h:114
@ PIN_DOWN
The pin extends downwards from the connection: Probably on the top side of the symbol.
Definition pin_type.h:131
void Scan(PTREE *aTree, DSNLEXER *aLexer)
Fill an empty PTREE with information from a KiCad s-expression stream.
Definition ptree.cpp:80
boost::property_tree::ptree PTREE
Definition ptree.h:48
@ RPT_SEVERITY_WARNING
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_INFO
@ RPT_SEVERITY_ACTION
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
@ SYMBOL_FILTER_NON_POWER
@ SYMBOL_FILTER_ALL
std::set< wxString > GetSheetNamesFromPaths(const std::set< wxString > &aSheetPaths, const SCHEMATIC &aSchematic)
Get human-readable sheet names from a set of sheet paths, e.g.
bool SwapPinGeometry(SCH_PIN *aFirst, SCH_PIN *aSecond)
Swap the positions/lengths/etc.
bool SymbolHasSheetInstances(const SCH_SYMBOL &aSymbol, const wxString &aCurrentProject, std::set< wxString > *aSheetPaths, std::set< wxString > *aProjectNames)
Returns true when the given symbol has instances, e.g.
@ NO_CLEANUP
Definition schematic.h:77
wxString EscapeHTML(const wxString &aString)
Return a new wxString escaped for embedding in HTML.
wxString From_UTF8(const char *cstring)
void AccumulateDescriptions(wxString &aDesc, const T &aItemCollection)
Build a comma-separated list from a collection of wxStrings.
std::vector< wxString > m_unitPinNumbers
std::map< wxString, wxString > m_schNetsByPin
std::vector< BACKANNOTATE_UNIT_SWAP_STEP > m_steps
std::set< size_t > m_swappedCandidateIndices
Container for Pcbnew footprint data.Map to hold NETLIST footprints data.
std::map< wxString, wxString > m_pinMap
std::map< wxString, wxString > m_fieldsMap
@ SYM_ORIENT_270
Definition symbol.h:38
@ SYM_MIRROR_Y
Definition symbol.h:40
@ SYM_ORIENT_180
Definition symbol.h:37
@ SYM_MIRROR_X
Definition symbol.h:39
@ SYM_ORIENT_90
Definition symbol.h:36
@ SYM_ORIENT_0
Definition symbol.h:35
@ USER
The field ID hasn't been set yet; field is invalid.
@ FOOTPRINT
Field Name Module PCB, i.e. "16DIP300".
@ REFERENCE
Field Reference of part, i.e. "IC21".
@ VALUE
Field Value of part, i.e. "3.3K".
wxString GetCanonicalFieldName(FIELD_T aFieldType)
std::string path
KIBIS_PIN * partner
KIBIS_PIN * pin
#define kv
@ SCH_LABEL_T
Definition typeinfo.h:164
@ SCH_HIER_LABEL_T
Definition typeinfo.h:166
@ SCH_SHEET_PIN_T
Definition typeinfo.h:171
@ SCH_GLOBAL_LABEL_T
Definition typeinfo.h:165
@ SCH_PIN_T
Definition typeinfo.h:150
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683
Definition of file extensions used in Kicad.