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 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 <fctsys.h>
26 #include <base_units.h>
27 #include <pcb_edit_frame.h>
28 #include <board_design_settings.h>
29 #include <bitmaps.h>
30 #include <widgets/wx_grid.h>
31 #include <grid_tricks.h>
32 
34 
35 
37 {
39 };
40 
42 {
45 };
46 
48 {
52 };
53 
54 
56  PAGED_DIALOG* aParent, PCB_EDIT_FRAME* aFrame,
57  PANEL_SETUP_FEATURE_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 
69  m_viaSizesRemoveButton->SetBitmap( KiBitmap( trash_xpm ) );
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 
89 {
90  // Delete the GRID_TRICKS.
91  m_trackWidthsGrid->PopEventHandler( true );
92  m_viaSizesGrid->PopEventHandler( true );
93  m_diffPairsGrid->PopEventHandler( true );
94 }
95 
97 {
98  if( m_trackWidthsGrid->GetNumberRows() > 0 )
99  m_trackWidthsGrid->DeleteRows( 0, m_trackWidthsGrid->GetNumberRows() );
100 
101  if( m_viaSizesGrid->GetNumberRows() > 0 )
102  m_viaSizesGrid->DeleteRows( 0, m_viaSizesGrid->GetNumberRows() );
103 
104  if( m_diffPairsGrid->GetNumberRows() > 0 )
105  m_diffPairsGrid->DeleteRows( 0, m_diffPairsGrid->GetNumberRows() );
106 
107  // Skip the first item, which is the current netclass value
108  for( unsigned ii = 1; ii < m_BrdSettings->m_TrackWidthList.size(); ii++ )
109  {
111  }
112 
113  // Skip the first item, which is the current netclass value
114  for( unsigned ii = 1; ii < m_BrdSettings->m_ViasDimensionsList.size(); ii++ )
115  {
117  m_BrdSettings->m_ViasDimensionsList[ii].m_Drill );
118  }
119 
120  // Skip the first item, which is the current netclass value
121  for( unsigned ii = 1; ii < m_BrdSettings->m_DiffPairDimensionsList.size(); ii++ )
122  {
125  m_BrdSettings->m_DiffPairDimensionsList[ii].m_ViaGap );
126  }
127 
128  return true;
129 }
130 
131 
133 {
134  if( !validateData() )
135  return false;
136 
137  wxString msg;
138  std::vector<int> trackWidths;
139  std::vector<VIA_DIMENSION> vias;
140  std::vector<DIFF_PAIR_DIMENSION> diffPairs;
141 
145  {
146  return false;
147  }
148 
149  for( int row = 0; row < m_trackWidthsGrid->GetNumberRows(); ++row )
150  {
151  msg = m_trackWidthsGrid->GetCellValue( row, TR_WIDTH_COL );
152 
153  if( !msg.IsEmpty() )
154  trackWidths.push_back( ValueFromString( m_Frame->GetUserUnits(), msg, true ) );
155  }
156 
157  for( int row = 0; row < m_viaSizesGrid->GetNumberRows(); ++row )
158  {
159  msg = m_viaSizesGrid->GetCellValue( row, VIA_SIZE_COL );
160 
161  if( !msg.IsEmpty() )
162  {
163  VIA_DIMENSION via_dim;
164  via_dim.m_Diameter = ValueFromString( m_Frame->GetUserUnits(), msg, true );
165 
166  msg = m_viaSizesGrid->GetCellValue( row, VIA_DRILL_COL );
167 
168  if( !msg.IsEmpty() )
169  via_dim.m_Drill = ValueFromString( m_Frame->GetUserUnits(), msg, true );
170 
171  vias.push_back( via_dim );
172  }
173  }
174 
175  for( int row = 0; row < m_diffPairsGrid->GetNumberRows(); ++row )
176  {
177  msg = m_diffPairsGrid->GetCellValue( row, DP_WIDTH_COL );
178 
179  if( !msg.IsEmpty() )
180  {
181  DIFF_PAIR_DIMENSION diffPair_dim;
182  diffPair_dim.m_Width = ValueFromString( m_Frame->GetUserUnits(), msg, true );
183 
184  msg = m_diffPairsGrid->GetCellValue( row, DP_GAP_COL );
185  diffPair_dim.m_Gap = ValueFromString( m_Frame->GetUserUnits(), msg, true );
186 
187  msg = m_diffPairsGrid->GetCellValue( row, DP_VIA_GAP_COL );
188 
189  if( !msg.IsEmpty() )
190  diffPair_dim.m_ViaGap = ValueFromString( m_Frame->GetUserUnits(), msg, true );
191 
192  diffPairs.push_back( diffPair_dim );
193  }
194  }
195 
196  // Sort lists by increasing value
197  sort( trackWidths.begin(), trackWidths.end() );
198  sort( vias.begin(), vias.end() );
199  sort( diffPairs.begin(), diffPairs.end() );
200 
201  trackWidths.insert( trackWidths.begin(), m_BrdSettings->m_TrackWidthList[ 0 ] );
202  m_BrdSettings->m_TrackWidthList = trackWidths;
203 
204  vias.insert( vias.begin(), m_BrdSettings->m_ViasDimensionsList[ 0 ] );
206 
207  diffPairs.insert( diffPairs.begin(), m_BrdSettings->m_DiffPairDimensionsList[ 0 ] );
209 
210  return true;
211 }
212 
213 
215 {
219  {
220  return false;
221  }
222 
223  wxString msg;
224  int minViaAnnulus = m_ConstraintsPanel->m_viaMinAnnulus.GetValue();
225  int minViaDia = m_ConstraintsPanel->m_viaMinSize.GetValue();
226  int minThroughHole = m_ConstraintsPanel->m_throughHoleMin.GetValue();
227  int minTrackWidth = m_ConstraintsPanel->m_trackMinWidth.GetValue();
228  int minClearance = m_ConstraintsPanel->m_minClearance.GetValue();
229 
230  // Test tracks
231  for( int row = 0; row < m_trackWidthsGrid->GetNumberRows(); ++row )
232  {
233  wxString tvalue = m_trackWidthsGrid->GetCellValue( row, TR_WIDTH_COL );
234 
235  if( tvalue.IsEmpty() )
236  continue;
237 
238  if( ValueFromString( m_Frame->GetUserUnits(), tvalue ) < minTrackWidth )
239  {
240  msg.Printf( _( "Track width less than minimum track width (%s)." ),
241  StringFromValue( m_Frame->GetUserUnits(), minTrackWidth, true, true ) );
242  m_Parent->SetError( msg, this, m_trackWidthsGrid, row, TR_WIDTH_COL );
243  return false;
244  }
245  }
246 
247  // Test vias
248  for( int row = 0; row < m_viaSizesGrid->GetNumberRows(); ++row )
249  {
250  wxString viaDia = m_viaSizesGrid->GetCellValue( row, VIA_SIZE_COL );
251 
252  if( viaDia.IsEmpty() )
253  continue;
254 
255  if( ValueFromString( m_Frame->GetUserUnits(), viaDia ) < minViaDia )
256  {
257  msg.Printf( _( "Via diameter less than minimum via diameter (%s)." ),
258  StringFromValue( m_Frame->GetUserUnits(), minViaDia, true, true ) );
259  m_Parent->SetError( msg, this, m_viaSizesGrid, row, VIA_SIZE_COL );
260  return false;
261  }
262 
263  wxString viaDrill = m_viaSizesGrid->GetCellValue( row, VIA_DRILL_COL );
264 
265  if( viaDrill.IsEmpty() )
266  {
267  msg = _( "No via drill defined." );
268  m_Parent->SetError( msg, this, m_viaSizesGrid, row, VIA_DRILL_COL );
269  return false;
270  }
271 
272  if( ValueFromString( m_Frame->GetUserUnits(), viaDrill ) < minThroughHole )
273  {
274  msg.Printf( _( "Via drill less than minimum through hole (%s)." ),
275  StringFromValue( m_Frame->GetUserUnits(), minThroughHole, true, true ) );
276  m_Parent->SetError( msg, this, m_viaSizesGrid, row, VIA_DRILL_COL );
277  return false;
278  }
279 
280  if( ValueFromString( m_Frame->GetUserUnits(), viaDrill )
281  >= ValueFromString( m_Frame->GetUserUnits(), viaDia ) )
282  {
283  msg = _( "Via drill larger than via diameter." );
284  m_Parent->SetError( msg, this, m_viaSizesGrid, row, VIA_DRILL_COL );
285  return false;
286  }
287 
288  if( ( ValueFromString( m_Frame->GetUserUnits(), viaDia )
289  - ValueFromString( m_Frame->GetUserUnits(), viaDrill ) ) / 2 < minViaAnnulus )
290  {
291  msg.Printf( _( "Diameter and drill leave via annulus less than minimum (%s)." ),
292  StringFromValue( m_Frame->GetUserUnits(), minViaAnnulus, true, true ) );
293  m_Parent->SetError( msg, this, m_viaSizesGrid, row, VIA_SIZE_COL );
294  return false;
295  }
296  }
297 
298  // Test diff pairs
299  for( int row = 0; row < m_diffPairsGrid->GetNumberRows(); ++row )
300  {
301  wxString tvalue = m_diffPairsGrid->GetCellValue( row, 0 );
302 
303  if( tvalue.IsEmpty() )
304  continue;
305 
306  if( ValueFromString( m_Frame->GetUserUnits(), tvalue ) < minTrackWidth )
307  {
308  msg.Printf( _( "Differential pair track width less than minimum track width (%s)." ),
309  StringFromValue( m_Frame->GetUserUnits(), minTrackWidth, true, true ) );
310  m_Parent->SetError( msg, this, m_diffPairsGrid, row, 0 );
311  return false;
312  }
313 
314  wxString gap = m_diffPairsGrid->GetCellValue( row, 1 );
315 
316  if( gap.IsEmpty() )
317  {
318  msg = _( "No differential pair gap defined." );
319  m_Parent->SetError( msg, this, m_diffPairsGrid, row, 1 );
320  return false;
321  }
322 
323  if( ValueFromString( m_Frame->GetUserUnits(), gap ) < minClearance )
324  {
325  msg.Printf( _( "Differential pair gap less than minimum clearance (%s)." ),
326  StringFromValue( m_Frame->GetUserUnits(), minClearance, true, true ) );
327  m_Parent->SetError( msg, this, m_diffPairsGrid, row, 1 );
328  return false;
329  }
330 
331  wxString viaGap = m_diffPairsGrid->GetCellValue( row, 2 );
332 
333  if( viaGap.IsEmpty() )
334  continue;
335 
336  if( ValueFromString( m_Frame->GetUserUnits(), viaGap ) < minClearance )
337  {
338  msg.Printf( _( "Differential pair via gap less than minimum clearance (%s)." ),
339  StringFromValue( m_Frame->GetUserUnits(), minClearance, true, true ) );
340  m_Parent->SetError( msg, this, m_diffPairsGrid, row, 2 );
341  return false;
342  }
343  }
344 
345  return true;
346 }
347 
348 
350 {
351  int i = m_trackWidthsGrid->GetNumberRows();
352 
353  m_trackWidthsGrid->AppendRows( 1 );
354 
355  wxString val = StringFromValue( m_Frame->GetUserUnits(), aWidth, true, true );
356  m_trackWidthsGrid->SetCellValue( i, TR_WIDTH_COL, val );
357 }
358 
359 
360 void PANEL_SETUP_TRACKS_AND_VIAS::AppendViaSize( const int aSize, const int aDrill )
361 {
362  int i = m_viaSizesGrid->GetNumberRows();
363 
364  m_viaSizesGrid->AppendRows( 1 );
365 
366  wxString val = StringFromValue( m_Frame->GetUserUnits(), aSize, true, true );
367  m_viaSizesGrid->SetCellValue( i, VIA_SIZE_COL, val );
368 
369  if( aDrill > 0 )
370  {
371  val = StringFromValue( m_Frame->GetUserUnits(), aDrill, true, true );
372  m_viaSizesGrid->SetCellValue( i, VIA_DRILL_COL, val );
373  }
374 }
375 
376 
377 void PANEL_SETUP_TRACKS_AND_VIAS::AppendDiffPairs( const int aWidth, const int aGap,
378  const int aViaGap )
379 {
380  int i = m_diffPairsGrid->GetNumberRows();
381 
382  m_diffPairsGrid->AppendRows( 1 );
383 
384  wxString val = StringFromValue( m_Frame->GetUserUnits(), aWidth, true, true );
385  m_diffPairsGrid->SetCellValue( i, DP_WIDTH_COL, val );
386 
387  if( aGap > 0 )
388  {
389  val = StringFromValue( m_Frame->GetUserUnits(), aGap, true, true );
390  m_diffPairsGrid->SetCellValue( i, DP_GAP_COL, val );
391  }
392 
393  if( aViaGap > 0 )
394  {
395  val = StringFromValue( m_Frame->GetUserUnits(), aViaGap, true, true );
396  m_diffPairsGrid->SetCellValue( i, DP_VIA_GAP_COL, val );
397  }
398 }
399 
401 {
402  AppendTrackWidth( 0 );
403 
404  m_trackWidthsGrid->MakeCellVisible( m_trackWidthsGrid->GetNumberRows() - 1, TR_WIDTH_COL );
405  m_trackWidthsGrid->SetGridCursor( m_trackWidthsGrid->GetNumberRows() - 1, TR_WIDTH_COL );
406 
407  m_trackWidthsGrid->EnableCellEditControl( true );
408  m_trackWidthsGrid->ShowCellEditControl();
409 }
410 
411 
413 {
414  int curRow = m_trackWidthsGrid->GetGridCursorRow();
415 
416  if( curRow < 0 || m_trackWidthsGrid->GetNumberRows() <= curRow )
417  return;
418 
419  m_trackWidthsGrid->DeleteRows( curRow, 1 );
420 
421  curRow = std::max( 0, curRow - 1 );
422  m_trackWidthsGrid->MakeCellVisible( curRow, m_trackWidthsGrid->GetGridCursorCol() );
423  m_trackWidthsGrid->SetGridCursor( curRow, m_trackWidthsGrid->GetGridCursorCol() );
424 }
425 
426 
428 {
429  AppendViaSize( 0, 0 );
430 
431  m_viaSizesGrid->MakeCellVisible( m_viaSizesGrid->GetNumberRows() - 1, VIA_SIZE_COL );
432  m_viaSizesGrid->SetGridCursor( m_viaSizesGrid->GetNumberRows() - 1, VIA_SIZE_COL );
433 
434  m_viaSizesGrid->EnableCellEditControl( true );
435  m_viaSizesGrid->ShowCellEditControl();
436 }
437 
438 
440 {
441  int curRow = m_viaSizesGrid->GetGridCursorRow();
442 
443  if( curRow < 0 || m_viaSizesGrid->GetNumberRows() <= curRow )
444  return;
445 
446  m_viaSizesGrid->DeleteRows( curRow, 1 );
447 
448  curRow = std::max( 0, curRow - 1 );
449  m_viaSizesGrid->MakeCellVisible( curRow, m_viaSizesGrid->GetGridCursorCol() );
450  m_viaSizesGrid->SetGridCursor( curRow, m_viaSizesGrid->GetGridCursorCol() );
451 }
452 
453 
455 {
456  AppendDiffPairs( 0, 0, 0 );
457 
458  m_diffPairsGrid->MakeCellVisible( m_diffPairsGrid->GetNumberRows() - 1, DP_WIDTH_COL );
459  m_diffPairsGrid->SetGridCursor( m_diffPairsGrid->GetNumberRows() - 1, DP_WIDTH_COL );
460 
461  m_diffPairsGrid->EnableCellEditControl( true );
462  m_diffPairsGrid->ShowCellEditControl();
463 }
464 
465 
467 {
468  int curRow = m_diffPairsGrid->GetGridCursorRow();
469 
470  if( curRow < 0 || m_diffPairsGrid->GetNumberRows() <= curRow )
471  return;
472 
473  m_diffPairsGrid->DeleteRows( curRow, 1 );
474 
475  curRow = std::max( 0, curRow - 1 );
476  m_diffPairsGrid->MakeCellVisible( curRow, m_diffPairsGrid->GetGridCursorCol() );
477  m_diffPairsGrid->SetGridCursor( curRow, m_diffPairsGrid->GetGridCursorCol() );
478 }
479 
480 
482 {
486 
487  // Note: do not change the board, as we need to get the current nets from it for
488  // netclass memberships. All the netclass definitions and dimension lists are in
489  // the BOARD_DESIGN_SETTINGS.
490 
491  BOARD_DESIGN_SETTINGS* savedSettings = m_BrdSettings;
492 
493  m_BrdSettings = &aBoard->GetDesignSettings();
495 
496  m_BrdSettings = savedSettings;
497 }
498 
const BITMAP_OPAQUE trash_xpm[1]
Definition: trash.cpp:46
Struct VIA_DIMENSION is a small helper container to handle a stock of specific vias each with unique ...
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
PANEL_SETUP_FEATURE_CONSTRAINTS * m_ConstraintsPanel
GRID_TRICKS is used to add mouse and command handling (such as cut, copy, and paste) to a WX_GRID ins...
Definition: grid_tricks.h:51
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Function GetDesignSettings.
Definition: class_board.h:551
void SetError(const wxString &aMessage, const wxString &aPageName, int aCtrlId, int aRow=-1, int aCol=-1)
Struct DIFF_PAIR_DIMENSION is a small helper container to handle a stock of specific differential pai...
void OnAddDiffPairsClick(wxCommandEvent &event) override
wxBitmap KiBitmap(BITMAP_DEF aBitmap)
Construct a wxBitmap from a memory record, held in a BITMAP_DEF.
Definition: bitmap.cpp:80
void OnRemoveDiffPairsClick(wxCommandEvent &event) override
void OnAddViaSizesClick(wxCommandEvent &event) override
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:174
void AppendViaSize(const int aSize, const int aDrill)
PANEL_SETUP_TRACKS_AND_VIAS(PAGED_DIALOG *aParent, PCB_EDIT_FRAME *aFrame, PANEL_SETUP_FEATURE_CONSTRAINTS *aConstraintsPanel)
BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:180
#define _(s)
Definition: 3d_actions.cpp:33
void OnAddTrackWidthsClick(wxCommandEvent &event) override
PCB_EDIT_FRAME is the main frame for Pcbnew.
std::vector< VIA_DIMENSION > m_ViasDimensionsList
virtual long long int GetValue()
Function GetValue Returns the current value in Internal Units.
Class PANEL_SETUP_TRACKS_AND_VIAS_BASE.
void OnRemoveTrackWidthsClick(wxCommandEvent &event) override
BOARD * GetBoard() const
const BITMAP_OPAQUE small_plus_xpm[1]
Definition: small_plus.cpp:20
wxString StringFromValue(EDA_UNITS aUnits, double aValue, bool aAddUnitSymbol, bool aUseMils, EDA_DATA_TYPE aType)
Function StringFromValue returns the string from aValue according to units (inch, mm ....
Definition: base_units.cpp:233
void AppendDiffPairs(const int aWidth, const int aGap, const int aViaGap)
EDA_UNITS GetUserUnits() const
Return the user units currently in use.
long long int ValueFromString(EDA_UNITS aUnits, const wxString &aTextValue, bool aUseMils, EDA_DATA_TYPE aType)
Function ValueFromString converts aTextValue in aUnits to internal units used by the application.
Definition: base_units.cpp:471
BOARD_DESIGN_SETTINGS contains design settings for a BOARD object.