KiCad PCB EDA Suite
pcb_calculator_frame.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) 1992-2015 jean-pierre.charras
5  * Copyright (C) 1992-2020 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 3
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 along
18  * with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 #include <wx/wx.h>
21 
22 #include <bitmaps.h>
24 #include <kiface_i.h>
25 #include "bitmaps/viacalc.cpp"
26 #include "bitmaps/regul.cpp"
27 #include "bitmaps/regul_3pins.cpp"
29 #include "class_regulator_data.h"
30 #include "pcb_calculator_frame.h"
32 
33 
34 // extension of pcb_calculator data filename:
35 const wxString DataFileNameExt( wxT("pcbcalc") );
36 
37 PCB_CALCULATOR_FRAME::PCB_CALCULATOR_FRAME( KIWAY* aKiway, wxWindow* aParent ) :
38  PCB_CALCULATOR_FRAME_BASE( aParent ),
39  m_lastNotebookPage( -1 ),
40  m_macHack( true )
41 {
42  m_bpButtonCalcAtt->SetBitmap( KiBitmap( small_down_xpm ) );
43  m_bpButtonAnalyze->SetBitmap( KiBitmap( small_down_xpm ) );
45 
46  SetKiway( this, aKiway );
50  m_RegulatorListChanged = false;
52  m_TWNested = false;
53 
54  SHAPE_POLY_SET dummy; // A ugly trick to force the linker to include
55  // some methods in code and avoid link errors
56 
57  // Populate transline list ordered like in dialog menu list
58  const static TRANSLINE_TYPE_ID tltype_list[8] =
59  {
61  CPW_TYPE,
64  COAX_TYPE,
68  };
69 
70  for( int ii = 0; ii < 8; ii++ )
71  m_transline_list.push_back( new TRANSLINE_IDENT( tltype_list[ii] ) );
72 
73  // Populate attenuator list ordered like in dialog menu list
74  m_attenuator_list.push_back( new ATTENUATOR_PI() );
75  m_attenuator_list.push_back( new ATTENUATOR_TEE() );
76  m_attenuator_list.push_back( new ATTENUATOR_BRIDGE() );
77  m_attenuator_list.push_back( new ATTENUATOR_SPLITTER() );
79 
80  wxFont infoFont = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT );
81  infoFont.SetSymbolicSize( wxFONTSIZE_SMALL );
82  m_staticTextAttMsg->SetFont( infoFont );
83 
84  m_IadjUnitLabel->SetLabel( wxT( "µA" ) );
85 
86  m_attZinUnit->SetLabel( wxT( "Ω" ) );
87  m_attZoutUnit->SetLabel( wxT( "Ω" ) );
88  m_attR1Unit->SetLabel( wxT( "Ω" ) );
89  m_attR2Unit->SetLabel( wxT( "Ω" ) );
90  m_attR3Unit->SetLabel( wxT( "Ω" ) );
91 
92  m_r1Units->SetLabel( wxT( "kΩ" ) );
93  m_r2Units->SetLabel( wxT( "kΩ" ) );
94 
95  m_reqResUnits->SetLabel( wxT( "kΩ" ) );
96  m_exclude1Units->SetLabel( wxT( "kΩ" ) );
97  m_exclude2Units->SetLabel( wxT( "kΩ" ) );
98 
99  m_EpsilonR_label->SetLabel( wxT( "εr" ) );
100 
101  m_trackTempUnits->SetLabel( wxT( "°C" ) );
102  m_resistivityUnits->SetLabel( wxT( "Ω•m" ) );
103 
104  m_viaResistivityUnits->SetLabel( wxT( "Ω•m" ) );
105 
106  m_viaTempUnits->SetLabel( wxT( "°C" ) );
107  m_viaResUnits->SetLabel( wxT( "Ω" ) );
108  m_viaThermalResUnits->SetLabel( wxT( "°C/W" ) );
109  m_viaReactanceUnits->SetLabel( wxT( "Ω" ) );
110 
111  m_extTrackResUnits->SetLabel( wxT( "Ω" ) );
112  m_intTrackResUnits->SetLabel( wxT( "Ω" ) );
113 
114  LoadSettings( config() );
115 
116  ReadDataFile();
117 
119  m_TranslineSelection->SetSelection( m_currTransLineType );
120 
125 
126  SetAttenuator( m_AttenuatorsSelection->GetSelection() );
127 
128  ToleranceSelection( m_rbToleranceSelection->GetSelection() );
129 
131 
133 
136 
137  // Give an icon
138  wxIcon icon;
139  wxIconBundle icon_bundle;
140 
141  icon.CopyFromBitmap( KiBitmap( icon_pcbcalculator_xpm ) );
142  icon_bundle.AddIcon( icon );
143  icon.CopyFromBitmap( KiBitmap( icon_pcbcalculator_32_xpm ) );
144  icon_bundle.AddIcon( icon );
145  icon.CopyFromBitmap( KiBitmap( icon_pcbcalculator_16_xpm ) );
146  icon_bundle.AddIcon( icon );
147 
148  SetIcons( icon_bundle );
149 
150  // Autosize the the row label column to be sure label are not truncated
151  m_gridClassesValuesDisplay->SetRowLabelSize( wxGRID_AUTOSIZE );
152  m_gridElectricalSpacingValues->SetRowLabelSize( wxGRID_AUTOSIZE );
153 
154  GetSizer()->SetSizeHints( this );
155 
156  // Set previous size and position
157  SetSize( m_framePos.x, m_framePos.y, m_frameSize.x, m_frameSize.y );
158 
159  if( m_framePos == wxDefaultPosition )
160  Centre();
161 }
162 
163 
165 {
166  for( unsigned ii = 0; ii < m_transline_list.size(); ii++ )
167  delete m_transline_list[ii];
168 
169  for( unsigned ii = 0; ii < m_attenuator_list.size(); ii++ )
170  delete m_attenuator_list[ii];
171 
172  delete m_ccValueNamesBitmap;
173  delete m_ccValuesBitmap;
174  delete m_ccMultipliersBitmap;
175  delete m_ccTolerancesBitmap;
176 
177  // This needed for OSX: avoids furter OnDraw processing after this destructor and before
178  // the native window is destroyed
179  this->Freeze();
180 }
181 
182 
183 void PCB_CALCULATOR_FRAME::OnUpdateUI( wxUpdateUIEvent& event )
184 {
185  if( m_Notebook->GetSelection() != m_lastNotebookPage )
186  {
187  // Kick all the things that wxWidgets can't seem to redraw on its own.
188  // This is getting seriously ridiculous....
189 
190  wxCommandEvent event2( wxEVT_RADIOBUTTON );
191  event2.SetEventObject( m_TranslineSelection );
192  event2.SetInt( m_currTransLineType );
193  m_TranslineSelection->Command( event2 );
194 
195  for( int i = 0; i < m_attenuator_list.size(); ++i )
196  {
198  {
199  event2.SetEventObject( m_AttenuatorsSelection );
200  event2.SetInt( i );
201  m_AttenuatorsSelection->Command( event2 );
202  break;
203  }
204  }
205 
206  ToleranceSelection( m_rbToleranceSelection->GetSelection() );
207 
208  m_viaBitmap->SetBitmap( KiBitmap( viacalc_xpm ) );
211  m_bitmapRegul4pins->SetBitmap( KiBitmap( regul_xpm ) );
212 
213  m_attenuatorBitmap->GetParent()->Layout();
214  m_attenuatorBitmap->GetParent()->Refresh();
215 
216  m_panelESeriesHelp->Refresh();
217  m_htmlWinFormulas->Refresh();
218 
219  // Until it's shown on screen the above won't work; but doing it anyway at least keeps
220  // putting new OnUpdateUI events into the queue until it *is* shown on screen.
221  if( m_Notebook->IsShownOnScreen() )
222  {
223  // Work around an OSX bug where the wxGrid children don't get placed correctly until
224  // the first resize event.
225 #ifdef __WXMAC__
226  if( m_macHack )
227  {
228  wxSize pageSize = m_panelElectricalSpacing->GetSize();
229 
230  pageSize.x -= 100;
231  m_panelElectricalSpacing->SetSize( pageSize );
232  m_panelElectricalSpacing->Layout();
233 
234  pageSize.x += 100;
235  m_panelElectricalSpacing->SetSize( pageSize );
236  m_panelElectricalSpacing->Layout();
237 
238  m_macHack = false;
239  }
240 #endif
241 
242  m_lastNotebookPage = m_Notebook->GetSelection();
243  }
244  }
245 }
246 
247 
248 void PCB_CALCULATOR_FRAME::OnClosePcbCalc( wxCloseEvent& event )
249 {
251  {
252  wxString msg;
253  wxString title = _( "Write Data Failed" );
254 
255  if( GetDataFilename().IsEmpty() )
256  {
257  msg = _( "No data filename to save modifications.\n"
258  "Do you want to exit and abandon your changes?" );
259 
260  if( wxMessageBox( msg, title, wxYES_NO | wxICON_QUESTION ) == wxNO )
261  return;
262  }
263  else
264  {
265  if( !WriteDataFile() )
266  {
267  msg.Printf( _( "Unable to write file '%s'\n"
268  "Do you want to exit and abandon your changes?"),
269  GetDataFilename() );
270 
271  if( wxMessageBox( msg, title, wxYES_NO | wxICON_ERROR ) == wxNO )
272  return;
273  }
274  }
275  }
276 
277  event.Skip();
278 }
279 
280 
282 {
283  if( aCfg == NULL )
284  return;
285 
287 
288  PCB_CALCULATOR_SETTINGS* cfg = static_cast<PCB_CALCULATOR_SETTINGS*>( aCfg );
289 
290  m_currTransLineType = static_cast<TRANSLINE_TYPE_ID>( cfg->m_TransLine.type );
291  m_Notebook->ChangeSelection( cfg->m_LastPage );
292  m_rbToleranceSelection->SetSelection( cfg->m_ColorCodeTolerance );
293  m_AttenuatorsSelection->SetSelection( cfg->m_Attenuators.type );
294  m_BoardClassesUnitsSelector->SetSelection( cfg->m_BoardClassUnits );
295 
296  // Regul panel config:
297  m_RegulR1Value->SetValue( cfg->m_Regulators.r1 );
298  m_RegulR2Value->SetValue( cfg->m_Regulators.r2 );
299  m_RegulVrefValue->SetValue( cfg->m_Regulators.vref );
300  m_RegulVoutValue->SetValue( cfg->m_Regulators.vout );
303  m_choiceRegType->SetSelection( cfg->m_Regulators.type );
304 
305  wxRadioButton* regprms[3] = { m_rbRegulR1, m_rbRegulR2, m_rbRegulVout };
306 
307  if( cfg->m_Regulators.last_param >= 3 )
308  cfg->m_Regulators.last_param = 0;
309 
310  for( int ii = 0; ii < 3; ii++ )
311  regprms[ii]->SetValue( cfg->m_Regulators.last_param == ii );
312 
313  // Electrical panel config
316 
317  for( TRANSLINE_IDENT* transline : m_transline_list )
318  transline->ReadConfig();
319 
320  for( ATTENUATOR* attenuator : m_attenuator_list )
321  attenuator->ReadConfig();
322 }
323 
324 
326 {
327  if( aCfg == NULL )
328  return;
329 
331 
332  // Save current parameters values in config.
333  auto cfg = dynamic_cast<PCB_CALCULATOR_SETTINGS*>( Kiface().KifaceSettings() );
334 
335  if( cfg )
336  {
337  cfg->m_LastPage = m_Notebook->GetSelection();
338  cfg->m_TransLine.type = m_currTransLineType;
339  cfg->m_Attenuators.type = m_AttenuatorsSelection->GetSelection();
340  cfg->m_ColorCodeTolerance = m_rbToleranceSelection->GetSelection();
341  cfg->m_BoardClassUnits = m_BoardClassesUnitsSelector->GetSelection();
342 
343  cfg->m_Electrical.spacing_units = m_ElectricalSpacingUnitsSelector->GetSelection();
344  cfg->m_Electrical.spacing_voltage = m_ElectricalSpacingVoltage->GetValue();
345 
346  Regulators_WriteConfig( cfg );
347  }
348 
350 
352 
353  for( unsigned ii = 0; ii < m_transline_list.size(); ii++ )
354  m_transline_list[ii]->WriteConfig();
355 
356  for( unsigned ii = 0; ii < m_attenuator_list.size(); ii++ )
357  m_attenuator_list[ii]->WriteConfig();
358 }
359 
360 
366 void PCB_CALCULATOR_FRAME::OnTranslineAnalyse( wxCommandEvent& event )
367 {
368  if( m_currTransLine )
369  {
372  }
373 }
374 
375 
381 void PCB_CALCULATOR_FRAME::OnTranslineSynthetize( wxCommandEvent& event )
382 {
383  if( m_currTransLine )
384  {
387  }
388 }
389 
390 
391 /* returns the full filename of the selected pcb_calculator data file
392  * the extension file is forced
393  */
395 {
396  if( m_regulators_fileNameCtrl->GetValue().IsEmpty() )
397  return wxEmptyString;
398 
399  wxFileName fn( m_regulators_fileNameCtrl->GetValue() );
400  fn.SetExt( DataFileNameExt );
401  return fn.GetFullPath();
402 }
403 
404 
405 /* Initialize the full filename of the selected pcb_calculator data file
406  * force the standard extension of the file (.pcbcalc)
407  * aFilename = the full filename, with or without extension
408  */
409 void PCB_CALCULATOR_FRAME::SetDataFilename( const wxString & aFilename)
410 {
411  if( aFilename.IsEmpty() )
412  m_regulators_fileNameCtrl->SetValue( wxEmptyString );
413 
414  else
415  {
416  wxFileName fn( aFilename );
417  fn.SetExt( DataFileNameExt );
418  m_regulators_fileNameCtrl->SetValue( fn.GetFullPath() );
419  }
420 }
void ToleranceSelection(int aSelection)
Definition: colorcode.cpp:44
void SetKiway(wxWindow *aDest, KIWAY *aKiway)
It is only used for debugging, since "this" is not a wxWindow*.
virtual APP_SETTINGS_BASE * config() const
Returns the settings object used in SaveSettings(), and is overloaded in KICAD_MANAGER_FRAME.
virtual void SaveSettings(APP_SETTINGS_BASE *aCfg)
Save common frame parameters to a configuration data file.
PCB_CALCULATOR_FRAME(KIWAY *aKiway, wxWindow *aParent)
const BITMAP_OPAQUE icon_pcbcalculator_xpm[1]
static LIB_PART * dummy()
Used to draw a dummy shape when a LIB_PART is not found in library.
Definition: sch_symbol.cpp:69
enum PCB_CALCULATOR_FRAME::@39 m_TWMode
Class PCB_CALCULATOR_FRAME_BASE.
void OnUpdateUI(wxUpdateUIEvent &event) override
const BITMAP_OPAQUE icon_pcbcalculator_16_xpm[1]
const BITMAP_OPAQUE regul_3pins_xpm[1]
void OnTranslineAnalyse(wxCommandEvent &event) override
Function OnTranslineAnalyse Run a new analyse for the current transline with current parameters and d...
const wxString GetDataFilename()
wxBitmap KiBitmap(BITMAP_DEF aBitmap)
Construct a wxBitmap from a memory record, held in a BITMAP_DEF.
Definition: bitmap.cpp:82
const BITMAP_OPAQUE regul_xpm[1]
Definition: regul.cpp:331
void writeTrackWidthConfig()
Function writeTrackWidthConfig Write Track width parameters in config.
const BITMAP_OPAQUE viacalc_xpm[1]
Definition: viacalc.cpp:1070
wxBitmap * m_SchBitMap
wxArrayString GetRegList() const
APP_SETTINGS_BASE is a settings class that should be derived for each standalone KiCad application.
Definition: app_settings.h:99
#define NULL
void initESeriesPanel()
Definition: eserie.cpp:394
void analyze()
Definition: transline.cpp:203
void initTrackWidthPanel()
Panel-specific initializers.
Contains structures for storage of regulator data.
Represent a set of closed polygons.
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
void SaveSettings(APP_SETTINGS_BASE *aCfg) override
Save common frame parameters to a configuration data file.
const BITMAP_OPAQUE small_down_xpm[1]
Definition: small_down.cpp:23
void TransfDlgDataToTranslineParams()
Function TransfDlgDataToTranslineParams Read values entered in dialog frame, and transfert these valu...
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition: kiway.h:260
void OnTranslineSynthetize(wxCommandEvent &event) override
Function OnTranslineSynthetize Run a new synthezis for the current transline with current parameters ...
const BITMAP_OPAQUE small_up_xpm[1]
Definition: small_up.cpp:23
void OnClosePcbCalc(wxCloseEvent &event) override
enum TRANSLINE_TYPE_ID m_currTransLineType
TRANSLINE_TYPE_ID
void TranslineTypeSelection(enum TRANSLINE_TYPE_ID aType)
Function TranslineTypeSelection Must be called after selection of a new transline.
void LoadSettings(APP_SETTINGS_BASE *aCfg) override
Load common frame parameters from a configuration file.
void ElectricalSpacingUpdateData(double aUnitScale)
void SetDataFilename(const wxString &aFilename)
Initialize the full filename of the selected pcb_calculator data file force the standard extension of...
REGULATOR_LIST m_RegulatorList
const wxString DataFileNameExt(wxT("pcbcalc"))
#define _(s)
Definition: 3d_actions.cpp:33
virtual double GetUnitScale() override
Function GetUnitScale.
UNIT_SELECTOR_LEN * m_BoardClassesUnitsSelector
void SetAttenuator(unsigned aIdx)
Definition: attenuators.cpp:44
std::vector< TRANSLINE_IDENT * > m_transline_list
UNIT_SELECTOR_LEN * m_ElectricalSpacingUnitsSelector
virtual void synthesize()
Definition: transline.cpp:212
void writeViaSizeConfig()
Function writeViaSizeConfig Write Via Size parameters in config.
void Regulators_WriteConfig(PCB_CALCULATOR_SETTINGS *aCfg)
Write regulators parameters in config.
const BITMAP_OPAQUE icon_pcbcalculator_32_xpm[1]
void SelectLastSelectedRegulator()
Function SelectLastSelectedRegulator If m_lastSelectedRegulatorName is empty, just calls RegulatorPag...
void BoardClassesUpdateData(double aUnitScale)
virtual void LoadSettings(APP_SETTINGS_BASE *aCfg)
Load common frame parameters from a configuration file.
std::vector< ATTENUATOR * > m_attenuator_list