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-2016 Wayne Stambaugh <stambaughw@verizon.net>
6  * Copyright (C) 2004-2016 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( Prj().SchLibs(), 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  // DBG( printf( "%s: ref:%s fpid:%s\n", __func__, TO_UTF8( reference ), TO_UTF8( footprint ) ); )
93 
94  // Search the component in the flat list
95  for( unsigned ii = 0; ii < refs.GetCount(); ++ii )
96  {
97  if( reference == refs[ii].GetRef() )
98  {
99  // We have found a candidate.
100  // Note: it can be not unique (multiple parts per package)
101  // So we *do not* stop the search here
102  SCH_COMPONENT* component = refs[ii].GetComp();
103  SCH_FIELD* fpfield = component->GetField( FOOTPRINT );
104  const wxString& oldfp = fpfield->GetText();
105 
106  if( !oldfp && fpfield->IsVisible() )
107  {
108  fpfield->SetVisible( false );
109  }
110 
111  // DBG( printf("%s: ref:%s fpid:%s\n", __func__, TO_UTF8( refs[ii].GetRef() ), TO_UTF8( footprint ) );)
112  if( oldfp != footprint )
113  isChanged = true;
114 
115  fpfield->SetText( footprint );
116  }
117  }
118  }
119  }
120  catch( const PTREE_ERROR& ex )
121  {
122  // remap the exception to something the caller is likely to understand.
123  THROW_IO_ERROR( ex.what() );
124  }
125 
126  if( isChanged )
127  OnModify();
128 }
129 
130 
131 bool SCH_EDIT_FRAME::ProcessCmpToFootprintLinkFile( const wxString& aFullFilename,
132  bool aForceVisibilityState,
133  bool aVisibilityState )
134 {
135  // Build a flat list of components in schematic:
136  SCH_REFERENCE_LIST referencesList;
137  SCH_SHEET_LIST sheetList( g_RootSheet );
138 
139  sheetList.GetComponents( Prj().SchLibs(), referencesList, false );
140 
141  FILE* cmpFile = wxFopen( aFullFilename, wxT( "rt" ) );
142 
143  if( cmpFile == NULL )
144  return false;
145 
146  // cmpFileReader dtor will close cmpFile
147  FILE_LINE_READER cmpFileReader( cmpFile, aFullFilename );
148 
149  // Now, for each component found in file,
150  // replace footprint field value by the new value:
151  wxString reference;
152  wxString footprint;
153  wxString buffer;
154  wxString value;
155 
156  while( cmpFileReader.ReadLine() )
157  {
158  buffer = FROM_UTF8( cmpFileReader.Line() );
159 
160  if( !buffer.StartsWith( wxT( "BeginCmp" ) ) )
161  continue;
162 
163  // Begin component description.
164  reference.Empty();
165  footprint.Empty();
166 
167  while( cmpFileReader.ReadLine() )
168  {
169  buffer = FROM_UTF8( cmpFileReader.Line() );
170 
171  if( buffer.StartsWith( wxT( "EndCmp" ) ) )
172  break;
173 
174  // store string value, stored between '=' and ';' delimiters.
175  value = buffer.AfterFirst( '=' );
176  value = value.BeforeLast( ';' );
177  value.Trim(true);
178  value.Trim(false);
179 
180  if( buffer.StartsWith( wxT( "Reference" ) ) )
181  {
182  reference = value;
183  }
184  else if( buffer.StartsWith( wxT( "IdModule" ) ) )
185  {
186  footprint = value;
187  }
188  }
189 
190  // A block is read: initialize the footprint field of the corresponding component
191  // if the footprint name is not empty
192  if( reference.IsEmpty() )
193  continue;
194 
195  // Search the component in the flat list
196  for( unsigned ii = 0; ii < referencesList.GetCount(); ii++ )
197  {
198  if( reference == referencesList[ii].GetRef() )
199  {
200  // We have found a candidate.
201  // Note: it can be not unique (multiple units per part)
202  // So we *do not* stop the search here
203  SCH_COMPONENT* component = referencesList[ii].GetComp();
204  SCH_FIELD* fpfield = component->GetField( FOOTPRINT );
205 
206  fpfield->SetText( footprint );
207 
208  if( aForceVisibilityState )
209  {
210  component->GetField( FOOTPRINT )->SetVisible( aVisibilityState );
211  }
212  }
213  }
214  }
215 
216  return true;
217 }
218 
219 
221 {
222  wxString path = wxPathOnly( Prj().GetProjectFullName() );
223 
224  wxFileDialog dlg( this, _( "Load Component Footprint Link File" ),
225  path, wxEmptyString,
227  wxFD_OPEN | wxFD_FILE_MUST_EXIST );
228 
229  if( dlg.ShowModal() == wxID_CANCEL )
230  return false;
231 
232  wxString filename = dlg.GetPath();
233 
234  wxArrayString choices;
235  choices.Add( _( "Keep existing footprint field visibility" ) );
236  choices.Add( _( "Show all footprint fields" ) );
237  choices.Add( _( "Hide all footprint fields" ) );
238 
239  wxSingleChoiceDialog choiceDlg( this, _( "Select the footprint field visibility setting." ),
240  _( "Change Visibility" ), choices );
241 
242 
243  if( choiceDlg.ShowModal() == wxID_CANCEL )
244  return false;
245 
246  bool forceVisibility = (choiceDlg.GetSelection() != 0 );
247  bool visibilityState = (choiceDlg.GetSelection() == 1 );
248 
249  if( !ProcessCmpToFootprintLinkFile( filename, forceVisibility, visibilityState ) )
250  {
251  wxString msg = wxString::Format( _( "Failed to open component-footprint link file '%s'" ),
252  filename.GetData() );
253 
254  DisplayError( this, msg );
255  return false;
256  }
257 
258  OnModify();
259  return true;
260 }
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:196
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()
Function ReadCmpToFootprintLinkFile Loads a .cmp file from CvPcb and update the footprin field of com...
Definition: backanno.cpp:220
This file is part of the common library TODO brief description.
This file is part of the common library.
void OnModify()
Function OnModify Must be called after a schematic change in order to set the "modify" flag of the cu...
Definition: schframe.cpp:770
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
const wxString ComponentFileExtensionWildcard
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(PART_LIBS *aLibs, SCH_REFERENCE_LIST &aReferences, bool aIncludePowerSymbols=true)
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:71
#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)
Function ProcessStuffFile gets footprint info from each line in the Stuff File by Ref Desg...
Definition: backanno.cpp:131