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>
|
2023-08-24 21:09:53 +00:00
|
|
|
* Copyright (C) 1992-2023 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
|
|
|
|
2021-03-19 23:38:11 +00:00
|
|
|
#include "pcbnew_scripting_helpers.h"
|
|
|
|
|
2019-12-05 14:03:15 +00:00
|
|
|
#include <action_plugin.h>
|
2020-11-12 20:19:22 +00:00
|
|
|
#include <board.h>
|
2021-06-06 19:03:10 +00:00
|
|
|
#include <board_design_settings.h>
|
2020-11-14 18:11:28 +00:00
|
|
|
#include <pcb_marker.h>
|
2019-12-05 14:03:15 +00:00
|
|
|
#include <cstdlib>
|
2023-04-21 03:17:39 +00:00
|
|
|
#include <drawing_sheet/ds_data_model.h>
|
2020-09-23 00:23:14 +00:00
|
|
|
#include <drc/drc_engine.h>
|
|
|
|
#include <drc/drc_item.h>
|
2020-06-05 22:21:41 +00:00
|
|
|
#include <fp_lib_table.h>
|
2023-04-19 02:44:04 +00:00
|
|
|
#include <core/ignore.h>
|
2012-03-17 15:17:13 +00:00
|
|
|
#include <io_mgr.h>
|
2021-07-29 09:56:22 +00:00
|
|
|
#include <string_utils.h>
|
2021-03-20 15:35:37 +00:00
|
|
|
#include <macros.h>
|
2019-12-05 14:03:15 +00:00
|
|
|
#include <pcbnew_scripting_helpers.h>
|
2020-06-05 22:21:41 +00:00
|
|
|
#include <project.h>
|
2023-09-28 03:15:54 +00:00
|
|
|
#include <project_pcb.h>
|
2021-12-07 23:55:08 +00:00
|
|
|
#include <project/net_settings.h>
|
|
|
|
#include <project/project_file.h>
|
2020-05-31 21:42:04 +00:00
|
|
|
#include <settings/settings_manager.h>
|
2021-07-28 19:48:03 +00:00
|
|
|
#include <specctra.h>
|
2020-09-29 15:30:04 +00:00
|
|
|
#include <project/project_local_settings.h>
|
2020-05-31 21:42:04 +00:00
|
|
|
#include <wildcards_and_files_ext.h>
|
2021-04-28 12:02:47 +00:00
|
|
|
#include <locale_io.h>
|
2021-05-01 07:50:29 +00:00
|
|
|
#include <wx/app.h>
|
2023-04-21 03:17:39 +00:00
|
|
|
#include <wx/crt.h>
|
2021-04-28 12:02:47 +00:00
|
|
|
|
2012-03-17 15:17:13 +00:00
|
|
|
|
2021-07-19 23:56:05 +00:00
|
|
|
static PCB_EDIT_FRAME* s_PcbEditFrame = nullptr;
|
2020-05-31 21:42:04 +00:00
|
|
|
static SETTINGS_MANAGER* s_SettingsManager = nullptr;
|
|
|
|
|
2020-09-26 01:27:39 +00:00
|
|
|
|
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
|
2021-07-19 23:56:05 +00:00
|
|
|
return nullptr;
|
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
|
|
|
|
2023-08-24 21:09:53 +00:00
|
|
|
void ScriptingOnDestructPcbEditFrame( PCB_EDIT_FRAME* aPcbEditFrame )
|
|
|
|
{
|
|
|
|
if( s_PcbEditFrame == aPcbEditFrame )
|
|
|
|
s_PcbEditFrame = nullptr;
|
|
|
|
}
|
|
|
|
|
2012-07-31 21:00:33 +00:00
|
|
|
BOARD* LoadBoard( wxString& aFileName )
|
2012-04-14 21:18:27 +00:00
|
|
|
{
|
2021-03-03 01:06:25 +00:00
|
|
|
if( aFileName.EndsWith( KiCadPcbFileExtension ) )
|
2017-10-03 10:23:52 +00:00
|
|
|
return LoadBoard( aFileName, IO_MGR::KICAD_SEXP );
|
2021-03-03 01:06:25 +00:00
|
|
|
else if( aFileName.EndsWith( LegacyPcbFileExtension ) )
|
2013-03-15 16:35:24 +00:00
|
|
|
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-09-26 01:27:39 +00:00
|
|
|
{
|
2021-08-14 13:18:11 +00:00
|
|
|
if( s_PcbEditFrame )
|
|
|
|
{
|
|
|
|
s_SettingsManager = s_PcbEditFrame->GetSettingsManager();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Ensure wx system settings stuff is available
|
2021-10-05 16:57:25 +00:00
|
|
|
ignore_unused( wxTheApp );
|
2021-08-14 13:18:11 +00:00
|
|
|
s_SettingsManager = new SETTINGS_MANAGER( true );
|
|
|
|
}
|
2020-09-26 01:27:39 +00:00
|
|
|
}
|
2020-05-31 21:42:04 +00:00
|
|
|
|
|
|
|
return s_SettingsManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PROJECT* GetDefaultProject()
|
|
|
|
{
|
2021-07-23 09:28:02 +00:00
|
|
|
// For some reasons, LoadProject() needs a C locale, so ensure we have the right locale
|
|
|
|
// This is mainly when running QA Python tests
|
|
|
|
LOCALE_IO dummy;
|
|
|
|
|
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();
|
|
|
|
|
2021-04-28 12:02:47 +00:00
|
|
|
// Ensure the "C" locale is temporary set, before reading any file
|
|
|
|
// It also avoid wxWidget alerts about locale issues, later, when using Python 3
|
|
|
|
LOCALE_IO dummy;
|
|
|
|
|
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
|
|
|
{
|
2022-03-05 17:26:33 +00:00
|
|
|
if( wxFileExists( projectPath ) )
|
|
|
|
{
|
|
|
|
GetSettingsManager()->LoadProject( projectPath, false );
|
|
|
|
project = GetSettingsManager()->GetProject( projectPath );
|
|
|
|
}
|
2020-05-31 21:42:04 +00:00
|
|
|
}
|
2021-10-24 18:06:38 +00:00
|
|
|
else if( s_PcbEditFrame && project == &GetSettingsManager()->Prj() )
|
|
|
|
{
|
|
|
|
// Project is already loaded? Then so is the board
|
|
|
|
return s_PcbEditFrame->GetBoard();
|
|
|
|
}
|
2020-05-31 21:42:04 +00:00
|
|
|
|
|
|
|
// Board cannot be loaded without a project, so create the default project
|
|
|
|
if( !project )
|
|
|
|
project = GetDefaultProject();
|
|
|
|
|
2023-04-21 03:17:39 +00:00
|
|
|
BASE_SCREEN::m_DrawingSheetFileName = project->GetProjectFile().m_BoardDrawingSheetFile;
|
|
|
|
|
|
|
|
// Load the drawing sheet from the filename stored in BASE_SCREEN::m_DrawingSheetFileName.
|
|
|
|
// If empty, or not existing, the default drawing sheet is loaded.
|
|
|
|
wxString filename = DS_DATA_MODEL::ResolvePath( BASE_SCREEN::m_DrawingSheetFileName,
|
|
|
|
project->GetProjectPath() );
|
|
|
|
|
|
|
|
if( !DS_DATA_MODEL::GetTheInstance().LoadDrawingSheet( filename ) )
|
|
|
|
wxFprintf( stderr, _( "Error loading drawing sheet." ) );
|
|
|
|
|
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 );
|
2020-09-29 11:33:44 +00:00
|
|
|
|
|
|
|
// Move legacy view settings to local project settings
|
|
|
|
if( !brd->m_LegacyVisibleLayers.test( Rescue ) )
|
2020-09-29 15:30:04 +00:00
|
|
|
project->GetLocalSettings().m_VisibleLayers = brd->m_LegacyVisibleLayers;
|
2020-09-29 11:33:44 +00:00
|
|
|
|
|
|
|
if( !brd->m_LegacyVisibleItems.test( GAL_LAYER_INDEX( GAL_LAYER_ID_BITMASK_END ) ) )
|
2020-09-29 15:30:04 +00:00
|
|
|
project->GetLocalSettings().m_VisibleItems = brd->m_LegacyVisibleItems;
|
2020-09-29 11:33:44 +00:00
|
|
|
|
|
|
|
BOARD_DESIGN_SETTINGS& bds = brd->GetDesignSettings();
|
|
|
|
bds.m_DRCEngine = std::make_shared<DRC_ENGINE>( brd, &bds );
|
|
|
|
|
2020-10-01 22:43:34 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
wxFileName rules = pro;
|
|
|
|
rules.SetExt( DesignRulesFileExtension );
|
|
|
|
bds.m_DRCEngine->InitEngine( rules );
|
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
|
|
|
// Best efforts...
|
|
|
|
}
|
|
|
|
|
2023-04-15 09:54:53 +00:00
|
|
|
for( PCB_MARKER* marker : brd->ResolveDRCExclusions( true ) )
|
2020-09-29 11:33:44 +00:00
|
|
|
brd->Add( marker );
|
|
|
|
|
2017-08-17 15:55:05 +00:00
|
|
|
brd->BuildConnectivity();
|
2018-12-04 01:03:44 +00:00
|
|
|
brd->BuildListOfNets();
|
2022-11-22 14:24:20 +00:00
|
|
|
brd->SynchronizeNetsAndNetClasses( false );
|
2023-03-13 20:25:27 +00:00
|
|
|
brd->UpdateUserUnits( brd, nullptr );
|
2018-12-04 01:03:44 +00:00
|
|
|
}
|
|
|
|
|
2020-05-31 21:42:04 +00:00
|
|
|
return brd;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-19 02:43:05 +00:00
|
|
|
BOARD* NewBoard( wxString& aFileName )
|
|
|
|
{
|
|
|
|
wxFileName boardFn = aFileName;
|
|
|
|
wxFileName proFn = aFileName;
|
|
|
|
proFn.SetExt( ProjectFileExtension );
|
|
|
|
proFn.MakeAbsolute();
|
|
|
|
|
|
|
|
wxString projectPath = proFn.GetFullPath();
|
|
|
|
|
|
|
|
// Ensure the "C" locale is temporary set, before reading any file
|
|
|
|
// It also avoids wxWidgets alerts about locale issues, later, when using Python 3
|
|
|
|
LOCALE_IO dummy;
|
|
|
|
|
|
|
|
GetSettingsManager()->LoadProject( projectPath, false );
|
|
|
|
PROJECT* project = GetSettingsManager()->GetProject( projectPath );
|
|
|
|
|
|
|
|
BOARD* brd = new BOARD();
|
|
|
|
|
|
|
|
brd->SetProject( project );
|
|
|
|
BOARD_DESIGN_SETTINGS& bds = brd->GetDesignSettings();
|
|
|
|
bds.m_DRCEngine = std::make_shared<DRC_ENGINE>( brd, &bds );
|
|
|
|
|
|
|
|
SaveBoard( aFileName, brd );
|
|
|
|
|
|
|
|
return brd;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-31 21:42:04 +00:00
|
|
|
BOARD* CreateEmptyBoard()
|
|
|
|
{
|
2021-08-14 13:18:11 +00:00
|
|
|
// Creating a new board is not possible if running inside KiCad
|
|
|
|
if( s_PcbEditFrame )
|
|
|
|
return nullptr;
|
|
|
|
|
2020-05-31 21:42:04 +00:00
|
|
|
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
|
|
|
|
2022-06-24 22:27:40 +00:00
|
|
|
bool SaveBoard( wxString& aFileName, BOARD* aBoard, IO_MGR::PCB_FILE_T aFormat, bool aSkipSettings )
|
2012-03-17 15:17:13 +00:00
|
|
|
{
|
2017-08-17 15:55:05 +00:00
|
|
|
aBoard->BuildConnectivity();
|
2022-11-22 14:24:20 +00:00
|
|
|
aBoard->SynchronizeNetsAndNetClasses( false );
|
2012-03-17 15:17:13 +00:00
|
|
|
|
2022-08-23 16:41:11 +00:00
|
|
|
// Ensure the "C" locale is temporary set, before saving any file
|
|
|
|
// It also avoid wxWidget alerts about locale issues, later, when using Python 3
|
|
|
|
LOCALE_IO dummy;
|
|
|
|
|
2021-08-14 13:18:11 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
IO_MGR::Save( aFormat, aFileName, aBoard, nullptr );
|
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2017-05-24 16:08:15 +00:00
|
|
|
|
2022-06-24 22:27:40 +00:00
|
|
|
if( !aSkipSettings )
|
|
|
|
{
|
|
|
|
wxFileName pro = aFileName;
|
|
|
|
pro.SetExt( ProjectFileExtension );
|
|
|
|
pro.MakeAbsolute();
|
2020-05-31 21:42:04 +00:00
|
|
|
|
2022-06-24 22:27:40 +00:00
|
|
|
GetSettingsManager()->SaveProjectAs( pro.GetFullPath(), aBoard->GetProject() );
|
|
|
|
}
|
2020-05-31 21:42:04 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
|
2022-06-24 22:27:40 +00:00
|
|
|
bool SaveBoard( wxString& aFileName, BOARD* aBoard, bool aSkipSettings )
|
2017-06-23 17:22:27 +00:00
|
|
|
{
|
2022-06-24 22:27:40 +00:00
|
|
|
return SaveBoard( aFileName, aBoard, IO_MGR::KICAD_SEXP, aSkipSettings );
|
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;
|
|
|
|
|
2023-09-28 03:15:54 +00:00
|
|
|
return PROJECT_PCB::PcbFootprintLibs( project );
|
2020-06-05 22:21:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-28 19:48:03 +00:00
|
|
|
|
|
|
|
bool ExportSpecctraDSN( BOARD* aBoard, wxString& aFullFilename )
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
ExportBoardToSpecctraFile( aBoard, aFullFilename );
|
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-19 23:56:05 +00:00
|
|
|
bool ExportVRML( const wxString& aFullFileName, double aMMtoWRMLunit, bool aExport3DFiles,
|
|
|
|
bool aUseRelativePaths, const wxString& a3D_Subdir, double aXRef, double aYRef )
|
2020-08-28 11:17:50 +00:00
|
|
|
{
|
|
|
|
if( s_PcbEditFrame )
|
|
|
|
{
|
|
|
|
bool ok = s_PcbEditFrame->ExportVRML_File( aFullFileName, aMMtoWRMLunit,
|
2021-02-21 10:38:10 +00:00
|
|
|
aExport3DFiles, aUseRelativePaths,
|
2021-03-18 18:53:36 +00:00
|
|
|
a3D_Subdir, aXRef, aYRef );
|
2020-08-28 11:17:50 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-09 16:45:36 +00:00
|
|
|
bool ExportFootprintsToLibrary( bool aStoreInNewLib, const wxString& aLibName, wxString* aLibPath )
|
2019-04-15 22:54:47 +00:00
|
|
|
{
|
|
|
|
if( s_PcbEditFrame )
|
|
|
|
{
|
2020-11-09 16:45:36 +00:00
|
|
|
s_PcbEditFrame->ExportFootprintsToLibrary( aStoreInNewLib, aLibName, aLibPath );
|
2019-04-15 22:54:47 +00:00
|
|
|
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 )
|
|
|
|
{
|
2022-12-26 16:23:44 +00:00
|
|
|
s_PcbEditFrame->RebuildAndRefresh();
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-29 01:45:02 +00:00
|
|
|
std::deque<BOARD_ITEM*> GetCurrentSelection()
|
|
|
|
{
|
|
|
|
std::deque<BOARD_ITEM*> items;
|
|
|
|
|
|
|
|
if( s_PcbEditFrame )
|
|
|
|
{
|
|
|
|
SELECTION& selection = s_PcbEditFrame->GetCurrentSelection();
|
|
|
|
|
|
|
|
std::for_each( selection.begin(), selection.end(),
|
|
|
|
[&items]( EDA_ITEM* item )
|
|
|
|
{
|
|
|
|
items.push_back( static_cast<BOARD_ITEM*>( item ) );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
return items;
|
|
|
|
}
|
|
|
|
|
2023-09-19 01:54:49 +00:00
|
|
|
void FocusOnItem( BOARD_ITEM* aItem, PCB_LAYER_ID aLayer )
|
|
|
|
{
|
|
|
|
if( s_PcbEditFrame )
|
|
|
|
{
|
|
|
|
s_PcbEditFrame->FocusOnItem( aItem, aLayer );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-29 01:45:02 +00:00
|
|
|
|
2018-03-06 08:28:57 +00:00
|
|
|
bool IsActionRunning()
|
|
|
|
{
|
|
|
|
return ACTION_PLUGINS::IsActionRunning();
|
|
|
|
}
|
2020-09-23 00:23:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
bool WriteDRCReport( BOARD* aBoard, const wxString& aFileName, EDA_UNITS aUnits,
|
2020-11-11 23:05:59 +00:00
|
|
|
bool aReportAllTrackErrors )
|
2020-09-23 00:23:14 +00:00
|
|
|
{
|
|
|
|
wxCHECK( aBoard, false );
|
|
|
|
|
2022-09-19 16:09:59 +00:00
|
|
|
BOARD_DESIGN_SETTINGS& bds = aBoard->GetDesignSettings();
|
2020-09-23 00:23:14 +00:00
|
|
|
std::shared_ptr<DRC_ENGINE> engine = bds.m_DRCEngine;
|
2022-09-19 16:09:59 +00:00
|
|
|
UNITS_PROVIDER unitsProvider( pcbIUScale, aUnits );
|
2020-09-23 00:23:14 +00:00
|
|
|
|
|
|
|
if( !engine )
|
|
|
|
{
|
|
|
|
bds.m_DRCEngine = std::make_shared<DRC_ENGINE>( aBoard, &bds );
|
|
|
|
engine = bds.m_DRCEngine;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxCHECK( engine, false );
|
|
|
|
|
2020-09-26 01:20:54 +00:00
|
|
|
wxFileName fn = aBoard->GetFileName();
|
2020-09-25 01:26:23 +00:00
|
|
|
fn.SetExt( DesignRulesFileExtension );
|
2022-01-06 19:45:37 +00:00
|
|
|
PROJECT* prj = nullptr;
|
|
|
|
|
|
|
|
if( aBoard->GetProject() )
|
|
|
|
prj = aBoard->GetProject();
|
|
|
|
else if( s_SettingsManager )
|
|
|
|
prj = &s_SettingsManager->Prj();
|
|
|
|
|
|
|
|
wxCHECK( prj, false );
|
|
|
|
|
2023-02-24 13:43:48 +00:00
|
|
|
// Load the global fp-lib-table otherwise we can't check the libs parity
|
|
|
|
wxFileName fn_flp = FP_LIB_TABLE::GetGlobalTableFileName();
|
2023-08-03 12:55:14 +00:00
|
|
|
if( fn_flp.FileExists() ) {
|
|
|
|
GFootprintTable.Clear();
|
2023-02-24 13:43:48 +00:00
|
|
|
GFootprintTable.Load( fn_flp.GetFullPath() );
|
2023-08-03 12:55:14 +00:00
|
|
|
}
|
2023-02-24 13:43:48 +00:00
|
|
|
|
2022-01-06 19:45:37 +00:00
|
|
|
wxString drcRulesPath = prj->AbsolutePath( fn.GetFullName() );
|
2020-09-25 01:26:23 +00:00
|
|
|
|
2022-12-19 11:39:20 +00:00
|
|
|
// Rebuild The Instance of ENUM_MAP<PCB_LAYER_ID> (layer names list), because the DRC
|
|
|
|
// engine can use layer names (canonical and/or user names)
|
|
|
|
ENUM_MAP<PCB_LAYER_ID>& layerEnum = ENUM_MAP<PCB_LAYER_ID>::Instance();
|
|
|
|
layerEnum.Choices().Clear();
|
|
|
|
layerEnum.Undefined( UNDEFINED_LAYER );
|
|
|
|
|
|
|
|
for( LSEQ seq = LSET::AllLayersMask().Seq(); seq; ++seq )
|
|
|
|
{
|
|
|
|
layerEnum.Map( *seq, LSET::Name( *seq ) ); // Add Canonical name
|
|
|
|
layerEnum.Map( *seq, aBoard->GetLayerName( *seq ) ); // Add User name
|
|
|
|
}
|
|
|
|
|
2020-09-23 00:23:14 +00:00
|
|
|
try
|
|
|
|
{
|
2020-09-25 01:26:23 +00:00
|
|
|
engine->InitEngine( drcRulesPath );
|
2020-09-23 00:23:14 +00:00
|
|
|
}
|
2022-12-19 11:39:20 +00:00
|
|
|
catch( PARSE_ERROR& err )
|
2020-09-23 00:23:14 +00:00
|
|
|
{
|
2022-12-19 11:39:20 +00:00
|
|
|
fprintf( stderr, "Init DRC engine: err <%s>\n", TO_UTF8( err.What() ) ); fflush( stderr);
|
2020-09-23 00:23:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::shared_ptr<DRC_ITEM>> footprints;
|
|
|
|
std::vector<std::shared_ptr<DRC_ITEM>> unconnected;
|
|
|
|
std::vector<std::shared_ptr<DRC_ITEM>> violations;
|
|
|
|
|
|
|
|
engine->SetProgressReporter( nullptr );
|
|
|
|
|
|
|
|
engine->SetViolationHandler(
|
2022-08-15 16:59:34 +00:00
|
|
|
[&]( const std::shared_ptr<DRC_ITEM>& aItem, VECTOR2D aPos, int aLayer )
|
2020-09-23 00:23:14 +00:00
|
|
|
{
|
2022-12-15 13:27:14 +00:00
|
|
|
if( aItem->GetErrorCode() == DRCE_MISSING_FOOTPRINT
|
2020-09-23 00:23:14 +00:00
|
|
|
|| aItem->GetErrorCode() == DRCE_DUPLICATE_FOOTPRINT
|
|
|
|
|| aItem->GetErrorCode() == DRCE_EXTRA_FOOTPRINT
|
|
|
|
|| aItem->GetErrorCode() == DRCE_NET_CONFLICT )
|
|
|
|
{
|
|
|
|
footprints.push_back( aItem );
|
|
|
|
}
|
|
|
|
else if( aItem->GetErrorCode() == DRCE_UNCONNECTED_ITEMS )
|
|
|
|
{
|
|
|
|
unconnected.push_back( aItem );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
violations.push_back( aItem );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2020-10-28 17:55:57 +00:00
|
|
|
engine->RunTests( aUnits, aReportAllTrackErrors, false );
|
2020-09-23 00:23:14 +00:00
|
|
|
engine->ClearViolationHandler();
|
|
|
|
|
|
|
|
// TODO: Unify this with DIALOG_DRC::writeReport
|
|
|
|
|
|
|
|
FILE* fp = wxFopen( aFileName, wxT( "w" ) );
|
|
|
|
|
|
|
|
if( fp == nullptr )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
std::map<KIID, EDA_ITEM*> itemMap;
|
|
|
|
aBoard->FillItemMap( itemMap );
|
|
|
|
|
|
|
|
fprintf( fp, "** Drc report for %s **\n", TO_UTF8( aBoard->GetFileName() ) );
|
|
|
|
|
|
|
|
wxDateTime now = wxDateTime::Now();
|
|
|
|
|
|
|
|
fprintf( fp, "** Created on %s **\n", TO_UTF8( now.Format( wxT( "%F %T" ) ) ) );
|
|
|
|
|
|
|
|
fprintf( fp, "\n** Found %d DRC violations **\n", static_cast<int>( violations.size() ) );
|
|
|
|
|
|
|
|
for( const std::shared_ptr<DRC_ITEM>& item : violations )
|
|
|
|
{
|
2022-12-15 13:27:14 +00:00
|
|
|
SEVERITY severity = item->GetParent() ? item->GetParent()->GetSeverity()
|
2022-12-18 17:18:53 +00:00
|
|
|
: bds.GetSeverity( item->GetErrorCode() );
|
2022-09-19 16:09:59 +00:00
|
|
|
fprintf( fp, "%s", TO_UTF8( item->ShowReport( &unitsProvider, severity, itemMap ) ) );
|
2020-09-23 00:23:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fprintf( fp, "\n** Found %d unconnected pads **\n", static_cast<int>( unconnected.size() ) );
|
|
|
|
|
|
|
|
for( const std::shared_ptr<DRC_ITEM>& item : unconnected )
|
|
|
|
{
|
2020-12-14 13:33:51 +00:00
|
|
|
SEVERITY severity = bds.GetSeverity( item->GetErrorCode() );
|
2022-09-19 16:09:59 +00:00
|
|
|
fprintf( fp, "%s", TO_UTF8( item->ShowReport( &unitsProvider, severity, itemMap ) ) );
|
2020-09-23 00:23:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fprintf( fp, "\n** Found %d Footprint errors **\n", static_cast<int>( footprints.size() ) );
|
|
|
|
|
|
|
|
for( const std::shared_ptr<DRC_ITEM>& item : footprints )
|
|
|
|
{
|
2020-12-14 13:33:51 +00:00
|
|
|
SEVERITY severity = bds.GetSeverity( item->GetErrorCode() );
|
2022-09-19 16:09:59 +00:00
|
|
|
fprintf( fp, "%s", TO_UTF8( item->ShowReport( &unitsProvider, severity, itemMap ) ) );
|
2020-09-23 00:23:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fprintf( fp, "\n** End of Report **\n" );
|
|
|
|
fclose( fp );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|