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
35#include <geometry/chamfer.h>
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<PCB_SHAPE> aItem ) = 0;
73
79 virtual void MarkItemModified( PCB_SHAPE& aItem ) = 0;
80
86 virtual void DeleteItem( PCB_SHAPE& aItem ) = 0;
87 };
88
94 {
95 public:
101 using CREATION_HANDLER = std::function<void( std::unique_ptr<PCB_SHAPE> )>;
102
108 using MODIFICATION_HANDLER = std::function<void( PCB_SHAPE& )>;
109
115 using DELETION_HANDLER = std::function<void( PCB_SHAPE& )>;
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<PCB_SHAPE> aItem ) override
132 {
133 m_creationHandler( std::move( aItem ) );
134 }
135
141 void MarkItemModified( PCB_SHAPE& aItem ) override { m_modificationHandler( aItem ); }
142
148 void DeleteItem( PCB_SHAPE& 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 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
304
309{
310public:
312 ITEM_MODIFICATION_ROUTINE( aBoard, aHandler ), m_workingPolygon( nullptr )
313 {
314 }
315
316 void ProcessShape( PCB_SHAPE& aPcbShape );
317
318protected:
320
321 virtual bool ProcessSubsequentPolygon( const SHAPE_POLY_SET& aPolygon ) = 0;
322
323private:
325};
326
328{
329public:
331 POLYGON_BOOLEAN_ROUTINE( aBoard, aHandler )
332 {
333 }
334
335 wxString GetCommitDescription() const override;
336 std::optional<wxString> GetStatusMessage() const override;
337
338private:
339 bool ProcessSubsequentPolygon( const SHAPE_POLY_SET& aPolygon ) override;
340};
341
342
344{
345public:
347 POLYGON_BOOLEAN_ROUTINE( aBoard, aHandler )
348 {
349 }
350
351 wxString GetCommitDescription() const override;
352 std::optional<wxString> GetStatusMessage() const override;
353
354private:
355 bool ProcessSubsequentPolygon( const SHAPE_POLY_SET& aPolygon ) override;
356};
357
358
360{
361public:
363 POLYGON_BOOLEAN_ROUTINE( aBoard, aHandler )
364 {
365 }
366
367 wxString GetCommitDescription() const override;
368 std::optional<wxString> GetStatusMessage() const override;
369
370private:
371 bool ProcessSubsequentPolygon( const SHAPE_POLY_SET& aPolygon ) override;
372};
373
374#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:77
A handler that is based on a set of callbacks provided by the user of the ITEM_MODIFICATION_ROUTINE.
void DeleteItem(PCB_SHAPE &aItem) override
Report that the tool has deleted an item on the board.
std::function< void(PCB_SHAPE &)> MODIFICATION_HANDLER
Handler for modifying or deleting an existing item on the board.
void AddNewItem(std::unique_ptr< PCB_SHAPE > aItem) override
Report that the tools wants to add a new item to the board.
std::function< void(PCB_SHAPE &)> DELETION_HANDLER
Handler for modifying or deleting an existing item on the board.
std::function< void(std::unique_ptr< PCB_SHAPE >)> CREATION_HANDLER
Handler for creating a new item on the board.
CALLABLE_BASED_HANDLER(CREATION_HANDLER aCreationHandler, MODIFICATION_HANDLER aModificationHandler, DELETION_HANDLER aDeletionHandler)
void MarkItemModified(PCB_SHAPE &aItem) override
Report that the tool has modified an item on the board.
virtual void DeleteItem(PCB_SHAPE &aItem)=0
Report that the tool has deleted an item on the board.
virtual void MarkItemModified(PCB_SHAPE &aItem)=0
Report that the tool has modified an item on the board.
virtual void AddNewItem(std::unique_ptr< PCB_SHAPE > 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 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)
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.
POLYGON_BOOLEAN_ROUTINE(BOARD_ITEM *aBoard, CHANGE_HANDLER &aHandler)
PCB_SHAPE * GetWorkingPolygon() const
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
Definition: seg.h:42
Represent a set of closed polygons.
STL namespace.
Parameters that define a simple chamfer operation.
Definition: chamfer.h:37