KiCad PCB EDA Suite
router_preview_item.cpp
Go to the documentation of this file.
1 /*
2  * KiRouter - a push-and-(sometimes-)shove PCB router
3  *
4  * Copyright (C) 2013-2014 CERN
5  * Copyright (C) 2016 KiCad Developers, see AUTHORS.txt for contributors.
6  * Author: Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
7  *
8  * This program is free software: you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the
10  * Free Software Foundation, either version 3 of the License, or (at your
11  * option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include <deque>
23 #include <gal/color4d.h>
24 #include <view/view.h>
25 
26 #include <geometry/shape_rect.h>
27 #include <geometry/shape_convex.h>
28 
29 #include "class_track.h"
30 #include <pcb_painter.h>
31 
32 #include "router_preview_item.h"
33 
34 #include "pns_line.h"
35 #include "pns_segment.h"
36 #include "pns_via.h"
37 
38 using namespace KIGFX;
39 
42 {
43  m_view = aView;
44 
45  m_shape = NULL;
46  m_clearance = -1;
48 
49  m_showTrackClearance = false;
50  m_showViaClearance = false;
51 
52  // initialize variables, overwritten by Update( aItem ), if aItem != NULL
53  m_router = NULL;
54  m_type = PR_SHAPE;
55  m_style = 0;
56  m_width = 0;
57  m_depth = 0;
58 
59  if( aItem )
60  Update( aItem );
61 }
62 
63 
65 {
66  delete m_shape;
67 }
68 
69 
71 {
72  m_originLayer = aItem->Layers().Start();
73 
74  if( aItem->OfKind( PNS::ITEM::LINE_T ) )
75  {
76  const PNS::LINE* l = static_cast<const PNS::LINE*>( aItem );
77 
78  if( !l->SegmentCount() )
79  return;
80  }
81 
82  assert( m_originLayer >= 0 );
83 
86  m_color.a = 0.8;
87  m_depth = BaseOverlayDepth - aItem->Layers().Start();
88  m_shape = aItem->Shape()->Clone();
89 
90  switch( aItem->Kind() )
91  {
92  case PNS::ITEM::LINE_T:
93  m_type = PR_SHAPE;
94  m_width = ( (PNS::LINE*) aItem )->Width();
95  break;
96 
98  {
99  PNS::SEGMENT* seg = (PNS::SEGMENT*) aItem;
100  m_type = PR_SHAPE;
101  m_width = seg->Width();
102  break;
103  }
104 
105  case PNS::ITEM::VIA_T:
107  m_type = PR_SHAPE;
108  m_width = 0;
109  m_color = COLOR4D( 0.7, 0.7, 0.7, 0.8 );
111  break;
112 
113  case PNS::ITEM::SOLID_T:
114  m_type = PR_SHAPE;
115  m_width = 0;
116  break;
117 
118  default:
119  break;
120  }
121 
122  if( aItem->Marker() & PNS::MK_VIOLATION )
123  m_color = COLOR4D( 0, 1, 0, 1 );
124 }
125 
126 
128 {
129  BOX2I bbox;
130 
131  switch( m_type )
132  {
133  case PR_SHAPE:
134  bbox = m_shape->BBox();
135  bbox.Inflate( m_width / 2 );
136  return bbox;
137 
138  case PR_POINT:
139  bbox = BOX2I ( m_pos - VECTOR2I( 100000, 100000 ), VECTOR2I( 200000, 200000 ) );
140  return bbox;
141 
142  default:
143  break;
144  }
145 
146  return bbox;
147 }
148 
149 
151 {
152  for( int s = 0; s < aL.SegmentCount(); s++ )
153  gal->DrawLine( aL.CSegment( s ).A, aL.CSegment( s ).B );
154 
155  if( aL.IsClosed() )
156  gal->DrawLine( aL.CSegment( -1 ).B, aL.CSegment( 0 ).A );
157 }
158 
159 
160 void ROUTER_PREVIEW_ITEM::ViewDraw( int aLayer, KIGFX::VIEW* aView ) const
161 {
162  auto gal = aView->GetGAL();
163  //col.Brighten(0.7);
164  gal->SetLayerDepth( m_depth );
165 
166  if( m_type == PR_SHAPE )
167  {
168  if( !m_shape )
169  return;
170 
171  gal->SetLineWidth( m_width );
172  gal->SetStrokeColor( m_color );
173  gal->SetFillColor( m_color );
174  gal->SetIsStroke( m_width ? true : false );
175  gal->SetIsFill( true );
176 
177  switch( m_shape->Type() )
178  {
179  case SH_LINE_CHAIN:
180  {
181  const SHAPE_LINE_CHAIN* l = (const SHAPE_LINE_CHAIN*) m_shape;
182  drawLineChain( *l, gal );
183 
184  if( m_showTrackClearance && m_clearance > 0 )
185  {
186  gal->SetLayerDepth( ClearanceOverlayDepth );
187  gal->SetStrokeColor( COLOR4D( DARKDARKGRAY ) );
188  gal->SetFillColor( COLOR4D( DARKDARKGRAY ) );
189  gal->SetLineWidth( m_width + 2 * m_clearance );
190  drawLineChain( *l, gal );
191  }
192  break;
193  }
194 
195  case SH_SEGMENT:
196  {
197  const SHAPE_SEGMENT* s = (const SHAPE_SEGMENT*) m_shape;
198  gal->DrawSegment( s->GetSeg().A, s->GetSeg().B, s->GetWidth() );
199 
200  if( m_showTrackClearance && m_clearance > 0 )
201  {
202  gal->SetLayerDepth( ClearanceOverlayDepth );
203  gal->SetStrokeColor( COLOR4D( DARKDARKGRAY ) );
204  gal->SetFillColor( COLOR4D( DARKDARKGRAY ) );
205  gal->DrawSegment( s->GetSeg().A, s->GetSeg().B, s->GetWidth() + 2 * m_clearance );
206  }
207 
208  break;
209  }
210 
211  case SH_CIRCLE:
212  {
213  const SHAPE_CIRCLE* c = (const SHAPE_CIRCLE*) m_shape;
214  gal->DrawCircle( c->GetCenter(), c->GetRadius() );
215 
216  if( m_showViaClearance && m_clearance > 0 )
217  {
218  gal->SetLayerDepth( ClearanceOverlayDepth );
219  gal->SetFillColor( COLOR4D( DARKDARKGRAY ) );
220  gal->SetIsStroke( false );
221  gal->DrawCircle( c->GetCenter(), c->GetRadius() + m_clearance );
222  }
223 
224  break;
225  }
226 
227  case SH_RECT:
228  {
229  const SHAPE_RECT* r = (const SHAPE_RECT*) m_shape;
230  gal->DrawRectangle( r->GetPosition(), r->GetPosition() + r->GetSize() );
231 
232  if( m_clearance > 0 )
233  {
234  gal->SetLayerDepth( ClearanceOverlayDepth );
235  VECTOR2I p0( r->GetPosition() ), s( r->GetSize() );
236  gal->SetStrokeColor( COLOR4D( DARKDARKGRAY ) );
237  gal->SetIsStroke( true );
238  gal->SetLineWidth( 2 * m_clearance );
239  gal->DrawLine( p0, VECTOR2I( p0.x + s.x, p0.y ) );
240  gal->DrawLine( p0, VECTOR2I( p0.x, p0.y + s.y ) );
241  gal->DrawLine( p0 + s , VECTOR2I( p0.x + s.x, p0.y ) );
242  gal->DrawLine( p0 + s, VECTOR2I( p0.x, p0.y + s.y ) );
243  }
244 
245  break;
246  }
247 
248  case SH_CONVEX:
249  {
250  const SHAPE_CONVEX* c = (const SHAPE_CONVEX*) m_shape;
251  std::deque<VECTOR2D> polygon = std::deque<VECTOR2D>();
252  for( int i = 0; i < c->PointCount(); i++ )
253  {
254  polygon.push_back( c->CDPoint( i ) );
255  }
256  gal->DrawPolygon( polygon );
257 
258  if( m_clearance > 0 )
259  {
260  gal->SetLayerDepth( ClearanceOverlayDepth );
261  gal->SetStrokeColor( COLOR4D( DARKDARKGRAY ) );
262  gal->SetIsStroke( true );
263  gal->SetLineWidth( 2 * m_clearance );
264  // need the implicit last segment to be explicit for DrawPolyline
265  polygon.push_back( c->CDPoint( 0 ) );
266  gal->DrawPolyline( polygon );
267  }
268  break;
269  }
270 
271  case SH_POLY_SET:
272  case SH_COMPOUND:
273  case SH_ARC:
274  break; // Not yet in use
275  }
276  }
277 }
278 
279 
280 void ROUTER_PREVIEW_ITEM::Line( const SHAPE_LINE_CHAIN& aLine, int aWidth, int aStyle )
281 {
282  m_originLayer = m_layer = 0;
283  m_width = aWidth;
284  m_color = assignColor( aStyle );
285  m_type = PR_SHAPE;
286  m_depth = -1024; // TODO gal->GetMinDepth()
287  m_shape = aLine.Clone();
288 }
289 
290 
291 void ROUTER_PREVIEW_ITEM::Point( const VECTOR2I& aPos, int aStyle )
292 {
293 }
294 
295 
296 void ROUTER_PREVIEW_ITEM::Box( const BOX2I& aBox, int aStyle )
297 {
298 }
299 
300 
302 {
303  auto settings = static_cast<PCB_RENDER_SETTINGS*>( m_view->GetPainter()->GetSettings() );
304 
305  return settings->GetLayerColor( aLayer );
306 }
307 
308 
309 const COLOR4D ROUTER_PREVIEW_ITEM::assignColor( int aStyle ) const
310 {
311  COLOR4D color;
312 
313  switch( aStyle )
314  {
315  case 0:
316  color = COLOR4D( 0, 1, 0, 1 ); break;
317 
318  case 1:
319  color = COLOR4D( 1, 0, 0, 1 ); break;
320 
321  case 2:
322  color = COLOR4D( 1, 1, 0, 1 ); break;
323 
324  case 3:
325  color = COLOR4D( 0, 0, 1, 1 ); break;
326 
327  case 4:
328  color = COLOR4D( 1, 1, 1, 1 ); break;
329 
330  case 5:
331  color = COLOR4D( 1, 1, 0, 1 ); break;
332 
333  case 6:
334  color = COLOR4D( 0, 1, 1, 1 ); break;
335 
336  case 32:
337  color = COLOR4D( 0, 0, 1, 1 ); break;
338 
339  default:
340  color = COLOR4D( 0.4, 0.4, 0.4, 1 ); break;
341  }
342 
343  return color;
344 }
345 
346 const int ROUTER_PREVIEW_ITEM::ClearanceOverlayDepth = -VIEW::VIEW_MAX_LAYERS - 10;
347 const int ROUTER_PREVIEW_ITEM::BaseOverlayDepth = -VIEW::VIEW_MAX_LAYERS - 20;
348 const int ROUTER_PREVIEW_ITEM::ViaOverlayDepth = -VIEW::VIEW_MAX_LAYERS - 50;
set of polygons (with holes, etc.)
Definition: shape.h:49
Class ITEM.
Definition: pns_item.h:53
static const int ClearanceOverlayDepth
BOX2< VECTOR2I > BOX2I
Definition: box2.h:468
const KIGFX::COLOR4D assignColor(int aStyle) const
const VECTOR2I GetCenter() const
Definition: shape_circle.h:84
Class CAIRO_GAL is the cairo implementation of the graphics abstraction layer.
Definition: class_module.h:58
const LAYER_RANGE & Layers() const
Function Layers()
Definition: pns_item.h:209
static const int BaseOverlayDepth
the 3d code uses this value
Definition: typeinfo.h:80
int color
Definition: DXF_plotter.cpp:62
int GetRadius() const
Definition: shape_circle.h:79
virtual const SHAPE * Shape() const
Function Shape()
Definition: pns_item.h:298
Class SHAPE_CONVEX.
Definition: shape_convex.h:42
const KIGFX::COLOR4D getLayerColor(int aLayer) const
void Line(const SHAPE_LINE_CHAIN &aLine, int aWidth=0, int aStyle=0)
virtual void DrawLine(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint)
Draw a line.
VECTOR2< int > VECTOR2I
Definition: vector2d.h:589
virtual int Marker() const
Definition: pns_item.h:313
const COLOR4D & GetLayerColor(int aLayer) const
Function GetLayerColor Returns the color used to draw a layer.
Definition: painter.h:206
virtual void SetLayerDepth(double aLayerDepth)
Set the depth of the layer (position on the z-axis)
virtual const BOX2I BBox(int aClearance=0) const =0
Function BBox()
Functions relatives to tracks, vias and segments used to fill zones.
bool OfKind(int aKindMask) const
Function OfKind()
Definition: pns_item.h:132
virtual SHAPE * Clone() const
Function Clone()
Definition: shape.h:94
Class PCB_RENDER_SETTINGS Stores PCB specific render settings.
Definition: pcb_painter.h:62
const SEG CSegment(int aIndex) const
Function CSegment()
double a
Alpha component.
Definition: color4d.h:294
GAL * GetGAL() const
Function GetGAL() Returns the GAL this view is using to draw graphical primitives.
Definition: view.h:177
int SegmentCount() const
Returns the number of segments in the line
Definition: pns_line.h:129
int PointCount() const
Function PointCount()
Definition: shape_convex.h:85
void Update(const PNS::ITEM *aItem)
const VECTOR2I & GetPosition() const
Function GetPosition()
Definition: shape_rect.h:100
int GetWidth() const
Definition: shape_segment.h:80
compound shape, consisting of multiple simple shapes
Definition: shape.h:50
general purpose overlay
void drawLineChain(const SHAPE_LINE_CHAIN &aL, KIGFX::GAL *aGal) const
int Width() const
Definition: pns_segment.h:88
PAINTER * GetPainter() const
Function GetPainter() Returns the painter object used by the view for drawing VIEW_ITEMS.
Definition: view.h:196
int Start() const
Definition: pns_layerset.h:83
virtual void ViewDraw(int aLayer, KIGFX::VIEW *aView) const override
Function ViewDraw() Draws the parts of the object belonging to layer aLayer.
line chain (polyline)
Definition: shape.h:46
void Point(const VECTOR2I &aPos, int aStyle=0)
PnsKind Kind() const
Function Kind()
Definition: pns_item.h:122
virtual RENDER_SETTINGS * GetSettings()=0
Function GetSettings Returns pointer to current settings that are going to be used when drawing items...
BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Function Inflate inflates the rectangle horizontally by dx and vertically by dy.
Definition: box2.h:266
convex polygon
Definition: shape.h:48
const SEG & GetSeg() const
Definition: shape_segment.h:70
SHAPE_TYPE Type() const
Function Type()
Definition: shape.h:83
Class SHAPE_LINE_CHAIN.
size_t i
Definition: json11.cpp:597
VECTOR2I A
Definition: seg.h:46
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:162
line segment
Definition: shape.h:45
const BOX2I ViewBBox() const override
Function ViewBBox() returns the bounding box of the item covering all its layers. ...
bool IsClosed() const
Function IsClosed()
SHAPE * Clone() const override
Function Clone()
Definition: shape.h:43
Class VIEW.
Definition: view.h:58
const VECTOR2D CDPoint(int aIndex) const
Function CDPoint()
Definition: shape_convex.h:126
static const int ViaOverlayDepth
circle
Definition: shape.h:47
const VECTOR2I GetSize() const
Function GetSize()
Definition: shape_rect.h:110
void Box(const BOX2I &aBox, int aStyle=0)
int SegmentCount() const
Function SegmentCount()
ROUTER_PREVIEW_ITEM(const PNS::ITEM *aItem=NULL, KIGFX::VIEW *aView=NULL)
axis-aligned rectangle
Definition: shape.h:44
Class GAL is the abstract interface for drawing on a 2D-surface.
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39
VECTOR2I B
Definition: seg.h:47