KiCad PCB EDA Suite
panel_setup_tracks_and_vias.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) 2018-2021 KiCad Developers, see change_log.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 
25 #include <base_units.h>
26 #include <pcb_edit_frame.h>
27 #include <board_design_settings.h>
28 #include <bitmaps.h>
29 #include <widgets/wx_grid.h>
30 #include <wx/treebook.h>
31 #include <grid_tricks.h>
32 
34 
35 
37 {
39 };
40 
42 {
45 };
46 
48 {
52 };
53 
54 
56  PCB_EDIT_FRAME* aFrame,
57  PANEL_SETUP_CONSTRAINTS* aConstraintsPanel ) :
58  PANEL_SETUP_TRACKS_AND_VIAS_BASE( aParent->GetTreebook() )
59 {
60  m_Parent = aParent;
61  m_Frame = aFrame;
62  m_Pcb = m_Frame->GetBoard();
64  m_ConstraintsPanel = aConstraintsPanel;
65 
72 
73  // Membership combobox editors require a bit more room, so increase the row size of
74  // all our grids for consistency
75  m_trackWidthsGrid->SetDefaultRowSize( m_trackWidthsGrid->GetDefaultRowSize() + 4 );
76  m_viaSizesGrid->SetDefaultRowSize( m_viaSizesGrid->GetDefaultRowSize() + 4 );
77  m_diffPairsGrid->SetDefaultRowSize( m_diffPairsGrid->GetDefaultRowSize() + 4 );
78 
79  m_trackWidthsGrid->PushEventHandler( new GRID_TRICKS( m_trackWidthsGrid ) );
80  m_viaSizesGrid->PushEventHandler( new GRID_TRICKS( m_viaSizesGrid ) );
81  m_diffPairsGrid->PushEventHandler( new GRID_TRICKS( m_diffPairsGrid ) );
82 
83  m_trackWidthsGrid->SetSelectionMode( wxGrid::wxGridSelectionModes::wxGridSelectRows );
84  m_viaSizesGrid->SetSelectionMode( wxGrid::wxGridSelectionModes::wxGridSelectRows );
85  m_diffPairsGrid->SetSelectionMode( wxGrid::wxGridSelectionModes::wxGridSelectRows );
86 
87  // Ensure width of columns is enough to enter any reasonable value
88  WX_GRID* grid_list[] = { m_trackWidthsGrid, m_viaSizesGrid, m_diffPairsGrid, nullptr };
89  int min_linesize = m_trackWidthsGrid->GetTextExtent( wxT( "000.000000 mm " ) ).x;
90 
91  for( int ii = 0; grid_list[ii]; ii++ )
92  {
93  WX_GRID* curr_grid = grid_list[ii];
94 
95  for( int col = 0; col < curr_grid->GetNumberCols(); col++ )
96  {
97  int min_w = curr_grid->GetVisibleWidth( col, true, true, true );
98  int best_w = std::max( min_linesize, min_w );
99  curr_grid->SetColMinimalWidth( col, best_w );
100  curr_grid->SetColSize( col,best_w );
101  }
102  }
103 
104  m_Frame->Bind( UNITS_CHANGED, &PANEL_SETUP_TRACKS_AND_VIAS::onUnitsChanged, this );
105 }
106 
107 
109 {
110  // Delete the GRID_TRICKS.
111  m_trackWidthsGrid->PopEventHandler( true );
112  m_viaSizesGrid->PopEventHandler( true );
113  m_diffPairsGrid->PopEventHandler( true );
114 
115  m_Frame->Unbind( UNITS_CHANGED, &PANEL_SETUP_TRACKS_AND_VIAS::onUnitsChanged, this );
116 }
117 
118 
119 void PANEL_SETUP_TRACKS_AND_VIAS::onUnitsChanged( wxCommandEvent& aEvent )
120 {
121  BOARD_DESIGN_SETTINGS tempBDS( nullptr, "dummy" );
123 
124  m_BrdSettings = &tempBDS; // No, address of stack var does not escape function
125 
128 
129  m_BrdSettings = saveBDS;
130 
131  aEvent.Skip();
132 }
133 
134 
136 {
140 
141  // Skip the first item, which is the current netclass value
142  for( unsigned ii = 1; ii < m_BrdSettings->m_TrackWidthList.size(); ii++ )
143  {
145  }
146 
147  // Skip the first item, which is the current netclass value
148  for( unsigned ii = 1; ii < m_BrdSettings->m_ViasDimensionsList.size(); ii++ )
149  {
151  m_BrdSettings->m_ViasDimensionsList[ii].m_Drill );
152  }
153 
154  // Skip the first item, which is the current netclass value
155  for( unsigned ii = 1; ii < m_BrdSettings->m_DiffPairDimensionsList.size(); ii++ )
156  {
159  m_BrdSettings->m_DiffPairDimensionsList[ii].m_ViaGap );
160  }
161 
162  return true;
163 }
164 
165 
167 {
171  {
172  return false;
173  }
174 
175  wxString msg;
176  std::vector<int> trackWidths;
177  std::vector<VIA_DIMENSION> vias;
178  std::vector<DIFF_PAIR_DIMENSION> diffPairs;
179 
183  {
184  return false;
185  }
186 
187  // Test ONLY for malformed data. Design rules and constraints are the business of DRC.
188 
189  for( int row = 0; row < m_trackWidthsGrid->GetNumberRows(); ++row )
190  {
191  msg = m_trackWidthsGrid->GetCellValue( row, TR_WIDTH_COL );
192 
193  if( !msg.IsEmpty() )
194  trackWidths.push_back( ValueFromString( m_Frame->GetUserUnits(), msg ) );
195  }
196 
197  for( int row = 0; row < m_viaSizesGrid->GetNumberRows(); ++row )
198  {
199  msg = m_viaSizesGrid->GetCellValue( row, VIA_SIZE_COL );
200 
201  if( !msg.IsEmpty() )
202  {
203  VIA_DIMENSION via_dim;
204  via_dim.m_Diameter = ValueFromString( m_Frame->GetUserUnits(), msg );
205 
206  msg = m_viaSizesGrid->GetCellValue( row, VIA_DRILL_COL );
207 
208  if( !msg.IsEmpty() )
209  via_dim.m_Drill = ValueFromString( m_Frame->GetUserUnits(), msg );
210 
211  vias.push_back( via_dim );
212  }
213  }
214 
215  for( int row = 0; row < m_diffPairsGrid->GetNumberRows(); ++row )
216  {
217  msg = m_diffPairsGrid->GetCellValue( row, DP_WIDTH_COL );
218 
219  if( !msg.IsEmpty() )
220  {
221  DIFF_PAIR_DIMENSION diffPair_dim;
222  diffPair_dim.m_Width = ValueFromString( m_Frame->GetUserUnits(), msg );
223 
224  msg = m_diffPairsGrid->GetCellValue( row, DP_GAP_COL );
225  diffPair_dim.m_Gap = ValueFromString( m_Frame->GetUserUnits(), msg );
226 
227  msg = m_diffPairsGrid->GetCellValue( row, DP_VIA_GAP_COL );
228 
229  if( !msg.IsEmpty() )
230  diffPair_dim.m_ViaGap = ValueFromString( m_Frame->GetUserUnits(), msg );
231 
232  diffPairs.push_back( diffPair_dim );
233  }
234  }
235 
236  // Sort lists by increasing value
237  sort( trackWidths.begin(), trackWidths.end() );
238  sort( vias.begin(), vias.end() );
239  sort( diffPairs.begin(), diffPairs.end() );
240 
241  // These are all stored in project file, not board, so no need for OnModify()
242 
243  trackWidths.insert( trackWidths.begin(), 0 ); // dummy value for "use netclass"
244  m_BrdSettings->m_TrackWidthList = trackWidths;
245 
246  vias.insert( vias.begin(), { 0, 0 } ); // dummy value for "use netclass"
248 
249  diffPairs.insert( diffPairs.begin(), { 0, 0, 0 } ); // dummy value for "use netclass"
251 
252  return true;
253 }
254 
255 
257 {
261  {
262  return false;
263  }
264 
265  wxString msg;
266 
267  // Test vias
268  for( int row = 0; row < m_viaSizesGrid->GetNumberRows(); ++row )
269  {
270  wxString viaDia = m_viaSizesGrid->GetCellValue( row, VIA_SIZE_COL );
271  wxString viaDrill = m_viaSizesGrid->GetCellValue( row, VIA_DRILL_COL );
272 
273  if( !viaDia.IsEmpty() && viaDrill.IsEmpty() )
274  {
275  msg = _( "No via hole size defined." );
276  m_Parent->SetError( msg, this, m_viaSizesGrid, row, VIA_DRILL_COL );
277  return false;
278  }
279 
280  }
281 
282  // Test diff pairs
283  for( int row = 0; row < m_diffPairsGrid->GetNumberRows(); ++row )
284  {
285  wxString dpWidth = m_diffPairsGrid->GetCellValue( row, 0 );
286  wxString dpGap = m_diffPairsGrid->GetCellValue( row, 1 );
287 
288  if( !dpWidth.IsEmpty() && dpGap.IsEmpty() )
289  {
290  msg = _( "No differential pair gap defined." );
291  m_Parent->SetError( msg, this, m_diffPairsGrid, row, 1 );
292  return false;
293  }
294  }
295 
296  return true;
297 }
298 
299 
301 {
302  int i = m_trackWidthsGrid->GetNumberRows();
303 
304  m_trackWidthsGrid->AppendRows( 1 );
305 
306  wxString val = StringFromValue( m_Frame->GetUserUnits(), aWidth, true );
307  m_trackWidthsGrid->SetCellValue( i, TR_WIDTH_COL, val );
308 }
309 
310 
311 void PANEL_SETUP_TRACKS_AND_VIAS::AppendViaSize( const int aSize, const int aDrill )
312 {
313  int i = m_viaSizesGrid->GetNumberRows();
314 
315  m_viaSizesGrid->AppendRows( 1 );
316 
317  wxString val = StringFromValue( m_Frame->GetUserUnits(), aSize, true );
318  m_viaSizesGrid->SetCellValue( i, VIA_SIZE_COL, val );
319 
320  if( aDrill > 0 )
321  {
322  val = StringFromValue( m_Frame->GetUserUnits(), aDrill, true );
323  m_viaSizesGrid->SetCellValue( i, VIA_DRILL_COL, val );
324  }
325 }
326 
327 
328 void PANEL_SETUP_TRACKS_AND_VIAS::AppendDiffPairs( const int aWidth, const int aGap,
329  const int aViaGap )
330 {
331  int i = m_diffPairsGrid->GetNumberRows();
332 
333  m_diffPairsGrid->AppendRows( 1 );
334 
335  wxString val = StringFromValue( m_Frame->GetUserUnits(), aWidth, true );
336  m_diffPairsGrid->SetCellValue( i, DP_WIDTH_COL, val );
337 
338  if( aGap > 0 )
339  {
340  val = StringFromValue( m_Frame->GetUserUnits(), aGap, true );
341  m_diffPairsGrid->SetCellValue( i, DP_GAP_COL, val );
342  }
343 
344  if( aViaGap > 0 )
345  {
346  val = StringFromValue( m_Frame->GetUserUnits(), aViaGap, true );
347  m_diffPairsGrid->SetCellValue( i, DP_VIA_GAP_COL, val );
348  }
349 }
350 
352 {
353  AppendTrackWidth( 0 );
354 
355  m_trackWidthsGrid->MakeCellVisible( m_trackWidthsGrid->GetNumberRows() - 1, TR_WIDTH_COL );
356  m_trackWidthsGrid->SetGridCursor( m_trackWidthsGrid->GetNumberRows() - 1, TR_WIDTH_COL );
357 
358  m_trackWidthsGrid->EnableCellEditControl( true );
359  m_trackWidthsGrid->ShowCellEditControl();
360 }
361 
362 
364 {
365  int curRow = m_trackWidthsGrid->GetGridCursorRow();
366 
367  if( curRow < 0 || m_trackWidthsGrid->GetNumberRows() <= curRow )
368  return;
369 
370  m_trackWidthsGrid->DeleteRows( curRow, 1 );
371 
372  curRow = std::max( 0, curRow - 1 );
373  m_trackWidthsGrid->MakeCellVisible( curRow, m_trackWidthsGrid->GetGridCursorCol() );
374  m_trackWidthsGrid->SetGridCursor( curRow, m_trackWidthsGrid->GetGridCursorCol() );
375 }
376 
377 
379 {
380  AppendViaSize( 0, 0 );
381 
382  m_viaSizesGrid->MakeCellVisible( m_viaSizesGrid->GetNumberRows() - 1, VIA_SIZE_COL );
383  m_viaSizesGrid->SetGridCursor( m_viaSizesGrid->GetNumberRows() - 1, VIA_SIZE_COL );
384 
385  m_viaSizesGrid->EnableCellEditControl( true );
386  m_viaSizesGrid->ShowCellEditControl();
387 }
388 
389 
391 {
392  int curRow = m_viaSizesGrid->GetGridCursorRow();
393 
394  if( curRow < 0 || m_viaSizesGrid->GetNumberRows() <= curRow )
395  return;
396 
397  m_viaSizesGrid->DeleteRows( curRow, 1 );
398 
399  curRow = std::max( 0, curRow - 1 );
400  m_viaSizesGrid->MakeCellVisible( curRow, m_viaSizesGrid->GetGridCursorCol() );
401  m_viaSizesGrid->SetGridCursor( curRow, m_viaSizesGrid->GetGridCursorCol() );
402 }
403 
404 
406 {
407  AppendDiffPairs( 0, 0, 0 );
408 
409  m_diffPairsGrid->MakeCellVisible( m_diffPairsGrid->GetNumberRows() - 1, DP_WIDTH_COL );
410  m_diffPairsGrid->SetGridCursor( m_diffPairsGrid->GetNumberRows() - 1, DP_WIDTH_COL );
411 
412  m_diffPairsGrid->EnableCellEditControl( true );
413  m_diffPairsGrid->ShowCellEditControl();
414 }
415 
416 
418 {
419  int curRow = m_diffPairsGrid->GetGridCursorRow();
420 
421  if( curRow < 0 || m_diffPairsGrid->GetNumberRows() <= curRow )
422  return;
423 
424  m_diffPairsGrid->DeleteRows( curRow, 1 );
425 
426  curRow = std::max( 0, curRow - 1 );
427  m_diffPairsGrid->MakeCellVisible( curRow, m_diffPairsGrid->GetGridCursorCol() );
428  m_diffPairsGrid->SetGridCursor( curRow, m_diffPairsGrid->GetGridCursorCol() );
429 }
430 
431 
433 {
437 
438  // Note: do not change the board, as we need to get the current nets from it for
439  // netclass memberships. All the netclass definitions and dimension lists are in
440  // the BOARD_DESIGN_SETTINGS.
441 
442  BOARD_DESIGN_SETTINGS* savedSettings = m_BrdSettings;
443 
444  m_BrdSettings = &aBoard->GetDesignSettings();
446 
447  m_BrdSettings = savedSettings;
448 }
449 
Container to handle a stock of specific vias each with unique diameter and drill sizes in the BOARD c...
void OnRemoveViaSizesClick(wxCommandEvent &event) override
Implementation of conversion functions that require both schematic and board internal units.
std::vector< int > m_TrackWidthList
std::vector< DIFF_PAIR_DIMENSION > m_DiffPairDimensionsList
Add mouse and command handling (such as cut, copy, and paste) to a WX_GRID instance.
Definition: grid_tricks.h:55
void SetError(const wxString &aMessage, const wxString &aPageName, int aCtrlId, int aRow=-1, int aCol=-1)
void onUnitsChanged(wxCommandEvent &aEvent)
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.cpp:590
Container to handle a stock of specific differential pairs each with unique track width,...
int GetVisibleWidth(int aCol, bool aHeader=true, bool aContents=false, bool aKeep=true)
Calculates the specified column based on the actual size of the text on screen.
Definition: wx_grid.cpp:282
void OnAddDiffPairsClick(wxCommandEvent &event) override
long long int ValueFromString(EDA_UNITS aUnits, const wxString &aTextValue, EDA_DATA_TYPE aType)
Function ValueFromString converts aTextValue in aUnits to internal units used by the application.
Definition: base_units.cpp:411
PANEL_SETUP_TRACKS_AND_VIAS(PAGED_DIALOG *aParent, PCB_EDIT_FRAME *aFrame, PANEL_SETUP_CONSTRAINTS *aConstraintsPanel)
void OnRemoveDiffPairsClick(wxCommandEvent &event) override
void OnAddViaSizesClick(wxCommandEvent &event) override
#define _(s)
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:226
wxBitmap KiBitmap(BITMAPS aBitmap, int aHeightTag)
Construct a wxBitmap from an image identifier Returns the image from the active theme if the image ha...
Definition: bitmap.cpp:105
void AppendViaSize(const int aSize, const int aDrill)
PANEL_SETUP_CONSTRAINTS * m_ConstraintsPanel
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:191
void OnAddTrackWidthsClick(wxCommandEvent &event) override
The main frame for Pcbnew.
std::vector< VIA_DIMENSION > m_ViasDimensionsList
void ClearRows()
wxWidgets recently added an ASSERT which fires if the position is greater than or equal to the number...
Definition: wx_grid.h:109
wxString StringFromValue(EDA_UNITS aUnits, double aValue, bool aAddUnitSymbol, EDA_DATA_TYPE aType)
Convert a value to a string using double notation.
Definition: base_units.cpp:204
Class PANEL_SETUP_TRACKS_AND_VIAS_BASE.
void OnRemoveTrackWidthsClick(wxCommandEvent &event) override
BOARD * GetBoard() const
void AppendDiffPairs(const int aWidth, const int aGap, const int aViaGap)
EDA_UNITS GetUserUnits() const
Return the user units currently in use.
Container for design settings for a BOARD object.