KiCad PCB EDA Suite
dialog_select_net_from_list.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) 2019 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 1992-2019 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 #include <fctsys.h>
26 #include <kicad_string.h>
27 #include <kicad_device_context.h>
28 #include <class_drawpanel.h>
29 #include <pcbnew.h>
30 #include <pcb_edit_frame.h>
31 #include <class_board.h>
33 #include <eda_pattern_match.h>
35 
36 #include <view/view.h>
37 #include <view/view_controls.h>
38 #include <pcb_painter.h>
40 
42 {
43 private:
44 public:
47 
48  // returns true if a net was selected, and its name in aName
49  bool GetNetName( wxString& aName );
50 
56  void HighlightNet( const wxString& aNetName );
57 
58 private:
59  void onSelChanged( wxDataViewEvent& event ) override;
60  void onFilterChange( wxCommandEvent& event ) override;
61  void onListSize( wxSizeEvent& event ) override;
62  void onReport( wxCommandEvent& event ) override;
63 
64  void buildNetsList();
65  wxString getListColumnHeaderNet() { return _( "Net" ); };
66  wxString getListColumnHeaderName() { return _( "Name" ); };
67  wxString getListColumnHeaderCount() { return _( "Pad Count" ); };
68  wxString getListColumnHeaderVias() { return _( "Via Count" ); };
69  wxString getListColumnHeaderBoard() { return _( "Board Length" ); };
70  wxString getListColumnHeaderDie() { return _( "Die Length" ); };
71  wxString getListColumnHeaderLength() { return _( "Length" ); };
72  void adjustListColumns();
73 
74  wxArrayString m_netsInitialNames; // The list of escaped netnames (original names)
75  wxString m_selection;
79 };
80 
81 
82 void PCB_EDIT_FRAME::ListNetsAndSelect( wxCommandEvent& event )
83 {
84  DIALOG_SELECT_NET_FROM_LIST dlg( this );
85  wxString netname;
86 
87  if( dlg.ShowModal() == wxID_CANCEL )
88  {
89  // Clear highlight
90  dlg.HighlightNet( "" );
91  }
92 }
93 
94 
96  : DIALOG_SELECT_NET_FROM_LIST_BASE( aParent ), m_frame( aParent )
97 {
98  m_brd = aParent->GetBoard();
99  m_wasSelected = false;
100 
101  m_netsList->AppendTextColumn( getListColumnHeaderNet(), wxDATAVIEW_CELL_INERT, 0, wxALIGN_LEFT, 0 );
102  m_netsList->AppendTextColumn( getListColumnHeaderName(), wxDATAVIEW_CELL_INERT, 0, wxALIGN_LEFT, 0 );
103  m_netsList->AppendTextColumn( getListColumnHeaderCount(), wxDATAVIEW_CELL_INERT, 0, wxALIGN_CENTER, 0 );
104  m_netsList->AppendTextColumn( getListColumnHeaderVias(), wxDATAVIEW_CELL_INERT, 0, wxALIGN_CENTER, 0 );
105  m_netsList->AppendTextColumn( getListColumnHeaderBoard(), wxDATAVIEW_CELL_INERT, 0, wxALIGN_CENTER, 0 );
106  m_netsList->AppendTextColumn( getListColumnHeaderDie(), wxDATAVIEW_CELL_INERT, 0, wxALIGN_CENTER, 0 );
107  m_netsList->AppendTextColumn( getListColumnHeaderLength(), wxDATAVIEW_CELL_INERT, 0, wxALIGN_CENTER, 0 );
108 
109  // The fact that we're a list should keep the control from reserving space for the
110  // expander buttons... but it doesn't. Fix by forcing the indent to 0.
111  m_netsList->SetIndent( 0 );
112 
113  buildNetsList();
114 
116 
117  m_sdbSizerOK->SetDefault();
118 
120 }
121 
122 
124 {
125  wxString netFilter = m_textCtrlFilter->GetValue();
127 
128  constexpr KICAD_T types[] = { PCB_TRACE_T, PCB_VIA_T, PCB_PAD_T, EOT };
129 
130  filter.SetPattern( netFilter.MakeUpper() );
131 
132  m_netsList->DeleteAllItems();
133  m_netsInitialNames.Clear();
134 
135  auto connectivity = m_brd->GetConnectivity();
136 
137  auto units = GetUserUnits();
138 
139  // Populate the nets list with nets names matching the filters:
140  // Note: the filtering is case insensitive.
141  for( unsigned netcode = 0; netcode < m_brd->GetNetCount(); netcode++ )
142  {
143  NETINFO_ITEM* net = m_brd->GetNetInfo().GetNetItem( netcode );
144 
145  if( !netFilter.IsEmpty() )
146  {
147  wxString netname = UnescapeString( net->GetNetname() );
148 
149  if( filter.Find( netname.MakeUpper() ) == EDA_PATTERN_NOT_FOUND )
150  continue;
151  }
152 
153  unsigned nodes = m_brd->GetNodesCount( netcode );
154 
155  if( !m_cbShowZeroPad->IsChecked() && nodes == 0 )
156  continue;
157 
158  wxVector<wxVariant> dataLine;
159 
160  dataLine.push_back( wxVariant( wxString::Format( "%.3d", netcode ) ) );
161  dataLine.push_back( wxVariant( UnescapeString( net->GetNetname() ) ) );
162  m_netsInitialNames.Add( net->GetNetname() );
163 
164  if( netcode )
165  {
166  dataLine.push_back( wxVariant( wxString::Format( "%u", nodes ) ) );
167 
168  int lenPadToDie = 0;
169  int len = 0;
170  int viaCount = 0;
171 
172  for( auto item : connectivity->GetNetItems( netcode, types ) )
173  {
174 
175  if( item->Type() == PCB_PAD_T )
176  {
177  D_PAD *pad = dyn_cast<D_PAD*>( item );
178  lenPadToDie += pad->GetPadToDieLength();
179  }
180  else if( item->Type() == PCB_TRACE_T )
181  {
182  TRACK *track = dyn_cast<TRACK*>( item );
183  len += track->GetLength();
184  }
185  else if( item->Type() == PCB_VIA_T )
186  {
187  viaCount++;
188  }
189  }
190 
191  dataLine.push_back( wxVariant( wxString::Format( "%u", viaCount ) ) );
192  dataLine.push_back( wxVariant( MessageTextFromValue( units, len ) ) );
193  dataLine.push_back( wxVariant( MessageTextFromValue( units, lenPadToDie ) ) );
194  dataLine.push_back( wxVariant( MessageTextFromValue( units, len + lenPadToDie ) ) );
195  }
196  else // For the net 0 (unconnected pads), the pad count is not known
197  {
198  dataLine.push_back( wxVariant( "---" ) );
199  dataLine.push_back( wxVariant( "---" ) ); // vias
200  dataLine.push_back( wxVariant( "---" ) ); // board
201  dataLine.push_back( wxVariant( "---" ) ); // die
202  dataLine.push_back( wxVariant( "---" ) ); // length
203  }
204 
205  m_netsList->AppendItem( dataLine );
206  }
207 
208  m_wasSelected = false;
209 }
210 
211 
212 void DIALOG_SELECT_NET_FROM_LIST::HighlightNet( const wxString& aNetName )
213 {
214  int netCode = -1;
215 
216  if( !aNetName.IsEmpty() )
217  {
218  NETINFO_ITEM* net = m_brd->FindNet( aNetName );
219 
220  if( net )
221  netCode = net->GetNet();
222  }
223 
224  if( m_frame->IsGalCanvasActive() )
225  {
226  auto galCanvas = m_frame->GetGalCanvas();
227  KIGFX::RENDER_SETTINGS* render = galCanvas->GetView()->GetPainter()->GetSettings();
228  render->SetHighlight( netCode >= 0, netCode );
229 
230  galCanvas->GetView()->UpdateAllLayersColor();
231  galCanvas->Refresh();
232  }
233  else
234  {
236 
237  if( m_brd->IsHighLightNetON() )
238  m_frame->HighLight( &dc );
239 
240  m_brd->SetHighLightNet( netCode );
241  m_frame->HighLight( &dc );
242  }
243 }
244 
245 
247 {
248 }
249 
250 
251 void DIALOG_SELECT_NET_FROM_LIST::onFilterChange( wxCommandEvent& event )
252 {
253  buildNetsList();
254 }
255 
256 
258 {
259  int selected_row = m_netsList->GetSelectedRow();
260 
261  if( selected_row >= 0 )
262  {
263  // We no not use the displayed net name returnded by
264  // m_netsList->GetTextValue( selected_row, 1 ); because we need the initial escaped net name
265  m_selection = m_netsInitialNames[ selected_row ];
266  m_wasSelected = true;
267 
269  }
270  else
271  {
272  HighlightNet( "" );
273  m_wasSelected = false;
274  }
275 }
276 
277 
279 {
280  int w0, w1, w2, w3, w4, w5, w6;
281 
289  wxClientDC dc( GetParent() );
290  int h, minw, minw_col0;
291 
292  dc.GetTextExtent( getListColumnHeaderNet()+"MM", &w0, &h );
293  dc.GetTextExtent( getListColumnHeaderCount()+"MM", &w2, &h );
294  dc.GetTextExtent( getListColumnHeaderVias()+"MM", &w3, &h );
295  dc.GetTextExtent( getListColumnHeaderBoard()+"MM", &w4, &h );
296  dc.GetTextExtent( getListColumnHeaderDie()+"MM", &w5, &h );
297  dc.GetTextExtent( getListColumnHeaderLength()+"MM", &w6, &h );
298  dc.GetTextExtent( "M00000,000 mmM", &minw, &h );
299  dc.GetTextExtent( "M00000M", &minw_col0, &h );
300 
301  // Considering left and right margins.
302  // For wxRenderGeneric it is 5px.
303  w0 = std::max( w0+10, minw_col0);
304  w2 = std::max( w2+10, minw);
305  w3 = std::max( w3+10, minw);
306  w4 = std::max( w4+10, minw);
307  w5 = std::max( w5+10, minw);
308  w6 = std::max( w6+10, minw);
309 
310  m_netsList->GetColumn( 0 )->SetWidth( w0 );
311  m_netsList->GetColumn( 2 )->SetWidth( w2 );
312  m_netsList->GetColumn( 3 )->SetWidth( w3 );
313  m_netsList->GetColumn( 4 )->SetWidth( w4 );
314  m_netsList->GetColumn( 5 )->SetWidth( w5 );
315  m_netsList->GetColumn( 6 )->SetWidth( w6 );
316 
317  // At resizing of the list the width of middle column (Net Names) changes only.
318  int width = m_netsList->GetClientSize().x;
319  w1 = width - w0 - w2 - w3 - w4 - w5 - w6;
320 
321  // Column 1 (net names) need a minimal width to display net names
322  dc.GetTextExtent( "MMMMMMMMMMMMMMMMMMMM", &minw, &h );
323  w1 = std::max( w1, minw );
324 
325  m_netsList->GetColumn( 1 )->SetWidth( w1 );
326 
327  m_netsList->Refresh();
328 }
329 
330 
331 void DIALOG_SELECT_NET_FROM_LIST::onListSize( wxSizeEvent& aEvent )
332 {
333  aEvent.Skip();
335 }
336 
337 
339 {
340  aName = m_selection;
341  return m_wasSelected;
342 }
343 
344 
345 void DIALOG_SELECT_NET_FROM_LIST::onReport( wxCommandEvent& aEvent )
346 {
347  wxFileDialog dlg( this, _( "Report file" ), "", "",
348  _( "Report file" ) + AddFileExtListToFilter( { "csv" } ),
349  wxFD_SAVE );
350 
351  if( dlg.ShowModal() == wxID_CANCEL )
352  return;
353 
354  wxTextFile f( dlg.GetPath() );
355 
356  f.Create();
357 
358  int rows = m_netsList->GetItemCount();
359  wxString txt;
360 
361  // Print Header:
362  txt.Printf( "\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";",
363  _( "Net Id" ), _( "Net name" ),
364  _( "Pad count" ), _( "Via count" ),
365  _( "Board length" ), _( "Die length" ), _( "Net length" ) );
366  f.AddLine( txt );
367 
368  // Print list of nets:
369  for( int row = 1; row < rows; row++ )
370  {
371  txt.Printf( "%s;\"%s\";%s;%s;%s;%s;%s;",
372  m_netsList->GetTextValue( row, 0 ), // net id
373  m_netsList->GetTextValue( row, 1 ), // net name
374  m_netsList->GetTextValue( row, 2 ), // Pad count
375  m_netsList->GetTextValue( row, 3 ), // Via count
376  m_netsList->GetTextValue( row, 4 ), // Board length
377  m_netsList->GetTextValue( row, 5 ), // Die length
378  m_netsList->GetTextValue( row, 6 ) ); // net length
379 
380  f.AddLine( txt );
381  }
382 
383  f.Write();
384  f.Close();
385 }
386 
NETINFO_ITEM * FindNet(int aNetcode) const
Function FindNet searches for a net with the given netcode.
Class RENDER_SETTINGS Contains all the knowledge about how graphical objects are drawn on any output ...
Definition: painter.h:56
VIEW_CONTROLS class definition.
Class BOARD to handle a board.
bool IsHighLightNetON() const
Function IsHighLightNetON.
Definition: class_board.h:383
EDA_DRAW_PANEL_GAL * GetGalCanvas() const
Return a pointer to GAL-based canvas of given EDA draw frame.
Definition: draw_frame.h:931
int GetPadToDieLength() const
Definition: class_pad.h:424
void FinishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
class D_PAD, a pad in a footprint
Definition: typeinfo.h:90
void onListSize(wxSizeEvent &event) override
DIALOG_SELECT_NET_FROM_LIST(PCB_EDIT_FRAME *aParent)
search types array terminator (End Of Types)
Definition: typeinfo.h:82
KICAD_T
Enum KICAD_T is the set of class identification values, stored in EDA_ITEM::m_StructType.
Definition: typeinfo.h:78
class TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:95
virtual bool SetPattern(const wxString &aPattern) override
Set the pattern against which candidates will be matched.
double GetLength() const
Function GetLength returns the length of the track using the hypotenuse calculation.
Definition: class_track.h:186
Abstract pattern-matching tool and implementations.
wxString MessageTextFromValue(EDA_UNITS_T aUnits, int aValue, bool aUseMils)
Definition: base_units.cpp:125
bool IsGalCanvasActive() const
Function IsGalCanvasActive is used to check which canvas (GAL-based or standard) is currently in use.
Definition: draw_frame.h:924
a helper to handle the real device context used in KiCad
void onSelChanged(wxDataViewEvent &event) override
unsigned GetNodesCount(int aNet=-1)
Function GetNodesCount.
unsigned GetNetCount() const
Function GetNetCount.
Definition: class_board.h:777
Class DIALOG_SELECT_NET_FROM_LIST_BASE.
void onFilterChange(wxCommandEvent &event) override
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.
const wxString & GetNetname() const
Function GetNetname.
Definition: netinfo.h:235
void HighLight(wxDC *DC)
Function HighLight.
Definition: highlight.cpp:104
static const int EDA_PATTERN_NOT_FOUND
virtual EDA_DRAW_PANEL * GetCanvas() const
Definition: draw_frame.h:399
NETINFO_LIST & GetNetInfo()
Definition: class_board.h:748
void HighlightNet(const wxString &aNetName)
Visually highlights a net.
void SetHighLightNet(int aNetCode)
Function SetHighLightNet.
Definition: class_board.h:374
Class NETINFO_ITEM handles the data for a net.
Definition: netinfo.h:65
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
int GetNet() const
Function GetNet.
Definition: netinfo.h:227
#define max(a, b)
Definition: auxiliary.h:86
Class BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:170
#define INSTALL_UNBUFFERED_DC(name, parent)
wxString UnescapeString(const wxString &aSource)
Definition: string.cpp:123
Class PCB_EDIT_FRAME is the main frame for Pcbnew.
void ListNetsAndSelect(wxCommandEvent &event)
Function ListNetsAndSelect called by a command event displays the sorted list of nets in a dialog fra...
virtual int Find(const wxString &aCandidate) const override
Return the location of a match iff a given candidate string matches the set pattern.
class VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:96
BOARD * GetBoard() const
NETINFO_ITEM * GetNetItem(int aNetCode) const
Function GetItem.
void onReport(wxCommandEvent &event) override
void SetHighlight(bool aEnabled, int aNetcode=-1)
Function SetHighlight Turns on/off highlighting - it may be done for the active layer or the specifie...
Definition: painter.h:140
wxString AddFileExtListToFilter(const std::vector< std::string > &aExts)
Build the wildcard extension file dialog wildcard filter to add to the base message dialog.
EDA_UNITS_T GetUserUnits() const override
Definition: dialog_shim.h:133