KiCad PCB EDA Suite
pcbnew/dialogs/dialog_netlist.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) 1992-2019 KiCad Developers, see AUTHORS.txt for contributors.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
24 #include <fctsys.h>
25 #include <project.h>
26 #include <kiface_i.h>
27 #include <confirm.h>
28 #include <macros.h>
29 #include <html_messagebox.h>
30 #include <pcb_edit_frame.h>
31 #include <pcbnew_settings.h>
32 #include <reporter.h>
33 #include <bitmaps.h>
34 #include <drc/drc.h>
35 #include <drc/drc_item.h>
36 #include <tool/tool_manager.h>
37 #include <tools/pcb_actions.h>
38 #include <class_board.h>
43 
44 #include <dialog_netlist.h>
45 
46 #include <wx_html_report_panel.h>
47 
48 
50 {
51  wxString netlistName = GetLastPath( LAST_PATH_NETLIST );
52 
53  DIALOG_NETLIST dlg( this, netlistName );
54 
55  dlg.ShowModal();
56 
57  SetLastPath( LAST_PATH_NETLIST, netlistName );
58 }
59 
62 
63 
64 DIALOG_NETLIST::DIALOG_NETLIST( PCB_EDIT_FRAME* aParent, wxString& aNetlistFullFilename )
65  : DIALOG_NETLIST_BASE( aParent ),
66  m_parent( aParent ),
67  m_netlistPath( aNetlistFullFilename ),
68  m_initialized( false ),
69  m_runDragCommand( false )
70 {
72  m_browseButton->SetBitmap( KiBitmap( folder_xpm ) );
73 
74  auto cfg = m_parent->GetSettings();
75 
76  m_cbUpdateFootprints->SetValue( cfg->m_NetlistDialog.update_footprints );
77  m_cbDeleteShortingTracks->SetValue( cfg->m_NetlistDialog.delete_shorting_tracks );
78  m_cbDeleteExtraFootprints->SetValue( cfg->m_NetlistDialog.delete_extra_footprints );
79  m_cbDeleteSinglePadNets->SetValue( cfg->m_NetlistDialog.delete_single_pad_nets );
80 
82  m_matchByTimestamp->SetSelection( m_matchByUUID ? 0 : 1 );
83 
84  m_MessageWindow->SetLabel( _("Changes To Be Applied") );
85  m_MessageWindow->SetVisibleSeverities( cfg->m_NetlistDialog.report_filter );
86 
87  // We use a sdbSizer to get platform-dependent ordering of the action buttons, but
88  // that requires us to correct the button labels here.
89  m_sdbSizer1OK->SetLabel( _( "Update PCB" ) );
90  m_sdbSizer1Apply->SetLabel( _( "Rebuild Ratsnest" ) );
91  m_sdbSizer1Cancel->SetLabel( _( "Close" ) );
92  m_buttonsSizer->Layout();
93 
94  m_sdbSizer1OK->SetDefault();
96 
97  m_initialized = true;
98  loadNetlist( true );
99 }
100 
102 {
103  m_warnForNoNetPads = m_cbWarnNoNetPad->GetValue();
104  m_matchByUUID = m_matchByTimestamp->GetSelection() == 0;
105 
106  auto cfg = m_parent->GetSettings();
107 
109  cfg->m_NetlistDialog.update_footprints = m_cbUpdateFootprints->GetValue();
110  cfg->m_NetlistDialog.delete_shorting_tracks = m_cbDeleteShortingTracks->GetValue();
111  cfg->m_NetlistDialog.delete_extra_footprints = m_cbDeleteExtraFootprints->GetValue();
112  cfg->m_NetlistDialog.delete_single_pad_nets = m_cbDeleteSinglePadNets->GetValue();
113 
114  if( m_runDragCommand )
115  {
117  controls->SetCursorPosition( controls->GetMousePosition() );
119  }
120 }
121 
122 
123 void DIALOG_NETLIST::OnOpenNetlistClick( wxCommandEvent& event )
124 {
125  wxString dirPath = wxFileName( Prj().GetProjectFullName() ).GetPath();
126 
127  wxString filename = m_parent->GetLastPath( LAST_PATH_NETLIST );
128 
129  if( !filename.IsEmpty() )
130  {
131  wxFileName fn = filename;
132  dirPath = fn.GetPath();
133  filename = fn.GetFullName();
134  }
135 
136  wxFileDialog FilesDialog( this, _( "Select Netlist" ), dirPath, filename,
137  NetlistFileWildcard(), wxFD_DEFAULT_STYLE | wxFD_FILE_MUST_EXIST );
138 
139  if( FilesDialog.ShowModal() != wxID_OK )
140  return;
141 
142  m_NetlistFilenameCtrl->SetValue( FilesDialog.GetPath() );
144 }
145 
146 void DIALOG_NETLIST::OnUpdatePCB( wxCommandEvent& event )
147 {
148  BOARD* pcb = m_parent->GetBoard();
149  wxFileName fn = m_NetlistFilenameCtrl->GetValue();
150 
151  if( !fn.IsOk() )
152  {
153  wxMessageBox( _("Please, choose a valid netlist file.") );
154  return;
155  }
156 
157  if( !fn.FileExists() )
158  {
159  wxMessageBox( _("The netlist file does not exist.") );
160  return;
161  }
162 
163  // Give the user a chance to bail out when making changes from a netlist.
164  if( pcb->IsEmpty() || IsOK( this, _( "The changes made cannot be undone. "
165  "Are you sure you want to update the PCB?" ) ) )
166  {
167  m_MessageWindow->SetLabel( _( "Changes Applied To PCB" ) );
168  loadNetlist( false );
169 
170  m_sdbSizer1Cancel->SetDefault();
171  }
172 }
173 
174 
175 void DIALOG_NETLIST::OnTestFootprintsClick( wxCommandEvent& event )
176 {
177  if( m_parent->GetBoard()->GetFirstModule() == nullptr )
178  {
179  DisplayInfoMessage( this, _( "No footprints." ) );
180  return;
181  }
182 
183  wxString netlistFilename = m_NetlistFilenameCtrl->GetValue();
184  NETLIST netlist;
185  wxBusyCursor dummy; // Shows an hourglass while calculating.
186 
187  if( !m_parent->ReadNetlistFromFile( netlistFilename, netlist, NULL_REPORTER::GetInstance() ) )
188  return;
189 
190  HTML_MESSAGE_BOX dlg( this, _( "Check footprints" ) );
191  std::vector<DRC_ITEM*> drcItems;
192 
193  DRC::TestFootprints( netlist, m_parent->GetBoard(), GetUserUnits(), drcItems );
194 
195  for( DRC_ITEM* item : drcItems )
196  dlg.AddHTML_Text( item->ShowHtml( GetUserUnits() ) );
197 
198  dlg.ShowModal();
199 }
200 
201 
202 void DIALOG_NETLIST::OnFilenameKillFocus( wxFocusEvent& event )
203 {
205  event.Skip();
206 }
207 
208 
210 {
211  if( m_initialized )
212  {
213  wxFileName fn = m_NetlistFilenameCtrl->GetValue();
214 
215  if( fn.IsOk() )
216  {
217  if( fn.FileExists() )
218  {
219  m_netlistPath = m_NetlistFilenameCtrl->GetValue();
220  loadNetlist( true );
221  }
222  else
223  {
225  REPORTER& reporter = m_MessageWindow->Reporter();
226  reporter.Report( _( "The netlist file does not exist." ), RPT_SEVERITY_ERROR );
227  }
228  }
229  }
230 }
231 
232 
233 void DIALOG_NETLIST::OnMatchChanged( wxCommandEvent& event )
234 {
235  if( m_initialized )
236  loadNetlist( true );
237 }
238 
239 
240 void DIALOG_NETLIST::OnOptionChanged( wxCommandEvent& event )
241 {
242  if( m_initialized )
243  loadNetlist( true );
244 }
245 
246 
247 void DIALOG_NETLIST::OnCompileRatsnestClick( wxCommandEvent& event )
248 {
249  // Rebuild the board connectivity:
250  auto board = m_parent->GetBoard();
251  board->GetConnectivity()->RecalculateRatsnest();
252 }
253 
254 
255 void DIALOG_NETLIST::OnUpdateUIValidNetlistFile( wxUpdateUIEvent& aEvent )
256 {
257  aEvent.Enable( !m_NetlistFilenameCtrl->GetValue().IsEmpty() );
258 }
259 
260 
261 void DIALOG_NETLIST::loadNetlist( bool aDryRun )
262 {
263  wxString netlistFileName = m_NetlistFilenameCtrl->GetValue();
264  wxFileName fn = netlistFileName;
265 
266  if( !fn.IsOk() || !fn.FileExists() )
267  return;
268 
270  REPORTER& reporter = m_MessageWindow->Reporter();
271 
272  wxBusyCursor busy;
273 
274  wxString msg;
275  msg.Printf( _( "Reading netlist file \"%s\".\n" ), GetChars( netlistFileName ) );
276  reporter.ReportHead( msg, RPT_SEVERITY_INFO );
277 
278  if( m_matchByTimestamp->GetSelection() == 1 )
279  msg = _( "Using reference designators to match components and footprints.\n" );
280  else
281  msg = _( "Using tstamps (unique IDs) to match components and footprints.\n" );
282 
283  reporter.ReportHead( msg, RPT_SEVERITY_INFO );
284  m_MessageWindow->SetLazyUpdate( true ); // Use lazy update to speed the creation of the report
285  // (the window is not updated for each message)
286  m_matchByUUID = m_matchByTimestamp->GetSelection() == 0;
287 
288  NETLIST netlist;
289 
292  netlist.SetReplaceFootprints( m_cbUpdateFootprints->GetValue() );
293 
294  if( !m_parent->ReadNetlistFromFile( netlistFileName, netlist, reporter ) )
295  return;
296 
298  updater.SetReporter ( &reporter );
299  updater.SetIsDryRun( aDryRun );
300  updater.SetLookupByTimestamp( m_matchByUUID );
301  updater.SetDeleteUnusedComponents ( m_cbDeleteExtraFootprints->GetValue() );
302  updater.SetReplaceFootprints( m_cbUpdateFootprints->GetValue() );
303  updater.SetDeleteSinglePadNets( m_cbDeleteSinglePadNets->GetValue() );
304  m_warnForNoNetPads = m_cbWarnNoNetPad->GetValue();
305  updater.SetWarnPadNoNetInNetlist( m_warnForNoNetPads );
306  updater.UpdateNetlist( netlist );
307 
308  // The creation of the report was made without window update: the full page must be displayed
309  m_MessageWindow->Flush( true );
310 
311  if( aDryRun )
312  return;
313 
315 }
316 
317 
void SetVisibleSeverities(int aSeverities)
Set the visible severity filter.
const BITMAP_OPAQUE folder_xpm[1]
Definition: folder.cpp:20
static void TestFootprints(NETLIST &aNetlist, BOARD *aPCB, EDA_UNITS aUnits, std::vector< DRC_ITEM * > &aDRCList)
Test the board footprints against a netlist.
void SetLazyUpdate(bool aLazyUpdate)
Sets the lasy update.
static TOOL_ACTION move
move or drag an item
Definition: pcb_actions.h:103
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
KIGFX::VIEW_CONTROLS * GetViewControls() const
Function GetViewControls() Returns a pointer to the VIEW_CONTROLS instance used in the panel.
void OnMatchChanged(wxCommandEvent &event) override
virtual VECTOR2D GetMousePosition(bool aWorldCoordinates=true) const =0
Function GetMousePosition() Returns the current mouse pointer position.
PCB_EDIT_FRAME * m_parent
This file is part of the common library.
BOARD_NETLIST_UPDATER updates the BOARD with a new netlist.
PCB_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
bool ReadNetlistFromFile(const wxString &aFilename, NETLIST &aNetlist, REPORTER &aReporter)
Reads a netlist from a file into a NETLIST object.
void Flush(bool aSort=false)
Forces updating the HTML page, after the report is built in lazy mode If aSort = true,...
void FinishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Function RunAction() Runs the specified action.
Definition: tool_manager.h:139
BOARD_NETLIST_UPDATER class definition.
virtual void SetCursorPosition(const VECTOR2D &aPosition, bool aWarpView=true, bool aTriggeredByArrows=false, long aArrowCommand=0)=0
Moves cursor to the requested position expressed in world coordinates.
void SetFindByTimeStamp(bool aFindByTimeStamp)
Definition: pcb_netlist.h:309
void SetReporter(REPORTER *aReporter)
Sets the reporter object
REPORTER is a pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:62
void OnNetlistChanged(BOARD_NETLIST_UPDATER &aUpdater, bool *aRunDragCommand)
Called after netlist is updated.
void OnTestFootprintsClick(wxCommandEvent &event) override
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)=0
Function Report is a pure virtual function to override in the derived object.
void OnFilenameKillFocus(wxFocusEvent &event) override
wxString GetLastPath(LAST_PATH_TYPE aType)
Get the last path for a particular type.
This file contains miscellaneous commonly used macros and functions.
EDA_UNITS GetUserUnits() const
Definition: dialog_shim.h:132
wxBitmap KiBitmap(BITMAP_DEF aBitmap)
Construct a wxBitmap from a memory record, held in a BITMAP_DEF.
Definition: bitmap.cpp:80
void SetReplaceFootprints(bool aReplaceFootprints)
Definition: pcb_netlist.h:313
NETLIST stores all of information read from a netlist along with the flags used to update the NETLIST...
Definition: pcb_netlist.h:212
DIALOG_NETLIST m_NetlistDialog
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
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:306
MODULE * GetFirstModule() const
Gets the first module in the list (used in footprint viewer/editor) or NULL if none.
Definition: class_board.h:283
Definition of file extensions used in Kicad.
VIEW_CONTROLS is an interface for classes handling user events controlling the view behaviour (such a...
HTML_MESSAGE_BOX.
static bool m_warnForNoNetPads
WX_HTML_REPORT_PANEL * m_MessageWindow
virtual REPORTER & ReportHead(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Function ReportHead Places the report at the beginning of the list for objects that support ordering.
Definition: reporter.h:101
void OnUpdatePCB(wxCommandEvent &event) override
DIALOG_NETLIST(PCB_EDIT_FRAME *aParent, wxString &aNetlistFullFilename)
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:101
void Clear()
clears the report panel
static LIB_PART * dummy()
Used to draw a dummy shape when a LIB_PART is not found in library.
BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:163
#define _(s)
Definition: 3d_actions.cpp:33
void AddHTML_Text(const wxString &message)
Add HTML text (without any change) to message list.
wxString & m_netlistPath
void OnUpdateUIValidNetlistFile(wxUpdateUIEvent &aEvent) override
PCB_EDIT_FRAME is the main frame for Pcbnew.
void OnCompileRatsnestClick(wxCommandEvent &event) override
void SetDeleteExtraFootprints(bool aDeleteExtraFootprints)
Definition: pcb_netlist.h:298
static REPORTER & GetInstance()
Definition: reporter.cpp:104
BOARD * GetBoard() const
static bool m_matchByUUID
void DisplayInfoMessage(wxWindow *aParent, const wxString &aMessage, const wxString &aExtraInfo)
Display an informational message box with aMessage.
Definition: confirm.cpp:267
void SetLabel(const wxString &aLabel) override
sets the frame label
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Display a yes/no dialog with aMessage and returns the user response.
Definition: confirm.cpp:283
bool IsEmpty() const
Definition: class_board.h:264
void SetLastPath(LAST_PATH_TYPE aType, const wxString &aLastPath)
Set the path of the last file successfully read.
PCBNEW_SETTINGS * GetSettings()