2020-09-08 19:51:22 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2020 Roberto Fernandez Bautista <roberto.fer.bau@gmail.com>
|
2024-01-26 13:59:07 +00:00
|
|
|
* Copyright (C) 2020-2024 KiCad Developers, see AUTHORS.txt for contributors.
|
2020-09-08 19:51:22 +00:00
|
|
|
*
|
|
|
|
* 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 3 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-19 17:39:26 +00:00
|
|
|
#include <sch_io/cadstar/cadstar_sch_archive_loader.h>
|
2023-12-24 00:31:24 +00:00
|
|
|
#include <sch_io/cadstar/sch_io_cadstar_archive.h>
|
2023-12-19 17:39:26 +00:00
|
|
|
#include <io/cadstar/cadstar_parts_lib_parser.h>
|
2020-09-08 19:51:22 +00:00
|
|
|
|
2023-02-26 22:37:05 +00:00
|
|
|
#include <lib_symbol.h>
|
|
|
|
#include <progress_reporter.h>
|
2023-09-28 03:04:53 +00:00
|
|
|
#include <project_sch.h>
|
2022-11-06 16:51:52 +00:00
|
|
|
#include <string_utf8_map.h>
|
2020-09-08 19:51:22 +00:00
|
|
|
#include <sch_screen.h>
|
|
|
|
#include <sch_sheet.h>
|
|
|
|
#include <schematic.h>
|
2023-12-24 00:31:24 +00:00
|
|
|
#include <sch_io/kicad_sexpr/sch_io_kicad_sexpr.h>
|
2020-09-20 21:08:46 +00:00
|
|
|
#include <wildcards_and_files_ext.h>
|
2023-08-28 09:28:55 +00:00
|
|
|
#include <wx_filename.h>
|
2023-10-15 02:56:38 +00:00
|
|
|
#include <wx/dir.h>
|
2023-11-08 20:37:38 +00:00
|
|
|
#include <wx/wfstream.h>
|
|
|
|
#include <wx/txtstrm.h>
|
2020-09-08 19:51:22 +00:00
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
bool SCH_IO_CADSTAR_ARCHIVE::CanReadLibrary( const wxString& aFileName ) const
|
2023-11-08 20:37:38 +00:00
|
|
|
{
|
2023-12-19 17:39:26 +00:00
|
|
|
if( !SCH_IO::CanReadLibrary( aFileName ) )
|
2023-11-08 20:37:38 +00:00
|
|
|
return false;
|
|
|
|
|
2023-11-08 22:23:22 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
CADSTAR_PARTS_LIB_PARSER p;
|
2023-11-08 23:12:10 +00:00
|
|
|
return p.CheckFileHeader( aFileName.utf8_string() );
|
2023-11-08 22:23:22 +00:00
|
|
|
}
|
|
|
|
catch( const std::runtime_error& )
|
|
|
|
{
|
|
|
|
}
|
2023-11-08 20:37:38 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
int SCH_IO_CADSTAR_ARCHIVE::GetModifyHash() const
|
2020-09-08 19:51:22 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
SCH_SHEET* SCH_IO_CADSTAR_ARCHIVE::LoadSchematicFile( const wxString& aFileName,
|
2024-01-26 13:59:07 +00:00
|
|
|
SCHEMATIC* aSchematic,
|
|
|
|
SCH_SHEET* aAppendToMe,
|
|
|
|
const STRING_UTF8_MAP* aProperties )
|
2020-09-08 19:51:22 +00:00
|
|
|
{
|
2023-02-26 16:27:25 +00:00
|
|
|
wxCHECK( !aFileName.IsEmpty() && aSchematic, nullptr );
|
2020-09-08 19:51:22 +00:00
|
|
|
|
2020-09-20 21:08:46 +00:00
|
|
|
SCH_SHEET* rootSheet = nullptr;
|
|
|
|
|
2020-09-08 19:51:22 +00:00
|
|
|
|
|
|
|
if( aAppendToMe )
|
|
|
|
{
|
|
|
|
wxCHECK_MSG( aSchematic->IsValid(), nullptr, "Can't append to a schematic with no root!" );
|
2020-09-20 21:08:46 +00:00
|
|
|
rootSheet = &aSchematic->Root();
|
2020-09-08 19:51:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-09-20 21:08:46 +00:00
|
|
|
rootSheet = new SCH_SHEET( aSchematic );
|
|
|
|
rootSheet->SetFileName( aFileName );
|
2021-12-09 00:14:04 +00:00
|
|
|
aSchematic->SetRoot( rootSheet );
|
2020-09-08 19:51:22 +00:00
|
|
|
}
|
|
|
|
|
2020-09-20 21:08:46 +00:00
|
|
|
if( !rootSheet->GetScreen() )
|
2020-09-08 19:51:22 +00:00
|
|
|
{
|
2020-09-20 21:08:46 +00:00
|
|
|
SCH_SCREEN* screen = new SCH_SCREEN( aSchematic );
|
2020-09-08 19:51:22 +00:00
|
|
|
screen->SetFileName( aFileName );
|
2020-09-20 21:08:46 +00:00
|
|
|
rootSheet->SetScreen( screen );
|
|
|
|
}
|
|
|
|
|
2023-02-26 16:27:25 +00:00
|
|
|
CADSTAR_SCH_ARCHIVE_LOADER csaLoader( aFileName, m_reporter, m_progressReporter );
|
|
|
|
csaLoader.Load( aSchematic, rootSheet );
|
|
|
|
|
|
|
|
// SAVE SYMBOLS TO PROJECT LIBRARY:
|
2023-09-28 03:04:53 +00:00
|
|
|
SYMBOL_LIB_TABLE* libTable = PROJECT_SCH::SchSymbolLibTable( &aSchematic->Prj() );
|
2020-09-20 21:08:46 +00:00
|
|
|
|
2023-02-26 16:27:25 +00:00
|
|
|
wxCHECK_MSG( libTable, nullptr, "Could not load symbol lib table." );
|
2020-09-20 21:08:46 +00:00
|
|
|
|
2024-05-09 14:23:28 +00:00
|
|
|
wxFileName fn = aSchematic->Prj().GetProjectFullName();
|
|
|
|
wxString libName = CADSTAR_SCH_ARCHIVE_LOADER::CreateLibName( fn, nullptr );
|
2020-09-20 21:08:46 +00:00
|
|
|
|
2023-02-26 16:27:25 +00:00
|
|
|
wxFileName libFileName( aSchematic->Prj().GetProjectPath(), libName,
|
2023-12-28 02:10:01 +00:00
|
|
|
FILEEXT::KiCadSymbolLibFileExtension );
|
2020-09-20 21:08:46 +00:00
|
|
|
|
2023-12-27 20:39:29 +00:00
|
|
|
IO_RELEASER<SCH_IO> sch_plugin( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_KICAD ) );
|
2020-09-20 21:08:46 +00:00
|
|
|
|
|
|
|
if( !libTable->HasLibrary( libName ) )
|
|
|
|
{
|
|
|
|
// Create a new empty symbol library.
|
2023-12-27 00:25:41 +00:00
|
|
|
sch_plugin->CreateLibrary( libFileName.GetFullPath() );
|
2020-09-20 21:08:46 +00:00
|
|
|
wxString libTableUri = "${KIPRJMOD}/" + libFileName.GetFullName();
|
|
|
|
|
|
|
|
// Add the new library to the project symbol library table.
|
|
|
|
libTable->InsertRow(
|
|
|
|
new SYMBOL_LIB_TABLE_ROW( libName, libTableUri, wxString( "KiCad" ) ) );
|
|
|
|
|
|
|
|
// Save project symbol library table.
|
2023-02-26 16:27:25 +00:00
|
|
|
wxFileName fn( aSchematic->Prj().GetProjectPath(),
|
|
|
|
SYMBOL_LIB_TABLE::GetSymbolLibTableFileName() );
|
2020-09-20 21:08:46 +00:00
|
|
|
|
|
|
|
// So output formatter goes out of scope and closes the file before reloading.
|
|
|
|
{
|
|
|
|
FILE_OUTPUTFORMATTER formatter( fn.GetFullPath() );
|
|
|
|
libTable->Format( &formatter, 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Relaod the symbol library table.
|
|
|
|
aSchematic->Prj().SetElem( PROJECT::ELEM_SYMBOL_LIB_TABLE, NULL );
|
2023-09-28 03:04:53 +00:00
|
|
|
PROJECT_SCH::SchSymbolLibTable( &aSchematic->Prj() );
|
2020-09-20 21:08:46 +00:00
|
|
|
}
|
2020-09-13 21:42:17 +00:00
|
|
|
|
2023-02-26 16:27:25 +00:00
|
|
|
// set properties to prevent save file on every symbol save
|
|
|
|
STRING_UTF8_MAP properties;
|
2023-12-24 00:31:24 +00:00
|
|
|
properties.emplace( SCH_IO_KICAD_SEXPR::PropBuffering, "" );
|
2023-02-26 16:27:25 +00:00
|
|
|
|
|
|
|
for( LIB_SYMBOL* const& symbol : csaLoader.GetLoadedSymbols() )
|
|
|
|
sch_plugin->SaveSymbol( libFileName.GetFullPath(), symbol, &properties );
|
2020-09-20 21:08:46 +00:00
|
|
|
|
|
|
|
sch_plugin->SaveLibrary( libFileName.GetFullPath() );
|
2020-09-13 21:42:17 +00:00
|
|
|
|
2023-02-26 16:27:25 +00:00
|
|
|
// Link up all symbols in the design to the newly created library
|
|
|
|
for( SCH_SHEET_PATH& sheet : aSchematic->GetSheets() )
|
|
|
|
{
|
|
|
|
for( SCH_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
|
|
|
|
{
|
|
|
|
SCH_SYMBOL* sym = static_cast<SCH_SYMBOL*>( item );
|
|
|
|
|
|
|
|
if( sym->GetLibId().IsLegacy() )
|
|
|
|
{
|
|
|
|
LIB_ID libid = sym->GetLibId();
|
|
|
|
libid.SetLibNickname( libName );
|
|
|
|
sym->SetLibId( libid );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-08-18 21:19:28 +00:00
|
|
|
// Need to fix up junctions after import to retain connectivity
|
|
|
|
aSchematic->FixupJunctions();
|
|
|
|
|
2020-09-20 21:08:46 +00:00
|
|
|
return rootSheet;
|
2020-09-08 19:51:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
void SCH_IO_CADSTAR_ARCHIVE::EnumerateSymbolLib( wxArrayString& aSymbolNameList,
|
2024-01-26 13:59:07 +00:00
|
|
|
const wxString& aLibraryPath,
|
|
|
|
const STRING_UTF8_MAP* aProperties )
|
2023-02-26 22:37:05 +00:00
|
|
|
{
|
2023-05-28 21:02:38 +00:00
|
|
|
ensureLoadedLibrary( aLibraryPath, aProperties );
|
2023-02-26 22:37:05 +00:00
|
|
|
|
2023-05-28 21:02:38 +00:00
|
|
|
for( auto& [libnameStr, libSymbol] : m_libCache )
|
|
|
|
aSymbolNameList.Add( libSymbol->GetName() );
|
2023-02-26 22:37:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
void SCH_IO_CADSTAR_ARCHIVE::EnumerateSymbolLib( std::vector<LIB_SYMBOL*>& aSymbolList,
|
2024-01-26 13:59:07 +00:00
|
|
|
const wxString& aLibraryPath,
|
|
|
|
const STRING_UTF8_MAP* aProperties )
|
2023-02-26 22:37:05 +00:00
|
|
|
{
|
2023-05-28 21:02:38 +00:00
|
|
|
ensureLoadedLibrary( aLibraryPath, aProperties );
|
2023-03-07 21:38:15 +00:00
|
|
|
|
2023-05-28 21:02:38 +00:00
|
|
|
for( auto& [libnameStr, libSymbol] : m_libCache )
|
|
|
|
aSymbolList.push_back( libSymbol.get() );
|
2023-02-26 22:37:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
LIB_SYMBOL* SCH_IO_CADSTAR_ARCHIVE::LoadSymbol( const wxString& aLibraryPath,
|
2024-01-26 13:59:07 +00:00
|
|
|
const wxString& aAliasName,
|
|
|
|
const STRING_UTF8_MAP* aProperties )
|
2023-02-26 22:37:05 +00:00
|
|
|
{
|
2023-05-28 21:02:38 +00:00
|
|
|
ensureLoadedLibrary( aLibraryPath, aProperties );
|
2023-03-07 21:38:15 +00:00
|
|
|
|
2023-05-28 21:02:38 +00:00
|
|
|
if( m_libCache.count( aAliasName ) )
|
|
|
|
return m_libCache.at( aAliasName ).get();
|
2023-03-07 21:38:15 +00:00
|
|
|
|
2023-02-26 22:37:05 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
void SCH_IO_CADSTAR_ARCHIVE::GetAvailableSymbolFields( std::vector<wxString>& aNames )
|
2023-02-26 22:37:05 +00:00
|
|
|
{
|
|
|
|
std::set<wxString> fieldNames;
|
|
|
|
|
|
|
|
for( auto& [libnameStr, libSymbol] : m_libCache )
|
|
|
|
{
|
2024-04-12 21:00:41 +00:00
|
|
|
std::vector<SCH_FIELD*> fields;
|
2023-02-26 22:37:05 +00:00
|
|
|
libSymbol->GetFields( fields );
|
|
|
|
|
2024-04-12 21:00:41 +00:00
|
|
|
for( SCH_FIELD* field : fields )
|
2023-02-26 22:37:05 +00:00
|
|
|
{
|
|
|
|
if( field->IsMandatory() )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
fieldNames.insert( field->GetName() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::copy( fieldNames.begin(), fieldNames.end(), std::back_inserter( aNames ) );
|
|
|
|
}
|
2023-05-28 21:02:38 +00:00
|
|
|
|
|
|
|
|
2023-12-27 00:25:41 +00:00
|
|
|
void SCH_IO_CADSTAR_ARCHIVE::GetLibraryOptions( STRING_UTF8_MAP* aListToAppendTo ) const
|
2023-05-28 21:02:38 +00:00
|
|
|
{
|
|
|
|
( *aListToAppendTo )["csa"] =
|
|
|
|
UTF8( _( "Path to the CADSTAR schematic archive (*.csa) file related to this CADSTAR "
|
|
|
|
"parts library. If none specified it is assumed to be 'symbol.csa' in the "
|
|
|
|
"same folder." ) );
|
|
|
|
|
|
|
|
( *aListToAppendTo )["fplib"] =
|
|
|
|
UTF8( _( "Name of the footprint library related to the symbols in this library. You "
|
|
|
|
"should create a separate entry for the CADSTAR PCB Archive (*.cpa) file in "
|
|
|
|
"the footprint library tables. If none specified, 'cadstarpcblib' is assumed." ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
void SCH_IO_CADSTAR_ARCHIVE::ensureLoadedLibrary( const wxString& aLibraryPath,
|
2024-01-26 13:59:07 +00:00
|
|
|
const STRING_UTF8_MAP* aProperties )
|
2023-05-28 21:02:38 +00:00
|
|
|
{
|
|
|
|
wxFileName csafn;
|
|
|
|
wxString fplibname = "cadstarpcblib";
|
|
|
|
|
2023-08-28 18:49:18 +00:00
|
|
|
if( aProperties && aProperties->count( "csa" ) )
|
2023-05-28 21:02:38 +00:00
|
|
|
{
|
|
|
|
csafn = wxFileName( aProperties->at( "csa" ) );
|
2023-08-27 18:08:46 +00:00
|
|
|
|
|
|
|
if( !csafn.IsAbsolute() )
|
|
|
|
{
|
|
|
|
wxFileName libDir( aLibraryPath );
|
|
|
|
libDir.ClearExt();
|
|
|
|
libDir.SetName( "" );
|
2023-08-28 09:28:55 +00:00
|
|
|
// wxPATH_NORM_ALL is deprecated in wxWidgets 3.1, so use our flags
|
|
|
|
csafn.Normalize( FN_NORMALIZE_FLAGS, libDir.GetAbsolutePath() );
|
2023-08-27 18:08:46 +00:00
|
|
|
}
|
2023-05-28 21:02:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-10-15 02:56:38 +00:00
|
|
|
// If none specified, look for the
|
|
|
|
// .csa file in same folder as the .lib
|
2023-05-28 21:02:38 +00:00
|
|
|
csafn = wxFileName( aLibraryPath );
|
|
|
|
csafn.SetExt( "csa" );
|
2023-10-15 02:56:38 +00:00
|
|
|
|
|
|
|
if( !csafn.FileExists() )
|
|
|
|
{
|
|
|
|
csafn.SetName( "symbol" );
|
|
|
|
|
|
|
|
if( !csafn.FileExists() )
|
|
|
|
{
|
|
|
|
csafn = wxDir::FindFirst( csafn.GetPath(), wxS( "*.csa" ),
|
|
|
|
wxDIR_FILES | wxDIR_HIDDEN );
|
|
|
|
|
|
|
|
if( !csafn.FileExists() )
|
|
|
|
{
|
|
|
|
THROW_IO_ERROR( wxString::Format(
|
|
|
|
_( "Cannot find the .csa file corresponding to library '%s'." ),
|
|
|
|
aLibraryPath ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-28 21:02:38 +00:00
|
|
|
}
|
|
|
|
|
2023-08-28 18:49:18 +00:00
|
|
|
if( aProperties && aProperties->count( "fplib" ) )
|
2023-05-28 21:02:38 +00:00
|
|
|
{
|
2023-09-14 15:35:19 +00:00
|
|
|
fplibname = aProperties->at( "fplib" ).wx_str();
|
2023-05-28 21:02:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get timestamp
|
|
|
|
long long timestamp = 0;
|
|
|
|
wxFileName fn( aLibraryPath );
|
|
|
|
|
|
|
|
if( fn.IsFileReadable() )
|
|
|
|
timestamp = fn.GetModificationTime().GetValue().GetValue();
|
|
|
|
|
|
|
|
|
|
|
|
if( fn.IsFileReadable()
|
|
|
|
&& m_cachePath == aLibraryPath
|
|
|
|
&& m_cachecsafn.GetFullPath() == csafn.GetFullPath()
|
|
|
|
&& m_cachefplibname == fplibname
|
|
|
|
&& m_cacheTimestamp == timestamp )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update cache
|
|
|
|
m_libCache.clear();
|
|
|
|
|
|
|
|
CADSTAR_SCH_ARCHIVE_LOADER csaLoader( csafn.GetFullPath(), m_reporter, m_progressReporter );
|
|
|
|
csaLoader.SetFpLibName( fplibname );
|
|
|
|
|
|
|
|
std::vector<LIB_SYMBOL*> symbols = csaLoader.LoadPartsLib( aLibraryPath );
|
|
|
|
|
|
|
|
for( LIB_SYMBOL* sym : symbols )
|
|
|
|
{
|
|
|
|
m_libCache.insert( { sym->GetName(), std::unique_ptr<LIB_SYMBOL>( sym ) } );
|
|
|
|
}
|
|
|
|
|
|
|
|
m_cachePath = aLibraryPath;
|
|
|
|
m_cachecsafn = csafn;
|
|
|
|
m_cachefplibname = fplibname;
|
|
|
|
m_cacheTimestamp = timestamp;
|
|
|
|
}
|
|
|
|
|