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 The 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 <project.h>
28#include <schematic.h>
29#include <sch_screen.h>
30
31// For SCH parsing
34#include <richio.h>
35
37
38namespace KI_TEST
39{
40
41void DumpSchematicToFile( SCHEMATIC& aSchematic, SCH_SHEET& aSheet, const std::string& aFilename )
42{
44 io.SaveSchematicFile( aFilename, &aSheet, &aSchematic );
45}
46
47void LoadSheetSchematicContents( const std::string& fileName, SCH_SHEET* sheet )
48{
49 std::ifstream fileStream;
50 fileStream.open( fileName );
51 wxASSERT( fileStream.is_open() );
53 reader.SetStream( fileStream );
54 SCH_IO_KICAD_SEXPR_PARSER parser( &reader, nullptr, 0, sheet );
55 parser.ParseSchematic( sheet );
56}
57
58void LoadHierarchy( SCHEMATIC* schematic, SCH_SHEET* sheet, const std::string& sheetFilename,
59 std::unordered_map<std::string, SCH_SCREEN*>& parsedScreens )
60{
61 SCH_SCREEN* screen = nullptr;
62
63 if( !sheet->GetScreen() )
64 {
65 // Construct paths
66 const wxFileName fileName( sheetFilename );
67 const std::string filePath( fileName.GetPath( wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR ) );
68 const std::string fileBareName( fileName.GetFullName() );
69
70 // Check for existing screen
71 auto screenFound = parsedScreens.find( fileBareName );
72 if( screenFound != parsedScreens.end() )
73 screen = screenFound->second;
74
75 // Configure sheet with existing screen, or load screen
76 if( screen )
77 {
78 // Screen already loaded - assign to sheet
79 sheet->SetScreen( screen );
80 sheet->GetScreen()->SetParent( schematic );
81 }
82 else
83 {
84 // Load screen and assign to sheet
85 screen = new SCH_SCREEN( schematic );
86 parsedScreens.insert( { fileBareName, screen } );
87 sheet->SetScreen( screen );
88 sheet->GetScreen()->SetFileName( sheetFilename );
89 LoadSheetSchematicContents( sheetFilename, sheet );
90 }
91
92 // Recurse through child sheets
93 for( SCH_ITEM* item : sheet->GetScreen()->Items().OfType( SCH_SHEET_T ) )
94 {
95 SCH_SHEET* childSheet = static_cast<SCH_SHEET*>( item );
96 wxFileName childSheetFilename = childSheet->GetFileName();
97 if( !childSheetFilename.IsAbsolute() )
98 childSheetFilename.MakeAbsolute( filePath );
99 std::string childSheetFullFilename( childSheetFilename.GetFullPath() );
100 LoadHierarchy( schematic, childSheet, childSheetFullFilename, parsedScreens );
101 }
102 }
103}
104
105std::unique_ptr<SCHEMATIC> LoadHierarchyFromRoot( const std::string& rootFilename,
107{
108 std::unique_ptr<SCHEMATIC> schematic( new SCHEMATIC( nullptr ) );
109 std::unordered_map<std::string, SCH_SCREEN*> parsedScreens;
110
111 schematic->SetProject( project );
112 schematic->Reset();
113 SCH_SHEET* defaultSheet = schematic->GetTopLevelSheet( 0 );
114
115 SCH_SHEET* rootSheet = new SCH_SHEET( schematic.get() );
116 LoadHierarchy( schematic.get(), rootSheet, rootFilename, parsedScreens );
117 schematic->AddTopLevelSheet( rootSheet );
118 schematic->RemoveTopLevelSheet( defaultSheet );
119 delete defaultSheet;
120
121 return schematic;
122}
123
124
125void LoadSchematic( SETTINGS_MANAGER& aSettingsManager, const wxString& aRelPath,
126 std::unique_ptr<SCHEMATIC>& aSchematic )
127{
128 if( aSchematic )
129 {
130 PROJECT* prj = &aSchematic->Project();
131
132 aSchematic->SetProject( nullptr );
133 aSettingsManager.UnloadProject( prj, false );
134 aSchematic->Reset();
135 }
136
137 std::string absPath = GetEeschemaTestDataDir() + aRelPath.ToStdString();
138 wxFileName projectFile( absPath + ".kicad_pro" );
139 wxFileName legacyProject( absPath + ".pro" );
140 std::string schematicPath = absPath + ".kicad_sch";
141
142 if( projectFile.Exists() )
143 aSettingsManager.LoadProject( projectFile.GetFullPath() );
144 else if( legacyProject.Exists() )
145 aSettingsManager.LoadProject( legacyProject.GetFullPath() );
146 else
147 aSettingsManager.LoadProject( "" );
148
149 aSettingsManager.Prj().SetElem( PROJECT::ELEM::LEGACY_SYMBOL_LIBS, nullptr );
150
151 aSchematic = LoadHierarchyFromRoot( schematicPath, &aSettingsManager.Prj() );
152
153 SCH_SCREENS screens( aSchematic->Root() );
154
155 for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
156 screen->UpdateLocalLibSymbolLinks();
157
158 SCH_SHEET_LIST sheets = aSchematic->BuildSheetListSortedByPageNumbers();
159
160 // Restore all of the loaded symbol instances from the root sheet screen.
161 sheets.UpdateSymbolInstanceData( aSchematic->RootScreen()->GetSymbolInstances() );
162 sheets.UpdateSheetInstanceData( aSchematic->RootScreen()->GetSheetInstances() );
163
164 if( aSchematic->RootScreen()->GetFileFormatVersionAtLoad() < 20230221 )
166
167 if( aSchematic->RootScreen()->GetFileFormatVersionAtLoad() < 20221206 )
168 {
169 for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
170 screen->MigrateSimModels();
171 }
172
173
174 sheets.AnnotatePowerSymbols();
175
176 // NOTE: This is required for multi-unit symbols to be correct
177 // Normally called from SCH_EDIT_FRAME::FixupJunctions() but could be refactored
178 for( SCH_SHEET_PATH& sheet : sheets )
179 sheet.UpdateAllScreenReferences();
180
181 // NOTE: SchematicCleanUp is not called; QA schematics must already be clean or else
182 // SchematicCleanUp must be freed from its UI dependencies.
183
184 aSchematic->ConnectionGraph()->Recalculate( sheets, true );
185}
186
187} // namespace KI_TEST
virtual void SetParent(EDA_ITEM *aParent)
Definition eda_item.h:113
EE_TYPE OfType(KICAD_T aType) const
Definition sch_rtree.h:241
Container for project specific data.
Definition project.h:65
virtual void SetElem(PROJECT::ELEM aIndex, _ELEM *aElem)
Definition project.cpp:380
@ LEGACY_SYMBOL_LIBS
Definition project.h:72
Holds all the data relating to one schematic.
Definition schematic.h:88
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:167
Container class that holds multiple SCH_SCREEN objects in a hierarchy.
Definition sch_screen.h:747
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()
Get the full RTree, usually for iterating.
Definition sch_screen.h:118
void SetFileName(const wxString &aFileName)
Set the file name for this screen to aFileName.
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:48
wxString GetFileName() const
Return the filename corresponding to this sheet.
Definition sch_sheet.h:376
SCH_SCREEN * GetScreen() const
Definition sch_sheet.h:145
void SetScreen(SCH_SCREEN *aScreen)
Set the SCH_SCREEN associated with this sheet to aScreen.
bool LoadProject(const wxString &aFullPath, bool aSetActive=true)
Load a project or sets up a new project with a specified path.
bool UnloadProject(PROJECT *aProject, bool aSave=true)
Save, unload and unregister 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:179