KiCad PCB EDA Suite
Loading...
Searching...
No Matches
drawing_tool.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) 2014-2017 CERN
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * @author Maciej Suminski <[email protected]>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
23#ifndef __DRAWING_TOOL_H
24#define __DRAWING_TOOL_H
25
26#include <functional>
27#include <stack>
28#include <optional>
29#include <tool/tool_menu.h>
30#include <tools/pcb_selection.h>
31#include <tools/pcb_tool_base.h>
32#include <tools/pcb_actions.h>
33
34namespace KIGFX
35{
36 class VIEW;
37 class VIEW_CONTROLS;
38}
39
40class BOARD;
42class PCB_SHAPE;
45class STATUS_MIN_MAX_POPUP;
46
47
51
53{
54public:
57
59 bool Init() override;
60
62 void Reset( RESET_REASON aReason ) override;
63
89
94 MODE GetDrawingMode() const;
95
98 std::vector<BOARD_ITEM*> DrawSpecificationStackup( const VECTOR2I& origin, PCB_LAYER_ID aLayer,
99 bool aDrawNow, VECTOR2I* tablesize );
100
103 int PlaceStackup( const TOOL_EVENT& aEvent );
104
107 int PlaceTuningPattern( const TOOL_EVENT& aEvent );
108
116 int DrawLine( const TOOL_EVENT& aEvent );
117
124 int DrawRectangle( const TOOL_EVENT& aEvent );
125
132 int DrawCircle( const TOOL_EVENT& aEvent );
133
134 int DrawEllipse( const TOOL_EVENT& aEvent );
135
136 int DrawEllipseArc( const TOOL_EVENT& aEvent );
137
145 int DrawArc( const TOOL_EVENT& aEvent );
146
152 int DrawBezier( const TOOL_EVENT& aEvent );
153
158 int PlaceReferenceImage( const TOOL_EVENT& aEvent );
159
163 int PlacePoint( const TOOL_EVENT& aEvent );
164
169 int PlaceText( const TOOL_EVENT& aEvent );
170
171 /*
172 * Start interactively drawing a table (rows & columns of TEXTBOXes).
173 */
174 int DrawTable( const TOOL_EVENT& aEvent );
175
181 int DrawBarcode( const TOOL_EVENT& aEvent );
182
190 int DrawDimension( const TOOL_EVENT& aEvent );
191
205 int DrawZone( const TOOL_EVENT& aEvent );
206
207 int DrawVia( const TOOL_EVENT& aEvent );
208
212 int PlaceImportedGraphics( const TOOL_EVENT& aEvent );
213
227 std::vector<BOARD_ITEM*>& aItems,
228 std::vector<BOARD_ITEM*>& aPreview, LSET* aLayers );
229
230
234 int SetAnchor( const TOOL_EVENT& aEvent );
235
236
238 void setTransitions() override;
239
240 void SetStroke( const STROKE_PARAMS& aStroke, PCB_LAYER_ID aLayer )
241 {
242 m_layer = aLayer;
243 m_stroke = aStroke;
244 }
245
246 void UpdateStatusBar() const;
247
248private:
250 {
251 // The drawing was accepted "normally"
252 // E.g. for a poly-line, you might then begin a chained next segment
254 // The drawing was cancelled with no shape accepted.
256 // The drawing was reset - no shape was accepted this time,
257 // but the tool remains active.
259 // A shape was accepted, but the tool should reset for the
260 // next one (e.g. no chaining)
262 };
263
275 bool drawShape( const TOOL_EVENT& aTool, PCB_SHAPE** aGraphic,
276 std::optional<VECTOR2D> aStartingPoint,
277 std::stack<PCB_SHAPE*>* aCommittedGraphics );
278
279 int runSimpleShapeDraw( const TOOL_EVENT& aEvent, SHAPE_T aShapeType, MODE aMode, const wxString& aCommitLabel,
280 std::function<bool( const TOOL_EVENT&, PCB_SHAPE**, std::optional<VECTOR2D> )> aDrawer );
281
290 bool drawArc( const TOOL_EVENT& aTool, PCB_SHAPE** aGraphic,
291 std::optional<VECTOR2D> aStartingPoint );
292
293
307 std::unique_ptr<PCB_SHAPE> drawOneBezier( const TOOL_EVENT& aTool,
308 const OPT_VECTOR2I& aStartingPoint,
309 const OPT_VECTOR2I& aStartingControl1Point,
310 DRAW_ONE_RESULT& aResult );
311
312
322
335 bool getSourceZoneForAction( ZONE_MODE aMode, ZONE** aZone );
336
343
351 VECTOR2I getClampedDifferenceEnd( const VECTOR2I& aOrigin, const VECTOR2I& aEnd )
352 {
353 typedef std::numeric_limits<int> coord_limits;
354 const int guardValue = 1;
355
356 VECTOR2I::extended_type maxDiff = coord_limits::max() - guardValue;
357
358 VECTOR2I::extended_type xDiff = VECTOR2I::extended_type( aEnd.x ) - aOrigin.x;
359 VECTOR2I::extended_type yDiff = VECTOR2I::extended_type( aEnd.y ) - aOrigin.y;
360
361 if( xDiff > maxDiff )
362 xDiff = maxDiff;
363 if( yDiff > maxDiff )
364 yDiff = maxDiff;
365
366 if( xDiff < -maxDiff )
367 xDiff = -maxDiff;
368 if( yDiff < -maxDiff )
369 yDiff = -maxDiff;
370
371 return aOrigin + VECTOR2I( int( xDiff ), int( yDiff ) );
372 }
373
381 VECTOR2I getClampedRadiusEnd( const VECTOR2I& aOrigin, const VECTOR2I& aEnd )
382 {
383 typedef std::numeric_limits<int> coord_limits;
384 const int guardValue = 10;
385
386 VECTOR2I::extended_type xDiff = VECTOR2I::extended_type( aEnd.x ) - aOrigin.x;
387 VECTOR2I::extended_type yDiff = VECTOR2I::extended_type( aEnd.y ) - aOrigin.y;
388
389 double maxRadius = coord_limits::max() / 2 - guardValue;
390 double radius = std::hypot( xDiff, yDiff );
391
392 if( radius > maxRadius )
393 {
394 double scaleFactor = maxRadius / radius;
395
396 xDiff = KiROUND<double, int>( xDiff * scaleFactor );
397 yDiff = KiROUND<double, int>( yDiff * scaleFactor );
398 }
399
400 return aOrigin + VECTOR2I( int( xDiff ), int( yDiff ) );
401 }
402
408 bool m_inDrawingTool; // Re-entrancy guard
409
410 PCB_LAYER_ID m_layer; // The layer we last drew on
411 STROKE_PARAMS m_stroke; // Current stroke for multi-segment drawing
413
417
418 static const unsigned int WIDTH_STEP; // Amount of width change for one -/+ key press
419 static const unsigned int COORDS_PADDING; // Padding from coordinates limits for this tool
420
421
422 friend class ZONE_CREATE_HELPER; // give internal access to helper classes
423};
424
425#endif /* __DRAWING_TOOL_H */
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:986
A base class derived from BOARD_ITEM for items that can be connected and have a net,...
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:372
TEXT_ATTRIBUTES m_textAttrs
MODE GetDrawingMode() const
Return the current drawing mode of the DRAWING_TOOL or MODE::NONE if not currently in any drawing mod...
void SetStroke(const STROKE_PARAMS &aStroke, PCB_LAYER_ID aLayer)
PCB_SELECTION m_preview
int DrawEllipseArc(const TOOL_EVENT &aEvent)
PCB_LAYER_ID m_layer
int DrawTable(const TOOL_EVENT &aEvent)
int SetAnchor(const TOOL_EVENT &aEvent)
Place the footprint anchor (only in footprint editor).
int DrawDimension(const TOOL_EVENT &aEvent)
Start interactively drawing a dimension.
int PlacePoint(const TOOL_EVENT &aEvent)
Place a reference 0D point.
int DrawVia(const TOOL_EVENT &aEvent)
KIGFX::VIEW_CONTROLS * m_controls
friend class ZONE_CREATE_HELPER
KIGFX::VIEW * m_view
STROKE_PARAMS m_stroke
int DrawZone(const TOOL_EVENT &aEvent)
Start interactively drawing a zone.
int DrawBezier(const TOOL_EVENT &aEvent)
Start interactively drawing a bezier curve.
int DrawLine(const TOOL_EVENT &aEvent)
Start interactively drawing a line.
int DrawArc(const TOOL_EVENT &aEvent)
Start interactively drawing an arc.
VECTOR2I getClampedDifferenceEnd(const VECTOR2I &aOrigin, const VECTOR2I &aEnd)
Clamps the end vector to respect numeric limits of difference representation.
BOARD_CONNECTED_ITEM * m_pickerItem
bool drawShape(const TOOL_EVENT &aTool, PCB_SHAPE **aGraphic, std::optional< VECTOR2D > aStartingPoint, std::stack< PCB_SHAPE * > *aCommittedGraphics)
Start drawing a selected shape (i.e.
void setTransitions() override
This method is meant to be overridden in order to specify handlers for events.
int PlaceImportedGraphics(const TOOL_EVENT &aEvent)
Place a drawing imported from a DXF or SVG file.
int PlaceStackup(const TOOL_EVENT &aEvent)
VECTOR2I getClampedRadiusEnd(const VECTOR2I &aOrigin, const VECTOR2I &aEnd)
Clamps the end vector to respect numeric limits of radius representation.
static const unsigned int WIDTH_STEP
int runSimpleShapeDraw(const TOOL_EVENT &aEvent, SHAPE_T aShapeType, MODE aMode, const wxString &aCommitLabel, std::function< bool(const TOOL_EVENT &, PCB_SHAPE **, std::optional< VECTOR2D >)> aDrawer)
static const unsigned int COORDS_PADDING
bool drawArc(const TOOL_EVENT &aTool, PCB_SHAPE **aGraphic, std::optional< VECTOR2D > aStartingPoint)
Start drawing an arc.
int InteractivePlaceWithPreview(const TOOL_EVENT &aEvent, std::vector< BOARD_ITEM * > &aItems, std::vector< BOARD_ITEM * > &aPreview, LSET *aLayers)
Interactively place a set of BOARD_ITEM.
int DrawBarcode(const TOOL_EVENT &aEvent)
Starts interactively drawing a barcode.
int DrawEllipse(const TOOL_EVENT &aEvent)
int DrawCircle(const TOOL_EVENT &aEvent)
Start interactively drawing a circle.
int PlaceTuningPattern(const TOOL_EVENT &aEvent)
std::vector< BOARD_ITEM * > DrawSpecificationStackup(const VECTOR2I &origin, PCB_LAYER_ID aLayer, bool aDrawNow, VECTOR2I *tablesize)
BOARD * m_board
int DrawRectangle(const TOOL_EVENT &aEvent)
Start interactively drawing a rectangle.
bool Init() override
Init() is called once upon a registration of the tool.
void constrainDimension(PCB_DIMENSION_BASE *aDim)
Force the dimension lime to be drawn on multiple of 45 degrees.
int PlaceText(const TOOL_EVENT &aEvent)
Display a dialog that allows one to input text and its settings and then lets the user decide where t...
PCB_BASE_EDIT_FRAME * m_frame
void UpdateStatusBar() const
int PlaceReferenceImage(const TOOL_EVENT &aEvent)
Display a dialog that allows one to select a reference image and then decide where to place the image...
PCB_TUNING_PATTERN * m_tuningPattern
bool getSourceZoneForAction(ZONE_MODE aMode, ZONE **aZone)
Draw a polygon, that is added as a zone or a keepout area.
std::unique_ptr< PCB_SHAPE > drawOneBezier(const TOOL_EVENT &aTool, const OPT_VECTOR2I &aStartingPoint, const OPT_VECTOR2I &aStartingControl1Point, DRAW_ONE_RESULT &aResult)
Draw a bezier curve.
An interface for classes handling user events controlling the view behavior such as zooming,...
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition view.h:63
LSET is a set of PCB_LAYER_IDs.
Definition lset.h:37
Common, abstract interface for edit frames.
Abstract dimension API.
PCB_TOOL_BASE(TOOL_ID aId, const std::string &aName)
Constructor.
Class that handles the drawing of a polygon, including management of last corner deletion and drawing...
Simple container to manage line stroke parameters.
RESET_REASON
Determine the reason of reset for a tool.
Definition tool_base.h:74
Generic, UI-independent tool event.
Definition tool_event.h:167
VECTOR2_TRAITS< int32_t >::extended_type extended_type
Definition vector2d.h:69
Handle a list of polygons defining a copper zone.
Definition zone.h:70
SHAPE_T
Definition eda_shape.h:44
void Reset() override
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
The Cairo implementation of the graphics abstraction layer.
Definition eda_group.h:29
ZONE_MODE
Definition pcb_actions.h:31
std::optional< VECTOR2I > OPT_VECTOR2I
Definition seg.h:35
int radius
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683