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 // Values are compared with case sensitivity
214 return StrNumCmp( m_value, item.m_value, false );
215 }
216
217 int CompareRef( const SCH_REFERENCE& item ) const
218 {
219 // References are compared ignoring case
220 return StrNumCmp( m_ref, item.m_ref, true );
221 }
222
223 int CompareLibId( const SCH_REFERENCE& item ) const
224 {
225 return GetSymbol()->GetLibId().compare( item.GetSymbol()->GetLibId() );
226 }
227
232 bool IsSameInstance( const SCH_REFERENCE& other ) const
233 {
234 // Only compare symbol and path.
235 // We may have changed the unit number or the designator but
236 // can still be referencing the same instance.
237 return GetSymbol() == other.GetSymbol()
238 && GetSheetPath().Path() == other.GetSheetPath().Path();
239 }
240
242 {
243 if( GetLibPart() )
244 return GetLibPart()->UnitsLocked();
245 else
246 return true; // Assume units locked when we don't have a library
247 }
248
249 void SetRefNum( int aNum )
250 {
251 m_numRef = aNum;
252 m_numRefStr = formatRefStr( aNum );
253 }
254
255 bool GetSymbolDNP( const wxString& aVariant = wxEmptyString ) const;
256 bool GetSymbolExcludedFromBOM( const wxString& aVariant = wxEmptyString ) const;
257 bool GetSymbolExcludedFromSim( const wxString& aVariant = wxEmptyString ) const;
258 bool GetSymbolExcludedFromBoard() const;
259 bool GetSymbolExcludedFromPosFiles( const wxString& aVariant = wxEmptyString ) const;
260
261 void SetSymbolDNP( bool aEnable, const wxString& aVariant = wxEmptyString );
262 void SetSymbolExcludedFromBOM( bool aEnable, const wxString& aVariant = wxEmptyString );
263 void SetSymbolExcludedFromSim( bool aEnable, const wxString& aVariant = wxEmptyString );
264 void SetSymbolExcludedFromBoard( bool aEnable );
265 void SetSymbolExcludedFromPosFiles( bool aEnable, const wxString& aVariant = wxEmptyString );
266
267private:
268 wxString formatRefStr( int aNumber ) const;
269
270private:
271 friend class SCH_REFERENCE_LIST;
272
274 wxString m_ref; // it's private, use the accessors please
278 int m_unit;
280 wxString m_value;
281 wxString m_footprint;
283 bool m_isNew;
287 wxString m_numRefStr;
290};
291
292
296typedef std::function<void( ERCE_T aType, const wxString& aMsg, SCH_REFERENCE* aItemA,
298
299
308{
309public:
312
314 {
315 return m_flatList[ aIndex ];
316 }
317
318 const SCH_REFERENCE& operator[]( int aIndex ) const
319 {
320 return m_flatList[ aIndex ];
321 }
322
323 void Clear()
324 {
325 m_flatList.clear();
326 }
327
328 size_t GetCount() const { return m_flatList.size(); }
329
330 SCH_REFERENCE& GetItem( size_t aIdx ) { return m_flatList[aIdx]; }
331 const SCH_REFERENCE& GetItem( size_t aIdx ) const { return m_flatList[aIdx]; }
332
333 SCH_REFERENCE* FindItem( const SCH_REFERENCE& aItem );
334
335 void AddItem( const SCH_REFERENCE& aItem ) { m_flatList.push_back( aItem ); }
336
342 void RemoveItem( unsigned int aIndex );
343
350 bool Contains( const SCH_REFERENCE& aItem ) const;
351
352 /* Sort functions:
353 * Sort functions are used to sort symbols for annotation or BOM generation. Because
354 * sorting depends on what we want to do, there are many sort functions.
355 * Note:
356 * When creating BOM, symbols are fully annotated. References are something like U3,
357 * U5 or R4, R8. When annotating, some or all symbols are not annotated, i.e. ref is
358 * only U or R, with no number.
359 */
360
369 {
370 for( unsigned ii = 0; ii < GetCount(); ii++ )
371 m_flatList[ii].Split();
372 }
373
381 {
382 for( unsigned ii = 0; ii < GetCount(); ii++ )
383 m_flatList[ii].m_isNew = true;
384 }
385
395 {
396 /* update the reference numbers */
397 for( unsigned ii = 0; ii < GetCount(); ii++ )
398 m_flatList[ii].Annotate();
399 }
400
413 void ReannotateByOptions( ANNOTATE_ORDER_T aSortOption,
414 ANNOTATE_ALGO_T aAlgoOption,
415 int aStartNumber,
416 const SCH_REFERENCE_LIST& aAdditionalRefs,
417 bool aStartAtCurrent,
418 SCH_SHEET_LIST* aHierarchy );
419
430 void ReannotateDuplicates( const SCH_REFERENCE_LIST& aAdditionalReferences, ANNOTATE_ALGO_T aAlgoOption );
431
445 void AnnotateByOptions( enum ANNOTATE_ORDER_T aSortOption,
446 enum ANNOTATE_ALGO_T aAlgoOption,
447 int aStartNumber,
448 const SCH_MULTI_UNIT_REFERENCE_MAP& aLockedUnitMap,
449 const SCH_REFERENCE_LIST& aAdditionalRefs,
450 bool aStartAtCurrent );
451
473 void Annotate( bool aUseSheetNum, int aSheetIntervalId, int aStartNumber,
474 const SCH_MULTI_UNIT_REFERENCE_MAP& aLockedUnitMap,
475 const SCH_REFERENCE_LIST& aAdditionalRefs,
476 bool aStartAtCurrent = false );
477
490 int CheckAnnotation( ANNOTATION_ERROR_HANDLER aErrorHandler );
491
503 {
504 sort( m_flatList.begin(), m_flatList.end(), sortByXPosition );
505 }
506
518 {
519 sort( m_flatList.begin(), m_flatList.end(), sortByYPosition );
520 }
521
528 {
529 sort( m_flatList.begin(), m_flatList.end(), sortByTimeStamp );
530 }
531
544 {
545 sort( m_flatList.begin(), m_flatList.end(), sortByRefAndValue );
546 }
547
556 {
557 sort( m_flatList.begin(), m_flatList.end(), sortByReferenceOnly );
558 }
559
567 {
568 sort( m_flatList.begin(), m_flatList.end(), sortBySymbolPtr );
569 }
570
574 int FindRef( const wxString& aPath ) const;
575
582 int FindRefByFullPath( const wxString& aFullPath ) const;
583
592 void GetRefsInUse( int aIndex, std::vector<int>& aIdList, int aMinRefId ) const;
593
600 std::vector<int> GetUnitsMatchingRef( const SCH_REFERENCE& aRef ) const;
601
610 int FindFirstUnusedReference( const SCH_REFERENCE& aRef, int aMinValue,
611 const std::vector<int>& aRequiredUnits ) const;
612
613 std::vector<SCH_SYMBOL_INSTANCE> GetSymbolInstances() const;
614
615#if defined(DEBUG)
616 void Show( const char* aPrefix = "" );
617#endif
618
619 std::shared_ptr<REFDES_TRACKER> GetRefDesTracker() const { return m_refDesTracker; }
620 void SetRefDesTracker( std::shared_ptr<REFDES_TRACKER> aTracker ) { m_refDesTracker = aTracker; }
621
622 friend class BACK_ANNOTATION;
623
624 typedef std::vector<SCH_REFERENCE>::iterator iterator;
625 typedef std::vector<SCH_REFERENCE>::const_iterator const_iterator;
626
627 iterator begin() { return m_flatList.begin(); }
628
629 iterator end() { return m_flatList.end(); }
630
631 const_iterator begin() const { return m_flatList.begin(); }
632
633 const_iterator end() const { return m_flatList.end(); }
634
635 iterator erase( iterator position ) { return m_flatList.erase( position ); }
636
637 iterator erase( iterator first, iterator last ) { return m_flatList.erase( first, last ); }
638
639private:
640 static bool sortByRefAndValue( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
641
642 static bool sortByXPosition( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
643
644 static bool sortByYPosition( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
645
646 static bool sortByTimeStamp( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
647
648 static bool sortByReferenceOnly( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
649
650 static bool sortBySymbolPtr( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
651
652 // Used for sorting static sortByTimeStamp function
653 friend class BACK_ANNOTATE;
654
655private:
656 std::vector<SCH_REFERENCE> m_flatList;
657 std::shared_ptr<REFDES_TRACKER> m_refDesTracker;
658};
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:73
wxString SubReference(int aUnit, bool aAddSeparator=true) const
const LIB_ID & GetLibId() const override
Definition sch_symbol.h:163
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