KiCad PCB EDA Suite
Loading...
Searching...
No Matches
rule_area_create_helper.cpp
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) 2017-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#include <core/spinlock.h>
27#include <sch_commit.h>
28#include <sch_edit_frame.h>
29#include <sch_rule_area.h>
30#include <tool/tool_manager.h>
31#include <tools/ee_actions.h>
33
34
36 TOOL_MANAGER* aMgr ) :
37 m_parentView( aView ),
38 m_frame( aFrame ), m_toolManager( aMgr )
39{
41}
42
43
45{
46 // remove the preview from the view
49}
50
51
52std::unique_ptr<SCH_RULE_AREA> RULE_AREA_CREATE_HELPER::createNewRuleArea()
53{
54 std::unique_ptr<SCH_RULE_AREA> ruleArea = std::make_unique<SCH_RULE_AREA>();
55 ruleArea->SetLineStyle( LINE_STYLE::DASH );
56 ruleArea->SetLineColor( COLOR4D::UNSPECIFIED );
57
58 return ruleArea;
59}
60
61
62void RULE_AREA_CREATE_HELPER::commitRuleArea( std::unique_ptr<SCH_RULE_AREA> aRuleArea )
63{
64 SCH_COMMIT commit( m_toolManager );
65
66 SCH_RULE_AREA* ruleArea = aRuleArea.release();
67
68 commit.Add( ruleArea, m_frame->GetScreen() );
69 commit.Push( _( "Draw Rule Area" ) );
70
72
74}
75
76
78{
80
81 if( m_rule_area )
82 {
84
85 SCH_RENDER_SETTINGS renderSettings;
86 COLOR_SETTINGS* colorSettings = m_frame->GetColorSettings();
87 renderSettings.LoadColors( colorSettings );
88
89 COLOR4D color = renderSettings.GetLayerColor( LAYER_RULE_AREAS );
92 m_previewItem.SetFillColor( color.WithAlpha( 0.2 ) );
93
95
97 }
98
99 return m_rule_area != nullptr;
100}
101
102
104{
105 // Handle a cancel-interactive
106 if( m_rule_area && !aMgr.IsPolygonInProgress() )
107 {
108 m_rule_area = nullptr;
110 return;
111 }
112
113 // send the points to the preview item
115 aMgr.GetLoopLinePoints() );
117}
118
119
121{
122 auto& finalPoints = aMgr.GetLockedInPoints();
123
124 if( finalPoints.PointCount() < 3 )
125 {
126 // Just scrap the rule area in progress
127 m_rule_area = nullptr;
128 }
129 else
130 {
131 SHAPE_POLY_SET ruleShape;
132
133 ruleShape.NewOutline();
134 auto& outline = ruleShape.Outline( 0 );
135
136 for( int i = 0; i < finalPoints.PointCount(); ++i )
137 outline.Append( finalPoints.CPoint( i ) );
138
139 // In DEG45 mode, we may have intermediate points in the leader that should be included
140 // as they are shown in the preview. These typically maintain the 45 constraint
142 {
143 const SHAPE_LINE_CHAIN leaderPts = aMgr.GetLeaderLinePoints();
144 for( int i = 1; i < leaderPts.PointCount(); i++ )
145 outline.Append( leaderPts.CPoint( i ) );
146
147 const SHAPE_LINE_CHAIN loopPts = aMgr.GetLoopLinePoints();
148 for( int i = 1; i < loopPts.PointCount() - 1; i++ )
149 outline.Append( loopPts.CPoint( i ) );
150 }
151
152 outline.SetClosed( true );
153 outline.Simplify( true );
154
155 // Remove the start point if it lies on the line between neighbouring points.
156 // Simplify doesn't handle that currently.
157 if( outline.PointCount() >= 3 )
158 {
159 SEG seg( outline.CPoint( -1 ), outline.CPoint( 1 ) );
160
161 if( seg.LineDistance( outline.CPoint( 0 ) ) <= 1 )
162 outline.Remove( 0 );
163 }
164
165 m_rule_area->SetPolyShape( ruleShape );
166
167 // hand the rule area over to the committer
168 commitRuleArea( std::move( m_rule_area ) );
169 m_rule_area = nullptr;
170 }
171
173}
174
175
int color
Definition: DXF_plotter.cpp:58
Color settings are a bit different than most of the settings objects in that there can be more than o...
COMMIT & Add(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Notify observers that aItem has been added.
Definition: commit.h:80
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:88
static TOOL_ACTION clearSelection
Clears the current selection.
Definition: ee_actions.h:56
static TOOL_ACTION addItemToSel
Selects an item (specified as the event parameter).
Definition: ee_actions.h:59
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:104
void SetLeaderColor(KIGFX::COLOR4D leaderColor)
Gets the bounding box of the polygon.
void SetLineColor(KIGFX::COLOR4D lineColor)
Sets the color of the outline leader line.
void SetPoints(const SHAPE_LINE_CHAIN &aLockedInPts, const SHAPE_LINE_CHAIN &aLeaderPts, const SHAPE_LINE_CHAIN &aLoopPts)
Set the polygon points.
void SetFillColor(const COLOR4D &aNewColor)
Set the line width to set before drawing preview.
const COLOR4D & GetLayerColor(int aLayer) const
Return the color used to draw a layer.
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition: view.h:68
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Add a VIEW_ITEM to the view.
Definition: view.cpp:315
virtual void Remove(VIEW_ITEM *aItem)
Remove a VIEW_ITEM from the view.
Definition: view.cpp:354
virtual void Update(const VIEW_ITEM *aItem, int aUpdateFlags) const
For dynamic VIEWs, inform the associated VIEW that the graphical representation of this item has chan...
Definition: view.cpp:1631
void ClearPreview()
Definition: view.cpp:1653
void SetVisible(VIEW_ITEM *aItem, bool aIsVisible=true)
Set the item visibility.
Definition: view.cpp:1558
Class that handles the drawing of a polygon, including management of last corner deletion and drawing...
const SHAPE_LINE_CHAIN & GetLockedInPoints() const
Get the "locked-in" points that describe the polygon itself.
LEADER_MODE GetLeaderMode() const
void SetLeaderMode(LEADER_MODE aMode)
Set the leader mode to use when calculating the leader/returner lines.
const SHAPE_LINE_CHAIN & GetLoopLinePoints() const
Get the points from the current cursor position to the polygon start point.
const SHAPE_LINE_CHAIN & GetLeaderLinePoints() const
Get the points comprising the leader line (the line from the last locked-in point to the current curs...
std::unique_ptr< SCH_RULE_AREA > createNewRuleArea()
Create a new SCH_RULE_AREA.
RULE_AREA_CREATE_HELPER(KIGFX::VIEW &aView, SCH_EDIT_FRAME *aFrame, TOOL_MANAGER *aMgr)
std::unique_ptr< SCH_RULE_AREA > m_rule_area
The TOOL_MANAGER running the tool.
void OnGeometryChange(const POLYGON_GEOM_MANAGER &aMgr) override
Called when the polygon is complete.
void OnComplete(const POLYGON_GEOM_MANAGER &aMgr) override
KIGFX::VIEW & m_parentView
The active schematic edit frame.
bool OnFirstPoint(POLYGON_GEOM_MANAGER &aMgr) override
Called before the first point is added - clients can do initialization here, and can veto the start o...
void commitRuleArea(std::unique_ptr< SCH_RULE_AREA > aRuleArea)
Commit the current rule area in progress to the schematic.
KIGFX::PREVIEW::POLYGON_ITEM m_previewItem
< The preview item to display
SCH_EDIT_FRAME * m_frame
The rule area in progress.
COLOR_SETTINGS * GetColorSettings(bool aForceRefresh=false) const override
Returns a pointer to the active color theme settings.
virtual void Push(const wxString &aMessage=wxT("A commit"), int aCommitFlags=0) override
Revert the commit by restoring the modified items state.
Definition: sch_commit.cpp:405
Schematic editor (Eeschema) main window.
SCH_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
void LoadColors(const COLOR_SETTINGS *aSettings) override
Definition: seg.h:42
int LineDistance(const VECTOR2I &aP, bool aDetermineSide=false) const
Return the closest Euclidean distance between point aP and the line defined by the ends of segment (t...
Definition: seg.cpp:341
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
int PointCount() const
Return the number of points (vertices) in this line chain.
void Append(int aX, int aY, bool aAllowDuplication=false)
Append a new point at the end of the line chain.
const VECTOR2I & CPoint(int aIndex) const
Return a reference to a given point in the line chain.
Represent a set of closed polygons.
SHAPE_LINE_CHAIN & Outline(int aIndex)
Return the reference to aIndex-th outline in the set.
int NewOutline()
Creates a new empty polygon in the set and returns its index.
Master controller class:
Definition: tool_manager.h:62
bool RunAction(const std::string &aActionName, T aParam)
Run the specified action immediately, pausing the current action to run the new one.
Definition: tool_manager.h:150
#define _(s)
@ LAYER_RULE_AREAS
Definition: layer_ids.h:369
@ GEOMETRY
Position or shape has changed.
Definition: view_item.h:54