KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sch_reference_list.h
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) 1992-2011 jean-pierre Charras <[email protected]>
5 * Copyright (C) 2011 Wayne Stambaugh <[email protected]>
6 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22#pragma once
23
24#include <map>
25
26#include <lib_symbol.h>
27#include <macros.h>
28#include <sch_sheet_path.h>
29#include <sch_symbol.h>
30#include <sch_text.h>
31#include <erc/erc_settings.h>
32
33class REFDES_TRACKER;
34
42
51
52
62
63
72{
73public:
76 {
77 m_rootSymbol = nullptr;
78 m_unit = 0;
79 m_isNew = false;
80 m_numRef = 0;
81 m_flag = 0;
82 m_sheetNum = 0;
83 }
84
85 SCH_REFERENCE( SCH_SYMBOL* aSymbol, const SCH_SHEET_PATH& aSheetPath );
86
87 SCH_SYMBOL* GetSymbol() const { return m_rootSymbol; }
88
89 LIB_SYMBOL* GetLibPart() const { return m_rootSymbol->GetLibSymbolRef().get(); }
90
91 const SCH_SHEET_PATH& GetSheetPath() const { return m_sheetPath; }
92
94
95 int GetUnit() const { return m_unit; }
96 void SetUnit( int aUnit ) { m_unit = aUnit; }
97 bool IsMultiUnit() const { return GetLibPart()->GetUnitCount() > 1; }
98
99 const wxString GetValue() const { return m_value; }
100 void SetValue( const wxString& aValue ) { m_value = aValue; }
101
102 const wxString GetFootprint() const { return m_footprint; }
103 void SetFootprint( const wxString& aFP ) { m_footprint = aFP; }
104
105 void SetSheetNumber( int aSheetNumber ) { m_sheetNum = aSheetNumber; }
106
110 const wxString GetPath() const
111 {
112 return m_sheetPath.PathAsString();
113 }
114
118 const wxString GetFullPath() const
119 {
120 return m_sheetPath.PathAsString() + m_symbolUuid.AsString();
121 }
122
126 bool operator<( const SCH_REFERENCE& aRef ) const { return GetFullPath() < aRef.GetFullPath(); }
127
131 void Annotate();
132
139 bool AlwaysAnnotate() const;
140
148 void Split();
149
155 bool IsSplitNeeded() const;
156 bool IsSplit() const { return !IsSplitNeeded(); }
157
158 void SetRef( const wxString& aReference ) { m_ref = aReference; }
159 wxString GetRef() const { return m_ref; }
160
161 void SetRefStr( const std::string& aReference ) { m_ref = aReference; }
162 const char* GetRefStr() const { return m_ref.c_str(); }
163
165 wxString GetFullRef() const
166 {
167 wxString ref = GetRef();
168
169 if( IsSplit() )
170 {
171 if( !m_numRefStr.IsEmpty() )
172 ref += m_numRefStr;
173 else
174 ref << m_numRef;
175 }
176
177 if( GetSymbol()->GetUnitCount() > 1 )
178 ref += GetSymbol()->SubReference( GetUnit() );
179
180 return ref;
181 }
182
185 wxString GetCanonicalRef( bool aIncludeUnit ) const
186 {
187 wxString ref = GetRef();
188
189 if( IsSplit() )
190 {
191 if( m_numRef >= 0 )
192 ref << m_numRef;
193 else
194 ref += m_numRefStr;
195 }
196
197 if( aIncludeUnit && GetSymbol()->GetUnitCount() > 1 )
198 ref += GetSymbol()->SubReference( GetUnit() );
199
200 return ref;
201 }
202
203 wxString GetRefNumber() const
204 {
205 if( m_numRef < 0 )
206 return wxT( "?" );
207 else
208 return m_numRefStr;
209 }
210
211 int CompareValue( const SCH_REFERENCE& item ) const
212 {
213 // Sort based on natural sort, but don't consider naturally-equivalent values
214 // to be equal. Run them through a second, non-natural compare.
215
216 int natural = StrNumCmp( m_value, item.m_value, false );
217
218 if( natural != 0 )
219 return natural;
220
221 return m_value.Cmp( item.m_value );
222 }
223
224 int CompareRef( const SCH_REFERENCE& item ) const
225 {
226 // Sort based on natural sort, but don't consider naturally-equivalent values
227 // to be equal. Run them through a second, non-natural compare.
228
229 int natural = StrNumCmp( m_ref, item.m_ref, false );
230
231 if( natural != 0 )
232 return natural;
233
234 return m_ref.Cmp( item.m_ref );
235 }
236
237 int CompareLibId( const SCH_REFERENCE& item ) const
238 {
239 return GetSymbol()->GetLibId().compare( item.GetSymbol()->GetLibId() );
240 }
241
246 bool IsSameInstance( const SCH_REFERENCE& other ) const
247 {
248 // Only compare symbol and path.
249 // We may have changed the unit number or the designator but
250 // can still be referencing the same instance.
251 return GetSymbol() == other.GetSymbol()
252 && GetSheetPath().Path() == other.GetSheetPath().Path();
253 }
254
256 {
257 if( GetLibPart() )
258 return GetLibPart()->UnitsLocked();
259 else
260 return true; // Assume units locked when we don't have a library
261 }
262
263 void SetRefNum( int aNum )
264 {
265 m_numRef = aNum;
266 m_numRefStr = formatRefStr( aNum );
267 }
268
269 bool GetSymbolDNP( const wxString& aVariant = wxEmptyString ) const;
270 bool GetSymbolExcludedFromBOM( const wxString& aVariant = wxEmptyString ) const;
271 bool GetSymbolExcludedFromSim( const wxString& aVariant = wxEmptyString ) const;
272 bool GetSymbolExcludedFromBoard() const;
273 bool GetSymbolExcludedFromPosFiles( const wxString& aVariant = wxEmptyString ) const;
274
275 void SetSymbolDNP( bool aEnable, const wxString& aVariant = wxEmptyString );
276 void SetSymbolExcludedFromBOM( bool aEnable, const wxString& aVariant = wxEmptyString );
277 void SetSymbolExcludedFromSim( bool aEnable, const wxString& aVariant = wxEmptyString );
278 void SetSymbolExcludedFromBoard( bool aEnable );
279 void SetSymbolExcludedFromPosFiles( bool aEnable, const wxString& aVariant = wxEmptyString );
280
281private:
282 wxString formatRefStr( int aNumber ) const;
283
284private:
285 friend class SCH_REFERENCE_LIST;
286
288 wxString m_ref; // it's private, use the accessors please
292 int m_unit;
294 wxString m_value;
295 wxString m_footprint;
297 bool m_isNew;
301 wxString m_numRefStr;
304};
305
306
310typedef std::function<void( ERCE_T aType, const wxString& aMsg, SCH_REFERENCE* aItemA,
312
313
322{
323public:
326
328 {
329 return m_flatList[ aIndex ];
330 }
331
332 const SCH_REFERENCE& operator[]( int aIndex ) const
333 {
334 return m_flatList[ aIndex ];
335 }
336
337 void Clear()
338 {
339 m_flatList.clear();
340 }
341
342 size_t GetCount() const { return m_flatList.size(); }
343
344 SCH_REFERENCE& GetItem( size_t aIdx ) { return m_flatList[aIdx]; }
345 const SCH_REFERENCE& GetItem( size_t aIdx ) const { return m_flatList[aIdx]; }
346
347 SCH_REFERENCE* FindItem( const SCH_REFERENCE& aItem );
348
349 void AddItem( const SCH_REFERENCE& aItem ) { m_flatList.push_back( aItem ); }
350
356 void RemoveItem( unsigned int aIndex );
357
364 bool Contains( const SCH_REFERENCE& aItem ) const;
365
366 /* Sort functions:
367 * Sort functions are used to sort symbols for annotation or BOM generation. Because
368 * sorting depends on what we want to do, there are many sort functions.
369 * Note:
370 * When creating BOM, symbols are fully annotated. References are something like U3,
371 * U5 or R4, R8. When annotating, some or all symbols are not annotated, i.e. ref is
372 * only U or R, with no number.
373 */
374
383 {
384 for( unsigned ii = 0; ii < GetCount(); ii++ )
385 m_flatList[ii].Split();
386 }
387
395 {
396 for( unsigned ii = 0; ii < GetCount(); ii++ )
397 m_flatList[ii].m_isNew = true;
398 }
399
409 {
410 /* update the reference numbers */
411 for( unsigned ii = 0; ii < GetCount(); ii++ )
412 m_flatList[ii].Annotate();
413 }
414
427 void ReannotateByOptions( ANNOTATE_ORDER_T aSortOption,
428 ANNOTATE_ALGO_T aAlgoOption,
429 int aStartNumber,
430 const SCH_REFERENCE_LIST& aAdditionalRefs,
431 bool aStartAtCurrent,
432 SCH_SHEET_LIST* aHierarchy );
433
444 void ReannotateDuplicates( const SCH_REFERENCE_LIST& aAdditionalReferences, ANNOTATE_ALGO_T aAlgoOption );
445
459 void AnnotateByOptions( enum ANNOTATE_ORDER_T aSortOption,
460 enum ANNOTATE_ALGO_T aAlgoOption,
461 int aStartNumber,
462 const SCH_MULTI_UNIT_REFERENCE_MAP& aLockedUnitMap,
463 const SCH_REFERENCE_LIST& aAdditionalRefs,
464 bool aStartAtCurrent );
465
487 void Annotate( bool aUseSheetNum, int aSheetIntervalId, int aStartNumber,
488 const SCH_MULTI_UNIT_REFERENCE_MAP& aLockedUnitMap,
489 const SCH_REFERENCE_LIST& aAdditionalRefs,
490 bool aStartAtCurrent = false );
491
504 int CheckAnnotation( ANNOTATION_ERROR_HANDLER aErrorHandler );
505
517 {
518 sort( m_flatList.begin(), m_flatList.end(), sortByXPosition );
519 }
520
532 {
533 sort( m_flatList.begin(), m_flatList.end(), sortByYPosition );
534 }
535
542 {
543 sort( m_flatList.begin(), m_flatList.end(), sortByTimeStamp );
544 }
545
558 {
559 sort( m_flatList.begin(), m_flatList.end(), sortByRefAndValue );
560 }
561
570 {
571 sort( m_flatList.begin(), m_flatList.end(), sortByReferenceOnly );
572 }
573
581 {
582 sort( m_flatList.begin(), m_flatList.end(), sortBySymbolPtr );
583 }
584
588 int FindRef( const wxString& aPath ) const;
589
596 int FindRefByFullPath( const wxString& aFullPath ) const;
597
606 void GetRefsInUse( int aIndex, std::vector<int>& aIdList, int aMinRefId ) const;
607
614 std::vector<int> GetUnitsMatchingRef( const SCH_REFERENCE& aRef ) const;
615
624 int FindFirstUnusedReference( const SCH_REFERENCE& aRef, int aMinValue,
625 const std::vector<int>& aRequiredUnits ) const;
626
627 std::vector<SCH_SYMBOL_INSTANCE> GetSymbolInstances() const;
628
629#if defined(DEBUG)
630 void Show( const char* aPrefix = "" );
631#endif
632
633 std::shared_ptr<REFDES_TRACKER> GetRefDesTracker() const { return m_refDesTracker; }
634 void SetRefDesTracker( std::shared_ptr<REFDES_TRACKER> aTracker ) { m_refDesTracker = aTracker; }
635
636 friend class BACK_ANNOTATION;
637
638 typedef std::vector<SCH_REFERENCE>::iterator iterator;
639 typedef std::vector<SCH_REFERENCE>::const_iterator const_iterator;
640
641 iterator begin() { return m_flatList.begin(); }
642
643 iterator end() { return m_flatList.end(); }
644
645 const_iterator begin() const { return m_flatList.begin(); }
646
647 const_iterator end() const { return m_flatList.end(); }
648
649 iterator erase( iterator position ) { return m_flatList.erase( position ); }
650
651 iterator erase( iterator first, iterator last ) { return m_flatList.erase( first, last ); }
652
653private:
654 static bool sortByRefAndValue( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
655
656 static bool sortByXPosition( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
657
658 static bool sortByYPosition( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
659
660 static bool sortByTimeStamp( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
661
662 static bool sortByReferenceOnly( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
663
664 static bool sortBySymbolPtr( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
665
666 // Used for sorting static sortByTimeStamp function
667 friend class BACK_ANNOTATE;
668
669private:
670 std::vector<SCH_REFERENCE> m_flatList;
671 std::shared_ptr<REFDES_TRACKER> m_refDesTracker;
672};
Definition kiid.h:46
int compare(const LIB_ID &aLibId) const
Compare the contents of LIB_ID objects by performing a std::string comparison of the library nickname...
Definition lib_id.cpp:174
Define a library symbol object.
Definition lib_symbol.h:114
bool UnitsLocked() const
Check whether symbol units are interchangeable.
int GetUnitCount() const override
Class to efficiently track reference designators and provide next available designators.
Container to create a flattened list of symbols because in a complex hierarchy, a symbol can be used ...
void SortByReferenceOnly()
Sort the list of references by reference.
SCH_REFERENCE * FindItem(const SCH_REFERENCE &aItem)
SCH_REFERENCE & GetItem(size_t aIdx)
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 bool sortByRefAndValue(const SCH_REFERENCE &item1, const SCH_REFERENCE &item2)
static bool sortBySymbolPtr(const SCH_REFERENCE &item1, const SCH_REFERENCE &item2)
bool Contains(const SCH_REFERENCE &aItem) const
Return true if aItem exists in this list.
void AnnotateByOptions(enum ANNOTATE_ORDER_T aSortOption, enum ANNOTATE_ALGO_T aAlgoOption, int aStartNumber, const SCH_MULTI_UNIT_REFERENCE_MAP &aLockedUnitMap, const SCH_REFERENCE_LIST &aAdditionalRefs, bool aStartAtCurrent)
Annotate the references by the provided options.
void Annotate(bool aUseSheetNum, int aSheetIntervalId, int aStartNumber, const 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 SortByYCoordinate()
Sort the list of references by Y position.
void SetRefDesTracker(std::shared_ptr< REFDES_TRACKER > aTracker)
const SCH_REFERENCE & operator[](int aIndex) const
void SortByTimeStamp()
Sort the flat list by Time Stamp (sheet path + timestamp).
iterator erase(iterator position)
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 SortBySymbolPtr()
Sort the list by the symbol pointer.
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).
std::shared_ptr< REFDES_TRACKER > m_refDesTracker
A list of previously used reference designators.
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
iterator erase(iterator first, iterator last)
static bool sortByReferenceOnly(const SCH_REFERENCE &item1, const SCH_REFERENCE &item2)
std::vector< SCH_REFERENCE >::iterator iterator
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 ...
const SCH_REFERENCE & GetItem(size_t aIdx) const
int FindRefByFullPath(const wxString &aFullPath) const
Search the list for a symbol with the given KIID path (as string).
std::vector< SCH_REFERENCE >::const_iterator const_iterator
std::shared_ptr< REFDES_TRACKER > GetRefDesTracker() const
void RemoveAnnotation()
Treat all symbols in this list as non-annotated.
const_iterator end() const
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.
void ReannotateDuplicates(const SCH_REFERENCE_LIST &aAdditionalReferences, ANNOTATE_ALGO_T aAlgoOption)
Convenience function for the Paste Unique functionality.
static bool sortByYPosition(const SCH_REFERENCE &item1, const SCH_REFERENCE &item2)
const_iterator begin() const
SCH_REFERENCE & operator[](int aIndex)
void UpdateAnnotation()
Update the symbol references for the schematic project (or the current sheet).
int CheckAnnotation(ANNOTATION_ERROR_HANDLER aErrorHandler)
Check for annotations errors.
A helper to define a symbol's reference designator in a schematic.
void SetSymbolExcludedFromBoard(bool aEnable)
const SCH_SHEET_PATH & GetSheetPath() const
void SetSymbolExcludedFromBOM(bool aEnable, const wxString &aVariant=wxEmptyString)
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 GetCanonicalRef(bool aIncludeUnit) const
Return reference name in canonical format, optionally with unit.
wxString m_footprint
The footprint assigned.
wxString formatRefStr(int aNumber) const
wxString GetFullRef() const
Return reference name with unit altogether.
void SetValue(const wxString &aValue)
int CompareRef(const SCH_REFERENCE &item) const
void SetRef(const wxString &aReference)
const wxString GetFootprint() const
friend class SCH_REFERENCE_LIST
const wxString GetFullPath() const
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).
SCH_SHEET_PATH & GetSheetPath()
bool m_isNew
True if not yet annotated.
bool GetSymbolExcludedFromBOM(const wxString &aVariant=wxEmptyString) const
bool operator<(const SCH_REFERENCE &aRef) const
Compare by full path to make std::set work.
void SetUnit(int aUnit)
bool GetSymbolDNP(const wxString &aVariant=wxEmptyString) const
void Split()
Attempt to split the reference designator into a name (U) and number (1).
SCH_SYMBOL * GetSymbol() const
bool GetSymbolExcludedFromPosFiles(const wxString &aVariant=wxEmptyString) const
void SetFootprint(const wxString &aFP)
wxString m_ref
Symbol reference prefix, without number (for IC1, this is IC) )
void SetRefNum(int aNum)
void SetSymbolExcludedFromSim(bool aEnable, const wxString &aVariant=wxEmptyString)
wxString GetRef() const
int m_sheetNum
The sheet number for the reference.
void SetRefStr(const std::string &aReference)
bool IsSplitNeeded() const
Determine if this reference needs to be split or if it likely already has been.
int m_numRef
The numeric part of the reference designator.
bool GetSymbolExcludedFromBoard() const
LIB_SYMBOL * GetLibPart() const
const wxString GetValue() const
void SetSheetNumber(int aSheetNumber)
const char * GetRefStr() const
bool IsSameInstance(const SCH_REFERENCE &other) const
Return whether this reference refers to the same symbol instance (symbol and sheet) as another.
int CompareLibId(const SCH_REFERENCE &item) const
KIID m_symbolUuid
UUID of the symbol.
bool GetSymbolExcludedFromSim(const wxString &aVariant=wxEmptyString) const
void SetSymbolExcludedFromPosFiles(bool aEnable, const wxString &aVariant=wxEmptyString)
int GetUnit() const
bool IsMultiUnit() const
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.
const wxString GetPath() const
wxString GetRefNumber() const
int CompareValue(const SCH_REFERENCE &item) const
void Annotate()
Update the annotation of the symbol according the current object state.
void SetSymbolDNP(bool aEnable, const wxString &aVariant=wxEmptyString)
bool IsSplit() const
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
KIID_PATH Path() const
Get the sheet path as an KIID_PATH.
Schematic symbol object.
Definition sch_symbol.h:75
wxString SubReference(int aUnit, bool aAddSeparator=true) const
const LIB_ID & GetLibId() const override
Definition sch_symbol.h:165
ERCE_T
ERC error codes.
This file contains miscellaneous commonly used macros and functions.
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_SCOPE_T
Schematic annotation scope options.
@ ANNOTATE_SELECTION
Annotate the selection.
@ ANNOTATE_CURRENT_SHEET
Annotate the current sheet.
@ ANNOTATE_ALL
Annotate the full schematic.
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.
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
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.
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708