KiCad PCB EDA Suite
dialog_global_deletion.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) 1992-2021 KiCad Developers, see AUTHORS.txt for contributors.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU 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, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
24 #include <functional>
25 using namespace std::placeholders;
26 
27 #include <confirm.h>
28 #include <pcb_edit_frame.h>
29 #include <ratsnest/ratsnest_data.h>
30 #include <board_commit.h>
31 #include <board.h>
32 #include <footprint.h>
33 #include <pcb_track.h>
34 #include <tool/tool_manager.h>
35 #include <tools/pcb_actions.h>
36 #include <tools/global_edit_tool.h>
37 #include <dialog_global_deletion.h>
38 
39 
42 {
43  m_Parent = parent;
45  m_trackFilterLocked->Enable( m_delTracks->GetValue() );
46  m_trackFilterUnlocked->Enable( m_delTracks->GetValue() );
47  m_trackFilterVias->Enable( m_delTracks->GetValue() );
48  m_footprintFilterLocked->Enable( m_delFootprints->GetValue() );
49  m_footprintFilterUnlocked->Enable( m_delFootprints->GetValue() );
50  m_drawingFilterLocked->Enable( m_delDrawings->GetValue() );
51  m_drawingFilterUnlocked->Enable( m_delDrawings->GetValue() );
52  m_sdbSizer1OK->SetDefault();
53  SetFocus();
54  GetSizer()->SetSizeHints( this );
55  Centre();
56 }
57 
58 
60 {
61  PCB_EDIT_FRAME* editFrame = getEditFrame<PCB_EDIT_FRAME>();
62  DIALOG_GLOBAL_DELETION dlg( editFrame );
63 
64  dlg.SetCurrentLayer( frame()->GetActiveLayer() );
65 
66  if( dlg.ShowModal() == wxID_OK )
67  dlg.DoGlobalDeletions();
68 
69  return 0;
70 }
71 
72 
74 {
75  m_currentLayer = aLayer;
76  m_textCtrlCurrLayer->SetValue( m_Parent->GetBoard()->GetLayerName( ToLAYER_ID( aLayer ) ) );
77 }
78 
79 
80 void DIALOG_GLOBAL_DELETION::onCheckDeleteTracks( wxCommandEvent& event )
81 {
82  m_trackFilterLocked->Enable( m_delTracks->GetValue() );
83  m_trackFilterUnlocked->Enable( m_delTracks->GetValue() );
84  m_trackFilterVias->Enable( m_delTracks->GetValue() );
85 }
86 
87 
89 {
90  m_footprintFilterLocked->Enable( m_delFootprints->GetValue() );
91  m_footprintFilterUnlocked->Enable( m_delFootprints->GetValue() );
92 }
93 
94 
95 void DIALOG_GLOBAL_DELETION::onCheckDeleteDrawings( wxCommandEvent& event )
96 {
97  m_drawingFilterLocked->Enable( m_delDrawings->GetValue() );
98  m_drawingFilterUnlocked->Enable( m_delDrawings->GetValue() );
99 }
100 
101 
103 {
104  bool gen_rastnest = false;
105 
106  // Clear selection before removing any items
108 
109  bool delete_all = false;
110 
111  if( m_delAll->GetValue() )
112  {
113  if( !IsOK( GetParent(), _( "Are you sure you want to delete the entire board?" ) ) )
114  return;
115 
116  delete_all = true;
117  }
118  else if( !IsOK( GetParent(), _( "Are you sure you want to delete the selected items?" ) ) )
119  {
120  return;
121  }
122 
123  BOARD* board = m_Parent->GetBoard();
124  BOARD_COMMIT commit( m_Parent );
125  LSET layers_filter = LSET().set();
126 
127  if( m_rbLayersOption->GetSelection() != 0 ) // Use current layer only
128  layers_filter = LSET( ToLAYER_ID( m_currentLayer ) );
129 
130  if( delete_all || m_delZones->GetValue() )
131  {
132  int area_index = 0;
133  auto item = board->GetArea( area_index );
134 
135  while( item )
136  {
137  if( delete_all || layers_filter[item->GetLayer()] )
138  {
139  commit.Remove( item );
140  gen_rastnest = true;
141  }
142 
143  area_index++;
144  item = board->GetArea( area_index );
145  }
146  }
147 
148  bool delete_shapes = m_delDrawings->GetValue() || m_delBoardEdges->GetValue();
149  bool delete_texts = m_delTexts->GetValue();
150 
151  if( delete_all || delete_shapes || delete_texts )
152  {
153  // Layer mask for texts
154  LSET del_text_layers = layers_filter;
155 
156  // Layer mask for drawings
157  LSET masque_layer;
158 
159  if( m_delDrawings->GetValue() )
160  masque_layer = LSET::AllNonCuMask().set( Edge_Cuts, false );
161 
162  if( m_delBoardEdges->GetValue() )
163  masque_layer.set( Edge_Cuts );
164 
165  masque_layer &= layers_filter;
166 
167  for( BOARD_ITEM* item : board->Drawings() )
168  {
169  KICAD_T type = item->Type();
170  LAYER_NUM layer = item->GetLayer();
171 
172  if( !delete_all )
173  {
174  if( type == PCB_SHAPE_T )
175  {
176  if( !delete_shapes || !masque_layer[layer] )
177  continue;
178 
179  if( item->IsLocked() && !m_drawingFilterLocked->GetValue() )
180  continue;
181 
182  if( !item->IsLocked() && !m_drawingFilterUnlocked->GetValue() )
183  continue;
184  }
185  else if( type == PCB_TEXT_T )
186  {
187  if( !delete_texts || !del_text_layers[layer] )
188  continue;
189  }
190  }
191 
192  commit.Remove( item );
193  }
194  }
195 
196  if( delete_all || m_delFootprints->GetValue() )
197  {
198  for( FOOTPRINT* footprint : board->Footprints() )
199  {
200  if( !delete_all )
201  {
202  if( footprint->IsLocked() && !m_footprintFilterLocked->GetValue() )
203  continue;
204 
205  if( !footprint->IsLocked() && !m_footprintFilterUnlocked->GetValue() )
206  continue;
207 
208  if( !layers_filter[footprint->GetLayer()] )
209  continue;
210  }
211 
212  commit.Remove( footprint );
213  gen_rastnest = true;
214  }
215  }
216 
217  if( delete_all || m_delTracks->GetValue() )
218  {
219  for( PCB_TRACK* track : board->Tracks() )
220  {
221  if( !delete_all )
222  {
223  if( track->Type() == PCB_TRACE_T )
224  {
225  if( track->IsLocked() && !m_trackFilterLocked->GetValue() )
226  continue;
227 
228  if( !track->IsLocked() && !m_trackFilterUnlocked->GetValue() )
229  continue;
230  }
231 
232  if( ( track->Type() == PCB_VIA_T ) && !m_trackFilterVias->GetValue() )
233  continue;
234 
235  if( ( track->GetLayerSet() & layers_filter ) == 0 )
236  continue;
237  }
238 
239  commit.Remove( track );
240  gen_rastnest = true;
241  }
242  }
243 
244  commit.Push( "Global delete" );
245 
246  if( m_delMarkers->GetValue() )
247  board->DeleteMARKERs();
248 
249  if( gen_rastnest )
250  m_Parent->Compile_Ratsnest( true );
251 
252  // There is a chance that some of tracks have changed their nets, so rebuild ratsnest
253  // from scratch.
254  m_Parent->GetCanvas()->Refresh();
255 }
static TOOL_ACTION selectionClear
Clear the current selection.
Definition: pcb_actions.h:59
const wxString GetLayerName(PCB_LAYER_ID aLayer) const
Return the name of a aLayer.
Definition: board.cpp:361
void Compile_Ratsnest(bool aDisplayStatus)
Create the entire board ratsnest.
Definition: ratsnest.cpp:40
This file is part of the common library.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:80
DIALOG_GLOBAL_DELETION(PCB_EDIT_FRAME *parent)
PCB_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
Class that computes missing connections on a PCB.
Class DIALOG_GLOBAL_DELETION_BASE.
static LSET AllNonCuMask()
Return a mask holding all layer minus CU layers.
Definition: lset.cpp:773
class PCB_TEXT, text on a layer
Definition: typeinfo.h:91
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Run the specified action.
Definition: tool_manager.h:143
int LAYER_NUM
This can be replaced with int and removed.
Definition: layer_ids.h:40
ZONE * GetArea(int index) const
Return the Zone at a given index.
Definition: board.h:834
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition: typeinfo.h:77
class PCB_TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:95
PCB_BASE_EDIT_FRAME * frame() const
LSET is a set of PCB_LAYER_IDs.
Definition: layer_ids.h:502
void DeleteMARKERs()
Delete all MARKERS from the board.
Definition: board.cpp:827
FOOTPRINTS & Footprints()
Definition: board.h:233
Generic, UI-independent tool event.
Definition: tool_event.h:152
void onCheckDeleteFootprints(wxCommandEvent &event) override
#define _(s)
COMMIT & Remove(EDA_ITEM *aItem)
Notify observers that aItem has been removed.
Definition: commit.h:90
int GlobalDeletions(const TOOL_EVENT &aEvent)
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:190
void SetCurrentLayer(LAYER_NUM aLayer)
Definition: layer_ids.h:70
virtual void Refresh(bool aEraseBackground=true, const wxRect *aRect=nullptr) override
Update the board display after modifying it by a python script (note: it is automatically called by a...
The main frame for Pcbnew.
virtual void Push(const wxString &aMessage=wxT("A commit"), bool aCreateUndoEntry=true, bool aSetDirtyBit=true) override
Revert the commit by restoring the modified items state.
void onCheckDeleteDrawings(wxCommandEvent &event) override
class PCB_VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:96
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
Definition: tools_holder.h:54
BOARD * GetBoard() const
class PCB_SHAPE, a segment not on copper layers
Definition: typeinfo.h:90
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Display a yes/no dialog with aMessage and returns the user response.
Definition: confirm.cpp:315
DRAWINGS & Drawings()
Definition: board.h:236
PCB_LAYER_ID ToLAYER_ID(int aLayer)
Definition: lset.cpp:905
TRACKS & Tracks()
Definition: board.h:230
void onCheckDeleteTracks(wxCommandEvent &event) override