1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
|
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2018 Jean-Pierre Charras, jp.charras at wanadoo.fr
* Copyright (C) 1992-2018 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
/**
* @file eeschema/dialogs/dialog_bom.cpp
* @brief Dialog box for creating bom and other documents from generic netlist.
*/
#include <fctsys.h>
#include <pgm_base.h>
#include <kiface_i.h>
#include <confirm.h>
#include <gestfich.h>
#include <sch_edit_frame.h>
#include <wx/dynarray.h>
#include <netlist.h>
#include <netlist_exporter_generic.h>
#include <sch_sheet.h>
#include <invoke_sch_dialog.h>
#include <dialog_helpers.h>
#include <dialog_bom_base.h>
#include <html_messagebox.h>
#include <reporter.h>
#define BOM_PLUGINS_KEY wxT("bom_plugins")
#define BOM_PLUGIN_SELECTED_KEY wxT("bom_plugin_selected")
const char * s_bomHelpInfo =
#include <dialog_bom_help_html.h>
;
#include <dialog_bom_cfg_lexer.h>
using namespace T_BOMCFG_T;
/**
* Structure BOM_PLUGIN
* holds data of the BOM plugin.
*/
struct BOM_PLUGIN
{
wxString Name;
wxString Command;
wxArrayString Options;
};
/**
* Define wxArray of BOM_PLUGIN.
*/
WX_DECLARE_OBJARRAY( BOM_PLUGIN, BOM_PLUGIN_ARRAY );
#include <wx/arrimpl.cpp>
WX_DEFINE_OBJARRAY( BOM_PLUGIN_ARRAY )
/**
* Class BOM_CFG_READER_PARSER
* holds data and functions pertinent to parsing a S-expression file
* for a WORKSHEET_LAYOUT.
*/
class BOM_CFG_READER_PARSER : public DIALOG_BOM_CFG_LEXER
{
BOM_PLUGIN_ARRAY* m_pluginsList;
public:
BOM_CFG_READER_PARSER( BOM_PLUGIN_ARRAY* aPlugins,
const char* aData, const wxString& aSource );
void Parse();
private:
void parsePlugin();
};
// PCB_PLOT_PARAMS_PARSER
BOM_CFG_READER_PARSER::BOM_CFG_READER_PARSER( BOM_PLUGIN_ARRAY* aPlugins,
const char* aLine,
const wxString& aSource ) :
DIALOG_BOM_CFG_LEXER( aLine, aSource )
{
m_pluginsList = aPlugins;
}
void BOM_CFG_READER_PARSER::Parse()
{
T token;
while( ( token = NextTok() ) != T_RIGHT )
{
if( token == T_EOF)
break;
if( token == T_LEFT )
token = NextTok();
if( token == T_plugins )
continue;
switch( token )
{
case T_plugin: // Defines a new plugin
parsePlugin();
break;
default:
// Unexpected( CurText() );
break;
}
}
}
void BOM_CFG_READER_PARSER::parsePlugin()
{
BOM_PLUGIN plugin;
NeedSYMBOLorNUMBER();
plugin.Name = FromUTF8();
T token;
while( ( token = NextTok() ) != T_RIGHT )
{
if( token == T_EOF)
break;
switch( token )
{
case T_LEFT:
break;
case T_cmd:
NeedSYMBOLorNUMBER();
plugin.Command = FromUTF8();
NeedRIGHT();
break;
case T_opts:
NeedSYMBOLorNUMBER();
plugin.Options.Add( FromUTF8() );
NeedRIGHT();
break;
default:
Unexpected( CurText() );
break;
}
}
if( ! plugin.Name.IsEmpty() )
m_pluginsList->Add( plugin );
}
// The main dialog frame to run scripts to build bom
class DIALOG_BOM : public DIALOG_BOM_BASE
{
private:
SCH_EDIT_FRAME* m_parent;
BOM_PLUGIN_ARRAY m_plugins;
wxConfigBase* m_config; // to store the "plugins"
public:
// Constructor and destructor
DIALOG_BOM( SCH_EDIT_FRAME* parent );
~DIALOG_BOM();
private:
void OnPluginSelected( wxCommandEvent& event ) override;
void OnRunPlugin( wxCommandEvent& event ) override;
void OnCancelClick( wxCommandEvent& event ) override;
void OnHelp( wxCommandEvent& event ) override;
void OnAddPlugin( wxCommandEvent& event ) override;
void OnRemovePlugin( wxCommandEvent& event ) override;
void OnEditPlugin( wxCommandEvent& event ) override;
void OnCommandLineEdited( wxCommandEvent& event ) override;
void OnNameEdited( wxCommandEvent& event ) override;
void OnShowConsoleChanged( wxCommandEvent& event ) override;
void pluginInit();
void installPluginsList();
/**
* @return the Plugin filename from a command line
* @param aCommand = the command line
*/
wxString getPluginFileName( const wxString& aCommand );
/**
* display (when exists) the text found between the keyword "@package"
* (compatible with doxygen comments)
* and the end of comment block (""" in python", --> in xml)
*/
void displayPluginInfo( FILE * aFile, const wxString& aFilename );
/**
* Browse plugin files, and set m_CommandStringCtrl field
* @return a command line ro run the plugin
*/
wxString choosePlugin();
};
// Create and show DIALOG_BOM.
int InvokeDialogCreateBOM( SCH_EDIT_FRAME* aCaller )
{
DIALOG_BOM dlg( aCaller );
return dlg.ShowModal();
}
DIALOG_BOM::DIALOG_BOM( SCH_EDIT_FRAME* parent ) :
DIALOG_BOM_BASE( parent )
{
m_parent = parent;
m_config = Kiface().KifaceSettings();
installPluginsList();
#ifndef __WINDOWS__
m_checkBoxShowConsole->Show( false );
#endif
// Now all widgets have the size fixed, call FinishDialogSettings
FinishDialogSettings();
}
DIALOG_BOM::~DIALOG_BOM()
{
// Save the plugin descriptions in config.
// the config stores only one string.
// plugins are saved inside a S expr:
// ( plugins
// ( plugin "plugin name 1" (cmd "command line 1") )
// ( plugin "plugin name 2" (cmd "command line 2") (opts "option1") (opts "option2") )
// ....
// )
STRING_FORMATTER writer;
writer.Print( 0, "(plugins" );
for( unsigned ii = 0; ii < m_plugins.GetCount(); ii++ )
{
writer.Print( 1, "(plugin %s (cmd %s)",
writer.Quotew( m_plugins.Item( ii ).Name ).c_str(),
writer.Quotew( m_plugins.Item( ii ).Command ).c_str() );
for( unsigned jj = 0; jj < m_plugins.Item( ii ).Options.GetCount(); jj++ )
{
writer.Print( 1, "(opts %s)",
writer.Quotew( m_plugins.Item( ii ).Options.Item( jj ) ).c_str() );
}
writer.Print( 0, ")" );
}
writer.Print( 0, ")" );
wxString list( FROM_UTF8( writer.GetString().c_str() ) );
m_config->Write( BOM_PLUGINS_KEY, list );
wxString active_plugin_name = m_lbPlugins->GetStringSelection( );
m_config->Write( BOM_PLUGIN_SELECTED_KEY, active_plugin_name );
}
/* Read the initialized plugins in config and fill the list
* of names
*/
void DIALOG_BOM::installPluginsList()
{
wxString list, active_plugin_name;
m_config->Read( BOM_PLUGINS_KEY, &list );
m_config->Read( BOM_PLUGIN_SELECTED_KEY, &active_plugin_name );
if( !list.IsEmpty() )
{
BOM_CFG_READER_PARSER cfg_parser( &m_plugins, TO_UTF8( list ),
wxT( "plugins" ) );
try
{
cfg_parser.Parse();
}
catch( const IO_ERROR& )
{
// wxLogMessage( ioe.What() );
}
}
// Populate list box
for( unsigned ii = 0; ii < m_plugins.GetCount(); ii++ )
{
m_lbPlugins->Append( m_plugins.Item( ii ).Name );
if( active_plugin_name == m_plugins.Item( ii ).Name )
m_lbPlugins->SetSelection( ii );
}
pluginInit();
}
void DIALOG_BOM::OnPluginSelected( wxCommandEvent& event )
{
pluginInit();
}
#include <wx/ffile.h>
void DIALOG_BOM::pluginInit()
{
int ii = m_lbPlugins->GetSelection();
if( ii < 0 )
{
m_textCtrlName->SetValue( wxEmptyString );
m_textCtrlCommand->SetValue( wxEmptyString );
return;
}
m_textCtrlName->SetValue( m_plugins.Item( ii ).Name );
m_textCtrlCommand->SetValue( m_plugins.Item( ii ).Command );
#ifdef __WINDOWS__
if( m_plugins.Item( ii ).Options.Index( wxT( "show_console" ) ) == wxNOT_FOUND )
m_checkBoxShowConsole->SetValue( false );
else
m_checkBoxShowConsole->SetValue( true );
#endif
wxString pluginName = getPluginFileName( m_textCtrlCommand->GetValue() );
if( pluginName.IsEmpty() )
return;
FILE* pluginFile = wxFopen( pluginName, "rt" );
if( pluginFile == NULL )
{
wxString msg;
msg.Printf( _( "Failed to open file \"%s\"" ), GetChars( pluginName ) );
DisplayError( this, msg );
return;
}
displayPluginInfo( pluginFile, pluginName );
}
void DIALOG_BOM::displayPluginInfo( FILE * aFile, const wxString& aFilename )
{
m_Messages->Clear();
// display (when exists) the text found between the keyword "@package"
// (compatible with doxygen comments)
// and the end of comment block (""" in python", --> in xml)
wxString data;
wxFFile fdata( aFile ); // dtor will close the file
if( !fdata.ReadAll( &data ) )
return;
wxString header( wxT( "@package" ) );
wxString endsection( wxT( "-->" ) ); // For xml
wxFileName fn( aFilename );
if( fn.GetExt().IsSameAs( wxT("py"), false ) )
endsection = wxT( "\"\"\"" );
else if( !fn.GetExt().IsSameAs( wxT("xsl"), false ) )
// If this is not a python file, we know nothing about file
// and the info cannot be found
return;
// Extract substring between @package and """
int strstart = data.Find( header );
if( strstart == wxNOT_FOUND )
return;
strstart += header.Length();
int strend = data.find( endsection, strstart );
if( strend == wxNOT_FOUND)
return;
// Remove empty line if any
while( data[strstart] < ' ' )
strstart++;
m_Messages->SetValue( data.SubString( strstart, strend-1 ) );
}
/**
* Function RunPlugin
* run the plugin command line
*/
void DIALOG_BOM::OnRunPlugin( wxCommandEvent& event )
{
wxFileName fn;
// Calculate the xml netlist filename
fn = g_RootSheet->GetScreen()->GetFileName();
fn.SetPath( wxPathOnly( Prj().GetProjectFullName() ) );
fn.ClearExt();
wxString fullfilename = fn.GetFullPath();
m_parent->ClearMsgPanel();
wxString reportmsg;
WX_STRING_REPORTER reporter( &reportmsg );
m_parent->SetNetListerCommand( m_textCtrlCommand->GetValue() );
#ifdef __WINDOWS__
if( m_checkBoxShowConsole->IsChecked() )
m_parent->SetExecFlags( wxEXEC_SHOW_CONSOLE );
#endif
m_parent->CreateNetlist( -1, fullfilename, 0, &reporter, false );
m_Messages->SetValue( reportmsg );
}
void DIALOG_BOM::OnCancelClick( wxCommandEvent& event )
{
EndModal( wxID_CANCEL );
}
/**
* Function OnRemovePlugin
* Remove a plugin from the list
*/
void DIALOG_BOM::OnRemovePlugin( wxCommandEvent& event )
{
int ii = m_lbPlugins->GetSelection();
if( ii < 0 )
return;
m_lbPlugins->Delete( ii );
m_plugins.RemoveAt( ii );
// Select the next item, if exists
if( (int)m_lbPlugins->GetCount() >= ii )
ii = m_lbPlugins->GetCount() - 1;
if( ii >= 0 )
m_lbPlugins->SetSelection( ii );
pluginInit();
}
/**
* Function OnAddPlugin
* Add a new panel for a new netlist plugin
*/
void DIALOG_BOM::OnAddPlugin( wxCommandEvent& event )
{
wxString cmdLine = choosePlugin();
BOM_PLUGIN newPlugin;
if( cmdLine.IsEmpty() )
return;
// Creates a new plugin entry
wxFileName fn( getPluginFileName( cmdLine ) );
wxString defaultName = fn.GetName();
wxString name = wxGetTextFromUser( _("Plugin name in plugin list") ,
_("Plugin name"), defaultName );
if( name.IsEmpty() )
return;
// Verify if it does not exists
for( unsigned ii = 0; ii < m_plugins.GetCount(); ii++ )
{
if( name == m_plugins.Item( ii ).Name )
{
wxMessageBox( _("This name already exists. Abort") );
return;
}
}
// Append the new plugin
newPlugin.Name = name;
newPlugin.Command = wxEmptyString;
m_plugins.Add( newPlugin );
m_lbPlugins->SetSelection( m_lbPlugins->GetCount() - 1 );
m_lbPlugins->Append( name );
m_lbPlugins->SetSelection( m_lbPlugins->GetCount() - 1 );
m_textCtrlCommand->SetValue( cmdLine );
pluginInit();
}
/*
* Browse plugin files, and set m_CommandStringCtrl field
*/
wxString DIALOG_BOM::choosePlugin()
{
wxString mask = wxT( "*" );
#ifndef __WXMAC__
wxString path = Pgm().GetExecutablePath();
#else
wxString path = GetOSXKicadDataDir() + wxT( "/plugins" );
#endif
wxString fullFileName = EDA_FILE_SELECTOR( _( "Plugin files:" ),
path,
wxEmptyString,
wxEmptyString,
mask,
this,
wxFD_OPEN,
true );
if( fullFileName.IsEmpty() )
return wxEmptyString;
// Creates a default command line,
// suitable to run the external tool xslproc or python
// The default command line depending on plugin extension, currently
// "xsl" or "exe" or "py" or "pyw"
wxString cmdLine;
wxFileName fn( fullFileName );
wxString ext = fn.GetExt();
// Python requires on Windows the path of the script ends by '/' instead of '\'
// otherwise import does not find modules in the same folder as the python script
// We cannot change all '\' to '/' in command line because it causes issues with files
// that are stored on network resources and pointed to using the Windows
// Universal Naming Convention format. (full filename starting by \\server\)
//
// I hope changing the last separator only to '/' will work.
#ifdef __WINDOWS__
if( ext == wxT("py" ) || ext == wxT("pyw" ) )
{
wxString sc_path = fn.GetPathWithSep();
sc_path.RemoveLast();
fullFileName = sc_path +'/' + fn.GetFullName();;
}
#endif
if( ext == "xsl" )
cmdLine.Printf( "xsltproc -o \"%%O\" \"%s\" \"%%I\"", GetChars( fullFileName ) );
else if( ext == "exe" )
cmdLine.Printf( "\"%s\" < \"%%I\" > \"%%O\"", GetChars( fullFileName ) );
else if( ext == "py" )
cmdLine.Printf( "python \"%s\" \"%%I\" \"%%O\"", GetChars( fullFileName ) );
else if( ext == "pyw" )
#ifdef __WINDOWS__
cmdLine.Printf( "pythonw \"%s\" \"%%I\" \"%%O\"", GetChars( fullFileName ) );
#else
cmdLine.Printf( "python \"%s\" \"%%I\" \"%%O\"", GetChars( fullFileName ) );
#endif
else
cmdLine.Printf( "\"%s\"", GetChars( fullFileName ) );
return cmdLine;
}
wxString DIALOG_BOM::getPluginFileName( const wxString& aCommand )
{
wxString pluginName;
// Try to find the plugin name.
// This is possible if the name ends by .py or .pyw or .xsl or .exe
int pos = -1;
if( (pos = aCommand.Find( wxT(".pyw") )) != wxNOT_FOUND )
pos += 3;
else if( (pos = aCommand.Find( wxT(".py") )) != wxNOT_FOUND )
pos += 2;
else if( (pos = aCommand.Find( wxT(".xsl") )) != wxNOT_FOUND )
pos += 3;
else if( (pos = aCommand.Find( wxT(".exe") )) != wxNOT_FOUND )
pos += 3;
// the end of plugin name is at position pos.
if( pos > 0 )
{
// Be sure this is the end of the name: the next char is " or space
int eos = aCommand[pos+1];
if( eos == ' '|| eos == '\"' )
{
// search for the starting point of the name
int jj = pos-1;
while( jj >= 0 )
if( aCommand[jj] != eos )
jj--;
else
break;
// extract the name
if( jj >= 0 )
{
eos = aCommand[jj];
if( eos == ' '|| eos == '\"' ) // do not include delimiters
jj++;
pluginName = aCommand.SubString( jj, pos );
}
}
}
// Using a format like %P is possible in plugin name, so expand it
wxString prj_dir = Prj().GetProjectPath();
if( prj_dir.EndsWith( '/' ) || prj_dir.EndsWith( '\\' ) )
prj_dir.RemoveLast();
pluginName.Replace( wxT( "%P" ), prj_dir.GetData(), true );
return pluginName;
}
void DIALOG_BOM::OnEditPlugin( wxCommandEvent& event )
{
wxString pluginName = getPluginFileName( m_textCtrlCommand->GetValue() );
if( pluginName.Length() <= 2 ) // if name != ""
{
wxMessageBox( _("Plugin file name not found. Cannot edit plugin file") );
return;
}
AddDelimiterString( pluginName );
wxString editorname = Pgm().GetEditorName();
if( !editorname.IsEmpty() )
ExecuteFile( this, editorname, pluginName );
else
wxMessageBox( _("No text editor selected in KiCad. Please choose it") );
}
void DIALOG_BOM::OnHelp( wxCommandEvent& event )
{
HTML_MESSAGE_BOX help_Dlg( this, _("Bom Generation Help") );
help_Dlg.SetDialogSizeInDU( 500, 350 );
wxString msg = FROM_UTF8(s_bomHelpInfo);
help_Dlg.m_htmlWindow->AppendToPage( msg );
help_Dlg.ShowModal();
}
void DIALOG_BOM::OnCommandLineEdited( wxCommandEvent& event )
{
int ii = m_lbPlugins->GetSelection();
if( ii < 0 )
return;
m_plugins.Item( ii ).Command = m_textCtrlCommand->GetValue();
}
void DIALOG_BOM::OnNameEdited( wxCommandEvent& event )
{
int ii = m_lbPlugins->GetSelection();
if( ii < 0 )
return;
m_plugins.Item( ii ).Name = m_textCtrlName->GetValue();
m_lbPlugins->SetString( ii, m_plugins.Item( ii ).Name );
}
void DIALOG_BOM::OnShowConsoleChanged( wxCommandEvent& event )
{
#ifdef __WINDOWS__
int ii = m_lbPlugins->GetSelection();
if( ii < 0 )
return;
if( m_checkBoxShowConsole->IsChecked() )
{
if( m_plugins.Item( ii ).Options.Index( wxT( "show_console" ) ) == wxNOT_FOUND )
m_plugins.Item( ii ).Options.Add( wxT( "show_console" ) );
}
else
{
m_plugins.Item( ii ).Options.Remove( wxT( "show_console" ) );
}
#endif
}
|