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 <fctsys.h>
26 #include <common.h>
27 #include <gr_basic.h>
28 #include <base_struct.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 <sch_component.h>
36 #include <sch_sheet.h>
37 #include <sch_pin.h>
38 #include <schematic.h>
39 #include <general.h>
40 #include <netclass.h>
41 #include <project/project_file.h>
42 #include <project/net_settings.h>
43 
44 
45 /* Constructor and destructor for SCH_ITEM */
46 /* They are not inline because this creates problems with gcc at linking time
47  * in debug mode
48  */
49 
50 SCH_ITEM::SCH_ITEM( EDA_ITEM* aParent, KICAD_T aType ) :
51  EDA_ITEM( aParent, aType )
52 {
53  m_Layer = LAYER_WIRE; // It's only a default, in fact
55  m_connectivity_dirty = true;
56 }
57 
58 
59 SCH_ITEM::SCH_ITEM( const SCH_ITEM& aItem ) :
60  EDA_ITEM( aItem )
61 {
62  m_Layer = aItem.m_Layer;
64  m_connectivity_dirty = true;
65 }
66 
67 
69 {
70  // Do not let the connections container go out of scope with any objects or they
71  // will be deleted by the container will cause the Eeschema to crash. These objects
72  // are owned by the sheet object container.
73  if( !m_connections.empty() )
74  m_connections.clear();
75 
76  for( const auto& it : m_connection_map )
77  delete it.second;
78 }
79 
80 
81 SCH_ITEM* SCH_ITEM::Duplicate( bool doClone ) const
82 {
83  SCH_ITEM* newItem = (SCH_ITEM*) Clone();
84 
85  if( !doClone )
86  const_cast<KIID&>( newItem->m_Uuid ) = KIID();
87 
88  newItem->ClearFlags( SELECTED | BRIGHTENED );
89 
90  if( newItem->Type() == SCH_COMPONENT_T )
91  {
92  SCH_COMPONENT* component = (SCH_COMPONENT*) newItem;
93 
94  for( SCH_PIN* pin : component->GetPins() )
95  pin->ClearFlags( SELECTED | BRIGHTENED );
96 
97  for( SCH_FIELD& field : component->GetFields() )
98  field.ClearFlags( SELECTED | BRIGHTENED );
99  }
100 
101  if( newItem->Type() == SCH_SHEET_T )
102  {
103  SCH_SHEET* sheet = (SCH_SHEET*) newItem;
104 
105  for( SCH_FIELD& field : sheet->GetFields() )
106  field.ClearFlags( SELECTED | BRIGHTENED );
107 
108  for( SCH_SHEET_PIN* pin : sheet->GetPins() )
109  pin->ClearFlags( SELECTED | BRIGHTENED );
110  }
111 
112  return newItem;
113 }
114 
115 
117 {
118  EDA_ITEM* parent = GetParent();
119 
120  while( parent )
121  {
122  if( parent->Type() == SCHEMATIC_T )
123  return static_cast<SCHEMATIC*>( parent );
124  else
125  parent = parent->GetParent();
126  }
127 
128  return nullptr;
129 }
130 
131 
132 void SCH_ITEM::ViewGetLayers( int aLayers[], int& aCount ) const
133 {
134  // Basic fallback
135  aCount = 2;
136  aLayers[0] = LAYER_DEVICE;
137  aLayers[1] = LAYER_SELECTION_SHADOWS;
138 }
139 
140 
141 bool SCH_ITEM::IsConnected( const wxPoint& aPosition ) const
142 {
143  if( ( m_Flags & STRUCT_DELETED ) || ( m_Flags & SKIP_STRUCT ) )
144  return false;
145 
146  return doIsConnected( aPosition );
147 }
148 
149 
151 {
152  // Warning: the m_connection_map can be empty.
153  if( m_connection_map.size() && m_connection_map.count( aSheet ) )
154  return m_connection_map.at( aSheet );
155 
156  return nullptr;
157 }
158 
159 
160 NETCLASSPTR SCH_ITEM::NetClass() const
161 {
162  if( m_connection_map.size() )
163  {
164  NET_SETTINGS& netSettings = Schematic()->Prj().GetProjectFile().NetSettings();
165  const wxString& netname = m_connection_map.begin()->second->Name( true );
166  const wxString& netclassName = netSettings.m_NetClassAssignments[ netname ];
167 
168  if( !netclassName.IsEmpty() )
169  return netSettings.m_NetClasses.Find( netclassName );
170  else
171  return netSettings.m_NetClasses.GetDefault();
172  }
173 
174  return nullptr;
175 }
176 
177 
179 {
180  return m_connected_items[ aSheet ];
181 }
182 
183 
184 void SCH_ITEM::AddConnectionTo( const SCH_SHEET_PATH& aSheet, SCH_ITEM* aItem )
185 {
186  m_connected_items[ aSheet ].insert( aItem );
187 }
188 
189 
191  CONNECTION_GRAPH* aGraph )
192 {
193  if( Connection( aSheet ) )
194  {
195  Connection( aSheet )->Reset();
196  Connection( aSheet )->SetSheet( aSheet );
197  return Connection( aSheet );
198  }
199 
200  auto connection = new SCH_CONNECTION( this );
201  connection->SetSheet( aSheet );
202  m_connection_map.insert( std::make_pair( aSheet, connection ) );
203  connection->SetGraph( aGraph );
204 
205  return connection;
206 }
207 
208 
210 {
211  wxFAIL_MSG( wxT( "SwapData() method not implemented for class " ) + GetClass() );
212 }
213 
214 
215 bool SCH_ITEM::operator < ( const SCH_ITEM& aItem ) const
216 {
217  if( Type() != aItem.Type() )
218  return Type() < aItem.Type();
219 
220  if( m_Uuid != aItem.m_Uuid )
221  return m_Uuid < aItem.m_Uuid;
222 
223  if( GetPosition().x != aItem.GetPosition().x )
224  return GetPosition().x < aItem.GetPosition().x;
225 
226  return GetPosition().y < aItem.GetPosition().y;
227 }
228 
229 
230 void SCH_ITEM::Plot( PLOTTER* aPlotter )
231 {
232  wxFAIL_MSG( wxT( "Plot() method not implemented for class " ) + GetClass() );
233 }
SCH_FIELD instances are attached to a component and provide a place for the component's value,...
Definition: sch_field.h:52
SCH_LAYER_ID m_Layer
Definition: sch_item.h:199
std::map< wxString, wxString > m_NetClassAssignments
Definition: net_settings.h:43
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:42
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:184
#define SKIP_STRUCT
flag indicating that the structure should be ignored
Definition: base_struct.h:128
EDA_ITEMS m_connections
Definition: sch_item.h:200
SCHEMATIC * Schematic() const
Searches the item hierarchy to find a SCHEMATIC.
Definition: sch_item.cpp:116
NET_SETTINGS & NetSettings()
Definition: project_file.h:92
void SetSheet(SCH_SHEET_PATH aSheet)
virtual wxPoint GetPosition() const
Definition: base_struct.h:337
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.
void GetFields(std::vector< SCH_FIELD * > &aVector, bool aVisibleOnly)
Populates a std::vector with SCH_FIELDs.
std::vector< SCH_FIELD > & GetFields()
Definition: sch_sheet.h:268
#define SELECTED
Definition: base_struct.h:124
Definition: common.h:68
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:190
SCH_ITEM * Duplicate(bool doClone=false) const
Routine to create a new copy of given item.
Definition: sch_item.cpp:81
virtual void SwapData(SCH_ITEM *aItem)
Swap the internal data structures aItem with the schematic item.
Definition: sch_item.cpp:209
NETCLASSPTR NetClass() const
Definition: sch_item.cpp:160
void Reset()
Clears connectivity information.
VTBL_ENTRY PROJECT_FILE & GetProjectFile() const
Definition: project.h:141
#define BRIGHTENED
item is drawn with a bright contour
Definition: base_struct.h:139
#define STRUCT_DELETED
flag indication structures to be erased
Definition: base_struct.h:126
EDA_ITEM * GetParent() const
Definition: base_struct.h:195
NET_SETTINGS stores various net-related settings in a project context.
Definition: net_settings.h:31
bool m_connectivity_dirty
True if connectivity info might be out of date.
Definition: sch_item.h:212
wxLogTrace helper definitions.
Define a sheet pin (label) used in sheets to create hierarchical schematics.
Definition: sch_sheet.h:84
std::unordered_map< SCH_SHEET_PATH, SCH_CONNECTION * > m_connection_map
Stores connectivity information, per sheet.
Definition: sch_item.h:209
SCH_SHEET_PATH.
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:132
SCH_CONNECTION * Connection(const SCH_SHEET_PATH &aPath) const
Retrieve the connection associated with this object in the given sheet.
Definition: sch_item.cpp:150
std::vector< SCH_PIN * > GetPins(const SCH_SHEET_PATH *aSheet=nullptr) const
Retrieves a list of the SCH_PINs for the given sheet path.
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition: sch_sheet.h:216
const KIID m_Uuid
Definition: base_struct.h:162
std::vector< SCH_SHEET_PIN * > & GetPins()
Definition: sch_sheet.h:362
virtual void Plot(PLOTTER *aPlotter)
Plot the schematic item to aPlotter.
Definition: sch_item.cpp:230
Base plotter engine class.
Definition: plotter.h:114
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:178
Each graphical item can have a SCH_CONNECTION describing its logical connection (to a bus or net).
STATUS_FLAGS m_Flags
Definition: base_struct.h:176
Schematic symbol object.
Definition: sch_component.h:80
PROJECT & Prj() const
Return a reference to the project this schematic is part of.
Definition: schematic.h:77
~SCH_ITEM()
Definition: sch_item.cpp:68
EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boards.
Definition: base_struct.h:159
The common library.
virtual wxString GetClass() const override
Function GetClass returns the class name.
Definition: sch_item.h:221
void ClearFlags(STATUS_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition: base_struct.h:233
NETCLASSPTR GetDefault() const
Function GetDefault.
Definition: netclass.h:264
virtual bool operator<(const SCH_ITEM &aItem) const
Definition: sch_item.cpp:215
virtual bool doIsConnected(const wxPoint &aPosition) const
Provide the object specific test to see if it is connected to aPosition.
Definition: sch_item.h:501
std::unordered_set< SCH_ITEM * > SCH_ITEM_SET
Definition: sch_item.h:147
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:194
bool IsConnected(const wxPoint &aPoint) const
Test the item to see if it is connected to aPoint.
Definition: sch_item.cpp:141
KICAD_T Type() const
Function Type()
Definition: base_struct.h:193
SCH_ITEM(EDA_ITEM *aParent, KICAD_T aType)
Definition: sch_item.cpp:50