KiCad PCB EDA Suite
create_3Dgraphic_brd_items.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) 2015-2016 Mario Luzeiro <mrluzeiro@ua.pt>
5  * Copyright (C) 1992-2021 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
33 #include "../3d_rendering/3d_render_raytracing/shapes2D/ring_2d.h"
34 #include "../3d_rendering/3d_render_raytracing/shapes2D/filled_circle_2d.h"
35 #include "../3d_rendering/3d_render_raytracing/shapes2D/round_segment_2d.h"
36 #include "../3d_rendering/3d_render_raytracing/shapes2D/triangle_2d.h"
37 #include <board_adapter.h>
38 #include <board.h>
39 #include <footprint.h>
40 #include <pad.h>
41 #include <pcb_text.h>
42 #include <fp_shape.h>
43 #include <zone.h>
44 #include <string_utils.h>
45 #include <fp_text.h>
47 #include <trigo.h>
48 #include <geometry/shape_segment.h>
50 #include <geometry/shape_circle.h>
51 #include <geometry/shape_rect.h>
52 #include <geometry/shape_simple.h>
53 #include <gr_text.h>
54 #include <utility>
55 #include <vector>
56 #include <wx/log.h>
57 
58 
59 // These variables are parameters used in addTextSegmToContainer.
60 // But addTextSegmToContainer is a call-back function,
61 // so we cannot send them as arguments.
62 static int s_textWidth;
64 static float s_biuTo3Dunits;
65 static const BOARD_ITEM* s_boardItem = nullptr;
66 
67 
68 // This is a call back function, used by GRText to draw the 3D text shape:
69 void addTextSegmToContainer( int x0, int y0, int xf, int yf, void* aData )
70 {
71  const SFVEC2F start3DU( x0 * s_biuTo3Dunits, -y0 * s_biuTo3Dunits );
72  const SFVEC2F end3DU ( xf * s_biuTo3Dunits, -yf * s_biuTo3Dunits );
73 
74  if( Is_segment_a_circle( start3DU, end3DU ) )
76  *s_boardItem) );
77  else
79  *s_boardItem ) );
80 }
81 
82 
83 // Based on
84 // void PCB_TEXT::TransformTextShapeWithClearanceToPolygon
85 // board_items_to_polygon_shape_transform.cpp
87  PCB_LAYER_ID aLayerId, int aClearanceValue )
88 {
89  wxSize size = aText->GetTextSize();
90 
91  if( aText->IsMirrored() )
92  size.x = -size.x;
93 
94  s_boardItem = (const BOARD_ITEM *) &aText;
95  s_dstcontainer = aDstContainer;
96  s_textWidth = aText->GetEffectiveTextPenWidth() + ( 2 * aClearanceValue );
98 
99  // not actually used, but needed by GRText
100  const COLOR4D dummy_color;
101 
102  // Use the actual text width to generate segments. The segment position depend on
103  // text thickness and justification
104  bool isBold = aText->IsBold();
105  int penWidth = aText->GetEffectiveTextPenWidth();
106 
107  GRText( nullptr, aText->GetTextPos(), dummy_color, aText->GetShownText(),
108  aText->GetTextAngle(), size, aText->GetHorizJustify(), aText->GetVertJustify(),
109  penWidth, aText->IsItalic(), isBold, addTextSegmToContainer );
110 }
111 
112 
114  CONTAINER_2D_BASE* aDstContainer,
115  PCB_LAYER_ID aLayerId, int aClearanceValue )
116 {
117  addShapeWithClearance( &aDimension->Text(), aDstContainer, aLayerId, aClearanceValue );
118 
119  const int linewidth = aDimension->GetLineThickness() + ( 2 * aClearanceValue );
120 
121  for( const std::shared_ptr<SHAPE>& shape : aDimension->GetShapes() )
122  {
123  switch( shape->Type() )
124  {
125  case SH_SEGMENT:
126  {
127  const SEG& seg = static_cast<const SHAPE_SEGMENT*>( shape.get() )->GetSeg();
128 
129  const SFVEC2F start3DU( seg.A.x * m_biuTo3Dunits, -seg.A.y * m_biuTo3Dunits );
130 
131  const SFVEC2F end3DU( seg.B.x * m_biuTo3Dunits, -seg.B.y * m_biuTo3Dunits );
132 
133  aDstContainer->Add( new ROUND_SEGMENT_2D( start3DU, end3DU, linewidth * m_biuTo3Dunits,
134  *aDimension ) );
135  break;
136  }
137 
138  case SH_CIRCLE:
139  {
140  int radius = static_cast<const SHAPE_CIRCLE*>( shape.get() )->GetRadius();
141  int deltar = aDimension->GetLineThickness();
142 
143  SFVEC2F center( shape->Centre().x * m_biuTo3Dunits,
144  shape->Centre().y * m_biuTo3Dunits );
145 
146  aDstContainer->Add( new RING_2D( center, ( radius - deltar ) * m_biuTo3Dunits,
147  ( radius + deltar ) * m_biuTo3Dunits, *aDimension ) );
148 
149  break;
150  }
151 
152  default:
153  break;
154  }
155 
156  }
157 
158 }
159 
160 
161 // Based on
162 // void FOOTPRINT::TransformFPShapesWithClearanceToPolygonSet
163 // board_items_to_polygon_shape_transform.cpp#L204
165  CONTAINER_2D_BASE* aDstContainer,
166  PCB_LAYER_ID aLayerId, int aInflateValue )
167 {
168  std::vector<FP_TEXT*> texts; // List of FP_TEXT to convert
169  FP_SHAPE* outline;
170 
171  for( BOARD_ITEM* item : aFootprint->GraphicalItems() )
172  {
173  switch( item->Type() )
174  {
175  case PCB_FP_TEXT_T:
176  {
177  FP_TEXT* text = static_cast<FP_TEXT*>( item );
178 
179  if( text->GetLayer() == aLayerId && text->IsVisible() )
180  texts.push_back( text );
181  }
182  break;
183 
184 
185  case PCB_FP_SHAPE_T:
186  {
187  outline = (FP_SHAPE*) item;
188 
189  if( outline->GetLayer() != aLayerId )
190  break;
191 
192  addShapeWithClearance( (const PCB_SHAPE*) outline, aDstContainer, aLayerId, 0 );
193  }
194  break;
195 
196  default:
197  break;
198  }
199  }
200 
201  // Convert texts for footprints
202  if( aFootprint->Reference().GetLayer() == aLayerId && aFootprint->Reference().IsVisible() )
203  texts.push_back( &aFootprint->Reference() );
204 
205  if( aFootprint->Value().GetLayer() == aLayerId && aFootprint->Value().IsVisible() )
206  texts.push_back( &aFootprint->Value() );
207 
208  s_boardItem = (const BOARD_ITEM *)&aFootprint->Value();
209  s_dstcontainer = aDstContainer;
211 
212  for( FP_TEXT* text : texts )
213  {
214  s_textWidth = text->GetEffectiveTextPenWidth() + ( 2 * aInflateValue );
215  wxSize size = text->GetTextSize();
216  bool isBold = text->IsBold();
217  int penWidth = text->GetEffectiveTextPenWidth();
218 
219  if( text->IsMirrored() )
220  size.x = -size.x;
221 
222  GRText( nullptr, text->GetTextPos(), BLACK, text->GetShownText(), text->GetDrawRotation(),
223  size, text->GetHorizJustify(), text->GetVertJustify(), penWidth, text->IsItalic(),
224  isBold, addTextSegmToContainer );
225  }
226 }
227 
228 
229 void BOARD_ADAPTER::createTrack( const PCB_TRACK* aTrack, CONTAINER_2D_BASE* aDstContainer,
230  int aClearanceValue )
231 {
232  SFVEC2F start3DU( aTrack->GetStart().x * m_biuTo3Dunits,
233  -aTrack->GetStart().y * m_biuTo3Dunits ); // y coord is inverted
234 
235  switch( aTrack->Type() )
236  {
237  case PCB_VIA_T:
238  {
239  const float radius = ( ( aTrack->GetWidth() / 2 ) + aClearanceValue ) * m_biuTo3Dunits;
240  aDstContainer->Add( new FILLED_CIRCLE_2D( start3DU, radius, *aTrack ) );
241  break;
242  }
243 
244  case PCB_ARC_T:
245  {
246  const PCB_ARC* arc = static_cast<const PCB_ARC*>( aTrack );
247  VECTOR2D center( arc->GetCenter() );
248  double arc_angle = arc->GetAngle();
249  double radius = arc->GetRadius();
250  int arcsegcount = GetArcToSegmentCount( radius, Millimeter2iu( 0.005), arc_angle/10 );
251  int circlesegcount;
252 
253  // We need a circle to segment count. However, the arc angle can be small, and the
254  // radius very big. so we calculate a reasonable value for circlesegcount.
255  if( arcsegcount <= 1 ) // The arc will be approximated by a segment
256  circlesegcount = 1;
257  else
258  {
259  double cnt = arcsegcount * 3600/std::abs( arc_angle );
260 
261 #define SEG_CNT_MAX 128
262  if( cnt < SEG_CNT_MAX )
263  {
264  circlesegcount = (int)cnt;
265 
266  if( circlesegcount == 0 )
267  circlesegcount = 1;
268  }
269  else
270  {
271  circlesegcount = SEG_CNT_MAX;
272  }
273  }
274 
275  transformArcToSegments( wxPoint( center.x, center.y ), arc->GetStart(),
276  arc_angle, circlesegcount,
277  arc->GetWidth() + 2 * aClearanceValue, aDstContainer, *arc );
278  break;
279  }
280 
281  case PCB_TRACE_T: // Track is a usual straight segment
282  {
283  SFVEC2F end3DU( aTrack->GetEnd().x * m_biuTo3Dunits, -aTrack->GetEnd().y * m_biuTo3Dunits );
284 
285  // Cannot add segments that have the same start and end point
286  if( Is_segment_a_circle( start3DU, end3DU ) )
287  {
288  const float radius = ((aTrack->GetWidth() / 2) + aClearanceValue) * m_biuTo3Dunits;
289 
290  aDstContainer->Add( new FILLED_CIRCLE_2D( start3DU, radius, *aTrack ) );
291  }
292  else
293  {
294  const float width = (aTrack->GetWidth() + 2 * aClearanceValue ) * m_biuTo3Dunits;
295 
296  aDstContainer->Add( new ROUND_SEGMENT_2D( start3DU, end3DU, width, *aTrack ) );
297  }
298 
299  break;
300  }
301 
302  default:
303  break;
304  }
305 }
306 
307 
309  PCB_LAYER_ID aLayer,
310  const wxSize& aClearanceValue ) const
311 {
312  SHAPE_POLY_SET poly;
313  wxSize clearance = aClearanceValue;
314 
315  // Our shape-based builder can't handle negative or differing x:y clearance values (the
316  // former are common for solder paste while the later get generated when a relative paste
317  // margin is used with an oblong pad). So we apply this huge hack and fake a larger pad to
318  // run the general-purpose polygon builder on.
319  // Of course being a hack it falls down when dealing with custom shape pads (where the size
320  // is only the size of the anchor), so for those we punt and just use aClearanceValue.x.
321 
322  if( ( clearance.x < 0 || clearance.x != clearance.y )
323  && aPad->GetShape() != PAD_SHAPE::CUSTOM )
324  {
325  PAD dummy( *aPad );
326  dummy.SetSize( aPad->GetSize() + clearance + clearance );
327  dummy.TransformShapeWithClearanceToPolygon( poly, aLayer, 0, ARC_HIGH_DEF, ERROR_INSIDE );
328  clearance = { 0, 0 };
329  }
330  else
331  {
332  auto padShapes = std::static_pointer_cast<SHAPE_COMPOUND>( aPad->GetEffectiveShape() );
333 
334  for( const SHAPE* shape : padShapes->Shapes() )
335  {
336  switch( shape->Type() )
337  {
338  case SH_SEGMENT:
339  {
340  const SHAPE_SEGMENT* seg = (SHAPE_SEGMENT*) shape;
341  const SFVEC2F start3DU( seg->GetSeg().A.x * m_biuTo3Dunits,
342  -seg->GetSeg().A.y * m_biuTo3Dunits );
343  const SFVEC2F end3DU ( seg->GetSeg().B.x * m_biuTo3Dunits,
344  -seg->GetSeg().B.y * m_biuTo3Dunits );
345  const int width = seg->GetWidth() + clearance.x * 2;
346 
347  // Cannot add segments that have the same start and end point
348  if( Is_segment_a_circle( start3DU, end3DU ) )
349  {
350  aDstContainer->Add( new FILLED_CIRCLE_2D( start3DU,
351  ( width / 2 ) * m_biuTo3Dunits,
352  *aPad ) );
353  }
354  else
355  {
356  aDstContainer->Add( new ROUND_SEGMENT_2D( start3DU, end3DU,
357  width * m_biuTo3Dunits,
358  *aPad ) );
359  }
360  }
361  break;
362 
363  case SH_CIRCLE:
364  {
365  const SHAPE_CIRCLE* circle = (SHAPE_CIRCLE*) shape;
366  const int radius = circle->GetRadius() + clearance.x;
367  const SFVEC2F center( circle->GetCenter().x * m_biuTo3Dunits,
368  -circle->GetCenter().y * m_biuTo3Dunits );
369 
370  aDstContainer->Add( new FILLED_CIRCLE_2D( center, radius * m_biuTo3Dunits,
371  *aPad ) );
372  }
373  break;
374 
375  case SH_RECT:
376  {
377  SHAPE_RECT* rect = (SHAPE_RECT*) shape;
378 
379  poly.NewOutline();
380  poly.Append( rect->GetPosition() );
381  poly.Append( rect->GetPosition().x + rect->GetSize().x, rect->GetPosition().y );
382  poly.Append( rect->GetPosition() + rect->GetSize() );
383  poly.Append( rect->GetPosition().x, rect->GetPosition().y + rect->GetSize().y );
384  }
385  break;
386 
387  case SH_SIMPLE:
388  poly.AddOutline( static_cast<const SHAPE_SIMPLE*>( shape )->Vertices() );
389  break;
390 
391  case SH_POLY_SET:
392  poly = *(SHAPE_POLY_SET*) shape;
393  break;
394 
395  case SH_ARC:
396  {
397  SHAPE_ARC* arc = (SHAPE_ARC*) shape;
399 
400  for( int i = 0; i < l.SegmentCount(); i++ )
401  {
402  SHAPE_SEGMENT seg( l.Segment( i ).A, l.Segment( i ).B, arc->GetWidth() );
403  const SFVEC2F start3DU( seg.GetSeg().A.x * m_biuTo3Dunits,
404  -seg.GetSeg().A.y * m_biuTo3Dunits );
405  const SFVEC2F end3DU( seg.GetSeg().B.x * m_biuTo3Dunits,
406  -seg.GetSeg().B.y * m_biuTo3Dunits );
407  const int width = arc->GetWidth() + clearance.x * 2;
408 
409  // Cannot add segments that have the same start and end point
410  if( Is_segment_a_circle( start3DU, end3DU ) )
411  {
412  aDstContainer->Add( new FILLED_CIRCLE_2D( start3DU,
413  ( width / 2 ) * m_biuTo3Dunits,
414  *aPad ) );
415  }
416  else
417  {
418  aDstContainer->Add( new ROUND_SEGMENT_2D( start3DU, end3DU,
419  width * m_biuTo3Dunits,
420  *aPad ) );
421  }
422  }
423  }
424  break;
425 
426  default:
427  wxFAIL_MSG( "BOARD_ADAPTER::createPadWithClearance no implementation for "
428  + SHAPE_TYPE_asString( shape->Type() ) );
429  break;
430  }
431  }
432  }
433 
434  if( !poly.IsEmpty() )
435  {
436  if( clearance.x )
437  poly.Inflate( clearance.x, 32 );
438 
439  // Add the PAD polygon
440  ConvertPolygonToTriangles( poly, *aDstContainer, m_biuTo3Dunits, *aPad );
441  }
442 }
443 
444 
445 OBJECT_2D* BOARD_ADAPTER::createPadWithDrill( const PAD* aPad, int aInflateValue )
446 {
447  wxSize drillSize = aPad->GetDrillSize();
448 
449  if( !drillSize.x || !drillSize.y )
450  {
451  wxLogTrace( m_logTrace, wxT( "BOARD_ADAPTER::createPadWithDrill - found an invalid pad" ) );
452  return nullptr;
453  }
454 
455  if( drillSize.x == drillSize.y ) // usual round hole
456  {
457  const int radius = ( drillSize.x / 2 ) + aInflateValue;
458 
459  const SFVEC2F center( aPad->GetPosition().x * m_biuTo3Dunits,
460  -aPad->GetPosition().y * m_biuTo3Dunits );
461 
462  return new FILLED_CIRCLE_2D( center, radius * m_biuTo3Dunits, *aPad );
463 
464  }
465  else // Oblong hole
466  {
467  const SHAPE_SEGMENT* seg = aPad->GetEffectiveHoleShape();
468  float width = seg->GetWidth() + aInflateValue * 2;
469 
470  SFVEC2F start3DU( seg->GetSeg().A.x * m_biuTo3Dunits,
471  -seg->GetSeg().A.y * m_biuTo3Dunits );
472 
473  SFVEC2F end3DU ( seg->GetSeg().B.x * m_biuTo3Dunits,
474  -seg->GetSeg().B.y * m_biuTo3Dunits );
475 
476  return new ROUND_SEGMENT_2D( start3DU, end3DU, width * m_biuTo3Dunits, *aPad );
477  }
478 
479  return nullptr;
480 }
481 
482 
484  CONTAINER_2D_BASE* aDstContainer,
485  PCB_LAYER_ID aLayerId, int aInflateValue,
486  bool aSkipNPTHPadsWihNoCopper, bool aSkipPlatedPads,
487  bool aSkipNonPlatedPads )
488 {
489  for( PAD* pad : aFootprint->Pads() )
490  {
491  if( !pad->IsOnLayer( aLayerId ) )
492  continue;
493 
494  // Skip pad annulus when not connected on this layer (if removing is enabled)
495  if( !pad->FlashLayer( aLayerId ) && IsCopperLayer( aLayerId ) )
496  continue;
497 
498  // NPTH pads are not drawn on layers if the
499  // shape size and pos is the same as their hole:
500  if( aSkipNPTHPadsWihNoCopper && ( pad->GetAttribute() == PAD_ATTRIB::NPTH ) )
501  {
502  if( pad->GetDrillSize() == pad->GetSize() && pad->GetOffset() == wxPoint( 0, 0 ) )
503  {
504  switch( pad->GetShape() )
505  {
506  case PAD_SHAPE::CIRCLE:
507  if( pad->GetDrillShape() == PAD_DRILL_SHAPE_CIRCLE )
508  continue;
509 
510  break;
511 
512  case PAD_SHAPE::OVAL:
513  if( pad->GetDrillShape() != PAD_DRILL_SHAPE_CIRCLE )
514  continue;
515 
516  break;
517 
518  default:
519  break;
520  }
521  }
522  }
523 
524  const bool isPlated = ( ( aLayerId == F_Cu ) && pad->FlashLayer( F_Mask ) ) ||
525  ( ( aLayerId == B_Cu ) && pad->FlashLayer( B_Mask ) );
526 
527  if( aSkipPlatedPads && isPlated )
528  continue;
529 
530  if( aSkipNonPlatedPads && !isPlated )
531  continue;
532 
533  wxSize margin( aInflateValue, aInflateValue );
534 
535  switch( aLayerId )
536  {
537  case F_Mask:
538  case B_Mask:
539  margin.x += pad->GetSolderMaskMargin();
540  margin.y += pad->GetSolderMaskMargin();
541  break;
542 
543  case F_Paste:
544  case B_Paste:
545  margin += pad->GetSolderPasteMargin();
546  break;
547 
548  default:
549  break;
550  }
551 
552  createPadWithClearance( pad, aDstContainer, aLayerId, margin );
553  }
554 }
555 
556 
557 // based on TransformArcToPolygon function from
558 // common/convert_basic_shapes_to_polygon.cpp
559 void BOARD_ADAPTER::transformArcToSegments( const wxPoint& aCentre, const wxPoint& aStart,
560  double aArcAngle, int aCircleToSegmentsCount,
561  int aWidth, CONTAINER_2D_BASE* aDstContainer,
562  const BOARD_ITEM& aBoardItem )
563 {
564  wxPoint arc_start, arc_end;
565  int delta = 3600 / aCircleToSegmentsCount; // rotate angle in 0.1 degree
566 
567  arc_end = arc_start = aStart;
568 
569  if( aArcAngle != 3600 )
570  {
571  RotatePoint( &arc_end, aCentre, -aArcAngle );
572  }
573 
574  if( aArcAngle < 0 )
575  {
576  std::swap( arc_start, arc_end );
577  aArcAngle = -aArcAngle;
578  }
579 
580  // Compute the ends of segments and creates poly
581  wxPoint curr_end = arc_start;
582  wxPoint curr_start = arc_start;
583 
584  for( int ii = delta; ii < aArcAngle; ii += delta )
585  {
586  curr_end = arc_start;
587  RotatePoint( &curr_end, aCentre, -ii );
588 
589  const SFVEC2F start3DU( curr_start.x * m_biuTo3Dunits, -curr_start.y * m_biuTo3Dunits );
590  const SFVEC2F end3DU ( curr_end.x * m_biuTo3Dunits, -curr_end.y * m_biuTo3Dunits );
591 
592  if( Is_segment_a_circle( start3DU, end3DU ) )
593  {
594  aDstContainer->Add( new FILLED_CIRCLE_2D( start3DU, ( aWidth / 2 ) * m_biuTo3Dunits,
595  aBoardItem ) );
596  }
597  else
598  {
599  aDstContainer->Add( new ROUND_SEGMENT_2D( start3DU, end3DU, aWidth * m_biuTo3Dunits,
600  aBoardItem ) );
601  }
602 
603  curr_start = curr_end;
604  }
605 
606  if( curr_end != arc_end )
607  {
608  const SFVEC2F start3DU( curr_end.x * m_biuTo3Dunits, -curr_end.y * m_biuTo3Dunits );
609  const SFVEC2F end3DU ( arc_end.x * m_biuTo3Dunits, -arc_end.y * m_biuTo3Dunits );
610 
611  if( Is_segment_a_circle( start3DU, end3DU ) )
612  {
613  aDstContainer->Add( new FILLED_CIRCLE_2D( start3DU, ( aWidth / 2 ) * m_biuTo3Dunits,
614  aBoardItem ) );
615  }
616  else
617  {
618  aDstContainer->Add( new ROUND_SEGMENT_2D( start3DU, end3DU, aWidth * m_biuTo3Dunits,
619  aBoardItem ) );
620  }
621  }
622 }
623 
624 
625 // Based on
626 // TransformShapeWithClearanceToPolygon
627 // board_items_to_polygon_shape_transform.cpp#L431
629  CONTAINER_2D_BASE* aDstContainer, PCB_LAYER_ID aLayerId,
630  int aClearanceValue )
631 {
632  // The full width of the lines to create
633  // The extra 1 protects the inner/outer radius values from degeneracy
634  const int linewidth = aShape->GetWidth() + ( 2 * aClearanceValue ) + 1;
635 
636  switch( aShape->GetShape() )
637  {
638  case SHAPE_T::CIRCLE:
639  {
640  const SFVEC2F center3DU( aShape->GetCenter().x * m_biuTo3Dunits,
641  -aShape->GetCenter().y * m_biuTo3Dunits );
642 
643  float inner_radius = ( aShape->GetRadius() - linewidth / 2 ) * m_biuTo3Dunits;
644  float outer_radius = ( aShape->GetRadius() + linewidth / 2 ) * m_biuTo3Dunits;
645 
646  if( inner_radius < 0 )
647  inner_radius = 0;
648 
649  if( aShape->IsFilled() )
650  aDstContainer->Add( new FILLED_CIRCLE_2D( center3DU, outer_radius, *aShape ) );
651  else
652  aDstContainer->Add( new RING_2D( center3DU, inner_radius, outer_radius, *aShape ) );
653  }
654  break;
655 
656  case SHAPE_T::RECT:
657  if( aShape->IsFilled() )
658  {
659  SHAPE_POLY_SET polyList;
660 
661  aShape->TransformShapeWithClearanceToPolygon( polyList, aLayerId, 0,
662  ARC_HIGH_DEF, ERROR_INSIDE );
663 
664  polyList.Simplify( SHAPE_POLY_SET::PM_FAST );
665 
666  ConvertPolygonToTriangles( polyList, *aDstContainer, m_biuTo3Dunits, *aShape );
667  }
668  else
669  {
670  std::vector<wxPoint> pts = aShape->GetRectCorners();
671 
672  const SFVEC2F topLeft3DU( pts[0].x * m_biuTo3Dunits, -pts[0].y * m_biuTo3Dunits );
673  const SFVEC2F topRight3DU( pts[1].x * m_biuTo3Dunits, -pts[1].y * m_biuTo3Dunits );
674  const SFVEC2F botRight3DU( pts[2].x * m_biuTo3Dunits, -pts[2].y * m_biuTo3Dunits );
675  const SFVEC2F botLeft3DU( pts[3].x * m_biuTo3Dunits, -pts[3].y * m_biuTo3Dunits );
676 
677  aDstContainer->Add( new ROUND_SEGMENT_2D( topLeft3DU, topRight3DU,
678  linewidth * m_biuTo3Dunits, *aShape ) );
679  aDstContainer->Add( new ROUND_SEGMENT_2D( topRight3DU, botRight3DU,
680  linewidth * m_biuTo3Dunits, *aShape ) );
681  aDstContainer->Add( new ROUND_SEGMENT_2D( botRight3DU, botLeft3DU,
682  linewidth * m_biuTo3Dunits, *aShape ) );
683  aDstContainer->Add( new ROUND_SEGMENT_2D( botLeft3DU, topLeft3DU,
684  linewidth * m_biuTo3Dunits, *aShape ) );
685  }
686  break;
687 
688  case SHAPE_T::ARC:
689  {
690  unsigned int segCount = GetCircleSegmentCount( aShape->GetBoundingBox().GetSizeMax() );
691 
692  transformArcToSegments( aShape->GetCenter(), aShape->GetArcStart(), aShape->GetAngle(),
693  segCount, linewidth, aDstContainer, *aShape );
694  }
695  break;
696 
697  case SHAPE_T::SEGMENT:
698  {
699  const SFVEC2F start3DU( aShape->GetStart().x * m_biuTo3Dunits,
700  -aShape->GetStart().y * m_biuTo3Dunits );
701 
702  const SFVEC2F end3DU ( aShape->GetEnd().x * m_biuTo3Dunits,
703  -aShape->GetEnd().y * m_biuTo3Dunits );
704 
705  if( Is_segment_a_circle( start3DU, end3DU ) )
706  {
707  aDstContainer->Add( new FILLED_CIRCLE_2D( start3DU, ( linewidth / 2 ) * m_biuTo3Dunits,
708  *aShape ) );
709  }
710  else
711  {
712  aDstContainer->Add( new ROUND_SEGMENT_2D( start3DU, end3DU, linewidth * m_biuTo3Dunits,
713  *aShape ) );
714  }
715  }
716  break;
717 
718  case SHAPE_T::BEZIER:
719  case SHAPE_T::POLY:
720  {
721  SHAPE_POLY_SET polyList;
722 
723  aShape->TransformShapeWithClearanceToPolygon( polyList, aLayerId, 0,
724  ARC_HIGH_DEF, ERROR_INSIDE );
725 
726  polyList.Simplify( SHAPE_POLY_SET::PM_FAST );
727 
728  if( polyList.IsEmpty() ) // Just for caution
729  break;
730 
731  ConvertPolygonToTriangles( polyList, *aDstContainer, m_biuTo3Dunits, *aShape );
732  }
733  break;
734 
735  default:
736  wxFAIL_MSG( "BOARD_ADAPTER::addShapeWithClearance no implementation for "
737  + SHAPE_T_asString( aShape->GetShape()) );
738  break;
739  }
740 }
741 
742 
743 // Based on
744 // TransformSolidAreasShapesToPolygonSet
745 // board_items_to_polygon_shape_transform.cpp
746 void BOARD_ADAPTER::addSolidAreasShapes( const ZONE* aZoneContainer,
747  CONTAINER_2D_BASE* aDstContainer, PCB_LAYER_ID aLayerId )
748 {
749  // Copy the polys list because we have to simplify it
750  SHAPE_POLY_SET polyList = SHAPE_POLY_SET( aZoneContainer->GetFilledPolysList( aLayerId ) );
751 
752  // This convert the poly in outline and holes
753  ConvertPolygonToTriangles( polyList, *aDstContainer, m_biuTo3Dunits, *aZoneContainer );
754 
755  // add filled areas outlines, which are drawn with thick lines segments
756  // but only if filled polygons outlines have thickness
757  if( !aZoneContainer->GetFilledPolysUseThickness() )
758  return;
759 
760  float line_thickness = aZoneContainer->GetMinThickness() * m_biuTo3Dunits;
761 
762  for( int i = 0; i < polyList.OutlineCount(); ++i )
763  {
764  // Add outline
765  const SHAPE_LINE_CHAIN& pathOutline = polyList.COutline( i );
766 
767  for( int j = 0; j < pathOutline.PointCount(); ++j )
768  {
769  const VECTOR2I& a = pathOutline.CPoint( j );
770  const VECTOR2I& b = pathOutline.CPoint( j + 1 );
771 
772  SFVEC2F start3DU( a.x * m_biuTo3Dunits, -a.y * m_biuTo3Dunits );
773  SFVEC2F end3DU ( b.x * m_biuTo3Dunits, -b.y * m_biuTo3Dunits );
774 
775  if( Is_segment_a_circle( start3DU, end3DU ) )
776  {
777  float radius = line_thickness/2;
778 
779  if( radius > 0.0 ) // degenerated circles crash 3D viewer
780  aDstContainer->Add( new FILLED_CIRCLE_2D( start3DU, radius, *aZoneContainer ) );
781  }
782  else
783  {
784  aDstContainer->Add( new ROUND_SEGMENT_2D( start3DU, end3DU, line_thickness,
785  *aZoneContainer ) );
786  }
787  }
788 
789  // Add holes (of the poly, ie: the open parts) for this outline
790  for( int h = 0; h < polyList.HoleCount( i ); ++h )
791  {
792  const SHAPE_LINE_CHAIN& pathHole = polyList.CHole( i, h );
793 
794  for( int j = 0; j < pathHole.PointCount(); j++ )
795  {
796  const VECTOR2I& a = pathHole.CPoint( j );
797  const VECTOR2I& b = pathHole.CPoint( j + 1 );
798 
799  SFVEC2F start3DU( a.x * m_biuTo3Dunits, -a.y * m_biuTo3Dunits );
800  SFVEC2F end3DU ( b.x * m_biuTo3Dunits, -b.y * m_biuTo3Dunits );
801 
802  if( Is_segment_a_circle( start3DU, end3DU ) )
803  {
804  float radius = line_thickness/2;
805 
806  if( radius > 0.0 ) // degenerated circles crash 3D viewer
807  aDstContainer->Add( new FILLED_CIRCLE_2D( start3DU, radius,
808  *aZoneContainer ) );
809  }
810  else
811  {
812  aDstContainer->Add( new ROUND_SEGMENT_2D( start3DU, end3DU, line_thickness,
813  *aZoneContainer ) );
814  }
815  }
816  }
817  }
818 }
819 
820 
822  int aWidth )
823 {
824  if( aPad->GetShape() == PAD_SHAPE::CIRCLE ) // Draw a ring
825  {
826  const SFVEC2F center3DU( aPad->ShapePos().x * m_biuTo3Dunits,
827  -aPad->ShapePos().y * m_biuTo3Dunits );
828 
829  const int radius = aPad->GetSize().x / 2;
830  const float inner_radius = ( radius - aWidth / 2 ) * m_biuTo3Dunits;
831  const float outer_radius = ( radius + aWidth / 2 ) * m_biuTo3Dunits;
832 
833  aDstContainer->Add( new RING_2D( center3DU, inner_radius, outer_radius, *aPad ) );
834 
835  return;
836  }
837 
838  // For other shapes, add outlines as thick segments in polygon buffer
839  const std::shared_ptr<SHAPE_POLY_SET>& corners = aPad->GetEffectivePolygon();
840  const SHAPE_LINE_CHAIN& path = corners->COutline( 0 );
841 
842  for( int j = 0; j < path.PointCount(); j++ )
843  {
844  const VECTOR2I& a = path.CPoint( j );
845  const VECTOR2I& b = path.CPoint( j + 1 );
846 
847  SFVEC2F start3DU( a.x * m_biuTo3Dunits, -a.y * m_biuTo3Dunits );
848  SFVEC2F end3DU ( b.x * m_biuTo3Dunits, -b.y * m_biuTo3Dunits );
849 
850  if( Is_segment_a_circle( start3DU, end3DU ) )
851  {
852  aDstContainer->Add( new FILLED_CIRCLE_2D( start3DU, ( aWidth / 2 ) * m_biuTo3Dunits,
853  *aPad ) );
854  }
855  else
856  {
857  aDstContainer->Add( new ROUND_SEGMENT_2D( start3DU, end3DU, aWidth * m_biuTo3Dunits,
858  *aPad ) );
859  }
860  }
861 }
bool IsBold() const
Definition: eda_text.h:183
virtual wxPoint GetCenter() const override
This defaults to the center of the bounding box if not overridden.
Definition: pcb_track.h:282
Arcs (with rounded ends)
bool IsFilled() const
Definition: pcb_shape.h:75
Bezier Curve.
void ConvertPolygonToTriangles(SHAPE_POLY_SET &aPolyList, CONTAINER_2D_BASE &aDstContainer, float aBiuTo3dUnitsScale, const BOARD_ITEM &aBoardItem)
int OutlineCount() const
Return the number of vertices in a given outline/hole.
EDA_TEXT_VJUSTIFY_T GetVertJustify() const
Definition: eda_text.h:199
bool IsMirrored() const
Definition: eda_text.h:189
class FP_TEXT, text in a footprint
Definition: typeinfo.h:92
polygon (not yet used for tracks, but could be in microwave apps)
int GetRadius() const
Definition: shape_circle.h:107
const wxPoint & GetEnd() const
Definition: pcb_track.h:105
const wxPoint & GetEnd() const
Return the ending point of the graphic.
Definition: pcb_shape.h:134
void buildPadOutlineAsSegments(const PAD *aPad, CONTAINER_2D_BASE *aDstContainer, int aWidth)
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:80
#define SEG_CNT_MAX
SHAPE_T GetShape() const
Definition: pcb_shape.h:110
OBJECT_2D * createPadWithDrill(const PAD *aPad, int aInflateValue)
PCB_TEXT & Text()
int GetRadius() const
Return the radius of this item.
Definition: pcb_shape.cpp:487
void createPadWithClearance(const PAD *aPad, CONTAINER_2D_BASE *aDstContainer, PCB_LAYER_ID aLayer, const wxSize &aClearanceValue) const
bool IsEmpty() const
std::vector< wxPoint > GetRectCorners() const
Definition: pcb_shape.cpp:1048
int GetWidth() const
Definition: pcb_shape.h:97
void transformArcToSegments(const wxPoint &aCentre, const wxPoint &aStart, double aArcAngle, int aCircleToSegmentsCount, int aWidth, CONTAINER_2D_BASE *aDstContainer, const BOARD_ITEM &aBoardItem)
usual segment : line with rounded ends
static void isPlated(LIBEVAL::CONTEXT *aCtx, void *self)
double GetTextAngle() const
Definition: eda_text.h:174
class PCB_ARC, an arc track segment on a copper layer
Definition: typeinfo.h:97
const std::vector< std::shared_ptr< SHAPE > > & GetShapes() const
const SHAPE_SEGMENT * GetEffectiveHoleShape() const
Return a SHAPE object representing the pad's hole.
Definition: pad.cpp:306
static CONTAINER_2D_BASE * s_dstcontainer
void GRText(wxDC *aDC, const wxPoint &aPos, const COLOR4D &aColor, const wxString &aText, double aOrient, const wxSize &aSize, enum EDA_TEXT_HJUSTIFY_T aH_justify, enum EDA_TEXT_VJUSTIFY_T aV_justify, int aWidth, bool aItalic, bool aBold, void(*aCallback)(int x0, int y0, int xf, int yf, void *aData), void *aCallbackData, PLOTTER *aPlotter)
Draw a graphic text (like footprint texts).
Definition: gr_text.cpp:129
class FP_SHAPE, a footprint edge
Definition: typeinfo.h:93
Abstract dimension API.
Definition: pcb_dimension.h:95
const SHAPE_POLY_SET & GetFilledPolysList(PCB_LAYER_ID aLayer) const
Definition: zone.h:635
const SHAPE_LINE_CHAIN & CHole(int aOutline, int aHole) const
const VECTOR2I GetCenter() const
Definition: shape_circle.h:112
int GetWidth() const
Definition: pcb_track.h:102
Definition: color4d.h:44
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:229
void addFootprintShapesWithClearance(const FOOTPRINT *aFootprint, CONTAINER_2D_BASE *aDstContainer, PCB_LAYER_ID aLayerId, int aInflateValue)
int PointCount() const
Return the number of points (vertices) in this line chain.
const SEG & GetSeg() const
class PCB_TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:95
int GetEffectiveTextPenWidth(int aDefaultWidth=0) const
The EffectiveTextPenWidth uses the text thickness if > 1 or aDefaultWidth.
Definition: eda_text.cpp:149
PADS & Pads()
Definition: footprint.h:168
wxPoint GetArcStart() const
Definition: pcb_shape.h:156
FP_TEXT & Value()
read/write accessors:
Definition: footprint.h:466
static int s_textWidth
void Add(OBJECT_2D *aObject)
Definition: container_2d.h:49
const VECTOR2I GetSize() const
Definition: shape_rect.h:124
bool IsItalic() const
Definition: eda_text.h:180
FP_TEXT & Reference()
Definition: footprint.h:467
static LIB_SYMBOL * dummy()
Used to draw a dummy shape when a LIB_SYMBOL is not found in library.
Definition: sch_symbol.cpp:72
wxString GetShownText(int aDepth=0) const override
Return the string actually shown after processing of the base text.
Definition: pcb_text.cpp:56
const VECTOR2I & CPoint(int aIndex) const
Return a reference to a given point in the line chain.
const wxSize & GetDrillSize() const
Definition: pad.h:243
wxPoint ShapePos() const
Definition: pad.cpp:673
like PAD_PTH, but not plated
void addShapeWithClearance(const PCB_TEXT *aText, CONTAINER_2D_BASE *aDstContainer, PCB_LAYER_ID aLayerId, int aClearanceValue)
EDA_TEXT_HJUSTIFY_T GetHorizJustify() const
Definition: eda_text.h:198
int GetMinThickness() const
Definition: zone.h:244
glm::vec2 SFVEC2F
Definition: xv3d_types.h:42
static float s_biuTo3Dunits
Represent a set of closed polygons.
virtual bool IsVisible() const
Definition: eda_text.h:186
const VECTOR2I & GetPosition() const
Definition: shape_rect.h:116
const wxSize & GetTextSize() const
Definition: eda_text.h:238
double GetRadius() const
Definition: pcb_track.cpp:968
const wxSize & GetSize() const
Definition: pad.h:233
wxPoint GetCenter() const override
This defaults to the center of the bounding box if not overridden.
Definition: pcb_shape.cpp:385
void addSolidAreasShapes(const ZONE *aZoneContainer, CONTAINER_2D_BASE *aDstContainer, PCB_LAYER_ID aLayerId)
const wxPoint & GetStart() const
Return the starting point of the graphic.
Definition: pcb_shape.h:124
static wxString SHAPE_T_asString(SHAPE_T a)
Definition: board_item.h:57
void addPadsWithClearance(const FOOTPRINT *aFootprint, CONTAINER_2D_BASE *aDstContainer, PCB_LAYER_ID aLayerId, int aInflateValue, bool aSkipNPTHPadsWihNoCopper, bool aSkipPlatedPads, bool aSkipNonPlatedPads)
double m_biuTo3Dunits
Top (End) Z position of each layer in 3D units.
PAD_SHAPE GetShape() const
Definition: pad.h:170
static wxString SHAPE_TYPE_asString(SHAPE_TYPE a)
Definition: shape.h:55
circular arc
Definition: shape.h:50
void createTrack(const PCB_TRACK *aTrack, CONTAINER_2D_BASE *aDstContainer, int aClearanceValue)
DRAWINGS & GraphicalItems()
Definition: footprint.h:171
virtual std::shared_ptr< SHAPE > GetEffectiveShape(PCB_LAYER_ID aLayer=UNDEFINED_LAYER) const override
Some pad shapes can be complex (rounded/chamfered rectangle), even without considering custom shapes.
Definition: pad.cpp:297
a few functions useful in geometry calculations.
void Simplify(POLYGON_MODE aFastMode)
Handle a list of polygons defining a copper zone.
Definition: zone.h:56
An abstract shape on 2D plane.
Definition: shape.h:116
bool Is_segment_a_circle(const SFVEC2F &aStart, const SFVEC2F &aEnd)
Check if segment start and end is very close to each other.
int NewOutline()
Creates a new hole in a given outline.
int HoleCount(int aOutline) const
Return the reference to aIndex-th outline in the set.
int SegmentCount() const
Return the number of segments in this line chain.
bool IsCopperLayer(LAYER_NUM aLayerId)
Tests whether a layer is a copper layer.
Definition: layer_ids.h:787
circle
Definition: shape.h:46
void Inflate(int aAmount, int aCircleSegCount, CORNER_STRATEGY aCornerStrategy=ROUND_ALL_CORNERS)
Perform outline inflation/deflation.
Definition: seg.h:40
int AddOutline(const SHAPE_LINE_CHAIN &aOutline)
Adds a new hole to the given outline (default: last) and returns its index.
SEG Segment(int aIndex)
Return a copy of the aIndex-th segment in the line chain.
int GetWidth() const
Definition: shape_arc.h:156
set of polygons (with holes, etc.)
Definition: shape.h:48
double GetAngle() const
Definition: pcb_track.cpp:974
const SHAPE_LINE_CHAIN ConvertToPolyline(double aAccuracy=DefaultAccuracyForPCB(), double *aEffectiveAccuracy=nullptr) const
Construct a SHAPE_LINE_CHAIN of segments from a given arc.
Definition: shape_arc.cpp:498
static const wxChar * m_logTrace
Trace mask used to enable or disable the trace output of this class.
wxPoint GetPosition() const override
Definition: pad.h:178
Represent a polyline (an zero-thickness chain of connected line segments).
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:64
const SHAPE_LINE_CHAIN & COutline(int aIndex) const
Definition: layer_ids.h:70
const EDA_RECT GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
Definition: pcb_shape.cpp:622
VECTOR2I A
Definition: seg.h:48
double GetAngle() const
Definition: pcb_shape.h:107
const std::shared_ptr< SHAPE_POLY_SET > & GetEffectivePolygon() const
Definition: pad.cpp:288
void addTextSegmToContainer(int x0, int y0, int xf, int yf, void *aData)
const wxPoint & GetTextPos() const
Definition: eda_text.h:247
constexpr int delta
class PCB_VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:96
axis-aligned rectangle
Definition: shape.h:43
segment with non rounded ends
static const BOARD_ITEM * s_boardItem
Definition: pad.h:57
void TransformShapeWithClearanceToPolygon(SHAPE_POLY_SET &aCornerBuffer, PCB_LAYER_ID aLayer, int aClearanceValue, int aError, ERROR_LOC aErrorLoc, bool ignoreLineWidth=false) const override
Convert the draw segment to a closed polygon.
int GetWidth() const
static constexpr int Millimeter2iu(double mm)
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Definition: board_item.h:171
simple polygon
Definition: shape.h:47
int GetArcToSegmentCount(int aRadius, int aErrorMax, double aArcAngleDegree)
unsigned int GetCircleSegmentCount(float aDiameter3DU) const
int GetSizeMax() const
Definition: eda_rect.h:96
int GetLineThickness() const
const wxPoint & GetStart() const
Definition: pcb_track.h:108
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:113
line segment
Definition: shape.h:44
bool GetFilledPolysUseThickness() const
Definition: zone.h:689
int Append(int x, int y, int aOutline=-1, int aHole=-1, bool aAllowDuplication=false)
Add a new vertex to the contour indexed by aOutline and aHole (defaults to the outline of the last po...
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:103
VECTOR2I B
Definition: seg.h:49