KiCad PCB EDA Suite
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 <trace_helpers.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 <general.h>
37#include <netclass.h>
40
41
42// Rendering fonts is expensive (particularly when using outline fonts). At small effective
43// sizes (ie: zoomed out) the visual differences between outline and/or stroke fonts and the
44// bitmap font becomes immaterial, and there's often more to draw when zoomed out so the
45// performance gain becomes more significant.
46#define BITMAP_FONT_SIZE_THRESHOLD 3
47
48
49/* Constructor and destructor for SCH_ITEM */
50/* They are not inline because this creates problems with gcc at linking time in debug mode */
51
53 EDA_ITEM( aParent, aType )
54{
55 m_layer = LAYER_WIRE; // It's only a default, in fact
57 m_connectivity_dirty = false; // Item is unconnected until it is placed, so it's clean
58}
59
60
62 EDA_ITEM( aItem )
63{
64 m_layer = aItem.m_layer;
67}
68
69
71{
72 m_layer = aItem.m_layer;
75
76 return *this;
77}
78
79
81{
82 // Do not let the connections container go out of scope with any objects or they
83 // will be deleted by the container will cause the Eeschema to crash. These objects
84 // are owned by the sheet object container.
85 if( !m_connections.empty() )
86 m_connections.clear();
87
88 for( const auto& it : m_connection_map )
89 delete it.second;
90}
91
92
93SCH_ITEM* SCH_ITEM::Duplicate( bool doClone ) const
94{
95 SCH_ITEM* newItem = (SCH_ITEM*) Clone();
96
97 if( !doClone )
98 const_cast<KIID&>( newItem->m_Uuid ) = KIID();
99
100 newItem->ClearFlags( SELECTED | BRIGHTENED );
101
102 newItem->RunOnChildren(
103 []( SCH_ITEM* aChild )
104 {
105 aChild->ClearFlags( SELECTED | BRIGHTENED );
106 } );
107
108 return newItem;
109}
110
111
113{
114 EDA_ITEM* parent = GetParent();
115
116 while( parent )
117 {
118 if( parent->Type() == SCHEMATIC_T )
119 return static_cast<SCHEMATIC*>( parent );
120 else
121 parent = parent->GetParent();
122 }
123
124 return nullptr;
125}
126
127
128void SCH_ITEM::ViewGetLayers( int aLayers[], int& aCount ) const
129{
130 // Basic fallback
131 aCount = 2;
132 aLayers[0] = LAYER_DEVICE;
133 aLayers[1] = LAYER_SELECTION_SHADOWS;
134}
135
136
137bool SCH_ITEM::IsConnected( const VECTOR2I& aPosition ) const
138{
139 if(( m_flags & STRUCT_DELETED ) || ( m_flags & SKIP_STRUCT ) )
140 return false;
141
142 return doIsConnected( aPosition );
143}
144
145
147{
148 if( !IsConnectable() )
149 return nullptr;
150
151 wxCHECK_MSG( !IsConnectivityDirty(), nullptr,
152 wxT( "Shouldn't be asking for connection if connectivity is dirty!" ) );
153
154 if( !aSheet )
155 aSheet = &Schematic()->CurrentSheet();
156
157 auto it = m_connection_map.find( *aSheet );
158
159 if( it == m_connection_map.end() )
160 return nullptr;
161 else
162 return it->second;
163}
164
165
166std::shared_ptr<NETCLASS> SCH_ITEM::GetEffectiveNetClass( const SCH_SHEET_PATH* aSheet ) const
167{
168 static std::shared_ptr<NETCLASS> nullNetclass = std::make_shared<NETCLASS>( wxEmptyString );
169
170 SCHEMATIC* schematic = Schematic();
171
172 if( schematic )
173 {
174 std::shared_ptr<NET_SETTINGS>& netSettings = schematic->Prj().GetProjectFile().m_NetSettings;
175 SCH_CONNECTION* connection = Connection( aSheet );
176
177 if( connection )
178 return netSettings->GetEffectiveNetClass( connection->Name() );
179 else
180 return netSettings->m_DefaultNetClass;
181 }
182
183 return nullNetclass;
184}
185
186
188{
189 return m_connected_items[ aSheet ];
190}
191
192
194{
195 SCH_ITEM_SET& set = m_connected_items[ aSheet ];
196
197 // The vector elements are small, so reserve 1k at a time to prevent re-allocations
198 if( set.size() == set.capacity() )
199 set.reserve( set.size() + 4096 );
200
201 set.emplace_back( aItem );
202}
203
204
206 CONNECTION_GRAPH* aGraph )
207{
208 SetConnectivityDirty( false );
209
210 SCH_CONNECTION* connection = Connection( &aSheet );
211
212 if( connection )
213 {
214 connection->Reset();
215 }
216 else
217 {
218 connection = new SCH_CONNECTION( this );
219 m_connection_map.insert( std::make_pair( aSheet, connection ) );
220 }
221
222 connection->SetGraph( aGraph );
223 connection->SetSheet( aSheet );
224 return connection;
225}
226
227
229 CONNECTION_GRAPH* aGraph )
230{
231 if( !IsConnectable() )
232 return nullptr;
233
234 SetConnectivityDirty( false );
235
236 SCH_CONNECTION* connection = Connection( &aSheet );
237
238 if( connection )
239 return connection;
240 else
241 return InitializeConnection( aSheet, aGraph );
242}
243
244
246{
248}
249
250
252{
253 auto clearTextCaches =
254 []( SCH_ITEM* aItem )
255 {
256 EDA_TEXT* text = dynamic_cast<EDA_TEXT*>( aItem );
257
258 if( text )
259 {
260 text->ClearBoundingBoxCache();
261 text->ClearRenderCache();
262 }
263 };
264
265 clearTextCaches( this );
266
267 RunOnChildren( clearTextCaches );
268}
269
270
271bool SCH_ITEM::operator < ( const SCH_ITEM& aItem ) const
272{
273 if( Type() != aItem.Type() )
274 return Type() < aItem.Type();
275
276 if( GetPosition().x != aItem.GetPosition().x )
277 return GetPosition().x < aItem.GetPosition().x;
278
279 if( GetPosition().y != aItem.GetPosition().y )
280 return GetPosition().y < aItem.GetPosition().y;
281
282 return m_Uuid < aItem.m_Uuid;
283}
284
285
286const wxString& SCH_ITEM::GetDefaultFont() const
287{
288 EESCHEMA_SETTINGS* cfg = Pgm().GetSettingsManager().GetAppSettings<EESCHEMA_SETTINGS>();
289
290 return cfg->m_Appearance.default_font;
291}
292
293
294bool SCH_ITEM::RenderAsBitmap( double aWorldScale ) const
295{
296 if( IsHypertext() )
297 return false;
298
299 if( const EDA_TEXT* text = dynamic_cast<const EDA_TEXT*>( this ) )
300 return text->GetTextHeight() * aWorldScale < BITMAP_FONT_SIZE_THRESHOLD;
301
302 return false;
303}
304
305
306void SCH_ITEM::Plot( PLOTTER* aPlotter, bool aBackground ) const
307{
308 wxFAIL_MSG( wxT( "Plot() method not implemented for class " ) + GetClass() );
309}
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:249
const KIID m_Uuid
Definition: eda_item.h:492
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:143
EDA_ITEM_FLAGS m_flags
Definition: eda_item.h:498
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
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:72
Definition: kiid.h:47
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:148
Holds all the data relating to one schematic.
Definition: schematic.h:60
SCH_SHEET_PATH & CurrentSheet() const override
Definition: schematic.h:119
PROJECT & Prj() const override
Return a reference to the project this schematic is part of.
Definition: schematic.h:75
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:349
SCH_ITEM(EDA_ITEM *aParent, KICAD_T aType)
Definition: sch_item.cpp:52
SCH_ITEM & operator=(const SCH_ITEM &aPin)
Definition: sch_item.cpp:70
EDA_ITEMS m_connections
Definition: sch_item.h:492
virtual bool doIsConnected(const VECTOR2I &aPosition) const
Provide the object specific test to see if it is connected to aPosition.
Definition: sch_item.h:488
const wxString & GetDefaultFont() const
Definition: sch_item.cpp:286
virtual ~SCH_ITEM()
Definition: sch_item.cpp:80
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:112
SCH_CONNECTION * InitializeConnection(const SCH_SHEET_PATH &aPath, CONNECTION_GRAPH *aGraph)
Create a new connection object associated with this object.
Definition: sch_item.cpp:205
void AddConnectionTo(const SCH_SHEET_PATH &aPath, SCH_ITEM *aItem)
Add a connection link between this item and another.
Definition: sch_item.cpp:193
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:187
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:498
std::shared_ptr< NETCLASS > GetEffectiveNetClass(const SCH_SHEET_PATH *aSheet=nullptr) const
Definition: sch_item.cpp:166
bool m_connectivity_dirty
Definition: sch_item.h:503
virtual void ClearCaches()
Definition: sch_item.cpp:251
virtual void RunOnChildren(const std::function< void(SCH_ITEM *)> &aFunction)
Definition: sch_item.h:444
virtual void SwapData(SCH_ITEM *aItem)
Swap the internal data structures aItem with the schematic item.
Definition: sch_item.cpp:245
void SetConnectivityDirty(bool aDirty=true)
Definition: sch_item.h:415
FIELDS_AUTOPLACED m_fieldsAutoplaced
Definition: sch_item.h:493
bool RenderAsBitmap(double aWorldScale) const override
Definition: sch_item.cpp:294
bool IsConnectivityDirty() const
Definition: sch_item.h:413
bool IsConnected(const VECTOR2I &aPoint) const
Test the item to see if it is connected to aPoint.
Definition: sch_item.cpp:137
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:128
std::unordered_map< SCH_SHEET_PATH, SCH_CONNECTION * > m_connection_map
Store connectivity information, per sheet.
Definition: sch_item.h:501
virtual bool operator<(const SCH_ITEM &aItem) const
Definition: sch_item.cpp:271
virtual bool IsHypertext() const
Allow items to support hypertext actions when hovered/clicked.
Definition: sch_item.h:239
SCH_CONNECTION * GetOrInitConnection(const SCH_SHEET_PATH &aPath, CONNECTION_GRAPH *aGraph)
Definition: sch_item.cpp:228
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:146
virtual void Plot(PLOTTER *aPlotter, bool aBackground) const
Plot the schematic item to aPlotter.
Definition: sch_item.cpp:306
SCH_LAYER_ID m_layer
Definition: sch_item.h:491
SCH_ITEM * Duplicate(bool doClone=false) const
Routine to create a new copy of given item.
Definition: sch_item.cpp:93
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:357
@ LAYER_WIRE
Definition: layer_ids.h:344
@ LAYER_SELECTION_SHADOWS
Definition: layer_ids.h:381
#define UNIMPLEMENTED_FOR(type)
Definition: macros.h:120
see class PGM_BASE
#define BITMAP_FONT_SIZE_THRESHOLD
Definition: sch_item.cpp:46
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:111
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