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 
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  if( newItem->Type() == SCH_COMPONENT_T )
88  {
89  SCH_COMPONENT* component = (SCH_COMPONENT*) newItem;
90 
91  for( SCH_PIN* pin : component->GetSchPins() )
92  pin->ClearFlags( SELECTED | BRIGHTENED );
93 
94  for( SCH_FIELD& field : component->GetFields() )
95  field.ClearFlags( SELECTED | BRIGHTENED );
96  }
97 
98  if( newItem->Type() == SCH_SHEET_T )
99  {
100  SCH_SHEET* sheet = (SCH_SHEET*) newItem;
101 
102  for( SCH_FIELD& field : sheet->GetFields() )
103  field.ClearFlags( SELECTED | BRIGHTENED );
104 
105  for( SCH_SHEET_PIN* pin : sheet->GetPins() )
106  pin->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 
129 void 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 
138 bool SCH_ITEM::IsConnected( const wxPoint& 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  // Warning: the m_connection_map can be empty.
150  if( m_connection_map.size() && m_connection_map.count( aSheet ) )
151  return m_connection_map.at( aSheet );
152 
153  return nullptr;
154 }
155 
156 
158 {
159  return m_connected_items[ aSheet ];
160 }
161 
162 
163 void SCH_ITEM::AddConnectionTo( const SCH_SHEET_PATH& aSheet, SCH_ITEM* aItem )
164 {
165  m_connected_items[ aSheet ].insert( aItem );
166 }
167 
168 
170  CONNECTION_GRAPH* aGraph )
171 {
172  if( Connection( aSheet ) )
173  {
174  Connection( aSheet )->Reset();
175  Connection( aSheet )->SetSheet( aSheet );
176  return Connection( aSheet );
177  }
178 
179  auto connection = new SCH_CONNECTION( this );
180  connection->SetSheet( aSheet );
181  m_connection_map.insert( std::make_pair( aSheet, connection ) );
182  connection->SetGraph( aGraph );
183 
184  return connection;
185 }
186 
187 
189 {
190  wxFAIL_MSG( wxT( "SwapData() method not implemented for class " ) + GetClass() );
191 }
192 
193 
194 bool SCH_ITEM::operator < ( const SCH_ITEM& aItem ) const
195 {
196  if( Type() != aItem.Type() )
197  return Type() < aItem.Type();
198 
199  if( m_Uuid != aItem.m_Uuid )
200  return m_Uuid < aItem.m_Uuid;
201 
202  if( GetPosition().x != aItem.GetPosition().x )
203  return GetPosition().x < aItem.GetPosition().x;
204 
205  return GetPosition().y < aItem.GetPosition().y;
206 }
207 
208 
209 void SCH_ITEM::Plot( PLOTTER* aPlotter )
210 {
211  wxFAIL_MSG( wxT( "Plot() method not implemented for class " ) + GetClass() );
212 }
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:191
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:157
std::unordered_map< SCH_SHEET_PATH, ITEM_SET > m_connected_items
Stores pointers to other items that are connected to this one, per sheet.
Definition: sch_item.h:198
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:163
#define SKIP_STRUCT
flag indicating that the structure should be ignored
Definition: base_struct.h:128
EDA_ITEMS m_connections
Definition: sch_item.h:192
SCHEMATIC * Schematic() const
Searches the item hierarchy to find a SCHEMATIC.
Definition: sch_item.cpp:113
void SetSheet(SCH_SHEET_PATH aSheet)
FIELDS_AUTOPLACED m_fieldsAutoplaced
Definition: sch_item.h:193
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.
virtual const wxPoint GetPosition() const
Definition: base_struct.h:337
std::vector< SCH_FIELD > & GetFields()
Definition: sch_sheet.h:268
#define SELECTED
Definition: base_struct.h:124
Definition: common.h:68
SCH_CONNECTION * InitializeConnection(const SCH_SHEET_PATH &aPath, CONNECTION_GRAPH *aGraph)
Creates a new connection object associated with this object.
Definition: sch_item.cpp:169
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:188
void Reset()
Clears connectivity information.
#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
bool m_connectivity_dirty
True if connectivity info might be out of date.
Definition: sch_item.h:204
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:201
SCH_SHEET_PATH.
std::unordered_set< SCH_ITEM * > ITEM_SET
Definition: sch_item.h:146
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
SCH_CONNECTION * Connection(const SCH_SHEET_PATH &aPath) const
Retrieve the connection associated with this object in the given sheet.
Definition: sch_item.cpp:147
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:209
Base plotter engine class.
Definition: plotter.h:114
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:88
~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: base_struct.h:159
The common library.
virtual wxString GetClass() const override
Function GetClass returns the class name.
Definition: sch_item.h:213
void ClearFlags(STATUS_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition: base_struct.h:233
virtual bool operator<(const SCH_ITEM &aItem) const
Definition: sch_item.cpp:194
virtual bool doIsConnected(const wxPoint &aPosition) const
Provide the object specific test to see if it is connected to aPosition.
Definition: sch_item.h:510
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:186
bool IsConnected(const wxPoint &aPoint) const
Test the item to see if it is connected to aPoint.
Definition: sch_item.cpp:138
SCH_PIN_PTRS GetSchPins(const SCH_SHEET_PATH *aSheet=nullptr) const
Retrieves a list of the SCH_PINs for the given sheet path.
KICAD_T Type() const
Function Type()
Definition: base_struct.h:193
SCH_ITEM(EDA_ITEM *aParent, KICAD_T aType)
Definition: sch_item.cpp:47