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 <schematic.h>
39 #include <dsnlexer.h>
40 #include <ptree.h>
41 #include <boost/property_tree/ptree.hpp>
42 #include <wx/choicdlg.h>
44 
45 
46 void SCH_EDITOR_CONTROL::BackAnnotateFootprints( const std::string& aChangedSetOfReferences )
47 {
48  // Build a flat list of components in schematic:
49  SCH_REFERENCE_LIST refs;
51  bool isChanged = false;
52 
53  sheets.GetComponents( refs, false );
54 
55  DSNLEXER lexer( aChangedSetOfReferences, FROM_UTF8( __func__ ) );
56  PTREE doc;
57 
58  try
59  {
60  Scan( &doc, &lexer );
61 
62  CPTREE& back_anno = doc.get_child( "back_annotation" );
63  wxString footprint;
64 
65  for( PTREE::const_iterator ref = back_anno.begin(); ref != back_anno.end(); ++ref )
66  {
67  wxASSERT( ref->first == "ref" );
68 
69  wxString reference = (UTF8&) ref->second.front().first;
70 
71  // Ensure the "fpid" node contains a footprint name, and get it if exists
72  if( ref->second.get_child( "fpid" ).size() )
73  {
74  wxString tmp = (UTF8&) ref->second.get_child( "fpid" ).front().first;
75  footprint = tmp;
76  }
77  else
78  footprint.Empty();
79 
80  // Search the component in the flat list
81  for( unsigned ii = 0; ii < refs.GetCount(); ++ii )
82  {
83  if( reference == refs[ii].GetRef() )
84  {
85  // We have found a candidate.
86  // Note: it can be not unique (multiple parts per package)
87  // So we *do not* stop the search here
88  SCH_COMPONENT* component = refs[ii].GetComp();
89  // For backwards-compatibility CvPcb currently updates all instances of a
90  // component (even though it lists these instances separately).
91  SCH_SHEET_PATH* sheetPath = nullptr; // &refs[ii].GetSheetPath();
92  wxString oldfp = refs[ii].GetFootprint();
93 
94  if( oldfp.IsEmpty() && component->GetField( FOOTPRINT )->IsVisible() )
95  component->GetField( FOOTPRINT )->SetVisible( false );
96 
97  if( oldfp != footprint )
98  {
99  isChanged = true;
100  component->SetFootprint( sheetPath, footprint );
101  }
102  }
103  }
104  }
105  }
106  catch( const PTREE_ERROR& ex )
107  {
108  // remap the exception to something the caller is likely to understand.
109  THROW_IO_ERROR( ex.what() );
110  }
111 
112  if( isChanged )
113  {
114  m_frame->SyncView();
115  m_frame->GetCanvas()->Refresh();
116  m_frame->OnModify();
117  }
118 }
119 
120 
121 bool SCH_EDITOR_CONTROL::processCmpToFootprintLinkFile( const wxString& aFullFilename,
122  bool aForceVisibilityState,
123  bool aVisibilityState )
124 {
125  // Build a flat list of components in schematic:
126  SCH_REFERENCE_LIST referencesList;
127  SCH_SHEET_LIST sheetList = m_frame->Schematic().GetSheets();
128 
129  sheetList.GetComponents( referencesList, false );
130 
131  FILE* cmpFile = wxFopen( aFullFilename, wxT( "rt" ) );
132 
133  if( cmpFile == NULL )
134  return false;
135 
136  // cmpFileReader dtor will close cmpFile
137  FILE_LINE_READER cmpFileReader( cmpFile, aFullFilename );
138 
139  // Now, for each component found in file,
140  // replace footprint field value by the new value:
141  wxString reference;
142  wxString footprint;
143  wxString buffer;
144  wxString value;
145 
146  while( cmpFileReader.ReadLine() )
147  {
148  buffer = FROM_UTF8( cmpFileReader.Line() );
149 
150  if( !buffer.StartsWith( wxT( "BeginCmp" ) ) )
151  continue;
152 
153  // Begin component description.
154  reference.Empty();
155  footprint.Empty();
156 
157  while( cmpFileReader.ReadLine() )
158  {
159  buffer = FROM_UTF8( cmpFileReader.Line() );
160 
161  if( buffer.StartsWith( wxT( "EndCmp" ) ) )
162  break;
163 
164  // store string value, stored between '=' and ';' delimiters.
165  value = buffer.AfterFirst( '=' );
166  value = value.BeforeLast( ';' );
167  value.Trim(true);
168  value.Trim(false);
169 
170  if( buffer.StartsWith( wxT( "Reference" ) ) )
171  reference = value;
172  else if( buffer.StartsWith( wxT( "IdModule" ) ) )
173  footprint = value;
174  }
175 
176  // A block is read: initialize the footprint field of the corresponding component
177  // if the footprint name is not empty
178  if( reference.IsEmpty() )
179  continue;
180 
181  // Search the component in the flat list
182  for( unsigned ii = 0; ii < referencesList.GetCount(); ii++ )
183  {
184  if( reference == referencesList[ii].GetRef() )
185  {
186  // We have found a candidate.
187  // Note: it can be not unique (multiple units per part)
188  // So we *do not* stop the search here
189  SCH_COMPONENT* component = referencesList[ii].GetComp();
190  SCH_SHEET_PATH* sheetPath = &referencesList[ii].GetSheetPath();
191 
192  component->SetFootprint( sheetPath, footprint );
193 
194  if( aForceVisibilityState )
195  component->GetField( FOOTPRINT )->SetVisible( aVisibilityState );
196  }
197  }
198  }
199 
200  return true;
201 }
202 
203 
205 {
206  wxString path = wxPathOnly( m_frame->Prj().GetProjectFullName() );
207 
208  wxFileDialog dlg( m_frame, _( "Load Symbol Footprint Link File" ),
209  path, wxEmptyString,
211  wxFD_OPEN | wxFD_FILE_MUST_EXIST );
212 
213  if( dlg.ShowModal() == wxID_CANCEL )
214  return 0;
215 
216  wxString filename = dlg.GetPath();
217 
218  wxArrayString choices;
219  choices.Add( _( "Keep existing footprint field visibility" ) );
220  choices.Add( _( "Show all footprint fields" ) );
221  choices.Add( _( "Hide all footprint fields" ) );
222 
223  wxSingleChoiceDialog choiceDlg( m_frame, _( "Select the footprint field visibility setting." ),
224  _( "Change Visibility" ), choices );
225 
226  if( choiceDlg.ShowModal() == wxID_CANCEL )
227  return 0;
228 
229  bool forceVisibility = (choiceDlg.GetSelection() != 0 );
230  bool visibilityState = (choiceDlg.GetSelection() == 1 );
231 
232  if( !processCmpToFootprintLinkFile( filename, forceVisibility, visibilityState ) )
233  {
234  wxString msg = wxString::Format( _( "Failed to open component-footprint link file \"%s\"" ),
235  filename.GetData() );
236 
237  DisplayError( m_frame, msg );
238  return 0;
239  }
240 
241  m_frame->SyncView();
242  m_frame->GetCanvas()->Refresh();
243  m_frame->OnModify();
244  return 0;
245 }
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.
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:114
SCH_SHEET_LIST GetSheets() const
Builds and returns an updated schematic hierarchy TODO: can this be cached?
Definition: schematic.h:89
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:86
bool IsVisible() const
Definition: eda_text.h:186
void SetVisible(bool aVisible)
Definition: eda_text.h:185
Field Name Module PCB, i.e. "16DIP300".
SCH_REFERENCE_LIST is used to create a flattened list of components because in a complex hierarchy,...
boost::property_tree::ptree PTREE
Definition: ptree.h:54
void BackAnnotateFootprints(const std::string &aChangedSetOfReferences)
Definition: backanno.cpp:46
FILE_LINE_READER is a LINE_READER that reads from an open file.
Definition: richio.h:180
#define NULL
void GetComponents(SCH_REFERENCE_LIST &aReferences, bool aIncludePowerSymbols=true, bool aForceIncludeOrphanComponents=false) const
Function GetComponents adds a SCH_REFERENCE() object to aReferences for each component in the list of...
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:121
SCHEMATIC & Schematic() const
Definition of file extensions used in Kicad.
int ImportFPAssignments(const TOOL_EVENT &aEvent)
Definition: backanno.cpp:204
SCH_SHEET_PATH.
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:118
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...
unsigned GetCount() const
Function GetCount.
SCH_FIELD * GetField(int aFieldNdx)
Returns a field in this symbol.
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:201
#define _(s)
Definition: 3d_actions.cpp:33
Schematic symbol object.
Definition: sch_component.h:80
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:79
void SetFootprint(const SCH_SHEET_PATH *sheet, const wxString &aFootprint)