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.

127 lines
3.4 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. * @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. #include "pcb_selection_conditions.h"
  25. #include "selection_tool.h"
  26. #include <board_connected_item.h>
  27. #include <functional>
  28. using namespace std::placeholders;
  29. bool PCB_SELECTION_CONDITIONS::OnlyConnectedItems( const SELECTION& aSelection )
  30. {
  31. if( aSelection.Empty() )
  32. return false;
  33. for( const auto &item : aSelection )
  34. {
  35. auto type = item->Type();
  36. if( type != PCB_PAD_T && type != PCB_VIA_T && type != PCB_TRACE_T && type != PCB_ZONE_T )
  37. return false;
  38. }
  39. return true;
  40. }
  41. SELECTION_CONDITION PCB_SELECTION_CONDITIONS::SameNet( bool aAllowUnconnected )
  42. {
  43. return std::bind( &PCB_SELECTION_CONDITIONS::sameNetFunc, _1, aAllowUnconnected );
  44. }
  45. SELECTION_CONDITION PCB_SELECTION_CONDITIONS::SameLayer()
  46. {
  47. return std::bind( &PCB_SELECTION_CONDITIONS::sameLayerFunc, _1 );
  48. }
  49. bool PCB_SELECTION_CONDITIONS::sameNetFunc( const SELECTION& aSelection, bool aAllowUnconnected )
  50. {
  51. if( aSelection.Empty() )
  52. return false;
  53. int netcode = -1; // -1 stands for 'net code is not yet determined'
  54. for( const auto& aitem : aSelection )
  55. {
  56. int current_netcode = -1;
  57. const BOARD_CONNECTED_ITEM* item =
  58. dynamic_cast<const BOARD_CONNECTED_ITEM*>( aitem );
  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. assert( current_netcode >= 0 );
  72. if( netcode < 0 )
  73. {
  74. netcode = current_netcode;
  75. if( netcode == NETINFO_LIST::UNCONNECTED && !aAllowUnconnected )
  76. return false;
  77. }
  78. else if( netcode != current_netcode )
  79. {
  80. return false;
  81. }
  82. }
  83. return true;
  84. }
  85. bool PCB_SELECTION_CONDITIONS::sameLayerFunc( const SELECTION& aSelection )
  86. {
  87. if( aSelection.Empty() )
  88. return false;
  89. LSET layerSet;
  90. layerSet.set();
  91. for( const auto& i : aSelection )
  92. {
  93. auto item = static_cast<BOARD_ITEM*>( i );
  94. layerSet &= item->GetLayerSet();
  95. if( !layerSet.any() ) // there are no common layers left
  96. return false;
  97. }
  98. return true;
  99. }