KiCad PCB EDA Suite
ee_collectors.cpp
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) 2011 Wayne Stambaugh <stambaughw@gmail.com>
5  * Copyright (C) 2004-2021 KiCad Developers, see AUTHORS.txt for contributors.
6  * Copyright (C) 2019 CERN
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 #include <macros.h>
27 #include <trace_helpers.h>
28 #include <ee_collectors.h>
29 #include <lib_item.h>
30 #include <sch_bus_entry.h>
31 #include <sch_symbol.h>
32 #include <sch_line.h>
33 #include <sch_screen.h>
34 #include <sch_sheet_path.h>
35 #include <transform.h>
36 #include "sch_reference_list.h"
37 
38 
41  EOT
42 };
43 
44 
46  SCH_TEXT_T,
55  SCH_LINE_T,
58  EOT
59 };
60 
61 
64  EOT
65 };
66 
67 
70  EOT
71 };
72 
73 
75 {
81  SCH_LINE_T,
83  SCH_TEXT_T,
91  EOT
92 };
93 
94 
96  SCH_LINE_T,
97  EOT
98 };
99 
100 
102  SCH_SYMBOL_T,
103  SCH_SHEET_T,
105  EOT
106 };
107 
108 
109 SEARCH_RESULT EE_COLLECTOR::Inspect( EDA_ITEM* aItem, void* aTestData )
110 {
111  if( aItem->Type() == LIB_PIN_T )
112  {
113  // Special selection rules apply to pins of different units when edited in
114  // synchronized pins mode. Leave it to EE_SELECTION_TOOL::Selectable() to
115  // decide what to do with them.
116  }
117  else if( m_Unit || m_Convert )
118  {
119  LIB_ITEM* lib_item = dynamic_cast<LIB_ITEM*>( aItem );
120 
121  if( m_Unit && lib_item && lib_item->GetUnit() && lib_item->GetUnit() != m_Unit )
123 
124  if( m_Convert && lib_item && lib_item->GetConvert() && lib_item->GetConvert() != m_Convert )
126  }
127 
128  if( aItem->HitTest( m_refPos, m_Threshold ) )
129  Append( aItem );
130 
132 }
133 
134 
135 void EE_COLLECTOR::Collect( SCH_SCREEN* aScreen, const KICAD_T aFilterList[], const wxPoint& aPos,
136  int aUnit, int aConvert )
137 {
138  Empty(); // empty the collection just in case
139 
140  SetScanTypes( aFilterList );
141  m_Unit = aUnit;
142  m_Convert = aConvert;
143 
144  // remember where the snapshot was taken from and pass refPos to the Inspect() function.
145  SetRefPos( aPos );
146 
147  if( aScreen )
148  {
149  // Symbols and sheets own their own children so have to be visited even if
150  // they're not in the filter list
151  bool symbolsVisited = false;
152  bool sheetsVisited = false;
153  bool globalLabelsVisited = false;
154 
155  for( const KICAD_T* filter = aFilterList; *filter != EOT; ++filter )
156  {
157  for( SCH_ITEM* item : aScreen->Items().OfType( *filter ) )
158  {
159  if( *filter == SCH_SYMBOL_T || *filter == SCH_LOCATE_ANY_T )
160  symbolsVisited = true;
161 
162  if( *filter == SCH_SHEET_T || *filter == SCH_LOCATE_ANY_T )
163  sheetsVisited = true;
164 
166  globalLabelsVisited = true;
167 
168  item->Visit( m_inspector, nullptr, m_scanTypes );
169  }
170  }
171 
172  if( !symbolsVisited )
173  {
174  for( SCH_ITEM* item : aScreen->Items().OfType( SCH_SYMBOL_T ) )
175  item->Visit( m_inspector, nullptr, m_scanTypes );
176  }
177 
178  if( !sheetsVisited )
179  {
180  for( SCH_ITEM* item : aScreen->Items().OfType( SCH_SHEET_T ) )
181  item->Visit( m_inspector, nullptr, m_scanTypes );
182  }
183 
184  if( !globalLabelsVisited )
185  {
186  for( SCH_ITEM* item : aScreen->Items().OfType( SCH_GLOBAL_LABEL_T ) )
187  item->Visit( m_inspector, nullptr, m_scanTypes );
188  }
189  }
190 }
191 
192 
193 void EE_COLLECTOR::Collect( LIB_ITEMS_CONTAINER& aItems, const KICAD_T aFilterList[],
194  const wxPoint& aPos, int aUnit, int aConvert )
195 {
196  Empty(); // empty the collection just in case
197 
198  SetScanTypes( aFilterList );
199  m_Unit = aUnit;
200  m_Convert = aConvert;
201 
202  // remember where the snapshot was taken from and pass refPos to the Inspect() function.
203  SetRefPos( aPos );
204 
205  for( LIB_ITEM& item : aItems )
206  {
207  if( item.Visit( m_inspector, nullptr, m_scanTypes ) == SEARCH_RESULT::QUIT )
208  break;
209  }
210 }
211 
212 
214 {
215  if( GetCount() != 2 )
216  return false;
217 
218  bool is_busentry0 = ( dynamic_cast<SCH_BUS_ENTRY_BASE*>( m_list[0] ) != nullptr );
219  bool is_busentry1 = ( dynamic_cast<SCH_BUS_ENTRY_BASE*>( m_list[1] ) != nullptr );
220 
221  if(( m_list[0]->Type() == SCH_LINE_T) && ( m_list[1]->Type() == SCH_LINE_T) )
222  return ( ( SCH_LINE* ) m_list[0])->GetLayer() == ( ( SCH_LINE* ) m_list[1])->GetLayer();
223 
224  if(( m_list[0]->Type() == SCH_LINE_T) && is_busentry1 )
225  return true;
226 
227  if( is_busentry0 && ( m_list[1]->Type() == SCH_LINE_T) )
228  return true;
229 
230  return false;
231 }
232 
233 
234 void CollectOtherUnits( const wxString& aRef, int aUnit, const LIB_ID& aLibId,
235  SCH_SHEET_PATH& aSheet, std::vector<SCH_SYMBOL*>* otherUnits )
236 {
237  SCH_REFERENCE_LIST symbols;
238  aSheet.GetSymbols( symbols );
239 
240  for( unsigned i = 0; i < symbols.GetCount(); i++ )
241  {
242  SCH_REFERENCE symbol = symbols[i];
243 
244  if( symbol.GetRef() == aRef && symbol.GetSymbol()->GetLibId() == aLibId
245  && symbol.GetUnit() != aUnit )
246  otherUnits->push_back( symbol.GetSymbol() );
247  }
248 }
249 
250 
void Empty()
Clear the list.
Definition: collector.h:95
EE_TYPE OfType(KICAD_T aType) const
Definition: sch_rtree.h:216
int m_Threshold
Definition: collector.h:248
void Collect(SCH_SCREEN *aScreen, const KICAD_T aFilterList[], const wxPoint &aPos, int aUnit=0, int aConvert=0)
Scan a EDA_ITEM using this class's Inspector method which does the collection.
void SetScanTypes(const KICAD_T *scanTypes)
Record the list of KICAD_T types to consider for collection by the Inspect() function.
Definition: collector.h:220
std::vector< EDA_ITEM * > m_list
Definition: collector.h:254
static const KICAD_T FieldOwners[]
Definition: ee_collectors.h:48
void CollectOtherUnits(const wxString &aRef, int aUnit, const LIB_ID &aLibId, SCH_SHEET_PATH &aSheet, std::vector< SCH_SYMBOL * > *otherUnits)
int GetUnit() const
SCH_SYMBOL * GetSymbol() const
static const KICAD_T MovableItems[]
Definition: ee_collectors.h:44
static const KICAD_T SheetsOnly[]
Definition: ee_collectors.h:46
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
search types array terminator (End Of Types)
Definition: typeinfo.h:81
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition: typeinfo.h:77
The base class for drawable items used by schematic library symbols.
Definition: lib_item.h:61
Container to create a flattened list of symbols because in a complex hierarchy, a symbol can be used ...
This file contains miscellaneous commonly used macros and functions.
void Append(EDA_ITEM *item)
Add an item to the end of the list.
Definition: collector.h:105
virtual bool HitTest(const wxPoint &aPosition, int aAccuracy=0) const
Test if aPosition is contained within or on the bounding box of an item.
Definition: eda_item.h:225
wxString GetRef() const
int GetCount() const
Return the number of objects in the list.
Definition: collector.h:87
int GetUnit() const
Definition: lib_item.h:259
INSPECTOR_FUNC m_inspector
Definition: collector.h:258
void SetRefPos(const wxPoint &aRefPos)
Definition: collector.h:225
wxPoint m_refPos
Definition: collector.h:259
size_t GetCount() const
wxLogTrace helper definitions.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
bool IsCorner() const
Test if the collected items form a corner of two line segments.
int GetConvert() const
Definition: lib_item.h:262
const KICAD_T * m_scanTypes
Definition: collector.h:257
Segment description base class to describe items which have 2 end points (track, wire,...
Definition: sch_line.h:37
EE_RTREE & Items()
Definition: sch_screen.h:102
void GetSymbols(SCH_REFERENCE_LIST &aReferences, bool aIncludePowerSymbols=true, bool aForceIncludeOrphanSymbols=false) const
Adds SCH_REFERENCE object to aReferences for each symbol in the sheet.
static const KICAD_T AllItems[]
Definition: ee_collectors.h:42
SEARCH_RESULT Inspect(EDA_ITEM *aItem, void *aTestData) override
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:100
static const KICAD_T SymbolsOnly[]
Definition: ee_collectors.h:45
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
SEARCH_RESULT
Definition: eda_item.h:41
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:197
static const KICAD_T EditableItems[]
Definition: ee_collectors.h:43
A helper to define a symbol's reference designator in a schematic.
static const KICAD_T WiresOnly[]
Definition: ee_collectors.h:47
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:113
const LIB_ID & GetLibId() const
Definition: sch_symbol.h:147