2016-07-06 09:22:56 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2016 CERN
|
2020-02-13 13:39:52 +00:00
|
|
|
* Copyright (C) 2016-2020 KiCad Developers, see change_log.txt for contributors.
|
2016-07-06 09:22:56 +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 <wx/filename.h>
|
|
|
|
#include <wx/uri.h>
|
|
|
|
|
|
|
|
#include <sch_io_mgr.h>
|
|
|
|
#include <sch_legacy_plugin.h>
|
2017-02-27 10:07:52 +00:00
|
|
|
#include <sch_eagle_plugin.h>
|
2020-02-13 13:39:52 +00:00
|
|
|
#include <sch_sexpr_plugin.h>
|
2016-07-06 09:22:56 +00:00
|
|
|
|
|
|
|
#include <wildcards_and_files_ext.h>
|
|
|
|
|
2017-12-15 11:37:46 +00:00
|
|
|
#define FMT_UNIMPLEMENTED _( "Plugin \"%s\" does not implement the \"%s\" function." )
|
|
|
|
#define FMT_NOTFOUND _( "Plugin type \"%s\" is not found." )
|
2016-07-06 09:22:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 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();
|
2020-02-13 13:39:52 +00:00
|
|
|
case SCH_KICAD:
|
|
|
|
return new SCH_SEXPR_PLUGIN();
|
2017-02-27 10:07:52 +00:00
|
|
|
case SCH_EAGLE:
|
|
|
|
return new SCH_EAGLE_PLUGIN();
|
2016-07-06 09:22:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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 )
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
return wxString::Format( _( "Unknown SCH_FILE_T value: %d" ), aType );
|
|
|
|
|
|
|
|
case SCH_LEGACY:
|
|
|
|
return wxString( wxT( "Legacy" ) );
|
2017-02-27 10:07:52 +00:00
|
|
|
|
2020-02-13 13:39:52 +00:00
|
|
|
case SCH_KICAD:
|
2020-02-28 14:03:09 +00:00
|
|
|
return wxString( wxT( "KiCad" ) );
|
2020-02-13 13:39:52 +00:00
|
|
|
|
2017-02-27 10:07:52 +00:00
|
|
|
case SCH_EAGLE:
|
|
|
|
return wxString( wxT( "EAGLE" ) );
|
2016-07-06 09:22:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
2020-02-28 14:03:09 +00:00
|
|
|
else if( aType == wxT( "KiCad" ) )
|
2020-02-13 13:39:52 +00:00
|
|
|
return SCH_KICAD;
|
2017-02-27 10:07:52 +00:00
|
|
|
else if( aType == wxT( "EAGLE" ) )
|
|
|
|
return SCH_EAGLE;
|
2016-07-06 09:22:56 +00:00
|
|
|
|
|
|
|
// 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 );
|
|
|
|
|
|
|
|
if( plugin != NULL )
|
|
|
|
{
|
|
|
|
ext = plugin->GetFileExtension();
|
|
|
|
ReleasePlugin( plugin );
|
|
|
|
}
|
|
|
|
|
|
|
|
return ext;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-28 14:03:09 +00:00
|
|
|
const wxString SCH_IO_MGR::GetLibraryFileExtension( SCH_FILE_T aFileType )
|
|
|
|
{
|
|
|
|
wxString ext = wxEmptyString;
|
|
|
|
SCH_PLUGIN* plugin = FindPlugin( aFileType );
|
|
|
|
|
|
|
|
if( plugin != NULL )
|
|
|
|
{
|
|
|
|
ext = plugin->GetLibraryFileExtension();
|
|
|
|
ReleasePlugin( plugin );
|
|
|
|
}
|
|
|
|
|
|
|
|
return ext;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-06 09:22:56 +00:00
|
|
|
SCH_IO_MGR::SCH_FILE_T SCH_IO_MGR::GuessPluginTypeFromLibPath( const wxString& aLibPath )
|
|
|
|
{
|
|
|
|
SCH_FILE_T ret = SCH_LEGACY; // default guess, unless detected otherwise.
|
|
|
|
wxFileName fn( aLibPath );
|
|
|
|
|
2020-02-28 14:03:09 +00:00
|
|
|
if( fn.GetExt() == SchematicLibraryFileExtension )
|
2016-07-06 09:22:56 +00:00
|
|
|
{
|
|
|
|
ret = SCH_LEGACY;
|
|
|
|
}
|
2020-02-13 13:39:52 +00:00
|
|
|
else if( fn.GetExt() == KiCadSymbolLibFileExtension )
|
|
|
|
{
|
|
|
|
ret = SCH_KICAD;
|
|
|
|
}
|
2016-07-06 09:22:56 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SCH_SHEET* SCH_IO_MGR::Load( SCH_FILE_T aFileType, const wxString& aFileName, KIWAY* aKiway,
|
|
|
|
SCH_SHEET* aAppendToMe, const PROPERTIES* aProperties )
|
|
|
|
{
|
|
|
|
// release the SCH_PLUGIN even if an exception is thrown.
|
|
|
|
SCH_PLUGIN::SCH_PLUGIN_RELEASER pi( FindPlugin( aFileType ) );
|
|
|
|
|
|
|
|
if( (SCH_PLUGIN*) pi ) // test pi->plugin
|
|
|
|
{
|
|
|
|
return pi->Load( aFileName, aKiway, aAppendToMe, aProperties ); // virtual
|
|
|
|
}
|
|
|
|
|
|
|
|
THROW_IO_ERROR( wxString::Format( FMT_NOTFOUND, ShowType( aFileType ).GetData() ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SCH_IO_MGR::Save( SCH_FILE_T aFileType, const wxString& aFileName,
|
2016-07-11 19:48:46 +00:00
|
|
|
SCH_SCREEN* aSchematic, KIWAY* aKiway, const PROPERTIES* aProperties )
|
2016-07-06 09:22:56 +00:00
|
|
|
{
|
|
|
|
// release the SCH_PLUGIN even if an exception is thrown.
|
|
|
|
SCH_PLUGIN::SCH_PLUGIN_RELEASER pi( FindPlugin( aFileType ) );
|
|
|
|
|
|
|
|
if( (SCH_PLUGIN*) pi ) // test pi->plugin
|
|
|
|
{
|
2016-07-11 19:48:46 +00:00
|
|
|
pi->Save( aFileName, aSchematic, aKiway, aProperties ); // virtual
|
2016-07-06 09:22:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
THROW_IO_ERROR( wxString::Format( FMT_NOTFOUND, ShowType( aFileType ).GetData() ) );
|
|
|
|
}
|
2017-06-26 11:13:43 +00:00
|
|
|
|
|
|
|
|
2017-11-02 20:41:29 +00:00
|
|
|
DECLARE_ENUM_VECTOR( SCH_IO_MGR, SCH_FILE_T )
|