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
37
45static int guessNickname( FP_LIB_TABLE* aTbl, LIB_ID* aFootprintId )
46{
47 if( aFootprintId->GetLibNickname().size() )
48 return 0;
49
50 wxString nick;
51 wxString fpname = aFootprintId->GetLibItemName();
52
53 std::vector<wxString> nicks = aTbl->GetLogicalLibs();
54
55 // Search each library going through libraries alphabetically.
56 for( unsigned libNdx = 0; libNdx<nicks.size(); ++libNdx )
57 {
58 wxArrayString fpnames;
59
60 aTbl->FootprintEnumerate( fpnames, nicks[libNdx], true );
61
62 for( unsigned nameNdx = 0; nameNdx<fpnames.size(); ++nameNdx )
63 {
64 if( fpname == fpnames[nameNdx] )
65 {
66 if( !nick )
67 nick = nicks[libNdx];
68 else
69 return 2; // duplicate, the guess would not be certain
70 }
71 }
72 }
73
74 if( nick.size() )
75 {
76 aFootprintId->SetLibNickname( nick );
77 return 0;
78 }
79
80 return 1;
81}
82
83
84bool CVPCB_MAINFRAME::readNetListAndFpFiles( const std::string& aNetlist )
85{
86 wxString msg;
87 bool hasMissingNicks = false;
88
89 readSchematicNetlist( aNetlist );
90
91 if( m_symbolsListBox == nullptr )
92 return false;
93
94 wxSafeYield();
95
97
100
102
104 {
105 for( unsigned i = 0; i < m_netlist.GetCount(); i++ )
106 {
107 COMPONENT* component = m_netlist.GetComponent( i );
108
109 if( component->GetFPID().empty() )
110 continue;
111
112 if( component->GetFPID().IsLegacy() )
113 hasMissingNicks = true;
114 }
115 }
116
117 // Check if footprint links were generated before the footprint library table was implemented.
118 if( hasMissingNicks )
119 {
120 msg = _( "Some of the assigned footprints are legacy entries with no library names. Would "
121 "you like KiCad to attempt to convert them to the new required LIB_ID format? "
122 "(If you answer no, then these assignments will be cleared and you will need to "
123 "re-assign them manually.)" );
124
125 if( IsOK( this, msg ) )
126 {
127 msg.Clear();
128
129 try
130 {
131 for( unsigned i = 0; i < m_netlist.GetCount(); i++ )
132 {
133 COMPONENT* component = m_netlist.GetComponent( i );
134
135 if( component->GetFPID().IsLegacy() )
136 {
137 // get this first here, it's possibly obsoleted if we get it too soon.
139
140 int guess = guessNickname( tbl, (LIB_ID*) &component->GetFPID() );
141
142 switch( guess )
143 {
144 case 0:
145 m_modified = true;
146 break;
147
148 case 1:
149 msg += wxString::Format( _( "Component '%s' footprint '%s' <b>not "
150 "found</b> in any library.\n" ),
151 component->GetReference(),
152 component->GetFPID().GetLibItemName().wx_str() );
153 break;
154
155 case 2:
156 msg += wxString::Format( _( "Component '%s' footprint '%s' was found "
157 "in <b>multiple</b> libraries.\n" ),
158 component->GetReference(),
159 component->GetFPID().GetLibItemName().wx_str() );
160 break;
161 }
162 }
163 }
164 }
165 catch( const IO_ERROR& ioe )
166 {
167 msg = ioe.What();
168 msg += wxT( "\n\n" );
169 msg += _( "First check your footprint library table entries." );
170
171 wxMessageBox( msg, _( "Problematic Footprint Library Tables" ) );
172 return false;
173 }
174
175 if( msg.size() )
176 {
177 HTML_MESSAGE_BOX dlg( this, wxEmptyString );
178
179 dlg.MessageSet( _( "The following errors occurred attempting to convert the "
180 "footprint assignments:\n\n" ) );
181 dlg.ListSet( msg );
182 dlg.MessageSet( _( "\nYou will need to reassign them manually if you want them "
183 "to be updated correctly the next time you import the "
184 "netlist in Pcbnew." ) );
185
186#if 1
187 dlg.ShowModal();
188#else
189 dlg.Fit();
190
191 // Modeless lets user watch while fixing the problems, but its not working.
192 dlg.Show( true );
193#endif
194 }
195 }
196 else
197 {
198 // Clear the legacy footprint assignments.
199 for( unsigned i = 0; i < m_netlist.GetCount(); i++ )
200 {
201 COMPONENT* component = m_netlist.GetComponent( i );
202
203 if( component->GetFPID().IsLegacy() )
204 {
205 component->SetFPID( LIB_ID() );
206 m_modified = true;
207 }
208 }
209 }
210 }
211
212
213 // Display a dialog to select footprint selection, if the netlist
214 // and the .cmp file give 2 different valid footprints
215 std::vector <int > m_indexes; // indexes of footprints in netlist
216
217 for( unsigned ii = 0; ii < m_netlist.GetCount(); ii++ )
218 {
219 COMPONENT* component = m_netlist.GetComponent( ii );
220
221 if( component->GetAltFPID().empty() )
222 continue;
223
224 if( component->GetFPID().IsLegacy() || component->GetAltFPID().IsLegacy() )
225 continue;
226
227 m_indexes.push_back( ii );
228 }
229
230 // If a n assignment conflict is found,
231 // open a dialog to chose between schematic assignment
232 // and .cmp file assignment:
233 if( m_indexes.size() > 0 )
234 {
236
237 for( unsigned ii = 0; ii < m_indexes.size(); ii++ )
238 {
239 COMPONENT* component = m_netlist.GetComponent( m_indexes[ii] );
240
241 wxString cmpfpid = component->GetFPID().Format();
242 wxString schfpid = component->GetAltFPID().Format();
243
244 dlg.Add( component->GetReference(), schfpid, cmpfpid );
245 }
246
247 if( dlg.ShowModal() == wxID_OK )
248 {
249 // Update the fp selection:
250 for( unsigned ii = 0; ii < m_indexes.size(); ii++ )
251 {
252 COMPONENT* component = m_netlist.GetComponent( m_indexes[ii] );
253
254 int choice = dlg.GetSelection( component->GetReference() );
255
256 if( choice == 0 ) // the schematic (alt fpid) is chosen:
257 component->SetFPID( component->GetAltFPID() );
258 }
259 }
260 }
261
262 int firstUnassigned = wxNOT_FOUND;
263
264 // Populates the component list box:
265 for( unsigned i = 0; i < m_netlist.GetCount(); i++ )
266 {
267 COMPONENT* component = m_netlist.GetComponent( i );
268
270 component->GetReference(),
271 component->GetValue(),
272 From_UTF8( component->GetFPID().Format().c_str() ) );
273
275
276 if( firstUnassigned == wxNOT_FOUND && component->GetFPID().empty() )
277 firstUnassigned = i;
278
279 if( !m_FootprintsList->GetFootprintInfo( component->GetFPID().Format().wx_str() ) )
281 }
282
283 if( firstUnassigned >= 0 )
284 m_symbolsListBox->SetSelection( firstUnassigned, true );
285
287
288 return true;
289}
290
291
293{
294 std::string payload;
296
298
299 payload = sf.GetString();
301
302 if( doSaveSchematic )
303 {
304 payload = "";
306
307 if( payload == "success" )
308 SetStatusText( _( "Schematic saved" ), 1 );
309 }
310
311 // Changes are saved, so reset the flag
312 m_modified = false;
313
314 return true;
315}
Store all of the related footprint information found in a netlist.
Definition: pcb_netlist.h:86
const wxString & GetReference() const
Definition: pcb_netlist.h:127
const wxString & GetValue() const
Definition: pcb_netlist.h:130
void SetFPID(const LIB_ID &aFPID)
Definition: pcb_netlist.h:144
const LIB_ID & GetAltFPID() const
Definition: pcb_netlist.h:148
const LIB_ID & GetFPID() const
Definition: pcb_netlist.h:145
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
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:244
COMPONENT * GetComponent(unsigned aIndex)
Return the COMPONENT at aIndex.
Definition: pcb_netlist.h:252
void FormatCvpcbNetlist(OUTPUTFORMATTER *aOut)
Definition: pcb_netlist.h:303
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:433
const std::string & GetString()
Definition: richio.h:456
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:360
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)