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 <[email protected]>
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/raytracing/shapes2D/ring_2d.h"
34 #include "../3d_rendering/raytracing/shapes2D/filled_circle_2d.h"
35 #include "../3d_rendering/raytracing/shapes2D/round_segment_2d.h"
36 #include "../3d_rendering/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  wxSize dummySize = aPad->GetSize() + clearance + clearance;
326 
327  if( dummySize.x <= 0 || dummySize.y <= 0 )
328  return;
329 
330  PAD dummy( *aPad );
331  dummy.SetSize( dummySize );
332  dummy.TransformShapeWithClearanceToPolygon( poly, aLayer, 0, ARC_HIGH_DEF, ERROR_INSIDE );
333  clearance = { 0, 0 };
334  }
335  else
336  {
337  auto padShapes = std::static_pointer_cast<SHAPE_COMPOUND>( aPad->GetEffectiveShape() );
338 
339  for( const SHAPE* shape : padShapes->Shapes() )
340  {
341  switch( shape->Type() )
342  {
343  case SH_SEGMENT:
344  {
345  const SHAPE_SEGMENT* seg = (SHAPE_SEGMENT*) shape;
346  const SFVEC2F start3DU( seg->GetSeg().A.x * m_biuTo3Dunits,
347  -seg->GetSeg().A.y * m_biuTo3Dunits );
348  const SFVEC2F end3DU ( seg->GetSeg().B.x * m_biuTo3Dunits,
349  -seg->GetSeg().B.y * m_biuTo3Dunits );
350  const double width = seg->GetWidth() + clearance.x * 2;
351 
352  // Cannot add segments that have the same start and end point
353  if( Is_segment_a_circle( start3DU, end3DU ) )
354  {
355  aDstContainer->Add( new FILLED_CIRCLE_2D( start3DU,
356  ( width / 2 ) * m_biuTo3Dunits,
357  *aPad ) );
358  }
359  else
360  {
361  aDstContainer->Add( new ROUND_SEGMENT_2D( start3DU, end3DU,
362  width * m_biuTo3Dunits,
363  *aPad ) );
364  }
365  }
366  break;
367 
368  case SH_CIRCLE:
369  {
370  const SHAPE_CIRCLE* circle = (SHAPE_CIRCLE*) shape;
371  const double radius = circle->GetRadius() + clearance.x;
372  const SFVEC2F center( circle->GetCenter().x * m_biuTo3Dunits,
373  -circle->GetCenter().y * m_biuTo3Dunits );
374 
375  aDstContainer->Add( new FILLED_CIRCLE_2D( center, radius * m_biuTo3Dunits,
376  *aPad ) );
377  }
378  break;
379 
380  case SH_RECT:
381  {
382  SHAPE_RECT* rect = (SHAPE_RECT*) shape;
383 
384  poly.NewOutline();
385  poly.Append( rect->GetPosition() );
386  poly.Append( rect->GetPosition().x + rect->GetSize().x, rect->GetPosition().y );
387  poly.Append( rect->GetPosition() + rect->GetSize() );
388  poly.Append( rect->GetPosition().x, rect->GetPosition().y + rect->GetSize().y );
389  }
390  break;
391 
392  case SH_SIMPLE:
393  poly.AddOutline( static_cast<const SHAPE_SIMPLE*>( shape )->Vertices() );
394  break;
395 
396  case SH_POLY_SET:
397  poly = *(SHAPE_POLY_SET*) shape;
398  break;
399 
400  case SH_ARC:
401  {
402  SHAPE_ARC* arc = (SHAPE_ARC*) shape;
404 
405  for( int i = 0; i < l.SegmentCount(); i++ )
406  {
407  SHAPE_SEGMENT seg( l.Segment( i ).A, l.Segment( i ).B, arc->GetWidth() );
408  const SFVEC2F start3DU( seg.GetSeg().A.x * m_biuTo3Dunits,
409  -seg.GetSeg().A.y * m_biuTo3Dunits );
410  const SFVEC2F end3DU( seg.GetSeg().B.x * m_biuTo3Dunits,
411  -seg.GetSeg().B.y * m_biuTo3Dunits );
412  const double width = arc->GetWidth() + clearance.x * 2;
413 
414  // Cannot add segments that have the same start and end point
415  if( Is_segment_a_circle( start3DU, end3DU ) )
416  {
417  aDstContainer->Add( new FILLED_CIRCLE_2D( start3DU,
418  ( width / 2 ) * m_biuTo3Dunits,
419  *aPad ) );
420  }
421  else
422  {
423  aDstContainer->Add( new ROUND_SEGMENT_2D( start3DU, end3DU,
424  width * m_biuTo3Dunits,
425  *aPad ) );
426  }
427  }
428  }
429  break;
430 
431  default:
432  wxFAIL_MSG( wxT( "BOARD_ADAPTER::createPadWithClearance no implementation for " )
433  + SHAPE_TYPE_asString( shape->Type() ) );
434  break;
435  }
436  }
437  }
438 
439  if( !poly.IsEmpty() )
440  {
441  if( clearance.x )
442  poly.Inflate( clearance.x, 32 );
443 
444  // Add the PAD polygon
445  ConvertPolygonToTriangles( poly, *aDstContainer, m_biuTo3Dunits, *aPad );
446  }
447 }
448 
449 
450 OBJECT_2D* BOARD_ADAPTER::createPadWithDrill( const PAD* aPad, int aInflateValue )
451 {
452  wxSize drillSize = aPad->GetDrillSize();
453 
454  if( !drillSize.x || !drillSize.y )
455  {
456  wxLogTrace( m_logTrace, wxT( "BOARD_ADAPTER::createPadWithDrill - found an invalid pad" ) );
457  return nullptr;
458  }
459 
460  if( drillSize.x == drillSize.y ) // usual round hole
461  {
462  const double radius = ( drillSize.x / 2.0 ) + aInflateValue;
463 
464  const SFVEC2F center( aPad->GetPosition().x * m_biuTo3Dunits,
465  -aPad->GetPosition().y * m_biuTo3Dunits );
466 
467  return new FILLED_CIRCLE_2D( center, radius * m_biuTo3Dunits, *aPad );
468 
469  }
470  else // Oblong hole
471  {
472  const SHAPE_SEGMENT* seg = aPad->GetEffectiveHoleShape();
473  double width = seg->GetWidth() + aInflateValue * 2;
474 
475  SFVEC2F start3DU( seg->GetSeg().A.x * m_biuTo3Dunits,
476  -seg->GetSeg().A.y * m_biuTo3Dunits );
477 
478  SFVEC2F end3DU ( seg->GetSeg().B.x * m_biuTo3Dunits,
479  -seg->GetSeg().B.y * m_biuTo3Dunits );
480 
481  return new ROUND_SEGMENT_2D( start3DU, end3DU, width * m_biuTo3Dunits, *aPad );
482  }
483 
484  return nullptr;
485 }
486 
487 
489  CONTAINER_2D_BASE* aDstContainer,
490  PCB_LAYER_ID aLayerId, int aInflateValue,
491  bool aSkipNPTHPadsWihNoCopper, bool aSkipPlatedPads,
492  bool aSkipNonPlatedPads )
493 {
494  for( PAD* pad : aFootprint->Pads() )
495  {
496  if( !pad->FlashLayer( aLayerId ) )
497  continue;
498 
499  // NPTH pads are not drawn on layers if the 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  wxSize margin( aInflateValue, aInflateValue );
525 
526  switch( aLayerId )
527  {
528  case F_Cu:
529  if( aSkipPlatedPads && pad->FlashLayer( F_Mask ) )
530  continue;
531 
532  if( aSkipNonPlatedPads && !pad->FlashLayer( F_Mask ) )
533  continue;
534 
535  break;
536 
537  case B_Cu:
538  if( aSkipPlatedPads && pad->FlashLayer( B_Mask ) )
539  continue;
540 
541  if( aSkipNonPlatedPads && !pad->FlashLayer( B_Mask ) )
542  continue;
543 
544  break;
545 
546  case F_Mask:
547  case B_Mask:
548  margin.x += pad->GetSolderMaskMargin();
549  margin.y += pad->GetSolderMaskMargin();
550  break;
551 
552  case F_Paste:
553  case B_Paste:
554  margin += pad->GetSolderPasteMargin();
555  break;
556 
557  default:
558  break;
559  }
560 
561  createPadWithClearance( pad, aDstContainer, aLayerId, margin );
562  }
563 }
564 
565 
566 // based on TransformArcToPolygon function from
567 // common/convert_basic_shapes_to_polygon.cpp
568 void BOARD_ADAPTER::transformArcToSegments( const wxPoint& aCentre, const wxPoint& aStart,
569  double aArcAngle, int aCircleToSegmentsCount,
570  int aWidth, CONTAINER_2D_BASE* aDstContainer,
571  const BOARD_ITEM& aBoardItem )
572 {
573  wxPoint arc_start, arc_end;
574  double delta = 3600 / aCircleToSegmentsCount; // rotate angle in 0.1 degree
575 
576  arc_end = arc_start = aStart;
577 
578  if( aArcAngle != 3600 )
579  {
580  RotatePoint( &arc_end, aCentre, -aArcAngle );
581  }
582 
583  if( aArcAngle < 0 )
584  {
585  std::swap( arc_start, arc_end );
586  aArcAngle = -aArcAngle;
587  }
588 
589  // Compute the ends of segments and creates poly
590  wxPoint curr_end = arc_start;
591  wxPoint curr_start = arc_start;
592 
593  for( double ii = delta; ii < aArcAngle; ii += delta )
594  {
595  curr_end = arc_start;
596  RotatePoint( &curr_end, aCentre, -ii );
597 
598  const SFVEC2F start3DU( curr_start.x * m_biuTo3Dunits, -curr_start.y * m_biuTo3Dunits );
599  const SFVEC2F end3DU ( curr_end.x * m_biuTo3Dunits, -curr_end.y * m_biuTo3Dunits );
600 
601  if( Is_segment_a_circle( start3DU, end3DU ) )
602  {
603  aDstContainer->Add( new FILLED_CIRCLE_2D( start3DU, ( aWidth / 2 ) * m_biuTo3Dunits,
604  aBoardItem ) );
605  }
606  else
607  {
608  aDstContainer->Add( new ROUND_SEGMENT_2D( start3DU, end3DU, aWidth * m_biuTo3Dunits,
609  aBoardItem ) );
610  }
611 
612  curr_start = curr_end;
613  }
614 
615  if( curr_end != arc_end )
616  {
617  const SFVEC2F start3DU( curr_end.x * m_biuTo3Dunits, -curr_end.y * m_biuTo3Dunits );
618  const SFVEC2F end3DU ( arc_end.x * m_biuTo3Dunits, -arc_end.y * m_biuTo3Dunits );
619 
620  if( Is_segment_a_circle( start3DU, end3DU ) )
621  {
622  aDstContainer->Add( new FILLED_CIRCLE_2D( start3DU, ( aWidth / 2 ) * m_biuTo3Dunits,
623  aBoardItem ) );
624  }
625  else
626  {
627  aDstContainer->Add( new ROUND_SEGMENT_2D( start3DU, end3DU, aWidth * m_biuTo3Dunits,
628  aBoardItem ) );
629  }
630  }
631 }
632 
633 
634 // Based on
635 // TransformShapeWithClearanceToPolygon
636 // board_items_to_polygon_shape_transform.cpp#L431
638  CONTAINER_2D_BASE* aDstContainer, PCB_LAYER_ID aLayerId,
639  int aClearanceValue )
640 {
641  // The full width of the lines to create
642  // The extra 1 protects the inner/outer radius values from degeneracy
643  const double linewidth = aShape->GetWidth() + ( 2 * aClearanceValue ) + 1;
644 
645  switch( aShape->GetShape() )
646  {
647  case SHAPE_T::CIRCLE:
648  {
649  const SFVEC2F center3DU( aShape->GetCenter().x * m_biuTo3Dunits,
650  -aShape->GetCenter().y * m_biuTo3Dunits );
651 
652  float inner_radius = ( aShape->GetRadius() - linewidth / 2 ) * m_biuTo3Dunits;
653  float outer_radius = ( aShape->GetRadius() + linewidth / 2 ) * m_biuTo3Dunits;
654 
655  if( inner_radius < 0 )
656  inner_radius = 0;
657 
658  if( aShape->IsFilled() )
659  aDstContainer->Add( new FILLED_CIRCLE_2D( center3DU, outer_radius, *aShape ) );
660  else
661  aDstContainer->Add( new RING_2D( center3DU, inner_radius, outer_radius, *aShape ) );
662  }
663  break;
664 
665  case SHAPE_T::RECT:
666  if( aShape->IsFilled() )
667  {
668  SHAPE_POLY_SET polyList;
669 
670  aShape->TransformShapeWithClearanceToPolygon( polyList, aLayerId, 0,
671  ARC_HIGH_DEF, ERROR_INSIDE );
672 
673  polyList.Simplify( SHAPE_POLY_SET::PM_FAST );
674 
675  ConvertPolygonToTriangles( polyList, *aDstContainer, m_biuTo3Dunits, *aShape );
676  }
677  else
678  {
679  std::vector<wxPoint> pts = aShape->GetRectCorners();
680 
681  const SFVEC2F topLeft3DU( pts[0].x * m_biuTo3Dunits, -pts[0].y * m_biuTo3Dunits );
682  const SFVEC2F topRight3DU( pts[1].x * m_biuTo3Dunits, -pts[1].y * m_biuTo3Dunits );
683  const SFVEC2F botRight3DU( pts[2].x * m_biuTo3Dunits, -pts[2].y * m_biuTo3Dunits );
684  const SFVEC2F botLeft3DU( pts[3].x * m_biuTo3Dunits, -pts[3].y * m_biuTo3Dunits );
685 
686  aDstContainer->Add( new ROUND_SEGMENT_2D( topLeft3DU, topRight3DU,
687  linewidth * m_biuTo3Dunits, *aShape ) );
688  aDstContainer->Add( new ROUND_SEGMENT_2D( topRight3DU, botRight3DU,
689  linewidth * m_biuTo3Dunits, *aShape ) );
690  aDstContainer->Add( new ROUND_SEGMENT_2D( botRight3DU, botLeft3DU,
691  linewidth * m_biuTo3Dunits, *aShape ) );
692  aDstContainer->Add( new ROUND_SEGMENT_2D( botLeft3DU, topLeft3DU,
693  linewidth * m_biuTo3Dunits, *aShape ) );
694  }
695  break;
696 
697  case SHAPE_T::ARC:
698  {
699  unsigned int segCount = GetCircleSegmentCount( aShape->GetBoundingBox().GetSizeMax() );
700 
701  transformArcToSegments( aShape->GetCenter(), aShape->GetStart(), aShape->GetArcAngle(),
702  segCount, linewidth, aDstContainer, *aShape );
703  }
704  break;
705 
706  case SHAPE_T::SEGMENT:
707  {
708  const SFVEC2F start3DU( aShape->GetStart().x * m_biuTo3Dunits,
709  -aShape->GetStart().y * m_biuTo3Dunits );
710 
711  const SFVEC2F end3DU ( aShape->GetEnd().x * m_biuTo3Dunits,
712  -aShape->GetEnd().y * m_biuTo3Dunits );
713 
714  if( Is_segment_a_circle( start3DU, end3DU ) )
715  {
716  aDstContainer->Add( new FILLED_CIRCLE_2D( start3DU, ( linewidth / 2 ) * m_biuTo3Dunits,
717  *aShape ) );
718  }
719  else
720  {
721  aDstContainer->Add( new ROUND_SEGMENT_2D( start3DU, end3DU, linewidth * m_biuTo3Dunits,
722  *aShape ) );
723  }
724  }
725  break;
726 
727  case SHAPE_T::BEZIER:
728  case SHAPE_T::POLY:
729  {
730  SHAPE_POLY_SET polyList;
731 
732  aShape->TransformShapeWithClearanceToPolygon( polyList, aLayerId, 0,
733  ARC_HIGH_DEF, ERROR_INSIDE );
734 
735  polyList.Simplify( SHAPE_POLY_SET::PM_FAST );
736 
737  if( polyList.IsEmpty() ) // Just for caution
738  break;
739 
740  ConvertPolygonToTriangles( polyList, *aDstContainer, m_biuTo3Dunits, *aShape );
741  }
742  break;
743 
744  default:
745  wxFAIL_MSG( wxT( "BOARD_ADAPTER::addShapeWithClearance no implementation for " )
746  + aShape->SHAPE_T_asString() );
747  break;
748  }
749 }
750 
751 
752 // Based on
753 // TransformSolidAreasShapesToPolygonSet
754 // board_items_to_polygon_shape_transform.cpp
755 void BOARD_ADAPTER::addSolidAreasShapes( const ZONE* aZoneContainer,
756  CONTAINER_2D_BASE* aDstContainer, PCB_LAYER_ID aLayerId )
757 {
758  // Copy the polys list because we have to simplify it
759  SHAPE_POLY_SET polyList = SHAPE_POLY_SET( aZoneContainer->GetFilledPolysList( aLayerId ) );
760 
761  // This convert the poly in outline and holes
762  ConvertPolygonToTriangles( polyList, *aDstContainer, m_biuTo3Dunits, *aZoneContainer );
763 
764  // add filled areas outlines, which are drawn with thick lines segments
765  // but only if filled polygons outlines have thickness
766  if( !aZoneContainer->GetFilledPolysUseThickness() )
767  return;
768 
769  float line_thickness = aZoneContainer->GetMinThickness() * m_biuTo3Dunits;
770 
771  for( int i = 0; i < polyList.OutlineCount(); ++i )
772  {
773  // Add outline
774  const SHAPE_LINE_CHAIN& pathOutline = polyList.COutline( i );
775 
776  for( int j = 0; j < pathOutline.PointCount(); ++j )
777  {
778  const VECTOR2I& a = pathOutline.CPoint( j );
779  const VECTOR2I& b = pathOutline.CPoint( j + 1 );
780 
781  SFVEC2F start3DU( a.x * m_biuTo3Dunits, -a.y * m_biuTo3Dunits );
782  SFVEC2F end3DU ( b.x * m_biuTo3Dunits, -b.y * m_biuTo3Dunits );
783 
784  if( Is_segment_a_circle( start3DU, end3DU ) )
785  {
786  float radius = line_thickness/2;
787 
788  if( radius > 0.0 ) // degenerated circles crash 3D viewer
789  aDstContainer->Add( new FILLED_CIRCLE_2D( start3DU, radius, *aZoneContainer ) );
790  }
791  else
792  {
793  aDstContainer->Add( new ROUND_SEGMENT_2D( start3DU, end3DU, line_thickness,
794  *aZoneContainer ) );
795  }
796  }
797 
798  // Add holes (of the poly, ie: the open parts) for this outline
799  for( int h = 0; h < polyList.HoleCount( i ); ++h )
800  {
801  const SHAPE_LINE_CHAIN& pathHole = polyList.CHole( i, h );
802 
803  for( int j = 0; j < pathHole.PointCount(); j++ )
804  {
805  const VECTOR2I& a = pathHole.CPoint( j );
806  const VECTOR2I& b = pathHole.CPoint( j + 1 );
807 
808  SFVEC2F start3DU( a.x * m_biuTo3Dunits, -a.y * m_biuTo3Dunits );
809  SFVEC2F end3DU ( b.x * m_biuTo3Dunits, -b.y * m_biuTo3Dunits );
810 
811  if( Is_segment_a_circle( start3DU, end3DU ) )
812  {
813  float radius = line_thickness/2;
814 
815  if( radius > 0.0 ) // degenerated circles crash 3D viewer
816  aDstContainer->Add( new FILLED_CIRCLE_2D( start3DU, radius,
817  *aZoneContainer ) );
818  }
819  else
820  {
821  aDstContainer->Add( new ROUND_SEGMENT_2D( start3DU, end3DU, line_thickness,
822  *aZoneContainer ) );
823  }
824  }
825  }
826  }
827 }
828 
829 
831  int aWidth )
832 {
833  if( aPad->GetShape() == PAD_SHAPE::CIRCLE ) // Draw a ring
834  {
835  const SFVEC2F center3DU( aPad->ShapePos().x * m_biuTo3Dunits,
836  -aPad->ShapePos().y * m_biuTo3Dunits );
837 
838  const double radius = aPad->GetSize().x / 2.0;
839  const float inner_radius = ( radius - aWidth / 2.0 ) * m_biuTo3Dunits;
840  const float outer_radius = ( radius + aWidth / 2.0 ) * m_biuTo3Dunits;
841 
842  aDstContainer->Add( new RING_2D( center3DU, inner_radius, outer_radius, *aPad ) );
843 
844  return;
845  }
846 
847  // For other shapes, add outlines as thick segments in polygon buffer
848  const std::shared_ptr<SHAPE_POLY_SET>& corners = aPad->GetEffectivePolygon();
849  const SHAPE_LINE_CHAIN& path = corners->COutline( 0 );
850 
851  for( int j = 0; j < path.PointCount(); j++ )
852  {
853  const VECTOR2I& a = path.CPoint( j );
854  const VECTOR2I& b = path.CPoint( j + 1 );
855 
856  SFVEC2F start3DU( a.x * m_biuTo3Dunits, -a.y * m_biuTo3Dunits );
857  SFVEC2F end3DU ( b.x * m_biuTo3Dunits, -b.y * m_biuTo3Dunits );
858 
859  if( Is_segment_a_circle( start3DU, end3DU ) )
860  {
861  aDstContainer->Add( new FILLED_CIRCLE_2D( start3DU, ( aWidth / 2 ) * m_biuTo3Dunits,
862  *aPad ) );
863  }
864  else
865  {
866  aDstContainer->Add( new ROUND_SEGMENT_2D( start3DU, end3DU, aWidth * m_biuTo3Dunits,
867  *aPad ) );
868  }
869  }
870 }
bool IsBold() const
Definition: eda_text.h:204
virtual wxPoint GetCenter() const override
This defaults to the center of the bounding box if not overridden.
Definition: pcb_track.h:281
int GetWidth() const
Definition: eda_shape.h:98
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:220
bool IsMirrored() const
Definition: eda_text.h:210
class FP_TEXT, text in a footprint
Definition: typeinfo.h:92
int GetRadius() const
Definition: shape_circle.h:107
const wxPoint & GetEnd() const
Definition: pcb_track.h:105
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:49
const wxPoint & GetStart() const
Return the starting point of the graphic.
Definition: eda_shape.h:106
#define SEG_CNT_MAX
OBJECT_2D * createPadWithDrill(const PAD *aPad, int aInflateValue)
PCB_TEXT & Text()
void createPadWithClearance(const PAD *aPad, CONTAINER_2D_BASE *aDstContainer, PCB_LAYER_ID aLayer, const wxSize &aClearanceValue) const
bool IsEmpty() const
void transformArcToSegments(const wxPoint &aCentre, const wxPoint &aStart, double aArcAngle, int aCircleToSegmentsCount, int aWidth, CONTAINER_2D_BASE *aDstContainer, const BOARD_ITEM &aBoardItem)
double GetArcAngle() const
Definition: eda_shape.cpp:536
double GetTextAngle() const
Definition: eda_text.h:195
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:336
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:637
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:159
PADS & Pads()
Definition: footprint.h:169
FP_TEXT & Value()
read/write accessors:
Definition: footprint.h:502
static int s_textWidth
void Add(OBJECT_2D *aObject)
Definition: container_2d.h:49
bool IsFilled() const
Definition: eda_shape.h:90
const VECTOR2I GetSize() const
Definition: shape_rect.h:124
bool IsItalic() const
Definition: eda_text.h:201
FP_TEXT & Reference()
Definition: footprint.h:503
static LIB_SYMBOL * dummy()
Used to draw a dummy shape when a LIB_SYMBOL is not found in library.
Definition: sch_symbol.cpp:72
std::vector< wxPoint > GetRectCorners() const
Definition: eda_shape.cpp:981
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.
virtual std::shared_ptr< SHAPE > GetEffectiveShape(PCB_LAYER_ID aLayer=UNDEFINED_LAYER, FLASHING aFlash=FLASHING::DEFAULT) const override
Some pad shapes can be complex (rounded/chamfered rectangle), even without considering custom shapes.
Definition: pad.cpp:309
const wxSize & GetDrillSize() const
Definition: pad.h:243
wxPoint ShapePos() const
Definition: pad.cpp:706
like PAD_PTH, but not plated
const wxPoint & GetEnd() const
Return the ending point of the graphic.
Definition: eda_shape.h:131
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:219
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:207
const VECTOR2I & GetPosition() const
Definition: shape_rect.h:116
const wxSize & GetTextSize() const
Definition: eda_text.h:259
double GetRadius() const
Definition: pcb_track.cpp:946
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.h:79
void addSolidAreasShapes(const ZONE *aZoneContainer, CONTAINER_2D_BASE *aDstContainer, PCB_LAYER_ID aLayerId)
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:172
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.
wxString SHAPE_T_asString() const
Definition: eda_shape.cpp:71
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.
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:952
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:458
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 containing arcs as well as line segments: A chain of connected line and/or arc s...
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:65
const SHAPE_LINE_CHAIN & COutline(int aIndex) const
Definition: layer_ids.h:71
const EDA_RECT GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
Definition: pcb_shape.h:102
VECTOR2I A
Definition: seg.h:48
const std::shared_ptr< SHAPE_POLY_SET > & GetEffectivePolygon() const
Definition: pad.cpp:300
void addTextSegmToContainer(int x0, int y0, int xf, int yf, void *aData)
const wxPoint & GetTextPos() const
Definition: eda_text.h:268
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
SHAPE_T GetShape() const
Definition: eda_shape.h:101
int GetRadius() const
Definition: eda_shape.cpp:472
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 shape to a closed polygon.
Definition: pcb_shape.cpp:236
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:143
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:105
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:112
line segment
Definition: shape.h:44
bool GetFilledPolysUseThickness() const
Definition: zone.h:691
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