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, 2024 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
44
45void SCH_EDITOR_CONTROL::AssignFootprints( const std::string& aChangedSetOfReferences )
46{
47 // Build a flat list of symbols in schematic:
49 SCH_COMMIT commit( m_frame );
50 bool isChanged = false;
51
52 m_frame->Schematic().Hierarchy().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 commit.Modify( symbol, screen );
102
103 symbol->SetFootprintFieldText( footprint );
104 }
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 commit.Push( wxS( "Assign Footprints" ) );
119 }
120}
121
122
123bool SCH_EDITOR_CONTROL::processCmpToFootprintLinkFile( const wxString& aFullFilename,
124 bool aForceVisibilityState,
125 bool aVisibilityState )
126{
127 // Build a flat list of symbols in schematic:
128 SCH_REFERENCE_LIST referencesList;
129 m_frame->Schematic().Hierarchy().GetSymbols( referencesList, false );
130
131 FILE* cmpFile = wxFopen( aFullFilename, wxT( "rt" ) );
132
133 if( cmpFile == nullptr )
134 return false;
135
136 // cmpFileReader dtor will close cmpFile
137 FILE_LINE_READER cmpFileReader( cmpFile, aFullFilename );
138
139 // Now, for each symbol 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 symbol 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 symbol
177 // if the footprint name is not empty
178 if( reference.IsEmpty() )
179 continue;
180
181 // Search the symbol 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_SYMBOL* symbol = referencesList[ ii ].GetSymbol();
190
191 symbol->SetFootprintFieldText( footprint );
192
193 if( aForceVisibilityState )
194 symbol->GetField( FOOTPRINT_FIELD )->SetVisible( aVisibilityState );
195 }
196 }
197 }
198
199 return true;
200}
201
202
204{
205 wxString path = wxPathOnly( m_frame->Prj().GetProjectFullName() );
206
207 wxFileDialog dlg( m_frame, _( "Load Symbol Footprint Link File" ),
208 path, wxEmptyString,
210 wxFD_OPEN | wxFD_FILE_MUST_EXIST );
211
212 if( dlg.ShowModal() == wxID_CANCEL )
213 return 0;
214
215 wxString filename = dlg.GetPath();
216
217 wxArrayString choices;
218 choices.Add( _( "Keep existing footprint field visibility" ) );
219 choices.Add( _( "Show all footprint fields" ) );
220 choices.Add( _( "Hide all footprint fields" ) );
221
222 wxSingleChoiceDialog choiceDlg( m_frame, _( "Select the footprint field visibility setting." ),
223 _( "Change Visibility" ), choices );
224
225 if( choiceDlg.ShowModal() == wxID_CANCEL )
226 return 0;
227
228 bool forceVisibility = (choiceDlg.GetSelection() != 0 );
229 bool visibilityState = (choiceDlg.GetSelection() == 1 );
230
231 if( !processCmpToFootprintLinkFile( filename, forceVisibility, visibilityState ) )
232 {
233 wxString msg = wxString::Format( _( "Failed to open symbol-footprint link file '%s'." ),
234 filename.GetData() );
235
236 DisplayError( m_frame, msg );
237 return 0;
238 }
239
240 m_frame->SyncView();
242 m_frame->OnModify();
243 return 0;
244}
COMMIT & Modify(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Create an undo entry for an item that has been already modified.
Definition: commit.h:105
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:174
virtual void SetVisible(bool aVisible)
Definition: eda_text.cpp:377
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 Hierarchy() const override
Return the full schematic flattened hierarchical sheet list.
Definition: schematic.cpp:214
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.
virtual void Push(const wxString &aMessage=wxT("A commit"), int aCommitFlags=0) override
Revert the commit by restoring the modified items state.
Definition: sch_commit.cpp:432
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...
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:104
SCH_FIELD * GetField(MANDATORY_FIELD_T aFieldType)
Return a mandatory field in this symbol.
Definition: sch_symbol.cpp:939
void SetFootprintFieldText(const wxString &aFootprint)
Definition: sch_symbol.cpp:933
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.