KiCad PCB EDA Suite
Loading...
Searching...
No Matches
zone_filler_tool.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) 2014-2017 CERN
5 * Copyright (C) 2014-2023 KiCad Developers, see AUTHORS.txt for contributors.
6 * @author Maciej Suminski <[email protected]>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, you may find one here:
20 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21 * or you may search the http://www.gnu.org website for the version 2 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25#include <cstdint>
26#include <thread>
27#include <zone.h>
29#include <board_commit.h>
30#include <footprint.h>
31#include <pcb_track.h>
32#include <pad.h>
33#include <pcb_group.h>
35#include <progress_reporter.h>
36#include <widgets/wx_infobar.h>
38#include <wx/event.h>
39#include <wx/hyperlink.h>
40#include <tool/tool_manager.h>
41#include <tool/actions.h>
43#include "pcb_actions.h"
44#include "zone_filler_tool.h"
45#include "zone_filler.h"
46#include "teardrop/teardrop.h"
47#include <core/profile.h>
48
50 PCB_TOOL_BASE( "pcbnew.ZoneFiller" ),
51 m_fillInProgress( false )
52{
53}
54
55
57{
58}
59
60
62{
63}
64
65
66void ZONE_FILLER_TOOL::CheckAllZones( wxWindow* aCaller, PROGRESS_REPORTER* aReporter )
67{
68 if( !getEditFrame<PCB_EDIT_FRAME>()->m_ZoneFillsDirty || m_fillInProgress )
69 return;
70
71 m_fillInProgress = true;
72
73 std::vector<ZONE*> toFill;
74
75 for( ZONE* zone : board()->Zones() )
76 toFill.push_back( zone );
77
78 BOARD_COMMIT commit( this );
79 std::unique_ptr<WX_PROGRESS_REPORTER> reporter;
80
81 m_filler = std::make_unique<ZONE_FILLER>( frame()->GetBoard(), &commit );
82
83 if( aReporter )
84 {
85 m_filler->SetProgressReporter( aReporter );
86 }
87 else
88 {
89 reporter = std::make_unique<WX_PROGRESS_REPORTER>( aCaller, _( "Checking Zones" ), 4 );
90 m_filler->SetProgressReporter( reporter.get() );
91 }
92
93 if( m_filler->Fill( toFill, true, aCaller ) )
94 {
95 commit.Push( _( "Fill Zone(s)" ), SKIP_CONNECTIVITY | ZONE_FILL_OP );
96 getEditFrame<PCB_EDIT_FRAME>()->m_ZoneFillsDirty = false;
97 }
98 else
99 {
100 commit.Revert();
101 }
102
104 refresh();
105
106 m_fillInProgress = false;
107 m_filler.reset( nullptr );
108}
109
110
112{
113 canvas()->SetFocus();
114 canvas()->Unbind( wxEVT_IDLE, &ZONE_FILLER_TOOL::singleShotRefocus, this );
115}
116
117
118void ZONE_FILLER_TOOL::FillAllZones( wxWindow* aCaller, PROGRESS_REPORTER* aReporter )
119{
120 if( m_fillInProgress )
121 return;
122
123 m_fillInProgress = true;
124
125 PCB_EDIT_FRAME* frame = getEditFrame<PCB_EDIT_FRAME>();
126 BOARD_COMMIT commit( this );
127 std::unique_ptr<WX_PROGRESS_REPORTER> reporter;
128 TEARDROP_MANAGER teardropMgr( board(), m_toolMgr );
129 std::vector<ZONE*> toFill;
130
131 teardropMgr.UpdateTeardrops( commit, nullptr, nullptr, true /* forceFullUpdate */ );
132
133 board()->IncrementTimeStamp(); // Clear caches
134
135 for( ZONE* zone : board()->Zones() )
136 toFill.push_back( zone );
137
138 m_filler = std::make_unique<ZONE_FILLER>( board(), &commit );
139
140 if( !board()->GetDesignSettings().m_DRCEngine->RulesValid() )
141 {
142 WX_INFOBAR* infobar = frame->GetInfoBar();
143 wxHyperlinkCtrl* button = new wxHyperlinkCtrl( infobar, wxID_ANY, _( "Show DRC rules" ),
144 wxEmptyString );
145
146 button->Bind( wxEVT_COMMAND_HYPERLINK,
147 std::function<void( wxHyperlinkEvent& aEvent )>(
148 [frame]( wxHyperlinkEvent& aEvent )
149 {
150 frame->ShowBoardSetupDialog( _( "Rules" ) );
151 } ) );
152
153 infobar->RemoveAllButtons();
154 infobar->AddButton( button );
155
156 infobar->ShowMessageFor( _( "Zone fills may be inaccurate. DRC rules contain errors." ),
157 10000, wxICON_WARNING );
158 }
159
160 if( aReporter )
161 {
162 m_filler->SetProgressReporter( aReporter );
163 }
164 else
165 {
166 reporter = std::make_unique<WX_PROGRESS_REPORTER>( aCaller, _( "Fill All Zones" ), 5 );
167 m_filler->SetProgressReporter( reporter.get() );
168 }
169
170 if( m_filler->Fill( toFill ) )
171 {
172 m_filler->GetProgressReporter()->AdvancePhase();
173
174 commit.Push( _( "Fill Zone(s)" ), SKIP_CONNECTIVITY | ZONE_FILL_OP );
175 frame->m_ZoneFillsDirty = false;
176 }
177 else
178 {
179 commit.Revert();
180 }
181
183 refresh();
184
185 if( m_filler->IsDebug() )
186 frame->UpdateUserInterface();
187
188 m_fillInProgress = false;
189 m_filler.reset( nullptr );
190
191 // wxWidgets has keyboard focus issues after the progress reporter. Re-setting the focus
192 // here doesn't work, so we delay it to an idle event.
193 canvas()->Bind( wxEVT_IDLE, &ZONE_FILLER_TOOL::singleShotRefocus, this );
194}
195
196
198{
199 PCB_EDIT_FRAME* frame = getEditFrame<PCB_EDIT_FRAME>();
200 std::vector<ZONE*> toFill;
201
202 for( ZONE* zone : board()->Zones() )
203 {
204 if( !zone->IsFilled() || m_dirtyZoneIDs.count( zone->m_Uuid ) )
205 toFill.push_back( zone );
206 }
207
208 if( toFill.empty() )
209 return 0;
210
211 if( m_fillInProgress )
212 return 0;
213
214 unsigned startTime = GetRunningMicroSecs();
215 m_fillInProgress = true;
216
217 m_dirtyZoneIDs.clear();
218
219 board()->IncrementTimeStamp(); // Clear caches
220
221 BOARD_COMMIT commit( this );
222 std::unique_ptr<WX_PROGRESS_REPORTER> reporter;
223 int pts = 0;
224
225 m_filler = std::make_unique<ZONE_FILLER>( board(), &commit );
226
227 if( !board()->GetDesignSettings().m_DRCEngine->RulesValid() )
228 {
229 WX_INFOBAR* infobar = frame->GetInfoBar();
230 wxHyperlinkCtrl* button = new wxHyperlinkCtrl( infobar, wxID_ANY, _( "Show DRC rules" ),
231 wxEmptyString );
232
233 button->Bind( wxEVT_COMMAND_HYPERLINK,
234 std::function<void( wxHyperlinkEvent& aLocEvent )>(
235 [frame]( wxHyperlinkEvent& aLocEvent )
236 {
237 frame->ShowBoardSetupDialog( _( "Rules" ) );
238 } ) );
239
240 infobar->RemoveAllButtons();
241 infobar->AddButton( button );
242
243 infobar->ShowMessageFor( _( "Zone fills may be inaccurate. DRC rules contain errors." ),
244 10000, wxICON_WARNING );
245 }
246
247 for( ZONE* zone : toFill )
248 {
249 for( PCB_LAYER_ID layer : zone->GetLayerSet().Seq() )
250 pts += zone->GetFilledPolysList( layer )->FullPointCount();
251
252 if( pts > 1000 )
253 {
254 wxString title = wxString::Format( _( "Refill %d Zones" ), (int) toFill.size() );
255
256 reporter = std::make_unique<WX_PROGRESS_REPORTER>( frame, title, 5 );
257 m_filler->SetProgressReporter( reporter.get() );
258 break;
259 }
260 }
261
262 if( m_filler->Fill( toFill ) )
263 commit.Push( _( "Auto-fill Zone(s)" ), APPEND_UNDO | SKIP_CONNECTIVITY | ZONE_FILL_OP );
264 else
265 commit.Revert();
266
268 refresh();
269
270 if( GetRunningMicroSecs() - startTime > 3000000 ) // 3 seconds
271 {
272 WX_INFOBAR* infobar = frame->GetInfoBar();
273
274 wxHyperlinkCtrl* button = new wxHyperlinkCtrl( infobar, wxID_ANY, _( "Open Preferences" ),
275 wxEmptyString );
276
277 button->Bind( wxEVT_COMMAND_HYPERLINK, std::function<void( wxHyperlinkEvent& )>(
278 [this]( wxHyperlinkEvent& )
279 {
280 getEditFrame<PCB_EDIT_FRAME>()->ShowPreferences( _( "Editing Options" ),
281 _( "PCB Editor" ) );
282 } ) );
283
284 infobar->RemoveAllButtons();
285 infobar->AddButton( button );
286 infobar->ShowMessageFor( _( "Automatic refill of zones can be turned off in Preferences "
287 "if it becomes too slow." ),
288 10000, wxICON_INFORMATION, WX_INFOBAR::MESSAGE_TYPE::GENERIC );
289 }
290
291 if( m_filler->IsDebug() )
292 frame->UpdateUserInterface();
293
294 m_fillInProgress = false;
295 m_filler.reset( nullptr );
296
297 // wxWidgets has keyboard focus issues after the progress reporter. Re-setting the focus
298 // here doesn't work, so we delay it to an idle event.
299 canvas()->Bind( wxEVT_IDLE, &ZONE_FILLER_TOOL::singleShotRefocus, this );
300
301 return 0;
302}
303
304
306{
307 if( m_fillInProgress )
308 {
309 wxBell();
310 return -1;
311 }
312
313 std::vector<ZONE*> toFill;
314
315 if( ZONE* passedZone = aEvent.Parameter<ZONE*>() )
316 {
317 toFill.push_back( passedZone );
318 }
319 else
320 {
321 const PCB_SELECTION& sel = m_toolMgr->GetTool<PCB_SELECTION_TOOL>()->RequestSelection(
322 []( const VECTOR2I& aPt, GENERAL_COLLECTOR& aCollector, PCB_SELECTION_TOOL* sTool )
323 {
324 } );
325
326 for( EDA_ITEM* item : sel )
327 {
328 if( ZONE* zone = dynamic_cast<ZONE*>( item ) )
329 toFill.push_back( zone );
330 }
331 }
332
333 // Bail out of the filler if there is nothing to fill
334 if( toFill.empty() )
335 {
336 wxBell();
337 return -1;
338 }
339
340 m_fillInProgress = true;
341
342 BOARD_COMMIT commit( this );
343 std::unique_ptr<WX_PROGRESS_REPORTER> reporter;
344
345 m_filler = std::make_unique<ZONE_FILLER>( board(), &commit );
346
347 reporter = std::make_unique<WX_PROGRESS_REPORTER>( frame(), _( "Fill Zone" ), 5 );
348 m_filler->SetProgressReporter( reporter.get() );
349
350 if( m_filler->Fill( toFill ) )
351 {
352 reporter->AdvancePhase();
353 commit.Push( _( "Fill Zone(s)" ), SKIP_CONNECTIVITY | ZONE_FILL_OP );
354 }
355 else
356 {
357 commit.Revert();
358 }
359
361 refresh();
362
363 m_fillInProgress = false;
364 m_filler.reset( nullptr );
365 return 0;
366}
367
368
370{
371 FillAllZones( frame() );
372 return 0;
373}
374
375
377{
378 const PCB_SELECTION& sel = m_toolMgr->GetTool<PCB_SELECTION_TOOL>()->RequestSelection(
379 []( const VECTOR2I& aPt, GENERAL_COLLECTOR& aCollector, PCB_SELECTION_TOOL* sTool )
380 {
381 } );
382
383 std::vector<ZONE*> toUnfill;
384
385 for( EDA_ITEM* item : sel )
386 {
387 if( ZONE* zone = dynamic_cast<ZONE*>( item ) )
388 toUnfill.push_back( zone );
389 }
390
391 // Bail out if there are no zones
392 if( toUnfill.empty() )
393 {
394 wxBell();
395 return -1;
396 }
397
398 BOARD_COMMIT commit( this );
399
400 for( ZONE* zone : toUnfill )
401 {
402 commit.Modify( zone );
403
404 zone->UnFill();
405 }
406
407 commit.Push( _( "Unfill Zone" ), ZONE_FILL_OP );
408
409 refresh();
410
411 return 0;
412}
413
414
416{
417 BOARD_COMMIT commit( this );
418
419 for( ZONE* zone : board()->Zones() )
420 {
421 commit.Modify( zone );
422
423 zone->UnFill();
424 }
425
426 commit.Push( _( "Unfill All Zones" ), ZONE_FILL_OP );
427
428 refresh();
429
430 return 0;
431}
432
433
435{
437 return m_filler->GetProgressReporter();
438 else
439 return nullptr;
440}
441
442
444{
448}
449
450
452{
453 // Note: KIGFX::REPAINT isn't enough for things that go from invisible to visible as
454 // they won't be found in the view layer's itemset for re-painting.
456 [&]( KIGFX::VIEW_ITEM* aItem ) -> bool
457 {
458 if( PCB_VIA* via = dynamic_cast<PCB_VIA*>( aItem ) )
459 {
460 return via->GetRemoveUnconnected();
461 }
462 else if( PAD* pad = dynamic_cast<PAD*>( aItem ) )
463 {
464 return pad->GetRemoveUnconnected();
465 }
466
467 return false;
468 } );
469
470 canvas()->Refresh();
471}
472
473
475{
476 return aEvent->IsAction( &PCB_ACTIONS::zoneFill )
478 || aEvent->IsAction( &PCB_ACTIONS::zoneUnfill )
480
481 // Don't include zoneFillDirty; that's a system action not a user action
482}
483
484
486{
487 // Zone actions
493}
#define ZONE_FILL_OP
Definition: board_commit.h:43
virtual void Push(const wxString &aMessage=wxEmptyString, int aCommitFlags=0) override
Revert the commit by restoring the modified items state.
virtual void Revert() override
bool BuildConnectivity(PROGRESS_REPORTER *aReporter=nullptr)
Build or rebuild the board connectivity database for the board, especially the list of connected item...
Definition: board.cpp:184
void IncrementTimeStamp()
Definition: board.cpp:253
COMMIT & Modify(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Create an undo entry for an item that has been already modified.
Definition: commit.h:105
WX_INFOBAR * GetInfoBar()
virtual void Refresh(bool aEraseBackground=true, const wxRect *aRect=nullptr) override
void SetFocus() override
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:85
static const TOOL_EVENT ConnectivityChangedEvent
Selected item had a property changed (except movement)
Definition: actions.h:257
Used when the right click button is pressed, or when the select tool is in effect.
Definition: collectors.h:206
An abstract base class for deriving all objects that can be added to a VIEW.
Definition: view_item.h:84
void UpdateAllItemsConditionally(int aUpdateFlags, std::function< bool(VIEW_ITEM *)> aCondition)
Update items in the view according to the given flags and condition.
Definition: view.cpp:1511
Definition: pad.h:59
static TOOL_ACTION zoneFillAll
Definition: pcb_actions.h:392
static TOOL_ACTION zoneFill
Definition: pcb_actions.h:391
static TOOL_ACTION zoneUnfill
Definition: pcb_actions.h:394
static TOOL_ACTION zoneUnfillAll
Definition: pcb_actions.h:395
static TOOL_ACTION zoneFillDirty
Definition: pcb_actions.h:393
virtual KIGFX::PCB_VIEW * GetView() const override
Return a pointer to the #VIEW instance used in the panel.
void RedrawRatsnest()
Return the bounding box of the view that should be used if model is not valid.
The main frame for Pcbnew.
The selection tool: currently supports:
PCB_BASE_EDIT_FRAME * frame() const
BOARD * board() const
PCB_DRAW_PANEL_GAL * canvas() const
A progress reporter interface for use in multi-threaded environments.
TEARDROP_MANAGER manage and build teardrop areas A teardrop area is a polygonal area (a copper ZONE) ...
Definition: teardrop.h:97
void UpdateTeardrops(BOARD_COMMIT &aCommit, const std::vector< BOARD_ITEM * > *dirtyPadsAndVias, const std::set< PCB_TRACK * > *dirtyTracks, bool aForceFullUpdate=false)
Update teardrops on a list of items.
Definition: teardrop.cpp:151
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:216
RESET_REASON
Determine the reason of reset for a tool.
Definition: tool_base.h:78
Generic, UI-independent tool event.
Definition: tool_event.h:167
bool IsAction(const TOOL_ACTION *aAction) const
Test if the event contains an action issued upon activation of the given TOOL_ACTION.
Definition: tool_event.cpp:82
T Parameter() const
Return a parameter assigned to the event.
Definition: tool_event.h:460
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Define which state (aStateFunc) to go when a certain event arrives (aConditions).
void PostEvent(const TOOL_EVENT &aEvent)
Put an event to the event queue to be processed at the end of event processing cycle.
A modified version of the wxInfoBar class that allows us to:
Definition: wx_infobar.h:75
void RemoveAllButtons()
Remove all the buttons that have been added by the user.
Definition: wx_infobar.cpp:301
void ShowMessageFor(const wxString &aMessage, int aTime, int aFlags=wxICON_INFORMATION, MESSAGE_TYPE aType=WX_INFOBAR::MESSAGE_TYPE::GENERIC)
Show the infobar with the provided message and icon for a specific period of time.
Definition: wx_infobar.cpp:140
void AddButton(wxButton *aButton)
Add an already created button to the infobar.
Definition: wx_infobar.cpp:260
@ GENERIC
GENERIC Are messages that do not have special handling.
void CheckAllZones(wxWindow *aCaller, PROGRESS_REPORTER *aReporter=nullptr)
static bool IsZoneFillAction(const TOOL_EVENT *aEvent)
void Reset(RESET_REASON aReason) override
Bring the tool to a known, initial state.
void FillAllZones(wxWindow *aCaller, PROGRESS_REPORTER *aReporter=nullptr)
int ZoneFill(const TOOL_EVENT &aEvent)
std::unique_ptr< ZONE_FILLER > m_filler
PROGRESS_REPORTER * GetProgressReporter()
int ZoneUnfillAll(const TOOL_EVENT &aEvent)
void setTransitions() override
This method is meant to be overridden in order to specify handlers for events.
void refresh()
Set up handlers for various events.
std::set< KIID > m_dirtyZoneIDs
int ZoneFillAll(const TOOL_EVENT &aEvent)
int ZoneFillDirty(const TOOL_EVENT &aEvent)
void singleShotRefocus(wxIdleEvent &)
< Refocus on an idle event (used after the Progress Reporter messes up the focus).
int ZoneUnfill(const TOOL_EVENT &aEvent)
Handle a list of polygons defining a copper zone.
Definition: zone.h:72
#define _(s)
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:60
@ ALL
All except INITIAL_ADD.
Definition: view_item.h:58
Class to handle a set of BOARD_ITEMs.
BOARD * GetBoard()
unsigned GetRunningMicroSecs()
An alternate way to calculate an elapsed time (in microsecondes) to class PROF_COUNTER.
#define SKIP_CONNECTIVITY
Definition: sch_commit.h:43
#define APPEND_UNDO
Definition: sch_commit.h:41