KiCad PCB EDA Suite
Loading...
Searching...
No Matches
headless_pcb_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
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
22#include <board.h>
23#include <board_loader.h>
25#include <common.h>
27#include <drc/drc_engine.h>
28#include <footprint.h>
29#include <gestfich.h>
33#include <pcbnew_settings.h>
34#include <pgm_base.h>
35#include <project.h>
36#include <reporter.h>
38#include <spread_footprints.h>
39#include <tool/tool_manager.h>
40#include <tools/drc_tool.h>
41#include <wx/debug.h>
42
43
44HEADLESS_PCB_CONTEXT::HEADLESS_PCB_CONTEXT( std::unique_ptr<BOARD> aBoard, PROJECT* aProject,
45 APP_SETTINGS_BASE* aSettings,
46 KIWAY* aKiway ) :
47 m_board( std::move( aBoard ) ),
48 m_project( aProject ),
49 m_kiway( aKiway ),
50 m_toolManager( std::make_unique<TOOL_MANAGER>() )
51{
52 wxCHECK( m_board, /* void */ );
53 wxCHECK( m_project, /* void */ );
54
55 m_board->SetProject( m_project );
56 m_toolManager->SetEnvironment( m_board.get(), nullptr, nullptr, aSettings, nullptr );
57}
58
59
61{
62 // Sever the board↔project linkage before destruction. The PROJECT holds a raw pointer
63 // (m_BoardSettings) to the board's design settings. If the board is destroyed while the
64 // project still exists, that pointer becomes dangling.
65 if( m_board )
66 m_board->ClearProject();
67}
68
69
71{
72 return m_board.get();
73}
74
75
77{
78 // Shouldn't be able to construct this without a project
79 wxASSERT( m_project );
80 return *m_project;
81}
82
83
88
89
91{
92 if( !m_board )
93 return wxEmptyString;
94
95 return m_board->GetFileName();
96}
97
98
100{
101 m_contentModified = aModified;
102
103 // The editor does this from OnModify
104 if( aModified && m_board )
106}
107
108
110{
111 if( !m_board )
112 return false;
113
114 wxString fileName = m_board->GetFileName();
115
116 if( fileName.IsEmpty() )
117 return false;
118
119 bool success = BOARD_LOADER::SaveBoard( fileName, *m_board );
120
121 if( success )
122 {
123 m_contentModified = false;
124
125 wxFileName pro = fileName;
126 pro.SetExt( FILEEXT::ProjectFileExtension );
127 pro.MakeAbsolute();
128
129 Pgm().GetSettingsManager().SaveProjectAs( pro.GetFullPath(), m_board->GetProject() );
130 }
131
132 return success;
133}
134
135
136bool HEADLESS_PCB_CONTEXT::SaveBoardAs( const wxString& aFileName )
137{
138 if( !m_board || aFileName.IsEmpty() )
139 return false;
140
141 wxFileName boardPath( EnsureFileExtension( aFileName, FILEEXT::KiCadPcbFileExtension ) );
142 boardPath.MakeAbsolute();
143
144 if( boardPath.GetName().IsEmpty() )
145 return false;
146
147 if( ( !boardPath.DirExists() && !boardPath.Mkdir() ) || !boardPath.IsDirWritable() )
148 return false;
149
150 wxString outPath = boardPath.GetFullPath();
151
152 if( !BOARD_LOADER::SaveBoard( outPath, *m_board ) )
153 return false;
154
155 m_board->SetFileName( outPath );
156 m_contentModified = false;
157
158 wxFileName pro( boardPath );
159 pro.SetExt( FILEEXT::ProjectFileExtension );
160
161 Pgm().GetSettingsManager().SaveProjectAs( pro.GetFullPath(), m_board->GetProject() );
162
163 return true;
164}
165
166
167bool HEADLESS_PCB_CONTEXT::SavePcbCopy( const wxString& aFileName, bool aCreateProject, bool aHeadless )
168{
169 if( !m_board || aFileName.IsEmpty() )
170 return false;
171
172 wxFileName pro = aFileName;
173 pro.SetExt( FILEEXT::ProjectFileExtension );
174 pro.MakeAbsolute();
175
176 if( aCreateProject && pro.FileExists() )
177 return false;
178
179 wxFileName currentRules( m_board->GetDesignRulesPath() );
180 wxFileName rulesFile = aFileName;
181 rulesFile.SetExt( FILEEXT::DesignRulesFileExtension );
182 rulesFile.MakeAbsolute();
183
184 if( currentRules.FileExists() && rulesFile.FileExists() )
185 return false;
186
187 wxString outPath = aFileName;
188
189 bool success = BOARD_LOADER::SaveBoard( outPath, *m_board );
190
191 if( success && aCreateProject )
192 Pgm().GetSettingsManager().SaveProjectCopy( pro.GetFullPath(), m_board->GetProject() );
193
194 if( currentRules.FileExists() )
195 {
196 wxString msg;
197 KiCopyFile( currentRules.GetFullPath(), rulesFile.GetFullPath(), msg );
198 }
199
200 return success;
201}
202
203
204bool HEADLESS_PCB_CONTEXT::ReadNetlistFromFile( const wxString& aFilename, NETLIST& aNetlist, REPORTER& aReporter )
205{
206 wxString msg;
207
208 try
209 {
210 std::unique_ptr<NETLIST_READER> netlistReader(
211 NETLIST_READER::GetNetlistReader( &aNetlist, aFilename, wxEmptyString ) );
212
213 if( !netlistReader.get() )
214 {
215 msg.Printf( _( "Cannot open netlist file '%s'." ), aFilename );
216 aReporter.Report( msg, RPT_SEVERITY_ERROR );
217 return false;
218 }
219
220 netlistReader->LoadNetlist();
221 LoadNetlistFootprints( GetBoard(), aNetlist, aReporter );
222 }
223 catch( const IO_ERROR& ioe )
224 {
225 msg.Printf( _( "Error loading netlist.\n%s" ), ioe.What().GetData() );
226 aReporter.Report( msg, RPT_SEVERITY_ERROR );
227 return false;
228 }
229
230 return true;
231}
232
233
234std::unique_ptr<BOARD_NETLIST_UPDATER> HEADLESS_PCB_CONTEXT::MakeNetlistUpdater()
235{
236 return std::make_unique<BOARD_NETLIST_UPDATER>( GetToolManager(), GetBoard() );
237}
238
239
241{
242 BOARD* board = GetBoard();
243
244 // Re-sync nets and netclasses
245 board->SynchronizeNetsAndNetClasses( false );
246
247 // Recompute component classes
250
251 // Resync DRC rules to account for new aggregate netclass / component class rules
252 if( DRC_TOOL* drcTool = m_toolManager->GetTool<DRC_TOOL>() )
253 {
254 if( drcTool->GetDRCEngine() )
255 {
256 try
257 {
258 drcTool->GetDRCEngine()->InitEngine( board->GetDesignRulesPath() );
259 }
260 catch( PARSE_ERROR& )
261 {
262 }
263 }
264 }
265
266 std::vector<FOOTPRINT*> newFootprints = aUpdater.GetAddedFootprints();
267 SpreadFootprints( &newFootprints, { 0, 0 }, true );
268
269 board->CompileRatsnest();
270
272}
273
274
276{
277 wxCHECK( m_board && m_project, false );
278
279 wxString fileName = m_board->GetFileName();
280
281 if( fileName.IsEmpty() || !wxFileExists( fileName ) )
282 return false;
283
285
286 if( pluginType == PCB_IO_MGR::FILE_TYPE_NONE )
287 return false;
288
289 std::unique_ptr<BOARD> reloaded;
290
291 try
292 {
293 reloaded = BOARD_LOADER::Load( fileName, pluginType, m_project );
294 }
295 catch( ... )
296 {
297 return false;
298 }
299
300 if( !reloaded )
301 return false;
302
303 m_board = std::move( reloaded );
304 m_board->SetProject( m_project );
305 m_toolManager->SetEnvironment( m_board.get(), nullptr, nullptr, GetAppSettings<PCBNEW_SETTINGS>( "pcbnew" ),
306 nullptr );
307 m_contentModified = false;
308
309 return true;
310}
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)
static std::unique_ptr< BOARD > Load(const wxString &aFileName, PCB_IO_MGR::PCB_FILE_T aFormat, PROJECT *aProject, const OPTIONS &aOptions)
Update the BOARD with a new netlist.
std::vector< FOOTPRINT * > GetAddedFootprints() const
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:411
void CompileRatsnest()
Rebuild the entire board ratsnest.
Definition board.cpp:3923
void SynchronizeNetsAndNetClasses(bool aResetTrackAndViaSizes)
Copy NETCLASS info to each NET, based on NET membership in a NETCLASS.
Definition board.cpp:3268
wxString GetDesignRulesPath() const
Return the absolute path to the design rules file for this board.
Definition board.cpp:437
COMPONENT_CLASS_MANAGER & GetComponentClassManager()
Gets the component class manager.
Definition board.h:1708
void InvalidateComponentClasses()
Invalidates any caches component classes and recomputes caches if required.
void RebuildRequiredCaches(FOOTPRINT *aFootprint=nullptr) const
Rebuilds any caches that may be required by custom assignment rules.
BOARD * GetBoard() const override
bool SavePcbCopy(const wxString &aFileName, bool aCreateProject, bool aHeadless) override
void OnNetlistChanged(BOARD_NETLIST_UPDATER &aUpdater) override
Post-import board sync (nets, classes, DRC, ratsnest, new footprint placement).
PROJECT & Prj() const override
bool SaveBoardAs(const wxString &aFileName) override
Save the board to a new path and change the active board/project.
TOOL_MANAGER * GetToolManager() const override
std::unique_ptr< TOOL_MANAGER > m_toolManager
void SetContentModified(bool aModified=true) override
HEADLESS_PCB_CONTEXT(std::unique_ptr< BOARD > aBoard, PROJECT *aProject, APP_SETTINGS_BASE *aSettings, KIWAY *aKiway=nullptr)
std::unique_ptr< BOARD > m_board
std::unique_ptr< BOARD_NETLIST_UPDATER > MakeNetlistUpdater() override
Create a netlist updater bound to this context's board.
bool ReadNetlistFromFile(const wxString &aFilename, NETLIST &aNetlist, REPORTER &aReporter) override
Read a netlist file and preload component footprints.
wxString GetCurrentFileName() const override
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
virtual const wxString What() const
A composite of Problem() and Where()
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition kiway.h:348
static NETLIST_READER * GetNetlistReader(NETLIST *aNetlist, const wxString &aNetlistFileName, const wxString &aCompFootprintFileName=wxEmptyString)
Attempt to determine the net list file type of aNetlistFileName and return the appropriate NETLIST_RE...
Store information read from a netlist along with the flags used to update the NETLIST in the BOARD.
PCB_FILE_T
The set of file types that the PCB_IO_MGR knows about, and for which there has been a plugin written,...
Definition pcb_io_mgr.h:52
static PCB_FILE_T FindPluginTypeFromBoardPath(const wxString &aFileName, int aCtl=0)
Return a plugin type given a path for a board file.
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition pgm_base.h:124
Container for project specific data.
Definition project.h:63
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:73
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Report a string with a given severity.
Definition reporter.h:102
void SaveProjectAs(const wxString &aFullPath, PROJECT *aProject=nullptr)
Set the currently loaded project path and saves it (pointers remain valid).
bool SaveProjectCopy(const wxString &aFullPath, PROJECT *aProject=nullptr)
Save a copy of the current project under the given path.
Master controller class:
wxString EnsureFileExtension(const wxString &aFilename, const wxString &aExtension)
It's annoying to throw up nag dialogs when the extension isn't right.
Definition common.cpp:880
#define _(s)
int RefreshGeneratedTables(BOARD &aBoard)
Bring every generated table on the board up to date and return how many were rebuilt.
void KiCopyFile(const wxString &aSrcPath, const wxString &aDestPath, wxString &aErrors)
Definition gestfich.cpp:416
static const std::string ProjectFileExtension
static const std::string DesignRulesFileExtension
static const std::string KiCadPcbFileExtension
#define KICTL_KICAD_ONLY
chosen file is from KiCad according to user
STL namespace.
void LoadNetlistFootprints(BOARD *aBoard, NETLIST &aNetlist, REPORTER &aReporter)
Load the footprints for each SCH_SYMBOL in aNetlist from the list of libraries.
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
@ RPT_SEVERITY_ERROR
T * GetAppSettings(const char *aFilename)
void SpreadFootprints(std::vector< FOOTPRINT * > *aFootprints, const VECTOR2I &aTargetBoxPosition, bool aGroupBySheet, int aComponentGap, int aGroupGap)
Footprints (after loaded by reading a netlist for instance) are moved to be in a small free area (out...
A filename or source description, a problem input line, a line number, a byte offset,...