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.

317 lines
7.6 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2020 Joshua Redstone redstone at gmail.com
  5. * Copyright (C) 1992-2020 KiCad Developers, see AUTHORS.txt for contributors.
  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 <bitmaps.h>
  25. #include <class_pcb_group.h>
  26. #include <confirm.h>
  27. #include <msgpanel.h>
  28. #include <view/view.h>
  29. PCB_GROUP::PCB_GROUP( BOARD* parent ) : BOARD_ITEM( (BOARD_ITEM*) parent, PCB_GROUP_T )
  30. {
  31. }
  32. bool PCB_GROUP::AddItem( BOARD_ITEM* item )
  33. {
  34. return m_items.insert( item ).second;
  35. }
  36. bool PCB_GROUP::RemoveItem( const BOARD_ITEM* item )
  37. {
  38. return m_items.erase( const_cast<BOARD_ITEM*>( item ) ) == 1;
  39. }
  40. wxPoint PCB_GROUP::GetPosition() const
  41. {
  42. return GetBoundingBox().Centre();
  43. }
  44. void PCB_GROUP::SetPosition( const wxPoint& newpos )
  45. {
  46. wxPoint delta = newpos - GetPosition();
  47. Move( delta );
  48. }
  49. EDA_ITEM* PCB_GROUP::Clone() const
  50. {
  51. // Use copy constructor to get the same uuid and other fields
  52. PCB_GROUP* newGroup = new PCB_GROUP( *this );
  53. return newGroup;
  54. }
  55. PCB_GROUP* PCB_GROUP::DeepClone() const
  56. {
  57. // Use copy constructor to get the same uuid and other fields
  58. PCB_GROUP* newGroup = new PCB_GROUP( *this );
  59. newGroup->m_items.clear();
  60. for( auto member : m_items )
  61. {
  62. if( member->Type() == PCB_GROUP_T )
  63. {
  64. newGroup->AddItem( static_cast<PCB_GROUP*>( member )->DeepClone() );
  65. }
  66. else
  67. {
  68. newGroup->AddItem( static_cast<BOARD_ITEM*>( member->Clone() ) );
  69. }
  70. }
  71. return newGroup;
  72. }
  73. PCB_GROUP* PCB_GROUP::DeepDuplicate() const
  74. {
  75. PCB_GROUP* newGroup = static_cast<PCB_GROUP*>( this->Duplicate() );
  76. newGroup->m_items.clear();
  77. for( auto member : m_items )
  78. {
  79. if( member->Type() == PCB_GROUP_T )
  80. {
  81. newGroup->AddItem( static_cast<PCB_GROUP*>( member )->DeepDuplicate() );
  82. }
  83. else
  84. {
  85. newGroup->AddItem( static_cast<BOARD_ITEM*>( member->Duplicate() ) );
  86. }
  87. }
  88. return newGroup;
  89. }
  90. void PCB_GROUP::SwapData( BOARD_ITEM* aImage )
  91. {
  92. assert( aImage->Type() == PCB_GROUP_T );
  93. std::swap( *( (PCB_GROUP*) this ), *( (PCB_GROUP*) aImage ) );
  94. }
  95. bool PCB_GROUP::HitTest( const wxPoint& aPosition, int aAccuracy ) const
  96. {
  97. EDA_RECT rect = GetBoundingBox();
  98. return rect.Inflate( aAccuracy ).Contains( aPosition );
  99. }
  100. bool PCB_GROUP::HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy ) const
  101. {
  102. EDA_RECT arect = aRect;
  103. arect.Inflate( aAccuracy );
  104. EDA_RECT bbox = GetBoundingBox();
  105. if( aContained )
  106. return arect.Contains( bbox );
  107. else
  108. {
  109. // If the rect does not intersect the bounding box, skip any tests
  110. if( !aRect.Intersects( bbox ) )
  111. return false;
  112. for( BOARD_ITEM* member : m_items )
  113. {
  114. if( member->HitTest( arect, false, 0 ) )
  115. return true;
  116. }
  117. // No items were hit
  118. return false;
  119. }
  120. }
  121. const EDA_RECT PCB_GROUP::GetBoundingBox() const
  122. {
  123. EDA_RECT area;
  124. bool isFirst = true;
  125. for( BOARD_ITEM* item : m_items )
  126. {
  127. if( isFirst )
  128. {
  129. area = item->GetBoundingBox();
  130. isFirst = false;
  131. }
  132. else
  133. {
  134. area.Merge( item->GetBoundingBox() );
  135. }
  136. }
  137. area.Inflate( Millimeter2iu( 0.25 ) ); // Give a min size to the area
  138. return area;
  139. }
  140. SEARCH_RESULT PCB_GROUP::Visit( INSPECTOR inspector, void* testData, const KICAD_T scanTypes[] )
  141. {
  142. for( const KICAD_T* stype = scanTypes; *stype != EOT; ++stype )
  143. {
  144. // If caller wants to inspect my type
  145. if( *stype == Type() )
  146. {
  147. if( SEARCH_RESULT::QUIT == inspector( this, testData ) )
  148. return SEARCH_RESULT::QUIT;
  149. }
  150. }
  151. return SEARCH_RESULT::CONTINUE;
  152. }
  153. LSET PCB_GROUP::GetLayerSet() const
  154. {
  155. LSET aSet;
  156. for( BOARD_ITEM* item : m_items )
  157. {
  158. aSet |= item->GetLayerSet();
  159. }
  160. return aSet;
  161. }
  162. void PCB_GROUP::ViewGetLayers( int aLayers[], int& aCount ) const
  163. {
  164. // What layer to put bounding box on? change in class_pcb_group.cpp
  165. std::unordered_set<int> layers = { LAYER_ANCHOR }; // for bounding box
  166. for( BOARD_ITEM* item : m_items )
  167. {
  168. int member_layers[KIGFX::VIEW::VIEW_MAX_LAYERS], member_layers_count;
  169. item->ViewGetLayers( member_layers, member_layers_count );
  170. for( int i = 0; i < member_layers_count; i++ )
  171. layers.insert( member_layers[i] );
  172. }
  173. aCount = layers.size();
  174. int i = 0;
  175. for( int layer : layers )
  176. aLayers[i++] = layer;
  177. }
  178. unsigned int PCB_GROUP::ViewGetLOD( int aLayer, KIGFX::VIEW* aView ) const
  179. {
  180. if( aView->IsLayerVisible( LAYER_ANCHOR ) )
  181. return 0;
  182. return std::numeric_limits<unsigned int>::max();
  183. }
  184. void PCB_GROUP::Move( const wxPoint& aMoveVector )
  185. {
  186. for( auto member : m_items )
  187. {
  188. member->Move( aMoveVector );
  189. }
  190. }
  191. void PCB_GROUP::Rotate( const wxPoint& aRotCentre, double aAngle )
  192. {
  193. for( BOARD_ITEM* item : m_items )
  194. {
  195. item->Rotate( aRotCentre, aAngle );
  196. }
  197. }
  198. void PCB_GROUP::Flip( const wxPoint& aCentre, bool aFlipLeftRight )
  199. {
  200. for( BOARD_ITEM* item : m_items )
  201. {
  202. item->Flip( aCentre, aFlipLeftRight );
  203. }
  204. }
  205. wxString PCB_GROUP::GetSelectMenuText( EDA_UNITS aUnits ) const
  206. {
  207. if( m_name.empty() )
  208. {
  209. return wxString::Format( _( "Anonymous group %s with %ld members" ),
  210. m_Uuid.AsString(), m_items.size() );
  211. }
  212. return wxString::Format( _( "Group \"%s\" with %ld members" ), m_name, m_items.size() );
  213. }
  214. BITMAP_DEF PCB_GROUP::GetMenuImage() const
  215. {
  216. return module_xpm;
  217. }
  218. void PCB_GROUP::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
  219. {
  220. aList.emplace_back( _( "Group" ), m_name.empty() ? _( "Anonymous" ) :
  221. wxString::Format( "\"%s\"", m_name ), DARKCYAN );
  222. aList.emplace_back( _( "Members" ), wxString::Format( "%ld", m_items.size() ), BROWN );
  223. }
  224. void PCB_GROUP::RunOnChildren( const std::function<void( BOARD_ITEM* )>& aFunction )
  225. {
  226. try
  227. {
  228. for( BOARD_ITEM* item : m_items )
  229. aFunction( item );
  230. }
  231. catch( std::bad_function_call& )
  232. {
  233. DisplayError( NULL, wxT( "Error running PCB_GROUP::RunOnChildren" ) );
  234. }
  235. }
  236. void PCB_GROUP::RunOnDescendants( const std::function<void( BOARD_ITEM* )>& aFunction )
  237. {
  238. try
  239. {
  240. for( BOARD_ITEM* item : m_items )
  241. {
  242. aFunction( item );
  243. if( item->Type() == PCB_GROUP_T )
  244. static_cast<PCB_GROUP*>( item )->RunOnDescendants( aFunction );
  245. }
  246. }
  247. catch( std::bad_function_call& )
  248. {
  249. DisplayError( NULL, wxT( "Error running PCB_GROUP::RunOnDescendants" ) );
  250. }
  251. }