kicad/pcbnew/librairi.cpp

566 lines
15 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 <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 <legacy_plugin.h> // temporarily, for LoadMODULE()
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 = 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* file = wxFopen( dlg.GetPath(), wxT( "rt" ) );
if( file == NULL )
2007-10-30 21:30:58 +00:00
{
wxString msg;
msg.Printf( _( "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 );
}
LOCALE_IO toggle;
FILE_LINE_READER fileReader( file, dlg.GetPath() );
FILTER_READER reader( fileReader );
// Read header and test file type
reader.ReadLine();
char* line = reader.Line();
bool footprint_Is_GPCB_Format = false;
if( strnicmp( line, FOOTPRINT_LIBRARY_HEADER, FOOTPRINT_LIBRARY_HEADER_CNT ) != 0 )
2007-10-30 21:30:58 +00:00
{
if( strnicmp( line, "Element", 7 ) == 0 )
{
footprint_Is_GPCB_Format = true;
}
else
{
DisplayError( this, _( "Not a module file" ) );
return NULL;
}
2007-10-30 21:30:58 +00:00
}
// Read file: Search the description starting line (skip lib header)
if( !footprint_Is_GPCB_Format )
{
while( reader.ReadLine() )
{
if( strnicmp( line, "$MODULE", 7 ) == 0 )
break;
}
}
2007-10-30 21:30:58 +00:00
MODULE* module;
2007-10-30 21:30:58 +00:00
if( footprint_Is_GPCB_Format )
{
// @todo GEDA plugin
module = new MODULE( GetBoard() );
module->Read_GPCB_Descr( dlg.GetPath() );
}
else
{
try
{
PLUGIN::RELEASER pi( IO_MGR::PluginFind( IO_MGR::LEGACY ) );
LEGACY_PLUGIN* lp = (LEGACY_PLUGIN*)(PLUGIN*)pi;
lp->SetReader( &reader );
module = lp->LoadMODULE();
}
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 );
fn.SetExt( aCreateSysLib ? FootprintLibFileExtension : ModExportFileExtension );
2007-10-30 21:30:58 +00:00
2010-12-29 17:47:32 +00:00
if( aCreateSysLib )
path = wxGetApp().ReturnLastVisitedLibraryPath();
else if( config )
config->Read( EXPORT_IMPORT_LASTPATH_KEY, &path );
2008-03-12 11:49:16 +00:00
2010-12-29 17:47:32 +00:00
title = aCreateSysLib ? _( "Create New Library" ) : _( "Export Module" );
wildcard = aCreateSysLib ? FootprintLibFileWildcard : ModExportFileWildcard;
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();
wxGetApp().SaveLastVisitedLibraryPath( fn.GetPath() );
2007-10-30 21:30:58 +00:00
if( !aCreateSysLib && 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
wxString libPath = fn.GetFullPath();
try
{
// @todo : hard code this as IO_MGR::KICAD plugin, what would be the reason to "export"
// any other single footprint type, with clipboard support coming?
// Use IO_MGR::LEGACY for now, until the IO_MGR::KICAD plugin is ready.
PLUGIN::RELEASER pi( IO_MGR::PluginFind( IO_MGR::LEGACY ) );
2011-01-16 05:11:17 +00:00
try
{
// try to delete the library whether it exists or not, quietly.
pi->FootprintLibDelete( libPath );
}
catch( IO_ERROR ioe )
{
// Ignore this, it will often happen and is not an error because
// the library may not exist. If library was in a read only directory,
// it will still exist as we get to the FootprintLibCreate() below.
}
pi->FootprintLibCreate( libPath );
pi->FootprintSave( libPath, aModule );
}
catch( IO_ERROR ioe )
{
DisplayError( this, ioe.errorText );
return;
}
msg.Printf( _( "Module exported in file <%s>" ), libPath.GetData() );
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( FootprintLibFileWildcard ),
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->m_LastEdit_Time = time( NULL );
// 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
{
wxString msg;
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
{
msg.Printf( _( "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
}