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:
332};
333
334
339{
340public:
342 ITEM_MODIFICATION_ROUTINE( aBoard, aHandler )
343 {
344 }
345
346 void ProcessShape( PCB_SHAPE& aPcbShape );
347
351 void Finalize();
352
358 virtual std::optional<wxString> GetStatusMessage() const = 0;
359
360protected:
362
363 virtual bool ProcessSubsequentPolygon( const SHAPE_POLY_SET& aPolygon ) = 0;
364
365private:
368
369 bool m_firstPolygon = true;
370 int m_width = 0;
371 PCB_LAYER_ID m_layer = PCB_LAYER_ID::UNDEFINED_LAYER;
372 FILL_T m_fillMode = FILL_T::NO_FILL;
373};
374
376{
377public:
379 POLYGON_BOOLEAN_ROUTINE( aBoard, aHandler )
380 {
381 }
382
383 wxString GetCommitDescription() const override;
384 std::optional<wxString> GetStatusMessage() const override;
385
386private:
387 bool ProcessSubsequentPolygon( const SHAPE_POLY_SET& aPolygon ) override;
388};
389
390
392{
393public:
395 POLYGON_BOOLEAN_ROUTINE( aBoard, aHandler )
396 {
397 }
398
399 wxString GetCommitDescription() const override;
400 std::optional<wxString> GetStatusMessage() const override;
401
402private:
403 bool ProcessSubsequentPolygon( const SHAPE_POLY_SET& aPolygon ) override;
404};
405
406
408{
409public:
411 POLYGON_BOOLEAN_ROUTINE( aBoard, aHandler )
412 {
413 }
414
415 wxString GetCommitDescription() const override;
416 std::optional<wxString> GetStatusMessage() const override;
417
418private:
419 bool ProcessSubsequentPolygon( const SHAPE_POLY_SET& aPolygon ) override;
420};
421
422
424{
425public:
427 {
434 std::optional<int> gridRounding;
436 };
437
438 OUTSET_ROUTINE( BOARD_ITEM* aBoard, CHANGE_HANDLER& aHandler, const PARAMETERS& aParams ) :
439 ITEM_MODIFICATION_ROUTINE( aBoard, aHandler ),
440 m_params( aParams )
441 {
442 }
443
444 wxString GetCommitDescription() const override;
445
446 std::optional<wxString> GetStatusMessage() const;
447
448 void ProcessItem( BOARD_ITEM& aItem );
449
450private:
452};
453
454#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:78
Pairwise add dogbone corners to an internal corner.
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.
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
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.
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)
Pairwise extend to corner or meeting tool.
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.
Pairwise line tool that adds a fillet to the lines.
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)
const PARAMETERS m_params
OUTSET_ROUTINE(BOARD_ITEM *aBoard, CHANGE_HANDLER &aHandler, const PARAMETERS &aParams)
wxString GetCommitDescription() const override
std::optional< wxString > GetStatusMessage() const
A tool that acts on a pair of lines.
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.
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.
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
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.
FILL_T
Definition: eda_shape.h:56
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:60
STL namespace.
Parameters that define a simple chamfer operation.