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 <stambaughw@gmail.com>
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 
32 class KIWAY;
33 class LINE_READER;
34 class SCH_SCREEN;
35 class SCH_SHEET;
36 struct SCH_SHEET_INSTANCE;
37 class SCH_SHEET_PATH;
38 class SCH_BITMAP;
39 class SCH_JUNCTION;
40 class SCH_NO_CONNECT;
41 class SCH_LINE;
42 class SCH_BUS_ENTRY_BASE;
43 class SCH_TEXT;
44 class SCH_SYMBOL;
45 class SCH_FIELD;
47 class PROPERTIES;
48 class EE_SELECTION;
50 class LIB_SYMBOL;
51 class SYMBOL_LIB;
52 class BUS_ALIAS;
53 
61 {
62 public:
63 
65  virtual ~SCH_SEXPR_PLUGIN();
66 
67  const wxString GetName() const override
68  {
69  return wxT( "Eeschema s-expression" );
70  }
71 
72  const wxString GetFileExtension() const override
73  {
74  return wxT( "kicad_sch" );
75  }
76 
77  const wxString GetLibraryFileExtension() const override
78  {
79  return wxT( "kicad_sym" );
80  }
81 
87  static const char* PropBuffering;
88 
89  int GetModifyHash() const override;
90 
91  SCH_SHEET* Load( const wxString& aFileName, SCHEMATIC* aSchematic,
92  SCH_SHEET* aAppendToMe = nullptr,
93  const PROPERTIES* aProperties = nullptr ) override;
94 
95  void LoadContent( LINE_READER& aReader, SCH_SHEET* aSheet,
96  int aVersion = SEXPR_SCHEMATIC_FILE_VERSION );
97 
98  void Save( const wxString& aFileName, SCH_SHEET* aSheet, SCHEMATIC* aSchematic,
99  const PROPERTIES* aProperties = nullptr ) override;
100 
101  void Format( SCH_SHEET* aSheet );
102 
103  void Format( EE_SELECTION* aSelection, SCH_SHEET_PATH* aSelectionPath,
104  SCH_SHEET_LIST* aFullSheetHierarchy, OUTPUTFORMATTER* aFormatter );
105 
106  void EnumerateSymbolLib( wxArrayString& aSymbolNameList,
107  const wxString& aLibraryPath,
108  const PROPERTIES* aProperties = nullptr ) override;
109  void EnumerateSymbolLib( std::vector<LIB_SYMBOL*>& aSymbolList,
110  const wxString& aLibraryPath,
111  const PROPERTIES* aProperties = nullptr ) override;
112  LIB_SYMBOL* LoadSymbol( const wxString& aLibraryPath, const wxString& aAliasName,
113  const PROPERTIES* aProperties = nullptr ) override;
114  void SaveSymbol( const wxString& aLibraryPath, const LIB_SYMBOL* aSymbol,
115  const PROPERTIES* aProperties = nullptr ) override;
116  void DeleteSymbol( const wxString& aLibraryPath, const wxString& aSymbolName,
117  const PROPERTIES* aProperties = nullptr ) override;
118  void CreateSymbolLib( const wxString& aLibraryPath,
119  const PROPERTIES* aProperties = nullptr ) override;
120  bool DeleteSymbolLib( const wxString& aLibraryPath,
121  const PROPERTIES* aProperties = nullptr ) override;
122  void SaveLibrary( const wxString& aLibraryPath,
123  const PROPERTIES* aProperties = nullptr ) override;
124 
125  bool CheckHeader( const wxString& aFileName ) override;
126  bool IsSymbolLibWritable( const wxString& aLibraryPath ) override;
127 
128  const wxString& GetError() const override { return m_error; }
129 
130  static LIB_SYMBOL* ParseLibSymbol( LINE_READER& aReader,
131  int aVersion = SEXPR_SCHEMATIC_FILE_VERSION );
132  static void FormatLibSymbol( LIB_SYMBOL* aPart, OUTPUTFORMATTER& aFormatter );
133 
134 private:
135  void loadHierarchy( SCH_SHEET* aSheet );
136  void loadFile( const wxString& aFileName, SCH_SHEET* aSheet );
137 
138  void saveSymbol( SCH_SYMBOL* aSymbol, SCH_SHEET_PATH* aSheetPath, int aNestLevel );
139  void saveField( SCH_FIELD* aField, int aNestLevel );
140  void saveBitmap( SCH_BITMAP* aBitmap, int aNestLevel );
141  void saveSheet( SCH_SHEET* aSheet, int aNestLevel );
142  void saveJunction( SCH_JUNCTION* aJunction, int aNestLevel );
143  void saveNoConnect( SCH_NO_CONNECT* aNoConnect, int aNestLevel );
144  void saveBusEntry( SCH_BUS_ENTRY_BASE* aBusEntry, int aNestLevel );
145  void saveLine( SCH_LINE* aLine, int aNestLevel );
146  void saveText( SCH_TEXT* aText, int aNestLevel );
147  void saveBusAlias( std::shared_ptr<BUS_ALIAS> aAlias, int aNestLevel );
148  void saveInstances( const std::vector<SCH_SHEET_INSTANCE>& aSheets,
149  const std::vector<SYMBOL_INSTANCE_REFERENCE>& aSymbols, int aNestLevel );
150 
151  void cacheLib( const wxString& aLibraryFileName, const PROPERTIES* aProperties );
152  bool isBuffering( const PROPERTIES* aProperties );
153 
154 protected:
155  int m_version;
157 
158  wxString m_error;
159 
160  wxString m_path;
161  std::stack<wxString> m_currentPath;
166 
168  void init( SCHEMATIC* aSchematic, const PROPERTIES* aProperties = nullptr );
169 };
170 
171 #endif // _SCH_SEXPR_PLUGIN_H_
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
SCHEMATIC * m_schematic
Passed to Load(), the schematic object being loaded.
Instances are attached to a symbol or sheet and provide a place for the symbol's value,...
Definition: sch_field.h:49
int m_version
Version of file being loaded.
An abstract class from which implementation specific LINE_READERs may be derived to read single lines...
Definition: richio.h:80
void saveLine(SCH_LINE *aLine, int aNestLevel)
void saveNoConnect(SCH_NO_CONNECT *aNoConnect, int aNestLevel)
void Format(SCH_SHEET *aSheet)
Holds all the data relating to one schematic.
Definition: schematic.h:59
wxString m_error
For throwing exceptions or errors on partial loads.
Object used to load, save, search, and otherwise manipulate symbol library files.
void saveJunction(SCH_JUNCTION *aJunction, int aNestLevel)
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.
const wxString GetFileExtension() const override
Return the file extension for the SCH_PLUGIN.
std::stack< wxString > m_currentPath
Stack to maintain nested sheet paths.
SCH_SEXPR_PLUGIN_CACHE * m_cache
SCH_SHEET * m_rootSheet
The root sheet of the schematic being loaded..
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 saveText(SCH_TEXT *aText, int aNestLevel)
void saveInstances(const std::vector< SCH_SHEET_INSTANCE > &aSheets, const std::vector< SYMBOL_INSTANCE_REFERENCE > &aSymbols, int aNestLevel)
An interface used to output 8 bit text in a convenient way.
Definition: richio.h:306
void saveField(SCH_FIELD *aField, int aNestLevel)
void SaveSymbol(const wxString &aLibraryPath, const LIB_SYMBOL *aSymbol, const PROPERTIES *aProperties=nullptr) override
Write aSymbol to an existing library located at aLibraryPath.
A name/value tuple with unique names and optional values.
Definition: properties.h:33
Define a library symbol object.
Definition: lib_symbol.h:96
Base class for a bus or wire entry.
Definition: sch_bus_entry.h:42
A cache assistant for the symbol library portion of the SCH_PLUGIN API, and only for the SCH_SEXPR_PL...
void saveBusEntry(SCH_BUS_ENTRY_BASE *aBusEntry, int aNestLevel)
static LIB_SYMBOL * ParseLibSymbol(LINE_READER &aReader, int aVersion=SEXPR_SCHEMATIC_FILE_VERSION)
Base class that schematic file and library loading and saving plugins should derive from.
Definition: sch_io_mgr.h:151
void saveBusAlias(std::shared_ptr< BUS_ALIAS > aAlias, int aNestLevel)
void CreateSymbolLib(const wxString &aLibraryPath, const PROPERTIES *aProperties=nullptr) override
Create a new empty symbol library at aLibraryPath.
static void FormatLibSymbol(LIB_SYMBOL *aPart, OUTPUTFORMATTER &aFormatter)
void cacheLib(const wxString &aLibraryFileName, const PROPERTIES *aProperties)
void SaveLibrary(const wxString &aLibraryPath, const PROPERTIES *aProperties=nullptr) override
A simple container for schematic symbol instance information.
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.
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition: kiway.h:258
bool isBuffering(const PROPERTIES *aProperties)
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
void loadFile(const wxString &aFileName, SCH_SHEET *aSheet)
Object to handle a bitmap image that can be inserted in a schematic.
Definition: sch_bitmap.h:40
A SCH_PLUGIN derivation for loading schematic files using the new s-expression file format.
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition: sch_sheet.h:54
bool IsSymbolLibWritable(const wxString &aLibraryPath) override
Return true if the library at aLibraryPath is writable.
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,...
OUTPUTFORMATTER * m_out
The output formatter for saving SCH_SCREEN objects.
const wxString GetName() const override
Return a brief hard coded name for this SCH_PLUGIN.
void LoadContent(LINE_READER &aReader, SCH_SHEET *aSheet, int aVersion=SEXPR_SCHEMATIC_FILE_VERSION)
const wxString GetLibraryFileExtension() const override
Return the library file extension for the SCH_PLUGIN object.
A simple container for sheet instance information.
wxString m_path
Root project path for loading child sheets.
Schematic symbol object.
Definition: sch_symbol.h:78
Segment description base class to describe items which have 2 end points (track, wire,...
Definition: sch_line.h:37
bool CheckHeader(const wxString &aFileName) override
Return true if the first line in aFileName begins with the expected header.
void saveSheet(SCH_SHEET *aSheet, int aNestLevel)
const wxString & GetError() const override
Return an error string to the caller.
void loadHierarchy(SCH_SHEET *aSheet)
void saveSymbol(SCH_SYMBOL *aSymbol, SCH_SHEET_PATH *aSheetPath, int aNestLevel)
void init(SCHEMATIC *aSchematic, const PROPERTIES *aProperties=nullptr)
initialize PLUGIN like a constructor would.
#define SEXPR_SCHEMATIC_FILE_VERSION
Schematic file version.
int GetModifyHash() const override
Return the modification hash from the library cache.
static const char * PropBuffering
The property used internally by the plugin to enable cache buffering which prevents the library file ...
void saveBitmap(SCH_BITMAP *aBitmap, int aNestLevel)
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...
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...