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, see <https://www.gnu.org/licenses/>.
18 */
19
20#ifndef ITEM_MODIFICATION_ROUTINE_H_
21#define ITEM_MODIFICATION_ROUTINE_H_
22
23#include <functional>
24#include <memory>
25#include <optional>
26#include <vector>
27
28#include <board_item.h>
29#include <pcb_shape.h>
30
32
43{
44public:
45 /*
46 * Handlers for receiving changes from the tool
47 *
48 * These are used to allow the tool's caller to make changes to
49 * affected board items using extra information that the tool
50 * does not have access to (e.g. is this an FP editor, was
51 * the line created from a rectangle and needs to be added, not
52 * modified, etc).
53 *
54 * We can't store them up until the end, because modifications
55 * need the old state to be known, so this allows the caller to
56 * inject the dependencies for how to handle the changes.
57 */
59 {
60 public:
61 virtual ~CHANGE_HANDLER() = default;
62
68 virtual void AddNewItem( std::unique_ptr<BOARD_ITEM> aItem ) = 0;
69
75 virtual void MarkItemModified( BOARD_ITEM& aItem ) = 0;
76
82 virtual void DeleteItem( BOARD_ITEM& aItem ) = 0;
83 };
84
90 {
91 public:
97 using CREATION_HANDLER = std::function<void( std::unique_ptr<BOARD_ITEM> )>;
98
104 using MODIFICATION_HANDLER = std::function<void( BOARD_ITEM& )>;
105
111 using DELETION_HANDLER = std::function<void( BOARD_ITEM& )>;
112
114 MODIFICATION_HANDLER aModificationHandler,
115 DELETION_HANDLER aDeletionHandler ) :
116 m_creationHandler( std::move( aCreationHandler ) ),
117 m_modificationHandler( std::move( aModificationHandler ) ),
118 m_deletionHandler( std::move( aDeletionHandler ) )
119 {
120 }
121
127 void AddNewItem( std::unique_ptr<BOARD_ITEM> aItem ) override
128 {
129 m_creationHandler( std::move( aItem ) );
130 }
131
137 void MarkItemModified( BOARD_ITEM& aItem ) override { m_modificationHandler( aItem ); }
138
144 void DeleteItem( BOARD_ITEM& aItem ) override { m_deletionHandler( aItem ); }
145
149 };
150
152 m_board( aBoard ),
153 m_handler( aHandler ),
154 m_numSuccesses( 0 ),
155 m_numFailures( 0 )
156 {
157 }
158
159 virtual ~ITEM_MODIFICATION_ROUTINE() = default;
160
161 unsigned GetSuccesses() const { return m_numSuccesses; }
162
163 unsigned GetFailures() const { return m_numFailures; }
164
165 virtual wxString GetCommitDescription() const = 0;
166
167protected:
171 BOARD_ITEM* GetBoard() const { return m_board; }
172
177
182
190 bool ModifyLineOrDeleteIfZeroLength( PCB_SHAPE& aItem, const std::optional<SEG>& aSeg );
191
196
197private:
200
203};
204
209{
210public:
212 ITEM_MODIFICATION_ROUTINE( aBoard, aHandler )
213 {
214 }
215
231 virtual void ProcessLinePair( PCB_SHAPE& aLineA, PCB_SHAPE& aLineB ) = 0;
232
238 virtual std::optional<wxString> GetStatusMessage( int aSegmentCount ) const = 0;
239};
240
245{
246public:
247 LINE_FILLET_ROUTINE( BOARD_ITEM* aBoard, CHANGE_HANDLER& aHandler, int filletRadiusIU ) :
248 PAIRWISE_LINE_ROUTINE( aBoard, aHandler ),
249 m_filletRadiusIU( filletRadiusIU )
250 {
251 }
252
253 wxString GetCommitDescription() const override;
254 std::optional<wxString> GetStatusMessage( int aSegmentCount ) const override;
255
256 void ProcessLinePair( PCB_SHAPE& aLineA, PCB_SHAPE& aLineB ) override;
257
258private:
260};
261
266{
267public:
269 CHAMFER_PARAMS aChamferParams ) :
270 PAIRWISE_LINE_ROUTINE( aBoard, aHandler ),
271 m_chamferParams( std::move( aChamferParams ) )
272 {
273 }
274
275 wxString GetCommitDescription() const override;
276 std::optional<wxString> GetStatusMessage( int aSegmentCount ) const override;
277
278 void ProcessLinePair( PCB_SHAPE& aLineA, PCB_SHAPE& aLineB ) override;
279
280private:
282};
283
288{
289public:
291 PAIRWISE_LINE_ROUTINE( aBoard, aHandler )
292 {
293 }
294
295 wxString GetCommitDescription() const override;
296 std::optional<wxString> GetStatusMessage( int aSegmentCount ) const override;
297
298 void ProcessLinePair( PCB_SHAPE& aLineA, PCB_SHAPE& aLineB ) override;
299};
300
305{
306public:
308 {
311 };
312
314 PAIRWISE_LINE_ROUTINE( aBoard, aHandler ),
315 m_params( std::move( aParams ) ),
316 m_haveNarrowMouths( false )
317 {
318 }
319
320 wxString GetCommitDescription() const override;
321 std::optional<wxString> GetStatusMessage( int aSegmentCount ) const override;
322
323 void ProcessLinePair( PCB_SHAPE& aLineA, PCB_SHAPE& aLineB ) override;
324
325private:
328};
329
330
335{
336public:
338 ITEM_MODIFICATION_ROUTINE( aBoard, aHandler )
339 {
340 }
341
345 virtual bool IsCommutative() const = 0;
346
347 void ProcessShape( PCB_SHAPE& aPcbShape );
348
352 void Finalize();
353
359 virtual std::optional<wxString> GetStatusMessage() const = 0;
360
361protected:
363
364 virtual bool ProcessSubsequentPolygon( const SHAPE_POLY_SET& aPolygon ) = 0;
365
366private:
369
370 bool m_firstPolygon = true;
371 int m_width = 0;
374};
375
377{
378public:
380 POLYGON_BOOLEAN_ROUTINE( aBoard, aHandler )
381 {
382 }
383
384 bool IsCommutative() const override { return true; }
385
386 wxString GetCommitDescription() const override;
387 std::optional<wxString> GetStatusMessage() const override;
388
389private:
390 bool ProcessSubsequentPolygon( const SHAPE_POLY_SET& aPolygon ) override;
391};
392
393
395{
396public:
398 POLYGON_BOOLEAN_ROUTINE( aBoard, aHandler )
399 {
400 }
401
402 bool IsCommutative() const override { return false; }
403
404 wxString GetCommitDescription() const override;
405 std::optional<wxString> GetStatusMessage() const override;
406
407private:
408 bool ProcessSubsequentPolygon( const SHAPE_POLY_SET& aPolygon ) override;
409};
410
411
413{
414public:
416 POLYGON_BOOLEAN_ROUTINE( aBoard, aHandler )
417 {
418 }
419
420 bool IsCommutative() const override { return true; }
421
422 wxString GetCommitDescription() const override;
423 std::optional<wxString> GetStatusMessage() const override;
424
425private:
426 bool ProcessSubsequentPolygon( const SHAPE_POLY_SET& aPolygon ) override;
427};
428
429
431{
432public:
444
445 OUTSET_ROUTINE( BOARD_ITEM* aBoard, CHANGE_HANDLER& aHandler, const PARAMETERS& aParams ) :
446 ITEM_MODIFICATION_ROUTINE( aBoard, aHandler ),
447 m_params( aParams )
448 {
449 }
450
451 wxString GetCommitDescription() const override;
452
453 std::optional<wxString> GetStatusMessage() const;
454
455 void ProcessItem( BOARD_ITEM& aItem );
456
457private:
459};
460
461#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:81
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:59
@ NO_FILL
Definition eda_shape.h:60
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
@ UNDEFINED_LAYER
Definition layer_ids.h:57
STL namespace.
Parameters that define a simple chamfer operation.