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, jaen-pierre.charras@gipsa-lab.inpg.com
5  * Copyright (C) 1992-2020 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 <sch_component.h>
33 #include <sch_sheet.h>
34 #include <sch_pin.h>
35 #include <schematic.h>
36 #include <general.h>
37 #include <netclass.h>
38 #include <project/project_file.h>
39 #include <project/net_settings.h>
40 
41 
42 /* Constructor and destructor for SCH_ITEM */
43 /* They are not inline because this creates problems with gcc at linking time
44  * in debug mode
45  */
46 
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
52  m_connectivity_dirty = true;
53 }
54 
55 
56 SCH_ITEM::SCH_ITEM( const SCH_ITEM& aItem ) :
57  EDA_ITEM( aItem )
58 {
59  m_layer = aItem.m_layer;
61  m_connectivity_dirty = true;
62 }
63 
64 
66 {
67  // Do not let the connections container go out of scope with any objects or they
68  // will be deleted by the container will cause the Eeschema to crash. These objects
69  // are owned by the sheet object container.
70  if( !m_connections.empty() )
71  m_connections.clear();
72 
73  for( const auto& it : m_connection_map )
74  delete it.second;
75 }
76 
77 
78 SCH_ITEM* SCH_ITEM::Duplicate( bool doClone ) const
79 {
80  SCH_ITEM* newItem = (SCH_ITEM*) Clone();
81 
82  if( !doClone )
83  const_cast<KIID&>( newItem->m_Uuid ) = KIID();
84 
85  newItem->ClearFlags( SELECTED | BRIGHTENED );
86 
87  newItem->RunOnChildren(
88  []( SCH_ITEM* aChild )
89  {
90  aChild->ClearFlags( SELECTED | BRIGHTENED );
91  } );
92 
93  return newItem;
94 }
95 
96 
98 {
99  EDA_ITEM* parent = GetParent();
100 
101  while( parent )
102  {
103  if( parent->Type() == SCHEMATIC_T )
104  return static_cast<SCHEMATIC*>( parent );
105  else
106  parent = parent->GetParent();
107  }
108 
109  return nullptr;
110 }
111 
112 
113 void SCH_ITEM::ViewGetLayers( int aLayers[], int& aCount ) const
114 {
115  // Basic fallback
116  aCount = 2;
117  aLayers[0] = LAYER_DEVICE;
118  aLayers[1] = LAYER_SELECTION_SHADOWS;
119 }
120 
121 
122 bool SCH_ITEM::IsConnected( const wxPoint& aPosition ) const
123 {
124  if(( m_flags & STRUCT_DELETED ) || ( m_flags & SKIP_STRUCT ) )
125  return false;
126 
127  return doIsConnected( aPosition );
128 }
129 
130 
132 {
133  if( !aSheet )
134  aSheet = &Schematic()->CurrentSheet();
135 
136  auto it = m_connection_map.find( *aSheet );
137 
138  if( it == m_connection_map.end() )
139  return nullptr;
140  else
141  return it->second;
142 }
143 
144 
145 NETCLASSPTR SCH_ITEM::NetClass( const SCH_SHEET_PATH* aSheet ) const
146 {
147  if( m_connection_map.size() )
148  {
149  SCH_CONNECTION* connection = Connection( aSheet );
150 
151  if( connection )
152  {
153  NET_SETTINGS& netSettings = Schematic()->Prj().GetProjectFile().NetSettings();
154  const wxString& netclassName = netSettings.GetNetclassName( connection->Name() );
155 
156  return netSettings.m_NetClasses.Find( netclassName );
157  }
158  }
159 
160  return nullptr;
161 }
162 
163 
165 {
166  return m_connected_items[ aSheet ];
167 }
168 
169 
170 void SCH_ITEM::AddConnectionTo( const SCH_SHEET_PATH& aSheet, SCH_ITEM* aItem )
171 {
172  m_connected_items[ aSheet ].insert( aItem );
173 }
174 
175 
177  CONNECTION_GRAPH* aGraph )
178 {
179  SCH_CONNECTION* connection = Connection( &aSheet );
180 
181  if( connection )
182  {
183  connection->Reset();
184  }
185  else
186  {
187  connection = new SCH_CONNECTION( this );
188  m_connection_map.insert( std::make_pair( aSheet, connection ) );
189  connection->SetGraph( aGraph );
190  }
191 
192  connection->SetSheet( aSheet );
193  return connection;
194 }
195 
196 
198 {
199  wxFAIL_MSG( wxT( "SwapData() method not implemented for class " ) + GetClass() );
200 }
201 
202 
203 bool SCH_ITEM::operator < ( const SCH_ITEM& aItem ) const
204 {
205  if( Type() != aItem.Type() )
206  return Type() < aItem.Type();
207 
208  if( m_Uuid != aItem.m_Uuid )
209  return m_Uuid < aItem.m_Uuid;
210 
211  if( GetPosition().x != aItem.GetPosition().x )
212  return GetPosition().x < aItem.GetPosition().x;
213 
214  return GetPosition().y < aItem.GetPosition().y;
215 }
216 
217 
218 void SCH_ITEM::Plot( PLOTTER* aPlotter )
219 {
220  wxFAIL_MSG( wxT( "Plot() method not implemented for class " ) + GetClass() );
221 }
NETCLASSPTR Find(const wxString &aName) const
Function Find searches this container for a NETCLASS given by aName.
Definition: netclass.cpp:132
Holds all the data relating to one schematic A schematic may consist of one or more sheets (and one r...
Definition: schematic.h:44
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)
Adds a connection link between this item and another.
Definition: sch_item.cpp:170
EDA_ITEMS m_connections
Definition: sch_item.h:200
SCHEMATIC * Schematic() const
Searches the item hierarchy to find a SCHEMATIC.
Definition: sch_item.cpp:97
NET_SETTINGS & NetSettings()
Definition: project_file.h:92
void SetSheet(SCH_SHEET_PATH aSheet)
virtual wxPoint GetPosition() const
Definition: eda_item.h:325
FIELDS_AUTOPLACED m_fieldsAutoplaced
Definition: sch_item.h:201
KICAD_T
Enum KICAD_T is the set of class identification values, stored in EDA_ITEM::m_structType.
Definition: typeinfo.h:78
virtual EDA_ITEM * Clone() const
Function Clone creates a duplicate of this item with linked list members set to NULL.
Definition: eda_item.cpp:97
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:131
NETCLASSPTR NetClass(const SCH_SHEET_PATH *aSheet=nullptr) const
Definition: sch_item.cpp:145
virtual void RunOnChildren(const std::function< void(SCH_ITEM *)> &aFunction)
Definition: sch_item.h:475
Definition: kiid.h:44
#define BRIGHTENED
item is drawn with a bright contour
Definition: eda_item.h:129
SCH_LAYER_ID m_layer
Definition: sch_item.h:199
NETCLASSES m_NetClasses
Definition: net_settings.h:39
SCH_CONNECTION * InitializeConnection(const SCH_SHEET_PATH &aPath, CONNECTION_GRAPH *aGraph)
Creates a new connection object associated with this object.
Definition: sch_item.cpp:176
wxString Name(bool aIgnoreSheet=false) const
SCH_ITEM * Duplicate(bool doClone=false) const
Routine to create a new copy of given item.
Definition: sch_item.cpp:78
virtual void SwapData(SCH_ITEM *aItem)
Swap the internal data structures aItem with the schematic item.
Definition: sch_item.cpp:197
void Reset()
Clears connectivity information.
VTBL_ENTRY PROJECT_FILE & GetProjectFile() const
Definition: project.h:143
#define SELECTED
Definition: eda_item.h:113
void SetGraph(CONNECTION_GRAPH *aGraph)
EDA_ITEM * GetParent() const
Definition: eda_item.h:183
NET_SETTINGS stores various net-related settings in a project context.
Definition: net_settings.h:31
bool m_connectivity_dirty
Definition: sch_item.h:211
wxLogTrace helper definitions.
#define STRUCT_DELETED
flag indication structures to be erased
Definition: eda_item.h:115
std::unordered_map< SCH_SHEET_PATH, SCH_CONNECTION * > m_connection_map
Stores connectivity information, per sheet.
Definition: sch_item.h:209
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
Stores pointers to other items that are connected to this one, per sheet.
Definition: sch_item.h:206
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:113
const KIID m_Uuid
Definition: eda_item.h:151
virtual void Plot(PLOTTER *aPlotter)
Plot the schematic item to aPlotter.
Definition: sch_item.cpp:218
Base plotter engine class.
Definition: plotter.h:131
SCH_ITEM_SET & ConnectedItems(const SCH_SHEET_PATH &aPath)
Retrieves the set of items connected to this item on the given sheet.
Definition: sch_item.cpp:164
Each graphical item can have a SCH_CONNECTION describing its logical connection (to a bus or net).
PROJECT & Prj() const
Return a reference to the project this schematic is part of.
Definition: schematic.h:85
~SCH_ITEM()
Definition: sch_item.cpp:65
EDA_ITEM is a base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:148
virtual wxString GetClass() const override
Function GetClass returns the class name.
Definition: sch_item.h:220
void ClearFlags(STATUS_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition: eda_item.h:221
virtual bool operator<(const SCH_ITEM &aItem) const
Definition: sch_item.cpp:203
virtual bool doIsConnected(const wxPoint &aPosition) const
Provide the object specific test to see if it is connected to aPosition.
Definition: sch_item.h:513
std::unordered_set< SCH_ITEM * > SCH_ITEM_SET
Definition: sch_item.h:147
#define SKIP_STRUCT
flag indicating that the structure should be ignored
Definition: eda_item.h:117
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
STATUS_FLAGS m_flags
Definition: eda_item.h:164
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:194
bool IsConnected(const wxPoint &aPoint) const
Test the item to see if it is connected to aPoint.
Definition: sch_item.cpp:122
KICAD_T Type() const
Function Type()
Definition: eda_item.h:181
SCH_SHEET_PATH & CurrentSheet() const
Definition: schematic.h:127
SCH_ITEM(EDA_ITEM *aParent, KICAD_T aType)
Definition: sch_item.cpp:47