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.

390 lines
10 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-2023 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 <sch_connection.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 <trace_helpers.h>
  36. #include <general.h>
  37. #include <netclass.h>
  38. #include <project/project_file.h>
  39. #include <project/net_settings.h>
  40. // Rendering fonts is expensive (particularly when using outline fonts). At small effective
  41. // sizes (ie: zoomed out) the visual differences between outline and/or stroke fonts and the
  42. // bitmap font becomes immaterial, and there's often more to draw when zoomed out so the
  43. // performance gain becomes more significant.
  44. #define BITMAP_FONT_SIZE_THRESHOLD 3
  45. /* Constructor and destructor for SCH_ITEM */
  46. /* They are not inline because this creates problems with gcc at linking time in debug mode */
  47. SCH_ITEM::SCH_ITEM( EDA_ITEM* aParent, KICAD_T aType ) :
  48. EDA_ITEM( aParent, aType )
  49. {
  50. m_layer = LAYER_WIRE; // It's only a default, in fact
  51. m_fieldsAutoplaced = FIELDS_AUTOPLACED_NO;
  52. m_connectivity_dirty = false; // Item is unconnected until it is placed, so it's clean
  53. }
  54. SCH_ITEM::SCH_ITEM( const SCH_ITEM& aItem ) :
  55. EDA_ITEM( aItem )
  56. {
  57. m_layer = aItem.m_layer;
  58. m_fieldsAutoplaced = aItem.m_fieldsAutoplaced;
  59. m_connectivity_dirty = aItem.m_connectivity_dirty;
  60. }
  61. SCH_ITEM& SCH_ITEM::operator=( const SCH_ITEM& aItem )
  62. {
  63. m_layer = aItem.m_layer;
  64. m_fieldsAutoplaced = aItem.m_fieldsAutoplaced;
  65. m_connectivity_dirty = aItem.m_connectivity_dirty;
  66. return *this;
  67. }
  68. SCH_ITEM::~SCH_ITEM()
  69. {
  70. // Do not let the connections container go out of scope with any objects or they
  71. // will be deleted by the container will cause the Eeschema to crash. These objects
  72. // are owned by the sheet object container.
  73. if( !m_connections.empty() )
  74. m_connections.clear();
  75. for( const auto& it : m_connection_map )
  76. delete it.second;
  77. }
  78. SCH_ITEM* SCH_ITEM::Duplicate( bool doClone ) const
  79. {
  80. SCH_ITEM* newItem = (SCH_ITEM*) Clone();
  81. if( !doClone )
  82. const_cast<KIID&>( newItem->m_Uuid ) = KIID();
  83. newItem->ClearFlags( SELECTED | BRIGHTENED );
  84. newItem->RunOnChildren(
  85. []( SCH_ITEM* aChild )
  86. {
  87. aChild->ClearFlags( SELECTED | BRIGHTENED );
  88. } );
  89. return newItem;
  90. }
  91. SCHEMATIC* SCH_ITEM::Schematic() const
  92. {
  93. EDA_ITEM* parent = GetParent();
  94. while( parent )
  95. {
  96. if( parent->Type() == SCHEMATIC_T )
  97. return static_cast<SCHEMATIC*>( parent );
  98. else
  99. parent = parent->GetParent();
  100. }
  101. return nullptr;
  102. }
  103. void SCH_ITEM::ViewGetLayers( int aLayers[], int& aCount ) const
  104. {
  105. // Basic fallback
  106. aCount = 2;
  107. aLayers[0] = LAYER_DEVICE;
  108. aLayers[1] = LAYER_SELECTION_SHADOWS;
  109. }
  110. bool SCH_ITEM::IsConnected( const VECTOR2I& aPosition ) const
  111. {
  112. if(( m_flags & STRUCT_DELETED ) || ( m_flags & SKIP_STRUCT ) )
  113. return false;
  114. return doIsConnected( aPosition );
  115. }
  116. SCH_CONNECTION* SCH_ITEM::Connection( const SCH_SHEET_PATH* aSheet ) const
  117. {
  118. if( !IsConnectable() )
  119. return nullptr;
  120. if( !aSheet )
  121. aSheet = &Schematic()->CurrentSheet();
  122. auto it = m_connection_map.find( *aSheet );
  123. if( it == m_connection_map.end() )
  124. return nullptr;
  125. else
  126. return it->second;
  127. }
  128. void SCH_ITEM::SetConnectionGraph( CONNECTION_GRAPH* aGraph )
  129. {
  130. for( auto& [path, conn] : m_connection_map )
  131. {
  132. conn->SetGraph( aGraph );
  133. for( auto& member : conn->AllMembers() )
  134. member->SetGraph( aGraph );
  135. }
  136. }
  137. std::shared_ptr<NETCLASS> SCH_ITEM::GetEffectiveNetClass( const SCH_SHEET_PATH* aSheet ) const
  138. {
  139. static std::shared_ptr<NETCLASS> nullNetclass = std::make_shared<NETCLASS>( wxEmptyString );
  140. SCHEMATIC* schematic = Schematic();
  141. if( schematic )
  142. {
  143. std::shared_ptr<NET_SETTINGS>& netSettings = schematic->Prj().GetProjectFile().m_NetSettings;
  144. SCH_CONNECTION* connection = Connection( aSheet );
  145. if( connection )
  146. return netSettings->GetEffectiveNetClass( connection->Name() );
  147. else
  148. return netSettings->m_DefaultNetClass;
  149. }
  150. return nullNetclass;
  151. }
  152. SCH_ITEM_SET& SCH_ITEM::ConnectedItems( const SCH_SHEET_PATH& aSheet )
  153. {
  154. return m_connected_items[ aSheet ];
  155. }
  156. void SCH_ITEM::AddConnectionTo( const SCH_SHEET_PATH& aSheet, SCH_ITEM* aItem )
  157. {
  158. SCH_ITEM_SET& set = m_connected_items[ aSheet ];
  159. // The vector elements are small, so reserve 1k at a time to prevent re-allocations
  160. if( set.size() == set.capacity() )
  161. set.reserve( set.size() + 4096 );
  162. set.emplace_back( aItem );
  163. }
  164. SCH_CONNECTION* SCH_ITEM::InitializeConnection( const SCH_SHEET_PATH& aSheet,
  165. CONNECTION_GRAPH* aGraph )
  166. {
  167. SCH_CONNECTION* connection = Connection( &aSheet );
  168. // N.B. Do not clear the dirty connectivity flag here because we may need
  169. // to create a connection for a different sheet, and we don't want to
  170. // skip the connection creation because the flag is cleared.
  171. if( connection )
  172. {
  173. connection->Reset();
  174. }
  175. else
  176. {
  177. connection = new SCH_CONNECTION( this );
  178. m_connection_map.insert( std::make_pair( aSheet, connection ) );
  179. }
  180. connection->SetGraph( aGraph );
  181. connection->SetSheet( aSheet );
  182. return connection;
  183. }
  184. SCH_CONNECTION* SCH_ITEM::GetOrInitConnection( const SCH_SHEET_PATH& aSheet,
  185. CONNECTION_GRAPH* aGraph )
  186. {
  187. if( !IsConnectable() )
  188. return nullptr;
  189. SCH_CONNECTION* connection = Connection( &aSheet );
  190. if( connection )
  191. return connection;
  192. else
  193. return InitializeConnection( aSheet, aGraph );
  194. }
  195. const wxString& SCH_ITEM::GetCachedDriverName() const
  196. {
  197. static wxString s_empty;
  198. return s_empty;
  199. }
  200. void SCH_ITEM::SwapData( SCH_ITEM* aItem )
  201. {
  202. UNIMPLEMENTED_FOR( GetClass() );
  203. }
  204. void SCH_ITEM::SwapFlags( SCH_ITEM* aItem )
  205. {
  206. EDA_ITEM_FLAGS editFlags = GetEditFlags();
  207. EDA_ITEM_FLAGS tempFlags = GetTempFlags();
  208. EDA_ITEM_FLAGS aItem_editFlags = aItem->GetEditFlags();
  209. EDA_ITEM_FLAGS aItem_tempFlags = aItem->GetTempFlags();
  210. std::swap( m_flags, aItem->m_flags );
  211. ClearEditFlags();
  212. SetFlags( editFlags );
  213. ClearTempFlags();
  214. SetFlags( tempFlags );
  215. aItem->ClearEditFlags();
  216. aItem->SetFlags( aItem_editFlags );
  217. aItem->ClearTempFlags();
  218. aItem->SetFlags( aItem_tempFlags );
  219. }
  220. void SCH_ITEM::ClearCaches()
  221. {
  222. auto clearTextCaches =
  223. []( SCH_ITEM* aItem )
  224. {
  225. EDA_TEXT* text = dynamic_cast<EDA_TEXT*>( aItem );
  226. if( text )
  227. {
  228. text->ClearBoundingBoxCache();
  229. text->ClearRenderCache();
  230. }
  231. };
  232. clearTextCaches( this );
  233. RunOnChildren( clearTextCaches );
  234. }
  235. bool SCH_ITEM::operator < ( const SCH_ITEM& aItem ) const
  236. {
  237. if( Type() != aItem.Type() )
  238. return Type() < aItem.Type();
  239. if( GetPosition().x != aItem.GetPosition().x )
  240. return GetPosition().x < aItem.GetPosition().x;
  241. if( GetPosition().y != aItem.GetPosition().y )
  242. return GetPosition().y < aItem.GetPosition().y;
  243. return m_Uuid < aItem.m_Uuid;
  244. }
  245. const wxString& SCH_ITEM::GetDefaultFont() const
  246. {
  247. EESCHEMA_SETTINGS* cfg = Pgm().GetSettingsManager().GetAppSettings<EESCHEMA_SETTINGS>();
  248. return cfg->m_Appearance.default_font;
  249. }
  250. const KIFONT::METRICS& SCH_ITEM::GetFontMetrics() const
  251. {
  252. if( SCHEMATIC* schematic = Schematic() )
  253. return schematic->Settings().m_FontMetrics;
  254. return KIFONT::METRICS::Default();
  255. }
  256. bool SCH_ITEM::RenderAsBitmap( double aWorldScale ) const
  257. {
  258. if( IsHypertext() )
  259. return false;
  260. if( const EDA_TEXT* text = dynamic_cast<const EDA_TEXT*>( this ) )
  261. return text->GetTextHeight() * aWorldScale < BITMAP_FONT_SIZE_THRESHOLD;
  262. return false;
  263. }
  264. void SCH_ITEM::Plot( PLOTTER* aPlotter, bool aBackground,
  265. const SCH_PLOT_SETTINGS& aPlotSettings ) const
  266. {
  267. wxFAIL_MSG( wxT( "Plot() method not implemented for class " ) + GetClass() );
  268. }
  269. static struct SCH_ITEM_DESC
  270. {
  271. SCH_ITEM_DESC()
  272. {
  273. #ifdef NOTYET
  274. ENUM_MAP<SCH_LAYER_ID>& layerEnum = ENUM_MAP<SCH_LAYER_ID>::Instance();
  275. if( layerEnum.Choices().GetCount() == 0 )
  276. {
  277. layerEnum.Undefined( SCH_LAYER_ID_END );
  278. for( SCH_LAYER_ID value : magic_enum::enum_values<SCH_LAYER_ID>() )
  279. layerEnum.Map( value, LayerName( value ) );
  280. }
  281. #endif
  282. PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
  283. REGISTER_TYPE( SCH_ITEM );
  284. propMgr.InheritsAfter( TYPE_HASH( SCH_ITEM ), TYPE_HASH( EDA_ITEM ) );
  285. // Not sure if this will ever be needed
  286. // propMgr.AddProperty( new PROPERTY_ENUM<SCH_ITEM, SCH_LAYER_ID>( _HKI( "Layer" ),
  287. // &SCH_ITEM::SetLayer, &SCH_ITEM::GetLayer ) )
  288. // .SetIsHiddenFromPropertiesManager();
  289. // Not yet functional in UI
  290. // propMgr.AddProperty( new PROPERTY<SCH_ITEM, bool>( _HKI( "Locked" ),
  291. // &SCH_ITEM::SetLocked, &SCH_ITEM::IsLocked ) );
  292. }
  293. } _SCH_ITEM_DESC;
  294. IMPLEMENT_ENUM_TO_WXANY( SCH_LAYER_ID )