KiCad PCB EDA Suite
Loading...
Searching...
No Matches
construction_geom.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
25
26#include <layer_ids.h>
30#include <view/view.h>
31
32using namespace KIGFX;
33
34
36 EDA_ITEM( nullptr, NOT_USED ), // Never added to a BOARD/SCHEMATIC so it needs no type
37 m_color( COLOR4D::WHITE ), m_persistentColor( COLOR4D::WHITE )
38{
39}
40
41
42void CONSTRUCTION_GEOM::AddDrawable( const DRAWABLE& aItem, bool aPersistent )
43{
44 m_drawables.push_back( { aItem, aPersistent } );
45}
46
47
49{
50 m_drawables.clear();
51}
52
53
55{
56 // We could be a bit more circumspect here, but much of the time the
57 // extended lines go across the whole screen anyway
58 BOX2I bbox;
59 bbox.SetMaximum();
60 return bbox;
61}
62
63
64void CONSTRUCTION_GEOM::ViewDraw( int aLayer, VIEW* aView ) const
65{
66 GAL& gal = *aView->GetGAL();
67
68 gal.SetIsFill( false );
69 gal.SetIsStroke( true );
70 gal.SetLineWidth( 1 );
71
72 const BOX2I viewport = BOX2ISafe( aView->GetViewport() );
73
74 // Prevents extremely short snap lines from inhibiting drawing
75 // These can happen due to rounding in intersections, etc.
76 // (usually it is length 1 IU)
77 const int minSnapLineLength = 10;
78 const bool haveSnapLine = m_snapLine && m_snapLine->Length() >= minSnapLineLength;
79
80 // Avoid fighting with the snap line
81 const auto drawLineIfNotAlsoSnapLine = [&]( const SEG& aLine )
82 {
83 if( !haveSnapLine || !aLine.ApproxCollinear( *m_snapLine, 1 ) )
84 {
85 gal.DrawLine( aLine.A, aLine.B );
86 }
87 };
88
89 // Draw all the items
90 for( const DRAWABLE_INFO& drawable : m_drawables )
91 {
92 gal.SetStrokeColor( drawable.IsPersistent ? m_persistentColor : m_color );
93
94 std::visit(
95 [&]( const auto& visited )
96 {
97 using ItemType = std::decay_t<decltype( visited )>;
98
99 if constexpr( std::is_same_v<ItemType, LINE> )
100 {
101 // Extend the segment to the viewport boundary
102 std::optional<SEG> segToBoundary =
103 KIGEOM::ClipLineToBox( visited, viewport );
104
105 if( segToBoundary )
106 {
107 drawLineIfNotAlsoSnapLine( *segToBoundary );
108 }
109 }
110 else if constexpr( std::is_same_v<ItemType, HALF_LINE> )
111 {
112 // Extend the ray to the viewport boundary
113 std::optional<SEG> segToBoundary =
114 KIGEOM::ClipHalfLineToBox( visited, viewport );
115
116 if( segToBoundary )
117 {
118 drawLineIfNotAlsoSnapLine( *segToBoundary );
119 }
120 }
121 else if constexpr( std::is_same_v<ItemType, SEG> )
122 {
123 drawLineIfNotAlsoSnapLine( visited );
124 }
125 else if constexpr( std::is_same_v<ItemType, CIRCLE> )
126 {
127 gal.DrawCircle( visited.Center, visited.Radius );
128 }
129 else if constexpr( std::is_same_v<ItemType, SHAPE_ARC> )
130 {
131 gal.DrawArc( visited.GetCenter(), visited.GetRadius(),
132 visited.GetStartAngle(), visited.GetCentralAngle() );
133 }
134 else if constexpr( std::is_same_v<ItemType, VECTOR2I> )
135 {
136 KIGFX::DrawCross( gal, visited, aView->ToWorld( 16 ) );
137 }
138 },
139 drawable.Item );
140 }
141
142 if( haveSnapLine )
143 {
145
146 const int dashSizeBasis = aView->ToWorld( 12 );
147 const int snapOriginMarkerSize = aView->ToWorld( 16 );
148
149 // Avoid clash with the snap marker if very close
150 const int omitStartMarkerIfWithinLength = aView->ToWorld( 8 );
151
152 // The line itself
153 KIGFX::DrawDashedLine( gal, *m_snapLine, dashSizeBasis );
154
155 // The line origin marker if the line is long enough
156 if( m_snapLine->A.Distance( m_snapLine->B ) > omitStartMarkerIfWithinLength )
157 {
158 KIGFX::DrawCross( gal, m_snapLine->A, snapOriginMarkerSize );
159 gal.DrawCircle( m_snapLine->A, snapOriginMarkerSize / 2 );
160 }
161 }
162}
163
164
165std::vector<int> CONSTRUCTION_GEOM::ViewGetLayers() const
166{
167 std::vector<int> layers{ LAYER_GP_OVERLAY };
168 return layers;
169}
constexpr BOX2I BOX2ISafe(const BOX2D &aInput)
Definition: box2.h:929
constexpr void SetMaximum()
Definition: box2.h:80
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:89
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:104
std::optional< SEG > m_snapLine
const BOX2I ViewBBox() const override
Return the bounding box of the item covering all its layers.
std::vector< DRAWABLE_INFO > m_drawables
void AddDrawable(const DRAWABLE &aItem, bool aIsPersistent)
std::variant< SEG, LINE, HALF_LINE, CIRCLE, SHAPE_ARC, VECTOR2I > DRAWABLE
std::vector< int > ViewGetLayers() const override
Return the all the layers within the VIEW the object is painted on.
void ViewDraw(int aLayer, VIEW *aView) const override
Draw the parts of the object belonging to layer aLayer.
Abstract interface for drawing on a 2D-surface.
virtual void SetIsFill(bool aIsFillEnabled)
Enable/disable fill.
virtual void DrawCircle(const VECTOR2D &aCenterPoint, double aRadius)
Draw a circle using world coordinates.
virtual void SetLineWidth(float aLineWidth)
Set the line width.
virtual void SetStrokeColor(const COLOR4D &aColor)
Set the stroke color.
virtual void SetIsStroke(bool aIsStrokeEnabled)
Enable/disable stroked outlines.
virtual void DrawLine(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint)
Draw a line.
virtual void DrawArc(const VECTOR2D &aCenterPoint, double aRadius, const EDA_ANGLE &aStartAngle, const EDA_ANGLE &aAngle)
Draw an arc.
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition: view.h:67
BOX2D GetViewport() const
Return the current viewport visible area rectangle.
Definition: view.cpp:520
GAL * GetGAL() const
Return the #GAL this view is using to draw graphical primitives.
Definition: view.h:198
VECTOR2D ToWorld(const VECTOR2D &aCoord, bool aAbsolute=true) const
Converts a screen space point/vector to a point/vector in world space coordinates.
Definition: view.cpp:457
Definition: seg.h:42
@ WHITE
Definition: color4d.h:48
ItemType
Utility functions for drawing compound items (i.e.
@ LAYER_GP_OVERLAY
General purpose overlay.
Definition: layer_ids.h:241
std::optional< SEG > ClipHalfLineToBox(const HALF_LINE &aRay, const BOX2I &aBox)
Get the segment of a half-line that is inside a box, if any.
std::optional< SEG > ClipLineToBox(const LINE &aLine, const BOX2I &aBox)
Get the segment of a line that is inside a box, if any.
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:247
void DrawCross(GAL &aGal, const VECTOR2I &aPosition, int aSize)
Draw a cross at a given position.
void DrawDashedLine(GAL &aGal, const SEG &aSeg, double aDashSize)
Draw a dashed line.
Utility functions for working with shapes.
@ NOT_USED
the 3d code uses this value
Definition: typeinfo.h:79