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;
56  wxString m_Value;
58  wxString m_Footprint;
60  bool m_IsNew;
61  int m_SheetNum;
63  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_NumRef = 0;
78  m_Flag = 0;
79  m_SheetNum = 0;
80  }
81 
82  SCH_REFERENCE( SCH_COMPONENT* aComponent, LIB_PART* aLibComponent,
83  const SCH_SHEET_PATH& aSheetPath );
84 
85  SCH_COMPONENT* GetComp() const { return m_RootCmp; }
86 
87  LIB_PART* GetLibPart() const { return m_Entry; }
88 
89  const SCH_SHEET_PATH& GetSheetPath() const { return m_SheetPath; }
90 
92 
93  int GetUnit() const { return m_Unit; }
94  void SetUnit( int aUnit ) { m_Unit = aUnit; }
95 
96  const wxString GetValue() const { return m_Value; }
97  void SetValue( const wxString& aValue ) { m_Value = aValue; }
98 
99  const wxString GetFootprint() const { return m_Footprint; }
100  void SetFootprint( const wxString& aFP ) { m_Footprint = aFP; }
101 
102  void SetSheetNumber( int aSheetNumber ) { m_SheetNum = aSheetNumber; }
103 
104  const wxString GetPath() const
105  {
107  }
108 
113  void Annotate();
114 
122  void Split();
123 
124  /* Some accessors which hide the strategy of how the reference is stored,
125  thereby making it easy to change that strategy.
126  */
127 
128  void SetRef( const wxString& aReference )
129  {
130  m_Ref = aReference;
131  }
132 
133  wxString GetRef() const
134  {
135  return m_Ref;
136  }
137 
138  void SetRefStr( const std::string& aReference )
139  {
140  m_Ref = aReference;
141  }
142 
143  const char* GetRefStr() const
144  {
145  return m_Ref.c_str();
146  }
147 
149  wxString GetFullRef()
150  {
151  if( GetComp()->GetUnitCount() > 1 )
152  return GetRef() + LIB_PART::SubReference( GetUnit() );
153  else
154  return GetRef();
155  }
156 
157  wxString GetRefNumber() const
158  {
159  wxString ref;
160 
161  if( m_NumRef < 0 )
162  return wxT( "?" );
163 
164  // To avoid a risk of duplicate, for power components
165  // the ref number is 0nnn instead of nnn.
166  // Just because sometimes only power components are annotated
167  if( GetLibPart() && GetLibPart()->IsPower() )
168  ref = wxT( "0" );
169 
170  return ref << m_NumRef;
171  }
172 
173  int CompareValue( const SCH_REFERENCE& item ) const
174  {
175  return m_Value.Cmp( item.m_Value );
176  }
177 
178  int CompareRef( const SCH_REFERENCE& item ) const
179  {
180  return m_Ref.compare( item.m_Ref );
181  }
182 
183  int CompareLibName( const SCH_REFERENCE& item ) const
184  {
186  item.m_RootCmp->GetLibId().GetLibItemName() );
187  }
188 
194  bool IsSameInstance( const SCH_REFERENCE& other ) const
195  {
196  // JEY TODO: should this be checking unit as well?
197  return GetComp() == other.GetComp()
198  && GetSheetPath().Path() == other.GetSheetPath().Path();
199  }
200 
202  {
203  return m_Entry->UnitsLocked();
204  }
205 };
206 
207 
216 {
217 private:
218  std::vector<SCH_REFERENCE> flatList;
219 
220 public:
224  {
225  }
226 
227  SCH_REFERENCE& operator[]( int aIndex )
228  {
229  return flatList[ aIndex ];
230  }
231 
232  void Clear()
233  {
234  flatList.clear();
235  }
236 
241  unsigned GetCount() const
242  {
243  return flatList.size();
244  }
245 
250  SCH_REFERENCE& GetItem( int aIdx )
251  {
252  return flatList[aIdx];
253  }
254 
260  void AddItem( SCH_REFERENCE& aItem )
261  {
262  flatList.push_back( aItem );
263  }
264 
271  void RemoveItem( unsigned int aIndex );
272 
273  /* Sort functions:
274  * Sort functions are used to sort components for annotation or BOM generation.
275  * Because sorting depends on what we want to do, there are many sort functions.
276  * Note:
277  * When creating BOM, components are fully annotated.
278  * references are something like U3, U5 or R4, R8
279  * When annotating, some or all components are not annotated,
280  * i.e. ref is only U or R, with no number.
281  */
282 
292  {
293  for( unsigned ii = 0; ii < GetCount(); ii++ )
294  flatList[ii].Split();
295  }
296 
305  {
306  /* update the reference numbers */
307  for( unsigned ii = 0; ii < GetCount(); ii++ )
308  {
309  flatList[ii].Annotate();
310  }
311  }
312 
330  void Annotate( bool aUseSheetNum, int aSheetIntervalId, int aStartNumber,
331  SCH_MULTI_UNIT_REFERENCE_MAP aLockedUnitMap );
332 
348  int CheckAnnotation( REPORTER& aReporter );
349 
365  {
366  sort( flatList.begin(), flatList.end(), sortByXPosition );
367  }
368 
384  {
385  sort( flatList.begin(), flatList.end(), sortByYPosition );
386  }
387 
394  {
395  sort( flatList.begin(), flatList.end(), sortByTimeStamp );
396  }
397 
414  {
415  sort( flatList.begin(), flatList.end(), sortByRefAndValue );
416  }
417 
430  {
431  sort( flatList.begin(), flatList.end(), sortByReferenceOnly );
432  }
433 
439  int FindRef( const wxString& aPath ) const;
440 
449  int FindUnit( size_t aIndex, int aUnit );
450 
456  int FindRefByPath( const wxString& aPath ) const;
457 
466  void GetRefsInUse( int aIndex, std::vector< int >& aIdList, int aMinRefId );
467 
477  int GetLastReference( int aIndex, int aMinValue );
478 
479 #if defined(DEBUG)
480  void Show( const char* aPrefix = "" )
481  {
482  printf( "%s\n", aPrefix );
483 
484  for( unsigned i=0; i < flatList.size(); ++i )
485  {
486  SCH_REFERENCE& schref = flatList[i];
487 
488  printf( " [%-2d] ref:%-8s num:%-3d lib_part:%s\n",
489  i,
490  schref.m_Ref.c_str(),
491  schref.m_NumRef,
492  TO_UTF8( schref.GetLibPart()->GetName() )
493  );
494  }
495  }
496 #endif
497 
504  static wxString Shorthand( std::vector<SCH_REFERENCE> aList );
505 
506  friend class BACK_ANNOTATION;
507 
508 private:
509  /* sort functions used to sort flatList
510  */
511 
512  static bool sortByRefAndValue( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
513 
514  static bool sortByXPosition( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
515 
516  static bool sortByYPosition( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
517 
518  static bool sortByTimeStamp( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
519 
520  static bool sortByReferenceOnly( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
521 
533  int CreateFirstFreeRefId( std::vector<int>& aIdList, int aFirstValue );
534 
535  // Used for sorting static sortByTimeStamp function
536  friend class BACK_ANNOTATE;
537 };
538 
539 #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 the list for a component with the given KIID 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 m_Footprint
The footprint assigned.
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,...
int FindRef(const wxString &aPath) const
searches the list for a component with a given reference.
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...
int m_Unit
The unit number for components with multiple parts per package.
wxString GetRef() const
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:100
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.
int CompareLibName(const SCH_REFERENCE &item) const
static bool sortByRefAndValue(const SCH_REFERENCE &item1, const SCH_REFERENCE &item2)
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.
const wxString GetFootprint() const
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...
wxString m_Value
The component value.
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 used to recieve, check, and apply a NETLIST from PCBNEW.
Definition: backannotate.h:55
LIB_PART * GetLibPart() const
bool IsSameInstance(const SCH_REFERENCE &other) const
Function IsSameInstance returns whether this reference refers to the same component instance (compone...
void SetFootprint(const wxString &aFP)
static wxString SubReference(int aUnit, bool aAddSeparator=true)
Schematic symbol object.
Definition: sch_component.h:80
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)
const wxString GetValue() const
void SetValue(const wxString &aValue)
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
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)