37#include <unordered_set>
54 for(
unsigned ii = 0; ii <
GetCount(); ii++ )
160 for(
size_t i = 0; i <
m_flatList.size(); ++i )
162 if(
m_flatList[i].GetFullPath() == aFullPath )
172 for(
size_t i = 0; i <
m_flatList.size(); ++i )
183 int aMinRefId )
const
190 if(
m_flatList[aIndex].CompareRef( ref ) == 0 && ref.m_numRef >= aMinRefId && !ref.m_isNew )
191 aIdList.push_back( ref.m_numRef );
194 std::sort( aIdList.begin(), aIdList.end() );
204 std::vector<int> unitsList;
207 unitsList.push_back( aRef.
m_unit );
211 if( ref.CompareValue( aRef ) != 0 )
214 if( ref.CompareLibName( aRef ) != 0 )
218 if( ref.IsSplitNeeded() )
221 if( ref.CompareRef( aRef ) != 0 )
227 unitsList.push_back( ref.m_unit );
230 std::sort( unitsList.begin(), unitsList.end() );
241 const std::vector<int>& aRequiredUnits )
const
245 std::map<int, std::vector<SCH_REFERENCE>> refNumberMap;
250 if( ref.CompareRef( aRef ) != 0 )
256 refNumberMap[ref.m_numRef].push_back( ref );
260 int minFreeNumber = aMinValue;
262 for( ; refNumberMap[minFreeNumber].size() > 0; ++minFreeNumber )
264 auto isNumberInUse = [&]() ->
bool
266 for(
const int& unit : aRequiredUnits )
268 for(
const SCH_REFERENCE& ref : refNumberMap[minFreeNumber] )
270 if( ref.CompareLibName( aRef ) || ref.CompareValue( aRef )
271 || ref.GetUnit() == unit )
281 if( !isNumberInUse() )
282 return minFreeNumber;
285 return minFreeNumber;
291 std::vector<SCH_SYMBOL_INSTANCE> retval;
296 instance.
m_Path = ref.GetSheetPath().Path();
298 instance.
m_Unit = ref.GetUnit();
300 retval.push_back( instance );
309 int expectedId = aFirstValue;
315 for( ; ii < aIdList.size(); ii++ )
317 if( expectedId <= aIdList[ii] )
323 for( ; ii < aIdList.size(); ii++ )
325 if( expectedId != aIdList[ii] )
328 aIdList.insert( aIdList.begin() + ii, expectedId );
337 aIdList.push_back( expectedId );
348 if( aUnitNumber < 0 )
349 fullref <<
".." << aItem.
GetUnit();
351 fullref <<
".." << aUnitNumber;
361 bool aStartAtCurrent,
369 for(
size_t i = 0; i <
GetCount(); i++ )
380 wxCHECK2_MSG(
path,
continue,
381 wxS(
"Attempting to annotate item on sheet not part of the "
388 if( refstr[refstr.Len() - 1] ==
'?' )
393 lockedSymbols[refstr].AddItem( ref );
396 AnnotateByOptions( aSortOption, aAlgoOption, aStartNumber, lockedSymbols, aAdditionalRefs,
412 bool aStartAtCurrent )
414 switch( aSortOption )
424 switch( aAlgoOption )
435 aStartAtCurrent =
false;
441 aStartAtCurrent =
false;
445 Annotate( useSheetNum, idStep, aStartNumber, aLockedUnitMap, aAdditionalRefs, aStartAtCurrent );
467 std::unordered_set<wxString> inUseRefs;
469 for(
size_t i = 0; i < aAdditionalRefs.
GetCount(); i++ )
472 additionalRef.
Split();
484 int LastReferenceNumber = 0;
498 minRefId =
m_flatList[first].m_sheetNum * aSheetIntervalId + 1;
500 minRefId = aStartNumber + 1;
503 for(
unsigned ii = 0; ii <
m_flatList.size(); ii++ )
507 if( ref_unit.m_flag )
513 for( SCH_MULTI_UNIT_REFERENCE_MAP::value_type& pair : aLockedUnitMap )
515 unsigned n_refs = pair.second.
GetCount();
517 for(
unsigned thisRefI = 0; thisRefI < n_refs; ++thisRefI )
523 lockedList = &pair.second;
528 if( lockedList !=
nullptr )
532 if( (
m_flatList[first].CompareRef( ref_unit ) != 0 )
533 || ( aUseSheetNum && (
m_flatList[first].m_sheetNum != ref_unit.m_sheetNum ) ) )
540 minRefId = ref_unit.m_sheetNum * aSheetIntervalId + 1;
542 minRefId = aStartNumber + 1;
546 if( aStartAtCurrent && ref_unit.m_numRef > 0 )
547 minRefId = ref_unit.m_numRef;
549 wxCHECK( ref_unit.GetLibPart(), );
552 if( ref_unit.GetLibPart()->GetUnitCount() <= 1 )
554 if( ref_unit.m_isNew )
556 std::vector<int> idList;
559 ref_unit.m_numRef = LastReferenceNumber;
560 ref_unit.m_numRefStr = wxString::Format(
"%d", LastReferenceNumber );
564 ref_unit.m_isNew =
false;
570 if( lockedList !=
nullptr )
572 unsigned n_refs = lockedList->
GetCount();
575 if( ref_unit.m_isNew )
578 ref_unit.m_numRef = LastReferenceNumber;
579 ref_unit.m_numRefStr = wxString::Format(
"%d", LastReferenceNumber );
580 ref_unit.m_isNew =
false;
584 for(
unsigned lockedRefI = 0; lockedRefI < n_refs; ++lockedRefI )
604 for(
unsigned jj = ii + 1; jj <
m_flatList.size(); jj++ )
614 if( inUseRefs.find( ref_candidate ) == inUseRefs.end() )
617 m_flatList[jj].m_numRefStr = ref_unit.m_numRefStr;
622 inUseRefs.insert( ref_candidate );
628 else if( ref_unit.m_isNew )
633 std::vector<int> units = { ref_unit.GetUnit() };
635 ref_unit.m_numRef = LastReferenceNumber;
636 ref_unit.m_isNew =
false;
644 wxASSERT( originalSize ==
GetCount() );
661 for(
unsigned ii = 0; ii <
m_flatList.size(); ii++ )
674 &&
m_flatList[ii].GetLibPart()->GetUnitCount() > 1 )
676 msg.Printf(
_(
"Item not annotated: %s%s (unit %d)" ),
683 msg.Printf(
_(
"Item not annotated: %s%s" ),
m_flatList[ii].GetRef(), tmp );
701 msg.Printf(
_(
"Error: symbol %s%s%s (unit %d) exceeds units defined (%d)" ),
706 m_flatList[ii].GetLibPart()->GetUnitCount() );
717 for(
int ii = 0; ii < imax; ii++ )
740 msg.Printf(
_(
"Duplicate items %s%s%s\n" ),
744 : wxString( wxT(
"" ) ) );
765 msg.Printf(
_(
"Differing unit counts for item %s%s%s and %s%s%s\n" ),
781 msg.Printf(
_(
"Different values for %s%d%s (%s) and %s%d%s (%s)" ),
802 wxASSERT( aSymbol !=
nullptr );
814 if( aSymbol->
GetRef( &aSheetPath ).IsEmpty() )
815 aSymbol->
SetRef( &aSheetPath, wxT(
"DefRef?" ) );
817 wxString ref = aSymbol->
GetRef( &aSheetPath );
822 if( aSymbol->
GetValue(
false, &aSheetPath,
false ).IsEmpty() )
859 int ll = refText.length() - 1;
861 if( refText[ll] ==
'?' )
869 else if( isdigit( refText[ll] ) == 0 )
877 if( (refText[ll] <=
' ' ) || isdigit( refText[ll] ) )
881 if( isdigit( refText[ll + 1] ) )
884 const char* cp = refText.c_str() + ll + 1;
890 refText.erase( ll + 1 );
904 int ll = refText.length() - 1;
906 return ( refText[ll] ==
'?' ) || isdigit( refText[ll] );
911 const wxString& refDelimiter,
912 const wxString& refRangeDelimiter )
917 while( i < aList.size() )
919 wxString ref = aList[ i ].GetRef();
920 int numRef = aList[ i ].m_numRef;
924 while( i + range < aList.size()
925 && aList[ i + range ].GetRef() == ref
926 && aList[ i + range ].m_numRef ==
int( numRef + range ) )
930 if( range == 2 && refRangeDelimiter.IsEmpty() )
934 if( !retVal.IsEmpty() )
935 retVal << refDelimiter;
939 retVal << ref << aList[ i ].GetRefNumber();
941 else if( range == 2 || refRangeDelimiter.IsEmpty() )
943 retVal << ref << aList[ i ].GetRefNumber();
944 retVal << refDelimiter;
945 retVal << ref << aList[ i + 1 ].GetRefNumber();
949 retVal << ref << aList[ i ].GetRefNumber();
950 retVal << refRangeDelimiter;
951 retVal << ref << aList[ i + ( range - 1 ) ].GetRefNumber();
962void SCH_REFERENCE_LIST::Show(
const char* aPrefix )
964 printf(
"%s\n", aPrefix );
966 for(
unsigned i = 0; i <
m_flatList.size(); ++i )
970 printf(
" [%-2d] ref:%-8s num:%-3d lib_part:%s\n", i, schref.
m_ref.ToStdString().c_str(),
wxString GetName() const override
int GetUnitCount() const override
virtual void SetUnit(int aUnit)
Container to create a flattened list of symbols because in a complex hierarchy, a symbol can be used ...
void ReannotateByOptions(ANNOTATE_ORDER_T aSortOption, ANNOTATE_ALGO_T aAlgoOption, int aStartNumber, const SCH_REFERENCE_LIST &aAdditionalRefs, bool aStartAtCurrent, SCH_SHEET_LIST *aHierarchy)
Forces reannotation of the provided references.
static int createFirstFreeRefId(std::vector< int > &aIdList, int aFirstValue)
Search for the first free reference number in aListId of reference numbers in use.
static bool sortByRefAndValue(const SCH_REFERENCE &item1, const SCH_REFERENCE &item2)
bool Contains(const SCH_REFERENCE &aItem) const
Return true if aItem exists in this list.
void SortByYCoordinate()
Sort the list of references by Y position.
void AnnotateByOptions(enum ANNOTATE_ORDER_T aSortOption, enum ANNOTATE_ALGO_T aAlgoOption, int aStartNumber, SCH_MULTI_UNIT_REFERENCE_MAP aLockedUnitMap, const SCH_REFERENCE_LIST &aAdditionalRefs, bool aStartAtCurrent)
Annotate the references by the provided options.
static bool sortByTimeStamp(const SCH_REFERENCE &item1, const SCH_REFERENCE &item2)
int FindRef(const wxString &aPath) const
Search the list for a symbol with a given reference.
void Annotate(bool aUseSheetNum, int aSheetIntervalId, int aStartNumber, SCH_MULTI_UNIT_REFERENCE_MAP aLockedUnitMap, const SCH_REFERENCE_LIST &aAdditionalRefs, bool aStartAtCurrent=false)
Set the reference designators in the list that have not been annotated.
void SortByXCoordinate()
Sort the list of references by X position.
static bool sortByXPosition(const SCH_REFERENCE &item1, const SCH_REFERENCE &item2)
int FindFirstUnusedReference(const SCH_REFERENCE &aRef, int aMinValue, const std::vector< int > &aRequiredUnits) const
Return the first unused reference number from the properties given in aRef, ensuring all of the units...
void SplitReferences()
Attempt to split all reference designators into a name (U) and number (1).
void SortByRefAndValue()
Sort the list of references by value.
std::vector< int > GetUnitsMatchingRef(const SCH_REFERENCE &aRef) const
Return all the unit numbers for a given reference, comparing library reference, value,...
std::vector< SCH_REFERENCE > m_flatList
static bool sortByReferenceOnly(const SCH_REFERENCE &item1, const SCH_REFERENCE &item2)
void GetRefsInUse(int aIndex, std::vector< int > &aIdList, int aMinRefId) const
Add all the reference designator numbers greater than aMinRefId to aIdList skipping the reference at ...
void ReannotateDuplicates(const SCH_REFERENCE_LIST &aAdditionalReferences)
Convenience function for the Paste Unique functionality.
static wxString Shorthand(std::vector< SCH_REFERENCE > aList, const wxString &refDelimiter, const wxString &refRangeDelimiter)
Return a shorthand string representing all the references in the list.
int FindRefByFullPath(const wxString &aFullPath) const
Search the list for a symbol with the given KIID path (as string).
std::vector< SCH_SYMBOL_INSTANCE > GetSymbolInstances() const
void AddItem(const SCH_REFERENCE &aItem)
void RemoveItem(unsigned int aIndex)
Remove an item from the list of references.
static bool sortByYPosition(const SCH_REFERENCE &item1, const SCH_REFERENCE &item2)
int CheckAnnotation(ANNOTATION_ERROR_HANDLER aErrorHandler)
Check for annotations errors.
A helper to define a symbol's reference designator in a schematic.
const SCH_SHEET_PATH & GetSheetPath() const
SCH_SYMBOL * m_rootSymbol
The symbol associated the reference object.
int m_unit
The unit number for symbol with multiple parts per package.
SCH_SHEET_PATH m_sheetPath
The sheet path for this reference.
wxString m_footprint
The footprint assigned.
int CompareLibName(const SCH_REFERENCE &item) const
int CompareRef(const SCH_REFERENCE &item) const
void SetRef(const wxString &aReference)
bool AlwaysAnnotate() const
Verify the reference should always be automatically annotated.
wxString m_numRefStr
The numeric part in original string form (may have leading zeroes)
bool m_isNew
True if not yet annotated.
void Split()
Attempt to split the reference designator into a name (U) and number (1).
bool IsSplitNeeded()
Determine if this reference needs to be split or if it likely already has been.
SCH_SYMBOL * GetSymbol() const
wxString m_ref
Symbol reference prefix, without number (for IC1, this is IC) )
int m_sheetNum
The sheet number for the reference.
void SetRefStr(const std::string &aReference)
int m_numRef
The numeric part of the reference designator.
LIB_SYMBOL * GetLibPart() const
void SetSheetNumber(int aSheetNumber)
const char * GetRefStr() const
Return reference name with unit altogether.
bool IsSameInstance(const SCH_REFERENCE &other) const
Return whether this reference refers to the same symbol instance (symbol and sheet) as another.
KIID m_symbolUuid
UUID of the symbol.
wxString m_value
The symbol value.
VECTOR2I m_symbolPos
The physical position of the symbol in schematic used to annotate by X or Y position.
wxString GetRefNumber() const
int CompareValue(const SCH_REFERENCE &item) const
void Annotate()
Update the annotation of the symbol according the current object state.
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
SCH_SHEET_PATH * FindSheetForPath(const SCH_SHEET_PATH *aPath)
Return a pointer to the first SCH_SHEET_PATH object (not necessarily the only one) matching the provi...
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
int Cmp(const SCH_SHEET_PATH &aSheetPathToTest) const
Compare if this is the same sheet path as aSheetPathToTest.
wxString SubReference(int aUnit, bool aAddSeparator=true) const
void SetValueFieldText(const wxString &aValue)
const wxString GetValue(bool aResolve, const SCH_SHEET_PATH *aPath, bool aAllowExtraText) const override
void SetRef(const SCH_SHEET_PATH *aSheet, const wxString &aReference)
Set the reference for the given sheet path for this symbol.
const wxString GetFootprintFieldText(bool aResolve, const SCH_SHEET_PATH *aPath, bool aAllowExtraText) const
VECTOR2I GetPosition() const override
int GetUnitSelection(const SCH_SHEET_PATH *aSheet) const
Return the instance-specific unit selection for the given sheet path.
void SetUnitSelection(const SCH_SHEET_PATH *aSheet, int aUnitSelection)
Set the selected unit of this symbol on one sheet.
std::unique_ptr< LIB_SYMBOL > & GetLibSymbolRef()
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const override
@ ERCE_UNANNOTATED
Symbol has not been annotated.
@ ERCE_DUPLICATE_REFERENCE
More than one symbol with the same reference.
@ ERCE_EXTRA_UNITS
Symbol has more units than are defined.
@ ERCE_DIFFERENT_UNIT_VALUE
Units of same symbol have different values.
void remove_duplicates(_Container &__c)
Deletes all duplicate values from __c.
wxString buildFullReference(const SCH_REFERENCE &aItem, int aUnitNumber=-1)
std::function< void(ERCE_T aType, const wxString &aMsg, SCH_REFERENCE *aItemA, SCH_REFERENCE *aItemB)> ANNOTATION_ERROR_HANDLER
Define a standard error handler for annotation errors.
ANNOTATE_ORDER_T
Schematic annotation order options.
@ UNSORTED
Annotate by position of symbol in the schematic sheet object list.
@ SORT_BY_X_POSITION
Annotate by X position from left to right.
@ SORT_BY_Y_POSITION
Annotate by Y position from top to bottom.
ANNOTATE_ALGO_T
Schematic annotation type options.
@ SHEET_NUMBER_X_1000
Annotate using the first free reference number starting at the sheet number * 1000.
@ INCREMENTAL_BY_REF
Annotate incrementally using the first free reference number.
@ SHEET_NUMBER_X_100
Annotate using the first free reference number starting at the sheet number * 100.
std::map< wxString, SCH_REFERENCE_LIST > SCH_MULTI_UNIT_REFERENCE_MAP
Container to map reference designators for multi-unit parts.
int StrNumCmp(const wxString &aString1, const wxString &aString2, bool aIgnoreCase)
Compare two strings with alphanumerical content.
wxString From_UTF8(const char *cstring)
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
A simple container for schematic symbol instance information.