KiCad PCB EDA Suite
Loading...
Searching...
No Matches
board_loader.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
21#include <board_loader.h>
22
23#include <base_screen.h>
24#include <board.h>
28#include <drc/drc_engine.h>
29#include <filename_resolver.h>
30#include <ki_exception.h>
31#include <pcb_marker.h>
32#include <pgm_base.h>
33#include <project.h>
35#include <properties/property.h>
36#include <reporter.h>
38#include <unordered_set>
39#include <wx/image.h>
40#include <wx/log.h>
41
42
43std::unique_ptr<BOARD> BOARD_LOADER::Load( const wxString& aFileName, PCB_IO_MGR::PCB_FILE_T aFormat,
44 PROJECT* aProject, const OPTIONS& aOptions )
45{
46 if( !aProject || aFormat == PCB_IO_MGR::FILE_TYPE_NONE )
47 return nullptr;
48
49 // TODO(JE) need to factor out for MDI
51
52 std::unique_ptr<BOARD> board;
53
54 if( aOptions.plugin_configurator || aOptions.reporter || aOptions.post_load_hook )
55 {
57
58 if( !pi )
59 return nullptr;
60
61 if( aOptions.plugin_configurator )
62 aOptions.plugin_configurator( *pi );
63
64 if( aOptions.reporter )
65 pi->SetReporter( aOptions.reporter );
66
67 pi->SetProgressReporter( aOptions.progress_reporter );
68
69 // own the board first so a throwing hook can't leak it
70 board = pi->LoadBoard( aFileName, aOptions.properties, aProject );
71
72 // grab cached lib footprints while the plugin is alive
73 if( board && aOptions.post_load_hook )
74 aOptions.post_load_hook( *pi );
75 }
76 else
77 {
78 board = PCB_IO_MGR::Load( aFormat, aFileName, aOptions.properties, aProject, aOptions.progress_reporter );
79 }
80
81 if( !board )
82 return nullptr;
83
84 if( aOptions.initialize_after_load )
85 initializeLoadedBoard( board.get(), aFileName, aProject, aOptions );
86
87 return board;
88}
89
90
91std::unique_ptr<BOARD> BOARD_LOADER::Load( const wxString& aFileName,
93 PROJECT* aProject )
94{
95 return Load( aFileName, aFormat, aProject, OPTIONS{} );
96}
97
98
99void BOARD_LOADER::initializeLoadedBoard( BOARD* aBoard, const wxString& aFileName,
100 PROJECT* aProject, const OPTIONS& aOptions )
101{
102 if( !aBoard || !aProject )
103 return;
104
105 FILENAME_RESOLVER resolver;
106 resolver.SetProject( aProject );
107 resolver.SetProgramBase( PgmOrNull() );
108
109 wxString filename = resolver.ResolvePath( BASE_SCREEN::m_DrawingSheetFileName,
110 aProject->GetProjectPath(),
111 { aBoard->GetEmbeddedFiles() } );
112
113 wxString msg;
114
115 if( !DS_DATA_MODEL::GetTheInstance().LoadDrawingSheet( filename, &msg ) )
116 {
117 if( aOptions.drawing_sheet_error_callback )
119 }
120
122
123 layerEnum.Choices().Clear();
124 layerEnum.Undefined( UNDEFINED_LAYER );
125
126 for( PCB_LAYER_ID layer : LSET::AllLayersMask() )
127 {
128 layerEnum.Map( layer, LSET::Name( layer ) );
129 layerEnum.Map( layer, aBoard->GetLayerName( layer ) );
130 }
131
132 aBoard->SetProject( aProject );
133
135 bds.m_DRCEngine = std::make_shared<DRC_ENGINE>( aBoard, &bds );
136
137 try
138 {
139 wxFileName rules = aFileName;
140 rules.SetExt( FILEEXT::DesignRulesFileExtension );
141 bds.m_DRCEngine->InitEngine( rules );
142 }
143 catch( ... )
144 {
145 // Only matters if user tries to run DRC later; will be reported at that time
146 }
147
148 for( PCB_MARKER* marker : aBoard->ResolveDRCExclusions( true ) )
149 aBoard->Add( marker );
150
151 aBoard->BuildConnectivity();
152 aBoard->BuildListOfNets();
153 aBoard->SynchronizeNetsAndNetClasses( true );
154
155 // Apply component class assignment rules from the project. Without this, conditions like
156 // A.hasComponentClass('Inductor') in custom DRC rules never match because no footprints
157 // have any dynamic component class assigned. The interactive load path does this in
158 // PCB_EDIT_FRAME::OpenProjectFiles; CLI and API consumers go through here.
159 if( !aBoard->SynchronizeComponentClasses( std::unordered_set<wxString>() )
160 && aOptions.reporter )
161 {
162 aOptions.reporter->Report( _( "Could not load component class assignment rules" ),
164 }
165
167
168 aBoard->UpdateUserUnits( aBoard, nullptr );
169}
170
171
172std::unique_ptr<BOARD> BOARD_LOADER::CreateEmptyBoard( PROJECT* aProject )
173{
174 std::unique_ptr<BOARD> brd = std::make_unique<BOARD>();
175 brd->SetProject( aProject );
176
177 BOARD_DESIGN_SETTINGS& bds = brd->GetDesignSettings();
178 bds.m_DRCEngine = std::make_shared<DRC_ENGINE>( brd.get(), &bds );
179
180 return brd;
181}
182
183
184bool BOARD_LOADER::SaveBoard( wxString& aFileName, BOARD& aBoard, PCB_IO_MGR::PCB_FILE_T aFormat )
185{
186 aBoard.BuildConnectivity();
187 aBoard.SynchronizeNetsAndNetClasses( false );
188 aBoard.SynchronizeProperties();
189 RefreshGeneratedTables( aBoard );
190
191 try
192 {
193 PCB_IO_MGR::Save( aFormat, aFileName, aBoard, nullptr );
194 }
195 catch( const IO_ERROR& ioe )
196 {
197 wxLogError( _( "Cannot save board '%s': %s" ), aFileName, ioe.What() );
198 return false;
199 }
200 catch( const std::exception& e )
201 {
202 // Rethrow so std::bad_alloc and similar aren't silently turned into a false return.
203 wxLogError( _( "Unexpected error saving board '%s': %s" ), aFileName,
204 wxString::FromUTF8( e.what() ) );
205 throw;
206 }
207
208 return true;
209}
210
211
212bool BOARD_LOADER::SaveBoard( wxString& aFileName, BOARD& aBoard )
213{
214 return SaveBoard( aFileName, aBoard, PCB_IO_MGR::KICAD_SEXP );
215}
BASE_SCREEN class implementation.
static wxString m_DrawingSheetFileName
the name of the drawing sheet file, or empty to use the default drawing sheet
Definition base_screen.h:81
Container for design settings for a BOARD object.
std::shared_ptr< DRC_ENGINE > m_DRCEngine
static bool SaveBoard(wxString &aFileName, BOARD &aBoard, PCB_IO_MGR::PCB_FILE_T aFormat)
static std::unique_ptr< BOARD > CreateEmptyBoard(PROJECT *aProject)
static std::unique_ptr< BOARD > Load(const wxString &aFileName, PCB_IO_MGR::PCB_FILE_T aFormat, PROJECT *aProject, const OPTIONS &aOptions)
static void initializeLoadedBoard(BOARD *aBoard, const wxString &aFileName, PROJECT *aProject, const OPTIONS &aOptions)
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:411
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false) override
Removes an item from the container.
Definition board.cpp:1548
void BuildListOfNets()
Definition board.h:1189
void UpdateUserUnits(BOARD_ITEM *aItem, KIGFX::VIEW *aView)
Update any references within aItem (or its descendants) to the user units.
Definition board.cpp:2078
bool BuildConnectivity(PROGRESS_REPORTER *aReporter=nullptr)
Build or rebuild the board connectivity database for the board, especially the list of connected item...
Definition board.cpp:369
void SynchronizeNetsAndNetClasses(bool aResetTrackAndViaSizes)
Copy NETCLASS info to each NET, based on NET membership in a NETCLASS.
Definition board.cpp:3268
void SetProject(PROJECT *aProject, bool aReferenceOnly=false)
Link a board to a given project.
Definition board.cpp:379
std::vector< PCB_MARKER * > ResolveDRCExclusions(bool aCreateMarkers)
Rebuild DRC markers from the serialized data in BOARD_DESIGN_SETTINGS.
Definition board.cpp:595
const wxString GetLayerName(PCB_LAYER_ID aLayer) const
Return the name of a aLayer.
Definition board.cpp:960
void SynchronizeProperties()
Copy the current project's text variables into the boards property cache.
Definition board.cpp:2996
bool SynchronizeComponentClasses(const std::unordered_set< wxString > &aNewSheetPaths) const
Copy component class / component class generator information from the project settings.
Definition board.cpp:3312
void SynchronizeTuningProfileProperties()
Ensure that all time domain properties providers are in sync with current settings.
Definition board.cpp:3222
BOARD_DESIGN_SETTINGS & GetDesignSettings()
Definition board.cpp:1318
void InitEngine(const wxFileName &aRulePath)
Initialize the DRC engine.
static DS_DATA_MODEL & GetTheInstance()
Return the instance of DS_DATA_MODEL used in the application.
ENUM_MAP & Map(T aValue, const wxString &aName)
Definition property.h:817
static ENUM_MAP< T > & Instance()
Definition property.h:811
ENUM_MAP & Undefined(T aValue)
Definition property.h:824
wxPGChoices & Choices()
Definition property.h:862
Provide an extensible class to resolve 3D model paths.
wxString ResolvePath(const wxString &aFileName, const wxString &aWorkingPath, std::vector< const EMBEDDED_FILES * > aEmbeddedFilesStack)
Determine the full path of the given file name.
void SetProgramBase(PGM_BASE *aBase)
Set a pointer to the application's PGM_BASE instance used to extract the local env vars.
bool SetProject(const PROJECT *aProject, bool *flgChanged=nullptr)
Set the current KiCad project directory as the first entry in the model path list.
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()
static const LSET & AllLayersMask()
Definition lset.cpp:637
static wxString Name(PCB_LAYER_ID aLayerId)
Return the fixed name association with aLayerId.
Definition lset.cpp:184
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
@ KICAD_SEXP
S-expression Pcbnew file format.
Definition pcb_io_mgr.h:54
static void Save(PCB_FILE_T aFileType, const wxString &aFileName, BOARD &aBoard, const std::map< std::string, UTF8 > *aProperties=nullptr)
Write either a full aBoard to a storage file in a format that this implementation knows about,...
static PCB_IO * FindPlugin(PCB_FILE_T aFileType)
Return a PCB_IO which the caller can use to import, export, save, or load design documents.
static std::unique_ptr< BOARD > Load(PCB_FILE_T aFileType, const wxString &aFileName, const std::map< std::string, UTF8 > *aProperties=nullptr, PROJECT *aProject=nullptr, PROGRESS_REPORTER *aProgressReporter=nullptr)
Find the requested PCB_IO and if found, calls the PCB_IO::LoadBoard() function on it using the argume...
wxString m_BoardDrawingSheetFile
PcbNew params.
Container for project specific data.
Definition project.h:63
virtual const wxString GetProjectPath() const
Return the full path of the project.
Definition project.cpp:277
virtual PROJECT_FILE & GetProjectFile() const
Definition project.h:201
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Report a string with a given severity.
Definition reporter.h:102
#define _(s)
int RefreshGeneratedTables(BOARD &aBoard)
Bring every generated table on the board up to date and return how many were rebuilt.
static const std::string DesignRulesFileExtension
std::unique_ptr< T > IO_RELEASER
Helper to hold and release an IO_BASE object when exceptions are thrown.
Definition io_mgr.h:33
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
@ UNDEFINED_LAYER
Definition layer_ids.h:57
PGM_BASE * PgmOrNull()
Return a reference that can be nullptr when running a shared lib from a script, not from a kicad app.
see class PGM_BASE
@ RPT_SEVERITY_WARNING
std::function< void(const wxString &, const wxString &)> drawing_sheet_error_callback
std::function< void(PCB_IO &)> plugin_configurator
std::function< void(PCB_IO &)> post_load_hook
const std::map< std::string, UTF8 > * properties
PROGRESS_REPORTER * progress_reporter
Definition of file extensions used in Kicad.