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.

194 lines
5.3 KiB

7 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2006 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.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 <fctsys.h>
  25. #include <common.h>
  26. #include <gr_basic.h>
  27. #include <base_struct.h>
  28. #include <trace_helpers.h>
  29. #include <sch_item.h>
  30. #include <sch_screen.h>
  31. #include <sch_sheet_path.h>
  32. #include <sch_draw_panel.h>
  33. #include <sch_edit_frame.h>
  34. #include <sch_component.h>
  35. #include <sch_sheet.h>
  36. #include <sch_pin.h>
  37. #include <general.h>
  38. /* Constructor and destructor for SCH_ITEM */
  39. /* They are not inline because this creates problems with gcc at linking time
  40. * in debug mode
  41. */
  42. SCH_ITEM::SCH_ITEM( EDA_ITEM* aParent, KICAD_T aType ) :
  43. EDA_ITEM( aParent, aType )
  44. {
  45. m_Layer = LAYER_WIRE; // It's only a default, in fact
  46. m_fieldsAutoplaced = FIELDS_AUTOPLACED_NO;
  47. m_connectivity_dirty = true;
  48. }
  49. SCH_ITEM::SCH_ITEM( const SCH_ITEM& aItem ) :
  50. EDA_ITEM( aItem )
  51. {
  52. m_Layer = aItem.m_Layer;
  53. m_fieldsAutoplaced = aItem.m_fieldsAutoplaced;
  54. m_connectivity_dirty = true;
  55. }
  56. SCH_ITEM::~SCH_ITEM()
  57. {
  58. // Do not let the connections container go out of scope with any objects or they
  59. // will be deleted by the container will cause the Eeschema to crash. These objects
  60. // are owned by the sheet object container.
  61. if( !m_connections.empty() )
  62. m_connections.clear();
  63. for( const auto& it : m_connection_map )
  64. delete it.second;
  65. }
  66. SCH_ITEM* SCH_ITEM::Duplicate( bool doClone ) const
  67. {
  68. SCH_ITEM* newItem = (SCH_ITEM*) Clone();
  69. if( !doClone )
  70. const_cast<KIID&>( newItem->m_Uuid ) = KIID();
  71. newItem->ClearFlags( SELECTED | HIGHLIGHTED | BRIGHTENED );
  72. if( newItem->Type() == SCH_COMPONENT_T )
  73. {
  74. SCH_COMPONENT* component = (SCH_COMPONENT*) newItem;
  75. for( SCH_PIN* pin : component->GetSchPins() )
  76. pin->ClearFlags( SELECTED | HIGHLIGHTED | BRIGHTENED );
  77. for( SCH_FIELD& field : component->GetFields() )
  78. field.ClearFlags( SELECTED | HIGHLIGHTED | BRIGHTENED );
  79. }
  80. if( newItem->Type() == SCH_SHEET_T )
  81. {
  82. SCH_SHEET* sheet = (SCH_SHEET*) newItem;
  83. for( SCH_FIELD& field : sheet->GetFields() )
  84. field.ClearFlags( SELECTED | HIGHLIGHTED | BRIGHTENED );
  85. for( SCH_SHEET_PIN* pin : sheet->GetPins() )
  86. pin->ClearFlags( SELECTED | HIGHLIGHTED | BRIGHTENED );
  87. }
  88. return newItem;
  89. }
  90. void SCH_ITEM::ViewGetLayers( int aLayers[], int& aCount ) const
  91. {
  92. // Basic fallback
  93. aCount = 2;
  94. aLayers[0] = LAYER_DEVICE;
  95. aLayers[1] = LAYER_SELECTION_SHADOWS;
  96. }
  97. bool SCH_ITEM::IsConnected( const wxPoint& aPosition ) const
  98. {
  99. if( ( m_Flags & STRUCT_DELETED ) || ( m_Flags & SKIP_STRUCT ) )
  100. return false;
  101. return doIsConnected( aPosition );
  102. }
  103. SCH_CONNECTION* SCH_ITEM::Connection( const SCH_SHEET_PATH& aSheet ) const
  104. {
  105. // Warning: the m_connection_map can be empty.
  106. if( m_connection_map.size() && m_connection_map.count( aSheet ) )
  107. return m_connection_map.at( aSheet );
  108. return nullptr;
  109. }
  110. ITEM_SET& SCH_ITEM::ConnectedItems( const SCH_SHEET_PATH& aSheet )
  111. {
  112. return m_connected_items[ aSheet ];
  113. }
  114. void SCH_ITEM::AddConnectionTo( const SCH_SHEET_PATH& aSheet, SCH_ITEM* aItem )
  115. {
  116. m_connected_items[ aSheet ].insert( aItem );
  117. }
  118. SCH_CONNECTION* SCH_ITEM::InitializeConnection( const SCH_SHEET_PATH& aSheet )
  119. {
  120. if( Connection( aSheet ) )
  121. {
  122. Connection( aSheet )->Reset();
  123. Connection( aSheet )->SetSheet( aSheet );
  124. return Connection( aSheet );
  125. }
  126. auto connection = new SCH_CONNECTION( this );
  127. connection->SetSheet( aSheet );
  128. m_connection_map.insert( std::make_pair( aSheet, connection ) );
  129. return connection;
  130. }
  131. void SCH_ITEM::SwapData( SCH_ITEM* aItem )
  132. {
  133. wxFAIL_MSG( wxT( "SwapData() method not implemented for class " ) + GetClass() );
  134. }
  135. bool SCH_ITEM::operator < ( const SCH_ITEM& aItem ) const
  136. {
  137. if( Type() != aItem.Type() )
  138. return Type() < aItem.Type();
  139. if( m_Uuid != aItem.m_Uuid )
  140. return m_Uuid < aItem.m_Uuid;
  141. if( GetPosition().x != aItem.GetPosition().x )
  142. return GetPosition().x < aItem.GetPosition().x;
  143. return GetPosition().y < aItem.GetPosition().y;
  144. }
  145. void SCH_ITEM::Plot( PLOTTER* aPlotter )
  146. {
  147. wxFAIL_MSG( wxT( "Plot() method not implemented for class " ) + GetClass() );
  148. }