KiCad PCB EDA Suite
Loading...
Searching...
No Matches
readwrite_dlgs.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) 2018 Jean-Pierre Charras, jean-pierre.charras
5 * Copyright (C) 2011 Wayne Stambaugh <[email protected]>
6 * Copyright (C) 1992-2023 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 <fp_lib_table.h>
29#include <kiway.h>
30#include <lib_id.h>
31#include <string_utils.h>
32
33#include <cvpcb_mainframe.h>
35#include <project_pcb.h>
36#include <wx/msgdlg.h>
37
38
46static int guessNickname( FP_LIB_TABLE* aTbl, LIB_ID* aFootprintId )
47{
48 if( aFootprintId->GetLibNickname().size() )
49 return 0;
50
51 wxString nick;
52 wxString fpname = aFootprintId->GetLibItemName();
53
54 std::vector<wxString> nicks = aTbl->GetLogicalLibs();
55
56 // Search each library going through libraries alphabetically.
57 for( unsigned libNdx = 0; libNdx<nicks.size(); ++libNdx )
58 {
59 wxArrayString fpnames;
60
61 aTbl->FootprintEnumerate( fpnames, nicks[libNdx], true );
62
63 for( unsigned nameNdx = 0; nameNdx<fpnames.size(); ++nameNdx )
64 {
65 if( fpname == fpnames[nameNdx] )
66 {
67 if( !nick )
68 nick = nicks[libNdx];
69 else
70 return 2; // duplicate, the guess would not be certain
71 }
72 }
73 }
74
75 if( nick.size() )
76 {
77 aFootprintId->SetLibNickname( nick );
78 return 0;
79 }
80
81 return 1;
82}
83
84
85bool CVPCB_MAINFRAME::readNetListAndFpFiles( const std::string& aNetlist )
86{
87 wxString msg;
88 bool hasMissingNicks = false;
89
90 readSchematicNetlist( aNetlist );
91
92 if( m_symbolsListBox == nullptr )
93 return false;
94
95 wxSafeYield();
96
98
101
103
105 {
106 for( unsigned i = 0; i < m_netlist.GetCount(); i++ )
107 {
108 COMPONENT* component = m_netlist.GetComponent( i );
109
110 if( component->GetFPID().empty() )
111 continue;
112
113 if( component->GetFPID().IsLegacy() )
114 hasMissingNicks = true;
115 }
116 }
117
118 // Check if footprint links were generated before the footprint library table was implemented.
119 if( hasMissingNicks )
120 {
121 msg = _( "Some of the assigned footprints are legacy entries with no library names. Would "
122 "you like KiCad to attempt to convert them to the new required LIB_ID format? "
123 "(If you answer no, then these assignments will be cleared and you will need to "
124 "re-assign them manually.)" );
125
126 if( IsOK( this, msg ) )
127 {
128 msg.Clear();
129
130 try
131 {
132 for( unsigned i = 0; i < m_netlist.GetCount(); i++ )
133 {
134 COMPONENT* component = m_netlist.GetComponent( i );
135
136 if( component->GetFPID().IsLegacy() )
137 {
138 // get this first here, it's possibly obsoleted if we get it too soon.
140
141 int guess = guessNickname( tbl, (LIB_ID*) &component->GetFPID() );
142
143 switch( guess )
144 {
145 case 0:
146 m_modified = true;
147 break;
148
149 case 1:
150 msg += wxString::Format( _( "Component '%s' footprint '%s' <b>not "
151 "found</b> in any library.\n" ),
152 component->GetReference(),
153 component->GetFPID().GetLibItemName().wx_str() );
154 break;
155
156 case 2:
157 msg += wxString::Format( _( "Component '%s' footprint '%s' was found "
158 "in <b>multiple</b> libraries.\n" ),
159 component->GetReference(),
160 component->GetFPID().GetLibItemName().wx_str() );
161 break;
162 }
163 }
164 }
165 }
166 catch( const IO_ERROR& ioe )
167 {
168 msg = ioe.What();
169 msg += wxT( "\n\n" );
170 msg += _( "First check your footprint library table entries." );
171
172 wxMessageBox( msg, _( "Problematic Footprint Library Tables" ) );
173 return false;
174 }
175
176 if( msg.size() )
177 {
178 HTML_MESSAGE_BOX dlg( this, wxEmptyString );
179
180 dlg.MessageSet( _( "The following errors occurred attempting to convert the "
181 "footprint assignments:\n\n" ) );
182 dlg.ListSet( msg );
183 dlg.MessageSet( _( "\nYou will need to reassign them manually if you want them "
184 "to be updated correctly the next time you import the "
185 "netlist in Pcbnew." ) );
186
187#if 1
188 dlg.ShowModal();
189#else
190 dlg.Fit();
191
192 // Modeless lets user watch while fixing the problems, but its not working.
193 dlg.Show( true );
194#endif
195 }
196 }
197 else
198 {
199 // Clear the legacy footprint assignments.
200 for( unsigned i = 0; i < m_netlist.GetCount(); i++ )
201 {
202 COMPONENT* component = m_netlist.GetComponent( i );
203
204 if( component->GetFPID().IsLegacy() )
205 {
206 component->SetFPID( LIB_ID() );
207 m_modified = true;
208 }
209 }
210 }
211 }
212
213
214 // Display a dialog to select footprint selection, if the netlist
215 // and the .cmp file give 2 different valid footprints
216 std::vector <int > m_indexes; // indexes of footprints in netlist
217
218 for( unsigned ii = 0; ii < m_netlist.GetCount(); ii++ )
219 {
220 COMPONENT* component = m_netlist.GetComponent( ii );
221
222 if( component->GetAltFPID().empty() )
223 continue;
224
225 if( component->GetFPID().IsLegacy() || component->GetAltFPID().IsLegacy() )
226 continue;
227
228 m_indexes.push_back( ii );
229 }
230
231 // If a n assignment conflict is found,
232 // open a dialog to chose between schematic assignment
233 // and .cmp file assignment:
234 if( m_indexes.size() > 0 )
235 {
237
238 for( unsigned ii = 0; ii < m_indexes.size(); ii++ )
239 {
240 COMPONENT* component = m_netlist.GetComponent( m_indexes[ii] );
241
242 wxString cmpfpid = component->GetFPID().Format();
243 wxString schfpid = component->GetAltFPID().Format();
244
245 dlg.Add( component->GetReference(), schfpid, cmpfpid );
246 }
247
248 if( dlg.ShowModal() == wxID_OK )
249 {
250 // Update the fp selection:
251 for( unsigned ii = 0; ii < m_indexes.size(); ii++ )
252 {
253 COMPONENT* component = m_netlist.GetComponent( m_indexes[ii] );
254
255 int choice = dlg.GetSelection( component->GetReference() );
256
257 if( choice == 0 ) // the schematic (alt fpid) is chosen:
258 component->SetFPID( component->GetAltFPID() );
259 }
260 }
261 }
262
263 int firstUnassigned = wxNOT_FOUND;
264
265 // Populates the component list box:
266 for( unsigned i = 0; i < m_netlist.GetCount(); i++ )
267 {
268 COMPONENT* component = m_netlist.GetComponent( i );
269
271 component->GetReference(),
272 component->GetValue(),
273 From_UTF8( component->GetFPID().Format().c_str() ) );
274
276
277 if( firstUnassigned == wxNOT_FOUND && component->GetFPID().empty() )
278 firstUnassigned = i;
279
280 if( !m_FootprintsList->GetFootprintInfo( component->GetFPID().Format().wx_str() ) )
282 }
283
284 if( firstUnassigned >= 0 )
285 m_symbolsListBox->SetSelection( firstUnassigned, true );
286
288
289 return true;
290}
291
292
294{
295 std::string payload;
297
299
300 payload = sf.GetString();
302
303 if( doSaveSchematic )
304 {
305 payload = "";
307
308 if( payload == "success" )
309 SetStatusText( _( "Schematic saved" ), 1 );
310 }
311
312 // Changes are saved, so reset the flag
313 m_modified = false;
314
315 return true;
316}
Store all of the related footprint information found in a netlist.
Definition: pcb_netlist.h:88
const wxString & GetReference() const
Definition: pcb_netlist.h:129
const wxString & GetValue() const
Definition: pcb_netlist.h:132
void SetFPID(const LIB_ID &aFPID)
Definition: pcb_netlist.h:146
const LIB_ID & GetAltFPID() const
Definition: pcb_netlist.h:150
const LIB_ID & GetFPID() const
Definition: pcb_netlist.h:147
bool readNetListAndFpFiles(const std::string &aNetlist)
Load the netlist file built on the fly by Eeschema and loads footprint libraries from fp lib tables.
bool LoadFootprintFiles()
Read the list of footprint (*.mod files) and generate the list of footprints.
void SetStatusText(const wxString &aText, int aNumber=0) override
wxString formatSymbolDesc(int idx, const wxString &aReference, const wxString &aValue, const wxString &aFootprint)
void DisplayStatus()
Update the information displayed on the status bar at bottom of the main frame.
FOOTPRINT_LIST * m_FootprintsList
SYMBOLS_LISTBOX * m_symbolsListBox
int readSchematicNetlist(const std::string &aNetlist)
Read the netlist (.net) file built on the fly by Eeschema.
bool SaveFootprintAssociation(bool doSaveSchematic)
Save the edits that the user has done by sending them back to Eeschema via the kiway.
void Add(const wxString &aRef, const wxString &aFpSchName, const wxString &aFpCmpName)
Add a line to the selection list.
bool Show(bool show) override
int ShowModal() override
FOOTPRINT_INFO * GetFootprintInfo(const wxString &aFootprintName)
Get info for a footprint by id.
void FootprintEnumerate(wxArrayString &aFootprintNames, const wxString &aNickname, bool aBestEfforts)
Return a list of footprint names contained within the library given by aNickname.
void MessageSet(const wxString &message)
Add a message (in bold) to message list.
void ListSet(const wxString &aList)
Add a list of items.
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:77
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:30
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
KIWAY & Kiway() const
Return a reference to the KIWAY that this object has an opportunity to participate in.
Definition: kiway_holder.h:55
virtual void ExpressMail(FRAME_T aDestination, MAIL_T aCommand, std::string &aPayload, wxWindow *aSource=nullptr)
Send aPayload to aDestination from aSource.
Definition: kiway.cpp:527
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:49
int SetLibNickname(const UTF8 &aLibNickname)
Override the logical library name portion of the LIB_ID to aLibNickname.
Definition: lib_id.cpp:99
bool empty() const
Definition: lib_id.h:193
UTF8 Format() const
Definition: lib_id.cpp:118
const UTF8 & GetLibItemName() const
Definition: lib_id.h:102
bool IsLegacy() const
Definition: lib_id.h:180
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:87
std::vector< wxString > GetLogicalLibs()
Return the logical library names, all of them that are pertinent to a look up done on this LIB_TABLE.
unsigned GetCount() const
Definition: pcb_netlist.h:262
COMPONENT * GetComponent(unsigned aIndex)
Return the COMPONENT at aIndex.
Definition: pcb_netlist.h:270
void FormatCvpcbNetlist(OUTPUTFORMATTER *aOut)
Definition: pcb_netlist.h:316
bool AnyFootprintsLinked() const
static FP_LIB_TABLE * PcbFootprintLibs(PROJECT *aProject)
Return the table of footprint libraries without Kiway.
Definition: project_pcb.cpp:37
Implement an OUTPUTFORMATTER to a memory buffer.
Definition: richio.h:449
const std::string & GetString()
Definition: richio.h:472
void SetSelection(int index, bool State=true)
void AppendWarning(int index)
void AppendLine(const wxString &text)
const char * c_str() const
Definition: utf8.h:103
std::string::size_type size() const
Definition: utf8.h:111
wxString wx_str() const
Definition: utf8.cpp:45
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Display a yes/no dialog with aMessage and returns the user response.
Definition: confirm.cpp:250
This file is part of the common library.
#define _(s)
@ FRAME_SCH
Definition: frame_type.h:34
@ MAIL_SCH_SAVE
Definition: mail_type.h:43
@ MAIL_ASSIGN_FOOTPRINTS
Definition: mail_type.h:42
static int guessNickname(FP_LIB_TABLE *aTbl, LIB_ID *aFootprintId)
Return true if the resultant LIB_ID has a certain nickname.
wxString From_UTF8(const char *cstring)