2020-02-13 13:39:52 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2020 CERN
|
2023-01-04 20:39:50 +00:00
|
|
|
* Copyright (C) 2021-2023 KiCad Developers, see AUTHORS.txt for contributors.
|
2020-02-13 13:39:52 +00:00
|
|
|
*
|
|
|
|
* @author Wayne Stambaugh <stambaughw@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
#ifndef SCH_IO_KICAD_SEXPR_H_
|
|
|
|
#define SCH_IO_KICAD_SEXPR_H_
|
2023-08-26 19:28:53 +00:00
|
|
|
|
2020-02-13 13:39:52 +00:00
|
|
|
#include <memory>
|
2023-12-19 17:39:26 +00:00
|
|
|
#include <sch_io/sch_io.h>
|
|
|
|
#include <sch_io/sch_io_mgr.h>
|
2020-05-13 21:58:30 +00:00
|
|
|
#include <sch_file_versions.h>
|
2023-01-11 14:17:36 +00:00
|
|
|
#include <sch_sheet_path.h>
|
2020-02-13 13:39:52 +00:00
|
|
|
#include <stack>
|
2023-08-26 19:28:53 +00:00
|
|
|
#include <wildcards_and_files_ext.h>
|
2023-09-08 02:29:40 +00:00
|
|
|
#include <wx/string.h>
|
2020-02-13 13:39:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
class KIWAY;
|
|
|
|
class LINE_READER;
|
|
|
|
class SCH_SCREEN;
|
|
|
|
class SCH_SHEET;
|
2021-04-12 19:08:02 +00:00
|
|
|
struct SCH_SHEET_INSTANCE;
|
2020-02-13 13:39:52 +00:00
|
|
|
class SCH_BITMAP;
|
|
|
|
class SCH_JUNCTION;
|
|
|
|
class SCH_NO_CONNECT;
|
|
|
|
class SCH_LINE;
|
2021-07-17 19:56:18 +00:00
|
|
|
class SCH_SHAPE;
|
2020-02-13 13:39:52 +00:00
|
|
|
class SCH_BUS_ENTRY_BASE;
|
|
|
|
class SCH_TEXT;
|
2022-01-25 22:33:37 +00:00
|
|
|
class SCH_TEXTBOX;
|
2023-11-25 12:38:24 +00:00
|
|
|
class SCH_TABLE;
|
2021-06-10 14:10:55 +00:00
|
|
|
class SCH_SYMBOL;
|
2020-02-13 13:39:52 +00:00
|
|
|
class SCH_FIELD;
|
2023-01-04 20:39:50 +00:00
|
|
|
struct SCH_SYMBOL_INSTANCE;
|
2022-11-06 16:51:52 +00:00
|
|
|
class STRING_UTF8_MAP;
|
2020-05-15 19:53:59 +00:00
|
|
|
class EE_SELECTION;
|
2023-12-24 00:31:24 +00:00
|
|
|
class SCH_IO_KICAD_SEXPR_LIB_CACHE;
|
2021-06-10 18:51:46 +00:00
|
|
|
class LIB_SYMBOL;
|
2021-06-15 12:31:28 +00:00
|
|
|
class SYMBOL_LIB;
|
2020-02-13 13:39:52 +00:00
|
|
|
class BUS_ALIAS;
|
|
|
|
|
|
|
|
/**
|
2023-12-19 17:39:26 +00:00
|
|
|
* A #SCH_IO derivation for loading schematic files using the new s-expression
|
2020-02-13 13:39:52 +00:00
|
|
|
* file format.
|
|
|
|
*
|
2023-12-19 17:39:26 +00:00
|
|
|
* As with all SCH_IOs there is no UI dependencies i.e. windowing calls allowed.
|
2020-02-13 13:39:52 +00:00
|
|
|
*/
|
2023-12-24 00:31:24 +00:00
|
|
|
class SCH_IO_KICAD_SEXPR : public SCH_IO
|
2020-02-13 13:39:52 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
SCH_IO_KICAD_SEXPR();
|
|
|
|
virtual ~SCH_IO_KICAD_SEXPR();
|
2020-02-13 13:39:52 +00:00
|
|
|
|
2023-12-27 16:34:59 +00:00
|
|
|
const IO_BASE::IO_FILE_DESC GetSchematicFileDesc() const override
|
2020-02-13 13:39:52 +00:00
|
|
|
{
|
2023-12-27 16:34:59 +00:00
|
|
|
return IO_BASE::IO_FILE_DESC( _HKI( "KiCad s-expression schematic files" ),
|
2023-12-28 02:10:01 +00:00
|
|
|
{ FILEEXT::KiCadSchematicFileExtension } );
|
2020-02-28 14:03:09 +00:00
|
|
|
}
|
|
|
|
|
2023-12-27 16:34:59 +00:00
|
|
|
const IO_BASE::IO_FILE_DESC GetLibraryDesc() const override
|
2020-02-28 14:03:09 +00:00
|
|
|
{
|
2023-12-27 16:34:59 +00:00
|
|
|
return IO_BASE::IO_FILE_DESC( _HKI( "KiCad symbol library files" ),
|
2023-12-28 02:10:01 +00:00
|
|
|
{ FILEEXT::KiCadSymbolLibFileExtension } );
|
2020-02-13 13:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The property used internally by the plugin to enable cache buffering which prevents
|
|
|
|
* the library file from being written every time the cache is changed. This is useful
|
|
|
|
* when writing the schematic cache library file or saving a library to a new file name.
|
|
|
|
*/
|
|
|
|
static const char* PropBuffering;
|
|
|
|
|
|
|
|
int GetModifyHash() const override;
|
|
|
|
|
2023-08-26 19:28:53 +00:00
|
|
|
SCH_SHEET* LoadSchematicFile( const wxString& aFileName, SCHEMATIC* aSchematic,
|
|
|
|
SCH_SHEET* aAppendToMe = nullptr,
|
|
|
|
const STRING_UTF8_MAP* aProperties = nullptr ) override;
|
2020-02-13 13:39:52 +00:00
|
|
|
|
2020-05-13 21:58:30 +00:00
|
|
|
void LoadContent( LINE_READER& aReader, SCH_SHEET* aSheet,
|
|
|
|
int aVersion = SEXPR_SCHEMATIC_FILE_VERSION );
|
2020-02-13 13:39:52 +00:00
|
|
|
|
2023-08-26 19:28:53 +00:00
|
|
|
void SaveSchematicFile( const wxString& aFileName, SCH_SHEET* aSheet, SCHEMATIC* aSchematic,
|
|
|
|
const STRING_UTF8_MAP* aProperties = nullptr ) override;
|
2020-02-13 13:39:52 +00:00
|
|
|
|
2020-04-26 20:53:29 +00:00
|
|
|
void Format( SCH_SHEET* aSheet );
|
2020-02-13 13:39:52 +00:00
|
|
|
|
2021-05-01 22:56:37 +00:00
|
|
|
void Format( EE_SELECTION* aSelection, SCH_SHEET_PATH* aSelectionPath,
|
2022-11-22 19:35:27 +00:00
|
|
|
SCHEMATIC& aSchematic, OUTPUTFORMATTER* aFormatter, bool aForClipboard );
|
2020-02-13 13:39:52 +00:00
|
|
|
|
|
|
|
void EnumerateSymbolLib( wxArrayString& aSymbolNameList,
|
|
|
|
const wxString& aLibraryPath,
|
2022-11-06 16:51:52 +00:00
|
|
|
const STRING_UTF8_MAP* aProperties = nullptr ) override;
|
2021-06-10 18:51:46 +00:00
|
|
|
void EnumerateSymbolLib( std::vector<LIB_SYMBOL*>& aSymbolList,
|
|
|
|
const wxString& aLibraryPath,
|
2022-11-06 16:51:52 +00:00
|
|
|
const STRING_UTF8_MAP* aProperties = nullptr ) override;
|
2021-06-10 18:51:46 +00:00
|
|
|
LIB_SYMBOL* LoadSymbol( const wxString& aLibraryPath, const wxString& aAliasName,
|
2022-11-06 16:51:52 +00:00
|
|
|
const STRING_UTF8_MAP* aProperties = nullptr ) override;
|
2021-06-10 18:51:46 +00:00
|
|
|
void SaveSymbol( const wxString& aLibraryPath, const LIB_SYMBOL* aSymbol,
|
2022-11-06 16:51:52 +00:00
|
|
|
const STRING_UTF8_MAP* aProperties = nullptr ) override;
|
2020-02-13 13:39:52 +00:00
|
|
|
void DeleteSymbol( const wxString& aLibraryPath, const wxString& aSymbolName,
|
2022-11-06 16:51:52 +00:00
|
|
|
const STRING_UTF8_MAP* aProperties = nullptr ) override;
|
2023-12-27 00:25:41 +00:00
|
|
|
void CreateLibrary( const wxString& aLibraryPath,
|
|
|
|
const STRING_UTF8_MAP* aProperties = nullptr ) override;
|
|
|
|
bool DeleteLibrary( const wxString& aLibraryPath,
|
|
|
|
const STRING_UTF8_MAP* aProperties = nullptr ) override;
|
2020-02-13 13:39:52 +00:00
|
|
|
void SaveLibrary( const wxString& aLibraryPath,
|
2022-11-06 16:51:52 +00:00
|
|
|
const STRING_UTF8_MAP* aProperties = nullptr ) override;
|
2020-02-13 13:39:52 +00:00
|
|
|
|
2023-12-27 00:25:41 +00:00
|
|
|
bool IsLibraryWritable( const wxString& aLibraryPath ) override;
|
2020-02-13 13:39:52 +00:00
|
|
|
|
2022-08-28 23:02:12 +00:00
|
|
|
void GetAvailableSymbolFields( std::vector<wxString>& aNames ) override;
|
|
|
|
void GetDefaultSymbolFields( std::vector<wxString>& aNames ) override;
|
|
|
|
|
2020-02-13 13:39:52 +00:00
|
|
|
const wxString& GetError() const override { return m_error; }
|
|
|
|
|
2023-06-12 22:13:23 +00:00
|
|
|
static std::vector<LIB_SYMBOL*> ParseLibSymbols( std::string& aSymbolText,
|
|
|
|
std::string aSource,
|
|
|
|
int aFileVersion = SEXPR_SCHEMATIC_FILE_VERSION );
|
2021-06-15 12:31:28 +00:00
|
|
|
static void FormatLibSymbol( LIB_SYMBOL* aPart, OUTPUTFORMATTER& aFormatter );
|
2020-02-13 13:39:52 +00:00
|
|
|
|
|
|
|
private:
|
2022-09-08 22:17:41 +00:00
|
|
|
void loadHierarchy( const SCH_SHEET_PATH& aParentSheetPath, SCH_SHEET* aSheet );
|
2020-04-26 20:53:29 +00:00
|
|
|
void loadFile( const wxString& aFileName, SCH_SHEET* aSheet );
|
2020-02-13 13:39:52 +00:00
|
|
|
|
2022-10-04 21:45:59 +00:00
|
|
|
void saveSymbol( SCH_SYMBOL* aSymbol, const SCHEMATIC& aSchematic, int aNestLevel,
|
2023-12-01 19:18:47 +00:00
|
|
|
bool aForClipboard, const SCH_SHEET_PATH* aRelativePath = nullptr );
|
2020-03-16 13:04:50 +00:00
|
|
|
void saveField( SCH_FIELD* aField, int aNestLevel );
|
|
|
|
void saveBitmap( SCH_BITMAP* aBitmap, int aNestLevel );
|
|
|
|
void saveSheet( SCH_SHEET* aSheet, int aNestLevel );
|
|
|
|
void saveJunction( SCH_JUNCTION* aJunction, int aNestLevel );
|
|
|
|
void saveNoConnect( SCH_NO_CONNECT* aNoConnect, int aNestLevel );
|
|
|
|
void saveBusEntry( SCH_BUS_ENTRY_BASE* aBusEntry, int aNestLevel );
|
|
|
|
void saveLine( SCH_LINE* aLine, int aNestLevel );
|
2021-07-17 19:56:18 +00:00
|
|
|
void saveShape( SCH_SHAPE* aShape, int aNestLevel );
|
2020-03-16 13:04:50 +00:00
|
|
|
void saveText( SCH_TEXT* aText, int aNestLevel );
|
2022-01-25 22:33:37 +00:00
|
|
|
void saveTextBox( SCH_TEXTBOX* aText, int aNestLevel );
|
2023-11-25 12:38:24 +00:00
|
|
|
void saveTable( SCH_TABLE* aTable, int aNestLevel );
|
2020-03-16 13:04:50 +00:00
|
|
|
void saveBusAlias( std::shared_ptr<BUS_ALIAS> aAlias, int aNestLevel );
|
2022-10-01 12:44:21 +00:00
|
|
|
void saveInstances( const std::vector<SCH_SHEET_INSTANCE>& aSheets, int aNestLevel );
|
2020-02-13 13:39:52 +00:00
|
|
|
|
2022-11-06 16:51:52 +00:00
|
|
|
void cacheLib( const wxString& aLibraryFileName, const STRING_UTF8_MAP* aProperties );
|
|
|
|
bool isBuffering( const STRING_UTF8_MAP* aProperties );
|
2020-02-13 13:39:52 +00:00
|
|
|
|
|
|
|
protected:
|
2021-06-23 22:53:08 +00:00
|
|
|
int m_version; ///< Version of file being loaded.
|
|
|
|
int m_nextFreeFieldId;
|
2022-10-01 12:44:21 +00:00
|
|
|
bool m_appending; ///< Schematic load append status.
|
2021-06-23 22:53:08 +00:00
|
|
|
wxString m_error; ///< For throwing exceptions or errors on partial
|
|
|
|
///< loads.
|
|
|
|
|
|
|
|
wxString m_path; ///< Root project path for loading child sheets.
|
|
|
|
std::stack<wxString> m_currentPath; ///< Stack to maintain nested sheet paths
|
|
|
|
SCH_SHEET* m_rootSheet; ///< The root sheet of the schematic being loaded.
|
2022-12-15 14:57:53 +00:00
|
|
|
SCH_SHEET_PATH m_currentSheetPath;
|
2021-06-23 22:53:08 +00:00
|
|
|
SCHEMATIC* m_schematic;
|
|
|
|
OUTPUTFORMATTER* m_out; ///< The formatter for saving SCH_SCREEN objects.
|
2023-12-24 00:31:24 +00:00
|
|
|
SCH_IO_KICAD_SEXPR_LIB_CACHE* m_cache;
|
2020-02-13 13:39:52 +00:00
|
|
|
|
|
|
|
/// initialize PLUGIN like a constructor would.
|
2022-11-06 16:51:52 +00:00
|
|
|
void init( SCHEMATIC* aSchematic, const STRING_UTF8_MAP* aProperties = nullptr );
|
2020-02-13 13:39:52 +00:00
|
|
|
};
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
#endif // SCH_IO_KICAD_SEXPR_H_
|