KiCad PCB EDA Suite
Loading...
Searching...
No Matches
item_modification_routine.h
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
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#ifndef ITEM_MODIFICATION_ROUTINE_H_
25#define ITEM_MODIFICATION_ROUTINE_H_
26
27#include <functional>
28#include <memory>
29#include <optional>
30#include <vector>
31
32#include <board_item.h>
33#include <pcb_shape.h>
34
36
47{
48public:
49 /*
50 * Handlers for receiving changes from the tool
51 *
52 * These are used to allow the tool's caller to make changes to
53 * affected board items using extra information that the tool
54 * does not have access to (e.g. is this an FP editor, was
55 * the line created from a rectangle and needs to be added, not
56 * modified, etc).
57 *
58 * We can't store them up until the end, because modifications
59 * need the old state to be known, so this allows the caller to
60 * inject the dependencies for how to handle the changes.
61 */
63 {
64 public:
65 virtual ~CHANGE_HANDLER() = default;
66
72 virtual void AddNewItem( std::unique_ptr<BOARD_ITEM> aItem ) = 0;
73
79 virtual void MarkItemModified( BOARD_ITEM& aItem ) = 0;
80
86 virtual void DeleteItem( BOARD_ITEM& aItem ) = 0;
87 };
88
94 {
95 public:
101 using CREATION_HANDLER = std::function<void( std::unique_ptr<BOARD_ITEM> )>;
102
108 using MODIFICATION_HANDLER = std::function<void( BOARD_ITEM& )>;
109
115 using DELETION_HANDLER = std::function<void( BOARD_ITEM& )>;
116
118 MODIFICATION_HANDLER aModificationHandler,
119 DELETION_HANDLER aDeletionHandler ) :
120 m_creationHandler( std::move( aCreationHandler ) ),
121 m_modificationHandler( std::move( aModificationHandler ) ),
122 m_deletionHandler( std::move( aDeletionHandler ) )
123 {
124 }
125
131 void AddNewItem( std::unique_ptr<BOARD_ITEM> aItem ) override
132 {
133 m_creationHandler( std::move( aItem ) );
134 }
135
141 void MarkItemModified( BOARD_ITEM& aItem ) override { m_modificationHandler( aItem ); }
142
148 void DeleteItem( BOARD_ITEM& aItem ) override { m_deletionHandler( aItem ); }
149
153 };
154
156 m_board( aBoard ),
157 m_handler( aHandler ),
158 m_numSuccesses( 0 ),
159 m_numFailures( 0 )
160 {
161 }
162
163 virtual ~ITEM_MODIFICATION_ROUTINE() = default;
164
165 unsigned GetSuccesses() const { return m_numSuccesses; }
166
167 unsigned GetFailures() const { return m_numFailures; }
168
169 virtual wxString GetCommitDescription() const = 0;
170
171protected:
175 BOARD_ITEM* GetBoard() const { return m_board; }
176
181
186
194 bool ModifyLineOrDeleteIfZeroLength( PCB_SHAPE& aItem, const std::optional<SEG>& aSeg );
195
200
201private:
204
207};
208
213{
214public:
216 ITEM_MODIFICATION_ROUTINE( aBoard, aHandler )
217 {
218 }
219
235 virtual void ProcessLinePair( PCB_SHAPE& aLineA, PCB_SHAPE& aLineB ) = 0;
236
242 virtual std::optional<wxString> GetStatusMessage( int aSegmentCount ) const = 0;
243};
244
249{
250public:
251 LINE_FILLET_ROUTINE( BOARD_ITEM* aBoard, CHANGE_HANDLER& aHandler, int filletRadiusIU ) :
252 PAIRWISE_LINE_ROUTINE( aBoard, aHandler ),
253 m_filletRadiusIU( filletRadiusIU )
254 {
255 }
256
257 wxString GetCommitDescription() const override;
258 std::optional<wxString> GetStatusMessage( int aSegmentCount ) const override;
259
260 void ProcessLinePair( PCB_SHAPE& aLineA, PCB_SHAPE& aLineB ) override;
261
262private:
264};
265
270{
271public:
273 CHAMFER_PARAMS aChamferParams ) :
274 PAIRWISE_LINE_ROUTINE( aBoard, aHandler ),
275 m_chamferParams( std::move( aChamferParams ) )
276 {
277 }
278
279 wxString GetCommitDescription() const override;
280 std::optional<wxString> GetStatusMessage( int aSegmentCount ) const override;
281
282 void ProcessLinePair( PCB_SHAPE& aLineA, PCB_SHAPE& aLineB ) override;
283
284private:
286};
287
292{
293public:
295 PAIRWISE_LINE_ROUTINE( aBoard, aHandler )
296 {
297 }
298
299 wxString GetCommitDescription() const override;
300 std::optional<wxString> GetStatusMessage( int aSegmentCount ) const override;
301
302 void ProcessLinePair( PCB_SHAPE& aLineA, PCB_SHAPE& aLineB ) override;
303};
304
309{
310public:
312 {
315 };
316
318 PAIRWISE_LINE_ROUTINE( aBoard, aHandler ),
319 m_params( std::move( aParams ) ),
320 m_haveNarrowMouths( false )
321 {
322 }
323
324 wxString GetCommitDescription() const override;
325 std::optional<wxString> GetStatusMessage( int aSegmentCount ) const override;
326
327 void ProcessLinePair( PCB_SHAPE& aLineA, PCB_SHAPE& aLineB ) override;
328
329private:
330 // Lazily load board outline polygons (outer outline + any holes)
331 bool EnsureBoardOutline() const;
332
335 mutable bool m_boardOutlineCached = false;
337};
338
339
344{
345public:
347 ITEM_MODIFICATION_ROUTINE( aBoard, aHandler )
348 {
349 }
350
354 virtual bool IsCommutative() const = 0;
355
356 void ProcessShape( PCB_SHAPE& aPcbShape );
357
361 void Finalize();
362
368 virtual std::optional<wxString> GetStatusMessage() const = 0;
369
370protected:
372
373 virtual bool ProcessSubsequentPolygon( const SHAPE_POLY_SET& aPolygon ) = 0;
374
375private:
378
379 bool m_firstPolygon = true;
380 int m_width = 0;
383};
384
386{
387public:
389 POLYGON_BOOLEAN_ROUTINE( aBoard, aHandler )
390 {
391 }
392
393 bool IsCommutative() const override { return true; }
394
395 wxString GetCommitDescription() const override;
396 std::optional<wxString> GetStatusMessage() const override;
397
398private:
399 bool ProcessSubsequentPolygon( const SHAPE_POLY_SET& aPolygon ) override;
400};
401
402
404{
405public:
407 POLYGON_BOOLEAN_ROUTINE( aBoard, aHandler )
408 {
409 }
410
411 bool IsCommutative() const override { return false; }
412
413 wxString GetCommitDescription() const override;
414 std::optional<wxString> GetStatusMessage() const override;
415
416private:
417 bool ProcessSubsequentPolygon( const SHAPE_POLY_SET& aPolygon ) override;
418};
419
420
422{
423public:
425 POLYGON_BOOLEAN_ROUTINE( aBoard, aHandler )
426 {
427 }
428
429 bool IsCommutative() const override { return true; }
430
431 wxString GetCommitDescription() const override;
432 std::optional<wxString> GetStatusMessage() const override;
433
434private:
435 bool ProcessSubsequentPolygon( const SHAPE_POLY_SET& aPolygon ) override;
436};
437
438
440{
441public:
453
454 OUTSET_ROUTINE( BOARD_ITEM* aBoard, CHANGE_HANDLER& aHandler, const PARAMETERS& aParams ) :
455 ITEM_MODIFICATION_ROUTINE( aBoard, aHandler ),
456 m_params( aParams )
457 {
458 }
459
460 wxString GetCommitDescription() const override;
461
462 std::optional<wxString> GetStatusMessage() const;
463
464 void ProcessItem( BOARD_ITEM& aItem );
465
466private:
468};
469
470#endif /* ITEM_MODIFICATION_ROUTINE_H_ */
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:79
SHAPE_POLY_SET m_boardOutline
Cached board outline polygons.
std::optional< wxString > GetStatusMessage(int aSegmentCount) const override
Get a status message to show when the routine is complete.
DOGBONE_CORNER_ROUTINE(BOARD_ITEM *aBoard, CHANGE_HANDLER &aHandler, PARAMETERS aParams)
wxString GetCommitDescription() const override
void ProcessLinePair(PCB_SHAPE &aLineA, PCB_SHAPE &aLineB) override
Perform the action on the pair of lines given.
void MarkItemModified(BOARD_ITEM &aItem) override
Report that the tool has modified an item on the board.
std::function< void(BOARD_ITEM &)> MODIFICATION_HANDLER
Handler for modifying or deleting an existing item on the board.
std::function< void(std::unique_ptr< BOARD_ITEM >)> CREATION_HANDLER
Handler for creating a new item on the board.
CALLABLE_BASED_HANDLER(CREATION_HANDLER aCreationHandler, MODIFICATION_HANDLER aModificationHandler, DELETION_HANDLER aDeletionHandler)
void DeleteItem(BOARD_ITEM &aItem) override
Report that the tool has deleted an item on the board.
void AddNewItem(std::unique_ptr< BOARD_ITEM > aItem) override
Report that the tools wants to add a new item to the board.
std::function< void(BOARD_ITEM &)> DELETION_HANDLER
Handler for modifying or deleting an existing item on the board.
virtual void MarkItemModified(BOARD_ITEM &aItem)=0
Report that the tool has modified an item on the board.
virtual void DeleteItem(BOARD_ITEM &aItem)=0
Report that the tool has deleted an item on the board.
virtual void AddNewItem(std::unique_ptr< BOARD_ITEM > aItem)=0
Report that the tools wants to add a new item to the board.
ITEM_MODIFICATION_ROUTINE(BOARD_ITEM *aBoard, CHANGE_HANDLER &aHandler)
void AddFailure()
Mark that one of the actions failed.
virtual wxString GetCommitDescription() const =0
void AddSuccess()
Mark that one of the actions succeeded.
bool ModifyLineOrDeleteIfZeroLength(PCB_SHAPE &aItem, const std::optional< SEG > &aSeg)
Helper function useful for multiple tools: modify a line or delete it if it has zero length.
BOARD_ITEM * GetBoard() const
The BOARD used when creating new shapes.
virtual ~ITEM_MODIFICATION_ROUTINE()=default
CHANGE_HANDLER & GetHandler()
Access the handler for making changes to the board.
std::optional< wxString > GetStatusMessage(int aSegmentCount) const override
Get a status message to show when the routine is complete.
wxString GetCommitDescription() const override
void ProcessLinePair(PCB_SHAPE &aLineA, PCB_SHAPE &aLineB) override
Perform the action on the pair of lines given.
const CHAMFER_PARAMS m_chamferParams
LINE_CHAMFER_ROUTINE(BOARD_ITEM *aBoard, CHANGE_HANDLER &aHandler, CHAMFER_PARAMS aChamferParams)
wxString GetCommitDescription() const override
std::optional< wxString > GetStatusMessage(int aSegmentCount) const override
Get a status message to show when the routine is complete.
LINE_EXTENSION_ROUTINE(BOARD_ITEM *aBoard, CHANGE_HANDLER &aHandler)
void ProcessLinePair(PCB_SHAPE &aLineA, PCB_SHAPE &aLineB) override
Perform the action on the pair of lines given.
std::optional< wxString > GetStatusMessage(int aSegmentCount) const override
Get a status message to show when the routine is complete.
wxString GetCommitDescription() const override
void ProcessLinePair(PCB_SHAPE &aLineA, PCB_SHAPE &aLineB) override
Perform the action on the pair of lines given.
LINE_FILLET_ROUTINE(BOARD_ITEM *aBoard, CHANGE_HANDLER &aHandler, int filletRadiusIU)
void ProcessItem(BOARD_ITEM &aItem)
OUTSET_ROUTINE(BOARD_ITEM *aBoard, CHANGE_HANDLER &aHandler, const PARAMETERS &aParams)
wxString GetCommitDescription() const override
std::optional< wxString > GetStatusMessage() const
PAIRWISE_LINE_ROUTINE(BOARD_ITEM *aBoard, CHANGE_HANDLER &aHandler)
virtual std::optional< wxString > GetStatusMessage(int aSegmentCount) const =0
Get a status message to show when the routine is complete.
virtual void ProcessLinePair(PCB_SHAPE &aLineA, PCB_SHAPE &aLineB)=0
Perform the action on the pair of lines given.
virtual bool IsCommutative() const =0
False if the order of the polygons matters.
SHAPE_POLY_SET m_workingPolygons
This can be disjoint, which will be fixed at the end.
void Finalize()
Clear up any outstanding work.
virtual std::optional< wxString > GetStatusMessage() const =0
Get a status message to show when the routine is complete.
POLYGON_BOOLEAN_ROUTINE(BOARD_ITEM *aBoard, CHANGE_HANDLER &aHandler)
void ProcessShape(PCB_SHAPE &aPcbShape)
virtual bool ProcessSubsequentPolygon(const SHAPE_POLY_SET &aPolygon)=0
bool IsCommutative() const override
False if the order of the polygons matters.
std::optional< wxString > GetStatusMessage() const override
Get a status message to show when the routine is complete.
wxString GetCommitDescription() const override
POLYGON_INTERSECT_ROUTINE(BOARD_ITEM *aBoard, CHANGE_HANDLER &aHandler)
bool ProcessSubsequentPolygon(const SHAPE_POLY_SET &aPolygon) override
bool ProcessSubsequentPolygon(const SHAPE_POLY_SET &aPolygon) override
wxString GetCommitDescription() const override
std::optional< wxString > GetStatusMessage() const override
Get a status message to show when the routine is complete.
POLYGON_MERGE_ROUTINE(BOARD_ITEM *aBoard, CHANGE_HANDLER &aHandler)
bool IsCommutative() const override
False if the order of the polygons matters.
bool IsCommutative() const override
False if the order of the polygons matters.
wxString GetCommitDescription() const override
POLYGON_SUBTRACT_ROUTINE(BOARD_ITEM *aBoard, CHANGE_HANDLER &aHandler)
std::optional< wxString > GetStatusMessage() const override
Get a status message to show when the routine is complete.
bool ProcessSubsequentPolygon(const SHAPE_POLY_SET &aPolygon) override
Represent a set of closed polygons.
FILL_T
Definition eda_shape.h:56
@ NO_FILL
Definition eda_shape.h:57
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:60
@ UNDEFINED_LAYER
Definition layer_ids.h:61
STL namespace.
Parameters that define a simple chamfer operation.