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