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 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#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/sch_actions.h>
33
34
42
43
45{
46 // remove the preview from the view
47 m_parentView.SetVisible( &m_previewItem, false );
48 m_parentView.Remove( &m_previewItem );
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
71 m_toolManager->RunAction<EDA_ITEM*>( ACTIONS::selectItem, ruleArea );
72
73 m_parentView.ClearPreview();
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 );
90 m_previewItem.SetLineColor( color );
91 m_previewItem.SetLeaderColor( color );
92 m_previewItem.SetFillColor( color.WithAlpha( 0.2 ) );
93
94 m_parentView.SetVisible( &m_previewItem, true );
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;
109 m_parentView.SetVisible( &m_previewItem, false );
110 return;
111 }
112
113 // send the points to the preview item
114 m_previewItem.SetPoints( aMgr.GetLockedInPoints(), aMgr.GetLeaderLinePoints(),
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.CLastPoint(), 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
172 m_parentView.SetVisible( &m_previewItem, false );
173}
174
175
int color
static TOOL_ACTION selectItem
Select an item (specified as the event parameter).
Definition actions.h:226
static TOOL_ACTION selectionClear
Clear the current selection.
Definition actions.h:223
static const COLOR4D UNSPECIFIED
For legacy support; used as a value to indicate color hasn't been set yet.
Definition color4d.h:398
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)
Add a new item to the model.
Definition commit.h:78
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:98
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:104
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:66
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.
virtual void Push(const wxString &aMessage=wxT("A commit"), int aCommitFlags=0) override
Execute the changes.
Schematic editor (Eeschema) main window.
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:717
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:
#define _(s)
@ DEG45
45 Degree only
@ DEG90
90 Degree only
@ LAYER_RULE_AREAS
Definition layer_ids.h:464
@ GEOMETRY
Position or shape has changed.
Definition view_item.h:55