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-2018 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 
31 #ifndef _SCH_REFERENCE_LIST_H_
32 #define _SCH_REFERENCE_LIST_H_
33 
34 
35 #include <macros.h>
36 
37 #include <class_libentry.h>
38 #include <sch_sheet_path.h>
39 #include <sch_component.h>
40 #include <sch_text.h>
41 
42 #include <map>
43 
44 class SCH_REFERENCE;
45 class SCH_REFERENCE_LIST;
46 
56 {
58  UTF8 m_Ref; // it's private, use the accessors please
62  int m_Unit;
66  bool m_IsNew;
67  int m_SheetNum;
70  int m_NumRef;
72  int m_Flag;
73 
74  friend class SCH_REFERENCE_LIST;
75 
76 
77 public:
78 
80  m_SheetPath()
81  {
82  m_RootCmp = NULL;
83  m_Entry = NULL;
84  m_Unit = 0;
85  m_TimeStamp = 0;
86  m_IsNew = false;
87  m_Value = NULL;
88  m_NumRef = 0;
89  m_Flag = 0;
90  m_SheetNum = 0;
91  }
92 
93  SCH_REFERENCE( SCH_COMPONENT* aComponent, LIB_PART* aLibComponent,
94  SCH_SHEET_PATH& aSheetPath );
95 
96  SCH_COMPONENT* GetComp() const { return m_RootCmp; }
97 
98  LIB_PART* GetLibPart() const { return m_Entry; }
99 
100  const SCH_SHEET_PATH& GetSheetPath() const { return m_SheetPath; }
101 
103  {
104  return m_SheetPath;
105  }
106 
107  int GetUnit() const { return m_Unit; }
108 
109  void SetSheetNumber( int aSheetNumber ) { m_SheetNum = aSheetNumber; }
110 
115  void Annotate();
116 
124  void Split();
125 
126  /* Some accessors which hide the strategy of how the reference is stored,
127  thereby making it easy to change that strategy.
128  */
129 
130  void SetRef( const wxString& aReference )
131  {
132  m_Ref = aReference;
133  }
134 
135  wxString GetRef() const
136  {
137  return m_Ref;
138  }
139  void SetRefStr( const std::string& aReference )
140  {
141  m_Ref = aReference;
142  }
143  const char* GetRefStr() const
144  {
145  return m_Ref.c_str();
146  }
147 
148  wxString GetRefNumber() const
149  {
150  wxString ref;
151 
152  if( m_NumRef < 0 )
153  return wxT( "?" );
154 
155  // To avoid a risk of duplicate, for power components
156  // the ref number is 0nnn instead of nnn.
157  // Just because sometimes only power components are annotated
158  if( GetLibPart() && GetLibPart()->IsPower() )
159  ref = wxT( "0" );
160 
161  return ref << m_NumRef;
162  }
163 
164  int CompareValue( const SCH_REFERENCE& item ) const
165  {
166  return m_Value->GetText().Cmp( item.m_Value->GetText() );
167  }
168 
169  int CompareRef( const SCH_REFERENCE& item ) const
170  {
171  return m_Ref.compare( item.m_Ref );
172  }
173 
174  int CompareLibName( const SCH_REFERENCE& item ) const
175  {
177  item.m_RootCmp->GetLibId().GetLibItemName() );
178  }
179 
185  bool IsSameInstance( const SCH_REFERENCE& other ) const
186  {
187  return GetComp() == other.GetComp() && GetSheetPath().Path() == other.GetSheetPath().Path();
188  }
189 
191  {
192  return m_Entry->UnitsLocked();
193  }
194 };
195 
196 
205 {
206 private:
207  std::vector <SCH_REFERENCE> componentFlatList;
208 
209 public:
213  {
214  }
215 
216  SCH_REFERENCE& operator[]( int aIndex )
217  {
218  return componentFlatList[ aIndex ];
219  }
220 
225  unsigned GetCount()
226  {
227  return componentFlatList.size();
228  }
229 
234  SCH_REFERENCE& GetItem( int aIdx )
235  {
236  return componentFlatList[aIdx];
237  }
238 
244  void AddItem( SCH_REFERENCE& aItem )
245  {
246  componentFlatList.push_back( aItem );
247  }
248 
255  void RemoveItem( unsigned int aIndex );
256 
264 
265  /* Sort functions:
266  * Sort functions are used to sort components for annotation or BOM generation.
267  * Because sorting depends on what we want to do, there are many sort functions.
268  * Note:
269  * When creating BOM, components are fully annotated.
270  * references are something like U3, U5 or R4, R8
271  * When annotating, some or all components are not annotated,
272  * i.e. ref is only U or R, with no number.
273  */
274 
284  {
285  for( unsigned ii = 0; ii < GetCount(); ii++ )
286  componentFlatList[ii].Split();
287  }
288 
297  {
298  /* update the reference numbers */
299  for( unsigned ii = 0; ii < GetCount(); ii++ )
300  {
301  componentFlatList[ii].Annotate();
302  }
303  }
304 
322  void Annotate( bool aUseSheetNum, int aSheetIntervalId, int aStartNumber,
323  SCH_MULTI_UNIT_REFERENCE_MAP aLockedUnitMap );
324 
340  int CheckAnnotation( REPORTER& aReporter );
341 
357  {
358  sort( componentFlatList.begin(), componentFlatList.end(), sortByXPosition );
359  }
360 
376  {
377  sort( componentFlatList.begin(), componentFlatList.end(), sortByYPosition );
378  }
379 
386  {
387  sort( componentFlatList.begin(), componentFlatList.end(), sortByTimeStamp );
388  }
389 
406  {
407  sort( componentFlatList.begin(), componentFlatList.end(), sortByRefAndValue );
408  }
409 
422  {
424  }
425 
434  int FindUnit( size_t aIndex, int aUnit );
435 
444  void GetRefsInUse( int aIndex, std::vector< int >& aIdList, int aMinRefId );
445 
455  int GetLastReference( int aIndex, int aMinValue );
456 
457 #if defined(DEBUG)
458  void Show( const char* aPrefix = "" )
459  {
460  printf( "%s\n", aPrefix );
461 
462  for( unsigned i=0; i<componentFlatList.size(); ++i )
463  {
464  SCH_REFERENCE& schref = componentFlatList[i];
465 
466  printf( " [%-2d] ref:%-8s num:%-3d lib_part:%s\n",
467  i,
468  schref.m_Ref.c_str(),
469  schref.m_NumRef,
470  TO_UTF8( schref.GetLibPart()->GetName() )
471  );
472  }
473  }
474 #endif
475 
482  static wxString Shorthand( std::vector<SCH_REFERENCE> aList );
483 
484 
485 private:
486  /* sort functions used to sort componentFlatList
487  */
488 
489  static bool sortByRefAndValue( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
490 
491  static bool sortByXPosition( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
492 
493  static bool sortByYPosition( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
494 
495  static bool sortByTimeStamp( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
496 
497  static bool sortByReferenceOnly( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
498 
510  int CreateFirstFreeRefId( std::vector<int>& aIdList, int aFirstValue );
511 };
512 
513 #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
std::vector< SCH_REFERENCE > componentFlatList
const UTF8 & GetLibItemName() const
Definition: lib_id.h:114
void RemoveSubComponentsFromList()
Function RemoveSubComponentsFromList Remove sub components from the list, when multiples parts per pa...
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).
const SCH_SHEET_PATH & GetSheetPath() const
UTF8 m_Ref
Component reference prefix, without number (for IC1, this is IC) )
int m_NumRef
The numeric part of the reference designator.
SCH_COMPONENT * GetComp() const
void AddItem(SCH_REFERENCE &aItem)
Function AddItem adds a SCH_REFERENCE object to the list of references.
static wxString Shorthand(std::vector< SCH_REFERENCE > aList)
Function Shorthand Returns a shorthand string representing all the references in the list.
int GetUnit() 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:61
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:109
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:48
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.
#define NULL
wxString Path() const
Function Path the path uses the time stamps which do not changes even when editing sheet parameters a...
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...
timestamp_t m_TimeStamp
The time stamp for the reference.
SCH_SHEET_PATH m_SheetPath
The sheet path for this reference.
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).
LIB_PART * GetLibPart() const
bool IsSameInstance(const SCH_REFERENCE &other) const
Function IsSameInstance returns whether this reference refers to the same component instance (compone...
SCH_COMPONENT describes a real schematic component.
Definition: sch_component.h:89
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)
uint32_t timestamp_t
timestamp_t is our type to represent unique IDs for all kinds of elements; historically simply the ti...
Definition: common.h:52
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.
unsigned GetCount()
Function GetCount.
const LIB_ID & GetLibId() const
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:123
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)