From d76c6bc884ab0b0468d93ebf60a0e91181645d63 Mon Sep 17 00:00:00 2001 From: Jon Evans Date: Thu, 18 Nov 2021 21:43:05 -0500 Subject: [PATCH] Add NewBoard API to allow standalone BOARD creation Fixes https://gitlab.com/kicad/code/kicad/-/issues/9687 --- .../scripting/pcbnew_scripting_helpers.cpp | 28 +++++++++++++++++++ .../scripting/pcbnew_scripting_helpers.h | 8 ++++++ 2 files changed, 36 insertions(+) diff --git a/pcbnew/python/scripting/pcbnew_scripting_helpers.cpp b/pcbnew/python/scripting/pcbnew_scripting_helpers.cpp index 73038fcb99..0cfd105820 100644 --- a/pcbnew/python/scripting/pcbnew_scripting_helpers.cpp +++ b/pcbnew/python/scripting/pcbnew_scripting_helpers.cpp @@ -191,6 +191,34 @@ BOARD* LoadBoard( wxString& aFileName, IO_MGR::PCB_FILE_T aFormat ) } +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( brd, &bds ); + + SaveBoard( aFileName, brd ); + + return brd; +} + + BOARD* CreateEmptyBoard() { // Creating a new board is not possible if running inside KiCad diff --git a/pcbnew/python/scripting/pcbnew_scripting_helpers.h b/pcbnew/python/scripting/pcbnew_scripting_helpers.h index a4ab2efdc4..772a86c60f 100644 --- a/pcbnew/python/scripting/pcbnew_scripting_helpers.h +++ b/pcbnew/python/scripting/pcbnew_scripting_helpers.h @@ -45,6 +45,14 @@ BOARD* LoadBoard( wxString& aFileName, IO_MGR::PCB_FILE_T aFormat ); // Default LoadBoard() to load .kicad_pcb files:. BOARD* LoadBoard( wxString& aFileName ); +/** + * Creates a new board and project with the given filename (will overwrite existing files!) + * + * @param aFileName is the filename (including full path if desired) of the kicad_pcb to create + * @return a pointer to the board if it was created, or None if not + */ +BOARD* NewBoard( wxString& aFileName ); + SETTINGS_MANAGER* GetSettingsManager(); /**