KiCad PCB EDA Suite
schematic.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) 2020 KiCad Developers, see AUTHORS.txt for contributors.
5  *
6  * This program is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation, either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <bus_alias.h>
21 #include <connection_graph.h>
22 #include <erc_settings.h>
23 #include <project.h>
24 #include <project/project_file.h>
25 #include <project/net_settings.h>
26 #include <schematic.h>
27 #include <sch_screen.h>
28 
29 
31  EDA_ITEM( nullptr, SCHEMATIC_T ),
32  m_project( nullptr ),
33  m_rootSheet( nullptr )
34 {
36  m_connectionGraph = new CONNECTION_GRAPH( this );
37 
38  SetProject( aPrj );
39 }
40 
41 
43 {
44  delete m_currentSheet;
45  delete m_connectionGraph;
46 }
47 
48 
50 {
51  // Assume project already saved
52  if( m_project )
53  {
55 
56  delete project.m_ErcSettings;
57  delete project.m_SchematicSettings;
58 
59  project.m_ErcSettings = nullptr;
60  project.m_SchematicSettings = nullptr;
61  }
62 
63  delete m_rootSheet;
64 
65  m_rootSheet = nullptr;
66 
69 }
70 
71 
73 {
74  if( m_project )
75  {
77 
78  delete project.m_ErcSettings;
79  delete project.m_SchematicSettings;
80 
81  project.m_ErcSettings = nullptr;
82  project.m_SchematicSettings = nullptr;
83  }
84 
85  m_project = aPrj;
86 
87  if( m_project )
88  {
90  project.m_ErcSettings = new ERC_SETTINGS( &project, "erc" );
91  project.m_SchematicSettings = new SCHEMATIC_SETTINGS( &project, "schematic" );
92 
95 
96  project.m_ErcSettings->LoadFromFile();
97  }
98 }
99 
100 
102 {
103  wxASSERT( m_project );
104  PROJECT_FILE& project = m_project->GetProjectFile();
105 
106  project.m_TemplateFieldNames = aTemplates;
107 
108  if( project.m_SchematicSettings )
109  project.m_SchematicSettings->m_TemplateFieldNames = aTemplates;
110 }
111 
112 
113 void SCHEMATIC::SetRoot( SCH_SHEET* aRootSheet )
114 {
115  wxCHECK_RET( aRootSheet, "Call to SetRoot with null SCH_SHEET!" );
116 
117  m_rootSheet = aRootSheet;
118 
121 
123 }
124 
125 
127 {
128  return IsValid() ? m_rootSheet->GetScreen() : nullptr;
129 }
130 
131 
132 wxString SCHEMATIC::GetFileName() const
133 {
134  return IsValid() ? m_rootSheet->GetScreen()->GetFileName() : wxString( wxEmptyString );
135 }
136 
137 
139 {
140  wxASSERT( m_project );
142 }
143 
144 
146 {
147  wxASSERT( m_project );
149 }
150 
151 
152 std::shared_ptr<BUS_ALIAS> SCHEMATIC::GetBusAlias( const wxString& aLabel ) const
153 {
154  for( const auto& sheet : GetSheets() )
155  {
156  for( const auto& alias : sheet.LastScreen()->GetBusAliases() )
157  {
158  if( alias->GetName() == aLabel )
159  return alias;
160  }
161  }
162 
163  return nullptr;
164 }
165 
166 
168 {
169  std::vector<wxString> names;
170 
171  SCH_SCREENS allScreens( Root() );
172 
173  for( SCH_SCREEN* screen = allScreens.GetFirst(); screen; screen = allScreens.GetNext() )
174  {
175  for( SCH_ITEM* item : screen->Items() )
176  {
177  switch( item->Type() )
178  {
179  case SCH_PIN_T:
180  {
181  SCH_PIN* pin = static_cast<SCH_PIN*>( item );
182 
183  if( pin->IsPowerConnection() )
184  names.emplace_back( pin->GetName() );
185  }
186  break;
187 
188  case SCH_LABEL_T:
189  case SCH_GLOBAL_LABEL_T:
190  case SCH_HIER_LABEL_T:
191  case SCH_SHEET_PIN_T:
192  {
193  wxString unescaped = static_cast<SCH_TEXT*>( item )->GetShownText();
194 
195  wxString busPrefix;
196  std::vector<wxString> busMembers;
197 
198  if( NET_SETTINGS::ParseBusVector( unescaped, nullptr, nullptr ) )
199  {
200  // Allow netclass assignment to an entire vector.
201  names.emplace_back( EscapeString( unescaped, CTX_NETNAME ) );
202  }
203  else if( NET_SETTINGS::ParseBusGroup( unescaped, &busPrefix, &busMembers ) )
204  {
205  // Allow netclass assignment to an entire group.
206  names.emplace_back( EscapeString( unescaped, CTX_NETNAME ) );
207 
208  // Named bus groups generate a net prefix, unnamed ones don't
209  if( !busPrefix.IsEmpty() )
210  busPrefix += wxT( "." );
211 
212  for( const wxString& member : busMembers )
213  {
214  // Handle alias inside bus group member list
215  if( const std::shared_ptr<BUS_ALIAS>& alias = GetBusAlias( member ) )
216  {
217  for( const wxString& alias_member : alias->Members() )
218  names.emplace_back( busPrefix + alias_member );
219  }
220  else
221  {
222  names.emplace_back( busPrefix + member );
223  }
224  }
225  }
226  else
227  {
228  names.emplace_back( EscapeString( unescaped, CTX_NETNAME ) );
229  }
230  }
231  break;
232 
233  default:
234  break;
235  }
236  }
237  }
238 
239  return names;
240 }
241 
242 
const wxString & GetFileName() const
Definition: sch_screen.h:185
SCH_SCREEN * GetNext()
PROJECT holds project specific data.
Definition: project.h:61
SCH_SHEET_LIST GetSheets() const
Builds and returns an updated schematic hierarchy TODO: can this be cached?
Definition: schematic.h:92
const wxString & GetName() const
Definition: sch_pin.h:102
SCH_SHEET_PATH * m_currentSheet
The sheet path of the sheet currently being edited or displayed.
Definition: schematic.h:58
SCHEMATIC(PROJECT *aPrj)
Definition: schematic.cpp:30
Calculates the connectivity of a schematic and generates netlists.
void push_back(SCH_SHEET *aSheet)
Forwarded method from std::vector.
SCHEMATIC_SETTINGS & Settings() const
Definition: schematic.cpp:138
bool IsValid() const
A simple test if the schematic is loaded, not a complete one.
Definition: schematic.h:111
TEMPLATES * m_TemplateFieldNames
wxString GetFileName() const
Helper to retrieve the filename from the root sheet screen.
Definition: schematic.cpp:132
CONNECTION_GRAPH * m_connectionGraph
Holds and calculates connectivity information of this schematic.
Definition: schematic.h:61
SCH_SCREEN * GetScreen() const
Definition: sch_sheet.h:282
PROJECT_FILE is the backing store for a PROJECT, in JSON format.
Definition: project_file.h:61
void SetRoot(SCH_SHEET *aRootSheet)
Initializes the schematic with a new root sheet.
Definition: schematic.cpp:113
static bool ParseBusGroup(wxString aGroup, wxString *name, std::vector< wxString > *aMemberList)
Parses a bus group label into the name and a list of components.
void SetTemplateFieldNames(TEMPLATES *aTemplates)
Sets up the template fieldnames link if this project is opened in eeschema.
Definition: schematic.cpp:101
std::shared_ptr< BUS_ALIAS > GetBusAlias(const wxString &aLabel) const
Returns a pointer to a bus alias object for the given label, or null if one doesn't exist.
Definition: schematic.cpp:152
TEMPLATES * m_TemplateFieldNames
A pointer to the template fieldnames object owned by the parent SCH_BASE_FRAME.
Definition: project_file.h:148
VTBL_ENTRY PROJECT_FILE & GetProjectFile() const
Definition: project.h:129
bool LoadFromFile(const std::string &aDirectory="") override
Loads the JSON document from the parent and then calls Load()
PROJECT * m_project
Definition: schematic.h:47
ERC_SETTINGS * m_ErcSettings
Eeschema params.
Definition: project_file.h:134
SCHEMATIC_SETTINGS * m_SchematicSettings
Definition: project_file.h:137
Container for ERC settings.
Definition: erc_settings.h:87
SCH_SHEET_PATH.
void SetProject(PROJECT *aPrj)
Definition: schematic.cpp:72
void clear()
Forwarded method from std::vector.
SCH_SHEET * m_rootSheet
The top-level sheet in this schematic hierarchy (or potentially the only one)
Definition: schematic.h:50
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition: sch_sheet.h:216
void Reset()
Initializes this schematic to a blank one, unloading anything existing.
Definition: schematic.cpp:49
SCH_SHEET & Root() const
Definition: schematic.h:97
wxString EscapeString(const wxString &aSource, ESCAPE_CONTEXT aContext)
These Escape/Unescape routines use HTML-entity-reference-style encoding to handle characters which ar...
Definition: string.cpp:50
static bool ParseBusVector(const wxString &aBus, wxString *aName, std::vector< wxString > *aMemberList)
Parses a bus vector (e.g.
EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boards.
Definition: base_struct.h:159
These settings were stored in SCH_BASE_FRAME previously.
SCH_SCREEN * GetFirst()
bool IsPowerConnection() const
Definition: sch_pin.h:108
ERC_SETTINGS & ErcSettings() const
Definition: schematic.cpp:145
SCH_SCREEN * RootScreen() const
Helper to retreive the screen of the root sheet.
Definition: schematic.cpp:126
std::vector< wxString > GetNetClassAssignmentCandidates()
Returns a list of name candidates for netclass assignment.
Definition: schematic.cpp:167
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:187
Container class that holds multiple SCH_SCREEN objects in a hierarchy.
Definition: sch_screen.h:546