KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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
47{
48 for( EDA_ITEM* item : aSelection.Items() )
49 {
50 if( item->IsBOARD_ITEM() && static_cast<BOARD_ITEM*>( item )->IsLocked() )
51 return true;
52 }
53
54 return false;
55}
56
57
59{
60 for( EDA_ITEM* item : aSelection.Items() )
61 {
62 if( item->IsBOARD_ITEM() && !static_cast<BOARD_ITEM*>( item )->IsLocked() )
63 return true;
64 }
65
66 return false;
67}
68
69
70bool PCB_SELECTION_CONDITIONS::sameNetFunc( const SELECTION& aSelection, bool aAllowUnconnected )
71{
72 if( aSelection.Empty() )
73 return false;
74
75 int netcode = -1; // -1 stands for 'net code is not yet determined'
76
77 for( const EDA_ITEM* aitem : aSelection )
78 {
79 int current_netcode = -1;
80
81 const BOARD_CONNECTED_ITEM* item = dynamic_cast<const BOARD_CONNECTED_ITEM*>( aitem );
82
83 if( item )
84 {
85 current_netcode = item->GetNetCode();
86 }
87 else
88 {
89 if( !aAllowUnconnected )
90 return false;
91 else
92 // if it is not a BOARD_CONNECTED_ITEM, treat it as if there was no net assigned
93 current_netcode = 0;
94 }
95
96 assert( current_netcode >= 0 );
97
98 if( netcode < 0 )
99 {
100 netcode = current_netcode;
101
102 if( netcode == NETINFO_LIST::UNCONNECTED && !aAllowUnconnected )
103 return false;
104 }
105 else if( netcode != current_netcode )
106 {
107 return false;
108 }
109 }
110
111 return true;
112}
113
114
116{
117 if( aSelection.Empty() )
118 return false;
119
120 LSET layerSet;
121 layerSet.set();
122
123 for( const EDA_ITEM* i : aSelection )
124 {
125 const BOARD_ITEM* item = static_cast<const BOARD_ITEM*>( i );
126 layerSet &= item->GetLayerSet();
127
128 if( !layerSet.any() ) // there are no common layers left
129 return false;
130 }
131
132 return true;
133}
BASE_SET & set(size_t pos)
Definition: base_set.h:115
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:79
virtual LSET GetLayerSet() const
Return a std::bitset of all layers on which the item physically resides.
Definition: board_item.h:257
virtual bool IsLocked() const
Definition: board_item.cpp:75
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:89
LSET is a set of PCB_LAYER_IDs.
Definition: lset.h:36
static const int UNCONNECTED
Constant that holds the "unconnected net" number (typically 0) all items "connected" to this net are ...
Definition: netinfo.h:381
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 bool HasUnlockedItems(const SELECTION &aSelection)
Test if any selected items are unlocked.
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()
static bool HasLockedItems(const SELECTION &aSelection)
Test if any selected items are locked.
std::deque< EDA_ITEM * > & Items()
Definition: selection.h:177
bool Empty() const
Checks if there is anything selected.
Definition: selection.h:110
std::function< bool(const SELECTION &)> SELECTION_CONDITION
Functor type that checks a specific condition for selected items.