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-2019 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>
23 #include <panel_setup_netclasses.h>
26 #include <../board_stackup_manager/panel_board_stackup.h>
27 #include <confirm.h>
28 #include <kiface_i.h>
29 #include <drc/drc_item.h>
30 #include <dialog_import_settings.h>
31 #include <io_mgr.h>
32 #include <panel_setup_severities.h>
33 #include <panel_text_variables.h>
34 #include <project.h>
35 #include <project/project_file.h>
37 #include <widgets/infobar.h>
39 
40 #include "dialog_board_setup.h"
41 #include "panel_setup_rules.h"
42 
44  PAGED_DIALOG( aFrame, _( "Board Setup" ), false, _( "Import Settings from Another Board..." ) ),
45  m_frame( aFrame )
46 {
47  BOARD* board = aFrame->GetBoard();
49 
50  m_layers = new PANEL_SETUP_LAYERS( this, aFrame );
52  m_constraints = new PANEL_SETUP_FEATURE_CONSTRAINTS( this, aFrame );
53  m_rules = new PANEL_SETUP_RULES( this, aFrame );
55  m_maskAndPaste = new PANEL_SETUP_MASK_AND_PASTE( this, aFrame );
57 
59  bds.m_DRCSeverities );
60 
62  board->GetNetClassAssignmentCandidates(), false );
63 
65 
66  /*
67  * WARNING: If you change page names you MUST update calls to ShowBoardSetupDialog().
68  */
69 
70  m_treebook->AddPage( new wxPanel( this ), _( "Board Stackup" ) );
71  m_treebook->AddSubPage( m_layers, _( "Board Editor Layers" ) );
72  m_treebook->AddSubPage( m_physicalStackup, _( "Physical Stackup" ) );
73  // Change this value if m_physicalStackup is not the page 2 of m_treebook
74  m_physicalStackupPage = 2; // The page number (from 0) to select the m_physicalStackup panel
75 
76  m_treebook->AddPage( new wxPanel( this ), _( "Defaults" ) );
77  m_treebook->AddSubPage( m_textAndGraphics, _( "Text & Graphics" ) );
78  m_treebook->AddSubPage( m_tracksAndVias, _( "Tracks & Vias" ) );
79  m_treebook->AddSubPage( m_maskAndPaste, _( "Solder Mask/Paste" ) );
80 
81  m_treebook->AddPage( new wxPanel( this ), _( "Design Rules" ) );
82  m_treebook->AddSubPage( m_constraints, _( "Constraints" ) );
83  m_treebook->AddSubPage( m_rules, _( "Rules" ) );
84  m_treebook->AddSubPage( m_severities, _( "Violation Severity" ) );
85 
86  m_treebook->AddPage( new wxPanel( this ), _( "Project" ) );
87  m_treebook->AddSubPage( m_netclasses, _( "Net Classes" ) );
88  m_treebook->AddSubPage( m_textVars, _( "Text Variables" ) );
89 
90  for( size_t i = 0; i < m_treebook->GetPageCount(); ++i )
91  m_macHack.push_back( true );
92 
93  // Connect Events
94  m_treebook->Connect( wxEVT_TREEBOOK_PAGE_CHANGED,
95  wxBookCtrlEventHandler( DIALOG_BOARD_SETUP::OnPageChange ), NULL, this );
96 
97  if( Prj().IsReadOnly() )
98  m_infoBar->ShowMessage( _( "Project is missing or read-only. Changes will not be saved." ) );
99 
101 }
102 
103 
105 {
106  m_treebook->Disconnect( wxEVT_TREEBOOK_PAGE_CHANGED,
107  wxBookCtrlEventHandler( DIALOG_BOARD_SETUP::OnPageChange ), NULL, this );
108 }
109 
110 
111 void DIALOG_BOARD_SETUP::OnPageChange( wxBookCtrlEvent& event )
112 {
113  if( event.GetSelection() == m_physicalStackupPage )
115 
116 #ifdef __WXMAC__
117  // Work around an OSX bug where the wxGrid children don't get placed correctly until
118  // the first resize event
119  int page = event.GetSelection();
120 
121  if( m_macHack[ page ] )
122  {
123  wxSize pageSize = m_treebook->GetPage( page )->GetSize();
124  pageSize.x -= 1;
125  pageSize.y += 2;
126 
127  m_treebook->GetPage( page )->SetSize( pageSize );
128  m_macHack[ page ] = false;
129  }
130 #endif
131 }
132 
133 
134 // Run Import Settings... action
135 void DIALOG_BOARD_SETUP::OnAuxiliaryAction( wxCommandEvent& event )
136 {
137  DIALOG_IMPORT_SETTINGS importDlg( this, m_frame );
138 
139  if( importDlg.ShowModal() == wxID_CANCEL )
140  return;
141 
142  wxFileName boardFn( importDlg.GetFilePath() );
143  wxFileName projectFn( boardFn );
144 
145  projectFn.SetExt( ProjectFileExtension );
146 
147  if( !m_frame->GetSettingsManager()->LoadProject( projectFn.GetFullPath(), false ) )
148  {
149  wxString msg = wxString::Format( _( "Error importing settings from board:\n"
150  "Associated project file %s could not be loaded" ),
151  projectFn.GetFullPath() );
152  DisplayErrorMessage( this, msg );
153 
154  return;
155  }
156 
157  // Flag so user can stop work if it will result in deleted inner copper layers
158  // and still clean up this function properly.
159  bool okToProceed = true;
160 
161  PROJECT* otherPrj = m_frame->GetSettingsManager()->GetProject( projectFn.GetFullPath() );
162 
164 
165  BOARD* otherBoard = new BOARD();
166 
167  try
168  {
169  otherBoard = pi->Load( boardFn.GetFullPath(), nullptr, nullptr );
170 
171  if( importDlg.m_LayersOpt->GetValue() )
172  {
173  BOARD* loadedBoard = m_frame->GetBoard();
174 
175  // Check if "Import Settings" board has more layers than the current board.
176  okToProceed = m_layers->CheckCopperLayerCount( loadedBoard, otherBoard );
177  }
178  }
179  catch( const IO_ERROR& ioe )
180  {
181  // You wouldn't think boardFn.GetFullPath() would throw, but we get a stack buffer
182  // underflow from ASAN. While it's probably an ASAN error, a second try/catch doesn't
183  // cost us much.
184  try
185  {
186  if( ioe.Problem() != wxT( "CANCEL" ) )
187  {
188  wxString msg = wxString::Format( _( "Error loading board file:\n%s" ),
189  boardFn.GetFullPath() );
190  DisplayErrorMessage( this, msg, ioe.What() );
191  }
192 
193  if( otherPrj != &m_frame->Prj() )
194  m_frame->GetSettingsManager()->UnloadProject( otherPrj, false );
195  }
196  catch(...)
197  {
198  // That was already our best-efforts
199  }
200 
201  return;
202  }
203 
204 
205  if( okToProceed )
206  {
207  otherBoard->SetProject( otherPrj );
208 
209  if( importDlg.m_LayersOpt->GetValue() )
210  m_layers->ImportSettingsFrom( otherBoard );
211 
212  if( importDlg.m_TextAndGraphicsOpt->GetValue() )
213  m_textAndGraphics->ImportSettingsFrom( otherBoard );
214 
215  if( importDlg.m_ConstraintsOpt->GetValue() )
216  m_constraints->ImportSettingsFrom( otherBoard );
217 
218  if( importDlg.m_NetclassesOpt->GetValue() )
220 
221  if( importDlg.m_TracksAndViasOpt->GetValue() )
222  m_tracksAndVias->ImportSettingsFrom( otherBoard );
223 
224  if( importDlg.m_MaskAndPasteOpt->GetValue() )
225  m_maskAndPaste->ImportSettingsFrom( otherBoard );
226 
227  // If layers options are imported, import also the stackup
228  // layers options and stackup are linked, so they cannot be imported
229  // separately, and stackup can be imported only after layers options
230  //
231  // Note also currently only the list of enabled layers can be imported, because
232  // we import settings from a .pro project file, not the settings inside
233  // a board, and info only living in the board is not imported.
234  // TODO: Add import of physical settings now that we are actually loading the board here
235 
236  if( importDlg.m_LayersOpt->GetValue() )
237  m_physicalStackup->ImportSettingsFrom( otherBoard );
238 
239  if( importDlg.m_SeveritiesOpt->GetValue() )
241 
242  if( otherPrj != &m_frame->Prj() )
243  otherBoard->ClearProject();
244  }
245 
246  // Clean up and free memory before leaving
247  if( otherPrj != &m_frame->Prj() )
248  m_frame->GetSettingsManager()->UnloadProject( otherPrj, false );
249 
250  delete otherBoard;
251 }
void ImportSettingsFrom(std::map< int, int > &aSettings)
void ShowMessage(const wxString &aMessage, int aFlags=wxICON_INFORMATION) override
Show the info bar with the provided message and icon.
Definition: infobar.cpp:120
PANEL_SETUP_RULES * m_rules
PROJECT holds project specific data.
Definition: project.h:63
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:253
SETTINGS_MANAGER * GetSettingsManager() const
This file is part of the common library.
const std::string ProjectFileExtension
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...
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: class_board.h:558
PCB_EDIT_FRAME * m_frame
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...
RELEASER releases a PLUGIN in the context of a potential thrown exception, through its destructor.
Definition: io_mgr.h:580
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:79
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
void ImportSettingsFrom(BOARD *aBoard)
wxTreebook * m_treebook
Definition: paged_dialog.h:75
Definition of file extensions used in Kicad.
NETCLASSES & GetNetClasses() const
std::map< int, int > m_DRCSeverities
void ClearProject()
void ImportSettingsFrom(BOARD *aBoard)
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.
PANEL_SETUP_TRACKS_AND_VIAS * m_tracksAndVias
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:201
Information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:186
#define _(s)
Definition: 3d_actions.cpp:33
static PLUGIN * PluginFind(PCB_FILE_T aFileType)
Function PluginFind returns a PLUGIN which the caller can use to import, export, save,...
Definition: io_mgr.cpp:62
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:102
PCB_EDIT_FRAME is the main frame for Pcbnew.
PANEL_SETUP_TEXT_AND_GRAPHICS * m_textAndGraphics
PANEL_SETUP_MASK_AND_PASTE * m_maskAndPaste
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.
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:76
PROJECT * GetProject(const wxString &aFullPath) const
Retrieves a loaded project by name.
PANEL_SETUP_FEATURE_CONSTRAINTS * m_constraints
S-expression Pcbnew file format.
Definition: io_mgr.h:57
BOARD_DESIGN_SETTINGS contains design settings for a BOARD object.