KiCad PCB EDA Suite
sch_sexpr_plugin.h
Go to the documentation of this file.
1#ifndef _SCH_SEXPR_PLUGIN_H_
2#define _SCH_SEXPR_PLUGIN_H_
3
4/*
5 * This program source code file is part of KiCad, a free EDA CAD application.
6 *
7 * Copyright (C) 2020 CERN
8 * Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
9 *
10 * @author Wayne Stambaugh <[email protected]>
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26#include <memory>
27#include <sch_io_mgr.h>
28#include <sch_file_versions.h>
29#include <stack>
30
31
32class KIWAY;
33class LINE_READER;
34class SCH_SCREEN;
35class SCH_SHEET;
37class SCH_SHEET_PATH;
38class SCH_BITMAP;
39class SCH_JUNCTION;
40class SCH_NO_CONNECT;
41class SCH_LINE;
42class SCH_SHAPE;
44class SCH_TEXT;
45class SCH_TEXTBOX;
46class SCH_SYMBOL;
47class SCH_FIELD;
49class PROPERTIES;
50class EE_SELECTION;
52class LIB_SYMBOL;
53class SYMBOL_LIB;
54class BUS_ALIAS;
55
63{
64public:
65
67 virtual ~SCH_SEXPR_PLUGIN();
68
69 const wxString GetName() const override
70 {
71 return wxT( "Eeschema s-expression" );
72 }
73
74 const wxString GetFileExtension() const override
75 {
76 return wxT( "kicad_sch" );
77 }
78
79 const wxString GetLibraryFileExtension() const override
80 {
81 return wxT( "kicad_sym" );
82 }
83
84 void SetProgressReporter( PROGRESS_REPORTER* aReporter ) override
85 {
86 m_progressReporter = aReporter;
87 }
88
94 static const char* PropBuffering;
95
96 int GetModifyHash() const override;
97
98 SCH_SHEET* Load( const wxString& aFileName, SCHEMATIC* aSchematic,
99 SCH_SHEET* aAppendToMe = nullptr,
100 const PROPERTIES* aProperties = nullptr ) override;
101
102 void LoadContent( LINE_READER& aReader, SCH_SHEET* aSheet,
103 int aVersion = SEXPR_SCHEMATIC_FILE_VERSION );
104
105 void Save( const wxString& aFileName, SCH_SHEET* aSheet, SCHEMATIC* aSchematic,
106 const PROPERTIES* aProperties = nullptr ) override;
107
108 void Format( SCH_SHEET* aSheet );
109
110 void Format( EE_SELECTION* aSelection, SCH_SHEET_PATH* aSelectionPath,
111 SCH_SHEET_LIST* aFullSheetHierarchy, OUTPUTFORMATTER* aFormatter,
112 bool aForClipboard );
113
114 void EnumerateSymbolLib( wxArrayString& aSymbolNameList,
115 const wxString& aLibraryPath,
116 const PROPERTIES* aProperties = nullptr ) override;
117 void EnumerateSymbolLib( std::vector<LIB_SYMBOL*>& aSymbolList,
118 const wxString& aLibraryPath,
119 const PROPERTIES* aProperties = nullptr ) override;
120 LIB_SYMBOL* LoadSymbol( const wxString& aLibraryPath, const wxString& aAliasName,
121 const PROPERTIES* aProperties = nullptr ) override;
122 void SaveSymbol( const wxString& aLibraryPath, const LIB_SYMBOL* aSymbol,
123 const PROPERTIES* aProperties = nullptr ) override;
124 void DeleteSymbol( const wxString& aLibraryPath, const wxString& aSymbolName,
125 const PROPERTIES* aProperties = nullptr ) override;
126 void CreateSymbolLib( const wxString& aLibraryPath,
127 const PROPERTIES* aProperties = nullptr ) override;
128 bool DeleteSymbolLib( const wxString& aLibraryPath,
129 const PROPERTIES* aProperties = nullptr ) override;
130 void SaveLibrary( const wxString& aLibraryPath,
131 const PROPERTIES* aProperties = nullptr ) override;
132
133 bool CheckHeader( const wxString& aFileName ) override;
134 bool IsSymbolLibWritable( const wxString& aLibraryPath ) override;
135
136 const wxString& GetError() const override { return m_error; }
137
138 static LIB_SYMBOL* ParseLibSymbol( LINE_READER& aReader,
139 int aVersion = SEXPR_SCHEMATIC_FILE_VERSION );
140 static void FormatLibSymbol( LIB_SYMBOL* aPart, OUTPUTFORMATTER& aFormatter );
141
142private:
143 void loadHierarchy( SCH_SHEET* aSheet );
144 void loadFile( const wxString& aFileName, SCH_SHEET* aSheet );
145
146 void saveSymbol( SCH_SYMBOL* aSymbol, SCH_SHEET_PATH* aSheetPath, int aNestLevel,
147 bool aForClipboard );
148 void saveField( SCH_FIELD* aField, int aNestLevel );
149 void saveBitmap( SCH_BITMAP* aBitmap, int aNestLevel );
150 void saveSheet( SCH_SHEET* aSheet, int aNestLevel );
151 void saveJunction( SCH_JUNCTION* aJunction, int aNestLevel );
152 void saveNoConnect( SCH_NO_CONNECT* aNoConnect, int aNestLevel );
153 void saveBusEntry( SCH_BUS_ENTRY_BASE* aBusEntry, int aNestLevel );
154 void saveLine( SCH_LINE* aLine, int aNestLevel );
155 void saveShape( SCH_SHAPE* aShape, int aNestLevel );
156 void saveText( SCH_TEXT* aText, int aNestLevel );
157 void saveTextBox( SCH_TEXTBOX* aText, int aNestLevel );
158 void saveBusAlias( std::shared_ptr<BUS_ALIAS> aAlias, int aNestLevel );
159 void saveInstances( const std::vector<SCH_SHEET_INSTANCE>& aSheets,
160 const std::vector<SYMBOL_INSTANCE_REFERENCE>& aSymbols, int aNestLevel );
161
162 void cacheLib( const wxString& aLibraryFileName, const PROPERTIES* aProperties );
163 bool isBuffering( const PROPERTIES* aProperties );
164
165protected:
168
169 wxString m_error;
172
173 wxString m_path;
174 std::stack<wxString> m_currentPath;
179
181 void init( SCHEMATIC* aSchematic, const PROPERTIES* aProperties = nullptr );
182};
183
184#endif // _SCH_SEXPR_PLUGIN_H_
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition: kiway.h:267
Define a library symbol object.
Definition: lib_symbol.h:99
An abstract class from which implementation specific LINE_READERs may be derived to read single lines...
Definition: richio.h:81
An interface used to output 8 bit text in a convenient way.
Definition: richio.h:310
A progress reporter interface for use in multi-threaded environments.
A name/value tuple with unique names and optional values.
Definition: properties.h:34
Holds all the data relating to one schematic.
Definition: schematic.h:60
Object to handle a bitmap image that can be inserted in a schematic.
Definition: sch_bitmap.h:41
Base class for a bus or wire entry.
Definition: sch_bus_entry.h:38
Instances are attached to a symbol or sheet and provide a place for the symbol's value,...
Definition: sch_field.h:50
Segment description base class to describe items which have 2 end points (track, wire,...
Definition: sch_line.h:38
Base class that schematic file and library loading and saving plugins should derive from.
Definition: sch_io_mgr.h:153
A cache assistant for the KiCad s-expression symbol libraries.
A SCH_PLUGIN derivation for loading schematic files using the new s-expression file format.
wxString m_error
For throwing exceptions or errors on partial loads.
void saveBusAlias(std::shared_ptr< BUS_ALIAS > aAlias, int aNestLevel)
const wxString GetLibraryFileExtension() const override
Return the library file extension for the SCH_PLUGIN object.
void Save(const wxString &aFileName, SCH_SHEET *aSheet, SCHEMATIC *aSchematic, const PROPERTIES *aProperties=nullptr) override
Write aSchematic to a storage file in a format that this SCH_PLUGIN implementation knows about,...
void SaveSymbol(const wxString &aLibraryPath, const LIB_SYMBOL *aSymbol, const PROPERTIES *aProperties=nullptr) override
Write aSymbol to an existing library located at aLibraryPath.
void init(SCHEMATIC *aSchematic, const PROPERTIES *aProperties=nullptr)
initialize PLUGIN like a constructor would.
wxString m_path
Root project path for loading child sheets.
void saveField(SCH_FIELD *aField, int aNestLevel)
void saveTextBox(SCH_TEXTBOX *aText, int aNestLevel)
const wxString GetFileExtension() const override
Return the file extension for the SCH_PLUGIN.
SCHEMATIC * m_schematic
OUTPUTFORMATTER * m_out
The formatter for saving SCH_SCREEN objects.
void Format(SCH_SHEET *aSheet)
bool DeleteSymbolLib(const wxString &aLibraryPath, const PROPERTIES *aProperties=nullptr) override
Delete an existing symbol library and returns true if successful, or if library does not exist return...
static void FormatLibSymbol(LIB_SYMBOL *aPart, OUTPUTFORMATTER &aFormatter)
void saveInstances(const std::vector< SCH_SHEET_INSTANCE > &aSheets, const std::vector< SYMBOL_INSTANCE_REFERENCE > &aSymbols, int aNestLevel)
const wxString GetName() const override
Return a brief hard coded name for this SCH_PLUGIN.
std::stack< wxString > m_currentPath
Stack to maintain nested sheet paths.
void loadFile(const wxString &aFileName, SCH_SHEET *aSheet)
void saveLine(SCH_LINE *aLine, int aNestLevel)
virtual ~SCH_SEXPR_PLUGIN()
const wxString & GetError() const override
Return an error string to the caller.
void SaveLibrary(const wxString &aLibraryPath, const PROPERTIES *aProperties=nullptr) override
bool isBuffering(const PROPERTIES *aProperties)
SCH_SHEET * m_rootSheet
The root sheet of the schematic being loaded.
void SetProgressReporter(PROGRESS_REPORTER *aReporter) override
Set an optional progress reporter.
void saveText(SCH_TEXT *aText, int aNestLevel)
static LIB_SYMBOL * ParseLibSymbol(LINE_READER &aReader, int aVersion=SEXPR_SCHEMATIC_FILE_VERSION)
void DeleteSymbol(const wxString &aLibraryPath, const wxString &aSymbolName, const PROPERTIES *aProperties=nullptr) override
Delete the entire LIB_SYMBOL associated with aAliasName from the library aLibraryPath.
void EnumerateSymbolLib(wxArrayString &aSymbolNameList, const wxString &aLibraryPath, const PROPERTIES *aProperties=nullptr) override
Populate a list of LIB_SYMBOL alias names contained within the library aLibraryPath.
void saveSheet(SCH_SHEET *aSheet, int aNestLevel)
SCH_SEXPR_PLUGIN_CACHE * m_cache
LIB_SYMBOL * LoadSymbol(const wxString &aLibraryPath, const wxString &aAliasName, const PROPERTIES *aProperties=nullptr) override
Load a LIB_SYMBOL object having aPartName from the aLibraryPath containing a library format that this...
void CreateSymbolLib(const wxString &aLibraryPath, const PROPERTIES *aProperties=nullptr) override
Create a new empty symbol library at aLibraryPath.
void LoadContent(LINE_READER &aReader, SCH_SHEET *aSheet, int aVersion=SEXPR_SCHEMATIC_FILE_VERSION)
bool IsSymbolLibWritable(const wxString &aLibraryPath) override
Return true if the library at aLibraryPath is writable.
PROGRESS_REPORTER * m_progressReporter
void saveSymbol(SCH_SYMBOL *aSymbol, SCH_SHEET_PATH *aSheetPath, int aNestLevel, bool aForClipboard)
static const char * PropBuffering
The property used internally by the plugin to enable cache buffering which prevents the library file ...
void saveShape(SCH_SHAPE *aShape, int aNestLevel)
void cacheLib(const wxString &aLibraryFileName, const PROPERTIES *aProperties)
void saveBitmap(SCH_BITMAP *aBitmap, int aNestLevel)
bool CheckHeader(const wxString &aFileName) override
Return true if the first line in aFileName begins with the expected header.
void saveBusEntry(SCH_BUS_ENTRY_BASE *aBusEntry, int aNestLevel)
int m_version
Version of file being loaded.
int GetModifyHash() const override
Return the modification hash from the library cache.
void saveNoConnect(SCH_NO_CONNECT *aNoConnect, int aNestLevel)
void saveJunction(SCH_JUNCTION *aJunction, int aNestLevel)
SCH_SHEET * Load(const wxString &aFileName, SCHEMATIC *aSchematic, SCH_SHEET *aAppendToMe=nullptr, const PROPERTIES *aProperties=nullptr) override
Load information from some input file format that this SCH_PLUGIN implementation knows about,...
void loadHierarchy(SCH_SHEET *aSheet)
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
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:55
Schematic symbol object.
Definition: sch_symbol.h:79
Object used to load, save, search, and otherwise manipulate symbol library files.
#define SEXPR_SCHEMATIC_FILE_VERSION
Schematic file version.
A simple container for sheet instance information.
A simple container for schematic symbol instance information.