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
8.2 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 <pgm_base.h>
  25. #include <settings/settings_manager.h>
  26. #include <eeschema_settings.h>
  27. #include <eda_item.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 <schematic.h>
  35. #include <general.h>
  36. #include <netclass.h>
  37. #include <project/project_file.h>
  38. #include <project/net_settings.h>
  39. // Rendering fonts is expensive (particularly when using outline fonts). At small effective
  40. // sizes (ie: zoomed out) the visual differences between outline and/or stroke fonts and the
  41. // bitmap font becomes immaterial, and there's often more to draw when zoomed out so the
  42. // performance gain becomes more significant.
  43. #define BITMAP_FONT_SIZE_THRESHOLD 3
  44. /* Constructor and destructor for SCH_ITEM */
  45. /* They are not inline because this creates problems with gcc at linking time in debug mode */
  46. SCH_ITEM::SCH_ITEM( EDA_ITEM* aParent, KICAD_T aType ) :
  47. EDA_ITEM( aParent, aType )
  48. {
  49. m_layer = LAYER_WIRE; // It's only a default, in fact
  50. m_fieldsAutoplaced = FIELDS_AUTOPLACED_NO;
  51. m_connectivity_dirty = false; // Item is unconnected until it is placed, so it's clean
  52. }
  53. SCH_ITEM::SCH_ITEM( const SCH_ITEM& aItem ) :
  54. EDA_ITEM( aItem )
  55. {
  56. m_layer = aItem.m_layer;
  57. m_fieldsAutoplaced = aItem.m_fieldsAutoplaced;
  58. m_connectivity_dirty = aItem.m_connectivity_dirty;
  59. }
  60. SCH_ITEM& SCH_ITEM::operator=( const SCH_ITEM& aItem )
  61. {
  62. m_layer = aItem.m_layer;
  63. m_fieldsAutoplaced = aItem.m_fieldsAutoplaced;
  64. m_connectivity_dirty = aItem.m_connectivity_dirty;
  65. return *this;
  66. }
  67. SCH_ITEM::~SCH_ITEM()
  68. {
  69. // Do not let the connections container go out of scope with any objects or they
  70. // will be deleted by the container will cause the Eeschema to crash. These objects
  71. // are owned by the sheet object container.
  72. if( !m_connections.empty() )
  73. m_connections.clear();
  74. for( const auto& it : m_connection_map )
  75. delete it.second;
  76. }
  77. SCH_ITEM* SCH_ITEM::Duplicate( bool doClone ) const
  78. {
  79. SCH_ITEM* newItem = (SCH_ITEM*) Clone();
  80. if( !doClone )
  81. const_cast<KIID&>( newItem->m_Uuid ) = KIID();
  82. newItem->ClearFlags( SELECTED | BRIGHTENED );
  83. newItem->RunOnChildren(
  84. []( SCH_ITEM* aChild )
  85. {
  86. aChild->ClearFlags( SELECTED | BRIGHTENED );
  87. } );
  88. return newItem;
  89. }
  90. SCHEMATIC* SCH_ITEM::Schematic() const
  91. {
  92. EDA_ITEM* parent = GetParent();
  93. while( parent )
  94. {
  95. if( parent->Type() == SCHEMATIC_T )
  96. return static_cast<SCHEMATIC*>( parent );
  97. else
  98. parent = parent->GetParent();
  99. }
  100. return nullptr;
  101. }
  102. void SCH_ITEM::ViewGetLayers( int aLayers[], int& aCount ) const
  103. {
  104. // Basic fallback
  105. aCount = 2;
  106. aLayers[0] = LAYER_DEVICE;
  107. aLayers[1] = LAYER_SELECTION_SHADOWS;
  108. }
  109. bool SCH_ITEM::IsConnected( const VECTOR2I& aPosition ) const
  110. {
  111. if(( m_flags & STRUCT_DELETED ) || ( m_flags & SKIP_STRUCT ) )
  112. return false;
  113. return doIsConnected( aPosition );
  114. }
  115. SCH_CONNECTION* SCH_ITEM::Connection( const SCH_SHEET_PATH* aSheet ) const
  116. {
  117. if( !IsConnectable() )
  118. return nullptr;
  119. wxCHECK_MSG( !IsConnectivityDirty(), nullptr,
  120. wxT( "Shouldn't be asking for connection if connectivity is dirty!" ) );
  121. if( !aSheet )
  122. aSheet = &Schematic()->CurrentSheet();
  123. auto it = m_connection_map.find( *aSheet );
  124. if( it == m_connection_map.end() )
  125. return nullptr;
  126. else
  127. return it->second;
  128. }
  129. std::shared_ptr<NETCLASS> SCH_ITEM::GetEffectiveNetClass( const SCH_SHEET_PATH* aSheet ) const
  130. {
  131. static std::shared_ptr<NETCLASS> nullNetclass = std::make_shared<NETCLASS>( wxEmptyString );
  132. SCHEMATIC* schematic = Schematic();
  133. if( schematic )
  134. {
  135. std::shared_ptr<NET_SETTINGS>& netSettings = schematic->Prj().GetProjectFile().m_NetSettings;
  136. SCH_CONNECTION* connection = Connection( aSheet );
  137. if( connection )
  138. return netSettings->GetEffectiveNetClass( connection->Name() );
  139. else
  140. return netSettings->m_DefaultNetClass;
  141. }
  142. return nullNetclass;
  143. }
  144. SCH_ITEM_SET& SCH_ITEM::ConnectedItems( const SCH_SHEET_PATH& aSheet )
  145. {
  146. return m_connected_items[ aSheet ];
  147. }
  148. void SCH_ITEM::AddConnectionTo( const SCH_SHEET_PATH& aSheet, SCH_ITEM* aItem )
  149. {
  150. SCH_ITEM_SET& set = m_connected_items[ aSheet ];
  151. // The vector elements are small, so reserve 1k at a time to prevent re-allocations
  152. if( set.size() == set.capacity() )
  153. set.reserve( set.size() + 4096 );
  154. set.emplace_back( aItem );
  155. }
  156. SCH_CONNECTION* SCH_ITEM::InitializeConnection( const SCH_SHEET_PATH& aSheet,
  157. CONNECTION_GRAPH* aGraph )
  158. {
  159. SetConnectivityDirty( false );
  160. SCH_CONNECTION* connection = Connection( &aSheet );
  161. if( connection )
  162. {
  163. connection->Reset();
  164. }
  165. else
  166. {
  167. connection = new SCH_CONNECTION( this );
  168. m_connection_map.insert( std::make_pair( aSheet, connection ) );
  169. }
  170. connection->SetGraph( aGraph );
  171. connection->SetSheet( aSheet );
  172. return connection;
  173. }
  174. SCH_CONNECTION* SCH_ITEM::GetOrInitConnection( const SCH_SHEET_PATH& aSheet,
  175. CONNECTION_GRAPH* aGraph )
  176. {
  177. if( !IsConnectable() )
  178. return nullptr;
  179. SetConnectivityDirty( false );
  180. SCH_CONNECTION* connection = Connection( &aSheet );
  181. if( connection )
  182. return connection;
  183. else
  184. return InitializeConnection( aSheet, aGraph );
  185. }
  186. void SCH_ITEM::SwapData( SCH_ITEM* aItem )
  187. {
  188. UNIMPLEMENTED_FOR( GetClass() );
  189. }
  190. void SCH_ITEM::ClearCaches()
  191. {
  192. auto clearTextCaches =
  193. []( SCH_ITEM* aItem )
  194. {
  195. EDA_TEXT* text = dynamic_cast<EDA_TEXT*>( aItem );
  196. if( text )
  197. {
  198. text->ClearBoundingBoxCache();
  199. text->ClearRenderCache();
  200. }
  201. };
  202. clearTextCaches( this );
  203. RunOnChildren( clearTextCaches );
  204. }
  205. bool SCH_ITEM::operator < ( const SCH_ITEM& aItem ) const
  206. {
  207. if( Type() != aItem.Type() )
  208. return Type() < aItem.Type();
  209. if( GetPosition().x != aItem.GetPosition().x )
  210. return GetPosition().x < aItem.GetPosition().x;
  211. if( GetPosition().y != aItem.GetPosition().y )
  212. return GetPosition().y < aItem.GetPosition().y;
  213. return m_Uuid < aItem.m_Uuid;
  214. }
  215. const wxString& SCH_ITEM::GetDefaultFont() const
  216. {
  217. EESCHEMA_SETTINGS* cfg = Pgm().GetSettingsManager().GetAppSettings<EESCHEMA_SETTINGS>();
  218. return cfg->m_Appearance.default_font;
  219. }
  220. bool SCH_ITEM::RenderAsBitmap( double aWorldScale ) const
  221. {
  222. if( IsHypertext() )
  223. return false;
  224. if( const EDA_TEXT* text = dynamic_cast<const EDA_TEXT*>( this ) )
  225. return text->GetTextHeight() * aWorldScale < BITMAP_FONT_SIZE_THRESHOLD;
  226. return false;
  227. }
  228. void SCH_ITEM::Plot( PLOTTER* aPlotter, bool aBackground ) const
  229. {
  230. wxFAIL_MSG( wxT( "Plot() method not implemented for class " ) + GetClass() );
  231. }