KiCad PCB EDA Suite
backanno.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-2017 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 
31 #include <fctsys.h>
32 #include <confirm.h>
33 #include <kicad_string.h>
34 #include <gestfich.h>
35 #include <kiface_i.h>
36 #include <schframe.h>
37 #include <build_version.h>
39 
40 #include <general.h>
41 #include <sch_sheet_path.h>
42 #include <sch_component.h>
43 #include <sch_reference_list.h>
44 
45 #include <dsnlexer.h>
46 #include <ptree.h>
47 #include <boost/property_tree/ptree.hpp>
48 #include <wx/choicdlg.h>
49 
50 
51 void SCH_EDIT_FRAME::backAnnotateFootprints( const std::string& aChangedSetOfReferences )
52 {
53  // Build a flat list of components in schematic:
54  SCH_REFERENCE_LIST refs;
55  SCH_SHEET_LIST sheets( g_RootSheet );
56  bool isChanged = false;
57 
58  sheets.GetComponents( refs, false );
59 
60  DSNLEXER lexer( aChangedSetOfReferences, FROM_UTF8( __func__ ) );
61  PTREE doc;
62 
63  try
64  {
65  Scan( &doc, &lexer );
66 
67 #if defined(DEBUG) && 0
69  Format( &sf, 0, 0, doc );
70  printf( "%s: '%s'\n", __func__, sf.GetString().c_str() );
71 #endif
72 
73  CPTREE& back_anno = doc.get_child( "back_annotation" );
74  wxString footprint;
75 
76  for( PTREE::const_iterator ref = back_anno.begin(); ref != back_anno.end(); ++ref )
77  {
78  wxASSERT( ref->first == "ref" );
79 
80  wxString reference = (UTF8&) ref->second.front().first;
81 
82  // Ensure the "fpid" node contains a footprint name,
83  // and get it if exists
84  if( ref->second.get_child( "fpid" ).size() )
85  {
86  wxString tmp = (UTF8&) ref->second.get_child( "fpid" ).front().first;
87  footprint = tmp;
88  }
89  else
90  footprint.Empty();
91 
92  // Search the component in the flat list
93  for( unsigned ii = 0; ii < refs.GetCount(); ++ii )
94  {
95  if( reference == refs[ii].GetRef() )
96  {
97  // We have found a candidate.
98  // Note: it can be not unique (multiple parts per package)
99  // So we *do not* stop the search here
100  SCH_COMPONENT* component = refs[ii].GetComp();
101  SCH_FIELD* fpfield = component->GetField( FOOTPRINT );
102  const wxString& oldfp = fpfield->GetText();
103 
104  if( !oldfp && fpfield->IsVisible() )
105  {
106  fpfield->SetVisible( false );
107  }
108 
109  if( oldfp != footprint )
110  isChanged = true;
111 
112  fpfield->SetText( footprint );
113  }
114  }
115  }
116  }
117  catch( const PTREE_ERROR& ex )
118  {
119  // remap the exception to something the caller is likely to understand.
120  THROW_IO_ERROR( ex.what() );
121  }
122 
123  if( isChanged )
124  OnModify();
125 }
126 
127 
128 bool SCH_EDIT_FRAME::ProcessCmpToFootprintLinkFile( const wxString& aFullFilename,
129  bool aForceVisibilityState,
130  bool aVisibilityState )
131 {
132  // Build a flat list of components in schematic:
133  SCH_REFERENCE_LIST referencesList;
134  SCH_SHEET_LIST sheetList( g_RootSheet );
135 
136  sheetList.GetComponents( referencesList, false );
137 
138  FILE* cmpFile = wxFopen( aFullFilename, wxT( "rt" ) );
139 
140  if( cmpFile == NULL )
141  return false;
142 
143  // cmpFileReader dtor will close cmpFile
144  FILE_LINE_READER cmpFileReader( cmpFile, aFullFilename );
145 
146  // Now, for each component found in file,
147  // replace footprint field value by the new value:
148  wxString reference;
149  wxString footprint;
150  wxString buffer;
151  wxString value;
152 
153  while( cmpFileReader.ReadLine() )
154  {
155  buffer = FROM_UTF8( cmpFileReader.Line() );
156 
157  if( !buffer.StartsWith( wxT( "BeginCmp" ) ) )
158  continue;
159 
160  // Begin component description.
161  reference.Empty();
162  footprint.Empty();
163 
164  while( cmpFileReader.ReadLine() )
165  {
166  buffer = FROM_UTF8( cmpFileReader.Line() );
167 
168  if( buffer.StartsWith( wxT( "EndCmp" ) ) )
169  break;
170 
171  // store string value, stored between '=' and ';' delimiters.
172  value = buffer.AfterFirst( '=' );
173  value = value.BeforeLast( ';' );
174  value.Trim(true);
175  value.Trim(false);
176 
177  if( buffer.StartsWith( wxT( "Reference" ) ) )
178  {
179  reference = value;
180  }
181  else if( buffer.StartsWith( wxT( "IdModule" ) ) )
182  {
183  footprint = value;
184  }
185  }
186 
187  // A block is read: initialize the footprint field of the corresponding component
188  // if the footprint name is not empty
189  if( reference.IsEmpty() )
190  continue;
191 
192  // Search the component in the flat list
193  for( unsigned ii = 0; ii < referencesList.GetCount(); ii++ )
194  {
195  if( reference == referencesList[ii].GetRef() )
196  {
197  // We have found a candidate.
198  // Note: it can be not unique (multiple units per part)
199  // So we *do not* stop the search here
200  SCH_COMPONENT* component = referencesList[ii].GetComp();
201  SCH_FIELD* fpfield = component->GetField( FOOTPRINT );
202 
203  fpfield->SetText( footprint );
204 
205  if( aForceVisibilityState )
206  {
207  component->GetField( FOOTPRINT )->SetVisible( aVisibilityState );
208  }
209  }
210  }
211  }
212 
213  return true;
214 }
215 
216 
218 {
219  wxString path = wxPathOnly( Prj().GetProjectFullName() );
220 
221  wxFileDialog dlg( this, _( "Load Symbol Footprint Link File" ),
222  path, wxEmptyString,
224  wxFD_OPEN | wxFD_FILE_MUST_EXIST );
225 
226  if( dlg.ShowModal() == wxID_CANCEL )
227  return false;
228 
229  wxString filename = dlg.GetPath();
230 
231  wxArrayString choices;
232  choices.Add( _( "Keep existing footprint field visibility" ) );
233  choices.Add( _( "Show all footprint fields" ) );
234  choices.Add( _( "Hide all footprint fields" ) );
235 
236  wxSingleChoiceDialog choiceDlg( this, _( "Select the footprint field visibility setting." ),
237  _( "Change Visibility" ), choices );
238 
239 
240  if( choiceDlg.ShowModal() == wxID_CANCEL )
241  return false;
242 
243  bool forceVisibility = (choiceDlg.GetSelection() != 0 );
244  bool visibilityState = (choiceDlg.GetSelection() == 1 );
245 
246  if( !ProcessCmpToFootprintLinkFile( filename, forceVisibility, visibilityState ) )
247  {
248  wxString msg = wxString::Format( _( "Failed to open component-footprint link file '%s'" ),
249  filename.GetData() );
250 
251  DisplayError( this, msg );
252  return false;
253  }
254 
255  OnModify();
256  return true;
257 }
Class UTF8 is an 8 bit string that is assuredly encoded in UTF8, and supplies special conversion supp...
Definition: utf8.h:73
Class SCH_SHEET_LIST.
Class SCH_FIELD instances are attached to a component and provide a place for the component's value...
Definition: sch_field.h:56
char * ReadLine() override
Function ReadLine reads a line of text into the buffer and increments the line number counter...
Definition: richio.cpp:194
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:53
bool LoadCmpToFootprintLinkFile()
Loads a .cmp file from CvPcb and update the footprint field of components.
Definition: backanno.cpp:217
This file is part of the common library TODO brief description.
This file is part of the common library.
wxString ComponentFileWildcard()
void OnModify()
Must be called after a schematic change in order to set the "modify" flag of the current screen* and ...
Definition: schframe.cpp:773
void Scan(PTREE *aTree, DSNLEXER *aLexer)
Function Scan fills an empty PTREE with information from a KiCad s-expresion stream.
Definition: ptree.cpp:90
void SetVisible(bool aVisible)
Definition: eda_text.h:175
Field Name Module PCB, i.e. "16DIP300".
Class SCH_REFERENCE_LIST is used to create a flattened list of components because in a complex hierar...
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
boost::property_tree::ptree PTREE
Definition: ptree.h:54
Class FILE_LINE_READER is a LINE_READER that reads from an open file.
Definition: richio.h:180
SCH_SHEET * g_RootSheet
Definition: eeschema.cpp:55
SCH_FIELD * GetField(int aFieldNdx) const
Returns a field in this symbol.
const PTREE CPTREE
Definition: ptree.h:55
char * Line() const
Function Line returns a pointer to the last line that was read in.
Definition: richio.h:139
const wxString & GetText() const
Function GetText returns the string associated with the text object.
Definition: eda_text.h:130
The common library.
void backAnnotateFootprints(const std::string &aChangedSetOfReferences)
Definition: backanno.cpp:51
const std::string & GetString()
Definition: richio.h:475
boost::property_tree::ptree_error PTREE_ERROR
Definition: ptree.h:56
Definition the SCH_COMPONENT class for Eeschema.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
bool IsVisible() const
Definition: eda_text.h:176
Class SCH_COMPONENT describes a real schematic component.
Definition: sch_component.h:69
void GetComponents(SCH_REFERENCE_LIST &aReferences, bool aIncludePowerSymbols=true, bool aForceIncludeOrphanComponents=false)
Function GetComponents adds a SCH_REFERENCE() object to aReferences for each component in the list of...
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
unsigned GetCount()
Function GetCount.
Class STRING_FORMATTER implements OUTPUTFORMATTER to a memory buffer.
Definition: richio.h:445
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:73
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:38
Class DSNLEXER implements a lexical analyzer for the SPECCTRA DSN file format.
Definition: dsnlexer.h:79
virtual void SetText(const wxString &aText)
Definition: eda_text.h:141
bool ProcessCmpToFootprintLinkFile(const wxString &aFullFilename, bool aForceVisibilityState, bool aVisibilityState)
Read the footprint info from each line in the stuff file by reference designator. ...
Definition: backanno.cpp:128