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  EE_RTREE& items = aScreen->Items();
156 
157  for( const KICAD_T *filter = aFilterList; *filter != EOT; ++filter )
158  {
159  for( SCH_ITEM *item : items.Overlapping( *filter, aPos, m_Threshold ) )
160  {
161  if( *filter == SCH_SYMBOL_T || *filter == SCH_LOCATE_ANY_T )
162  symbolsVisited = true;
163 
164  if( *filter == SCH_SHEET_T || *filter == SCH_LOCATE_ANY_T )
165  sheetsVisited = true;
166 
168  globalLabelsVisited = true;
169 
170  item->Visit( m_inspector, nullptr, m_scanTypes );
171  }
172  }
173 
174  if( !symbolsVisited )
175  {
176  for( SCH_ITEM *item : items.Overlapping( SCH_SYMBOL_T, aPos, m_Threshold ) )
177  item->Visit( m_inspector, nullptr, m_scanTypes );
178  }
179 
180  if( !sheetsVisited )
181  {
182  for( SCH_ITEM *item : items.Overlapping( SCH_SHEET_T, aPos, m_Threshold ) )
183  item->Visit( m_inspector, nullptr, m_scanTypes );
184  }
185 
186  if( !globalLabelsVisited )
187  {
188  for( SCH_ITEM *item : items.Overlapping( SCH_GLOBAL_LABEL_T, aPos, m_Threshold ) )
189  item->Visit( m_inspector, nullptr, m_scanTypes );
190  }
191  }
192 }
193 
194 
195 void EE_COLLECTOR::Collect( LIB_ITEMS_CONTAINER& aItems, const KICAD_T aFilterList[],
196  const wxPoint& aPos, int aUnit, int aConvert )
197 {
198  Empty(); // empty the collection just in case
199 
200  SetScanTypes( aFilterList );
201  m_Unit = aUnit;
202  m_Convert = aConvert;
203 
204  // remember where the snapshot was taken from and pass refPos to the Inspect() function.
205  SetRefPos( aPos );
206 
207  for( LIB_ITEM& item : aItems )
208  {
209  if( item.Visit( m_inspector, nullptr, m_scanTypes ) == SEARCH_RESULT::QUIT )
210  break;
211  }
212 }
213 
214 
216 {
217  if( GetCount() != 2 )
218  return false;
219 
220  bool is_busentry0 = ( dynamic_cast<SCH_BUS_ENTRY_BASE*>( m_list[0] ) != nullptr );
221  bool is_busentry1 = ( dynamic_cast<SCH_BUS_ENTRY_BASE*>( m_list[1] ) != nullptr );
222 
223  if(( m_list[0]->Type() == SCH_LINE_T) && ( m_list[1]->Type() == SCH_LINE_T) )
224  return ( ( SCH_LINE* ) m_list[0])->GetLayer() == ( ( SCH_LINE* ) m_list[1])->GetLayer();
225 
226  if(( m_list[0]->Type() == SCH_LINE_T) && is_busentry1 )
227  return true;
228 
229  if( is_busentry0 && ( m_list[1]->Type() == SCH_LINE_T) )
230  return true;
231 
232  return false;
233 }
234 
235 
236 void CollectOtherUnits( const wxString& aRef, int aUnit, const LIB_ID& aLibId,
237  SCH_SHEET_PATH& aSheet, std::vector<SCH_SYMBOL*>* otherUnits )
238 {
239  SCH_REFERENCE_LIST symbols;
240  aSheet.GetSymbols( symbols );
241 
242  for( unsigned i = 0; i < symbols.GetCount(); i++ )
243  {
244  SCH_REFERENCE symbol = symbols[i];
245 
246  if( symbol.GetRef() == aRef && symbol.GetSymbol()->GetLibId() == aLibId
247  && symbol.GetUnit() != aUnit )
248  otherUnits->push_back( symbol.GetSymbol() );
249  }
250 }
251 
252 
void Empty()
Clear the list.
Definition: collector.h:90
int m_Threshold
Definition: collector.h:241
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:213
std::vector< EDA_ITEM * > m_list
Definition: collector.h:247
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)
Implements an R-tree for fast spatial and type indexing of schematic items.
Definition: sch_rtree.h:40
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:100
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:82
int GetUnit() const
Definition: lib_item.h:258
INSPECTOR_FUNC m_inspector
Definition: collector.h:251
void SetRefPos(const wxPoint &aRefPos)
Definition: collector.h:218
wxPoint m_refPos
Definition: collector.h:252
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:261
const KICAD_T * m_scanTypes
Definition: collector.h:250
EE_TYPE Overlapping(const EDA_RECT &aRect) const
Definition: sch_rtree.h:221
Segment description base class to describe items which have 2 end points (track, wire,...
Definition: sch_line.h:37
EE_RTREE & Items()
Gets the full RTree, usually for iterating.
Definition: sch_screen.h:110
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:182
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