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>
30#include <confirm.h>
32#include <widgets/wx_infobar.h>
33#include <kiface_base.h>
34#include <drc/drc_item.h>
36#include <pcb_io/pcb_io.h>
37#include <pcb_io/pcb_io_mgr.h>
48#include <project.h>
55
56#include "dialog_board_setup.h"
57
58#include <advanced_config.h>
59#include <dialog_board_setup.h>
60#include <footprint.h>
61
62
63#define RESOLVE_PAGE( T, pageIndex ) static_cast<T*>( m_treebook->ResolvePage( pageIndex ) )
64
65DIALOG_BOARD_SETUP::DIALOG_BOARD_SETUP( PCB_EDIT_FRAME* aFrame, wxWindow* aParentWindow ) :
66 PAGED_DIALOG( aParentWindow ? aParentWindow : aFrame, _( "Board Setup" ), false, false,
67 _( "Import Settings from Another Board..." ), wxSize( 980, 600 ) ),
68 m_frame( aFrame ),
69 m_layers( nullptr ),
70 m_boardFinish( nullptr ),
71 m_physicalStackup( nullptr ),
72 m_zones( nullptr ),
73 m_tuningProfiles( nullptr ),
74 m_netClasses( nullptr ),
75 m_currentPage( 0 ),
76 m_layersPage( 0 ),
79 m_defaultsPage( 0 ),
84 m_viaStacksPage( 0 ),
85 m_teardropsPage( 0 ),
91{
92 SetEvtHandlerEnabled( false );
93
94 /*
95 * WARNING: If you change page names you MUST update calls to ShowBoardSetupDialog().
96 */
97
98 m_treebook->AddPage( new wxPanel( GetTreebook() ), _( "Board Stackup" ) );
99
100 /*
101 * WARNING: Code currently relies on the layers setup coming before the physical stackup panel,
102 * and thus transferring data to the board first. See comment in
103 * PANEL_SETUP_BOARD_STACKUP::TransferDataFromWindow and rework this logic if it is determined
104 * that the order of these pages should be changed.
105 */
106 m_layersPage = m_treebook->GetPageCount();
107 m_treebook->AddLazySubPage(
108 [this]( wxWindow* aParent ) -> wxWindow*
109 {
110 return new PANEL_SETUP_LAYERS( aParent, m_frame );
111 }, _( "Board Editor Layers" ) );
112
113 m_physicalStackupPage = m_treebook->GetPageCount();
114 m_treebook->AddLazySubPage(
115 [this]( wxWindow* aParent ) -> wxWindow*
116 {
120 }, _( "Physical Stackup" ) );
121
122 m_boardFinishPage = m_treebook->GetPageCount();
123 m_treebook->AddLazySubPage(
124 [this]( wxWindow* aParent ) -> wxWindow*
125 {
126 return new PANEL_SETUP_BOARD_FINISH( aParent, m_frame );
127 }, _( "Board Finish" ) );
128
129 m_maskAndPastePage = m_treebook->GetPageCount();
130 m_treebook->AddLazySubPage(
131 [this]( wxWindow* aParent ) -> wxWindow*
132 {
133 return new PANEL_SETUP_MASK_AND_PASTE( aParent, m_frame );
134 }, _( "Solder Mask/Paste" ) );
135
136 m_drillChartPage = m_treebook->GetPageCount();
137 m_treebook->AddLazySubPage(
138 [this]( wxWindow* aParent ) -> wxWindow*
139 {
140 return new PANEL_SETUP_DRILL_CHART( aParent, m_frame );
141 }, _( "Drill Chart" ) );
142
143 m_treebook->AddPage( new wxPanel( GetTreebook() ), _( "Text & Graphics" ) );
144
145 m_defaultsPage = m_treebook->GetPageCount();
146 m_treebook->AddLazySubPage(
147 [this]( wxWindow* aParent ) -> wxWindow*
148 {
149 return new PANEL_SETUP_DEFAULTS( aParent, m_frame );
150 }, _( "Defaults" ) );
151
152 m_formattingPage = m_treebook->GetPageCount();
153 m_treebook->AddLazySubPage(
154 [this]( wxWindow* aParent ) -> wxWindow*
155 {
156 return new PANEL_SETUP_FORMATTING( aParent, m_frame );
157 }, _( "Formatting" ) );
158
159 m_treebook->AddLazySubPage(
160 [this]( wxWindow* aParent ) -> wxWindow*
161 {
162 return new PANEL_TEXT_VARIABLES( aParent, &Prj() );
163 }, _( "Text Variables" ) );
164
165 m_treebook->AddPage( new wxPanel( GetTreebook() ), _( "Design Rules" ) );
166
167 m_constraintsPage = m_treebook->GetPageCount();
168 m_treebook->AddLazySubPage(
169 [this]( wxWindow* aParent ) -> wxWindow*
170 {
171 return new PANEL_SETUP_CONSTRAINTS( aParent, m_frame );
172 }, _( "Constraints" ) );
173
174 m_tracksAndViasPage = m_treebook->GetPageCount();
175 m_treebook->AddLazySubPage(
176 [this]( wxWindow* aParent ) -> wxWindow*
177 {
178 return new PANEL_SETUP_TRACKS_AND_VIAS( aParent, m_frame );
179 }, _( "Pre-defined Sizes" ) );
180
181 m_viaStacksPage = m_treebook->GetPageCount();
182 m_treebook->AddLazySubPage(
183 [this]( wxWindow* aParent ) -> wxWindow*
184 {
185 return new PANEL_SETUP_VIA_STACKS( aParent, m_frame );
186 },
187 _( "Microvia Stacks" ) );
188
189 m_zonesPage = m_treebook->GetPageCount();
190 m_treebook->AddLazySubPage(
191 [this]( wxWindow* aParent ) -> wxWindow*
192 {
193 return new PANEL_SETUP_ZONES( aParent, m_frame, m_frame->GetBoard()->GetDesignSettings() );
194 },
195 _( "Zones" ) );
196
197 m_teardropsPage = m_treebook->GetPageCount();
198 m_treebook->AddLazySubPage(
199 [this]( wxWindow* aParent ) -> wxWindow*
200 {
201 return new PANEL_SETUP_TEARDROPS( aParent, m_frame );
202 }, _( "Teardrops" ) );
203
204 m_tuningPatternsPage = m_treebook->GetPageCount();
205 m_treebook->AddLazySubPage(
206 [this]( wxWindow* aParent ) -> wxWindow*
207 {
208 BOARD_DESIGN_SETTINGS& bds = m_frame->GetBoard()->GetDesignSettings();
209
210 return new PANEL_SETUP_TUNING_PATTERNS( aParent, m_frame,
214 }, _( "Length-tuning Patterns" ) );
215
216 m_tuningProfilesPage = m_treebook->GetPageCount();
217 m_treebook->AddLazySubPage(
218 [this]( wxWindow* aParent ) -> wxWindow*
219 {
220 BOARD* board = m_frame->GetBoard();
221 return new PANEL_SETUP_TUNING_PROFILES( aParent, m_frame, board,
222 m_frame->Prj().GetProjectFile().TuningProfileParameters() );
223 },
224 _( "Tuning Profiles" ) );
225
226 m_netclassesPage = m_treebook->GetPageCount();
227 m_treebook->AddLazySubPage(
228 [this]( wxWindow* aParent ) -> wxWindow*
229 {
230 BOARD* board = m_frame->GetBoard();
231 return new PANEL_SETUP_NETCLASSES( aParent, m_frame,
232 m_frame->Prj().GetProjectFile().NetSettings(),
234 false );
235 }, _( "Net Classes" ) );
236
237 m_componentClassesPage = m_treebook->GetPageCount();
238 m_treebook->AddLazySubPage(
239 [this]( wxWindow* aParent ) -> wxWindow*
240 {
241 // Construct the panel
243 aParent, m_frame, m_frame->Prj().GetProjectFile().ComponentClassSettings(),
244 this );
245 },
246 _( "Component Classes" ) );
247
248 m_customRulesPage = m_treebook->GetPageCount();
249 m_treebook->AddLazySubPage(
250 [this]( wxWindow* aParent ) -> wxWindow*
251 {
252 return new PANEL_SETUP_RULES( aParent, m_frame );
253 },
254 _( "Custom Rules" ) );
255
256 m_severitiesPage = m_treebook->GetPageCount();
257 m_treebook->AddLazySubPage(
258 [this]( wxWindow* aParent ) -> wxWindow*
259 {
260 BOARD* board = m_frame->GetBoard();
263 },
264 _( "Violation Severity" ) );
265
266 m_treebook->AddPage( new wxPanel( GetTreebook() ), _( "Board Data" ) );
267 m_embeddedFilesPage = m_treebook->GetPageCount();
268 m_treebook->AddLazySubPage(
269 [this]( wxWindow* aParent ) -> wxWindow*
270 {
271 BOARD* board = m_frame->GetBoard();
272
273 std::vector<const EMBEDDED_FILES*> inheritedFiles;
274
275 for( FOOTPRINT* fp : board->Footprints() )
276 inheritedFiles.push_back( fp->GetEmbeddedFiles() );
277
278 return new PANEL_EMBEDDED_FILES( aParent, board, NO_MARGINS, inheritedFiles );
279 },
280 _( "Embedded Files" ) );
281
282 for( size_t i = 0; i < m_treebook->GetPageCount(); ++i )
283 m_treebook->ExpandNode( i );
284
285 SetEvtHandlerEnabled( true );
286
288
289 if( Prj().IsReadOnly() )
290 {
291 m_infoBar->ShowMessage( _( "Project is missing or read-only. Some settings will not be editable." ),
292 wxICON_WARNING );
293 }
294
295 wxBookCtrlEvent evt( wxEVT_TREEBOOK_PAGE_CHANGED, wxID_ANY, 0 );
296
297 wxQueueEvent( m_treebook, evt.Clone() );
298}
299
300
304
305
307{
309 return false;
310
311 // References to renamed profiles can only be fixed up after all pages have saved
312 auto* tuningProfiles =
313 static_cast<PANEL_SETUP_TUNING_PROFILES*>( m_treebook->GetResolvedPage( m_tuningProfilesPage ) );
314
315 if( tuningProfiles )
316 {
317 const std::map<wxString, wxString> renames = tuningProfiles->TakeProfileRenames();
318
319 if( !renames.empty() )
320 {
321 std::shared_ptr<NET_SETTINGS>& netSettings = m_frame->Prj().GetProjectFile().NetSettings();
322
323 auto remapProfile = [&renames]( const std::shared_ptr<NETCLASS>& aNetclass )
324 {
325 auto it = renames.find( aNetclass->GetTuningProfile() );
326
327 if( it != renames.end() )
328 aNetclass->SetTuningProfile( it->second );
329 };
330
331 remapProfile( netSettings->GetDefaultNetclass() );
332
333 for( const auto& [name, netclass] : netSettings->GetNetclasses() )
334 remapProfile( netclass );
335
336 netSettings->ClearAllCaches();
337 }
338 }
339
340 return true;
341}
342
343
344void DIALOG_BOARD_SETUP::onPageChanged( wxBookCtrlEvent& aEvent )
345{
347
348 size_t page = aEvent.GetSelection();
349
350 if( m_physicalStackupPage > 0 ) // Don't run this during initialization
351 {
353 || page == m_tuningProfilesPage || page == m_zonesPage )
354 {
360 }
361
362 // Ensure layer page always gets updated even if we aren't moving towards it
364 {
365 m_layers->SyncCopperLayers( m_physicalStackup->GetCopperLayerCount() );
366
367 // Avoid calling SyncCopperLayers twice if moving from stackup to tuning profiles directly
368 m_tuningProfiles->SyncCopperLayers( m_physicalStackup->GetCopperLayerCount() );
369 }
370
371 if( page == m_physicalStackupPage )
372 {
373 m_physicalStackup->OnLayersOptionsChanged( m_layers->GetUILayerMask() );
374 }
376 {
377 m_netClasses->RemapDelayProfileNames( m_tuningProfiles->TakeProfileRenames() );
378 m_netClasses->UpdateDelayProfileNames( m_tuningProfiles->GetDelayProfileNames() );
379 }
380 else if( page == m_tuningProfilesPage )
381 {
382 m_tuningProfiles->SyncCopperLayers( m_physicalStackup->GetCopperLayerCount() );
383 }
384 else if( page == m_zonesPage )
385 {
386 m_zones->SyncCopperLayers( m_physicalStackup->GetCopperLayerCount() );
387 }
388
389 if( Prj().IsReadOnly() )
390 {
391 KIUI::Disable( m_treebook->GetPage( page ) );
392 }
393 }
394
395 m_currentPage = page;
396}
397
398
399void DIALOG_BOARD_SETUP::onAuxiliaryAction( wxCommandEvent& aEvent )
400{
401 DIALOG_IMPORT_SETTINGS importDlg( this, m_frame );
402
403 if( importDlg.ShowModal() == wxID_CANCEL )
404 return;
405
406 wxFileName boardFn( importDlg.GetFilePath() );
407
408 SCRATCH_PROJECT scratch( *m_frame->GetSettingsManager(), boardFn.GetFullPath(),
409 /*aRequireProjectFile=*/true );
410
411 if( !scratch.IsValid() )
412 {
413 wxFileName projectFn( boardFn );
414 projectFn.SetExt( FILEEXT::ProjectFileExtension );
415
416 wxString msg = wxString::Format( _( "Error importing settings from board:\n"
417 "Associated project file %s could not be loaded" ),
418 projectFn.GetFullPath() );
419 DisplayErrorMessage( this, msg );
420
421 return;
422 }
423
424 PROJECT* otherPrj = scratch.GetProject();
425
429
430 // Flag so user can stop work if it will result in deleted inner copper layers
431 // and still clean up this function properly.
432 bool okToProceed = true;
433
435 std::unique_ptr<BOARD> otherBoard;
436
437 try
438 {
439 WX_PROGRESS_REPORTER progressReporter( this, _( "Load PCB" ), 1, PR_CAN_ABORT );
440
441 pi->SetProgressReporter( &progressReporter );
442
443 otherBoard = pi->LoadBoard( boardFn.GetFullPath() );
444
445 if( importDlg.m_LayersOpt->GetValue() )
446 {
447 BOARD* loadedBoard = m_frame->GetBoard();
448
449 // Check if "Import Settings" board has more layers than the current board.
450 okToProceed = m_layers->CheckCopperLayerCount( loadedBoard, otherBoard.get() );
451 }
452 }
453 catch( const IO_CANCELLED& )
454 {
455 // A user-cancelled load is not an error.
456 return;
457 }
458 catch( const IO_ERROR& ioe )
459 {
460 // You wouldn't think boardFn.GetFullPath() would throw, but we get a stack buffer
461 // underflow from ASAN. While it's probably an ASAN error, a second try/catch doesn't
462 // cost us much.
463 try
464 {
465 wxString msg = wxString::Format( _( "Error loading board file:\n%s" ), boardFn.GetFullPath() );
466 DisplayErrorMessage( this, msg, ioe.What() );
467 }
468 catch(...)
469 {
470 // That was already our best-efforts
471 }
472
473 return;
474 }
475
476 if( okToProceed )
477 {
478 BOARD* other = otherBoard.get();
479
480 other->SetProject( otherPrj );
481
482 // Always ClearProject before destruction, even if a later
483 // ImportSettingsFrom throws. Skipping clear when otherPrj is the
484 // active project preserves the live editor's BoardSettings.
485 const bool activeProject = ( otherPrj == &m_frame->Prj() );
486
487 struct BOARD_DETACH_GUARD
488 {
489 BOARD* board;
490 bool skip;
491 ~BOARD_DETACH_GUARD() { if( board && !skip ) board->ClearProject(); }
492 } detachGuard{ other, activeProject };
493
494 // If layers options are imported, import also the stackup
495 // layers options and stackup are linked, so they cannot be imported
496 // separately, and stackup can be imported only after layers options
497 if( importDlg.m_LayersOpt->GetValue() )
498 {
499 m_physicalStackup->ImportSettingsFrom( other );
500 m_layers->ImportSettingsFrom( other );
501 m_boardFinish->ImportSettingsFrom( other );
502 }
503
504 if( importDlg.m_TextAndGraphicsOpt->GetValue() )
505 RESOLVE_PAGE( PANEL_SETUP_DEFAULTS, m_defaultsPage )->ImportSettingsFrom( other );
506
507 if( importDlg.m_FormattingOpt->GetValue() )
508 RESOLVE_PAGE( PANEL_SETUP_FORMATTING, m_formattingPage )->ImportSettingsFrom( other );
509
510 if( importDlg.m_ConstraintsOpt->GetValue() )
511 RESOLVE_PAGE( PANEL_SETUP_CONSTRAINTS, m_constraintsPage )->ImportSettingsFrom( other );
512
513 if( importDlg.m_NetclassesOpt->GetValue() )
514 {
515 PROJECT_FILE& otherProjectFile = otherPrj->GetProjectFile();
516
518 m_netclassesPage )->ImportSettingsFrom( otherProjectFile.m_NetSettings );
519 }
520
521 if( importDlg.m_ComponentClassesOpt->GetValue() )
522 {
523 PROJECT_FILE& otherProjectFile = otherPrj->GetProjectFile();
524
526 m_componentClassesPage )->ImportSettingsFrom( otherProjectFile.m_ComponentClassSettings );
527 }
528
529 if( importDlg.m_TracksAndViasOpt->GetValue() )
530 {
531 RESOLVE_PAGE( PANEL_SETUP_TRACKS_AND_VIAS, m_tracksAndViasPage )->ImportSettingsFrom( other );
532 RESOLVE_PAGE( PANEL_SETUP_VIA_STACKS, m_viaStacksPage )->ImportSettingsFrom( other );
533 }
534
535 if( importDlg.m_ZonesOpt->GetValue() )
536 RESOLVE_PAGE( PANEL_SETUP_ZONES, m_zonesPage )->ImportSettingsFrom( other );
537
538 if( importDlg.m_TeardropsOpt->GetValue() )
539 RESOLVE_PAGE( PANEL_SETUP_TEARDROPS, m_teardropsPage )->ImportSettingsFrom( other );
540
541 if( importDlg.m_TuningPatternsOpt->GetValue() )
542 RESOLVE_PAGE( PANEL_SETUP_TUNING_PATTERNS, m_tuningPatternsPage )->ImportSettingsFrom( other );
543
544 if( importDlg.m_MaskAndPasteOpt->GetValue() )
545 RESOLVE_PAGE( PANEL_SETUP_MASK_AND_PASTE, m_maskAndPastePage )->ImportSettingsFrom( other );
546
547 if( importDlg.m_ZoneHatchingOffsetsOpt->GetValue() )
548 RESOLVE_PAGE( PANEL_SETUP_ZONES, m_zonesPage )->ImportHatchOffsetsFrom( other );
549
550 if( importDlg.m_CustomRulesOpt->GetValue() )
551 RESOLVE_PAGE( PANEL_SETUP_RULES, m_customRulesPage )->ImportSettingsFrom( other );
552
553 if( importDlg.m_SeveritiesOpt->GetValue() )
554 {
555 BOARD_DESIGN_SETTINGS& otherSettings = other->GetDesignSettings();
556
558 m_severitiesPage )->ImportSettingsFrom( otherSettings.m_DRCSeverities );
559 }
560
561 if( importDlg.m_TuningProfilesOpt->GetValue() )
562 {
563 PROJECT_FILE& otherProjectFile = otherPrj->GetProjectFile();
564
566 m_tuningProfilesPage )->ImportSettingsFrom( otherProjectFile.TuningProfileParameters() );
567 }
568
569 }
570}
const char * name
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:410
std::set< wxString > GetNetClassAssignmentCandidates() const
Return the set of netname candidates for netclass assignment.
Definition board.cpp:3139
const FOOTPRINTS & Footprints() const
Definition board.h:464
void SetProject(PROJECT *aProject, bool aReferenceOnly=false)
Link a board to a given project.
Definition board.cpp:377
void ClearProject()
Definition board.cpp:418
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition board.cpp:1301
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
bool TransferDataFromWindow() override
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:159
An exception saying that the user cancelled an interactive part of a load, import,...
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()
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)
bool TransferDataFromWindow() override
WX_TREEBOOK * m_treebook
virtual void onPageChanged(wxBookCtrlEvent &aEvent)
Board-wide drill chart and map setup.
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 PCB_IO 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:63
virtual PROJECT_FILE & GetProjectFile() const
Definition project.h:201
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