KiCad PCB EDA Suite
arc_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 (C) 2017 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 
25 
28 
30 #include <view/view.h>
31 #include <pcb_painter.h>
32 
33 #include <common.h>
34 #include <base_units.h>
35 
36 using namespace KIGFX::PREVIEW;
37 
39  EDA_ITEM( NOT_USED ),
40  m_constructMan( aManager ),
41  m_units( aUnits )
42 {
43 }
44 
45 
47 {
48  BOX2I tmp;
49 
50  // no bounding box when no graphic shown
51  if( m_constructMan.IsReset() )
52  return tmp;
53 
54  // just enclose the whle circular area
55  auto origin = m_constructMan.GetOrigin();
56  auto radius = m_constructMan.GetRadius();
57  VECTOR2D rVec( radius, radius );
58 
59  tmp.SetOrigin( origin + rVec );
60  tmp.SetEnd( origin - rVec );
61  tmp.Normalize();
62  return tmp;
63 }
64 
65 
72 double getNormDeciDegFromRad( double aRadians )
73 {
74  double degs = RAD2DECIDEG( aRadians );
75 
76  // normalise to +/- 360
77  while( degs < -3600.0 )
78  degs += 3600.0;
79 
80  while( degs > 3600.0 )
81  degs -= 3600.0;
82 
83  return degs;
84 }
85 
86 
87 void ARC_ASSISTANT::ViewDraw( int aLayer, KIGFX::VIEW* aView ) const
88 {
89  auto& gal = *aView->GetGAL();
90 
91  // not in a position to draw anything
92  if( m_constructMan.IsReset() )
93  return;
94 
95  gal.ResetTextAttributes();
96 
97  // constant text size on screen
98  SetConstantGlyphHeight( gal, 12.0 );
99 
100  // angle reference arc size
101  const double innerRad = 12.0 / gal.GetWorldScale();
102 
103  const auto origin = m_constructMan.GetOrigin();
104 
105  KIGFX::PREVIEW::DRAW_CONTEXT preview_ctx( *aView );
106 
107  // draw first radius line
108  bool dimFirstLine = m_constructMan.GetStep() > ARC_GEOM_MANAGER::SET_START;
109 
110  preview_ctx.DrawLineWithAngleHighlight(
111  origin, m_constructMan.GetStartRadiusEnd(), dimFirstLine );
112 
113  std::vector<wxString> cursorStrings;
114 
116  {
117  // haven't started the angle selection phase yet
118 
119  double initAngle = m_constructMan.GetStartAngle();
120 
121  const auto angleRefLineEnd = m_constructMan.GetOrigin() + VECTOR2D( innerRad * 1.5, 0.0 );
122 
123  // draw the short reference baseline
124  preview_ctx.DrawLine( origin, angleRefLineEnd, false );
125 
126  // draw the angle reference arc
127  preview_ctx.DrawArcWithAngleHighlight( origin, innerRad, initAngle, 0.0 );
128 
129  // draw the radius guide circle
130  preview_ctx.DrawCircle( origin, m_constructMan.GetRadius(), true );
131 
132  double degs = getNormDeciDegFromRad( initAngle );
133 
134  cursorStrings.push_back( DimensionLabel( "r", m_constructMan.GetRadius(), m_units ) );
135  cursorStrings.push_back( DimensionLabel( wxString::FromUTF8( "θ" ), degs, DEGREES ) );
136  }
137  else
138  {
139  preview_ctx.DrawLineWithAngleHighlight( origin, m_constructMan.GetEndRadiusEnd(), false );
140 
141  auto start = m_constructMan.GetStartAngle();
142  auto subtended = m_constructMan.GetSubtended();
143 
144  preview_ctx.DrawArcWithAngleHighlight( origin, innerRad, start, start + subtended );
145 
146  double subtendedDeg = getNormDeciDegFromRad( subtended );
147  double endAngleDeg = getNormDeciDegFromRad( start + subtended );
148 
149  // draw dimmed extender line to cursor
150  preview_ctx.DrawLineWithAngleHighlight( origin, m_constructMan.GetLastPoint(), true );
151 
152  cursorStrings.push_back( DimensionLabel( wxString::FromUTF8( "Δθ" ), subtendedDeg, DEGREES ) );
153  cursorStrings.push_back( DimensionLabel( wxString::FromUTF8( "θ" ), endAngleDeg, DEGREES ) );
154  }
155 
156  // place the text next to cursor, on opposite side from radius
158  origin - m_constructMan.GetLastPoint(),
159  cursorStrings );
160 }
void ViewDraw(int aLayer, KIGFX::VIEW *aView) const override final
Draw the assistance (with reference to the contstruction manager.
VECTOR2I GetEndRadiusEnd() const
Get the coordinates of the arc end point
Implementation of conversion functions that require both schematic and board internal units.
the 3d code uses this value
Definition: typeinfo.h:80
double RAD2DECIDEG(double rad)
Definition: trigo.h:213
GAL * GetGAL() const
Function GetGAL() Returns the GAL this view is using to draw graphical primitives.
Definition: view.h:180
VECTOR2I GetStartRadiusEnd() const
Get the coordinates of the arc start
const ARC_GEOM_MANAGER & m_constructMan
Definition: arc_assistant.h:68
double GetStartAngle() const
Get the angle of the vector leading to the start point (valid if step >= SET_START)
double GetSubtended() const
Get the angle of the vector leading to the end point (valid if step >= SET_ANGLE)
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...
void DrawLine(const VECTOR2I &aStart, const VECTOR2I &aEnd, bool aDeEmphasised)
Draw a simple line on the current layer.
void ResetTextAttributes()
Reset text attributes to default styling.
VECTOR2< double > VECTOR2D
Definition: vector2d.h:586
Waiting to lock in origin point
VECTOR2I GetOrigin() const
Get the centre point of the arc (valid when state > SET_ORIGIN)
BOX2< Vec > & Normalize()
Function Normalize ensures that the height ant width are positive.
Definition: box2.h:127
double GetRadius() const
Get the radius of the arc (valid if step >= SET_START)
void DrawTextNextToCursor(KIGFX::VIEW *aView, const VECTOR2D &aCursorPos, const VECTOR2D &aTextQuadrant, const std::vector< wxString > &aStrings)
Draw strings next to the cursor.
void DrawCircle(const VECTOR2I &aOrigin, double aRad, bool aDeEmphasised)
Draw a preview circle on the current layer.
A KIGFX::PREVIEW::DRAW_CONTEXT is a wrapper around a GAL and some other settings that makes it easy t...
Definition: draw_context.h:43
void SetEnd(coord_type x, coord_type y)
Definition: box2.h:223
VECTOR2I GetLastPoint() const
Gets the last point added (locked in or not).
ARC_ASSISTANT(const ARC_GEOM_MANAGER &aManager, EDA_UNITS_T aUnits)
const BOX2I ViewBBox() const override
Function ViewBBox() returns the bounding box of the item covering all its layers.
double getNormDeciDegFromRad(double aRadians)
Get deci-degrees from radians, normalised to +/- 360.
ARC_STEPS GetStep() const
Get the current step the mananger is on (useful when drawing something depends on the current state)
void SetConstantGlyphHeight(KIGFX::GAL &aGal, double aHeight)
Set the GAL glyph height to a constant scaled value, so that it always looks the same on screen.
void SetOrigin(const Vec &pos)
Definition: box2.h:208
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:157
The common library.
wxString DimensionLabel(const wxString &prefix, double aVal, EDA_UNITS_T aUnits)
Get a formatted string showing a dimension to a sane precision with an optional prefix and unit suffi...
Class VIEW.
Definition: view.h:61
void DrawArcWithAngleHighlight(const VECTOR2I &aOrigin, double aRad, double aStartAngle, double aEndAngle)
Draw an arc on the current layer, with a special highlight when the line angle is a multiple of 45 de...
EDA_UNITS_T
Definition: common.h:157