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-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
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 if( !aSheet )
153 aSheet = &Schematic()->CurrentSheet();
154
155 auto it = m_connection_map.find( *aSheet );
156
157 if( it == m_connection_map.end() )
158 return nullptr;
159 else
160 return it->second;
161}
162
163
165{
166 for( auto& [path, conn] : m_connection_map )
167 {
168 conn->SetGraph( aGraph );
169
170 for( auto& member : conn->AllMembers() )
171 member->SetGraph( aGraph );
172 }
173}
174
175
176std::shared_ptr<NETCLASS> SCH_ITEM::GetEffectiveNetClass( const SCH_SHEET_PATH* aSheet ) const
177{
178 static std::shared_ptr<NETCLASS> nullNetclass = std::make_shared<NETCLASS>( wxEmptyString );
179
180 SCHEMATIC* schematic = Schematic();
181
182 if( schematic )
183 {
184 std::shared_ptr<NET_SETTINGS>& netSettings = schematic->Prj().GetProjectFile().m_NetSettings;
185 SCH_CONNECTION* connection = Connection( aSheet );
186
187 if( connection )
188 return netSettings->GetEffectiveNetClass( connection->Name() );
189 else
190 return netSettings->m_DefaultNetClass;
191 }
192
193 return nullNetclass;
194}
195
196
198{
199 return m_connected_items[ aSheet ];
200}
201
202
204{
205 SCH_ITEM_SET& set = m_connected_items[ aSheet ];
206
207 // The vector elements are small, so reserve 1k at a time to prevent re-allocations
208 if( set.size() == set.capacity() )
209 set.reserve( set.size() + 4096 );
210
211 set.emplace_back( aItem );
212}
213
214
216 CONNECTION_GRAPH* aGraph )
217{
218 SCH_CONNECTION* connection = Connection( &aSheet );
219
220 // N.B. Do not clear the dirty connectivity flag here because we may need
221 // to create a connection for a different sheet, and we don't want to
222 // skip the connection creation because the flag is cleared.
223 if( connection )
224 {
225 connection->Reset();
226 }
227 else
228 {
229 connection = new SCH_CONNECTION( this );
230 m_connection_map.insert( std::make_pair( aSheet, connection ) );
231 }
232
233 connection->SetGraph( aGraph );
234 connection->SetSheet( aSheet );
235 return connection;
236}
237
238
240 CONNECTION_GRAPH* aGraph )
241{
242 if( !IsConnectable() )
243 return nullptr;
244
245 SCH_CONNECTION* connection = Connection( &aSheet );
246
247 if( connection )
248 return connection;
249 else
250 return InitializeConnection( aSheet, aGraph );
251}
252
253
254const wxString& SCH_ITEM::GetCachedDriverName() const
255{
256 static wxString s_empty;
257 return s_empty;
258}
259
260
262{
264}
265
266
268{
269 EDA_ITEM_FLAGS editFlags = GetEditFlags();
270 EDA_ITEM_FLAGS tempFlags = GetTempFlags();
271 EDA_ITEM_FLAGS aItem_editFlags = aItem->GetEditFlags();
272 EDA_ITEM_FLAGS aItem_tempFlags = aItem->GetTempFlags();
273
274 std::swap( m_flags, aItem->m_flags );
275
277 SetFlags( editFlags );
279 SetFlags( tempFlags );
280
281 aItem->ClearEditFlags();
282 aItem->SetFlags( aItem_editFlags );
283 aItem->ClearTempFlags();
284 aItem->SetFlags( aItem_tempFlags );
285}
286
287
289{
290 auto clearTextCaches =
291 []( SCH_ITEM* aItem )
292 {
293 EDA_TEXT* text = dynamic_cast<EDA_TEXT*>( aItem );
294
295 if( text )
296 {
297 text->ClearBoundingBoxCache();
298 text->ClearRenderCache();
299 }
300 };
301
302 clearTextCaches( this );
303
304 RunOnChildren( clearTextCaches );
305}
306
307
308bool SCH_ITEM::operator < ( const SCH_ITEM& aItem ) const
309{
310 if( Type() != aItem.Type() )
311 return Type() < aItem.Type();
312
313 if( GetPosition().x != aItem.GetPosition().x )
314 return GetPosition().x < aItem.GetPosition().x;
315
316 if( GetPosition().y != aItem.GetPosition().y )
317 return GetPosition().y < aItem.GetPosition().y;
318
319 return m_Uuid < aItem.m_Uuid;
320}
321
322
323const wxString& SCH_ITEM::GetDefaultFont() const
324{
325 EESCHEMA_SETTINGS* cfg = Pgm().GetSettingsManager().GetAppSettings<EESCHEMA_SETTINGS>();
326
327 return cfg->m_Appearance.default_font;
328}
329
330
332{
333 if( SCHEMATIC* schematic = Schematic() )
334 return schematic->Settings().m_FontMetrics;
335
337}
338
339
340bool SCH_ITEM::RenderAsBitmap( double aWorldScale ) const
341{
342 if( IsHypertext() )
343 return false;
344
345 if( const EDA_TEXT* text = dynamic_cast<const EDA_TEXT*>( this ) )
346 return text->GetTextHeight() * aWorldScale < BITMAP_FONT_SIZE_THRESHOLD;
347
348 return false;
349}
350
351
352void SCH_ITEM::Plot( PLOTTER* aPlotter, bool aBackground ) const
353{
354 wxFAIL_MSG( wxT( "Plot() method not implemented for class " ) + GetClass() );
355}
356
357
358static struct SCH_ITEM_DESC
359{
361 {
362#ifdef NOTYET
364
365 if( layerEnum.Choices().GetCount() == 0 )
366 {
367 layerEnum.Undefined( SCH_LAYER_ID_END );
368
369 for( SCH_LAYER_ID value : magic_enum::enum_values<SCH_LAYER_ID>() )
370 layerEnum.Map( value, LayerName( value ) );
371 }
372#endif
373
377
378 // Not sure if this will ever be needed
379// propMgr.AddProperty( new PROPERTY_ENUM<SCH_ITEM, SCH_LAYER_ID>( _HKI( "Layer" ),
380// &SCH_ITEM::SetLayer, &SCH_ITEM::GetLayer ) )
381// .SetIsHiddenFromPropertiesManager();
382
383 // Not yet functional in UI
384// propMgr.AddProperty( new PROPERTY<SCH_ITEM, bool>( _HKI( "Locked" ),
385// &SCH_ITEM::SetLocked, &SCH_ITEM::IsLocked ) );
386 }
388
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:239
EDA_ITEM_FLAGS GetEditFlags() const
Definition: eda_item.h:129
void SetFlags(EDA_ITEM_FLAGS aMask)
Definition: eda_item.h:123
const KIID m_Uuid
Definition: eda_item.h:482
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
void ClearTempFlags()
Definition: eda_item.h:149
EDA_ITEM_FLAGS m_flags
Definition: eda_item.h:487
EDA_ITEM_FLAGS GetTempFlags() const
Definition: eda_item.h:142
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:82
void ClearEditFlags()
Definition: eda_item.h:137
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:80
ENUM_MAP & Map(T aValue, const wxString &aName)
Definition: property.h:639
static ENUM_MAP< T > & Instance()
Definition: property.h:633
ENUM_MAP & Undefined(T aValue)
Definition: property.h:646
wxPGChoices & Choices()
Definition: property.h:682
static const METRICS & Default()
Definition: font.cpp:52
Definition: kiid.h:49
Base plotter engine class.
Definition: plotter.h:104
std::shared_ptr< NET_SETTINGS > m_NetSettings
Net settings for this project (owned here)
Definition: project_file.h:172
virtual PROJECT_FILE & GetProjectFile() const
Definition: project.h:158
Provide class metadata.Helper macro to map type hashes to names.
Definition: property_mgr.h:74
void InheritsAfter(TYPE_ID aDerived, TYPE_ID aBase)
Declare an inheritance relationship between types.
static PROPERTY_MANAGER & Instance()
Definition: property_mgr.h:76
Holds all the data relating to one schematic.
Definition: schematic.h:75
SCH_SHEET_PATH & CurrentSheet() const override
Definition: schematic.h:136
PROJECT & Prj() const override
Return a reference to the project this schematic is part of.
Definition: schematic.h:90
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:150
virtual bool IsConnectable() const
Definition: sch_item.h:362
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:511
virtual bool doIsConnected(const VECTOR2I &aPosition) const
Provide the object specific test to see if it is connected to aPosition.
Definition: sch_item.h:507
const wxString & GetDefaultFont() const
Definition: sch_item.cpp:323
virtual ~SCH_ITEM()
Definition: sch_item.cpp:81
virtual wxString GetClass() const override
Return the class name.
Definition: sch_item.h:160
virtual const wxString & GetCachedDriverName() const
Definition: sch_item.cpp:254
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:215
void AddConnectionTo(const SCH_SHEET_PATH &aPath, SCH_ITEM *aItem)
Add a connection link between this item and another.
Definition: sch_item.cpp:203
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:197
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:517
std::shared_ptr< NETCLASS > GetEffectiveNetClass(const SCH_SHEET_PATH *aSheet=nullptr) const
Definition: sch_item.cpp:176
bool m_connectivity_dirty
Definition: sch_item.h:522
virtual void ClearCaches()
Definition: sch_item.cpp:288
virtual void RunOnChildren(const std::function< void(SCH_ITEM *)> &aFunction)
Definition: sch_item.h:463
virtual void SwapData(SCH_ITEM *aItem)
Swap the internal data structures aItem with the schematic item.
Definition: sch_item.cpp:261
FIELDS_AUTOPLACED m_fieldsAutoplaced
Definition: sch_item.h:512
bool RenderAsBitmap(double aWorldScale) const override
Definition: sch_item.cpp:340
void SetConnectionGraph(CONNECTION_GRAPH *aGraph)
Updates the connection graph for all connections in this item.
Definition: sch_item.cpp:164
void SwapFlags(SCH_ITEM *aItem)
Swap the non-temp and non-edit flags.
Definition: sch_item.cpp:267
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:520
virtual bool operator<(const SCH_ITEM &aItem) const
Definition: sch_item.cpp:308
virtual bool IsHypertext() const
Allow items to support hypertext actions when hovered/clicked.
Definition: sch_item.h:250
SCH_CONNECTION * GetOrInitConnection(const SCH_SHEET_PATH &aPath, CONNECTION_GRAPH *aGraph)
Definition: sch_item.cpp:239
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
const KIFONT::METRICS & GetFontMetrics() const
Definition: sch_item.cpp:331
virtual void Plot(PLOTTER *aPlotter, bool aBackground) const
Plot the schematic item to aPlotter.
Definition: sch_item.cpp:352
SCH_LAYER_ID m_layer
Definition: sch_item.h:510
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
std::uint32_t EDA_ITEM_FLAGS
wxString LayerName(int aLayer)
Returns the default display name for a given layer.
Definition: layer_id.cpp:30
SCH_LAYER_ID
Eeschema drawing layers.
Definition: layer_ids.h:346
@ LAYER_DEVICE
Definition: layer_ids.h:362
@ LAYER_WIRE
Definition: layer_ids.h:349
@ SCH_LAYER_ID_END
Definition: layer_ids.h:396
@ LAYER_SELECTION_SHADOWS
Definition: layer_ids.h:387
#define UNIMPLEMENTED_FOR(type)
Definition: macros.h:96
see class PGM_BASE
#define TYPE_HASH(x)
Definition: property.h:64
#define IMPLEMENT_ENUM_TO_WXANY(type)
Definition: property.h:733
#define REGISTER_TYPE(x)
Definition: property_mgr.h:356
#define BITMAP_FONT_SIZE_THRESHOLD
Definition: sch_item.cpp:47
static struct SCH_ITEM_DESC _SCH_ITEM_DESC
std::vector< SCH_ITEM * > SCH_ITEM_SET
Definition: sch_item.h:139
@ FIELDS_AUTOPLACED_NO
Definition: sch_item.h:59
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:187