2012-03-17 17:30:03 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 NBEE Embedded Systems, Miguel Angel Ajo <miguelangel@nbee.es>
|
2017-03-17 14:41:00 +00:00
|
|
|
* Copyright (C) 1992-2017 KiCad Developers, see AUTHORS.txt for contributors.
|
2012-03-17 17:30:03 +00:00
|
|
|
*
|
|
|
|
* 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 pcbnew_scripting_helpers.cpp
|
|
|
|
* @brief Scripting helper functions for pcbnew functionality
|
|
|
|
*/
|
|
|
|
|
2012-04-20 21:20:56 +00:00
|
|
|
#include <Python.h>
|
2018-08-05 10:28:10 +00:00
|
|
|
#undef HAVE_CLOCK_GETTIME // macro is defined in Python.h and causes redefine warning
|
2012-03-17 17:30:03 +00:00
|
|
|
|
2019-12-05 14:03:15 +00:00
|
|
|
#include <action_plugin.h>
|
2012-03-17 15:17:13 +00:00
|
|
|
#include <build_version.h>
|
|
|
|
#include <class_board.h>
|
2019-12-05 14:03:15 +00:00
|
|
|
#include <cstdlib>
|
2020-06-05 22:21:41 +00:00
|
|
|
#include <fp_lib_table.h>
|
2012-03-17 15:17:13 +00:00
|
|
|
#include <io_mgr.h>
|
2019-12-05 14:03:15 +00:00
|
|
|
#include <kicad_string.h>
|
2012-04-20 21:20:56 +00:00
|
|
|
#include <macros.h>
|
2017-12-03 10:19:42 +00:00
|
|
|
#include <pcb_draw_panel_gal.h>
|
2019-12-05 14:03:15 +00:00
|
|
|
#include <pcbnew.h>
|
|
|
|
#include <pcbnew_scripting_helpers.h>
|
2020-06-05 22:21:41 +00:00
|
|
|
#include <project.h>
|
2020-05-31 21:42:04 +00:00
|
|
|
#include <settings/settings_manager.h>
|
|
|
|
#include <wildcards_and_files_ext.h>
|
2012-03-17 15:17:13 +00:00
|
|
|
|
2017-08-27 06:04:12 +00:00
|
|
|
static PCB_EDIT_FRAME* s_PcbEditFrame = NULL;
|
2012-03-17 15:17:13 +00:00
|
|
|
|
2020-05-31 21:42:04 +00:00
|
|
|
static SETTINGS_MANAGER* s_SettingsManager = nullptr;
|
|
|
|
|
2013-03-15 16:35:24 +00:00
|
|
|
BOARD* GetBoard()
|
2012-03-17 15:17:13 +00:00
|
|
|
{
|
2017-08-27 06:04:12 +00:00
|
|
|
if( s_PcbEditFrame )
|
|
|
|
return s_PcbEditFrame->GetBoard();
|
2013-03-15 16:35:24 +00:00
|
|
|
else
|
|
|
|
return NULL;
|
2012-03-17 15:17:13 +00:00
|
|
|
}
|
|
|
|
|
2013-03-15 16:35:24 +00:00
|
|
|
|
2017-08-27 06:04:12 +00:00
|
|
|
void ScriptingSetPcbEditFrame( PCB_EDIT_FRAME* aPcbEditFrame )
|
2012-03-17 15:17:13 +00:00
|
|
|
{
|
2017-08-27 06:04:12 +00:00
|
|
|
s_PcbEditFrame = aPcbEditFrame;
|
2012-03-17 15:17:13 +00:00
|
|
|
}
|
|
|
|
|
2013-03-15 16:35:24 +00:00
|
|
|
|
2012-07-31 21:00:33 +00:00
|
|
|
BOARD* LoadBoard( wxString& aFileName )
|
2012-04-14 21:18:27 +00:00
|
|
|
{
|
2013-03-15 16:35:24 +00:00
|
|
|
if( aFileName.EndsWith( wxT( ".kicad_pcb" ) ) )
|
2017-10-03 10:23:52 +00:00
|
|
|
return LoadBoard( aFileName, IO_MGR::KICAD_SEXP );
|
2012-04-22 11:14:58 +00:00
|
|
|
|
2013-03-15 16:35:24 +00:00
|
|
|
else if( aFileName.EndsWith( wxT( ".brd" ) ) )
|
|
|
|
return LoadBoard( aFileName, IO_MGR::LEGACY );
|
2013-01-16 12:38:30 +00:00
|
|
|
|
2012-04-22 11:14:58 +00:00
|
|
|
// as fall back for any other kind use the legacy format
|
2013-03-15 16:35:24 +00:00
|
|
|
return LoadBoard( aFileName, IO_MGR::LEGACY );
|
2012-04-14 21:18:27 +00:00
|
|
|
}
|
2012-03-17 15:17:13 +00:00
|
|
|
|
2013-03-15 16:35:24 +00:00
|
|
|
|
2020-05-31 21:42:04 +00:00
|
|
|
SETTINGS_MANAGER* GetSettingsManager()
|
|
|
|
{
|
|
|
|
if( !s_SettingsManager )
|
2020-06-20 14:36:16 +00:00
|
|
|
s_SettingsManager = new SETTINGS_MANAGER( true );
|
2020-05-31 21:42:04 +00:00
|
|
|
|
|
|
|
return s_SettingsManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PROJECT* GetDefaultProject()
|
|
|
|
{
|
2020-06-20 14:36:16 +00:00
|
|
|
PROJECT* project = GetSettingsManager()->GetProject( "" );
|
2020-05-31 21:42:04 +00:00
|
|
|
|
2020-06-20 14:36:16 +00:00
|
|
|
if( !project )
|
2020-05-31 21:42:04 +00:00
|
|
|
{
|
|
|
|
GetSettingsManager()->LoadProject( "" );
|
|
|
|
project = GetSettingsManager()->GetProject( "" );
|
|
|
|
}
|
|
|
|
|
|
|
|
return project;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-31 21:00:33 +00:00
|
|
|
BOARD* LoadBoard( wxString& aFileName, IO_MGR::PCB_FILE_T aFormat )
|
2012-03-17 15:17:13 +00:00
|
|
|
{
|
2020-05-31 21:42:04 +00:00
|
|
|
wxFileName pro = aFileName;
|
|
|
|
pro.SetExt( ProjectFileExtension );
|
|
|
|
pro.MakeAbsolute();
|
|
|
|
wxString projectPath = pro.GetFullPath();
|
|
|
|
|
2020-06-20 14:36:16 +00:00
|
|
|
PROJECT* project = GetSettingsManager()->GetProject( projectPath );
|
2020-05-31 21:42:04 +00:00
|
|
|
|
2020-06-20 14:36:16 +00:00
|
|
|
if( !project )
|
2020-05-31 21:42:04 +00:00
|
|
|
{
|
2020-06-20 14:36:16 +00:00
|
|
|
GetSettingsManager()->LoadProject( projectPath );
|
|
|
|
GetSettingsManager()->GetProject( projectPath );
|
2020-05-31 21:42:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Board cannot be loaded without a project, so create the default project
|
|
|
|
if( !project )
|
|
|
|
project = GetDefaultProject();
|
|
|
|
|
2017-08-17 15:55:05 +00:00
|
|
|
BOARD* brd = IO_MGR::Load( aFormat, aFileName );
|
|
|
|
|
|
|
|
if( brd )
|
2018-12-04 01:03:44 +00:00
|
|
|
{
|
2020-05-31 21:42:04 +00:00
|
|
|
brd->SetProject( project );
|
2017-08-17 15:55:05 +00:00
|
|
|
brd->BuildConnectivity();
|
2018-12-04 01:03:44 +00:00
|
|
|
brd->BuildListOfNets();
|
|
|
|
brd->SynchronizeNetsAndNetClasses();
|
|
|
|
}
|
|
|
|
|
2020-05-31 21:42:04 +00:00
|
|
|
return brd;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BOARD* CreateEmptyBoard()
|
|
|
|
{
|
|
|
|
BOARD* brd = new BOARD();
|
|
|
|
|
|
|
|
brd->SetProject( GetDefaultProject() );
|
2017-08-17 15:55:05 +00:00
|
|
|
|
|
|
|
return brd;
|
2012-03-17 15:17:13 +00:00
|
|
|
}
|
|
|
|
|
2013-03-15 16:35:24 +00:00
|
|
|
|
2017-06-23 17:22:27 +00:00
|
|
|
bool SaveBoard( wxString& aFileName, BOARD* aBoard, IO_MGR::PCB_FILE_T aFormat )
|
2012-03-17 15:17:13 +00:00
|
|
|
{
|
2017-08-17 15:55:05 +00:00
|
|
|
aBoard->BuildConnectivity();
|
2013-03-15 16:35:24 +00:00
|
|
|
aBoard->SynchronizeNetsAndNetClasses();
|
2014-05-15 08:11:17 +00:00
|
|
|
aBoard->GetDesignSettings().SetCurrentNetClass( NETCLASS::Default );
|
2012-03-17 15:17:13 +00:00
|
|
|
|
2017-06-23 17:22:27 +00:00
|
|
|
IO_MGR::Save( aFormat, aFileName, aBoard, NULL );
|
2017-05-24 16:08:15 +00:00
|
|
|
|
2020-05-31 21:42:04 +00:00
|
|
|
wxFileName pro = aFileName;
|
|
|
|
pro.SetExt( ProjectFileExtension );
|
|
|
|
pro.MakeAbsolute();
|
|
|
|
wxString projectPath = pro.GetFullPath();
|
|
|
|
|
|
|
|
GetSettingsManager()->SaveProject( pro.GetFullPath() );
|
|
|
|
|
2013-03-15 16:35:24 +00:00
|
|
|
return true;
|
2012-03-17 15:17:13 +00:00
|
|
|
}
|
2017-03-17 14:41:00 +00:00
|
|
|
|
|
|
|
|
2017-06-23 17:22:27 +00:00
|
|
|
bool SaveBoard( wxString& aFileName, BOARD* aBoard )
|
|
|
|
{
|
2017-10-03 10:23:52 +00:00
|
|
|
return SaveBoard( aFileName, aBoard, IO_MGR::KICAD_SEXP );
|
2017-06-23 17:22:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-05 22:21:41 +00:00
|
|
|
FP_LIB_TABLE* GetFootprintLibraryTable()
|
|
|
|
{
|
|
|
|
BOARD* board = GetBoard();
|
|
|
|
|
|
|
|
if( !board )
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
PROJECT* project = board->GetProject();
|
|
|
|
|
|
|
|
if( !project )
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return project->PcbFootprintLibs();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wxArrayString GetFootprintLibraries()
|
|
|
|
{
|
|
|
|
wxArrayString footprintLibraryNames;
|
|
|
|
|
|
|
|
FP_LIB_TABLE* tbl = GetFootprintLibraryTable();
|
|
|
|
|
|
|
|
if( !tbl )
|
|
|
|
return footprintLibraryNames;
|
|
|
|
|
|
|
|
for( const wxString& name : tbl->GetLogicalLibs() )
|
|
|
|
footprintLibraryNames.Add( name );
|
|
|
|
|
|
|
|
return footprintLibraryNames;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wxArrayString GetFootprints( const wxString& aNickName )
|
|
|
|
{
|
|
|
|
wxArrayString footprintNames;
|
|
|
|
|
|
|
|
FP_LIB_TABLE* tbl = GetFootprintLibraryTable();
|
|
|
|
|
|
|
|
if( !tbl )
|
|
|
|
return footprintNames;
|
|
|
|
|
|
|
|
tbl->FootprintEnumerate( footprintNames, aNickName, true );
|
|
|
|
|
|
|
|
return footprintNames;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-15 22:54:47 +00:00
|
|
|
bool ExportSpecctraDSN( wxString& aFullFilename )
|
|
|
|
{
|
|
|
|
if( s_PcbEditFrame )
|
|
|
|
{
|
|
|
|
bool ok = s_PcbEditFrame->ExportSpecctraFile( aFullFilename );
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-28 11:17:50 +00:00
|
|
|
bool ExportVRML( const wxString& aFullFileName, double aMMtoWRMLunit,
|
|
|
|
bool aExport3DFiles, bool aUseRelativePaths,
|
|
|
|
bool aUsePlainPCB, const wxString& a3D_Subdir,
|
|
|
|
double aXRef, double aYRef )
|
|
|
|
{
|
|
|
|
if( s_PcbEditFrame )
|
|
|
|
{
|
|
|
|
bool ok = s_PcbEditFrame->ExportVRML_File( aFullFileName, aMMtoWRMLunit,
|
|
|
|
aExport3DFiles, aUseRelativePaths,
|
|
|
|
aUsePlainPCB, a3D_Subdir, aXRef, aYRef );
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2019-04-15 22:54:47 +00:00
|
|
|
|
|
|
|
bool ImportSpecctraSES( wxString& aFullFilename )
|
|
|
|
{
|
|
|
|
if( s_PcbEditFrame )
|
|
|
|
{
|
|
|
|
bool ok = s_PcbEditFrame->ImportSpecctraSession( aFullFilename );
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ArchiveModulesOnBoard( bool aStoreInNewLib, const wxString& aLibName, wxString* aLibPath )
|
|
|
|
{
|
|
|
|
if( s_PcbEditFrame )
|
|
|
|
{
|
|
|
|
s_PcbEditFrame->ArchiveModulesOnBoard( aStoreInNewLib, aLibName, aLibPath );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-17 14:41:00 +00:00
|
|
|
void Refresh()
|
|
|
|
{
|
2017-08-27 06:04:12 +00:00
|
|
|
if( s_PcbEditFrame )
|
|
|
|
{
|
2017-12-03 10:19:42 +00:00
|
|
|
auto board = s_PcbEditFrame->GetBoard();
|
|
|
|
board->BuildConnectivity();
|
|
|
|
|
2019-06-13 17:28:55 +00:00
|
|
|
// Re-init everything: this is the easy way to do that
|
2019-05-30 12:25:08 +00:00
|
|
|
s_PcbEditFrame->ActivateGalCanvas();
|
2019-06-13 17:28:55 +00:00
|
|
|
s_PcbEditFrame->GetCanvas()->Refresh();
|
2017-08-27 06:04:12 +00:00
|
|
|
}
|
2017-03-17 14:41:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-27 06:04:12 +00:00
|
|
|
void UpdateUserInterface()
|
|
|
|
{
|
|
|
|
if( s_PcbEditFrame )
|
|
|
|
s_PcbEditFrame->UpdateUserInterface();
|
2017-03-17 14:41:00 +00:00
|
|
|
}
|
2018-03-06 08:28:57 +00:00
|
|
|
|
|
|
|
|
2019-02-19 13:46:08 +00:00
|
|
|
int GetUserUnits()
|
|
|
|
{
|
|
|
|
if( s_PcbEditFrame )
|
2019-12-20 14:47:41 +00:00
|
|
|
return static_cast<int>( s_PcbEditFrame->GetUserUnits() );
|
2019-02-19 13:46:08 +00:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-06 08:28:57 +00:00
|
|
|
bool IsActionRunning()
|
|
|
|
{
|
|
|
|
return ACTION_PLUGINS::IsActionRunning();
|
|
|
|
}
|