KiCad PCB EDA Suite
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 (C) 2017-2020 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 along
17  * with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 #include <pcb_edit_frame.h>
20 #include <panel_setup_layers.h>
26 #include <../board_stackup_manager/panel_board_stackup.h>
27 #include <../board_stackup_manager/panel_board_finish.h>
28 #include <confirm.h>
29 #include <kiface_i.h>
30 #include <drc/drc_item.h>
31 #include <dialog_import_settings.h>
32 #include <io_mgr.h>
34 #include <panel_text_variables.h>
35 #include <project.h>
36 #include <project/project_file.h>
38 #include <widgets/infobar.h>
41 
42 #include "dialog_board_setup.h"
43 #include "panel_setup_rules.h"
44 
45 using std::placeholders::_1;
46 
48  PAGED_DIALOG( aFrame, _( "Board Setup" ), false,
49  _( "Import Settings from Another Board..." ) ),
50  m_frame( aFrame )
51 {
52  BOARD* board = aFrame->GetBoard();
54 
55  m_layers = new PANEL_SETUP_LAYERS( this, aFrame );
57  m_constraints = new PANEL_SETUP_CONSTRAINTS( this, aFrame );
58  m_rules = new PANEL_SETUP_RULES( this, aFrame );
60  m_maskAndPaste = new PANEL_SETUP_MASK_AND_PASTE( this, aFrame );
62  m_boardFinish = new PANEL_SETUP_BOARD_FINISH( this, board );
63 
65  bds.m_DRCSeverities );
66 
68  board->GetNetClassAssignmentCandidates(), false );
69 
71 
72  /*
73  * WARNING: If you change page names you MUST update calls to ShowBoardSetupDialog().
74  */
75 
76  m_treebook->AddPage( new wxPanel( this ), _( "Board Stackup" ) );
77 
78  /*
79  * WARNING: Code currently relies on the layers setup coming before the physical stackup panel,
80  * and thus transferring data to the board first. See comment in
81  * PANEL_SETUP_BOARD_STACKUP::TransferDataFromWindow and rework this logic if it is determined
82  * that the order of these pages should be changed.
83  */
84  m_treebook->AddSubPage( m_layers, _( "Board Editor Layers" ) );
85  m_layerSetupPage = 1;
86  m_treebook->AddSubPage( m_physicalStackup, _( "Physical Stackup" ) );
87  // Change this value if m_physicalStackup is not the page 2 of m_treebook
88  m_physicalStackupPage = 2; // The page number (from 0) to select the m_physicalStackup panel
89  m_treebook->AddSubPage( m_boardFinish, _( "Board Finish" ) );
90  m_treebook->AddSubPage( m_maskAndPaste, _( "Solder Mask/Paste" ) );
91 
92  m_treebook->AddPage( new wxPanel( this ), _( "Text & Graphics" ) );
93  m_treebook->AddSubPage( m_textAndGraphics, _( "Defaults" ) );
94  m_treebook->AddSubPage( m_textVars, _( "Text Variables" ) );
95 
96  m_treebook->AddPage( new wxPanel( this ), _( "Design Rules" ) );
97  m_treebook->AddSubPage( m_constraints, _( "Constraints" ) );
98  m_treebook->AddSubPage( m_tracksAndVias, _( "Pre-defined Sizes" ) );
99  m_treebook->AddSubPage( m_netclasses, _( "Net Classes" ) );
100  m_treebook->AddSubPage( m_rules, _( "Custom Rules" ) );
101  m_treebook->AddSubPage( m_severities, _( "Violation Severity" ) );
102 
103  for( size_t i = 0; i < m_treebook->GetPageCount(); ++i )
104  m_macHack.push_back( true );
105 
106  AddAuxiliaryAction( "Validate Settings", "Check for problems with board settings",
107  std::bind( &DIALOG_BOARD_SETUP::OnValidate, this, _1 ) );
108 
109  // Connect Events
110  m_treebook->Connect( wxEVT_TREEBOOK_PAGE_CHANGED,
111  wxBookCtrlEventHandler( DIALOG_BOARD_SETUP::OnPageChange ), NULL, this );
112 
114 }
115 
116 
118 {
119  m_treebook->Disconnect( wxEVT_TREEBOOK_PAGE_CHANGED,
120  wxBookCtrlEventHandler( DIALOG_BOARD_SETUP::OnPageChange ), NULL, this );
121 }
122 
123 
124 void DIALOG_BOARD_SETUP::OnPageChange( wxBookCtrlEvent& event )
125 {
126  if( event.GetSelection() == m_physicalStackupPage )
127  {
129  m_infoBar->Dismiss();
130  }
131  else if( event.GetSelection() == m_layerSetupPage )
132  {
134  m_infoBar->Dismiss();
135  }
136  else if( Prj().IsReadOnly() )
137  {
139  _( "Project is missing or read-only. Changes will not be saved." ) );
140  }
141 
142 #ifdef __WXMAC__
143  // Work around an OSX bug where the wxGrid children don't get placed correctly until
144  // the first resize event
145  int page = event.GetSelection();
146 
147  if( m_macHack[ page ] )
148  {
149  wxSize pageSize = m_treebook->GetPage( page )->GetSize();
150  pageSize.x -= 1;
151  pageSize.y += 2;
152 
153  m_treebook->GetPage( page )->SetSize( pageSize );
154  m_macHack[ page ] = false;
155  }
156 #endif
157 }
158 
159 
160 // Run Import Settings... action
161 void DIALOG_BOARD_SETUP::OnAuxiliaryAction( wxCommandEvent& event )
162 {
163  DIALOG_IMPORT_SETTINGS importDlg( this, m_frame );
164 
165  if( importDlg.ShowModal() == wxID_CANCEL )
166  return;
167 
168  wxFileName boardFn( importDlg.GetFilePath() );
169  wxFileName projectFn( boardFn );
170 
171  projectFn.SetExt( ProjectFileExtension );
172 
173  if( !m_frame->GetSettingsManager()->LoadProject( projectFn.GetFullPath(), false ) )
174  {
175  wxString msg = wxString::Format( _( "Error importing settings from board:\n"
176  "Associated project file %s could not be loaded" ),
177  projectFn.GetFullPath() );
178  DisplayErrorMessage( this, msg );
179 
180  return;
181  }
182 
183  // Flag so user can stop work if it will result in deleted inner copper layers
184  // and still clean up this function properly.
185  bool okToProceed = true;
186 
187  PROJECT* otherPrj = m_frame->GetSettingsManager()->GetProject( projectFn.GetFullPath() );
188 
190 
191  BOARD* otherBoard = nullptr;
192 
193  try
194  {
195  otherBoard = pi->Load( boardFn.GetFullPath(), nullptr, nullptr );
196 
197  if( importDlg.m_LayersOpt->GetValue() )
198  {
199  BOARD* loadedBoard = m_frame->GetBoard();
200 
201  // Check if "Import Settings" board has more layers than the current board.
202  okToProceed = m_layers->CheckCopperLayerCount( loadedBoard, otherBoard );
203  }
204  }
205  catch( const IO_ERROR& ioe )
206  {
207  // You wouldn't think boardFn.GetFullPath() would throw, but we get a stack buffer
208  // underflow from ASAN. While it's probably an ASAN error, a second try/catch doesn't
209  // cost us much.
210  try
211  {
212  if( ioe.Problem() != wxT( "CANCEL" ) )
213  {
214  wxString msg = wxString::Format( _( "Error loading board file:\n%s" ),
215  boardFn.GetFullPath() );
216  DisplayErrorMessage( this, msg, ioe.What() );
217  }
218 
219  if( otherPrj != &m_frame->Prj() )
220  m_frame->GetSettingsManager()->UnloadProject( otherPrj, false );
221  }
222  catch(...)
223  {
224  // That was already our best-efforts
225  }
226 
227  return;
228  }
229 
230 
231  if( okToProceed )
232  {
233  otherBoard->SetProject( otherPrj );
234 
235  if( importDlg.m_LayersOpt->GetValue() )
236  m_layers->ImportSettingsFrom( otherBoard );
237 
238  if( importDlg.m_TextAndGraphicsOpt->GetValue() )
239  m_textAndGraphics->ImportSettingsFrom( otherBoard );
240 
241  if( importDlg.m_ConstraintsOpt->GetValue() )
242  m_constraints->ImportSettingsFrom( otherBoard );
243 
244  if( importDlg.m_NetclassesOpt->GetValue() )
246 
247  if( importDlg.m_TracksAndViasOpt->GetValue() )
248  m_tracksAndVias->ImportSettingsFrom( otherBoard );
249 
250  if( importDlg.m_MaskAndPasteOpt->GetValue() )
251  m_maskAndPaste->ImportSettingsFrom( otherBoard );
252 
253  // If layers options are imported, import also the stackup
254  // layers options and stackup are linked, so they cannot be imported
255  // separately, and stackup can be imported only after layers options
256  //
257  // Note also currently only the list of enabled layers can be imported, because
258  // we import settings from a .pro project file, not the settings inside
259  // a board, and info only living in the board is not imported.
260  // TODO: Add import of physical settings now that we are actually loading the board here
261 
262  if( importDlg.m_LayersOpt->GetValue() )
263  m_physicalStackup->ImportSettingsFrom( otherBoard );
264 
265  if( importDlg.m_SeveritiesOpt->GetValue() )
267 
268  if( otherPrj != &m_frame->Prj() )
269  otherBoard->ClearProject();
270  }
271 
272  // Clean up and free memory before leaving
273  if( otherPrj != &m_frame->Prj() )
274  m_frame->GetSettingsManager()->UnloadProject( otherPrj, false );
275 
276  delete otherBoard;
277 }
void ShowMessage(const wxString &aMessage, int aFlags=wxICON_INFORMATION) override
Show the info bar with the provided message and icon.
Definition: infobar.cpp:124
PANEL_SETUP_RULES * m_rules
Container for project specific data.
Definition: project.h:62
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:266
SETTINGS_MANAGER * GetSettingsManager() const
This file is part of the common library.
const std::string ProjectFileExtension
void SyncCopperLayers(int aNumCopperLayers)
Called when switching to this tab to make sure that any changes to the copper layer count made on the...
PANEL_SETUP_NETCLASSES * m_netclasses
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.h:591
PCB_EDIT_FRAME * m_frame
void ImportSettingsFrom(std::map< int, SEVERITY > &aSettings)
virtual const wxString Problem() const
what was the problem?
Definition: exceptions.cpp:45
PANEL_SETUP_BOARD_STACKUP * m_physicalStackup
void OnAuxiliaryAction(wxCommandEvent &event) override
PANEL_SETUP_SEVERITIES * m_severities
void OnLayersOptionsChanged(LSET aNewLayerSet)
Must be called if the copper layers count has changed or solder mask, solder paste or silkscreen laye...
Releases a PLUGIN in the context of a potential thrown exception through its destructor.
Definition: io_mgr.h:545
void Dismiss() override
Dismisses the infobar and updates the containing layout and AUI manager (if one is provided).
Definition: infobar.cpp:166
bool CheckCopperLayerCount(BOARD *aWorkingBoard, BOARD *aImportedBoard)
Check and warn if inner copper layers will be deleted.
#define NULL
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:29
WX_INFOBAR * m_infoBar
Definition: paged_dialog.h:85
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
void ImportSettingsFrom(BOARD *aBoard)
wxTreebook * m_treebook
Definition: paged_dialog.h:81
Definition of file extensions used in Kicad.
NETCLASSES & GetNetClasses() const
void ClearProject()
Definition: board.cpp:169
void ImportSettingsFrom(BOARD *aBoard)
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
DIALOG_BOARD_SETUP(PCB_EDIT_FRAME *aFrame)
bool LoadProject(const wxString &aFullPath, bool aSetActive=true)
Loads a project or sets up a new project with a specified path.
void SetProject(PROJECT *aProject)
Links a board to a given project.
Definition: board.cpp:139
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
PANEL_SETUP_TRACKS_AND_VIAS * m_tracksAndVias
void ImportSettingsFrom(BOARD *aBoard)
PANEL_SETUP_CONSTRAINTS * m_constraints
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:190
#define _(s)
Definition: 3d_actions.cpp:33
static PLUGIN * PluginFind(PCB_FILE_T aFileType)
Return a PLUGIN which the caller can use to import, export, save, or load design documents.
Definition: io_mgr.cpp:58
std::map< int, SEVERITY > m_DRCSeverities
bool UnloadProject(PROJECT *aProject, bool aSave=true)
Saves, unloads and unregisters the given PROJECT.
static std::vector< std::reference_wrapper< RC_ITEM > > GetItemsWithSeverities()
Definition: drc_item.h:100
The main frame for Pcbnew.
PANEL_SETUP_TEXT_AND_GRAPHICS * m_textAndGraphics
void OnValidate(wxCommandEvent &aEvent)
void AddAuxiliaryAction(const wxString &aTitle, const wxString &aTooltip, std::function< void(wxCommandEvent &)> aHandler)
PANEL_SETUP_MASK_AND_PASTE * m_maskAndPaste
PANEL_SETUP_BOARD_FINISH * m_boardFinish
PANEL_TEXT_VARIABLES * m_textVars
BOARD * GetBoard() const
void ImportSettingsFrom(NETCLASSES *aBoard)
PANEL_SETUP_LAYERS * m_layers
std::vector< bool > m_macHack
void OnPageChange(wxBookCtrlEvent &event)
std::vector< wxString > GetNetClassAssignmentCandidates()
Return a list of name candidates for netclass assignment.
Definition: board.cpp:1392
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:75
PROJECT * GetProject(const wxString &aFullPath) const
Retrieves a loaded project by name.
S-expression Pcbnew file format.
Definition: io_mgr.h:55
Container for design settings for a BOARD object.