kicad/eeschema/tools/assign_footprints.cpp

245 lines
8.2 KiB
C++
Raw Normal View History

/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
* Copyright (C) 2008 Wayne Stambaugh <stambaughw@gmail.com>
* Copyright (C) 2004-2021 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 <confirm.h>
#include <kicad_string.h>
* 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
#include <kiface_i.h>
2018-01-30 10:49:51 +00:00
#include <sch_edit_frame.h>
#include <wildcards_and_files_ext.h>
2016-02-15 20:16:54 +00:00
#include <sch_sheet_path.h>
#include <sch_symbol.h>
#include <sch_reference_list.h>
#include <schematic.h>
#include <dsnlexer.h>
#include <ptree.h>
#include <boost/property_tree/ptree.hpp>
#include <tools/sch_editor_control.h>
#include <wx/choicdlg.h>
#include <wx/filedlg.h>
void SCH_EDITOR_CONTROL::AssignFootprints( const std::string& aChangedSetOfReferences )
{
2021-06-10 14:10:55 +00:00
// Build a flat list of symbols in schematic:
SCH_REFERENCE_LIST refs;
SCH_SHEET_LIST sheets = m_frame->Schematic().GetSheets();
bool isChanged = false;
sheets.GetSymbols( refs, false );
DSNLEXER lexer( aChangedSetOfReferences, FROM_UTF8( __func__ ) );
PTREE doc;
try
{
Scan( &doc, &lexer );
CPTREE& back_anno = doc.get_child( "cvpcb_netlist" );
wxString footprint;
for( PTREE::const_iterator ref = back_anno.begin(); ref != back_anno.end(); ++ref )
{
wxASSERT( ref->first == "ref" );
wxString reference = (UTF8&) ref->second.front().first;
// Ensure the "fpid" node contains a footprint name, and get it if exists
if( ref->second.get_child( "fpid" ).size() )
{
wxString tmp = (UTF8&) ref->second.get_child( "fpid" ).front().first;
footprint = tmp;
}
else
footprint.Empty();
2021-06-10 14:10:55 +00:00
// Search the symbol in the flat list
for( unsigned ii = 0; ii < refs.GetCount(); ++ii )
{
if( reference == refs[ii].GetRef() )
{
// We have found a candidate.
// Note: it can be not unique (multiple parts per package)
// So we *do not* stop the search here
2021-06-10 14:10:55 +00:00
SCH_SYMBOL* symbol = refs[ ii ].GetSymbol();
// For backwards-compatibility CvPcb currently updates all instances of a
2021-06-10 14:10:55 +00:00
// symbol (even though it lists these instances separately).
SCH_SHEET_PATH* sheetPath = nullptr; // &refs[ii].GetSheetPath();
wxString oldfp = refs[ii].GetFootprint();
2021-06-10 14:10:55 +00:00
if( oldfp.IsEmpty() && symbol->GetField( FOOTPRINT_FIELD )->IsVisible() )
symbol->GetField( FOOTPRINT_FIELD )->SetVisible( false );
if( oldfp != footprint )
{
isChanged = true;
2021-06-10 14:10:55 +00:00
symbol->SetFootprint( sheetPath, footprint );
}
}
}
}
}
catch( const PTREE_ERROR& ex )
{
// remap the exception to something the caller is likely to understand.
THROW_IO_ERROR( ex.what() );
}
if( isChanged )
{
m_frame->SyncView();
m_frame->GetCanvas()->Refresh();
m_frame->OnModify();
}
}
bool SCH_EDITOR_CONTROL::processCmpToFootprintLinkFile( const wxString& aFullFilename,
bool aForceVisibilityState,
bool aVisibilityState )
{
2021-06-10 14:10:55 +00:00
// Build a flat list of symbols in schematic:
SCH_REFERENCE_LIST referencesList;
SCH_SHEET_LIST sheetList = m_frame->Schematic().GetSheets();
sheetList.GetSymbols( referencesList, false );
FILE* cmpFile = wxFopen( aFullFilename, wxT( "rt" ) );
2021-07-16 20:13:26 +00:00
if( cmpFile == nullptr )
return false;
// cmpFileReader dtor will close cmpFile
FILE_LINE_READER cmpFileReader( cmpFile, aFullFilename );
2021-06-10 14:10:55 +00:00
// Now, for each symbol found in file,
// replace footprint field value by the new value:
wxString reference;
wxString footprint;
wxString buffer;
wxString value;
while( cmpFileReader.ReadLine() )
{
buffer = FROM_UTF8( cmpFileReader.Line() );
if( !buffer.StartsWith( wxT( "BeginCmp" ) ) )
continue;
2021-06-10 14:10:55 +00:00
// Begin symbol description.
reference.Empty();
footprint.Empty();
while( cmpFileReader.ReadLine() )
{
buffer = FROM_UTF8( cmpFileReader.Line() );
if( buffer.StartsWith( wxT( "EndCmp" ) ) )
break;
// store string value, stored between '=' and ';' delimiters.
value = buffer.AfterFirst( '=' );
value = value.BeforeLast( ';' );
value.Trim(true);
value.Trim(false);
if( buffer.StartsWith( wxT( "Reference" ) ) )
reference = value;
else if( buffer.StartsWith( wxT( "IdModule" ) ) )
footprint = value;
}
2021-06-10 14:10:55 +00:00
// A block is read: initialize the footprint field of the corresponding symbol
// if the footprint name is not empty
if( reference.IsEmpty() )
continue;
2021-06-10 14:10:55 +00:00
// Search the symbol in the flat list
for( unsigned ii = 0; ii < referencesList.GetCount(); ii++ )
{
if( reference == referencesList[ii].GetRef() )
{
// We have found a candidate.
// Note: it can be not unique (multiple units per part)
// So we *do not* stop the search here
2021-06-10 14:10:55 +00:00
SCH_SYMBOL* symbol = referencesList[ ii ].GetSymbol();
SCH_SHEET_PATH* sheetPath = &referencesList[ii].GetSheetPath();
symbol->SetFootprint( sheetPath, footprint );
if( aForceVisibilityState )
symbol->GetField( FOOTPRINT_FIELD )->SetVisible( aVisibilityState );
}
}
}
return true;
}
int SCH_EDITOR_CONTROL::ImportFPAssignments( const TOOL_EVENT& aEvent )
{
wxString path = wxPathOnly( m_frame->Prj().GetProjectFullName() );
wxFileDialog dlg( m_frame, _( "Load Symbol Footprint Link File" ),
path, wxEmptyString,
FootprintAssignmentFileWildcard(),
wxFD_OPEN | wxFD_FILE_MUST_EXIST );
if( dlg.ShowModal() == wxID_CANCEL )
return 0;
wxString filename = dlg.GetPath();
wxArrayString choices;
choices.Add( _( "Keep existing footprint field visibility" ) );
choices.Add( _( "Show all footprint fields" ) );
choices.Add( _( "Hide all footprint fields" ) );
wxSingleChoiceDialog choiceDlg( m_frame, _( "Select the footprint field visibility setting." ),
_( "Change Visibility" ), choices );
if( choiceDlg.ShowModal() == wxID_CANCEL )
return 0;
bool forceVisibility = (choiceDlg.GetSelection() != 0 );
bool visibilityState = (choiceDlg.GetSelection() == 1 );
if( !processCmpToFootprintLinkFile( filename, forceVisibility, visibilityState ) )
{
2021-06-28 23:44:07 +00:00
wxString msg = wxString::Format( _( "Failed to open symbol-footprint link file '%s'." ),
filename.GetData() );
DisplayError( m_frame, msg );
return 0;
}
m_frame->SyncView();
m_frame->GetCanvas()->Refresh();
m_frame->OnModify();
return 0;
}