kicad/pcbnew/load_select_footprint.cpp

470 lines
14 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-2020 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 <board.h>
#include <footprint.h>
#include <confirm.h>
#include <connectivity/connectivity_data.h>
#include <dialog_footprint_chooser.h>
#include <dialog_get_footprint_by_name.h>
#include <eda_list_dialog.h>
#include <footprint_edit_frame.h>
#include <footprint_tree_pane.h>
#include <footprint_viewer_frame.h>
#include <fp_lib_table.h>
#include <io_mgr.h>
#include <string_utils.h>
#include <kiway.h>
#include <lib_id.h>
#include <macros.h>
#include <pcb_edit_frame.h>
#include <pcbnew_settings.h>
#include <board_design_settings.h>
#include <view/view_controls.h>
#include <widgets/lib_tree.h>
#include <widgets/wx_progress_reporters.h>
#include <dialog_pad_properties.h>
#include <project_pcb.h>
2020-11-07 14:31:50 +00:00
static wxArrayString s_FootprintHistoryList;
static unsigned s_FootprintHistoryMaxCount = 8;
2020-11-07 14:31:50 +00:00
static void AddFootprintToHistory( const wxString& aName )
{
// Remove duplicates
2020-11-07 14:31:50 +00:00
for( int ii = s_FootprintHistoryList.GetCount() - 1; ii >= 0; --ii )
{
2020-11-07 14:31:50 +00:00
if( s_FootprintHistoryList[ ii ] == aName )
s_FootprintHistoryList.RemoveAt((size_t) ii );
}
// Add the new name at the beginning of the history list
2020-11-07 14:31:50 +00:00
s_FootprintHistoryList.Insert( aName, 0 );
// Remove extra names
2020-11-07 14:31:50 +00:00
while( s_FootprintHistoryList.GetCount() >= s_FootprintHistoryMaxCount )
s_FootprintHistoryList.RemoveAt( s_FootprintHistoryList.GetCount() - 1 );
}
2007-05-06 16:03:28 +00:00
#include <bitmaps.h>
2020-11-13 15:15:52 +00:00
bool FOOTPRINT_EDIT_FRAME::LoadFootprintFromBoard( FOOTPRINT* aFootprint )
2007-05-06 16:03:28 +00:00
{
bool is_last_fp_from_brd = IsCurrentFPFromBoard();
2020-11-13 15:15:52 +00:00
FOOTPRINT* newFootprint = nullptr;
PCB_EDIT_FRAME* frame = (PCB_EDIT_FRAME*) Kiway().Player( FRAME_PCB_EDITOR, false );
if( frame == nullptr ) // happens if no board editor opened
return false;
2007-05-06 16:03:28 +00:00
if( aFootprint == nullptr )
{
2020-11-08 21:29:04 +00:00
if( !frame->GetBoard() || !frame->GetBoard()->GetFirstFootprint() )
return false;
2007-05-06 16:03:28 +00:00
2020-11-07 14:31:50 +00:00
aFootprint = SelectFootprintFromBoard( frame->GetBoard() );
}
2007-05-06 16:03:28 +00:00
if( aFootprint == nullptr )
return false;
2007-05-06 16:03:28 +00:00
// Ensure we do not have the pad editor open (that is apseudo modal dlg).
// LoadFootprintFromBoard() can be called from the board editor, and we must ensure
// no footprint item is currently in edit
if( wxWindow::FindWindowByName( PAD_PROPERTIES_DLG_NAME ) )
wxWindow::FindWindowByName( PAD_PROPERTIES_DLG_NAME )->Close();
if( !Clear_Pcb( true ) )
return false;
2007-05-06 16:03:28 +00:00
m_boardFootprintUuids.clear();
auto recordAndUpdateUuid =
[&]( BOARD_ITEM* aItem )
{
KIID newId;
m_boardFootprintUuids[ newId ] = aItem->m_Uuid;
const_cast<KIID&>( aItem->m_Uuid ) = newId;
};
newFootprint = (FOOTPRINT*) aFootprint->Clone(); // Keep existing uuids
2020-11-07 14:31:50 +00:00
newFootprint->SetParent( GetBoard() );
newFootprint->SetParentGroup( nullptr );
2020-11-07 14:31:50 +00:00
newFootprint->SetLink( aFootprint->m_Uuid );
2007-05-06 16:03:28 +00:00
2020-11-07 14:31:50 +00:00
newFootprint->ClearFlags();
recordAndUpdateUuid( newFootprint );
newFootprint->RunOnDescendants(
[&]( BOARD_ITEM* aItem )
{
if( aItem->Type() == PCB_PAD_T )
aItem->SetLocked( false );
aItem->ClearFlags();
recordAndUpdateUuid( aItem );
} );
AddFootprintToBoard( newFootprint );
2007-05-06 16:03:28 +00:00
2021-01-09 12:08:04 +00:00
// Clear references to any net info, because the footprint editor does know any thing about
// nets handled by the current edited board.
// Moreover we do not want to save any reference to an unknown net when saving the footprint
2021-01-09 12:08:04 +00:00
// in lib cache so we force the ORPHANED dummy net info for all pads.
2020-11-07 14:31:50 +00:00
newFootprint->ClearAllNets();
2007-05-06 16:03:28 +00:00
GetCanvas()->GetViewControls()->SetCrossHairCursorPosition( VECTOR2D( 0, 0 ), false );
2020-11-10 21:20:03 +00:00
PlaceFootprint( newFootprint );
newFootprint->SetPosition( VECTOR2I( 0, 0 ) ); // cursor in GAL may not yet be initialized
// Put it on FRONT layer,
2020-11-16 00:45:43 +00:00
// because this is the default in Footprint Editor, and in libs
2020-11-07 14:31:50 +00:00
if( newFootprint->GetLayer() != F_Cu )
newFootprint->Flip( newFootprint->GetPosition(), frame->GetPcbNewSettings()->m_FlipLeftRight );
// Put it in orientation 0,
2020-11-16 00:45:43 +00:00
// because this is the default orientation in Footprint Editor, and in libs
2022-01-13 17:27:36 +00:00
newFootprint->SetOrientation( ANGLE_0 );
Zoom_Automatique( false );
2020-11-07 14:31:50 +00:00
m_adapter->SetPreselectNode( newFootprint->GetFPID(), 0 );
ClearUndoRedoList();
GetScreen()->SetContentModified( false );
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
}
Update3DView( true, true );
UpdateView();
GetCanvas()->Refresh();
m_treePane->GetLibTree()->RefreshLibTree(); // update any previously-highlighted items
return true;
2007-05-06 16:03:28 +00:00
}
FOOTPRINT* PCB_BASE_FRAME::SelectFootprintFromLibrary( LIB_ID aPreselect )
{
2020-11-07 14:31:50 +00:00
wxString footprintName;
LIB_ID fpid;
2020-11-13 15:15:52 +00:00
FOOTPRINT* footprint = nullptr;
2011-12-07 05:28:49 +00:00
static wxString lastComponentName;
DIALOG_FOOTPRINT_CHOOSER dialog( this, aPreselect, s_FootprintHistoryList );
if( dialog.ShowModal() == wxID_CANCEL )
return nullptr;
fpid = dialog.GetSelectedLibId();
if( !fpid.IsValid() )
return nullptr;
else
footprintName = fpid.Format().wx_str();
try
{
2020-11-07 14:31:50 +00:00
footprint = loadFootprint( fpid );
}
catch( const IO_ERROR& )
{
}
2020-11-07 14:31:50 +00:00
if( footprint )
{
2020-11-07 14:31:50 +00:00
lastComponentName = footprintName;
AddFootprintToHistory( footprintName );
}
2020-11-07 14:31:50 +00:00
return footprint;
}
2020-11-13 15:15:52 +00:00
FOOTPRINT* PCB_BASE_FRAME::LoadFootprint( const LIB_ID& aFootprintId )
{
FOOTPRINT* footprint = nullptr;
try
{
2020-11-10 21:20:03 +00:00
footprint = loadFootprint( aFootprintId );
}
catch( const IO_ERROR& )
{
}
2020-11-10 21:20:03 +00:00
return footprint;
}
2020-11-13 15:15:52 +00:00
FOOTPRINT* PCB_BASE_FRAME::loadFootprint( const LIB_ID& aFootprintId )
{
FP_LIB_TABLE* fptbl = PROJECT_PCB::PcbFootprintLibs( &Prj() );
* 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, nullptr, wxT( "Cannot look up LIB_ID in NULL FP_LIB_TABLE." ) );
2020-11-13 15:15:52 +00:00
FOOTPRINT *footprint = nullptr;
2020-12-04 12:13:11 +00:00
// When loading a footprint from a library in the footprint editor
// the items UUIDs must be keep and not reinitialized
bool keepUUID = IsType( FRAME_FOOTPRINT_EDITOR );
try
{
footprint = fptbl->FootprintLoadWithOptionalNickname( aFootprintId, keepUUID );
}
catch( const IO_ERROR& )
{
}
2020-11-10 21:20:03 +00:00
if( footprint )
{
// If the footprint is found, clear all net info to be sure there are no broken links to
// any netinfo list (should be not needed, but it can be edited from the footprint editor )
2020-11-10 21:20:03 +00:00
footprint->ClearAllNets();
if( m_pcb && !m_pcb->IsFootprintHolder() )
{
BOARD_DESIGN_SETTINGS& bds = m_pcb->GetDesignSettings();
footprint->ApplyDefaultSettings( *m_pcb, bds.m_StyleFPFields, bds.m_StyleFPText,
bds.m_StyleFPShapes );
}
}
2020-11-10 21:20:03 +00:00
return footprint;
2007-05-06 16:03:28 +00:00
}
2020-11-13 15:15:52 +00:00
FOOTPRINT* FOOTPRINT_EDIT_FRAME::SelectFootprintFromBoard( BOARD* aPcb )
2007-05-06 16:03:28 +00:00
{
2020-11-10 21:20:03 +00:00
static wxString oldName; // Save name of last footprint selected.
wxString fpname;
wxString msg;
wxArrayString listnames;
2020-11-13 15:15:52 +00:00
for( FOOTPRINT* footprint : aPcb->Footprints() )
2020-11-10 21:20:03 +00:00
listnames.Add( footprint->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 nullptr;
oldName = fpname;
for( FOOTPRINT* fp : aPcb->Footprints() )
{
if( fpname == fp->GetReference() )
return fp;
}
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( wxEmptyString, aLibraryPath );
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 );
if( dstType == IO_MGR::FILE_TYPE_NONE )
dstType = IO_MGR::KICAD_SEXP;
try
{
PLUGIN::RELEASER cur( IO_MGR::PluginFind( curType ) );
PLUGIN::RELEASER dst( IO_MGR::PluginFind( dstType ) );
if( !cur )
{
msg = wxString::Format( _( "Unable to find a reader for '%s'." ), curLibPath );
DisplayError( this, msg );
return false;
}
if( !dst )
{
msg = wxString::Format( _( "Unable to find a writer for '%s'." ), dstLibPath );
DisplayError( this, msg );
return false;
}
wxArrayString footprints;
cur->FootprintEnumerate( footprints, curLibPath, false );
for( unsigned i = 0; i < footprints.size(); ++i )
{
2020-11-13 15:15:52 +00:00
const FOOTPRINT* footprint = cur->GetEnumeratedFootprint( curLibPath, footprints[i] );
dst->FootprintSave( dstLibPath, footprint );
2021-06-28 23:44:07 +00:00
msg = wxString::Format( _( "Footprint '%s' saved." ), footprints[i] );
SetStatusText( msg );
}
}
catch( const IO_ERROR& ioe )
{
DisplayError( this, ioe.What() );
return false;
}
2021-06-28 23:44:07 +00:00
msg = wxString::Format( _( "Footprint library '%s' saved as '%s'." ),
curLibPath,
dstLibPath );
DisplayInfoMessage( this, msg );
SetStatusText( wxEmptyString );
return true;
}
static FOOTPRINT* s_FootprintInitialCopy = nullptr; // Copy of footprint for abort/undo command
2020-11-10 21:20:03 +00:00
static PICKED_ITEMS_LIST s_PickedList; // A pick-list to save initial footprint
// and dragged tracks
2020-11-13 15:15:52 +00:00
FOOTPRINT* PCB_BASE_FRAME::GetFootprintFromBoardByReference()
{
2020-11-10 21:20:03 +00:00
wxString footprintName;
wxArrayString fplist;
// Build list of available fp references, to display them in dialog
2022-02-04 22:44:59 +00:00
for( FOOTPRINT* fp : GetBoard()->Footprints() )
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 nullptr;
2020-11-10 21:20:03 +00:00
footprintName = dlg.GetValue();
footprintName.Trim( true );
footprintName.Trim( false );
2020-11-10 21:20:03 +00:00
if( !footprintName.IsEmpty() )
{
2022-02-04 22:44:59 +00:00
for( FOOTPRINT* fp : GetBoard()->Footprints() )
{
2022-02-04 22:44:59 +00:00
if( fp->GetReference().CmpNoCase( footprintName ) == 0 )
return fp;
}
}
return nullptr;
}
2020-11-13 15:15:52 +00:00
void PCB_BASE_FRAME::PlaceFootprint( FOOTPRINT* aFootprint, bool aRecreateRatsnest )
{
if( aFootprint == nullptr )
return;
OnModify();
2020-11-10 21:20:03 +00:00
if( aFootprint->IsNew() )
{
2020-11-10 21:20:03 +00:00
SaveCopyInUndoList( aFootprint, UNDO_REDO::NEWITEM );
}
2020-11-10 21:20:03 +00:00
else if( aFootprint->IsMoving() )
{
2020-11-10 21:20:03 +00:00
ITEM_PICKER picker( nullptr, aFootprint, UNDO_REDO::CHANGED );
picker.SetLink( s_FootprintInitialCopy );
s_PickedList.PushItem( picker );
s_FootprintInitialCopy = nullptr; // the picker is now owner of s_ModuleInitialCopy.
}
if( s_PickedList.GetCount() )
{
2020-08-26 18:04:32 +00:00
SaveCopyInUndoList( s_PickedList, UNDO_REDO::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();
}
2022-01-02 02:06:40 +00:00
aFootprint->SetPosition( GetCanvas()->GetViewControls()->GetCursorPosition() );
2020-11-10 21:20:03 +00:00
aFootprint->ClearFlags();
2020-11-10 21:20:03 +00:00
delete s_FootprintInitialCopy;
s_FootprintInitialCopy = nullptr;
if( aRecreateRatsnest )
2020-11-10 21:20:03 +00:00
m_pcb->GetConnectivity()->Update( aFootprint );
if( aRecreateRatsnest )
2019-05-30 15:11:17 +00:00
Compile_Ratsnest( true );
2020-11-10 21:20:03 +00:00
SetMsgPanel( aFootprint );
}