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 <reporter.h>
32 #include <bitmaps.h>
33 #include <tool/tool_manager.h>
34 #include <tools/drc.h>
35 #include <tools/pcb_actions.h>
36 #include <class_board.h>
41 #include <dialog_netlist.h>
42 
43 #include <wx_html_report_panel.h>
44 
45 #define NETLIST_FILTER_MESSAGES_KEY wxT("NetlistReportFilterMsg")
46 #define NETLIST_UPDATEFOOTPRINTS_KEY wxT("NetlistUpdateFootprints")
47 #define NETLIST_DELETESHORTINGTRACKS_KEY wxT("NetlistDeleteShortingTracks")
48 #define NETLIST_DELETEEXTRAFOOTPRINTS_KEY wxT("NetlistDeleteExtraFootprints")
49 #define NETLIST_DELETESINGLEPADNETS_KEY wxT("NetlistDeleteSinglePadNets")
50 
52 {
53  wxString netlistName = GetLastPath( LAST_PATH_NETLIST );
54 
55  DIALOG_NETLIST dlg( this, netlistName );
56 
57  dlg.ShowModal();
58 
59  SetLastPath( LAST_PATH_NETLIST, netlistName );
60 }
61 
63 
64 
65 DIALOG_NETLIST::DIALOG_NETLIST( PCB_EDIT_FRAME* aParent, wxString& aNetlistFullFilename )
66  : DIALOG_NETLIST_BASE( aParent ),
67  m_parent( aParent ),
68  m_netlistPath( aNetlistFullFilename ),
69  m_initialized( false ),
70  m_runDragCommand( false )
71 {
73 
75  m_browseButton->SetBitmap( KiBitmap( folder_xpm ) );
76 
82 
83  m_MessageWindow->SetLabel( _("Changes To Be Applied") );
85 
86  // We use a sdbSizer to get platform-dependent ordering of the action buttons, but
87  // that requires us to correct the button labels here.
88  m_sdbSizer1OK->SetLabel( _( "Update PCB" ) );
89  m_sdbSizer1Apply->SetLabel( _( "Rebuild Ratsnest" ) );
90  m_sdbSizer1Cancel->SetLabel( _( "Close" ) );
91  m_buttonsSizer->Layout();
92 
93  m_sdbSizer1OK->SetDefault();
95 
96  m_initialized = true;
97  loadNetlist( true );
98 }
99 
101 {
102  m_warnForNoNetPads = m_cbWarnNoNetPad->GetValue();
103 
109 
110  if( m_runDragCommand )
111  {
113  controls->SetCursorPosition( controls->GetMousePosition() );
115  }
116 }
117 
118 
119 void DIALOG_NETLIST::OnOpenNetlistClick( wxCommandEvent& event )
120 {
121  wxString dirPath = wxFileName( Prj().GetProjectFullName() ).GetPath();
122 
123  wxString filename = m_parent->GetLastPath( LAST_PATH_NETLIST );
124 
125  if( !filename.IsEmpty() )
126  {
127  wxFileName fn = filename;
128  dirPath = fn.GetPath();
129  filename = fn.GetFullName();
130  }
131 
132  wxFileDialog FilesDialog( this, _( "Select Netlist" ), dirPath, filename,
133  NetlistFileWildcard(), wxFD_DEFAULT_STYLE | wxFD_FILE_MUST_EXIST );
134 
135  if( FilesDialog.ShowModal() != wxID_OK )
136  return;
137 
138  m_NetlistFilenameCtrl->SetValue( FilesDialog.GetPath() );
140 }
141 
142 void DIALOG_NETLIST::OnUpdatePCB( wxCommandEvent& event )
143 {
144  BOARD* pcb = m_parent->GetBoard();
145  wxFileName fn = m_NetlistFilenameCtrl->GetValue();
146 
147  if( !fn.IsOk() )
148  {
149  wxMessageBox( _("Please, choose a valid netlist file.") );
150  return;
151  }
152 
153  if( !fn.FileExists() )
154  {
155  wxMessageBox( _("The netlist file does not exist.") );
156  return;
157  }
158 
159  // Give the user a chance to bail out when making changes from a netlist.
160  if( pcb->IsEmpty() || IsOK( this, _( "The changes made cannot be undone. "
161  "Are you sure you want to update the PCB?" ) ) )
162  {
163  m_MessageWindow->SetLabel( _( "Changes Applied To PCB" ) );
164  loadNetlist( false );
165 
166  m_sdbSizer1Cancel->SetDefault();
167  }
168 }
169 
170 
171 void DIALOG_NETLIST::OnTestFootprintsClick( wxCommandEvent& event )
172 {
173  if( m_parent->GetBoard()->GetFirstModule() == nullptr )
174  {
175  DisplayInfoMessage( this, _( "No footprints." ) );
176  return;
177  }
178 
179  wxString netlistFilename = m_NetlistFilenameCtrl->GetValue();
180  NETLIST netlist;
181  wxBusyCursor dummy; // Shows an hourglass while calculating.
182 
183  if( !m_parent->ReadNetlistFromFile( netlistFilename, netlist, NULL_REPORTER::GetInstance() ) )
184  return;
185 
186  HTML_MESSAGE_BOX dlg( this, _( "Check footprints" ) );
187  DRC_LIST drcItems;
188 
189  DRC::TestFootprints( netlist, m_parent->GetBoard(), GetUserUnits(), drcItems );
190 
191  for( DRC_ITEM* item : drcItems )
192  dlg.AddHTML_Text( item->ShowHtml( GetUserUnits() ) );
193 
194  dlg.ShowModal();
195 }
196 
197 
198 void DIALOG_NETLIST::OnFilenameKillFocus( wxFocusEvent& event )
199 {
201  event.Skip();
202 }
203 
204 
206 {
207  if( m_initialized )
208  {
209  wxFileName fn = m_NetlistFilenameCtrl->GetValue();
210 
211  if( fn.IsOk() )
212  {
213  if( fn.FileExists() )
214  {
215  m_netlistPath = m_NetlistFilenameCtrl->GetValue();
216  loadNetlist( true );
217  }
218  else
219  {
221  REPORTER& reporter = m_MessageWindow->Reporter();
222  reporter.Report( _( "The netlist file does not exist." ), REPORTER::RPT_ERROR );
223  }
224  }
225  }
226 }
227 
228 
229 void DIALOG_NETLIST::OnMatchChanged( wxCommandEvent& event )
230 {
231  if( m_initialized )
232  loadNetlist( true );
233 }
234 
235 
236 void DIALOG_NETLIST::OnOptionChanged( wxCommandEvent& event )
237 {
238  if( m_initialized )
239  loadNetlist( true );
240 }
241 
242 
243 void DIALOG_NETLIST::OnCompileRatsnestClick( wxCommandEvent& event )
244 {
245  // Rebuild the board connectivity:
246  auto board = m_parent->GetBoard();
247  board->GetConnectivity()->RecalculateRatsnest();
248 }
249 
250 
251 void DIALOG_NETLIST::OnUpdateUIValidNetlistFile( wxUpdateUIEvent& aEvent )
252 {
253  aEvent.Enable( !m_NetlistFilenameCtrl->GetValue().IsEmpty() );
254 }
255 
256 
257 void DIALOG_NETLIST::loadNetlist( bool aDryRun )
258 {
259  wxString netlistFileName = m_NetlistFilenameCtrl->GetValue();
260  wxFileName fn = netlistFileName;
261 
262  if( !fn.IsOk() || !fn.FileExists() )
263  return;
264 
266  REPORTER& reporter = m_MessageWindow->Reporter();
267 
268  wxBusyCursor busy;
269 
270  wxString msg;
271  msg.Printf( _( "Reading netlist file \"%s\".\n" ), GetChars( netlistFileName ) );
272  reporter.ReportHead( msg, REPORTER::RPT_INFO );
273 
274  if( m_matchByTimestamp->GetSelection() == 0 )
275  msg = _( "Using references to match components and footprints.\n" );
276  else
277  msg = _( "Using tstamp fields to match components and footprints.\n" );
278 
279  reporter.ReportHead( msg, REPORTER::RPT_INFO );
280  m_MessageWindow->SetLazyUpdate( true ); // Use lazy update to speed the creation of the report
281  // (the window is not updated for each message)
282  NETLIST netlist;
283 
285  netlist.SetFindByTimeStamp( m_matchByTimestamp->GetSelection() == 1 );
286  netlist.SetReplaceFootprints( m_cbUpdateFootprints->GetValue() );
287 
288  if( !m_parent->ReadNetlistFromFile( netlistFileName, netlist, reporter ) )
289  return;
290 
292  updater.SetReporter ( &reporter );
293  updater.SetIsDryRun( aDryRun );
294  updater.SetLookupByTimestamp( m_matchByTimestamp->GetSelection() == 1 );
295  updater.SetDeleteUnusedComponents ( m_cbDeleteExtraFootprints->GetValue() );
296  updater.SetReplaceFootprints( m_cbUpdateFootprints->GetValue() );
297  updater.SetDeleteSinglePadNets( m_cbDeleteSinglePadNets->GetValue() );
298  m_warnForNoNetPads = m_cbWarnNoNetPad->GetValue();
299  updater.SetWarnPadNoNetInNetlist( m_warnForNoNetPads );
300  updater.UpdateNetlist( netlist );
301 
302  // The creation of the report was made without window update: the full page must be displayed
303  m_MessageWindow->Flush( true );
304 
305  if( aDryRun )
306  return;
307 
309 }
310 
311 
void SetVisibleSeverities(int aSeverities)
Set the visible severity filter.
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
Class DRC_ITEM is a holder for a DRC (in Pcbnew) or ERC (in Eeschema) error item.
Definition: drc_item.h:48
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.
Class BOARD_NETLIST_UPDATER updates the BOARD with a new netlist.
wxConfigBase * m_config
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.
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...
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Function RunAction() Runs the specified action.
Definition: tool_manager.h:109
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:314
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
void OnFilenameKillFocus(wxFocusEvent &event) override
wxString GetLastPath(LAST_PATH_TYPE aType)
Get the last path for a particular type.
#define NETLIST_DELETEEXTRAFOOTPRINTS_KEY
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
Definition: kicad.cpp:52
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:1353
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:318
Class NETLIST stores all of information read from a netlist along with the flags used to update the N...
Definition: pcb_netlist.h:217
#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
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:301
MODULE * GetFirstModule() const
Gets the first module in the list (used in footprint viewer/editor) or NULL if none.
Definition: class_board.h:276
Definition of file extensions used in Kicad.
Class VIEW_CONTROLS is an interface for classes handling user events controlling the view behaviour (...
std::vector< DRC_ITEM * > DRC_LIST
Definition: drc.h:175
Class HTML_MESSAGE_BOX.
#define _(s)
static bool m_warnForNoNetPads
WX_HTML_REPORT_PANEL * m_MessageWindow
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
EDA_UNITS_T GetUserUnits() const
Definition: dialog_shim.h:132
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.
Class BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:161
void AddHTML_Text(const wxString &message)
Add HTML text (without any change) to message list.
wxString & m_netlistPath
void OnUpdateUIValidNetlistFile(wxUpdateUIEvent &aEvent) override
Class PCB_EDIT_FRAME is the main frame for Pcbnew.
void OnCompileRatsnestClick(wxCommandEvent &event) override
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_UNDEFINED)=0
Function Report is a pure virtual function to override in the derived object.
void SetDeleteExtraFootprints(bool aDeleteExtraFootprints)
Definition: pcb_netlist.h:303
#define NETLIST_UPDATEFOOTPRINTS_KEY
static REPORTER & GetInstance()
Definition: reporter.cpp:104
BOARD * GetBoard() const
void DisplayInfoMessage(wxWindow *aParent, const wxString &aMessage, const wxString &aExtraInfo)
Display an informational message box with aMessage.
Definition: confirm.cpp:264
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:280
bool IsEmpty() const
Definition: class_board.h:257
void SetLastPath(LAST_PATH_TYPE aType, const wxString &aLastPath)
Set the path of the last file successfully read.