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, exPosFiles = 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, Exclude from BOM, and Exclude from Position Files out of the
337 // properties if they exist
338 for( const auto& child : item.second )
339 {
340 if( child.first != "property" )
341 continue;
342
343 auto property = child.second;
344 auto name = property.get_child_optional( "name" );
345
346 if( !name )
347 continue;
348
349 if( name.get().front().first == "dnp" )
350 dnp = true;
351 else if( name.get().front().first == "exclude_from_bom" )
352 exBOM = true;
353 else if( name.get().front().first == "exclude_from_pos_files" )
354 exPosFiles = true;
355 }
356
357 boost::optional<const PTREE&> nets = item.second.get_child_optional( "nets" );
358
359 if( nets )
360 {
361 for( const std::pair<const std::string, PTREE>& pin_net : nets.get() )
362 {
363 wxASSERT( pin_net.first == "pin_net" );
364 wxString pinNumber = UTF8( pin_net.second.front().first );
365 wxString netName = UTF8( pin_net.second.back().first );
366 pinNetMap[ pinNumber ] = netName;
367 }
368 }
369 }
370 catch( ... )
371 {
372 wxLogWarning( "Cannot parse PCB netlist for back-annotation." );
373 }
374
375 // Use lower_bound for not to iterate over map twice
376 auto nearestItem = m_pcbFootprints.lower_bound( path );
377
378 if( nearestItem != m_pcbFootprints.end() && nearestItem->first == path )
379 {
380 // Module with this path already exists - generate error
381 msg.Printf( _( "Footprints '%s' and '%s' linked to same symbol." ),
382 DescribeRef( nearestItem->second->m_ref ),
383 DescribeRef( ref ) );
384 m_reporter.ReportHead( msg, RPT_SEVERITY_ERROR );
385 }
386 else
387 {
388 // Add footprint to the map
389 std::shared_ptr<PCB_FP_DATA> data = std::make_shared<PCB_FP_DATA>( ref, footprint, value, dnp, exBOM,
390 exPosFiles, pinNetMap, fieldsMap );
391 m_pcbFootprints.insert( nearestItem, std::make_pair( path, data ) );
392 }
393 }
394}
395
396
398{
399 for( const auto& [pcbPath, pcbData] : m_pcbFootprints )
400 {
401 int refIndex;
402 bool foundInMultiunit = false;
403
404 for( const auto& [_, refList] : m_multiUnitsRefs )
405 {
407 refIndex = refList.FindRef( pcbPath );
408 else
409 refIndex = refList.FindRefByFullPath( pcbPath );
410
411 if( refIndex >= 0 )
412 {
413 // If footprint linked to multi unit symbol, we add all symbol's units to
414 // the change list
415 foundInMultiunit = true;
416
417 for( size_t i = 0; i < refList.GetCount(); ++i )
418 {
419 refList[ i ].GetSymbol()->ClearFlags(SKIP_STRUCT );
420 m_changelist.emplace_back( CHANGELIST_ITEM( refList[i], pcbData ) );
421 }
422
423 break;
424 }
425 }
426
427 if( foundInMultiunit )
428 continue;
429
431 refIndex = m_refs.FindRef( pcbPath );
432 else
433 refIndex = m_refs.FindRefByFullPath( pcbPath );
434
435 if( refIndex >= 0 )
436 {
437 m_refs[ refIndex ].GetSymbol()->ClearFlags( SKIP_STRUCT );
438 m_changelist.emplace_back( CHANGELIST_ITEM( m_refs[refIndex], pcbData ) );
439 }
440 else
441 {
442 // Haven't found linked symbol in multiunits or common refs. Generate error
443 m_reporter.ReportTail( wxString::Format( _( "Cannot find symbol for footprint '%s'." ),
444 DescribeRef( pcbData->m_ref ) ),
446 }
447 }
448}
449
451{
452 m_refs.SortByTimeStamp();
453
454 std::sort( m_changelist.begin(), m_changelist.end(),
455 []( const CHANGELIST_ITEM& a, const CHANGELIST_ITEM& b )
456 {
457 return SCH_REFERENCE_LIST::sortByTimeStamp( a.first, b.first );
458 } );
459
460 size_t i = 0;
461
462 for( const std::pair<SCH_REFERENCE, std::shared_ptr<PCB_FP_DATA>>& item : m_changelist )
463 {
464 // Refs and changelist are both sorted by paths, so we just go over m_refs and
465 // generate errors before we will find m_refs member to which item linked
466 while( i < m_refs.GetCount() && m_refs[i].GetPath() != item.first.GetPath() )
467 {
468 const SCH_REFERENCE& ref = m_refs[i];
469
470 if( ref.GetSymbol()->GetExcludedFromBoard() )
471 {
472 m_reporter.ReportTail( wxString::Format( _( "Footprint '%s' is not present on PCB. "
473 "Corresponding symbols in schematic must be "
474 "manually deleted (if desired)." ),
475 DescribeRef( m_refs[i].GetRef() ) ),
477 }
478
479 ++i;
480 }
481
482 ++i;
483 }
484
485 if( m_matchByReference && !m_frame->ReadyToNetlist( _( "Re-linking footprints requires a fully "
486 "annotated schematic." ) ) )
487 {
488 m_reporter.ReportTail( _( "Footprint re-linking canceled by user." ), RPT_SEVERITY_ERROR );
489 }
490}
491
492
494{
495 SCH_COMMIT commit( m_frame );
496 wxString msg;
497
498 std::set<CHANGELIST_ITEM*> unitSwapItems;
499
500 // First, optionally handle unit swaps across multi-unit symbols where possible
501 // This needs to happen before the rest of the normal changelist processing,
502 // because the swaps will modify the changelist
504 {
505 REPORTER& debugReporter = NULL_REPORTER::GetInstance(); // Change to m_reporter for debugging
506
507 // Group changelist items by shared PCB footprint data pointer
508 std::map<std::shared_ptr<PCB_FP_DATA>, std::vector<CHANGELIST_ITEM*>> changesPerFp;
509
510 msg.Printf( wxT( "DEBUG(unit-swap): grouped changelist into %zu footprint(s) for unit-swap processing:" ),
511 changesPerFp.size() );
512
513 // The changelist will have multiple entries for multi-unit symbols, each unit ref, e.g. U1A, U1B
514 // will point to the same PCB_FP_DATA. Grouping by pointer allows us to handle all units of a symbol
515 // together by working on all changes to the same PCB footprint at once.
516 for( CHANGELIST_ITEM& item : m_changelist )
517 {
518 changesPerFp[item.second].push_back( &item );
519 msg += wxT( " " ) + item.first.GetRef();
520 }
521
522 debugReporter.ReportHead( msg, RPT_SEVERITY_INFO );
523
524 // Handle all changes per footprint
525 for( auto& fpChangelistPair : changesPerFp )
526 {
527 std::set<SCH_SYMBOL*> swappedSymbols;
528 std::set<CHANGELIST_ITEM*> swappedItems;
529 std::shared_ptr<PCB_FP_DATA> fp = fpChangelistPair.first;
530 auto& changedFpItems = fpChangelistPair.second;
531
532 // Build symbol unit list for this footprint (multi-unit candidates)
533 // Snapshot of one schematic unit instance (ref + sheet path + nets) for matching
534 struct SYM_UNIT
535 {
536 SCH_SYMBOL* sym = nullptr;
537 SCH_SCREEN* screen = nullptr;
538 const SCH_SHEET_PATH* sheetPath = nullptr;
539 wxString ref;
540 int currentUnit = 0;
541 // Track these so we avoid re-applying label/field changes to swapped units
542 CHANGELIST_ITEM* changeItem = nullptr;
543 std::map<wxString, wxString> schNetsByPin; // pinNumber -> net (schematic)
544 std::map<wxString, wxString> pcbNetsByPin; // pinNumber -> net (from PCB pin map)
545 std::vector<wxString> unitPinNumbers; // library-defined pin numbers per unit
546 std::vector<wxString> schNetsInUnitOrder;
547 std::vector<wxString> pcbNetsInUnitOrder;
548 };
549
550 // All symbol units for this footprint
551 std::vector<SYM_UNIT> symbolUnits;
552
553 std::map<LIB_SYMBOL*, std::vector<LIB_SYMBOL::UNIT_PIN_INFO>> unitPinsByLibSymbol;
554
555 auto getUnitPins =
556 [&]( SCH_SYMBOL* symbol, int unitNumber ) -> std::vector<wxString>
557 {
558 if( unitNumber <= 0 )
559 return {};
560
561 if( !symbol )
562 return {};
563
564 LIB_SYMBOL* libSymbol = symbol->GetLibSymbolRef().get();
565
566 if( !libSymbol )
567 return {};
568
569 auto found = unitPinsByLibSymbol.find( libSymbol );
570
571 if( found == unitPinsByLibSymbol.end() )
572 found = unitPinsByLibSymbol.emplace( libSymbol, libSymbol->GetUnitPinInfo() ).first;
573
574 const std::vector<LIB_SYMBOL::UNIT_PIN_INFO>& unitInfos = found->second;
575
576 if( unitNumber > static_cast<int>( unitInfos.size() ) )
577 return {};
578
579 return unitInfos[unitNumber - 1].m_pinNumbers;
580 };
581
582 for( CHANGELIST_ITEM* changedItem : changedFpItems )
583 {
584 SCH_REFERENCE& ref = changedItem->first;
585 SCH_SYMBOL* symbol = ref.GetSymbol();
586 SCH_SCREEN* screen = ref.GetSheetPath().LastScreen();
587
588 if( !symbol )
589 continue;
590
591 // Collect nets keyed by pin number. Ordering by XY is intentionally avoided
592 // here; see comment in SYM_UNIT above.
593 SYM_UNIT symbolUnit;
594 symbolUnit.sym = symbol;
595 symbolUnit.screen = screen;
596 symbolUnit.ref = symbol->GetRef( &ref.GetSheetPath(), true );
597 symbolUnit.sheetPath = &ref.GetSheetPath();
598 symbolUnit.changeItem = changedItem;
599
600 int currentUnit = ref.GetUnit();
601
602 if( currentUnit <= 0 )
603 currentUnit = symbol->GetUnitSelection( &ref.GetSheetPath() );
604
605 if( currentUnit <= 0 )
606 currentUnit = symbol->GetUnit();
607
608 symbolUnit.currentUnit = currentUnit;
609 symbolUnit.unitPinNumbers = getUnitPins( symbol, symbolUnit.currentUnit );
610
611 const SCH_SHEET_PATH& sheetPath = ref.GetSheetPath();
612
613 for( SCH_PIN* pin : symbol->GetPins( &ref.GetSheetPath() ) )
614 {
615 const wxString& pinNum = pin->GetNumber();
616
617 // PCB nets from footprint pin map
618 auto it = fp->m_pinMap.find( pinNum );
619 symbolUnit.pcbNetsByPin[pinNum] = ( it != fp->m_pinMap.end() ) ? it->second : wxString();
620
621 // Schematic nets from connections
622 if( SCH_PIN* p = symbol->GetPin( pinNum ) )
623 {
624 if( SCH_CONNECTION* connection = p->Connection( &sheetPath ) )
625 symbolUnit.schNetsByPin[pinNum] = connection->Name( true );
626 else
627 symbolUnit.schNetsByPin[pinNum] = wxString();
628 }
629 else
630 symbolUnit.schNetsByPin[pinNum] = wxString();
631 }
632
633 symbolUnit.pcbNetsInUnitOrder = netsInUnitOrder( symbolUnit.unitPinNumbers, symbolUnit.pcbNetsByPin );
634 symbolUnit.schNetsInUnitOrder = netsInUnitOrder( symbolUnit.unitPinNumbers, symbolUnit.schNetsByPin );
635
636 symbolUnits.push_back( symbolUnit );
637 }
638
639 auto vectorToString =
640 []( const std::vector<wxString>& values ) -> wxString
641 {
642 return fmt::format( L"{}", fmt::join( values, L", " ) );
643 };
644
645 auto mapToString =
646 [vectorToString]( const std::map<wxString, wxString>& pinMap ) -> wxString
647 {
648 std::vector<wxString> entries;
649
650 for( const std::pair<const wxString, wxString>& pin : pinMap )
651 entries.push_back( pin.first + '=' + pin.second );
652
653 return vectorToString( entries );
654 };
655
656 msg.Printf( wxT( "DEBUG(unit-swap): footprint %s processed (%zu units, dryRun=%d)." ), fp->m_ref,
657 symbolUnits.size(), m_dryRun ? 1 : 0 );
658 debugReporter.ReportHead( msg, RPT_SEVERITY_INFO );
659
660 // For debugging, sort the symbol units by ref
661 std::sort( symbolUnits.begin(), symbolUnits.end(),
662 []( const SYM_UNIT& a, const SYM_UNIT& b )
663 {
664 return a.ref < b.ref;
665 } );
666
667 for( const SYM_UNIT& su : symbolUnits )
668 {
669 wxString pcbPins = mapToString( su.pcbNetsByPin );
670 wxString schPins = mapToString( su.schNetsByPin );
671 wxString unitPins = vectorToString( su.unitPinNumbers );
672 wxString pcbUnitNetSeq = vectorToString( su.pcbNetsInUnitOrder );
673 wxString schUnitNetSeq = vectorToString( su.schNetsInUnitOrder );
674
675 msg.Printf( wxT( "DEBUG(unit-swap): unit %d: %s pcbPins[%s] schPins[%s] unitPins[%s] pcbUnitNets[%s] "
676 "schUnitNets[%s]." ),
677 su.currentUnit, su.ref, pcbPins, schPins, unitPins, pcbUnitNetSeq, schUnitNetSeq );
678 debugReporter.ReportHead( msg, RPT_SEVERITY_INFO );
679 }
680
681 if( symbolUnits.size() < 2 )
682 continue;
683
684 std::vector<BACKANNOTATE_UNIT_SWAP_CANDIDATE> candidates;
685
686 for( const SYM_UNIT& symbolUnit : symbolUnits )
687 {
689 candidate.m_ref = symbolUnit.ref;
690 candidate.m_currentUnit = symbolUnit.currentUnit;
691 candidate.m_schNetsByPin = symbolUnit.schNetsByPin;
692 candidate.m_unitPinNumbers = symbolUnit.unitPinNumbers;
693 candidates.push_back( candidate );
694 }
695
696 BACKANNOTATE_UNIT_SWAP_PLAN plan = PlanBackannotateUnitSwaps( candidates, fp->m_pinMap );
697
698 if( !plan.m_mappingOk )
699 {
700 msg.Printf( wxT( "DEBUG(unit-swap): mapping failed for footprint %s." ), fp->m_ref );
701 debugReporter.ReportHead( msg, RPT_SEVERITY_INFO );
702 continue;
703 }
704
705 if( plan.m_identity )
706 {
707 msg.Printf( wxT( "DEBUG(unit-swap): footprint %s already aligned (identity mapping)." ), fp->m_ref );
708 debugReporter.ReportHead( msg, RPT_SEVERITY_INFO );
709 continue;
710 }
711
712 if( !m_dryRun )
713 {
714 for( size_t candidateIdx : plan.m_swappedCandidateIndices )
715 commit.Modify( symbolUnits[candidateIdx].sym, symbolUnits[candidateIdx].screen );
716 }
717
718 for( const BACKANNOTATE_UNIT_SWAP_STEP& step : plan.m_steps )
719 {
720 SYM_UNIT& a = symbolUnits[step.m_firstIndex];
721 SYM_UNIT& b = symbolUnits[step.m_secondIndex];
722 int aUnit = a.currentUnit;
723 int bUnit = b.currentUnit;
724
725 if( !m_dryRun )
726 {
727 a.sym->SetUnit( bUnit );
728 b.sym->SetUnit( aUnit );
729
730 if( const SCH_SHEET_PATH* sheet = a.sheetPath )
731 a.sym->SetUnitSelection( sheet, bUnit );
732
733 if( const SCH_SHEET_PATH* sheet = b.sheetPath )
734 b.sym->SetUnitSelection( sheet, aUnit );
735 }
736
737 a.currentUnit = bUnit;
738 b.currentUnit = aUnit;
739
740 swappedSymbols.insert( a.sym );
741 swappedSymbols.insert( b.sym );
742
743 if( a.changeItem )
744 {
745 swappedItems.insert( a.changeItem );
746 unitSwapItems.insert( a.changeItem );
747 }
748
749 if( b.changeItem )
750 {
751 swappedItems.insert( b.changeItem );
752 unitSwapItems.insert( b.changeItem );
753 }
754
755 wxString baseRef = a.sym->GetRef( a.sheetPath, false );
756 wxString unitAString = a.sym->SubReference( aUnit, false );
757 wxString unitBString = b.sym->SubReference( bUnit, false );
758
759 if( unitAString.IsEmpty() )
760 unitAString.Printf( wxT( "%d" ), aUnit );
761
762 if( unitBString.IsEmpty() )
763 unitBString.Printf( wxT( "%d" ), bUnit );
764
765 msg.Printf( _( "Swap %s unit %s with unit %s." ),
766 DescribeRef( baseRef ),
767 unitAString,
768 unitBString );
769 m_reporter.ReportHead( msg, RPT_SEVERITY_ACTION );
771 }
772
773 msg.Printf( wxT( "DEBUG(unit-swap): applied %zu swap steps for footprint %s." ),
774 plan.m_steps.size(), fp->m_ref );
775 debugReporter.ReportHead( msg, RPT_SEVERITY_INFO );
776
777 // Remove label updates for swapped symbols by marking their SKIP_STRUCT flag
778 if( !m_dryRun )
779 {
780 for( CHANGELIST_ITEM* changedItem : changedFpItems )
781 {
782 SCH_SYMBOL* symbol = changedItem->first.GetSymbol();
783
784 if( !symbol )
785 continue;
786
787 if( swappedItems.count( changedItem ) || swappedSymbols.count( symbol ) )
788 symbol->SetFlags( SKIP_STRUCT );
789
790 int updatedUnit = symbol->GetUnitSelection( &changedItem->first.GetSheetPath() );
791 changedItem->first.SetUnit( updatedUnit );
792 }
793 }
794 }
795 }
796
797 // Apply changes from change list
798 for( CHANGELIST_ITEM& item : m_changelist )
799 {
800 SCH_REFERENCE& ref = item.first;
801 PCB_FP_DATA& fpData = *item.second;
802 SCH_SYMBOL* symbol = ref.GetSymbol();
803 SCH_SCREEN* screen = ref.GetSheetPath().LastScreen();
804 wxString oldFootprint = ref.GetFootprint();
805 wxString oldValue = ref.GetValue();
806 bool oldDNP = ref.GetSymbol()->GetDNP();
807 bool oldExBOM = ref.GetSymbol()->GetExcludedFromBOM();
808 bool oldExPosFiles = ref.GetSymbol()->GetExcludedFromPosFiles();
809 // Skip prevents us from re-applying label/field changes to units we just swapped
810 bool skip = ( ref.GetSymbol()->GetFlags() & SKIP_STRUCT ) > 0 || unitSwapItems.count( &item ) > 0;
811
812 auto boolString =
813 []( bool b ) -> wxString
814 {
815 return b ? _( "true" ) : _( "false" );
816 };
817
818 if( !m_dryRun )
819 commit.Modify( symbol, screen, RECURSE_MODE::NO_RECURSE );
820
821 if( m_processReferences && ref.GetRef() != fpData.m_ref && !skip
822 && !symbol->GetField( FIELD_T::REFERENCE )->HasTextVars() )
823 {
825 msg.Printf( _( "Change %s reference designator to '%s'." ),
826 DescribeRef( ref.GetRef() ),
827 fpData.m_ref );
828
829 if( !m_dryRun )
830 symbol->SetRef( &ref.GetSheetPath(), fpData.m_ref );
831
832 m_reporter.ReportHead( msg, RPT_SEVERITY_ACTION );
833 }
834
835 if( m_processFootprints && oldFootprint != fpData.m_footprint && !skip
836 && !symbol->GetField( FIELD_T::FOOTPRINT )->HasTextVars() )
837 {
839 msg.Printf( _( "Change %s footprint assignment from '%s' to '%s'." ),
840 DescribeRef( ref.GetRef() ),
841 EscapeHTML( oldFootprint ),
842 EscapeHTML( fpData.m_footprint ) );
843
844 if( !m_dryRun )
845 symbol->SetFootprintFieldText( fpData.m_footprint );
846
847 m_reporter.ReportHead( msg, RPT_SEVERITY_ACTION );
848 }
849
850 if( m_processValues && oldValue != fpData.m_value && !skip
851 && !symbol->GetField( FIELD_T::VALUE )->HasTextVars() )
852 {
854 msg.Printf( _( "Change %s value from '%s' to '%s'." ),
855 DescribeRef( ref.GetRef() ),
856 EscapeHTML( oldValue ),
857 EscapeHTML( fpData.m_value ) );
858
859 if( !m_dryRun )
860 symbol->SetValueFieldText( fpData.m_value );
861
862 m_reporter.ReportHead( msg, RPT_SEVERITY_ACTION );
863 }
864
865 if( m_processAttributes && oldDNP != fpData.m_DNP && !skip )
866 {
868 msg.Printf( _( "Change %s 'Do not populate' from '%s' to '%s'." ),
869 DescribeRef( ref.GetRef() ),
870 boolString( oldDNP ),
871 boolString( fpData.m_DNP ) );
872
873 if( !m_dryRun )
874 symbol->SetDNP( fpData.m_DNP );
875
876 m_reporter.ReportHead( msg, RPT_SEVERITY_ACTION );
877 }
878
879 if( m_processAttributes && oldExBOM != fpData.m_excludeFromBOM && !skip )
880 {
882 msg.Printf( _( "Change %s 'Exclude from bill of materials' from '%s' to '%s'." ),
883 DescribeRef( ref.GetRef() ),
884 boolString( oldExBOM ),
885 boolString( fpData.m_excludeFromBOM ) );
886
887 if( !m_dryRun )
888 symbol->SetExcludedFromBOM( fpData.m_excludeFromBOM );
889
890 m_reporter.ReportHead( msg, RPT_SEVERITY_ACTION );
891 }
892
893 if( m_processAttributes && oldExPosFiles != fpData.m_excludeFromPosFiles && !skip )
894 {
896 msg.Printf( _( "Change %s 'Exclude from position files' from '%s' to '%s'." ), DescribeRef( ref.GetRef() ),
897 boolString( oldExPosFiles ), boolString( fpData.m_excludeFromPosFiles ) );
898
899 if( !m_dryRun )
901
902 m_reporter.ReportHead( msg, RPT_SEVERITY_ACTION );
903 }
904
905 std::set<wxString> swappedPins;
906
907 // Try to satisfy footprint pad net swaps by moving symbol pins before falling back to labels.
908 if( m_preferPinSwaps && m_processNetNames && !skip )
909 swappedPins = applyPinSwaps( symbol, ref, fpData, &commit );
910
911 if( m_processNetNames && !skip )
912 {
913 for( const std::pair<const wxString, wxString>& entry : fpData.m_pinMap )
914 {
915 const wxString& pinNumber = entry.first;
916 const wxString& shortNetName = entry.second;
917 SCH_PIN* pin = symbol->GetPin( pinNumber );
918
919 // Skip pins that the user preferred to handle with pin swaps in applyPreferredPinSwaps()
920 if( swappedPins.count( pinNumber ) > 0 )
921 continue;
922
923 if( !pin )
924 {
925 msg.Printf( _( "Cannot find %s pin '%s'." ),
926 DescribeRef( ref.GetRef() ),
927 EscapeHTML( pinNumber ) );
928 m_reporter.ReportHead( msg, RPT_SEVERITY_ERROR );
929
930 continue;
931 }
932
933 SCH_CONNECTION* connection = pin->Connection( &ref.GetSheetPath() );
934
935 if( connection && connection->Name( true ) != shortNetName )
936 {
937 processNetNameChange( &commit, ref.GetRef(), pin, connection,
938 connection->Name( true ), shortNetName );
939 }
940 }
941 }
942
944 {
945 // Need to handle three cases: existing field, new field, deleted field
946 for( const std::pair<const wxString, wxString>& field : fpData.m_fieldsMap )
947 {
948 const wxString& fpFieldName = field.first;
949 const wxString& fpFieldValue = field.second;
950 SCH_FIELD* symField = symbol->GetField( fpFieldName );
951
952 // Skip fields that are individually controlled
953 if( fpFieldName == GetCanonicalFieldName( FIELD_T::REFERENCE )
954 || fpFieldName == GetCanonicalFieldName( FIELD_T::VALUE ) )
955 {
956 continue;
957 }
958
959 // 1. Existing fields has changed value
960 // PCB Field value is checked against the shown text because this is the value
961 // with all the variables resolved. The footprints field value gets the symbol's
962 // resolved value when the PCB is updated from the schematic.
963 if( symField
964 && !symField->HasTextVars()
965 && symField->GetShownText( &ref.GetSheetPath(), false ) != fpFieldValue )
966 {
968 msg.Printf( _( "Change %s field '%s' value to '%s'." ),
969 DescribeRef( ref.GetRef() ),
970 EscapeHTML( symField->GetCanonicalName() ),
971 EscapeHTML( fpFieldValue ) );
972
973 if( !m_dryRun )
974 symField->SetText( fpFieldValue );
975
976 m_reporter.ReportHead( msg, RPT_SEVERITY_ACTION );
977 }
978
979 // 2. New field has been added to footprint and needs to be added to symbol
980 if( symField == nullptr )
981 {
983 msg.Printf( _( "Add %s field '%s' with value '%s'." ),
984 DescribeRef( ref.GetRef() ),
985 EscapeHTML( fpFieldName ),
986 EscapeHTML( fpFieldValue ) );
987
988 if( !m_dryRun )
989 {
990 SCH_FIELD newField( symbol, FIELD_T::USER, fpFieldName );
991 newField.SetText( fpFieldValue );
992 newField.SetTextPos( symbol->GetPosition() );
993 newField.SetVisible( false ); // Don't clutter up the schematic
994 symbol->AddField( newField );
995 }
996
997 m_reporter.ReportHead( msg, RPT_SEVERITY_ACTION );
998 }
999 }
1000
1001 // 3. Existing field has been deleted from footprint and needs to be deleted from symbol
1002 // Check all symbol fields for existence in the footprint field map.
1003 // Collect names first to avoid iterator invalidation when removing fields.
1004 std::vector<wxString> fieldsToDelete;
1005
1006 for( SCH_FIELD& field : symbol->GetFields() )
1007 {
1008 if( field.IsMandatory() )
1009 continue;
1010
1011 if( fpData.m_fieldsMap.find( field.GetCanonicalName() ) == fpData.m_fieldsMap.end() )
1012 {
1014 msg.Printf( _( "Delete %s field '%s.'" ),
1015 DescribeRef( ref.GetRef() ),
1016 EscapeHTML( field.GetName() ) );
1017
1018 fieldsToDelete.push_back( field.GetName() );
1019 m_reporter.ReportHead( msg, RPT_SEVERITY_ACTION );
1020 }
1021 }
1022
1023 if( !m_dryRun )
1024 {
1025 for( const wxString& fieldName : fieldsToDelete )
1026 symbol->RemoveField( fieldName );
1027 }
1028 }
1029
1030 if( symbol->GetFlags() & SKIP_STRUCT )
1031 symbol->ClearFlags( SKIP_STRUCT );
1032
1033 unitSwapItems.erase( &item );
1034
1035 // TODO: back-annotate netclass changes?
1036 }
1037
1038 if( !m_dryRun )
1039 {
1040 m_frame->RecalculateConnections( &commit, NO_CLEANUP );
1041 m_frame->UpdateNetHighlightStatus();
1042
1043 commit.Push( _( "Update Schematic from PCB" ) );
1044 }
1045}
1046
1047
1049{
1051
1052 // Initial orientation from the pin
1053 switch( aPin->GetLibPin()->GetOrientation() )
1054 {
1055 default:
1056 case PIN_ORIENTATION::PIN_RIGHT: spin = SPIN_STYLE::LEFT; break;
1057 case PIN_ORIENTATION::PIN_UP: spin = SPIN_STYLE::BOTTOM; break;
1058 case PIN_ORIENTATION::PIN_DOWN: spin = SPIN_STYLE::UP; break;
1059 case PIN_ORIENTATION::PIN_LEFT: spin = SPIN_STYLE::RIGHT; break;
1060 }
1061
1062 // Reorient based on the actual symbol orientation now
1063 struct ORIENT
1064 {
1065 int flag;
1066 int n_rots;
1067 int mirror_x;
1068 int mirror_y;
1069 }
1070 orientations[] =
1071 {
1072 { SYM_ORIENT_0, 0, 0, 0 },
1073 { SYM_ORIENT_90, 1, 0, 0 },
1074 { SYM_ORIENT_180, 2, 0, 0 },
1075 { SYM_ORIENT_270, 3, 0, 0 },
1076 { SYM_MIRROR_X + SYM_ORIENT_0, 0, 1, 0 },
1077 { SYM_MIRROR_X + SYM_ORIENT_90, 1, 1, 0 },
1078 { SYM_MIRROR_Y, 0, 0, 1 },
1079 { SYM_MIRROR_X + SYM_ORIENT_270, 3, 1, 0 },
1080 { SYM_MIRROR_Y + SYM_ORIENT_0, 0, 0, 1 },
1081 { SYM_MIRROR_Y + SYM_ORIENT_90, 1, 0, 1 },
1082 { SYM_MIRROR_Y + SYM_ORIENT_180, 2, 0, 1 },
1083 { SYM_MIRROR_Y + SYM_ORIENT_270, 3, 0, 1 }
1084 };
1085
1086 ORIENT o = orientations[ 0 ];
1087
1088 const SCH_SYMBOL* parentSymbol = static_cast<const SCH_SYMBOL*>( aPin->GetParentSymbol() );
1089
1090 if( !parentSymbol )
1091 return spin;
1092
1093 int symbolOrientation = parentSymbol->GetOrientation();
1094
1095 for( const ORIENT& i : orientations )
1096 {
1097 if( i.flag == symbolOrientation )
1098 {
1099 o = i;
1100 break;
1101 }
1102 }
1103
1104 for( int i = 0; i < o.n_rots; i++ )
1105 spin = spin.RotateCCW();
1106
1107 if( o.mirror_x )
1108 spin = spin.MirrorX();
1109
1110 if( o.mirror_y )
1111 spin = spin.MirrorY();
1112
1113 return spin;
1114}
1115
1116
1117void addConnections( SCH_ITEM* aItem, const SCH_SHEET_PATH& aSheetPath, std::set<SCH_ITEM*>& connectedItems )
1118{
1119 if( connectedItems.insert( aItem ).second )
1120 {
1121 for( SCH_ITEM* connectedItem : aItem->ConnectedItems( aSheetPath ) )
1122 addConnections( connectedItem, aSheetPath, connectedItems );
1123 }
1124}
1125
1126
1127std::set<wxString> BACK_ANNOTATE::applyPinSwaps( SCH_SYMBOL* aSymbol, const SCH_REFERENCE& aReference,
1128 const PCB_FP_DATA& aFpData, SCH_COMMIT* aCommit )
1129{
1130 // Tracks pin numbers that we end up swapping so that the caller can skip any
1131 // duplicate label-only handling for those pins.
1132 std::set<wxString> swappedPins;
1133
1134 if( !aSymbol )
1135 return swappedPins;
1136
1137 SCH_SCREEN* screen = aReference.GetSheetPath().LastScreen();
1138
1139 if( !screen )
1140 return swappedPins;
1141
1142 wxCHECK( m_frame, swappedPins );
1143
1144 // Used to build the list of schematic pins whose current net assignment does not match the PCB.
1145 struct PIN_CHANGE
1146 {
1147 SCH_PIN* pin;
1148 wxString pinNumber;
1149 wxString currentNet;
1150 wxString targetNet;
1151 };
1152
1153 std::vector<PIN_CHANGE> mismatches;
1154
1155 // Helper map that lets us find all pins currently on a given net.
1156 std::map<wxString, std::vector<size_t>> pinsByCurrentNet;
1157
1158 // Build the mismatch list by inspecting each footprint pin that differs from the schematic.
1159 for( const std::pair<const wxString, wxString>& entry : aFpData.m_pinMap )
1160 {
1161 const wxString& pinNumber = entry.first;
1162 const wxString& desiredNet = entry.second;
1163
1164 SCH_PIN* pin = aSymbol->GetPin( pinNumber );
1165
1166 // Ignore power pins and anything marked as non-connectable. Power pins can map to
1167 // hidden/global references, e.g. implicit power connections on logic symbols.
1168 // KiCad pins are currently always connectable, but the extra guard keeps the
1169 // logic robust if alternate pin types (e.g. explicit mechanical/NC pins)
1170 // ever start reporting false.
1171 if( !pin || pin->IsPower() || !pin->IsConnectable() )
1172 continue;
1173
1174 SCH_CONNECTION* connection = pin->Connection( &aReference.GetSheetPath() );
1175 wxString currentNet = connection ? connection->Name( true ) : wxString();
1176
1177 if( desiredNet.IsEmpty() || currentNet.IsEmpty() )
1178 continue;
1179
1180 if( desiredNet == currentNet )
1181 continue;
1182
1183 size_t idx = mismatches.size();
1184 mismatches.push_back( { pin, pinNumber, currentNet, desiredNet } );
1185 pinsByCurrentNet[currentNet].push_back( idx );
1186 }
1187
1188 if( mismatches.size() < 2 )
1189 return swappedPins;
1190
1191 // Track which mismatch entries we have already consumed, and the underlying pin objects
1192 // we still need to clean up wiring around once the geometry swap is done.
1193 std::vector<bool> handled( mismatches.size(), false );
1194 std::vector<SCH_PIN*> swappedPinObjects;
1195 bool swappedLibPins = false;
1196 wxString msg;
1197
1198 bool allowPinSwaps = false;
1199 wxString currentProjectName = m_frame->Prj().GetProjectName();
1200
1201 if( m_frame->eeconfig() )
1202 allowPinSwaps = m_frame->eeconfig()->m_Input.allow_unconstrained_pin_swaps;
1203
1204 std::set<wxString> sharedSheetPaths;
1205 std::set<wxString> sharedProjectNames;
1206 bool sharedSheetSymbol = SymbolHasSheetInstances( *aSymbol, currentProjectName,
1207 &sharedSheetPaths, &sharedProjectNames );
1208
1209 std::set<wxString> friendlySheetNames;
1210
1211 if( sharedSheetSymbol && !sharedSheetPaths.empty() )
1212 friendlySheetNames = GetSheetNamesFromPaths( sharedSheetPaths, m_frame->Schematic() );
1213
1214 // Check each mismatch and try to find a partner whose desired net matches our current net
1215 // (i.e. the two pins have been swapped on the PCB).
1216 for( size_t i = 0; i < mismatches.size(); ++i )
1217 {
1218 // Skip entries already handled by a previous successful swap.
1219 if( handled[i] )
1220 continue;
1221
1222 PIN_CHANGE& change = mismatches[i];
1223
1224 // Find candidate pins whose current net equals the net we want to move this pin to.
1225 auto range = pinsByCurrentNet.find( change.targetNet );
1226
1227 // Nobody currently on the desired net, so there’s no reciprocal swap to apply.
1228 if( range == pinsByCurrentNet.end() )
1229 continue;
1230
1231 // Track the best partner index in case we discover a reciprocal mismatch below.
1232 size_t partnerIdx = std::numeric_limits<size_t>::max();
1233
1234 // Examine every pin that presently lives on the net we want to move to.
1235 for( size_t candidateIdx : range->second )
1236 {
1237 if( candidateIdx == i || handled[candidateIdx] )
1238 continue;
1239
1240 PIN_CHANGE& candidate = mismatches[candidateIdx];
1241
1242 // Potential swap partner must want to move to our current net, i.e. the PCB swapped the
1243 // two nets between these pins.
1244 if( candidate.targetNet == change.currentNet )
1245 {
1246 partnerIdx = candidateIdx;
1247 break;
1248 }
1249 }
1250
1251 // No viable partner found; either there was no reciprocal net mismatch or it was already
1252 // consumed. Leave this entry for label-based handling.
1253 if( partnerIdx == std::numeric_limits<size_t>::max() )
1254 continue;
1255
1256 PIN_CHANGE& partner = mismatches[partnerIdx];
1257
1258 // Sanity check: both pins must belong to the same schematic symbol before we swap geometry;
1259 // this prevents us from moving pin outlines between different units of a multi-unit symbol.
1260 if( change.pin->GetParentSymbol() != partner.pin->GetParentSymbol() )
1261 continue;
1262
1263 if( !allowPinSwaps || sharedSheetSymbol )
1264 {
1265 if( !sharedProjectNames.empty() )
1266 {
1267 std::vector<wxString> otherProjects;
1268
1269 for( const wxString& name : sharedProjectNames )
1270 {
1271 if( !currentProjectName.IsEmpty() && name.IsSameAs( currentProjectName ) )
1272 continue;
1273
1274 otherProjects.push_back( name );
1275 }
1276
1277 wxString projects = AccumulateDescriptions( otherProjects );
1278
1279 if( projects.IsEmpty() )
1280 {
1281 msg.Printf( _( "Would swap %s pins %s and %s to match PCB, but the symbol is shared across other projects." ),
1282 DescribeRef( aReference.GetRef() ),
1283 EscapeHTML( change.pin->GetShownNumber() ),
1284 EscapeHTML( partner.pin->GetShownNumber() ) );
1285 }
1286 else
1287 {
1288 msg.Printf( _( "Would swap %s pins %s and %s to match PCB, but the symbol is shared across other "
1289 "projects (%s)." ),
1290 aReference.GetRef(), EscapeHTML( change.pin->GetShownNumber() ),
1291 EscapeHTML( partner.pin->GetShownNumber() ), projects );
1292 }
1293 }
1294 else if( !friendlySheetNames.empty() )
1295 {
1296 wxString sheets = AccumulateDescriptions( friendlySheetNames );
1297
1298 msg.Printf( _( "Would swap %s pins %s and %s to match PCB, but the symbol is used by multiple sheet "
1299 "instances (%s)." ),
1300 DescribeRef( aReference.GetRef() ),
1301 EscapeHTML( change.pin->GetShownNumber() ),
1302 EscapeHTML( partner.pin->GetShownNumber() ), sheets );
1303 }
1304 else if( sharedSheetSymbol )
1305 {
1306 msg.Printf( _( "Would swap %s pins %s and %s to match PCB, but the symbol is shared." ),
1307 DescribeRef( aReference.GetRef() ),
1308 EscapeHTML( change.pin->GetShownNumber() ),
1309 EscapeHTML( partner.pin->GetShownNumber() ) );
1310 }
1311 else
1312 {
1313 msg.Printf( _( "Would swap %s pins %s and %s to match PCB, but unconstrained pin swaps are disabled in "
1314 "the schematic preferences." ),
1315 DescribeRef( aReference.GetRef() ),
1316 EscapeHTML( change.pin->GetShownNumber() ),
1317 EscapeHTML( partner.pin->GetShownNumber() ) );
1318 }
1319 m_reporter.ReportHead( msg, RPT_SEVERITY_INFO );
1320
1321 handled[i] = true;
1322 handled[partnerIdx] = true;
1323 continue;
1324 }
1325
1326 if( !m_dryRun )
1327 {
1328 wxCHECK2( aCommit, continue );
1329
1330 // Record the two pins in the commit and physically swap their local geometry.
1331 aCommit->Modify( change.pin, screen, RECURSE_MODE::RECURSE );
1332 aCommit->Modify( partner.pin, screen, RECURSE_MODE::RECURSE );
1333
1334 swappedLibPins |= SwapPinGeometry( change.pin, partner.pin );
1335 }
1336
1337 // Don’t pick either entry again for another pairing.
1338 handled[i] = true;
1339 handled[partnerIdx] = true;
1340
1341 // Remember which pin numbers we touched so the caller can suppress duplicate work.
1342 swappedPins.insert( change.pinNumber );
1343 swappedPins.insert( partner.pinNumber );
1344 swappedPinObjects.push_back( change.pin );
1345 swappedPinObjects.push_back( partner.pin );
1346
1348
1349 msg.Printf( _( "Swap %s pins %s and %s to match PCB." ),
1350 DescribeRef( aReference.GetRef() ),
1351 EscapeHTML( change.pin->GetShownNumber() ),
1352 EscapeHTML( partner.pin->GetShownNumber() ) );
1353 m_reporter.ReportHead( msg, RPT_SEVERITY_ACTION );
1354 }
1355
1356 // Nothing left to do if we didn't find a valid swap pair when we were in dry-run mode.
1357 if( swappedPinObjects.empty() )
1358 return swappedPins;
1359
1360 if( !m_dryRun )
1361 {
1362 if( swappedLibPins )
1363 aSymbol->UpdatePins();
1364
1365 m_frame->UpdateItem( aSymbol, false, true );
1366
1367 if( TOOL_MANAGER* toolMgr = m_frame->GetToolManager() )
1368 {
1369 if( SCH_LINE_WIRE_BUS_TOOL* lwbTool = toolMgr->GetTool<SCH_LINE_WIRE_BUS_TOOL>() )
1370 {
1371 SCH_SELECTION cleanupSelection( screen );
1372
1373 // Make sure we tidy up any wires connected to the pins whose geometry just moved.
1374 for( SCH_PIN* swappedPin : swappedPinObjects )
1375 cleanupSelection.Add( swappedPin );
1376
1377 lwbTool->TrimOverLappingWires( aCommit, &cleanupSelection );
1378 lwbTool->AddJunctionsIfNeeded( aCommit, &cleanupSelection );
1379 }
1380 }
1381
1382 m_frame->Schematic().CleanUp( aCommit );
1383 }
1384
1385 return swappedPins;
1386}
1387
1388
1389void BACK_ANNOTATE::processNetNameChange( SCH_COMMIT* aCommit, const wxString& aRef, SCH_PIN* aPin,
1390 const SCH_CONNECTION* aConnection,
1391 const wxString& aOldName, const wxString& aNewName )
1392{
1393 wxString msg;
1394
1395 // Find a physically-connected driver. We can't use the SCH_CONNECTION's m_driver because
1396 // it has already been resolved by merging subgraphs with the same label, etc., and our
1397 // name change may cause that resolution to change.
1398
1399 std::set<SCH_ITEM*> connectedItems;
1400 SCH_ITEM* driver = nullptr;
1402
1403 addConnections( aPin, aConnection->Sheet(), connectedItems );
1404
1405 for( SCH_ITEM* item : connectedItems )
1406 {
1408
1409 if( priority > driverPriority )
1410 {
1411 driver = item;
1412 driverPriority = priority;
1413 }
1414 }
1415
1416 switch( driver->Type() )
1417 {
1418 case SCH_LABEL_T:
1419 case SCH_GLOBAL_LABEL_T:
1420 case SCH_HIER_LABEL_T:
1421 case SCH_SHEET_PIN_T:
1423
1424 msg.Printf( _( "Change %s pin %s net label from '%s' to '%s'." ),
1425 DescribeRef( aRef ),
1426 EscapeHTML( aPin->GetShownNumber() ),
1427 EscapeHTML( aOldName ),
1428 EscapeHTML( aNewName ) );
1429
1430 if( !m_dryRun )
1431 {
1432 aCommit->Modify( driver, aConnection->Sheet().LastScreen() );
1433 static_cast<SCH_LABEL_BASE*>( driver )->SetText( aNewName );
1434 }
1435
1436 m_reporter.ReportHead( msg, RPT_SEVERITY_ACTION );
1437 break;
1438
1439 case SCH_PIN_T:
1440 if( static_cast<SCH_PIN*>( driver )->IsPower() )
1441 {
1442 msg.Printf( _( "Net %s cannot be changed to %s because it is driven by a power pin." ),
1443 EscapeHTML( aOldName ),
1444 EscapeHTML( aNewName ) );
1445
1446 m_reporter.ReportHead( msg, RPT_SEVERITY_ERROR );
1447 break;
1448 }
1449
1450 // The physical connection walk could not find a label driver. This can happen when
1451 // a label sits in the middle of an unsplit wire. Fall back to the connection graph's
1452 // resolved driver which handles this case through subgraph merging.
1453 {
1454 CONNECTION_GRAPH* connGraph = m_frame->Schematic().ConnectionGraph();
1455 CONNECTION_SUBGRAPH* sg = connGraph ? connGraph->GetSubgraphForItem( aPin ) : nullptr;
1456
1458 {
1459 SCH_ITEM* resolvedDriver = const_cast<SCH_ITEM*>( sg->GetDriver() );
1460
1462 msg.Printf( _( "Change %s pin %s net label from '%s' to '%s'." ), DescribeRef( aRef ),
1463 EscapeHTML( aPin->GetShownNumber() ), EscapeHTML( aOldName ), EscapeHTML( aNewName ) );
1464
1465 if( !m_dryRun )
1466 {
1467 if( resolvedDriver->Type() == SCH_PIN_T )
1468 {
1469 SCH_PIN* powerPin = static_cast<SCH_PIN*>( resolvedDriver );
1470 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( powerPin->GetParentSymbol() );
1471
1472 aCommit->Modify( symbol, sg->GetSheet().LastScreen() );
1473 symbol->GetField( FIELD_T::VALUE )->SetText( aNewName );
1474 }
1475 else
1476 {
1477 aCommit->Modify( resolvedDriver, sg->GetSheet().LastScreen() );
1478 static_cast<SCH_LABEL_BASE*>( resolvedDriver )->SetText( aNewName );
1479 }
1480 }
1481
1482 m_reporter.ReportHead( msg, RPT_SEVERITY_ACTION );
1483 break;
1484 }
1485 }
1486
1488 msg.Printf( _( "Add label '%s' to %s pin %s net." ),
1489 EscapeHTML( aNewName ),
1490 DescribeRef( aRef ),
1491 EscapeHTML( aPin->GetShownNumber() ) );
1492
1493 if( !m_dryRun )
1494 {
1495 SCHEMATIC_SETTINGS& settings = m_frame->Schematic().Settings();
1496 SCH_LABEL* label = new SCH_LABEL( driver->GetPosition(), aNewName );
1497 label->SetParent( &m_frame->Schematic() );
1498 label->SetTextSize( VECTOR2I( settings.m_DefaultTextSize, settings.m_DefaultTextSize ) );
1499 label->SetSpinStyle( orientLabel( static_cast<SCH_PIN*>( driver ) ) );
1500 label->SetFlags( IS_NEW );
1501
1502 SCH_SCREEN* screen = aConnection->Sheet().LastScreen();
1503 aCommit->Add( label, screen );
1504 }
1505
1506 m_reporter.ReportHead( msg, RPT_SEVERITY_ACTION );
1507 break;
1508
1509 default:
1510 break;
1511 }
1512}
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:286
void SetFlags(EDA_ITEM_FLAGS aMask)
Definition eda_item.h:156
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:158
virtual void SetParent(EDA_ITEM *aParent)
Definition eda_item.cpp:93
EDA_ITEM_FLAGS GetFlags() const
Definition eda_item.h:159
void SetTextSize(VECTOR2I aNewSize, bool aEnforceMinTextSize=true)
Definition eda_text.cpp:536
void SetTextPos(const VECTOR2I &aPoint)
Definition eda_text.cpp:580
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:166
const SYMBOL * GetParentSymbol() const
Definition sch_item.cpp:278
int GetUnit() const
Definition sch_item.h:237
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:562
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:73
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:889
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:181
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:468
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:53
@ MAIL_PCB_GET_NETLIST
Definition mail_type.h:52
@ 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 * partner
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.