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 (C) 2023 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
176 virtual std::optional<wxString> GetStatusMessage() const = 0;
177
178protected:
182 BOARD_ITEM* GetBoard() const { return m_board; }
183
188
193
201 bool ModifyLineOrDeleteIfZeroLength( PCB_SHAPE& aItem, const std::optional<SEG>& aSeg );
202
207
208private:
211
214};
215
220{
221public:
223 ITEM_MODIFICATION_ROUTINE( aBoard, aHandler )
224 {
225 }
226
242 virtual void ProcessLinePair( PCB_SHAPE& aLineA, PCB_SHAPE& aLineB ) = 0;
243};
244
249{
250public:
251 LINE_FILLET_ROUTINE( BOARD_ITEM* aBoard, CHANGE_HANDLER& aHandler, int filletRadiusIU ) :
252 PAIRWISE_LINE_ROUTINE( aBoard, aHandler ), m_filletRadiusIU( filletRadiusIU )
253 {
254 }
255
256 wxString GetCommitDescription() const override;
257 std::optional<wxString> GetStatusMessage() const override;
258
259 void ProcessLinePair( PCB_SHAPE& aLineA, PCB_SHAPE& aLineB ) override;
260
261private:
263};
264
269{
270public:
272 CHAMFER_PARAMS aChamferParams ) :
273 PAIRWISE_LINE_ROUTINE( aBoard, aHandler ),
274 m_chamferParams( std::move( aChamferParams ) )
275 {
276 }
277
278 wxString GetCommitDescription() const override;
279 std::optional<wxString> GetStatusMessage() const override;
280
281 void ProcessLinePair( PCB_SHAPE& aLineA, PCB_SHAPE& aLineB ) override;
282
283private:
285};
286
291{
292public:
294 PAIRWISE_LINE_ROUTINE( aBoard, aHandler )
295 {
296 }
297
298 wxString GetCommitDescription() const override;
299 std::optional<wxString> GetStatusMessage() const override;
300
301 void ProcessLinePair( PCB_SHAPE& aLineA, PCB_SHAPE& aLineB ) override;
302};
303
308{
309public:
311 {
314 };
315
317 PAIRWISE_LINE_ROUTINE( aBoard, aHandler ), m_params( std::move( aParams ) ),
318 m_haveNarrowMouths( false )
319 {
320 }
321
322 wxString GetCommitDescription() const override;
323 std::optional<wxString> GetStatusMessage() const override;
324
325 void ProcessLinePair( PCB_SHAPE& aLineA, PCB_SHAPE& aLineB ) override;
326
327private:
330};
331
332
337{
338public:
340 ITEM_MODIFICATION_ROUTINE( aBoard, aHandler )
341 {
342 }
343
344 void ProcessShape( PCB_SHAPE& aPcbShape );
345
349 void Finalize();
350
351protected:
353
354 virtual bool ProcessSubsequentPolygon( const SHAPE_POLY_SET& aPolygon ) = 0;
355
356private:
359
360 bool m_firstPolygon = true;
361 int m_width = 0;
362 PCB_LAYER_ID m_layer = PCB_LAYER_ID::UNDEFINED_LAYER;
363 bool m_filled = false;
364};
365
367{
368public:
370 POLYGON_BOOLEAN_ROUTINE( aBoard, aHandler )
371 {
372 }
373
374 wxString GetCommitDescription() const override;
375 std::optional<wxString> GetStatusMessage() const override;
376
377private:
378 bool ProcessSubsequentPolygon( const SHAPE_POLY_SET& aPolygon ) override;
379};
380
381
383{
384public:
386 POLYGON_BOOLEAN_ROUTINE( aBoard, aHandler )
387 {
388 }
389
390 wxString GetCommitDescription() const override;
391 std::optional<wxString> GetStatusMessage() const override;
392
393private:
394 bool ProcessSubsequentPolygon( const SHAPE_POLY_SET& aPolygon ) override;
395};
396
397
399{
400public:
402 POLYGON_BOOLEAN_ROUTINE( aBoard, aHandler )
403 {
404 }
405
406 wxString GetCommitDescription() const override;
407 std::optional<wxString> GetStatusMessage() const override;
408
409private:
410 bool ProcessSubsequentPolygon( const SHAPE_POLY_SET& aPolygon ) override;
411};
412
413
415{
416public:
418 {
425 std::optional<int> gridRounding;
427 };
428
429 OUTSET_ROUTINE( BOARD_ITEM* aBoard, CHANGE_HANDLER& aHandler, const PARAMETERS& aParams ) :
430 ITEM_MODIFICATION_ROUTINE( aBoard, aHandler ), m_params( aParams )
431 {
432 }
433
434 wxString GetCommitDescription() const override;
435
436 std::optional<wxString> GetStatusMessage() const override;
437
438 void ProcessItem( BOARD_ITEM& aItem );
439
440private:
442};
443
444#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:80
Pairwise add dogbone corners to an internal corner.
DOGBONE_CORNER_ROUTINE(BOARD_ITEM *aBoard, CHANGE_HANDLER &aHandler, PARAMETERS aParams)
std::optional< wxString > GetStatusMessage() 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.
A handler that is based on a set of callbacks provided by the user of the ITEM_MODIFICATION_ROUTINE.
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.
An object that has the ability to modify items on a board.
ITEM_MODIFICATION_ROUTINE(BOARD_ITEM *aBoard, CHANGE_HANDLER &aHandler)
void AddFailure()
Mark that one of the actions failed.
virtual wxString GetCommitDescription() const =0
virtual std::optional< wxString > GetStatusMessage() const =0
Get a status message to show when the routine is complete.
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.
Pairwise line tool that adds a chamfer between the lines.
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)
std::optional< wxString > GetStatusMessage() const override
Get a status message to show when the routine is complete.
Pairwise extend to corner or meeting tool.
wxString GetCommitDescription() const override
std::optional< wxString > GetStatusMessage() 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.
Pairwise line tool that adds a fillet to the lines.
wxString GetCommitDescription() const override
void ProcessLinePair(PCB_SHAPE &aLineA, PCB_SHAPE &aLineB) override
Perform the action on the pair of lines given.
std::optional< wxString > GetStatusMessage() const override
Get a status message to show when the routine is complete.
LINE_FILLET_ROUTINE(BOARD_ITEM *aBoard, CHANGE_HANDLER &aHandler, int filletRadiusIU)
void ProcessItem(BOARD_ITEM &aItem)
std::optional< wxString > GetStatusMessage() const override
Get a status message to show when the routine is complete.
const PARAMETERS m_params
OUTSET_ROUTINE(BOARD_ITEM *aBoard, CHANGE_HANDLER &aHandler, const PARAMETERS &aParams)
wxString GetCommitDescription() const override
A tool that acts on a pair of lines.
PAIRWISE_LINE_ROUTINE(BOARD_ITEM *aBoard, CHANGE_HANDLER &aHandler)
virtual void ProcessLinePair(PCB_SHAPE &aLineA, PCB_SHAPE &aLineB)=0
Perform the action on the pair of lines given.
A routine that modifies polygons using boolean operations.
SHAPE_POLY_SET m_workingPolygons
This can be disjoint, which will be fixed at the end.
void Finalize()
Clear up any outstanding work.
POLYGON_BOOLEAN_ROUTINE(BOARD_ITEM *aBoard, CHANGE_HANDLER &aHandler)
void ProcessShape(PCB_SHAPE &aPcbShape)
virtual bool ProcessSubsequentPolygon(const SHAPE_POLY_SET &aPolygon)=0
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)
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.
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:60
STL namespace.
Parameters that define a simple chamfer operation.