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, 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/erc_settings.h>
37
38class REFDES_TRACKER;
39
42{
46};
47
48
51{
54 UNSORTED,
56};
57
58
61{
67};
68
69
78{
79public:
82 {
83 m_rootSymbol = 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, const SCH_SHEET_PATH& aSheetPath );
92
93 SCH_SYMBOL* GetSymbol() const { return m_rootSymbol; }
94
95 LIB_SYMBOL* GetLibPart() const { return m_rootSymbol->GetLibSymbolRef().get(); }
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
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( bool aIncludeUnit = true ) const
171 {
172 wxString refNum = m_numRefStr;
173
174 if( refNum.IsEmpty() )
175 refNum << m_numRef;
176
177 if( aIncludeUnit && GetSymbol()->GetUnitCount() > 1 )
178 return GetRef() + refNum + GetSymbol()->SubReference( GetUnit() );
179 else
180 return GetRef() + refNum;
181 }
182
183 wxString GetRefNumber() const
184 {
185 if( m_numRef < 0 )
186 return wxT( "?" );
187 else
188 return m_numRefStr;
189 }
190
191 int CompareValue( const SCH_REFERENCE& item ) const
192 {
193 return m_value.Cmp( item.m_value );
194 }
195
196 int CompareRef( const SCH_REFERENCE& item ) const
197 {
198 return m_ref.CmpNoCase( item.m_ref );
199 }
200
201 int CompareLibName( const SCH_REFERENCE& item ) const
202 {
205 }
206
211 bool IsSameInstance( const SCH_REFERENCE& other ) const
212 {
213 // Only compare symbol and path.
214 // We may have changed the unit number or the designator but
215 // can still be referencing the same instance.
216 return GetSymbol() == other.GetSymbol()
217 && GetSheetPath().Path() == other.GetSheetPath().Path();
218 }
219
221 {
222 if( GetLibPart() )
223 return GetLibPart()->UnitsLocked();
224 else
225 return true; // Assume units locked when we don't have a library
226 }
227
228 void SetRefNum( int aNum )
229 {
230 m_numRef = aNum;
231 m_numRefStr = formatRefStr( aNum );
232 }
233
234private:
235 wxString formatRefStr( int aNumber ) const;
236
237private:
238 friend class SCH_REFERENCE_LIST;
239
241 wxString m_ref; // it's private, use the accessors please
245 int m_unit;
247 wxString m_value;
248 wxString m_footprint;
250 bool m_isNew;
254 wxString m_numRefStr;
257};
258
259
263typedef std::function<void( ERCE_T aType, const wxString& aMsg, SCH_REFERENCE* aItemA,
265
266
275{
276public:
278 {}
279
281 {
282 return m_flatList[ aIndex ];
283 }
284
285 const SCH_REFERENCE& operator[]( int aIndex ) const
286 {
287 return m_flatList[ aIndex ];
288 }
289
290 void Clear()
291 {
292 m_flatList.clear();
293 }
294
295 size_t GetCount() const { return m_flatList.size(); }
296
297 SCH_REFERENCE& GetItem( size_t aIdx ) { return m_flatList[aIdx]; }
298 const SCH_REFERENCE& GetItem( size_t aIdx ) const { return m_flatList[aIdx]; }
299
300 void AddItem( const SCH_REFERENCE& aItem ) { m_flatList.push_back( aItem ); }
301
307 void RemoveItem( unsigned int aIndex );
308
315 bool Contains( const SCH_REFERENCE& aItem ) const;
316
317 /* Sort functions:
318 * Sort functions are used to sort symbols for annotation or BOM generation. Because
319 * sorting depends on what we want to do, there are many sort functions.
320 * Note:
321 * When creating BOM, symbols are fully annotated. References are something like U3,
322 * U5 or R4, R8. When annotating, some or all symbols are not annotated, i.e. ref is
323 * only U or R, with no number.
324 */
325
334 {
335 for( unsigned ii = 0; ii < GetCount(); ii++ )
336 m_flatList[ii].Split();
337 }
338
346 {
347 for( unsigned ii = 0; ii < GetCount(); ii++ )
348 m_flatList[ii].m_isNew = true;
349 }
350
360 {
361 /* update the reference numbers */
362 for( unsigned ii = 0; ii < GetCount(); ii++ )
363 m_flatList[ii].Annotate();
364 }
365
380 void ReannotateByOptions( ANNOTATE_ORDER_T aSortOption,
381 ANNOTATE_ALGO_T aAlgoOption,
382 int aStartNumber,
383 const SCH_REFERENCE_LIST& aAdditionalRefs,
384 bool aStartAtCurrent,
385 SCH_SHEET_LIST* aHierarchy );
386
397 void ReannotateDuplicates( const SCH_REFERENCE_LIST& aAdditionalReferences );
398
414 void AnnotateByOptions( enum ANNOTATE_ORDER_T aSortOption,
415 enum ANNOTATE_ALGO_T aAlgoOption,
416 int aStartNumber,
417 const SCH_MULTI_UNIT_REFERENCE_MAP& aLockedUnitMap,
418 const SCH_REFERENCE_LIST& aAdditionalRefs,
419 bool aStartAtCurrent );
420
442 void Annotate( bool aUseSheetNum, int aSheetIntervalId, int aStartNumber,
443 const SCH_MULTI_UNIT_REFERENCE_MAP& aLockedUnitMap,
444 const SCH_REFERENCE_LIST& aAdditionalRefs,
445 bool aStartAtCurrent = false );
446
459 int CheckAnnotation( ANNOTATION_ERROR_HANDLER aErrorHandler );
460
472 {
473 sort( m_flatList.begin(), m_flatList.end(), sortByXPosition );
474 }
475
487 {
488 sort( m_flatList.begin(), m_flatList.end(), sortByYPosition );
489 }
490
497 {
498 sort( m_flatList.begin(), m_flatList.end(), sortByTimeStamp );
499 }
500
513 {
514 sort( m_flatList.begin(), m_flatList.end(), sortByRefAndValue );
515 }
516
525 {
526 sort( m_flatList.begin(), m_flatList.end(), sortByReferenceOnly );
527 }
528
532 int FindRef( const wxString& aPath ) const;
533
540 int FindRefByFullPath( const wxString& aFullPath ) const;
541
550 void GetRefsInUse( int aIndex, std::vector<int>& aIdList, int aMinRefId ) const;
551
558 std::vector<int> GetUnitsMatchingRef( const SCH_REFERENCE& aRef ) const;
559
568 int FindFirstUnusedReference( const SCH_REFERENCE& aRef, int aMinValue,
569 const std::vector<int>& aRequiredUnits ) const;
570
571 std::vector<SCH_SYMBOL_INSTANCE> GetSymbolInstances() const;
572
573#if defined(DEBUG)
574 void Show( const char* aPrefix = "" );
575#endif
576
582 static wxString Shorthand( std::vector<SCH_REFERENCE> aList, const wxString& refDelimiter,
583 const wxString& refRangeDelimiter );
584
585 std::shared_ptr<REFDES_TRACKER> GetRefDesTracker() const
586 {
587 return m_refDesTracker;
588 }
589
590 void SetRefDesTracker( std::shared_ptr<REFDES_TRACKER> aTracker )
591 {
592 m_refDesTracker = aTracker;
593 }
594
595 friend class BACK_ANNOTATION;
596
597 typedef std::vector<SCH_REFERENCE>::iterator iterator;
598 typedef std::vector<SCH_REFERENCE>::const_iterator const_iterator;
599
600 iterator begin() { return m_flatList.begin(); }
601
602 iterator end() { return m_flatList.end(); }
603
604 const_iterator begin() const { return m_flatList.begin(); }
605
606 const_iterator end() const { return m_flatList.end(); }
607
608 iterator erase( iterator position ) { return m_flatList.erase( position ); }
609
610 iterator erase( iterator first, iterator last ) { return m_flatList.erase( first, last ); }
611
612private:
613 static bool sortByRefAndValue( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
614
615 static bool sortByXPosition( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
616
617 static bool sortByYPosition( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
618
619 static bool sortByTimeStamp( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
620
621 static bool sortByReferenceOnly( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
622
623 // Used for sorting static sortByTimeStamp function
624 friend class BACK_ANNOTATE;
625
626 std::vector<SCH_REFERENCE> m_flatList;
627
628 std::shared_ptr<REFDES_TRACKER> m_refDesTracker;
629};
630
631#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:246
const UTF8 & GetLibItemName() const
Definition: lib_id.h:102
Define a library symbol object.
Definition: lib_symbol.h:85
bool UnitsLocked() const
Check whether symbol units are interchangeable.
Definition: lib_symbol.h:289
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 & 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)
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 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
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
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
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.
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.
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
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
Compare by full path to make std::set work.
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) )
void SetRefNum(int aNum)
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
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.
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.
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:75
wxString SubReference(int aUnit, bool aAddSeparator=true) const
Definition: sch_symbol.cpp:677
const LIB_ID & GetLibId() const override
Definition: sch_symbol.h:164
std::unique_ptr< LIB_SYMBOL > & GetLibSymbolRef()
Definition: sch_symbol.h:183
int compare(const std::string &s) const
Definition: utf8.h:118
ERCE_T
ERC error codes.
Definition: erc_settings.h:36
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.