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 The 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, see <https://www.gnu.org/licenses/>.
20 */
21
22#include <bitmaps.h>
24#include <dialog_sim_model.h>
25#include <sim/sim_property.h>
27#include <sim/sim_model.h>
29#include <sim/sim_model_ibis.h>
33#include <grid_tricks.h>
36#include <kiplatform/ui.h>
37#include <confirm.h>
38#include <string_utils.h>
39
40#include <wx/filedlg.h>
41#include <fmt/format.h>
42#include <sch_edit_frame.h>
43#include <widgets/wx_infobar.h>
47#include <wx/filedlg.h>
48#include <wx/log.h>
49
51
52#define FORCE_REFRESH_FROM_MODEL true
53
54
55bool equivalent( SIM_MODEL::DEVICE_T a, SIM_MODEL::DEVICE_T b )
56{
57 // A helper to handle SPICE's use of 'E' and 'H' for voltage sources and 'F' and 'G' for
58 // current sources
59 return a == b
60 || SIM_MODEL::DeviceInfo( a ).description == SIM_MODEL::DeviceInfo( b ).description;
61};
62
63
64template <typename T>
65DIALOG_SIM_MODEL<T>::DIALOG_SIM_MODEL( wxWindow* aParent, EDA_BASE_FRAME* aFrame, T& aSymbol,
66 std::vector<SCH_FIELD>& aFields ) :
67 DIALOG_SIM_MODEL_BASE( aParent ),
68 m_frame( aFrame ),
69 m_symbol( aSymbol ),
70 m_fields( aFields ),
74 m_prevModel( nullptr ),
76 m_scintillaTricksCode( nullptr ),
77 m_scintillaTricksSubckt( nullptr ),
78 m_firstCategory( nullptr ),
79 m_prevParamGridSelection( nullptr ),
81{
83 m_infoBar->AddCloseButton();
84
85 if constexpr (std::is_same_v<T, SCH_SYMBOL>)
86 {
87 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( &aSymbol );
88 m_filesStack.push_back( symbol->Schematic()->GetEmbeddedFiles() );
89
90 if( symbol->GetLibSymbolRef() )
92 }
93 else if constexpr (std::is_same_v<T, LIB_SYMBOL>)
94 {
95 LIB_SYMBOL* symbol = static_cast<LIB_SYMBOL*>( &aSymbol );
97 }
98
99 if( EMBEDDED_FILES* symbolEmbeddedFiles = aSymbol.GetEmbeddedFiles() )
100 m_filesStack.push_back( symbolEmbeddedFiles );
101
102 m_libraryModelsMgr.SetFilesStack( m_filesStack );
103 m_builtinModelsMgr.SetFilesStack( m_filesStack );
104
105 for( SCH_PIN* pin : aSymbol.GetGraphicalPins( ALL_UNITS, ALL_BODY_STYLES ) )
106 {
107 // Body styles (including De Morgan variants) are equivalences, not additional items to simulate
108 if( !pin->GetParentSymbol()->IsMultiBodyStyle() || pin->GetBodyStyle() < 2 )
109 m_sortedPartPins.push_back( pin );
110 }
111
112 std::sort( m_sortedPartPins.begin(), m_sortedPartPins.end(),
113 []( const SCH_PIN* lhs, const SCH_PIN* rhs )
114 {
115 // We sort by StrNumCmp because SIM_MODEL_BASE sorts with it too.
116 return StrNumCmp( lhs->GetNumber(), rhs->GetNumber(), CMP_T::IGNORE_CASE ) < 0;
117 } );
118
119 m_waveformChoice->Clear();
120 m_deviceChoice->Clear();
121 m_deviceSubtypeChoice->Clear();
122
123 m_scintillaTricksCode = new SCINTILLA_TRICKS( m_codePreview, wxT( "{}" ), false );
124 m_scintillaTricksSubckt = new SCINTILLA_TRICKS( m_subckt, wxT( "()" ), false );
125
126 m_paramGridMgr->Bind( wxEVT_PG_SELECTED, &DIALOG_SIM_MODEL::onParamGridSelectionChange, this );
127
128 wxPropertyGrid* grid = m_paramGrid->GetGrid();
129 grid->SetCellDisabledTextColour( wxSystemSettings::GetColour( wxSYS_COLOUR_GRAYTEXT ) );
130 grid->Bind( wxEVT_SET_FOCUS, &DIALOG_SIM_MODEL::onParamGridSetFocus, this );
131 grid->Bind( wxEVT_UPDATE_UI, &DIALOG_SIM_MODEL::onUpdateUI, this );
132
133 grid->DedicateKey( WXK_RETURN );
134 grid->DedicateKey( WXK_NUMPAD_ENTER );
135 grid->DedicateKey( WXK_UP );
136 grid->DedicateKey( WXK_DOWN );
137
138#if wxCHECK_VERSION( 3, 3, 0 )
139 grid->AddActionTrigger( wxPGKeyboardAction::Edit, WXK_RETURN );
140 grid->AddActionTrigger( wxPGKeyboardAction::NextProperty, WXK_RETURN );
141 grid->AddActionTrigger( wxPGKeyboardAction::Edit, WXK_NUMPAD_ENTER );
142 grid->AddActionTrigger( wxPGKeyboardAction::NextProperty, WXK_NUMPAD_ENTER );
143#else
144 grid->AddActionTrigger( wxPG_ACTION_EDIT, WXK_RETURN );
145 grid->AddActionTrigger( wxPG_ACTION_NEXT_PROPERTY, WXK_RETURN );
146 grid->AddActionTrigger( wxPG_ACTION_EDIT, WXK_NUMPAD_ENTER );
147 grid->AddActionTrigger( wxPG_ACTION_NEXT_PROPERTY, WXK_NUMPAD_ENTER );
148#endif
149
150 m_pinAssignmentsGrid->ClearRows();
151 m_pinAssignmentsGrid->PushEventHandler( new GRID_TRICKS( m_pinAssignmentsGrid ) );
152
155}
156
157
158template <typename T>
160{
161 // Disable all properties. This is necessary because some of their methods are called after
162 // destruction of DIALOG_SIM_MODEL, oddly. When disabled, they never access their models.
163 for( wxPropertyGridIterator it = m_paramGrid->GetIterator(); !it.AtEnd(); ++it )
164 {
165 if( SIM_PROPERTY* prop = dynamic_cast<SIM_PROPERTY*>( *it ) )
166 prop->Disable();
167 }
168
169 // Delete the GRID_TRICKS.
170 m_pinAssignmentsGrid->PopEventHandler( true );
171
174}
175
176
177template <typename T>
179{
180 wxCommandEvent dummyEvent;
181 wxString deviceType;
182 wxString modelType;
183 wxString modelParams;
184 wxString pinMap;
185 bool storeInValue = false;
186
188
189 // Infer RLC and VI models if they aren't specified
191 &deviceType, &modelType, &modelParams, &pinMap ) )
192 {
193 SetFieldValue( m_fields, SIM_DEVICE_FIELD, deviceType.ToStdString() );
194
195 if( !modelType.IsEmpty() )
196 SetFieldValue( m_fields, SIM_DEVICE_SUBTYPE_FIELD, modelType.ToStdString() );
197
198 SetFieldValue( m_fields, SIM_PARAMS_FIELD, modelParams.ToStdString() );
199
200 SetFieldValue( m_fields, SIM_PINS_FIELD, pinMap.ToStdString() );
201
202 storeInValue = true;
203
204 // WriteFields() only rewrites Value when storeInValue stays on, so remember the original
205 // text first to restore it verbatim if the user turns storeInValue off.
206 SCH_FIELD* valueField = FindField( m_fields, FIELD_T::VALUE );
207
208 m_inferredValueRestore = valueField->GetText();
210 valueField->SetText( wxT( "${SIM.PARAMS}" ) );
211 }
212
213 wxString libraryFilename = GetFieldValue( &m_fields, SIM_LIBRARY::LIBRARY_FIELD, true, 0 );
214 wxFileName tmp( libraryFilename );
215
216 if( !tmp.GetFullName().IsEmpty() )
217 {
218 // The model is sourced from a library, optionally with instance overrides.
219 m_rbLibraryModel->SetValue( true );
220
221 if( !loadLibrary( libraryFilename, reporter ) )
222 {
223 if( reporter.HasMessage() )
224 m_infoBar->ShowMessage( reporter.GetMessages() );
225
226 m_libraryPathText->ChangeValue( libraryFilename );
228
229 m_libraryModelsMgr.CreateModel( nullptr, SIM_MODEL::PinNumbers( m_sortedPartPins ), m_fields, true, 0,
230 reporter );
231
232 m_modelListBox->Clear();
233 m_modelListBox->Append( _( "<unknown>" ) );
234 m_modelListBox->SetSelection( 0 );
235 }
236 else
237 {
238 std::string modelName = GetFieldValue( &m_fields, SIM_LIBRARY::NAME_FIELD, true, 0 );
239 int modelIdx = m_modelListBox->FindString( modelName );
240
241 if( modelIdx == wxNOT_FOUND )
242 {
243 m_infoBar->ShowMessage( wxString::Format( _( "No model named '%s' in library." ), modelName ) );
244
245 // Default to first item in library if any exist
246 if( m_modelListBox->GetCount() > 0 )
247 m_modelListBox->SetSelection( 0 );
248 }
249 else
250 {
251 m_infoBar->Hide();
252 m_modelListBox->SetSelection( modelIdx );
253 }
254
255 m_curModelType = curModel().GetType();
256 }
257
258 if( isIbisLoaded() && ( m_modelListBox->GetSelection() >= 0 ) )
259 {
260 int idx = 0;
261 wxString sel = m_modelListBox->GetStringSelection();
262
263 if( m_modelListBoxEntryToLibraryIdx.contains( sel ) )
264 idx = m_modelListBoxEntryToLibraryIdx.at( sel );
265
266 SIM_MODEL_IBIS* ibismodel = dynamic_cast<SIM_MODEL_IBIS*>( &m_libraryModelsMgr.GetModels()[idx].get() );
267
268 if( ibismodel )
269 {
270 onModelNameChoice( dummyEvent ); // refresh list of pins
271
272 int i = 0;
273
274 for( const std::pair<std::string, std::string>& strs : ibismodel->GetIbisPins() )
275 {
276 if( strs.first == GetFieldValue( &m_fields, SIM_LIBRARY_IBIS::PIN_FIELD, true, 0 ) )
277 {
278 auto ibisLibrary = static_cast<const SIM_LIBRARY_IBIS*>( library() );
279
280 ibismodel->ChangePin( *ibisLibrary, strs.first );
281 m_pinCombobox->SetSelection( static_cast<int>( i ) );
282 break;
283 }
284 i++;
285 }
286
287 if( i < static_cast<int>( ibismodel->GetIbisPins().size() ) )
288 {
289 onPinCombobox( dummyEvent ); // refresh list of models
290
291 wxString ibisModel = GetFieldValue( &m_fields, SIM_LIBRARY_IBIS::MODEL_FIELD, true, 0 );
292 m_pinModelCombobox->SetStringSelection( ibisModel );
293 }
294
295 if( GetFieldValue( &m_fields, SIM_LIBRARY_IBIS::DIFF_FIELD, true, 0 ) == "1" )
296 {
297 ibismodel->SwitchSingleEndedDiff( true );
298 m_differentialCheckbox->SetValue( true );
299 }
300 else
301 {
302 ibismodel->SwitchSingleEndedDiff( false );
303 m_differentialCheckbox->SetValue( false );
304 }
305
306 // SetIbisModel derives the IO mode from KIBIS_MODEL::m_type
307 // (Series / Series_switch override the differential layout).
308 auto ibisLibrary = static_cast<const SIM_LIBRARY_IBIS*>( library() );
309 std::string pinNum = GetFieldValue( &m_fields, SIM_LIBRARY_IBIS::PIN_FIELD,
310 true, 0 );
312 true, 0 );
313
314 if( ibisLibrary && !pinNum.empty() && !modelNm.empty() )
315 ibismodel->SetIbisModel( *ibisLibrary, pinNum, modelNm );
316 }
317 }
318 }
319 else if( !GetFieldValue( &m_fields, SIM_DEVICE_FIELD, false, 0 ).empty()
321 {
322 // The model is sourced from the instance.
323 m_rbBuiltinModel->SetValue( true );
324
325 reporter.Clear();
327
328 if( reporter.HasMessage() )
329 DisplayErrorMessage( this, reporter.GetMessages() );
330 }
331
332 for( SIM_MODEL::TYPE type : SIM_MODEL::TYPE_ITERATOR() )
333 {
334 if( m_rbBuiltinModel->GetValue() && type == m_curModelType )
335 {
336 reporter.Clear();
338
339 if( reporter.HasMessage() )
340 {
341 DisplayErrorMessage( this, _( "Failed to read simulation model from fields." )
342 + wxT( "\n\n" ) + reporter.GetMessages() );
343 }
344 }
345 else
346 {
348 }
349
350 SIM_MODEL::DEVICE_T deviceTypeT = SIM_MODEL::TypeInfo( type ).deviceType;
351
352 if( !m_curModelTypeOfDeviceType.count( deviceTypeT ) )
353 m_curModelTypeOfDeviceType[deviceTypeT] = type;
354 }
355
356 if( storeInValue )
357 curModel().SetIsStoredInValue( true );
358
359 m_saveInValueCheckbox->SetValue( curModel().IsStoredInValue() );
360
362
363 onRadioButton( dummyEvent );
364 return DIALOG_SIM_MODEL_BASE::TransferDataToWindow();
365}
366
367
368template <typename T>
370{
371 if( !m_pinAssignmentsGrid->CommitPendingChanges() )
372 return false;
373
374 if( !m_paramGrid->GetGrid()->CommitChangesFromEditor() )
375 return false;
376
377 if( !DIALOG_SIM_MODEL_BASE::TransferDataFromWindow() )
378 return false;
379
381 std::string path;
382 std::string name;
383
384 if( m_rbLibraryModel->GetValue() )
385 {
386 path = m_libraryPathText->GetValue();
387 wxFileName fn( path );
388
389 if( !path.starts_with( FILEEXT::KiCadUriPrefix ) && fn.MakeRelativeTo( Prj().GetProjectPath() )
390 && !fn.GetFullPath().StartsWith( ".." ) )
391 {
392 path = fn.GetFullPath();
393 }
394
395 if( m_modelListBox->GetSelection() >= 0 )
396 name = m_modelListBox->GetStringSelection().ToStdString();
397 else if( dynamic_cast<SIM_MODEL_SPICE_FALLBACK*>( &model ) )
399 }
400
403
404 if( isIbisLoaded() && !m_libraryModelsMgr.GetModels().empty() )
405 {
406 int idx = 0;
407 wxString sel = m_modelListBox->GetStringSelection();
408
409 if( m_modelListBoxEntryToLibraryIdx.contains( sel ) )
410 idx = m_modelListBoxEntryToLibraryIdx.at( sel );
411
412 auto* ibismodel =
413 static_cast<SIM_MODEL_IBIS*>( &m_libraryModelsMgr.GetModels().at( idx ).get() );
414
415 if( ibismodel )
416 {
417 std::string pins;
418 std::string modelName = std::string( m_pinModelCombobox->GetValue().c_str() );
419 std::string differential;
420
421 if( m_pinCombobox->GetSelection() >= 0 )
422 pins = ibismodel->GetIbisPins().at( m_pinCombobox->GetSelection() ).first;
423
424 if( ibismodel->CanDifferential() && m_differentialCheckbox->GetValue() )
425 differential = "1";
426
430 }
431 }
432
433 if( model.GetType() == SIM_MODEL::TYPE::RAWSPICE )
434 {
435 if( m_modelNotebook->GetSelection() == 0 )
437
438 wxString code = m_codePreview->GetText().Trim( true ).Trim( false );
439 model.SetParamValue( "model", std::string( code.ToUTF8() ) );
440 }
441
442 model.SetIsStoredInValue( m_saveInValueCheckbox->GetValue() );
443
444 for( int row = 0; row < m_pinAssignmentsGrid->GetNumberRows(); ++row )
445 {
446 wxString modelPinName = m_pinAssignmentsGrid->GetCellValue( row, PIN_COLUMN::MODEL );
447 wxString symbolPinName = m_sortedPartPins.at( row )->GetShownNumber();
448
449 model.AssignSymbolPinNumberToModelPin( getModelPinIndex( modelPinName ),
450 std::string( symbolPinName.ToUTF8() ) );
451 }
452
454
455 curModel().WriteFields( m_fields );
456
458 model.IsStoredInValue() );
459
460 // Decomposition is a component-level choice and is independent of the model fields written
461 // above. Only multi-unit symbols expose it; write it after WriteFields() so it is preserved.
462 if( m_symbol.GetUnitCount() > 1 )
463 {
464 SIM_DECOMPOSITION decomposition;
465
466 if( m_decompositionChoice->GetSelection() == 1 )
467 {
469
470 wxStringTokenizer sharedTokens( m_sharedPinsText->GetValue(), wxS( ", \t" ),
471 wxTOKEN_STRTOK );
472
473 while( sharedTokens.HasMoreTokens() )
474 decomposition.sharedModelPins.push_back( sharedTokens.GetNextToken() );
475 }
476
477 // Whole-device formats to an empty string, which clears any previous repeat selection.
478 SetFieldValue( m_fields, SIM_DECOMPOSITION_FIELD, decomposition.Format().ToStdString(),
479 false );
480 }
481
482 return true;
483}
484
485
486template <typename T>
487void DIALOG_SIM_MODEL<T>::RestoreInferredValue( std::vector<SCH_FIELD>& aFields,
488 const wxString& aOriginalValue, bool aOverwritten,
489 bool aStoredInValue )
490{
491 if( !aOverwritten || aStoredInValue )
492 return;
493
494 if( SCH_FIELD* valueField = FindField( aFields, FIELD_T::VALUE ) )
495 valueField->SetText( aOriginalValue );
496}
497
498
499template <typename T>
501{
502 bool multiUnit = m_symbol.GetUnitCount() > 1;
503
504 m_decompositionLabel->Show( multiUnit );
505 m_decompositionChoice->Show( multiUnit );
506 m_sharedPinsLabel->Show( multiUnit );
507 m_sharedPinsText->Show( multiUnit );
508
509 if( multiUnit )
510 {
513
514 bool repeat = decomposition.mode == SIM_DECOMPOSITION::MODE::REPEAT_PER_UNIT;
515
516 m_decompositionChoice->SetSelection( repeat ? 1 : 0 );
517
518 wxString shared;
519
520 for( const wxString& pin : decomposition.sharedModelPins )
521 {
522 if( !shared.IsEmpty() )
523 shared += wxS( ", " );
524
525 shared += pin;
526 }
527
528 m_sharedPinsText->SetValue( shared );
529 m_sharedPinsLabel->Enable( repeat );
530 m_sharedPinsText->Enable( repeat );
531 }
532
533 m_pinAssignmentsPanel->Layout();
534}
535
536
537template <typename T>
539{
540 bool repeat = m_decompositionChoice->GetSelection() == 1;
541
542 m_sharedPinsLabel->Enable( repeat );
543 m_sharedPinsText->Enable( repeat );
544
545 aEvent.Skip();
546}
547
548
549template <typename T>
551{
552 // always enable the library browser button -- it makes for fewer clicks if the user has a
553 // whole bunch of inferred passives that they want to specify library models for
554 m_browseButton->Enable();
555
556 // if we're in an undetermined state then enable everything for faster access
557 bool undetermined = !m_rbLibraryModel->GetValue() && !m_rbBuiltinModel->GetValue();
558 bool enableLibCtrls = m_rbLibraryModel->GetValue() || undetermined;
559 bool enableBuiltinCtrls = m_rbBuiltinModel->GetValue() || undetermined;
560
561 m_pathLabel->Enable( enableLibCtrls );
562 m_libraryPathText->Enable( enableLibCtrls );
563 m_modelNameLabel->Enable( enableLibCtrls );
564 m_modelFilter->Enable( enableLibCtrls && !isIbisLoaded() );
565 m_modelListBox->Enable( enableLibCtrls );
566 m_pinLabel->Enable( enableLibCtrls );
567 m_pinCombobox->Enable( enableLibCtrls );
568 m_differentialCheckbox->Enable( enableLibCtrls );
569 m_pinModelLabel->Enable( enableLibCtrls );
570 m_pinModelCombobox->Enable( enableLibCtrls );
571 m_waveformLabel->Enable( enableLibCtrls );
572 m_waveformChoice->Enable( enableLibCtrls );
573
574 m_deviceLabel->Enable( enableBuiltinCtrls );
575 m_deviceChoice->Enable( enableBuiltinCtrls );
576 m_deviceSubtypeLabel->Enable( enableBuiltinCtrls );
577 m_deviceSubtypeChoice->Enable( enableBuiltinCtrls );
578
580
585 updatePinAssignments( model, false );
586
587 std::string ref = GetFieldValue( &m_fields, SIM_REFERENCE_FIELD, false, 0 );
588
589 m_modelPanel->Layout();
590 m_pinAssignmentsPanel->Layout();
591 m_parametersPanel->Layout();
592 m_codePanel->Layout();
593
594 SendSizeEvent( wxSEND_EVENT_POST );
595
597}
598
599
600template <typename T>
602{
603 SIM_MODEL_IBIS* modelibis = isIbisLoaded() ? dynamic_cast<SIM_MODEL_IBIS*>( aModel ) : nullptr;
604
605 m_pinLabel->Show( isIbisLoaded() );
606 m_pinCombobox->Show( isIbisLoaded() );
607 m_pinModelLabel->Show( isIbisLoaded() );
609 m_waveformLabel->Show( isIbisLoaded() );
611
612 if( aModel != m_prevModel )
613 {
614 m_waveformChoice->Clear();
615
616 if( isIbisLoaded() )
617 {
618 for( SIM_MODEL::TYPE type : { SIM_MODEL::TYPE::KIBIS_DEVICE,
619 SIM_MODEL::TYPE::KIBIS_DRIVER_DC,
620 SIM_MODEL::TYPE::KIBIS_DRIVER_RECT,
621 SIM_MODEL::TYPE::KIBIS_DRIVER_PRBS } )
622 {
623 SIM_MODEL::DEVICE_T deviceType = SIM_MODEL::TypeInfo( type ).deviceType;
624 const std::string& deviceTypeDesc = SIM_MODEL::DeviceInfo( deviceType ).description;
625
626 if( deviceType == aModel->GetDeviceType()
627 || deviceTypeDesc == aModel->GetDeviceInfo().description )
628 {
629 m_waveformChoice->Append( SIM_MODEL::TypeInfo( type ).description );
630
631 if( type == aModel->GetType() )
632 m_waveformChoice->SetSelection( m_waveformChoice->GetCount() - 1 );
633 }
634 }
635 }
636 }
637
638 m_differentialCheckbox->Show( isIbisLoaded() && modelibis && modelibis->CanDifferential() );
639 m_modelNameLabel->SetLabel( isIbisLoaded() ? _( "Component:" ) : _( "Model:" ) );
640}
641
642
643template <typename T>
645{
646 // Change the Type choice to match the current device type.
647 if( aModel != m_prevModel )
648 {
649 m_deviceChoice->Clear();
650 m_deviceSubtypeChoice->Clear();
651
652 if( !m_rbLibraryModel->GetValue() )
653 {
654 for( SIM_MODEL::DEVICE_T deviceType : SIM_MODEL::DEVICE_T_ITERATOR() )
655 {
656 if( !SIM_MODEL::DeviceInfo( deviceType ).showInMenu )
657 continue;
658
659 m_deviceChoice->Append( SIM_MODEL::DeviceInfo( deviceType ).description );
660
661 if( equivalent( deviceType, aModel->GetDeviceType() ) )
662 m_deviceChoice->SetSelection( m_deviceChoice->GetCount() - 1 );
663 }
664
665 for( SIM_MODEL::TYPE type : SIM_MODEL::TYPE_ITERATOR() )
666 {
667 if( type == SIM_MODEL::TYPE::KIBIS_DEVICE
668 || type == SIM_MODEL::TYPE::KIBIS_DRIVER_DC
669 || type == SIM_MODEL::TYPE::KIBIS_DRIVER_RECT
670 || type == SIM_MODEL::TYPE::KIBIS_DRIVER_PRBS )
671 {
672 continue;
673 }
674
675 SIM_MODEL::DEVICE_T deviceType = SIM_MODEL::TypeInfo( type ).deviceType;
676 const std::string& deviceTypeDesc = SIM_MODEL::DeviceInfo( deviceType ).description;
677
678 if( deviceType == aModel->GetDeviceType()
679 || deviceTypeDesc == aModel->GetDeviceInfo().description )
680 {
681 m_deviceSubtypeChoice->Append( SIM_MODEL::TypeInfo( type ).description );
682
683 if( type == aModel->GetType() )
684 m_deviceSubtypeChoice->SetSelection( m_deviceSubtypeChoice->GetCount() - 1 );
685 }
686 }
687 }
688
689 m_deviceSubtypeLabel->Show( m_deviceSubtypeChoice->GetCount() > 1 );
690 m_deviceSubtypeChoice->Show( m_deviceSubtypeChoice->GetCount() > 1 );
691 }
692
693 if( dynamic_cast<SIM_MODEL_RAW_SPICE*>( aModel ) )
694 m_modelNotebook->SetSelection( 1 );
695 else
696 m_modelNotebook->SetSelection( 0 );
697
698 if( aModel->HasPrimaryValue() )
699 {
700 const SIM_MODEL::PARAM& primary = aModel->GetParam( 0 );
701
702 m_saveInValueCheckbox->SetLabel( wxString::Format( _( "Save parameter '%s (%s)' in Value "
703 "field" ),
704 primary.info.description,
705 primary.info.name ) );
706 m_saveInValueCheckbox->Enable( true );
707 }
708 else
709 {
710 m_saveInValueCheckbox->SetLabel( _( "Save primary parameter in Value field" ) );
711 m_saveInValueCheckbox->SetValue( false );
712 m_saveInValueCheckbox->Enable( false );
713 }
714}
715
716
717template <typename T>
719{
720 if( aModel != m_prevModel )
721 {
722 // This wxPropertyGridManager column and header stuff has to be here because it segfaults in
723 // the constructor.
724
725 m_paramGridMgr->SetColumnCount( PARAM_COLUMN::END_ );
726
727 m_paramGridMgr->SetColumnTitle( PARAM_COLUMN::DESCRIPTION, _( "Parameter" ) );
728 m_paramGridMgr->SetColumnTitle( PARAM_COLUMN::UNIT, _( "Unit" ) );
729 m_paramGridMgr->SetColumnTitle( PARAM_COLUMN::DEFAULT, _( "Default" ) );
730 m_paramGridMgr->SetColumnTitle( PARAM_COLUMN::TYPE, _( "Type" ) );
731
732 m_paramGridMgr->ShowHeader();
733
734
735 m_paramGrid->Clear();
736
737 m_firstCategory = m_paramGrid->Append( new wxPropertyCategory( "Geometry" ) );
738 m_paramGrid->HideProperty( "Geometry" );
739
740 m_paramGrid->Append( new wxPropertyCategory( "AC" ) );
741 m_paramGrid->HideProperty( "AC" );
742
743 m_paramGrid->Append( new wxPropertyCategory( "DC" ) );
744 m_paramGrid->HideProperty( "DC" );
745
746 m_paramGrid->Append( new wxPropertyCategory( "S-Parameters" ) );
747 m_paramGrid->HideProperty( "S-Parameters" );
748
749 m_paramGrid->Append( new wxPropertyCategory( "Capacitance" ) );
750 m_paramGrid->HideProperty( "Capacitance" );
751
752 m_paramGrid->Append( new wxPropertyCategory( "Temperature" ) );
753 m_paramGrid->HideProperty( "Temperature" );
754
755 m_paramGrid->Append( new wxPropertyCategory( "Noise" ) );
756 m_paramGrid->HideProperty( "Noise" );
757
758 m_paramGrid->Append( new wxPropertyCategory( "Distributed Quantities" ) );
759 m_paramGrid->HideProperty( "Distributed Quantities" );
760
761 m_paramGrid->Append( new wxPropertyCategory( "Waveform" ) );
762 m_paramGrid->HideProperty( "Waveform" );
763
764 m_paramGrid->Append( new wxPropertyCategory( "Limiting Values" ) );
765 m_paramGrid->HideProperty( "Limiting Values" );
766
767 m_paramGrid->Append( new wxPropertyCategory( "Advanced" ) );
768 m_paramGrid->HideProperty( "Advanced" );
769
770 m_paramGrid->Append( new wxPropertyCategory( "Flags" ) );
771 m_paramGrid->HideProperty( "Flags" );
772
773 m_paramGrid->CollapseAll();
774
775 for( int i = 0; i < aModel->GetParamCount(); ++i )
776 addParamPropertyIfRelevant( aModel, i );
777
778 m_paramGrid->CollapseAll();
779 m_paramGrid->Expand( "AC" );
780 m_paramGrid->Expand( "Waveform" );
781 }
782
783 adjustParamGridColumns( m_paramGrid->GetGrid()->GetSize().GetX(), true );
784
785 // Set all properties to default colors.
786 // Update properties in models that have autofill.
787 for( wxPropertyGridIterator it = m_paramGrid->GetIterator(); !it.AtEnd(); ++it )
788 {
789 wxColour bgCol = m_paramGrid->GetGrid()->GetPropertyDefaultCell().GetBgCol();
790 wxColour fgCol = m_paramGrid->GetGrid()->GetPropertyDefaultCell().GetFgCol();
791
792 for( int col = 0; col < m_paramGridMgr->GetColumnCount(); ++col )
793 {
794 ( *it )->GetCell( col ).SetBgCol( bgCol );
795 ( *it )->GetCell( col ).SetFgCol( fgCol );
796 }
797
798 SIM_PROPERTY* prop = dynamic_cast<SIM_PROPERTY*>( *it );
799
800 if( !prop )
801 continue;
802
803 const SIM_MODEL::PARAM& param = prop->GetParam();
804
805 // Model values other than the currently edited value may have changed. Update them.
806 // This feature is called "autofill" and present only in certain models. Don't do it for
807 // models that don't have it for performance reasons.
808 if( aModel->HasAutofill() )
809 ( *it )->SetValueFromString( param.value );
810 }
811}
812
813
814template <typename T>
816{
817 if( dynamic_cast<SIM_MODEL_SPICE_FALLBACK*>( aModel ) )
818 return;
819
820 wxString text;
821 SPICE_ITEM item;
822
823 item.modelName = m_modelListBox->GetStringSelection();
824
825 if( m_rbBuiltinModel->GetValue() || item.modelName == "" )
827
828 text << aModel->SpiceGenerator().Preview( item );
829
830 m_codePreview->SetText( text );
831 m_codePreview->SelectNone();
832}
833
834
835template <typename T>
836void DIALOG_SIM_MODEL<T>::updatePinAssignments( SIM_MODEL* aModel, bool aForceRefreshFromModel )
837{
838 m_pinAssignmentsGrid->CommitPendingChanges( true );
839
840 if( m_pinAssignmentsGrid->GetNumberRows() == 0 )
841 {
842 m_pinAssignmentsGrid->AppendRows( (int) m_sortedPartPins.size() );
843
844 for( int ii = 0; ii < m_pinAssignmentsGrid->GetNumberRows(); ++ii )
845 {
846 wxString symbolPinString = getSymbolPinString( ii );
847
848 m_pinAssignmentsGrid->SetReadOnly( ii, PIN_COLUMN::SYMBOL );
849 m_pinAssignmentsGrid->SetCellValue( ii, PIN_COLUMN::SYMBOL, symbolPinString );
850 }
851
852 aForceRefreshFromModel = true;
853 }
854
855 if( aForceRefreshFromModel )
856 {
857 // Reset the grid.
858 for( int row = 0; row < m_pinAssignmentsGrid->GetNumberRows(); ++row )
859 m_pinAssignmentsGrid->SetCellValue( row, PIN_COLUMN::MODEL, _( "Not Connected" ) );
860
861 // Now set up the grid values in the Model column.
862 for( int modelPinIndex = 0; modelPinIndex < aModel->GetPinCount(); ++modelPinIndex )
863 {
864 wxString symbolPinNumber = aModel->GetPin( modelPinIndex ).symbolPinNumber;
865
866 if( symbolPinNumber == "" )
867 continue;
868
869 int symbolPinRow = findSymbolPinRow( symbolPinNumber );
870
871 if( symbolPinRow == -1 )
872 continue;
873
874 wxString modelPinString = getModelPinString( aModel, modelPinIndex );
875 m_pinAssignmentsGrid->SetCellValue( symbolPinRow, PIN_COLUMN::MODEL, modelPinString );
876 }
877 }
878
879 for( int ii = 0; ii < m_pinAssignmentsGrid->GetNumberRows(); ++ii )
880 {
881 // Set up the Model column cell editors with dropdown options.
882 std::vector<BITMAPS> modelPinIcons;
883 wxArrayString modelPinChoices;
884
885 for( int jj = 0; jj < aModel->GetPinCount(); ++jj )
886 {
887 if( aModel->GetPin( jj ).symbolPinNumber != "" )
888 modelPinIcons.push_back( PinShapeGetBitmap( GRAPHIC_PINSHAPE::LINE ) );
889 else
890 modelPinIcons.push_back( BITMAPS::INVALID_BITMAP );
891
892 modelPinChoices.Add( getModelPinString( aModel, jj ) );
893 }
894
895 modelPinIcons.push_back( BITMAPS::INVALID_BITMAP );
896 modelPinChoices.Add( _( "Not Connected" ) );
897
898 // This is not a memory leak; `SetCellEditor()` calls `DecRef()` on its previous editor.
899 m_pinAssignmentsGrid->SetCellEditor( ii, PIN_COLUMN::MODEL,
900 new GRID_CELL_ICON_TEXT_POPUP( modelPinIcons, modelPinChoices ) );
901
902 // Assignment stays the same, but model pin names need to be updated
903 int modelPinIndex = getModelPinIndex( m_pinAssignmentsGrid->GetCellValue( ii, PIN_COLUMN::MODEL ) );
904
905 if( modelPinIndex >= 0 )
906 m_pinAssignmentsGrid->SetCellValue( ii, PIN_COLUMN::MODEL, getModelPinString( aModel, modelPinIndex ) );
907 }
908
909 // TODO: Show a preview of the symbol with the pin numbers shown.
910
911 if( aModel->GetType() == SIM_MODEL::TYPE::SUBCKT )
912 {
913 SIM_MODEL_SUBCKT* subckt = static_cast<SIM_MODEL_SUBCKT*>( aModel );
914 m_subckt->SetText( subckt->GetSpiceCode() );
915 m_subckt->SetEditable( false );
916 }
917 else
918 {
919 m_subcktLabel->Show( false );
920 m_subckt->Show( false );
921 }
922}
923
924
925template <typename T>
927{
928 for( int i = 0; i < aModel->GetPinCount(); ++i )
929 {
930 if( !m_symbol.GetPin( aModel->GetPin( i ).symbolPinNumber ) )
931 aModel->AssignSymbolPinNumberToModelPin( i, "" );
932 }
933}
934
935
936template <typename T>
937bool DIALOG_SIM_MODEL<T>::loadLibrary( const wxString& aLibraryPath, REPORTER& aReporter, bool aForceReload )
938{
939 if( m_prevLibrary == aLibraryPath && !aForceReload )
940 return true;
941
942 m_libraryModelsMgr.SetForceFullParse();
943 m_libraryModelsMgr.SetLibrary( aLibraryPath, aReporter );
944 m_libraryPathText->ChangeValue( aLibraryPath );
946
948 {
949 m_libraryModelsMgr.Clear();
950
951 if( m_modelListBox->GetSelection() != wxNOT_FOUND )
952 m_modelListBox->SetSelection( wxNOT_FOUND );
953
954 if( m_modelListBox->GetCount() )
955 m_modelListBox->Clear();
956
957 wxArrayString emptyArray;
958 m_pinModelCombobox->Set( emptyArray );
959 m_pinCombobox->Set( emptyArray );
960 m_pinModelCombobox->SetSelection( -1 );
961 m_pinCombobox->SetSelection( -1 );
962 m_waveformChoice->Clear();
963 m_prevLibrary.Clear();
964
965 return false;
966 }
967
968 std::string modelName = GetFieldValue( &m_fields, SIM_LIBRARY::NAME_FIELD, true, 0 );
969
970 const auto partPinNumbers = SIM_MODEL::PinNumbers( m_sortedPartPins );
971
972 for( const auto& [baseModelName, baseModel] : library()->GetModels() )
973 {
974 if( baseModelName == modelName )
975 m_libraryModelsMgr.CreateModel( &baseModel, partPinNumbers, m_fields, true, 0, aReporter );
976 else
977 m_libraryModelsMgr.CreateModel( &baseModel, partPinNumbers, aReporter );
978 }
979
980 m_rbLibraryModel->SetValue( true );
981
982 wxArrayString modelNames;
983
984 bool modelNameExists = false;
985 for( const auto& [name, model] : library()->GetModels() )
986 {
987 modelNames.Add( name );
989
990 if( wxString( name ).IsSameAs( modelName, false ) )
991 modelNameExists = true;
992 }
993
994 modelNames.Sort();
995
996 m_modelListBox->Clear();
997 m_modelListBox->Append( modelNames );
998
999 if( !modelNameExists )
1000 {
1001 m_infoBar->ShowMessage( wxString::Format( _( "No model named '%s' in '%s'." ),
1002 modelName,
1003 aLibraryPath ) );
1004 return false;
1005 }
1006
1007 if( isIbisLoaded() )
1008 {
1009 wxArrayString emptyArray;
1010 m_pinModelCombobox->Set( emptyArray );
1011 m_pinCombobox->Set( emptyArray );
1012 m_pinModelCombobox->SetSelection( -1 );
1013 m_pinCombobox->SetSelection( -1 );
1014 }
1015
1016 m_modelListBox->SetStringSelection( modelName );
1017
1018 if( m_modelListBox->GetSelection() < 0 && m_modelListBox->GetCount() > 0 )
1019 {
1020 m_modelListBox->SetSelection( 0 ); // WARNING: SetSelection() does NOT fire events
1021 wxCommandEvent dummy;
1023 }
1024
1025 m_curModelType = curModel().GetType();
1026
1027 m_prevLibrary = aLibraryPath;
1028 return true;
1029}
1030
1031
1032template <typename T>
1034{
1035 if( aModel->GetParam( aParamIndex ).info.dir == SIM_MODEL::PARAM::DIR_OUT )
1036 return;
1037
1038 switch( aModel->GetParam( aParamIndex ).info.category )
1039 {
1040 case CATEGORY::AC:
1041 m_paramGrid->HideProperty( "AC", false );
1042 m_paramGrid->AppendIn( "AC", newParamProperty( aModel, aParamIndex ) );
1043 break;
1044
1045 case CATEGORY::DC:
1046 m_paramGrid->HideProperty( "DC", false );
1047 m_paramGrid->AppendIn( "DC", newParamProperty( aModel, aParamIndex ) );
1048 break;
1049
1050 case CATEGORY::S_PARAM:
1051 m_paramGrid->HideProperty( "S-Parameters", false );
1052 m_paramGrid->AppendIn( "S-Parameters", newParamProperty( aModel, aParamIndex ) );
1053 break;
1054
1055 case CATEGORY::CAPACITANCE:
1056 m_paramGrid->HideProperty( "Capacitance", false );
1057 m_paramGrid->AppendIn( "Capacitance", newParamProperty( aModel, aParamIndex ) );
1058 break;
1059
1060 case CATEGORY::TEMPERATURE:
1061 m_paramGrid->HideProperty( "Temperature", false );
1062 m_paramGrid->AppendIn( "Temperature", newParamProperty( aModel, aParamIndex ) );
1063 break;
1064
1065 case CATEGORY::NOISE:
1066 m_paramGrid->HideProperty( "Noise", false );
1067 m_paramGrid->AppendIn( "Noise", newParamProperty( aModel, aParamIndex ) );
1068 break;
1069
1070 case CATEGORY::DISTRIBUTED_QUANTITIES:
1071 m_paramGrid->HideProperty( "Distributed Quantities", false );
1072 m_paramGrid->AppendIn( "Distributed Quantities", newParamProperty( aModel, aParamIndex ) );
1073 break;
1074
1075 case CATEGORY::WAVEFORM:
1076 m_paramGrid->HideProperty( "Waveform", false );
1077 m_paramGrid->AppendIn( "Waveform", newParamProperty( aModel, aParamIndex ) );
1078 break;
1079
1080 case CATEGORY::GEOMETRY:
1081 m_paramGrid->HideProperty( "Geometry", false );
1082 m_paramGrid->AppendIn( "Geometry", newParamProperty( aModel, aParamIndex ) );
1083 break;
1084
1085 case CATEGORY::LIMITING_VALUES:
1086 m_paramGrid->HideProperty( "Limiting Values", false );
1087 m_paramGrid->AppendIn( "Limiting Values", newParamProperty( aModel, aParamIndex ) );
1088 break;
1089
1090 case CATEGORY::ADVANCED:
1091 m_paramGrid->HideProperty( "Advanced", false );
1092 m_paramGrid->AppendIn( "Advanced", newParamProperty( aModel, aParamIndex ) );
1093 break;
1094
1095 case CATEGORY::FLAGS:
1096 m_paramGrid->HideProperty( "Flags", false );
1097 m_paramGrid->AppendIn( "Flags", newParamProperty( aModel, aParamIndex ) );
1098 break;
1099
1100 default:
1101 m_paramGrid->Insert( m_firstCategory, newParamProperty( aModel, aParamIndex ) );
1102 break;
1103
1104 case CATEGORY::INITIAL_CONDITIONS:
1105 case CATEGORY::SUPERFLUOUS:
1106 return;
1107 }
1108}
1109
1110
1111template <typename T>
1112wxPGProperty* DIALOG_SIM_MODEL<T>::newParamProperty( SIM_MODEL* aModel, int aParamIndex ) const
1113{
1114 const SIM_MODEL::PARAM& param = aModel->GetParam( aParamIndex );
1115 wxString paramDescription;
1116
1117 if( param.info.description == "" )
1118 paramDescription = wxString::Format( "%s", param.info.name );
1119 else
1120 paramDescription = wxString::Format( "%s (%s)", param.info.description, param.info.name );
1121
1122 wxPGProperty* prop = nullptr;
1123
1124 switch( param.info.type )
1125 {
1127 prop = new SIM_BOOL_PROPERTY( paramDescription, param.info.name, *aModel, aParamIndex );
1128 prop->SetAttribute( wxPG_BOOL_USE_CHECKBOX, true );
1129 break;
1130
1132 prop = new SIM_STRING_PROPERTY( paramDescription, param.info.name, *aModel, aParamIndex,
1134 break;
1135
1137 prop = new SIM_STRING_PROPERTY( paramDescription, param.info.name, *aModel, aParamIndex,
1139 break;
1140
1141 //case TYPE_COMPLEX:
1142 // break;
1143
1145 // Special case: K-line mutual inductance statement parameters l1 and l2 are references
1146 // to other inductors in the circuit.
1147 if( dynamic_cast<SIM_MODEL_L_MUTUAL*>( aModel ) != nullptr
1148 && ( param.info.name == "l1" || param.info.name == "l2" ) )
1149 {
1150 wxArrayString inductors;
1151
1152 if( SCH_EDIT_FRAME* schEditFrame = dynamic_cast<SCH_EDIT_FRAME*>( m_frame ) )
1153 {
1154 SPICE_CIRCUIT_MODEL circuit( &schEditFrame->Schematic(), &schEditFrame->Kiway() );
1155 NULL_REPORTER devNul;
1156
1158
1159 for( const SPICE_ITEM& item : circuit.GetItems() )
1160 {
1161 if( item.model->GetDeviceType() == SIM_MODEL::DEVICE_T::L )
1162 inductors.push_back( item.refName );
1163 }
1164
1165 inductors.Sort(
1166 []( const wxString& a, const wxString& b ) -> int
1167 {
1168 return StrNumCmp( a, b, CMP_T::IGNORE_CASE );
1169 } );
1170 }
1171
1172 if( inductors.empty() )
1173 {
1174 prop = new SIM_STRING_PROPERTY( paramDescription, param.info.name, *aModel, aParamIndex,
1176 }
1177 else
1178 {
1179 prop = new SIM_ENUM_PROPERTY( paramDescription, param.info.name, *aModel, aParamIndex,
1180 inductors );
1181 }
1182 }
1183 else if( param.info.enumValues.empty() )
1184 {
1185 prop = new SIM_STRING_PROPERTY( paramDescription, param.info.name, *aModel, aParamIndex,
1187 }
1188 else
1189 {
1190 wxArrayString values;
1191
1192 for( const std::string& string : aModel->GetParam( aParamIndex ).info.enumValues )
1193 values.Add( string );
1194
1195 prop = new SIM_ENUM_PROPERTY( paramDescription, param.info.name, *aModel, aParamIndex,
1196 values );
1197 }
1198 break;
1199
1200 default:
1201 prop = new wxStringProperty( paramDescription, param.info.name );
1202 break;
1203 }
1204
1205 prop->SetAttribute( wxPG_ATTR_UNITS, wxString::FromUTF8( param.info.unit.c_str() ) );
1206
1207 // Legacy due to the way we extracted the parameters from Ngspice.
1208 prop->SetCell( 3, wxString::FromUTF8( param.info.defaultValue ) );
1209
1210 wxString typeStr;
1211
1212 switch( param.info.type )
1213 {
1214 case SIM_VALUE::TYPE_BOOL: typeStr = wxT( "Bool" ); break;
1215 case SIM_VALUE::TYPE_INT: typeStr = wxT( "Int" ); break;
1216 case SIM_VALUE::TYPE_FLOAT: typeStr = wxT( "Float" ); break;
1217 case SIM_VALUE::TYPE_COMPLEX: typeStr = wxT( "Complex" ); break;
1218 case SIM_VALUE::TYPE_STRING: typeStr = wxT( "String" ); break;
1219 case SIM_VALUE::TYPE_BOOL_VECTOR: typeStr = wxT( "Bool Vector" ); break;
1220 case SIM_VALUE::TYPE_INT_VECTOR: typeStr = wxT( "Int Vector" ); break;
1221 case SIM_VALUE::TYPE_FLOAT_VECTOR: typeStr = wxT( "Float Vector" ); break;
1222 case SIM_VALUE::TYPE_COMPLEX_VECTOR: typeStr = wxT( "Complex Vector" ); break;
1223 }
1224
1225 prop->SetCell( PARAM_COLUMN::TYPE, typeStr );
1226
1227 return prop;
1228}
1229
1230
1231template <typename T>
1232int DIALOG_SIM_MODEL<T>::findSymbolPinRow( const wxString& aSymbolPinNumber ) const
1233{
1234 for( int row = 0; row < static_cast<int>( m_sortedPartPins.size() ); ++row )
1235 {
1237
1238 if( pin->GetNumber() == aSymbolPinNumber )
1239 return row;
1240 }
1241
1242 return -1;
1243}
1244
1245
1246template <typename T>
1248{
1249 if( m_rbLibraryModel->GetValue() )
1250 {
1251 wxString sel = m_modelListBox->GetStringSelection();
1252
1253 if( m_modelListBoxEntryToLibraryIdx.contains( sel ) )
1254 {
1255 int idx = m_modelListBoxEntryToLibraryIdx.at( sel );
1256
1257 if( idx >= 0 && idx < (int) m_libraryModelsMgr.GetModels().size() )
1258 return m_libraryModelsMgr.GetModels().at( idx ).get();
1259 }
1260 }
1261 else
1262 {
1263 if( (int) m_curModelType < (int) m_builtinModelsMgr.GetModels().size() )
1264 return m_builtinModelsMgr.GetModels().at( static_cast<int>( m_curModelType ) );
1265 }
1266
1267 return m_builtinModelsMgr.GetModels().at( (int) SIM_MODEL::TYPE::NONE );
1268}
1269
1270
1271template <typename T>
1273{
1274 if( m_libraryModelsMgr.GetLibraries().size() == 1 )
1275 return &m_libraryModelsMgr.GetLibraries().begin()->second.get();
1276
1277 return nullptr;
1278}
1279
1280
1281template <typename T>
1282wxString DIALOG_SIM_MODEL<T>::getSymbolPinString( int symbolPinIndex ) const
1283{
1284 SCH_PIN* pin = m_sortedPartPins.at( symbolPinIndex );
1285 wxString pinNumber;
1286 wxString pinName;
1287
1288 if( pin )
1289 {
1290 pinNumber = pin->GetShownNumber();
1291 pinName = pin->GetShownName();
1292 }
1293
1294 if( !pinName.IsEmpty() && pinName != pinNumber )
1295 pinNumber += wxString::Format( wxT( " (\"%s\")" ), pinName );
1296
1297 return pinNumber;
1298}
1299
1300
1301template <typename T>
1302wxString DIALOG_SIM_MODEL<T>::getModelPinString( SIM_MODEL* aModel, int aModelPinIndex ) const
1303{
1304 wxString modelPinName;
1305
1306 if( aModelPinIndex >= 0 && aModelPinIndex < aModel->GetPinCount() )
1307 modelPinName = aModel->GetPin( aModelPinIndex ).modelPinName;
1308
1309 wxString modelPinNumber = wxString::Format( "%d", aModelPinIndex + 1 );
1310
1311 if( !modelPinName.IsEmpty() && modelPinName != modelPinNumber )
1312 modelPinNumber += wxString::Format( wxT( " (\"%s\")" ), modelPinName );
1313
1314 return modelPinNumber;
1315}
1316
1317
1318template <typename T>
1319int DIALOG_SIM_MODEL<T>::getModelPinIndex( const wxString& aModelPinString ) const
1320{
1321 if( aModelPinString == "Not Connected" )
1323
1324 int length = aModelPinString.Find( " " );
1325
1326 if( length == wxNOT_FOUND )
1327 length = static_cast<int>( aModelPinString.Length() );
1328
1329 long result = 0;
1330 aModelPinString.Mid( 0, length ).ToCLong( &result );
1331
1332 return static_cast<int>( result - 1 );
1333}
1334
1335
1336template <typename T>
1337void DIALOG_SIM_MODEL<T>::onRadioButton( wxCommandEvent& aEvent )
1338{
1339 m_prevModel = nullptr; // Ensure the Model panel will be rebuild after updating other params.
1340 updateWidgets();
1341}
1342
1343
1344template <typename T>
1345void DIALOG_SIM_MODEL<T>::onLibraryPathText( wxCommandEvent& aEvent )
1346{
1347 m_rbLibraryModel->SetValue( true );
1348}
1349
1350
1351template <typename T>
1353{
1354 m_rbLibraryModel->SetValue( true );
1355
1357 wxString path = m_libraryPathText->GetValue();
1358
1359 if( loadLibrary( path, reporter, true ) || path.IsEmpty() )
1360 m_infoBar->Hide();
1361 else if( reporter.HasMessage() )
1362 m_infoBar->ShowMessage( reporter.GetMessages() );
1363
1364 updateWidgets();
1365}
1366
1367
1368template <typename T>
1370{
1371 CallAfter(
1372 [this]()
1373 {
1374 // Disable logging -- otherwise we'll end up in an endless loop of show-log,
1375 // kill-focus, show-log, kill-focus, etc.
1376 wxLogNull doNotLog;
1377
1378 wxCommandEvent dummy;
1380 } );
1381
1382 aEvent.Skip(); // mandatory in wxFocusEvent events
1383}
1384
1385
1386template <typename T>
1387void DIALOG_SIM_MODEL<T>::onBrowseButtonClick( wxCommandEvent& aEvent )
1388{
1389 static wxString s_mruPath;
1390
1391 wxString path = s_mruPath.IsEmpty() ? Prj().GetProjectPath() : s_mruPath;
1392 wxFileDialog dlg( this, _( "Browse Models" ), path );
1394
1395 dlg.SetCustomizeHook( customize );
1396
1398
1399 if( dlg.ShowModal() == wxID_CANCEL )
1400 return;
1401
1402 m_rbLibraryModel->SetValue( true );
1403
1404 path = dlg.GetPath();
1405 wxFileName fn( path );
1406 s_mruPath = fn.GetPath();
1407
1408 if( customize.GetEmbed() )
1409 {
1410 EMBEDDED_FILES::EMBEDDED_FILE* result = m_filesStack.back()->AddFile( fn, false );
1411 path = result->GetLink();
1412 }
1413 else if( fn.MakeRelativeTo( Prj().GetProjectPath() ) && !fn.GetFullPath().StartsWith( wxS( ".." ) ) )
1414 {
1415 path = fn.GetFullPath();
1416 }
1417
1419
1420 if( loadLibrary( path, reporter, true ) )
1421 m_infoBar->Hide();
1422 else
1423 m_infoBar->ShowMessage( reporter.GetMessages() );
1424
1425 updateWidgets();
1426}
1427
1428
1429template <typename T>
1430void DIALOG_SIM_MODEL<T>::onFilterCharHook( wxKeyEvent& aKeyStroke )
1431{
1432 int count = m_modelListBox->GetCount();
1433
1434 if( count == 0 )
1435 {
1436 aKeyStroke.Skip();
1437 return;
1438 }
1439
1440 int sel = m_modelListBox->GetSelection();
1441
1442 switch( aKeyStroke.GetKeyCode() )
1443 {
1444 case WXK_UP:
1445 if( sel == wxNOT_FOUND )
1446 sel = count - 1;
1447 else
1448 sel = std::max( sel - 1, 0 );
1449
1450 break;
1451
1452 case WXK_DOWN:
1453 if( sel == wxNOT_FOUND )
1454 sel = 0;
1455 else
1456 sel = std::min( sel + 1, count - 1 );
1457
1458 break;
1459
1460 case WXK_RETURN:
1461 wxPostEvent( this, wxCommandEvent( wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK ) );
1462 return;
1463
1464 default:
1465 aKeyStroke.Skip();
1466 return;
1467 }
1468
1469 m_modelListBox->SetSelection( sel ); // WARNING: SetSelection() does NOT fire events
1470 wxCommandEvent dummy;
1472}
1473
1474
1475template <typename T>
1476void DIALOG_SIM_MODEL<T>::onModelFilter( wxCommandEvent& aEvent )
1477{
1478 if( library() == nullptr ) // No library loaded
1479 return;
1480
1481 wxArrayString modelNames;
1482 wxString current = m_modelListBox->GetStringSelection();
1483 wxString filter = wxT( "*" ) + m_modelFilter->GetValue() + wxT( "*" );
1484
1485 for( const auto& [name, model] : library()->GetModels() )
1486 {
1487 wxString wx_name( name );
1488
1489 if( wx_name.Matches( filter ) )
1490 modelNames.Add( wx_name );
1491 }
1492
1493 modelNames.Sort();
1494
1495 m_modelListBox->Clear();
1496 m_modelListBox->Append( modelNames );
1497
1498 if( m_modelListBox->GetCount() > 0 )
1499 {
1500 if( !m_modelListBox->SetStringSelection( current ) )
1501 {
1502 m_modelListBox->SetSelection( 0 ); // WARNING: SetSelection() does NOT fire events
1503 wxCommandEvent dummy;
1505 }
1506 }
1507}
1508
1509
1510template <typename T>
1511void DIALOG_SIM_MODEL<T>::onModelNameChoice( wxCommandEvent& aEvent )
1512{
1513 if( isIbisLoaded() )
1514 {
1515 wxArrayString pinLabels;
1516 SIM_MODEL_IBIS* modelkibis = dynamic_cast<SIM_MODEL_IBIS*>( &curModel() );
1517
1518 wxCHECK2( modelkibis, return );
1519
1520 for( std::pair<wxString, wxString> strs : modelkibis->GetIbisPins() )
1521 pinLabels.Add( strs.first + wxT( " - " ) + strs.second );
1522
1523 m_pinCombobox->Set( pinLabels );
1524
1525 wxArrayString emptyArray;
1526 m_pinModelCombobox->Set( emptyArray );
1527 }
1528
1529 m_rbLibraryModel->SetValue( true );
1530
1531 if( SIM_MODEL_SPICE_FALLBACK* fallback =
1532 dynamic_cast<SIM_MODEL_SPICE_FALLBACK*>( &curModel() ) )
1533 {
1534 wxArrayString lines = wxSplit( fallback->GetSpiceCode(), '\n' );
1535 wxString code;
1536
1537 for( const wxString& line : lines )
1538 {
1539 if( !line.StartsWith( '*' ) )
1540 {
1541 if( !code.IsEmpty() )
1542 code += "\n";
1543
1544 code += line;
1545 }
1546 }
1547
1548 m_infoBar->ShowMessage( wxString::Format( _( "Failed to parse:\n\n"
1549 "%s\n"
1550 "Using generic SPICE model." ),
1551 code ) );
1552 }
1553 else
1554 {
1555 m_infoBar->Hide();
1556 }
1557
1558 updateWidgets();
1559}
1560
1561
1562template <typename T>
1563void DIALOG_SIM_MODEL<T>::onPinCombobox( wxCommandEvent& aEvent )
1564{
1565 wxArrayString modelLabels;
1566
1567 SIM_MODEL_IBIS& ibisModel = static_cast<SIM_MODEL_IBIS&>( curModel() );
1568
1569 std::vector<std::pair<std::string, std::string>> strs = ibisModel.GetIbisPins();
1570 std::string pinNumber = strs.at( m_pinCombobox->GetSelection() ).first;
1571
1572 const SIM_LIBRARY_IBIS* ibisLibrary = dynamic_cast<const SIM_LIBRARY_IBIS*>( library() );
1573
1574 ibisModel.ChangePin( *ibisLibrary, pinNumber );
1575
1576 ibisModel.m_enableDiff = ibisLibrary->isPinDiff( ibisModel.GetComponentName(), pinNumber );
1577
1578 for( wxString modelName : ibisModel.GetIbisModels() )
1579 modelLabels.Add( modelName );
1580
1581 m_pinModelCombobox->Set( modelLabels );
1582
1583 if( m_pinModelCombobox->GetCount() == 1 )
1584 m_pinModelCombobox->SetSelection( 0 );
1585 else
1586 m_pinModelCombobox->SetSelection( -1 );
1587
1588 updateWidgets();
1589}
1590
1591
1592template <typename T>
1594{
1595 m_pinCombobox->SetSelection( m_pinCombobox->FindString( m_pinCombobox->GetValue() ) );
1596
1597 onPinModelCombobox( aEvent );
1598}
1599
1600
1601template <typename T>
1602void DIALOG_SIM_MODEL<T>::onPinModelCombobox( wxCommandEvent& aEvent )
1603{
1604 if( SIM_MODEL_IBIS* ibismodel = dynamic_cast<SIM_MODEL_IBIS*>( &curModel() ) )
1605 {
1606 const SIM_LIBRARY_IBIS* ibisLibrary = dynamic_cast<const SIM_LIBRARY_IBIS*>( library() );
1607
1608 // Skip uncommitted combobox text so SetIbisModel doesn't silently fail.
1609 if( ibisLibrary
1610 && m_pinCombobox->GetSelection() >= 0
1611 && m_pinModelCombobox->GetSelection() >= 0 )
1612 {
1613 std::string pinNumber = ibismodel->GetIbisPins()
1614 .at( m_pinCombobox->GetSelection() )
1615 .first;
1616 std::string modelName = m_pinModelCombobox->GetStringSelection().ToStdString();
1617
1618 ibismodel->SetIbisModel( *ibisLibrary, pinNumber, modelName );
1619 }
1620 }
1621
1622 updateWidgets();
1623}
1624
1625
1626template <typename T>
1628{
1629 m_pinModelCombobox->SetSelection( m_pinModelCombobox->FindString( m_pinModelCombobox->GetValue() ) );
1630}
1631
1632
1633template <typename T>
1635{
1636 if( SIM_MODEL_IBIS* modelibis = dynamic_cast<SIM_MODEL_IBIS*>( &curModel() ) )
1637 {
1638 bool diff = m_differentialCheckbox->GetValue() && modelibis->CanDifferential();
1639 modelibis->SwitchSingleEndedDiff( diff );
1640
1641 updateWidgets();
1642 }
1643}
1644
1645
1646template <typename T>
1647void DIALOG_SIM_MODEL<T>::onDeviceTypeChoice( wxCommandEvent& aEvent )
1648{
1649 m_rbBuiltinModel->SetValue( true );
1650
1651 for( SIM_MODEL::DEVICE_T deviceType : SIM_MODEL::DEVICE_T_ITERATOR() )
1652 {
1653 if( SIM_MODEL::DeviceInfo( deviceType ).description == m_deviceChoice->GetStringSelection() )
1654 {
1655 m_curModelType = m_curModelTypeOfDeviceType.at( deviceType );
1656 break;
1657 }
1658 }
1659
1660 updateWidgets();
1661}
1662
1663
1664template <typename T>
1665void DIALOG_SIM_MODEL<T>::onWaveformChoice( wxCommandEvent& aEvent )
1666{
1667 SIM_MODEL::DEVICE_T deviceType = curModel().GetDeviceType();
1668 wxString typeDescription = m_waveformChoice->GetStringSelection();
1669
1670 for( SIM_MODEL::TYPE type : { SIM_MODEL::TYPE::KIBIS_DEVICE,
1671 SIM_MODEL::TYPE::KIBIS_DRIVER_DC,
1672 SIM_MODEL::TYPE::KIBIS_DRIVER_RECT,
1673 SIM_MODEL::TYPE::KIBIS_DRIVER_PRBS } )
1674 {
1675 if( equivalent( deviceType, SIM_MODEL::TypeInfo( type ).deviceType )
1676 && typeDescription == SIM_MODEL::TypeInfo( type ).description )
1677 {
1678 int idx = 0;
1679 wxString sel = m_modelListBox->GetStringSelection();
1680
1681 if( m_modelListBoxEntryToLibraryIdx.contains( sel ) )
1682 idx = m_modelListBoxEntryToLibraryIdx.at( sel );
1683
1684 SIM_MODEL_IBIS& baseModel = static_cast<SIM_MODEL_IBIS&>( m_libraryModelsMgr.GetModels()[idx].get() );
1685
1686 m_libraryModelsMgr.SetModel( idx, std::make_unique<SIM_MODEL_IBIS>( type, baseModel ) );
1687
1688 try
1689 {
1690 m_libraryModelsMgr.GetModels()[idx].get().ReadDataFields(
1692 }
1693 catch( IO_ERROR& err )
1694 {
1695 DisplayErrorMessage( this, err.What() );
1696 }
1697
1698 m_curModelType = type;
1699 break;
1700 }
1701 }
1702
1703 m_curModelTypeOfDeviceType.at( deviceType ) = m_curModelType;
1704 updateWidgets();
1705}
1706
1707
1708template <typename T>
1709void DIALOG_SIM_MODEL<T>::onTypeChoice( wxCommandEvent& aEvent )
1710{
1711 SIM_MODEL::DEVICE_T deviceType = curModel().GetDeviceType();
1712 wxString typeDescription = m_deviceSubtypeChoice->GetStringSelection();
1713
1714 for( SIM_MODEL::TYPE type : SIM_MODEL::TYPE_ITERATOR() )
1715 {
1716 if( equivalent( deviceType, SIM_MODEL::TypeInfo( type ).deviceType )
1717 && typeDescription == SIM_MODEL::TypeInfo( type ).description )
1718 {
1719 m_curModelType = type;
1720 break;
1721 }
1722 }
1723
1724 m_curModelTypeOfDeviceType.at( deviceType ) = m_curModelType;
1725 updateWidgets();
1726}
1727
1728
1729template <typename T>
1730void DIALOG_SIM_MODEL<T>::onPageChanging( wxBookCtrlEvent& event )
1731{
1733}
1734
1735
1736template <typename T>
1738{
1739 int symbolPinIndex = aEvent.GetRow();
1740 wxString oldModelPinName = aEvent.GetString();
1741 wxString modelPinName = m_pinAssignmentsGrid->GetCellValue( aEvent.GetRow(), aEvent.GetCol() );
1742
1743 int oldModelPinIndex = getModelPinIndex( oldModelPinName );
1744 int modelPinIndex = getModelPinIndex( modelPinName );
1745
1746 if( oldModelPinIndex != SIM_MODEL_PIN::NOT_CONNECTED )
1747 curModel().AssignSymbolPinNumberToModelPin( oldModelPinIndex, "" );
1748
1749 if( modelPinIndex != SIM_MODEL_PIN::NOT_CONNECTED )
1750 {
1751 SCH_PIN* symbolPin = m_sortedPartPins.at( symbolPinIndex );
1752
1753 curModel().AssignSymbolPinNumberToModelPin( modelPinIndex, symbolPin->GetShownNumber() );
1754 }
1755
1757
1758 aEvent.Skip();
1759}
1760
1761
1762template <typename T>
1764{
1765 wxGridUpdateLocker deferRepaintsTillLeavingScope( m_pinAssignmentsGrid );
1766
1768 m_pinAssignmentsGrid->SetColSize( PIN_COLUMN::MODEL, gridWidth / 2 );
1769 m_pinAssignmentsGrid->SetColSize( PIN_COLUMN::SYMBOL, gridWidth / 2 );
1770
1771 aEvent.Skip();
1772}
1773
1774
1775template <typename T>
1777{
1778 // By default, when a property grid is focused, the textbox is not immediately focused until
1779 // Tab key is pressed. This is inconvenient, so we fix that here.
1780
1781 wxPropertyGrid* grid = m_paramGrid->GetGrid();
1782 wxPGProperty* selected = grid->GetSelection();
1783
1784 if( !selected )
1785 selected = grid->wxPropertyGridInterface::GetFirst();
1786
1787#if wxCHECK_VERSION( 3, 3, 0 )
1788 if( selected )
1789 grid->DoSelectProperty( selected, wxPGSelectPropertyFlags::Focus );
1790#else
1791 if( selected )
1792 grid->DoSelectProperty( selected, wxPG_SEL_FOCUS );
1793#endif
1794
1795 aEvent.Skip();
1796}
1797
1798
1799template <typename T>
1800void DIALOG_SIM_MODEL<T>::onParamGridSelectionChange( wxPropertyGridEvent& aEvent )
1801{
1802 wxPropertyGrid* grid = m_paramGrid->GetGrid();
1803
1804 // Jump over categories.
1805 if( grid->GetSelection() && grid->GetSelection()->IsCategory() )
1806 {
1807 wxPGProperty* selection = grid->GetSelection();
1808
1809 // If the new selection is immediately above the previous selection, we jump up. Otherwise
1810 // we jump down. We do this by simulating up or down arrow keys.
1811
1812 wxPropertyGridIterator it = grid->GetIterator( wxPG_ITERATE_VISIBLE, selection );
1813 it.Next();
1814
1815 wxKeyEvent* keyEvent = new wxKeyEvent( wxEVT_KEY_DOWN );
1816
1817 if( *it == m_prevParamGridSelection )
1818 {
1819 if( !selection->IsExpanded() )
1820 {
1821 grid->Expand( selection );
1822 keyEvent->m_keyCode = WXK_DOWN;
1823 wxQueueEvent( grid, keyEvent );
1824
1825 // Does not work for some reason.
1826 /*m_paramGrid->DoSelectProperty( selection->Item( selection->GetChildCount() - 1 ),
1827 wxPG_SEL_FOCUS );*/
1828 }
1829 else
1830 {
1831 keyEvent->m_keyCode = WXK_UP;
1832 wxQueueEvent( grid, keyEvent );
1833 }
1834 }
1835 else
1836 {
1837 if( !selection->IsExpanded() )
1838 grid->Expand( selection );
1839
1840 keyEvent->m_keyCode = WXK_DOWN;
1841 wxQueueEvent( grid, keyEvent );
1842 }
1843
1844 m_prevParamGridSelection = grid->GetSelection();
1845 return;
1846 }
1847
1848 wxWindow* editorControl = grid->GetEditorControl();
1849
1850 if( !editorControl )
1851 {
1852 m_prevParamGridSelection = grid->GetSelection();
1853 return;
1854 }
1855
1856 // Without this the user had to press tab before they could edit the field.
1857 editorControl->SetFocus();
1858 m_prevParamGridSelection = grid->GetSelection();
1859}
1860
1861
1862template <typename T>
1863void DIALOG_SIM_MODEL<T>::onUpdateUI( wxUpdateUIEvent& aEvent )
1864{
1865 // This is currently patched in wxPropertyGrid::ScrollWindow() in the Mac wxWidgets fork.
1866 // However, we may need this version if it turns out to be an issue on other platforms and
1867 // we can't get it upstreamed.
1868#if 0
1869 // It's a shame to do this on the UpdateUI event, but neither the wxPropertyGridManager,
1870 // wxPropertyGridPage, wxPropertyGrid, nor the wxPropertyGrid's GetCanvas() window appear
1871 // to get scroll events.
1872
1873 wxPropertyGrid* grid = m_paramGrid->GetGrid();
1874 wxTextCtrl* ctrl = grid->GetEditorTextCtrl();
1875
1876 if( ctrl )
1877 {
1878 wxRect ctrlRect = ctrl->GetScreenRect();
1879 wxRect gridRect = grid->GetScreenRect();
1880
1881 if( ctrlRect.GetTop() < gridRect.GetTop() || ctrlRect.GetBottom() > gridRect.GetBottom() )
1882 grid->ClearSelection();
1883 }
1884#endif
1885}
1886
1887
1888template <typename T>
1889void DIALOG_SIM_MODEL<T>::adjustParamGridColumns( int aWidth, bool aForce )
1890{
1891 wxPropertyGrid* grid = m_paramGridMgr->GetGrid();
1892 int margin = 15;
1893 int indent = 20;
1894
1895 if( aWidth != m_lastParamGridWidth || aForce )
1896 {
1897 m_lastParamGridWidth = aWidth;
1898
1899 grid->FitColumns();
1900
1901 std::vector<int> colWidths;
1902
1903 for( size_t ii = 0; ii < grid->GetColumnCount(); ii++ )
1904 {
1905 if( ii == PARAM_COLUMN::DESCRIPTION )
1906 colWidths.push_back( grid->GetState()->GetColumnWidth( ii ) + margin + indent );
1907 else if( ii == PARAM_COLUMN::VALUE )
1908 colWidths.push_back( std::max( 72, grid->GetState()->GetColumnWidth( ii ) ) + margin );
1909 else
1910 colWidths.push_back( 60 + margin );
1911
1912 aWidth -= colWidths[ ii ];
1913 }
1914
1915 for( size_t ii = 0; ii < grid->GetColumnCount(); ii++ )
1916 grid->SetColumnProportion( ii, colWidths[ ii ] );
1917
1918 grid->ResetColumnSizes();
1919 grid->RefreshEditor();
1920 }
1921}
1922
1923
1924template <typename T>
1926{
1927 adjustParamGridColumns( event.GetSize().GetX(), false );
1928
1929 event.Skip();
1930}
1931
1932
1933template class DIALOG_SIM_MODEL<SCH_SYMBOL>;
1934template class DIALOG_SIM_MODEL<LIB_SYMBOL>;
const char * name
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:106
@ INVALID_BITMAP
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
wxPropertyGridManager * m_paramGridMgr
DIALOG_SIM_MODEL_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &title=_("Simulation Model Editor"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
wxStaticText * m_decompositionLabel
wxStaticText * m_deviceSubtypeLabel
STD_BITMAP_BUTTON * m_browseButton
wxStyledTextCtrl * m_subckt
wxPropertyGridPage * m_paramGrid
wxStyledTextCtrl * m_codePreview
const SIM_MODEL * m_prevModel
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
void onFilterCharHook(wxKeyEvent &aKeyStroke) 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)
SIM_LIB_MGR m_builtinModelsMgr
int getModelPinIndex(const wxString &aModelPinString) const
void onModelFilter(wxCommandEvent &aEvent) override
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_FIELD > & m_fields
std::vector< SCH_PIN * > m_sortedPartPins
Pins of the current part.
wxPGProperty * newParamProperty(SIM_MODEL *aModel, int aParamIndex) const
void onDecompositionModeChoice(wxCommandEvent &aEvent) override
wxPGProperty * m_prevParamGridSelection
void onPinModelCombobox(wxCommandEvent &event) override
const SIM_LIBRARY * library() const
SIM_MODEL & curModel() const
SIM_LIB_MGR m_libraryModelsMgr
void onBrowseButtonClick(wxCommandEvent &aEvent) override
void onPinComboboxTextEnter(wxCommandEvent &event) override
void onPinCombobox(wxCommandEvent &event) override
wxPGProperty * m_firstCategory
SCINTILLA_TRICKS * m_scintillaTricksCode
wxString m_inferredValueRestore
std::map< wxString, int > m_modelListBoxEntryToLibraryIdx
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
SIM_MODEL::TYPE m_curModelType
void updateModelCodeTab(SIM_MODEL *aModel)
void onSizeParamGrid(wxSizeEvent &event) override
void onPageChanging(wxNotebookEvent &event) override
std::vector< EMBEDDED_FILES * > m_filesStack
void updateDecompositionControls()
Show/populate the decomposition controls (multi-unit only) and sync their enabled state.
void onWaveformChoice(wxCommandEvent &aEvent) override
std::map< SIM_MODEL::DEVICE_T, SIM_MODEL::TYPE > m_curModelTypeOfDeviceType
void onPinModelComboboxTextEnter(wxCommandEvent &event) override
EDA_BASE_FRAME * m_frame
void onLibraryPathTextEnter(wxCommandEvent &aEvent) override
static void RestoreInferredValue(std::vector< SCH_FIELD > &aFields, const wxString &aOriginalValue, bool aOverwritten, bool aStoredInValue)
Restore an inferred passive's Value field after WriteFields().
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.
virtual const wxString What() const
A composite of Problem() and Where()
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
Define a library symbol object.
Definition lib_symbol.h:114
void AppendParentEmbeddedFiles(std::vector< EMBEDDED_FILES * > &aStack) const
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:267
virtual const wxString GetProjectPath() const
Return the full path of the project.
Definition project.cpp:277
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:73
virtual bool HasMessageOfSeverity(int aSeverityMask) const
Returns true if the reporter has one or more messages matching the specified severity mask.
Definition reporter.h:152
EMBEDDED_FILES * GetEmbeddedFiles() override
Schematic editor (Eeschema) main window.
virtual const wxString & GetText() const override
Return the string associated with the text object.
Definition sch_field.h:139
void SetText(const wxString &aText) override
SCHEMATIC * Schematic() const
Search the item hierarchy to find a SCHEMATIC.
Definition sch_item.cpp:302
const wxString & GetShownNumber() const
Definition sch_pin.cpp:711
Schematic symbol object.
Definition sch_symbol.h:76
std::unique_ptr< LIB_SYMBOL > & GetLibSymbolRef()
Definition sch_symbol.h:185
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:31
static constexpr auto NAME_FIELD
Definition sim_library.h:32
bool SetIbisModel(const SIM_LIBRARY_IBIS &aLib, const std::string &aPinNumber, const std::string &aModelName)
Bind to a KIBIS model, set IO mode from its type, manage sw_state.
std::vector< std::pair< std::string, std::string > > GetIbisPins() const
void SwitchSingleEndedDiff(bool aDiff) override
bool CanDifferential() const
std::vector< std::string > GetIbisModels() const
bool ChangePin(const SIM_LIBRARY_IBIS &aLib, const std::string &aPinNumber)
update the list of available models based on the pin number.
std::string GetComponentName() const
static INFO TypeInfo(TYPE aType)
int GetPinCount() const
Definition sim_model.h:478
static bool InferSimModel(const wxString &aPrefix, std::span< const wxString > aPins, std::vector< SCH_FIELD > *aFields, bool aResolve, int aDepth, SIM_VALUE_GRAMMAR::NOTATION aNotation, wxString *aDeviceType, wxString *aModelType, wxString *aModelParams, wxString *aPinMap)
aPins follows lexical symbol-pin number order, including duplicate numbers.
const SPICE_GENERATOR & SpiceGenerator() const
Definition sim_model.h:429
virtual const PARAM & GetParam(unsigned aParamIndex) const
static TYPE ReadTypeFromFields(const std::vector< SCH_FIELD > &aFields, bool aResolve, int aDepth, REPORTER &aReporter)
int GetParamCount() const
Definition sim_model.h:488
void AssignSymbolPinNumberToModelPin(int aPinIndex, const wxString &aSymbolPinNumber)
DEVICE_INFO GetDeviceInfo() const
Definition sim_model.h:467
DEVICE_T GetDeviceType() const
Definition sim_model.h:470
static DEVICE_INFO DeviceInfo(DEVICE_T aDeviceType)
Definition sim_model.cpp:58
virtual bool HasAutofill() const
Definition sim_model.h:505
const SIM_MODEL_PIN & GetPin(unsigned aIndex) const
Definition sim_model.h:479
virtual bool HasPrimaryValue() const
Definition sim_model.h:506
TYPE GetType() const
Definition sim_model.h:471
static std::vector< wxString > PinNumbers(std::span< const SCH_PIN *const > aPins)
const SIM_MODEL::PARAM & GetParam() const
@ TYPE_FLOAT_VECTOR
Definition sim_value.h:71
@ TYPE_BOOL_VECTOR
Definition sim_value.h:69
@ TYPE_INT_VECTOR
Definition sim_value.h:70
@ TYPE_COMPLEX_VECTOR
Definition sim_value.h:72
@ TYPE_STRING
Definition sim_value.h:67
@ TYPE_COMPLEX
Definition sim_value.h:66
Special netlist exporter flavor that allows one to override simulation commands.
virtual std::string Preview(const SPICE_ITEM &aItem) const
A wrapper for reporting to a wxString object.
Definition reporter.h:242
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition confirm.cpp:217
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_REFRESH_FROM_MODEL
SIM_MODEL::PARAM::CATEGORY CATEGORY
#define _(s)
@ NONE
Definition eda_fill.h:42
static const std::string KiCadUriPrefix
PROJECT & Prj()
Definition kicad.cpp:730
static bool loadLibrary(SYMBOL_LIBRARY_ADAPTER &aAdapter, const wxString &aNickname, std::vector< KI_ERROR > &aErrors)
wxSize GetUnobscuredSize(const wxWindow *aWindow)
Tries to determine the size of the viewport of a scrollable widget (wxDataViewCtrl,...
Definition wxgtk/ui.cpp:496
void AllowNetworkFileSystems(wxDialog *aDialog)
Configure a file dialog to show network and virtual file systems.
Definition wxgtk/ui.cpp:741
KICOMMON_API wxFont GetStatusFont(wxWindow *aWindow)
BITMAPS PinShapeGetBitmap(GRAPHIC_PINSHAPE shape)
Definition pin_type.cpp:246
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_UNDEFINED
void SetFieldValue(std::vector< SCH_FIELD > &aFields, const wxString &aFieldName, const std::string &aValue, bool aIsVisible=true, const SCH_SHEET_PATH *aSheetPath=nullptr, const wxString &aVariantName=wxEmptyString)
Definition sch_field.h:453
wxString GetFieldValue(const std::vector< SCH_FIELD > *aFields, FIELD_T aFieldType)
Definition sch_field.h:428
const SCH_FIELD * FindField(const std::vector< SCH_FIELD > &aFields, FIELD_T aFieldId)
Definition sch_field.h:390
#define SIM_PINS_FIELD
Definition sim_model.h:51
#define SIM_DEVICE_FIELD
Definition sim_model.h:49
#define SIM_REFERENCE_FIELD
Definition sim_model.h:46
#define SIM_PARAMS_FIELD
Definition sim_model.h:53
#define SIM_DECOMPOSITION_FIELD
Definition sim_model.h:52
#define SIM_DEVICE_SUBTYPE_FIELD
Definition sim_model.h:50
std::vector< FAB_LAYER_COLOR > dummy
int StrNumCmp(const wxString &aString1, const wxString &aString2, CMP_T aCaseSensitivity)
Compare two strings with alphanumerical content.
@ IGNORE_CASE
Per-component decomposition descriptor stored in the Sim.Decomposition field.
static SIM_DECOMPOSITION Parse(const wxString &aField)
std::vector< wxString > sharedModelPins
wxString Format() const
std::vector< std::string > enumValues
Definition sim_model.h:386
SIM_VALUE::TYPE type
Definition sim_model.h:377
std::string defaultValue
Definition sim_model.h:380
std::string description
Definition sim_model.h:381
std::string value
Definition sim_model.h:398
const INFO & info
Definition sim_model.h:399
static constexpr auto NOT_CONNECTED
Definition sim_model.h:71
const std::string modelPinName
Definition sim_model.h:68
wxString symbolPinNumber
Definition sim_model.h:69
std::string modelName
#define ALL_UNITS
Definition symbol.h:150
#define ALL_BODY_STYLES
Definition symbol.h:151
@ REFERENCE
Field Reference of part, i.e. "IC21".
@ VALUE
Field Value of part, i.e. "3.3K".
std::string subckt
std::string path
IbisParser parser & reporter
KIBIS_MODEL * model
KIBIS_PIN * pin
wxString result
Test unit parsing edge cases and error handling.