KiCad PCB EDA Suite
Go to the documentation of this file.
1 #ifndef _SCH_SEXPR_PLUGIN_H_
2 #define _SCH_SEXPR_PLUGIN_H_
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 <>
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
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 <>.
24  */
26 #include <memory>
27 #include <sch_io_mgr.h>
28 #include <sch_file_versions.h>
29 #include <stack>
32 class KIWAY;
33 class LINE_READER;
34 class SCH_SCREEN;
35 class SCH_SHEET;
37 class SCH_SHEET_PATH;
38 class SCH_BITMAP;
39 class SCH_JUNCTION;
40 class SCH_NO_CONNECT;
41 class SCH_LINE;
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;
61 {
62 public:
65  virtual ~SCH_SEXPR_PLUGIN();
67  const wxString GetName() const override
68  {
69  return wxT( "Eeschema s-expression" );
70  }
72  const wxString GetFileExtension() const override
73  {
74  return wxT( "kicad_sch" );
75  }
77  const wxString GetLibraryFileExtension() const override
78  {
79  return wxT( "kicad_sym" );
80  }
82  void SetProgressReporter( PROGRESS_REPORTER* aReporter ) override
83  {
84  m_progressReporter = aReporter;
85  }
92  static const char* PropBuffering;
94  int GetModifyHash() const override;
96  SCH_SHEET* Load( const wxString& aFileName, SCHEMATIC* aSchematic,
97  SCH_SHEET* aAppendToMe = nullptr,
98  const PROPERTIES* aProperties = nullptr ) override;
100  void LoadContent( LINE_READER& aReader, SCH_SHEET* aSheet,
103  void Save( const wxString& aFileName, SCH_SHEET* aSheet, SCHEMATIC* aSchematic,
104  const PROPERTIES* aProperties = nullptr ) override;
106  void Format( SCH_SHEET* aSheet );
108  void Format( EE_SELECTION* aSelection, SCH_SHEET_PATH* aSelectionPath,
109  SCH_SHEET_LIST* aFullSheetHierarchy, OUTPUTFORMATTER* aFormatter );
111  void EnumerateSymbolLib( wxArrayString& aSymbolNameList,
112  const wxString& aLibraryPath,
113  const PROPERTIES* aProperties = nullptr ) override;
114  void EnumerateSymbolLib( std::vector<LIB_SYMBOL*>& aSymbolList,
115  const wxString& aLibraryPath,
116  const PROPERTIES* aProperties = nullptr ) override;
117  LIB_SYMBOL* LoadSymbol( const wxString& aLibraryPath, const wxString& aAliasName,
118  const PROPERTIES* aProperties = nullptr ) override;
119  void SaveSymbol( const wxString& aLibraryPath, const LIB_SYMBOL* aSymbol,
120  const PROPERTIES* aProperties = nullptr ) override;
121  void DeleteSymbol( const wxString& aLibraryPath, const wxString& aSymbolName,
122  const PROPERTIES* aProperties = nullptr ) override;
123  void CreateSymbolLib( const wxString& aLibraryPath,
124  const PROPERTIES* aProperties = nullptr ) override;
125  bool DeleteSymbolLib( const wxString& aLibraryPath,
126  const PROPERTIES* aProperties = nullptr ) override;
127  void SaveLibrary( const wxString& aLibraryPath,
128  const PROPERTIES* aProperties = nullptr ) override;
130  bool CheckHeader( const wxString& aFileName ) override;
131  bool IsSymbolLibWritable( const wxString& aLibraryPath ) override;
133  const wxString& GetError() const override { return m_error; }
135  static LIB_SYMBOL* ParseLibSymbol( LINE_READER& aReader,
137  static void FormatLibSymbol( LIB_SYMBOL* aPart, OUTPUTFORMATTER& aFormatter );
139 private:
140  void loadHierarchy( SCH_SHEET* aSheet );
141  void loadFile( const wxString& aFileName, SCH_SHEET* aSheet );
143  void saveSymbol( SCH_SYMBOL* aSymbol, SCH_SHEET_PATH* aSheetPath, int aNestLevel );
144  void saveField( SCH_FIELD* aField, int aNestLevel );
145  void saveBitmap( SCH_BITMAP* aBitmap, int aNestLevel );
146  void saveSheet( SCH_SHEET* aSheet, int aNestLevel );
147  void saveJunction( SCH_JUNCTION* aJunction, int aNestLevel );
148  void saveNoConnect( SCH_NO_CONNECT* aNoConnect, int aNestLevel );
149  void saveBusEntry( SCH_BUS_ENTRY_BASE* aBusEntry, int aNestLevel );
150  void saveLine( SCH_LINE* aLine, int aNestLevel );
151  void saveText( SCH_TEXT* aText, int aNestLevel );
152  void saveBusAlias( std::shared_ptr<BUS_ALIAS> aAlias, int aNestLevel );
153  void saveInstances( const std::vector<SCH_SHEET_INSTANCE>& aSheets,
154  const std::vector<SYMBOL_INSTANCE_REFERENCE>& aSymbols, int aNestLevel );
156  void cacheLib( const wxString& aLibraryFileName, const PROPERTIES* aProperties );
157  bool isBuffering( const PROPERTIES* aProperties );
159 protected:
160  int m_version;
163  wxString m_error;
167  wxString m_path;
168  std::stack<wxString> m_currentPath;
175  void init( SCHEMATIC* aSchematic, const PROPERTIES* aProperties = nullptr );
176 };
178 #endif // _SCH_SEXPR_PLUGIN_H_
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
SCHEMATIC * m_schematic
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)
A progress reporter interface for use in multi-threaded environments.
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_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:309
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:152
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: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
PROGRESS_REPORTER * m_progressReporter
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,...
The 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 SetProgressReporter(PROGRESS_REPORTER *aReporter) override
Set an optional progress reporter.
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.
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...