KiCad PCB EDA Suite
Loading...
Searching...
No Matches
schematic_file_util.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) 2022 KiCad Developers, see AUTHORS.TXT for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 3
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one at
18 * http://www.gnu.org/licenses/
19 */
20
21#include <schematic_file_util.h>
22#include <qa_utils/wx_utils/unit_test_utils.h> // GetEeschemaTestDataDir()
23
25
26#include <connection_graph.h>
27#include <schematic.h>
28#include <sch_screen.h>
29
30// For SCH parsing
33#include <richio.h>
34
36
37namespace KI_TEST
38{
39
40void DumpSchematicToFile( SCHEMATIC& aSchematic, SCH_SHEET& aSheet, const std::string& aFilename )
41{
43 io.SaveSchematicFile( aFilename, &aSheet, &aSchematic );
44}
45
46void LoadSheetSchematicContents( const std::string& fileName, SCH_SHEET* sheet )
47{
48 std::ifstream fileStream;
49 fileStream.open( fileName );
50 wxASSERT( fileStream.is_open() );
52 reader.SetStream( fileStream );
53 SCH_IO_KICAD_SEXPR_PARSER parser( &reader, nullptr, 0, sheet );
54 parser.ParseSchematic( sheet );
55}
56
57void LoadHierarchy( SCHEMATIC* schematic, SCH_SHEET* sheet, const std::string& sheetFilename,
58 std::unordered_map<std::string, SCH_SCREEN*>& parsedScreens )
59{
60 SCH_SCREEN* screen = nullptr;
61
62 if( !sheet->GetScreen() )
63 {
64 // Construct paths
65 const wxFileName fileName( sheetFilename );
66 const std::string filePath( fileName.GetPath( wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR ) );
67 const std::string fileBareName( fileName.GetFullName() );
68
69 // Check for existing screen
70 auto screenFound = parsedScreens.find( fileBareName );
71 if( screenFound != parsedScreens.end() )
72 screen = screenFound->second;
73
74 // Configure sheet with existing screen, or load screen
75 if( screen )
76 {
77 // Screen already loaded - assign to sheet
78 sheet->SetScreen( screen );
79 sheet->GetScreen()->SetParent( schematic );
80 }
81 else
82 {
83 // Load screen and assign to sheet
84 screen = new SCH_SCREEN( schematic );
85 parsedScreens.insert( { fileBareName, screen } );
86 sheet->SetScreen( screen );
87 sheet->GetScreen()->SetFileName( sheetFilename );
88 LoadSheetSchematicContents( sheetFilename, sheet );
89 }
90
91 // Recurse through child sheets
92 for( SCH_ITEM* item : sheet->GetScreen()->Items().OfType( SCH_SHEET_T ) )
93 {
94 SCH_SHEET* childSheet = static_cast<SCH_SHEET*>( item );
95 wxFileName childSheetFilename = childSheet->GetFileName();
96 if( !childSheetFilename.IsAbsolute() )
97 childSheetFilename.MakeAbsolute( filePath );
98 std::string childSheetFullFilename( childSheetFilename.GetFullPath() );
99 LoadHierarchy( schematic, childSheet, childSheetFullFilename, parsedScreens );
100 }
101 }
102}
103
104std::unique_ptr<SCHEMATIC> LoadHierarchyFromRoot( const std::string& rootFilename,
106{
107 std::unique_ptr<SCHEMATIC> schematic( new SCHEMATIC( nullptr ) );
108 std::unordered_map<std::string, SCH_SCREEN*> parsedScreens;
109
110 schematic->SetProject( project );
111 SCH_SHEET* rootSheet = new SCH_SHEET( schematic.get() );
112 schematic->SetRoot( rootSheet );
113 LoadHierarchy( schematic.get(), rootSheet, rootFilename, parsedScreens );
114
115 return schematic;
116}
117
118
119void LoadSchematic( SETTINGS_MANAGER& aSettingsManager, const wxString& aRelPath,
120 std::unique_ptr<SCHEMATIC>& aSchematic )
121{
122 if( aSchematic )
123 {
124 PROJECT* prj = &aSchematic->Prj();
125
126 aSchematic->SetProject( nullptr );
127 aSettingsManager.UnloadProject( prj, false );
128 aSchematic->Reset();
129 }
130
131 std::string absPath = GetEeschemaTestDataDir() + aRelPath.ToStdString();
132 wxFileName projectFile( absPath + ".kicad_pro" );
133 wxFileName legacyProject( absPath + ".pro" );
134 std::string schematicPath = absPath + ".kicad_sch";
135
136 if( projectFile.Exists() )
137 aSettingsManager.LoadProject( projectFile.GetFullPath() );
138 else if( legacyProject.Exists() )
139 aSettingsManager.LoadProject( legacyProject.GetFullPath() );
140 else
141 aSettingsManager.LoadProject( "" );
142
143 aSettingsManager.Prj().SetElem( PROJECT::ELEM::SCH_SYMBOL_LIBS, nullptr );
144
145 aSchematic = LoadHierarchyFromRoot( schematicPath, &aSettingsManager.Prj() );
146
147 SCH_SCREENS screens( aSchematic->Root() );
148
149 for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
150 screen->UpdateLocalLibSymbolLinks();
151
152 SCH_SHEET_LIST sheets = aSchematic->BuildSheetListSortedByPageNumbers();
153
154 // Restore all of the loaded symbol instances from the root sheet screen.
155 sheets.UpdateSymbolInstanceData( aSchematic->RootScreen()->GetSymbolInstances() );
156 sheets.UpdateSheetInstanceData( aSchematic->RootScreen()->GetSheetInstances() );
157
158 if( aSchematic->RootScreen()->GetFileFormatVersionAtLoad() < 20230221 )
160
161 if( aSchematic->RootScreen()->GetFileFormatVersionAtLoad() < 20221206 )
162 {
163 for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
164 screen->MigrateSimModels();
165 }
166
167 sheets.AnnotatePowerSymbols();
168
169 // NOTE: This is required for multi-unit symbols to be correct
170 // Normally called from SCH_EDIT_FRAME::FixupJunctions() but could be refactored
171 for( SCH_SHEET_PATH& sheet : sheets )
172 sheet.UpdateAllScreenReferences();
173
174 // NOTE: SchematicCleanUp is not called; QA schematics must already be clean or else
175 // SchematicCleanUp must be freed from its UI dependencies.
176
177 aSchematic->ConnectionGraph()->Recalculate( sheets, true );
178}
179
180} // namespace KI_TEST
virtual void SetParent(EDA_ITEM *aParent)
Definition: eda_item.h:104
EE_TYPE OfType(KICAD_T aType) const
Definition: sch_rtree.h:238
Container for project specific data.
Definition: project.h:64
virtual void SetElem(PROJECT::ELEM aIndex, _ELEM *aElem)
Definition: project.cpp:348
Holds all the data relating to one schematic.
Definition: schematic.h:77
Object to parser s-expression symbol library and schematic file formats.
void ParseSchematic(SCH_SHEET *aSheet, bool aIsCopyablyOnly=false, int aFileVersion=SEXPR_SCHEMATIC_FILE_VERSION)
Parse the internal LINE_READER object into aSheet.
A SCH_IO derivation for loading schematic files using the new s-expression file format.
void SaveSchematicFile(const wxString &aFileName, SCH_SHEET *aSheet, SCHEMATIC *aSchematic, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Write aSchematic to a storage file in a format that this SCH_IO implementation knows about,...
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:166
Container class that holds multiple SCH_SCREEN objects in a hierarchy.
Definition: sch_screen.h:712
SCH_SCREEN * GetNext()
void FixLegacyPowerSymbolMismatches()
Fix legacy power symbols that have mismatched value text fields and invisible power pin names.
SCH_SCREEN * GetFirst()
EE_RTREE & Items()
Gets the full RTree, usually for iterating.
Definition: sch_screen.h:108
void SetFileName(const wxString &aFileName)
Set the file name for this screen to aFileName.
Definition: sch_screen.cpp:118
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
void UpdateSheetInstanceData(const std::vector< SCH_SHEET_INSTANCE > &aSheetInstances)
Update all of the sheet instance information using aSheetInstances.
void AnnotatePowerSymbols()
Silently annotate the not yet annotated power symbols of the entire hierarchy of the sheet path list.
void UpdateSymbolInstanceData(const std::vector< SCH_SYMBOL_INSTANCE > &aSymbolInstances)
Update all of the symbol instance information using aSymbolInstances.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition: sch_sheet.h:57
wxString GetFileName() const
Return the filename corresponding to this sheet.
Definition: sch_sheet.h:306
SCH_SCREEN * GetScreen() const
Definition: sch_sheet.h:110
void SetScreen(SCH_SCREEN *aScreen)
Set the SCH_SCREEN associated with this sheet to aScreen.
Definition: sch_sheet.cpp:173
bool LoadProject(const wxString &aFullPath, bool aSetActive=true)
Loads a project or sets up a new project with a specified path.
bool UnloadProject(PROJECT *aProject, bool aSave=true)
Saves, unloads and unregisters the given PROJECT.
PROJECT & Prj() const
A helper while we are not MDI-capable – return the one and only project.
LINE_READER that wraps a given std::istream instance.
void SetStream(std::istream &aStream)
Set the stream for this line reader.
std::unique_ptr< SCHEMATIC > LoadHierarchyFromRoot(const std::string &rootFilename, PROJECT *project)
void LoadSheetSchematicContents(const std::string &fileName, SCH_SHEET *sheet)
void LoadHierarchy(SCHEMATIC *schematic, SCH_SHEET *sheet, const std::string &sheetFilename, std::unordered_map< std::string, SCH_SCREEN * > &parsedScreens)
void LoadSchematic(SETTINGS_MANAGER &aSettingsManager, const wxString &aRelPath, std::unique_ptr< SCHEMATIC > &aSchematic)
std::string GetEeschemaTestDataDir()
Get the configured location of Eeschema test data.
void DumpSchematicToFile(SCHEMATIC &aSchematic, SCH_SHEET &aSheet, const std::string &aFilename)
@ SCH_SHEET_T
Definition: typeinfo.h:174