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
|
2024-01-26 13:59:07 +00:00
|
|
|
* Copyright (C) 2021-2024 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
2020-03-27 12:52:40 +00:00
|
|
|
// For some reason wxWidgets is built with wxUSE_BASE64 unset so expose the wxWidgets
|
|
|
|
// base64 code.
|
|
|
|
#define wxUSE_BASE64 1
|
|
|
|
#include <wx/base64.h>
|
2021-06-03 12:11:15 +00:00
|
|
|
#include <wx/log.h>
|
2020-02-13 13:39:52 +00:00
|
|
|
#include <wx/mstream.h>
|
2020-07-25 21:53:58 +00:00
|
|
|
#include <advanced_config.h>
|
2022-08-27 17:23:43 +00:00
|
|
|
#include <base_units.h>
|
2023-11-18 22:34:09 +00:00
|
|
|
#include <build_version.h>
|
2020-02-13 13:39:52 +00:00
|
|
|
#include <trace_helpers.h>
|
2020-10-24 01:38:50 +00:00
|
|
|
#include <locale_io.h>
|
2020-02-13 13:39:52 +00:00
|
|
|
#include <sch_bitmap.h>
|
|
|
|
#include <sch_bus_entry.h>
|
2021-02-24 13:48:02 +00:00
|
|
|
#include <sch_symbol.h>
|
2021-06-10 14:10:55 +00:00
|
|
|
#include <sch_edit_frame.h> // SYMBOL_ORIENTATION_T
|
2020-02-13 13:39:52 +00:00
|
|
|
#include <sch_junction.h>
|
|
|
|
#include <sch_line.h>
|
2024-04-20 09:44:34 +00:00
|
|
|
#include <sch_pin.h>
|
2021-07-17 19:56:18 +00:00
|
|
|
#include <sch_shape.h>
|
2020-02-13 13:39:52 +00:00
|
|
|
#include <sch_no_connect.h>
|
2024-04-25 14:24:46 +00:00
|
|
|
#include <sch_rule_area.h>
|
2020-02-13 13:39:52 +00:00
|
|
|
#include <sch_text.h>
|
2022-01-25 22:33:37 +00:00
|
|
|
#include <sch_textbox.h>
|
2023-11-25 12:38:24 +00:00
|
|
|
#include <sch_table.h>
|
|
|
|
#include <sch_tablecell.h>
|
2020-02-13 13:39:52 +00:00
|
|
|
#include <sch_sheet.h>
|
2021-04-06 21:15:49 +00:00
|
|
|
#include <sch_sheet_pin.h>
|
2020-05-13 02:00:37 +00:00
|
|
|
#include <schematic.h>
|
2020-02-13 13:39:52 +00:00
|
|
|
#include <sch_screen.h>
|
2023-12-19 17:39:26 +00:00
|
|
|
#include <io/kicad/kicad_io_utils.h>
|
2020-03-16 13:04:50 +00:00
|
|
|
#include <schematic_lexer.h>
|
2023-12-24 00:31:24 +00:00
|
|
|
#include <sch_io/kicad_sexpr/sch_io_kicad_sexpr.h>
|
|
|
|
#include <sch_io/kicad_sexpr/sch_io_kicad_sexpr_parser.h>
|
|
|
|
#include <sch_io/kicad_sexpr/sch_io_kicad_sexpr_lib_cache.h>
|
|
|
|
#include <sch_io/kicad_sexpr/sch_io_kicad_sexpr_common.h>
|
2021-06-09 19:32:58 +00:00
|
|
|
#include <symbol_lib_table.h> // for PropPowerSymsOnly definition.
|
2020-05-15 19:53:59 +00:00
|
|
|
#include <ee_selection.h>
|
2021-07-29 09:56:22 +00:00
|
|
|
#include <string_utils.h>
|
2021-06-07 19:12:30 +00:00
|
|
|
#include <wx_filename.h> // for ::ResolvePossibleSymlinks()
|
2021-08-14 20:05:21 +00:00
|
|
|
#include <progress_reporter.h>
|
2022-08-21 19:35:02 +00:00
|
|
|
#include <boost/algorithm/string/join.hpp>
|
2020-04-14 18:11:50 +00:00
|
|
|
|
2020-03-25 15:27:15 +00:00
|
|
|
using namespace TSCHEMATIC_T;
|
2020-02-13 13:39:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
#define SCH_PARSE_ERROR( text, reader, pos ) \
|
|
|
|
THROW_PARSE_ERROR( text, reader.GetSource(), reader.Line(), \
|
|
|
|
reader.LineNumber(), pos - reader.Line() )
|
|
|
|
|
|
|
|
|
2023-12-24 04:31:22 +00:00
|
|
|
SCH_IO_KICAD_SEXPR::SCH_IO_KICAD_SEXPR() : SCH_IO( wxS( "Eeschema s-expression" ) )
|
2020-02-13 13:39:52 +00:00
|
|
|
{
|
2021-07-16 20:13:26 +00:00
|
|
|
init( nullptr );
|
2020-02-13 13:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
SCH_IO_KICAD_SEXPR::~SCH_IO_KICAD_SEXPR()
|
2020-02-13 13:39:52 +00:00
|
|
|
{
|
|
|
|
delete m_cache;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
void SCH_IO_KICAD_SEXPR::init( SCHEMATIC* aSchematic, const STRING_UTF8_MAP* aProperties )
|
2020-02-13 13:39:52 +00:00
|
|
|
{
|
2021-03-09 15:08:51 +00:00
|
|
|
m_version = 0;
|
2022-10-01 12:44:21 +00:00
|
|
|
m_appending = false;
|
2021-03-09 15:08:51 +00:00
|
|
|
m_rootSheet = nullptr;
|
|
|
|
m_schematic = aSchematic;
|
|
|
|
m_cache = nullptr;
|
|
|
|
m_out = nullptr;
|
|
|
|
m_nextFreeFieldId = 100; // number arbitrarily > MANDATORY_FIELDS or SHEET_MANDATORY_FIELDS
|
2020-02-13 13:39:52 +00:00
|
|
|
}
|
|
|
|
|
2021-06-23 22:53:08 +00:00
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
SCH_SHEET* SCH_IO_KICAD_SEXPR::LoadSchematicFile( const wxString& aFileName, SCHEMATIC* aSchematic,
|
2024-01-26 13:59:07 +00:00
|
|
|
SCH_SHEET* aAppendToMe,
|
|
|
|
const STRING_UTF8_MAP* aProperties )
|
2020-02-13 13:39:52 +00:00
|
|
|
{
|
2020-05-21 03:00:23 +00:00
|
|
|
wxASSERT( !aFileName || aSchematic != nullptr );
|
2020-02-13 13:39:52 +00:00
|
|
|
|
|
|
|
LOCALE_IO toggle; // toggles on, then off, the C locale.
|
|
|
|
SCH_SHEET* sheet;
|
|
|
|
|
|
|
|
wxFileName fn = aFileName;
|
|
|
|
|
|
|
|
// Unfortunately child sheet file names the legacy schematic file format are not fully
|
|
|
|
// qualified and are always appended to the project path. The aFileName attribute must
|
|
|
|
// always be an absolute path so the project path can be used for load child sheet files.
|
|
|
|
wxASSERT( fn.IsAbsolute() );
|
|
|
|
|
|
|
|
if( aAppendToMe )
|
|
|
|
{
|
2022-10-01 12:44:21 +00:00
|
|
|
m_appending = true;
|
2022-12-15 14:57:53 +00:00
|
|
|
wxLogTrace( traceSchPlugin, "Append \"%s\" to sheet \"%s\".",
|
2020-02-13 13:39:52 +00:00
|
|
|
aFileName, aAppendToMe->GetFileName() );
|
|
|
|
|
|
|
|
wxFileName normedFn = aAppendToMe->GetFileName();
|
|
|
|
|
|
|
|
if( !normedFn.IsAbsolute() )
|
|
|
|
{
|
|
|
|
if( aFileName.Right( normedFn.GetFullPath().Length() ) == normedFn.GetFullPath() )
|
|
|
|
m_path = aFileName.Left( aFileName.Length() - normedFn.GetFullPath().Length() );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( m_path.IsEmpty() )
|
2020-05-21 03:00:23 +00:00
|
|
|
m_path = aSchematic->Prj().GetProjectPath();
|
2020-02-13 13:39:52 +00:00
|
|
|
|
2022-12-15 14:57:53 +00:00
|
|
|
wxLogTrace( traceSchPlugin, "Normalized append path \"%s\".", m_path );
|
2020-02-13 13:39:52 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-21 03:00:23 +00:00
|
|
|
m_path = aSchematic->Prj().GetProjectPath();
|
2020-02-13 13:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_currentPath.push( m_path );
|
2020-05-21 03:00:23 +00:00
|
|
|
init( aSchematic, aProperties );
|
2020-05-13 02:00:37 +00:00
|
|
|
|
2021-07-16 20:13:26 +00:00
|
|
|
if( aAppendToMe == nullptr )
|
2020-02-13 13:39:52 +00:00
|
|
|
{
|
|
|
|
// Clean up any allocated memory if an exception occurs loading the schematic.
|
2020-10-26 23:49:11 +00:00
|
|
|
std::unique_ptr<SCH_SHEET> newSheet = std::make_unique<SCH_SHEET>( aSchematic );
|
2020-11-15 15:11:41 +00:00
|
|
|
|
|
|
|
wxFileName relPath( aFileName );
|
2022-11-22 19:35:27 +00:00
|
|
|
|
2020-11-17 15:24:03 +00:00
|
|
|
// Do not use wxPATH_UNIX as option in MakeRelativeTo(). It can create incorrect
|
|
|
|
// relative paths on Windows, because paths have a disk identifier (C:, D: ...)
|
|
|
|
relPath.MakeRelativeTo( aSchematic->Prj().GetProjectPath() );
|
2020-11-15 15:11:41 +00:00
|
|
|
|
|
|
|
newSheet->SetFileName( relPath.GetFullPath() );
|
2020-02-13 13:39:52 +00:00
|
|
|
m_rootSheet = newSheet.get();
|
2022-09-08 22:17:41 +00:00
|
|
|
loadHierarchy( SCH_SHEET_PATH(), newSheet.get() );
|
2020-02-13 13:39:52 +00:00
|
|
|
|
|
|
|
// If we got here, the schematic loaded successfully.
|
|
|
|
sheet = newSheet.release();
|
2020-05-05 19:12:56 +00:00
|
|
|
m_rootSheet = nullptr; // Quiet Coverity warning.
|
2020-02-13 13:39:52 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-13 02:00:37 +00:00
|
|
|
wxCHECK_MSG( aSchematic->IsValid(), nullptr, "Can't append to a schematic with no root!" );
|
|
|
|
m_rootSheet = &aSchematic->Root();
|
2020-02-13 13:39:52 +00:00
|
|
|
sheet = aAppendToMe;
|
2022-09-08 22:17:41 +00:00
|
|
|
loadHierarchy( SCH_SHEET_PATH(), sheet );
|
2020-02-13 13:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wxASSERT( m_currentPath.size() == 1 ); // only the project path should remain
|
|
|
|
|
2021-05-01 22:27:23 +00:00
|
|
|
m_currentPath.pop(); // Clear the path stack for next call to Load
|
|
|
|
|
2020-02-13 13:39:52 +00:00
|
|
|
return sheet;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Everything below this comment is recursive. Modify with care.
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
void SCH_IO_KICAD_SEXPR::loadHierarchy( const SCH_SHEET_PATH& aParentSheetPath, SCH_SHEET* aSheet )
|
2020-02-13 13:39:52 +00:00
|
|
|
{
|
2022-12-15 14:57:53 +00:00
|
|
|
m_currentSheetPath.push_back( aSheet );
|
|
|
|
|
2021-07-16 20:13:26 +00:00
|
|
|
SCH_SCREEN* screen = nullptr;
|
2020-02-13 13:39:52 +00:00
|
|
|
|
|
|
|
if( !aSheet->GetScreen() )
|
|
|
|
{
|
|
|
|
// SCH_SCREEN objects store the full path and file name where the SCH_SHEET object only
|
|
|
|
// stores the file name and extension. Add the project path to the file name and
|
|
|
|
// extension to compare when calling SCH_SHEET::SearchHierarchy().
|
|
|
|
wxFileName fileName = aSheet->GetFileName();
|
|
|
|
|
|
|
|
if( !fileName.IsAbsolute() )
|
|
|
|
fileName.MakeAbsolute( m_currentPath.top() );
|
|
|
|
|
2021-06-09 19:32:58 +00:00
|
|
|
// Save the current path so that it gets restored when descending and ascending the
|
2020-02-13 13:39:52 +00:00
|
|
|
// sheet hierarchy which allows for sheet schematic files to be nested in folders
|
|
|
|
// relative to the last path a schematic was loaded from.
|
2022-12-15 14:57:53 +00:00
|
|
|
wxLogTrace( traceSchPlugin, "Saving path '%s'", m_currentPath.top() );
|
2020-02-13 13:39:52 +00:00
|
|
|
m_currentPath.push( fileName.GetPath() );
|
2022-12-15 14:57:53 +00:00
|
|
|
wxLogTrace( traceSchPlugin, "Current path '%s'", m_currentPath.top() );
|
|
|
|
wxLogTrace( traceSchPlugin, "Loading '%s'", fileName.GetFullPath() );
|
2020-02-13 13:39:52 +00:00
|
|
|
|
2022-09-08 22:17:41 +00:00
|
|
|
SCH_SHEET_PATH ancestorSheetPath = aParentSheetPath;
|
|
|
|
|
|
|
|
while( !ancestorSheetPath.empty() )
|
|
|
|
{
|
|
|
|
if( ancestorSheetPath.LastScreen()->GetFileName() == fileName.GetFullPath() )
|
|
|
|
{
|
|
|
|
if( !m_error.IsEmpty() )
|
|
|
|
m_error += "\n";
|
|
|
|
|
|
|
|
m_error += wxString::Format( _( "Could not load sheet '%s' because it already "
|
|
|
|
"appears as a direct ancestor in the schematic "
|
|
|
|
"hierarchy." ),
|
|
|
|
fileName.GetFullPath() );
|
|
|
|
|
|
|
|
fileName = wxEmptyString;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ancestorSheetPath.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ancestorSheetPath.empty() )
|
2022-12-15 14:57:53 +00:00
|
|
|
{
|
|
|
|
// Existing schematics could be either in the root sheet path or the current sheet
|
|
|
|
// load path so we have to check both.
|
|
|
|
if( !m_rootSheet->SearchHierarchy( fileName.GetFullPath(), &screen ) )
|
|
|
|
m_currentSheetPath.at( 0 )->SearchHierarchy( fileName.GetFullPath(), &screen );
|
|
|
|
}
|
2020-02-13 13:39:52 +00:00
|
|
|
|
|
|
|
if( screen )
|
|
|
|
{
|
|
|
|
aSheet->SetScreen( screen );
|
2020-05-13 02:00:37 +00:00
|
|
|
aSheet->GetScreen()->SetParent( m_schematic );
|
2020-02-13 13:39:52 +00:00
|
|
|
// Do not need to load the sub-sheets - this has already been done.
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-21 02:27:48 +00:00
|
|
|
aSheet->SetScreen( new SCH_SCREEN( m_schematic ) );
|
2020-02-13 13:39:52 +00:00
|
|
|
aSheet->GetScreen()->SetFileName( fileName.GetFullPath() );
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2020-04-26 20:53:29 +00:00
|
|
|
loadFile( fileName.GetFullPath(), aSheet );
|
2020-02-13 13:39:52 +00:00
|
|
|
}
|
|
|
|
catch( const IO_ERROR& ioe )
|
|
|
|
{
|
|
|
|
// If there is a problem loading the root sheet, there is no recovery.
|
|
|
|
if( aSheet == m_rootSheet )
|
2021-06-30 15:06:07 +00:00
|
|
|
throw;
|
2020-02-13 13:39:52 +00:00
|
|
|
|
|
|
|
// For all subsheets, queue up the error message for the caller.
|
|
|
|
if( !m_error.IsEmpty() )
|
|
|
|
m_error += "\n";
|
|
|
|
|
|
|
|
m_error += ioe.What();
|
|
|
|
}
|
2020-04-26 20:53:29 +00:00
|
|
|
|
2022-01-05 17:54:10 +00:00
|
|
|
if( fileName.FileExists() )
|
|
|
|
{
|
|
|
|
aSheet->GetScreen()->SetFileReadOnly( !fileName.IsFileWritable() );
|
|
|
|
aSheet->GetScreen()->SetFileExists( true );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
aSheet->GetScreen()->SetFileReadOnly( !fileName.IsDirWritable() );
|
|
|
|
aSheet->GetScreen()->SetFileExists( false );
|
|
|
|
}
|
2021-10-14 13:12:40 +00:00
|
|
|
|
2022-09-08 22:17:41 +00:00
|
|
|
SCH_SHEET_PATH currentSheetPath = aParentSheetPath;
|
|
|
|
currentSheetPath.push_back( aSheet );
|
|
|
|
|
|
|
|
// This was moved out of the try{} block so that any sheet definitions that
|
2020-04-26 20:53:29 +00:00
|
|
|
// the plugin fully parsed before the exception was raised will be loaded.
|
2021-06-23 22:53:08 +00:00
|
|
|
for( SCH_ITEM* aItem : aSheet->GetScreen()->Items().OfType( SCH_SHEET_T ) )
|
2020-04-26 20:53:29 +00:00
|
|
|
{
|
|
|
|
wxCHECK2( aItem->Type() == SCH_SHEET_T, /* do nothing */ );
|
2021-06-23 22:53:08 +00:00
|
|
|
SCH_SHEET* sheet = static_cast<SCH_SHEET*>( aItem );
|
2020-04-26 20:53:29 +00:00
|
|
|
|
|
|
|
// Recursion starts here.
|
2022-09-08 22:17:41 +00:00
|
|
|
loadHierarchy( currentSheetPath, sheet );
|
2020-04-26 20:53:29 +00:00
|
|
|
}
|
2020-02-13 13:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_currentPath.pop();
|
2022-12-15 14:57:53 +00:00
|
|
|
wxLogTrace( traceSchPlugin, "Restoring path \"%s\"", m_currentPath.top() );
|
2020-02-13 13:39:52 +00:00
|
|
|
}
|
2022-12-15 14:57:53 +00:00
|
|
|
|
|
|
|
m_currentSheetPath.pop_back();
|
2020-02-13 13:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
void SCH_IO_KICAD_SEXPR::loadFile( const wxString& aFileName, SCH_SHEET* aSheet )
|
2020-02-13 13:39:52 +00:00
|
|
|
{
|
|
|
|
FILE_LINE_READER reader( aFileName );
|
|
|
|
|
2021-06-23 22:53:08 +00:00
|
|
|
size_t lineCount = 0;
|
|
|
|
|
|
|
|
if( m_progressReporter )
|
|
|
|
{
|
|
|
|
m_progressReporter->Report( wxString::Format( _( "Loading %s..." ), aFileName ) );
|
|
|
|
|
|
|
|
if( !m_progressReporter->KeepRefreshing() )
|
2023-10-05 07:55:52 +00:00
|
|
|
THROW_IO_ERROR( _( "Open cancelled by user." ) );
|
2021-06-23 22:53:08 +00:00
|
|
|
|
|
|
|
while( reader.ReadLine() )
|
|
|
|
lineCount++;
|
|
|
|
|
|
|
|
reader.Rewind();
|
|
|
|
}
|
|
|
|
|
2023-12-28 23:25:47 +00:00
|
|
|
SCH_IO_KICAD_SEXPR_PARSER parser( &reader, m_progressReporter, lineCount, m_rootSheet,
|
|
|
|
m_appending );
|
2020-02-13 13:39:52 +00:00
|
|
|
|
2020-04-26 20:53:29 +00:00
|
|
|
parser.ParseSchematic( aSheet );
|
2020-02-13 13:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
void SCH_IO_KICAD_SEXPR::LoadContent( LINE_READER& aReader, SCH_SHEET* aSheet, int aFileVersion )
|
2020-05-13 21:58:30 +00:00
|
|
|
{
|
|
|
|
wxCHECK( aSheet, /* void */ );
|
|
|
|
|
2020-05-20 12:08:38 +00:00
|
|
|
LOCALE_IO toggle;
|
2023-12-24 00:31:24 +00:00
|
|
|
SCH_IO_KICAD_SEXPR_PARSER parser( &aReader );
|
2020-05-13 21:58:30 +00:00
|
|
|
|
|
|
|
parser.ParseSchematic( aSheet, true, aFileVersion );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
void SCH_IO_KICAD_SEXPR::SaveSchematicFile( const wxString& aFileName, SCH_SHEET* aSheet,
|
2024-01-26 13:59:07 +00:00
|
|
|
SCHEMATIC* aSchematic,
|
|
|
|
const STRING_UTF8_MAP* aProperties )
|
2020-02-13 13:39:52 +00:00
|
|
|
{
|
2021-07-16 20:13:26 +00:00
|
|
|
wxCHECK_RET( aSheet != nullptr, "NULL SCH_SHEET object." );
|
2020-02-13 13:39:52 +00:00
|
|
|
wxCHECK_RET( !aFileName.IsEmpty(), "No schematic file name defined." );
|
|
|
|
|
|
|
|
LOCALE_IO toggle; // toggles on, then off, the C locale, to write floating point values.
|
|
|
|
|
2020-05-21 03:00:23 +00:00
|
|
|
init( aSchematic, aProperties );
|
2020-02-13 13:39:52 +00:00
|
|
|
|
|
|
|
wxFileName fn = aFileName;
|
|
|
|
|
|
|
|
// File names should be absolute. Don't assume everything relative to the project path
|
|
|
|
// works properly.
|
|
|
|
wxASSERT( fn.IsAbsolute() );
|
|
|
|
|
2023-09-24 18:58:19 +00:00
|
|
|
PRETTIFIED_FILE_OUTPUTFORMATTER formatter( fn.GetFullPath() );
|
2020-02-13 13:39:52 +00:00
|
|
|
|
|
|
|
m_out = &formatter; // no ownership
|
|
|
|
|
2020-04-26 20:53:29 +00:00
|
|
|
Format( aSheet );
|
2021-10-14 13:12:40 +00:00
|
|
|
|
2023-01-12 12:46:52 +00:00
|
|
|
if( aSheet->GetScreen() )
|
|
|
|
aSheet->GetScreen()->SetFileExists( true );
|
2020-02-13 13:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
void SCH_IO_KICAD_SEXPR::Format( SCH_SHEET* aSheet )
|
2020-02-13 13:39:52 +00:00
|
|
|
{
|
2021-07-16 20:13:26 +00:00
|
|
|
wxCHECK_RET( aSheet != nullptr, "NULL SCH_SHEET* object." );
|
|
|
|
wxCHECK_RET( m_schematic != nullptr, "NULL SCHEMATIC* object." );
|
2020-02-13 13:39:52 +00:00
|
|
|
|
2024-06-06 16:58:48 +00:00
|
|
|
SCH_SHEET_LIST sheets = m_schematic->BuildSheetListSortedByPageNumbers();
|
2020-04-26 20:53:29 +00:00
|
|
|
SCH_SCREEN* screen = aSheet->GetScreen();
|
|
|
|
|
|
|
|
wxCHECK( screen, /* void */ );
|
|
|
|
|
2023-11-18 22:34:09 +00:00
|
|
|
m_out->Print( 0, "(kicad_sch (version %d) (generator \"eeschema\") (generator_version \"%s\")\n\n",
|
|
|
|
SEXPR_SCHEMATIC_FILE_VERSION, GetMajorMinorVersion().c_str().AsChar() );
|
2020-03-16 13:04:50 +00:00
|
|
|
|
2023-11-22 17:32:41 +00:00
|
|
|
KICAD_FORMAT::FormatUuid( m_out, screen->m_uuid );
|
2021-04-06 18:27:24 +00:00
|
|
|
|
2020-04-26 20:53:29 +00:00
|
|
|
screen->GetPageSettings().Format( m_out, 1, 0 );
|
2020-03-16 13:04:50 +00:00
|
|
|
m_out->Print( 0, "\n" );
|
2020-04-26 20:53:29 +00:00
|
|
|
screen->GetTitleBlock().Format( m_out, 1, 0 );
|
2020-03-16 13:04:50 +00:00
|
|
|
|
2020-04-16 16:43:50 +00:00
|
|
|
// Save cache library.
|
|
|
|
m_out->Print( 1, "(lib_symbols\n" );
|
|
|
|
|
2023-06-11 14:44:12 +00:00
|
|
|
for( const auto& [ libItemName, libSymbol ] : screen->GetLibSymbols() )
|
2023-12-24 00:31:24 +00:00
|
|
|
SCH_IO_KICAD_SEXPR_LIB_CACHE::SaveSymbol( libSymbol, *m_out, 2, libItemName );
|
2020-04-16 16:43:50 +00:00
|
|
|
|
|
|
|
m_out->Print( 1, ")\n\n" );
|
|
|
|
|
2022-09-11 17:41:54 +00:00
|
|
|
for( const std::shared_ptr<BUS_ALIAS>& alias : screen->GetBusAliases() )
|
2020-03-16 13:04:50 +00:00
|
|
|
saveBusAlias( alias, 1 );
|
2020-02-13 13:39:52 +00:00
|
|
|
|
2020-03-16 13:04:50 +00:00
|
|
|
// Enforce item ordering
|
2021-11-02 20:06:37 +00:00
|
|
|
auto cmp =
|
|
|
|
[]( const SCH_ITEM* a, const SCH_ITEM* b )
|
|
|
|
{
|
|
|
|
if( a->Type() != b->Type() )
|
|
|
|
return a->Type() < b->Type();
|
|
|
|
|
|
|
|
return a->m_Uuid < b->m_Uuid;
|
|
|
|
};
|
2020-05-12 11:53:45 +00:00
|
|
|
|
2020-03-16 13:04:50 +00:00
|
|
|
std::multiset<SCH_ITEM*, decltype( cmp )> save_map( cmp );
|
|
|
|
|
2020-05-12 11:53:45 +00:00
|
|
|
for( SCH_ITEM* item : screen->Items() )
|
2023-04-19 23:59:59 +00:00
|
|
|
{
|
|
|
|
// Markers are not saved, so keep them from being considered below
|
|
|
|
if( item->Type() != SCH_MARKER_T )
|
|
|
|
save_map.insert( item );
|
|
|
|
}
|
2020-03-16 13:04:50 +00:00
|
|
|
|
|
|
|
KICAD_T itemType = TYPE_NOT_INIT;
|
2020-03-25 15:27:15 +00:00
|
|
|
SCH_LAYER_ID layer = SCH_LAYER_ID_START;
|
2020-03-16 13:04:50 +00:00
|
|
|
|
2020-03-25 15:27:15 +00:00
|
|
|
for( SCH_ITEM* item : save_map )
|
2020-02-13 13:39:52 +00:00
|
|
|
{
|
2020-03-16 13:04:50 +00:00
|
|
|
if( itemType != item->Type() )
|
|
|
|
{
|
|
|
|
itemType = item->Type();
|
|
|
|
|
2021-06-10 14:10:55 +00:00
|
|
|
if( itemType != SCH_SYMBOL_T
|
2021-11-02 18:32:39 +00:00
|
|
|
&& itemType != SCH_JUNCTION_T
|
|
|
|
&& itemType != SCH_SHEET_T )
|
|
|
|
{
|
2020-03-16 13:04:50 +00:00
|
|
|
m_out->Print( 0, "\n" );
|
2021-11-02 18:32:39 +00:00
|
|
|
}
|
2020-03-16 13:04:50 +00:00
|
|
|
}
|
|
|
|
|
2020-02-13 13:39:52 +00:00
|
|
|
switch( item->Type() )
|
|
|
|
{
|
2021-06-10 14:10:55 +00:00
|
|
|
case SCH_SYMBOL_T:
|
2020-03-16 13:04:50 +00:00
|
|
|
m_out->Print( 0, "\n" );
|
2024-06-06 16:58:48 +00:00
|
|
|
saveSymbol( static_cast<SCH_SYMBOL*>( item ), *m_schematic, sheets, 1, false );
|
2020-02-13 13:39:52 +00:00
|
|
|
break;
|
2020-03-25 15:27:15 +00:00
|
|
|
|
2020-02-13 13:39:52 +00:00
|
|
|
case SCH_BITMAP_T:
|
2020-03-16 13:04:50 +00:00
|
|
|
saveBitmap( static_cast<SCH_BITMAP*>( item ), 1 );
|
2020-02-13 13:39:52 +00:00
|
|
|
break;
|
2020-03-25 15:27:15 +00:00
|
|
|
|
2020-02-13 13:39:52 +00:00
|
|
|
case SCH_SHEET_T:
|
2020-03-16 13:04:50 +00:00
|
|
|
m_out->Print( 0, "\n" );
|
2024-06-06 16:58:48 +00:00
|
|
|
saveSheet( static_cast<SCH_SHEET*>( item ), sheets, 1 );
|
2020-02-13 13:39:52 +00:00
|
|
|
break;
|
2020-03-25 15:27:15 +00:00
|
|
|
|
2020-02-13 13:39:52 +00:00
|
|
|
case SCH_JUNCTION_T:
|
2020-03-16 13:04:50 +00:00
|
|
|
saveJunction( static_cast<SCH_JUNCTION*>( item ), 1 );
|
2020-02-13 13:39:52 +00:00
|
|
|
break;
|
2020-03-25 15:27:15 +00:00
|
|
|
|
2020-02-13 13:39:52 +00:00
|
|
|
case SCH_NO_CONNECT_T:
|
2020-03-16 13:04:50 +00:00
|
|
|
saveNoConnect( static_cast<SCH_NO_CONNECT*>( item ), 1 );
|
2020-02-13 13:39:52 +00:00
|
|
|
break;
|
2020-03-25 15:27:15 +00:00
|
|
|
|
2020-02-13 13:39:52 +00:00
|
|
|
case SCH_BUS_WIRE_ENTRY_T:
|
|
|
|
case SCH_BUS_BUS_ENTRY_T:
|
2020-03-16 13:04:50 +00:00
|
|
|
saveBusEntry( static_cast<SCH_BUS_ENTRY_BASE*>( item ), 1 );
|
2020-02-13 13:39:52 +00:00
|
|
|
break;
|
2020-03-25 15:27:15 +00:00
|
|
|
|
2020-02-13 13:39:52 +00:00
|
|
|
case SCH_LINE_T:
|
2020-03-25 15:27:15 +00:00
|
|
|
if( layer != item->GetLayer() )
|
|
|
|
{
|
|
|
|
if( layer == SCH_LAYER_ID_START )
|
|
|
|
{
|
|
|
|
layer = item->GetLayer();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
layer = item->GetLayer();
|
|
|
|
m_out->Print( 0, "\n" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-16 13:04:50 +00:00
|
|
|
saveLine( static_cast<SCH_LINE*>( item ), 1 );
|
2020-02-13 13:39:52 +00:00
|
|
|
break;
|
2020-03-25 15:27:15 +00:00
|
|
|
|
2021-07-17 19:56:18 +00:00
|
|
|
case SCH_SHAPE_T:
|
|
|
|
saveShape( static_cast<SCH_SHAPE*>( item ), 1 );
|
|
|
|
break;
|
2024-04-25 14:24:46 +00:00
|
|
|
|
|
|
|
case SCH_RULE_AREA_T:
|
|
|
|
saveRuleArea( static_cast<SCH_RULE_AREA*>( item ), 1 );
|
|
|
|
break;
|
2021-07-17 19:56:18 +00:00
|
|
|
|
2020-02-13 13:39:52 +00:00
|
|
|
case SCH_TEXT_T:
|
|
|
|
case SCH_LABEL_T:
|
|
|
|
case SCH_GLOBAL_LABEL_T:
|
|
|
|
case SCH_HIER_LABEL_T:
|
2022-01-24 13:40:39 +00:00
|
|
|
case SCH_DIRECTIVE_LABEL_T:
|
2020-03-16 13:04:50 +00:00
|
|
|
saveText( static_cast<SCH_TEXT*>( item ), 1 );
|
2020-02-13 13:39:52 +00:00
|
|
|
break;
|
2020-03-25 15:27:15 +00:00
|
|
|
|
2022-01-25 22:33:37 +00:00
|
|
|
case SCH_TEXTBOX_T:
|
|
|
|
saveTextBox( static_cast<SCH_TEXTBOX*>( item ), 1 );
|
|
|
|
break;
|
|
|
|
|
2023-11-25 12:38:24 +00:00
|
|
|
case SCH_TABLE_T:
|
|
|
|
saveTable( static_cast<SCH_TABLE*>( item ), 1 );
|
|
|
|
break;
|
|
|
|
|
2020-02-13 13:39:52 +00:00
|
|
|
default:
|
2023-12-24 00:31:24 +00:00
|
|
|
wxASSERT( "Unexpected schematic object type in SCH_IO_KICAD_SEXPR::Format()" );
|
2020-02-13 13:39:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-09 15:02:16 +00:00
|
|
|
if( aSheet->HasRootInstance() )
|
2020-04-26 20:53:29 +00:00
|
|
|
{
|
2022-12-09 15:02:16 +00:00
|
|
|
std::vector< SCH_SHEET_INSTANCE> instances;
|
|
|
|
|
|
|
|
instances.emplace_back( aSheet->GetRootInstance() );
|
|
|
|
saveInstances( instances, 1 );
|
2020-04-26 20:53:29 +00:00
|
|
|
}
|
|
|
|
|
2020-02-13 13:39:52 +00:00
|
|
|
m_out->Print( 0, ")\n" );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
void SCH_IO_KICAD_SEXPR::Format( EE_SELECTION* aSelection, SCH_SHEET_PATH* aSelectionPath,
|
2023-12-28 23:25:47 +00:00
|
|
|
SCHEMATIC& aSchematic, OUTPUTFORMATTER* aFormatter,
|
|
|
|
bool aForClipboard )
|
2020-02-13 13:39:52 +00:00
|
|
|
{
|
2022-10-04 21:45:59 +00:00
|
|
|
wxCHECK( aSelection && aSelectionPath && aFormatter, /* void */ );
|
2020-05-15 19:53:59 +00:00
|
|
|
|
2020-05-18 19:47:19 +00:00
|
|
|
LOCALE_IO toggle;
|
2024-06-06 16:58:48 +00:00
|
|
|
SCH_SHEET_LIST sheets = aSchematic.BuildSheetListSortedByPageNumbers();
|
2020-05-18 19:47:19 +00:00
|
|
|
|
2022-11-22 19:35:27 +00:00
|
|
|
m_schematic = &aSchematic;
|
2020-02-13 13:39:52 +00:00
|
|
|
m_out = aFormatter;
|
|
|
|
|
2024-03-28 14:31:20 +00:00
|
|
|
size_t i;
|
|
|
|
SCH_ITEM* item;
|
2021-06-10 18:51:46 +00:00
|
|
|
std::map<wxString, LIB_SYMBOL*> libSymbols;
|
2024-03-28 14:31:20 +00:00
|
|
|
SCH_SCREEN* screen = aSelection->GetScreen();
|
|
|
|
std::set<SCH_TABLE*> promotedTables;
|
2020-05-15 19:53:59 +00:00
|
|
|
|
|
|
|
for( i = 0; i < aSelection->GetSize(); ++i )
|
|
|
|
{
|
|
|
|
item = dynamic_cast<SCH_ITEM*>( aSelection->GetItem( i ) );
|
|
|
|
|
|
|
|
wxCHECK2( item, continue );
|
|
|
|
|
2021-06-10 14:10:55 +00:00
|
|
|
if( item->Type() != SCH_SYMBOL_T )
|
2020-05-15 19:53:59 +00:00
|
|
|
continue;
|
|
|
|
|
2021-06-10 14:10:55 +00:00
|
|
|
SCH_SYMBOL* symbol = dynamic_cast<SCH_SYMBOL*>( item );
|
2020-05-15 19:53:59 +00:00
|
|
|
|
|
|
|
wxCHECK2( symbol, continue );
|
|
|
|
|
|
|
|
wxString libSymbolLookup = symbol->GetLibId().Format().wx_str();
|
|
|
|
|
|
|
|
if( !symbol->UseLibIdLookup() )
|
|
|
|
libSymbolLookup = symbol->GetSchSymbolLibraryName();
|
|
|
|
|
|
|
|
auto it = screen->GetLibSymbols().find( libSymbolLookup );
|
|
|
|
|
|
|
|
if( it != screen->GetLibSymbols().end() )
|
|
|
|
libSymbols[ libSymbolLookup ] = it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !libSymbols.empty() )
|
|
|
|
{
|
|
|
|
m_out->Print( 0, "(lib_symbols\n" );
|
|
|
|
|
2022-04-14 16:08:49 +00:00
|
|
|
for( const std::pair<const wxString, LIB_SYMBOL*>& libSymbol : libSymbols )
|
2024-03-28 14:31:20 +00:00
|
|
|
{
|
2023-12-28 23:25:47 +00:00
|
|
|
SCH_IO_KICAD_SEXPR_LIB_CACHE::SaveSymbol( libSymbol.second, *m_out, 1,
|
|
|
|
libSymbol.first );
|
2024-03-28 14:31:20 +00:00
|
|
|
}
|
2020-05-15 19:53:59 +00:00
|
|
|
|
|
|
|
m_out->Print( 0, ")\n\n" );
|
|
|
|
}
|
|
|
|
|
|
|
|
for( i = 0; i < aSelection->GetSize(); ++i )
|
2020-02-13 13:39:52 +00:00
|
|
|
{
|
2020-05-15 19:53:59 +00:00
|
|
|
item = (SCH_ITEM*) aSelection->GetItem( i );
|
2020-02-13 13:39:52 +00:00
|
|
|
|
|
|
|
switch( item->Type() )
|
|
|
|
{
|
2021-06-10 14:10:55 +00:00
|
|
|
case SCH_SYMBOL_T:
|
2023-12-01 19:18:47 +00:00
|
|
|
saveSymbol( static_cast<SCH_SYMBOL*>( item ), aSchematic, 0, aForClipboard,
|
|
|
|
aSelectionPath );
|
2020-02-13 13:39:52 +00:00
|
|
|
break;
|
2020-03-25 15:27:15 +00:00
|
|
|
|
2020-02-13 13:39:52 +00:00
|
|
|
case SCH_BITMAP_T:
|
2020-03-16 13:04:50 +00:00
|
|
|
saveBitmap( static_cast< SCH_BITMAP* >( item ), 0 );
|
2020-02-13 13:39:52 +00:00
|
|
|
break;
|
2020-03-25 15:27:15 +00:00
|
|
|
|
2020-02-13 13:39:52 +00:00
|
|
|
case SCH_SHEET_T:
|
2024-06-06 16:58:48 +00:00
|
|
|
saveSheet( static_cast< SCH_SHEET* >( item ), sheets, 0 );
|
2020-02-13 13:39:52 +00:00
|
|
|
break;
|
2020-03-25 15:27:15 +00:00
|
|
|
|
2020-02-13 13:39:52 +00:00
|
|
|
case SCH_JUNCTION_T:
|
2020-03-16 13:04:50 +00:00
|
|
|
saveJunction( static_cast< SCH_JUNCTION* >( item ), 0 );
|
2020-02-13 13:39:52 +00:00
|
|
|
break;
|
2020-03-25 15:27:15 +00:00
|
|
|
|
2020-02-13 13:39:52 +00:00
|
|
|
case SCH_NO_CONNECT_T:
|
2020-03-16 13:04:50 +00:00
|
|
|
saveNoConnect( static_cast< SCH_NO_CONNECT* >( item ), 0 );
|
2020-02-13 13:39:52 +00:00
|
|
|
break;
|
2020-03-25 15:27:15 +00:00
|
|
|
|
2020-02-13 13:39:52 +00:00
|
|
|
case SCH_BUS_WIRE_ENTRY_T:
|
|
|
|
case SCH_BUS_BUS_ENTRY_T:
|
2020-03-16 13:04:50 +00:00
|
|
|
saveBusEntry( static_cast< SCH_BUS_ENTRY_BASE* >( item ), 0 );
|
2020-02-13 13:39:52 +00:00
|
|
|
break;
|
2020-03-25 15:27:15 +00:00
|
|
|
|
2020-02-13 13:39:52 +00:00
|
|
|
case SCH_LINE_T:
|
2020-03-16 13:04:50 +00:00
|
|
|
saveLine( static_cast< SCH_LINE* >( item ), 0 );
|
2020-02-13 13:39:52 +00:00
|
|
|
break;
|
2020-03-25 15:27:15 +00:00
|
|
|
|
2021-07-17 19:56:18 +00:00
|
|
|
case SCH_SHAPE_T:
|
|
|
|
saveShape( static_cast<SCH_SHAPE*>( item ), 0 );
|
|
|
|
break;
|
|
|
|
|
2024-04-25 14:24:46 +00:00
|
|
|
case SCH_RULE_AREA_T:
|
|
|
|
saveRuleArea( static_cast<SCH_RULE_AREA*>( item ), 0 );
|
|
|
|
break;
|
|
|
|
|
2020-02-13 13:39:52 +00:00
|
|
|
case SCH_TEXT_T:
|
|
|
|
case SCH_LABEL_T:
|
|
|
|
case SCH_GLOBAL_LABEL_T:
|
|
|
|
case SCH_HIER_LABEL_T:
|
2022-01-24 13:40:39 +00:00
|
|
|
case SCH_DIRECTIVE_LABEL_T:
|
2022-02-06 12:01:10 +00:00
|
|
|
saveText( static_cast<SCH_TEXT*>( item ), 0 );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SCH_TEXTBOX_T:
|
|
|
|
saveTextBox( static_cast<SCH_TEXTBOX*>( item ), 0 );
|
2020-02-13 13:39:52 +00:00
|
|
|
break;
|
2020-03-25 15:27:15 +00:00
|
|
|
|
2024-03-28 14:31:20 +00:00
|
|
|
case SCH_TABLECELL_T:
|
|
|
|
{
|
|
|
|
SCH_TABLE* table = static_cast<SCH_TABLE*>( item->GetParent() );
|
|
|
|
|
|
|
|
if( promotedTables.count( table ) )
|
|
|
|
break;
|
|
|
|
|
|
|
|
table->SetFlags( SKIP_STRUCT );
|
|
|
|
saveTable( table, 0 );
|
|
|
|
table->ClearFlags( SKIP_STRUCT );
|
|
|
|
promotedTables.insert( table );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SCH_TABLE_T:
|
|
|
|
item->ClearFlags( SKIP_STRUCT );
|
|
|
|
saveTable( static_cast<SCH_TABLE*>( item ), 0 );
|
|
|
|
break;
|
|
|
|
|
2020-02-13 13:39:52 +00:00
|
|
|
default:
|
2023-12-24 00:31:24 +00:00
|
|
|
wxASSERT( "Unexpected schematic object type in SCH_IO_KICAD_SEXPR::Format()" );
|
2020-02-13 13:39:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
void SCH_IO_KICAD_SEXPR::saveSymbol( SCH_SYMBOL* aSymbol, const SCHEMATIC& aSchematic,
|
2024-06-06 16:58:48 +00:00
|
|
|
const SCH_SHEET_LIST& aSheetList,
|
2023-12-28 23:25:47 +00:00
|
|
|
int aNestLevel, bool aForClipboard,
|
|
|
|
const SCH_SHEET_PATH* aRelativePath )
|
2020-02-13 13:39:52 +00:00
|
|
|
{
|
2020-03-16 13:04:50 +00:00
|
|
|
wxCHECK_RET( aSymbol != nullptr && m_out != nullptr, "" );
|
|
|
|
|
2020-03-25 15:27:15 +00:00
|
|
|
std::string libName;
|
2020-02-13 13:39:52 +00:00
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
wxString symbol_name = aSymbol->GetLibId().Format();
|
2020-02-13 13:39:52 +00:00
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
if( symbol_name.size() )
|
2020-02-13 13:39:52 +00:00
|
|
|
{
|
2021-06-10 18:51:46 +00:00
|
|
|
libName = toUTFTildaText( symbol_name );
|
2020-02-13 13:39:52 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-03-25 15:27:15 +00:00
|
|
|
libName = "_NONAME_";
|
2020-02-13 13:39:52 +00:00
|
|
|
}
|
2020-03-16 13:04:50 +00:00
|
|
|
|
2022-01-14 15:34:41 +00:00
|
|
|
EDA_ANGLE angle;
|
|
|
|
int orientation = aSymbol->GetOrientation() & ~( SYM_MIRROR_X | SYM_MIRROR_Y );
|
2020-03-16 13:04:50 +00:00
|
|
|
|
2021-06-10 14:10:55 +00:00
|
|
|
if( orientation == SYM_ORIENT_90 )
|
2022-01-14 15:34:41 +00:00
|
|
|
angle = ANGLE_90;
|
2021-06-10 14:10:55 +00:00
|
|
|
else if( orientation == SYM_ORIENT_180 )
|
2022-01-14 15:34:41 +00:00
|
|
|
angle = ANGLE_180;
|
2021-06-10 14:10:55 +00:00
|
|
|
else if( orientation == SYM_ORIENT_270 )
|
2022-01-14 15:34:41 +00:00
|
|
|
angle = ANGLE_270;
|
2020-03-16 13:04:50 +00:00
|
|
|
else
|
2022-01-14 15:34:41 +00:00
|
|
|
angle = ANGLE_0;
|
2020-03-16 13:04:50 +00:00
|
|
|
|
2020-04-16 16:43:50 +00:00
|
|
|
m_out->Print( aNestLevel, "(symbol" );
|
|
|
|
|
|
|
|
if( !aSymbol->UseLibIdLookup() )
|
2021-01-25 22:01:49 +00:00
|
|
|
{
|
2020-04-16 16:43:50 +00:00
|
|
|
m_out->Print( 0, " (lib_name %s)",
|
|
|
|
m_out->Quotew( aSymbol->GetSchSymbolLibraryName() ).c_str() );
|
2021-01-25 22:01:49 +00:00
|
|
|
}
|
2020-04-16 16:43:50 +00:00
|
|
|
|
|
|
|
m_out->Print( 0, " (lib_id %s) (at %s %s %s)",
|
|
|
|
m_out->Quotew( aSymbol->GetLibId().Format().wx_str() ).c_str(),
|
2022-11-22 19:35:27 +00:00
|
|
|
EDA_UNIT_UTILS::FormatInternalUnits( schIUScale,
|
|
|
|
aSymbol->GetPosition().x ).c_str(),
|
|
|
|
EDA_UNIT_UTILS::FormatInternalUnits( schIUScale,
|
|
|
|
aSymbol->GetPosition().y ).c_str(),
|
2022-08-29 23:30:25 +00:00
|
|
|
EDA_UNIT_UTILS::FormatAngle( angle ).c_str() );
|
2020-03-16 13:04:50 +00:00
|
|
|
|
2021-06-10 14:10:55 +00:00
|
|
|
bool mirrorX = aSymbol->GetOrientation() & SYM_MIRROR_X;
|
|
|
|
bool mirrorY = aSymbol->GetOrientation() & SYM_MIRROR_Y;
|
2020-03-16 13:04:50 +00:00
|
|
|
|
|
|
|
if( mirrorX || mirrorY )
|
|
|
|
{
|
2020-03-25 15:27:15 +00:00
|
|
|
m_out->Print( 0, " (mirror" );
|
2020-03-16 13:04:50 +00:00
|
|
|
|
|
|
|
if( mirrorX )
|
|
|
|
m_out->Print( 0, " x" );
|
|
|
|
|
|
|
|
if( mirrorY )
|
|
|
|
m_out->Print( 0, " y" );
|
|
|
|
|
2020-03-25 15:27:15 +00:00
|
|
|
m_out->Print( 0, ")" );
|
2020-03-16 13:04:50 +00:00
|
|
|
}
|
2020-03-25 15:27:15 +00:00
|
|
|
|
2022-08-20 17:47:43 +00:00
|
|
|
// The symbol unit is always set to the first instance regardless of the current sheet
|
|
|
|
// instance to prevent file churn.
|
2024-06-06 16:58:48 +00:00
|
|
|
int unit = ( aSymbol->GetInstances().size() == 0 ) ? aSymbol->GetUnit()
|
|
|
|
: aSymbol->GetInstances()[0].m_Unit;
|
2022-03-11 17:37:05 +00:00
|
|
|
|
2023-12-01 19:18:47 +00:00
|
|
|
if( aForClipboard && aRelativePath )
|
|
|
|
{
|
|
|
|
SCH_SYMBOL_INSTANCE unitInstance;
|
|
|
|
|
|
|
|
if( aSymbol->GetInstance( unitInstance, aRelativePath->Path() ) )
|
|
|
|
unit = unitInstance.m_Unit;
|
|
|
|
}
|
|
|
|
|
2022-03-11 17:37:05 +00:00
|
|
|
m_out->Print( 0, " (unit %d)", unit );
|
2020-05-05 13:14:14 +00:00
|
|
|
|
2024-04-06 13:14:44 +00:00
|
|
|
if( aSymbol->GetBodyStyle() == BODY_STYLE::DEMORGAN )
|
2024-01-26 16:16:13 +00:00
|
|
|
m_out->Print( 0, " (convert %d)", aSymbol->GetBodyStyle() );
|
2020-05-05 13:14:14 +00:00
|
|
|
|
|
|
|
m_out->Print( 0, "\n" );
|
2020-03-16 13:04:50 +00:00
|
|
|
|
2023-08-08 16:32:08 +00:00
|
|
|
m_out->Print( aNestLevel + 1, "(exclude_from_sim %s)",
|
|
|
|
( aSymbol->GetExcludedFromSim() ) ? "yes" : "no" );
|
|
|
|
m_out->Print( 0, " (in_bom %s)", ( aSymbol->GetExcludedFromBOM() ) ? "no" : "yes" );
|
2023-06-25 02:03:06 +00:00
|
|
|
m_out->Print( 0, " (on_board %s)", ( aSymbol->GetExcludedFromBoard() ) ? "no" : "yes" );
|
2022-09-16 16:20:36 +00:00
|
|
|
m_out->Print( 0, " (dnp %s)", ( aSymbol->GetDNP() ) ? "yes" : "no" );
|
2020-06-03 12:30:57 +00:00
|
|
|
|
2021-04-02 16:08:13 +00:00
|
|
|
if( aSymbol->GetFieldsAutoplaced() != FIELDS_AUTOPLACED_NO )
|
2023-11-24 19:29:53 +00:00
|
|
|
m_out->Print( 0, " (fields_autoplaced yes)" );
|
2021-04-02 16:08:13 +00:00
|
|
|
|
2020-06-03 12:30:57 +00:00
|
|
|
m_out->Print( 0, "\n" );
|
|
|
|
|
2023-11-22 17:32:41 +00:00
|
|
|
KICAD_FORMAT::FormatUuid( m_out, aSymbol->m_Uuid );
|
2020-03-16 13:04:50 +00:00
|
|
|
|
2021-03-09 15:08:51 +00:00
|
|
|
m_nextFreeFieldId = MANDATORY_FIELDS;
|
2020-05-01 15:57:45 +00:00
|
|
|
|
2020-03-26 11:02:59 +00:00
|
|
|
for( SCH_FIELD& field : aSymbol->GetFields() )
|
2020-03-16 13:04:50 +00:00
|
|
|
{
|
2022-03-11 00:32:34 +00:00
|
|
|
int id = field.GetId();
|
|
|
|
wxString value = field.GetText();
|
|
|
|
|
2023-12-28 23:25:47 +00:00
|
|
|
if( !aForClipboard && aSymbol->GetInstances().size() )
|
2022-03-11 17:37:05 +00:00
|
|
|
{
|
2022-04-14 16:08:49 +00:00
|
|
|
// The instance fields are always set to the default instance regardless of the
|
|
|
|
// sheet instance to prevent file churn.
|
|
|
|
if( id == REFERENCE_FIELD )
|
|
|
|
{
|
2023-12-28 23:25:47 +00:00
|
|
|
field.SetText( aSymbol->GetInstances()[0].m_Reference );
|
2022-04-14 16:08:49 +00:00
|
|
|
}
|
|
|
|
else if( id == VALUE_FIELD )
|
|
|
|
{
|
2023-05-05 13:21:56 +00:00
|
|
|
field.SetText( aSymbol->GetField( VALUE_FIELD )->GetText() );
|
2022-04-14 16:08:49 +00:00
|
|
|
}
|
|
|
|
else if( id == FOOTPRINT_FIELD )
|
|
|
|
{
|
2023-05-05 13:21:56 +00:00
|
|
|
field.SetText( aSymbol->GetField( FOOTPRINT_FIELD )->GetText() );
|
2022-04-14 16:08:49 +00:00
|
|
|
}
|
2022-03-11 17:37:05 +00:00
|
|
|
}
|
2023-12-28 23:25:47 +00:00
|
|
|
else if( aForClipboard && aSymbol->GetInstances().size() && aRelativePath
|
2023-12-01 19:18:47 +00:00
|
|
|
&& ( id == REFERENCE_FIELD ) )
|
|
|
|
{
|
|
|
|
SCH_SYMBOL_INSTANCE instance;
|
|
|
|
|
|
|
|
if( aSymbol->GetInstance( instance, aRelativePath->Path() ) )
|
|
|
|
field.SetText( instance.m_Reference );
|
|
|
|
}
|
2022-03-11 00:32:34 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
saveField( &field, aNestLevel + 1 );
|
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
2022-03-11 17:37:05 +00:00
|
|
|
// Restore the changed field text on write error.
|
2022-03-11 00:32:34 +00:00
|
|
|
if( id == REFERENCE_FIELD || id == VALUE_FIELD || id == FOOTPRINT_FIELD )
|
|
|
|
field.SetText( value );
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( id == REFERENCE_FIELD || id == VALUE_FIELD || id == FOOTPRINT_FIELD )
|
|
|
|
field.SetText( value );
|
2020-03-16 13:04:50 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 00:32:34 +00:00
|
|
|
for( const std::unique_ptr<SCH_PIN>& pin : aSymbol->GetRawPins() )
|
2020-08-21 15:54:24 +00:00
|
|
|
{
|
2021-01-25 22:01:49 +00:00
|
|
|
if( pin->GetAlt().IsEmpty() )
|
|
|
|
{
|
2023-11-22 17:32:41 +00:00
|
|
|
m_out->Print( aNestLevel + 1, "(pin %s ", m_out->Quotew( pin->GetNumber() ).c_str() );
|
|
|
|
KICAD_FORMAT::FormatUuid( m_out, pin->m_Uuid );
|
|
|
|
m_out->Print( aNestLevel + 1, ")\n" );
|
2021-01-25 22:01:49 +00:00
|
|
|
}
|
|
|
|
else
|
2020-08-21 15:54:24 +00:00
|
|
|
{
|
2023-11-22 17:32:41 +00:00
|
|
|
m_out->Print( aNestLevel + 1, "(pin %s ", m_out->Quotew( pin->GetNumber() ).c_str() );
|
|
|
|
KICAD_FORMAT::FormatUuid( m_out, pin->m_Uuid );
|
|
|
|
m_out->Print( aNestLevel + 1, " (alternate %s))\n",
|
2020-08-21 15:54:24 +00:00
|
|
|
m_out->Quotew( pin->GetAlt() ).c_str() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-28 23:25:47 +00:00
|
|
|
if( !aSymbol->GetInstances().empty() )
|
2022-10-14 22:16:28 +00:00
|
|
|
{
|
2024-04-20 12:57:00 +00:00
|
|
|
std::map<KIID, std::vector<SCH_SYMBOL_INSTANCE>> projectInstances;
|
|
|
|
|
2022-10-14 22:16:28 +00:00
|
|
|
m_out->Print( aNestLevel + 1, "(instances\n" );
|
2022-10-01 12:44:21 +00:00
|
|
|
|
2024-04-20 12:57:00 +00:00
|
|
|
wxString projectName;
|
2022-10-14 22:16:28 +00:00
|
|
|
KIID lastProjectUuid;
|
|
|
|
KIID rootSheetUuid = aSchematic.Root().m_Uuid;
|
2022-10-01 12:44:21 +00:00
|
|
|
|
2024-04-20 12:57:00 +00:00
|
|
|
for( const SCH_SYMBOL_INSTANCE& inst : aSymbol->GetInstances() )
|
2022-10-01 12:44:21 +00:00
|
|
|
{
|
2023-12-03 12:47:57 +00:00
|
|
|
// Zero length KIID_PATH objects are not valid and will cause a crash below.
|
2024-04-20 12:57:00 +00:00
|
|
|
wxCHECK2( inst.m_Path.size(), continue );
|
2023-12-03 12:47:57 +00:00
|
|
|
|
2022-10-14 22:16:28 +00:00
|
|
|
// If the instance data is part of this design but no longer has an associated sheet
|
|
|
|
// path, don't save it. This prevents large amounts of orphaned instance data for the
|
|
|
|
// current project from accumulating in the schematic files.
|
2024-04-20 12:57:00 +00:00
|
|
|
bool isOrphaned = ( inst.m_Path[0] == rootSheetUuid )
|
2024-06-06 16:58:48 +00:00
|
|
|
&& !aSheetList.GetSheetPathByKIIDPath( inst.m_Path );
|
2022-10-01 12:44:21 +00:00
|
|
|
|
2024-04-20 12:57:00 +00:00
|
|
|
// Keep all instance data when copying to the clipboard. They may be needed on paste.
|
|
|
|
if( !aForClipboard && isOrphaned )
|
2022-10-14 22:16:28 +00:00
|
|
|
continue;
|
2022-10-01 12:44:21 +00:00
|
|
|
|
2024-04-20 12:57:00 +00:00
|
|
|
auto it = projectInstances.find( inst.m_Path[0] );
|
2022-10-01 12:44:21 +00:00
|
|
|
|
2024-04-20 12:57:00 +00:00
|
|
|
if( it == projectInstances.end() )
|
|
|
|
{
|
|
|
|
projectInstances[ inst.m_Path[0] ] = { inst };
|
2022-10-14 22:16:28 +00:00
|
|
|
}
|
2024-04-20 12:57:00 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
it->second.emplace_back( inst );
|
|
|
|
}
|
|
|
|
}
|
2022-10-01 12:44:21 +00:00
|
|
|
|
2024-04-20 12:57:00 +00:00
|
|
|
for( auto& [uuid, instances] : projectInstances )
|
|
|
|
{
|
|
|
|
wxCHECK2( instances.size(), continue );
|
2023-12-01 19:18:47 +00:00
|
|
|
|
2024-04-20 12:57:00 +00:00
|
|
|
// Sort project instances by KIID_PATH.
|
|
|
|
std::sort( instances.begin(), instances.end(),
|
|
|
|
[]( SCH_SYMBOL_INSTANCE& aLhs, SCH_SYMBOL_INSTANCE& aRhs )
|
|
|
|
{
|
|
|
|
return aLhs.m_Path < aRhs.m_Path;
|
|
|
|
} );
|
2023-12-01 19:18:47 +00:00
|
|
|
|
2024-04-20 12:57:00 +00:00
|
|
|
projectName = instances[0].m_ProjectName;
|
2022-10-01 12:44:21 +00:00
|
|
|
|
2024-04-20 12:57:00 +00:00
|
|
|
m_out->Print( aNestLevel + 2, "(project %s\n",
|
|
|
|
m_out->Quotew( projectName ).c_str() );
|
2022-10-14 22:16:28 +00:00
|
|
|
|
2024-04-20 12:57:00 +00:00
|
|
|
for( const SCH_SYMBOL_INSTANCE& instance : instances )
|
2022-10-14 22:16:28 +00:00
|
|
|
{
|
2024-04-20 12:57:00 +00:00
|
|
|
wxString path;
|
|
|
|
KIID_PATH tmp = instance.m_Path;
|
|
|
|
|
|
|
|
if( aForClipboard && aRelativePath )
|
|
|
|
tmp.MakeRelativeTo( aRelativePath->Path() );
|
|
|
|
|
|
|
|
path = tmp.AsString();
|
|
|
|
|
|
|
|
m_out->Print( aNestLevel + 3, "(path %s\n",
|
|
|
|
m_out->Quotew( path ).c_str() );
|
|
|
|
m_out->Print( aNestLevel + 4, "(reference %s) (unit %d)\n",
|
|
|
|
m_out->Quotew( instance.m_Reference ).c_str(),
|
|
|
|
instance.m_Unit );
|
|
|
|
m_out->Print( aNestLevel + 3, ")\n" );
|
2022-10-14 22:16:28 +00:00
|
|
|
}
|
2024-04-20 12:57:00 +00:00
|
|
|
|
|
|
|
m_out->Print( aNestLevel + 2, ")\n" ); // Closes `project`.
|
2022-10-14 22:16:28 +00:00
|
|
|
}
|
2022-10-01 12:44:21 +00:00
|
|
|
|
2022-10-14 22:16:28 +00:00
|
|
|
m_out->Print( aNestLevel + 1, ")\n" ); // Closes `instances`.
|
2022-10-01 12:44:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_out->Print( aNestLevel, ")\n" ); // Closes `symbol`.
|
2020-02-13 13:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
void SCH_IO_KICAD_SEXPR::saveField( SCH_FIELD* aField, int aNestLevel )
|
2020-02-13 13:39:52 +00:00
|
|
|
{
|
2020-03-16 13:04:50 +00:00
|
|
|
wxCHECK_RET( aField != nullptr && m_out != nullptr, "" );
|
|
|
|
|
2021-10-12 20:05:37 +00:00
|
|
|
wxString fieldName = aField->GetCanonicalName();
|
2020-03-25 15:27:15 +00:00
|
|
|
// For some reason (bug in legacy parser?) the field ID for non-mandatory fields is -1 so
|
|
|
|
// check for this in order to correctly use the field name.
|
2020-03-16 13:04:50 +00:00
|
|
|
|
2020-05-01 15:57:45 +00:00
|
|
|
if( aField->GetId() == -1 /* undefined ID */ )
|
|
|
|
{
|
2023-08-01 11:37:53 +00:00
|
|
|
// Replace the default name built by GetCanonicalName() by
|
|
|
|
// the field name if exists
|
|
|
|
if( !aField->GetName().IsEmpty() )
|
|
|
|
fieldName = aField->GetName();
|
|
|
|
|
2021-03-09 15:08:51 +00:00
|
|
|
aField->SetId( m_nextFreeFieldId );
|
|
|
|
m_nextFreeFieldId += 1;
|
|
|
|
}
|
|
|
|
else if( aField->GetId() >= m_nextFreeFieldId )
|
|
|
|
{
|
|
|
|
m_nextFreeFieldId = aField->GetId() + 1;
|
2020-05-01 15:57:45 +00:00
|
|
|
}
|
|
|
|
|
2022-10-01 00:16:24 +00:00
|
|
|
m_out->Print( aNestLevel, "(property %s %s (at %s %s %s)",
|
2020-03-16 13:04:50 +00:00
|
|
|
m_out->Quotew( fieldName ).c_str(),
|
|
|
|
m_out->Quotew( aField->GetText() ).c_str(),
|
2022-11-22 19:35:27 +00:00
|
|
|
EDA_UNIT_UTILS::FormatInternalUnits( schIUScale,
|
|
|
|
aField->GetPosition().x ).c_str(),
|
|
|
|
EDA_UNIT_UTILS::FormatInternalUnits( schIUScale,
|
|
|
|
aField->GetPosition().y ).c_str(),
|
2022-08-29 23:30:25 +00:00
|
|
|
EDA_UNIT_UTILS::FormatAngle( aField->GetTextAngle() ).c_str() );
|
2020-03-16 13:04:50 +00:00
|
|
|
|
2022-09-03 20:54:05 +00:00
|
|
|
if( aField->IsNameShown() )
|
2023-11-24 19:29:53 +00:00
|
|
|
m_out->Print( 0, " (show_name yes)" );
|
2022-10-15 18:08:11 +00:00
|
|
|
|
|
|
|
if( !aField->CanAutoplace() )
|
2023-11-24 19:29:53 +00:00
|
|
|
m_out->Print( 0, " (do_not_autoplace yes)" );
|
2022-09-03 20:54:05 +00:00
|
|
|
|
2020-04-05 22:51:37 +00:00
|
|
|
if( !aField->IsDefaultFormatting()
|
2022-09-16 23:42:20 +00:00
|
|
|
|| ( aField->GetTextHeight() != schIUScale.MilsToIU( DEFAULT_SIZE_TEXT ) ) )
|
2020-03-16 13:04:50 +00:00
|
|
|
{
|
|
|
|
m_out->Print( 0, "\n" );
|
|
|
|
aField->Format( m_out, aNestLevel, 0 );
|
|
|
|
m_out->Print( aNestLevel, ")\n" ); // Closes property token with font effects.
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_out->Print( 0, ")\n" ); // Closes property token without font effects.
|
|
|
|
}
|
2020-02-13 13:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
void SCH_IO_KICAD_SEXPR::saveBitmap( SCH_BITMAP* aBitmap, int aNestLevel )
|
2020-02-13 13:39:52 +00:00
|
|
|
{
|
2020-03-16 13:04:50 +00:00
|
|
|
wxCHECK_RET( aBitmap != nullptr && m_out != nullptr, "" );
|
2020-02-13 13:39:52 +00:00
|
|
|
|
|
|
|
const wxImage* image = aBitmap->GetImage()->GetImageData();
|
|
|
|
|
2021-07-16 20:13:26 +00:00
|
|
|
wxCHECK_RET( image != nullptr, "wxImage* is NULL" );
|
2020-02-13 13:39:52 +00:00
|
|
|
|
2020-03-25 15:27:15 +00:00
|
|
|
m_out->Print( aNestLevel, "(image (at %s %s)",
|
2022-11-22 19:35:27 +00:00
|
|
|
EDA_UNIT_UTILS::FormatInternalUnits( schIUScale,
|
|
|
|
aBitmap->GetPosition().x ).c_str(),
|
|
|
|
EDA_UNIT_UTILS::FormatInternalUnits( schIUScale,
|
|
|
|
aBitmap->GetPosition().y ).c_str() );
|
2020-03-16 13:04:50 +00:00
|
|
|
|
2023-02-17 00:41:57 +00:00
|
|
|
double scale = aBitmap->GetImage()->GetScale();
|
|
|
|
|
|
|
|
// 20230121 or older file format versions assumed 300 image PPI at load/save.
|
|
|
|
// Let's keep compatibility by changing image scale.
|
|
|
|
if( SEXPR_SCHEMATIC_FILE_VERSION <= 20230121 )
|
|
|
|
{
|
2023-02-18 12:04:30 +00:00
|
|
|
BITMAP_BASE* bm_image = aBitmap->GetImage();
|
|
|
|
scale = scale * 300.0 / bm_image->GetPPI();
|
2023-02-17 00:41:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( scale != 1.0 )
|
|
|
|
m_out->Print( 0, " (scale %g)", scale );
|
2020-03-16 13:04:50 +00:00
|
|
|
|
|
|
|
m_out->Print( 0, "\n" );
|
2021-01-25 22:01:49 +00:00
|
|
|
|
2023-11-22 17:32:41 +00:00
|
|
|
KICAD_FORMAT::FormatUuid( m_out, aBitmap->m_Uuid );
|
2021-01-25 22:01:49 +00:00
|
|
|
|
2020-03-16 13:04:50 +00:00
|
|
|
m_out->Print( aNestLevel + 1, "(data" );
|
2020-02-13 13:39:52 +00:00
|
|
|
|
2023-06-08 15:54:36 +00:00
|
|
|
wxString out = wxBase64Encode( aBitmap->GetImage()->GetImageDataBuffer() );
|
2020-02-13 13:39:52 +00:00
|
|
|
|
2020-03-27 12:52:40 +00:00
|
|
|
// Apparently the MIME standard character width for base64 encoding is 76 (unconfirmed)
|
|
|
|
// so use it in a vein attempt to be standard like.
|
|
|
|
#define MIME_BASE64_LENGTH 76
|
2020-02-13 13:39:52 +00:00
|
|
|
|
2020-03-27 12:52:40 +00:00
|
|
|
size_t first = 0;
|
|
|
|
|
|
|
|
while( first < out.Length() )
|
|
|
|
{
|
|
|
|
m_out->Print( 0, "\n" );
|
2023-11-25 18:28:31 +00:00
|
|
|
m_out->Print( aNestLevel + 2, "\"%s\"", TO_UTF8( out( first, MIME_BASE64_LENGTH ) ) );
|
2020-03-27 12:52:40 +00:00
|
|
|
first += MIME_BASE64_LENGTH;
|
2020-02-13 13:39:52 +00:00
|
|
|
}
|
|
|
|
|
2020-03-25 15:27:15 +00:00
|
|
|
m_out->Print( 0, "\n" );
|
2020-03-16 13:04:50 +00:00
|
|
|
m_out->Print( aNestLevel + 1, ")\n" ); // Closes data token.
|
|
|
|
m_out->Print( aNestLevel, ")\n" ); // Closes image token.
|
2020-02-13 13:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-06-06 16:58:48 +00:00
|
|
|
void SCH_IO_KICAD_SEXPR::saveSheet( SCH_SHEET* aSheet, const SCH_SHEET_LIST& aSheetList,
|
|
|
|
int aNestLevel )
|
2020-02-13 13:39:52 +00:00
|
|
|
{
|
2020-03-16 13:04:50 +00:00
|
|
|
wxCHECK_RET( aSheet != nullptr && m_out != nullptr, "" );
|
|
|
|
|
2021-04-02 16:08:13 +00:00
|
|
|
m_out->Print( aNestLevel, "(sheet (at %s %s) (size %s %s)",
|
2022-11-22 19:35:27 +00:00
|
|
|
EDA_UNIT_UTILS::FormatInternalUnits( schIUScale,
|
|
|
|
aSheet->GetPosition().x ).c_str(),
|
|
|
|
EDA_UNIT_UTILS::FormatInternalUnits( schIUScale,
|
|
|
|
aSheet->GetPosition().y ).c_str(),
|
|
|
|
EDA_UNIT_UTILS::FormatInternalUnits( schIUScale,
|
2023-02-19 03:40:07 +00:00
|
|
|
aSheet->GetSize().x ).c_str(),
|
2022-11-22 19:35:27 +00:00
|
|
|
EDA_UNIT_UTILS::FormatInternalUnits( schIUScale,
|
2023-02-19 03:40:07 +00:00
|
|
|
aSheet->GetSize().y ).c_str() );
|
2020-03-16 13:04:50 +00:00
|
|
|
|
2024-06-02 21:26:49 +00:00
|
|
|
m_out->Print( 0, " (exclude_from_sim %s)", ( aSheet->GetExcludedFromSim() ) ? "yes" : "no" );
|
|
|
|
m_out->Print( 0, " (in_bom %s)", ( aSheet->GetExcludedFromBOM() ) ? "no" : "yes" );
|
|
|
|
m_out->Print( 0, " (on_board %s)", ( aSheet->GetExcludedFromBoard() ) ? "no" : "yes" );
|
|
|
|
m_out->Print( 0, " (dnp %s)", ( aSheet->GetDNP() ) ? "yes" : "no" );
|
|
|
|
|
2021-04-02 16:08:13 +00:00
|
|
|
if( aSheet->GetFieldsAutoplaced() != FIELDS_AUTOPLACED_NO )
|
2023-11-24 19:29:53 +00:00
|
|
|
m_out->Print( 0, " (fields_autoplaced yes)" );
|
2021-04-02 16:08:13 +00:00
|
|
|
|
|
|
|
m_out->Print( 0, "\n" );
|
|
|
|
|
2023-11-25 13:05:45 +00:00
|
|
|
STROKE_PARAMS stroke( aSheet->GetBorderWidth(), LINE_STYLE::SOLID, aSheet->GetBorderColor() );
|
2020-06-19 11:48:00 +00:00
|
|
|
|
|
|
|
stroke.SetWidth( aSheet->GetBorderWidth() );
|
2022-09-16 04:38:10 +00:00
|
|
|
stroke.Format( m_out, schIUScale, aNestLevel + 1 );
|
2020-04-07 14:00:58 +00:00
|
|
|
|
2020-03-16 13:04:50 +00:00
|
|
|
m_out->Print( 0, "\n" );
|
|
|
|
|
2020-07-29 11:26:15 +00:00
|
|
|
m_out->Print( aNestLevel + 1, "(fill (color %d %d %d %0.4f))\n",
|
2020-05-19 17:22:54 +00:00
|
|
|
KiROUND( aSheet->GetBackgroundColor().r * 255.0 ),
|
|
|
|
KiROUND( aSheet->GetBackgroundColor().g * 255.0 ),
|
|
|
|
KiROUND( aSheet->GetBackgroundColor().b * 255.0 ),
|
|
|
|
aSheet->GetBackgroundColor().a );
|
|
|
|
|
2023-11-22 17:32:41 +00:00
|
|
|
KICAD_FORMAT::FormatUuid( m_out, aSheet->m_Uuid );
|
2020-05-19 17:22:54 +00:00
|
|
|
|
2021-03-09 15:08:51 +00:00
|
|
|
m_nextFreeFieldId = SHEET_MANDATORY_FIELDS;
|
2020-05-06 14:47:51 +00:00
|
|
|
|
2020-05-06 15:55:37 +00:00
|
|
|
for( SCH_FIELD& field : aSheet->GetFields() )
|
2020-03-16 13:04:50 +00:00
|
|
|
{
|
2020-05-06 15:55:37 +00:00
|
|
|
saveField( &field, aNestLevel + 1 );
|
2020-03-16 13:04:50 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 14:00:58 +00:00
|
|
|
for( const SCH_SHEET_PIN* pin : aSheet->GetPins() )
|
2020-03-16 13:04:50 +00:00
|
|
|
{
|
2020-05-19 17:22:54 +00:00
|
|
|
m_out->Print( aNestLevel + 1, "(pin %s %s (at %s %s %s)\n",
|
2020-03-16 13:04:50 +00:00
|
|
|
EscapedUTF8( pin->GetText() ).c_str(),
|
|
|
|
getSheetPinShapeToken( pin->GetShape() ),
|
2022-11-22 19:35:27 +00:00
|
|
|
EDA_UNIT_UTILS::FormatInternalUnits( schIUScale,
|
|
|
|
pin->GetPosition().x ).c_str(),
|
|
|
|
EDA_UNIT_UTILS::FormatInternalUnits( schIUScale,
|
|
|
|
pin->GetPosition().y ).c_str(),
|
2022-08-29 23:30:25 +00:00
|
|
|
EDA_UNIT_UTILS::FormatAngle( getSheetPinAngle( pin->GetSide() ) ).c_str() );
|
2020-03-16 13:04:50 +00:00
|
|
|
|
2020-05-19 17:22:54 +00:00
|
|
|
pin->Format( m_out, aNestLevel + 1, 0 );
|
2021-01-25 22:01:49 +00:00
|
|
|
|
2023-11-22 17:32:41 +00:00
|
|
|
KICAD_FORMAT::FormatUuid( m_out, pin->m_Uuid );
|
2021-01-25 22:01:49 +00:00
|
|
|
|
|
|
|
m_out->Print( aNestLevel + 1, ")\n" ); // Closes pin token.
|
2020-03-16 13:04:50 +00:00
|
|
|
}
|
|
|
|
|
2022-12-25 16:04:45 +00:00
|
|
|
// Save all sheet instances here except the root sheet instance.
|
2022-12-09 15:02:16 +00:00
|
|
|
std::vector< SCH_SHEET_INSTANCE > sheetInstances = aSheet->GetInstances();
|
|
|
|
|
|
|
|
auto it = sheetInstances.begin();
|
|
|
|
|
|
|
|
while( it != sheetInstances.end() )
|
|
|
|
{
|
|
|
|
if( it->m_Path.size() == 0 )
|
|
|
|
it = sheetInstances.erase( it );
|
|
|
|
else
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !sheetInstances.empty() )
|
2022-11-22 19:35:27 +00:00
|
|
|
{
|
|
|
|
m_out->Print( aNestLevel + 1, "(instances\n" );
|
|
|
|
|
|
|
|
KIID lastProjectUuid;
|
|
|
|
KIID rootSheetUuid = m_schematic->Root().m_Uuid;
|
|
|
|
bool project_open = false;
|
|
|
|
|
2022-12-09 15:02:16 +00:00
|
|
|
for( size_t i = 0; i < sheetInstances.size(); i++ )
|
2022-11-22 19:35:27 +00:00
|
|
|
{
|
|
|
|
// If the instance data is part of this design but no longer has an associated sheet
|
|
|
|
// path, don't save it. This prevents large amounts of orphaned instance data for the
|
|
|
|
// current project from accumulating in the schematic files.
|
|
|
|
//
|
|
|
|
// Keep all instance data when copying to the clipboard. It may be needed on paste.
|
2022-12-09 15:02:16 +00:00
|
|
|
if( ( sheetInstances[i].m_Path[0] == rootSheetUuid )
|
2024-06-06 16:58:48 +00:00
|
|
|
&& !aSheetList.GetSheetPathByKIIDPath( sheetInstances[i].m_Path, false ) )
|
2022-11-22 19:35:27 +00:00
|
|
|
{
|
2022-12-09 15:02:16 +00:00
|
|
|
if( project_open && ( ( i + 1 == sheetInstances.size() )
|
|
|
|
|| lastProjectUuid != sheetInstances[i+1].m_Path[0] ) )
|
2022-11-22 19:35:27 +00:00
|
|
|
{
|
2022-12-25 16:04:45 +00:00
|
|
|
m_out->Print( aNestLevel + 2, ")\n" ); // Closes `project` token.
|
2022-11-22 19:35:27 +00:00
|
|
|
project_open = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-12-09 15:02:16 +00:00
|
|
|
if( lastProjectUuid != sheetInstances[i].m_Path[0] )
|
2022-11-22 19:35:27 +00:00
|
|
|
{
|
|
|
|
wxString projectName;
|
|
|
|
|
2022-12-09 15:02:16 +00:00
|
|
|
if( sheetInstances[i].m_Path[0] == rootSheetUuid )
|
2022-11-22 19:35:27 +00:00
|
|
|
projectName = m_schematic->Prj().GetProjectName();
|
|
|
|
else
|
2022-12-09 15:02:16 +00:00
|
|
|
projectName = sheetInstances[i].m_ProjectName;
|
2022-11-22 19:35:27 +00:00
|
|
|
|
2022-12-09 15:02:16 +00:00
|
|
|
lastProjectUuid = sheetInstances[i].m_Path[0];
|
2022-11-22 19:35:27 +00:00
|
|
|
m_out->Print( aNestLevel + 2, "(project %s\n",
|
|
|
|
m_out->Quotew( projectName ).c_str() );
|
|
|
|
project_open = true;
|
|
|
|
}
|
|
|
|
|
2022-12-09 15:02:16 +00:00
|
|
|
wxString path = sheetInstances[i].m_Path.AsString();
|
2022-11-22 19:35:27 +00:00
|
|
|
|
|
|
|
m_out->Print( aNestLevel + 3, "(path %s (page %s))\n",
|
|
|
|
m_out->Quotew( path ).c_str(),
|
2022-12-09 15:02:16 +00:00
|
|
|
m_out->Quotew( sheetInstances[i].m_PageNumber ).c_str() );
|
2022-11-22 19:35:27 +00:00
|
|
|
|
2022-12-09 15:02:16 +00:00
|
|
|
if( project_open && ( ( i + 1 == sheetInstances.size() )
|
|
|
|
|| lastProjectUuid != sheetInstances[i+1].m_Path[0] ) )
|
2022-11-22 19:35:27 +00:00
|
|
|
{
|
2022-12-25 16:04:45 +00:00
|
|
|
m_out->Print( aNestLevel + 2, ")\n" ); // Closes `project` token.
|
2022-11-22 19:35:27 +00:00
|
|
|
project_open = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-25 16:04:45 +00:00
|
|
|
m_out->Print( aNestLevel + 1, ")\n" ); // Closes `instances` token.
|
2022-11-22 19:35:27 +00:00
|
|
|
}
|
|
|
|
|
2020-05-19 17:22:54 +00:00
|
|
|
m_out->Print( aNestLevel, ")\n" ); // Closes sheet token.
|
2020-02-13 13:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
void SCH_IO_KICAD_SEXPR::saveJunction( SCH_JUNCTION* aJunction, int aNestLevel )
|
2020-02-13 13:39:52 +00:00
|
|
|
{
|
2020-03-16 13:04:50 +00:00
|
|
|
wxCHECK_RET( aJunction != nullptr && m_out != nullptr, "" );
|
|
|
|
|
2021-11-23 22:15:25 +00:00
|
|
|
m_out->Print( aNestLevel, "(junction (at %s %s) (diameter %s) (color %d %d %d %s)\n",
|
2022-11-22 19:35:27 +00:00
|
|
|
EDA_UNIT_UTILS::FormatInternalUnits( schIUScale,
|
|
|
|
aJunction->GetPosition().x ).c_str(),
|
|
|
|
EDA_UNIT_UTILS::FormatInternalUnits( schIUScale,
|
|
|
|
aJunction->GetPosition().y ).c_str(),
|
|
|
|
EDA_UNIT_UTILS::FormatInternalUnits( schIUScale,
|
|
|
|
aJunction->GetDiameter() ).c_str(),
|
2020-06-24 17:35:33 +00:00
|
|
|
KiROUND( aJunction->GetColor().r * 255.0 ),
|
|
|
|
KiROUND( aJunction->GetColor().g * 255.0 ),
|
|
|
|
KiROUND( aJunction->GetColor().b * 255.0 ),
|
2022-09-17 03:35:16 +00:00
|
|
|
FormatDouble2Str( aJunction->GetColor().a ).c_str() );
|
2021-11-23 22:15:25 +00:00
|
|
|
|
2023-11-22 17:32:41 +00:00
|
|
|
KICAD_FORMAT::FormatUuid( m_out, aJunction->m_Uuid );
|
2021-11-23 22:15:25 +00:00
|
|
|
|
|
|
|
m_out->Print( aNestLevel, ")\n" );
|
2020-02-13 13:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
void SCH_IO_KICAD_SEXPR::saveNoConnect( SCH_NO_CONNECT* aNoConnect, int aNestLevel )
|
2020-02-13 13:39:52 +00:00
|
|
|
{
|
2020-03-16 13:04:50 +00:00
|
|
|
wxCHECK_RET( aNoConnect != nullptr && m_out != nullptr, "" );
|
|
|
|
|
2023-11-22 17:32:41 +00:00
|
|
|
m_out->Print( aNestLevel, "(no_connect (at %s %s)",
|
2022-11-22 19:35:27 +00:00
|
|
|
EDA_UNIT_UTILS::FormatInternalUnits( schIUScale,
|
|
|
|
aNoConnect->GetPosition().x ).c_str(),
|
|
|
|
EDA_UNIT_UTILS::FormatInternalUnits( schIUScale,
|
2023-11-22 17:32:41 +00:00
|
|
|
aNoConnect->GetPosition().y ).c_str() );
|
|
|
|
KICAD_FORMAT::FormatUuid( m_out, aNoConnect->m_Uuid );
|
|
|
|
m_out->Print( aNestLevel, ")\n" );
|
2020-02-13 13:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
void SCH_IO_KICAD_SEXPR::saveBusEntry( SCH_BUS_ENTRY_BASE* aBusEntry, int aNestLevel )
|
2020-02-13 13:39:52 +00:00
|
|
|
{
|
2020-03-16 13:04:50 +00:00
|
|
|
wxCHECK_RET( aBusEntry != nullptr && m_out != nullptr, "" );
|
|
|
|
|
|
|
|
// Bus to bus entries are converted to bus line segments.
|
|
|
|
if( aBusEntry->GetClass() == "SCH_BUS_BUS_ENTRY" )
|
|
|
|
{
|
|
|
|
SCH_LINE busEntryLine( aBusEntry->GetPosition(), LAYER_BUS );
|
|
|
|
|
2020-09-08 13:27:13 +00:00
|
|
|
busEntryLine.SetEndPoint( aBusEntry->GetEnd() );
|
2020-03-16 13:04:50 +00:00
|
|
|
saveLine( &busEntryLine, aNestLevel );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-06-24 17:35:33 +00:00
|
|
|
m_out->Print( aNestLevel, "(bus_entry (at %s %s) (size %s %s)\n",
|
2022-11-22 19:35:27 +00:00
|
|
|
EDA_UNIT_UTILS::FormatInternalUnits( schIUScale,
|
|
|
|
aBusEntry->GetPosition().x ).c_str(),
|
|
|
|
EDA_UNIT_UTILS::FormatInternalUnits( schIUScale,
|
|
|
|
aBusEntry->GetPosition().y ).c_str(),
|
|
|
|
EDA_UNIT_UTILS::FormatInternalUnits( schIUScale,
|
2023-02-19 03:40:07 +00:00
|
|
|
aBusEntry->GetSize().x ).c_str(),
|
2022-11-22 19:35:27 +00:00
|
|
|
EDA_UNIT_UTILS::FormatInternalUnits( schIUScale,
|
2023-02-19 03:40:07 +00:00
|
|
|
aBusEntry->GetSize().y ).c_str() );
|
2020-06-19 11:48:00 +00:00
|
|
|
|
2022-09-16 04:38:10 +00:00
|
|
|
aBusEntry->GetStroke().Format( m_out, schIUScale, aNestLevel + 1 );
|
2020-06-19 11:48:00 +00:00
|
|
|
|
2020-06-24 17:35:33 +00:00
|
|
|
m_out->Print( 0, "\n" );
|
2021-01-25 22:01:49 +00:00
|
|
|
|
2023-11-22 17:32:41 +00:00
|
|
|
KICAD_FORMAT::FormatUuid( m_out, aBusEntry->m_Uuid );
|
2021-01-25 22:01:49 +00:00
|
|
|
|
2020-06-24 17:35:33 +00:00
|
|
|
m_out->Print( aNestLevel, ")\n" );
|
2020-03-16 13:04:50 +00:00
|
|
|
}
|
2020-02-13 13:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
void SCH_IO_KICAD_SEXPR::saveShape( SCH_SHAPE* aShape, int aNestLevel )
|
2021-07-17 19:56:18 +00:00
|
|
|
{
|
|
|
|
wxCHECK_RET( aShape != nullptr && m_out != nullptr, "" );
|
|
|
|
|
|
|
|
switch( aShape->GetShape() )
|
|
|
|
{
|
|
|
|
case SHAPE_T::ARC:
|
2022-01-25 22:33:37 +00:00
|
|
|
formatArc( m_out, aNestLevel, aShape, false, aShape->GetStroke(), aShape->GetFillMode(),
|
2024-04-27 12:47:56 +00:00
|
|
|
aShape->GetFillColor(), false, aShape->m_Uuid );
|
2021-07-17 19:56:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SHAPE_T::CIRCLE:
|
2022-01-25 22:33:37 +00:00
|
|
|
formatCircle( m_out, aNestLevel, aShape, false, aShape->GetStroke(), aShape->GetFillMode(),
|
2024-04-27 12:47:56 +00:00
|
|
|
aShape->GetFillColor(), false, aShape->m_Uuid );
|
2021-07-17 19:56:18 +00:00
|
|
|
break;
|
|
|
|
|
2023-07-24 16:07:56 +00:00
|
|
|
case SHAPE_T::RECTANGLE:
|
2022-01-25 22:33:37 +00:00
|
|
|
formatRect( m_out, aNestLevel, aShape, false, aShape->GetStroke(), aShape->GetFillMode(),
|
2024-04-27 12:47:56 +00:00
|
|
|
aShape->GetFillColor(), false, aShape->m_Uuid );
|
2021-07-17 19:56:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SHAPE_T::BEZIER:
|
2022-01-25 22:33:37 +00:00
|
|
|
formatBezier( m_out, aNestLevel, aShape, false, aShape->GetStroke(), aShape->GetFillMode(),
|
2024-04-27 12:47:56 +00:00
|
|
|
aShape->GetFillColor(), false, aShape->m_Uuid );
|
2021-07-17 19:56:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SHAPE_T::POLY:
|
2022-01-25 22:33:37 +00:00
|
|
|
formatPoly( m_out, aNestLevel, aShape, false, aShape->GetStroke(), aShape->GetFillMode(),
|
2024-04-27 12:47:56 +00:00
|
|
|
aShape->GetFillColor(), false, aShape->m_Uuid );
|
2021-07-17 19:56:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
UNIMPLEMENTED_FOR( aShape->SHAPE_T_asString() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-04-25 14:24:46 +00:00
|
|
|
void SCH_IO_KICAD_SEXPR::saveRuleArea( SCH_RULE_AREA* aRuleArea, int aNestLevel )
|
|
|
|
{
|
|
|
|
wxCHECK_RET( aRuleArea != nullptr && m_out != nullptr, "" );
|
|
|
|
|
|
|
|
m_out->Print( aNestLevel, "(rule_area " );
|
|
|
|
saveShape( aRuleArea, aNestLevel + 1 );
|
|
|
|
m_out->Print( aNestLevel, ")\n" );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
void SCH_IO_KICAD_SEXPR::saveLine( SCH_LINE* aLine, int aNestLevel )
|
2020-02-13 13:39:52 +00:00
|
|
|
{
|
2020-03-16 13:04:50 +00:00
|
|
|
wxCHECK_RET( aLine != nullptr && m_out != nullptr, "" );
|
|
|
|
|
|
|
|
wxString lineType;
|
|
|
|
|
2020-10-17 13:16:09 +00:00
|
|
|
STROKE_PARAMS line_stroke = aLine->GetStroke();
|
|
|
|
|
2020-03-16 13:04:50 +00:00
|
|
|
switch( aLine->GetLayer() )
|
|
|
|
{
|
2020-10-17 13:16:09 +00:00
|
|
|
case LAYER_BUS: lineType = "bus"; break;
|
|
|
|
case LAYER_WIRE: lineType = "wire"; break;
|
2021-07-17 19:56:18 +00:00
|
|
|
case LAYER_NOTES: lineType = "polyline"; break;
|
|
|
|
default:
|
|
|
|
UNIMPLEMENTED_FOR( LayerName( aLine->GetLayer() ) );
|
2020-03-16 13:04:50 +00:00
|
|
|
}
|
|
|
|
|
2020-05-19 17:22:54 +00:00
|
|
|
m_out->Print( aNestLevel, "(%s (pts (xy %s %s) (xy %s %s))\n",
|
2020-03-16 13:04:50 +00:00
|
|
|
TO_UTF8( lineType ),
|
2022-11-22 19:35:27 +00:00
|
|
|
EDA_UNIT_UTILS::FormatInternalUnits( schIUScale,
|
|
|
|
aLine->GetStartPoint().x ).c_str(),
|
|
|
|
EDA_UNIT_UTILS::FormatInternalUnits( schIUScale,
|
|
|
|
aLine->GetStartPoint().y ).c_str(),
|
|
|
|
EDA_UNIT_UTILS::FormatInternalUnits( schIUScale,
|
|
|
|
aLine->GetEndPoint().x ).c_str(),
|
|
|
|
EDA_UNIT_UTILS::FormatInternalUnits( schIUScale,
|
|
|
|
aLine->GetEndPoint().y ).c_str() );
|
2020-03-16 13:04:50 +00:00
|
|
|
|
2022-09-16 04:38:10 +00:00
|
|
|
line_stroke.Format( m_out, schIUScale, aNestLevel + 1 );
|
2020-05-19 17:22:54 +00:00
|
|
|
m_out->Print( 0, "\n" );
|
2021-01-25 22:01:49 +00:00
|
|
|
|
2023-11-22 17:32:41 +00:00
|
|
|
KICAD_FORMAT::FormatUuid( m_out, aLine->m_Uuid );
|
2021-01-25 22:01:49 +00:00
|
|
|
|
2020-05-19 17:22:54 +00:00
|
|
|
m_out->Print( aNestLevel, ")\n" );
|
2020-02-13 13:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
void SCH_IO_KICAD_SEXPR::saveText( SCH_TEXT* aText, int aNestLevel )
|
2020-02-13 13:39:52 +00:00
|
|
|
{
|
2020-03-16 13:04:50 +00:00
|
|
|
wxCHECK_RET( aText != nullptr && m_out != nullptr, "" );
|
|
|
|
|
2022-03-27 16:48:55 +00:00
|
|
|
// Note: label is nullptr SCH_TEXT, but not for SCH_LABEL_XXX,
|
2022-01-24 13:40:39 +00:00
|
|
|
SCH_LABEL_BASE* label = dynamic_cast<SCH_LABEL_BASE*>( aText );
|
|
|
|
|
2020-03-16 13:04:50 +00:00
|
|
|
m_out->Print( aNestLevel, "(%s %s",
|
|
|
|
getTextTypeToken( aText->Type() ),
|
|
|
|
m_out->Quotew( aText->GetText() ).c_str() );
|
|
|
|
|
2023-04-09 11:14:21 +00:00
|
|
|
if( aText->Type() == SCH_TEXT_T )
|
|
|
|
{
|
2023-08-08 16:32:08 +00:00
|
|
|
m_out->Print( 0, " (exclude_from_sim %s)\n", aText->GetExcludedFromSim() ? "yes" : "no" );
|
2023-04-09 11:14:21 +00:00
|
|
|
}
|
|
|
|
else if( aText->Type() == SCH_DIRECTIVE_LABEL_T )
|
2021-10-12 20:05:37 +00:00
|
|
|
{
|
2022-01-24 13:40:39 +00:00
|
|
|
SCH_DIRECTIVE_LABEL* flag = static_cast<SCH_DIRECTIVE_LABEL*>( aText );
|
2021-10-12 20:05:37 +00:00
|
|
|
|
|
|
|
m_out->Print( 0, " (length %s)",
|
2022-11-22 19:35:27 +00:00
|
|
|
EDA_UNIT_UTILS::FormatInternalUnits( schIUScale,
|
|
|
|
flag->GetPinLength() ).c_str() );
|
2021-10-12 20:05:37 +00:00
|
|
|
}
|
|
|
|
|
2022-01-16 16:19:36 +00:00
|
|
|
EDA_ANGLE angle = aText->GetTextAngle();
|
|
|
|
|
2022-06-27 22:20:40 +00:00
|
|
|
if( label )
|
2021-10-12 20:05:37 +00:00
|
|
|
{
|
2023-09-07 16:09:53 +00:00
|
|
|
if( label->Type() == SCH_GLOBAL_LABEL_T
|
|
|
|
|| label->Type() == SCH_HIER_LABEL_T
|
|
|
|
|| label->Type() == SCH_DIRECTIVE_LABEL_T )
|
2022-06-27 22:20:40 +00:00
|
|
|
{
|
2022-03-27 16:48:55 +00:00
|
|
|
m_out->Print( 0, " (shape %s)", getSheetPinShapeToken( label->GetShape() ) );
|
2022-06-27 22:20:40 +00:00
|
|
|
}
|
2022-01-16 16:19:36 +00:00
|
|
|
|
|
|
|
// The angle of the text is always 0 or 90 degrees for readibility reasons,
|
|
|
|
// but the item itself can have more rotation (-90 and 180 deg)
|
2023-09-07 16:09:53 +00:00
|
|
|
switch( label->GetSpinStyle() )
|
2022-01-16 16:19:36 +00:00
|
|
|
{
|
|
|
|
default:
|
2023-09-07 16:09:53 +00:00
|
|
|
case SPIN_STYLE::LEFT: angle += ANGLE_180; break;
|
|
|
|
case SPIN_STYLE::UP: break;
|
|
|
|
case SPIN_STYLE::RIGHT: break;
|
|
|
|
case SPIN_STYLE::BOTTOM: angle += ANGLE_180; break;
|
2022-01-16 16:19:36 +00:00
|
|
|
}
|
2021-10-12 20:05:37 +00:00
|
|
|
}
|
2020-03-16 13:04:50 +00:00
|
|
|
|
|
|
|
if( aText->GetText().Length() < 50 )
|
|
|
|
{
|
|
|
|
m_out->Print( 0, " (at %s %s %s)",
|
2022-11-22 19:35:27 +00:00
|
|
|
EDA_UNIT_UTILS::FormatInternalUnits( schIUScale,
|
|
|
|
aText->GetPosition().x ).c_str(),
|
|
|
|
EDA_UNIT_UTILS::FormatInternalUnits( schIUScale,
|
|
|
|
aText->GetPosition().y ).c_str(),
|
2022-08-29 23:30:25 +00:00
|
|
|
EDA_UNIT_UTILS::FormatAngle( angle ).c_str() );
|
2020-03-16 13:04:50 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_out->Print( 0, "\n" );
|
|
|
|
m_out->Print( aNestLevel + 1, "(at %s %s %s)",
|
2022-11-22 19:35:27 +00:00
|
|
|
EDA_UNIT_UTILS::FormatInternalUnits( schIUScale,
|
|
|
|
aText->GetPosition().x ).c_str(),
|
|
|
|
EDA_UNIT_UTILS::FormatInternalUnits( schIUScale,
|
|
|
|
aText->GetPosition().y ).c_str(),
|
2022-08-29 23:30:25 +00:00
|
|
|
EDA_UNIT_UTILS::FormatAngle( angle ).c_str() );
|
2020-03-16 13:04:50 +00:00
|
|
|
}
|
|
|
|
|
2021-04-02 16:08:13 +00:00
|
|
|
if( aText->GetFieldsAutoplaced() != FIELDS_AUTOPLACED_NO )
|
2023-11-24 19:29:53 +00:00
|
|
|
m_out->Print( 0, " (fields_autoplaced yes)" );
|
2021-04-02 16:08:13 +00:00
|
|
|
|
2021-01-25 22:01:49 +00:00
|
|
|
m_out->Print( 0, "\n" );
|
2022-01-25 22:33:37 +00:00
|
|
|
aText->EDA_TEXT::Format( m_out, aNestLevel, 0 );
|
2021-01-25 22:01:49 +00:00
|
|
|
|
2023-11-22 17:32:41 +00:00
|
|
|
KICAD_FORMAT::FormatUuid( m_out, aText->m_Uuid );
|
2021-01-25 22:01:49 +00:00
|
|
|
|
2021-10-12 20:05:37 +00:00
|
|
|
if( label )
|
2020-11-17 16:02:47 +00:00
|
|
|
{
|
2021-10-12 20:05:37 +00:00
|
|
|
for( SCH_FIELD& field : label->GetFields() )
|
|
|
|
saveField( &field, aNestLevel + 1 );
|
2020-11-17 16:02:47 +00:00
|
|
|
}
|
|
|
|
|
2020-05-19 17:22:54 +00:00
|
|
|
m_out->Print( aNestLevel, ")\n" ); // Closes text token.
|
2020-02-13 13:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
void SCH_IO_KICAD_SEXPR::saveTextBox( SCH_TEXTBOX* aTextBox, int aNestLevel )
|
2022-01-25 22:33:37 +00:00
|
|
|
{
|
|
|
|
wxCHECK_RET( aTextBox != nullptr && m_out != nullptr, "" );
|
|
|
|
|
2023-11-25 12:38:24 +00:00
|
|
|
m_out->Print( aNestLevel, "(%s %s\n",
|
|
|
|
aTextBox->Type() == SCH_TABLECELL_T ? "table_cell" : "text_box",
|
2022-01-25 22:33:37 +00:00
|
|
|
m_out->Quotew( aTextBox->GetText() ).c_str() );
|
|
|
|
|
2022-03-28 09:32:07 +00:00
|
|
|
VECTOR2I pos = aTextBox->GetStart();
|
|
|
|
VECTOR2I size = aTextBox->GetEnd() - pos;
|
|
|
|
|
2024-01-20 17:05:21 +00:00
|
|
|
m_out->Print( aNestLevel + 1, "(exclude_from_sim %s) (at %s %s %s) (size %s %s) (margins %s %s %s %s)",
|
2023-08-08 16:32:08 +00:00
|
|
|
aTextBox->GetExcludedFromSim() ? "yes" : "no",
|
2022-09-16 04:38:10 +00:00
|
|
|
EDA_UNIT_UTILS::FormatInternalUnits( schIUScale, pos.x ).c_str(),
|
|
|
|
EDA_UNIT_UTILS::FormatInternalUnits( schIUScale, pos.y ).c_str(),
|
2022-08-29 23:30:25 +00:00
|
|
|
EDA_UNIT_UTILS::FormatAngle( aTextBox->GetTextAngle() ).c_str(),
|
2022-09-16 04:38:10 +00:00
|
|
|
EDA_UNIT_UTILS::FormatInternalUnits( schIUScale, size.x ).c_str(),
|
2024-01-20 17:05:21 +00:00
|
|
|
EDA_UNIT_UTILS::FormatInternalUnits( schIUScale, size.y ).c_str(),
|
|
|
|
EDA_UNIT_UTILS::FormatInternalUnits( schIUScale, aTextBox->GetMarginLeft() ).c_str(),
|
|
|
|
EDA_UNIT_UTILS::FormatInternalUnits( schIUScale, aTextBox->GetMarginTop() ).c_str(),
|
|
|
|
EDA_UNIT_UTILS::FormatInternalUnits( schIUScale, aTextBox->GetMarginRight() ).c_str(),
|
|
|
|
EDA_UNIT_UTILS::FormatInternalUnits( schIUScale, aTextBox->GetMarginBottom() ).c_str() );
|
2022-01-25 22:33:37 +00:00
|
|
|
|
2023-11-25 12:38:24 +00:00
|
|
|
if( SCH_TABLECELL* cell = dynamic_cast<SCH_TABLECELL*>( aTextBox ) )
|
|
|
|
m_out->Print( 0, " (span %d %d)", cell->GetColSpan(), cell->GetRowSpan() );
|
|
|
|
|
2022-01-25 22:33:37 +00:00
|
|
|
m_out->Print( 0, "\n" );
|
2023-11-25 12:38:24 +00:00
|
|
|
|
|
|
|
if( aTextBox->Type() != SCH_TABLECELL_T )
|
|
|
|
{
|
|
|
|
aTextBox->GetStroke().Format( m_out, schIUScale, aNestLevel + 1 );
|
|
|
|
m_out->Print( 0, "\n" );
|
|
|
|
}
|
|
|
|
|
2022-01-25 22:33:37 +00:00
|
|
|
formatFill( m_out, aNestLevel + 1, aTextBox->GetFillMode(), aTextBox->GetFillColor() );
|
|
|
|
m_out->Print( 0, "\n" );
|
|
|
|
|
|
|
|
aTextBox->EDA_TEXT::Format( m_out, aNestLevel, 0 );
|
|
|
|
|
|
|
|
if( aTextBox->m_Uuid != niluuid )
|
2023-11-22 17:32:41 +00:00
|
|
|
KICAD_FORMAT::FormatUuid( m_out, aTextBox->m_Uuid );
|
2022-01-25 22:33:37 +00:00
|
|
|
|
|
|
|
m_out->Print( aNestLevel, ")\n" );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-11-25 12:38:24 +00:00
|
|
|
void SCH_IO_KICAD_SEXPR::saveTable( SCH_TABLE* aTable, int aNestLevel )
|
|
|
|
{
|
2024-03-28 14:31:20 +00:00
|
|
|
if( aTable->GetFlags() & SKIP_STRUCT )
|
|
|
|
{
|
|
|
|
aTable = static_cast<SCH_TABLE*>( aTable->Clone() );
|
|
|
|
|
|
|
|
int minCol = aTable->GetColCount();
|
|
|
|
int maxCol = -1;
|
|
|
|
int minRow = aTable->GetRowCount();
|
|
|
|
int maxRow = -1;
|
|
|
|
|
|
|
|
for( int row = 0; row < aTable->GetRowCount(); ++row )
|
|
|
|
{
|
|
|
|
for( int col = 0; col < aTable->GetColCount(); ++col )
|
|
|
|
{
|
|
|
|
SCH_TABLECELL* cell = aTable->GetCell( row, col );
|
|
|
|
|
|
|
|
if( cell->IsSelected() )
|
|
|
|
{
|
|
|
|
minRow = std::min( minRow, row );
|
|
|
|
maxRow = std::max( maxRow, row );
|
|
|
|
minCol = std::min( minCol, col );
|
|
|
|
maxCol = std::max( maxCol, col );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cell->SetFlags( STRUCT_DELETED );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wxCHECK_MSG( maxCol >= minCol && maxRow >= minRow, /*void*/, wxT( "No selected cells!" ) );
|
|
|
|
|
|
|
|
int destRow = 0;
|
|
|
|
|
|
|
|
for( int row = minRow; row <= maxRow; row++ )
|
|
|
|
aTable->SetRowHeight( destRow++, aTable->GetRowHeight( row ) );
|
|
|
|
|
|
|
|
int destCol = 0;
|
|
|
|
|
|
|
|
for( int col = minCol; col <= maxCol; col++ )
|
|
|
|
aTable->SetColWidth( destCol++, aTable->GetColWidth( col ) );
|
|
|
|
|
|
|
|
aTable->DeleteMarkedCells();
|
|
|
|
aTable->SetColCount( ( maxCol - minCol ) + 1 );
|
|
|
|
}
|
|
|
|
|
2023-11-25 12:38:24 +00:00
|
|
|
wxCHECK_RET( aTable != nullptr && m_out != nullptr, "" );
|
|
|
|
|
|
|
|
m_out->Print( aNestLevel, "(table (column_count %d)\n",
|
|
|
|
aTable->GetColCount() );
|
|
|
|
|
|
|
|
m_out->Print( aNestLevel + 1, "(border (external %s) (header %s)",
|
|
|
|
aTable->StrokeExternal() ? "yes" : "no",
|
|
|
|
aTable->StrokeHeader() ? "yes" : "no" );
|
|
|
|
|
|
|
|
if( aTable->StrokeExternal() || aTable->StrokeHeader() )
|
|
|
|
{
|
|
|
|
m_out->Print( 0, " " );
|
|
|
|
aTable->GetBorderStroke().Format( m_out, schIUScale, 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
m_out->Print( 0, ")\n" );
|
|
|
|
|
|
|
|
m_out->Print( aNestLevel + 1, "(separators (rows %s) (cols %s)",
|
|
|
|
aTable->StrokeRows() ? "yes" : "no",
|
|
|
|
aTable->StrokeColumns() ? "yes" : "no" );
|
|
|
|
|
|
|
|
if( aTable->StrokeRows() || aTable->StrokeColumns() )
|
|
|
|
{
|
|
|
|
m_out->Print( 0, " " );
|
|
|
|
aTable->GetSeparatorsStroke().Format( m_out, schIUScale, 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
m_out->Print( 0, ")\n" ); // Close `separators` token.
|
|
|
|
|
|
|
|
m_out->Print( aNestLevel + 1, "(column_widths" );
|
|
|
|
|
|
|
|
for( int col = 0; col < aTable->GetColCount(); ++col )
|
|
|
|
{
|
|
|
|
m_out->Print( 0, " %s",
|
|
|
|
EDA_UNIT_UTILS::FormatInternalUnits( schIUScale,
|
|
|
|
aTable->GetColWidth( col ) ).c_str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
m_out->Print( 0, ")\n" );
|
|
|
|
|
|
|
|
m_out->Print( aNestLevel + 1, "(row_heights" );
|
|
|
|
|
|
|
|
for( int row = 0; row < aTable->GetRowCount(); ++row )
|
|
|
|
{
|
|
|
|
m_out->Print( 0, " %s",
|
|
|
|
EDA_UNIT_UTILS::FormatInternalUnits( schIUScale,
|
|
|
|
aTable->GetRowHeight( row ) ).c_str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
m_out->Print( 0, ")\n" );
|
|
|
|
|
|
|
|
m_out->Print( aNestLevel + 1, "(cells\n" );
|
|
|
|
|
|
|
|
for( SCH_TABLECELL* cell : aTable->GetCells() )
|
|
|
|
saveTextBox( cell, aNestLevel + 2 );
|
|
|
|
|
|
|
|
m_out->Print( aNestLevel + 1, ")\n" ); // Close `cells` token.
|
|
|
|
m_out->Print( aNestLevel, ")\n" ); // Close `table` token.
|
2024-03-28 14:31:20 +00:00
|
|
|
|
|
|
|
if( aTable->GetFlags() & SKIP_STRUCT )
|
|
|
|
delete aTable;
|
2023-11-25 12:38:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
void SCH_IO_KICAD_SEXPR::saveBusAlias( std::shared_ptr<BUS_ALIAS> aAlias, int aNestLevel )
|
2020-02-13 13:39:52 +00:00
|
|
|
{
|
2021-07-16 20:13:26 +00:00
|
|
|
wxCHECK_RET( aAlias != nullptr, "BUS_ALIAS* is NULL" );
|
2020-02-13 13:39:52 +00:00
|
|
|
|
2022-10-02 16:37:02 +00:00
|
|
|
wxString members;
|
|
|
|
|
|
|
|
for( const wxString& member : aAlias->Members() )
|
|
|
|
{
|
|
|
|
if( !members.IsEmpty() )
|
|
|
|
members += wxS( " " );
|
|
|
|
|
|
|
|
members += m_out->Quotew( member );
|
|
|
|
}
|
|
|
|
|
2020-05-06 13:27:01 +00:00
|
|
|
m_out->Print( aNestLevel, "(bus_alias %s (members %s))\n",
|
|
|
|
m_out->Quotew( aAlias->GetName() ).c_str(),
|
2022-10-02 16:37:02 +00:00
|
|
|
TO_UTF8( members ) );
|
2020-02-13 13:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
void SCH_IO_KICAD_SEXPR::saveInstances( const std::vector<SCH_SHEET_INSTANCE>& aInstances,
|
2023-12-28 23:25:47 +00:00
|
|
|
int aNestLevel )
|
2021-04-12 19:08:02 +00:00
|
|
|
{
|
2022-12-09 15:02:16 +00:00
|
|
|
if( aInstances.size() )
|
2021-04-12 19:08:02 +00:00
|
|
|
{
|
|
|
|
m_out->Print( 0, "\n" );
|
|
|
|
m_out->Print( aNestLevel, "(sheet_instances\n" );
|
|
|
|
|
2022-12-09 15:02:16 +00:00
|
|
|
for( const SCH_SHEET_INSTANCE& instance : aInstances )
|
2021-04-12 19:08:02 +00:00
|
|
|
{
|
|
|
|
wxString path = instance.m_Path.AsString();
|
|
|
|
|
|
|
|
if( path.IsEmpty() )
|
|
|
|
path = wxT( "/" ); // Root path
|
|
|
|
|
|
|
|
m_out->Print( aNestLevel + 1, "(path %s (page %s))\n",
|
|
|
|
m_out->Quotew( path ).c_str(),
|
|
|
|
m_out->Quotew( instance.m_PageNumber ).c_str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
m_out->Print( aNestLevel, ")\n" ); // Close sheet instances token.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
void SCH_IO_KICAD_SEXPR::cacheLib( const wxString& aLibraryFileName,
|
2023-12-28 23:25:47 +00:00
|
|
|
const STRING_UTF8_MAP* aProperties )
|
2020-02-13 13:39:52 +00:00
|
|
|
{
|
|
|
|
if( !m_cache || !m_cache->IsFile( aLibraryFileName ) || m_cache->IsFileChanged() )
|
|
|
|
{
|
|
|
|
// a spectacular episode in memory management:
|
|
|
|
delete m_cache;
|
2023-12-24 00:31:24 +00:00
|
|
|
m_cache = new SCH_IO_KICAD_SEXPR_LIB_CACHE( aLibraryFileName );
|
2020-02-13 13:39:52 +00:00
|
|
|
|
2021-03-26 17:30:47 +00:00
|
|
|
if( !isBuffering( aProperties ) )
|
2020-02-13 13:39:52 +00:00
|
|
|
m_cache->Load();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
bool SCH_IO_KICAD_SEXPR::isBuffering( const STRING_UTF8_MAP* aProperties )
|
2020-02-13 13:39:52 +00:00
|
|
|
{
|
2023-12-24 00:31:24 +00:00
|
|
|
return ( aProperties && aProperties->Exists( SCH_IO_KICAD_SEXPR::PropBuffering ) );
|
2020-02-13 13:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
int SCH_IO_KICAD_SEXPR::GetModifyHash() const
|
2020-02-13 13:39:52 +00:00
|
|
|
{
|
|
|
|
if( m_cache )
|
|
|
|
return m_cache->GetModifyHash();
|
|
|
|
|
|
|
|
// If the cache hasn't been loaded, it hasn't been modified.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
void SCH_IO_KICAD_SEXPR::EnumerateSymbolLib( wxArrayString& aSymbolNameList,
|
2023-12-28 23:25:47 +00:00
|
|
|
const wxString& aLibraryPath,
|
|
|
|
const STRING_UTF8_MAP* aProperties )
|
2020-02-13 13:39:52 +00:00
|
|
|
{
|
|
|
|
LOCALE_IO toggle; // toggles on, then off, the C locale.
|
|
|
|
|
|
|
|
bool powerSymbolsOnly = ( aProperties &&
|
|
|
|
aProperties->find( SYMBOL_LIB_TABLE::PropPowerSymsOnly ) != aProperties->end() );
|
2021-03-26 17:30:47 +00:00
|
|
|
|
|
|
|
cacheLib( aLibraryPath, aProperties );
|
2020-02-13 13:39:52 +00:00
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
const LIB_SYMBOL_MAP& symbols = m_cache->m_symbols;
|
2020-02-13 13:39:52 +00:00
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
for( LIB_SYMBOL_MAP::const_iterator it = symbols.begin(); it != symbols.end(); ++it )
|
2020-02-13 13:39:52 +00:00
|
|
|
{
|
|
|
|
if( !powerSymbolsOnly || it->second->IsPower() )
|
|
|
|
aSymbolNameList.Add( it->first );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
void SCH_IO_KICAD_SEXPR::EnumerateSymbolLib( std::vector<LIB_SYMBOL*>& aSymbolList,
|
2023-12-28 23:25:47 +00:00
|
|
|
const wxString& aLibraryPath,
|
|
|
|
const STRING_UTF8_MAP* aProperties )
|
2020-02-13 13:39:52 +00:00
|
|
|
{
|
|
|
|
LOCALE_IO toggle; // toggles on, then off, the C locale.
|
|
|
|
|
|
|
|
bool powerSymbolsOnly = ( aProperties &&
|
|
|
|
aProperties->find( SYMBOL_LIB_TABLE::PropPowerSymsOnly ) != aProperties->end() );
|
2021-03-26 17:30:47 +00:00
|
|
|
|
|
|
|
cacheLib( aLibraryPath, aProperties );
|
2020-02-13 13:39:52 +00:00
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
const LIB_SYMBOL_MAP& symbols = m_cache->m_symbols;
|
2020-02-13 13:39:52 +00:00
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
for( LIB_SYMBOL_MAP::const_iterator it = symbols.begin(); it != symbols.end(); ++it )
|
2020-02-13 13:39:52 +00:00
|
|
|
{
|
|
|
|
if( !powerSymbolsOnly || it->second->IsPower() )
|
|
|
|
aSymbolList.push_back( it->second );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-28 23:25:47 +00:00
|
|
|
LIB_SYMBOL* SCH_IO_KICAD_SEXPR::LoadSymbol( const wxString& aLibraryPath,
|
|
|
|
const wxString& aSymbolName,
|
|
|
|
const STRING_UTF8_MAP* aProperties )
|
2020-02-13 13:39:52 +00:00
|
|
|
{
|
|
|
|
LOCALE_IO toggle; // toggles on, then off, the C locale.
|
|
|
|
|
2021-03-26 17:30:47 +00:00
|
|
|
cacheLib( aLibraryPath, aProperties );
|
2020-02-13 13:39:52 +00:00
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
LIB_SYMBOL_MAP::const_iterator it = m_cache->m_symbols.find( aSymbolName );
|
2020-02-13 13:39:52 +00:00
|
|
|
|
2023-04-15 16:46:32 +00:00
|
|
|
// We no longer escape '/' in symbol names, but we used to.
|
|
|
|
if( it == m_cache->m_symbols.end() && aSymbolName.Contains( '/' ) )
|
|
|
|
it = m_cache->m_symbols.find( EscapeString( aSymbolName, CTX_LEGACY_LIBID ) );
|
|
|
|
|
2023-08-29 17:29:39 +00:00
|
|
|
if( it == m_cache->m_symbols.end() && aSymbolName.Contains( wxT( "{slash}" ) ) )
|
|
|
|
{
|
|
|
|
wxString unescaped = aSymbolName;
|
|
|
|
unescaped.Replace( wxT( "{slash}" ), wxT( "/" ) );
|
|
|
|
it = m_cache->m_symbols.find( unescaped );
|
|
|
|
}
|
|
|
|
|
2020-02-13 13:39:52 +00:00
|
|
|
if( it == m_cache->m_symbols.end() )
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
void SCH_IO_KICAD_SEXPR::SaveSymbol( const wxString& aLibraryPath, const LIB_SYMBOL* aSymbol,
|
2023-12-28 23:25:47 +00:00
|
|
|
const STRING_UTF8_MAP* aProperties )
|
2020-02-13 13:39:52 +00:00
|
|
|
{
|
2021-02-15 17:08:48 +00:00
|
|
|
LOCALE_IO toggle; // toggles on, then off, the C locale.
|
2020-02-13 13:39:52 +00:00
|
|
|
|
2021-03-26 17:30:47 +00:00
|
|
|
cacheLib( aLibraryPath, aProperties );
|
2020-02-13 13:39:52 +00:00
|
|
|
|
|
|
|
m_cache->AddSymbol( aSymbol );
|
|
|
|
|
|
|
|
if( !isBuffering( aProperties ) )
|
|
|
|
m_cache->Save();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
void SCH_IO_KICAD_SEXPR::DeleteSymbol( const wxString& aLibraryPath, const wxString& aSymbolName,
|
2023-12-28 23:25:47 +00:00
|
|
|
const STRING_UTF8_MAP* aProperties )
|
2020-02-13 13:39:52 +00:00
|
|
|
{
|
2021-02-15 17:08:48 +00:00
|
|
|
LOCALE_IO toggle; // toggles on, then off, the C locale.
|
2020-02-13 13:39:52 +00:00
|
|
|
|
2021-03-26 17:30:47 +00:00
|
|
|
cacheLib( aLibraryPath, aProperties );
|
2020-02-13 13:39:52 +00:00
|
|
|
|
|
|
|
m_cache->DeleteSymbol( aSymbolName );
|
|
|
|
|
|
|
|
if( !isBuffering( aProperties ) )
|
|
|
|
m_cache->Save();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-27 00:25:41 +00:00
|
|
|
void SCH_IO_KICAD_SEXPR::CreateLibrary( const wxString& aLibraryPath,
|
2022-11-06 16:51:52 +00:00
|
|
|
const STRING_UTF8_MAP* aProperties )
|
2020-02-13 13:39:52 +00:00
|
|
|
{
|
|
|
|
if( wxFileExists( aLibraryPath ) )
|
|
|
|
{
|
2021-06-28 23:44:07 +00:00
|
|
|
THROW_IO_ERROR( wxString::Format( _( "Symbol library '%s' already exists." ),
|
|
|
|
aLibraryPath.GetData() ) );
|
2020-02-13 13:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LOCALE_IO toggle;
|
|
|
|
|
|
|
|
delete m_cache;
|
2023-12-24 00:31:24 +00:00
|
|
|
m_cache = new SCH_IO_KICAD_SEXPR_LIB_CACHE( aLibraryPath );
|
2020-02-13 13:39:52 +00:00
|
|
|
m_cache->SetModified();
|
|
|
|
m_cache->Save();
|
|
|
|
m_cache->Load(); // update m_writable and m_mod_time
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-27 00:25:41 +00:00
|
|
|
bool SCH_IO_KICAD_SEXPR::DeleteLibrary( const wxString& aLibraryPath,
|
2022-11-06 16:51:52 +00:00
|
|
|
const STRING_UTF8_MAP* aProperties )
|
2020-02-13 13:39:52 +00:00
|
|
|
{
|
|
|
|
wxFileName fn = aLibraryPath;
|
|
|
|
|
|
|
|
if( !fn.FileExists() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Some of the more elaborate wxRemoveFile() crap puts up its own wxLog dialog
|
|
|
|
// we don't want that. we want bare metal portability with no UI here.
|
|
|
|
if( wxRemove( aLibraryPath ) )
|
|
|
|
{
|
2021-06-28 23:44:07 +00:00
|
|
|
THROW_IO_ERROR( wxString::Format( _( "Symbol library '%s' cannot be deleted." ),
|
2020-02-13 13:39:52 +00:00
|
|
|
aLibraryPath.GetData() ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( m_cache && m_cache->IsFile( aLibraryPath ) )
|
|
|
|
{
|
|
|
|
delete m_cache;
|
2021-04-22 21:20:34 +00:00
|
|
|
m_cache = nullptr;
|
2020-02-13 13:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-28 23:25:47 +00:00
|
|
|
void SCH_IO_KICAD_SEXPR::SaveLibrary( const wxString& aLibraryPath,
|
|
|
|
const STRING_UTF8_MAP* aProperties )
|
2020-02-13 13:39:52 +00:00
|
|
|
{
|
|
|
|
if( !m_cache )
|
2023-12-24 00:31:24 +00:00
|
|
|
m_cache = new SCH_IO_KICAD_SEXPR_LIB_CACHE( aLibraryPath );
|
2020-02-13 13:39:52 +00:00
|
|
|
|
|
|
|
wxString oldFileName = m_cache->GetFileName();
|
|
|
|
|
|
|
|
if( !m_cache->IsFile( aLibraryPath ) )
|
|
|
|
{
|
|
|
|
m_cache->SetFileName( aLibraryPath );
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is a forced save.
|
|
|
|
m_cache->SetModified();
|
|
|
|
m_cache->Save();
|
|
|
|
m_cache->SetFileName( oldFileName );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-27 00:25:41 +00:00
|
|
|
bool SCH_IO_KICAD_SEXPR::IsLibraryWritable( const wxString& aLibraryPath )
|
2020-02-13 13:39:52 +00:00
|
|
|
{
|
2020-05-22 14:12:44 +00:00
|
|
|
wxFileName fn( aLibraryPath );
|
|
|
|
|
|
|
|
return ( fn.FileExists() && fn.IsFileWritable() ) || fn.IsDirWritable();
|
2020-02-13 13:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
void SCH_IO_KICAD_SEXPR::GetAvailableSymbolFields( std::vector<wxString>& aNames )
|
2022-08-28 23:02:12 +00:00
|
|
|
{
|
|
|
|
if( !m_cache )
|
|
|
|
return;
|
|
|
|
|
|
|
|
const LIB_SYMBOL_MAP& symbols = m_cache->m_symbols;
|
|
|
|
|
|
|
|
std::set<wxString> fieldNames;
|
|
|
|
|
|
|
|
for( LIB_SYMBOL_MAP::const_iterator it = symbols.begin(); it != symbols.end(); ++it )
|
|
|
|
{
|
2024-04-12 21:00:41 +00:00
|
|
|
std::vector<SCH_FIELD*> fields;
|
2022-08-28 23:02:12 +00:00
|
|
|
it->second->GetFields( fields );
|
|
|
|
|
2024-04-12 21:00:41 +00:00
|
|
|
for( SCH_FIELD* field : fields )
|
2022-08-28 23:02:12 +00:00
|
|
|
{
|
|
|
|
if( field->IsMandatory() )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// TODO(JE): enable configurability of this outside database libraries?
|
|
|
|
// if( field->ShowInChooser() )
|
|
|
|
fieldNames.insert( field->GetName() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::copy( fieldNames.begin(), fieldNames.end(), std::back_inserter( aNames ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
void SCH_IO_KICAD_SEXPR::GetDefaultSymbolFields( std::vector<wxString>& aNames )
|
2022-08-28 23:02:12 +00:00
|
|
|
{
|
|
|
|
GetAvailableSymbolFields( aNames );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
std::vector<LIB_SYMBOL*> SCH_IO_KICAD_SEXPR::ParseLibSymbols( std::string& aSymbolText,
|
2023-12-28 23:25:47 +00:00
|
|
|
std::string aSource,
|
|
|
|
int aFileVersion )
|
2020-02-13 13:39:52 +00:00
|
|
|
{
|
2023-06-12 22:13:23 +00:00
|
|
|
LOCALE_IO toggle; // toggles on, then off, the C locale.
|
|
|
|
LIB_SYMBOL* newSymbol = nullptr;
|
2021-06-10 18:51:46 +00:00
|
|
|
LIB_SYMBOL_MAP map;
|
2020-05-13 21:58:30 +00:00
|
|
|
|
2023-06-12 22:13:23 +00:00
|
|
|
std::vector<LIB_SYMBOL*> newSymbols;
|
2024-01-26 13:59:07 +00:00
|
|
|
std::unique_ptr<STRING_LINE_READER> reader = std::make_unique<STRING_LINE_READER>( aSymbolText,
|
|
|
|
aSource );
|
2023-06-12 22:13:23 +00:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2023-12-24 00:31:24 +00:00
|
|
|
SCH_IO_KICAD_SEXPR_PARSER parser( reader.get() );
|
2023-06-12 22:13:23 +00:00
|
|
|
|
|
|
|
newSymbol = parser.ParseSymbol( map, aFileVersion );
|
|
|
|
|
|
|
|
if( newSymbol )
|
|
|
|
newSymbols.emplace_back( newSymbol );
|
|
|
|
|
|
|
|
reader.reset( new STRING_LINE_READER( *reader ) );
|
|
|
|
}
|
|
|
|
while( newSymbol );
|
2020-05-13 21:58:30 +00:00
|
|
|
|
2023-06-12 22:13:23 +00:00
|
|
|
return newSymbols;
|
2020-02-13 13:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
void SCH_IO_KICAD_SEXPR::FormatLibSymbol( LIB_SYMBOL* symbol, OUTPUTFORMATTER & formatter )
|
2020-02-13 13:39:52 +00:00
|
|
|
{
|
2020-05-18 19:47:19 +00:00
|
|
|
|
2020-08-18 18:12:58 +00:00
|
|
|
LOCALE_IO toggle; // toggles on, then off, the C locale.
|
2023-12-24 00:31:24 +00:00
|
|
|
SCH_IO_KICAD_SEXPR_LIB_CACHE::SaveSymbol( symbol, formatter );
|
2020-02-13 13:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
const char* SCH_IO_KICAD_SEXPR::PropBuffering = "buffering";
|