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 (C) 1992-2023 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, you may find one here:
20 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21 * or you may search the http://www.gnu.org website for the version 2 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
26#ifndef _SCH_REFERENCE_LIST_H_
27#define _SCH_REFERENCE_LIST_H_
28
29#include <map>
30
31#include <lib_symbol.h>
32#include <macros.h>
33#include <sch_sheet_path.h>
34#include <sch_symbol.h>
35#include <sch_text.h>
36#include <erc_settings.h>
37
38
41{
45};
46
47
50{
53 UNSORTED,
55};
56
57
60{
66};
67
68
77{
78public:
81 {
82 m_rootSymbol = nullptr;
83 m_libPart = nullptr;
84 m_unit = 0;
85 m_isNew = false;
86 m_numRef = 0;
87 m_flag = 0;
88 m_sheetNum = 0;
89 }
90
91 SCH_REFERENCE( SCH_SYMBOL* aSymbol, LIB_SYMBOL* aLibSymbol, const SCH_SHEET_PATH& aSheetPath );
92
93 SCH_SYMBOL* GetSymbol() const { return m_rootSymbol; }
94
95 LIB_SYMBOL* GetLibPart() const { return m_libPart; }
96
97 const SCH_SHEET_PATH& GetSheetPath() const { return m_sheetPath; }
98
100
101 int GetUnit() const { return m_unit; }
102 void SetUnit( int aUnit ) { m_unit = aUnit; }
103 bool IsMultiUnit() const { return GetLibPart()->GetUnitCount() > 1; }
104
105 const wxString GetValue() const { return m_value; }
106 void SetValue( const wxString& aValue ) { m_value = aValue; }
107
108 const wxString GetFootprint() const { return m_footprint; }
109 void SetFootprint( const wxString& aFP ) { m_footprint = aFP; }
110
111 void SetSheetNumber( int aSheetNumber ) { m_sheetNum = aSheetNumber; }
112
116 const wxString GetPath() const
117 {
118 return m_sheetPath.PathAsString();
119 }
120
124 const wxString GetFullPath() const
125 {
127 }
128
129 /*
130 * Compares by full path to make std::set work
131 */
132 bool operator<( const SCH_REFERENCE& aRef ) const { return GetFullPath() < aRef.GetFullPath(); }
133
137 void Annotate();
138
145 bool AlwaysAnnotate() const;
146
154 void Split();
155
161 bool IsSplitNeeded();
162
163 void SetRef( const wxString& aReference ) { m_ref = aReference; }
164 wxString GetRef() const { return m_ref; }
165
166 void SetRefStr( const std::string& aReference ) { m_ref = aReference; }
167 const char* GetRefStr() const { return m_ref.c_str(); }
168
170 wxString GetFullRef() const
171 {
172 wxString refNum = m_numRefStr;
173
174 if( refNum.IsEmpty() )
175 refNum << m_numRef;
176
177 if( GetSymbol()->GetUnitCount() > 1 )
178 return GetRef() + refNum + GetSymbol()->SubReference( GetUnit() );
179 else
180 return GetRef() + refNum;
181 }
182
183 wxString GetRefNumber() const
184 {
185 wxString ref;
186
187 if( m_numRef < 0 )
188 return wxT( "?" );
189
190 // To avoid a risk of duplicate, for power symbols the ref number is 0nnn instead of nnn.
191 // Just because sometimes only power symbols are annotated
192 if( GetLibPart() && GetLibPart()->IsPower() )
193 ref = wxT( "0" );
194
195 return ref << m_numRef;
196 }
197
198 int CompareValue( const SCH_REFERENCE& item ) const
199 {
200 return m_value.Cmp( item.m_value );
201 }
202
203 int CompareRef( const SCH_REFERENCE& item ) const
204 {
205 return m_ref.CmpNoCase( item.m_ref );
206 }
207
208 int CompareLibName( const SCH_REFERENCE& item ) const
209 {
212 }
213
218 bool IsSameInstance( const SCH_REFERENCE& other ) const
219 {
220 // Only compare symbol and path.
221 // We may have changed the unit number or the designator but
222 // can still be referencing the same instance.
223 return GetSymbol() == other.GetSymbol()
224 && GetSheetPath().Path() == other.GetSheetPath().Path();
225 }
226
228 {
229 if( m_libPart )
230 return m_libPart->UnitsLocked();
231 else
232 return true; // Assume units locked when we don't have a library
233 }
234
235private:
236 friend class SCH_REFERENCE_LIST;
237
239 wxString m_ref; // it's private, use the accessors please
244 int m_unit;
246 wxString m_value;
247 wxString m_footprint;
249 bool m_isNew;
253 wxString m_numRefStr;
255};
256
257
261typedef std::function<void( ERCE_T aType, const wxString& aMsg, SCH_REFERENCE* aItemA,
263
264
273{
274public:
276 {
277 }
278
280 {
281 return m_flatList[ aIndex ];
282 }
283
284 const SCH_REFERENCE& operator[]( int aIndex ) const
285 {
286 return m_flatList[ aIndex ];
287 }
288
289 void Clear()
290 {
291 m_flatList.clear();
292 }
293
294 size_t GetCount() const { return m_flatList.size(); }
295
296 SCH_REFERENCE& GetItem( int aIdx ) { return m_flatList[aIdx]; }
297 const SCH_REFERENCE& GetItem( int aIdx ) const { return m_flatList[aIdx]; }
298
299 void AddItem( const SCH_REFERENCE& aItem ) { m_flatList.push_back( aItem ); }
300
306 void RemoveItem( unsigned int aIndex );
307
313 bool Contains( const SCH_REFERENCE& aItem ) const;
314
315 /* Sort functions:
316 * Sort functions are used to sort symbols for annotation or BOM generation. Because
317 * sorting depends on what we want to do, there are many sort functions.
318 * Note:
319 * When creating BOM, symbols are fully annotated. References are something like U3,
320 * U5 or R4, R8. When annotating, some or all symbols are not annotated, i.e. ref is
321 * only U or R, with no number.
322 */
323
332 {
333 for( unsigned ii = 0; ii < GetCount(); ii++ )
334 m_flatList[ii].Split();
335 }
336
343 {
344 for( unsigned ii = 0; ii < GetCount(); ii++ )
345 m_flatList[ii].m_isNew = true;
346 }
347
357 {
358 /* update the reference numbers */
359 for( unsigned ii = 0; ii < GetCount(); ii++ )
360 m_flatList[ii].Annotate();
361 }
362
377 void ReannotateByOptions( ANNOTATE_ORDER_T aSortOption,
378 ANNOTATE_ALGO_T aAlgoOption,
379 int aStartNumber,
380 const SCH_REFERENCE_LIST& aAdditionalRefs,
381 bool aStartAtCurrent,
382 SCH_SHEET_LIST* aHierarchy );
383
393 void ReannotateDuplicates( const SCH_REFERENCE_LIST& aAdditionalReferences );
394
410 void AnnotateByOptions( enum ANNOTATE_ORDER_T aSortOption,
411 enum ANNOTATE_ALGO_T aAlgoOption,
412 int aStartNumber,
413 SCH_MULTI_UNIT_REFERENCE_MAP aLockedUnitMap,
414 const SCH_REFERENCE_LIST& aAdditionalRefs,
415 bool aStartAtCurrent );
416
438 void Annotate( bool aUseSheetNum, int aSheetIntervalId, int aStartNumber,
439 SCH_MULTI_UNIT_REFERENCE_MAP aLockedUnitMap,
440 const SCH_REFERENCE_LIST& aAdditionalRefs, bool aStartAtCurrent = false );
441
454 int CheckAnnotation( ANNOTATION_ERROR_HANDLER aErrorHandler );
455
467 {
468 sort( m_flatList.begin(), m_flatList.end(), sortByXPosition );
469 }
470
482 {
483 sort( m_flatList.begin(), m_flatList.end(), sortByYPosition );
484 }
485
492 {
493 sort( m_flatList.begin(), m_flatList.end(), sortByTimeStamp );
494 }
495
508 {
509 sort( m_flatList.begin(), m_flatList.end(), sortByRefAndValue );
510 }
511
520 {
521 sort( m_flatList.begin(), m_flatList.end(), sortByReferenceOnly );
522 }
523
527 int FindRef( const wxString& aPath ) const;
528
535 int FindRefByFullPath( const wxString& aFullPath ) const;
536
545 void GetRefsInUse( int aIndex, std::vector<int>& aIdList, int aMinRefId ) const;
546
553 std::vector<int> GetUnitsMatchingRef( const SCH_REFERENCE& aRef ) const;
554
563 int FindFirstUnusedReference( const SCH_REFERENCE& aRef, int aMinValue,
564 const std::vector<int>& aRequiredUnits ) const;
565
566 std::vector<SCH_SYMBOL_INSTANCE> GetSymbolInstances() const;
567
568#if defined(DEBUG)
569 void Show( const char* aPrefix = "" );
570#endif
571
577 static wxString Shorthand( std::vector<SCH_REFERENCE> aList, const wxString& refDelimiter,
578 const wxString& refRangeDelimiter );
579
580 friend class BACK_ANNOTATION;
581
582 typedef std::vector<SCH_REFERENCE>::iterator iterator;
583 typedef std::vector<SCH_REFERENCE>::const_iterator const_iterator;
584
585 iterator begin() { return m_flatList.begin(); }
586
587 iterator end() { return m_flatList.end(); }
588
589 const_iterator begin() const { return m_flatList.begin(); }
590
591 const_iterator end() const { return m_flatList.end(); }
592
593 iterator erase( iterator position ) { return m_flatList.erase( position ); }
594
595 iterator erase( iterator first, iterator last ) { return m_flatList.erase( first, last ); }
596
597private:
598 static bool sortByRefAndValue( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
599
600 static bool sortByXPosition( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
601
602 static bool sortByYPosition( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
603
604 static bool sortByTimeStamp( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
605
606 static bool sortByReferenceOnly( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
607
620 static int createFirstFreeRefId( std::vector<int>& aIdList, int aFirstValue );
621
622 // Used for sorting static sortByTimeStamp function
623 friend class BACK_ANNOTATE;
624
625 std::vector<SCH_REFERENCE> m_flatList;
626};
627
628#endif // _SCH_REFERENCE_LIST_H_
Back annotation algorithm class used to receive, check, and apply a NETLIST from Pcbnew.
Definition: backannotate.h:56
Definition: kiid.h:49
wxString AsString() const
Definition: kiid.cpp:257
const UTF8 & GetLibItemName() const
Definition: lib_id.h:102
Define a library symbol object.
Definition: lib_symbol.h:99
bool UnitsLocked() const
Check whether symbol units are interchangeable.
Definition: lib_symbol.h:286
int GetUnitCount() const override
For items with units, return the number of units.
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.
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.
const SCH_REFERENCE & operator[](int aIndex) const
void SortByTimeStamp()
Sort the flat list by Time Stamp (sheet path + timestamp).
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.
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 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)
size_t GetCount() const
friend class BACK_ANNOTATION
const SCH_REFERENCE & GetItem(int aIdx) const
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).
SCH_REFERENCE & GetItem(int aIdx)
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 ...
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_REFERENCE >::const_iterator const_iterator
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.
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.
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
wxString GetFullRef() const
void SetValue(const wxString &aValue)
int CompareRef(const SCH_REFERENCE &item) const
void SetRef(const wxString &aReference)
const wxString GetFootprint() const
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 operator<(const SCH_REFERENCE &aRef) const
void SetUnit(int aUnit)
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) )
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.
LIB_SYMBOL * GetLibPart() const
const wxString GetValue() 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.
int GetUnit() const
bool IsMultiUnit() const
LIB_SYMBOL * m_libPart
The source symbol from a library.
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.
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.
wxString PathAsString() const
Return the path of time stamps which do not changes even when editing sheet parameters.
Schematic symbol object.
Definition: sch_symbol.h:109
wxString SubReference(int aUnit, bool aAddSeparator=true) const
Definition: sch_symbol.cpp:870
const LIB_ID & GetLibId() const
Definition: sch_symbol.h:203
int compare(const std::string &s) const
Definition: utf8.h:111
ERCE_T
ERC error codes.
Definition: erc_settings.h:37
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.