kicad/pcbnew/loadcmp.cpp

445 lines
13 KiB
C++
Raw Normal View History

/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2009 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
* Copyright (C) 1992-2011 KiCad Developers, see AUTHORS.txt for contributors.
*
* 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, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
/**
* @file pcbnew/loadcmp.cpp
* @brief Footprints selection and loading functions.
*/
2007-05-06 16:03:28 +00:00
#include "fctsys.h"
#include "class_drawpanel.h"
#include "confirm.h"
#include "eda_doc.h"
#include "kicad_string.h"
#include "appl_wxstruct.h"
2009-07-30 11:04:07 +00:00
#include "wxPcbStruct.h"
2010-11-18 21:16:28 +00:00
#include "dialog_helpers.h"
#include "filter_reader.h"
#include "gr_basic.h"
#include "macros.h"
#include "pcbcommon.h"
#include "class_board.h"
#include "class_module.h"
#include "pcbnew.h"
#include "module_editor_frame.h"
#include "footprint_info.h"
#include "class_footprint_library.h"
#include "dialog_get_component.h"
2007-05-06 16:03:28 +00:00
static void DisplayCmpDoc( wxString& Name );
2007-05-06 16:03:28 +00:00
static FOOTPRINT_LIST MList;
2007-05-06 16:03:28 +00:00
2011-12-07 05:28:49 +00:00
bool FOOTPRINT_EDIT_FRAME::Load_Module_From_BOARD( MODULE* aModule )
2007-05-06 16:03:28 +00:00
{
2011-12-07 05:28:49 +00:00
MODULE* newModule;
PCB_BASE_FRAME* parent = (PCB_BASE_FRAME*) GetParent();
2007-05-06 16:03:28 +00:00
2011-12-07 05:28:49 +00:00
if( aModule == NULL )
{
if( ! parent->GetBoard() || ! parent->GetBoard()->m_Modules )
return false;
2007-05-06 16:03:28 +00:00
2011-12-07 05:28:49 +00:00
aModule = Select_1_Module_From_BOARD( parent->GetBoard() );
}
2007-05-06 16:03:28 +00:00
2011-12-07 05:28:49 +00:00
if( aModule == NULL )
return false;
2007-05-06 16:03:28 +00:00
SetCurItem( NULL );
2007-05-06 16:03:28 +00:00
Clear_Pcb( false );
2007-05-06 16:03:28 +00:00
GetBoard()->m_Status_Pcb = 0;
2011-12-07 05:28:49 +00:00
newModule = new MODULE( GetBoard() );
newModule->Copy( aModule );
newModule->m_Link = aModule->m_TimeStamp;
2007-05-06 16:03:28 +00:00
2011-12-07 05:28:49 +00:00
aModule = newModule;
2011-12-07 05:28:49 +00:00
GetBoard()->Add( aModule );
2007-05-06 16:03:28 +00:00
2011-12-07 05:28:49 +00:00
aModule->m_Flags = 0;
2007-05-06 16:03:28 +00:00
GetBoard()->m_NetInfo->BuildListOfNets();
2007-05-06 16:03:28 +00:00
GetScreen()->SetCrossHairPosition( wxPoint( 0, 0 ) );
2011-12-07 05:28:49 +00:00
PlaceModule( aModule, NULL );
2011-12-07 05:28:49 +00:00
if( aModule->GetLayer() != LAYER_N_FRONT )
aModule->Flip( aModule->m_Pos );
2011-12-07 05:28:49 +00:00
Rotate_Module( NULL, aModule, 0, false );
GetScreen()->ClrModify();
Zoom_Automatique( false );
return true;
2007-05-06 16:03:28 +00:00
}
MODULE* PCB_BASE_FRAME::Load_Module_From_Library( const wxString& library, wxDC* DC )
2007-05-06 16:03:28 +00:00
{
2011-12-07 05:28:49 +00:00
MODULE* module;
wxPoint curspos = GetScreen()->GetCrossHairPosition();
wxString moduleName, keys;
bool AllowWildSeach = true;
static wxArrayString HistoryList;
static wxString lastCommponentName;
/* Ask for a component name or key words */
DIALOG_GET_COMPONENT dlg( this, GetComponentDialogPosition(), HistoryList,
_( "Place Module" ), false );
dlg.SetComponentName( lastCommponentName );
if( dlg.ShowModal() == wxID_CANCEL )
return NULL;
2011-12-07 05:28:49 +00:00
moduleName = dlg.GetComponentName();
2011-12-07 05:28:49 +00:00
if( moduleName.IsEmpty() ) /* Cancel command */
{
DrawPanel->MoveCursorToCrossHair();
return NULL;
}
2011-12-07 05:28:49 +00:00
moduleName.MakeUpper();
2011-12-07 05:28:49 +00:00
if( moduleName[0] == '=' ) // Selection by keywords
{
AllowWildSeach = false;
2011-12-07 05:28:49 +00:00
keys = moduleName.AfterFirst( '=' );
moduleName = Select_1_Module_From_List( this, library, wxEmptyString, keys );
2011-12-07 05:28:49 +00:00
if( moduleName.IsEmpty() ) /* Cancel command */
{
DrawPanel->MoveCursorToCrossHair();
return NULL;
}
}
2011-12-07 05:28:49 +00:00
else if( ( moduleName.Contains( wxT( "?" ) ) )
|| ( moduleName.Contains( wxT( "*" ) ) ) ) // Selection wild card
{
AllowWildSeach = false;
2011-12-07 05:28:49 +00:00
moduleName = Select_1_Module_From_List( this, library, moduleName, wxEmptyString );
if( moduleName.IsEmpty() )
{
DrawPanel->MoveCursorToCrossHair();
return NULL; /* Cancel command. */
}
}
2011-12-07 05:28:49 +00:00
module = GetModuleLibrary( library, moduleName, false );
if( ( module == NULL ) && AllowWildSeach ) /* Search with wild card */
{
AllowWildSeach = false;
2011-12-07 05:28:49 +00:00
wxString wildname = wxChar( '*' ) + moduleName + wxChar( '*' );
moduleName = wildname;
moduleName = Select_1_Module_From_List( this, library, moduleName, wxEmptyString );
2011-12-07 05:28:49 +00:00
if( moduleName.IsEmpty() )
{
DrawPanel->MoveCursorToCrossHair();
return NULL; /* Cancel command. */
}
else
{
2011-12-07 05:28:49 +00:00
module = GetModuleLibrary( library, moduleName, true );
}
}
GetScreen()->SetCrossHairPosition( curspos );
DrawPanel->MoveCursorToCrossHair();
if( module )
{
2011-12-07 05:28:49 +00:00
lastCommponentName = moduleName;
AddHistoryComponentName( HistoryList, moduleName );
module->m_Flags = IS_NEW;
module->m_Link = 0;
module->m_TimeStamp = GetNewTimeStamp();
GetBoard()->m_Status_Pcb = 0;
module->SetPosition( curspos );
/* TODO: call RecalculateAllTracksNetcode() only if some pads pads have
* a netname.
* If all pads are not connected (usually the case in module libraries,
* rebuild only the pad and list of nets ( faster)
*/
// GetBoard()->m_Pcb->m_NetInfo->BuildListOfNets();
RecalculateAllTracksNetcode();
if( DC )
2009-05-21 12:45:21 +00:00
module->Draw( DrawPanel, DC, GR_OR );
}
return module;
2007-05-06 16:03:28 +00:00
}
MODULE* PCB_BASE_FRAME::GetModuleLibrary( const wxString& aLibraryFullFilename,
const wxString& aModuleName,
bool aDisplayMessageError )
2007-05-06 16:03:28 +00:00
{
wxFileName fn;
wxString msg, tmp;
2011-12-07 05:28:49 +00:00
MODULE* newModule;
FILE* file = NULL;
2011-12-07 05:28:49 +00:00
bool one_lib = aLibraryFullFilename.IsEmpty() ? false : true;
2011-12-07 05:28:49 +00:00
for( unsigned ii = 0; ii < g_LibraryNames.GetCount(); ii++ )
{
if( one_lib )
fn = aLibraryFullFilename;
else
fn = wxFileName( wxEmptyString, g_LibraryNames[ii], ModuleFileExtension );
tmp = wxGetApp().FindLibraryPath( fn );
if( !tmp )
{
if( aDisplayMessageError )
{
msg.Printf( _( "PCB footprint library file <%s> not found in search paths." ),
GetChars( fn.GetFullName() ) );
wxMessageBox( msg, _( "Library Load Error" ), wxOK | wxICON_ERROR, this );
}
continue;
}
file = wxFopen( tmp, wxT( "rt" ) );
if( file == NULL )
{
msg.Printf( _( "Could not open PCB footprint library file <%s>." ),
GetChars( tmp ) );
wxMessageBox( msg, _( "Library Load Error" ), wxOK | wxICON_ERROR, this );
continue;
}
FILE_LINE_READER fileReader( file, tmp );
FILTER_READER reader( fileReader );
msg.Printf( _( "Scan Lib: %s" ), GetChars( tmp ) );
SetStatusText( msg );
FOOTPRINT_LIBRARY curr_lib( file, &reader );
if( !curr_lib.IsLibrary() )
{
msg.Printf( _( "<%s> is not a valid KiCad PCB footprint library file." ),
GetChars( tmp ) );
wxMessageBox( msg, _( "Library Load Error" ), wxOK | wxICON_ERROR, this );
return NULL;
}
2011-12-07 05:28:49 +00:00
// Reading the list of modules in the library.
curr_lib.ReadSectionIndex();
bool found = curr_lib.FindInList( aModuleName );
2011-12-07 05:28:49 +00:00
// Read library.
if( found )
{
2011-12-07 05:28:49 +00:00
wxString name;
fileReader.Rewind();
while( reader.ReadLine() )
{
2011-12-07 05:28:49 +00:00
char* line = reader.Line();
StrPurge( line + 8 );
2011-01-16 05:11:17 +00:00
if( strnicmp( line, "$MODULE", 7 ) != 0 )
continue;
2011-01-16 05:11:17 +00:00
// Read module name.
2011-12-07 05:28:49 +00:00
name = FROM_UTF8( line + 8 );
2011-12-07 05:28:49 +00:00
if( name.CmpNoCase( aModuleName ) == 0 )
{
2011-12-07 05:28:49 +00:00
newModule = new MODULE( GetBoard() );
2011-12-07 05:28:49 +00:00
// Temporarily switch the locale to standard C (needed to print
// floating point numbers like 1.3)
2011-12-07 05:28:49 +00:00
LOCALE_IO toggle;
newModule->ReadDescr( &reader );
GetBoard()->Add( newModule, ADD_APPEND );
SetStatusText( wxEmptyString );
2011-12-07 05:28:49 +00:00
return newModule;
}
}
}
if( one_lib )
break;
}
if( aDisplayMessageError )
{
msg.Printf( _( "Module <%s> not found" ), GetChars( aModuleName ) );
DisplayError( NULL, msg );
}
return NULL;
2007-05-06 16:03:28 +00:00
}
wxString PCB_BASE_FRAME::Select_1_Module_From_List( EDA_DRAW_FRAME* aWindow,
const wxString& aLibraryFullFilename,
const wxString& aMask,
const wxString& aKeyWord )
2007-05-06 16:03:28 +00:00
{
static wxString OldName; /* Save the name of the last module loaded. */
2011-02-08 18:39:41 +00:00
wxString CmpName;
wxString msg;
wxArrayString libnames_list;
if( aLibraryFullFilename.IsEmpty() )
libnames_list = g_LibraryNames;
else
libnames_list.Add( aLibraryFullFilename );
/* Find modules in libraries. */
MList.ReadFootprintFiles( libnames_list );
wxArrayString footprint_names_list;
2011-05-09 19:44:06 +00:00
if( !aKeyWord.IsEmpty() ) // Create a list of modules found by keyword.
{
for( unsigned ii = 0; ii < MList.GetCount(); ii++ )
{
if( KeyWordOk( aKeyWord, MList.GetItem(ii).m_KeyWord ) )
footprint_names_list.Add( MList.GetItem(ii).m_Module );
}
}
2011-05-09 19:44:06 +00:00
else if( !aMask.IsEmpty() ) // Create a list of modules found by pattern
{
for( unsigned ii = 0; ii < MList.GetCount(); ii++ )
{
wxString& candidate = MList.GetItem(ii).m_Module;
2011-05-09 19:44:06 +00:00
if( WildCompareString( aMask, candidate, false ) )
footprint_names_list.Add( candidate );
}
}
else // Create the full list of modules
{
for( unsigned ii = 0; ii < MList.GetCount(); ii++ )
footprint_names_list.Add( MList.GetItem(ii).m_Module );
}
if( footprint_names_list.GetCount() )
{
msg.Printf( _( "Modules [%d items]" ), footprint_names_list.GetCount() );
EDA_LIST_DIALOG dlg( aWindow, msg, footprint_names_list, OldName,
DisplayCmpDoc, GetComponentDialogPosition() );
if( dlg.ShowModal() == wxID_OK )
CmpName = dlg.GetTextSelection();
else
CmpName.Empty();
}
else
{
DisplayError( aWindow, _( "No footprint found" ) );
CmpName.Empty();
}
if( CmpName != wxEmptyString )
OldName = CmpName;
return CmpName;
2007-05-06 16:03:28 +00:00
}
/* Find and display the doc Component Name
* The list of doc is pointed to by mlist.
*/
static void DisplayCmpDoc( wxString& Name )
2007-05-06 16:03:28 +00:00
{
FOOTPRINT_INFO* module_info = MList.GetModuleInfo( Name );
if( !module_info )
{
Name.Empty();
return;
}
Name = module_info->m_Doc.IsEmpty() ? wxT( "No Doc" ) : module_info->m_Doc;
Name += wxT( "\nKeyW: " );
Name += module_info->m_KeyWord.IsEmpty() ? wxT( "No Keyword" ) : module_info->m_KeyWord;
2007-05-06 16:03:28 +00:00
}
MODULE* FOOTPRINT_EDIT_FRAME::Select_1_Module_From_BOARD( BOARD* aPcb )
2007-05-06 16:03:28 +00:00
{
2011-12-07 05:28:49 +00:00
MODULE* module;
static wxString OldName; /* Save name of last module selected. */
wxString CmpName, msg;
2010-11-19 18:50:23 +00:00
wxArrayString listnames;
2011-12-07 05:28:49 +00:00
module = aPcb->m_Modules;
2011-12-07 05:28:49 +00:00
for( ; module != NULL; module = (MODULE*) module->Next() )
listnames.Add( module->m_Reference->m_Text );
2010-11-19 18:50:23 +00:00
msg.Printf( _( "Modules [%d items]" ), listnames.GetCount() );
EDA_LIST_DIALOG dlg( this, msg, listnames, wxEmptyString );
2010-11-19 18:50:23 +00:00
dlg.SortList();
2010-11-19 18:50:23 +00:00
if( dlg.ShowModal() == wxID_OK )
CmpName = dlg.GetTextSelection();
else
return NULL;
OldName = CmpName;
2011-12-07 05:28:49 +00:00
module = aPcb->m_Modules;
2011-12-07 05:28:49 +00:00
for( ; module != NULL; module = (MODULE*) module->Next() )
{
2011-12-07 05:28:49 +00:00
if( CmpName == module->m_Reference->m_Text )
break;
}
2011-12-07 05:28:49 +00:00
return module;
2007-05-06 16:03:28 +00:00
}