KiCad PCB EDA Suite
Loading...
Searching...
No Matches
headless_board_context.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 * @author Jon Evans <[email protected]>
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
22#include <board.h>
23#include <board_loader.h>
24#include <project.h>
26#include <tool/tool_manager.h>
27#include <wx/debug.h>
28
29
30HEADLESS_BOARD_CONTEXT::HEADLESS_BOARD_CONTEXT( std::unique_ptr<BOARD> aBoard, PROJECT* aProject,
31 APP_SETTINGS_BASE* aSettings,
32 KIWAY* aKiway ) :
33 m_board( std::move( aBoard ) ),
34 m_project( aProject ),
35 m_kiway( aKiway ),
36 m_toolManager( std::make_unique<TOOL_MANAGER>() )
37{
38 wxCHECK( m_board, /* void */ );
39 wxCHECK( m_project, /* void */ );
40
41 m_board->SetProject( m_project );
42 m_toolManager->SetEnvironment( m_board.get(), nullptr, nullptr, aSettings, nullptr );
43}
44
45
47{
48 // Sever the board↔project linkage before destruction. The PROJECT holds a raw pointer
49 // (m_BoardSettings) to the board's design settings. If the board is destroyed while the
50 // project still exists, that pointer becomes dangling.
51 if( m_board )
52 m_board->ClearProject();
53}
54
55
57{
58 return m_board.get();
59}
60
61
63{
64 // Shouldn't be able to construct this without a project
65 wxASSERT( m_project );
66 return *m_project;
67}
68
69
74
75
77{
78 if( !m_board )
79 return wxEmptyString;
80
81 return m_board->GetFileName();
82}
83
84
86{
87 if( !m_board )
88 return false;
89
90 wxString fileName = m_board->GetFileName();
91
92 if( fileName.IsEmpty() )
93 return false;
94
95 bool success = BOARD_LOADER::SaveBoard( fileName, m_board.get() );
96
97 if( success )
98 {
99 wxFileName pro = fileName;
100 pro.SetExt( FILEEXT::ProjectFileExtension );
101 pro.MakeAbsolute();
102
103 Pgm().GetSettingsManager().SaveProjectAs( pro.GetFullPath(), m_board->GetProject() );
104 }
105
106 return success;
107}
108
109
110bool HEADLESS_BOARD_CONTEXT::SavePcbCopy( const wxString& aFileName, bool aCreateProject, bool aHeadless )
111{
112 if( !m_board || aFileName.IsEmpty() )
113 return false;
114
115 wxString outPath = aFileName;
116
117 bool success = BOARD_LOADER::SaveBoard( outPath, m_board.get() );
118
119 if( success && aCreateProject )
120 {
121 wxFileName pro = aFileName;
122 pro.SetExt( FILEEXT::ProjectFileExtension );
123 pro.MakeAbsolute();
124
125 Pgm().GetSettingsManager().SaveProjectAs( pro.GetFullPath(), m_board->GetProject() );
126 }
127
128 return success;
129}
APP_SETTINGS_BASE is a settings class that should be derived for each standalone KiCad application.
static bool SaveBoard(wxString &aFileName, BOARD *aBoard, PCB_IO_MGR::PCB_FILE_T aFormat)
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:323
PROJECT & Prj() const override
TOOL_MANAGER * GetToolManager() const override
BOARD * GetBoard() const override
std::unique_ptr< TOOL_MANAGER > m_toolManager
HEADLESS_BOARD_CONTEXT(std::unique_ptr< BOARD > aBoard, PROJECT *aProject, APP_SETTINGS_BASE *aSettings, KIWAY *aKiway=nullptr)
std::unique_ptr< BOARD > m_board
wxString GetCurrentFileName() const override
bool SavePcbCopy(const wxString &aFileName, bool aCreateProject, bool aHeadless) override
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition kiway.h:315
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition pgm_base.h:130
Container for project specific data.
Definition project.h:66
void SaveProjectAs(const wxString &aFullPath, PROJECT *aProject=nullptr)
Set the currently loaded project path and saves it (pointers remain valid).
Master controller class:
static const std::string ProjectFileExtension
STL namespace.
PGM_BASE & Pgm()
The global program "get" accessor.