KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sch_item.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2006 Jean-Pierre Charras, [email protected]
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
25#include <pgm_base.h>
27#include <eeschema_settings.h>
28#include <eda_item.h>
29#include <sch_connection.h>
30#include <sch_item.h>
31#include <sch_screen.h>
32#include <sch_sheet_path.h>
33#include <sch_draw_panel.h>
34#include <sch_edit_frame.h>
35#include <schematic.h>
36#include <trace_helpers.h>
37#include <general.h>
38#include <netclass.h>
41
42
43// Rendering fonts is expensive (particularly when using outline fonts). At small effective
44// sizes (ie: zoomed out) the visual differences between outline and/or stroke fonts and the
45// bitmap font becomes immaterial, and there's often more to draw when zoomed out so the
46// performance gain becomes more significant.
47#define BITMAP_FONT_SIZE_THRESHOLD 3
48
49
50/* Constructor and destructor for SCH_ITEM */
51/* They are not inline because this creates problems with gcc at linking time in debug mode */
52
54 EDA_ITEM( aParent, aType )
55{
56 m_layer = LAYER_WIRE; // It's only a default, in fact
58 m_connectivity_dirty = false; // Item is unconnected until it is placed, so it's clean
59}
60
61
63 EDA_ITEM( aItem )
64{
65 m_layer = aItem.m_layer;
68}
69
70
72{
73 m_layer = aItem.m_layer;
76
77 return *this;
78}
79
80
82{
83 // Do not let the connections container go out of scope with any objects or they
84 // will be deleted by the container will cause the Eeschema to crash. These objects
85 // are owned by the sheet object container.
86 if( !m_connections.empty() )
87 m_connections.clear();
88
89 for( const auto& it : m_connection_map )
90 delete it.second;
91}
92
93
94SCH_ITEM* SCH_ITEM::Duplicate( bool doClone ) const
95{
96 SCH_ITEM* newItem = (SCH_ITEM*) Clone();
97
98 if( !doClone )
99 const_cast<KIID&>( newItem->m_Uuid ) = KIID();
100
101 newItem->ClearFlags( SELECTED | BRIGHTENED );
102
103 newItem->RunOnChildren(
104 []( SCH_ITEM* aChild )
105 {
106 aChild->ClearFlags( SELECTED | BRIGHTENED );
107 } );
108
109 return newItem;
110}
111
112
114{
115 EDA_ITEM* parent = GetParent();
116
117 while( parent )
118 {
119 if( parent->Type() == SCHEMATIC_T )
120 return static_cast<SCHEMATIC*>( parent );
121 else
122 parent = parent->GetParent();
123 }
124
125 return nullptr;
126}
127
128
129void SCH_ITEM::ViewGetLayers( int aLayers[], int& aCount ) const
130{
131 // Basic fallback
132 aCount = 2;
133 aLayers[0] = LAYER_DEVICE;
134 aLayers[1] = LAYER_SELECTION_SHADOWS;
135}
136
137
138bool SCH_ITEM::IsConnected( const VECTOR2I& aPosition ) const
139{
140 if(( m_flags & STRUCT_DELETED ) || ( m_flags & SKIP_STRUCT ) )
141 return false;
142
143 return doIsConnected( aPosition );
144}
145
146
148{
149 if( !IsConnectable() )
150 return nullptr;
151
152 wxCHECK_MSG( !IsConnectivityDirty(), nullptr,
153 wxT( "Shouldn't be asking for connection if connectivity is dirty!" ) );
154
155 if( !aSheet )
156 aSheet = &Schematic()->CurrentSheet();
157
158 auto it = m_connection_map.find( *aSheet );
159
160 if( it == m_connection_map.end() )
161 return nullptr;
162 else
163 return it->second;
164}
165
166
168{
169 for( auto& [path, conn] : m_connection_map )
170 conn->SetGraph( aGraph );
171}
172
173
174std::shared_ptr<NETCLASS> SCH_ITEM::GetEffectiveNetClass( const SCH_SHEET_PATH* aSheet ) const
175{
176 static std::shared_ptr<NETCLASS> nullNetclass = std::make_shared<NETCLASS>( wxEmptyString );
177
178 SCHEMATIC* schematic = Schematic();
179
180 if( schematic )
181 {
182 std::shared_ptr<NET_SETTINGS>& netSettings = schematic->Prj().GetProjectFile().m_NetSettings;
183 SCH_CONNECTION* connection = Connection( aSheet );
184
185 if( connection )
186 return netSettings->GetEffectiveNetClass( connection->Name() );
187 else
188 return netSettings->m_DefaultNetClass;
189 }
190
191 return nullNetclass;
192}
193
194
196{
197 return m_connected_items[ aSheet ];
198}
199
200
202{
203 SCH_ITEM_SET& set = m_connected_items[ aSheet ];
204
205 // The vector elements are small, so reserve 1k at a time to prevent re-allocations
206 if( set.size() == set.capacity() )
207 set.reserve( set.size() + 4096 );
208
209 set.emplace_back( aItem );
210}
211
212
214 CONNECTION_GRAPH* aGraph )
215{
216 SetConnectivityDirty( false );
217
218 SCH_CONNECTION* connection = Connection( &aSheet );
219
220 if( connection )
221 {
222 connection->Reset();
223 }
224 else
225 {
226 connection = new SCH_CONNECTION( this );
227 m_connection_map.insert( std::make_pair( aSheet, connection ) );
228 }
229
230 connection->SetGraph( aGraph );
231 connection->SetSheet( aSheet );
232 return connection;
233}
234
235
237 CONNECTION_GRAPH* aGraph )
238{
239 if( !IsConnectable() )
240 return nullptr;
241
242 SetConnectivityDirty( false );
243
244 SCH_CONNECTION* connection = Connection( &aSheet );
245
246 if( connection )
247 return connection;
248 else
249 return InitializeConnection( aSheet, aGraph );
250}
251
252
254{
256}
257
258
260{
261 auto clearTextCaches =
262 []( SCH_ITEM* aItem )
263 {
264 EDA_TEXT* text = dynamic_cast<EDA_TEXT*>( aItem );
265
266 if( text )
267 {
268 text->ClearBoundingBoxCache();
269 text->ClearRenderCache();
270 }
271 };
272
273 clearTextCaches( this );
274
275 RunOnChildren( clearTextCaches );
276}
277
278
279bool SCH_ITEM::operator < ( const SCH_ITEM& aItem ) const
280{
281 if( Type() != aItem.Type() )
282 return Type() < aItem.Type();
283
284 if( GetPosition().x != aItem.GetPosition().x )
285 return GetPosition().x < aItem.GetPosition().x;
286
287 if( GetPosition().y != aItem.GetPosition().y )
288 return GetPosition().y < aItem.GetPosition().y;
289
290 return m_Uuid < aItem.m_Uuid;
291}
292
293
294const wxString& SCH_ITEM::GetDefaultFont() const
295{
296 EESCHEMA_SETTINGS* cfg = Pgm().GetSettingsManager().GetAppSettings<EESCHEMA_SETTINGS>();
297
298 return cfg->m_Appearance.default_font;
299}
300
301
302bool SCH_ITEM::RenderAsBitmap( double aWorldScale ) const
303{
304 if( IsHypertext() )
305 return false;
306
307 if( const EDA_TEXT* text = dynamic_cast<const EDA_TEXT*>( this ) )
308 return text->GetTextHeight() * aWorldScale < BITMAP_FONT_SIZE_THRESHOLD;
309
310 return false;
311}
312
313
314void SCH_ITEM::Plot( PLOTTER* aPlotter, bool aBackground ) const
315{
316 wxFAIL_MSG( wxT( "Plot() method not implemented for class " ) + GetClass() );
317}
Calculates the connectivity of a schematic and generates netlists.
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:85
virtual VECTOR2I GetPosition() const
Definition: eda_item.h:232
const KIID m_Uuid
Definition: eda_item.h:475
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:97
void ClearFlags(EDA_ITEM_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition: eda_item.h:125
EDA_ITEM_FLAGS m_flags
Definition: eda_item.h:480
EDA_ITEM * GetParent() const
Definition: eda_item.h:99
virtual EDA_ITEM * Clone() const
Create a duplicate of this item with linked list members set to NULL.
Definition: eda_item.cpp:81
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:72
Definition: kiid.h:48
Base plotter engine class.
Definition: plotter.h:110
std::shared_ptr< NET_SETTINGS > m_NetSettings
Net settings for this project (owned here)
Definition: project_file.h:168
virtual PROJECT_FILE & GetProjectFile() const
Definition: project.h:149
Holds all the data relating to one schematic.
Definition: schematic.h:72
SCH_SHEET_PATH & CurrentSheet() const override
Definition: schematic.h:133
PROJECT & Prj() const override
Return a reference to the project this schematic is part of.
Definition: schematic.h:87
Each graphical item can have a SCH_CONNECTION describing its logical connection (to a bus or net).
void Reset()
Clears connectivity information.
wxString Name(bool aIgnoreSheet=false) const
void SetGraph(CONNECTION_GRAPH *aGraph)
void SetSheet(SCH_SHEET_PATH aSheet)
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:147
virtual bool IsConnectable() const
Definition: sch_item.h:352
SCH_ITEM(EDA_ITEM *aParent, KICAD_T aType)
Definition: sch_item.cpp:53
SCH_ITEM & operator=(const SCH_ITEM &aPin)
Definition: sch_item.cpp:71
EDA_ITEMS m_connections
Definition: sch_item.h:498
virtual bool doIsConnected(const VECTOR2I &aPosition) const
Provide the object specific test to see if it is connected to aPosition.
Definition: sch_item.h:494
const wxString & GetDefaultFont() const
Definition: sch_item.cpp:294
virtual ~SCH_ITEM()
Definition: sch_item.cpp:81
virtual wxString GetClass() const override
Return the class name.
Definition: sch_item.h:157
SCHEMATIC * Schematic() const
Searches the item hierarchy to find a SCHEMATIC.
Definition: sch_item.cpp:113
SCH_CONNECTION * InitializeConnection(const SCH_SHEET_PATH &aPath, CONNECTION_GRAPH *aGraph)
Create a new connection object associated with this object.
Definition: sch_item.cpp:213
void AddConnectionTo(const SCH_SHEET_PATH &aPath, SCH_ITEM *aItem)
Add a connection link between this item and another.
Definition: sch_item.cpp:201
SCH_ITEM_SET & ConnectedItems(const SCH_SHEET_PATH &aPath)
Retrieve the set of items connected to this item on the given sheet.
Definition: sch_item.cpp:195
std::map< SCH_SHEET_PATH, SCH_ITEM_SET, SHEET_PATH_CMP > m_connected_items
Store pointers to other items that are connected to this one, per sheet.
Definition: sch_item.h:504
std::shared_ptr< NETCLASS > GetEffectiveNetClass(const SCH_SHEET_PATH *aSheet=nullptr) const
Definition: sch_item.cpp:174
bool m_connectivity_dirty
Definition: sch_item.h:509
virtual void ClearCaches()
Definition: sch_item.cpp:259
virtual void RunOnChildren(const std::function< void(SCH_ITEM *)> &aFunction)
Definition: sch_item.h:450
virtual void SwapData(SCH_ITEM *aItem)
Swap the internal data structures aItem with the schematic item.
Definition: sch_item.cpp:253
void SetConnectivityDirty(bool aDirty=true)
Definition: sch_item.h:418
FIELDS_AUTOPLACED m_fieldsAutoplaced
Definition: sch_item.h:499
bool RenderAsBitmap(double aWorldScale) const override
Definition: sch_item.cpp:302
bool IsConnectivityDirty() const
Definition: sch_item.h:416
void SetConnectionGraph(CONNECTION_GRAPH *aGraph)
Updates the connection graph for all connections in this item.
Definition: sch_item.cpp:167
bool IsConnected(const VECTOR2I &aPoint) const
Test the item to see if it is connected to aPoint.
Definition: sch_item.cpp:138
void ViewGetLayers(int aLayers[], int &aCount) const override
Return the layers the item is drawn on (which may be more than its "home" layer)
Definition: sch_item.cpp:129
std::unordered_map< SCH_SHEET_PATH, SCH_CONNECTION * > m_connection_map
Store connectivity information, per sheet.
Definition: sch_item.h:507
virtual bool operator<(const SCH_ITEM &aItem) const
Definition: sch_item.cpp:279
virtual bool IsHypertext() const
Allow items to support hypertext actions when hovered/clicked.
Definition: sch_item.h:242
SCH_CONNECTION * GetOrInitConnection(const SCH_SHEET_PATH &aPath, CONNECTION_GRAPH *aGraph)
Definition: sch_item.cpp:236
SCH_CONNECTION * Connection(const SCH_SHEET_PATH *aSheet=nullptr) const
Retrieve the connection associated with this object in the given sheet.
Definition: sch_item.cpp:147
virtual void Plot(PLOTTER *aPlotter, bool aBackground) const
Plot the schematic item to aPlotter.
Definition: sch_item.cpp:314
SCH_LAYER_ID m_layer
Definition: sch_item.h:497
SCH_ITEM * Duplicate(bool doClone=false) const
Routine to create a new copy of given item.
Definition: sch_item.cpp:94
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
#define BRIGHTENED
item is drawn with a bright contour
#define SELECTED
Item was manually selected by the user.
#define STRUCT_DELETED
flag indication structures to be erased
#define SKIP_STRUCT
flag indicating that the structure should be ignored
@ LAYER_DEVICE
Definition: layer_ids.h:361
@ LAYER_WIRE
Definition: layer_ids.h:348
@ LAYER_SELECTION_SHADOWS
Definition: layer_ids.h:385
#define UNIMPLEMENTED_FOR(type)
Definition: macros.h:120
see class PGM_BASE
#define BITMAP_FONT_SIZE_THRESHOLD
Definition: sch_item.cpp:47
std::vector< SCH_ITEM * > SCH_ITEM_SET
Definition: sch_item.h:136
@ FIELDS_AUTOPLACED_NO
Definition: sch_item.h:56
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:115
wxLogTrace helper definitions.
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition: typeinfo.h:78
@ SCHEMATIC_T
Definition: typeinfo.h:178