KiCad PCB EDA Suite
pcb_selection_conditions.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) 2014 CERN
5  * @author Maciej Suminski <maciej.suminski@cern.ch>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
26 #include "selection_tool.h"
27 #include <board_connected_item.h>
28 
29 #include <functional>
30 using namespace std::placeholders;
31 
32 
34 {
35  if( aSelection.Empty() )
36  return false;
37 
38  for( const auto &item : aSelection )
39  {
40  auto type = item->Type();
41 
42  if( type != PCB_PAD_T && type != PCB_VIA_T && type != PCB_TRACE_T && type != PCB_ZONE_T )
43  return false;
44  }
45 
46  return true;
47 }
48 
49 
51 {
52  return std::bind( &PCB_SELECTION_CONDITIONS::sameNetFunc, _1, aAllowUnconnected );
53 }
54 
55 
57 {
58  return std::bind( &PCB_SELECTION_CONDITIONS::sameLayerFunc, _1 );
59 }
60 
61 
62 
63 bool PCB_SELECTION_CONDITIONS::sameNetFunc( const SELECTION& aSelection, bool aAllowUnconnected )
64 {
65  if( aSelection.Empty() )
66  return false;
67 
68  int netcode = -1; // -1 stands for 'net code is not yet determined'
69 
70  for( const auto& aitem : aSelection )
71  {
72  int current_netcode = -1;
73 
74  const BOARD_CONNECTED_ITEM* item =
75  dynamic_cast<const BOARD_CONNECTED_ITEM*>( aitem );
76 
77  if( item )
78  {
79  current_netcode = item->GetNetCode();
80  }
81  else
82  {
83  if( !aAllowUnconnected )
84  return false;
85  else
86  // if it is not a BOARD_CONNECTED_ITEM, treat it as if there was no net assigned
87  current_netcode = 0;
88  }
89 
90  assert( current_netcode >= 0 );
91 
92  if( netcode < 0 )
93  {
94  netcode = current_netcode;
95 
96  if( netcode == NETINFO_LIST::UNCONNECTED && !aAllowUnconnected )
97  return false;
98  }
99  else if( netcode != current_netcode )
100  {
101  return false;
102  }
103  }
104 
105  return true;
106 }
107 
108 
110 {
111  if( aSelection.Empty() )
112  return false;
113 
114  LSET layerSet;
115  layerSet.set();
116 
117  for( const auto& i : aSelection )
118  {
119  auto item = static_cast<BOARD_ITEM*>( i );
120  layerSet &= item->GetLayerSet();
121 
122  if( !layerSet.any() ) // there are no common layers left
123  return false;
124  }
125 
126  return true;
127 }
bool Empty() const
Checks if there is anything selected.
Definition: selection.h:109
Class BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class...
static bool sameLayerFunc(const SELECTION &aSelection)
Helper function used by SameLayer()
class D_PAD, a pad in a footprint
Definition: typeinfo.h:90
Class BOARD_CONNECTED_ITEM is a base class derived from BOARD_ITEM for items that can be connected an...
class TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:95
static SELECTION_CONDITION SameLayer()
Function SameLayer Creates a functor that tests if selection contains items that belong exclusively t...
Class LSET is a set of PCB_LAYER_IDs.
class SEGZONE, a segment used to fill a zone area (segment on a copper layer)
Definition: typeinfo.h:97
std::function< bool(const SELECTION &)> SELECTION_CONDITION
Functor type that checks a specific condition for selected items.
int GetNetCode() const
Function GetNetCode.
size_t i
Definition: json11.cpp:597
class VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:96
Class BOARD_CONNECTED_ITEM.
static bool sameNetFunc(const SELECTION &aSelection, bool aAllowUnconnected)
Helper function used by SameNet()
static const int UNCONNECTED
Constant that holds the "unconnected net" number (typically 0) all items "connected" to this net are ...
Definition: netinfo.h:461
static bool OnlyConnectedItems(const SELECTION &aSelection)
Function OnlyConnectedItems Tests if selection contains exclusively connected items (pads...
static SELECTION_CONDITION SameNet(bool aAllowUnconnected=false)
Function SameNet Creates a functor that tests if selection contains items belonging to the same net o...