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 <pcbnew.h>
29 #include <class_board.h>
31 #include <eda_pattern_match.h>
33 #include <view/view.h>
34 #include <view/view_controls.h>
35 #include <pcb_painter.h>
37 
39 {
40 private:
41 public:
44 
45  // returns true if a net was selected, and its name in aName
46  bool GetNetName( wxString& aName );
47 
53  void HighlightNet( const wxString& aNetName );
54 
55 private:
56  void onSelChanged( wxDataViewEvent& event ) override;
57  void onFilterChange( wxCommandEvent& event ) override;
58  void onListSize( wxSizeEvent& event ) override;
59  void onReport( wxCommandEvent& event ) override;
60 
61  void buildNetsList();
62  wxString getListColumnHeaderNet() { return _( "Net" ); };
63  wxString getListColumnHeaderName() { return _( "Name" ); };
64  wxString getListColumnHeaderCount() { return _( "Pad Count" ); };
65  wxString getListColumnHeaderVias() { return _( "Via Count" ); };
66  wxString getListColumnHeaderBoard() { return _( "Board Length" ); };
67  wxString getListColumnHeaderDie() { return _( "Die Length" ); };
68  wxString getListColumnHeaderLength() { return _( "Length" ); };
69  void adjustListColumns();
70 
71  wxArrayString m_netsInitialNames; // The list of escaped netnames (original names)
72  wxString m_selection;
76 };
77 
78 
80 {
82  wxString netname;
83 
84  if( dlg.ShowModal() == wxID_CANCEL )
85  {
86  // Clear highlight
87  dlg.HighlightNet( "" );
88  }
89 
90  return 0;
91 }
92 
93 
95  : DIALOG_SELECT_NET_FROM_LIST_BASE( aParent ), m_frame( aParent )
96 {
97  m_brd = aParent->GetBoard();
98  m_wasSelected = false;
99 
100  m_netsList->AppendTextColumn( getListColumnHeaderNet(), wxDATAVIEW_CELL_INERT, 0, wxALIGN_LEFT, 0 );
101  m_netsList->AppendTextColumn( getListColumnHeaderName(), wxDATAVIEW_CELL_INERT, 0, wxALIGN_LEFT, 0 );
102  m_netsList->AppendTextColumn( getListColumnHeaderCount(), wxDATAVIEW_CELL_INERT, 0, wxALIGN_CENTER, 0 );
103  m_netsList->AppendTextColumn( getListColumnHeaderVias(), wxDATAVIEW_CELL_INERT, 0, wxALIGN_CENTER, 0 );
104  m_netsList->AppendTextColumn( getListColumnHeaderBoard(), wxDATAVIEW_CELL_INERT, 0, wxALIGN_CENTER, 0 );
105  m_netsList->AppendTextColumn( getListColumnHeaderDie(), wxDATAVIEW_CELL_INERT, 0, wxALIGN_CENTER, 0 );
106  m_netsList->AppendTextColumn( getListColumnHeaderLength(), wxDATAVIEW_CELL_INERT, 0, wxALIGN_CENTER, 0 );
107 
108  // The fact that we're a list should keep the control from reserving space for the
109  // expander buttons... but it doesn't. Fix by forcing the indent to 0.
110  m_netsList->SetIndent( 0 );
111 
112  buildNetsList();
113 
115 
116  m_sdbSizerOK->SetDefault();
117 
119 }
120 
121 
123 {
124  wxString netFilter = m_textCtrlFilter->GetValue();
126 
127  constexpr KICAD_T types[] = { PCB_TRACE_T, PCB_VIA_T, PCB_PAD_T, EOT };
128 
129  filter.SetPattern( netFilter.MakeUpper() );
130 
131  m_netsList->DeleteAllItems();
132  m_netsInitialNames.Clear();
133 
134  auto connectivity = m_brd->GetConnectivity();
135 
136  auto units = GetUserUnits();
137 
138  // Populate the nets list with nets names matching the filters:
139  // Note: the filtering is case insensitive.
140  for( unsigned netcode = 0; netcode < m_brd->GetNetCount(); netcode++ )
141  {
142  NETINFO_ITEM* net = m_brd->GetNetInfo().GetNetItem( netcode );
143 
144  if( !netFilter.IsEmpty() )
145  {
146  wxString netname = UnescapeString( net->GetNetname() );
147 
148  if( filter.Find( netname.MakeUpper() ) == EDA_PATTERN_NOT_FOUND )
149  continue;
150  }
151 
152  unsigned nodes = m_brd->GetNodesCount( netcode );
153 
154  if( !m_cbShowZeroPad->IsChecked() && nodes == 0 )
155  continue;
156 
157  wxVector<wxVariant> dataLine;
158 
159  dataLine.push_back( wxVariant( wxString::Format( "%.3d", netcode ) ) );
160  dataLine.push_back( wxVariant( UnescapeString( net->GetNetname() ) ) );
161  m_netsInitialNames.Add( net->GetNetname() );
162 
163  if( netcode )
164  {
165  dataLine.push_back( wxVariant( wxString::Format( "%u", nodes ) ) );
166 
167  int lenPadToDie = 0;
168  int len = 0;
169  int viaCount = 0;
170 
171  for( auto item : connectivity->GetNetItems( netcode, types ) )
172  {
173 
174  if( item->Type() == PCB_PAD_T )
175  {
176  D_PAD *pad = dyn_cast<D_PAD*>( item );
177  lenPadToDie += pad->GetPadToDieLength();
178  }
179  else if( item->Type() == PCB_TRACE_T )
180  {
181  TRACK *track = dyn_cast<TRACK*>( item );
182  len += track->GetLength();
183  }
184  else if( item->Type() == PCB_VIA_T )
185  {
186  viaCount++;
187  }
188  }
189 
190  dataLine.push_back( wxVariant( wxString::Format( "%u", viaCount ) ) );
191  dataLine.push_back( wxVariant( MessageTextFromValue( units, len ) ) );
192  dataLine.push_back( wxVariant( MessageTextFromValue( units, lenPadToDie ) ) );
193  dataLine.push_back( wxVariant( MessageTextFromValue( units, len + lenPadToDie ) ) );
194  }
195  else // For the net 0 (unconnected pads), the pad count is not known
196  {
197  dataLine.push_back( wxVariant( "---" ) );
198  dataLine.push_back( wxVariant( "---" ) ); // vias
199  dataLine.push_back( wxVariant( "---" ) ); // board
200  dataLine.push_back( wxVariant( "---" ) ); // die
201  dataLine.push_back( wxVariant( "---" ) ); // length
202  }
203 
204  m_netsList->AppendItem( dataLine );
205  }
206 
207  m_wasSelected = false;
208 }
209 
210 
211 void DIALOG_SELECT_NET_FROM_LIST::HighlightNet( const wxString& aNetName )
212 {
213  int netCode = -1;
214 
215  if( !aNetName.IsEmpty() )
216  {
217  NETINFO_ITEM* net = m_brd->FindNet( aNetName );
218 
219  if( net )
220  netCode = net->GetNet();
221  }
222 
224  render->SetHighlight( netCode >= 0, netCode );
225 
227  m_frame->GetCanvas()->Refresh();
228 }
229 
230 
232 {
233 }
234 
235 
236 void DIALOG_SELECT_NET_FROM_LIST::onFilterChange( wxCommandEvent& event )
237 {
238  buildNetsList();
239 }
240 
241 
243 {
244  int selected_row = m_netsList->GetSelectedRow();
245 
246  if( selected_row >= 0 )
247  {
248  // We no not use the displayed net name returnded by
249  // m_netsList->GetTextValue( selected_row, 1 ); because we need the initial escaped net name
250  m_selection = m_netsInitialNames[ selected_row ];
251  m_wasSelected = true;
252 
254  }
255  else
256  {
257  HighlightNet( "" );
258  m_wasSelected = false;
259  }
260 }
261 
262 
264 {
265  int w0, w1, w2, w3, w4, w5, w6;
266 
274  wxClientDC dc( GetParent() );
275  int h, minw, minw_col0;
276 
277  dc.GetTextExtent( getListColumnHeaderNet()+"MM", &w0, &h );
278  dc.GetTextExtent( getListColumnHeaderCount()+"MM", &w2, &h );
279  dc.GetTextExtent( getListColumnHeaderVias()+"MM", &w3, &h );
280  dc.GetTextExtent( getListColumnHeaderBoard()+"MM", &w4, &h );
281  dc.GetTextExtent( getListColumnHeaderDie()+"MM", &w5, &h );
282  dc.GetTextExtent( getListColumnHeaderLength()+"MM", &w6, &h );
283  dc.GetTextExtent( "M00000,000 mmM", &minw, &h );
284  dc.GetTextExtent( "M00000M", &minw_col0, &h );
285 
286  // Considering left and right margins.
287  // For wxRenderGeneric it is 5px.
288  w0 = std::max( w0+10, minw_col0);
289  w2 = std::max( w2+10, minw);
290  w3 = std::max( w3+10, minw);
291  w4 = std::max( w4+10, minw);
292  w5 = std::max( w5+10, minw);
293  w6 = std::max( w6+10, minw);
294 
295  m_netsList->GetColumn( 0 )->SetWidth( w0 );
296  m_netsList->GetColumn( 2 )->SetWidth( w2 );
297  m_netsList->GetColumn( 3 )->SetWidth( w3 );
298  m_netsList->GetColumn( 4 )->SetWidth( w4 );
299  m_netsList->GetColumn( 5 )->SetWidth( w5 );
300  m_netsList->GetColumn( 6 )->SetWidth( w6 );
301 
302  // At resizing of the list the width of middle column (Net Names) changes only.
303  int width = m_netsList->GetClientSize().x;
304  w1 = width - w0 - w2 - w3 - w4 - w5 - w6;
305 
306  // Column 1 (net names) need a minimal width to display net names
307  dc.GetTextExtent( "MMMMMMMMMMMMMMMMMMMM", &minw, &h );
308  w1 = std::max( w1, minw );
309 
310  m_netsList->GetColumn( 1 )->SetWidth( w1 );
311 
312  m_netsList->Refresh();
313 }
314 
315 
316 void DIALOG_SELECT_NET_FROM_LIST::onListSize( wxSizeEvent& aEvent )
317 {
318  aEvent.Skip();
320 }
321 
322 
324 {
325  aName = m_selection;
326  return m_wasSelected;
327 }
328 
329 
330 void DIALOG_SELECT_NET_FROM_LIST::onReport( wxCommandEvent& aEvent )
331 {
332  wxFileDialog dlg( this, _( "Report file" ), "", "",
333  _( "Report file" ) + AddFileExtListToFilter( { "csv" } ),
334  wxFD_SAVE );
335 
336  if( dlg.ShowModal() == wxID_CANCEL )
337  return;
338 
339  wxTextFile f( dlg.GetPath() );
340 
341  f.Create();
342 
343  int rows = m_netsList->GetItemCount();
344  wxString txt;
345 
346  // Print Header:
347  txt.Printf( "\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";",
348  _( "Net Id" ), _( "Net name" ),
349  _( "Pad count" ), _( "Via count" ),
350  _( "Board length" ), _( "Die length" ), _( "Net length" ) );
351  f.AddLine( txt );
352 
353  // Print list of nets:
354  for( int row = 1; row < rows; row++ )
355  {
356  txt.Printf( "%s;\"%s\";%s;%s;%s;%s;%s;",
357  m_netsList->GetTextValue( row, 0 ), // net id
358  m_netsList->GetTextValue( row, 1 ), // net name
359  m_netsList->GetTextValue( row, 2 ), // Pad count
360  m_netsList->GetTextValue( row, 3 ), // Via count
361  m_netsList->GetTextValue( row, 4 ), // Board length
362  m_netsList->GetTextValue( row, 5 ), // Die length
363  m_netsList->GetTextValue( row, 6 ) ); // net length
364 
365  f.AddLine( txt );
366  }
367 
368  f.Write();
369  f.Close();
370 }
371 
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.
PCB_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
int GetPadToDieLength() const
Definition: class_pad.h:436
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
PAINTER * GetPainter() const
Function GetPainter() Returns the painter object used by the view for drawing VIEW_ITEMS.
Definition: view.h:199
class TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:96
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:149
Abstract pattern-matching tool and implementations.
wxString MessageTextFromValue(EDA_UNITS_T aUnits, int aValue, bool aUseMils)
Definition: base_units.cpp:125
PCB_EDIT_FRAME * m_frame
void onSelChanged(wxDataViewEvent &event) override
unsigned GetNodesCount(int aNet=-1)
Function GetNodesCount.
unsigned GetNetCount() const
Function GetNetCount.
Definition: class_board.h:767
void SetHighlight(bool aEnabled, int aNetcode=-1, bool aHighlightItems=false)
Function SetHighlight Turns on/off highlighting - it may be done for the active layer,...
Definition: painter.h:141
Class DIALOG_SELECT_NET_FROM_LIST_BASE.
void onFilterChange(wxCommandEvent &event) override
Class TOOL_EVENT.
Definition: tool_event.h:171
std::shared_ptr< CONNECTIVITY_DATA > GetConnectivity() const
Function GetConnectivity() returns list of missing connections between components/tracks.
Definition: class_board.h:301
Definition of file extensions used in Kicad.
const wxString & GetNetname() const
Function GetNetname.
Definition: netinfo.h:233
static const int EDA_PATTERN_NOT_FOUND
NETINFO_LIST & GetNetInfo()
Definition: class_board.h:738
void HighlightNet(const wxString &aNetName)
Visually highlights a net.
#define _(s)
virtual KIGFX::PCB_VIEW * GetView() const override
Function GetView() Returns a pointer to the VIEW instance used in the panel.
virtual void Refresh(bool aEraseBackground=true, const wxRect *aRect=NULL) override
Update the board display after modifying it by a python script (note: it is automatically called by a...
virtual RENDER_SETTINGS * GetSettings()=0
Function GetSettings Returns pointer to current settings that are going to be used when drawing items...
Class NETINFO_ITEM handles the data for a net.
Definition: netinfo.h:65
EDA_UNITS_T GetUserUnits() const
Definition: dialog_shim.h:135
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:225
#define max(a, b)
Definition: auxiliary.h:86
Class BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:161
wxString UnescapeString(const wxString &aSource)
Definition: string.cpp:131
Class PCB_EDIT_FRAME is the main frame for Pcbnew.
int ListNets(const TOOL_EVENT &aEvent)
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:97
BOARD * GetBoard() const
NETINFO_ITEM * GetNetItem(int aNetCode) const
Function GetItem.
void UpdateAllLayersColor()
Function UpdateAllLayersColor() Applies the new coloring scheme to all layers.
Definition: view.cpp:798
void onReport(wxCommandEvent &event) override
wxString AddFileExtListToFilter(const std::vector< std::string > &aExts)
Build the wildcard extension file dialog wildcard filter to add to the base message dialog.