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
61  wxPoint m_CmpPos;
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 
102  int GetUnit() const { return m_Unit; }
103 
104  void SetSheetNumber( int aSheetNumber ) { m_SheetNum = aSheetNumber; }
105 
110  void Annotate();
111 
119  void Split();
120 
121  /* Some accessors which hide the strategy of how the reference is stored,
122  thereby making it easy to change that strategy.
123  */
124 
125  void SetRef( const wxString& aReference )
126  {
127  m_Ref = aReference;
128  }
129 
130  wxString GetRef() const
131  {
132  return m_Ref;
133  }
134  void SetRefStr( const std::string& aReference )
135  {
136  m_Ref = aReference;
137  }
138  const char* GetRefStr() const
139  {
140  return m_Ref.c_str();
141  }
142 
143  wxString GetRefNumber() const
144  {
145  wxString ref;
146 
147  if( m_NumRef < 0 )
148  return wxT( "?" );
149 
150  // To avoid a risk of duplicate, for power components
151  // the ref number is 0nnn instead of nnn.
152  // Just because sometimes only power components are annotated
153  if( GetLibPart() && GetLibPart()->IsPower() )
154  ref = wxT( "0" );
155 
156  return ref << m_NumRef;
157  }
158 
159  int CompareValue( const SCH_REFERENCE& item ) const
160  {
161  return m_Value->GetText().Cmp( item.m_Value->GetText() );
162  }
163 
164  int CompareRef( const SCH_REFERENCE& item ) const
165  {
166  return m_Ref.compare( item.m_Ref );
167  }
168 
169  int CompareLibName( const SCH_REFERENCE& item ) const
170  {
172  item.m_RootCmp->GetLibId().GetLibItemName() );
173  }
174 
180  bool IsSameInstance( const SCH_REFERENCE& other ) const
181  {
182  return GetComp() == other.GetComp() && GetSheetPath().Path() == other.GetSheetPath().Path();
183  }
184 
186  {
187  return m_Entry->UnitsLocked();
188  }
189 };
190 
191 
200 {
201 private:
202  std::vector <SCH_REFERENCE> componentFlatList;
203 
204 public:
208  {
209  }
210 
211  SCH_REFERENCE& operator[]( int aIndex )
212  {
213  return componentFlatList[ aIndex ];
214  }
215 
220  unsigned GetCount()
221  {
222  return componentFlatList.size();
223  }
224 
229  SCH_REFERENCE& GetItem( int aIdx )
230  {
231  return componentFlatList[aIdx];
232  }
233 
239  void AddItem( SCH_REFERENCE& aItem )
240  {
241  componentFlatList.push_back( aItem );
242  }
243 
250  void RemoveItem( unsigned int aIndex );
251 
259 
260  /* Sort functions:
261  * Sort functions are used to sort components for annotation or BOM generation.
262  * Because sorting depend on we want to do, there are many sort functions.
263  * Note:
264  * When creating BOM, components are fully annotated.
265  * references are something like U3, U5 or R4, R8
266  * When annotating, some or all components are not annotated,
267  * i.e. ref is only U or R, with no number.
268  */
269 
279  {
280  for( unsigned ii = 0; ii < GetCount(); ii++ )
281  componentFlatList[ii].Split();
282  }
283 
292  {
293  /* update the reference numbers */
294  for( unsigned ii = 0; ii < GetCount(); ii++ )
295  {
296  componentFlatList[ii].Annotate();
297  }
298  }
299 
317  void Annotate( bool aUseSheetNum, int aSheetIntervalId, int aStartNumber,
318  SCH_MULTI_UNIT_REFERENCE_MAP aLockedUnitMap );
319 
335  int CheckAnnotation( REPORTER& aReporter );
336 
352  {
353  sort( componentFlatList.begin(), componentFlatList.end(), sortByXPosition );
354  }
355 
371  {
372  sort( componentFlatList.begin(), componentFlatList.end(), sortByYPosition );
373  }
374 
381  {
382  sort( componentFlatList.begin(), componentFlatList.end(), sortByTimeStamp );
383  }
384 
401  {
402  sort( componentFlatList.begin(), componentFlatList.end(), sortByRefAndValue );
403  }
404 
417  {
419  }
420 
429  int FindUnit( size_t aIndex, int aUnit );
430 
439  void GetRefsInUse( int aIndex, std::vector< int >& aIdList, int aMinRefId );
440 
450  int GetLastReference( int aIndex, int aMinValue );
451 
452 #if defined(DEBUG)
453  void Show( const char* aPrefix = "" )
454  {
455  printf( "%s\n", aPrefix );
456 
457  for( unsigned i=0; i<componentFlatList.size(); ++i )
458  {
459  SCH_REFERENCE& schref = componentFlatList[i];
460 
461  printf( " [%-2d] ref:%-8s num:%-3d lib_part:%s\n",
462  i,
463  schref.m_Ref.c_str(),
464  schref.m_NumRef,
465  TO_UTF8( schref.GetLibPart()->GetName() )
466  );
467  }
468  }
469 #endif
470 
477  static wxString Shorthand( std::vector<SCH_REFERENCE> aList );
478 
479 
480 private:
481  /* sort functions used to sort componentFlatList
482  */
483 
484  static bool sortByRefAndValue( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
485 
486  static bool sortByXPosition( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
487 
488  static bool sortByYPosition( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
489 
490  static bool sortByTimeStamp( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
491 
492  static bool sortByReferenceOnly( const SCH_REFERENCE& item1, const SCH_REFERENCE& item2 );
493 
505  int CreateFirstFreeRefId( std::vector<int>& aIdList, int aFirstValue );
506 };
507 
508 #endif // _SCH_REFERENCE_LIST_H_
Class UTF8 is an 8 bit string that is assuredly encoded in UTF8, and supplies special conversion supp...
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.
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.
virtual const wxString GetText() const
Function GetText returns the string associated with the text object.
Definition: eda_text.h:147
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.
Class 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.
Class SCH_REFERENCE_LIST is used to create a flattened list of components because in a complex hierar...
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...
Class EDA_TEXT is a mix-in class (via multiple inheritance) that handles texts such as labels,...
Definition: eda_text.h:128
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:47
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.
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_ITEM 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...
static bool sortByYPosition(const SCH_REFERENCE &item1, const SCH_REFERENCE &item2)
Class 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.
const wxString & GetName() const
void SplitReferences()
Function SplitReferences attempts to split all reference designators into a name (U) and number (1).
LIB_PART * GetLibPart() const
size_t i
Definition: json11.cpp:597
bool IsSameInstance(const SCH_REFERENCE &other) const
Function IsSameInstance returns whether this reference refers to the same component instance (compone...
Class SCH_COMPONENT describes a real schematic component.
Definition: sch_component.h:73
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:53
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
Implementation of the label properties dialog.
bool UnitsLocked() const
Check whether part units are interchangeable.
Class 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)