KiCad PCB EDA Suite
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 <jean-pierre.charras@gipsa-lab.inpg.fr>
5  * Copyright (C) 1992-2011 Wayne Stambaugh <stambaughw@verizon.net>
6  * Copyright (C) 1992-2020 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 
30 #include <macros.h>
31 
32 #include <class_libentry.h>
33 #include <sch_sheet_path.h>
34 #include <sch_component.h>
35 #include <sch_text.h>
36 
37 #include <map>
38 
48 {
50  UTF8 m_Ref; // it's private, use the accessors please
54  int m_Unit;
58  bool m_IsNew;
59  int m_SheetNum;
62  int m_NumRef;
64  int m_Flag;
65 
66  friend class SCH_REFERENCE_LIST;
67 
68 public:
69 
71  m_SheetPath()
72  {
73  m_RootCmp = NULL;
74  m_Entry = NULL;
75  m_Unit = 0;
76  m_IsNew = false;
77  m_Value = NULL;
78  m_NumRef = 0;
79  m_Flag = 0;
80  m_SheetNum = 0;
81  }
82 
83  SCH_REFERENCE( SCH_COMPONENT* aComponent, LIB_PART* aLibComponent,
84  const SCH_SHEET_PATH& aSheetPath );
85 
86  SCH_COMPONENT* GetComp() const { return m_RootCmp; }
87 
88  LIB_PART* GetLibPart() const { return m_Entry; }
89 
90  const SCH_SHEET_PATH& GetSheetPath() const { return m_SheetPath; }
91 
93 
94  int GetUnit() const { return m_Unit; }
95 
96  void SetUnit( int aUnit ) { m_Unit = aUnit; }
97 
98  void SetSheetNumber( int aSheetNumber ) { m_SheetNum = aSheetNumber; }
99 
100  const wxString GetPath() const
101  {
103  }
104 
109  void Annotate();
110 
118  void Split();
119 
120  /* Some accessors which hide the strategy of how the reference is stored,
121  thereby making it easy to change that strategy.
122  */
123 
124  void SetRef( const wxString& aReference )
125  {
126  m_Ref = aReference;
127  }
128 
129  wxString GetRef() const
130  {
131  return m_Ref;
132  }
133 
134  void SetRefStr( const std::string& aReference )
135  {
136  m_Ref = aReference;
137  }
138 
139  const char* GetRefStr() const
140  {
141  return m_Ref.c_str();
142  }
143 
145  wxString GetFullRef()
146  {
147  if( GetComp()->GetUnitCount() > 1 )
148  return GetRef() + LIB_PART::SubReference( GetUnit() );
149  else
150  return GetRef();
151  }
152 
153  wxString GetRefNumber() const
154  {
155  wxString ref;
156 
157  if( m_NumRef < 0 )
158  return wxT( "?" );
159 
160  // To avoid a risk of duplicate, for power components
161  // the ref number is 0nnn instead of nnn.
162  // Just because sometimes only power components are annotated
163  if( GetLibPart() && GetLibPart()->IsPower() )
164  ref = wxT( "0" );
165 
166  return ref << m_NumRef;
167  }
168 
169  int CompareValue( const SCH_REFERENCE& item ) const
170  {
171  return m_Value->GetText().Cmp( item.m_Value->GetText() );
172  }
173 
174  int CompareRef( const SCH_REFERENCE& item ) const
175  {
176  return m_Ref.compare( item.m_Ref );
177  }
178 
179  int CompareLibName( const SCH_REFERENCE& item ) const
180  {
182  item.m_RootCmp->GetLibId().GetLibItemName() );
183  }
184 
190  bool IsSameInstance( const SCH_REFERENCE& other ) const
191  {
192  // JEY TODO: should this be checking unit as well?
193  return GetComp() == other.GetComp()
194  && GetSheetPath().Path() == other.GetSheetPath().Path();
195  }
196 
198  {
199  return m_Entry->UnitsLocked();
200  }
201 };
202 
203 
212 {
213 private:
214  std::vector <SCH_REFERENCE> flatList;
215 
216 public:
220  {
221  }
222 
223  SCH_REFERENCE& operator[]( int aIndex )
224  {
225  return flatList[ aIndex ];
226  }
227 
232  unsigned GetCount() const
233  {
234  return flatList.size();
235  }
236 
241  SCH_REFERENCE& GetItem( int aIdx )
242  {
243  return flatList[aIdx];
244  }
245 
251  void AddItem( SCH_REFERENCE& aItem )
252  {
253  flatList.push_back( aItem );
254  }
255 
262  void RemoveItem( unsigned int aIndex );
263 
264  /* Sort functions:
265  * Sort functions are used to sort components for annotation or BOM generation.
266  * Because sorting depends on what we want to do, there are many sort functions.
267  * Note:
268  * When creating BOM, components are fully annotated.
269  * references are something like U3, U5 or R4, R8
270  * When annotating, some or all components are not annotated,
271  * i.e. ref is only U or R, with no number.
272  */
273 
283  {
284  for( unsigned ii = 0; ii < GetCount(); ii++ )
285  flatList[ii].Split();
286  }
287 
296  {
297  /* update the reference numbers */
298  for( unsigned ii = 0; ii < GetCount(); ii++ )
299  {
300  flatList[ii].Annotate();
301  }
302  }
303 
321  void Annotate( bool aUseSheetNum, int aSheetIntervalId, int aStartNumber,
322  SCH_MULTI_UNIT_REFERENCE_MAP aLockedUnitMap );
323 
339  int CheckAnnotation( REPORTER& aReporter );
340 
356  {
357  sort( flatList.begin(), flatList.end(), sortByXPosition );
358  }
359 
375  {
376  sort( flatList.begin(), flatList.end(), sortByYPosition );
377  }
378 
385  {
386  sort( flatList.begin(), flatList.end(), sortByTimeStamp );
387  }
388 
405  {
406  sort( flatList.begin(), flatList.end(), sortByRefAndValue );
407  }
408 
421  {
422  sort( flatList.begin(), flatList.end(), sortByReferenceOnly );
423  }
424 
433  int FindUnit( size_t aIndex, int aUnit );
434 
440  int FindRefByPath( const wxString& aPath ) const;
441 
450  void GetRefsInUse( int aIndex, std::vector< int >& aIdList, int aMinRefId );
451 
461  int GetLastReference( int aIndex, int aMinValue );
462 
463 #if defined(DEBUG)
464  void Show( const char* aPrefix = "" )
465  {
466  printf( "%s\n", aPrefix );
467 
468  for( unsigned i=0; i < flatList.size(); ++i )
469  {
470  SCH_REFERENCE& schref = flatList[i];
471 
472  printf( " [%-2d] ref:%-8s num:%-3d lib_part:%s\n",
473  i,
474  schref.m_Ref.c_str(),
475  schref.m_NumRef,
476  TO_UTF8( schref.GetLibPart()->GetName() )
477  );
478  }
479  }
480 #endif
481 
488  static wxString Shorthand( std::vector<SCH_REFERENCE> aList );
489 
490  friend class BACK_ANNOTATION;
491 
492 private:
493  /* sort functions used to sort flatList
494  */
495 
496  static bool sortByRefAndValue( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
497 
498  static bool sortByXPosition( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
499 
500  static bool sortByYPosition( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
501 
502  static bool sortByTimeStamp( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
503 
504  static bool sortByReferenceOnly( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
505 
517  int CreateFirstFreeRefId( std::vector<int>& aIdList, int aFirstValue );
518 
519  // Used for sorting static sortByTimeStamp function
520  friend class BACK_ANNOTATE;
521 };
522 
523 #endif // _SCH_REFERENCE_LIST_H_
UTF8 is an 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to...
Definition: utf8.h:73
void SetSheetNumber(int aSheetNumber)
wxPoint m_CmpPos
The physical position of the component in schematic used to annotate by X or Y position.
static bool sortByTimeStamp(const SCH_REFERENCE &item1, const SCH_REFERENCE &item2)
void SortByYCoordinate()
Function sortByYCoordinate sorts the list of references by Y position.
SCH_REFERENCE & GetItem(int aIdx)
Function GetItem.
int compare(const std::string &s) const
Definition: utf8.h:116
const UTF8 & GetLibItemName() const
Definition: lib_id.h:114
KIID m_Uuid
UUID of the component.
void RemoveItem(unsigned int aIndex)
Function RemoveItem removes an item from the list of references.
wxString GetName() const override
void Split()
Function Split attempts to split the reference designator into a name (U) and number (1).
int FindRefByPath(const wxString &aPath) const
Searches unit with designated path.
const SCH_SHEET_PATH & GetSheetPath() const
void SetUnit(int aUnit)
UTF8 m_Ref
Component reference prefix, without number (for IC1, this is IC) )
int m_NumRef
The numeric part of the reference designator.
KIID_PATH Path() const
Get the sheet path as an KIID_PATH.
SCH_COMPONENT * GetComp() const
void AddItem(SCH_REFERENCE &aItem)
Function AddItem adds a SCH_REFERENCE object to the list of references.
wxString AsString() const
Definition: common.cpp:165
wxString PathAsString() const
Function PathAsString the path uses the time stamps which do not changes even when editing sheet para...
static wxString Shorthand(std::vector< SCH_REFERENCE > aList)
Function Shorthand Returns a shorthand string representing all the references in the list.
int GetUnit() const
const wxString GetPath() const
wxString GetRefNumber() const
int m_SheetNum
The sheet number for the reference.
REPORTER is a pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:64
void SortByRefAndValue()
Function SortByRefAndValue sorts the list of references by value.
SCH_REFERENCE_LIST is used to create a flattened list of components because in a complex hierarchy,...
This file contains miscellaneous commonly used macros and functions.
void SetRefStr(const std::string &aReference)
const char * c_str() const
Definition: utf8.h:107
void GetRefsInUse(int aIndex, std::vector< int > &aIdList, int aMinRefId)
Function GetRefsInUse adds all the reference designator numbers greater than aMinRefId to aIdList ski...
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:112
int m_Unit
The unit number for components with multiple parts per package.
wxString GetRef() const
Definition: common.h:68
void UpdateAnnotation()
function UpdateAnnotation Updates the reference components for the schematic project (or the current ...
void SortByTimeStamp()
Function SortComponentsByTimeStamp sort the flat list by Time Stamp (sheet path + timestamp).
friend class BACK_ANNOTATION
#define NULL
void SortByXCoordinate()
Function sortByXCoordinate sorts the list of references by X position.
int CompareValue(const SCH_REFERENCE &item) const
SCH_COMPONENT * m_RootCmp
The component associated the reference object.
EDA_TEXT * m_Value
The component value of the reference.
int CompareLibName(const SCH_REFERENCE &item) const
static bool sortByRefAndValue(const SCH_REFERENCE &item1, const SCH_REFERENCE &item2)
Class LIB_PIN definition.
LIB_PART * m_Entry
The source component from a library.
void Annotate()
Function Annotate updates the annotation of the component according the the current object state.
Define a library symbol object.
void Annotate(bool aUseSheetNum, int aSheetIntervalId, int aStartNumber, SCH_MULTI_UNIT_REFERENCE_MAP aLockedUnitMap)
Function Annotate set the reference designators in the list that have not been annotated.
int FindUnit(size_t aIndex, int aUnit)
searches the sorted list of components for a another component with the same reference and a given pa...
SCH_SHEET_PATH & GetSheetPath()
static bool sortByYPosition(const SCH_REFERENCE &item1, const SCH_REFERENCE &item2)
SCH_SHEET_PATH.
static bool sortByReferenceOnly(const SCH_REFERENCE &item1, const SCH_REFERENCE &item2)
std::map< wxString, SCH_REFERENCE_LIST > SCH_MULTI_UNIT_REFERENCE_MAP
Type SCH_MULTI_UNIT_REFERENCE_MAP is used to create a map of reference designators for multi-unit par...
const KIID m_Uuid
Definition: base_struct.h:162
unsigned GetCount() const
Function GetCount.
SCH_SHEET_PATH m_SheetPath
The sheet path for this reference.
wxString GetFullRef()
Return reference name with unit altogether
int CompareRef(const SCH_REFERENCE &item) const
SCH_REFERENCE & operator[](int aIndex)
const char * GetRefStr() const
void SortByReferenceOnly()
Function SortByReferenceOnly sorts the list of references by reference.
void SplitReferences()
Function SplitReferences attempts to split all reference designators into a name (U) and number (1).
Back annotation algorithm class.
Definition: backannotate.h:58
LIB_PART * GetLibPart() const
bool IsSameInstance(const SCH_REFERENCE &other) const
Function IsSameInstance returns whether this reference refers to the same component instance (compone...
static wxString SubReference(int aUnit, bool aAddSeparator=true)
Schematic symbol object.
Definition: sch_component.h:88
#define TO_UTF8(wxstring)
int CreateFirstFreeRefId(std::vector< int > &aIdList, int aFirstValue)
Function CreateFirstFreeRefId searches for the first free reference number in aListId of reference nu...
void SetRef(const wxString &aReference)
std::vector< SCH_REFERENCE > flatList
int GetLastReference(int aIndex, int aMinValue)
Function GetLastReference returns the last used (greatest) reference number in the reference list for...
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
int CheckAnnotation(REPORTER &aReporter)
Function CheckAnnotation check for annotations errors.
SCH_REFERENCE_LIST()
Constructor.
const LIB_ID & GetLibId() const
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:126
bool UnitsLocked() const
Check whether part units are interchangeable.
SCH_REFERENCE is used as a helper to define a component's reference designator in a schematic.
bool m_IsNew
True if not yet annotated.
static bool sortByXPosition(const SCH_REFERENCE &item1, const SCH_REFERENCE &item2)