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.

439 lines
12 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you may find one here:
  18. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. * or you may search the http://www.gnu.org website for the version 2 license,
  20. * or you may write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. #include <bitmaps.h>
  24. #include <eda_draw_frame.h>
  25. #include <eda_group.h>
  26. #include <geometry/shape_compound.h>
  27. #include <sch_item.h>
  28. #include <sch_group.h>
  29. #include <sch_screen.h>
  30. #include <sch_symbol.h>
  31. #include <symbol.h>
  32. #include <confirm.h>
  33. #include <widgets/msgpanel.h>
  34. #include <view/view.h>
  35. #include <wx/debug.h>
  36. SCH_GROUP::SCH_GROUP() : SCH_ITEM( nullptr, SCH_GROUP_T )
  37. {
  38. }
  39. SCH_GROUP::SCH_GROUP( SCH_ITEM* aParent ) : SCH_ITEM( aParent, SCH_GROUP_T )
  40. {
  41. }
  42. SCH_GROUP::SCH_GROUP( SCH_SCREEN* aParent ) : SCH_ITEM( aParent, SCH_GROUP_T )
  43. {
  44. }
  45. std::unordered_set<SCH_ITEM*> SCH_GROUP::GetSchItems() const
  46. {
  47. std::unordered_set<SCH_ITEM*> items;
  48. for( EDA_ITEM* item : m_items )
  49. {
  50. if( item->IsSCH_ITEM() )
  51. items.insert( static_cast<SCH_ITEM*>( item ) );
  52. }
  53. return items;
  54. }
  55. /*
  56. * @return if not in the symbol editor and aItem is in a symbol, returns the
  57. * symbol's parent group. Otherwise, returns the aItem's parent group.
  58. */
  59. EDA_GROUP* getClosestGroup( SCH_ITEM* aItem, bool isSymbolEditor )
  60. {
  61. if( !isSymbolEditor && aItem->GetParent() && aItem->GetParent()->Type() == SCH_SYMBOL_T )
  62. return static_cast<SCH_SYMBOL*>( aItem->GetParent() )->GetParentGroup();
  63. else
  64. return aItem->GetParentGroup();
  65. }
  66. /// Returns the top level group inside the aScope group, or nullptr
  67. EDA_GROUP* getNestedGroup( SCH_ITEM* aItem, EDA_GROUP* aScope, bool isSymbolEditor )
  68. {
  69. EDA_GROUP* group = getClosestGroup( aItem, isSymbolEditor );
  70. if( group == aScope )
  71. return nullptr;
  72. while( group && group->AsEdaItem()->GetParentGroup() && group->AsEdaItem()->GetParentGroup() != aScope )
  73. {
  74. if( group->AsEdaItem()->GetParent()->Type() == LIB_SYMBOL_T && isSymbolEditor )
  75. break;
  76. group = group->AsEdaItem()->GetParentGroup();
  77. }
  78. return group;
  79. }
  80. EDA_GROUP* SCH_GROUP::TopLevelGroup( SCH_ITEM* aItem, EDA_GROUP* aScope, bool isSymbolEditor )
  81. {
  82. return getNestedGroup( aItem, aScope, isSymbolEditor );
  83. }
  84. bool SCH_GROUP::WithinScope( SCH_ITEM* aItem, SCH_GROUP* aScope, bool isSymbolEditor )
  85. {
  86. EDA_GROUP* group = getClosestGroup( aItem, isSymbolEditor );
  87. if( group && group == aScope )
  88. return true;
  89. EDA_GROUP* nested = getNestedGroup( aItem, aScope, isSymbolEditor );
  90. return nested && nested->AsEdaItem()->GetParentGroup() && nested->AsEdaItem()->GetParentGroup() == aScope;
  91. }
  92. VECTOR2I SCH_GROUP::GetPosition() const
  93. {
  94. return GetBoundingBox().Centre();
  95. }
  96. void SCH_GROUP::SetPosition( const VECTOR2I& aNewpos )
  97. {
  98. VECTOR2I delta = aNewpos - GetPosition();
  99. Move( delta );
  100. }
  101. EDA_ITEM* SCH_GROUP::Clone() const
  102. {
  103. // Use copy constructor to get the same uuid and other fields
  104. SCH_GROUP* newGroup = new SCH_GROUP( *this );
  105. return newGroup;
  106. }
  107. SCH_GROUP* SCH_GROUP::DeepClone() const
  108. {
  109. // Use copy constructor to get the same uuid and other fields
  110. SCH_GROUP* newGroup = new SCH_GROUP( *this );
  111. newGroup->m_items.clear();
  112. for( EDA_ITEM* member : m_items )
  113. {
  114. if( member->Type() == SCH_GROUP_T )
  115. newGroup->AddItem( static_cast<SCH_GROUP*>( member )->DeepClone() );
  116. else
  117. newGroup->AddItem( static_cast<SCH_ITEM*>( member->Clone() ) );
  118. }
  119. return newGroup;
  120. }
  121. SCH_GROUP* SCH_GROUP::DeepDuplicate( bool addToParentGroup, SCH_COMMIT* aCommit ) const
  122. {
  123. SCH_GROUP* newGroup = static_cast<SCH_GROUP*>( Duplicate( addToParentGroup, aCommit ) );
  124. newGroup->m_items.clear();
  125. for( EDA_ITEM* member : m_items )
  126. {
  127. if( member->Type() == SCH_GROUP_T )
  128. newGroup->AddItem( static_cast<SCH_GROUP*>( member )->DeepDuplicate( IGNORE_PARENT_GROUP ) );
  129. else
  130. newGroup->AddItem( static_cast<SCH_ITEM*>( member )->Duplicate( IGNORE_PARENT_GROUP ) );
  131. }
  132. return newGroup;
  133. }
  134. void SCH_GROUP::swapData( SCH_ITEM* aImage )
  135. {
  136. assert( aImage->Type() == SCH_GROUP_T );
  137. SCH_GROUP* image = static_cast<SCH_GROUP*>( aImage );
  138. std::swap( m_items, image->m_items );
  139. std::swap( m_name, image->m_name );
  140. std::swap( m_designBlockLibId, image->m_designBlockLibId );
  141. }
  142. bool SCH_GROUP::HitTest( const VECTOR2I& aPosition, int aAccuracy ) const
  143. {
  144. // Groups are selected by promoting a selection of one of their children
  145. return false;
  146. }
  147. bool SCH_GROUP::HitTest( const BOX2I& aRect, bool aContained, int aAccuracy ) const
  148. {
  149. // Groups are selected by promoting a selection of one of their children
  150. return false;
  151. }
  152. const BOX2I SCH_GROUP::GetBoundingBox() const
  153. {
  154. BOX2I bbox;
  155. for( EDA_ITEM* item : m_items )
  156. {
  157. if( item->Type() == SCH_SYMBOL_T || item->Type() == LIB_SYMBOL_T )
  158. bbox.Merge( static_cast<SYMBOL*>( item )->GetBoundingBox() );
  159. else
  160. bbox.Merge( item->GetBoundingBox() );
  161. }
  162. bbox.Inflate( schIUScale.MilsToIU( 10 ) );
  163. return bbox;
  164. }
  165. INSPECT_RESULT SCH_GROUP::Visit( INSPECTOR aInspector, void* aTestData,
  166. const std::vector<KICAD_T>& aScanTypes )
  167. {
  168. for( KICAD_T scanType : aScanTypes )
  169. {
  170. if( scanType == Type() )
  171. {
  172. if( INSPECT_RESULT::QUIT == aInspector( this, aTestData ) )
  173. return INSPECT_RESULT::QUIT;
  174. }
  175. }
  176. return INSPECT_RESULT::CONTINUE;
  177. }
  178. std::vector<int> SCH_GROUP::ViewGetLayers() const
  179. {
  180. return { LAYER_SCHEMATIC_ANCHOR };
  181. }
  182. double SCH_GROUP::ViewGetLOD( int aLayer, const KIGFX::VIEW* aView ) const
  183. {
  184. if( aView->IsLayerVisible( LAYER_SCHEMATIC_ANCHOR ) )
  185. return LOD_SHOW;
  186. return LOD_HIDE;
  187. }
  188. void SCH_GROUP::Move( const VECTOR2I& aMoveVector )
  189. {
  190. for( EDA_ITEM* member : m_items )
  191. {
  192. EDA_ITEM_FLAGS flags = member->GetFlags();
  193. if( member->Type() == SCH_LINE_T )
  194. member->SetFlags( STARTPOINT | ENDPOINT );
  195. static_cast<SCH_ITEM*>( member )->Move( aMoveVector );
  196. member->SetFlags( flags );
  197. }
  198. }
  199. void SCH_GROUP::Rotate( const VECTOR2I& aCenter, bool aRotateCCW )
  200. {
  201. for( EDA_ITEM* member : m_items )
  202. {
  203. EDA_ITEM_FLAGS flags = member->GetFlags();
  204. if( member->Type() == SCH_LINE_T )
  205. member->SetFlags( STARTPOINT | ENDPOINT );
  206. static_cast<SCH_ITEM*>( member )->Rotate( aCenter, aRotateCCW );
  207. member->SetFlags( flags );
  208. }
  209. }
  210. void SCH_GROUP::MirrorHorizontally( int aCenter )
  211. {
  212. for( EDA_ITEM* member : m_items )
  213. {
  214. EDA_ITEM_FLAGS flags = member->GetFlags();
  215. if( member->Type() == SCH_LINE_T )
  216. member->SetFlags( STARTPOINT | ENDPOINT );
  217. static_cast<SCH_ITEM*>( member )->MirrorHorizontally( aCenter );
  218. member->SetFlags( flags );
  219. }
  220. }
  221. void SCH_GROUP::MirrorVertically( int aCenter )
  222. {
  223. for( EDA_ITEM* member : m_items )
  224. {
  225. EDA_ITEM_FLAGS flags = member->GetFlags();
  226. if( member->Type() == SCH_LINE_T )
  227. member->SetFlags( STARTPOINT | ENDPOINT );
  228. static_cast<SCH_ITEM*>( member )->MirrorVertically( aCenter );
  229. member->SetFlags( flags );
  230. }
  231. }
  232. void SCH_GROUP::Plot( PLOTTER* aPlotter, bool aBackground, const SCH_PLOT_OPTS& aPlotOpts,
  233. int aUnit, int aBodyStyle, const VECTOR2I& aOffset, bool aDimmed )
  234. {
  235. // TODO: should we plot the name & border of named groups?
  236. }
  237. wxString SCH_GROUP::GetItemDescription( UNITS_PROVIDER* aUnitsProvider, bool aFull ) const
  238. {
  239. if( m_name.empty() )
  240. return wxString::Format( _( "Unnamed Group, %zu members" ), m_items.size() );
  241. else
  242. return wxString::Format( _( "Group '%s', %zu members" ), m_name, m_items.size() );
  243. }
  244. BITMAPS SCH_GROUP::GetMenuImage() const
  245. {
  246. return BITMAPS::module;
  247. }
  248. void SCH_GROUP::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
  249. {
  250. aList.emplace_back( _( "Group" ), m_name.empty() ? _( "<unnamed>" ) : m_name );
  251. aList.emplace_back( _( "Members" ), wxString::Format( wxT( "%zu" ), m_items.size() ) );
  252. }
  253. bool SCH_GROUP::Matches( const EDA_SEARCH_DATA& aSearchData, void* aAuxData ) const
  254. {
  255. return EDA_ITEM::Matches( UnescapeString( GetName() ), aSearchData );
  256. }
  257. void SCH_GROUP::RunOnChildren( const std::function<void( SCH_ITEM* )>& aFunction, RECURSE_MODE aMode )
  258. {
  259. try
  260. {
  261. for( EDA_ITEM* item : m_items )
  262. {
  263. aFunction( static_cast<SCH_ITEM*>( item ) );
  264. if( item->Type() == SCH_GROUP_T && aMode == RECURSE_MODE::RECURSE )
  265. static_cast<SCH_GROUP*>( item )->RunOnChildren( aFunction, RECURSE_MODE::RECURSE );
  266. }
  267. }
  268. catch( std::bad_function_call& )
  269. {
  270. wxFAIL_MSG( wxT( "Error calling function in SCH_GROUP::RunOnChildren" ) );
  271. }
  272. }
  273. bool SCH_GROUP::operator==( const SCH_ITEM& aSchItem ) const
  274. {
  275. if( aSchItem.Type() != Type() )
  276. return false;
  277. const SCH_GROUP& other = static_cast<const SCH_GROUP&>( aSchItem );
  278. return *this == other;
  279. }
  280. bool SCH_GROUP::operator==( const SCH_GROUP& aOther ) const
  281. {
  282. if( m_items.size() != aOther.m_items.size() )
  283. return false;
  284. // The items in groups are in unordered sets hashed by the pointer value, so we need to
  285. // order them by UUID (EDA_ITEM_SET) to compare
  286. EDA_ITEM_SET itemSet( m_items.begin(), m_items.end() );
  287. EDA_ITEM_SET otherItemSet( aOther.m_items.begin(), aOther.m_items.end() );
  288. for( auto it1 = itemSet.begin(), it2 = otherItemSet.begin(); it1 != itemSet.end(); ++it1, ++it2 )
  289. {
  290. // Compare UUID instead of the items themselves because we only care if the contents
  291. // of the group has changed, not which elements in the group have changed
  292. if( ( *it1 )->m_Uuid != ( *it2 )->m_Uuid )
  293. return false;
  294. }
  295. return true;
  296. }
  297. double SCH_GROUP::Similarity( const SCH_ITEM& aOther ) const
  298. {
  299. if( aOther.Type() != Type() )
  300. return 0.0;
  301. const SCH_GROUP& other = static_cast<const SCH_GROUP&>( aOther );
  302. double similarity = 0.0;
  303. for( EDA_ITEM* item : m_items )
  304. {
  305. for( EDA_ITEM* otherItem : other.m_items )
  306. {
  307. similarity += static_cast<SCH_ITEM*>( item )->Similarity( *static_cast<SCH_ITEM*>( otherItem ) );
  308. }
  309. }
  310. return similarity / m_items.size();
  311. }
  312. static struct SCH_GROUP_DESC
  313. {
  314. SCH_GROUP_DESC()
  315. {
  316. PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
  317. REGISTER_TYPE( SCH_GROUP );
  318. propMgr.AddTypeCast( new TYPE_CAST<SCH_GROUP, SCH_ITEM> );
  319. propMgr.AddTypeCast( new TYPE_CAST<SCH_GROUP, EDA_GROUP> );
  320. propMgr.InheritsAfter( TYPE_HASH( SCH_GROUP ), TYPE_HASH( SCH_ITEM ) );
  321. propMgr.InheritsAfter( TYPE_HASH( SCH_GROUP ), TYPE_HASH( EDA_GROUP ) );
  322. propMgr.Mask( TYPE_HASH( SCH_GROUP ), TYPE_HASH( SCH_ITEM ), _HKI( "Position X" ) );
  323. propMgr.Mask( TYPE_HASH( SCH_GROUP ), TYPE_HASH( SCH_ITEM ), _HKI( "Position Y" ) );
  324. const wxString groupTab = _HKI( "Group Properties" );
  325. propMgr.AddProperty(
  326. new PROPERTY<EDA_GROUP, wxString>( _HKI( "Name" ), &SCH_GROUP::SetName, &SCH_GROUP::GetName ),
  327. groupTab );
  328. }
  329. } _SCH_GROUP_DESC;