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.

221 lines
5.7 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-2021 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 <eda_item.h>
  25. #include <trace_helpers.h>
  26. #include <sch_item.h>
  27. #include <sch_screen.h>
  28. #include <sch_sheet_path.h>
  29. #include <sch_draw_panel.h>
  30. #include <sch_edit_frame.h>
  31. #include <sch_symbol.h>
  32. #include <sch_sheet.h>
  33. #include <sch_pin.h>
  34. #include <schematic.h>
  35. #include <general.h>
  36. #include <netclass.h>
  37. #include <project/project_file.h>
  38. #include <project/net_settings.h>
  39. /* Constructor and destructor for SCH_ITEM */
  40. /* They are not inline because this creates problems with gcc at linking time
  41. * in debug mode
  42. */
  43. SCH_ITEM::SCH_ITEM( EDA_ITEM* aParent, KICAD_T aType ) :
  44. EDA_ITEM( aParent, aType )
  45. {
  46. m_layer = LAYER_WIRE; // It's only a default, in fact
  47. m_fieldsAutoplaced = FIELDS_AUTOPLACED_NO;
  48. m_connectivity_dirty = true;
  49. }
  50. SCH_ITEM::SCH_ITEM( const SCH_ITEM& aItem ) :
  51. EDA_ITEM( aItem )
  52. {
  53. m_layer = aItem.m_layer;
  54. m_fieldsAutoplaced = aItem.m_fieldsAutoplaced;
  55. m_connectivity_dirty = true;
  56. }
  57. SCH_ITEM::~SCH_ITEM()
  58. {
  59. // Do not let the connections container go out of scope with any objects or they
  60. // will be deleted by the container will cause the Eeschema to crash. These objects
  61. // are owned by the sheet object container.
  62. if( !m_connections.empty() )
  63. m_connections.clear();
  64. for( const auto& it : m_connection_map )
  65. delete it.second;
  66. }
  67. SCH_ITEM* SCH_ITEM::Duplicate( bool doClone ) const
  68. {
  69. SCH_ITEM* newItem = (SCH_ITEM*) Clone();
  70. if( !doClone )
  71. const_cast<KIID&>( newItem->m_Uuid ) = KIID();
  72. newItem->ClearFlags( SELECTED | BRIGHTENED );
  73. newItem->RunOnChildren(
  74. []( SCH_ITEM* aChild )
  75. {
  76. aChild->ClearFlags( SELECTED | BRIGHTENED );
  77. } );
  78. return newItem;
  79. }
  80. SCHEMATIC* SCH_ITEM::Schematic() const
  81. {
  82. EDA_ITEM* parent = GetParent();
  83. while( parent )
  84. {
  85. if( parent->Type() == SCHEMATIC_T )
  86. return static_cast<SCHEMATIC*>( parent );
  87. else
  88. parent = parent->GetParent();
  89. }
  90. return nullptr;
  91. }
  92. void SCH_ITEM::ViewGetLayers( int aLayers[], int& aCount ) const
  93. {
  94. // Basic fallback
  95. aCount = 2;
  96. aLayers[0] = LAYER_DEVICE;
  97. aLayers[1] = LAYER_SELECTION_SHADOWS;
  98. }
  99. bool SCH_ITEM::IsConnected( const wxPoint& aPosition ) const
  100. {
  101. if(( m_flags & STRUCT_DELETED ) || ( m_flags & SKIP_STRUCT ) )
  102. return false;
  103. return doIsConnected( aPosition );
  104. }
  105. SCH_CONNECTION* SCH_ITEM::Connection( const SCH_SHEET_PATH* aSheet ) const
  106. {
  107. if( !aSheet )
  108. aSheet = &Schematic()->CurrentSheet();
  109. auto it = m_connection_map.find( *aSheet );
  110. if( it == m_connection_map.end() )
  111. return nullptr;
  112. else
  113. return it->second;
  114. }
  115. NETCLASSPTR SCH_ITEM::NetClass( const SCH_SHEET_PATH* aSheet ) const
  116. {
  117. if( m_connection_map.size() )
  118. {
  119. SCH_CONNECTION* connection = Connection( aSheet );
  120. if( connection )
  121. {
  122. NET_SETTINGS& netSettings = Schematic()->Prj().GetProjectFile().NetSettings();
  123. const wxString& netclassName = netSettings.GetNetclassName( connection->Name() );
  124. return netSettings.m_NetClasses.Find( netclassName );
  125. }
  126. }
  127. return nullptr;
  128. }
  129. SCH_ITEM_SET& SCH_ITEM::ConnectedItems( const SCH_SHEET_PATH& aSheet )
  130. {
  131. return m_connected_items[ aSheet ];
  132. }
  133. void SCH_ITEM::AddConnectionTo( const SCH_SHEET_PATH& aSheet, SCH_ITEM* aItem )
  134. {
  135. m_connected_items[ aSheet ].insert( aItem );
  136. }
  137. SCH_CONNECTION* SCH_ITEM::InitializeConnection( const SCH_SHEET_PATH& aSheet,
  138. CONNECTION_GRAPH* aGraph )
  139. {
  140. SCH_CONNECTION* connection = Connection( &aSheet );
  141. if( connection )
  142. {
  143. connection->Reset();
  144. }
  145. else
  146. {
  147. connection = new SCH_CONNECTION( this );
  148. m_connection_map.insert( std::make_pair( aSheet, connection ) );
  149. }
  150. connection->SetGraph( aGraph );
  151. connection->SetSheet( aSheet );
  152. return connection;
  153. }
  154. void SCH_ITEM::SwapData( SCH_ITEM* aItem )
  155. {
  156. wxFAIL_MSG( wxT( "SwapData() method not implemented for class " ) + GetClass() );
  157. }
  158. bool SCH_ITEM::operator < ( const SCH_ITEM& aItem ) const
  159. {
  160. if( Type() != aItem.Type() )
  161. return Type() < aItem.Type();
  162. if( GetPosition().x != aItem.GetPosition().x )
  163. return GetPosition().x < aItem.GetPosition().x;
  164. if( GetPosition().y != aItem.GetPosition().y )
  165. return GetPosition().y < aItem.GetPosition().y;
  166. return m_Uuid < aItem.m_Uuid;
  167. }
  168. void SCH_ITEM::Plot( PLOTTER* aPlotter )
  169. {
  170. wxFAIL_MSG( wxT( "Plot() method not implemented for class " ) + GetClass() );
  171. }