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/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_unit = 0;
84 m_isNew = false;
85 m_numRef = 0;
86 m_flag = 0;
87 m_sheetNum = 0;
88 }
89
90 SCH_REFERENCE( SCH_SYMBOL* aSymbol, const SCH_SHEET_PATH& aSheetPath );
91
92 SCH_SYMBOL* GetSymbol() const { return m_rootSymbol; }
93
94 LIB_SYMBOL* GetLibPart() const { return m_rootSymbol->GetLibSymbolRef().get(); }
95
96 const SCH_SHEET_PATH& GetSheetPath() const { return m_sheetPath; }
97
99
100 int GetUnit() const { return m_unit; }
101 void SetUnit( int aUnit ) { m_unit = aUnit; }
102 bool IsMultiUnit() const { return GetLibPart()->GetUnitCount() > 1; }
103
104 const wxString GetValue() const { return m_value; }
105 void SetValue( const wxString& aValue ) { m_value = aValue; }
106
107 const wxString GetFootprint() const { return m_footprint; }
108 void SetFootprint( const wxString& aFP ) { m_footprint = aFP; }
109
110 void SetSheetNumber( int aSheetNumber ) { m_sheetNum = aSheetNumber; }
111
115 const wxString GetPath() const
116 {
117 return m_sheetPath.PathAsString();
118 }
119
123 const wxString GetFullPath() const
124 {
126 }
127
128 /*
129 * Compares by full path to make std::set work
130 */
131 bool operator<( const SCH_REFERENCE& aRef ) const { return GetFullPath() < aRef.GetFullPath(); }
132
136 void Annotate();
137
144 bool AlwaysAnnotate() const;
145
153 void Split();
154
160 bool IsSplitNeeded();
161
162 void SetRef( const wxString& aReference ) { m_ref = aReference; }
163 wxString GetRef() const { return m_ref; }
164
165 void SetRefStr( const std::string& aReference ) { m_ref = aReference; }
166 const char* GetRefStr() const { return m_ref.c_str(); }
167
169 wxString GetFullRef() const
170 {
171 wxString refNum = m_numRefStr;
172
173 if( refNum.IsEmpty() )
174 refNum << m_numRef;
175
176 if( GetSymbol()->GetUnitCount() > 1 )
177 return GetRef() + refNum + GetSymbol()->SubReference( GetUnit() );
178 else
179 return GetRef() + refNum;
180 }
181
182 wxString GetRefNumber() const
183 {
184 wxString ref;
185
186 if( m_numRef < 0 )
187 return wxT( "?" );
188
189 // To avoid a risk of duplicate, for power symbols the ref number is 0nnn instead of nnn.
190 // Just because sometimes only power symbols are annotated
191 if( GetSymbol() && GetLibPart() && GetLibPart()->IsPower() )
192 ref = wxT( "0" );
193
194 return ref << m_numRef;
195 }
196
197 int CompareValue( const SCH_REFERENCE& item ) const
198 {
199 return m_value.Cmp( item.m_value );
200 }
201
202 int CompareRef( const SCH_REFERENCE& item ) const
203 {
204 return m_ref.CmpNoCase( item.m_ref );
205 }
206
207 int CompareLibName( const SCH_REFERENCE& item ) const
208 {
211 }
212
217 bool IsSameInstance( const SCH_REFERENCE& other ) const
218 {
219 // Only compare symbol and path.
220 // We may have changed the unit number or the designator but
221 // can still be referencing the same instance.
222 return GetSymbol() == other.GetSymbol()
223 && GetSheetPath().Path() == other.GetSheetPath().Path();
224 }
225
227 {
228 if( GetLibPart() )
229 return GetLibPart()->UnitsLocked();
230 else
231 return true; // Assume units locked when we don't have a library
232 }
233
234private:
235 friend class SCH_REFERENCE_LIST;
236
238 wxString m_ref; // it's private, use the accessors please
242 int m_unit;
244 wxString m_value;
245 wxString m_footprint;
247 bool m_isNew;
251 wxString m_numRefStr;
253};
254
255
259typedef std::function<void( ERCE_T aType, const wxString& aMsg, SCH_REFERENCE* aItemA,
261
262
271{
272public:
274 {
275 }
276
278 {
279 return m_flatList[ aIndex ];
280 }
281
282 const SCH_REFERENCE& operator[]( int aIndex ) const
283 {
284 return m_flatList[ aIndex ];
285 }
286
287 void Clear()
288 {
289 m_flatList.clear();
290 }
291
292 size_t GetCount() const { return m_flatList.size(); }
293
294 SCH_REFERENCE& GetItem( size_t aIdx ) { return m_flatList[aIdx]; }
295 const SCH_REFERENCE& GetItem( size_t aIdx ) const { return m_flatList[aIdx]; }
296
297 void AddItem( const SCH_REFERENCE& aItem ) { m_flatList.push_back( aItem ); }
298
304 void RemoveItem( unsigned int aIndex );
305
311 bool Contains( const SCH_REFERENCE& aItem ) const;
312
313 /* Sort functions:
314 * Sort functions are used to sort symbols for annotation or BOM generation. Because
315 * sorting depends on what we want to do, there are many sort functions.
316 * Note:
317 * When creating BOM, symbols are fully annotated. References are something like U3,
318 * U5 or R4, R8. When annotating, some or all symbols are not annotated, i.e. ref is
319 * only U or R, with no number.
320 */
321
330 {
331 for( unsigned ii = 0; ii < GetCount(); ii++ )
332 m_flatList[ii].Split();
333 }
334
341 {
342 for( unsigned ii = 0; ii < GetCount(); ii++ )
343 m_flatList[ii].m_isNew = true;
344 }
345
355 {
356 /* update the reference numbers */
357 for( unsigned ii = 0; ii < GetCount(); ii++ )
358 m_flatList[ii].Annotate();
359 }
360
375 void ReannotateByOptions( ANNOTATE_ORDER_T aSortOption,
376 ANNOTATE_ALGO_T aAlgoOption,
377 int aStartNumber,
378 const SCH_REFERENCE_LIST& aAdditionalRefs,
379 bool aStartAtCurrent,
380 SCH_SHEET_LIST* aHierarchy );
381
391 void ReannotateDuplicates( const SCH_REFERENCE_LIST& aAdditionalReferences );
392
408 void AnnotateByOptions( enum ANNOTATE_ORDER_T aSortOption,
409 enum ANNOTATE_ALGO_T aAlgoOption,
410 int aStartNumber,
411 SCH_MULTI_UNIT_REFERENCE_MAP aLockedUnitMap,
412 const SCH_REFERENCE_LIST& aAdditionalRefs,
413 bool aStartAtCurrent );
414
436 void Annotate( bool aUseSheetNum, int aSheetIntervalId, int aStartNumber,
437 SCH_MULTI_UNIT_REFERENCE_MAP aLockedUnitMap,
438 const SCH_REFERENCE_LIST& aAdditionalRefs, bool aStartAtCurrent = false );
439
452 int CheckAnnotation( ANNOTATION_ERROR_HANDLER aErrorHandler );
453
465 {
466 sort( m_flatList.begin(), m_flatList.end(), sortByXPosition );
467 }
468
480 {
481 sort( m_flatList.begin(), m_flatList.end(), sortByYPosition );
482 }
483
490 {
491 sort( m_flatList.begin(), m_flatList.end(), sortByTimeStamp );
492 }
493
506 {
507 sort( m_flatList.begin(), m_flatList.end(), sortByRefAndValue );
508 }
509
518 {
519 sort( m_flatList.begin(), m_flatList.end(), sortByReferenceOnly );
520 }
521
525 int FindRef( const wxString& aPath ) const;
526
533 int FindRefByFullPath( const wxString& aFullPath ) const;
534
543 void GetRefsInUse( int aIndex, std::vector<int>& aIdList, int aMinRefId ) const;
544
551 std::vector<int> GetUnitsMatchingRef( const SCH_REFERENCE& aRef ) const;
552
561 int FindFirstUnusedReference( const SCH_REFERENCE& aRef, int aMinValue,
562 const std::vector<int>& aRequiredUnits ) const;
563
564 std::vector<SCH_SYMBOL_INSTANCE> GetSymbolInstances() const;
565
566#if defined(DEBUG)
567 void Show( const char* aPrefix = "" );
568#endif
569
575 static wxString Shorthand( std::vector<SCH_REFERENCE> aList, const wxString& refDelimiter,
576 const wxString& refRangeDelimiter );
577
578 friend class BACK_ANNOTATION;
579
580 typedef std::vector<SCH_REFERENCE>::iterator iterator;
581 typedef std::vector<SCH_REFERENCE>::const_iterator const_iterator;
582
583 iterator begin() { return m_flatList.begin(); }
584
585 iterator end() { return m_flatList.end(); }
586
587 const_iterator begin() const { return m_flatList.begin(); }
588
589 const_iterator end() const { return m_flatList.end(); }
590
591 iterator erase( iterator position ) { return m_flatList.erase( position ); }
592
593 iterator erase( iterator first, iterator last ) { return m_flatList.erase( first, last ); }
594
595private:
596 static bool sortByRefAndValue( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
597
598 static bool sortByXPosition( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
599
600 static bool sortByYPosition( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
601
602 static bool sortByTimeStamp( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
603
604 static bool sortByReferenceOnly( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
605
618 static int createFirstFreeRefId( std::vector<int>& aIdList, int aFirstValue );
619
620 // Used for sorting static sortByTimeStamp function
621 friend class BACK_ANNOTATE;
622
623 std::vector<SCH_REFERENCE> m_flatList;
624};
625
626#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:238
const UTF8 & GetLibItemName() const
Definition: lib_id.h:102
Define a library symbol object.
Definition: lib_symbol.h:78
bool UnitsLocked() const
Check whether symbol units are interchangeable.
Definition: lib_symbol.h:264
int GetUnitCount() const override
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 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
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).
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
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
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:104
wxString SubReference(int aUnit, bool aAddSeparator=true) const
Definition: sch_symbol.cpp:856
const LIB_ID & GetLibId() const override
Definition: sch_symbol.h:193
std::unique_ptr< LIB_SYMBOL > & GetLibSymbolRef()
Definition: sch_symbol.h:212
int compare(const std::string &s) const
Definition: utf8.h:112
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.