You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
3.1 KiB

8 years ago
  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 <maciej.suminski@cern.ch>
  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. #include "pcb_selection_conditions.h"
  26. #include "pcb_selection_tool.h"
  27. #include <board_connected_item.h>
  28. #include <functional>
  29. using namespace std::placeholders;
  30. SELECTION_CONDITION PCB_SELECTION_CONDITIONS::SameNet( bool aAllowUnconnected )
  31. {
  32. return std::bind( &PCB_SELECTION_CONDITIONS::sameNetFunc, _1, aAllowUnconnected );
  33. }
  34. SELECTION_CONDITION PCB_SELECTION_CONDITIONS::SameLayer()
  35. {
  36. return std::bind( &PCB_SELECTION_CONDITIONS::sameLayerFunc, _1 );
  37. }
  38. bool PCB_SELECTION_CONDITIONS::sameNetFunc( const SELECTION& aSelection, bool aAllowUnconnected )
  39. {
  40. if( aSelection.Empty() )
  41. return false;
  42. int netcode = -1; // -1 stands for 'net code is not yet determined'
  43. for( const EDA_ITEM* aitem : aSelection )
  44. {
  45. int current_netcode = -1;
  46. const BOARD_CONNECTED_ITEM* item = dynamic_cast<const BOARD_CONNECTED_ITEM*>( aitem );
  47. if( item )
  48. {
  49. current_netcode = item->GetNetCode();
  50. }
  51. else
  52. {
  53. if( !aAllowUnconnected )
  54. return false;
  55. else
  56. // if it is not a BOARD_CONNECTED_ITEM, treat it as if there was no net assigned
  57. current_netcode = 0;
  58. }
  59. assert( current_netcode >= 0 );
  60. if( netcode < 0 )
  61. {
  62. netcode = current_netcode;
  63. if( netcode == NETINFO_LIST::UNCONNECTED && !aAllowUnconnected )
  64. return false;
  65. }
  66. else if( netcode != current_netcode )
  67. {
  68. return false;
  69. }
  70. }
  71. return true;
  72. }
  73. bool PCB_SELECTION_CONDITIONS::sameLayerFunc( const SELECTION& aSelection )
  74. {
  75. if( aSelection.Empty() )
  76. return false;
  77. LSET layerSet;
  78. layerSet.set();
  79. for( const EDA_ITEM* i : aSelection )
  80. {
  81. const BOARD_ITEM* item = static_cast<const BOARD_ITEM*>( i );
  82. layerSet &= item->GetLayerSet();
  83. if( !layerSet.any() ) // there are no common layers left
  84. return false;
  85. }
  86. return true;
  87. }