kicad/eeschema/sch_io_mgr.cpp

220 lines
7.0 KiB
C++
Raw Normal View History

/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2016 CERN
* Copyright (C) 2016-2023 KiCad Developers, see change_log.txt for contributors.
*
* @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 <wx/filename.h>
#include <wx/uri.h>
#include <sch_io_mgr.h>
#include <sch_plugins/legacy/sch_legacy_plugin.h>
#include <sch_plugins/eagle/sch_eagle_plugin.h>
#include <sch_plugins/kicad/sch_sexpr_plugin.h>
#include <sch_plugins/altium/sch_altium_plugin.h>
#include <sch_plugins/cadstar/cadstar_sch_archive_plugin.h>
#include <sch_plugins/database/sch_database_plugin.h>
#include <sch_plugins/ltspice/ltspice_sch_plugin.h>
#include <wildcards_and_files_ext.h>
#include <common.h> // for ExpandEnvVarSubstitutions
#define FMT_UNIMPLEMENTED _( "Plugin \"%s\" does not implement the \"%s\" function." )
#define FMT_NOTFOUND _( "Plugin type \"%s\" is not found." )
// Some day plugins might be in separate DLL/DSOs, simply because of numbers of them
// and code size. Until then, use the simplest method:
// This implementation is one of two which could be done.
// The other one would cater to DLL/DSO's. But since it would be nearly
// impossible to link a KICAD type DLL/DSO right now without pulling in all
// ::Draw() functions, I forgo that option temporarily.
// Some day it may be possible to have some built in AND some DLL/DSO
// plugins coexisting.
SCH_PLUGIN* SCH_IO_MGR::FindPlugin( SCH_FILE_T aFileType )
{
// This implementation is subject to change, any magic is allowed here.
// The public SCH_IO_MGR API is the only pertinent public information.
switch( aFileType )
{
case SCH_LEGACY: return new SCH_LEGACY_PLUGIN();
case SCH_KICAD: return new SCH_SEXPR_PLUGIN();
case SCH_ALTIUM: return new SCH_ALTIUM_PLUGIN();
case SCH_CADSTAR_ARCHIVE: return new CADSTAR_SCH_ARCHIVE_PLUGIN();
case SCH_EAGLE: return new SCH_EAGLE_PLUGIN();
case SCH_DATABASE: return new SCH_DATABASE_PLUGIN();
case SCH_LTSPICE: return new SCH_LTSPICE_PLUGIN();
default: return nullptr;
}
}
void SCH_IO_MGR::ReleasePlugin( SCH_PLUGIN* aPlugin )
{
// This function is a place holder for a future point in time where
// the plugin is a DLL/DSO. It could do reference counting, and then
// unload the DLL/DSO when count goes to zero.
delete aPlugin;
}
const wxString SCH_IO_MGR::ShowType( SCH_FILE_T aType )
{
// keep this function in sync with EnumFromStr() relative to the
// text spellings. If you change the spellings, you will obsolete
// library tables, so don't do change, only additions are ok.
switch( aType )
{
case SCH_LEGACY: return wxString( wxT( "Legacy" ) );
case SCH_KICAD: return wxString( wxT( "KiCad" ) );
case SCH_ALTIUM: return wxString( wxT( "Altium" ) );
case SCH_CADSTAR_ARCHIVE: return wxString( wxT( "CADSTAR Schematic Archive" ) );
case SCH_EAGLE: return wxString( wxT( "EAGLE" ) );
case SCH_DATABASE: return wxString( wxT( "Database" ) );
case SCH_LTSPICE: return wxString( wxT( "LTSpice Schematic" ) );
default: return wxString::Format( _( "Unknown SCH_FILE_T value: %d" ),
aType );
}
}
SCH_IO_MGR::SCH_FILE_T SCH_IO_MGR::EnumFromStr( const wxString& aType )
{
// keep this function in sync with ShowType() relative to the
// text spellings. If you change the spellings, you will obsolete
// library tables, so don't do change, only additions are ok.
if( aType == wxT( "Legacy" ) )
return SCH_LEGACY;
else if( aType == wxT( "KiCad" ) )
return SCH_KICAD;
else if( aType == wxT( "Altium" ) )
return SCH_ALTIUM;
else if( aType == wxT( "CADSTAR Schematic Archive" ) )
return SCH_CADSTAR_ARCHIVE;
else if( aType == wxT( "EAGLE" ) )
return SCH_EAGLE;
else if( aType == wxT( "Database" ) )
return SCH_DATABASE;
// wxASSERT( blow up here )
return SCH_FILE_T( -1 );
}
const wxString SCH_IO_MGR::GetFileExtension( SCH_FILE_T aFileType )
{
wxString ext = wxEmptyString;
SCH_PLUGIN* plugin = FindPlugin( aFileType );
2021-07-16 20:13:26 +00:00
if( plugin != nullptr )
{
ext = plugin->GetFileExtension();
ReleasePlugin( plugin );
}
return ext;
}
const wxString SCH_IO_MGR::GetLibraryFileExtension( SCH_FILE_T aFileType )
{
wxString ext = wxEmptyString;
SCH_PLUGIN* plugin = FindPlugin( aFileType );
2021-07-16 20:13:26 +00:00
if( plugin != nullptr )
{
ext = plugin->GetLibraryFileExtension();
ReleasePlugin( plugin );
}
return ext;
}
SCH_IO_MGR::SCH_FILE_T SCH_IO_MGR::GuessPluginTypeFromLibPath( const wxString& aLibPath )
{
Make the new schematic and symbol library file formats the default. This is a very large and potentially disruptive change so this will be an unusually long and detailed commit message. The new file formats are now the default in both the schematic and symbol library editors. Existing symbol libraries will be saved in their current format until new features are added to library symbols. Once this happens, both the legacy schematic and symbol file formats will be no longer be savable and existing libraries will have to be converted. Saving to the legacy file formats is still available for round robin testing and should not be used for normal editing. When loading the legacy schematic file, it is imperative that the schematic library symbols are rescued and/or remapped to valid library identifiers. Otherwise, there will be no way to link to the original library symbol and the user will be required manually set the library identifier. The cached symbol will be saved in the schematic file so the last library symbol in the cache will still be used but there will be no way to update it from the original library. The next save after loading a legacy schematic file will be converted to the s-expression file format. Schematics with hierarchical sheets will automatically have all sheet file name extensions changed to .kicad_sym and saved to the new format as well. Appending schematics requires that the schematic to append has already been converted to the new file format. This is required to ensure that library symbols are guaranteed to be valid for the appended schematic. The schematic symbol library symbol link resolution has been moved out of the SCH_COMPONENT object and move into the SCH_SCREEN object that owns the symbol. This was done to ensure that there is a single place where the library symbol links get resolved rather than the dozen or so different code paths that previously existed. It also removes the necessity of the SCH_COMPONENT object of requiring any knowledge of the symbol library table and/or the cache library. When opening an s-expression schematic, the legacy cache library is not loaded so any library symbols not rescued cannot be loaded. Broken library symbol links will have to be manually resolved by adding the cache library to the symbol library table and changing the links in the schematic symbol. Now that the library symbols are embedded in the schematic file, the SCH_SCREEN object maintains the list of library symbols for the schematic automatically. No external manipulation of this library cache should ever occur. ADDED: S-expression schematic and symbol library file formats.
2020-04-16 16:43:50 +00:00
SCH_FILE_T ret = SCH_KICAD; // default guess, unless detected otherwise.
wxFileName fn( aLibPath );
wxString ext = fn.GetExt().Lower();
// .lib is shared between CADSTAR and Legacy KiCad file formats. Let's read the header
if( ext == LegacySymbolLibFileExtension )
{
wxString fullName = ExpandEnvVarSubstitutions( aLibPath, nullptr );
// Of course the file should exist to be read. If not, use the SCH_LEGACY
// format: it is more usual than SCH_CADSTAR_ARCHIVE
if( !wxFileExists( fullName ) )
return SCH_LEGACY;
for( SCH_FILE_T pluginType : { SCH_LEGACY, SCH_CADSTAR_ARCHIVE } )
{
SCH_PLUGIN::SCH_PLUGIN_RELEASER pi( SCH_IO_MGR::FindPlugin( pluginType ) );
if( pi )
{
if( pi->CheckHeader( fullName ) )
return pluginType;
}
}
// If not found, use the SCH_LEGACY.
return SCH_LEGACY;
}
for( SCH_IO_MGR::SCH_FILE_T piType : SCH_IO_MGR::SCH_FILE_T_vector )
{
if( SCH_IO_MGR::GetLibraryFileExtension( piType ).Lower() == ext )
{
ret = piType;
break;
}
}
return ret;
}
SCH_IO_MGR::SCH_FILE_T SCH_IO_MGR::GuessPluginTypeFromSchPath( const wxString& aSchematicPath )
{
Make the new schematic and symbol library file formats the default. This is a very large and potentially disruptive change so this will be an unusually long and detailed commit message. The new file formats are now the default in both the schematic and symbol library editors. Existing symbol libraries will be saved in their current format until new features are added to library symbols. Once this happens, both the legacy schematic and symbol file formats will be no longer be savable and existing libraries will have to be converted. Saving to the legacy file formats is still available for round robin testing and should not be used for normal editing. When loading the legacy schematic file, it is imperative that the schematic library symbols are rescued and/or remapped to valid library identifiers. Otherwise, there will be no way to link to the original library symbol and the user will be required manually set the library identifier. The cached symbol will be saved in the schematic file so the last library symbol in the cache will still be used but there will be no way to update it from the original library. The next save after loading a legacy schematic file will be converted to the s-expression file format. Schematics with hierarchical sheets will automatically have all sheet file name extensions changed to .kicad_sym and saved to the new format as well. Appending schematics requires that the schematic to append has already been converted to the new file format. This is required to ensure that library symbols are guaranteed to be valid for the appended schematic. The schematic symbol library symbol link resolution has been moved out of the SCH_COMPONENT object and move into the SCH_SCREEN object that owns the symbol. This was done to ensure that there is a single place where the library symbol links get resolved rather than the dozen or so different code paths that previously existed. It also removes the necessity of the SCH_COMPONENT object of requiring any knowledge of the symbol library table and/or the cache library. When opening an s-expression schematic, the legacy cache library is not loaded so any library symbols not rescued cannot be loaded. Broken library symbol links will have to be manually resolved by adding the cache library to the symbol library table and changing the links in the schematic symbol. Now that the library symbols are embedded in the schematic file, the SCH_SCREEN object maintains the list of library symbols for the schematic automatically. No external manipulation of this library cache should ever occur. ADDED: S-expression schematic and symbol library file formats.
2020-04-16 16:43:50 +00:00
SCH_FILE_T ret = SCH_KICAD; // default guess, unless detected otherwise.
wxFileName fn( aSchematicPath );
if( fn.GetExt() == LegacySchematicFileExtension )
{
ret = SCH_LEGACY;
}
else if( fn.GetExt() == KiCadSchematicFileExtension )
{
ret = SCH_KICAD;
}
return ret;
}