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 
25 #include <geometry/shape_rect.h>
26 #include <geometry/shape_convex.h>
27 
28 #include "class_track.h"
29 #include <pcb_painter.h>
30 
31 #include "router_preview_item.h"
32 
33 #include "pns_line.h"
34 #include "pns_segment.h"
35 #include "pns_via.h"
36 
37 using namespace KIGFX;
38 
41 {
42  m_view = aView;
43 
44  m_shape = NULL;
45  m_clearance = -1;
47 
48  m_showTrackClearance = false;
49  m_showViaClearance = false;
50 
51  // initialize variables, overwritten by Update( aItem ), if aItem != NULL
52  m_router = NULL;
53  m_type = PR_SHAPE;
54  m_style = 0;
55  m_width = 0;
56  m_depth = 0;
57 
58  if( aItem )
59  Update( aItem );
60 }
61 
62 
64 {
65  delete m_shape;
66 }
67 
68 
70 {
71  m_originLayer = aItem->Layers().Start();
72 
73  if( aItem->OfKind( PNS::ITEM::LINE_T ) )
74  {
75  const PNS::LINE* l = static_cast<const PNS::LINE*>( aItem );
76 
77  if( !l->SegmentCount() )
78  return;
79  }
80 
81  assert( m_originLayer >= 0 );
82 
85  m_color.a = 0.8;
86  m_depth = BaseOverlayDepth - aItem->Layers().Start();
87  m_shape = aItem->Shape()->Clone();
88 
89  switch( aItem->Kind() )
90  {
91  case PNS::ITEM::LINE_T:
92  m_type = PR_SHAPE;
93  m_width = ( (PNS::LINE*) aItem )->Width();
94  break;
95 
97  {
98  PNS::SEGMENT* seg = (PNS::SEGMENT*) aItem;
99  m_type = PR_SHAPE;
100  m_width = seg->Width();
101  break;
102  }
103 
104  case PNS::ITEM::VIA_T:
106  m_type = PR_SHAPE;
107  m_width = 0;
108  m_color = COLOR4D( 0.7, 0.7, 0.7, 0.8 );
110  break;
111 
112  case PNS::ITEM::SOLID_T:
113  m_type = PR_SHAPE;
114  m_width = 0;
115  break;
116 
117  default:
118  break;
119  }
120 
121  if( aItem->Marker() & PNS::MK_VIOLATION )
122  m_color = COLOR4D( 0, 1, 0, 1 );
123 }
124 
125 
127 {
128  BOX2I bbox;
129 
130  switch( m_type )
131  {
132  case PR_SHAPE:
133  bbox = m_shape->BBox();
134  bbox.Inflate( m_width / 2 );
135  return bbox;
136 
137  case PR_POINT:
138  bbox = BOX2I ( m_pos - VECTOR2I( 100000, 100000 ), VECTOR2I( 200000, 200000 ) );
139  return bbox;
140 
141  default:
142  break;
143  }
144 
145  return bbox;
146 }
147 
148 
150 {
151  for( int s = 0; s < aL.SegmentCount(); s++ )
152  gal->DrawLine( aL.CSegment( s ).A, aL.CSegment( s ).B );
153 
154  if( aL.IsClosed() )
155  gal->DrawLine( aL.CSegment( -1 ).B, aL.CSegment( 0 ).A );
156 }
157 
158 
159 void ROUTER_PREVIEW_ITEM::ViewDraw( int aLayer, KIGFX::VIEW* aView ) const
160 {
161  auto gal = aView->GetGAL();
162  //col.Brighten(0.7);
163  gal->SetLayerDepth( m_depth );
164 
165  if( m_type == PR_SHAPE )
166  {
167  if( !m_shape )
168  return;
169 
170  gal->SetLineWidth( m_width );
171  gal->SetStrokeColor( m_color );
172  gal->SetFillColor( m_color );
173  gal->SetIsStroke( m_width ? true : false );
174  gal->SetIsFill( true );
175 
176  switch( m_shape->Type() )
177  {
178  case SH_LINE_CHAIN:
179  {
180  const SHAPE_LINE_CHAIN* l = (const SHAPE_LINE_CHAIN*) m_shape;
181  drawLineChain( *l, gal );
182 
183  if( m_showTrackClearance && m_clearance > 0 )
184  {
185  gal->SetLayerDepth( ClearanceOverlayDepth );
186  gal->SetStrokeColor( COLOR4D( DARKDARKGRAY ) );
187  gal->SetFillColor( COLOR4D( DARKDARKGRAY ) );
188  gal->SetLineWidth( m_width + 2 * m_clearance );
189  drawLineChain( *l, gal );
190  }
191  break;
192  }
193 
194  case SH_SEGMENT:
195  {
196  const SHAPE_SEGMENT* s = (const SHAPE_SEGMENT*) m_shape;
197  gal->DrawSegment( s->GetSeg().A, s->GetSeg().B, s->GetWidth() );
198 
199  if( m_showTrackClearance && m_clearance > 0 )
200  {
201  gal->SetLayerDepth( ClearanceOverlayDepth );
202  gal->SetStrokeColor( COLOR4D( DARKDARKGRAY ) );
203  gal->SetFillColor( COLOR4D( DARKDARKGRAY ) );
204  gal->DrawSegment( s->GetSeg().A, s->GetSeg().B, s->GetWidth() + 2 * m_clearance );
205  }
206 
207  break;
208  }
209 
210  case SH_CIRCLE:
211  {
212  const SHAPE_CIRCLE* c = (const SHAPE_CIRCLE*) m_shape;
213  gal->DrawCircle( c->GetCenter(), c->GetRadius() );
214 
215  if( m_showViaClearance && m_clearance > 0 )
216  {
217  gal->SetLayerDepth( ClearanceOverlayDepth );
218  gal->SetFillColor( COLOR4D( DARKDARKGRAY ) );
219  gal->SetIsStroke( false );
220  gal->DrawCircle( c->GetCenter(), c->GetRadius() + m_clearance );
221  }
222 
223  break;
224  }
225 
226  case SH_RECT:
227  {
228  const SHAPE_RECT* r = (const SHAPE_RECT*) m_shape;
229  gal->DrawRectangle( r->GetPosition(), r->GetPosition() + r->GetSize() );
230 
231  if( m_clearance > 0 )
232  {
233  gal->SetLayerDepth( ClearanceOverlayDepth );
234  VECTOR2I p0( r->GetPosition() ), s( r->GetSize() );
235  gal->SetStrokeColor( COLOR4D( DARKDARKGRAY ) );
236  gal->SetIsStroke( true );
237  gal->SetLineWidth( 2 * m_clearance );
238  gal->DrawLine( p0, VECTOR2I( p0.x + s.x, p0.y ) );
239  gal->DrawLine( p0, VECTOR2I( p0.x, p0.y + s.y ) );
240  gal->DrawLine( p0 + s , VECTOR2I( p0.x + s.x, p0.y ) );
241  gal->DrawLine( p0 + s, VECTOR2I( p0.x, p0.y + s.y ) );
242  }
243 
244  break;
245  }
246 
247  case SH_CONVEX:
248  {
249  const SHAPE_CONVEX* c = (const SHAPE_CONVEX*) m_shape;
250  std::deque<VECTOR2D> polygon = std::deque<VECTOR2D>();
251  for( int i = 0; i < c->PointCount(); i++ )
252  {
253  polygon.push_back( c->CDPoint( i ) );
254  }
255  gal->DrawPolygon( polygon );
256 
257  if( m_clearance > 0 )
258  {
259  gal->SetLayerDepth( ClearanceOverlayDepth );
260  gal->SetStrokeColor( COLOR4D( DARKDARKGRAY ) );
261  gal->SetIsStroke( true );
262  gal->SetLineWidth( 2 * m_clearance );
263  // need the implicit last segment to be explicit for DrawPolyline
264  polygon.push_back( c->CDPoint( 0 ) );
265  gal->DrawPolyline( polygon );
266  }
267  break;
268  }
269 
270  case SH_POLY_SET:
271  case SH_COMPOUND:
272  break; // Not yet in use
273  }
274  }
275 }
276 
277 
278 void ROUTER_PREVIEW_ITEM::Line( const SHAPE_LINE_CHAIN& aLine, int aWidth, int aStyle )
279 {
280  m_originLayer = m_layer = 0;
281  m_width = aWidth;
282  m_color = assignColor( aStyle );
283  m_type = PR_SHAPE;
284  m_depth = -1024; // TODO gal->GetMinDepth()
285  m_shape = aLine.Clone();
286 }
287 
288 
289 void ROUTER_PREVIEW_ITEM::Point( const VECTOR2I& aPos, int aStyle )
290 {
291 }
292 
293 
294 void ROUTER_PREVIEW_ITEM::Box( const BOX2I& aBox, int aStyle )
295 {
296 }
297 
298 
300 {
301  auto settings = static_cast<PCB_RENDER_SETTINGS*>( m_view->GetPainter()->GetSettings() );
302 
303  return settings->GetLayerColor( aLayer );
304 }
305 
306 
307 const COLOR4D ROUTER_PREVIEW_ITEM::assignColor( int aStyle ) const
308 {
309  COLOR4D color;
310 
311  switch( aStyle )
312  {
313  case 0:
314  color = COLOR4D( 0, 1, 0, 1 ); break;
315 
316  case 1:
317  color = COLOR4D( 1, 0, 0, 1 ); break;
318 
319  case 2:
320  color = COLOR4D( 1, 1, 0, 1 ); break;
321 
322  case 3:
323  color = COLOR4D( 0, 0, 1, 1 ); break;
324 
325  case 4:
326  color = COLOR4D( 1, 1, 1, 1 ); break;
327 
328  case 5:
329  color = COLOR4D( 1, 1, 0, 1 ); break;
330 
331  case 6:
332  color = COLOR4D( 0, 1, 1, 1 ); break;
333 
334  case 32:
335  color = COLOR4D( 0, 0, 1, 1 ); break;
336 
337  default:
338  color = COLOR4D( 0.4, 0.4, 0.4, 1 ); break;
339  }
340 
341  return color;
342 }
343 
346 const int ROUTER_PREVIEW_ITEM::ViaOverlayDepth = -346;
any polygon (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:207
static const int BaseOverlayDepth
the 3d code uses this value
Definition: typeinfo.h:92
int GetRadius() const
Definition: shape_circle.h:79
virtual const SHAPE * Shape() const
Function Shape()
Definition: pns_item.h:296
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:590
virtual int Marker() const
Definition: pns_item.h:311
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:130
virtual SHAPE * Clone() const
Function Clone()
Definition: shape.h:93
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:289
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
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:120
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:82
Class SHAPE_LINE_CHAIN.
VECTOR2I A
Definition: seg.h:47
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:165
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:48