diff --git a/pcbnew/tools/selection_conditions.cpp b/pcbnew/tools/selection_conditions.cpp index db4c736b4f..d05be088c5 100644 --- a/pcbnew/tools/selection_conditions.cpp +++ b/pcbnew/tools/selection_conditions.cpp @@ -24,6 +24,7 @@ #include "selection_conditions.h" #include "selection_tool.h" +#include #include @@ -51,6 +52,18 @@ bool SELECTION_CONDITIONS::OnlyConnectedItems( const SELECTION& aSelection ) } +SELECTION_CONDITION SELECTION_CONDITIONS::SameNet() +{ + return boost::bind( &SELECTION_CONDITIONS::sameNetFunc, _1 ); +} + + +SELECTION_CONDITION SELECTION_CONDITIONS::SameLayer() +{ + return boost::bind( &SELECTION_CONDITIONS::sameLayerFunc, _1 ); +} + + SELECTION_CONDITION SELECTION_CONDITIONS::HasType( KICAD_T aType ) { return boost::bind( &SELECTION_CONDITIONS::hasTypeFunc, _1, aType ); @@ -81,6 +94,63 @@ SELECTION_CONDITION SELECTION_CONDITIONS::LessThan( int aNumber ) } +bool SELECTION_CONDITIONS::sameNetFunc( const SELECTION& aSelection ) +{ + if( aSelection.Empty() ) + return false; + + int netcode = -1; + + for( int i = 0; i < aSelection.Size(); ++i ) + { + const BOARD_CONNECTED_ITEM* item = + dynamic_cast( aSelection.Item( i ) ); + + if( !item ) + return false; + + if( netcode < 0 ) + { + netcode = item->GetNetCode(); + + if( netcode == NETINFO_LIST::UNCONNECTED ) + return false; + } + else if( netcode != item->GetNetCode() ) + { + return false; + } + } + + return true; +} + + +bool SELECTION_CONDITIONS::sameLayerFunc( const SELECTION& aSelection ) +{ + if( aSelection.Empty() ) + return false; + + LSET layerSet; + layerSet.set(); + + for( int i = 0; i < aSelection.Size(); ++i ) + { + const BOARD_ITEM* item = dynamic_cast( aSelection.Item( i ) ); + + if( !item ) + return false; + + layerSet &= item->GetLayerSet(); + + if( !layerSet.any() ) // there are no common layers left + return false; + } + + return true; +} + + bool SELECTION_CONDITIONS::hasTypeFunc( const SELECTION& aSelection, KICAD_T aType ) { for( int i = 0; i < aSelection.Size(); ++i ) diff --git a/pcbnew/tools/selection_conditions.h b/pcbnew/tools/selection_conditions.h index b001dd37d6..7f09533bc4 100644 --- a/pcbnew/tools/selection_conditions.h +++ b/pcbnew/tools/selection_conditions.h @@ -73,9 +73,29 @@ public: */ static bool OnlyConnectedItems( const SELECTION& aSelection ); + /** + * Function SameNet + * Creates a functor that tests if selection contains items belonging to the same net. If there + * are items which are not of BOARD_CONNECTED_ITEM type, the result is false. If all items do + * not have any net assigned, the result is negative (technically they have the same netcode, + * but in most cases they should not be connected together). + * @return Functor testing if selected items are exclusively connected items belonging to + * the same net (and netcode > 0). + */ + static SELECTION_CONDITION SameNet(); + + /** + * Function SameLayer + * Creates a functor that tests if selection contains items that belong exclusively to the same + * layer. In case of items belonging to multiple layers, it is enough to have a single common + * layer with other items. + * @return Functor testing if selected items share at least one common layer. + */ + static SELECTION_CONDITION SameLayer(); + /** * Function HasType - * Creates functor that tests if among the selected items there is at least one of a given type. + * Creates a functor that tests if among the selected items there is at least one of a given type. * @param aType is the type that is searched. * @return Functor testing for presence of items of a given type. */ @@ -83,7 +103,7 @@ public: /** * Function OnlyType - * Creates functor that tests if the selected items are *only* of given type. + * Creates a functor that tests if the selected items are *only* of given type. * @param aType is the type that is searched. * @return Functor testing if selected items are exclusively of one type.. */ @@ -91,7 +111,7 @@ public: /** * Function Count - * Creates functor that tests if the number of selected items is equal to the value given as + * Creates a functor that tests if the number of selected items is equal to the value given as * parameter. * @param aNumber is the number of expected items. * @return Functor testing if the number of selected items is equal aNumber. @@ -100,7 +120,7 @@ public: /** * Function MoreThan - * Creates functor that tests if the number of selected items is greater than the value given + * Creates a functor that tests if the number of selected items is greater than the value given * as parameter. * @param aNumber is the number used for comparison. * @return Functor testing if the number of selected items is greater than aNumber. @@ -109,7 +129,7 @@ public: /** * Function LessThan - * Creates functor that tests if the number of selected items is smaller than the value given + * Creates a functor that tests if the number of selected items is smaller than the value given * as parameter. * @param aNumber is the number used for comparison. * @return Functor testing if the number of selected items is smaller than aNumber. @@ -117,6 +137,9 @@ public: static SELECTION_CONDITION LessThan( int aNumber ); private: + static bool sameNetFunc( const SELECTION& aSelection ); + static bool sameLayerFunc( const SELECTION& aSelection ); + ///> Helper function used by HasType() static bool hasTypeFunc( const SELECTION& aSelection, KICAD_T aType );