kicad/pcbnew/load_select_footprint.cpp

516 lines
15 KiB
C++
Raw Normal View History

/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2018 Jean-Pierre Charras, jean-pierre.charras@ujf-grenoble.fr
* Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
* Copyright (C) 1992-2018 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
*/
#include <functional>
using namespace std::placeholders;
#include <class_board.h>
#include <class_module.h>
#include <confirm.h>
#include <connectivity/connectivity_data.h>
#include <dialog_choose_footprint.h>
#include <dialog_get_footprint_by_name.h>
#include <dialog_helpers.h>
#include <footprint_edit_frame.h>
#include <footprint_info_impl.h>
#include <footprint_tree_pane.h>
#include <footprint_viewer_frame.h>
#include <fp_lib_table.h>
#include <io_mgr.h>
#include <kicad_string.h>
#include <kiway.h>
#include <lib_id.h>
#include <macros.h>
#include <pcb_draw_panel_gal.h>
#include <pcb_edit_frame.h>
#include <pcbnew.h>
#include <pcbnew_settings.h>
#include <pgm_base.h>
#include <view/view_controls.h>
#include <widgets/lib_tree.h>
#include <widgets/progress_reporter.h>
#include "fp_tree_model_adapter.h"
2007-05-06 16:03:28 +00:00
static wxArrayString s_ModuleHistoryList;
static unsigned s_ModuleHistoryMaxCount = 8;
static void AddModuleToHistory( const wxString& aName )
{
// Remove duplicates
for( int ii = s_ModuleHistoryList.GetCount() - 1; ii >= 0; --ii )
{
if( s_ModuleHistoryList[ ii ] == aName )
s_ModuleHistoryList.RemoveAt( (size_t) ii );
}
// Add the new name at the beginning of the history list
s_ModuleHistoryList.Insert( aName, 0 );
// Remove extra names
while( s_ModuleHistoryList.GetCount() >= s_ModuleHistoryMaxCount )
s_ModuleHistoryList.RemoveAt( s_ModuleHistoryList.GetCount() - 1 );
}
2007-05-06 16:03:28 +00:00
2015-02-18 16:53:46 +00:00
static void clearModuleItemFlags( BOARD_ITEM* aItem )
{
aItem->ClearFlags();
}
#include <bitmaps.h>
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
{
bool is_last_fp_from_brd = IsCurrentFPFromBoard();
2011-12-07 05:28:49 +00:00
MODULE* newModule;
PCB_EDIT_FRAME* frame = (PCB_EDIT_FRAME*) Kiway().Player( FRAME_PCB_EDITOR, false );
if( frame == NULL ) // happens if no board editor opened
return false;
2007-05-06 16:03:28 +00:00
2011-12-07 05:28:49 +00:00
if( aModule == NULL )
{
if( !frame->GetBoard() || !frame->GetBoard()->GetFirstModule() )
return false;
2007-05-06 16:03:28 +00:00
aModule = SelectFootprintFromBoard( frame->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
if( !Clear_Pcb( true ) )
return false;
2007-05-06 16:03:28 +00:00
2020-02-20 12:11:04 +00:00
newModule = (MODULE*) aModule->Duplicate();
newModule->SetParent( GetBoard() );
2020-02-20 12:11:04 +00:00
newModule->SetLink( aModule->m_Uuid );
2007-05-06 16:03:28 +00:00
newModule->ClearFlags();
newModule->RunOnChildren( std::bind( &clearModuleItemFlags, _1 ) );
AddModuleToBoard( newModule );
2007-05-06 16:03:28 +00:00
// Clear references to any net info, because the footprint editor
// does know any thing about nets handled by the current edited board.
// Morever we do not want to save any reference to an unknown net when
// saving the footprint in lib cache
// so we force the ORPHANED dummy net info for all pads
newModule->ClearAllNets();
2007-05-06 16:03:28 +00:00
GetCanvas()->GetViewControls()->SetCrossHairCursorPosition( VECTOR2D( 0, 0 ), false );
PlaceModule( newModule );
newModule->SetPosition( wxPoint( 0, 0 ) ); // cursor in GAL may not be initialized at the moment
// Put it on FRONT layer,
// because this is the default in ModEdit, and in libs
if( newModule->GetLayer() != F_Cu )
newModule->Flip( newModule->GetPosition(), frame->Settings().m_FlipLeftRight );
// Put it in orientation 0,
// because this is the default orientation in ModEdit, and in libs
newModule->SetOrientation( 0 );
Zoom_Automatique( false );
m_adapter->SetPreselectNode( newModule->GetFPID(), 0 );
ClearUndoRedoList();
GetScreen()->ClrModify();
2019-06-03 23:50:44 +00:00
// Update the save items if needed.
if( !is_last_fp_from_brd )
2019-06-03 23:50:44 +00:00
{
ReCreateMenuBar();
ReCreateHToolbar();
if( IsSearchTreeShown() )
ToggleSearchTree();
2019-06-03 23:50:44 +00:00
}
2019-06-02 14:34:19 +00:00
Update3DView( true );
2019-05-27 16:16:54 +00:00
updateView();
GetCanvas()->Refresh();
m_treePane->GetLibTree()->RefreshLibTree(); // update any previously-highlighted items
return true;
2007-05-06 16:03:28 +00:00
}
wxString PCB_BASE_FRAME::SelectFootprintFromLibBrowser()
{
Modular-Kicad milestone B), major portions: *) Rework the set language support, simplify it by using KIWAY. Now any major frame with a "change language" menu can change the language for all KIWAY_PLAYERs in the whole KIWAY. Multiple KIWAYs are not supported yet. *) Simplify "modal wxFrame" support, and add that support exclusively to KIWAY_PLAYER where it is inherited by all derivatives. The function KIWAY_PLAYER::ShowModal() is in the vtable and so is cross module capable. *) Remove the requirements and assumptions that the wxFrame hierarchy always had PCB_EDIT_FRAME and SCH_EDIT_FRAME as immediate parents of their viewers and editors. This is no longer the case, nor required. *) Use KIWAY::Player() everywhere to make KIWAY_PLAYERs, this registers the KIWAY_PLAYER within the KIWAY and makes it very easy to find an open frame quickly. It also gives control to the KIWAY as to frame hierarchical relationships. *) Change single_top to use the KIWAY for loading a KIFACE and instantiating the single KIWAY_PLAYER, see bullet immediately above. *) Add KIWAY::OnKiwayEnd() and call it from PGM_BASE at program termination, this gives the KIFACEs a chance to save their final configuration dope to disk. *) Add dedicated FRAME_T's for the modal frames, so m_Ident can be tested and these modal frames are distinctly different than their non-modal equivalents. KIWAY_PLAYER::IsModal() is !not! a valid test during the wxFrame's constructor, so this is another important reason for having a dedicated FRAME_T for each modal wxFrame. On balance, more lines were deleted than were added to achieve all this.
2014-05-03 17:40:19 +00:00
// Close the current non-modal Lib browser if opened, and open a new one, in "modal" mode:
FOOTPRINT_VIEWER_FRAME* viewer;
viewer = (FOOTPRINT_VIEWER_FRAME*) Kiway().Player( FRAME_FOOTPRINT_VIEWER, false );
if( viewer )
{
viewer->Destroy();
// Destroy() does not immediately delete the viewer, if some events are pending.
// (for this reason delete operator cannot be used blindly with "top level" windows)
// so gives a slice of time to delete the viewer frame.
// This is especially important in OpenGL mode to avoid recreating context before
// the old one is deleted
wxSafeYield();
}
SetFocus();
// Creates the modal Lib browser:
viewer = (FOOTPRINT_VIEWER_FRAME*) Kiway().Player( FRAME_FOOTPRINT_VIEWER_MODAL, true, this );
Modular-Kicad milestone B), major portions: *) Rework the set language support, simplify it by using KIWAY. Now any major frame with a "change language" menu can change the language for all KIWAY_PLAYERs in the whole KIWAY. Multiple KIWAYs are not supported yet. *) Simplify "modal wxFrame" support, and add that support exclusively to KIWAY_PLAYER where it is inherited by all derivatives. The function KIWAY_PLAYER::ShowModal() is in the vtable and so is cross module capable. *) Remove the requirements and assumptions that the wxFrame hierarchy always had PCB_EDIT_FRAME and SCH_EDIT_FRAME as immediate parents of their viewers and editors. This is no longer the case, nor required. *) Use KIWAY::Player() everywhere to make KIWAY_PLAYERs, this registers the KIWAY_PLAYER within the KIWAY and makes it very easy to find an open frame quickly. It also gives control to the KIWAY as to frame hierarchical relationships. *) Change single_top to use the KIWAY for loading a KIFACE and instantiating the single KIWAY_PLAYER, see bullet immediately above. *) Add KIWAY::OnKiwayEnd() and call it from PGM_BASE at program termination, this gives the KIFACEs a chance to save their final configuration dope to disk. *) Add dedicated FRAME_T's for the modal frames, so m_Ident can be tested and these modal frames are distinctly different than their non-modal equivalents. KIWAY_PLAYER::IsModal() is !not! a valid test during the wxFrame's constructor, so this is another important reason for having a dedicated FRAME_T for each modal wxFrame. On balance, more lines were deleted than were added to achieve all this.
2014-05-03 17:40:19 +00:00
wxString fpid;
int ret = viewer->ShowModal( &fpid, this );
(void) ret; // make static analyser quiet
viewer->Destroy();
return fpid;
}
MODULE* PCB_BASE_FRAME::SelectFootprintFromLibTree( LIB_ID aPreselect )
2007-05-06 16:03:28 +00:00
{
FP_LIB_TABLE* fpTable = Prj().PcbFootprintLibs();
wxString moduleName;
LIB_ID fpid;
MODULE* module = NULL;
2011-12-07 05:28:49 +00:00
static wxString lastComponentName;
// Load footprint files:
WX_PROGRESS_REPORTER* progressReporter = new WX_PROGRESS_REPORTER( this,
_( "Loading Footprint Libraries" ), 3 );
GFootprintList.ReadFootprintFiles( fpTable, nullptr, progressReporter );
bool cancel = progressReporter->WasCancelled();
progressReporter->Destroy();
if( cancel )
return nullptr;
if( GFootprintList.GetErrorCount() )
GFootprintList.DisplayErrors( this );
auto adapterPtr( FP_TREE_MODEL_ADAPTER::Create( this, fpTable ) );
auto adapter = static_cast<FP_TREE_MODEL_ADAPTER*>( adapterPtr.get() );
std::vector<LIB_TREE_ITEM*> historyInfos;
for( auto const& item : s_ModuleHistoryList )
{
LIB_TREE_ITEM* fp_info = GFootprintList.GetModuleInfo( item );
// this can be null, for example, if the footprint has been deleted from a library.
if( fp_info != nullptr )
historyInfos.push_back( fp_info );
}
adapter->DoAddLibrary( "-- " + _( "Recently Used" ) + " --", wxEmptyString, historyInfos, true );
if( aPreselect.IsValid() )
adapter->SetPreselectNode( aPreselect, 0 );
else if( historyInfos.size() )
adapter->SetPreselectNode( historyInfos[0]->GetLibId(), 0 );
adapter->AddLibraries();
wxString title;
title.Printf( _( "Choose Footprint (%d items loaded)" ), adapter->GetItemCount() );
DIALOG_CHOOSE_FOOTPRINT dialog( this, title, adapterPtr );
if( dialog.ShowQuasiModal() == wxID_CANCEL )
return NULL;
if( dialog.IsExternalBrowserSelected() )
{
// SelectFootprintFromLibBrowser() returns the "full" footprint name, i.e.
// <lib_name>/<footprint name> or LIB_ID format "lib_name:fp_name:rev#"
moduleName = SelectFootprintFromLibBrowser();
if( moduleName.IsEmpty() ) // Cancel command
return NULL;
else
fpid.Parse( moduleName, LIB_ID::ID_PCB );
}
else
{
fpid = dialog.GetSelectedLibId();
if( !fpid.IsValid() )
return NULL;
else
moduleName = fpid.Format();
}
try
{
module = loadFootprint( fpid );
}
catch( const IO_ERROR& ioe )
{
wxLogDebug( wxT( "Error loading footprint '%s'.\n\nError: %s" ),
fpid.Format().c_str(),
ioe.What() );
}
if( module )
{
lastComponentName = moduleName;
AddModuleToHistory( moduleName );
}
return module;
}
MODULE* PCB_BASE_FRAME::LoadFootprint( const LIB_ID& aFootprintId )
{
MODULE* module = NULL;
try
{
module = loadFootprint( aFootprintId );
}
catch( const IO_ERROR& ioe )
{
wxLogDebug( wxT( "An error occurred attemping to load footprint '%s'.\n\nError: %s" ),
aFootprintId.Format().c_str(), GetChars( ioe.What() ) );
}
return module;
}
MODULE* PCB_BASE_FRAME::loadFootprint( const LIB_ID& aFootprintId )
{
FP_LIB_TABLE* fptbl = Prj().PcbFootprintLibs();
* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
2014-03-20 00:42:08 +00:00
wxCHECK_MSG( fptbl, NULL, wxT( "Cannot look up LIB_ID in NULL FP_LIB_TABLE." ) );
MODULE *module = nullptr;
try
{
module = fptbl->FootprintLoadWithOptionalNickname( aFootprintId );
}
catch( const IO_ERROR& ioe )
{
wxLogDebug( wxT( "An error occurred attemping to load footprint '%s'.\n\nError: %s" ),
aFootprintId.Format().c_str(), GetChars( ioe.What() ) );
}
// If the module is found, clear all net info,
// to be sure there is no broken links
// to any netinfo list (should be not needed, but it can be edited from
// the footprint editor )
if( module )
module->ClearAllNets();
return module;
2007-05-06 16:03:28 +00:00
}
MODULE* FOOTPRINT_EDIT_FRAME::SelectFootprintFromBoard( BOARD* aPcb )
2007-05-06 16:03:28 +00:00
{
static wxString oldName; // Save name of last module selected.
wxString fpname;
wxString msg;
wxArrayString listnames;
for( auto module : aPcb->Modules() )
listnames.Add( module->GetReference() );
msg.Printf( _( "Footprints [%u items]" ), (unsigned) listnames.GetCount() );
wxArrayString headers;
headers.Add( _( "Footprint" ) );
std::vector<wxArrayString> itemsToDisplay;
// Conversion from wxArrayString to vector of ArrayString
for( unsigned i = 0; i < listnames.GetCount(); i++ )
{
wxArrayString item;
item.Add( listnames[i] );
itemsToDisplay.push_back( item );
}
EDA_LIST_DIALOG dlg( this, msg, headers, itemsToDisplay, wxEmptyString );
2010-11-19 18:50:23 +00:00
if( dlg.ShowModal() == wxID_OK )
fpname = dlg.GetTextSelection();
else
return NULL;
oldName = fpname;
for( auto mod : aPcb->Modules() )
{
if( fpname == mod->GetReference() )
return mod;
}
return nullptr;
2007-05-06 16:03:28 +00:00
}
bool FOOTPRINT_EDIT_FRAME::SaveLibraryAs( const wxString& aLibraryPath )
{
const wxString& curLibPath = aLibraryPath;
wxString dstLibPath = CreateNewLibrary();
if( !dstLibPath )
return false; // user aborted in CreateNewLibrary()
wxBusyCursor dummy;
wxString msg;
IO_MGR::PCB_FILE_T dstType = IO_MGR::GuessPluginTypeFromLibPath( dstLibPath );
IO_MGR::PCB_FILE_T curType = IO_MGR::GuessPluginTypeFromLibPath( curLibPath );
try
{
PLUGIN::RELEASER cur( IO_MGR::PluginFind( curType ) );
PLUGIN::RELEASER dst( IO_MGR::PluginFind( dstType ) );
wxArrayString footprints;
cur->FootprintEnumerate( footprints, curLibPath, false );
for( unsigned i = 0; i < footprints.size(); ++i )
{
const MODULE* footprint = cur->GetEnumeratedFootprint( curLibPath, footprints[i] );
dst->FootprintSave( dstLibPath, footprint );
msg = wxString::Format( _( "Footprint \"%s\" saved" ), footprints[i] );
SetStatusText( msg );
}
}
catch( const IO_ERROR& ioe )
{
DisplayError( this, ioe.What() );
return false;
}
msg = wxString::Format( _( "Footprint library \"%s\" saved as \"%s\"." ),
curLibPath,
dstLibPath );
DisplayInfoMessage( this, msg );
SetStatusText( wxEmptyString );
return true;
}
static MODULE* s_ModuleInitialCopy = NULL; // Copy of module for abort/undo command
static PICKED_ITEMS_LIST s_PickedList; // a pick-list to save initial module
// and dragged tracks
MODULE* PCB_BASE_FRAME::GetFootprintFromBoardByReference()
{
wxString moduleName;
wxArrayString fplist;
// Build list of available fp references, to display them in dialog
for( auto fp : GetBoard()->Modules() )
fplist.Add( fp->GetReference() + wxT(" ( ") + fp->GetValue() + wxT(" )") );
fplist.Sort();
DIALOG_GET_FOOTPRINT_BY_NAME dlg( this, fplist );
if( dlg.ShowModal() != wxID_OK ) //Aborted by user
return NULL;
moduleName = dlg.GetValue();
moduleName.Trim( true );
moduleName.Trim( false );
if( !moduleName.IsEmpty() )
{
for( auto mod : GetBoard()->Modules() )
{
if( mod->GetReference().CmpNoCase( moduleName ) == 0 )
return mod;
}
}
return nullptr;
}
void PCB_BASE_FRAME::PlaceModule( MODULE* aModule, bool aRecreateRatsnest )
{
if( aModule == 0 )
return;
OnModify();
if( aModule->IsNew() )
{
SaveCopyInUndoList( aModule, UR_NEW );
}
else if( aModule->IsMoving() )
{
ITEM_PICKER picker( nullptr, aModule, UR_CHANGED );
picker.SetLink( s_ModuleInitialCopy );
s_PickedList.PushItem( picker );
s_ModuleInitialCopy = NULL; // the picker is now owner of s_ModuleInitialCopy.
}
if( s_PickedList.GetCount() )
{
SaveCopyInUndoList( s_PickedList, UR_UNSPECIFIED );
// Clear list, but DO NOT delete items, because they are owned by the saved undo
// list and they therefore in use
s_PickedList.ClearItemsList();
}
aModule->SetPosition( (wxPoint) GetCanvas()->GetViewControls()->GetCursorPosition() );
aModule->ClearFlags();
delete s_ModuleInitialCopy;
s_ModuleInitialCopy = NULL;
if( aRecreateRatsnest )
m_Pcb->GetConnectivity()->Update( aModule );
if( aRecreateRatsnest )
2019-05-30 15:11:17 +00:00
Compile_Ratsnest( true );
SetMsgPanel( aModule );
}