23#include <boost/property_tree/ptree.hpp>
49#include <fmt/ranges.h>
54 const std::map<wxString, wxString>& aNetByPin )
56 std::vector<wxString> nets;
60 for(
const wxString& pinNum : aPins )
62 auto it = aNetByPin.find( pinNum );
63 nets.push_back( it != aNetByPin.end() ? it->second : wxString() );
68 for(
const std::pair<const wxString, wxString>&
kv : aNetByPin )
69 nets.push_back(
kv.second );
78 const wxString& aVariantName,
const std::map<wxString, wxString>& aPcbNetsByPad,
79 std::map<wxString, wxString>& aPcbNetsByLogicalPin )
84 bool logicalPinsValid =
false;
85 const std::vector<wxString> logicalPins =
pin->GetStackedPinNumbers( &logicalPinsValid );
87 if( !logicalPinsValid || logicalPins.empty() )
91 bool padsValid =
false;
92 const std::vector<wxString> pads =
95 if( !padsValid || pads.empty() )
99 std::set<wxString> pcbNets;
101 for(
const wxString&
pad : pads )
103 auto it = aPcbNetsByPad.find(
pad );
104 pcbNets.insert( it != aPcbNetsByPad.end() ? it->second : wxString() );
109 if( pcbNets.size() != 1 )
112 const wxString& pcbNet = *pcbNets.begin();
114 for(
const wxString& pinNum : logicalPins )
116 auto [it, inserted] = aPcbNetsByLogicalPin.emplace( pinNum, pcbNet );
120 if( !inserted && it->second != pcbNet )
130 const std::vector<BACKANNOTATE_UNIT_SWAP_CANDIDATE>& aCandidates,
131 const std::map<wxString, wxString>& aPcbPinMap )
135 if( aCandidates.size() < 2 )
137 plan.m_mappingOk =
true;
141 std::map<size_t, size_t> desiredTarget;
142 std::set<size_t> usedTargets;
144 for(
size_t candidateIdx = 0; candidateIdx < aCandidates.size(); ++candidateIdx )
147 std::map<wxString, wxString> pcbNetsByPin;
151 auto it = aPcbPinMap.find( pinNum );
152 pcbNetsByPin[pinNum] = ( it != aPcbPinMap.end() ) ? it->second : wxString();
156 bool matched =
false;
158 for(
size_t matchIdx = 0; matchIdx < aCandidates.size(); ++matchIdx )
160 if( usedTargets.count( matchIdx ) )
164 std::vector<wxString> schNets =
167 if( pcbNets == schNets )
169 desiredTarget[candidateIdx] = matchIdx;
170 usedTargets.insert( matchIdx );
180 plan.m_mappingOk =
true;
181 plan.m_identity =
true;
183 for(
size_t candidateIdx = 0; candidateIdx < aCandidates.size(); ++candidateIdx )
185 auto it = desiredTarget.find( candidateIdx );
187 if( it == desiredTarget.end() || it->second != candidateIdx )
189 plan.m_identity =
false;
194 if(
plan.m_identity )
197 std::set<size_t> visited;
199 for(
size_t startIdx = 0; startIdx < aCandidates.size(); ++startIdx )
201 if( visited.count( startIdx ) )
204 std::vector<size_t> cycle;
205 size_t curIdx = startIdx;
207 while( !visited.count( curIdx ) )
209 visited.insert( curIdx );
210 cycle.push_back( curIdx );
212 auto nextIt = desiredTarget.find( curIdx );
214 if( nextIt == desiredTarget.end() || nextIt->second == curIdx )
217 curIdx = nextIt->second;
220 if( cycle.size() < 2 )
223 for(
size_t i = cycle.size() - 1; i > 0; --i )
225 size_t firstIdx = cycle[i - 1];
226 size_t secondIdx = cycle[i];
228 plan.m_steps.push_back( { firstIdx, secondIdx,
229 aCandidates[firstIdx].m_currentUnit,
230 aCandidates[secondIdx].m_currentUnit } );
231 plan.m_swappedCandidateIndices.insert( firstIdx );
232 plan.m_swappedCandidateIndices.insert( secondIdx );
241 bool aProcessFootprints,
bool aProcessValues,
bool aProcessReferences,
242 bool aProcessNetNames,
bool aProcessAttributes,
bool aProcessOtherFields,
243 bool aPreferUnitSwaps,
bool aPreferPinSwaps,
bool aDryRun ) :
290 "stand-alone mode.\n"
291 "You must launch the KiCad project manager and create a project." ) );
299 wxFileName fn(
m_frame->Prj().GetProjectFullName() );
300 fn.SetExt( FILEEXT::PcbFileExtension );
313 std::string nullPayload;
321 auto getStr = [](
const PTREE& pt ) -> wxString
323 return UTF8( pt.front().first );
333 Scan( &doc, &lexer );
335 PTREE& tree = doc.get_child(
"pcb_netlist" );
339 for(
const std::pair<const std::string, PTREE>& item : tree )
341 wxString
path, value, footprint;
342 bool dnp =
false, exBOM =
false, exSim =
false, exPosFiles =
false;
343 std::map<wxString, wxString> pinNetMap, fieldsMap;
344 wxASSERT( item.first ==
"ref" );
345 wxString ref = getStr( item.second );
352 path = getStr( item.second.get_child(
"timestamp" ) );
356 msg.Printf(
_(
"Footprint '%s' has no assigned symbol." ),
DescribeRef( ref ) );
361 footprint = getStr( item.second.get_child(
"fpid" ) );
362 value = getStr( item.second.get_child(
"value" ) );
365 boost::optional<const PTREE&> fields = item.second.get_child_optional(
"fields" );
370 for(
const std::pair<const std::string, PTREE>& field : fields.get() )
372 if( field.first !=
"field" )
376 const auto& fieldName = field.second.get_child_optional(
"name" );
377 const std::string& fieldValue = field.second.back().first;
382 fieldsMap[getStr( fieldName.get() )] = wxString::FromUTF8( fieldValue );
388 for(
const auto& child : item.second )
390 if( child.first !=
"property" )
393 auto property = child.second;
394 auto name =
property.get_child_optional(
"name" );
399 if(
name.get().front().first ==
"dnp" )
401 else if(
name.get().front().first ==
"exclude_from_bom" )
403 else if(
name.get().front().first ==
"exclude_from_sim" )
405 else if(
name.get().front().first ==
"exclude_from_pos_files" )
409 boost::optional<const PTREE&> nets = item.second.get_child_optional(
"nets" );
413 for(
const std::pair<const std::string, PTREE>& pin_net : nets.get() )
415 wxASSERT( pin_net.first ==
"pin_net" );
416 wxString pinNumber =
UTF8( pin_net.second.front().first );
417 wxString netName =
UTF8( pin_net.second.back().first );
418 pinNetMap[ pinNumber ] = netName;
424 wxLogWarning(
"Cannot parse PCB netlist for back-annotation." );
433 msg.Printf(
_(
"Footprints '%s' and '%s' linked to same symbol." ),
441 std::shared_ptr<PCB_FP_DATA> data = std::make_shared<PCB_FP_DATA>(
442 ref, footprint, value, dnp, exBOM, exSim, exPosFiles, pinNetMap, fieldsMap );
454 bool foundInMultiunit =
false;
459 refIndex = refList.FindRef( pcbPath );
461 refIndex = refList.FindRefByFullPath( pcbPath );
467 foundInMultiunit =
true;
469 for(
size_t i = 0; i < refList.GetCount(); ++i )
471 refList[ i ].GetSymbol()->ClearFlags(
SKIP_STRUCT );
479 if( foundInMultiunit )
483 refIndex =
m_refs.FindRef( pcbPath );
485 refIndex =
m_refs.FindRefByFullPath( pcbPath );
495 m_reporter.ReportTail( wxString::Format(
_(
"Cannot find symbol for footprint '%s'." ),
509 return SCH_REFERENCE_LIST::sortByTimeStamp( a.first, b.first );
518 while( i <
m_refs.GetCount() &&
m_refs[i].GetPath() != item.first.GetPath() )
524 m_reporter.ReportTail( wxString::Format(
_(
"Footprint '%s' is not present on PCB. "
525 "Corresponding symbols in schematic must be "
526 "manually deleted (if desired)." ),
538 "annotated schematic." ) ) )
545std::vector<BACKANNOTATE_REF>
549 std::vector<BACKANNOTATE_REF>
result( aRequests.size() );
550 std::map<const SCH_SYMBOL*, wxString> firstBase;
551 std::set<const SCH_SYMBOL*> refusedSymbols;
552 std::set<wxString> refusedBoardRefs;
558 && !( aSymbol->Schematic() && aSymbol->Schematic()->CopiesSelectDifferentUnits( aSymbol ) );
561 for(
size_t ii = 0; ii < aRequests.size(); ++ii )
564 const wxString& boardRef = aRequests[ii].second;
571 if( !boardRef.EndsWith( suffix ) || boardRef.length() <= suffix.length() )
573 result[ii].m_error = wxString::Format(
_(
"Cannot change %s to '%s', which lacks the sheet "
579 result[ii].m_base = boardRef.Left( boardRef.length() - suffix.length() );
581 if( shares( symbol ) )
583 auto [it, inserted] = firstBase.emplace( ref.
GetSymbol(),
result[ii].m_base );
585 if( !inserted && it->second !=
result[ii].m_base )
586 refusedSymbols.insert( ref.
GetSymbol() );
591 for(
bool changed =
true; changed; )
595 for(
size_t ii = 0; ii < aRequests.size(); ++ii )
597 const SCH_SYMBOL* symbol = aRequests[ii].first->GetSymbol();
598 const wxString& boardRef = aRequests[ii].second;
600 if(
result[ii].m_error.IsEmpty() && !refusedSymbols.contains( symbol )
601 && !refusedBoardRefs.contains( boardRef ) )
606 changed |= refusedBoardRefs.insert( boardRef ).second;
608 if( shares( symbol ) )
609 changed |= refusedSymbols.insert( symbol ).second;
611 if(
result[ii].m_error.IsEmpty() )
613 result[ii].m_error = wxString::Format(
_(
"Cannot change %s to '%s', because the change "
614 "cannot reach all of its units and sheets." ),
615 DescribeRef( aRequests[ii].first->GetComposedRef() ),
617 result[ii].m_base.clear();
632 std::optional<SCH_CONNECTIVITY::PUBLICATION_HOLD> hold( std::in_place,
m_frame->Schematic().Connectivity() );
634 std::set<CHANGELIST_ITEM*> unitSwapItems;
641 const wxString variantName =
m_frame->Schematic().GetCurrentVariant();
645 std::map<std::shared_ptr<PCB_FP_DATA>, std::vector<CHANGELIST_ITEM*>> changesPerFp;
647 msg.Printf( wxT(
"DEBUG(unit-swap): grouped changelist into %zu footprint(s) for unit-swap processing:" ),
648 changesPerFp.size() );
655 changesPerFp[item.second].push_back( &item );
656 msg += wxT(
" " ) + item.first.GetRef();
662 for(
auto& fpChangelistPair : changesPerFp )
664 std::set<SCH_SYMBOL*> swappedSymbols;
665 std::set<CHANGELIST_ITEM*> swappedItems;
666 std::shared_ptr<PCB_FP_DATA> fp = fpChangelistPair.first;
667 auto& changedFpItems = fpChangelistPair.second;
680 std::map<wxString, wxString> schNetsByPin;
681 std::map<wxString, wxString> pcbNetsByPin;
682 std::vector<wxString> unitPinNumbers;
683 std::vector<wxString> schNetsInUnitOrder;
684 std::vector<wxString> pcbNetsInUnitOrder;
688 std::vector<SYM_UNIT> symbolUnits;
689 std::map<wxString, wxString> pcbNetsByLogicalPin;
690 bool mappingValid =
true;
692 std::map<LIB_SYMBOL*, std::vector<LIB_SYMBOL::UNIT_PIN_INFO>> unitPinsByLibSymbol;
695 [&](
SCH_SYMBOL* symbol,
int unitNumber ) -> std::vector<wxString>
697 if( unitNumber <= 0 )
708 auto found = unitPinsByLibSymbol.find( libSymbol );
710 if( found == unitPinsByLibSymbol.end() )
711 found = unitPinsByLibSymbol.emplace( libSymbol, libSymbol->
GetUnitPinInfo() ).first;
713 const std::vector<LIB_SYMBOL::UNIT_PIN_INFO>& unitInfos = found->second;
715 if( unitNumber >
static_cast<int>( unitInfos.size() ) )
718 return unitInfos[unitNumber - 1].m_pinNumbers;
733 symbolUnit.sym = symbol;
734 symbolUnit.screen = screen;
737 symbolUnit.changeItem = changedItem;
739 int currentUnit = ref.
GetUnit();
741 if( currentUnit <= 0 )
744 if( currentUnit <= 0 )
745 currentUnit = symbol->
GetUnit();
747 symbolUnit.currentUnit = currentUnit;
748 symbolUnit.unitPinNumbers = getUnitPins( symbol, symbolUnit.currentUnit );
754 mappingValid =
false;
760 const wxString schNet =
pin->GetConnectionName( &sheetPath,
false,
true ).value_or( wxString() );
762 for(
const wxString& pinNum :
pin->GetStackedPinNumbers() )
764 symbolUnit.pcbNetsByPin[pinNum] = pcbNetsByLogicalPin.at( pinNum );
765 symbolUnit.schNetsByPin[pinNum] = schNet;
769 symbolUnit.pcbNetsInUnitOrder =
netsInUnitOrder( symbolUnit.unitPinNumbers, symbolUnit.pcbNetsByPin );
770 symbolUnit.schNetsInUnitOrder =
netsInUnitOrder( symbolUnit.unitPinNumbers, symbolUnit.schNetsByPin );
772 symbolUnits.push_back( symbolUnit );
775 auto vectorToString =
776 [](
const std::vector<wxString>& values ) -> wxString
778 return fmt::format(
L"{}", fmt::join( values,
L", " ) );
782 [vectorToString](
const std::map<wxString, wxString>& pinMap ) -> wxString
784 std::vector<wxString> entries;
786 for(
const std::pair<const wxString, wxString>&
pin : pinMap )
787 entries.push_back(
pin.first +
'=' +
pin.second );
789 return vectorToString( entries );
792 msg.Printf( wxT(
"DEBUG(unit-swap): footprint %s processed (%zu units, dryRun=%d)." ), fp->m_ref,
793 symbolUnits.size(),
m_dryRun ? 1 : 0 );
797 std::sort( symbolUnits.begin(), symbolUnits.end(),
798 [](
const SYM_UNIT& a,
const SYM_UNIT& b )
800 return a.ref < b.ref;
803 for(
const SYM_UNIT& su : symbolUnits )
805 wxString pcbPins = mapToString( su.pcbNetsByPin );
806 wxString schPins = mapToString( su.schNetsByPin );
807 wxString unitPins = vectorToString( su.unitPinNumbers );
808 wxString pcbUnitNetSeq = vectorToString( su.pcbNetsInUnitOrder );
809 wxString schUnitNetSeq = vectorToString( su.schNetsInUnitOrder );
811 msg.Printf( wxT(
"DEBUG(unit-swap): unit %d: %s pcbPins[%s] schPins[%s] unitPins[%s] pcbUnitNets[%s] "
812 "schUnitNets[%s]." ),
813 su.currentUnit, su.ref, pcbPins, schPins, unitPins, pcbUnitNetSeq, schUnitNetSeq );
817 if( !mappingValid || symbolUnits.size() < 2 )
820 std::vector<BACKANNOTATE_UNIT_SWAP_CANDIDATE>
candidates;
822 for(
const SYM_UNIT& symbolUnit : symbolUnits )
825 candidate.
m_ref = symbolUnit.ref;
834 if( !
plan.m_mappingOk )
836 msg.Printf( wxT(
"DEBUG(unit-swap): mapping failed for footprint %s." ), fp->m_ref );
841 if(
plan.m_identity )
843 msg.Printf( wxT(
"DEBUG(unit-swap): footprint %s already aligned (identity mapping)." ), fp->m_ref );
850 for(
size_t candidateIdx :
plan.m_swappedCandidateIndices )
851 commit.
Modify( symbolUnits[candidateIdx].sym, symbolUnits[candidateIdx].screen );
858 int aUnit = a.currentUnit;
859 int bUnit = b.currentUnit;
863 a.sym->SetUnit( bUnit );
864 b.sym->SetUnit( aUnit );
867 a.sym->SetUnitSelection( sheet, bUnit );
870 b.sym->SetUnitSelection( sheet, aUnit );
873 a.currentUnit = bUnit;
874 b.currentUnit = aUnit;
876 swappedSymbols.insert( a.sym );
877 swappedSymbols.insert( b.sym );
881 swappedItems.insert( a.changeItem );
882 unitSwapItems.insert( a.changeItem );
887 swappedItems.insert( b.changeItem );
888 unitSwapItems.insert( b.changeItem );
891 wxString baseRef = a.sym->GetRef( a.sheetPath,
false );
892 wxString unitAString = a.sym->SubReference( aUnit,
false );
893 wxString unitBString = b.sym->SubReference( bUnit,
false );
895 if( unitAString.IsEmpty() )
896 unitAString.Printf( wxT(
"%d" ), aUnit );
898 if( unitBString.IsEmpty() )
899 unitBString.Printf( wxT(
"%d" ), bUnit );
901 msg.Printf(
_(
"Swap %s unit %s with unit %s." ),
909 msg.Printf( wxT(
"DEBUG(unit-swap): applied %zu swap steps for footprint %s." ),
910 plan.m_steps.size(), fp->m_ref );
918 SCH_SYMBOL* symbol = changedItem->first.GetSymbol();
923 if( swappedItems.count( changedItem ) || swappedSymbols.count( symbol ) )
926 int updatedUnit = symbol->
GetUnitSelection( &changedItem->first.GetSheetPath() );
927 changedItem->first.SetUnit( updatedUnit );
933 std::vector<std::pair<const SCH_REFERENCE*, wxString>> refRequests;
934 const bool shareBases =
m_frame->Schematic().Settings().m_AnnotateReusedSheetSuffixes;
937 refRequests.emplace_back( &item.first, item.second->m_ref );
942 for(
size_t itemIdx = 0; itemIdx <
m_changelist.size(); ++itemIdx )
956 bool skip = ( ref.
GetSymbol()->GetFlags() &
SKIP_STRUCT ) > 0 || unitSwapItems.count( &item ) > 0;
959 [](
bool b ) -> wxString
961 return b ?
_(
"true" ) :
_(
"false" );
972 if( !newRef.
m_error.IsEmpty() )
979 msg.Printf(
_(
"Change %s reference designator to '%s'." ),
996 msg.Printf(
_(
"Change %s footprint assignment from '%s' to '%s'." ),
1011 msg.Printf(
_(
"Change %s value from '%s' to '%s'." ),
1025 msg.Printf(
_(
"Change %s 'Do not populate' from '%s' to '%s'." ),
1027 boolString( oldDNP ),
1028 boolString( fpData.
m_DNP ) );
1039 msg.Printf(
_(
"Change %s 'Exclude from bill of materials' from '%s' to '%s'." ),
1041 boolString( oldExBOM ),
1053 msg.Printf(
_(
"Change %s 'Exclude from simulation' from '%s' to '%s'." ),
1055 boolString( oldExSim ),
1067 msg.Printf(
_(
"Change %s 'Exclude from position files' from '%s' to '%s'." ),
DescribeRef( ref.
GetRef() ),
1076 std::set<wxString> swappedPins;
1080 swappedPins =
applyPinSwaps( symbol, ref, fpData, &commit );
1084 for(
const std::pair<const wxString, wxString>& entry : fpData.
m_pinMap )
1086 const wxString& pinNumber = entry.first;
1087 const wxString& shortNetName = entry.second;
1091 if( swappedPins.count( pinNumber ) > 0 )
1096 msg.Printf(
_(
"Cannot find %s pin '%s'." ),
1104 const std::optional<wxString> netName =
pin->GetConnectionName( &ref.
GetSheetPath(),
false,
true );
1106 if( netName && *netName != shortNetName )
1114 for(
const std::pair<const wxString, wxString>& field : fpData.
m_fieldsMap )
1116 const wxString& fpFieldName = field.first;
1117 const wxString& fpFieldValue = field.second;
1136 msg.Printf(
_(
"Change %s field '%s' value to '%s'." ),
1142 symField->
SetText( fpFieldValue );
1148 if( symField ==
nullptr )
1151 msg.Printf(
_(
"Add %s field '%s' with value '%s'." ),
1159 newField.
SetText( fpFieldValue );
1172 std::vector<wxString> fieldsToDelete;
1176 if( field.IsMandatory() )
1182 msg.Printf(
_(
"Delete %s field '%s.'" ),
1186 fieldsToDelete.push_back( field.GetName() );
1193 for(
const wxString& fieldName : fieldsToDelete )
1201 unitSwapItems.erase( &item );
1211 m_frame->UpdateNetHighlightStatus();
1213 commit.
Push(
_(
"Update Schematic from PCB" ) );
1256 ORIENT o = orientations[ 0 ];
1265 for(
const ORIENT& i : orientations )
1267 if( i.flag == symbolOrientation )
1274 for(
int i = 0; i < o.n_rots; i++ )
1289 if( !connectedItems.insert( aItem ).second )
1292 std::vector<SCH_ITEM*> neighbors;
1298 neighbors = connection->ConnectedItems();
1305 for(
SCH_ITEM* connectedItem : neighbors )
1315 std::set<wxString> swappedPins;
1325 wxCHECK(
m_frame, swappedPins );
1332 wxString currentNet;
1336 std::vector<PIN_CHANGE> mismatches;
1339 std::map<wxString, std::vector<size_t>> pinsByCurrentNet;
1342 for(
const std::pair<const wxString, wxString>& entry : aFpData.
m_pinMap )
1344 const wxString& pinNumber = entry.first;
1345 const wxString& desiredNet = entry.second;
1354 if( !
pin ||
pin->IsPower() || !
pin->IsConnectable() )
1357 wxString currentNet =
1358 pin->GetConnectionName( &aReference.
GetSheetPath(),
false,
true ).value_or( wxString() );
1360 if( desiredNet.IsEmpty() || currentNet.IsEmpty() )
1363 if( desiredNet == currentNet )
1366 size_t idx = mismatches.size();
1367 mismatches.push_back( {
pin, pinNumber, currentNet, desiredNet } );
1368 pinsByCurrentNet[currentNet].push_back( idx );
1371 if( mismatches.size() < 2 )
1376 std::vector<bool> handled( mismatches.size(),
false );
1377 std::vector<SCH_PIN*> swappedPinObjects;
1378 bool swappedLibPins =
false;
1381 bool allowPinSwaps =
false;
1382 wxString currentProjectName =
m_frame->Prj().GetProjectName();
1385 allowPinSwaps =
m_frame->eeconfig()->m_Input.allow_unconstrained_pin_swaps;
1387 std::set<wxString> sharedSheetPaths;
1388 std::set<wxString> sharedProjectNames;
1390 &sharedSheetPaths, &sharedProjectNames );
1392 std::set<wxString> friendlySheetNames;
1394 if( sharedSheetSymbol && !sharedSheetPaths.empty() )
1399 for(
size_t i = 0; i < mismatches.size(); ++i )
1405 PIN_CHANGE& change = mismatches[i];
1408 auto range = pinsByCurrentNet.find( change.targetNet );
1411 if( range == pinsByCurrentNet.end() )
1415 size_t partnerIdx = std::numeric_limits<size_t>::max();
1418 for(
size_t candidateIdx : range->second )
1420 if( candidateIdx == i || handled[candidateIdx] )
1423 PIN_CHANGE& candidate = mismatches[candidateIdx];
1427 if( candidate.targetNet == change.currentNet )
1429 partnerIdx = candidateIdx;
1436 if( partnerIdx == std::numeric_limits<size_t>::max() )
1439 PIN_CHANGE&
partner = mismatches[partnerIdx];
1443 if( change.pin->GetParentSymbol() !=
partner.pin->GetParentSymbol() )
1446 if( !allowPinSwaps || sharedSheetSymbol )
1448 if( !sharedProjectNames.empty() )
1450 std::vector<wxString> otherProjects;
1452 for(
const wxString&
name : sharedProjectNames )
1454 if( !currentProjectName.IsEmpty() &&
name.IsSameAs( currentProjectName ) )
1457 otherProjects.push_back(
name );
1462 if( projects.IsEmpty() )
1464 msg.Printf(
_(
"Would swap %s pins %s and %s to match PCB, but the symbol is shared across other projects." ),
1471 msg.Printf(
_(
"Would swap %s pins %s and %s to match PCB, but the symbol is shared across other "
1477 else if( !friendlySheetNames.empty() )
1481 msg.Printf(
_(
"Would swap %s pins %s and %s to match PCB, but the symbol is used by multiple sheet "
1482 "instances (%s)." ),
1487 else if( sharedSheetSymbol )
1489 msg.Printf(
_(
"Would swap %s pins %s and %s to match PCB, but the symbol is shared." ),
1496 msg.Printf(
_(
"Would swap %s pins %s and %s to match PCB, but unconstrained pin swaps are disabled in "
1497 "the schematic preferences." ),
1505 handled[partnerIdx] =
true;
1511 wxCHECK2( aCommit,
continue );
1522 handled[partnerIdx] =
true;
1525 swappedPins.insert( change.pinNumber );
1526 swappedPins.insert(
partner.pinNumber );
1527 swappedPinObjects.push_back( change.pin );
1528 swappedPinObjects.push_back(
partner.pin );
1532 msg.Printf(
_(
"Swap %s pins %s and %s to match PCB." ),
1540 if( swappedPinObjects.empty() )
1545 if( swappedLibPins )
1548 m_frame->UpdateItem( aSymbol,
false,
true );
1557 for(
SCH_PIN* swappedPin : swappedPinObjects )
1558 cleanupSelection.
Add( swappedPin );
1560 lwbTool->TrimOverLappingWires( aCommit, &cleanupSelection );
1561 lwbTool->AddJunctionsIfNeeded( aCommit, &cleanupSelection );
1565 m_frame->Schematic().CleanUp( aCommit );
1574 const wxString& aOldName,
const wxString& aNewName )
1582 std::set<SCH_ITEM*> connectedItems;
1588 for(
SCH_ITEM* item : connectedItems )
1592 if( priority > driverPriority )
1595 driverPriority = priority;
1599 switch( driver->
Type() )
1607 msg.Printf(
_(
"Change %s pin %s net label from '%s' to '%s'." ),
1623 if(
static_cast<SCH_PIN*
>( driver )->IsPower() )
1625 msg.Printf(
_(
"Net %s cannot be changed to %s because it is driven by a power pin." ),
1637 SCH_ITEM* resolvedDriver =
nullptr;
1646 : std::optional<SCH_SHEET_PATH>();
1648 if(
path && net->Driver() )
1650 resolvedDriver = net->Driver();
1651 driverSheet = *
path;
1670 msg.Printf(
_(
"Change %s pin %s net label from '%s' to '%s'." ),
DescribeRef( aRef ),
1686 static_cast<SCH_LABEL_BASE*
>( resolvedDriver )->SetText( aNewName );
1696 msg.Printf(
_(
"Add label '%s' to %s pin %s net." ),
1711 aCommit->
Add( label, screen );
std::vector< BACKANNOTATE_REF > ResolveBackannotatedRefs(const std::vector< std::pair< const SCH_REFERENCE *, wxString > > &aRequests, bool aShareBases)
Turn the references footprints carry into the base references of their schematic instances.
bool CollectBackannotatePcbNets(const SCH_SYMBOL &aSymbol, const SCH_SHEET_PATH &aSheetPath, const wxString &aVariantName, const std::map< wxString, wxString > &aPcbNetsByPad, std::map< wxString, wxString > &aPcbNetsByLogicalPin)
Append this unit's PCB nets to a map keyed by logical pin number, resolving pin-to-pad mappings.
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 PCB nets keyed by logical pin ...
std::vector< BACKANNOTATE_REF > ResolveBackannotatedRefs(const std::vector< std::pair< const SCH_REFERENCE *, wxString > > &aRequests, bool aShareBases)
Turn the references footprints carry into the base references of their schematic instances.
bool CollectBackannotatePcbNets(const SCH_SYMBOL &aSymbol, const SCH_SHEET_PATH &aSheetPath, const wxString &aVariantName, const std::map< wxString, wxString > &aPcbNetsByPad, std::map< wxString, wxString > &aPcbNetsByLogicalPin)
Append this unit's PCB nets to a map keyed by logical pin number, resolving pin-to-pad mappings.
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 PCB nets keyed by logical pin ...
static const ADVANCED_CFG & GetCfg()
Get the singleton instance's config, which is shared by all consumers.
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_processOtherFields
void getPcbModulesFromString(const std::string &aPayload)
Parse netlist sent over KiWay express mail interface and fill m_pcbFootprints.
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
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.
void processNetNameChange(SCH_COMMIT *aCommit, const wxString &aRef, SCH_PIN *aPin, const SCH_SHEET_PATH &aSheet, const wxString &aOldName, const wxString &aNewName)
void getChangeList()
Create changelist.
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.
COMMIT & Add(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Add a new item to the model.
Calculate the connectivity of a schematic and generate netlists.
A subgraph is a set of items that are electrically connected on a single sheet.
PRIORITY GetDriverPriority()
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.
virtual VECTOR2I GetPosition() const
void SetFlags(EDA_ITEM_FLAGS aMask)
KICAD_T Type() const
Returns the type of object.
void ClearFlags(EDA_ITEM_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
virtual void SetParent(EDA_ITEM *aParent)
EDA_ITEM_FLAGS GetFlags() const
virtual void SetTextSize(VECTOR2I aNewSize, bool aEnforceMinTextSize=true)
virtual void SetTextPos(const VECTOR2I &aPoint)
bool HasTextVars() const
Indicates the ShownText has text var references which need to be processed.
virtual void SetVisible(bool aVisible)
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.
std::vector< UNIT_PIN_INFO > GetUnitPinInfo() const
Return pin-number lists for each unit, ordered consistently for gate swapping.
static REPORTER & GetInstance()
A pure virtual class used to derive REPORTER objects from.
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.
These are loaded from Eeschema settings but then overwritten by the project settings.
Holds all the data relating to one schematic.
wxString GetPackageRefSuffix(const SCH_SYMBOL *aSymbol, const SCH_SHEET_PATH &aPath) const
Return the suffix the package of a multi-unit symbol's instance composes with.
SCH_SHEET_LIST Hierarchy() const
Return the full schematic flattened hierarchical sheet list.
SCH_CONNECTIVITY::FACADE & Connectivity() const
CONNECTION_GRAPH * ConnectionGraph() const
Commit for the schematic and symbol editors.
void Push(const wxString &aMessage=wxEmptyString, int aCommitFlags=0) override
Execute the changes.
std::optional< NET_VIEW > GetSubgraphForItem(const KIID &aItem, const KIID_PATH &aPath) const
std::optional< ITEM_VIEW > Connection(const KIID &aItem, const KIID_PATH &aPath) const
Schematic editor (Eeschema) main window.
wxString GetUntranslatedName() const
Get the untranslated field name for storage, variable look-up, etc.
wxString GetShownText(const SCH_SHEET_PATH *aPath, RESOLUTION_CONTEXT aContext, const wxString &aVariantName=wxEmptyString, int aDepth=0) const
void SetText(const wxString &aText) override
Base class for any item which can be embedded within the SCHEMATIC container class,...
const SYMBOL * GetParentSymbol() const
SCHEMATIC * Schematic() const
Search the item hierarchy to find a SCHEMATIC.
const std::vector< SCH_ITEM * > & ConnectedItems(const SCH_SHEET_PATH &aPath) const
Retrieve the set of items connected to this item on the given sheet.
virtual void SetSpinStyle(SPIN_STYLE aSpinStyle)
SCH_PIN * GetLibPin() const
PIN_ORIENTATION GetOrientation() const
const wxString & GetShownNumber() const
A helper to define a symbol's reference designator in a schematic.
const SCH_SHEET_PATH & GetSheetPath() const
wxString GetComposedRef() const
Return the reference as shown on this sheet path, with its sheet suffix.
const wxString GetFootprint() const
SCH_SYMBOL * GetSymbol() const
const wxString GetValue() const
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
std::optional< SCH_SHEET_PATH > GetSheetPathByKIIDPath(const KIID_PATH &aPath, bool aIncludeLastSheet=true) const
Finds a SCH_SHEET_PATH that matches the provided KIID_PATH.
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()
const KIID_PATH & PathRef() const
Borrow the cached path until this sheet path is modified or destroyed.
bool GetExcludedFromSim(const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) const override
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.
void SetExcludedFromSim(bool aEnable, const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) override
Set or clear the exclude from simulation flag.
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 base 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
void SetExcludedFromPosFiles(bool aEnable, const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) override
void SetUserRef(const SCH_SHEET_PATH &aSheet, const wxString &aBase)
Set a base reference chosen by the user on aSheet.
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()
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)
A null aItem is ignored; the selection never holds null members.
Spin style for labels of all kinds on schematics.
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.
An 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to and fro...
wxString DescribeRef(const wxString &aRef)
Returns a user-visible HTML string describing a footprint reference designator.
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
This file is part of the common library.
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
#define IS_NEW
New item, just created.
#define SKIP_STRUCT
flag indicating that the structure should be ignored
static std::vector< PENDING_PROPERTY > plan(const EDA_ITEM &aSource, const EDA_ITEM &aTarget, const std::set< wxString > &aEnabledKeys)
Target properties to write, paired with values read off the source.
@ PIN_UP
The pin extends upwards from the connection point: Probably on the bottom side of the symbol.
@ PIN_RIGHT
The pin extends rightwards from the connection point.
@ PIN_LEFT
The pin extends leftwards from the connection point: Probably on the right side of the symbol.
@ PIN_DOWN
The pin extends downwards from the connection: Probably on the top side of the symbol.
void Scan(PTREE *aTree, DSNLEXER *aLexer)
Fill an empty PTREE with information from a KiCad s-expression stream.
boost::property_tree::ptree PTREE
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
@ SYMBOL_FILTER_NON_POWER
wxString EscapeHTML(const wxString &aString)
Return a new wxString escaped for embedding in HTML.
std::vector< wxString > ExpandStackedPinNotation(const wxString &aPinName, bool *aValid)
Expand stacked pin notation like [1,2,3], [1-4], [A1-A4], or [AA1-AA3,AB4,CD12-CD14] into individual ...
wxString From_UTF8(const char *cstring)
void AccumulateDescriptions(wxString &aDesc, const T &aItemCollection)
Build a comma-separated list from a collection of wxStrings.
wxString m_base
Base reference the instance takes, when m_error is empty.
std::vector< wxString > m_unitPinNumbers
std::map< wxString, wxString > m_schNetsByPin
Container for Pcbnew footprint data.
std::map< wxString, wxString > m_pinMap
bool m_excludeFromPosFiles
std::map< wxString, wxString > m_fieldsMap
wxString GetDefaultFieldName(FIELD_T aFieldId, TRANSLATION aTranslation)
Return a default symbol field name for a mandatory field type.
@ 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".
static std::vector< int > candidates(const SEGMENT_INDEX &aIndex, const SEG &aQuery, int aPadding)
wxString result
Test unit parsing edge cases and error handling.
VECTOR2< int32_t > VECTOR2I
Definition of file extensions used in Kicad.