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 (C) 2004-2022 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_sheet_path.h>
32#include <sch_symbol.h>
33#include <sch_reference_list.h>
34#include <schematic.h>
35#include <dsnlexer.h>
36#include <ptree.h>
37#include <boost/property_tree/ptree.hpp>
39
40#include <wx/choicdlg.h>
41#include <wx/filedlg.h>
42
43
44void SCH_EDITOR_CONTROL::AssignFootprints( const std::string& aChangedSetOfReferences )
45{
46 // Build a flat list of symbols in schematic:
49 bool isChanged = false;
50 bool appendToUndoList = false;
51
52 sheets.GetSymbols( refs, false );
53
54 DSNLEXER lexer( aChangedSetOfReferences, From_UTF8( __func__ ) );
55 PTREE doc;
56
57 try
58 {
59 Scan( &doc, &lexer );
60
61 CPTREE& back_anno = doc.get_child( "cvpcb_netlist" );
62 wxString footprint;
63
64 for( PTREE::const_iterator ref = back_anno.begin(); ref != back_anno.end(); ++ref )
65 {
66 wxASSERT( ref->first == "ref" );
67
68 wxString reference = (UTF8&) ref->second.front().first;
69
70 // Ensure the "fpid" node contains a footprint name, and get it if exists
71 if( ref->second.get_child( "fpid" ).size() )
72 {
73 wxString tmp = (UTF8&) ref->second.get_child( "fpid" ).front().first;
74 footprint = tmp;
75 }
76 else
77 footprint.Empty();
78
79 // Search the symbol in the flat list
80 for( unsigned ii = 0; ii < refs.GetCount(); ++ii )
81 {
82 if( reference == refs[ii].GetRef() )
83 {
84 // We have found a candidate.
85 // Note: it can be not unique (multiple parts per package)
86 // So we *do not* stop the search here
87 SCH_SYMBOL* symbol = refs[ ii ].GetSymbol();
88
89 // For backwards-compatibility CvPcb currently updates all instances of a
90 // symbol (even though it lists these instances separately).
91 wxString oldfp = refs[ii].GetFootprint();
92
93 if( oldfp.IsEmpty() && symbol->GetField( FOOTPRINT_FIELD )->IsVisible() )
94 symbol->GetField( FOOTPRINT_FIELD )->SetVisible( false );
95
96 if( oldfp != footprint )
97 {
98 isChanged = true;
99 SCH_SCREEN* screen = refs[ii].GetSheetPath().LastScreen();
100
101 m_frame->SaveCopyInUndoList( screen, symbol, UNDO_REDO::CHANGED,
102 appendToUndoList, false );
103 appendToUndoList = true;
104
105 symbol->SetFootprintFieldText( footprint );
106 }
107 }
108 }
109 }
110 }
111 catch( const PTREE_ERROR& ex )
112 {
113 // remap the exception to something the caller is likely to understand.
114 THROW_IO_ERROR( ex.what() );
115 }
116
117 if( isChanged )
118 {
119 m_frame->SyncView();
121 m_frame->OnModify();
122 }
123}
124
125
126bool SCH_EDITOR_CONTROL::processCmpToFootprintLinkFile( const wxString& aFullFilename,
127 bool aForceVisibilityState,
128 bool aVisibilityState )
129{
130 // Build a flat list of symbols in schematic:
131 SCH_REFERENCE_LIST referencesList;
132 SCH_SHEET_LIST sheetList = m_frame->Schematic().GetSheets();
133
134 sheetList.GetSymbols( referencesList, false );
135
136 FILE* cmpFile = wxFopen( aFullFilename, wxT( "rt" ) );
137
138 if( cmpFile == nullptr )
139 return false;
140
141 // cmpFileReader dtor will close cmpFile
142 FILE_LINE_READER cmpFileReader( cmpFile, aFullFilename );
143
144 // Now, for each symbol found in file,
145 // replace footprint field value by the new value:
146 wxString reference;
147 wxString footprint;
148 wxString buffer;
149 wxString value;
150
151 while( cmpFileReader.ReadLine() )
152 {
153 buffer = From_UTF8( cmpFileReader.Line() );
154
155 if( !buffer.StartsWith( wxT( "BeginCmp" ) ) )
156 continue;
157
158 // Begin symbol description.
159 reference.Empty();
160 footprint.Empty();
161
162 while( cmpFileReader.ReadLine() )
163 {
164 buffer = From_UTF8( cmpFileReader.Line() );
165
166 if( buffer.StartsWith( wxT( "EndCmp" ) ) )
167 break;
168
169 // store string value, stored between '=' and ';' delimiters.
170 value = buffer.AfterFirst( '=' );
171 value = value.BeforeLast( ';' );
172 value.Trim(true);
173 value.Trim(false);
174
175 if( buffer.StartsWith( wxT( "Reference" ) ) )
176 reference = value;
177 else if( buffer.StartsWith( wxT( "IdModule" ) ) )
178 footprint = value;
179 }
180
181 // A block is read: initialize the footprint field of the corresponding symbol
182 // if the footprint name is not empty
183 if( reference.IsEmpty() )
184 continue;
185
186 // Search the symbol in the flat list
187 for( unsigned ii = 0; ii < referencesList.GetCount(); ii++ )
188 {
189 if( reference == referencesList[ii].GetRef() )
190 {
191 // We have found a candidate.
192 // Note: it can be not unique (multiple units per part)
193 // So we *do not* stop the search here
194 SCH_SYMBOL* symbol = referencesList[ ii ].GetSymbol();
195
196 symbol->SetFootprintFieldText( footprint );
197
198 if( aForceVisibilityState )
199 symbol->GetField( FOOTPRINT_FIELD )->SetVisible( aVisibilityState );
200 }
201 }
202 }
203
204 return true;
205}
206
207
209{
210 wxString path = wxPathOnly( m_frame->Prj().GetProjectFullName() );
211
212 wxFileDialog dlg( m_frame, _( "Load Symbol Footprint Link File" ),
213 path, wxEmptyString,
215 wxFD_OPEN | wxFD_FILE_MUST_EXIST );
216
217 if( dlg.ShowModal() == wxID_CANCEL )
218 return 0;
219
220 wxString filename = dlg.GetPath();
221
222 wxArrayString choices;
223 choices.Add( _( "Keep existing footprint field visibility" ) );
224 choices.Add( _( "Show all footprint fields" ) );
225 choices.Add( _( "Hide all footprint fields" ) );
226
227 wxSingleChoiceDialog choiceDlg( m_frame, _( "Select the footprint field visibility setting." ),
228 _( "Change Visibility" ), choices );
229
230 if( choiceDlg.ShowModal() == wxID_CANCEL )
231 return 0;
232
233 bool forceVisibility = (choiceDlg.GetSelection() != 0 );
234 bool visibilityState = (choiceDlg.GetSelection() == 1 );
235
236 if( !processCmpToFootprintLinkFile( filename, forceVisibility, visibilityState ) )
237 {
238 wxString msg = wxString::Format( _( "Failed to open symbol-footprint link file '%s'." ),
239 filename.GetData() );
240
241 DisplayError( m_frame, msg );
242 return 0;
243 }
244
245 m_frame->SyncView();
247 m_frame->OnModify();
248 return 0;
249}
Implement a lexical analyzer for the SPECCTRA DSN file format.
Definition: dsnlexer.h:80
virtual void Refresh(bool aEraseBackground=true, const wxRect *aRect=nullptr) override
virtual bool IsVisible() const
Definition: eda_text.h:151
virtual void SetVisible(bool aVisible)
Definition: eda_text.cpp:245
A LINE_READER that reads from an open file.
Definition: richio.h:185
char * ReadLine() override
Read a line of text into the buffer and increments the line number counter.
Definition: richio.cpp:244
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
char * Line() const
Return a pointer to the last line that was read in.
Definition: richio.h:129
virtual const wxString GetProjectFullName() const
Return the full path and name of the project.
Definition: project.cpp:129
SCH_SHEET_LIST GetSheets() const override
Builds and returns an updated schematic hierarchy TODO: can this be cached?
Definition: schematic.h:100
SCH_DRAW_PANEL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
void SyncView()
Mark all items for refresh.
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 OnModify() override
Must be called after a schematic change in order to set the "modify" flag and update other data struc...
void SaveCopyInUndoList(SCH_SCREEN *aScreen, SCH_ITEM *aItemToCopy, UNDO_REDO aTypeCommand, bool aAppend, bool aDirtyConnectivity=true)
Create a copy of the current schematic item, and put it in the undo list.
SCHEMATIC & Schematic() const
Container to create a flattened list of symbols because in a complex hierarchy, a symbol can be used ...
size_t GetCount() const
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
void GetSymbols(SCH_REFERENCE_LIST &aReferences, bool aIncludePowerSymbols=true, bool aForceIncludeOrphanSymbols=false) const
Add a SCH_REFERENCE object to aReferences for each symbol in the list of sheets.
Schematic symbol object.
Definition: sch_symbol.h:108
SCH_FIELD * GetField(MANDATORY_FIELD_T aFieldType)
Return a mandatory field in this symbol.
Definition: sch_symbol.cpp:937
void SetFootprintFieldText(const wxString &aFootprint)
Definition: sch_symbol.cpp:931
Generic, UI-independent tool event.
Definition: tool_event.h:167
An 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to and fro...
Definition: utf8.h:72
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:280
This file is part of the common library.
#define _(s)
static wxString FootprintAssignmentFileWildcard()
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:39
void Scan(PTREE *aTree, DSNLEXER *aLexer)
Fill an empty PTREE with information from a KiCad s-expression stream.
Definition: ptree.cpp:86
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
Field Name Module PCB, i.e. "16DIP300".
Definition of file extensions used in Kicad.