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 * Copyright (C) 2017-2022 KiCad Developers, see AUTHORS.txt for contributors.
6 * @author Maciej Suminski <[email protected]>
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
27#include "pcb_selection_tool.h"
29
30#include <functional>
31using namespace std::placeholders;
32
33
35{
36 return std::bind( &PCB_SELECTION_CONDITIONS::sameNetFunc, _1, aAllowUnconnected );
37}
38
39
41{
42 return std::bind( &PCB_SELECTION_CONDITIONS::sameLayerFunc, _1 );
43}
44
45
46bool PCB_SELECTION_CONDITIONS::sameNetFunc( const SELECTION& aSelection, bool aAllowUnconnected )
47{
48 if( aSelection.Empty() )
49 return false;
50
51 int netcode = -1; // -1 stands for 'net code is not yet determined'
52
53 for( const EDA_ITEM* aitem : aSelection )
54 {
55 int current_netcode = -1;
56
57 const BOARD_CONNECTED_ITEM* item = dynamic_cast<const BOARD_CONNECTED_ITEM*>( aitem );
58
59 if( item )
60 {
61 current_netcode = item->GetNetCode();
62 }
63 else
64 {
65 if( !aAllowUnconnected )
66 return false;
67 else
68 // if it is not a BOARD_CONNECTED_ITEM, treat it as if there was no net assigned
69 current_netcode = 0;
70 }
71
72 assert( current_netcode >= 0 );
73
74 if( netcode < 0 )
75 {
76 netcode = current_netcode;
77
78 if( netcode == NETINFO_LIST::UNCONNECTED && !aAllowUnconnected )
79 return false;
80 }
81 else if( netcode != current_netcode )
82 {
83 return false;
84 }
85 }
86
87 return true;
88}
89
90
92{
93 if( aSelection.Empty() )
94 return false;
95
96 LSET layerSet;
97 layerSet.set();
98
99 for( const EDA_ITEM* i : aSelection )
100 {
101 const BOARD_ITEM* item = static_cast<const BOARD_ITEM*>( i );
102 layerSet &= item->GetLayerSet();
103
104 if( !layerSet.any() ) // there are no common layers left
105 return false;
106 }
107
108 return true;
109}
110
111
A base class derived from BOARD_ITEM for items that can be connected and have a net,...
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:58
virtual LSET GetLayerSet() const
Return a std::bitset of all layers on which the item physically resides.
Definition: board_item.h:185
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:85
LSET is a set of PCB_LAYER_IDs.
Definition: layer_ids.h:530
static const int UNCONNECTED
Constant that forces initialization of a netinfo item to the NETINFO_ITEM ORPHANED (typically -1) whe...
Definition: netinfo.h:373
static SELECTION_CONDITION SameLayer()
Creates a functor that tests if selection contains items that belong exclusively to the same layer.
static bool sameLayerFunc(const SELECTION &aSelection)
static SELECTION_CONDITION SameNet(bool aAllowUnconnected=false)
Create a functor that tests if selection contains items belonging to the same net or are unconnected ...
static bool sameNetFunc(const SELECTION &aSelection, bool aAllowUnconnected)
< Helper function used by SameNet()
bool Empty() const
Checks if there is anything selected.
Definition: selection.h:107
std::function< bool(const SELECTION &)> SELECTION_CONDITION
< Functor type that checks a specific condition for selected items.