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