From 4a56ee42c516dfe1624d7d50d4f236c6373edc1f Mon Sep 17 00:00:00 2001 From: jean-pierre charras Date: Wed, 2 Sep 2015 15:55:36 +0200 Subject: [PATCH] Footprint wizard: Fix IO exception error in python scripts which happens sometimes, due to the fact these scripts printed messages to a console, but when Pcbnew is not run from a console, there is no IO channel to read these messages. The fix is not perfect (still no IO channel to read these messages) , but it works better. Also, fix erroneous comments in code, and cleanup code. --- pcbnew/footprint_wizard.cpp | 56 ++++++++++++------ pcbnew/footprint_wizard_frame.cpp | 59 ++++++++++--------- pcbnew/footprint_wizard_frame.h | 10 +++- pcbnew/scripting/pcbnew_footprint_wizards.cpp | 10 ++-- pcbnew/scripting/pcbnew_footprint_wizards.h | 3 +- .../plugins/HelpfulFootprintWizardPlugin.py | 8 +++ scripting/kicadplugins.i | 14 ++++- 7 files changed, 104 insertions(+), 56 deletions(-) diff --git a/pcbnew/footprint_wizard.cpp b/pcbnew/footprint_wizard.cpp index 90159f28c1..c334fd7593 100644 --- a/pcbnew/footprint_wizard.cpp +++ b/pcbnew/footprint_wizard.cpp @@ -120,7 +120,7 @@ void FOOTPRINT_WIZARD_FRAME::ReloadFootprint() FOOTPRINT_WIZARD* FOOTPRINT_WIZARD_FRAME::GetMyWizard() { - if( m_wizardName.Length()==0 ) + if( m_wizardName.Length() == 0 ) return NULL; FOOTPRINT_WIZARD* footprintWizard = FOOTPRINT_WIZARDS::GetWizard( m_wizardName ); @@ -182,10 +182,6 @@ void FOOTPRINT_WIZARD_FRAME::SelectCurrentWizard( wxCommandEvent& event ) } -/** - * Function SelectCurrentFootprint - * Selects the current footprint name and display it - */ void FOOTPRINT_WIZARD_FRAME::ParametersUpdated( wxGridEvent& event ) { int page = m_pageList->GetSelection(); @@ -195,21 +191,29 @@ void FOOTPRINT_WIZARD_FRAME::ParametersUpdated( wxGridEvent& event ) if( !footprintWizard ) return; - if( page<0 ) + if( page < 0 ) return; - int n = m_parameterGrid->GetNumberRows(); - wxArrayString arr; + wxArrayString prmValues = footprintWizard->GetParameterValues( page ); wxArrayString ptList = footprintWizard->GetParameterTypes( page ); - for( int i = 0; i < n; i++ ) + bool has_changed = false; + int count = m_parameterGrid->GetNumberRows(); + + // Skip extra event, useless + if( event.GetString() == m_parameterGrid->GetCellValue( event.GetRow(), m_columnPrmValue ) ) + return; + + for( int prm_id = 0; prm_id < count; ++prm_id ) { - wxString value = m_parameterGrid->GetCellValue( i, 1 ); + wxString value = m_parameterGrid->GetCellValue( prm_id, m_columnPrmValue ); // if this parameter is expected to be an internal // unit convert it back from the user format - if( ptList[i]==wxT( "IU" ) ) + if( ptList[prm_id]==wxT( "IU" ) ) { + // If our locale is set to use, for decimal point, just change it + // to be scripting compatible LOCALE_IO toggle; double dValue; @@ -220,18 +224,34 @@ void FOOTPRINT_WIZARD_FRAME::ParametersUpdated( wxGridEvent& event ) dValue = dValue / 1000.0; dValue = From_User_Unit( g_UserUnit, dValue ); - value.Printf( wxT( "%f" ), dValue ); + + // Internal units are int. Print them as int. + value.Printf( "%d", KiROUND( dValue ) ); + + if( prmValues[prm_id].EndsWith(".0") ) + { + prmValues[prm_id].RemoveLast(); + prmValues[prm_id].RemoveLast(); + } } - // If our locale is set to use , for decimal point, just change it - // to be scripting compatible - arr.Add( value ); + if( prmValues[prm_id] != value ) + { + has_changed = true; + prmValues[prm_id] = value; + } } - wxString res = footprintWizard->SetParameterValues( page, arr ); + if( has_changed ) + { + wxString res = footprintWizard->SetParameterValues( page, prmValues ); - ReloadFootprint(); - DisplayWizardInfos(); + if( !res.IsEmpty() ) + wxMessageBox( res ); + + ReloadFootprint(); + DisplayWizardInfos(); + } } diff --git a/pcbnew/footprint_wizard_frame.cpp b/pcbnew/footprint_wizard_frame.cpp index 583640e5d9..5fe317176f 100644 --- a/pcbnew/footprint_wizard_frame.cpp +++ b/pcbnew/footprint_wizard_frame.cpp @@ -75,22 +75,16 @@ BEGIN_EVENT_TABLE( FOOTPRINT_WIZARD_FRAME, EDA_DRAW_FRAME ) // listbox events EVT_LISTBOX( ID_FOOTPRINT_WIZARD_PAGE_LIST, FOOTPRINT_WIZARD_FRAME::ClickOnPageList ) - - -#if wxCHECK_VERSION( 3, 0, 0 ) EVT_GRID_CMD_CELL_CHANGED( ID_FOOTPRINT_WIZARD_PARAMETER_LIST, FOOTPRINT_WIZARD_FRAME::ParametersUpdated ) -#else - EVT_GRID_CMD_CELL_CHANGE( ID_FOOTPRINT_WIZARD_PARAMETER_LIST, - FOOTPRINT_WIZARD_FRAME::ParametersUpdated ) -#endif - - EVT_GRID_CMD_EDITOR_HIDDEN( ID_FOOTPRINT_WIZARD_PARAMETER_LIST, - FOOTPRINT_WIZARD_FRAME::ParametersUpdated ) EVT_MENU( ID_SET_RELATIVE_OFFSET, FOOTPRINT_WIZARD_FRAME::OnSetRelativeOffset ) END_EVENT_TABLE() +// Column index to display parameters in m_parameterGrid +int FOOTPRINT_WIZARD_FRAME::m_columnPrmName = 0; +int FOOTPRINT_WIZARD_FRAME::m_columnPrmValue = 1; +int FOOTPRINT_WIZARD_FRAME::m_columnPrmUnit = 2; #define FOOTPRINT_WIZARD_FRAME_NAME wxT( "FootprintWizard" ) @@ -131,6 +125,14 @@ FOOTPRINT_WIZARD_FRAME::FOOTPRINT_WIZARD_FRAME( KIWAY* aKiway, LoadSettings( config() ); SetSize( m_FramePos.x, m_FramePos.y, m_FrameSize.x, m_FrameSize.y ); + + // Set some display options here, because the FOOTPRINT_WIZARD_FRAME + // does not have a config menu to do that: + DISPLAY_OPTIONS* disp_opts = (DISPLAY_OPTIONS*) GetDisplayOptions(); + disp_opts->m_DisplayPadIsol = false; + disp_opts->m_DisplayPadNum = true; + GetBoard()->SetElementVisibility( PCB_VISIBLE(NO_CONNECTS_VISIBLE), false ); + GetScreen()->SetGrid( ID_POPUP_GRID_LEVEL_1000 + m_LastGridSizeId ); ReCreateHToolbar(); @@ -147,9 +149,9 @@ FOOTPRINT_WIZARD_FRAME::FOOTPRINT_WIZARD_FRAME( KIWAY* aKiway, m_parameterGrid->CreateGrid( 1, 3 ); // Columns - m_parameterGrid->SetColLabelValue( 0, _( "Parameter" ) ); - m_parameterGrid->SetColLabelValue( 1, _( "Value" ) ); - m_parameterGrid->SetColLabelValue( 2, _( "Units" ) ); + m_parameterGrid->SetColLabelValue( m_columnPrmName, _( "Parameter" ) ); + m_parameterGrid->SetColLabelValue( m_columnPrmValue, _( "Value" ) ); + m_parameterGrid->SetColLabelValue( m_columnPrmUnit, _( "Units" ) ); m_parameterGrid->SetColLabelAlignment( wxALIGN_LEFT, wxALIGN_CENTRE ); m_parameterGrid->AutoSizeColumns(); @@ -319,14 +321,13 @@ void FOOTPRINT_WIZARD_FRAME::ReCreateParameterList() m_parameterGrid->DeleteRows( 0, m_parameterGrid->GetNumberRows() ); m_parameterGrid->AppendRows( fpList.size() ); - wxString name, value, units; - for( unsigned int i = 0; iSetCellValue( i, 0, name ); - m_parameterGrid->SetReadOnly( i, 0 ); + m_parameterGrid->SetCellValue( i, m_columnPrmName, fpList[i] ); + m_parameterGrid->SetReadOnly( i, m_columnPrmName ); if( ptList[i]==wxT( "IU" ) ) { @@ -350,6 +351,8 @@ void FOOTPRINT_WIZARD_FRAME::ReCreateParameterList() units = wxT( "mm" ); } + // Use Double2Str to build the string, because useless trailing 0 + // are removed. The %f format does not remove them std::string s = Double2Str( dValue ); value = FROM_UTF8( s.c_str() ); } @@ -358,9 +361,9 @@ void FOOTPRINT_WIZARD_FRAME::ReCreateParameterList() units = wxT( "" ); } - m_parameterGrid->SetCellValue( i, 1, value ); - m_parameterGrid->SetCellValue( i, 2, units ); - m_parameterGrid->SetReadOnly( i, 2 ); + m_parameterGrid->SetCellValue( i, m_columnPrmValue, value ); + m_parameterGrid->SetCellValue( i, m_columnPrmUnit, units ); + m_parameterGrid->SetReadOnly( i, m_columnPrmUnit ); } m_parameterGrid->AutoSizeColumns(); @@ -596,14 +599,11 @@ void FOOTPRINT_WIZARD_FRAME::ReCreateHToolbar() m_mainToolBar->AddTool( ID_ZOOM_PAGE, wxEmptyString, KiBitmap( zoom_fit_in_page_xpm ), msg ); - if( IsModal() ) - { - // The library browser is called from a "load component" command - m_mainToolBar->AddSeparator(); - m_mainToolBar->AddTool( ID_FOOTPRINT_WIZARD_DONE, - wxEmptyString, KiBitmap( export_footprint_names_xpm ), - _( "Add footprint to board" ) ); - } + // The footprint wizard always can export the current footprint + m_mainToolBar->AddSeparator(); + m_mainToolBar->AddTool( ID_FOOTPRINT_WIZARD_DONE, + wxEmptyString, KiBitmap( export_footprint_names_xpm ), + _( "Export the footprint to the editor" ) ); // after adding the buttons to the toolbar, must call Realize() to // reflect the changes @@ -616,4 +616,5 @@ void FOOTPRINT_WIZARD_FRAME::ReCreateHToolbar() void FOOTPRINT_WIZARD_FRAME::ReCreateVToolbar() { + // Currently, there is no vertical toolbar } diff --git a/pcbnew/footprint_wizard_frame.h b/pcbnew/footprint_wizard_frame.h index 328b0af19b..815cfa559e 100644 --- a/pcbnew/footprint_wizard_frame.h +++ b/pcbnew/footprint_wizard_frame.h @@ -54,6 +54,11 @@ private: // Flags wxString m_configPath; ///< subpath for configuration + // Column index to display parameters in m_parameterGrid + static int m_columnPrmName; + static int m_columnPrmValue; + static int m_columnPrmUnit; + protected: wxString m_wizardName; ///< name of the current wizard wxString m_wizardDescription; ///< description of the wizard @@ -151,9 +156,12 @@ private: void SelectCurrentWizard( wxCommandEvent& event ); + /** + * Function ParametersUpdated + * Update the footprint python parameters values from the values in grid + */ void ParametersUpdated( wxGridEvent& event ); - bool OnRightClick( const wxPoint& MousePos, wxMenu* PopMenu ); /** diff --git a/pcbnew/scripting/pcbnew_footprint_wizards.cpp b/pcbnew/scripting/pcbnew_footprint_wizards.cpp index 70e2238d34..28b73d5b20 100644 --- a/pcbnew/scripting/pcbnew_footprint_wizards.cpp +++ b/pcbnew/scripting/pcbnew_footprint_wizards.cpp @@ -64,9 +64,11 @@ PyObject* PYTHON_FOOTPRINT_WIZARD::CallMethod( const char* aMethod, PyObject* aA if( PyErr_Occurred() ) { +#if 1 // defined(DEBUG) wxMessageBox( PyErrStringWithTraceback(), wxT( "Exception on python footprint wizard code" ), wxICON_ERROR | wxOK ); +#endif } if( result ) @@ -213,7 +215,7 @@ wxArrayString PYTHON_FOOTPRINT_WIZARD::GetParameterNames( int aPage ) ret = CallRetArrayStrMethod( "GetParameterNames", arglist ); Py_DECREF( arglist ); - for( unsigned i = 0; i