KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_board_setup.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 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19#include <jobs/scratch_doc.h>
20#include <pcb_edit_frame.h>
21#include <panel_setup_layers.h>
28#include <confirm.h>
30#include <widgets/wx_infobar.h>
31#include <kiface_base.h>
32#include <drc/drc_item.h>
34#include <pcb_io/pcb_io.h>
35#include <pcb_io/pcb_io_mgr.h>
46#include <project.h>
52
53#include "dialog_board_setup.h"
54
55#include <advanced_config.h>
56#include <dialog_board_setup.h>
57#include <footprint.h>
58
59
60#define RESOLVE_PAGE( T, pageIndex ) static_cast<T*>( m_treebook->ResolvePage( pageIndex ) )
61
62DIALOG_BOARD_SETUP::DIALOG_BOARD_SETUP( PCB_EDIT_FRAME* aFrame, wxWindow* aParentWindow ) :
63 PAGED_DIALOG( aParentWindow ? aParentWindow : aFrame, _( "Board Setup" ), false, false,
64 _( "Import Settings from Another Board..." ), wxSize( 980, 600 ) ),
65 m_frame( aFrame ),
66 m_layers( nullptr ),
67 m_boardFinish( nullptr ),
68 m_physicalStackup( nullptr ),
69 m_zones( nullptr ),
70 m_tuningProfiles( nullptr ),
71 m_netClasses( nullptr ),
72 m_currentPage( 0 ),
73 m_layersPage( 0 ),
76 m_defaultsPage( 0 ),
81 m_teardropsPage( 0 ),
87{
88 SetEvtHandlerEnabled( false );
89
90 /*
91 * WARNING: If you change page names you MUST update calls to ShowBoardSetupDialog().
92 */
93
94 m_treebook->AddPage( new wxPanel( GetTreebook() ), _( "Board Stackup" ) );
95
96 /*
97 * WARNING: Code currently relies on the layers setup coming before the physical stackup panel,
98 * and thus transferring data to the board first. See comment in
99 * PANEL_SETUP_BOARD_STACKUP::TransferDataFromWindow and rework this logic if it is determined
100 * that the order of these pages should be changed.
101 */
102 m_layersPage = m_treebook->GetPageCount();
103 m_treebook->AddLazySubPage(
104 [this]( wxWindow* aParent ) -> wxWindow*
105 {
106 return new PANEL_SETUP_LAYERS( aParent, m_frame );
107 }, _( "Board Editor Layers" ) );
108
109 m_physicalStackupPage = m_treebook->GetPageCount();
110 m_treebook->AddLazySubPage(
111 [this]( wxWindow* aParent ) -> wxWindow*
112 {
116 }, _( "Physical Stackup" ) );
117
118 m_boardFinishPage = m_treebook->GetPageCount();
119 m_treebook->AddLazySubPage(
120 [this]( wxWindow* aParent ) -> wxWindow*
121 {
122 return new PANEL_SETUP_BOARD_FINISH( aParent, m_frame );
123 }, _( "Board Finish" ) );
124
125 m_maskAndPastePage = m_treebook->GetPageCount();
126 m_treebook->AddLazySubPage(
127 [this]( wxWindow* aParent ) -> wxWindow*
128 {
129 return new PANEL_SETUP_MASK_AND_PASTE( aParent, m_frame );
130 }, _( "Solder Mask/Paste" ) );
131
132 m_treebook->AddPage( new wxPanel( GetTreebook() ), _( "Text & Graphics" ) );
133
134 m_defaultsPage = m_treebook->GetPageCount();
135 m_treebook->AddLazySubPage(
136 [this]( wxWindow* aParent ) -> wxWindow*
137 {
138 return new PANEL_SETUP_DEFAULTS( aParent, m_frame );
139 }, _( "Defaults" ) );
140
141 m_formattingPage = m_treebook->GetPageCount();
142 m_treebook->AddLazySubPage(
143 [this]( wxWindow* aParent ) -> wxWindow*
144 {
145 return new PANEL_SETUP_FORMATTING( aParent, m_frame );
146 }, _( "Formatting" ) );
147
148 m_treebook->AddLazySubPage(
149 [this]( wxWindow* aParent ) -> wxWindow*
150 {
151 return new PANEL_TEXT_VARIABLES( aParent, &Prj() );
152 }, _( "Text Variables" ) );
153
154 m_treebook->AddPage( new wxPanel( GetTreebook() ), _( "Design Rules" ) );
155
156 m_constraintsPage = m_treebook->GetPageCount();
157 m_treebook->AddLazySubPage(
158 [this]( wxWindow* aParent ) -> wxWindow*
159 {
160 return new PANEL_SETUP_CONSTRAINTS( aParent, m_frame );
161 }, _( "Constraints" ) );
162
163 m_tracksAndViasPage = m_treebook->GetPageCount();
164 m_treebook->AddLazySubPage(
165 [this]( wxWindow* aParent ) -> wxWindow*
166 {
167 return new PANEL_SETUP_TRACKS_AND_VIAS( aParent, m_frame );
168 }, _( "Pre-defined Sizes" ) );
169
170 m_zonesPage = m_treebook->GetPageCount();
171 m_treebook->AddLazySubPage(
172 [this]( wxWindow* aParent ) -> wxWindow*
173 {
174 return new PANEL_SETUP_ZONES( aParent, m_frame, m_frame->GetBoard()->GetDesignSettings() );
175 },
176 _( "Zones" ) );
177
178 m_teardropsPage = m_treebook->GetPageCount();
179 m_treebook->AddLazySubPage(
180 [this]( wxWindow* aParent ) -> wxWindow*
181 {
182 return new PANEL_SETUP_TEARDROPS( aParent, m_frame );
183 }, _( "Teardrops" ) );
184
185 m_tuningPatternsPage = m_treebook->GetPageCount();
186 m_treebook->AddLazySubPage(
187 [this]( wxWindow* aParent ) -> wxWindow*
188 {
189 BOARD_DESIGN_SETTINGS& bds = m_frame->GetBoard()->GetDesignSettings();
190
191 return new PANEL_SETUP_TUNING_PATTERNS( aParent, m_frame,
195 }, _( "Length-tuning Patterns" ) );
196
197 m_tuningProfilesPage = m_treebook->GetPageCount();
198 m_treebook->AddLazySubPage(
199 [this]( wxWindow* aParent ) -> wxWindow*
200 {
201 BOARD* board = m_frame->GetBoard();
202 return new PANEL_SETUP_TUNING_PROFILES( aParent, m_frame, board,
203 m_frame->Prj().GetProjectFile().TuningProfileParameters() );
204 },
205 _( "Tuning Profiles" ) );
206
207 m_netclassesPage = m_treebook->GetPageCount();
208 m_treebook->AddLazySubPage(
209 [this]( wxWindow* aParent ) -> wxWindow*
210 {
211 BOARD* board = m_frame->GetBoard();
212 return new PANEL_SETUP_NETCLASSES( aParent, m_frame,
213 m_frame->Prj().GetProjectFile().NetSettings(),
215 false );
216 }, _( "Net Classes" ) );
217
218 m_componentClassesPage = m_treebook->GetPageCount();
219 m_treebook->AddLazySubPage(
220 [this]( wxWindow* aParent ) -> wxWindow*
221 {
222 // Construct the panel
224 aParent, m_frame, m_frame->Prj().GetProjectFile().ComponentClassSettings(),
225 this );
226 },
227 _( "Component Classes" ) );
228
229 m_customRulesPage = m_treebook->GetPageCount();
230 m_treebook->AddLazySubPage(
231 [this]( wxWindow* aParent ) -> wxWindow*
232 {
233 return new PANEL_SETUP_RULES( aParent, m_frame );
234 },
235 _( "Custom Rules" ) );
236
237 m_severitiesPage = m_treebook->GetPageCount();
238 m_treebook->AddLazySubPage(
239 [this]( wxWindow* aParent ) -> wxWindow*
240 {
241 BOARD* board = m_frame->GetBoard();
244 },
245 _( "Violation Severity" ) );
246
247 m_treebook->AddPage( new wxPanel( GetTreebook() ), _( "Board Data" ) );
248 m_embeddedFilesPage = m_treebook->GetPageCount();
249 m_treebook->AddLazySubPage(
250 [this]( wxWindow* aParent ) -> wxWindow*
251 {
252 BOARD* board = m_frame->GetBoard();
253
254 std::vector<const EMBEDDED_FILES*> inheritedFiles;
255
256 for( FOOTPRINT* fp : board->Footprints() )
257 inheritedFiles.push_back( fp->GetEmbeddedFiles() );
258
259 return new PANEL_EMBEDDED_FILES( aParent, board, NO_MARGINS, inheritedFiles );
260 },
261 _( "Embedded Files" ) );
262
263 for( size_t i = 0; i < m_treebook->GetPageCount(); ++i )
264 m_treebook->ExpandNode( i );
265
266 SetEvtHandlerEnabled( true );
267
269
270 if( Prj().IsReadOnly() )
271 {
272 m_infoBar->ShowMessage( _( "Project is missing or read-only. Some settings will not be editable." ),
273 wxICON_WARNING );
274 }
275
276 wxBookCtrlEvent evt( wxEVT_TREEBOOK_PAGE_CHANGED, wxID_ANY, 0 );
277
278 wxQueueEvent( m_treebook, evt.Clone() );
279}
280
281
285
286
287void DIALOG_BOARD_SETUP::onPageChanged( wxBookCtrlEvent& aEvent )
288{
290
291 size_t page = aEvent.GetSelection();
292
293 if( m_physicalStackupPage > 0 ) // Don't run this during initialization
294 {
296 || page == m_tuningProfilesPage || page == m_zonesPage )
297 {
303 }
304
305 // Ensure layer page always gets updated even if we aren't moving towards it
307 {
308 m_layers->SyncCopperLayers( m_physicalStackup->GetCopperLayerCount() );
309
310 // Avoid calling SyncCopperLayers twice if moving from stackup to tuning profiles directly
311 m_tuningProfiles->SyncCopperLayers( m_physicalStackup->GetCopperLayerCount() );
312 }
313
314 if( page == m_physicalStackupPage )
315 {
316 m_physicalStackup->OnLayersOptionsChanged( m_layers->GetUILayerMask() );
317 }
319 {
320 m_netClasses->UpdateDelayProfileNames( m_tuningProfiles->GetDelayProfileNames() );
321 }
322 else if( page == m_tuningProfilesPage )
323 {
324 m_tuningProfiles->SyncCopperLayers( m_physicalStackup->GetCopperLayerCount() );
325 }
326 else if( page == m_zonesPage )
327 {
328 m_zones->SyncCopperLayers( m_physicalStackup->GetCopperLayerCount() );
329 }
330
331 if( Prj().IsReadOnly() )
332 {
333 KIUI::Disable( m_treebook->GetPage( page ) );
334 }
335 }
336
337 m_currentPage = page;
338}
339
340
341void DIALOG_BOARD_SETUP::onAuxiliaryAction( wxCommandEvent& aEvent )
342{
343 DIALOG_IMPORT_SETTINGS importDlg( this, m_frame );
344
345 if( importDlg.ShowModal() == wxID_CANCEL )
346 return;
347
348 wxFileName boardFn( importDlg.GetFilePath() );
349
350 SCRATCH_PROJECT scratch( *m_frame->GetSettingsManager(), boardFn.GetFullPath(),
351 /*aRequireProjectFile=*/true );
352
353 if( !scratch.IsValid() )
354 {
355 wxFileName projectFn( boardFn );
356 projectFn.SetExt( FILEEXT::ProjectFileExtension );
357
358 wxString msg = wxString::Format( _( "Error importing settings from board:\n"
359 "Associated project file %s could not be loaded" ),
360 projectFn.GetFullPath() );
361 DisplayErrorMessage( this, msg );
362
363 return;
364 }
365
366 PROJECT* otherPrj = scratch.GetProject();
367
371
372 // Flag so user can stop work if it will result in deleted inner copper layers
373 // and still clean up this function properly.
374 bool okToProceed = true;
375
377 std::unique_ptr<BOARD> otherBoard;
378
379 try
380 {
381 WX_PROGRESS_REPORTER progressReporter( this, _( "Load PCB" ), 1, PR_CAN_ABORT );
382
383 pi->SetProgressReporter( &progressReporter );
384
385 otherBoard.reset( pi->LoadBoard( boardFn.GetFullPath(), nullptr ) );
386
387 if( importDlg.m_LayersOpt->GetValue() )
388 {
389 BOARD* loadedBoard = m_frame->GetBoard();
390
391 // Check if "Import Settings" board has more layers than the current board.
392 okToProceed = m_layers->CheckCopperLayerCount( loadedBoard, otherBoard.get() );
393 }
394 }
395 catch( const IO_ERROR& ioe )
396 {
397 // You wouldn't think boardFn.GetFullPath() would throw, but we get a stack buffer
398 // underflow from ASAN. While it's probably an ASAN error, a second try/catch doesn't
399 // cost us much.
400 try
401 {
402 if( ioe.Problem() != wxT( "CANCEL" ) )
403 {
404 wxString msg = wxString::Format( _( "Error loading board file:\n%s" ), boardFn.GetFullPath() );
405 DisplayErrorMessage( this, msg, ioe.What() );
406 }
407 }
408 catch(...)
409 {
410 // That was already our best-efforts
411 }
412
413 return;
414 }
415
416 if( okToProceed )
417 {
418 BOARD* other = otherBoard.get();
419
420 other->SetProject( otherPrj );
421
422 // Always ClearProject before destruction, even if a later
423 // ImportSettingsFrom throws. Skipping clear when otherPrj is the
424 // active project preserves the live editor's BoardSettings.
425 const bool activeProject = ( otherPrj == &m_frame->Prj() );
426
427 struct BOARD_DETACH_GUARD
428 {
429 BOARD* board;
430 bool skip;
431 ~BOARD_DETACH_GUARD() { if( board && !skip ) board->ClearProject(); }
432 } detachGuard{ other, activeProject };
433
434 // If layers options are imported, import also the stackup
435 // layers options and stackup are linked, so they cannot be imported
436 // separately, and stackup can be imported only after layers options
437 if( importDlg.m_LayersOpt->GetValue() )
438 {
439 m_physicalStackup->ImportSettingsFrom( other );
440 m_layers->ImportSettingsFrom( other );
441 m_boardFinish->ImportSettingsFrom( other );
442 }
443
444 if( importDlg.m_TextAndGraphicsOpt->GetValue() )
445 RESOLVE_PAGE( PANEL_SETUP_DEFAULTS, m_defaultsPage )->ImportSettingsFrom( other );
446
447 if( importDlg.m_FormattingOpt->GetValue() )
448 RESOLVE_PAGE( PANEL_SETUP_FORMATTING, m_formattingPage )->ImportSettingsFrom( other );
449
450 if( importDlg.m_ConstraintsOpt->GetValue() )
451 RESOLVE_PAGE( PANEL_SETUP_CONSTRAINTS, m_constraintsPage )->ImportSettingsFrom( other );
452
453 if( importDlg.m_NetclassesOpt->GetValue() )
454 {
455 PROJECT_FILE& otherProjectFile = otherPrj->GetProjectFile();
456
458 m_netclassesPage )->ImportSettingsFrom( otherProjectFile.m_NetSettings );
459 }
460
461 if( importDlg.m_ComponentClassesOpt->GetValue() )
462 {
463 PROJECT_FILE& otherProjectFile = otherPrj->GetProjectFile();
464
466 m_componentClassesPage )->ImportSettingsFrom( otherProjectFile.m_ComponentClassSettings );
467 }
468
469 if( importDlg.m_TracksAndViasOpt->GetValue() )
470 RESOLVE_PAGE( PANEL_SETUP_TRACKS_AND_VIAS, m_tracksAndViasPage )->ImportSettingsFrom( other );
471
472 if( importDlg.m_ZonesOpt->GetValue() )
473 RESOLVE_PAGE( PANEL_SETUP_ZONES, m_zonesPage )->ImportSettingsFrom( other );
474
475 if( importDlg.m_TeardropsOpt->GetValue() )
476 RESOLVE_PAGE( PANEL_SETUP_TEARDROPS, m_teardropsPage )->ImportSettingsFrom( other );
477
478 if( importDlg.m_TuningPatternsOpt->GetValue() )
479 RESOLVE_PAGE( PANEL_SETUP_TUNING_PATTERNS, m_tuningPatternsPage )->ImportSettingsFrom( other );
480
481 if( importDlg.m_MaskAndPasteOpt->GetValue() )
482 RESOLVE_PAGE( PANEL_SETUP_MASK_AND_PASTE, m_maskAndPastePage )->ImportSettingsFrom( other );
483
484 if( importDlg.m_ZoneHatchingOffsetsOpt->GetValue() )
485 RESOLVE_PAGE( PANEL_SETUP_ZONES, m_zonesPage )->ImportHatchOffsetsFrom( other );
486
487 if( importDlg.m_CustomRulesOpt->GetValue() )
488 RESOLVE_PAGE( PANEL_SETUP_RULES, m_customRulesPage )->ImportSettingsFrom( other );
489
490 if( importDlg.m_SeveritiesOpt->GetValue() )
491 {
492 BOARD_DESIGN_SETTINGS& otherSettings = other->GetDesignSettings();
493
495 m_severitiesPage )->ImportSettingsFrom( otherSettings.m_DRCSeverities );
496 }
497
498 if( importDlg.m_TuningProfilesOpt->GetValue() )
499 {
500 PROJECT_FILE& otherProjectFile = otherPrj->GetProjectFile();
501
503 m_tuningProfilesPage )->ImportSettingsFrom( otherProjectFile.TuningProfileParameters() );
504 }
505
506 }
507}
Container for design settings for a BOARD object.
std::map< int, SEVERITY > m_DRCSeverities
PNS::MEANDER_SETTINGS m_DiffPairMeanderSettings
PNS::MEANDER_SETTINGS m_SingleTrackMeanderSettings
PNS::MEANDER_SETTINGS m_SkewMeanderSettings
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:372
std::set< wxString > GetNetClassAssignmentCandidates() const
Return the set of netname candidates for netclass assignment.
Definition board.cpp:2795
const FOOTPRINTS & Footprints() const
Definition board.h:420
void SetProject(PROJECT *aProject, bool aReferenceOnly=false)
Link a board to a given project.
Definition board.cpp:211
void ClearProject()
Definition board.cpp:252
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition board.cpp:1149
DIALOG_BOARD_SETUP(PCB_EDIT_FRAME *aFrame, wxWindow *aWindow=nullptr)
PCB_EDIT_FRAME * m_frame
PANEL_SETUP_TUNING_PROFILES * m_tuningProfiles
void onAuxiliaryAction(wxCommandEvent &aEvent) override
PANEL_SETUP_BOARD_STACKUP * m_physicalStackup
PANEL_SETUP_ZONES * m_zones
PANEL_SETUP_BOARD_FINISH * m_boardFinish
PANEL_SETUP_LAYERS * m_layers
void onPageChanged(wxBookCtrlEvent &aEvent) override
PANEL_SETUP_NETCLASSES * m_netClasses
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
int ShowModal() override
static std::vector< std::reference_wrapper< RC_ITEM > > GetItemsWithSeverities()
Definition drc_item.h:142
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()
virtual const wxString Problem() const
what was the problem?
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
WX_TREEBOOK * GetTreebook()
WX_INFOBAR * m_infoBar
PAGED_DIALOG(wxWindow *aParent, const wxString &aTitle, bool aShowReset, bool aShowOpenFolder, const wxString &aAuxiliaryAction=wxEmptyString, const wxSize &aInitialSize=wxDefaultSize)
WX_TREEBOOK * m_treebook
virtual void onPageChanged(wxBookCtrlEvent &aEvent)
The main frame for Pcbnew.
@ KICAD_SEXP
S-expression Pcbnew file format.
Definition pcb_io_mgr.h:54
static PCB_IO * FindPlugin(PCB_FILE_T aFileType)
Return a #PLUGIN which the caller can use to import, export, save, or load design documents.
The backing store for a PROJECT, in JSON format.
std::shared_ptr< NET_SETTINGS > m_NetSettings
Net settings for this project (owned here)
std::shared_ptr< TUNING_PROFILES > & TuningProfileParameters()
std::shared_ptr< COMPONENT_CLASS_SETTINGS > m_ComponentClassSettings
Component class settings for the project (owned here)
Container for project specific data.
Definition project.h:62
virtual PROJECT_FILE & GetProjectFile() const
Definition project.h:200
Move-only RAII wrapper for "load a foreign PROJECT non-active and unload it on scope exit.
Definition scratch_doc.h:55
bool IsValid() const
True iff the project loaded (or was already loaded).
PROJECT * GetProject() const
Multi-thread safe progress reporter dialog, intended for use of tasks that parallel reporting back of...
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition confirm.cpp:217
This file is part of the common library.
#define RESOLVE_PAGE(T, pageIndex)
#define _(s)
static const std::string ProjectFileExtension
std::unique_ptr< T > IO_RELEASER
Helper to hold and release an IO_BASE object when exceptions are thrown.
Definition io_mgr.h:33
KICOMMON_API void Disable(wxWindow *aWindow)
Makes a window read-only.
#define NO_MARGINS
Definition of file extensions used in Kicad.
#define PR_CAN_ABORT