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-2020 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 <kicad_string.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 
57 
58 // These variables are parameters used in addTextSegmToContainer.
59 // But addTextSegmToContainer is a call-back function,
60 // so we cannot send them as arguments.
61 static int s_textWidth;
63 static float s_biuTo3Dunits;
64 static const BOARD_ITEM* s_boardItem = nullptr;
65 
66 
67 // This is a call back function, used by GRText to draw the 3D text shape:
68 void addTextSegmToContainer( int x0, int y0, int xf, int yf, void* aData )
69 {
70  const SFVEC2F start3DU( x0 * s_biuTo3Dunits, -y0 * s_biuTo3Dunits );
71  const SFVEC2F end3DU ( xf * s_biuTo3Dunits, -yf * s_biuTo3Dunits );
72 
73  if( Is_segment_a_circle( start3DU, end3DU ) )
75  *s_boardItem) );
76  else
78  *s_boardItem ) );
79 }
80 
81 
82 // Based on
83 // void PCB_TEXT::TransformTextShapeWithClearanceToPolygon
84 // board_items_to_polygon_shape_transform.cpp
86  PCB_LAYER_ID aLayerId, int aClearanceValue )
87 {
88  wxSize size = aText->GetTextSize();
89 
90  if( aText->IsMirrored() )
91  size.x = -size.x;
92 
93  s_boardItem = (const BOARD_ITEM *) &aText;
94  s_dstcontainer = aDstContainer;
95  s_textWidth = aText->GetEffectiveTextPenWidth() + ( 2 * aClearanceValue );
97 
98  // not actually used, but needed by GRText
99  const COLOR4D dummy_color = COLOR4D::BLACK;
100  bool forceBold = true;
101  int penWidth = 0; // force max width for bold
102 
103  if( aText->IsMultilineAllowed() )
104  {
105  wxArrayString strings_list;
106  wxStringSplit( aText->GetShownText(), strings_list, '\n' );
107  std::vector<wxPoint> positions;
108  positions.reserve( strings_list.Count() );
109  aText->GetLinePositions( positions, strings_list.Count() );
110 
111  for( unsigned ii = 0; ii < strings_list.Count(); ++ii )
112  {
113  wxString txt = strings_list.Item( ii );
114 
115  GRText( nullptr, positions[ii], dummy_color, txt, aText->GetTextAngle(), size,
116  aText->GetHorizJustify(), aText->GetVertJustify(), penWidth,
117  aText->IsItalic(), forceBold, addTextSegmToContainer );
118  }
119  }
120  else
121  {
122  GRText( nullptr, aText->GetTextPos(), dummy_color, aText->GetShownText(),
123  aText->GetTextAngle(), size, aText->GetHorizJustify(), aText->GetVertJustify(),
124  penWidth, aText->IsItalic(), forceBold, addTextSegmToContainer );
125  }
126 }
127 
128 
130  CONTAINER_2D_BASE* aDstContainer,
131  PCB_LAYER_ID aLayerId, int aClearanceValue )
132 {
133  addShapeWithClearance( &aDimension->Text(), aDstContainer, aLayerId, aClearanceValue );
134 
135  const int linewidth = aDimension->GetLineThickness() + ( 2 * aClearanceValue );
136 
137  for( const std::shared_ptr<SHAPE>& shape : aDimension->GetShapes() )
138  {
139  switch( shape->Type() )
140  {
141  case SH_SEGMENT:
142  {
143  const SEG& seg = static_cast<const SHAPE_SEGMENT*>( shape.get() )->GetSeg();
144 
145  const SFVEC2F start3DU( seg.A.x * m_biuTo3Dunits, -seg.A.y * m_biuTo3Dunits );
146 
147  const SFVEC2F end3DU( seg.B.x * m_biuTo3Dunits, -seg.B.y * m_biuTo3Dunits );
148 
149  aDstContainer->Add( new ROUND_SEGMENT_2D( start3DU, end3DU, linewidth * m_biuTo3Dunits,
150  *aDimension ) );
151  break;
152  }
153 
154  case SH_CIRCLE:
155  {
156  int radius = static_cast<const SHAPE_CIRCLE*>( shape.get() )->GetRadius();
157  int deltar = aDimension->GetLineThickness();
158 
159  SFVEC2F center( shape->Centre().x * m_biuTo3Dunits,
160  shape->Centre().y * m_biuTo3Dunits );
161 
162  aDstContainer->Add( new RING_2D( center, ( radius - deltar ) * m_biuTo3Dunits,
163  ( radius + deltar ) * m_biuTo3Dunits, *aDimension ) );
164 
165  break;
166  }
167 
168  default:
169  break;
170  }
171 
172  }
173 
174 }
175 
176 
177 // Based on
178 // void FOOTPRINT::TransformFPShapesWithClearanceToPolygonSet
179 // board_items_to_polygon_shape_transform.cpp#L204
181  CONTAINER_2D_BASE* aDstContainer,
182  PCB_LAYER_ID aLayerId, int aInflateValue )
183 {
184  std::vector<FP_TEXT*> texts; // List of FP_TEXT to convert
185  FP_SHAPE* outline;
186 
187  for( BOARD_ITEM* item : aFootprint->GraphicalItems() )
188  {
189  switch( item->Type() )
190  {
191  case PCB_FP_TEXT_T:
192  {
193  FP_TEXT* text = static_cast<FP_TEXT*>( item );
194 
195  if( text->GetLayer() == aLayerId && text->IsVisible() )
196  texts.push_back( text );
197  }
198  break;
199 
200 
201  case PCB_FP_SHAPE_T:
202  {
203  outline = (FP_SHAPE*) item;
204 
205  if( outline->GetLayer() != aLayerId )
206  break;
207 
208  addShapeWithClearance( (const PCB_SHAPE*) outline, aDstContainer, aLayerId, 0 );
209  }
210  break;
211 
212  default:
213  break;
214  }
215  }
216 
217  // Convert texts for footprints
218  if( aFootprint->Reference().GetLayer() == aLayerId && aFootprint->Reference().IsVisible() )
219  texts.push_back( &aFootprint->Reference() );
220 
221  if( aFootprint->Value().GetLayer() == aLayerId && aFootprint->Value().IsVisible() )
222  texts.push_back( &aFootprint->Value() );
223 
224  s_boardItem = (const BOARD_ITEM *)&aFootprint->Value();
225  s_dstcontainer = aDstContainer;
227 
228  for( FP_TEXT* text : texts )
229  {
230  s_textWidth = text->GetEffectiveTextPenWidth() + ( 2 * aInflateValue );
231  wxSize size = text->GetTextSize();
232  bool forceBold = true;
233  int penWidth = 0; // force max width for bold
234 
235  if( text->IsMirrored() )
236  size.x = -size.x;
237 
238  GRText( nullptr, text->GetTextPos(), BLACK, text->GetShownText(), text->GetDrawRotation(),
239  size, text->GetHorizJustify(), text->GetVertJustify(), penWidth, text->IsItalic(),
240  forceBold, addTextSegmToContainer );
241  }
242 }
243 
244 
245 void BOARD_ADAPTER::createTrack( const TRACK* aTrack, CONTAINER_2D_BASE* aDstContainer,
246  int aClearanceValue )
247 {
248  SFVEC2F start3DU( aTrack->GetStart().x * m_biuTo3Dunits,
249  -aTrack->GetStart().y * m_biuTo3Dunits ); // y coord is inverted
250 
251  switch( aTrack->Type() )
252  {
253  case PCB_VIA_T:
254  {
255  const float radius = ( ( aTrack->GetWidth() / 2 ) + aClearanceValue ) * m_biuTo3Dunits;
256  aDstContainer->Add( new FILLED_CIRCLE_2D( start3DU, radius, *aTrack ) );
257  break;
258  }
259 
260  case PCB_ARC_T:
261  {
262  const ARC* arc = static_cast<const ARC*>( aTrack );
263  VECTOR2D center( arc->GetCenter() );
264  double arc_angle = arc->GetAngle();
265  double radius = arc->GetRadius();
266  int arcsegcount = GetArcToSegmentCount( radius, Millimeter2iu( 0.005), arc_angle/10 );
267  int circlesegcount;
268 
269  // We need a circle to segment count. However, the arc angle can be small, and the
270  // radius very big. so we calculate a reasonable value for circlesegcount.
271  if( arcsegcount <= 1 ) // The arc will be approximated by a segment
272  circlesegcount = 1;
273  else
274  {
275  double cnt = arcsegcount * 3600/std::abs( arc_angle );
276 
277 #define SEG_CNT_MAX 128
278  if( cnt < SEG_CNT_MAX )
279  {
280  circlesegcount = (int)cnt;
281 
282  if( circlesegcount == 0 )
283  circlesegcount = 1;
284  }
285  else
286  {
287  circlesegcount = SEG_CNT_MAX;
288  }
289  }
290 
291  transformArcToSegments( wxPoint( center.x, center.y ), arc->GetStart(),
292  arc_angle, circlesegcount,
293  arc->GetWidth() + 2 * aClearanceValue, aDstContainer, *arc );
294  break;
295  }
296 
297  case PCB_TRACE_T: // Track is a usual straight segment
298  {
299  SFVEC2F end3DU( aTrack->GetEnd().x * m_biuTo3Dunits, -aTrack->GetEnd().y * m_biuTo3Dunits );
300 
301  // Cannot add segments that have the same start and end point
302  if( Is_segment_a_circle( start3DU, end3DU ) )
303  {
304  const float radius = ((aTrack->GetWidth() / 2) + aClearanceValue) * m_biuTo3Dunits;
305 
306  aDstContainer->Add( new FILLED_CIRCLE_2D( start3DU, radius, *aTrack ) );
307  }
308  else
309  {
310  const float width = (aTrack->GetWidth() + 2 * aClearanceValue ) * m_biuTo3Dunits;
311 
312  aDstContainer->Add( new ROUND_SEGMENT_2D( start3DU, end3DU, width, *aTrack ) );
313  }
314 
315  break;
316  }
317 
318  default:
319  break;
320  }
321 }
322 
323 
325  PCB_LAYER_ID aLayer, wxSize aClearanceValue ) const
326 {
327  SHAPE_POLY_SET poly;
328 
329  // Our shape-based builder can't handle negative or differing x:y clearance values (the
330  // former are common for solder paste while the later get generated when a relative paste
331  // margin is used with an oblong pad). So we apply this huge hack and fake a larger pad to
332  // run the general-purpose polygon builder on.
333  // Of course being a hack it falls down when dealing with custom shape pads (where the size
334  // is only the size of the anchor), so for those we punt and just use aClearanceValue.x.
335 
336  if( ( aClearanceValue.x < 0 || aClearanceValue.x != aClearanceValue.y )
337  && aPad->GetShape() != PAD_SHAPE_CUSTOM )
338  {
339  PAD dummy( *aPad );
340  dummy.SetSize( aPad->GetSize() + aClearanceValue + aClearanceValue );
341  dummy.TransformShapeWithClearanceToPolygon( poly, aLayer, 0, ARC_HIGH_DEF, ERROR_INSIDE );
342  aClearanceValue = { 0, 0 };
343  }
344  else
345  {
346  auto padShapes = std::static_pointer_cast<SHAPE_COMPOUND>( aPad->GetEffectiveShape() );
347 
348  for( const SHAPE* shape : padShapes->Shapes() )
349  {
350  switch( shape->Type() )
351  {
352  case SH_SEGMENT:
353  {
354  const SHAPE_SEGMENT* seg = (SHAPE_SEGMENT*) shape;
355  const SFVEC2F start3DU( seg->GetSeg().A.x * m_biuTo3Dunits,
356  -seg->GetSeg().A.y * m_biuTo3Dunits );
357  const SFVEC2F end3DU ( seg->GetSeg().B.x * m_biuTo3Dunits,
358  -seg->GetSeg().B.y * m_biuTo3Dunits );
359  const int width = seg->GetWidth() + aClearanceValue.x * 2;
360 
361  // Cannot add segments that have the same start and end point
362  if( Is_segment_a_circle( start3DU, end3DU ) )
363  {
364  aDstContainer->Add( new FILLED_CIRCLE_2D( start3DU,
365  ( width / 2) * m_biuTo3Dunits,
366  *aPad ) );
367  }
368  else
369  {
370  aDstContainer->Add( new ROUND_SEGMENT_2D( start3DU, end3DU,
371  width * m_biuTo3Dunits,
372  *aPad ) );
373  }
374  }
375  break;
376 
377  case SH_CIRCLE:
378  {
379  const SHAPE_CIRCLE* circle = (SHAPE_CIRCLE*) shape;
380  const int radius = circle->GetRadius() + aClearanceValue.x;
381  const SFVEC2F center( circle->GetCenter().x * m_biuTo3Dunits,
382  -circle->GetCenter().y * m_biuTo3Dunits );
383 
384  aDstContainer->Add( new FILLED_CIRCLE_2D( center, radius * m_biuTo3Dunits,
385  *aPad ) );
386  }
387  break;
388 
389  case SH_RECT:
390  {
391  SHAPE_RECT* rect = (SHAPE_RECT*) shape;
392 
393  poly.NewOutline();
394  poly.Append( rect->GetPosition() );
395  poly.Append( rect->GetPosition().x + rect->GetSize().x, rect->GetPosition().y );
396  poly.Append( rect->GetPosition() + rect->GetSize() );
397  poly.Append( rect->GetPosition().x, rect->GetPosition().y + rect->GetSize().y );
398  }
399  break;
400 
401  case SH_SIMPLE:
402  poly.AddOutline( static_cast<const SHAPE_SIMPLE*>( shape )->Vertices() );
403  break;
404 
405  case SH_POLY_SET:
406  poly = *(SHAPE_POLY_SET*) shape;
407  break;
408 
409  default:
410  wxFAIL_MSG( "BOARD_ADAPTER::createPadWithClearance no implementation for "
411  + SHAPE_TYPE_asString( shape->Type() ) );
412  break;
413  }
414  }
415  }
416 
417  if( !poly.IsEmpty() )
418  {
419  if( aClearanceValue.x )
420  poly.Inflate( aClearanceValue.x, 32 );
421 
422  // Add the PAD polygon
423  ConvertPolygonToTriangles( poly, *aDstContainer, m_biuTo3Dunits, *aPad );
424  }
425 }
426 
427 
428 OBJECT_2D* BOARD_ADAPTER::createPadWithDrill( const PAD* aPad, int aInflateValue )
429 {
430  wxSize drillSize = aPad->GetDrillSize();
431 
432  if( !drillSize.x || !drillSize.y )
433  {
434  wxLogTrace( m_logTrace, wxT( "BOARD_ADAPTER::createPadWithDrill - found an invalid pad" ) );
435  return nullptr;
436  }
437 
438  if( drillSize.x == drillSize.y ) // usual round hole
439  {
440  const int radius = ( drillSize.x / 2 ) + aInflateValue;
441 
442  const SFVEC2F center( aPad->GetPosition().x * m_biuTo3Dunits,
443  -aPad->GetPosition().y * m_biuTo3Dunits );
444 
445  return new FILLED_CIRCLE_2D( center, radius * m_biuTo3Dunits, *aPad );
446 
447  }
448  else // Oblong hole
449  {
450  const SHAPE_SEGMENT* seg = aPad->GetEffectiveHoleShape();
451  float width = seg->GetWidth() + aInflateValue * 2;
452 
453  SFVEC2F start3DU( seg->GetSeg().A.x * m_biuTo3Dunits,
454  -seg->GetSeg().A.y * m_biuTo3Dunits );
455 
456  SFVEC2F end3DU ( seg->GetSeg().B.x * m_biuTo3Dunits,
457  -seg->GetSeg().B.y * m_biuTo3Dunits );
458 
459  return new ROUND_SEGMENT_2D( start3DU, end3DU, width * m_biuTo3Dunits, *aPad );
460  }
461 
462  return nullptr;
463 }
464 
465 
467  CONTAINER_2D_BASE* aDstContainer,
468  PCB_LAYER_ID aLayerId, int aInflateValue,
469  bool aSkipNPTHPadsWihNoCopper, bool aSkipPlatedPads,
470  bool aSkipNonPlatedPads )
471 {
472  for( PAD* pad : aFootprint->Pads() )
473  {
474  if( !pad->IsOnLayer( aLayerId ) )
475  continue;
476 
477  // Skip pad annulus when not connected on this layer (if removing is enabled)
478  if( !pad->FlashLayer( aLayerId ) && IsCopperLayer( aLayerId ) )
479  continue;
480 
481  // NPTH pads are not drawn on layers if the
482  // shape size and pos is the same as their hole:
483  if( aSkipNPTHPadsWihNoCopper && ( pad->GetAttribute() == PAD_ATTRIB_NPTH ) )
484  {
485  if( pad->GetDrillSize() == pad->GetSize() && pad->GetOffset() == wxPoint( 0, 0 ) )
486  {
487  switch( pad->GetShape() )
488  {
489  case PAD_SHAPE_CIRCLE:
490  if( pad->GetDrillShape() == PAD_DRILL_SHAPE_CIRCLE )
491  continue;
492  break;
493 
494  case PAD_SHAPE_OVAL:
495  if( pad->GetDrillShape() != PAD_DRILL_SHAPE_CIRCLE )
496  continue;
497  break;
498 
499  default:
500  break;
501  }
502  }
503  }
504 
505  const bool isPlated = ( ( aLayerId == F_Cu ) && pad->FlashLayer( F_Mask ) ) ||
506  ( ( aLayerId == B_Cu ) && pad->FlashLayer( B_Mask ) );
507 
508  if( aSkipPlatedPads && isPlated )
509  continue;
510 
511  if( aSkipNonPlatedPads && !isPlated )
512  continue;
513 
514  wxSize margin( aInflateValue, aInflateValue );
515 
516  switch( aLayerId )
517  {
518  case F_Mask:
519  case B_Mask:
520  margin.x += pad->GetSolderMaskMargin();
521  margin.y += pad->GetSolderMaskMargin();
522  break;
523 
524  case F_Paste:
525  case B_Paste:
526  margin += pad->GetSolderPasteMargin();
527  break;
528 
529  default:
530  break;
531  }
532 
533  createPadWithClearance( pad, aDstContainer, aLayerId, margin );
534  }
535 }
536 
537 
538 // based on TransformArcToPolygon function from
539 // common/convert_basic_shapes_to_polygon.cpp
540 void BOARD_ADAPTER::transformArcToSegments( const wxPoint& aCentre, const wxPoint& aStart,
541  double aArcAngle, int aCircleToSegmentsCount,
542  int aWidth, CONTAINER_2D_BASE* aDstContainer,
543  const BOARD_ITEM& aBoardItem )
544 {
545  wxPoint arc_start, arc_end;
546  int delta = 3600 / aCircleToSegmentsCount; // rotate angle in 0.1 degree
547 
548  arc_end = arc_start = aStart;
549 
550  if( aArcAngle != 3600 )
551  {
552  RotatePoint( &arc_end, aCentre, -aArcAngle );
553  }
554 
555  if( aArcAngle < 0 )
556  {
557  std::swap( arc_start, arc_end );
558  aArcAngle = -aArcAngle;
559  }
560 
561  // Compute the ends of segments and creates poly
562  wxPoint curr_end = arc_start;
563  wxPoint curr_start = arc_start;
564 
565  for( int ii = delta; ii < aArcAngle; ii += delta )
566  {
567  curr_end = arc_start;
568  RotatePoint( &curr_end, aCentre, -ii );
569 
570  const SFVEC2F start3DU( curr_start.x * m_biuTo3Dunits, -curr_start.y * m_biuTo3Dunits );
571  const SFVEC2F end3DU ( curr_end.x * m_biuTo3Dunits, -curr_end.y * m_biuTo3Dunits );
572 
573  if( Is_segment_a_circle( start3DU, end3DU ) )
574  {
575  aDstContainer->Add( new FILLED_CIRCLE_2D( start3DU, ( aWidth / 2 ) * m_biuTo3Dunits,
576  aBoardItem ) );
577  }
578  else
579  {
580  aDstContainer->Add( new ROUND_SEGMENT_2D( start3DU, end3DU, aWidth * m_biuTo3Dunits,
581  aBoardItem ) );
582  }
583 
584  curr_start = curr_end;
585  }
586 
587  if( curr_end != arc_end )
588  {
589  const SFVEC2F start3DU( curr_end.x * m_biuTo3Dunits, -curr_end.y * m_biuTo3Dunits );
590  const SFVEC2F end3DU ( arc_end.x * m_biuTo3Dunits, -arc_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 }
604 
605 
606 // Based on
607 // TransformShapeWithClearanceToPolygon
608 // board_items_to_polygon_shape_transform.cpp#L431
610  CONTAINER_2D_BASE* aDstContainer, PCB_LAYER_ID aLayerId,
611  int aClearanceValue )
612 {
613  // The full width of the lines to create
614  // The extra 1 protects the inner/outer radius values from degeneracy
615  const int linewidth = aShape->GetWidth() + ( 2 * aClearanceValue ) + 1;
616 
617  switch( aShape->GetShape() )
618  {
619  case S_CIRCLE:
620  {
621  const SFVEC2F center3DU( aShape->GetCenter().x * m_biuTo3Dunits,
622  -aShape->GetCenter().y * m_biuTo3Dunits );
623 
624  float inner_radius = ( aShape->GetRadius() - linewidth / 2 ) * m_biuTo3Dunits;
625  float outer_radius = ( aShape->GetRadius() + linewidth / 2 ) * m_biuTo3Dunits;
626 
627  if( inner_radius < 0 )
628  inner_radius = 0;
629 
630  if( aShape->IsFilled() )
631  aDstContainer->Add( new FILLED_CIRCLE_2D( center3DU, outer_radius, *aShape ) );
632  else
633  aDstContainer->Add( new RING_2D( center3DU, inner_radius, outer_radius, *aShape ) );
634  }
635  break;
636 
637  case S_RECT:
638  if( aShape->IsFilled() )
639  {
640  SHAPE_POLY_SET polyList;
641 
642  aShape->TransformShapeWithClearanceToPolygon( polyList, aLayerId, 0,
643  ARC_HIGH_DEF, ERROR_INSIDE );
644 
645  polyList.Simplify( SHAPE_POLY_SET::PM_FAST );
646 
647  ConvertPolygonToTriangles( polyList, *aDstContainer, m_biuTo3Dunits, *aShape );
648  }
649  else
650  {
651  std::vector<wxPoint> pts = aShape->GetRectCorners();
652 
653  const SFVEC2F topLeft3DU( pts[0].x * m_biuTo3Dunits, -pts[0].y * m_biuTo3Dunits );
654  const SFVEC2F topRight3DU( pts[1].x * m_biuTo3Dunits, -pts[1].y * m_biuTo3Dunits );
655  const SFVEC2F botRight3DU( pts[2].x * m_biuTo3Dunits, -pts[2].y * m_biuTo3Dunits );
656  const SFVEC2F botLeft3DU( pts[3].x * m_biuTo3Dunits, -pts[3].y * m_biuTo3Dunits );
657 
658  aDstContainer->Add( new ROUND_SEGMENT_2D( topLeft3DU, topRight3DU,
659  linewidth * m_biuTo3Dunits, *aShape ) );
660  aDstContainer->Add( new ROUND_SEGMENT_2D( topRight3DU, botRight3DU,
661  linewidth * m_biuTo3Dunits, *aShape ) );
662  aDstContainer->Add( new ROUND_SEGMENT_2D( botRight3DU, botLeft3DU,
663  linewidth * m_biuTo3Dunits, *aShape ) );
664  aDstContainer->Add( new ROUND_SEGMENT_2D( botLeft3DU, topLeft3DU,
665  linewidth * m_biuTo3Dunits, *aShape ) );
666  }
667  break;
668 
669  case S_ARC:
670  {
671  unsigned int segCount = GetCircleSegmentCount( aShape->GetBoundingBox().GetSizeMax() );
672 
673  transformArcToSegments( aShape->GetCenter(), aShape->GetArcStart(), aShape->GetAngle(),
674  segCount, linewidth, aDstContainer, *aShape );
675  }
676  break;
677 
678  case S_SEGMENT:
679  {
680  const SFVEC2F start3DU( aShape->GetStart().x * m_biuTo3Dunits,
681  -aShape->GetStart().y * m_biuTo3Dunits );
682 
683  const SFVEC2F end3DU ( aShape->GetEnd().x * m_biuTo3Dunits,
684  -aShape->GetEnd().y * m_biuTo3Dunits );
685 
686  if( Is_segment_a_circle( start3DU, end3DU ) )
687  {
688  aDstContainer->Add( new FILLED_CIRCLE_2D( start3DU, ( linewidth / 2 ) * m_biuTo3Dunits,
689  *aShape ) );
690  }
691  else
692  {
693  aDstContainer->Add( new ROUND_SEGMENT_2D( start3DU, end3DU, linewidth * m_biuTo3Dunits,
694  *aShape ) );
695  }
696  }
697  break;
698 
699  case S_CURVE:
700  case S_POLYGON:
701  {
702  SHAPE_POLY_SET polyList;
703 
704  aShape->TransformShapeWithClearanceToPolygon( polyList, aLayerId, 0,
705  ARC_HIGH_DEF, ERROR_INSIDE );
706 
707  polyList.Simplify( SHAPE_POLY_SET::PM_FAST );
708 
709  if( polyList.IsEmpty() ) // Just for caution
710  break;
711 
712  ConvertPolygonToTriangles( polyList, *aDstContainer, m_biuTo3Dunits, *aShape );
713  }
714  break;
715 
716  default:
717  wxFAIL_MSG( "BOARD_ADAPTER::addShapeWithClearance no implementation for "
718  + PCB_SHAPE_TYPE_T_asString( aShape->GetShape() ) );
719  break;
720  }
721 }
722 
723 
724 // Based on
725 // TransformSolidAreasShapesToPolygonSet
726 // board_items_to_polygon_shape_transform.cpp
727 void BOARD_ADAPTER::addSolidAreasShapes( const ZONE* aZoneContainer,
728  CONTAINER_2D_BASE* aDstContainer, PCB_LAYER_ID aLayerId )
729 {
730  // Copy the polys list because we have to simplify it
731  SHAPE_POLY_SET polyList = SHAPE_POLY_SET( aZoneContainer->GetFilledPolysList( aLayerId ) );
732 
733  // This convert the poly in outline and holes
734  ConvertPolygonToTriangles( polyList, *aDstContainer, m_biuTo3Dunits, *aZoneContainer );
735 
736  // add filled areas outlines, which are drawn with thick lines segments
737  // but only if filled polygons outlines have thickness
738  if( !aZoneContainer->GetFilledPolysUseThickness() )
739  return;
740 
741  float line_thickness = aZoneContainer->GetMinThickness() * m_biuTo3Dunits;
742 
743  for( int i = 0; i < polyList.OutlineCount(); ++i )
744  {
745  // Add outline
746  const SHAPE_LINE_CHAIN& pathOutline = polyList.COutline( i );
747 
748  for( int j = 0; j < pathOutline.PointCount(); ++j )
749  {
750  const VECTOR2I& a = pathOutline.CPoint( j );
751  const VECTOR2I& b = pathOutline.CPoint( j + 1 );
752 
753  SFVEC2F start3DU( a.x * m_biuTo3Dunits, -a.y * m_biuTo3Dunits );
754  SFVEC2F end3DU ( b.x * m_biuTo3Dunits, -b.y * m_biuTo3Dunits );
755 
756  if( Is_segment_a_circle( start3DU, end3DU ) )
757  {
758  float radius = line_thickness/2;
759 
760  if( radius > 0.0 ) // degenerated circles crash 3D viewer
761  aDstContainer->Add( new FILLED_CIRCLE_2D( start3DU, radius, *aZoneContainer ) );
762  }
763  else
764  {
765  aDstContainer->Add( new ROUND_SEGMENT_2D( start3DU, end3DU, line_thickness,
766  *aZoneContainer ) );
767  }
768  }
769 
770  // Add holes (of the poly, ie: the open parts) for this outline
771  for( int h = 0; h < polyList.HoleCount( i ); ++h )
772  {
773  const SHAPE_LINE_CHAIN& pathHole = polyList.CHole( i, h );
774 
775  for( int j = 0; j < pathHole.PointCount(); j++ )
776  {
777  const VECTOR2I& a = pathHole.CPoint( j );
778  const VECTOR2I& b = pathHole.CPoint( j + 1 );
779 
780  SFVEC2F start3DU( a.x * m_biuTo3Dunits, -a.y * m_biuTo3Dunits );
781  SFVEC2F end3DU ( b.x * m_biuTo3Dunits, -b.y * m_biuTo3Dunits );
782 
783  if( Is_segment_a_circle( start3DU, end3DU ) )
784  {
785  float radius = line_thickness/2;
786 
787  if( radius > 0.0 ) // degenerated circles crash 3D viewer
788  aDstContainer->Add( new FILLED_CIRCLE_2D( start3DU, radius,
789  *aZoneContainer ) );
790  }
791  else
792  {
793  aDstContainer->Add( new ROUND_SEGMENT_2D( start3DU, end3DU, line_thickness,
794  *aZoneContainer ) );
795  }
796  }
797  }
798  }
799 }
800 
801 
803  int aWidth )
804 {
805  if( aPad->GetShape() == PAD_SHAPE_CIRCLE ) // Draw a ring
806  {
807  const SFVEC2F center3DU( aPad->ShapePos().x * m_biuTo3Dunits,
808  -aPad->ShapePos().y * m_biuTo3Dunits );
809 
810  const int radius = aPad->GetSize().x / 2;
811  const float inner_radius = ( radius - aWidth / 2 ) * m_biuTo3Dunits;
812  const float outer_radius = ( radius + aWidth / 2 ) * m_biuTo3Dunits;
813 
814  aDstContainer->Add( new RING_2D( center3DU, inner_radius, outer_radius, *aPad ) );
815 
816  return;
817  }
818 
819  // For other shapes, add outlines as thick segments in polygon buffer
820  const std::shared_ptr<SHAPE_POLY_SET>& corners = aPad->GetEffectivePolygon();
821  const SHAPE_LINE_CHAIN& path = corners->COutline( 0 );
822 
823  for( int j = 0; j < path.PointCount(); j++ )
824  {
825  const VECTOR2I& a = path.CPoint( j );
826  const VECTOR2I& b = path.CPoint( j + 1 );
827 
828  SFVEC2F start3DU( a.x * m_biuTo3Dunits, -a.y * m_biuTo3Dunits );
829  SFVEC2F end3DU ( b.x * m_biuTo3Dunits, -b.y * m_biuTo3Dunits );
830 
831  if( Is_segment_a_circle( start3DU, end3DU ) )
832  {
833  aDstContainer->Add( new FILLED_CIRCLE_2D( start3DU, ( aWidth / 2 ) * m_biuTo3Dunits,
834  *aPad ) );
835  }
836  else
837  {
838  aDstContainer->Add( new ROUND_SEGMENT_2D( start3DU, end3DU, aWidth * m_biuTo3Dunits,
839  *aPad ) );
840  }
841  }
842 }
virtual wxPoint GetCenter() const override
This defaults to the center of the bounding box if not overridden.
Definition: track.h:301
bool IsFilled() const
Definition: pcb_shape.h:96
void createTrack(const TRACK *aTrack, CONTAINER_2D_BASE *aDstContainer, int aClearanceValue)
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:206
bool IsMirrored() const
Definition: eda_text.h:196
class FP_TEXT, text in a footprint
Definition: typeinfo.h:92
void createPadWithClearance(const PAD *aPad, CONTAINER_2D_BASE *aDstContainer, PCB_LAYER_ID aLayer, wxSize aClearanceValue) const
int GetRadius() const
Definition: shape_circle.h:102
const wxPoint & GetEnd() const
Function GetEnd returns the ending point of the graphic.
Definition: pcb_shape.h:156
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:82
#define SEG_CNT_MAX
OBJECT_2D * createPadWithDrill(const PAD *aPad, int aInflateValue)
const wxPoint & GetStart() const
Definition: track.h:116
bool IsVisible() const
Definition: eda_text.h:193
int GetRadius() const
Function GetRadius returns the radius of this item Has meaning only for arc and circle.
Definition: pcb_shape.h:201
bool IsEmpty() const
std::vector< wxPoint > GetRectCorners() const
Definition: pcb_shape.cpp:970
int GetWidth() const
Definition: pcb_shape.h:118
void transformArcToSegments(const wxPoint &aCentre, const wxPoint &aStart, double aArcAngle, int aCircleToSegmentsCount, int aWidth, CONTAINER_2D_BASE *aDstContainer, const BOARD_ITEM &aBoardItem)
polygon (not yet used for tracks, but could be in microwave apps)
Definition: board_item.h:54
static void isPlated(LIBEVAL::CONTEXT *aCtx, void *self)
double GetTextAngle() const
Definition: eda_text.h:181
class ARC, an arc track segment on a copper layer
Definition: typeinfo.h:97
usual segment : line with rounded ends
Definition: board_item.h:50
static LIB_PART * dummy()
Used to draw a dummy shape when a LIB_PART is not found in library.
Definition: sch_symbol.cpp:69
const SHAPE_SEGMENT * GetEffectiveHoleShape() const
Return a SHAPE object representing the pad's hole.
Definition: pcbnew/pad.cpp:286
static CONTAINER_2D_BASE * s_dstcontainer
Arcs (with rounded ends)
Definition: board_item.h:52
class FP_SHAPE, a footprint edge
Definition: typeinfo.h:93
const SHAPE_POLY_SET & GetFilledPolysList(PCB_LAYER_ID aLayer) const
Function GetFilledPolysList returns a reference to the list of filled polygons.
Definition: zone.h:655
PAD_SHAPE_T GetShape() const
Definition: pad.h:169
const SHAPE_LINE_CHAIN & CHole(int aOutline, int aHole) const
const VECTOR2I GetCenter() const
Definition: shape_circle.h:107
Definition: color4d.h:44
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:228
void Inflate(int aAmount, int aCircleSegmentsCount, CORNER_STRATEGY aCornerStrategy=ROUND_ALL_CORNERS)
Perform outline inflation/deflation.
like PAD_PTH, but not plated mechanical use only, no connection allowed
Definition: pad_shapes.h:85
segment with non rounded ends
Definition: board_item.h:51
void addFootprintShapesWithClearance(const FOOTPRINT *aFootprint, CONTAINER_2D_BASE *aDstContainer, PCB_LAYER_ID aLayerId, int aInflateValue)
int PointCount() const
Function PointCount()
const SEG & GetSeg() const
class 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:157
PADS & Pads()
Definition: footprint.h:164
wxPoint GetArcStart() const
Definition: pcb_shape.h:179
FP_TEXT & Value()
read/write accessors:
Definition: footprint.h:459
int GetLineThickness() const
Definition: dimension.h:187
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:187
FP_TEXT & Reference()
Definition: footprint.h:460
wxString GetShownText(int aDepth=0) const override
Return the string actually shown after processing of the base text.
Definition: pcb_text.cpp:52
const VECTOR2I & CPoint(int aIndex) const
Function Point()
const wxSize & GetDrillSize() const
Definition: pad.h:242
wxPoint ShapePos() const
Definition: pcbnew/pad.cpp:657
PCB_LAYER_ID
A quick note on layer IDs:
void addShapeWithClearance(const PCB_TEXT *aText, CONTAINER_2D_BASE *aDstContainer, PCB_LAYER_ID aLayerId, int aClearanceValue)
const std::vector< std::shared_ptr< SHAPE > > & GetShapes() const
Definition: dimension.h:215
EDA_TEXT_HJUSTIFY_T GetHorizJustify() const
Definition: eda_text.h:205
int GetMinThickness() const
Definition: zone.h:247
glm::vec2 SFVEC2F
Definition: xv3d_types.h:42
static float s_biuTo3Dunits
Represent a set of closed polygons.
const VECTOR2I & GetPosition() const
Definition: shape_rect.h:116
const wxSize & GetTextSize() const
Definition: eda_text.h:245
static wxString PCB_SHAPE_TYPE_T_asString(PCB_SHAPE_TYPE_T a)
Definition: board_item.h:59
const wxSize & GetSize() const
Definition: pad.h:232
PCB_TEXT & Text()
Definition: dimension.h:209
wxPoint GetCenter() const override
This defaults to the center of the bounding box if not overridden.
Definition: pcb_shape.cpp:341
void addSolidAreasShapes(const ZONE *aZoneContainer, CONTAINER_2D_BASE *aDstContainer, PCB_LAYER_ID aLayerId)
const wxPoint & GetStart() const
Function GetStart returns the starting point of the graphic.
Definition: pcb_shape.h:145
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.
static wxString SHAPE_TYPE_asString(SHAPE_TYPE a)
Definition: shape.h:55
DRAWINGS & GraphicalItems()
Definition: footprint.h:167
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: pcbnew/pad.cpp:277
a few functions useful in geometry calculations.
void Simplify(POLYGON_MODE aFastMode)
ZONE handles a list of polygons defining a copper zone.
Definition: zone.h:57
An abstract shape on 2D plane.
Definition: shape.h:116
double GetRadius() const
Definition: track.cpp:922
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.
bool IsMultilineAllowed() const
Definition: eda_text.h:203
circle
Definition: shape.h:46
void wxStringSplit(const wxString &aText, wxArrayString &aStrings, wxChar aSplitter)
Split aString to a string list separated at aSplitter.
Definition: string.cpp:807
void GRText(wxDC *aDC, const wxPoint &aPos, 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)
Function GRText Draw a graphic text (like footprint texts)
Definition: gr_text.cpp:131
Definition: seg.h:41
int AddOutline(const SHAPE_LINE_CHAIN &aOutline)
Adds a new hole to the given outline (default: last) and returns its index.
double GetAngle() const
Definition: track.cpp:928
set of polygons (with holes, etc.)
Definition: shape.h:48
int GetWidth() const
Definition: track.h:110
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:177
Definition: track.h:262
SHAPE_LINE_CHAIN.
const SHAPE_LINE_CHAIN & COutline(int aIndex) const
const EDA_RECT GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
Definition: pcb_shape.cpp:568
VECTOR2I A
Definition: seg.h:49
double GetAngle() const
Definition: pcb_shape.h:127
const std::shared_ptr< SHAPE_POLY_SET > & GetEffectivePolygon() const
Definition: pcbnew/pad.cpp:268
const wxPoint & GetEnd() const
Definition: track.h:113
void GetLinePositions(std::vector< wxPoint > &aPositions, int aLineCount) const
Populate aPositions with the position of each line of a multiline text, according to the vertical jus...
Definition: eda_text.cpp:424
bool IsCopperLayer(LAYER_NUM aLayerId)
Tests whether a layer is a copper layer.
ring
Definition: board_item.h:53
void addTextSegmToContainer(int x0, int y0, int xf, int yf, void *aData)
const wxPoint & GetTextPos() const
Definition: eda_text.h:254
class VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:96
axis-aligned rectangle
Definition: shape.h:43
PCB_SHAPE_TYPE_T GetShape() const
Definition: pcb_shape.h:130
static const BOARD_ITEM * s_boardItem
Definition: pad.h:60
void TransformShapeWithClearanceToPolygon(SHAPE_POLY_SET &aCornerBuffer, PCB_LAYER_ID aLayer, int aClearanceValue, int aError, ERROR_LOC aErrorLoc, bool ignoreLineWidth=false) const override
Function TransformShapeWithClearanceToPolygon Convert the draw segment to a closed polygon Used in fi...
Abstract dimension API.
Definition: dimension.h:95
int GetWidth() const
Bezier Curve.
Definition: board_item.h:55
static constexpr int Millimeter2iu(double mm)
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Definition: board_item.h:173
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:101
Definition: track.h:83
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:162
line segment
Definition: shape.h:44
bool GetFilledPolysUseThickness() const
Definition: zone.h:712
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:98
VECTOR2I B
Definition: seg.h:50