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:
48 bool isChanged = false;
49 bool appendToUndoList = false;
50
52
53 DSNLEXER lexer( aChangedSetOfReferences, From_UTF8( __func__ ) );
54 PTREE doc;
55
56 try
57 {
58 Scan( &doc, &lexer );
59
60 CPTREE& back_anno = doc.get_child( "cvpcb_netlist" );
61 wxString footprint;
62
63 for( PTREE::const_iterator ref = back_anno.begin(); ref != back_anno.end(); ++ref )
64 {
65 wxASSERT( ref->first == "ref" );
66
67 wxString reference = (UTF8&) ref->second.front().first;
68
69 // Ensure the "fpid" node contains a footprint name, and get it if exists
70 if( ref->second.get_child( "fpid" ).size() )
71 {
72 wxString tmp = (UTF8&) ref->second.get_child( "fpid" ).front().first;
73 footprint = tmp;
74 }
75 else
76 footprint.Empty();
77
78 // Search the symbol in the flat list
79 for( unsigned ii = 0; ii < refs.GetCount(); ++ii )
80 {
81 if( reference == refs[ii].GetRef() )
82 {
83 // We have found a candidate.
84 // Note: it can be not unique (multiple parts per package)
85 // So we *do not* stop the search here
86 SCH_SYMBOL* symbol = refs[ ii ].GetSymbol();
87
88 // For backwards-compatibility CvPcb currently updates all instances of a
89 // symbol (even though it lists these instances separately).
90 wxString oldfp = refs[ii].GetFootprint();
91
92 if( oldfp.IsEmpty() && symbol->GetField( FOOTPRINT_FIELD )->IsVisible() )
93 symbol->GetField( FOOTPRINT_FIELD )->SetVisible( false );
94
95 if( oldfp != footprint )
96 {
97 isChanged = true;
98 SCH_SCREEN* screen = refs[ii].GetSheetPath().LastScreen();
99
100 m_frame->SaveCopyInUndoList( screen, symbol, UNDO_REDO::CHANGED,
101 appendToUndoList, false );
102 appendToUndoList = true;
103
104 symbol->SetFootprintFieldText( 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();
120 m_frame->OnModify();
121 }
122}
123
124
125bool SCH_EDITOR_CONTROL::processCmpToFootprintLinkFile( const wxString& aFullFilename,
126 bool aForceVisibilityState,
127 bool aVisibilityState )
128{
129 // Build a flat list of symbols in schematic:
130 SCH_REFERENCE_LIST referencesList;
131 m_frame->Schematic().BuildUnorderedSheetList().GetSymbols( referencesList, false );
132
133 FILE* cmpFile = wxFopen( aFullFilename, wxT( "rt" ) );
134
135 if( cmpFile == nullptr )
136 return false;
137
138 // cmpFileReader dtor will close cmpFile
139 FILE_LINE_READER cmpFileReader( cmpFile, aFullFilename );
140
141 // Now, for each symbol found in file,
142 // replace footprint field value by the new value:
143 wxString reference;
144 wxString footprint;
145 wxString buffer;
146 wxString value;
147
148 while( cmpFileReader.ReadLine() )
149 {
150 buffer = From_UTF8( cmpFileReader.Line() );
151
152 if( !buffer.StartsWith( wxT( "BeginCmp" ) ) )
153 continue;
154
155 // Begin symbol description.
156 reference.Empty();
157 footprint.Empty();
158
159 while( cmpFileReader.ReadLine() )
160 {
161 buffer = From_UTF8( cmpFileReader.Line() );
162
163 if( buffer.StartsWith( wxT( "EndCmp" ) ) )
164 break;
165
166 // store string value, stored between '=' and ';' delimiters.
167 value = buffer.AfterFirst( '=' );
168 value = value.BeforeLast( ';' );
169 value.Trim(true);
170 value.Trim(false);
171
172 if( buffer.StartsWith( wxT( "Reference" ) ) )
173 reference = value;
174 else if( buffer.StartsWith( wxT( "IdModule" ) ) )
175 footprint = value;
176 }
177
178 // A block is read: initialize the footprint field of the corresponding symbol
179 // if the footprint name is not empty
180 if( reference.IsEmpty() )
181 continue;
182
183 // Search the symbol in the flat list
184 for( unsigned ii = 0; ii < referencesList.GetCount(); ii++ )
185 {
186 if( reference == referencesList[ii].GetRef() )
187 {
188 // We have found a candidate.
189 // Note: it can be not unique (multiple units per part)
190 // So we *do not* stop the search here
191 SCH_SYMBOL* symbol = referencesList[ ii ].GetSymbol();
192
193 symbol->SetFootprintFieldText( footprint );
194
195 if( aForceVisibilityState )
196 symbol->GetField( FOOTPRINT_FIELD )->SetVisible( aVisibilityState );
197 }
198 }
199 }
200
201 return true;
202}
203
204
206{
207 wxString path = wxPathOnly( m_frame->Prj().GetProjectFullName() );
208
209 wxFileDialog dlg( m_frame, _( "Load Symbol Footprint Link File" ),
210 path, wxEmptyString,
212 wxFD_OPEN | wxFD_FILE_MUST_EXIST );
213
214 if( dlg.ShowModal() == wxID_CANCEL )
215 return 0;
216
217 wxString filename = dlg.GetPath();
218
219 wxArrayString choices;
220 choices.Add( _( "Keep existing footprint field visibility" ) );
221 choices.Add( _( "Show all footprint fields" ) );
222 choices.Add( _( "Hide all footprint fields" ) );
223
224 wxSingleChoiceDialog choiceDlg( m_frame, _( "Select the footprint field visibility setting." ),
225 _( "Change Visibility" ), choices );
226
227 if( choiceDlg.ShowModal() == wxID_CANCEL )
228 return 0;
229
230 bool forceVisibility = (choiceDlg.GetSelection() != 0 );
231 bool visibilityState = (choiceDlg.GetSelection() == 1 );
232
233 if( !processCmpToFootprintLinkFile( filename, forceVisibility, visibilityState ) )
234 {
235 wxString msg = wxString::Format( _( "Failed to open symbol-footprint link file '%s'." ),
236 filename.GetData() );
237
238 DisplayError( m_frame, msg );
239 return 0;
240 }
241
242 m_frame->SyncView();
244 m_frame->OnModify();
245 return 0;
246}
Implement a lexical analyzer for the SPECCTRA DSN file format.
Definition: dsnlexer.h:81
virtual void Refresh(bool aEraseBackground=true, const wxRect *aRect=nullptr) override
virtual bool IsVisible() const
Definition: eda_text.h:147
virtual void SetVisible(bool aVisible)
Definition: eda_text.cpp:244
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 BuildUnorderedSheetList() const
Definition: schematic.h:101
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
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:105
SCH_FIELD * GetField(MANDATORY_FIELD_T aFieldType)
Return a mandatory field in this symbol.
Definition: sch_symbol.cpp:919
void SetFootprintFieldText(const wxString &aFootprint)
Definition: sch_symbol.cpp:913
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:170
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.