KiCad PCB EDA Suite
Loading...
Searching...
No Matches
via_cross_section_preview.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 modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * 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
22#include <algorithm>
23#include <vector>
24#include <kiplatform/ui.h>
25#include <math/util.h>
26#include <wx/dcclient.h>
27#include <wx/intl.h>
28#include <wx/region.h>
29
30
32
33
34// Paints each material as one body: first every piece in the edge colour one pixel larger,
35// then every piece in the fill colour, so touching pieces merge and only the outline of the
36// whole body remains. A mixed value is filled hatched so it cannot be mistaken for "off".
38{
39public:
40 BODY_PAINTER( wxDC& aDC, double aScaleX, double aScaleY, double aOriginX, double aOriginY ) :
41 m_dc( aDC ),
42 m_scaleX( aScaleX ),
43 m_scaleY( aScaleY ),
44 m_originX( aOriginX ),
45 m_originY( aOriginY )
46 {
47 }
48
49 wxPoint ToDevice( const GEOMETRY::POINT& aPoint ) const
50 {
51 return wxPoint( KiROUND( m_originX + aPoint.x * m_scaleX ), KiROUND( m_originY + aPoint.y * m_scaleY ) );
52 }
53
54 wxRect DeviceRect( const GEOMETRY::PIECE& aShape ) const
55 {
56 wxPoint tl = ToDevice( aShape.m_Points[0] );
57 wxPoint br = ToDevice( aShape.m_Points[1] );
58 return wxRect( tl.x, tl.y, br.x - tl.x, br.y - tl.y );
59 }
60
61 std::vector<wxPoint> DevicePoints( const GEOMETRY::PIECE& aShape ) const
62 {
63 std::vector<wxPoint> pts;
64
65 for( const GEOMETRY::POINT& pt : aShape.m_Points )
66 pts.push_back( ToDevice( pt ) );
67
68 return pts;
69 }
70
72 wxRegion Region( const GEOMETRY::PIECE& aShape ) const
73 {
74 if( aShape.m_Rect )
75 return wxRegion( DeviceRect( aShape ) );
76
77 std::vector<wxPoint> pts = DevicePoints( aShape );
78 return wxRegion( pts.size(), pts.data() );
79 }
80
81 void Material( const wxColour& aFill, const wxColour& aEdge, const GEOMETRY::BODY& aBody )
82 {
83 wxBrush solidBrush( aFill );
84 wxBrush hatchBrush( aFill, wxBRUSHSTYLE_CROSSDIAG_HATCH );
85
86 m_dc.SetPen( *wxTRANSPARENT_PEN );
87 m_dc.SetBrush( wxBrush( aEdge ) );
88
89 for( const GEOMETRY::PIECE& shape : aBody.m_Pieces )
90 {
91 if( shape.m_Rect )
92 {
93 wxRect r = DeviceRect( shape );
94 m_dc.DrawRectangle( r.x - 1, r.y - 1, r.width + 2, r.height + 2 );
95 }
96 else
97 {
98 std::vector<wxPoint> pts = DevicePoints( shape );
99 m_dc.SetPen( wxPen( aEdge, 3 ) );
100 m_dc.DrawPolygon( (int) pts.size(), pts.data() );
101 m_dc.SetPen( *wxTRANSPARENT_PEN );
102 }
103 }
104
105 for( const GEOMETRY::PIECE& shape : aBody.m_Pieces )
106 {
107 m_dc.SetBrush( shape.m_Mixed ? hatchBrush : solidBrush );
108
109 if( shape.m_Rect )
110 {
111 m_dc.DrawRectangle( DeviceRect( shape ) );
112 }
113 else
114 {
115 std::vector<wxPoint> pts = DevicePoints( shape );
116 m_dc.DrawPolygon( (int) pts.size(), pts.data() );
117 }
118 }
119 }
120
121private:
122 wxDC& m_dc;
123 double m_scaleX;
124 double m_scaleY;
125 double m_originX;
126 double m_originY;
127};
128
129
130VIA_CROSS_SECTION_PREVIEW::VIA_CROSS_SECTION_PREVIEW( wxWindow* aParent, wxWindowID aId, const wxPoint& aPos,
131 const wxSize& aSize, long aStyle ) :
132 wxPanel()
133{
134 Create( aParent, aId, aPos, aSize, aStyle );
135 SetMinSize( FromDIP( wxSize( 150, 300 ) ) );
136 SetToolTip( _( "Schematic cross section. Layer positions and cut depths are to scale with the "
137 "board thickness, widths are relative to the hole. Copper and mask thickness "
138 "are exaggerated." ) );
139 Bind( wxEVT_PAINT, &VIA_CROSS_SECTION_PREVIEW::onPaint, this );
140}
141
142
144{
145 m_state = aState;
146 Refresh();
147}
148
149
150void VIA_CROSS_SECTION_PREVIEW::onPaint( wxPaintEvent& aEvent )
151{
152 wxPaintDC dc( this );
153
154 const bool dark = KIPLATFORM::UI::IsDarkTheme();
155
156 const wxColour copper( 0xf2, 0x91, 0x00 );
157 const wxColour copperEdge = dark ? wxColour( 0xbf, 0x26, 0x41 ) : wxColour( 0x8c, 0x5a, 0x00 );
158 const wxColour core = dark ? wxColour( 0x5f, 0x5a, 0x47 ) : wxColour( 0xe9, 0xe1, 0xc6 );
159 const wxColour coreEdge = dark ? wxColour( 0x8e, 0x86, 0x6a ) : wxColour( 0x9c, 0x92, 0x6e );
160 const wxColour mask = dark ? wxColour( 0xf2, 0x64, 0x7e ) : wxColour( 0x2e, 0x8b, 0x2e );
161 const wxColour maskEdge = dark ? wxColour( 0xbf, 0x26, 0x41 ) : wxColour( 0x14, 0x52, 0x14 );
162 const wxColour cover( 0x42, 0xb8, 0xeb );
163 const wxColour coverEdge( 0x1a, 0x81, 0xc4 );
164 const wxColour plug = dark ? wxColour( 0xa9, 0xa9, 0xa9 ) : wxColour( 0xc4, 0xc4, 0xc4 );
165 const wxColour plugEdge = dark ? wxColour( 0xd8, 0xd8, 0xd8 ) : wxColour( 0x6e, 0x6e, 0x6e );
166
167 const GEOMETRY geometry( m_state );
168
169 const wxSize size = GetClientSize();
170 double scaleX = size.x / GEOMETRY::GRID_W;
171 double scaleY = size.y / geometry.GridHeight();
172
173 // Pin the aspect ratio at no more than the golden mean
174 scaleY = std::min( scaleY, 1.618 * scaleX );
175
176 const double originX = ( size.x - GEOMETRY::GRID_W * scaleX ) / 2;
177 const double originY = ( size.y - geometry.GridHeight() * scaleY ) / 2;
178
179 BODY_PAINTER painter( dc, scaleX, scaleY, originX, originY );
180
181 // Air is never painted. Backdrills are clipped out of everything under the mask, and the
182 // cuts out of the mask and cover as well, so the dialog shows through them.
183 wxRegion underMask( 0, 0, size.x, size.y );
184 wxRegion overMask( 0, 0, size.x, size.y );
185
186 for( const GEOMETRY::PIECE& bore : geometry.Bores() )
187 underMask.Subtract( painter.Region( bore ) );
188
189 for( const GEOMETRY::PIECE& cut : geometry.Cuts() )
190 {
191 underMask.Subtract( painter.Region( cut ) );
192 overMask.Subtract( painter.Region( cut ) );
193 }
194
195 for( const GEOMETRY::BODY& body : geometry.Bodies() )
196 {
197 dc.SetDeviceClippingRegion( body.m_Clip == GEOMETRY::CLIP::UNDER_MASK ? underMask : overMask );
198
199 switch( body.m_Material )
200 {
201 case GEOMETRY::MATERIAL::SOLDER_MASK: painter.Material( mask, maskEdge, body ); break;
202 case GEOMETRY::MATERIAL::CORE: painter.Material( core, coreEdge, body ); break;
203 case GEOMETRY::MATERIAL::PLUG: painter.Material( plug, plugEdge, body ); break;
204 case GEOMETRY::MATERIAL::COPPER: painter.Material( copper, copperEdge, body ); break;
205 case GEOMETRY::MATERIAL::COVER: painter.Material( cover, coverEdge, body ); break;
206 }
207
208 dc.DestroyClippingRegion();
209 }
210}
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:985
std::vector< wxPoint > DevicePoints(const GEOMETRY::PIECE &aShape) const
wxPoint ToDevice(const GEOMETRY::POINT &aPoint) const
void Material(const wxColour &aFill, const wxColour &aEdge, const GEOMETRY::BODY &aBody)
BODY_PAINTER(wxDC &aDC, double aScaleX, double aScaleY, double aOriginX, double aOriginY)
wxRegion Region(const GEOMETRY::PIECE &aShape) const
The device region a shape covers, for clipping.
wxRect DeviceRect(const GEOMETRY::PIECE &aShape) const
The shapes of a schematic via cross section, worked out from its protection and machining settings wi...
const std::vector< PIECE > & Bores() const
const std::vector< PIECE > & Cuts() const
const std::vector< BODY > & Bodies() const
VIA_CROSS_SECTION_GEOMETRY::STATE STATE
void onPaint(wxPaintEvent &aEvent)
VIA_CROSS_SECTION_PREVIEW(wxWindow *aParent, wxWindowID aId=wxID_ANY, const wxPoint &aPos=wxDefaultPosition, const wxSize &aSize=wxDefaultSize, long aStyle=0)
#define _(s)
bool IsDarkTheme()
Determine if the desktop interface is currently using a dark theme or a light theme.
Definition wxgtk/ui.cpp:57
bool m_Rect
A rectangle holds its top left and bottom right corners, a polygon its outline.
DATABASE_CONNECTION dc(odbcBackend(cs))
VIA_CROSS_SECTION_GEOMETRY GEOMETRY