KiCad PCB EDA Suite
pcbnew/dialogs/dialog_netlist.cpp
Go to the documentation of this file.
1 
6 /*
7  * This program source code file is part of KiCad, a free EDA CAD application.
8  *
9  * Copyright (C) 1992-2017 KiCad Developers, see change_log.txt for contributors.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, you may find one here:
23  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
24  * or you may search the http://www.gnu.org website for the version 2 license,
25  * or you may write to the Free Software Foundation, Inc.,
26  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
27  */
28 
29 #include <fctsys.h>
30 #include <project.h>
31 #include <kiface_i.h>
32 #include <confirm.h>
33 #include <macros.h>
34 #include <dialog_helpers.h>
35 #include <html_messagebox.h>
36 #include <base_units.h>
37 #include <pcb_edit_frame.h>
38 #include <pcb_netlist.h>
39 #include <netlist_reader.h>
40 #include <reporter.h>
41 #include <bitmaps.h>
42 #include <tool/tool_manager.h>
43 #include <board_design_settings.h>
44 #include <class_board.h>
45 #include <class_module.h>
48 #include <board_netlist_updater.h>
49 
50 #include <dialog_netlist.h>
51 #include <wx_html_report_panel.h>
52 #include <drc.h>
53 
54 #define NETLIST_FILTER_MESSAGES_KEY wxT("NetlistReportFilterMsg")
55 #define NETLIST_UPDATEFOOTPRINTS_KEY wxT("NetlistUpdateFootprints")
56 #define NETLIST_DELETESHORTINGTRACKS_KEY wxT("NetlistDeleteShortingTracks")
57 #define NETLIST_DELETEEXTRAFOOTPRINTS_KEY wxT("NetlistDeleteExtraFootprints")
58 #define NETLIST_DELETESINGLEPADNETS_KEY wxT("NetlistDeleteSinglePadNets")
59 
61 {
62  wxString netlistName = GetLastNetListRead();
63 
64  DIALOG_NETLIST dlg( this, netlistName );
65 
66  dlg.ShowModal();
67 
68  // Save project settings if needed.
69  // Project settings are saved in the corresponding <board name>.pro file
70  bool configChanged = !GetLastNetListRead().IsEmpty() && ( netlistName != GetLastNetListRead() );
71 
72  if( configChanged && !GetBoard()->GetFileName().IsEmpty() )
73  {
74  wxFileName fn = Prj().AbsolutePath( GetBoard()->GetFileName() );
75  fn.SetExt( ProjectFileExtension );
76  wxString path = fn.GetFullPath();
77  Prj().ConfigSave( Kiface().KifaceSearch(), GROUP_PCB, GetProjectFileParameters(), path );
78  }
79 }
80 
81 
82 DIALOG_NETLIST::DIALOG_NETLIST( PCB_EDIT_FRAME* aParent, const wxString & aNetlistFullFilename )
83  : DIALOG_NETLIST_BASE( aParent ),
84  m_parent( aParent ),
85  m_initialized( false ),
86  m_runDragCommand( false )
87 {
89 
90  m_NetlistFilenameCtrl->SetValue( aNetlistFullFilename );
91  m_browseButton->SetBitmap( KiBitmap( folder_xpm ) );
92 
97 
98  m_MessageWindow->SetLabel( _("Changes To Be Applied") );
100 
101  // We use a sdbSizer to get platform-dependent ordering of the action buttons, but
102  // that requires us to correct the button labels here.
103  m_sdbSizer1OK->SetLabel( _( "Update PCB" ) );
104  m_sdbSizer1Apply->SetLabel( _( "Rebuild Ratsnest" ) );
105  m_sdbSizer1Cancel->SetLabel( _( "Close" ) );
106  m_buttonsSizer->Layout();
107 
108  m_sdbSizer1OK->SetDefault();
110 
111  m_initialized = true;
112  loadNetlist( true );
113 }
114 
116 {
122 
123  if( m_runDragCommand )
124  m_parent->GetToolManager()->InvokeTool( "pcbnew.InteractiveEdit" );
125 }
126 
127 
128 void DIALOG_NETLIST::OnOpenNetlistClick( wxCommandEvent& event )
129 {
130  wxString dirPath = wxFileName( Prj().GetProjectFullName() ).GetPath();
131 
132  wxString filename = m_parent->GetLastNetListRead();
133 
134  if( !filename.IsEmpty() )
135  {
136  wxFileName fn = filename;
137  dirPath = fn.GetPath();
138  filename = fn.GetFullName();
139  }
140 
141  wxFileDialog FilesDialog( this, _( "Select Netlist" ), dirPath, filename,
142  NetlistFileWildcard(), wxFD_DEFAULT_STYLE | wxFD_FILE_MUST_EXIST );
143 
144  if( FilesDialog.ShowModal() != wxID_OK )
145  return;
146 
147  m_NetlistFilenameCtrl->SetValue( FilesDialog.GetPath() );
149 }
150 
151 void DIALOG_NETLIST::OnUpdatePCB( wxCommandEvent& event )
152 {
153  wxFileName fn = m_NetlistFilenameCtrl->GetValue();
154 
155  if( !fn.IsOk() )
156  {
157  wxMessageBox( _("Please, choose a valid netlist file.") );
158  return;
159  }
160 
161  if( !fn.FileExists() )
162  {
163  wxMessageBox( _("The netlist file does not exist.") );
164  return;
165  }
166 
167  // Give the user a chance to bail out when making changes from a netlist.
168  if( m_parent->GetBoard()->IsEmpty()
169  || IsOK( this, _( "The changes made cannot be undone. Are you sure you want to update the PCB?" ) ) )
170  {
171  m_MessageWindow->SetLabel( _( "Changes Applied To PCB" ) );
172  loadNetlist( false );
173 
174  m_sdbSizer1Cancel->SetDefault();
175  }
176 }
177 
178 
179 void DIALOG_NETLIST::OnTestFootprintsClick( wxCommandEvent& event )
180 {
181  if( m_parent->GetBoard()->m_Modules == nullptr )
182  {
183  DisplayInfoMessage( this, _( "No footprints." ) );
184  return;
185  }
186 
187  wxString netlistFilename = m_NetlistFilenameCtrl->GetValue();
188  NETLIST netlist;
189  wxBusyCursor dummy; // Shows an hourglass while calculating.
190 
191  if( !m_parent->ReadNetlistFromFile( netlistFilename, netlist, NULL_REPORTER::GetInstance() ) )
192  return;
193 
194  HTML_MESSAGE_BOX dlg( this, _( "Check footprints" ) );
195  DRC_LIST drcItems;
196 
197  DRC::TestFootprints( netlist, m_parent->GetBoard(), GetUserUnits(), drcItems );
198 
199  for( DRC_ITEM* item : drcItems )
200  dlg.AddHTML_Text( item->ShowHtml( GetUserUnits() ) );
201 
202  dlg.ShowModal();
203 }
204 
205 
206 void DIALOG_NETLIST::OnFilenameKillFocus( wxFocusEvent& event )
207 {
209 }
210 
211 
213 {
214  if( m_initialized )
215  {
216  wxFileName fn = m_NetlistFilenameCtrl->GetValue();
217  if( fn.IsOk() )
218  {
219  if( fn.FileExists() )
220  {
221  loadNetlist( true );
222  }
223  else
224  {
226  REPORTER& reporter = m_MessageWindow->Reporter();
227  reporter.Report( _("The netlist file does not exist."), REPORTER::RPT_ERROR );
228  }
229  }
230  }
231 }
232 
233 
234 void DIALOG_NETLIST::OnMatchChanged( wxCommandEvent& event )
235 {
236  if( m_initialized )
237  loadNetlist( true );
238 }
239 
240 
241 void DIALOG_NETLIST::OnOptionChanged( wxCommandEvent& event )
242 {
243  if( m_initialized )
244  loadNetlist( true );
245 }
246 
247 
248 void DIALOG_NETLIST::OnCompileRatsnestClick( wxCommandEvent& event )
249 {
250  // Rebuild the board connectivity:
251  auto board = m_parent->GetBoard();
252  board->GetConnectivity()->RecalculateRatsnest();
253 }
254 
255 
256 void DIALOG_NETLIST::OnUpdateUIValidNetlistFile( wxUpdateUIEvent& aEvent )
257 {
258  aEvent.Enable( !m_NetlistFilenameCtrl->GetValue().IsEmpty() );
259 }
260 
261 
262 void DIALOG_NETLIST::loadNetlist( bool aDryRun )
263 {
264  wxString netlistFileName = m_NetlistFilenameCtrl->GetValue();
265  wxFileName fn = netlistFileName;
266 
267  if( !fn.IsOk() || !fn.FileExists() )
268  return;
269 
271  REPORTER& reporter = m_MessageWindow->Reporter();
272 
273  wxBusyCursor busy;
274 
275  wxString msg;
276  msg.Printf( _( "Reading netlist file \"%s\".\n" ), GetChars( netlistFileName ) );
277  reporter.ReportHead( msg, REPORTER::RPT_INFO );
278 
279  if( m_matchByTimestamp->GetSelection() == 0 )
280  msg = _( "Using references to match components and footprints.\n" );
281  else
282  msg = _( "Using tstamp fields to match components and footprints.\n" );
283 
284  reporter.ReportHead( msg, REPORTER::RPT_INFO );
285  m_MessageWindow->SetLazyUpdate( true ); // Use lazy update to speed the creation of the report
286  // (the window is not updated for each message)
287  NETLIST netlist;
288 
290  netlist.SetFindByTimeStamp( m_matchByTimestamp->GetSelection() == 1 );
291  netlist.SetReplaceFootprints( m_cbUpdateFootprints->GetValue() );
292 
293  if( !m_parent->ReadNetlistFromFile( netlistFileName, netlist, reporter ) )
294  return;
295 
297  updater.SetReporter ( &reporter );
298  updater.SetIsDryRun( aDryRun );
299  updater.SetLookupByTimestamp( m_matchByTimestamp->GetSelection() == 1 );
300  updater.SetDeleteUnusedComponents ( m_cbDeleteExtraFootprints->GetValue() );
301  updater.SetReplaceFootprints( m_cbUpdateFootprints->GetValue() );
302  updater.SetDeleteSinglePadNets( m_cbDeleteSinglePadNets->GetValue() );
303  updater.UpdateNetlist( netlist );
304 
305  // The creation of the report was made without window update: the full page must be displayed
306  m_MessageWindow->Flush( true );
307 
308  if( aDryRun )
309  return;
310 
312 }
313 
314 
void SetVisibleSeverities(int aSeverities)
Set the visible severity filter.
void SetLazyUpdate(bool aLazyUpdate)
Sets the lasy update.
VTBL_ENTRY void ConfigSave(const SEARCH_STACK &aSList, const wxString &aGroupName, const PARAM_CFG_ARRAY &aParams, const wxString &aFileName=wxEmptyString)
Function ConfigSave saves the current "project" parameters into the wxConfigBase* derivative.
Definition: project.cpp:323
PARAM_CFG_ARRAY GetProjectFileParameters()
Function GetProjectFileParameters returns a project file parameter list for Pcbnew.
void OnMatchChanged(wxCommandEvent &event) override
Class DRC_ITEM is a holder for a DRC (in Pcbnew) or ERC (in Eeschema) error item.
Definition: drc_item.h:48
PCB_EDIT_FRAME * m_parent
Implementation of conversion functions that require both schematic and board internal units.
This file is part of the common library.
Class BOARD_NETLIST_UPDATER updates the BOARD with a new netlist.
const std::string ProjectFileExtension
Class BOARD to handle a board.
wxConfigBase * m_config
bool ReadNetlistFromFile(const wxString &aFilename, NETLIST &aNetlist, REPORTER &aReporter)
Reads a netlist from a file into a NETLIST object.
Definition: netlist.cpp:60
void Flush(bool aSort=false)
Forces updating the HTML page, after the report is built in lazy mode If aSort = true,...
wxConfigBase * KifaceSettings() const
Definition: kiface_i.h:103
void FinishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
BOARD_NETLIST_UPDATER class definition.
void SetFindByTimeStamp(bool aFindByTimeStamp)
Definition: pcb_netlist.h:311
void SetReporter(REPORTER *aReporter)
Sets the reporter object
Class REPORTER is a pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:61
void OnNetlistChanged(BOARD_NETLIST_UPDATER &aUpdater, bool *aRunDragCommand)
Called after netlist is updated.
Definition: netlist.cpp:95
void OnTestFootprintsClick(wxCommandEvent &event) override
const wxString & GetFileName() const
Definition: class_board.h:237
void OnFilenameKillFocus(wxFocusEvent &event) override
bool InvokeTool(TOOL_ID aToolId)
Function InvokeTool() Calls a tool by sending a tool activation event to tool of given ID.
#define NETLIST_DELETEEXTRAFOOTPRINTS_KEY
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
Definition: kicad.cpp:52
VTBL_ENTRY const wxString AbsolutePath(const wxString &aFileName) const
Function AbsolutePath fixes up aFileName if it is relative to the project's directory to be an absolu...
Definition: project.cpp:385
This file contains miscellaneous commonly used macros and functions.
static void TestFootprints(NETLIST &aNetlist, BOARD *aPCB, EDA_UNITS_T aUnits, DRC_LIST &aDRCList)
Test the board footprints against a netlist.
Definition: drc.cpp:1485
virtual REPORTER & ReportHead(const wxString &aText, SEVERITY aSeverity=RPT_UNDEFINED)
Function ReportHead Places the report at the beginning of the list for objects that support ordering.
Definition: reporter.h:120
wxBitmap KiBitmap(BITMAP_DEF aBitmap)
Construct a wxBitmap from a memory record, held in a BITMAP_DEF.
Definition: bitmap.cpp:79
#define NETLIST_DELETESINGLEPADNETS_KEY
#define NETLIST_FILTER_MESSAGES_KEY
void SetReplaceFootprints(bool aReplaceFootprints)
Definition: pcb_netlist.h:315
Class NETLIST stores all of information read from a netlist along with the flags used to update the N...
Definition: pcb_netlist.h:214
#define NETLIST_DELETESHORTINGTRACKS_KEY
void OnOpenNetlistClick(wxCommandEvent &event) override
wxString NetlistFileWildcard()
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
REPORTER & Reporter()
returns the reporter object that reports to this panel
TOOL_MANAGER * GetToolManager() const override
Return the tool manager instance, if any.
Definition: draw_frame.h:937
void OnOptionChanged(wxCommandEvent &event) override
Subclass of DIALOG_DISPLAY_HTML_TEXT_BASE, which is generated by wxFormBuilder.
std::shared_ptr< CONNECTIVITY_DATA > GetConnectivity() const
Function GetConnectivity() returns list of missing connections between components/tracks.
Definition: class_board.h:292
Definition of file extensions used in Kicad.
Helper dialog and control classes.
std::vector< DRC_ITEM * > DRC_LIST
Definition: drc.h:177
Class HTML_MESSAGE_BOX.
WX_HTML_REPORT_PANEL * m_MessageWindow
void OnUpdatePCB(wxCommandEvent &event) override
wxString GetLastNetListRead()
Get the last net list read with the net list dialog box.
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:100
void Clear()
clears the report panel
static LIB_PART * dummy()
Used when a LIB_PART is not found in library to draw a dummy shape This component is a 400 mils squar...
DLIST< MODULE > m_Modules
Definition: class_board.h:248
void AddHTML_Text(const wxString &message)
Function AddHTML_Text adds html text (without any change) to message list.
void OnUpdateUIValidNetlistFile(wxUpdateUIEvent &aEvent) override
Class PCB_EDIT_FRAME is the main frame for Pcbnew.
void OnCompileRatsnestClick(wxCommandEvent &event) override
DIALOG_NETLIST(PCB_EDIT_FRAME *aParent, const wxString &aNetlistFullFilename)
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_UNDEFINED)=0
Function Report is a pure virtual function to override in the derived object.
Module description (excepted pads)
void SetDeleteExtraFootprints(bool aDeleteExtraFootprints)
Definition: pcb_netlist.h:300
#define NETLIST_UPDATEFOOTPRINTS_KEY
static REPORTER & GetInstance()
Definition: reporter.cpp:104
BOARD * GetBoard() const
void DisplayInfoMessage(wxWindow *aParent, const wxString &aMessage, const wxString &aExtraInfo)
Function DisplayInfoMessage displays an informational message box with aMessage.
Definition: confirm.cpp:276
#define GROUP_PCB
Names of sub sections where to store project info in *.pro project config files.
Definition: config_params.h:43
void SetLabel(const wxString &aLabel) override
sets the frame label
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Function IsOK displays a yes/no dialog with aMessage and returns the user response.
Definition: confirm.cpp:294
bool IsEmpty() const
Definition: class_board.h:269
EDA_UNITS_T GetUserUnits() const override
Definition: dialog_shim.h:133