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, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <core/spinlock.h>
23#include <sch_commit.h>
24#include <sch_edit_frame.h>
25#include <sch_rule_area.h>
26#include <tool/tool_manager.h>
27#include <tools/sch_actions.h>
29
30
38
39
41{
42 // remove the preview from the view
43 m_parentView.SetVisible( &m_previewItem, false );
44 m_parentView.Remove( &m_previewItem );
45}
46
47
48std::unique_ptr<SCH_RULE_AREA> RULE_AREA_CREATE_HELPER::createNewRuleArea()
49{
50 std::unique_ptr<SCH_RULE_AREA> ruleArea = std::make_unique<SCH_RULE_AREA>();
51 ruleArea->SetLineStyle( LINE_STYLE::DASH );
52 ruleArea->SetLineColor( COLOR4D::UNSPECIFIED );
53
54 return ruleArea;
55}
56
57
58void RULE_AREA_CREATE_HELPER::commitRuleArea( std::unique_ptr<SCH_RULE_AREA> aRuleArea )
59{
60 SCH_COMMIT commit( m_toolManager );
61
62 SCH_RULE_AREA* ruleArea = aRuleArea.release();
63
64 commit.Add( ruleArea, m_frame->GetScreen() );
65 commit.Push( _( "Draw Rule Area" ) );
66
67 m_toolManager->RunAction<EDA_ITEM*>( ACTIONS::selectItem, ruleArea );
68
69 m_parentView.ClearPreview();
70}
71
72
74{
76
77 if( m_rule_area )
78 {
80
81 SCH_RENDER_SETTINGS renderSettings;
82 COLOR_SETTINGS* colorSettings = m_frame->GetColorSettings();
83 renderSettings.LoadColors( colorSettings );
84
85 COLOR4D color = renderSettings.GetLayerColor( LAYER_RULE_AREAS );
86 m_previewItem.SetLineColor( color );
87 m_previewItem.SetLeaderColor( color );
88 m_previewItem.SetFillColor( color.WithAlpha( 0.2 ) );
89
90 m_parentView.SetVisible( &m_previewItem, true );
91
93 }
94
95 return m_rule_area != nullptr;
96}
97
98
100{
101 // Handle a cancel-interactive
102 if( m_rule_area && !aMgr.IsPolygonInProgress() )
103 {
104 m_rule_area = nullptr;
105 m_parentView.SetVisible( &m_previewItem, false );
106 return;
107 }
108
109 // send the points to the preview item
110 m_previewItem.SetPoints( aMgr.GetLockedInPoints(), aMgr.GetLeaderLinePoints(),
111 aMgr.GetLoopLinePoints() );
113}
114
115
117{
118 auto& finalPoints = aMgr.GetLockedInPoints();
119
120 if( finalPoints.PointCount() < 3 )
121 {
122 // Just scrap the rule area in progress
123 m_rule_area = nullptr;
124 }
125 else
126 {
127 SHAPE_POLY_SET ruleShape;
128
129 ruleShape.NewOutline();
130 auto& outline = ruleShape.Outline( 0 );
131
132 for( int i = 0; i < finalPoints.PointCount(); ++i )
133 outline.Append( finalPoints.CPoint( i ) );
134
135 // In DEG45 mode, we may have intermediate points in the leader that should be included
136 // as they are shown in the preview. These typically maintain the 45 constraint
138 {
139 const SHAPE_LINE_CHAIN leaderPts = aMgr.GetLeaderLinePoints();
140 for( int i = 1; i < leaderPts.PointCount(); i++ )
141 outline.Append( leaderPts.CPoint( i ) );
142
143 const SHAPE_LINE_CHAIN loopPts = aMgr.GetLoopLinePoints();
144 for( int i = 1; i < loopPts.PointCount() - 1; i++ )
145 outline.Append( loopPts.CPoint( i ) );
146 }
147
148 outline.SetClosed( true );
149 outline.Simplify( true );
150
151 // Remove the start point if it lies on the line between neighbouring points.
152 // Simplify doesn't handle that currently.
153 if( outline.PointCount() >= 3 )
154 {
155 SEG seg( outline.CLastPoint(), outline.CPoint( 1 ) );
156
157 if( seg.LineDistance( outline.CPoint( 0 ) ) <= 1 )
158 outline.Remove( 0 );
159 }
160
161 m_rule_area->SetPolyShape( ruleShape );
162
163 // hand the rule area over to the committer
164 commitRuleArea( std::move( m_rule_area ) );
165 m_rule_area = nullptr;
166 }
167
168 m_parentView.SetVisible( &m_previewItem, false );
169}
170
171
static TOOL_ACTION selectItem
Select an item (specified as the event parameter).
Definition actions.h:223
static TOOL_ACTION selectionClear
Clear the current selection.
Definition actions.h:220
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:74
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:96
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
COLOR4D WithAlpha(double aAlpha) const
Return a color with the same color, but the given alpha.
Definition color4d.h:308
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:63
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:38
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:742
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:463
@ GEOMETRY
Position or shape has changed.
Definition view_item.h:51