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-2020 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_component.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 
74 SEARCH_RESULT EE_COLLECTOR::Inspect( EDA_ITEM* aItem, void* aTestData )
75 {
76  if( aItem->Type() == LIB_PIN_T )
77  {
78  // Special selection rules apply to pins of different units when edited in
79  // synchronized pins mode. Leave it to EE_SELECTION_TOOL::Selectable() to
80  // decide what to do with them.
81  }
82  else if( m_Unit || m_Convert )
83  {
84  LIB_ITEM* lib_item = dynamic_cast<LIB_ITEM*>( aItem );
85 
86  if( m_Unit && lib_item && lib_item->GetUnit() && lib_item->GetUnit() != m_Unit )
88 
89  if( m_Convert && lib_item && lib_item->GetConvert() && lib_item->GetConvert() != m_Convert )
91  }
92 
93  if( aItem->HitTest( m_RefPos, m_Threshold ) )
94  Append( aItem );
95 
97 }
98 
99 
100 void EE_COLLECTOR::Collect( SCH_SCREEN* aScreen, const KICAD_T aFilterList[], const wxPoint& aPos,
101  int aUnit, int aConvert )
102 {
103  Empty(); // empty the collection just in case
104 
105  SetScanTypes( aFilterList );
106  m_Unit = aUnit;
107  m_Convert = aConvert;
108 
109  // remember where the snapshot was taken from and pass refPos to the Inspect() function.
110  SetRefPos( aPos );
111 
112  if( aScreen )
113  {
114  // Components and sheets own their own children so have to be visited even if
115  // they're not in the filter list
116  bool componentsVisited = false;
117  bool sheetsVisited = false;
118 
119  for( const KICAD_T* filter = aFilterList; *filter != EOT; ++filter )
120  {
121  for( SCH_ITEM* item : aScreen->Items().OfType( *filter ) )
122  {
123  if( *filter == SCH_COMPONENT_T || *filter == SCH_LOCATE_ANY_T )
124  componentsVisited = true;
125 
126  if( *filter == SCH_SHEET_T || *filter == SCH_LOCATE_ANY_T )
127  sheetsVisited = true;
128 
129  item->Visit( m_inspector, nullptr, m_ScanTypes );
130  }
131  }
132 
133  if( !componentsVisited )
134  {
135  for( SCH_ITEM* item : aScreen->Items().OfType( SCH_COMPONENT_T ) )
136  item->Visit( m_inspector, nullptr, m_ScanTypes );
137  }
138 
139  if( !sheetsVisited )
140  {
141  for( SCH_ITEM* item : aScreen->Items().OfType( SCH_SHEET_T ) )
142  item->Visit( m_inspector, nullptr, m_ScanTypes );
143  }
144  }
145 }
146 
147 
148 void EE_COLLECTOR::Collect( LIB_ITEMS_CONTAINER& aItems, const KICAD_T aFilterList[],
149  const wxPoint& aPos, int aUnit, int aConvert )
150 {
151  Empty(); // empty the collection just in case
152 
153  SetScanTypes( aFilterList );
154  m_Unit = aUnit;
155  m_Convert = aConvert;
156 
157  // remember where the snapshot was taken from and pass refPos to the Inspect() function.
158  SetRefPos( aPos );
159 
160  for( auto& item : aItems )
161  {
162  if( item.Visit( m_inspector, nullptr, m_ScanTypes ) == SEARCH_RESULT::QUIT )
163  break;
164  }
165 }
166 
167 
169 {
170  if( GetCount() != 2 )
171  return false;
172 
173  bool is_busentry0 = (dynamic_cast<SCH_BUS_ENTRY_BASE*>( m_List[0] ) != NULL);
174  bool is_busentry1 = (dynamic_cast<SCH_BUS_ENTRY_BASE*>( m_List[1] ) != NULL);
175 
176  if( (m_List[0]->Type() == SCH_LINE_T) && (m_List[1]->Type() == SCH_LINE_T) )
177  return ( ( SCH_LINE* ) m_List[0])->GetLayer() == ( ( SCH_LINE* ) m_List[1])->GetLayer();
178 
179  if( (m_List[0]->Type() == SCH_LINE_T) && is_busentry1 )
180  return true;
181 
182  if( is_busentry0 && (m_List[1]->Type() == SCH_LINE_T) )
183  return true;
184 
185  return false;
186 }
187 
188 
190  std::vector<SCH_COMPONENT*>* otherUnits )
191 {
192  // Obviously, one can collect other units only if aUnit is annotated.
193  if( aUnit->GetUnitCount() > 1 && aUnit->IsAnnotated( &aSheet ) )
194  {
195  const LIB_ID thisLibId = aUnit->GetLibId();
196  const wxString thisRef = aUnit->GetRef( &aSheet );
197  int thisUnit = aUnit->GetUnit();
198 
199  SCH_REFERENCE_LIST components;
200  aSheet.GetComponents( components );
201 
202  for( unsigned i = 0; i < components.GetCount(); i++ )
203  {
204  SCH_REFERENCE component = components[i];
205 
206  if( component.GetRef() == thisRef && component.GetUnit() != thisUnit )
207  otherUnits->push_back( component.GetComp() );
208  }
209  }
210 }
211 
212 
void Empty()
Function Empty sets the list to empty.
Definition: collector.h:110
int m_Threshold
Definition: collector.h:67
void GetComponents(SCH_REFERENCE_LIST &aReferences, bool aIncludePowerSymbols=true, bool aForceIncludeOrphanComponents=false) const
Function GetComponents adds a SCH_REFERENCE() object to aReferences for each component in the sheet.
void Collect(SCH_SCREEN *aScreen, const KICAD_T aFilterList[], const wxPoint &aPos, int aUnit=0, int aConvert=0)
Function Collect scans a EDA_ITEM using this class's Inspector method, which does the collection.
void SetScanTypes(const KICAD_T *scanTypes)
Function SetScanTypes records the list of KICAD_T types to consider for collection by the Inspect() f...
Definition: collector.h:247
SCH_COMPONENT * GetComp() const
EE_TYPE OfType(KICAD_T aType)
Definition: sch_rtree.h:219
int GetUnitCount() const
Return the number of units per package of the symbol.
static const KICAD_T ComponentsOnly[]
Definition: ee_collectors.h:47
int GetUnit() const
static const KICAD_T SheetsOnly[]
Definition: ee_collectors.h:48
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:82
KICAD_T
Enum KICAD_T is the set of class identification values, stored in EDA_ITEM::m_StructType.
Definition: typeinfo.h:78
The base class for drawable items used by schematic library components.
Definition: lib_item.h:61
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 Append(EDA_ITEM *item)
Function Append adds an item to the end of the list.
Definition: collector.h:120
virtual bool HitTest(const wxPoint &aPosition, int aAccuracy=0) const
Function HitTest tests if aPosition is contained within or on the bounding box of an item.
Definition: base_struct.h:307
wxString GetRef() const
int GetCount() const
Function GetCount returns the number of objects in the list.
Definition: collector.h:101
int GetUnit() const
Definition: lib_item.h:295
int GetUnit() const
INSPECTOR_FUNC m_inspector
Definition: collector.h:62
#define NULL
void SetRefPos(const wxPoint &aRefPos)
Definition: collector.h:252
wxLogTrace helper definitions.
bool IsAnnotated(const SCH_SHEET_PATH *aSheet)
Checks if the component has a valid annotation (reference) for the given sheet path.
SCH_SHEET_PATH.
bool IsCorner() const
Function IsCorner tests if the collected items forms as corner of two line segments.
int GetConvert() const
Definition: lib_item.h:298
unsigned GetCount() const
Function GetCount.
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:158
static const KICAD_T AllItems[]
Definition: ee_collectors.h:45
Schematic symbol object.
Definition: sch_component.h:80
SEARCH_RESULT Inspect(EDA_ITEM *aItem, void *aTestData) override
EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boards.
Definition: base_struct.h:159
static bool GetLayer(MODEL_VRML &aModel, LAYER_NUM layer, VRML_LAYER **vlayer)
wxPoint m_RefPos
Definition: collector.h:63
void CollectOtherUnits(SCH_SHEET_PATH &aSheet, SCH_COMPONENT *aUnit, std::vector< SCH_COMPONENT * > *otherUnits)
SEARCH_RESULT
Definition: base_struct.h:51
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
std::vector< EDA_ITEM * > m_List
Definition: collector.h:58
const LIB_ID & GetLibId() const
const KICAD_T * m_ScanTypes
Definition: collector.h:61
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:194
static const KICAD_T EditableItems[]
Definition: ee_collectors.h:46
SCH_REFERENCE is used as a helper to define a component's reference designator in a schematic.
KICAD_T Type() const
Function Type()
Definition: base_struct.h:193
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const
Return the reference for the given sheet path.