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