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  *
9  * @author Wayne Stambaugh <stambaughw@gmail.com>
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License along
22  * with this program. If not, see <http://www.gnu.org/licenses/>.
23  */
24 
25 #include <memory>
26 #include <sch_io_mgr.h>
27 #include <sch_file_versions.h>
28 #include <stack>
29 
30 
31 class KIWAY;
32 class LINE_READER;
33 class SCH_SCREEN;
34 class SCH_SHEET;
35 struct SCH_SHEET_INSTANCE;
36 class SCH_SHEET_PATH;
37 class SCH_BITMAP;
38 class SCH_JUNCTION;
39 class SCH_NO_CONNECT;
40 class SCH_LINE;
41 class SCH_BUS_ENTRY_BASE;
42 class SCH_TEXT;
43 class SCH_COMPONENT;
44 class SCH_FIELD;
46 class PROPERTIES;
47 class EE_SELECTION;
49 class LIB_PART;
50 class PART_LIB;
51 class BUS_ALIAS;
52 
60 {
61 public:
62 
64  virtual ~SCH_SEXPR_PLUGIN();
65 
66  const wxString GetName() const override
67  {
68  return wxT( "Eeschema s-expression" );
69  }
70 
71  const wxString GetFileExtension() const override
72  {
73  return wxT( "kicad_sch" );
74  }
75 
76  const wxString GetLibraryFileExtension() const override
77  {
78  return wxT( "kicad_sym" );
79  }
80 
86  static const char* PropBuffering;
87 
88  int GetModifyHash() const override;
89 
90  SCH_SHEET* Load( const wxString& aFileName, SCHEMATIC* aSchematic,
91  SCH_SHEET* aAppendToMe = nullptr,
92  const PROPERTIES* aProperties = nullptr ) override;
93 
94  void LoadContent( LINE_READER& aReader, SCH_SHEET* aSheet,
95  int aVersion = SEXPR_SCHEMATIC_FILE_VERSION );
96 
97  void Save( const wxString& aFileName, SCH_SHEET* aSheet, SCHEMATIC* aSchematic,
98  const PROPERTIES* aProperties = nullptr ) override;
99 
100  void Format( SCH_SHEET* aSheet );
101 
102  void Format( EE_SELECTION* aSelection, SCH_SHEET_PATH* aSelectionPath,
103  SCH_SHEET_LIST* aFullSheetHierarchy, OUTPUTFORMATTER* aFormatter );
104 
105  void EnumerateSymbolLib( wxArrayString& aSymbolNameList,
106  const wxString& aLibraryPath,
107  const PROPERTIES* aProperties = nullptr ) override;
108  void EnumerateSymbolLib( std::vector<LIB_PART*>& aSymbolList,
109  const wxString& aLibraryPath,
110  const PROPERTIES* aProperties = nullptr ) override;
111  LIB_PART* LoadSymbol( const wxString& aLibraryPath, const wxString& aAliasName,
112  const PROPERTIES* aProperties = nullptr ) override;
113  void SaveSymbol( const wxString& aLibraryPath, const LIB_PART* aSymbol,
114  const PROPERTIES* aProperties = nullptr ) override;
115  void DeleteSymbol( const wxString& aLibraryPath, const wxString& aSymbolName,
116  const PROPERTIES* aProperties = nullptr ) override;
117  void CreateSymbolLib( const wxString& aLibraryPath,
118  const PROPERTIES* aProperties = nullptr ) override;
119  bool DeleteSymbolLib( const wxString& aLibraryPath,
120  const PROPERTIES* aProperties = nullptr ) override;
121  void SaveLibrary( const wxString& aLibraryPath,
122  const PROPERTIES* aProperties = nullptr ) override;
123 
124  bool CheckHeader( const wxString& aFileName ) override;
125  bool IsSymbolLibWritable( const wxString& aLibraryPath ) override;
126 
127  const wxString& GetError() const override { return m_error; }
128 
129  static LIB_PART* ParsePart( LINE_READER& aReader,
130  int aVersion = SEXPR_SCHEMATIC_FILE_VERSION );
131  static void FormatPart( LIB_PART* aPart, OUTPUTFORMATTER& aFormatter );
132 
133 private:
134  void loadHierarchy( SCH_SHEET* aSheet );
135  void loadFile( const wxString& aFileName, SCH_SHEET* aSheet );
136 
137  void saveSymbol( SCH_COMPONENT* aComponent, SCH_SHEET_PATH* aSheetPath, int aNestLevel );
138  void saveField( SCH_FIELD* aField, int aNestLevel );
139  void saveBitmap( SCH_BITMAP* aBitmap, int aNestLevel );
140  void saveSheet( SCH_SHEET* aSheet, int aNestLevel );
141  void saveJunction( SCH_JUNCTION* aJunction, int aNestLevel );
142  void saveNoConnect( SCH_NO_CONNECT* aNoConnect, int aNestLevel );
143  void saveBusEntry( SCH_BUS_ENTRY_BASE* aBusEntry, int aNestLevel );
144  void saveLine( SCH_LINE* aLine, int aNestLevel );
145  void saveText( SCH_TEXT* aText, int aNestLevel );
146  void saveBusAlias( std::shared_ptr<BUS_ALIAS> aAlias, int aNestLevel );
147  void saveInstances( const std::vector<SCH_SHEET_INSTANCE>& aSheets,
148  const std::vector<SYMBOL_INSTANCE_REFERENCE>& aSymbols, int aNestLevel );
149 
150  void cacheLib( const wxString& aLibraryFileName, const PROPERTIES* aProperties );
151  bool isBuffering( const PROPERTIES* aProperties );
152 
153 protected:
154  int m_version;
156 
157  wxString m_error;
158 
159  wxString m_path;
160  std::stack<wxString> m_currentPath;
165 
167  void init( SCHEMATIC* aSchematic, const PROPERTIES* aProperties = nullptr );
168 };
169 
170 #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:50
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)
static LIB_PART * ParsePart(LINE_READER &aReader, int aVersion=SEXPR_SCHEMATIC_FILE_VERSION)
Holds all the data relating to one schematic.
Definition: schematic.h:59
wxString m_error
For throwing exceptions or errors on partial loads.
void saveJunction(SCH_JUNCTION *aJunction, int aNestLevel)
void DeleteSymbol(const wxString &aLibraryPath, const wxString &aSymbolName, const PROPERTIES *aProperties=nullptr) override
Delete the entire LIB_PART 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)
static void FormatPart(LIB_PART *aPart, OUTPUTFORMATTER &aFormatter)
A name/value tuple with unique names and optional values.
Definition: properties.h:33
Base class for a bus or wire entry.
Definition: sch_bus_entry.h:42
A cache assistant for the part library portion of the SCH_PLUGIN API, and only for the SCH_SEXPR_PLUG...
LIB_PART * LoadSymbol(const wxString &aLibraryPath, const wxString &aAliasName, const PROPERTIES *aProperties=nullptr) override
Load a LIB_PART object having aPartName from the aLibraryPath containing a library format that this S...
void saveBusEntry(SCH_BUS_ENTRY_BASE *aBusEntry, int aNestLevel)
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.
void cacheLib(const wxString &aLibraryFileName, const PROPERTIES *aProperties)
void SaveLibrary(const wxString &aLibraryPath, const PROPERTIES *aProperties=nullptr) override
Define a library symbol object.
Definition: lib_symbol.h:93
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_PART 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:260
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.
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.
Schematic symbol object.
Definition: sch_symbol.h:78
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 init(SCHEMATIC *aSchematic, const PROPERTIES *aProperties=nullptr)
initialize PLUGIN like a constructor would.
#define SEXPR_SCHEMATIC_FILE_VERSION
Symbol library file version.
int GetModifyHash() const override
Return the modification hash from the library cache.
Object used to load, save, search, and otherwise manipulate symbol library files.
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)
void saveSymbol(SCH_COMPONENT *aComponent, SCH_SHEET_PATH *aSheetPath, 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...
void SaveSymbol(const wxString &aLibraryPath, const LIB_PART *aSymbol, const PROPERTIES *aProperties=nullptr) override
Write aSymbol to an existing library located at aLibraryPath.