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-2019 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 <general.h>
39 
40 
41 /* Constructor and destructor for SCH_ITEM */
42 /* They are not inline because this creates problems with gcc at linking time
43  * in debug mode
44  */
45 
46 SCH_ITEM::SCH_ITEM( EDA_ITEM* aParent, KICAD_T aType ) :
47  EDA_ITEM( aParent, aType )
48 {
49  m_Layer = LAYER_WIRE; // It's only a default, in fact
50  m_connectivity_dirty = true;
51 }
52 
53 
54 SCH_ITEM::SCH_ITEM( const SCH_ITEM& aItem ) :
55  EDA_ITEM( aItem )
56 {
57  m_Layer = aItem.m_Layer;
58  m_connectivity_dirty = true;
59 }
60 
61 
63 {
64  // Do not let the connections container go out of scope with any objects or they
65  // will be deleted by the container will cause the Eeschema to crash. These objects
66  // are owned by the sheet object container.
67  if( !m_connections.empty() )
68  m_connections.clear();
69 
70  for( const auto& it : m_connection_map )
71  delete it.second;
72 }
73 
74 
75 SCH_ITEM* SCH_ITEM::Duplicate( bool doClone )
76 {
77  SCH_ITEM* newItem = (SCH_ITEM*) Clone();
78 
79  if( doClone )
80  newItem->SetTimeStamp( GetTimeStamp() );
81 
82  newItem->ClearFlags( SELECTED | HIGHLIGHTED | BRIGHTENED );
83 
84  if( newItem->Type() == SCH_COMPONENT_T )
85  {
86  SCH_COMPONENT* component = (SCH_COMPONENT*) newItem;
87 
88  for( SCH_PIN& pin : component->GetPins() )
90 
91  std::vector<SCH_FIELD*> fields;
92  component->GetFields( fields, false );
93 
94  for( SCH_FIELD* field : fields )
95  field->ClearFlags( SELECTED | HIGHLIGHTED | BRIGHTENED );
96  }
97 
98  if( newItem->Type() == SCH_SHEET_T )
99  {
100  SCH_SHEET* sheet = (SCH_SHEET*) newItem;
101 
102  for( SCH_SHEET_PIN& pin : sheet->GetPins() )
104  }
105 
106  return newItem;
107 }
108 
109 
110 void SCH_ITEM::ViewGetLayers( int aLayers[], int& aCount ) const
111 {
112  // Basic fallback
113  aCount = 2;
114  aLayers[0] = LAYER_DEVICE;
115  aLayers[1] = LAYER_SELECTION_SHADOWS;
116 }
117 
118 
119 bool SCH_ITEM::IsConnected( const wxPoint& aPosition ) const
120 {
121  if( ( m_Flags & STRUCT_DELETED ) || ( m_Flags & SKIP_STRUCT ) )
122  return false;
123 
124  return doIsConnected( aPosition );
125 }
126 
127 
129 {
130  if( m_connection_map.count( aSheet ) )
131  return m_connection_map.at( aSheet );
132 
133  return nullptr;
134 }
135 
136 
137 std::unordered_set<SCH_ITEM*>& SCH_ITEM::ConnectedItems()
138 {
139  return m_connected_items;
140 }
141 
142 
144 {
145  m_connected_items.insert( aItem );
146 }
147 
148 
150 {
151  if( Connection( aSheet ) )
152  {
153  Connection( aSheet )->Reset();
154  Connection( aSheet )->SetSheet( aSheet );
155  return Connection( aSheet );
156  }
157 
158  auto connection = new SCH_CONNECTION( this );
159  connection->SetSheet( aSheet );
160  m_connection_map.insert( std::make_pair( aSheet, connection ) );
161  return connection;
162 }
163 
164 
166 {
167  wxFAIL_MSG( wxT( "SwapData() method not implemented for class " ) + GetClass() );
168 }
169 
170 
171 bool SCH_ITEM::operator < ( const SCH_ITEM& aItem ) const
172 {
173  wxCHECK_MSG( false, this->Type() < aItem.Type(),
174  wxT( "Less than operator not defined for " ) + GetClass() );
175 }
176 
177 
178 void SCH_ITEM::Plot( PLOTTER* aPlotter )
179 {
180  wxFAIL_MSG( wxT( "Plot() method not implemented for class " ) + GetClass() );
181 }
Class 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:119
SCH_SHEET_PINS & GetPins()
Definition: sch_sheet.h:335
void AddConnectionTo(SCH_ITEM *aItem)
Adds a connection link between this item and another.
Definition: sch_item.cpp:143
SCH_ITEM * Duplicate(bool doClone=false)
Routine to create a new copy of given item.
Definition: sch_item.cpp:75
#define SKIP_STRUCT
flag indicating that the structure should be ignored
Definition: base_struct.h:131
EDA_ITEMS m_connections
List of items connected to this item.
Definition: sch_item.h:120
void SetSheet(SCH_SHEET_PATH aSheet)
std::unordered_set< SCH_ITEM * > m_connected_items
Stores pointers to other items that are connected to this one (schematic only)
Definition: sch_item.h:125
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.
#define SELECTED
Definition: base_struct.h:127
virtual void SwapData(SCH_ITEM *aItem)
Function SwapData swap the internal data structures aItem with the schematic item.
Definition: sch_item.cpp:165
void Reset()
Clears connectivity information.
timestamp_t GetTimeStamp() const
Definition: base_struct.h:216
#define BRIGHTENED
item is drawn with a bright contour
Definition: base_struct.h:143
#define STRUCT_DELETED
flag indication structures to be erased
Definition: base_struct.h:129
bool m_connectivity_dirty
True if connectivity info might be out of date.
Definition: sch_item.h:131
wxLogTrace helper definitions.
Define a sheet pin (label) used in sheets to create hierarchical schematics.
Definition: sch_sheet.h:75
std::unordered_map< SCH_SHEET_PATH, SCH_CONNECTION * > m_connection_map
Stores connectivity information, per sheet.
Definition: sch_item.h:128
Class SCH_SHEET_PATH.
void ViewGetLayers(int aLayers[], int &aCount) const override
Function ViewGetLayers returns the layers the item is drawn on (which may be more than its "home" lay...
Definition: sch_item.cpp:110
SCH_CONNECTION * Connection(const SCH_SHEET_PATH &aPath) const
Retrieves the connection associated with this object in the given sheet.
Definition: sch_item.cpp:128
std::unordered_set< SCH_ITEM * > & ConnectedItems()
Retrieves the set of items connected to this item (schematic only)
Definition: sch_item.cpp:137
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition: sch_sheet.h:209
virtual void Plot(PLOTTER *aPlotter)
Function Plot plots the schematic item to aPlotter.
Definition: sch_item.cpp:178
Base plotter engine class.
Definition: plotter.h:97
void GetPins(std::vector< LIB_PIN * > &aPinsList)
Populate a vector with all the pins from the library object.
#define HIGHLIGHTED
item is drawn in normal colors, when the rest is darkened
Definition: base_struct.h:142
Each graphical item can have a SCH_CONNECTION describing its logical connection (to a bus or net).
STATUS_FLAGS m_Flags
Flag bits for editing and other uses.
Definition: base_struct.h:187
Class SCH_COMPONENT describes a real schematic component.
Definition: sch_component.h:73
~SCH_ITEM()
Definition: sch_item.cpp:62
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:163
The common library.
virtual wxString GetClass() const override
Function GetClass returns the class name.
Definition: sch_item.h:140
void ClearFlags(STATUS_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition: base_struct.h:266
virtual bool operator<(const SCH_ITEM &aItem) const
Definition: sch_item.cpp:171
virtual bool doIsConnected(const wxPoint &aPosition) const
Function doIsConnected provides the object specific test to see if it is connected to aPosition.
Definition: sch_item.h:413
SCH_CONNECTION * InitializeConnection(const SCH_SHEET_PATH &aPath)
Creates a new connection object associated with this object.
Definition: sch_item.cpp:149
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
Class SCH_ITEM is a base class for any item which can be embedded within the SCHEMATIC container clas...
Definition: sch_item.h:114
bool IsConnected(const wxPoint &aPoint) const
Function IsConnected tests the item to see if it is connected to aPoint.
Definition: sch_item.cpp:119
KICAD_T Type() const
Function Type()
Definition: base_struct.h:210
void SetTimeStamp(timestamp_t aNewTimeStamp)
Definition: base_struct.h:215
SCH_ITEM(EDA_ITEM *aParent, KICAD_T aType)
Definition: sch_item.cpp:46