KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_sim_model.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) 2022 Mikolaj Wielgus
5 * Copyright (C) 2022 CERN
6 * Copyright (C) 2022-2024 KiCad Developers, see AUTHORS.txt for contributors.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 3
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, you may find one here:
20 * https://www.gnu.org/licenses/gpl-3.0.html
21 * or you may search the http://www.gnu.org website for the version 3 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
27#include <dialog_sim_model.h>
28#include <sim/sim_property.h>
30#include <sim/sim_model.h>
31#include <sim/sim_model_ibis.h>
35#include <grid_tricks.h>
38#include <kiplatform/ui.h>
39#include <confirm.h>
40#include <string_utils.h>
41#include <locale_io.h>
42#include <wx/filedlg.h>
43#include <fmt/format.h>
44#include <sch_edit_frame.h>
47#include <wx/log.h>
48
50
51#define FORCE_UPDATE_PINS true
52
53
54bool equivalent( SIM_MODEL::DEVICE_T a, SIM_MODEL::DEVICE_T b )
55{
56 // A helper to handle SPICE's use of 'E' and 'H' for voltage sources and 'F' and 'G' for
57 // current sources
58 return a == b
59 || SIM_MODEL::DeviceInfo( a ).description == SIM_MODEL::DeviceInfo( b ).description;
60};
61
62
63template <typename T>
64DIALOG_SIM_MODEL<T>::DIALOG_SIM_MODEL( wxWindow* aParent, EDA_BASE_FRAME* aFrame, T& aSymbol,
65 std::vector<SCH_FIELD>& aFields ) :
66 DIALOG_SIM_MODEL_BASE( aParent ),
67 m_frame( aFrame ),
68 m_symbol( aSymbol ),
69 m_fields( aFields ),
70 m_libraryModelsMgr( &Prj() ),
71 m_builtinModelsMgr( &Prj() ),
72 m_prevModel( nullptr ),
73 m_curModelType( SIM_MODEL::TYPE::NONE ),
74 m_scintillaTricksCode( nullptr ),
75 m_scintillaTricksSubckt( nullptr ),
76 m_firstCategory( nullptr ),
77 m_prevParamGridSelection( nullptr ),
78 m_lastParamGridWidth( 0 )
79{
80 m_browseButton->SetBitmap( KiBitmapBundle( BITMAPS::small_folder ) );
81
82 for( SCH_PIN* pin : aSymbol.GetAllLibPins() )
83 {
84 // De Morgan conversions are equivalences, not additional items to simulate
85 if( !pin->GetParentSymbol()->HasAlternateBodyStyle() || pin->GetBodyStyle() < 2 )
86 m_sortedPartPins.push_back( pin );
87 }
88
89 std::sort( m_sortedPartPins.begin(), m_sortedPartPins.end(),
90 []( const SCH_PIN* lhs, const SCH_PIN* rhs )
91 {
92 // We sort by StrNumCmp because SIM_MODEL_BASE sorts with it too.
93 return StrNumCmp( lhs->GetNumber(), rhs->GetNumber(), true ) < 0;
94 } );
95
96 m_waveformChoice->Clear();
97 m_deviceChoice->Clear();
98 m_deviceSubtypeChoice->Clear();
99
100 m_scintillaTricksCode = new SCINTILLA_TRICKS( m_codePreview, wxT( "{}" ), false );
101 m_scintillaTricksSubckt = new SCINTILLA_TRICKS( m_subckt, wxT( "()" ), false );
102
103 m_paramGridMgr->Bind( wxEVT_PG_SELECTED, &DIALOG_SIM_MODEL::onParamGridSelectionChange, this );
104
105 wxPropertyGrid* grid = m_paramGrid->GetGrid();
106
107 // In wx 3.0 the color will be wrong sometimes.
108 grid->SetCellDisabledTextColour( wxSystemSettings::GetColour( wxSYS_COLOUR_GRAYTEXT ) );
109
110 grid->Bind( wxEVT_SET_FOCUS, &DIALOG_SIM_MODEL::onParamGridSetFocus, this );
111 grid->Bind( wxEVT_UPDATE_UI, &DIALOG_SIM_MODEL::onUpdateUI, this );
112
113 grid->DedicateKey( WXK_RETURN );
114 grid->DedicateKey( WXK_NUMPAD_ENTER );
115 grid->DedicateKey( WXK_UP );
116 grid->DedicateKey( WXK_DOWN );
117
118#if wxCHECK_VERSION( 3, 3, 0 )
119 grid->AddActionTrigger( wxPGKeyboardAction::Edit, WXK_RETURN );
120 grid->AddActionTrigger( wxPGKeyboardAction::NextProperty, WXK_RETURN );
121 grid->AddActionTrigger( wxPGKeyboardAction::Edit, WXK_NUMPAD_ENTER );
122 grid->AddActionTrigger( wxPGKeyboardAction::NextProperty, WXK_NUMPAD_ENTER );
123#else
124 grid->AddActionTrigger( wxPG_ACTION_EDIT, WXK_RETURN );
125 grid->AddActionTrigger( wxPG_ACTION_NEXT_PROPERTY, WXK_RETURN );
126 grid->AddActionTrigger( wxPG_ACTION_EDIT, WXK_NUMPAD_ENTER );
127 grid->AddActionTrigger( wxPG_ACTION_NEXT_PROPERTY, WXK_NUMPAD_ENTER );
128#endif
129
131 m_pinAssignmentsGrid->PushEventHandler( new GRID_TRICKS( m_pinAssignmentsGrid ) );
132
134
135 // Now all widgets have the size fixed, call FinishDialogSettings
137}
138
139
140template <typename T>
142{
143 // Disable all properties. This is necessary because some of their methods are called after
144 // destruction of DIALOG_SIM_MODEL, oddly. When disabled, they never access their models.
145 for( wxPropertyGridIterator it = m_paramGrid->GetIterator(); !it.AtEnd(); ++it )
146 {
147 SIM_PROPERTY* prop = dynamic_cast<SIM_PROPERTY*>( *it );
148
149 if( !prop )
150 continue;
151
152 prop->Disable();
153 }
154
155 // Delete the GRID_TRICKS.
156 m_pinAssignmentsGrid->PopEventHandler( true );
157
158 delete m_scintillaTricksCode;
159 delete m_scintillaTricksSubckt;
160}
161
162
163template <typename T>
165{
166 wxCommandEvent dummyEvent;
167 wxString deviceType;
168 wxString modelType;
169 wxString modelParams;
170 wxString pinMap;
171 bool storeInValue = false;
172
173 wxString msg;
174 WX_STRING_REPORTER reporter( &msg );
175
176 auto setFieldValue =
177 [&]( const wxString& aFieldName, const wxString& aValue )
178 {
179 for( SCH_FIELD& field : m_fields )
180 {
181 if( field.GetName() == aFieldName )
182 {
183 field.SetText( aValue );
184 return;
185 }
186 }
187
188 m_fields.emplace_back( &m_symbol, -1, aFieldName );
189 m_fields.back().SetText( aValue );
190 };
191
192 // Infer RLC and VI models if they aren't specified
193 if( SIM_MODEL::InferSimModel( m_symbol, &m_fields, false, SIM_VALUE_GRAMMAR::NOTATION::SI,
194 &deviceType, &modelType, &modelParams, &pinMap ) )
195 {
196 setFieldValue( SIM_DEVICE_FIELD, deviceType );
197
198 if( !modelType.IsEmpty() )
199 setFieldValue( SIM_DEVICE_SUBTYPE_FIELD, modelType );
200
201 setFieldValue( SIM_PARAMS_FIELD, modelParams );
202
203 setFieldValue( SIM_PINS_FIELD, pinMap );
204
205 storeInValue = true;
206
207 // In case the storeInValue checkbox is turned off (if it's left on then we'll overwrite
208 // this field with the actual value):
209 m_fields[ VALUE_FIELD ].SetText( wxT( "${SIM.PARAMS}" ) );
210 }
211
212 std::string libraryFilename = SIM_MODEL::GetFieldValue( &m_fields, SIM_LIBRARY::LIBRARY_FIELD );
213
214 if( libraryFilename != "" )
215 {
216 // The model is sourced from a library, optionally with instance overrides.
217 m_rbLibraryModel->SetValue( true );
218
219 if( !loadLibrary( libraryFilename, reporter ) )
220 {
221 if( reporter.HasMessage() )
222 m_infoBar->ShowMessage( msg );
223
224 m_libraryPathText->ChangeValue( libraryFilename );
225 m_curModelType = SIM_MODEL::ReadTypeFromFields( m_fields, reporter );
226
227 m_libraryModelsMgr.CreateModel( nullptr, m_sortedPartPins, m_fields, reporter );
228
229 m_modelNameChoice->Append( _( "<unknown>" ) );
230 m_modelNameChoice->SetSelection( 0 );
231 }
232 else
233 {
234 std::string modelName = SIM_MODEL::GetFieldValue( &m_fields, SIM_LIBRARY::NAME_FIELD );
235 int modelIdx = m_modelNameChoice->FindString( modelName );
236
237 if( modelIdx == wxNOT_FOUND )
238 {
239 m_infoBar->ShowMessage( wxString::Format( _( "No model named '%s' in library." ),
240 modelName ) );
241
242 // Default to first item in library
243 m_modelNameChoice->SetSelection( 0 );
244 }
245 else
246 {
247 m_infoBar->Hide();
248 m_modelNameChoice->SetSelection( modelIdx );
249 }
250
251 m_curModelType = curModel().GetType();
252 }
253
254 if( isIbisLoaded() && ( m_modelNameChoice->GetSelection() >= 0 ) )
255 {
256 int idx = m_modelNameChoice->GetSelection();
257 auto ibismodel = dynamic_cast<SIM_MODEL_IBIS*>( &m_libraryModelsMgr.GetModels()[idx].get() );
258
259 if( ibismodel )
260 {
261 onModelNameChoice( dummyEvent ); // refresh list of pins
262
263 int i = 0;
264
265 for( const std::pair<std::string, std::string>& strs : ibismodel->GetIbisPins() )
266 {
267 if( strs.first == SIM_MODEL::GetFieldValue( &m_fields, SIM_LIBRARY_IBIS::PIN_FIELD ) )
268 {
269 auto ibisLibrary = static_cast<const SIM_LIBRARY_IBIS*>( library() );
270
271 ibismodel->ChangePin( *ibisLibrary, strs.first );
272 m_pinCombobox->SetSelection( static_cast<int>( i ) );
273 break;
274 }
275 i++;
276 }
277
278 if( i < static_cast<int>( ibismodel->GetIbisPins().size() ) )
279 {
280 onPinCombobox( dummyEvent ); // refresh list of models
281
282 m_pinModelCombobox->SetStringSelection(
284 }
285
287 {
288 ibismodel->SwitchSingleEndedDiff( true );
289 m_differentialCheckbox->SetValue( true );
290 }
291 else
292 {
293 ibismodel->SwitchSingleEndedDiff( false );
294 m_differentialCheckbox->SetValue( false );
295 }
296 }
297 }
298 }
299 else if( !SIM_MODEL::GetFieldValue( &m_fields, SIM_DEVICE_FIELD ).empty()
301 {
302 // The model is sourced from the instance.
303 m_rbBuiltinModel->SetValue( true );
304
305 msg.clear();
306 m_curModelType = SIM_MODEL::ReadTypeFromFields( m_fields, reporter );
307
308 if( reporter.HasMessage() )
309 DisplayErrorMessage( this, msg );
310 }
311
312 for( SIM_MODEL::TYPE type : SIM_MODEL::TYPE_ITERATOR() )
313 {
314 if( m_rbBuiltinModel->GetValue() && type == m_curModelType )
315 {
316 msg.clear();
317 m_builtinModelsMgr.CreateModel( m_fields, m_sortedPartPins, false, reporter );
318
319 if( reporter.HasMessage() )
320 {
321 DisplayErrorMessage( this, _( "Failed to read simulation model from fields." )
322 + wxT( "\n\n" ) + msg );
323 }
324 }
325 else
326 {
327 m_builtinModelsMgr.CreateModel( type, m_sortedPartPins, reporter );
328 }
329
330 SIM_MODEL::DEVICE_T deviceTypeT = SIM_MODEL::TypeInfo( type ).deviceType;
331
332 if( !m_curModelTypeOfDeviceType.count( deviceTypeT ) )
333 m_curModelTypeOfDeviceType[deviceTypeT] = type;
334 }
335
336 if( storeInValue )
337 curModel().SetIsStoredInValue( true );
338
339 m_saveInValueCheckbox->SetValue( curModel().IsStoredInValue() );
340
341 onRadioButton( dummyEvent );
342 return DIALOG_SIM_MODEL_BASE::TransferDataToWindow();
343}
344
345
346template <typename T>
348{
349 m_pinAssignmentsGrid->CommitPendingChanges();
350 m_paramGrid->GetGrid()->CommitChangesFromEditor();
351
352 if( !DIALOG_SIM_MODEL_BASE::TransferDataFromWindow() )
353 return false;
354
355 SIM_MODEL& model = curModel();
356 std::string path;
357 std::string name;
358
359 if( m_rbLibraryModel->GetValue() )
360 {
361 path = m_libraryPathText->GetValue();
362 wxFileName fn( path );
363
364 if( fn.MakeRelativeTo( Prj().GetProjectPath() ) && !fn.GetFullPath().StartsWith( ".." ) )
365 path = fn.GetFullPath();
366
367 if( !m_modelNameChoice->IsEmpty() )
368 name = m_modelNameChoice->GetStringSelection().ToStdString();
369 else if( dynamic_cast<SIM_MODEL_SPICE_FALLBACK*>( &model ) )
371 }
372
375
376 if( isIbisLoaded() )
377 {
378 SIM_MODEL_IBIS* ibismodel = static_cast<SIM_MODEL_IBIS*>(
379 &m_libraryModelsMgr.GetModels().at( m_modelNameChoice->GetSelection() ).get() );
380
381 if( ibismodel )
382 {
383 std::string pins;
384 std::string modelName = std::string( m_pinModelCombobox->GetValue().c_str() );
385 std::string differential;
386
387 if( m_pinCombobox->GetSelection() >= 0 )
388 pins = ibismodel->GetIbisPins().at( m_pinCombobox->GetSelection() ).first;
389
390 if( ibismodel->CanDifferential() && m_differentialCheckbox->GetValue() )
391 differential = "1";
392
396 }
397 }
398
399 if( model.GetType() == SIM_MODEL::TYPE::RAWSPICE )
400 {
401 if( m_modelNotebook->GetSelection() == 0 )
402 updateModelCodeTab( &model );
403
404 wxString code = m_codePreview->GetText().Trim( true ).Trim( false );
405 model.SetParamValue( "model", std::string( code.ToUTF8() ) );
406 }
407
408 model.SetIsStoredInValue( m_saveInValueCheckbox->GetValue() );
409
410 for( int row = 0; row < m_pinAssignmentsGrid->GetNumberRows(); ++row )
411 {
412 wxString modelPinName = m_pinAssignmentsGrid->GetCellValue( row, PIN_COLUMN::MODEL );
413 wxString symbolPinName = m_sortedPartPins.at( row )->GetShownNumber();
414
415 model.AssignSymbolPinNumberToModelPin( getModelPinIndex( modelPinName ),
416 std::string( symbolPinName.ToUTF8() ) );
417 }
418
419 removeOrphanedPinAssignments( &model );
420
421 curModel().WriteFields( m_fields );
422
423 return true;
424}
425
426
427template <typename T>
429{
430 // always enable the library browser button -- it makes for fewer clicks if the user has a
431 // whole bunch of inferred passives that they want to specify library models for
432 m_browseButton->Enable();
433
434 // if we're in an undetermined state then enable everything for faster access
435 bool undetermined = !m_rbLibraryModel->GetValue() && !m_rbBuiltinModel->GetValue();
436 bool enableLibCtrls = m_rbLibraryModel->GetValue() || undetermined;
437 bool enableBuiltinCtrls = m_rbBuiltinModel->GetValue() || undetermined;
438
439 m_pathLabel->Enable( enableLibCtrls );
440 m_libraryPathText->Enable( enableLibCtrls );
441 m_modelNameLabel->Enable( enableLibCtrls );
442 m_modelNameChoice->Enable( enableLibCtrls );
443 m_pinLabel->Enable( enableLibCtrls );
444 m_pinCombobox->Enable( enableLibCtrls );
445 m_differentialCheckbox->Enable( enableLibCtrls );
446 m_pinModelLabel->Enable( enableLibCtrls );
447 m_pinModelCombobox->Enable( enableLibCtrls );
448 m_waveformLabel->Enable( enableLibCtrls );
449 m_waveformChoice->Enable( enableLibCtrls );
450
451 m_deviceLabel->Enable( enableBuiltinCtrls );
452 m_deviceChoice->Enable( enableBuiltinCtrls );
453 m_deviceSubtypeLabel->Enable( enableBuiltinCtrls );
454 m_deviceSubtypeChoice->Enable( enableBuiltinCtrls );
455
456 SIM_MODEL* model = &curModel();
457
458 updateIbisWidgets( model );
459 updateBuiltinModelWidgets( model );
460 updateModelParamsTab( model );
461 updateModelCodeTab( model );
462 updatePinAssignments( model, false );
463
464 std::string ref = SIM_MODEL::GetFieldValue( &m_fields, SIM_REFERENCE_FIELD );
465
466 m_modelPanel->Layout();
467 m_pinAssignmentsPanel->Layout();
468 m_parametersPanel->Layout();
469 m_codePanel->Layout();
470
471 SendSizeEvent( wxSEND_EVENT_POST );
472
473 m_prevModel = &curModel();
474}
475
476
477template <typename T>
479{
480 SIM_MODEL_IBIS* modelibis = isIbisLoaded() ? dynamic_cast<SIM_MODEL_IBIS*>( aModel )
481 : nullptr;
482
483 m_pinLabel->Show( isIbisLoaded() );
484 m_pinCombobox->Show( isIbisLoaded() );
485 m_pinModelLabel->Show( isIbisLoaded() );
486 m_pinModelCombobox->Show( isIbisLoaded() );
487 m_waveformLabel->Show( isIbisLoaded() );
488 m_waveformChoice->Show( isIbisLoaded() );
489
490 if( aModel != m_prevModel )
491 {
492 m_waveformChoice->Clear();
493
494 if( isIbisLoaded() )
495 {
496 for( SIM_MODEL::TYPE type : { SIM_MODEL::TYPE::KIBIS_DEVICE,
497 SIM_MODEL::TYPE::KIBIS_DRIVER_DC,
498 SIM_MODEL::TYPE::KIBIS_DRIVER_RECT,
499 SIM_MODEL::TYPE::KIBIS_DRIVER_PRBS } )
500 {
501 SIM_MODEL::DEVICE_T deviceType = SIM_MODEL::TypeInfo( type ).deviceType;
502 const std::string& deviceTypeDesc = SIM_MODEL::DeviceInfo( deviceType ).description;
503
504 if( deviceType == aModel->GetDeviceType()
505 || deviceTypeDesc == aModel->GetDeviceInfo().description )
506 {
507 m_waveformChoice->Append( SIM_MODEL::TypeInfo( type ).description );
508
509 if( type == aModel->GetType() )
510 m_waveformChoice->SetSelection( m_waveformChoice->GetCount() - 1 );
511 }
512 }
513 }
514 }
515
516 m_differentialCheckbox->Show( isIbisLoaded() && modelibis && modelibis->CanDifferential() );
517 m_modelNameLabel->SetLabel( isIbisLoaded() ? _( "Component:" ) : _( "Model:" ) );
518}
519
520
521template <typename T>
523{
524 // Change the Type choice to match the current device type.
525 if( aModel != m_prevModel )
526 {
527 m_deviceChoice->Clear();
528 m_deviceSubtypeChoice->Clear();
529
530 if( !m_rbLibraryModel->GetValue() )
531 {
532 for( SIM_MODEL::DEVICE_T deviceType : SIM_MODEL::DEVICE_T_ITERATOR() )
533 {
534 if( !SIM_MODEL::DeviceInfo( deviceType ).showInMenu )
535 continue;
536
537 m_deviceChoice->Append( SIM_MODEL::DeviceInfo( deviceType ).description );
538
539 if( equivalent( deviceType, aModel->GetDeviceType() ) )
540 m_deviceChoice->SetSelection( m_deviceChoice->GetCount() - 1 );
541 }
542
543 for( SIM_MODEL::TYPE type : SIM_MODEL::TYPE_ITERATOR() )
544 {
545 if( type == SIM_MODEL::TYPE::KIBIS_DEVICE
546 || type == SIM_MODEL::TYPE::KIBIS_DRIVER_DC
547 || type == SIM_MODEL::TYPE::KIBIS_DRIVER_RECT
548 || type == SIM_MODEL::TYPE::KIBIS_DRIVER_PRBS )
549 {
550 continue;
551 }
552
553 SIM_MODEL::DEVICE_T deviceType = SIM_MODEL::TypeInfo( type ).deviceType;
554 const std::string& deviceTypeDesc = SIM_MODEL::DeviceInfo( deviceType ).description;
555
556 if( deviceType == aModel->GetDeviceType()
557 || deviceTypeDesc == aModel->GetDeviceInfo().description )
558 {
559 m_deviceSubtypeChoice->Append( SIM_MODEL::TypeInfo( type ).description );
560
561 if( type == aModel->GetType() )
562 m_deviceSubtypeChoice->SetSelection( m_deviceSubtypeChoice->GetCount() - 1 );
563 }
564 }
565 }
566
567 m_deviceSubtypeLabel->Show( m_deviceSubtypeChoice->GetCount() > 1 );
568 m_deviceSubtypeChoice->Show( m_deviceSubtypeChoice->GetCount() > 1 );
569 }
570
571 if( dynamic_cast<SIM_MODEL_RAW_SPICE*>( aModel ) )
572 m_modelNotebook->SetSelection( 1 );
573 else
574 m_modelNotebook->SetSelection( 0 );
575
576 if( aModel->HasPrimaryValue() )
577 {
578 const SIM_MODEL::PARAM& primary = aModel->GetParam( 0 );
579
580 m_saveInValueCheckbox->SetLabel( wxString::Format( _( "Save parameter '%s (%s)' in Value "
581 "field" ),
582 primary.info.description,
583 primary.info.name ) );
584 m_saveInValueCheckbox->Enable( true );
585 }
586 else
587 {
588 m_saveInValueCheckbox->SetLabel( _( "Save primary parameter in Value field" ) );
589 m_saveInValueCheckbox->SetValue( false );
590 m_saveInValueCheckbox->Enable( false );
591 }
592}
593
594
595template <typename T>
597{
598 if( aModel != m_prevModel )
599 {
600 // This wxPropertyGridManager column and header stuff has to be here because it segfaults in
601 // the constructor.
602
603 m_paramGridMgr->SetColumnCount( PARAM_COLUMN::END_ );
604
605 m_paramGridMgr->SetColumnTitle( PARAM_COLUMN::DESCRIPTION, _( "Parameter" ) );
606 m_paramGridMgr->SetColumnTitle( PARAM_COLUMN::UNIT, _( "Unit" ) );
607 m_paramGridMgr->SetColumnTitle( PARAM_COLUMN::DEFAULT, _( "Default" ) );
608 m_paramGridMgr->SetColumnTitle( PARAM_COLUMN::TYPE, _( "Type" ) );
609
610 m_paramGridMgr->ShowHeader();
611
612
613 m_paramGrid->Clear();
614
615 m_firstCategory = m_paramGrid->Append( new wxPropertyCategory( "Geometry" ) );
616 m_paramGrid->HideProperty( "Geometry" );
617
618 m_paramGrid->Append( new wxPropertyCategory( "AC" ) );
619 m_paramGrid->HideProperty( "AC" );
620
621 m_paramGrid->Append( new wxPropertyCategory( "DC" ) );
622 m_paramGrid->HideProperty( "DC" );
623
624 m_paramGrid->Append( new wxPropertyCategory( "S-Parameters" ) );
625 m_paramGrid->HideProperty( "S-Parameters" );
626
627 m_paramGrid->Append( new wxPropertyCategory( "Capacitance" ) );
628 m_paramGrid->HideProperty( "Capacitance" );
629
630 m_paramGrid->Append( new wxPropertyCategory( "Temperature" ) );
631 m_paramGrid->HideProperty( "Temperature" );
632
633 m_paramGrid->Append( new wxPropertyCategory( "Noise" ) );
634 m_paramGrid->HideProperty( "Noise" );
635
636 m_paramGrid->Append( new wxPropertyCategory( "Distributed Quantities" ) );
637 m_paramGrid->HideProperty( "Distributed Quantities" );
638
639 m_paramGrid->Append( new wxPropertyCategory( "Waveform" ) );
640 m_paramGrid->HideProperty( "Waveform" );
641
642 m_paramGrid->Append( new wxPropertyCategory( "Limiting Values" ) );
643 m_paramGrid->HideProperty( "Limiting Values" );
644
645 m_paramGrid->Append( new wxPropertyCategory( "Advanced" ) );
646 m_paramGrid->HideProperty( "Advanced" );
647
648 m_paramGrid->Append( new wxPropertyCategory( "Flags" ) );
649 m_paramGrid->HideProperty( "Flags" );
650
651 m_paramGrid->CollapseAll();
652
653 for( int i = 0; i < aModel->GetParamCount(); ++i )
654 addParamPropertyIfRelevant( aModel, i );
655
656 m_paramGrid->CollapseAll();
657 m_paramGrid->Expand( "AC" );
658 m_paramGrid->Expand( "Waveform" );
659 }
660
661 adjustParamGridColumns( m_paramGrid->GetGrid()->GetSize().GetX(), true );
662
663 // Set all properties to default colors.
664 // Update properties in models that have autofill.
665 for( wxPropertyGridIterator it = m_paramGrid->GetIterator(); !it.AtEnd(); ++it )
666 {
667 wxColour bgCol = m_paramGrid->GetGrid()->GetPropertyDefaultCell().GetBgCol();
668 wxColour fgCol = m_paramGrid->GetGrid()->GetPropertyDefaultCell().GetFgCol();
669
670 for( int col = 0; col < m_paramGridMgr->GetColumnCount(); ++col )
671 {
672 ( *it )->GetCell( col ).SetBgCol( bgCol );
673 ( *it )->GetCell( col ).SetFgCol( fgCol );
674 }
675
676 SIM_PROPERTY* prop = dynamic_cast<SIM_PROPERTY*>( *it );
677
678 if( !prop )
679 continue;
680
681 const SIM_MODEL::PARAM& param = prop->GetParam();
682
683 // Model values other than the currently edited value may have changed. Update them.
684 // This feature is called "autofill" and present only in certain models. Don't do it for
685 // models that don't have it for performance reasons.
686 if( aModel->HasAutofill() )
687 ( *it )->SetValueFromString( param.value );
688 }
689}
690
691
692template <typename T>
694{
695 if( dynamic_cast<SIM_MODEL_SPICE_FALLBACK*>( aModel ) )
696 return;
697
698 wxString text;
699 SPICE_ITEM item;
700
701 item.modelName = m_modelNameChoice->GetStringSelection();
702
703 if( m_rbBuiltinModel->GetValue() || item.modelName == "" )
704 item.modelName = m_fields.at( REFERENCE_FIELD ).GetText();
705
706 text << aModel->SpiceGenerator().Preview( item );
707
708 m_codePreview->SetText( text );
709 m_codePreview->SelectNone();
710}
711
712
713template <typename T>
714void DIALOG_SIM_MODEL<T>::updatePinAssignments( SIM_MODEL* aModel, bool aForceUpdatePins )
715{
716 if( m_pinAssignmentsGrid->GetNumberRows() == 0 )
717 {
718 m_pinAssignmentsGrid->AppendRows( static_cast<int>( m_sortedPartPins.size() ) );
719
720 for( int ii = 0; ii < m_pinAssignmentsGrid->GetNumberRows(); ++ii )
721 {
722 wxString symbolPinString = getSymbolPinString( ii );
723
724 m_pinAssignmentsGrid->SetReadOnly( ii, PIN_COLUMN::SYMBOL );
725 m_pinAssignmentsGrid->SetCellValue( ii, PIN_COLUMN::SYMBOL, symbolPinString );
726 }
727
728 aForceUpdatePins = true;
729 }
730
731 if( aForceUpdatePins )
732 {
733 // Reset the grid.
734 for( int row = 0; row < m_pinAssignmentsGrid->GetNumberRows(); ++row )
735 m_pinAssignmentsGrid->SetCellValue( row, PIN_COLUMN::MODEL, _( "Not Connected" ) );
736
737 // Now set up the grid values in the Model column.
738 for( int modelPinIndex = 0; modelPinIndex < aModel->GetPinCount(); ++modelPinIndex )
739 {
740 wxString symbolPinNumber = aModel->GetPin( modelPinIndex ).symbolPinNumber;
741
742 if( symbolPinNumber == "" )
743 continue;
744
745 int symbolPinRow = findSymbolPinRow( symbolPinNumber );
746
747 if( symbolPinRow == -1 )
748 continue;
749
750 wxString modelPinString = getModelPinString( aModel, modelPinIndex );
751 m_pinAssignmentsGrid->SetCellValue( symbolPinRow, PIN_COLUMN::MODEL, modelPinString );
752 }
753 }
754
755 for( int ii = 0; ii < m_pinAssignmentsGrid->GetNumberRows(); ++ii )
756 {
757 // Set up the Model column cell editors with dropdown options.
758 std::vector<BITMAPS> modelPinIcons;
759 wxArrayString modelPinChoices;
760
761 for( int jj = 0; jj < aModel->GetPinCount(); ++jj )
762 {
763 if( aModel->GetPin( jj ).symbolPinNumber != "" )
764 modelPinIcons.push_back( PinShapeGetBitmap( GRAPHIC_PINSHAPE::LINE ) );
765 else
766 modelPinIcons.push_back( BITMAPS::INVALID_BITMAP );
767
768 modelPinChoices.Add( getModelPinString( aModel, jj ) );
769 }
770
771 modelPinIcons.push_back( BITMAPS::INVALID_BITMAP );
772 modelPinChoices.Add( _( "Not Connected" ) );
773
774 // Using `new` here shouldn't cause a memory leak because `SetCellEditor()` calls
775 // `DecRef()` on its last editor.
776 m_pinAssignmentsGrid->SetCellEditor( ii, PIN_COLUMN::MODEL,
777 new GRID_CELL_ICON_TEXT_POPUP( modelPinIcons,
778 modelPinChoices ) );
779 }
780
781 // TODO: Show a preview of the symbol with the pin numbers shown.
782
783 if( aModel->GetType() == SIM_MODEL::TYPE::SUBCKT )
784 {
785 SIM_MODEL_SUBCKT* subckt = static_cast<SIM_MODEL_SUBCKT*>( aModel );
786 m_subckt->SetText( subckt->GetSpiceCode() );
787 m_subckt->SetEditable( false );
788 }
789 else
790 {
791 m_subcktLabel->Show( false );
792 m_subckt->Show( false );
793 }
794}
795
796
797template <typename T>
799{
800 for( int i = 0; i < aModel->GetPinCount(); ++i )
801 {
802 if( !m_symbol.GetPin( aModel->GetPin( i ).symbolPinNumber ) )
803 aModel->AssignSymbolPinNumberToModelPin( i, "" );
804 }
805}
806
807
808template <typename T>
809bool DIALOG_SIM_MODEL<T>::loadLibrary( const wxString& aLibraryPath, REPORTER& aReporter,
810 bool aForceReload )
811{
812 if( m_prevLibrary == aLibraryPath && !aForceReload )
813 return true;
814
815 m_libraryModelsMgr.SetForceFullParse();
816 m_libraryModelsMgr.SetLibrary( aLibraryPath, aReporter );
817
818 if( aReporter.HasMessage() )
819 return false;
820
821 std::string modelName = SIM_MODEL::GetFieldValue( &m_fields, SIM_LIBRARY::NAME_FIELD );
822
823 for( const auto& [baseModelName, baseModel] : library()->GetModels() )
824 {
825 if( baseModelName == modelName )
826 m_libraryModelsMgr.CreateModel( &baseModel, m_sortedPartPins, m_fields, aReporter );
827 else
828 m_libraryModelsMgr.CreateModel( &baseModel, m_sortedPartPins, aReporter );
829 }
830
831 m_rbLibraryModel->SetValue( true );
832 m_libraryPathText->ChangeValue( aLibraryPath );
833
834 wxArrayString modelNames;
835
836 for( const auto& [name, model] : library()->GetModels() )
837 modelNames.Add( name );
838
839 m_modelNameChoice->Clear();
840 m_modelNameChoice->Append( modelNames );
841
842 if( isIbisLoaded() )
843 {
844 wxArrayString emptyArray;
845 m_pinModelCombobox->Set( emptyArray );
846 m_pinCombobox->Set( emptyArray );
847 m_pinModelCombobox->SetSelection( -1 );
848 m_pinCombobox->SetSelection( -1 );
849 }
850
851 m_prevLibrary = aLibraryPath;
852 return true;
853}
854
855
856template <typename T>
858{
859 if( aModel->GetParam( aParamIndex ).info.dir == SIM_MODEL::PARAM::DIR_OUT )
860 return;
861
862 switch( aModel->GetParam( aParamIndex ).info.category )
863 {
864 case CATEGORY::AC:
865 m_paramGrid->HideProperty( "AC", false );
866 m_paramGrid->AppendIn( "AC", newParamProperty( aModel, aParamIndex ) );
867 break;
868
869 case CATEGORY::DC:
870 m_paramGrid->HideProperty( "DC", false );
871 m_paramGrid->AppendIn( "DC", newParamProperty( aModel, aParamIndex ) );
872 break;
873
874 case CATEGORY::S_PARAM:
875 m_paramGrid->HideProperty( "S-Parameters", false );
876 m_paramGrid->AppendIn( "S-Parameters", newParamProperty( aModel, aParamIndex ) );
877 break;
878
879 case CATEGORY::CAPACITANCE:
880 m_paramGrid->HideProperty( "Capacitance", false );
881 m_paramGrid->AppendIn( "Capacitance", newParamProperty( aModel, aParamIndex ) );
882 break;
883
884 case CATEGORY::TEMPERATURE:
885 m_paramGrid->HideProperty( "Temperature", false );
886 m_paramGrid->AppendIn( "Temperature", newParamProperty( aModel, aParamIndex ) );
887 break;
888
889 case CATEGORY::NOISE:
890 m_paramGrid->HideProperty( "Noise", false );
891 m_paramGrid->AppendIn( "Noise", newParamProperty( aModel, aParamIndex ) );
892 break;
893
894 case CATEGORY::DISTRIBUTED_QUANTITIES:
895 m_paramGrid->HideProperty( "Distributed Quantities", false );
896 m_paramGrid->AppendIn( "Distributed Quantities", newParamProperty( aModel, aParamIndex ) );
897 break;
898
899 case CATEGORY::WAVEFORM:
900 m_paramGrid->HideProperty( "Waveform", false );
901 m_paramGrid->AppendIn( "Waveform", newParamProperty( aModel, aParamIndex ) );
902 break;
903
904 case CATEGORY::GEOMETRY:
905 m_paramGrid->HideProperty( "Geometry", false );
906 m_paramGrid->AppendIn( "Geometry", newParamProperty( aModel, aParamIndex ) );
907 break;
908
909 case CATEGORY::LIMITING_VALUES:
910 m_paramGrid->HideProperty( "Limiting Values", false );
911 m_paramGrid->AppendIn( "Limiting Values", newParamProperty( aModel, aParamIndex ) );
912 break;
913
914 case CATEGORY::ADVANCED:
915 m_paramGrid->HideProperty( "Advanced", false );
916 m_paramGrid->AppendIn( "Advanced", newParamProperty( aModel, aParamIndex ) );
917 break;
918
919 case CATEGORY::FLAGS:
920 m_paramGrid->HideProperty( "Flags", false );
921 m_paramGrid->AppendIn( "Flags", newParamProperty( aModel, aParamIndex ) );
922 break;
923
924 default:
925 m_paramGrid->Insert( m_firstCategory, newParamProperty( aModel, aParamIndex ) );
926 break;
927
928 case CATEGORY::INITIAL_CONDITIONS:
929 case CATEGORY::SUPERFLUOUS:
930 return;
931 }
932}
933
934
935template <typename T>
936wxPGProperty* DIALOG_SIM_MODEL<T>::newParamProperty( SIM_MODEL* aModel, int aParamIndex ) const
937{
938 const SIM_MODEL::PARAM& param = aModel->GetParam( aParamIndex );
939 wxString paramDescription;
940
941 if( param.info.description == "" )
942 paramDescription = wxString::Format( "%s", param.info.name );
943 else
944 paramDescription = wxString::Format( "%s (%s)", param.info.description, param.info.name );
945
946 wxPGProperty* prop = nullptr;
947
948 switch( param.info.type )
949 {
951 // TODO.
952 prop = new SIM_BOOL_PROPERTY( paramDescription, param.info.name, *aModel, aParamIndex );
953 prop->SetAttribute( wxPG_BOOL_USE_CHECKBOX, true );
954 break;
955
957 prop = new SIM_STRING_PROPERTY( paramDescription, param.info.name, *aModel, aParamIndex,
959 break;
960
962 prop = new SIM_STRING_PROPERTY( paramDescription, param.info.name, *aModel, aParamIndex,
964 break;
965
966 //case TYPE_COMPLEX:
967 // break;
968
970 // Special case: K-line mutual inductance statement parameters l1 and l2 are references
971 // to other inductors in the circuit.
972 if( dynamic_cast<SIM_MODEL_L_MUTUAL*>( aModel ) != nullptr
973 && ( param.info.name == "l1" || param.info.name == "l2" ) )
974 {
975 wxArrayString inductors;
976
977 if( SCH_EDIT_FRAME* schEditFrame = dynamic_cast<SCH_EDIT_FRAME*>( m_frame ) )
978 {
979 SPICE_CIRCUIT_MODEL circuit( &schEditFrame->Schematic() );
980 NULL_REPORTER devNul;
981
983 devNul );
984
985 for( const SPICE_ITEM& item : circuit.GetItems() )
986 {
987 if( item.model->GetDeviceType() == SIM_MODEL::DEVICE_T::L )
988 inductors.push_back( item.refName );
989 }
990
991 inductors.Sort(
992 []( const wxString& a, const wxString& b ) -> int
993 {
994 return StrNumCmp( a, b, true );
995 } );
996 }
997
998 if( inductors.empty() )
999 {
1000 prop = new SIM_STRING_PROPERTY( paramDescription, param.info.name, *aModel,
1001 aParamIndex, SIM_VALUE::TYPE_STRING );
1002 }
1003 else
1004 {
1005 prop = new SIM_ENUM_PROPERTY( paramDescription, param.info.name, *aModel,
1006 aParamIndex, inductors );
1007 }
1008 }
1009 else if( param.info.enumValues.empty() )
1010 {
1011 prop = new SIM_STRING_PROPERTY( paramDescription, param.info.name, *aModel,
1012 aParamIndex, SIM_VALUE::TYPE_STRING );
1013 }
1014 else
1015 {
1016 wxArrayString values;
1017
1018 for( const std::string& string : aModel->GetParam( aParamIndex ).info.enumValues )
1019 values.Add( string );
1020
1021 prop = new SIM_ENUM_PROPERTY( paramDescription, param.info.name, *aModel, aParamIndex,
1022 values );
1023 }
1024 break;
1025
1026 default:
1027 prop = new wxStringProperty( paramDescription, param.info.name );
1028 break;
1029 }
1030
1031 prop->SetAttribute( wxPG_ATTR_UNITS, wxString::FromUTF8( param.info.unit.c_str() ) );
1032
1033 // Legacy due to the way we extracted the parameters from Ngspice.
1034 prop->SetCell( 3, wxString::FromUTF8( param.info.defaultValue ) );
1035
1036 wxString typeStr;
1037
1038 switch( param.info.type )
1039 {
1040 case SIM_VALUE::TYPE_BOOL: typeStr = wxT( "Bool" ); break;
1041 case SIM_VALUE::TYPE_INT: typeStr = wxT( "Int" ); break;
1042 case SIM_VALUE::TYPE_FLOAT: typeStr = wxT( "Float" ); break;
1043 case SIM_VALUE::TYPE_COMPLEX: typeStr = wxT( "Complex" ); break;
1044 case SIM_VALUE::TYPE_STRING: typeStr = wxT( "String" ); break;
1045 case SIM_VALUE::TYPE_BOOL_VECTOR: typeStr = wxT( "Bool Vector" ); break;
1046 case SIM_VALUE::TYPE_INT_VECTOR: typeStr = wxT( "Int Vector" ); break;
1047 case SIM_VALUE::TYPE_FLOAT_VECTOR: typeStr = wxT( "Float Vector" ); break;
1048 case SIM_VALUE::TYPE_COMPLEX_VECTOR: typeStr = wxT( "Complex Vector" ); break;
1049 }
1050
1051 prop->SetCell( PARAM_COLUMN::TYPE, typeStr );
1052
1053 return prop;
1054}
1055
1056
1057template <typename T>
1058int DIALOG_SIM_MODEL<T>::findSymbolPinRow( const wxString& aSymbolPinNumber ) const
1059{
1060 for( int row = 0; row < static_cast<int>( m_sortedPartPins.size() ); ++row )
1061 {
1062 SCH_PIN* pin = m_sortedPartPins[row];
1063
1064 if( pin->GetNumber() == aSymbolPinNumber )
1065 return row;
1066 }
1067
1068 return -1;
1069}
1070
1071
1072template <typename T>
1074{
1075 if( m_rbLibraryModel->GetValue() )
1076 {
1077 int sel = m_modelNameChoice->GetSelection();
1078
1079 if( sel >= 0 && sel < static_cast<int>( m_libraryModelsMgr.GetModels().size() ) )
1080 return m_libraryModelsMgr.GetModels().at( sel ).get();
1081 }
1082 else
1083 {
1084 if( static_cast<int>( m_curModelType ) < static_cast<int>( m_builtinModelsMgr.GetModels().size() ) )
1085 return m_builtinModelsMgr.GetModels().at( static_cast<int>( m_curModelType ) );
1086 }
1087
1088 return m_builtinModelsMgr.GetModels().at( static_cast<int>( SIM_MODEL::TYPE::NONE ) );
1089}
1090
1091
1092template <typename T>
1094{
1095 if( m_libraryModelsMgr.GetLibraries().size() == 1 )
1096 return &m_libraryModelsMgr.GetLibraries().begin()->second.get();
1097
1098 return nullptr;
1099}
1100
1101
1102template <typename T>
1103wxString DIALOG_SIM_MODEL<T>::getSymbolPinString( int symbolPinIndex ) const
1104{
1105 SCH_PIN* pin = m_sortedPartPins.at( symbolPinIndex );
1106 wxString pinNumber;
1107 wxString pinName;
1108
1109 if( pin )
1110 {
1111 pinNumber = pin->GetShownNumber();
1112 pinName = pin->GetShownName();
1113 }
1114
1115 if( !pinName.IsEmpty() && pinName != pinNumber )
1116 pinNumber += wxString::Format( wxT( " (%s)" ), pinName );
1117
1118 return pinNumber;
1119}
1120
1121
1122template <typename T>
1123wxString DIALOG_SIM_MODEL<T>::getModelPinString( SIM_MODEL* aModel, int aModelPinIndex ) const
1124{
1125 const wxString& modelPinName = aModel->GetPin( aModelPinIndex ).modelPinName;
1126
1127 LOCALE_IO toggle;
1128
1129 wxString modelPinNumber = wxString::Format( "%d", aModelPinIndex + 1 );
1130
1131 if( !modelPinName.IsEmpty() && modelPinName != modelPinNumber )
1132 modelPinNumber += wxString::Format( wxT( " (%s)" ), modelPinName );
1133
1134 return modelPinNumber;
1135}
1136
1137
1138template <typename T>
1139int DIALOG_SIM_MODEL<T>::getModelPinIndex( const wxString& aModelPinString ) const
1140{
1141 if( aModelPinString == "Not Connected" )
1143
1144 int length = aModelPinString.Find( " " );
1145
1146 if( length == wxNOT_FOUND )
1147 length = static_cast<int>( aModelPinString.Length() );
1148
1149 long result = 0;
1150 aModelPinString.Mid( 0, length ).ToCLong( &result );
1151
1152 return static_cast<int>( result - 1 );
1153}
1154
1155
1156template <typename T>
1157void DIALOG_SIM_MODEL<T>::onRadioButton( wxCommandEvent& aEvent )
1158{
1159 m_prevModel = nullptr; // Ensure the Model panel will be rebuild after updating other params.
1160 updateWidgets();
1161}
1162
1163
1164template <typename T>
1165void DIALOG_SIM_MODEL<T>::onLibraryPathText( wxCommandEvent& aEvent )
1166{
1167 m_rbLibraryModel->SetValue( true );
1168}
1169
1170
1171template <typename T>
1173{
1174 m_rbLibraryModel->SetValue( true );
1175
1176 wxString msg;
1177 WX_STRING_REPORTER reporter( &msg );
1178 wxString path = m_libraryPathText->GetValue();
1179
1180 if( loadLibrary( path, reporter, true ) )
1181 m_infoBar->Hide();
1182 else if( reporter.HasMessage() )
1183 m_infoBar->ShowMessage( msg );
1184
1185 updateWidgets();
1186}
1187
1188
1189template <typename T>
1191{
1192 CallAfter(
1193 [this]()
1194 {
1195 // Disable logging -- otherwise we'll end up in an endless loop of show-log,
1196 // kill-focus, show-log, kill-focus, etc.
1197 wxLogNull doNotLog;
1198
1199 wxCommandEvent dummy;
1200 onLibraryPathTextEnter( dummy );
1201 } );
1202}
1203
1204
1205template <typename T>
1206void DIALOG_SIM_MODEL<T>::onBrowseButtonClick( wxCommandEvent& aEvent )
1207{
1208 static wxString s_mruPath;
1209
1210 wxString path = s_mruPath.IsEmpty() ? Prj().GetProjectPath() : s_mruPath;
1211 wxFileDialog dlg( this, _( "Browse Models" ), path );
1212
1213 if( dlg.ShowModal() == wxID_CANCEL )
1214 return;
1215
1216 m_rbLibraryModel->SetValue( true );
1217
1218 path = dlg.GetPath();
1219 wxFileName fn( path );
1220
1221 s_mruPath = fn.GetPath();
1222
1223 if( fn.MakeRelativeTo( Prj().GetProjectPath() ) && !fn.GetFullPath().StartsWith( wxS( ".." ) ) )
1224 path = fn.GetFullPath();
1225
1226 wxString msg;
1227 WX_STRING_REPORTER reporter( &msg );
1228
1229 if( loadLibrary( path, reporter, true ) )
1230 m_infoBar->Hide();
1231 else
1232 m_infoBar->ShowMessage( msg );
1233
1234 updateWidgets();
1235}
1236
1237
1238template <typename T>
1239void DIALOG_SIM_MODEL<T>::onModelNameChoice( wxCommandEvent& aEvent )
1240{
1241 if( isIbisLoaded() )
1242 {
1243 wxArrayString pinLabels;
1244 SIM_MODEL_IBIS* modelkibis = dynamic_cast<SIM_MODEL_IBIS*>( &curModel() );
1245
1246 wxCHECK2( modelkibis, return );
1247
1248 for( std::pair<wxString, wxString> strs : modelkibis->GetIbisPins() )
1249 pinLabels.Add( strs.first + wxT( " - " ) + strs.second );
1250
1251 m_pinCombobox->Set( pinLabels );
1252
1253 wxArrayString emptyArray;
1254 m_pinModelCombobox->Set( emptyArray );
1255 }
1256
1257 m_rbLibraryModel->SetValue( true );
1258 updateWidgets();
1259}
1260
1261
1262template <typename T>
1263void DIALOG_SIM_MODEL<T>::onPinCombobox( wxCommandEvent& aEvent )
1264{
1265 wxArrayString modelLabels;
1266
1267 SIM_MODEL_IBIS& ibisModel = static_cast<SIM_MODEL_IBIS&>( curModel() );
1268
1269 std::vector<std::pair<std::string, std::string>> strs = ibisModel.GetIbisPins();
1270 std::string pinNumber = strs.at( m_pinCombobox->GetSelection() ).first;
1271
1272 const SIM_LIBRARY_IBIS* ibisLibrary = dynamic_cast<const SIM_LIBRARY_IBIS*>( library() );
1273
1274 ibisModel.ChangePin( *ibisLibrary, pinNumber );
1275
1276 ibisModel.m_enableDiff = ibisLibrary->isPinDiff( ibisModel.GetComponentName(), pinNumber );
1277
1278 for( wxString modelName : ibisModel.GetIbisModels() )
1279 modelLabels.Add( modelName );
1280
1281 m_pinModelCombobox->Set( modelLabels );
1282
1283 if( m_pinModelCombobox->GetCount() == 1 )
1284 m_pinModelCombobox->SetSelection( 0 );
1285 else
1286 m_pinModelCombobox->SetSelection( -1 );
1287
1288 updateWidgets();
1289}
1290
1291
1292template <typename T>
1294{
1295 m_pinCombobox->SetSelection( m_pinCombobox->FindString( m_pinCombobox->GetValue() ) );
1296
1297 onPinModelCombobox( aEvent );
1298}
1299
1300
1301template <typename T>
1302void DIALOG_SIM_MODEL<T>::onPinModelCombobox( wxCommandEvent& aEvent )
1303{
1304 updateWidgets();
1305}
1306
1307
1308template <typename T>
1310{
1311 m_pinModelCombobox->SetSelection( m_pinModelCombobox->FindString( m_pinModelCombobox->GetValue() ) );
1312}
1313
1314template <typename T>
1316{
1317 SIM_MODEL_IBIS* modelibis = dynamic_cast<SIM_MODEL_IBIS*>( &curModel() );
1318
1319 wxCHECK( modelibis, /* void */ );
1320
1321 bool diff = m_differentialCheckbox->GetValue() && modelibis->CanDifferential();
1322 modelibis->SwitchSingleEndedDiff( diff );
1323
1324 updateWidgets();
1325}
1326
1327
1328template <typename T>
1329void DIALOG_SIM_MODEL<T>::onDeviceTypeChoice( wxCommandEvent& aEvent )
1330{
1331 m_rbBuiltinModel->SetValue( true );
1332
1333 for( SIM_MODEL::DEVICE_T deviceType : SIM_MODEL::DEVICE_T_ITERATOR() )
1334 {
1335 if( SIM_MODEL::DeviceInfo( deviceType ).description == m_deviceChoice->GetStringSelection() )
1336 {
1337 m_curModelType = m_curModelTypeOfDeviceType.at( deviceType );
1338 break;
1339 }
1340 }
1341
1342 updateWidgets();
1343}
1344
1345
1346template <typename T>
1347void DIALOG_SIM_MODEL<T>::onWaveformChoice( wxCommandEvent& aEvent )
1348{
1349 SIM_MODEL::DEVICE_T deviceType = curModel().GetDeviceType();
1350 wxString typeDescription = m_waveformChoice->GetStringSelection();
1351
1352 for( SIM_MODEL::TYPE type : { SIM_MODEL::TYPE::KIBIS_DEVICE,
1353 SIM_MODEL::TYPE::KIBIS_DRIVER_DC,
1354 SIM_MODEL::TYPE::KIBIS_DRIVER_RECT,
1355 SIM_MODEL::TYPE::KIBIS_DRIVER_PRBS } )
1356 {
1357 if( equivalent( deviceType, SIM_MODEL::TypeInfo( type ).deviceType )
1358 && typeDescription == SIM_MODEL::TypeInfo( type ).description )
1359 {
1360 int idx = m_modelNameChoice->GetSelection();
1361
1362 auto& baseModel = static_cast<SIM_MODEL_IBIS&>( m_libraryModelsMgr.GetModels()[idx].get() );
1363
1364 m_libraryModelsMgr.SetModel( idx, std::make_unique<SIM_MODEL_IBIS>( type, baseModel ) );
1365
1366 try
1367 {
1368 m_libraryModelsMgr.GetModels()[idx].get().ReadDataFields( &m_fields, m_sortedPartPins );
1369 }
1370 catch( IO_ERROR& err )
1371 {
1372 DisplayErrorMessage( this, err.What() );
1373 }
1374
1375 m_curModelType = type;
1376 break;
1377 }
1378 }
1379
1380 m_curModelTypeOfDeviceType.at( deviceType ) = m_curModelType;
1381 updateWidgets();
1382}
1383
1384
1385template <typename T>
1386void DIALOG_SIM_MODEL<T>::onTypeChoice( wxCommandEvent& aEvent )
1387{
1388 SIM_MODEL::DEVICE_T deviceType = curModel().GetDeviceType();
1389 wxString typeDescription = m_deviceSubtypeChoice->GetStringSelection();
1390
1391 for( SIM_MODEL::TYPE type : SIM_MODEL::TYPE_ITERATOR() )
1392 {
1393 if( equivalent( deviceType, SIM_MODEL::TypeInfo( type ).deviceType )
1394 && typeDescription == SIM_MODEL::TypeInfo( type ).description )
1395 {
1396 m_curModelType = type;
1397 break;
1398 }
1399 }
1400
1401 m_curModelTypeOfDeviceType.at( deviceType ) = m_curModelType;
1402 updateWidgets();
1403}
1404
1405
1406template <typename T>
1407void DIALOG_SIM_MODEL<T>::onPageChanging( wxBookCtrlEvent& event )
1408{
1409 updateModelCodeTab( &curModel() );
1410}
1411
1412
1413template <typename T>
1415{
1416 int symbolPinIndex = aEvent.GetRow();
1417 wxString oldModelPinName = aEvent.GetString();
1418 wxString modelPinName = m_pinAssignmentsGrid->GetCellValue( aEvent.GetRow(), aEvent.GetCol() );
1419
1420 int oldModelPinIndex = getModelPinIndex( oldModelPinName );
1421 int modelPinIndex = getModelPinIndex( modelPinName );
1422
1423 if( oldModelPinIndex != SIM_MODEL_PIN::NOT_CONNECTED )
1424 curModel().AssignSymbolPinNumberToModelPin( oldModelPinIndex, "" );
1425
1426 if( modelPinIndex != SIM_MODEL_PIN::NOT_CONNECTED )
1427 {
1428 SCH_PIN* symbolPin = m_sortedPartPins.at( symbolPinIndex );
1429
1430 curModel().AssignSymbolPinNumberToModelPin( modelPinIndex, symbolPin->GetShownNumber() );
1431 }
1432
1433 updatePinAssignments( &curModel(), FORCE_UPDATE_PINS );
1434
1435 aEvent.Skip();
1436}
1437
1438
1439template <typename T>
1441{
1442 wxGridUpdateLocker deferRepaintsTillLeavingScope( m_pinAssignmentsGrid );
1443
1444 int gridWidth = KIPLATFORM::UI::GetUnobscuredSize( m_pinAssignmentsGrid ).x;
1445 m_pinAssignmentsGrid->SetColSize( PIN_COLUMN::MODEL, gridWidth / 2 );
1446 m_pinAssignmentsGrid->SetColSize( PIN_COLUMN::SYMBOL, gridWidth / 2 );
1447
1448 aEvent.Skip();
1449}
1450
1451
1452template <typename T>
1454{
1455 // By default, when a property grid is focused, the textbox is not immediately focused until
1456 // Tab key is pressed. This is inconvenient, so we fix that here.
1457
1458 wxPropertyGrid* grid = m_paramGrid->GetGrid();
1459 wxPGProperty* selected = grid->GetSelection();
1460
1461 if( !selected )
1462 selected = grid->wxPropertyGridInterface::GetFirst();
1463
1464#if wxCHECK_VERSION( 3, 3, 0 )
1465 if( selected )
1466 grid->DoSelectProperty( selected, wxPGSelectPropertyFlags::Focus );
1467#else
1468 if( selected )
1469 grid->DoSelectProperty( selected, wxPG_SEL_FOCUS );
1470#endif
1471
1472 aEvent.Skip();
1473}
1474
1475
1476template <typename T>
1477void DIALOG_SIM_MODEL<T>::onParamGridSelectionChange( wxPropertyGridEvent& aEvent )
1478{
1479 wxPropertyGrid* grid = m_paramGrid->GetGrid();
1480
1481 // Jump over categories.
1482 if( grid->GetSelection() && grid->GetSelection()->IsCategory() )
1483 {
1484 wxPGProperty* selection = grid->GetSelection();
1485
1486 // If the new selection is immediately above the previous selection, we jump up. Otherwise
1487 // we jump down. We do this by simulating up or down arrow keys.
1488
1489 wxPropertyGridIterator it = grid->GetIterator( wxPG_ITERATE_VISIBLE, selection );
1490 it.Next();
1491
1492 wxKeyEvent* keyEvent = new wxKeyEvent( wxEVT_KEY_DOWN );
1493
1494 if( *it == m_prevParamGridSelection )
1495 {
1496 if( !selection->IsExpanded() )
1497 {
1498 grid->Expand( selection );
1499 keyEvent->m_keyCode = WXK_DOWN;
1500 wxQueueEvent( grid, keyEvent );
1501
1502 // Does not work for some reason.
1503 /*m_paramGrid->DoSelectProperty( selection->Item( selection->GetChildCount() - 1 ),
1504 wxPG_SEL_FOCUS );*/
1505 }
1506 else
1507 {
1508 keyEvent->m_keyCode = WXK_UP;
1509 wxQueueEvent( grid, keyEvent );
1510 }
1511 }
1512 else
1513 {
1514 if( !selection->IsExpanded() )
1515 grid->Expand( selection );
1516
1517 keyEvent->m_keyCode = WXK_DOWN;
1518 wxQueueEvent( grid, keyEvent );
1519 }
1520
1521 m_prevParamGridSelection = grid->GetSelection();
1522 return;
1523 }
1524
1525 wxWindow* editorControl = grid->GetEditorControl();
1526
1527 if( !editorControl )
1528 {
1529 m_prevParamGridSelection = grid->GetSelection();
1530 return;
1531 }
1532
1533 // Without this the user had to press tab before they could edit the field.
1534 editorControl->SetFocus();
1535 m_prevParamGridSelection = grid->GetSelection();
1536}
1537
1538
1539template <typename T>
1540void DIALOG_SIM_MODEL<T>::onUpdateUI( wxUpdateUIEvent& aEvent )
1541{
1542 // This is currently patched in wxPropertyGrid::ScrollWindow() in the Mac wxWidgets fork.
1543 // However, we may need this version if it turns out to be an issue on other platforms and
1544 // we can't get it upstreamed.
1545#if 0
1546 // It's a shame to do this on the UpdateUI event, but neither the wxPropertyGridManager,
1547 // wxPropertyGridPage, wxPropertyGrid, nor the wxPropertyGrid's GetCanvas() window appear
1548 // to get scroll events.
1549
1550 wxPropertyGrid* grid = m_paramGrid->GetGrid();
1551 wxTextCtrl* ctrl = grid->GetEditorTextCtrl();
1552
1553 if( ctrl )
1554 {
1555 wxRect ctrlRect = ctrl->GetScreenRect();
1556 wxRect gridRect = grid->GetScreenRect();
1557
1558 if( ctrlRect.GetTop() < gridRect.GetTop() || ctrlRect.GetBottom() > gridRect.GetBottom() )
1559 grid->ClearSelection();
1560 }
1561#endif
1562}
1563
1564
1565template <typename T>
1566void DIALOG_SIM_MODEL<T>::adjustParamGridColumns( int aWidth, bool aForce )
1567{
1568 wxPropertyGrid* grid = m_paramGridMgr->GetGrid();
1569 int margin = 15;
1570 int indent = 20;
1571
1572 if( aWidth != m_lastParamGridWidth || aForce )
1573 {
1574 m_lastParamGridWidth = aWidth;
1575
1576 grid->FitColumns();
1577
1578 std::vector<int> colWidths;
1579
1580 for( size_t ii = 0; ii < grid->GetColumnCount(); ii++ )
1581 {
1582 if( ii == PARAM_COLUMN::DESCRIPTION )
1583 colWidths.push_back( grid->GetState()->GetColumnWidth( ii ) + margin + indent );
1584 else if( ii == PARAM_COLUMN::VALUE )
1585 colWidths.push_back( std::max( 72, grid->GetState()->GetColumnWidth( ii ) ) + margin );
1586 else
1587 colWidths.push_back( 60 + margin );
1588
1589 aWidth -= colWidths[ ii ];
1590 }
1591
1592 for( size_t ii = 0; ii < grid->GetColumnCount(); ii++ )
1593 grid->SetColumnProportion( ii, colWidths[ ii ] );
1594
1595 grid->ResetColumnSizes();
1596 grid->RefreshEditor();
1597 }
1598}
1599
1600
1601template <typename T>
1603{
1604 adjustParamGridColumns( event.GetSize().GetX(), false );
1605
1606 event.Skip();
1607}
1608
1609
1610
1611template class DIALOG_SIM_MODEL<SCH_SYMBOL>;
1612template class DIALOG_SIM_MODEL<LIB_SYMBOL>;
const char * name
Definition: DXF_plotter.cpp:57
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap)
Definition: bitmap.cpp:110
@ INVALID_BITMAP
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
Class DIALOG_SIM_MODEL_BASE.
wxPropertyGridManager * m_paramGridMgr
STD_BITMAP_BUTTON * m_browseButton
wxStyledTextCtrl * m_subckt
wxPropertyGridPage * m_paramGrid
wxStyledTextCtrl * m_codePreview
void onTypeChoice(wxCommandEvent &aEvent) override
void onLibraryPathTextKillFocus(wxFocusEvent &aEvent) override
wxString getSymbolPinString(int aSymbolPinNumber) const
void updateBuiltinModelWidgets(SIM_MODEL *aModel)
void onPinAssignmentsGridCellChange(wxGridEvent &aEvent) override
int findSymbolPinRow(const wxString &aSymbolPinNumber) const
void onModelNameChoice(wxCommandEvent &aEvent) override
void onRadioButton(wxCommandEvent &aEvent) override
SCINTILLA_TRICKS * m_scintillaTricksSubckt
bool loadLibrary(const wxString &aLibraryPath, REPORTER &aReporter, bool aForceReload=false)
int getModelPinIndex(const wxString &aModelPinString) const
void onLibraryPathText(wxCommandEvent &aEvent) override
void onDifferentialCheckbox(wxCommandEvent &event) override
void onParamGridSelectionChange(wxPropertyGridEvent &aEvent)
void removeOrphanedPinAssignments(SIM_MODEL *aModel)
void adjustParamGridColumns(int aWidth, bool aForce)
std::vector< SCH_PIN * > m_sortedPartPins
wxPGProperty * newParamProperty(SIM_MODEL *aModel, int aParamIndex) const
void onPinModelCombobox(wxCommandEvent &event) override
const SIM_LIBRARY * library() const
SIM_MODEL & curModel() const
void onBrowseButtonClick(wxCommandEvent &aEvent) override
void onPinComboboxTextEnter(wxCommandEvent &event) override
void onPinCombobox(wxCommandEvent &event) override
SCINTILLA_TRICKS * m_scintillaTricksCode
void updatePinAssignments(SIM_MODEL *aModel, bool aForceUpdatePins)
void onUpdateUI(wxUpdateUIEvent &aEvent)
DIALOG_SIM_MODEL(wxWindow *aParent, EDA_BASE_FRAME *aFrame, T &aSymbol, std::vector< SCH_FIELD > &aFields)
wxString getModelPinString(SIM_MODEL *aModel, int aModelPinIndex) const
void onPinAssignmentsGridSize(wxSizeEvent &aEvent) override
void updateIbisWidgets(SIM_MODEL *aModel)
void onParamGridSetFocus(wxFocusEvent &aEvent)
void updateModelParamsTab(SIM_MODEL *aModel)
bool TransferDataFromWindow() override
void updateModelCodeTab(SIM_MODEL *aModel)
void onSizeParamGrid(wxSizeEvent &event) override
void onPageChanging(wxNotebookEvent &event) override
void onWaveformChoice(wxCommandEvent &aEvent) override
void onPinModelComboboxTextEnter(wxCommandEvent &event) override
void onLibraryPathTextEnter(wxCommandEvent &aEvent) override
void onDeviceTypeChoice(wxCommandEvent &aEvent) override
bool TransferDataToWindow() override
void addParamPropertyIfRelevant(SIM_MODEL *aModel, int aParamIndex)
The base frame for deriving all KiCad main window classes.
Add mouse and command handling (such as cut, copy, and paste) to a WX_GRID instance.
Definition: grid_tricks.h:61
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:77
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:30
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: locale_io.h:49
virtual bool ReadSchematicAndLibraries(unsigned aNetlistOptions, REPORTER &aReporter)
Process the schematic and Spice libraries to create net mapping and a list of SPICE_ITEMs.
const std::list< SPICE_ITEM > & GetItems() const
Return the list of items representing schematic symbols in the Spice world.
A singleton reporter that reports to nowhere.
Definition: reporter.h:223
virtual const wxString GetProjectPath() const
Return the full path of the project.
Definition: project.cpp:135
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:71
virtual bool HasMessage() const =0
Returns true if the reporter client is non-empty.
Schematic editor (Eeschema) main window.
Instances are attached to a symbol or sheet and provide a place for the symbol's value,...
Definition: sch_field.h:51
wxString GetShownNumber() const
Definition: sch_pin.cpp:458
Add cut/copy/paste, dark theme, autocomplete and brace highlighting to a wxStyleTextCtrl instance.
static constexpr auto MODEL_FIELD
static constexpr auto PIN_FIELD
static constexpr auto DIFF_FIELD
bool isPinDiff(const std::string &aComp, const std::string &aPinNumber) const
static constexpr auto LIBRARY_FIELD
Definition: sim_library.h:35
static constexpr auto NAME_FIELD
Definition: sim_library.h:36
std::vector< std::pair< std::string, std::string > > GetIbisPins() const
void SwitchSingleEndedDiff(bool aDiff) override
bool ChangePin(const SIM_LIBRARY_IBIS &aLib, std::string aPinNumber)
update the list of available models based on the pin number.
bool CanDifferential() const
std::vector< std::string > GetIbisModels() const
std::string GetComponentName() const
std::string GetSpiceCode() const
static TYPE ReadTypeFromFields(const std::vector< SCH_FIELD > &aFields, REPORTER &aReporter)
Definition: sim_model.cpp:386
static INFO TypeInfo(TYPE aType)
Definition: sim_model.cpp:101
static void SetFieldValue(std::vector< SCH_FIELD > &aFields, const wxString &aFieldName, const std::string &aValue)
Definition: sim_model.cpp:664
int GetPinCount() const
Definition: sim_model.h:471
void ReadDataFields(const std::vector< SCH_FIELD > *aFields, const std::vector< SCH_PIN * > &aPins)
Definition: sim_model.cpp:423
const SPICE_GENERATOR & SpiceGenerator() const
Definition: sim_model.h:435
virtual const PARAM & GetParam(unsigned aParamIndex) const
Definition: sim_model.cpp:780
static bool InferSimModel(T &aSymbol, std::vector< SCH_FIELD > *aFields, bool aResolve, SIM_VALUE_GRAMMAR::NOTATION aNotation, wxString *aDeviceType, wxString *aModelType, wxString *aModelParams, wxString *aPinMap)
Definition: sim_model.cpp:1064
static std::string GetFieldValue(const std::vector< SCH_FIELD > *aFields, const wxString &aFieldName, bool aResolve=true)
Definition: sim_model.cpp:645
int GetParamCount() const
Definition: sim_model.h:481
void AssignSymbolPinNumberToModelPin(int aPinIndex, const wxString &aSymbolPinNumber)
Definition: sim_model.cpp:749
DEVICE_INFO GetDeviceInfo() const
Definition: sim_model.h:460
DEVICE_T GetDeviceType() const
Definition: sim_model.h:463
static DEVICE_INFO DeviceInfo(DEVICE_T aDeviceType)
Definition: sim_model.cpp:56
virtual bool HasAutofill() const
Definition: sim_model.h:500
void SetParamValue(int aParamIndex, const std::string &aValue, SIM_VALUE::NOTATION aNotation=SIM_VALUE::NOTATION::SI)
Definition: sim_model.cpp:849
const SIM_MODEL_PIN & GetPin(unsigned aIndex) const
Definition: sim_model.h:472
void SetIsStoredInValue(bool aIsStoredInValue)
Definition: sim_model.h:506
virtual bool HasPrimaryValue() const
Definition: sim_model.h:501
TYPE GetType() const
Definition: sim_model.h:464
const SIM_MODEL::PARAM & GetParam() const
Definition: sim_property.h:60
@ TYPE_BOOL
Definition: sim_value.h:67
@ TYPE_FLOAT_VECTOR
Definition: sim_value.h:75
@ TYPE_BOOL_VECTOR
Definition: sim_value.h:73
@ TYPE_INT
Definition: sim_value.h:68
@ TYPE_FLOAT
Definition: sim_value.h:69
@ TYPE_INT_VECTOR
Definition: sim_value.h:74
@ TYPE_COMPLEX_VECTOR
Definition: sim_value.h:76
@ TYPE_STRING
Definition: sim_value.h:71
@ TYPE_COMPLEX
Definition: sim_value.h:70
Special netlist exporter flavor that allows one to override simulation commands.
virtual std::string Preview(const SPICE_ITEM &aItem) const
void SetBitmap(const wxBitmapBundle &aBmp)
void ClearRows()
wxWidgets recently added an ASSERT which fires if the position is greater than or equal to the number...
Definition: wx_grid.h:165
A wrapper for reporting to a wxString object.
Definition: reporter.h:164
bool HasMessage() const override
Returns true if the reporter client is non-empty.
Definition: reporter.cpp:80
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:195
This file is part of the common library.
static bool empty(const wxTextEntryBase *aCtrl)
bool equivalent(SIM_MODEL::DEVICE_T a, SIM_MODEL::DEVICE_T b)
#define FORCE_UPDATE_PINS
#define _(s)
PROJECT & Prj()
Definition: kicad.cpp:595
wxSize GetUnobscuredSize(const wxWindow *aWindow)
Tries to determine the size of the viewport of a scrollable widget (wxDataViewCtrl,...
Definition: wxgtk/ui.cpp:195
KICOMMON_API wxFont GetInfoFont(wxWindow *aWindow)
Definition: ui_common.cpp:154
BITMAPS PinShapeGetBitmap(GRAPHIC_PINSHAPE aShape)
Definition: pin_type.cpp:246
@ NONE
No connection to this item.
SIM_MODEL::TYPE TYPE
Definition: sim_model.cpp:53
#define SIM_PINS_FIELD
Definition: sim_model.h:54
#define SIM_DEVICE_FIELD
Definition: sim_model.h:52
#define SIM_REFERENCE_FIELD
Definition: sim_model.h:49
#define SIM_PARAMS_FIELD
Definition: sim_model.h:55
#define SIM_DEVICE_SUBTYPE_FIELD
Definition: sim_model.h:53
std::vector< FAB_LAYER_COLOR > dummy
int StrNumCmp(const wxString &aString1, const wxString &aString2, bool aIgnoreCase)
Compare two strings with alphanumerical content.
std::vector< std::string > enumValues
Definition: sim_model.h:388
SIM_VALUE::TYPE type
Definition: sim_model.h:379
std::string defaultValue
Definition: sim_model.h:382
std::string description
Definition: sim_model.h:383
std::string value
Definition: sim_model.h:400
const INFO & info
Definition: sim_model.h:401
static constexpr auto NOT_CONNECTED
Definition: sim_model.h:73
const std::string modelPinName
Definition: sim_model.h:70
wxString symbolPinNumber
Definition: sim_model.h:71
std::string modelName
@ VALUE_FIELD
Field Value of part, i.e. "3.3K".
@ REFERENCE_FIELD
Field Reference of part, i.e. "IC21".