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-2021 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/msgdlg.h>
21 #include <bitmaps.h>
23 #include <kiface_i.h>
25 #include "class_regulator_data.h"
26 #include "pcb_calculator_frame.h"
28 
29 
30 // extension of pcb_calculator data filename:
31 const wxString DataFileNameExt( wxT( "pcbcalc" ) );
32 
33 
34 PCB_CALCULATOR_FRAME::PCB_CALCULATOR_FRAME( KIWAY* aKiway, wxWindow* aParent ) :
35  PCB_CALCULATOR_FRAME_BASE( aParent ),
36  m_lastNotebookPage( -1 ),
37  m_macHack( true )
38 {
42 
43  SetKiway( this, aKiway );
44  m_currTransLine = nullptr;
46  m_currAttenuator = nullptr;
47  m_RegulatorListChanged = false;
49  m_TWNested = false;
50 
51  SHAPE_POLY_SET dummy; // A ugly trick to force the linker to include
52  // some methods in code and avoid link errors
53 
54  // Populate transline list ordered like in dialog menu list
55  const static TRANSLINE_TYPE_ID tltype_list[8] =
56  {
58  CPW_TYPE,
61  COAX_TYPE,
65  };
66 
67  for( int ii = 0; ii < 8; ii++ )
68  m_transline_list.push_back( new TRANSLINE_IDENT( tltype_list[ii] ) );
69 
70  // Populate attenuator list ordered like in dialog menu list
71  m_attenuator_list.push_back( new ATTENUATOR_PI() );
72  m_attenuator_list.push_back( new ATTENUATOR_TEE() );
73  m_attenuator_list.push_back( new ATTENUATOR_BRIDGE() );
74  m_attenuator_list.push_back( new ATTENUATOR_SPLITTER() );
76 
78 
79  m_IadjUnitLabel->SetLabel( wxT( "µA" ) );
80 
81  m_attZinUnit->SetLabel( wxT( "Ω" ) );
82  m_attZoutUnit->SetLabel( wxT( "Ω" ) );
83  m_attR1Unit->SetLabel( wxT( "Ω" ) );
84  m_attR2Unit->SetLabel( wxT( "Ω" ) );
85  m_attR3Unit->SetLabel( wxT( "Ω" ) );
86 
87  m_r1Units->SetLabel( wxT( "kΩ" ) );
88  m_r2Units->SetLabel( wxT( "kΩ" ) );
89 
90  m_reqResUnits->SetLabel( wxT( "kΩ" ) );
91  m_exclude1Units->SetLabel( wxT( "kΩ" ) );
92  m_exclude2Units->SetLabel( wxT( "kΩ" ) );
93 
94  m_EpsilonR_label->SetLabel( wxT( "εr" ) );
95 
96  m_trackTempUnits->SetLabel( wxT( "°C" ) );
97  m_resistivityUnits->SetLabel( wxT( "Ω•m" ) );
98 
99  m_viaResistivityUnits->SetLabel( wxT( "Ω•m" ) );
100 
101  m_viaTempUnits->SetLabel( wxT( "°C" ) );
102  m_viaResUnits->SetLabel( wxT( "Ω" ) );
103  m_viaThermalResUnits->SetLabel( wxT( "°C/W" ) );
104  m_viaReactanceUnits->SetLabel( wxT( "Ω" ) );
105 
106  m_extTrackResUnits->SetLabel( wxT( "Ω" ) );
107  m_intTrackResUnits->SetLabel( wxT( "Ω" ) );
108 
109  LoadSettings( config() );
110 
111  ReadDataFile();
112 
114  m_TranslineSelection->SetSelection( m_currTransLineType );
115 
120 
121  SetAttenuator( m_AttenuatorsSelection->GetSelection() );
122 
123  ToleranceSelection( m_rbToleranceSelection->GetSelection() );
124 
126 
128 
131 
132  // Give an icon
133  wxIcon icon;
134  wxIconBundle icon_bundle;
135 
136  icon.CopyFromBitmap( KiBitmap( BITMAPS::icon_pcbcalculator ) );
137  icon_bundle.AddIcon( icon );
138  icon.CopyFromBitmap( KiBitmap( BITMAPS::icon_pcbcalculator_32 ) );
139  icon_bundle.AddIcon( icon );
140  icon.CopyFromBitmap( KiBitmap( BITMAPS::icon_pcbcalculator_16 ) );
141  icon_bundle.AddIcon( icon );
142 
143  SetIcons( icon_bundle );
144 
145  // Autosize the row label column to be sure label are not truncated
146  m_gridClassesValuesDisplay->SetRowLabelSize( wxGRID_AUTOSIZE );
147  m_gridElectricalSpacingValues->SetRowLabelSize( wxGRID_AUTOSIZE );
148 
149  GetSizer()->SetSizeHints( this );
150 
151  // Set previous size and position
152  SetSize( m_framePos.x, m_framePos.y, m_frameSize.x, m_frameSize.y );
153 
154  if( m_framePos == wxDefaultPosition )
155  Centre();
156 }
157 
158 
160 {
161  for( unsigned ii = 0; ii < m_transline_list.size(); ii++ )
162  delete m_transline_list[ii];
163 
164  for( unsigned ii = 0; ii < m_attenuator_list.size(); ii++ )
165  delete m_attenuator_list[ii];
166 
167  delete m_ccValueNamesBitmap;
168  delete m_ccValuesBitmap;
169  delete m_ccMultipliersBitmap;
170  delete m_ccTolerancesBitmap;
171 
172  // This needed for OSX: avoids further OnDraw processing after this destructor and before
173  // the native window is destroyed
174  this->Freeze();
175 }
176 
177 
178 void PCB_CALCULATOR_FRAME::OnUpdateUI( wxUpdateUIEvent& event )
179 {
180  if( m_Notebook->GetSelection() != m_lastNotebookPage )
181  {
182  // Kick all the things that wxWidgets can't seem to redraw on its own.
183  // This is getting seriously ridiculous....
184 
185  wxCommandEvent event2( wxEVT_RADIOBUTTON );
186  event2.SetEventObject( m_TranslineSelection );
187  event2.SetInt( m_currTransLineType );
188  m_TranslineSelection->Command( event2 );
189 
190  for( int i = 0; i < m_attenuator_list.size(); ++i )
191  {
193  {
194  event2.SetEventObject( m_AttenuatorsSelection );
195  event2.SetInt( i );
196  m_AttenuatorsSelection->Command( event2 );
197  break;
198  }
199  }
200 
201  ToleranceSelection( m_rbToleranceSelection->GetSelection() );
202 
203  m_viaBitmap->SetBitmap( KiBitmap( BITMAPS::viacalc ) );
204  m_panelViaSize->Layout();
205 
208  m_bitmapRegul4pins->SetBitmap( KiBitmap( BITMAPS::regul ) );
209  m_panelRegulators->Layout();
210 
211  m_attenuatorBitmap->GetParent()->Layout();
212  m_attenuatorBitmap->GetParent()->Refresh();
213 
214  m_panelESeriesHelp->Refresh();
215  m_htmlWinFormulas->Refresh();
216 
217  // Until it's shown on screen the above won't work; but doing it anyway at least keeps
218  // putting new OnUpdateUI events into the queue until it *is* shown on screen.
219  if( m_Notebook->IsShownOnScreen() )
220  {
221  // Work around an OSX bug where the wxGrid children don't get placed correctly until
222  // the first resize event.
223 #ifdef __WXMAC__
224  if( m_macHack )
225  {
226  wxSize pageSize = m_panelElectricalSpacing->GetSize();
227 
228  pageSize.x -= 100;
229  m_panelElectricalSpacing->SetSize( pageSize );
230  m_panelElectricalSpacing->Layout();
231 
232  pageSize.x += 100;
233  m_panelElectricalSpacing->SetSize( pageSize );
234  m_panelElectricalSpacing->Layout();
235 
236  m_macHack = false;
237  }
238 #endif
239 
240  m_lastNotebookPage = m_Notebook->GetSelection();
241  }
242  }
243 }
244 
245 
246 void PCB_CALCULATOR_FRAME::OnClosePcbCalc( wxCloseEvent& event )
247 {
249  {
250  wxString msg;
251  wxString title = _( "Write Data Failed" );
252 
253  if( GetDataFilename().IsEmpty() )
254  {
255  msg = _( "No data filename to save modifications.\n"
256  "Do you want to exit and abandon your changes?" );
257 
258  if( wxMessageBox( msg, title, wxYES_NO | wxICON_QUESTION ) == wxNO )
259  return;
260  }
261  else
262  {
263  if( !WriteDataFile() )
264  {
265  msg.Printf( _( "Unable to write file '%s'\n"
266  "Do you want to exit and abandon your changes?"),
267  GetDataFilename() );
268 
269  if( wxMessageBox( msg, title, wxYES_NO | wxICON_ERROR ) == wxNO )
270  return;
271  }
272  }
273  }
274 
275  event.Skip();
276 }
277 
278 
280 {
281  if( aCfg == nullptr )
282  return;
283 
285 
286  PCB_CALCULATOR_SETTINGS* cfg = static_cast<PCB_CALCULATOR_SETTINGS*>( aCfg );
287 
288  m_currTransLineType = static_cast<TRANSLINE_TYPE_ID>( cfg->m_TransLine.type );
289  m_Notebook->ChangeSelection( cfg->m_LastPage );
290  m_rbToleranceSelection->SetSelection( cfg->m_ColorCodeTolerance );
291  m_AttenuatorsSelection->SetSelection( cfg->m_Attenuators.type );
292  m_BoardClassesUnitsSelector->SetSelection( cfg->m_BoardClassUnits );
293 
294  // Regul panel config:
295  m_RegulR1Value->SetValue( cfg->m_Regulators.r1 );
296  m_RegulR2Value->SetValue( cfg->m_Regulators.r2 );
297  m_RegulVrefValue->SetValue( cfg->m_Regulators.vref );
298  m_RegulVoutValue->SetValue( cfg->m_Regulators.vout );
301  m_choiceRegType->SetSelection( cfg->m_Regulators.type );
302 
303  wxRadioButton* regprms[3] = { m_rbRegulR1, m_rbRegulR2, m_rbRegulVout };
304 
305  if( cfg->m_Regulators.last_param >= 3 )
306  cfg->m_Regulators.last_param = 0;
307 
308  for( int ii = 0; ii < 3; ii++ )
309  regprms[ii]->SetValue( cfg->m_Regulators.last_param == ii );
310 
311  // Electrical panel config
314 
315  for( TRANSLINE_IDENT* transline : m_transline_list )
316  transline->ReadConfig();
317 
318  for( ATTENUATOR* attenuator : m_attenuator_list )
319  attenuator->ReadConfig();
320 }
321 
322 
324 {
325  if( aCfg == nullptr )
326  return;
327 
329 
330  // Save current parameters values in config.
331  auto cfg = dynamic_cast<PCB_CALCULATOR_SETTINGS*>( Kiface().KifaceSettings() );
332 
333  if( cfg )
334  {
335  cfg->m_LastPage = m_Notebook->GetSelection();
336  cfg->m_TransLine.type = m_currTransLineType;
337  cfg->m_Attenuators.type = m_AttenuatorsSelection->GetSelection();
338  cfg->m_ColorCodeTolerance = m_rbToleranceSelection->GetSelection();
339  cfg->m_BoardClassUnits = m_BoardClassesUnitsSelector->GetSelection();
340 
341  cfg->m_Electrical.spacing_units = m_ElectricalSpacingUnitsSelector->GetSelection();
342  cfg->m_Electrical.spacing_voltage = m_ElectricalSpacingVoltage->GetValue();
343 
344  Regulators_WriteConfig( cfg );
345  }
346 
348 
350 
351  for( unsigned ii = 0; ii < m_transline_list.size(); ii++ )
352  m_transline_list[ii]->WriteConfig();
353 
354  for( unsigned ii = 0; ii < m_attenuator_list.size(); ii++ )
355  m_attenuator_list[ii]->WriteConfig();
356 }
357 
358 
359 void PCB_CALCULATOR_FRAME::OnTranslineAnalyse( wxCommandEvent& event )
360 {
361  if( m_currTransLine )
362  {
365  }
366 }
367 
368 
369 void PCB_CALCULATOR_FRAME::OnTranslineSynthetize( wxCommandEvent& event )
370 {
371  if( m_currTransLine )
372  {
375  }
376 }
377 
378 
380 {
381  if( m_regulators_fileNameCtrl->GetValue().IsEmpty() )
382  return wxEmptyString;
383 
384  wxFileName fn( m_regulators_fileNameCtrl->GetValue() );
385  fn.SetExt( DataFileNameExt );
386  return fn.GetFullPath();
387 }
388 
389 
390 void PCB_CALCULATOR_FRAME::SetDataFilename( const wxString& aFilename )
391 {
392  if( aFilename.IsEmpty() )
393  {
394  m_regulators_fileNameCtrl->SetValue( wxEmptyString );
395  }
396  else
397  {
398  wxFileName fn( aFilename );
399  fn.SetExt( DataFileNameExt );
400  m_regulators_fileNameCtrl->SetValue( fn.GetFullPath() );
401  }
402 }
void ToleranceSelection(int aSelection)
Definition: colorcode.cpp:42
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)
void initViaSizePanel()
Definition: via.cpp:127
A class to handle a list of parameters of a given transline.
enum PCB_CALCULATOR_FRAME::@39 m_TWMode
Class PCB_CALCULATOR_FRAME_BASE.
void OnUpdateUI(wxUpdateUIEvent &event) override
static LIB_SYMBOL * dummy()
Used to draw a dummy shape when a LIB_SYMBOL is not found in library.
Definition: sch_symbol.cpp:71
void OnTranslineAnalyse(wxCommandEvent &event) override
Run a new analyze for the current transline with current parameters and displays the electrical param...
const wxString GetDataFilename()
void writeTrackWidthConfig()
Write track width parameters in configuration.
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
void initESeriesPanel()
Definition: eserie.cpp:393
void analyze()
Definition: transline.cpp:206
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.
void TransfDlgDataToTranslineParams()
Read values entered in dialog frame, and transfer these values in current transline parameters,...
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition: kiway.h:260
#define _(s)
void OnTranslineSynthetize(wxCommandEvent &event) override
Run a new synthesis for the current transline with current parameters and displays the geometrical pa...
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 OnClosePcbCalc(wxCloseEvent &event) override
enum TRANSLINE_TYPE_ID m_currTransLineType
TRANSLINE_TYPE_ID
void TranslineTypeSelection(enum TRANSLINE_TYPE_ID aType)
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
wxFont GetInfoFont()
Definition: ui_common.cpp:97
const wxString DataFileNameExt(wxT("pcbcalc"))
virtual double GetUnitScale() override
Function GetUnitScale.
UNIT_SELECTOR_LEN * m_BoardClassesUnitsSelector
void SetAttenuator(unsigned aIdx)
Definition: attenuators.cpp:43
std::vector< TRANSLINE_IDENT * > m_transline_list
UNIT_SELECTOR_LEN * m_ElectricalSpacingUnitsSelector
virtual void synthesize()
Definition: transline.cpp:215
void writeViaSizeConfig()
Write via size parameters in configuration.
Definition: via.cpp:157
void Regulators_WriteConfig(PCB_CALCULATOR_SETTINGS *aCfg)
Write regulators parameters in configuration.
void SelectLastSelectedRegulator()
If m_lastSelectedRegulatorName is empty, just calls RegulatorPageUpdate()
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