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 class SCH_BITMAP;
36 class SCH_JUNCTION;
37 class SCH_NO_CONNECT;
38 class SCH_LINE;
39 class SCH_BUS_ENTRY_BASE;
40 class SCH_TEXT;
41 class SCH_COMPONENT;
42 class SCH_FIELD;
43 class PROPERTIES;
44 class EE_SELECTION;
46 class LIB_PART;
47 class PART_LIB;
48 class BUS_ALIAS;
49 
57 {
58 public:
59 
61  virtual ~SCH_SEXPR_PLUGIN();
62 
63  const wxString GetName() const override
64  {
65  return wxT( "Eeschema s-expression" );
66  }
67 
68  const wxString GetFileExtension() const override
69  {
70  return wxT( "kicad_sch" );
71  }
72 
73  const wxString GetLibraryFileExtension() const override
74  {
75  return wxT( "kicad_sym" );
76  }
77 
83  static const char* PropBuffering;
84 
85  int GetModifyHash() const override;
86 
87  SCH_SHEET* Load( const wxString& aFileName, SCHEMATIC* aSchematic,
88  SCH_SHEET* aAppendToMe = nullptr,
89  const PROPERTIES* aProperties = nullptr ) override;
90 
91  void LoadContent( LINE_READER& aReader, SCH_SHEET* aSheet,
92  int aVersion = SEXPR_SCHEMATIC_FILE_VERSION );
93 
94  void Save( const wxString& aFileName, SCH_SHEET* aSheet, SCHEMATIC* aSchematic,
95  const PROPERTIES* aProperties = nullptr ) override;
96 
97  void Format( SCH_SHEET* aSheet );
98 
99  void Format( EE_SELECTION* aSelection, OUTPUTFORMATTER* aFormatter );
100 
101  void EnumerateSymbolLib( wxArrayString& aSymbolNameList,
102  const wxString& aLibraryPath,
103  const PROPERTIES* aProperties = nullptr ) override;
104  void EnumerateSymbolLib( std::vector<LIB_PART*>& aSymbolList,
105  const wxString& aLibraryPath,
106  const PROPERTIES* aProperties = nullptr ) override;
107  LIB_PART* LoadSymbol( const wxString& aLibraryPath, const wxString& aAliasName,
108  const PROPERTIES* aProperties = nullptr ) override;
109  void SaveSymbol( const wxString& aLibraryPath, const LIB_PART* aSymbol,
110  const PROPERTIES* aProperties = nullptr ) override;
111  void DeleteSymbol( const wxString& aLibraryPath, const wxString& aSymbolName,
112  const PROPERTIES* aProperties = nullptr ) override;
113  void CreateSymbolLib( const wxString& aLibraryPath,
114  const PROPERTIES* aProperties = nullptr ) override;
115  bool DeleteSymbolLib( const wxString& aLibraryPath,
116  const PROPERTIES* aProperties = nullptr ) override;
117  void SaveLibrary( const wxString& aLibraryPath,
118  const PROPERTIES* aProperties = nullptr ) override;
119 
120  bool CheckHeader( const wxString& aFileName ) override;
121  bool IsSymbolLibWritable( const wxString& aLibraryPath ) override;
122 
123  const wxString& GetError() const override { return m_error; }
124 
125  static LIB_PART* ParsePart( LINE_READER& aReader,
126  int aVersion = SEXPR_SCHEMATIC_FILE_VERSION );
127  static void FormatPart( LIB_PART* aPart, OUTPUTFORMATTER& aFormatter );
128 
129 private:
130  void loadHierarchy( SCH_SHEET* aSheet );
131  void loadFile( const wxString& aFileName, SCH_SHEET* aSheet );
132 
133  void saveSymbol( SCH_COMPONENT* aComponent, int aNestLevel );
134  void saveField( SCH_FIELD* aField, int aNestLevel );
135  void saveBitmap( SCH_BITMAP* aBitmap, int aNestLevel );
136  void saveSheet( SCH_SHEET* aSheet, int aNestLevel );
137  void saveJunction( SCH_JUNCTION* aJunction, int aNestLevel );
138  void saveNoConnect( SCH_NO_CONNECT* aNoConnect, int aNestLevel );
139  void saveBusEntry( SCH_BUS_ENTRY_BASE* aBusEntry, int aNestLevel );
140  void saveLine( SCH_LINE* aLine, int aNestLevel );
141  void saveText( SCH_TEXT* aText, int aNestLevel );
142  void saveBusAlias( std::shared_ptr<BUS_ALIAS> aAlias, int aNestLevel );
143 
144  void cacheLib( const wxString& aLibraryFileName );
145  bool isBuffering( const PROPERTIES* aProperties );
146 
147 protected:
148  int m_version;
149  int m_fieldId;
150 
152  wxString m_error;
153 
154  wxString m_path;
155  std::stack<wxString> m_currentPath;
161 
163  void init( SCHEMATIC* aSchematic, const PROPERTIES* aProperties = nullptr );
164 };
165 
166 #endif // _SCH_SEXPR_PLUGIN_H_
SCHEMATIC * m_schematic
Passed to Load(), the schematic object being loaded.
SCH_FIELD instances are attached to a component and provide a place for the component's value,...
Definition: sch_field.h:52
int m_version
Version of file being loaded.
LINE_READER is an abstract class from which implementation specific LINE_READERs may be derived to re...
Definition: richio.h:81
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 A schematic may consist of one or more sheets (and one r...
Definition: schematic.h:42
wxString m_error
For throwing exceptions or errors on partial schematic loads.
int m_fieldId
Non-mandatory schematic field ID counter.
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
Returns 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)
OUTPUTFORMATTER is an important interface (abstract class) used to output 8 bit text in a convenient ...
Definition: richio.h:327
void saveField(SCH_FIELD *aField, int aNestLevel)
static void FormatPart(LIB_PART *aPart, OUTPUTFORMATTER &aFormatter)
PROPERTIES is a name/value tuple with unique names and optional values.
Definition: properties.h:34
void saveSymbol(SCH_COMPONENT *aComponent, int aNestLevel)
Base class for a bus or wire entry.
Definition: sch_bus_entry.h:41
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 SaveLibrary(const wxString &aLibraryPath, const PROPERTIES *aProperties=nullptr) override
Define a library symbol object.
void cacheLib(const wxString &aLibraryFileName)
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.
KIWAY is a minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the s...
Definition: kiway.h:273
bool isBuffering(const PROPERTIES *aProperties)
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:42
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:216
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
Returns 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.
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:38
bool CheckHeader(const wxString &aFileName) override
Return true if the first line in aFileName begins with the expected header.
Schematic symbol object.
Definition: sch_component.h:88
const PROPERTIES * m_props
Passed via Save() or Load(), no ownership, may be nullptr.
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)
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.