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#ifndef _SCH_REFERENCE_LIST_H_
23#define _SCH_REFERENCE_LIST_H_
24
25#include <map>
26
27#include <lib_symbol.h>
28#include <macros.h>
29#include <sch_sheet_path.h>
30#include <sch_symbol.h>
31#include <sch_text.h>
32#include <erc/erc_settings.h>
33
34class REFDES_TRACKER;
35
43
52
53
63
64
73{
74public:
77 {
78 m_rootSymbol = nullptr;
79 m_unit = 0;
80 m_isNew = false;
81 m_numRef = 0;
82 m_flag = 0;
83 m_sheetNum = 0;
84 }
85
86 SCH_REFERENCE( SCH_SYMBOL* aSymbol, const SCH_SHEET_PATH& aSheetPath );
87
88 SCH_SYMBOL* GetSymbol() const { return m_rootSymbol; }
89
90 LIB_SYMBOL* GetLibPart() const { return m_rootSymbol->GetLibSymbolRef().get(); }
91
92 const SCH_SHEET_PATH& GetSheetPath() const { return m_sheetPath; }
93
95
96 int GetUnit() const { return m_unit; }
97 void SetUnit( int aUnit ) { m_unit = aUnit; }
98 bool IsMultiUnit() const { return GetLibPart()->GetUnitCount() > 1; }
99
100 const wxString GetValue() const { return m_value; }
101 void SetValue( const wxString& aValue ) { m_value = aValue; }
102
103 const wxString GetFootprint() const { return m_footprint; }
104 void SetFootprint( const wxString& aFP ) { m_footprint = aFP; }
105
106 void SetSheetNumber( int aSheetNumber ) { m_sheetNum = aSheetNumber; }
107
111 const wxString GetPath() const
112 {
113 return m_sheetPath.PathAsString();
114 }
115
119 const wxString GetFullPath() const
120 {
121 return m_sheetPath.PathAsString() + m_symbolUuid.AsString();
122 }
123
127 bool operator<( const SCH_REFERENCE& aRef ) const { return GetFullPath() < aRef.GetFullPath(); }
128
132 void Annotate();
133
140 bool AlwaysAnnotate() const;
141
149 void Split();
150
156 bool 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( bool aIncludeUnit = true ) const
166 {
167 wxString refNum = m_numRefStr;
168
169 if( refNum.IsEmpty() )
170 refNum << m_numRef;
171
172 if( aIncludeUnit && GetSymbol()->GetUnitCount() > 1 )
173 return GetRef() + refNum + GetSymbol()->SubReference( GetUnit() );
174 else
175 return GetRef() + refNum;
176 }
177
178 wxString GetRefNumber() const
179 {
180 if( m_numRef < 0 )
181 return wxT( "?" );
182 else
183 return m_numRefStr;
184 }
185
186 int CompareValue( const SCH_REFERENCE& item ) const
187 {
188 return m_value.Cmp( item.m_value );
189 }
190
191 int CompareRef( const SCH_REFERENCE& item ) const
192 {
193 return m_ref.CmpNoCase( item.m_ref );
194 }
195
196 int CompareLibName( const SCH_REFERENCE& item ) const
197 {
198 return m_rootSymbol->GetLibId().GetLibItemName().compare(
200 }
201
206 bool IsSameInstance( const SCH_REFERENCE& other ) const
207 {
208 // Only compare symbol and path.
209 // We may have changed the unit number or the designator but
210 // can still be referencing the same instance.
211 return GetSymbol() == other.GetSymbol()
212 && GetSheetPath().Path() == other.GetSheetPath().Path();
213 }
214
216 {
217 if( GetLibPart() )
218 return GetLibPart()->UnitsLocked();
219 else
220 return true; // Assume units locked when we don't have a library
221 }
222
223 void SetRefNum( int aNum )
224 {
225 m_numRef = aNum;
226 m_numRefStr = formatRefStr( aNum );
227 }
228
229 bool GetSymbolDNP( const wxString& aVariant = wxEmptyString ) const;
230 bool GetSymbolExcludedFromBOM( const wxString& aVariant = wxEmptyString ) const;
231 bool GetSymbolExcludedFromSim( const wxString& aVariant = wxEmptyString ) const;
232 bool GetSymbolExcludedFromBoard() const;
233
234 void SetSymbolDNP( bool aEnable, const wxString& aVariant = wxEmptyString );
235 void SetSymbolExcludedFromBOM( bool aEnable, const wxString& aVariant = wxEmptyString );
236 void SetSymbolExcludedFromSim( bool aEnable, const wxString& aVariant = wxEmptyString );
237 void SetSymbolExcludedFromBoard( bool aEnable );
238
239private:
240 wxString formatRefStr( int aNumber ) const;
241
242private:
243 friend class SCH_REFERENCE_LIST;
244
246 wxString m_ref; // it's private, use the accessors please
250 int m_unit;
252 wxString m_value;
253 wxString m_footprint;
255 bool m_isNew;
259 wxString m_numRefStr;
262};
263
264
268typedef std::function<void( ERCE_T aType, const wxString& aMsg, SCH_REFERENCE* aItemA,
270
271
280{
281public:
284
286 {
287 return m_flatList[ aIndex ];
288 }
289
290 const SCH_REFERENCE& operator[]( int aIndex ) const
291 {
292 return m_flatList[ aIndex ];
293 }
294
295 void Clear()
296 {
297 m_flatList.clear();
298 }
299
300 size_t GetCount() const { return m_flatList.size(); }
301
302 SCH_REFERENCE& GetItem( size_t aIdx ) { return m_flatList[aIdx]; }
303 const SCH_REFERENCE& GetItem( size_t aIdx ) const { return m_flatList[aIdx]; }
304
305 SCH_REFERENCE* FindItem( const SCH_REFERENCE& aItem );
306
307 void AddItem( const SCH_REFERENCE& aItem ) { m_flatList.push_back( aItem ); }
308
314 void RemoveItem( unsigned int aIndex );
315
322 bool Contains( const SCH_REFERENCE& aItem ) const;
323
324 /* Sort functions:
325 * Sort functions are used to sort symbols for annotation or BOM generation. Because
326 * sorting depends on what we want to do, there are many sort functions.
327 * Note:
328 * When creating BOM, symbols are fully annotated. References are something like U3,
329 * U5 or R4, R8. When annotating, some or all symbols are not annotated, i.e. ref is
330 * only U or R, with no number.
331 */
332
341 {
342 for( unsigned ii = 0; ii < GetCount(); ii++ )
343 m_flatList[ii].Split();
344 }
345
353 {
354 for( unsigned ii = 0; ii < GetCount(); ii++ )
355 m_flatList[ii].m_isNew = true;
356 }
357
367 {
368 /* update the reference numbers */
369 for( unsigned ii = 0; ii < GetCount(); ii++ )
370 m_flatList[ii].Annotate();
371 }
372
387 void ReannotateByOptions( ANNOTATE_ORDER_T aSortOption,
388 ANNOTATE_ALGO_T aAlgoOption,
389 int aStartNumber,
390 const SCH_REFERENCE_LIST& aAdditionalRefs,
391 bool aStartAtCurrent,
392 SCH_SHEET_LIST* aHierarchy );
393
404 void ReannotateDuplicates( const SCH_REFERENCE_LIST& aAdditionalReferences, ANNOTATE_ALGO_T aAlgoOption );
405
421 void AnnotateByOptions( enum ANNOTATE_ORDER_T aSortOption,
422 enum ANNOTATE_ALGO_T aAlgoOption,
423 int aStartNumber,
424 const SCH_MULTI_UNIT_REFERENCE_MAP& aLockedUnitMap,
425 const SCH_REFERENCE_LIST& aAdditionalRefs,
426 bool aStartAtCurrent );
427
449 void Annotate( bool aUseSheetNum, int aSheetIntervalId, int aStartNumber,
450 const SCH_MULTI_UNIT_REFERENCE_MAP& aLockedUnitMap,
451 const SCH_REFERENCE_LIST& aAdditionalRefs,
452 bool aStartAtCurrent = false );
453
466 int CheckAnnotation( ANNOTATION_ERROR_HANDLER aErrorHandler );
467
479 {
480 sort( m_flatList.begin(), m_flatList.end(), sortByXPosition );
481 }
482
494 {
495 sort( m_flatList.begin(), m_flatList.end(), sortByYPosition );
496 }
497
504 {
505 sort( m_flatList.begin(), m_flatList.end(), sortByTimeStamp );
506 }
507
520 {
521 sort( m_flatList.begin(), m_flatList.end(), sortByRefAndValue );
522 }
523
532 {
533 sort( m_flatList.begin(), m_flatList.end(), sortByReferenceOnly );
534 }
535
543 {
544 sort( m_flatList.begin(), m_flatList.end(), sortBySymbolPtr );
545 }
546
550 int FindRef( const wxString& aPath ) const;
551
558 int FindRefByFullPath( const wxString& aFullPath ) const;
559
568 void GetRefsInUse( int aIndex, std::vector<int>& aIdList, int aMinRefId ) const;
569
576 std::vector<int> GetUnitsMatchingRef( const SCH_REFERENCE& aRef ) const;
577
586 int FindFirstUnusedReference( const SCH_REFERENCE& aRef, int aMinValue,
587 const std::vector<int>& aRequiredUnits ) const;
588
589 std::vector<SCH_SYMBOL_INSTANCE> GetSymbolInstances() const;
590
591#if defined(DEBUG)
592 void Show( const char* aPrefix = "" );
593#endif
594
600 static wxString Shorthand( std::vector<SCH_REFERENCE> aList, const wxString& refDelimiter,
601 const wxString& refRangeDelimiter );
602
603 std::shared_ptr<REFDES_TRACKER> GetRefDesTracker() const
604 {
605 return m_refDesTracker;
606 }
607
608 void SetRefDesTracker( std::shared_ptr<REFDES_TRACKER> aTracker )
609 {
610 m_refDesTracker = aTracker;
611 }
612
613 friend class BACK_ANNOTATION;
614
615 typedef std::vector<SCH_REFERENCE>::iterator iterator;
616 typedef std::vector<SCH_REFERENCE>::const_iterator const_iterator;
617
618 iterator begin() { return m_flatList.begin(); }
619
620 iterator end() { return m_flatList.end(); }
621
622 const_iterator begin() const { return m_flatList.begin(); }
623
624 const_iterator end() const { return m_flatList.end(); }
625
626 iterator erase( iterator position ) { return m_flatList.erase( position ); }
627
628 iterator erase( iterator first, iterator last ) { return m_flatList.erase( first, last ); }
629
630private:
631 static bool sortByRefAndValue( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
632
633 static bool sortByXPosition( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
634
635 static bool sortByYPosition( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
636
637 static bool sortByTimeStamp( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
638
639 static bool sortByReferenceOnly( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
640
641 static bool sortBySymbolPtr( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
642
643 // Used for sorting static sortByTimeStamp function
644 friend class BACK_ANNOTATE;
645
646 std::vector<SCH_REFERENCE> m_flatList;
647
648 std::shared_ptr<REFDES_TRACKER> m_refDesTracker;
649};
650
651#endif // _SCH_REFERENCE_LIST_H_
Definition kiid.h:44
const UTF8 & GetLibItemName() const
Definition lib_id.h:98
Define a library symbol object.
Definition lib_symbol.h:79
bool UnitsLocked() const
Check whether symbol units are interchangeable.
Definition lib_symbol.h:283
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
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_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 m_footprint
The footprint assigned.
wxString formatRefStr(int aNumber) const
int CompareLibName(const SCH_REFERENCE &item) const
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).
bool IsSplitNeeded()
Determine if this reference needs to be split or if it likely already has been.
SCH_SYMBOL * GetSymbol() 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)
int m_numRef
The numeric part of the reference designator.
bool GetSymbolExcludedFromBoard() const
LIB_SYMBOL * GetLibPart() const
wxString GetFullRef(bool aIncludeUnit=true) const
Return reference name with unit altogether.
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.
KIID m_symbolUuid
UUID of the symbol.
bool GetSymbolExcludedFromSim(const wxString &aVariant=wxEmptyString) const
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)
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:69
wxString SubReference(int aUnit, bool aAddSeparator=true) const
const LIB_ID & GetLibId() const override
Definition sch_symbol.h:158
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.
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683