KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 <[email protected]>
6 * Copyright The 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 <string_utils.h>
28#include <kiface_base.h>
29#include <sch_edit_frame.h>
31#include <sch_commit.h>
32#include <sch_sheet_path.h>
33#include <sch_symbol.h>
34#include <sch_reference_list.h>
35#include <schematic.h>
36#include <dsnlexer.h>
37#include <ptree.h>
38#include <boost/property_tree/ptree.hpp>
40
41#include <wx/choicdlg.h>
42#include <wx/filedlg.h>
43#include <kiplatform/ui.h>
44
45
46void SCH_EDITOR_CONTROL::AssignFootprints( const std::string& aChangedSetOfReferences )
47{
48 // Build a flat list of symbols in schematic:
50 SCH_COMMIT commit( m_frame );
51 bool isChanged = false;
52
53 m_frame->Schematic().Hierarchy().GetSymbols( 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( "cvpcb_netlist" );
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 symbol 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_SYMBOL* symbol = refs[ ii ].GetSymbol();
89
90 // For backwards-compatibility CvPcb currently updates all instances of a
91 // symbol (even though it lists these instances separately).
92 wxString oldfp = refs[ii].GetFootprint();
93 SCH_FIELD* footprintField = symbol->GetField( FIELD_T::FOOTPRINT );
94
95 if( oldfp.IsEmpty() && footprintField->IsVisible() )
96 footprintField->SetVisible( false );
97
98 if( oldfp != footprint )
99 {
100 isChanged = true;
101 SCH_SCREEN* screen = refs[ii].GetSheetPath().LastScreen();
102
103 commit.Modify( symbol, screen, RECURSE_MODE::NO_RECURSE );
104 footprintField->SetText( footprint );
105 }
106 }
107 }
108 }
109 }
110 catch( const PTREE_ERROR& ex )
111 {
112 // remap the exception to something the caller is likely to understand.
113 THROW_IO_ERROR( ex.what() );
114 }
115
116 if( isChanged )
117 {
118 m_frame->SyncView();
119 commit.Push( wxS( "Assign Footprints" ) );
120 }
121}
122
123
124bool SCH_EDITOR_CONTROL::processCmpToFootprintLinkFile( const wxString& aFullFilename,
125 bool aForceVisibilityState,
126 bool aVisibilityState )
127{
128 // Build a flat list of symbols in schematic:
129 SCH_REFERENCE_LIST referencesList;
130 m_frame->Schematic().Hierarchy().GetSymbols( referencesList, false );
131
132 FILE* cmpFile = wxFopen( aFullFilename, wxT( "rt" ) );
133
134 if( cmpFile == nullptr )
135 return false;
136
137 // cmpFileReader dtor will close cmpFile
138 FILE_LINE_READER cmpFileReader( cmpFile, aFullFilename );
139
140 // Now, for each symbol found in file,
141 // replace footprint field value by the new value:
142 wxString reference;
143 wxString footprint;
144 wxString buffer;
145 wxString value;
146
147 while( cmpFileReader.ReadLine() )
148 {
149 buffer = From_UTF8( cmpFileReader.Line() );
150
151 if( !buffer.StartsWith( wxT( "BeginCmp" ) ) )
152 continue;
153
154 // Begin symbol description.
155 reference.Empty();
156 footprint.Empty();
157
158 while( cmpFileReader.ReadLine() )
159 {
160 buffer = From_UTF8( cmpFileReader.Line() );
161
162 if( buffer.StartsWith( wxT( "EndCmp" ) ) )
163 break;
164
165 // store string value, stored between '=' and ';' delimiters.
166 value = buffer.AfterFirst( '=' );
167 value = value.BeforeLast( ';' );
168 value.Trim(true);
169 value.Trim(false);
170
171 if( buffer.StartsWith( wxT( "Reference" ) ) )
172 reference = value;
173 else if( buffer.StartsWith( wxT( "IdModule" ) ) )
174 footprint = value;
175 }
176
177 // A block is read: initialize the footprint field of the corresponding symbol
178 // if the footprint name is not empty
179 if( reference.IsEmpty() )
180 continue;
181
182 // Search the symbol in the flat list
183 for( unsigned ii = 0; ii < referencesList.GetCount(); ii++ )
184 {
185 if( reference == referencesList[ii].GetRef() )
186 {
187 // We have found a candidate.
188 // Note: it can be not unique (multiple units per part)
189 // So we *do not* stop the search here
190 SCH_SYMBOL* symbol = referencesList[ ii ].GetSymbol();
191
192 symbol->SetFootprintFieldText( footprint );
193
194 if( aForceVisibilityState )
195 symbol->GetField( FIELD_T::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
214
215 if( dlg.ShowModal() == wxID_CANCEL )
216 return 0;
217
218 wxString filename = dlg.GetPath();
219
220 wxArrayString choices;
221 choices.Add( _( "Keep existing footprint field visibility" ) );
222 choices.Add( _( "Show all footprint fields" ) );
223 choices.Add( _( "Hide all footprint fields" ) );
224
225 wxSingleChoiceDialog choiceDlg( m_frame, _( "Select the footprint field visibility setting." ),
226 _( "Change Visibility" ), choices );
227
228 if( choiceDlg.ShowModal() == wxID_CANCEL )
229 return 0;
230
231 bool forceVisibility = (choiceDlg.GetSelection() != 0 );
232 bool visibilityState = (choiceDlg.GetSelection() == 1 );
233
234 if( !processCmpToFootprintLinkFile( filename, forceVisibility, visibilityState ) )
235 {
236 wxString msg = wxString::Format( _( "Failed to open symbol-footprint link file '%s'." ),
237 filename.GetData() );
238
239 DisplayError( m_frame, msg );
240 return 0;
241 }
242
243 m_frame->SyncView();
244 m_frame->GetCanvas()->Refresh();
245 m_frame->OnModify();
246 return 0;
247}
COMMIT & Modify(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr, RECURSE_MODE aRecurse=RECURSE_MODE::NO_RECURSE)
Modify a given item in the model.
Definition commit.h:106
Implement a lexical analyzer for the SPECCTRA DSN file format.
Definition dsnlexer.h:81
virtual bool IsVisible() const
Definition eda_text.h:187
virtual void SetVisible(bool aVisible)
Definition eda_text.cpp:398
A LINE_READER that reads from an open file.
Definition richio.h:158
char * ReadLine() override
Read a line of text into the buffer and increments the line number counter.
Definition richio.cpp:208
char * Line() const
Return a pointer to the last line that was read in.
Definition richio.h:102
virtual void Push(const wxString &aMessage=wxT("A commit"), int aCommitFlags=0) override
Execute the changes.
void AssignFootprints(const std::string &aChangedSetOfReferences)
int ImportFPAssignments(const TOOL_EVENT &aEvent)
bool processCmpToFootprintLinkFile(const wxString &aFullFilename, bool aForceVisibilityState, bool aVisibilityState)
Read the footprint info from each line in the stuff file by reference designator.
void SetText(const wxString &aText) override
Container to create a flattened list of symbols because in a complex hierarchy, a symbol can be used ...
Schematic symbol object.
Definition sch_symbol.h:76
void SetFootprintFieldText(const wxString &aFootprint)
SCH_FIELD * GetField(FIELD_T aFieldType)
Return a mandatory field in this symbol.
Generic, UI-independent tool event.
Definition tool_event.h:171
An 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to and fro...
Definition utf8.h:71
void DisplayError(wxWindow *aParent, const wxString &aText)
Display an error or warning message box with aMessage.
Definition confirm.cpp:177
This file is part of the common library.
#define _(s)
@ NO_RECURSE
Definition eda_item.h:52
static wxString FootprintAssignmentFileWildcard()
#define THROW_IO_ERROR(msg)
macro which captures the "call site" values of FILE_, __FUNCTION & LINE
void AllowNetworkFileSystems(wxDialog *aDialog)
Configure a file dialog to show network and virtual file systems.
Definition wxgtk/ui.cpp:717
void Scan(PTREE *aTree, DSNLEXER *aLexer)
Fill an empty PTREE with information from a KiCad s-expression stream.
Definition ptree.cpp:84
const PTREE CPTREE
Definition ptree.h:53
boost::property_tree::ptree_error PTREE_ERROR
Definition ptree.h:54
boost::property_tree::ptree PTREE
Definition ptree.h:52
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
wxString From_UTF8(const char *cstring)
@ FOOTPRINT
Field Name Module PCB, i.e. "16DIP300".
std::string path
Definition of file extensions used in Kicad.