kicad/pcbnew/librairi.cpp

615 lines
16 KiB
C++
Raw Normal View History

/**
* @file librairi.cpp
* @brief Manage module (footprint) libraries.
*/
2007-05-06 16:03:28 +00:00
#include <fctsys.h>
#include <wx/ffile.h>
#include <appl_wxstruct.h>
#include <class_drawpanel.h>
#include <confirm.h>
#include <kicad_string.h>
#include <gestfich.h>
#include <wxPcbStruct.h>
#include <dialog_helpers.h>
#include <richio.h>
#include <filter_reader.h>
#include <pcbcommon.h>
#include <macros.h>
#include <class_board.h>
#include <class_module.h>
#include <pcbnew.h>
#include <module_editor_frame.h>
#include <wildcards_and_files_ext.h>
#include <kicad_plugin.h>
#include <legacy_plugin.h>
2007-05-06 16:03:28 +00:00
#define BACKUP_EXT wxT( "bak" )
#define FILETMP_EXT wxT( "$$$" )
#define EXPORT_IMPORT_LASTPATH_KEY wxT( "import_last_path" )
2007-05-06 16:03:28 +00:00
const wxString ModExportFileExtension( wxT( "emp" ) );
static const wxString ModExportFileWildcard( _( "KiCad foot print export files (*.emp)|*.emp" ) );
static const wxString ModImportFileWildcard( _( "GPcb foot print files (*)|*" ) );
MODULE* FOOTPRINT_EDIT_FRAME::Import_Module()
2007-05-06 16:03:28 +00:00
{
// use the clipboard for this in the future?
wxString lastOpenedPathForLoading;
wxConfig* config = wxGetApp().GetSettings();
2007-10-30 21:30:58 +00:00
if( config )
config->Read( EXPORT_IMPORT_LASTPATH_KEY, &lastOpenedPathForLoading );
wxString importWildCard = FootprintLibFileWildcard + wxT("|") +
ModExportFileWildcard + wxT("|") +
ModImportFileWildcard;
wxFileDialog dlg( this, _( "Import Footprint Module" ),
lastOpenedPathForLoading, wxEmptyString,
importWildCard, wxFD_OPEN | wxFD_FILE_MUST_EXIST );
if( dlg.ShowModal() == wxID_CANCEL )
2007-10-30 21:30:58 +00:00
return NULL;
FILE* fp = wxFopen( dlg.GetPath(), wxT( "rt" ) );
if( !fp )
2007-10-30 21:30:58 +00:00
{
wxString msg = wxString::Format( _( "File <%s> not found" ), GetChars( dlg.GetPath() ) );
2007-10-30 21:30:58 +00:00
DisplayError( this, msg );
return NULL;
}
if( config ) // Save file path
{
lastOpenedPathForLoading = wxPathOnly( dlg.GetPath() );
config->Write( EXPORT_IMPORT_LASTPATH_KEY, lastOpenedPathForLoading );
}
wxString moduleName;
bool isGeda = false;
bool isLegacy = false;
{
FILE_LINE_READER freader( fp, dlg.GetPath() ); // I own fp, and will close it.
FILTER_READER reader( freader ); // skip blank lines
reader.ReadLine();
char* line = reader.Line();
if( !strnicmp( line, "(module", 7 ) )
{
// isKicad = true;
}
else if( !strnicmp( line, FOOTPRINT_LIBRARY_HEADER, FOOTPRINT_LIBRARY_HEADER_CNT ) )
{
isLegacy = true;
while( reader.ReadLine() )
{
if( !strnicmp( line, "$MODULE", 7 ) )
{
moduleName = FROM_UTF8( StrPurge( line + sizeof( "$MODULE" ) -1 ) );
break;
}
}
}
else if( !strnicmp( line, "Element", 7 ) )
{
isGeda = true;
}
else
{
DisplayError( this, _( "Not a module file" ) );
return NULL;
}
2007-10-30 21:30:58 +00:00
// fp is closed here by ~FILE_LINE_READER()
}
2007-10-30 21:30:58 +00:00
MODULE* module;
2007-10-30 21:30:58 +00:00
if( isGeda )
{
LOCALE_IO toggle;
// @todo GEDA plugin
module = new MODULE( GetBoard() );
module->Read_GPCB_Descr( dlg.GetPath() );
}
else if( isLegacy )
{
try
{
PLUGIN::RELEASER pi( IO_MGR::PluginFind( IO_MGR::LEGACY ) );
module = pi->FootprintLoad( dlg.GetPath(), moduleName );
if( !module )
{
wxString msg = wxString::Format(
_( "Unable to find or load footprint '%s' from lib path '%s'" ),
GetChars( moduleName ), GetChars( dlg.GetPath() ) );
DisplayError( this, msg );
return NULL;
}
}
catch( IO_ERROR ioe )
{
DisplayError( this, ioe.errorText );
return NULL;
}
}
else // if( isKicad )
{
try
{
// This technique was chosen to create an example of how reading
// the s-expression format from clipboard could be done.
wxString fcontents;
PCB_IO pcb_io;
wxFFile f( dlg.GetPath() );
if( !f.IsOpened() )
{
wxString msg = wxString::Format(
_( "Unable to find or load footprint from path '%s'" ),
GetChars( dlg.GetPath() ) );
DisplayError( this, msg );
return NULL;
}
f.ReadAll( &fcontents );
module = dynamic_cast<MODULE*>( pcb_io.Parse( fcontents ) );
if( !module )
{
wxString msg = wxString::Format(
_( "Unable to find or load footprint from lib path '%s'" ),
GetChars( dlg.GetPath() ) );
DisplayError( this, msg );
return NULL;
}
}
catch( IO_ERROR ioe )
{
DisplayError( this, ioe.errorText );
return NULL;
}
}
2007-10-30 21:30:58 +00:00
// Insert footprint in list
GetBoard()->Add( module );
2007-10-30 21:30:58 +00:00
// Display info :
module->DisplayInfo( this );
PlaceModule( module, NULL );
GetBoard()->m_Status_Pcb = 0;
GetBoard()->BuildListOfNets();
2007-10-30 21:30:58 +00:00
return module;
2007-05-06 16:03:28 +00:00
}
2007-10-30 21:30:58 +00:00
void FOOTPRINT_EDIT_FRAME::Export_Module( MODULE* aModule, bool aCreateSysLib )
2007-05-06 16:03:28 +00:00
{
wxFileName fn;
wxString msg, path, title, wildcard;
wxConfig* config = wxGetApp().GetSettings();
2007-10-30 21:30:58 +00:00
2010-12-29 17:47:32 +00:00
if( aModule == NULL )
2007-10-30 21:30:58 +00:00
return;
2010-12-29 17:47:32 +00:00
fn.SetName( aModule->m_LibRef );
2007-10-30 21:30:58 +00:00
// There is no longer any point to exporting as a new library. Work is planned
// for the library manager to make it easier to use.
title = _( "Export Module" );
wildcard = FootprintLibFileWildcard;
2008-03-12 11:49:16 +00:00
fn.SetExt( FootprintFileExtension );
config->Read( EXPORT_IMPORT_LASTPATH_KEY, &path );
fn.SetPath( path );
wxFileDialog dlg( this, msg, fn.GetPath(), fn.GetFullName(),
wxGetTranslation( wildcard ),
wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
if( dlg.ShowModal() == wxID_CANCEL )
2007-10-30 21:30:58 +00:00
return;
fn = dlg.GetPath();
2007-10-30 21:30:58 +00:00
if( config ) // Save file path
2007-10-30 21:30:58 +00:00
{
config->Write( EXPORT_IMPORT_LASTPATH_KEY, fn.GetPath() );
}
2008-03-12 11:49:16 +00:00
try
{
// Export as *.kicad_pcb format, using a strategy which is specifically chosen
// as an example on how it could also be used to send it to the system clipboard.
PCB_IO pcb_io( CTL_FOR_LIBRARY );
/* This module should *already* be "normalized" in a way such that
orientation is zero, etc., since it came from module editor.
module->SetTimeStamp( 0 );
module->SetParent( 0 );
module->SetOrientation( 0 );
*/
pcb_io.Format( aModule );
FILE* fp = wxFopen( dlg.GetPath(), wxT( "wt" ) );
fprintf( fp, "%s", pcb_io.GetStringOutput( false ).c_str() );
fclose( fp );
}
catch( IO_ERROR ioe )
{
DisplayError( this, ioe.errorText );
return;
}
msg.Printf( _( "Module exported to file <%s>" ), GetChars( dlg.GetPath() ) );
DisplayInfoMessage( this, msg );
2007-05-06 16:03:28 +00:00
}
2007-10-30 21:30:58 +00:00
void FOOTPRINT_EDIT_FRAME::Delete_Module_In_Library( const wxString& aLibName )
2007-05-06 16:03:28 +00:00
{
wxString footprintName = Select_1_Module_From_List( this, aLibName, wxEmptyString, wxEmptyString );
2007-10-30 21:30:58 +00:00
if( footprintName == wxEmptyString )
2007-10-30 21:30:58 +00:00
return;
// Confirmation
wxString msg = wxString::Format( _( "Ok to delete module '%s' in library '%s'" ),
footprintName.GetData(), aLibName.GetData() );
2011-01-16 05:11:17 +00:00
2007-10-30 21:30:58 +00:00
if( !IsOK( this, msg ) )
return;
2012-04-16 03:18:41 +00:00
try
2007-10-30 21:30:58 +00:00
{
PLUGIN::RELEASER pi( IO_MGR::PluginFind( IO_MGR::LEGACY ) );
2007-10-30 21:30:58 +00:00
pi->FootprintDelete( aLibName, footprintName );
2007-10-30 21:30:58 +00:00
}
2012-04-16 03:18:41 +00:00
catch( IO_ERROR ioe )
2007-10-30 21:30:58 +00:00
{
2012-04-16 03:18:41 +00:00
DisplayError( NULL, ioe.errorText );
2007-10-30 21:30:58 +00:00
return;
}
msg.Printf( _( "Component '%s' deleted from library '%s'" ),
footprintName.GetData(), aLibName.GetData() );
SetStatusText( msg );
2007-05-06 16:03:28 +00:00
}
/* Save modules in a library:
* param aNewModulesOnly:
* true : save modules not already existing in this lib
* false: save all modules
*/
void PCB_EDIT_FRAME::ArchiveModulesOnBoard( const wxString& aLibName, bool aNewModulesOnly )
2007-05-06 16:03:28 +00:00
{
wxString fileName = aLibName;
wxString path;
2007-10-30 21:30:58 +00:00
if( GetBoard()->m_Modules == NULL )
2007-10-30 21:30:58 +00:00
{
DisplayInfoMessage( this, _( "No modules to archive!" ) );
2007-10-30 21:30:58 +00:00
return;
}
path = wxGetApp().ReturnLastVisitedLibraryPath();
if( !aLibName )
2007-10-30 21:30:58 +00:00
{
wxFileDialog dlg( this, _( "Library" ), path,
wxEmptyString,
wxGetTranslation( LegacyFootprintLibFileWildcard ),
wxFD_SAVE );
2007-10-30 21:30:58 +00:00
if( dlg.ShowModal() == wxID_CANCEL )
2007-10-30 21:30:58 +00:00
return;
fileName = dlg.GetPath();
2007-10-30 21:30:58 +00:00
}
wxFileName fn( fileName );
wxGetApp().SaveLastVisitedLibraryPath( fn.GetPath() );
bool lib_exists = wxFileExists( fileName );
if( !aNewModulesOnly && lib_exists )
2007-10-30 21:30:58 +00:00
{
wxString msg;
msg.Printf( _( "Library %s exists, OK to replace ?" ), GetChars( fileName ) );
2007-10-30 21:30:58 +00:00
if( !IsOK( this, msg ) )
return;
}
m_canvas->SetAbortRequest( false );
2007-10-30 21:30:58 +00:00
try
2007-10-30 21:30:58 +00:00
{
PLUGIN::RELEASER pi( IO_MGR::PluginFind( IO_MGR::LEGACY ) );
// Delete old library if we're replacing it entirely.
if( lib_exists && !aNewModulesOnly )
2007-10-30 21:30:58 +00:00
{
pi->FootprintLibDelete( fileName );
lib_exists = false;
2007-10-30 21:30:58 +00:00
}
if( !lib_exists )
{
pi->FootprintLibCreate( fileName );
}
if( !aNewModulesOnly )
{
for( MODULE* m = GetBoard()->m_Modules; m; m = m->Next() )
{
pi->FootprintSave( fileName, m );
}
}
else
{
for( MODULE* m = GetBoard()->m_Modules; m; m = m->Next() )
{
if( !Save_Module_In_Library( fileName, m, false, false ) )
break;
2007-10-30 21:30:58 +00:00
// Check for request to stop backup (ESCAPE key actuated)
if( m_canvas->GetAbortRequest() )
break;
}
}
}
catch( IO_ERROR ioe )
2007-10-30 21:30:58 +00:00
{
DisplayError( this, ioe.errorText );
return;
2007-10-30 21:30:58 +00:00
}
2007-05-06 16:03:28 +00:00
}
bool PCB_BASE_FRAME::Save_Module_In_Library( const wxString& aLibName,
MODULE* aModule,
bool aOverwrite,
bool aDisplayDialog )
2007-05-06 16:03:28 +00:00
{
if( aModule == NULL )
return false;
aModule->DisplayInfo( this );
2007-10-30 21:30:58 +00:00
if( !wxFileExists( aLibName ) )
2007-10-30 21:30:58 +00:00
{
wxString msg = wxString::Format( _( "Library <%s> not found." ),
aLibName.GetData() );
2007-10-30 21:30:58 +00:00
DisplayError( this, msg );
return false;
2007-10-30 21:30:58 +00:00
}
if( !IsWritable( aLibName ) )
return false;
// Ask what to use as the footprint name in the library
wxString footprintName = aModule->GetLibRef();
2007-10-30 21:30:58 +00:00
if( aDisplayDialog )
2007-10-30 21:30:58 +00:00
{
wxTextEntryDialog dlg( this, _( "Name:" ), _( "Save Module" ), footprintName );
2010-07-20 18:11:34 +00:00
if( dlg.ShowModal() != wxID_OK )
return false; // canceled by user
footprintName = dlg.GetValue();
footprintName.Trim( true );
footprintName.Trim( false );
if( footprintName.IsEmpty() )
return false;
aModule->SetLibRef( footprintName );
}
// Ensure this footprint has a libname
if( footprintName.IsEmpty() )
{
footprintName = wxT("noname");
aModule->SetLibRef( footprintName );
2007-10-30 21:30:58 +00:00
}
MODULE* module_exists = NULL;
2007-10-30 21:30:58 +00:00
try
2007-10-30 21:30:58 +00:00
{
PLUGIN::RELEASER pi( IO_MGR::PluginFind( IO_MGR::LEGACY ) );
2007-10-30 21:30:58 +00:00
module_exists = pi->FootprintLoad( aLibName, footprintName );
if( module_exists )
2007-10-30 21:30:58 +00:00
{
delete module_exists;
// an existing footprint is found in current lib
if( aDisplayDialog )
{
wxString msg = wxString::Format(
_( "Footprint '%s' already exists in library '%s'" ),
footprintName.GetData(), aLibName.GetData() );
SetStatusText( msg );
}
if( !aOverwrite )
2007-10-30 21:30:58 +00:00
{
// Do not save the given footprint: an old one exists
return true;
2007-10-30 21:30:58 +00:00
}
}
// this always overwrites any existing footprint
pi->FootprintSave( aLibName, aModule );
}
catch( IO_ERROR ioe )
2007-10-30 21:30:58 +00:00
{
DisplayError( this, ioe.errorText );
return false;
2007-10-30 21:30:58 +00:00
}
if( aDisplayDialog )
2007-10-30 21:30:58 +00:00
{
wxString fmt = module_exists ?
_( "Component [%s] replaced in <%s>" ) :
_( "Component [%s] added in <%s>" );
wxString msg = wxString::Format( fmt, footprintName.GetData(), aLibName.GetData() );
SetStatusText( msg );
2007-10-30 21:30:58 +00:00
}
return true;
2007-05-06 16:03:28 +00:00
}
MODULE* PCB_BASE_FRAME::Create_1_Module( const wxString& aModuleName )
2007-05-06 16:03:28 +00:00
{
2007-10-30 21:30:58 +00:00
MODULE* Module;
2010-07-20 18:11:34 +00:00
wxString moduleName;
2007-10-30 21:30:58 +00:00
wxPoint newpos;
2010-07-20 18:11:34 +00:00
moduleName = aModuleName;
// Ask for the new module reference
2010-07-20 18:11:34 +00:00
if( moduleName.IsEmpty() )
2007-10-30 21:30:58 +00:00
{
2010-07-20 18:11:34 +00:00
wxTextEntryDialog dlg( this, _( "Module Reference:" ),
_( "Module Creation" ), moduleName );
if( dlg.ShowModal() != wxID_OK )
2010-07-20 18:11:34 +00:00
return NULL; //Aborted by user
moduleName = dlg.GetValue();
2010-07-20 18:11:34 +00:00
}
2010-07-20 18:11:34 +00:00
moduleName.Trim( true );
moduleName.Trim( false );
2010-07-20 18:11:34 +00:00
if( moduleName.IsEmpty( ) )
{
DisplayInfoMessage( this, _( "No reference, aborted" ) );
return NULL;
2007-10-30 21:30:58 +00:00
}
// Creates the new module and add it to the head of the linked list of modules
Module = new MODULE( GetBoard() );
2007-10-30 21:30:58 +00:00
GetBoard()->Add( Module );
2007-10-30 21:30:58 +00:00
// Update parameters: position, timestamp ...
newpos = GetScreen()->GetCrossHairPosition();
2007-10-30 21:30:58 +00:00
Module->SetPosition( newpos );
Module->SetLastEditTime();
2007-10-30 21:30:58 +00:00
// Update its name in lib
2010-07-20 18:11:34 +00:00
Module->m_LibRef = moduleName;
2007-10-30 21:30:58 +00:00
// Update reference:
2010-07-20 18:11:34 +00:00
Module->m_Reference->m_Text = moduleName;
Module->m_Reference->SetThickness( GetDesignSettings().m_ModuleTextWidth );
Module->m_Reference->SetSize( GetDesignSettings().m_ModuleTextSize );
2007-10-30 21:30:58 +00:00
// Set the value field to a default value
2007-10-30 21:30:58 +00:00
Module->m_Value->m_Text = wxT( "VAL**" );
Module->m_Value->SetThickness( GetDesignSettings().m_ModuleTextWidth );
Module->m_Value->SetSize( GetDesignSettings().m_ModuleTextSize );
2007-10-30 21:30:58 +00:00
Module->SetPosition( wxPoint( 0, 0 ) );
Module->DisplayInfo( this );
2007-10-30 21:30:58 +00:00
return Module;
2007-05-06 16:03:28 +00:00
}
void FOOTPRINT_EDIT_FRAME::Select_Active_Library()
2007-05-06 16:03:28 +00:00
{
if( g_LibraryNames.GetCount() == 0 )
2007-10-30 21:30:58 +00:00
return;
2007-05-06 16:03:28 +00:00
EDA_LIST_DIALOG dlg( this, _( "Select Active Library:" ), g_LibraryNames, m_CurrentLib );
2007-05-06 16:03:28 +00:00
2010-11-19 18:50:23 +00:00
if( dlg.ShowModal() != wxID_OK )
return;
2007-05-06 16:03:28 +00:00
wxFileName fileName = wxFileName( wxEmptyString, dlg.GetTextSelection(),
FootprintLibFileExtension );
fileName = wxGetApp().FindLibraryPath( fileName );
2007-05-06 16:03:28 +00:00
if( fileName.IsOk() && fileName.FileExists() )
{
m_CurrentLib = dlg.GetTextSelection();
}
else
{
wxString msg = wxString::Format(
_( "The footprint library <%s> could not be found in any of the search paths." ),
GetChars( dlg.GetTextSelection() ) );
DisplayError( this, msg );
m_CurrentLib.Empty();
}
UpdateTitle();
2007-05-06 16:03:28 +00:00
}
int FOOTPRINT_EDIT_FRAME::CreateLibrary( const wxString& aLibName )
2007-05-06 16:03:28 +00:00
{
wxFileName fileName = aLibName;
2007-10-30 21:30:58 +00:00
if( fileName.FileExists() )
2007-10-30 21:30:58 +00:00
{
wxString msg = wxString::Format(
_( "Library <%s> already exists." ),
aLibName.GetData() );
2007-10-30 21:30:58 +00:00
DisplayError( this, msg );
return 0;
}
if( !IsWritable( fileName ) )
return 0;
try
2007-10-30 21:30:58 +00:00
{
PLUGIN::RELEASER pi( IO_MGR::PluginFind( IO_MGR::LEGACY ) );
2007-10-30 21:30:58 +00:00
pi->FootprintLibCreate( aLibName );
}
catch( IO_ERROR ioe )
{
DisplayError( this, ioe.errorText );
return 0;
}
2007-10-30 21:30:58 +00:00
return 1; // remember how many times we succeeded
2007-05-06 16:03:28 +00:00
}