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.

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