KiCad PCB EDA Suite
assign_footprints.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 2008 Wayne Stambaugh <stambaughw@gmail.com>
6  * Copyright (C) 2004-2019 KiCad Developers, see AUTHORS.txt for contributors.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 #include <confirm.h>
27 #include <kicad_string.h>
28 #include <kiface_i.h>
29 #include <sch_edit_frame.h>
31 #include <sch_sheet_path.h>
32 #include <sch_component.h>
33 #include <sch_reference_list.h>
34 #include <schematic.h>
35 #include <dsnlexer.h>
36 #include <ptree.h>
37 #include <boost/property_tree/ptree.hpp>
38 #include <wx/choicdlg.h>
40 
41 
42 void SCH_EDITOR_CONTROL::AssignFootprints( const std::string& aChangedSetOfReferences )
43 {
44  // Build a flat list of components in schematic:
45  SCH_REFERENCE_LIST refs;
47  bool isChanged = false;
48 
49  sheets.GetSymbols( refs, false );
50 
51  DSNLEXER lexer( aChangedSetOfReferences, FROM_UTF8( __func__ ) );
52  PTREE doc;
53 
54  try
55  {
56  Scan( &doc, &lexer );
57 
58  CPTREE& back_anno = doc.get_child( "cvpcb_netlist" );
59  wxString footprint;
60 
61  for( PTREE::const_iterator ref = back_anno.begin(); ref != back_anno.end(); ++ref )
62  {
63  wxASSERT( ref->first == "ref" );
64 
65  wxString reference = (UTF8&) ref->second.front().first;
66 
67  // Ensure the "fpid" node contains a footprint name, and get it if exists
68  if( ref->second.get_child( "fpid" ).size() )
69  {
70  wxString tmp = (UTF8&) ref->second.get_child( "fpid" ).front().first;
71  footprint = tmp;
72  }
73  else
74  footprint.Empty();
75 
76  // Search the component in the flat list
77  for( unsigned ii = 0; ii < refs.GetCount(); ++ii )
78  {
79  if( reference == refs[ii].GetRef() )
80  {
81  // We have found a candidate.
82  // Note: it can be not unique (multiple parts per package)
83  // So we *do not* stop the search here
84  SCH_COMPONENT* component = refs[ ii ].GetSymbol();
85  // For backwards-compatibility CvPcb currently updates all instances of a
86  // component (even though it lists these instances separately).
87  SCH_SHEET_PATH* sheetPath = nullptr; // &refs[ii].GetSheetPath();
88  wxString oldfp = refs[ii].GetFootprint();
89 
90  if( oldfp.IsEmpty() && component->GetField( FOOTPRINT_FIELD )->IsVisible() )
91  component->GetField( FOOTPRINT_FIELD )->SetVisible( false );
92 
93  if( oldfp != footprint )
94  {
95  isChanged = true;
96  component->SetFootprint( sheetPath, footprint );
97  }
98  }
99  }
100  }
101  }
102  catch( const PTREE_ERROR& ex )
103  {
104  // remap the exception to something the caller is likely to understand.
105  THROW_IO_ERROR( ex.what() );
106  }
107 
108  if( isChanged )
109  {
110  m_frame->SyncView();
111  m_frame->GetCanvas()->Refresh();
112  m_frame->OnModify();
113  }
114 }
115 
116 
117 bool SCH_EDITOR_CONTROL::processCmpToFootprintLinkFile( const wxString& aFullFilename,
118  bool aForceVisibilityState,
119  bool aVisibilityState )
120 {
121  // Build a flat list of components in schematic:
122  SCH_REFERENCE_LIST referencesList;
123  SCH_SHEET_LIST sheetList = m_frame->Schematic().GetSheets();
124 
125  sheetList.GetSymbols( referencesList, false );
126 
127  FILE* cmpFile = wxFopen( aFullFilename, wxT( "rt" ) );
128 
129  if( cmpFile == NULL )
130  return false;
131 
132  // cmpFileReader dtor will close cmpFile
133  FILE_LINE_READER cmpFileReader( cmpFile, aFullFilename );
134 
135  // Now, for each component found in file,
136  // replace footprint field value by the new value:
137  wxString reference;
138  wxString footprint;
139  wxString buffer;
140  wxString value;
141 
142  while( cmpFileReader.ReadLine() )
143  {
144  buffer = FROM_UTF8( cmpFileReader.Line() );
145 
146  if( !buffer.StartsWith( wxT( "BeginCmp" ) ) )
147  continue;
148 
149  // Begin component description.
150  reference.Empty();
151  footprint.Empty();
152 
153  while( cmpFileReader.ReadLine() )
154  {
155  buffer = FROM_UTF8( cmpFileReader.Line() );
156 
157  if( buffer.StartsWith( wxT( "EndCmp" ) ) )
158  break;
159 
160  // store string value, stored between '=' and ';' delimiters.
161  value = buffer.AfterFirst( '=' );
162  value = value.BeforeLast( ';' );
163  value.Trim(true);
164  value.Trim(false);
165 
166  if( buffer.StartsWith( wxT( "Reference" ) ) )
167  reference = value;
168  else if( buffer.StartsWith( wxT( "IdModule" ) ) )
169  footprint = value;
170  }
171 
172  // A block is read: initialize the footprint field of the corresponding component
173  // if the footprint name is not empty
174  if( reference.IsEmpty() )
175  continue;
176 
177  // Search the component in the flat list
178  for( unsigned ii = 0; ii < referencesList.GetCount(); ii++ )
179  {
180  if( reference == referencesList[ii].GetRef() )
181  {
182  // We have found a candidate.
183  // Note: it can be not unique (multiple units per part)
184  // So we *do not* stop the search here
185  SCH_COMPONENT* symbol = referencesList[ ii ].GetSymbol();
186  SCH_SHEET_PATH* sheetPath = &referencesList[ii].GetSheetPath();
187 
188  symbol->SetFootprint( sheetPath, footprint );
189 
190  if( aForceVisibilityState )
191  symbol->GetField( FOOTPRINT_FIELD )->SetVisible( aVisibilityState );
192  }
193  }
194  }
195 
196  return true;
197 }
198 
199 
201 {
202  wxString path = wxPathOnly( m_frame->Prj().GetProjectFullName() );
203 
204  wxFileDialog dlg( m_frame, _( "Load Symbol Footprint Link File" ),
205  path, wxEmptyString,
207  wxFD_OPEN | wxFD_FILE_MUST_EXIST );
208 
209  if( dlg.ShowModal() == wxID_CANCEL )
210  return 0;
211 
212  wxString filename = dlg.GetPath();
213 
214  wxArrayString choices;
215  choices.Add( _( "Keep existing footprint field visibility" ) );
216  choices.Add( _( "Show all footprint fields" ) );
217  choices.Add( _( "Hide all footprint fields" ) );
218 
219  wxSingleChoiceDialog choiceDlg( m_frame, _( "Select the footprint field visibility setting." ),
220  _( "Change Visibility" ), choices );
221 
222  if( choiceDlg.ShowModal() == wxID_CANCEL )
223  return 0;
224 
225  bool forceVisibility = (choiceDlg.GetSelection() != 0 );
226  bool visibilityState = (choiceDlg.GetSelection() == 1 );
227 
228  if( !processCmpToFootprintLinkFile( filename, forceVisibility, visibilityState ) )
229  {
230  wxString msg = wxString::Format( _( "Failed to open component-footprint link file \"%s\"" ),
231  filename.GetData() );
232 
233  DisplayError( m_frame, msg );
234  return 0;
235  }
236 
237  m_frame->SyncView();
238  m_frame->GetCanvas()->Refresh();
239  m_frame->OnModify();
240  return 0;
241 }
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:240
UTF8 is an 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to...
Definition: utf8.h:73
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
char * ReadLine() override
Function ReadLine reads a line of text into the buffer and increments the line number counter.
Definition: richio.cpp:198
char * Line() const
Function Line returns a pointer to the last line that was read in.
Definition: richio.h:140
static wxString FROM_UTF8(const char *cstring)
function FROM_UTF8 converts a UTF8 encoded C string to a wxString for all wxWidgets build modes.
Definition: macros.h:109
SCH_SHEET_LIST GetSheets() const
Builds and returns an updated schematic hierarchy TODO: can this be cached?
Definition: schematic.h:97
void GetSymbols(SCH_REFERENCE_LIST &aReferences, bool aIncludePowerSymbols=true, bool aForceIncludeOrphanComponents=false) const
Add a SCH_REFERENCE object to aReferences for each component in the list of sheets.
This file is part of the common library.
wxString ComponentFileWildcard()
void Scan(PTREE *aTree, DSNLEXER *aLexer)
Function Scan fills an empty PTREE with information from a KiCad s-expresion stream.
Definition: ptree.cpp:86
bool IsVisible() const
Definition: eda_text.h:192
void SetVisible(bool aVisible)
Definition: eda_text.h:191
SCH_REFERENCE_LIST is used to create a flattened list of symbols because in a complex hierarchy,...
boost::property_tree::ptree PTREE
Definition: ptree.h:54
FILE_LINE_READER is a LINE_READER that reads from an open file.
Definition: richio.h:181
#define NULL
const PTREE CPTREE
Definition: ptree.h:55
void SyncView()
Mark all items for refresh.
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
SCH_DRAW_PANEL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
TOOL_EVENT.
Definition: tool_event.h:171
bool processCmpToFootprintLinkFile(const wxString &aFullFilename, bool aForceVisibilityState, bool aVisibilityState)
Read the footprint info from each line in the stuff file by reference designator.
SCHEMATIC & Schematic() const
Definition of file extensions used in Kicad.
int ImportFPAssignments(const TOOL_EVENT &aEvent)
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
boost::property_tree::ptree_error PTREE_ERROR
Definition: ptree.h:56
VTBL_ENTRY const wxString GetProjectFullName() const
Function GetProjectFullName returns the full path and name of the project.
Definition: project.cpp:116
virtual void Refresh(bool aEraseBackground=true, const wxRect *aRect=NULL) override
Update the board display after modifying it by a python script (note: it is automatically called by a...
Field Name Module PCB, i.e. "16DIP300".
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
unsigned GetCount() const
SCH_FIELD * GetField(int aFieldNdx)
Returns a field in this symbol.
#define _(s)
Definition: 3d_actions.cpp:33
Schematic symbol object.
Definition: sch_component.h:79
void AssignFootprints(const std::string &aChangedSetOfReferences)
void OnModify() override
Must be called after a schematic change in order to set the "modify" flag of the current screen and u...
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:38
DSNLEXER implements a lexical analyzer for the SPECCTRA DSN file format.
Definition: dsnlexer.h:80
void SetFootprint(const SCH_SHEET_PATH *sheet, const wxString &aFootprint)