KiCad PCB EDA Suite
Loading...
Searching...
No Matches
ellipse_assistant.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
21
24
26#include <view/view.h>
27
28#include <base_units.h>
29#include <math/util.h>
30#include <trigo.h>
31
32using namespace KIGFX::PREVIEW;
33
35 EDA_UNITS aUnits ) :
37 m_constructMan( aManager ),
38 m_iuScale( aIuScale ),
39 m_units( aUnits ),
40 m_drawArc( false )
41{
42}
43
44
46{
47 BOX2I tmp;
48
49 if( m_constructMan.IsReset() )
50 return tmp;
51
52 tmp.SetMaximum();
53 return tmp;
54}
55
56
57void ELLIPSE_ASSISTANT::ViewDraw( int aLayer, KIGFX::VIEW* aView ) const
58{
59 KIGFX::GAL& gal = *aView->GetGAL();
60
61 if( m_constructMan.IsReset() )
62 return;
63
65
66 KIGFX::PREVIEW::DRAW_CONTEXT preview_ctx( *aView );
67
69
71 {
72 // Bbox phase: draw bbox rectangle and fitted ellipse outline.
73
74 const VECTOR2I c1 = m_constructMan.GetBboxCorner1();
75 const VECTOR2I c2 = ( step == ELLIPSE_GEOM_MANAGER::SET_BBOX_C1 )
76 ? m_constructMan.GetLastPoint()
77 : m_constructMan.GetBboxCorner2();
78
79 // Draw bbox
80 preview_ctx.DrawRectangle( c1, c2, false );
81
82 // Draw fitted ellipse if we have valid radii
84 {
85 const ELLIPSE<int> ellipse = m_constructMan.GetEllipse();
86
87 if( m_drawArc )
88 {
89 preview_ctx.DrawEllipse( ellipse, false );
90 }
91
92 // Draw major axis
93 {
94 VECTOR2I endPoint = ellipse.GetPointAtAngle( ANGLE_0 );
95 preview_ctx.DrawLine( ellipse.Center, endPoint, true );
96 }
97
98 // Draw minor axis
99 {
100 VECTOR2I endPoint = ellipse.GetPointAtAngle( ANGLE_90 );
101 preview_ctx.DrawLine( ellipse.Center, endPoint, true );
102 }
103 }
104 }
105 else
106 {
107 // Angle phase: draw ellipse, radial lines at angles, and arc segment.
108 const ELLIPSE<int> ellipse = m_constructMan.GetEllipse();
109
110 // Draw full ellipse outline (de-emphasised)
111 if( m_drawArc )
112 {
113 preview_ctx.DrawEllipse( ellipse, true );
114 }
115
116 const VECTOR2I startPt = ellipse.GetArcStartPoint();
117 const VECTOR2I endPt = ellipse.GetArcEndPoint();
118 const VECTOR2I cursorPt = m_constructMan.GetLastPoint();
119
120 // Draw radial lines from center to start/end
121 preview_ctx.DrawLineWithAngleHighlight( ellipse.Center, startPt, false );
122 preview_ctx.DrawLineWithAngleHighlight( ellipse.Center, endPt, false );
123
124 // Draw dimmed extender line to cursor
125 preview_ctx.DrawLineWithAngleHighlight( ellipse.Center, cursorPt, true );
126
127 // Dimension labels
128 wxArrayString cursorStrings;
129
130 cursorStrings.push_back( DimensionLabel( wxString::FromUTF8( "Δθ" ), ellipse.GetSubtendedAngle().AsDegrees(),
132
133 EDA_ANGLE normalizedEnd = ellipse.EndAngle;
134 normalizedEnd.Normalize180();
135 cursorStrings.push_back(
136 DimensionLabel( wxString::FromUTF8( "θ" ), normalizedEnd.AsDegrees(), m_iuScale, EDA_UNITS::DEGREES ) );
137
138 DrawTextNextToCursor( aView, m_constructMan.GetLastPoint(), ellipse.Center - cursorPt, cursorStrings,
139 aLayer == LAYER_SELECT_OVERLAY );
140 }
141}
BOX2< VECTOR2I > BOX2I
Definition box2.h:918
constexpr void SetMaximum()
Definition box2.h:76
double AsDegrees() const
Definition eda_angle.h:116
EDA_ANGLE Normalize180()
Definition eda_angle.h:268
EDA_ITEM(EDA_ITEM *parent, KICAD_T idType, bool isSCH_ITEM=false, bool isBOARD_ITEM=false)
Definition eda_item.cpp:37
Plain ellipse / elliptical-arc data.
Definition ellipse.h:32
VECTOR2< NumericType > GetArcEndPoint() const
Get the point on the ellipse at the end angle of the arc.
Definition ellipse.h:93
VECTOR2< NumericType > GetPointAtAngle(EDA_ANGLE angle) const
Get the point on the ellipse at a given angle.
Definition ellipse.cpp:90
VECTOR2< NumericType > GetArcStartPoint() const
Get the point on the ellipse at the start angle of the arc.
Definition ellipse.h:88
EDA_ANGLE EndAngle
Definition ellipse.h:106
VECTOR2< NumericType > Center
Definition ellipse.h:101
EDA_ANGLE GetSubtendedAngle() const
Get the subtended angle of the ellipse or elliptical arc at the center.
Definition ellipse.cpp:82
Abstract interface for drawing on a 2D-surface.
void ResetTextAttributes()
Reset text attributes to default styling.
A KIGFX::PREVIEW::DRAW_CONTEXT is a wrapper around a GAL and some other settings that makes it easy t...
void DrawRectangle(const VECTOR2I &aC1, const VECTOR2I &aC2, bool aDeEmphasised)
Draw a rectangle on the current layer.
void DrawEllipse(const VECTOR2I &aOrigin, double aA, double aB, EDA_ANGLE aRot, bool aDeEmphasised)
Draw a preview ellipse on the current layer.
void DrawLine(const VECTOR2I &aStart, const VECTOR2I &aEnd, bool aDeEmphasised)
Draw a simple line on the current layer.
void DrawLineWithAngleHighlight(const VECTOR2I &aStart, const VECTOR2I &aEnd, bool aDeEmphasised)
Draw a straight line on the current layer, with a special highlight when the line angle is a multiple...
EDA_UNITS m_units
Draw the arc segment (or just the radius lines).
void ViewDraw(int aLayer, KIGFX::VIEW *aView) const override final
Draw the parts of the object belonging to layer aLayer.
const BOX2I ViewBBox() const override
Return the bounding box of the item covering all its layers.
ELLIPSE_ASSISTANT(const ELLIPSE_GEOM_MANAGER &aManager, const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits)
const ELLIPSE_GEOM_MANAGER & m_constructMan
Manage the construction of an elliptical arc through sequential setting of critical points: two bound...
@ SET_BBOX_C1
Waiting to lock in first bbox corner.
@ SET_BBOX_C2
Waiting to lock in second bbox corner.
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition view.h:63
GAL * GetGAL() const
Return the GAL this view is using to draw graphical primitives.
Definition view.h:207
static constexpr EDA_ANGLE ANGLE_0
Definition eda_angle.h:411
static constexpr EDA_ANGLE ANGLE_90
Definition eda_angle.h:413
EDA_UNITS
Definition eda_units.h:44
@ LAYER_SELECT_OVERLAY
Selected items overlay.
Definition layer_ids.h:276
void DrawTextNextToCursor(KIGFX::VIEW *aView, const VECTOR2D &aCursorPos, const VECTOR2D &aTextQuadrant, const wxArrayString &aStrings, bool aDrawingDropShadows)
Draw strings next to the cursor.
wxString DimensionLabel(const wxString &prefix, double aVal, const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits, bool aIncludeUnits=true)
Get a formatted string showing a dimension to a sane precision with an optional prefix and unit suffi...
@ NOT_USED
the 3d code uses this value
Definition typeinfo.h:72
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683