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>
30 #include <class_track.h>
32 #include <eda_pattern_match.h>
34 #include <view/view.h>
35 #include <view/view_controls.h>
36 #include <pcb_painter.h>
38 
40 {
41 private:
42 public:
45 
46  // returns true if a net was selected, and its name in aName
47  bool GetNetName( wxString& aName );
48 
54  void HighlightNet( const wxString& aNetName );
55 
56 private:
57  void onSelChanged( wxDataViewEvent& event ) override;
58  void onFilterChange( wxCommandEvent& event ) override;
59  void onListSize( wxSizeEvent& event ) override;
60  void onReport( wxCommandEvent& event ) override;
61 
62  void buildNetsList();
63  wxString getListColumnHeaderNet() { return _( "Net" ); };
64  wxString getListColumnHeaderName() { return _( "Name" ); };
65  wxString getListColumnHeaderCount() { return _( "Pad Count" ); };
66  wxString getListColumnHeaderVias() { return _( "Via Count" ); };
67  wxString getListColumnHeaderBoard() { return _( "Board Length" ); };
68  wxString getListColumnHeaderDie() { return _( "Die Length" ); };
69  wxString getListColumnHeaderLength() { return _( "Length" ); };
70  void adjustListColumns();
71 
72  wxArrayString m_netsInitialNames; // The list of escaped netnames (original names)
73  wxString m_selection;
77 };
78 
79 
81 {
83  wxString netname;
84 
85  if( dlg.ShowModal() == wxID_CANCEL )
86  {
87  // Clear highlight
88  dlg.HighlightNet( "" );
89  }
90 
91  return 0;
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 
225  render->SetHighlight( netCode >= 0, netCode );
226 
228  m_frame->GetCanvas()->Refresh();
229 }
230 
231 
233 {
234 }
235 
236 
237 void DIALOG_SELECT_NET_FROM_LIST::onFilterChange( wxCommandEvent& event )
238 {
239  buildNetsList();
240 }
241 
242 
244 {
245  int selected_row = m_netsList->GetSelectedRow();
246 
247  if( selected_row >= 0 )
248  {
249  // We no not use the displayed net name returnded by
250  // m_netsList->GetTextValue( selected_row, 1 ); because we need the initial escaped net name
251  m_selection = m_netsInitialNames[ selected_row ];
252  m_wasSelected = true;
253 
255  }
256  else
257  {
258  HighlightNet( "" );
259  m_wasSelected = false;
260  }
261 }
262 
263 
265 {
266  int w0, w1, w2, w3, w4, w5, w6;
267 
275  wxClientDC dc( GetParent() );
276  int h, minw, minw_col0;
277 
278  dc.GetTextExtent( getListColumnHeaderNet()+"MM", &w0, &h );
279  dc.GetTextExtent( getListColumnHeaderCount()+"MM", &w2, &h );
280  dc.GetTextExtent( getListColumnHeaderVias()+"MM", &w3, &h );
281  dc.GetTextExtent( getListColumnHeaderBoard()+"MM", &w4, &h );
282  dc.GetTextExtent( getListColumnHeaderDie()+"MM", &w5, &h );
283  dc.GetTextExtent( getListColumnHeaderLength()+"MM", &w6, &h );
284  dc.GetTextExtent( "M00000,000 mmM", &minw, &h );
285  dc.GetTextExtent( "M00000M", &minw_col0, &h );
286 
287  // Considering left and right margins.
288  // For wxRenderGeneric it is 5px.
289  w0 = std::max( w0+10, minw_col0);
290  w2 = std::max( w2+10, minw);
291  w3 = std::max( w3+10, minw);
292  w4 = std::max( w4+10, minw);
293  w5 = std::max( w5+10, minw);
294  w6 = std::max( w6+10, minw);
295 
296  m_netsList->GetColumn( 0 )->SetWidth( w0 );
297  m_netsList->GetColumn( 2 )->SetWidth( w2 );
298  m_netsList->GetColumn( 3 )->SetWidth( w3 );
299  m_netsList->GetColumn( 4 )->SetWidth( w4 );
300  m_netsList->GetColumn( 5 )->SetWidth( w5 );
301  m_netsList->GetColumn( 6 )->SetWidth( w6 );
302 
303  // At resizing of the list the width of middle column (Net Names) changes only.
304  int width = m_netsList->GetClientSize().x;
305  w1 = width - w0 - w2 - w3 - w4 - w5 - w6;
306 
307  // Column 1 (net names) need a minimal width to display net names
308  dc.GetTextExtent( "MMMMMMMMMMMMMMMMMMMM", &minw, &h );
309  w1 = std::max( w1, minw );
310 
311  m_netsList->GetColumn( 1 )->SetWidth( w1 );
312 
313  m_netsList->Refresh();
314 }
315 
316 
317 void DIALOG_SELECT_NET_FROM_LIST::onListSize( wxSizeEvent& aEvent )
318 {
319  aEvent.Skip();
321 }
322 
323 
325 {
326  aName = m_selection;
327  return m_wasSelected;
328 }
329 
330 
331 void DIALOG_SELECT_NET_FROM_LIST::onReport( wxCommandEvent& aEvent )
332 {
333  wxFileDialog dlg( this, _( "Report file" ), "", "",
334  _( "Report file" ) + AddFileExtListToFilter( { "csv" } ),
335  wxFD_SAVE );
336 
337  if( dlg.ShowModal() == wxID_CANCEL )
338  return;
339 
340  wxTextFile f( dlg.GetPath() );
341 
342  f.Create();
343 
344  int rows = m_netsList->GetItemCount();
345  wxString txt;
346 
347  // Print Header:
348  txt.Printf( "\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";",
349  _( "Net Id" ), _( "Net name" ),
350  _( "Pad count" ), _( "Via count" ),
351  _( "Board length" ), _( "Die length" ), _( "Net length" ) );
352  f.AddLine( txt );
353 
354  // Print list of nets:
355  for( int row = 1; row < rows; row++ )
356  {
357  txt.Printf( "%s;\"%s\";%s;%s;%s;%s;%s;",
358  m_netsList->GetTextValue( row, 0 ), // net id
359  m_netsList->GetTextValue( row, 1 ), // net name
360  m_netsList->GetTextValue( row, 2 ), // Pad count
361  m_netsList->GetTextValue( row, 3 ), // Via count
362  m_netsList->GetTextValue( row, 4 ), // Board length
363  m_netsList->GetTextValue( row, 5 ), // Die length
364  m_netsList->GetTextValue( row, 6 ) ); // net length
365 
366  f.AddLine( txt );
367  }
368 
369  f.Write();
370  f.Close();
371 }
372 
NETINFO_ITEM * FindNet(int aNetcode) const
Function FindNet searches for a net with the given netcode.
RENDER_SETTINGS Contains all the knowledge about how graphical objects are drawn on any output surfac...
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:459
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
Definitions for tracks, vias and zones.
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.
Abstract pattern-matching tool and implementations.
EDA_UNITS GetUserUnits() const
Definition: dialog_shim.h:132
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:742
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:136
Class DIALOG_SELECT_NET_FROM_LIST_BASE.
void onFilterChange(wxCommandEvent &event) override
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:306
Definition of file extensions used in Kicad.
const wxString & GetNetname() const
Function GetNetname.
Definition: netinfo.h:232
static const int EDA_PATTERN_NOT_FOUND
NETINFO_LIST & GetNetInfo()
Definition: class_board.h:713
void HighlightNet(const wxString &aNetName)
Visually highlights a net.
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...
wxString MessageTextFromValue(EDA_UNITS aUnits, int aValue, bool aUseMils, EDA_DATA_TYPE aType)
Definition: base_units.cpp:127
virtual RENDER_SETTINGS * GetSettings()=0
Function GetSettings Returns pointer to current settings that are going to be used when drawing items...
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:224
BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:163
#define _(s)
Definition: 3d_actions.cpp:33
wxString UnescapeString(const wxString &aSource)
Definition: string.cpp:131
PCB_EDIT_FRAME is the main frame for Pcbnew.
virtual double GetLength() const
Function GetLength returns the length of the track using the hypotenuse calculation.
Definition: class_track.h:141
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.