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 <eda_item.h>
26 #include <trace_helpers.h>
27 #include <sch_item.h>
28 #include <sch_screen.h>
29 #include <sch_sheet_path.h>
30 #include <sch_draw_panel.h>
31 #include <sch_edit_frame.h>
32 #include <schematic.h>
33 #include <general.h>
34 #include <netclass.h>
35 #include <project/project_file.h>
36 #include <project/net_settings.h>
37 
38 
39 /* Constructor and destructor for SCH_ITEM */
40 /* They are not inline because this creates problems with gcc at linking time
41  * in debug mode
42  */
43 
44 SCH_ITEM::SCH_ITEM( EDA_ITEM* aParent, KICAD_T aType ) :
45  EDA_ITEM( aParent, aType )
46 {
47  m_layer = LAYER_WIRE; // It's only a default, in fact
49  m_connectivity_dirty = false; // Item is unconnected until it is placed, so it's clean
50 }
51 
52 
53 SCH_ITEM::SCH_ITEM( const SCH_ITEM& aItem ) :
54  EDA_ITEM( aItem )
55 {
56  m_layer = aItem.m_layer;
59 }
60 
61 
63 {
64  m_layer = aItem.m_layer;
67 
68  return *this;
69 }
70 
71 
73 {
74  // Do not let the connections container go out of scope with any objects or they
75  // will be deleted by the container will cause the Eeschema to crash. These objects
76  // are owned by the sheet object container.
77  if( !m_connections.empty() )
78  m_connections.clear();
79 
80  for( const auto& it : m_connection_map )
81  delete it.second;
82 }
83 
84 
85 SCH_ITEM* SCH_ITEM::Duplicate( bool doClone ) const
86 {
87  SCH_ITEM* newItem = (SCH_ITEM*) Clone();
88 
89  if( !doClone )
90  const_cast<KIID&>( newItem->m_Uuid ) = KIID();
91 
92  newItem->ClearFlags( SELECTED | BRIGHTENED );
93 
94  newItem->RunOnChildren(
95  []( SCH_ITEM* aChild )
96  {
97  aChild->ClearFlags( SELECTED | BRIGHTENED );
98  } );
99 
100  return newItem;
101 }
102 
103 
105 {
106  EDA_ITEM* parent = GetParent();
107 
108  while( parent )
109  {
110  if( parent->Type() == SCHEMATIC_T )
111  return static_cast<SCHEMATIC*>( parent );
112  else
113  parent = parent->GetParent();
114  }
115 
116  return nullptr;
117 }
118 
119 
120 void SCH_ITEM::ViewGetLayers( int aLayers[], int& aCount ) const
121 {
122  // Basic fallback
123  aCount = 2;
124  aLayers[0] = LAYER_DEVICE;
125  aLayers[1] = LAYER_SELECTION_SHADOWS;
126 }
127 
128 
129 bool SCH_ITEM::IsConnected( const wxPoint& aPosition ) const
130 {
131  if(( m_flags & STRUCT_DELETED ) || ( m_flags & SKIP_STRUCT ) )
132  return false;
133 
134  return doIsConnected( aPosition );
135 }
136 
137 
139 {
140  if( !IsConnectable() )
141  return nullptr;
142 
143  wxCHECK_MSG( !IsConnectivityDirty(), nullptr,
144  wxT( "Shouldn't be asking for connection if connectivity is dirty!" ) );
145 
146  if( !aSheet )
147  aSheet = &Schematic()->CurrentSheet();
148 
149  auto it = m_connection_map.find( *aSheet );
150 
151  if( it == m_connection_map.end() )
152  return nullptr;
153  else
154  return it->second;
155 }
156 
157 
158 NETCLASSPTR SCH_ITEM::NetClass( const SCH_SHEET_PATH* aSheet ) const
159 {
160  if( m_connection_map.size() )
161  {
162  SCH_CONNECTION* connection = Connection( aSheet );
163 
164  if( connection )
165  {
166  NET_SETTINGS& netSettings = Schematic()->Prj().GetProjectFile().NetSettings();
167  const wxString& netclassName = netSettings.GetNetclassName( connection->Name() );
168 
169  return netSettings.m_NetClasses.Find( netclassName );
170  }
171  }
172 
173  return nullptr;
174 }
175 
176 
178 {
179  return m_connected_items[ aSheet ];
180 }
181 
182 
183 void SCH_ITEM::AddConnectionTo( const SCH_SHEET_PATH& aSheet, SCH_ITEM* aItem )
184 {
185  SCH_ITEM_SET& set = m_connected_items[ aSheet ];
186 
187  // The vector elements are small, so reserve 1k at a time to prevent re-allocations
188  if( set.size() == set.capacity() )
189  set.reserve( set.size() + 4096 );
190 
191  set.emplace_back( aItem );
192 }
193 
194 
196  CONNECTION_GRAPH* aGraph )
197 {
198  SetConnectivityDirty( false );
199 
200  SCH_CONNECTION* connection = Connection( &aSheet );
201 
202  if( connection )
203  {
204  connection->Reset();
205  }
206  else
207  {
208  connection = new SCH_CONNECTION( this );
209  m_connection_map.insert( std::make_pair( aSheet, connection ) );
210  }
211 
212  connection->SetGraph( aGraph );
213  connection->SetSheet( aSheet );
214  return connection;
215 }
216 
217 
219  CONNECTION_GRAPH* aGraph )
220 {
221  if( !IsConnectable() )
222  return nullptr;
223 
224  SetConnectivityDirty( false );
225 
226  SCH_CONNECTION* connection = Connection( &aSheet );
227 
228  if( connection )
229  return connection;
230  else
231  return InitializeConnection( aSheet, aGraph );
232 }
233 
234 
236 {
238 }
239 
240 
241 bool SCH_ITEM::operator < ( const SCH_ITEM& aItem ) const
242 {
243  if( Type() != aItem.Type() )
244  return Type() < aItem.Type();
245 
246  if( GetPosition().x != aItem.GetPosition().x )
247  return GetPosition().x < aItem.GetPosition().x;
248 
249  if( GetPosition().y != aItem.GetPosition().y )
250  return GetPosition().y < aItem.GetPosition().y;
251 
252  return m_Uuid < aItem.m_Uuid;
253 }
254 
255 
256 void SCH_ITEM::Plot( PLOTTER* aPlotter ) const
257 {
258  wxFAIL_MSG( wxT( "Plot() method not implemented for class " ) + GetClass() );
259 }
virtual bool IsConnectable() const
Definition: sch_item.h:349
NETCLASSPTR Find(const wxString &aName) const
Search this container for a NETCLASS given by aName.
Definition: netclass.cpp:132
SCH_CONNECTION * GetOrInitConnection(const SCH_SHEET_PATH &aPath, CONNECTION_GRAPH *aGraph)
Definition: sch_item.cpp:218
Holds all the data relating to one schematic.
Definition: schematic.h:59
const wxString & GetNetclassName(const wxString &aNetName) const
Calculates the connectivity of a schematic and generates netlists.
void AddConnectionTo(const SCH_SHEET_PATH &aPath, SCH_ITEM *aItem)
Add a connection link between this item and another.
Definition: sch_item.cpp:183
virtual PROJECT_FILE & GetProjectFile() const
Definition: project.h:145
EDA_ITEMS m_connections
Definition: sch_item.h:488
SCHEMATIC * Schematic() const
Searches the item hierarchy to find a SCHEMATIC.
Definition: sch_item.cpp:104
NET_SETTINGS & NetSettings()
Definition: project_file.h:96
void SetSheet(SCH_SHEET_PATH aSheet)
virtual wxPoint GetPosition() const
Definition: eda_item.h:251
EDA_ITEM_FLAGS m_flags
Definition: eda_item.h:480
FIELDS_AUTOPLACED m_fieldsAutoplaced
Definition: sch_item.h:489
bool IsConnectivityDirty() const
Definition: sch_item.h:413
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition: typeinfo.h:77
virtual EDA_ITEM * Clone() const
Create a duplicate of this item with linked list members set to NULL.
Definition: eda_item.cpp:83
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:138
NETCLASSPTR NetClass(const SCH_SHEET_PATH *aSheet=nullptr) const
Definition: sch_item.cpp:158
std::vector< SCH_ITEM * > SCH_ITEM_SET
Definition: sch_item.h:134
virtual void RunOnChildren(const std::function< void(SCH_ITEM *)> &aFunction)
Definition: sch_item.h:444
Definition: kiid.h:44
SCH_LAYER_ID m_layer
Definition: sch_item.h:487
NETCLASSES m_NetClasses
Definition: net_settings.h:40
SCH_CONNECTION * InitializeConnection(const SCH_SHEET_PATH &aPath, CONNECTION_GRAPH *aGraph)
Create a new connection object associated with this object.
Definition: sch_item.cpp:195
wxString Name(bool aIgnoreSheet=false) const
virtual void Plot(PLOTTER *aPlotter) const
Plot the schematic item to aPlotter.
Definition: sch_item.cpp:256
SCH_ITEM * Duplicate(bool doClone=false) const
Routine to create a new copy of given item.
Definition: sch_item.cpp:85
virtual void SwapData(SCH_ITEM *aItem)
Swap the internal data structures aItem with the schematic item.
Definition: sch_item.cpp:235
void Reset()
Clears connectivity information.
void SetGraph(CONNECTION_GRAPH *aGraph)
#define UNIMPLEMENTED_FOR(type)
Definition: macros.h:120
EDA_ITEM * GetParent() const
Definition: eda_item.h:114
#define STRUCT_DELETED
flag indication structures to be erased
NET_SETTINGS stores various net-related settings in a project context.
Definition: net_settings.h:32
bool m_connectivity_dirty
Definition: sch_item.h:499
void ClearFlags(EDA_ITEM_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition: eda_item.h:153
wxLogTrace helper definitions.
#define SELECTED
std::unordered_map< SCH_SHEET_PATH, SCH_CONNECTION * > m_connection_map
Store connectivity information, per sheet.
Definition: sch_item.h:497
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
std::unordered_map< SCH_SHEET_PATH, SCH_ITEM_SET > m_connected_items
Store pointers to other items that are connected to this one, per sheet.
Definition: sch_item.h:494
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:120
PROJECT & Prj() const override
Return a reference to the project this schematic is part of.
Definition: schematic.h:75
const KIID m_Uuid
Definition: eda_item.h:474
Base plotter engine class.
Definition: plotter.h:121
#define SKIP_STRUCT
flag indicating that the structure should be ignored
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:177
Each graphical item can have a SCH_CONNECTION describing its logical connection (to a bus or net).
virtual ~SCH_ITEM()
Definition: sch_item.cpp:72
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:99
virtual wxString GetClass() const override
Return the class name.
Definition: sch_item.h:193
virtual bool operator<(const SCH_ITEM &aItem) const
Definition: sch_item.cpp:241
virtual bool doIsConnected(const wxPoint &aPosition) const
Provide the object specific test to see if it is connected to aPosition.
Definition: sch_item.h:484
SCH_SHEET_PATH & CurrentSheet() const override
Definition: schematic.h:121
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:182
bool IsConnected(const wxPoint &aPoint) const
Test the item to see if it is connected to aPoint.
Definition: sch_item.cpp:129
SCH_ITEM & operator=(const SCH_ITEM &aPin)
Definition: sch_item.cpp:62
void SetConnectivityDirty(bool aDirty=true)
Definition: sch_item.h:415
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:112
#define BRIGHTENED
item is drawn with a bright contour
SCH_ITEM(EDA_ITEM *aParent, KICAD_T aType)
Definition: sch_item.cpp:44