You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

834 lines
29 KiB

4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
4 years ago
3 years ago
3 years ago
  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) 2023 CERN
  6. * Copyright (C) 1992-2023 KiCad Developers, see AUTHORS.txt for contributors.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, you may find one here:
  20. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  21. * or you may search the http://www.gnu.org website for the version 2 license,
  22. * or you may write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  24. */
  25. /**
  26. * @file create_3Dgraphic_brd_items.cpp
  27. * @brief This file implements the creation of 2D graphic primitives of pcb items:
  28. * pads, tracks, drawsegments, texts....
  29. * It is based on the function found in the files:
  30. * board_items_to_polygon_shape_transform.cpp
  31. */
  32. #include "../3d_rendering/raytracing/shapes2D/ring_2d.h"
  33. #include "../3d_rendering/raytracing/shapes2D/filled_circle_2d.h"
  34. #include "../3d_rendering/raytracing/shapes2D/round_segment_2d.h"
  35. #include "../3d_rendering/raytracing/shapes2D/triangle_2d.h"
  36. #include <board_adapter.h>
  37. #include <footprint.h>
  38. #include <pad.h>
  39. #include <pcb_text.h>
  40. #include <pcb_textbox.h>
  41. #include <board_design_settings.h>
  42. #include <pcb_painter.h> // for PCB_RENDER_SETTINGS
  43. #include <zone.h>
  44. #include <convert_basic_shapes_to_polygon.h>
  45. #include <trigo.h>
  46. #include <geometry/shape_segment.h>
  47. #include <geometry/geometry_utils.h>
  48. #include <geometry/shape_circle.h>
  49. #include <geometry/shape_rect.h>
  50. #include <geometry/shape_simple.h>
  51. #include <utility>
  52. #include <vector>
  53. #include <wx/log.h>
  54. #include <macros.h>
  55. #include <callback_gal.h>
  56. #define TO_3DU( x ) ( ( x ) * m_biuTo3Dunits )
  57. #define TO_SFVEC2F( vec ) SFVEC2F( TO_3DU( vec.x ), TO_3DU( -vec.y ) )
  58. void BOARD_ADAPTER::addText( const EDA_TEXT* aText, CONTAINER_2D_BASE* aContainer,
  59. const BOARD_ITEM* aOwner )
  60. {
  61. KIGFX::GAL_DISPLAY_OPTIONS empty_opts;
  62. TEXT_ATTRIBUTES attrs = aText->GetAttributes();
  63. float penWidth_3DU = TO_3DU( aText->GetEffectiveTextPenWidth() );
  64. KIFONT::FONT* font = aText->GetFont();
  65. if( !font )
  66. font = KIFONT::FONT::GetFont( wxEmptyString, aText->IsBold(), aText->IsItalic() );
  67. if( aOwner && aOwner->IsKnockout() )
  68. {
  69. SHAPE_POLY_SET finalPoly;
  70. const PCB_TEXT* pcbText = static_cast<const PCB_TEXT*>( aOwner );
  71. pcbText->TransformTextToPolySet( finalPoly, 0, m_board->GetDesignSettings().m_MaxError,
  72. ERROR_INSIDE );
  73. // Do not call finalPoly.Fracture() here: ConvertPolygonToTriangles() call it
  74. // if needed, and Fracture() called twice can create bad results and is useless
  75. ConvertPolygonToTriangles( finalPoly, *aContainer, m_biuTo3Dunits, *aOwner );
  76. }
  77. else
  78. {
  79. CALLBACK_GAL callback_gal( empty_opts,
  80. // Stroke callback
  81. [&]( const VECTOR2I& aPt1, const VECTOR2I& aPt2 )
  82. {
  83. const SFVEC2F pt1_3DU = TO_SFVEC2F( aPt1 );
  84. const SFVEC2F pt2_3DU = TO_SFVEC2F( aPt2 );
  85. if( penWidth_3DU == 0.0 )
  86. {
  87. // Don't attempt to render degenerate shapes
  88. }
  89. else if( Is_segment_a_circle( pt1_3DU, pt2_3DU ) )
  90. {
  91. // Cannot add segments that have the same start and end point
  92. aContainer->Add( new FILLED_CIRCLE_2D( pt1_3DU, penWidth_3DU / 2,
  93. *aOwner ) );
  94. }
  95. else
  96. {
  97. aContainer->Add( new ROUND_SEGMENT_2D( pt1_3DU, pt2_3DU, penWidth_3DU,
  98. *aOwner ) );
  99. }
  100. },
  101. // Triangulation callback
  102. [&]( const VECTOR2I& aPt1, const VECTOR2I& aPt2, const VECTOR2I& aPt3 )
  103. {
  104. aContainer->Add( new TRIANGLE_2D( TO_SFVEC2F( aPt1 ), TO_SFVEC2F( aPt2 ),
  105. TO_SFVEC2F( aPt3 ), *aOwner ) );
  106. } );
  107. attrs.m_Angle = aText->GetDrawRotation();
  108. font->Draw( &callback_gal, aText->GetShownText( true ), aText->GetDrawPos(), attrs,
  109. aOwner->GetFontMetrics() );
  110. }
  111. }
  112. void BOARD_ADAPTER::addShape( const PCB_DIMENSION_BASE* aDimension, CONTAINER_2D_BASE* aContainer,
  113. const BOARD_ITEM* aOwner )
  114. {
  115. addText( aDimension, aContainer, aDimension );
  116. const int linewidth = aDimension->GetLineThickness();
  117. for( const std::shared_ptr<SHAPE>& shape : aDimension->GetShapes() )
  118. {
  119. switch( shape->Type() )
  120. {
  121. case SH_SEGMENT:
  122. {
  123. const SEG& seg = static_cast<const SHAPE_SEGMENT*>( shape.get() )->GetSeg();
  124. aContainer->Add( new ROUND_SEGMENT_2D( TO_SFVEC2F( seg.A ), TO_SFVEC2F( seg.B ),
  125. TO_3DU( linewidth ), *aOwner ) );
  126. break;
  127. }
  128. case SH_CIRCLE:
  129. {
  130. int radius = static_cast<const SHAPE_CIRCLE*>( shape.get() )->GetRadius();
  131. int delta = aDimension->GetLineThickness() / 2;
  132. aContainer->Add( new RING_2D( TO_SFVEC2F( shape->Centre() ), TO_3DU( radius - delta ),
  133. TO_3DU( radius + delta ), *aOwner ) );
  134. break;
  135. }
  136. default:
  137. break;
  138. }
  139. }
  140. }
  141. void BOARD_ADAPTER::addFootprintShapes( const FOOTPRINT* aFootprint, CONTAINER_2D_BASE* aContainer,
  142. PCB_LAYER_ID aLayerId,
  143. const std::bitset<LAYER_3D_END>& aVisibilityFlags )
  144. {
  145. KIGFX::GAL_DISPLAY_OPTIONS empty_opts;
  146. for( PCB_FIELD* field : aFootprint->GetFields() )
  147. {
  148. if( field->GetLayer() == aLayerId && field->IsVisible() )
  149. {
  150. if( !aVisibilityFlags.test( LAYER_FP_TEXT ) )
  151. continue;
  152. else if( field->IsReference() && !aVisibilityFlags.test( LAYER_FP_REFERENCES ) )
  153. continue;
  154. else if( field->IsValue() && !aVisibilityFlags.test( LAYER_FP_VALUES ) )
  155. continue;
  156. addText( field, aContainer, field );
  157. }
  158. }
  159. for( BOARD_ITEM* item : aFootprint->GraphicalItems() )
  160. {
  161. switch( item->Type() )
  162. {
  163. case PCB_TEXT_T:
  164. {
  165. PCB_TEXT* text = static_cast<PCB_TEXT*>( item );
  166. if( text->GetLayer() == aLayerId && text->IsVisible() )
  167. {
  168. if( !aVisibilityFlags.test( LAYER_FP_TEXT ) )
  169. continue;
  170. else if( text->GetText() == wxT( "${REFERENCE}" ) && !aVisibilityFlags.test( LAYER_FP_REFERENCES ) )
  171. continue;
  172. else if( text->GetText() == wxT( "${VALUE}" ) && !aVisibilityFlags.test( LAYER_FP_VALUES ) )
  173. continue;
  174. addText( text, aContainer, text );
  175. }
  176. break;
  177. }
  178. case PCB_TEXTBOX_T:
  179. {
  180. PCB_TEXTBOX* textbox = static_cast<PCB_TEXTBOX*>( item );
  181. if( textbox->GetLayer() == aLayerId )
  182. {
  183. addShape( textbox, aContainer, aFootprint );
  184. addText( textbox, aContainer, aFootprint );
  185. }
  186. break;
  187. }
  188. case PCB_DIM_ALIGNED_T:
  189. case PCB_DIM_CENTER_T:
  190. case PCB_DIM_ORTHOGONAL_T:
  191. case PCB_DIM_RADIAL_T:
  192. case PCB_DIM_LEADER_T:
  193. {
  194. PCB_DIMENSION_BASE* dimension = static_cast<PCB_DIMENSION_BASE*>( item );
  195. if( dimension->GetLayer() == aLayerId )
  196. addShape( dimension, aContainer, aFootprint );
  197. break;
  198. }
  199. case PCB_SHAPE_T:
  200. {
  201. PCB_SHAPE* shape = static_cast<PCB_SHAPE*>( item );
  202. if( shape->GetLayer() == aLayerId )
  203. addShape( shape, aContainer, aFootprint );
  204. break;
  205. }
  206. default:
  207. break;
  208. }
  209. }
  210. }
  211. void BOARD_ADAPTER::createViaWithMargin( const PCB_TRACK* aTrack, CONTAINER_2D_BASE* aDstContainer,
  212. int aMargin )
  213. {
  214. SFVEC2F start3DU = TO_SFVEC2F( aTrack->GetStart() );
  215. SFVEC2F end3DU = TO_SFVEC2F( aTrack->GetEnd() );
  216. const float radius3DU = TO_3DU( ( aTrack->GetWidth() / 2 ) + aMargin );
  217. if( radius3DU > 0.0 )
  218. aDstContainer->Add( new FILLED_CIRCLE_2D( start3DU, radius3DU, *aTrack ) );
  219. }
  220. void BOARD_ADAPTER::createTrack( const PCB_TRACK* aTrack, CONTAINER_2D_BASE* aDstContainer )
  221. {
  222. SFVEC2F start3DU = TO_SFVEC2F( aTrack->GetStart() );
  223. SFVEC2F end3DU = TO_SFVEC2F( aTrack->GetEnd() );
  224. switch( aTrack->Type() )
  225. {
  226. case PCB_VIA_T:
  227. {
  228. const float radius3DU = TO_3DU( aTrack->GetWidth() / 2 );
  229. if( radius3DU > 0.0 )
  230. aDstContainer->Add( new FILLED_CIRCLE_2D( start3DU, radius3DU, *aTrack ) );
  231. break;
  232. }
  233. case PCB_ARC_T:
  234. {
  235. const PCB_ARC* arc = static_cast<const PCB_ARC*>( aTrack );
  236. VECTOR2D center( arc->GetCenter() );
  237. EDA_ANGLE arc_angle = arc->GetAngle();
  238. double radius = arc->GetRadius();
  239. int arcsegcount = GetArcToSegmentCount( radius, ARC_HIGH_DEF, arc_angle );
  240. int circlesegcount;
  241. // Avoid arcs that cannot be drawn
  242. if( radius < std::numeric_limits<double>::min() || arc_angle.IsZero() )
  243. break;
  244. // We need a circle to segment count. However, the arc angle can be small, and the
  245. // radius very big. so we calculate a reasonable value for circlesegcount.
  246. if( arcsegcount <= 1 ) // The arc will be approximated by a segment
  247. {
  248. circlesegcount = 1;
  249. }
  250. else
  251. {
  252. circlesegcount = KiROUND( arcsegcount * 360.0 / std::abs( arc_angle.AsDegrees() ) );
  253. circlesegcount = alg::clamp( 1, circlesegcount, 128 );
  254. }
  255. transformArcToSegments( VECTOR2I( center.x, center.y ), arc->GetStart(), arc_angle,
  256. circlesegcount, arc->GetWidth(), aDstContainer, *arc );
  257. break;
  258. }
  259. case PCB_TRACE_T: // Track is a usual straight segment
  260. {
  261. if( aTrack->GetWidth() == 0 )
  262. {
  263. // Don't attempt to render degenerate shapes
  264. }
  265. else if( Is_segment_a_circle( start3DU, end3DU ) )
  266. {
  267. // Cannot add segments that have the same start and end point
  268. aDstContainer->Add( new FILLED_CIRCLE_2D( start3DU, TO_3DU( aTrack->GetWidth() / 2 ),
  269. *aTrack ) );
  270. }
  271. else
  272. {
  273. aDstContainer->Add( new ROUND_SEGMENT_2D( start3DU, end3DU, TO_3DU( aTrack->GetWidth() ),
  274. *aTrack ) );
  275. }
  276. break;
  277. }
  278. default:
  279. break;
  280. }
  281. }
  282. void BOARD_ADAPTER::createPadWithMargin( const PAD* aPad, CONTAINER_2D_BASE* aContainer,
  283. PCB_LAYER_ID aLayer, const VECTOR2I& aMargin ) const
  284. {
  285. SHAPE_POLY_SET poly;
  286. int maxError = GetBoard()->GetDesignSettings().m_MaxError;
  287. VECTOR2I clearance = aMargin;
  288. // Our shape-based builder can't handle negative or differing x:y clearance values (the
  289. // former are common for solder paste while the later get generated when a relative paste
  290. // margin is used with an oblong pad). So we apply this huge hack and fake a larger pad to
  291. // run the general-purpose polygon builder on.
  292. // Of course being a hack it falls down when dealing with custom shape pads (where the size
  293. // is only the size of the anchor), so for those we punt and just use aMargin.x.
  294. if( ( clearance.x < 0 || clearance.x != clearance.y )
  295. && aPad->GetShape() != PAD_SHAPE::CUSTOM )
  296. {
  297. VECTOR2I dummySize = VECTOR2I( aPad->GetSize() ) + clearance + clearance;
  298. if( dummySize.x <= 0 || dummySize.y <= 0 )
  299. return;
  300. PAD dummy( *aPad );
  301. dummy.SetSize( VECTOR2I( dummySize.x, dummySize.y ) );
  302. dummy.TransformShapeToPolygon( poly, aLayer, 0, maxError, ERROR_INSIDE );
  303. clearance = { 0, 0 };
  304. // Remove group membership from dummy item before deleting
  305. dummy.SetParentGroup( nullptr );
  306. }
  307. else
  308. {
  309. auto padShapes = std::static_pointer_cast<SHAPE_COMPOUND>( aPad->GetEffectiveShape() );
  310. for( const SHAPE* shape : padShapes->Shapes() )
  311. {
  312. switch( shape->Type() )
  313. {
  314. case SH_SEGMENT:
  315. {
  316. const SHAPE_SEGMENT* seg = static_cast<const SHAPE_SEGMENT*>( shape );
  317. const SFVEC2F a3DU = TO_SFVEC2F( seg->GetSeg().A );
  318. const SFVEC2F b3DU = TO_SFVEC2F( seg->GetSeg().B );
  319. const double width3DU = TO_3DU( seg->GetWidth() + clearance.x * 2 );
  320. if( width3DU == 0.0 )
  321. {
  322. // Don't attempt to render degenerate shapes
  323. }
  324. else if( Is_segment_a_circle( a3DU, b3DU ) )
  325. {
  326. // Cannot add segments that have the same start and end point
  327. aContainer->Add( new FILLED_CIRCLE_2D( a3DU, width3DU / 2, *aPad ) );
  328. }
  329. else
  330. {
  331. aContainer->Add( new ROUND_SEGMENT_2D( a3DU, b3DU, width3DU, *aPad ) );
  332. }
  333. break;
  334. }
  335. case SH_CIRCLE:
  336. {
  337. const SHAPE_CIRCLE* circle = static_cast<const SHAPE_CIRCLE*>( shape );
  338. const double radius3DU = TO_3DU( circle->GetRadius() + clearance.x );
  339. const SFVEC2F center3DU = TO_SFVEC2F( circle->GetCenter() );
  340. // Don't render zero radius circles
  341. if( radius3DU > 0.0 )
  342. aContainer->Add( new FILLED_CIRCLE_2D( center3DU, radius3DU, *aPad ) );
  343. break;
  344. }
  345. case SH_RECT:
  346. {
  347. const SHAPE_RECT* rect = static_cast<const SHAPE_RECT*>( shape );
  348. poly.NewOutline();
  349. poly.Append( rect->GetPosition() );
  350. poly.Append( rect->GetPosition().x + rect->GetSize().x, rect->GetPosition().y );
  351. poly.Append( rect->GetPosition() + rect->GetSize() );
  352. poly.Append( rect->GetPosition().x, rect->GetPosition().y + rect->GetSize().y );
  353. break;
  354. }
  355. case SH_SIMPLE:
  356. poly.AddOutline( static_cast<const SHAPE_SIMPLE*>( shape )->Vertices() );
  357. break;
  358. case SH_POLY_SET:
  359. poly = *(SHAPE_POLY_SET*) shape;
  360. break;
  361. case SH_ARC:
  362. {
  363. const SHAPE_ARC* arc = static_cast<const SHAPE_ARC*>( shape );
  364. SHAPE_LINE_CHAIN l = arc->ConvertToPolyline( maxError );
  365. for( int i = 0; i < l.SegmentCount(); i++ )
  366. {
  367. SHAPE_SEGMENT seg( l.Segment( i ).A, l.Segment( i ).B, arc->GetWidth() );
  368. const SFVEC2F a3DU = TO_SFVEC2F( seg.GetSeg().A );
  369. const SFVEC2F b3DU = TO_SFVEC2F( seg.GetSeg().B );
  370. const double width3DU = TO_3DU( arc->GetWidth() + clearance.x * 2 );
  371. if( width3DU == 0.0 )
  372. {
  373. // Don't attempt to render degenerate shapes
  374. }
  375. else if( Is_segment_a_circle( a3DU, b3DU ) )
  376. {
  377. // Cannot add segments that have the same start and end point
  378. aContainer->Add( new FILLED_CIRCLE_2D( a3DU, width3DU / 2, *aPad ) );
  379. }
  380. else
  381. {
  382. aContainer->Add( new ROUND_SEGMENT_2D( a3DU, b3DU, width3DU, *aPad ) );
  383. }
  384. }
  385. break;
  386. }
  387. default:
  388. UNIMPLEMENTED_FOR( SHAPE_TYPE_asString( shape->Type() ) );
  389. break;
  390. }
  391. }
  392. }
  393. if( !poly.IsEmpty() )
  394. {
  395. if( clearance.x )
  396. poly.Inflate( clearance.x, SHAPE_POLY_SET::ROUND_ALL_CORNERS, maxError );
  397. // Add the PAD polygon
  398. ConvertPolygonToTriangles( poly, *aContainer, m_biuTo3Dunits, *aPad );
  399. }
  400. }
  401. OBJECT_2D* BOARD_ADAPTER::createPadWithDrill( const PAD* aPad, int aInflateValue )
  402. {
  403. if( !aPad->HasHole() )
  404. {
  405. wxLogTrace( m_logTrace, wxT( "BOARD_ADAPTER::createPadWithDrill - found an invalid pad" ) );
  406. return nullptr;
  407. }
  408. std::shared_ptr<SHAPE_SEGMENT> slot = aPad->GetEffectiveHoleShape();
  409. if( slot->GetSeg().A == slot->GetSeg().B )
  410. {
  411. return new FILLED_CIRCLE_2D( TO_SFVEC2F( slot->GetSeg().A ),
  412. TO_3DU( slot->GetWidth() / 2 + aInflateValue ),
  413. *aPad );
  414. }
  415. else
  416. {
  417. return new ROUND_SEGMENT_2D( TO_SFVEC2F( slot->GetSeg().A ),
  418. TO_SFVEC2F( slot->GetSeg().B ),
  419. TO_3DU( slot->GetWidth() + aInflateValue * 2 ),
  420. *aPad );
  421. }
  422. }
  423. void BOARD_ADAPTER::addPads( const FOOTPRINT* aFootprint, CONTAINER_2D_BASE* aContainer,
  424. PCB_LAYER_ID aLayerId, bool aSkipPlatedPads, bool aSkipNonPlatedPads )
  425. {
  426. for( PAD* pad : aFootprint->Pads() )
  427. {
  428. if( !pad->IsOnLayer( aLayerId ) )
  429. continue;
  430. if( IsCopperLayer( aLayerId ) )
  431. {
  432. // Skip pad annulus when there isn't one (note: this is more discerning than
  433. // pad->IsOnLayer(), which doesn't check for NPTH pads with holes that consume
  434. // the entire pad).
  435. if( !pad->IsOnCopperLayer() )
  436. continue;
  437. // Skip pad annulus when not connected on this layer (if removing is enabled)
  438. if( !pad->FlashLayer( aLayerId ) )
  439. continue;
  440. }
  441. VECTOR2I margin( 0, 0 );
  442. switch( aLayerId )
  443. {
  444. case F_Cu:
  445. if( aSkipPlatedPads && pad->FlashLayer( F_Mask ) )
  446. continue;
  447. if( aSkipNonPlatedPads && !pad->FlashLayer( F_Mask ) )
  448. continue;
  449. break;
  450. case B_Cu:
  451. if( aSkipPlatedPads && pad->FlashLayer( B_Mask ) )
  452. continue;
  453. if( aSkipNonPlatedPads && !pad->FlashLayer( B_Mask ) )
  454. continue;
  455. break;
  456. case F_Mask:
  457. case B_Mask:
  458. margin.x += pad->GetSolderMaskExpansion();
  459. margin.y += pad->GetSolderMaskExpansion();
  460. break;
  461. case F_Paste:
  462. case B_Paste:
  463. margin += pad->GetSolderPasteMargin();
  464. break;
  465. default:
  466. break;
  467. }
  468. createPadWithMargin( pad, aContainer, aLayerId, margin );
  469. }
  470. }
  471. // based on TransformArcToPolygon function from
  472. // common/convert_basic_shapes_to_polygon.cpp
  473. void BOARD_ADAPTER::transformArcToSegments( const VECTOR2I& aCentre, const VECTOR2I& aStart,
  474. const EDA_ANGLE& aArcAngle, int aCircleToSegmentsCount,
  475. int aWidth, CONTAINER_2D_BASE* aContainer,
  476. const BOARD_ITEM& aOwner )
  477. {
  478. // Don't attempt to render degenerate shapes
  479. if( aWidth == 0 )
  480. return;
  481. VECTOR2I arc_start, arc_end;
  482. EDA_ANGLE arcAngle( aArcAngle );
  483. EDA_ANGLE delta = ANGLE_360 / aCircleToSegmentsCount; // rotate angle
  484. arc_end = arc_start = aStart;
  485. if( arcAngle != ANGLE_360 )
  486. RotatePoint( arc_end, aCentre, -arcAngle );
  487. if( arcAngle < ANGLE_0 )
  488. {
  489. std::swap( arc_start, arc_end );
  490. arcAngle = -arcAngle;
  491. }
  492. // Compute the ends of segments and creates poly
  493. VECTOR2I curr_end = arc_start;
  494. VECTOR2I curr_start = arc_start;
  495. for( EDA_ANGLE ii = delta; ii < arcAngle; ii += delta )
  496. {
  497. curr_end = arc_start;
  498. RotatePoint( curr_end, aCentre, -ii );
  499. const SFVEC2F start3DU = TO_SFVEC2F( curr_start );
  500. const SFVEC2F end3DU = TO_SFVEC2F( curr_end );
  501. if( Is_segment_a_circle( start3DU, end3DU ) )
  502. aContainer->Add( new FILLED_CIRCLE_2D( start3DU, TO_3DU( aWidth / 2 ), aOwner ) );
  503. else
  504. aContainer->Add( new ROUND_SEGMENT_2D( start3DU, end3DU, TO_3DU( aWidth ), aOwner ) );
  505. curr_start = curr_end;
  506. }
  507. if( curr_end != arc_end )
  508. {
  509. const SFVEC2F start3DU = TO_SFVEC2F( curr_end );
  510. const SFVEC2F end3DU = TO_SFVEC2F( arc_end );
  511. if( Is_segment_a_circle( start3DU, end3DU ) )
  512. aContainer->Add( new FILLED_CIRCLE_2D( start3DU, TO_3DU( aWidth / 2 ), aOwner ) );
  513. else
  514. aContainer->Add( new ROUND_SEGMENT_2D( start3DU, end3DU, TO_3DU( aWidth ), aOwner ) );
  515. }
  516. }
  517. void BOARD_ADAPTER::addShape( const PCB_SHAPE* aShape, CONTAINER_2D_BASE* aContainer,
  518. const BOARD_ITEM* aOwner )
  519. {
  520. // The full width of the lines to create
  521. // The extra 1 protects the inner/outer radius values from degeneracy
  522. const int linewidth = aShape->GetWidth() + 1;
  523. PLOT_DASH_TYPE lineStyle = aShape->GetStroke().GetPlotStyle();
  524. if( lineStyle <= PLOT_DASH_TYPE::FIRST_TYPE )
  525. {
  526. switch( aShape->GetShape() )
  527. {
  528. case SHAPE_T::CIRCLE:
  529. {
  530. const SFVEC2F center3DU = TO_SFVEC2F( aShape->GetCenter() );
  531. float inner_radius3DU = TO_3DU( aShape->GetRadius() - linewidth / 2 );
  532. float outer_radius3DU = TO_3DU( aShape->GetRadius() + linewidth / 2 );
  533. if( inner_radius3DU < 0 )
  534. inner_radius3DU = 0.0;
  535. if( outer_radius3DU == 0.0 )
  536. {
  537. // Don't attempt to render degenerate shapes
  538. }
  539. else if( aShape->IsFilled() )
  540. {
  541. aContainer->Add( new FILLED_CIRCLE_2D( center3DU, outer_radius3DU,
  542. *aOwner ) );
  543. }
  544. else
  545. {
  546. aContainer->Add( new RING_2D( center3DU, inner_radius3DU, outer_radius3DU,
  547. *aOwner ) );
  548. }
  549. break;
  550. }
  551. case SHAPE_T::RECTANGLE:
  552. if( aShape->IsFilled() )
  553. {
  554. SHAPE_POLY_SET polyList;
  555. aShape->TransformShapeToPolygon( polyList, UNDEFINED_LAYER, 0, ARC_HIGH_DEF,
  556. ERROR_INSIDE );
  557. polyList.Simplify( SHAPE_POLY_SET::PM_FAST );
  558. ConvertPolygonToTriangles( polyList, *aContainer, m_biuTo3Dunits, *aOwner );
  559. }
  560. else
  561. {
  562. std::vector<VECTOR2I> pts = aShape->GetRectCorners();
  563. aContainer->Add( new ROUND_SEGMENT_2D( TO_SFVEC2F( pts[0] ), TO_SFVEC2F( pts[1] ),
  564. TO_3DU( linewidth ), *aOwner ) );
  565. aContainer->Add( new ROUND_SEGMENT_2D( TO_SFVEC2F( pts[1] ), TO_SFVEC2F( pts[2] ),
  566. TO_3DU( linewidth ), *aOwner ) );
  567. aContainer->Add( new ROUND_SEGMENT_2D( TO_SFVEC2F( pts[2] ), TO_SFVEC2F( pts[3] ),
  568. TO_3DU( linewidth ), *aOwner ) );
  569. aContainer->Add( new ROUND_SEGMENT_2D( TO_SFVEC2F( pts[3] ), TO_SFVEC2F( pts[0] ),
  570. TO_3DU( linewidth ), *aOwner ) );
  571. }
  572. break;
  573. case SHAPE_T::ARC:
  574. {
  575. unsigned int segCount = GetCircleSegmentCount( aShape->GetBoundingBox().GetSizeMax() );
  576. transformArcToSegments( aShape->GetCenter(), aShape->GetStart(), aShape->GetArcAngle(),
  577. segCount, linewidth, aContainer, *aOwner );
  578. break;
  579. }
  580. case SHAPE_T::SEGMENT:
  581. {
  582. const SFVEC2F start3DU = TO_SFVEC2F( aShape->GetStart() );
  583. const SFVEC2F end3DU = TO_SFVEC2F( aShape->GetEnd() );
  584. const double linewidth3DU = TO_3DU( linewidth );
  585. if( linewidth3DU == 0.0 )
  586. {
  587. // Don't attempt to render degenerate shapes
  588. }
  589. else if( Is_segment_a_circle( start3DU, end3DU ) )
  590. {
  591. // Cannot add segments that have the same start and end point
  592. aContainer->Add( new FILLED_CIRCLE_2D( start3DU, linewidth3DU / 2, *aOwner ) );
  593. }
  594. else
  595. {
  596. aContainer->Add( new ROUND_SEGMENT_2D( start3DU, end3DU, linewidth3DU, *aOwner ) );
  597. }
  598. break;
  599. }
  600. case SHAPE_T::BEZIER:
  601. case SHAPE_T::POLY:
  602. {
  603. SHAPE_POLY_SET polyList;
  604. aShape->TransformShapeToPolygon( polyList, UNDEFINED_LAYER, 0, ARC_HIGH_DEF,
  605. ERROR_INSIDE );
  606. if( polyList.IsEmpty() ) // Just for caution
  607. break;
  608. ConvertPolygonToTriangles( polyList, *aContainer, m_biuTo3Dunits, *aOwner );
  609. break;
  610. }
  611. default:
  612. wxFAIL_MSG( wxT( "BOARD_ADAPTER::addShape no implementation for " )
  613. + aShape->SHAPE_T_asString() );
  614. break;
  615. }
  616. }
  617. else if( linewidth > 0 )
  618. {
  619. std::vector<SHAPE*> shapes = aShape->MakeEffectiveShapes( true );
  620. SFVEC2F a3DU;
  621. SFVEC2F b3DU;
  622. double width3DU = TO_3DU( linewidth );
  623. const PCB_PLOT_PARAMS& plotParams = aShape->GetBoard()->GetPlotOptions();
  624. KIGFX::PCB_RENDER_SETTINGS renderSettings;
  625. renderSettings.SetDashLengthRatio( plotParams.GetDashedLineDashRatio() );
  626. renderSettings.SetGapLengthRatio( plotParams.GetDashedLineGapRatio() );
  627. for( SHAPE* shape : shapes )
  628. {
  629. STROKE_PARAMS::Stroke( shape, lineStyle, linewidth, &renderSettings,
  630. [&]( const VECTOR2I& a, const VECTOR2I& b )
  631. {
  632. a3DU = TO_SFVEC2F( a );
  633. b3DU = TO_SFVEC2F( b );
  634. if( Is_segment_a_circle( a3DU, b3DU ) )
  635. aContainer->Add( new FILLED_CIRCLE_2D( a3DU, width3DU / 2, *aOwner ) );
  636. else
  637. aContainer->Add( new ROUND_SEGMENT_2D( a3DU, b3DU, width3DU, *aOwner ) );
  638. } );
  639. }
  640. for( SHAPE* shape : shapes )
  641. delete shape;
  642. }
  643. }
  644. void BOARD_ADAPTER::addSolidAreasShapes( const ZONE* aZone, CONTAINER_2D_BASE* aContainer,
  645. PCB_LAYER_ID aLayerId )
  646. {
  647. // This convert the poly in outline and holes
  648. ConvertPolygonToTriangles( *aZone->GetFilledPolysList( aLayerId ), *aContainer,
  649. m_biuTo3Dunits, *aZone );
  650. }
  651. void BOARD_ADAPTER::buildPadOutlineAsSegments( const PAD* aPad, CONTAINER_2D_BASE* aContainer,
  652. int aWidth )
  653. {
  654. if( aPad->GetShape() == PAD_SHAPE::CIRCLE ) // Draw a ring
  655. {
  656. const SFVEC2F center3DU = TO_SFVEC2F( aPad->ShapePos() );
  657. const int radius = aPad->GetSize().x / 2;
  658. const float inner_radius3DU = TO_3DU( radius - aWidth / 2 );
  659. const float outer_radius3DU = TO_3DU( radius + aWidth / 2 );
  660. aContainer->Add( new RING_2D( center3DU, inner_radius3DU, outer_radius3DU, *aPad ) );
  661. return;
  662. }
  663. // For other shapes, add outlines as thick segments in polygon buffer
  664. const std::shared_ptr<SHAPE_POLY_SET>& corners = aPad->GetEffectivePolygon();
  665. const SHAPE_LINE_CHAIN& path = corners->COutline( 0 );
  666. for( int j = 0; j < path.PointCount(); j++ )
  667. {
  668. SFVEC2F start3DU = TO_SFVEC2F( path.CPoint( j ) );
  669. SFVEC2F end3DU = TO_SFVEC2F( path.CPoint( j + 1 ) );
  670. if( aWidth == 0 )
  671. {
  672. // Don't attempt to render degenerate shapes
  673. }
  674. else if( Is_segment_a_circle( start3DU, end3DU ) )
  675. {
  676. // Cannot add segments that have the same start and end point
  677. aContainer->Add( new FILLED_CIRCLE_2D( start3DU, TO_3DU( aWidth / 2 ), *aPad ) );
  678. }
  679. else
  680. {
  681. aContainer->Add( new ROUND_SEGMENT_2D( start3DU, end3DU, TO_3DU( aWidth ), *aPad ) );
  682. }
  683. }
  684. }