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.

184 lines
5.7 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2018 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright The 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. #include <board.h>
  25. #include <board_design_settings.h>
  26. #include <board_item.h>
  27. #include <pcb_shape.h>
  28. #include <pad.h>
  29. #include <convert_basic_shapes_to_polygon.h>
  30. #include <geometry/shape_rect.h>
  31. /*
  32. * Has meaning only for free shape pads.
  33. * add a free shape to the shape list.
  34. * the shape is a polygon (can be with thick outline), segment, circle or arc
  35. */
  36. void PAD::AddPrimitivePoly( PCB_LAYER_ID aLayer, const SHAPE_POLY_SET& aPoly, int aThickness,
  37. bool aFilled )
  38. {
  39. // If aPoly has holes, convert it to a polygon with no holes.
  40. SHAPE_POLY_SET poly_no_hole;
  41. poly_no_hole.Append( aPoly );
  42. if( poly_no_hole.HasHoles() )
  43. poly_no_hole.Fracture();
  44. // There should never be multiple shapes, but if there are, we split them into
  45. // primitives so that we can edit them both.
  46. for( int ii = 0; ii < poly_no_hole.OutlineCount(); ++ii )
  47. {
  48. SHAPE_POLY_SET poly_outline( poly_no_hole.COutline( ii ) );
  49. PCB_SHAPE* item = new PCB_SHAPE();
  50. item->SetShape( SHAPE_T::POLY );
  51. item->SetFilled( aFilled );
  52. item->SetPolyShape( poly_outline );
  53. item->SetStroke( STROKE_PARAMS( aThickness, LINE_STYLE::SOLID ) );
  54. item->SetParent( this );
  55. m_padStack.AddPrimitive( item, aLayer );
  56. }
  57. SetDirty();
  58. }
  59. void PAD::AddPrimitivePoly( PCB_LAYER_ID aLayer, const std::vector<VECTOR2I>& aPoly, int aThickness,
  60. bool aFilled )
  61. {
  62. PCB_SHAPE* item = new PCB_SHAPE( nullptr, SHAPE_T::POLY );
  63. item->SetFilled( aFilled );
  64. item->SetPolyPoints( aPoly );
  65. item->SetStroke( STROKE_PARAMS( aThickness, LINE_STYLE::SOLID ) );
  66. item->SetParent( this );
  67. m_padStack.AddPrimitive( item, aLayer );
  68. SetDirty();
  69. }
  70. void PAD::ReplacePrimitives( PCB_LAYER_ID aLayer,
  71. const std::vector<std::shared_ptr<PCB_SHAPE>>& aPrimitivesList )
  72. {
  73. // clear old list
  74. DeletePrimitivesList( aLayer );
  75. // Import to the given shape list
  76. if( aPrimitivesList.size() )
  77. AppendPrimitives( aLayer, aPrimitivesList );
  78. SetDirty();
  79. }
  80. void PAD::AppendPrimitives( PCB_LAYER_ID aLayer,
  81. const std::vector<std::shared_ptr<PCB_SHAPE>>& aPrimitivesList )
  82. {
  83. // Add duplicates of aPrimitivesList to the pad primitives list:
  84. for( const std::shared_ptr<PCB_SHAPE>& prim : aPrimitivesList )
  85. AddPrimitive( aLayer, new PCB_SHAPE( *prim ) );
  86. SetDirty();
  87. }
  88. void PAD::AddPrimitive( PCB_LAYER_ID aLayer, PCB_SHAPE* aPrimitive )
  89. {
  90. aPrimitive->SetParent( this );
  91. m_padStack.AddPrimitive( aPrimitive, aLayer );
  92. SetDirty();
  93. }
  94. // clear the basic shapes list and associated data
  95. void PAD::DeletePrimitivesList( PCB_LAYER_ID aLayer )
  96. {
  97. if( aLayer == UNDEFINED_LAYER )
  98. {
  99. m_padStack.ForEachUniqueLayer(
  100. [&]( PCB_LAYER_ID l )
  101. {
  102. m_padStack.ClearPrimitives( l );
  103. } );
  104. }
  105. else
  106. {
  107. m_padStack.ClearPrimitives( aLayer);
  108. }
  109. SetDirty();
  110. }
  111. void PAD::addPadPrimitivesToPolygon( PCB_LAYER_ID aLayer, SHAPE_POLY_SET* aMergedPolygon,
  112. int aError, ERROR_LOC aErrorLoc ) const
  113. {
  114. SHAPE_POLY_SET polyset;
  115. for( const std::shared_ptr<PCB_SHAPE>& primitive : m_padStack.Primitives( aLayer ) )
  116. {
  117. if( !primitive->IsProxyItem() )
  118. primitive->TransformShapeToPolygon( polyset, UNDEFINED_LAYER, 0, aError, aErrorLoc );
  119. }
  120. polyset.Simplify();
  121. // Merge all polygons with the initial pad anchor shape
  122. if( polyset.OutlineCount() )
  123. {
  124. aMergedPolygon->BooleanAdd( polyset );
  125. aMergedPolygon->Fracture();
  126. }
  127. }
  128. void PAD::MergePrimitivesAsPolygon( PCB_LAYER_ID aLayer, SHAPE_POLY_SET* aMergedPolygon,
  129. ERROR_LOC aErrorLoc ) const
  130. {
  131. const BOARD* board = GetBoard();
  132. int maxError = board ? board->GetDesignSettings().m_MaxError : ARC_HIGH_DEF;
  133. aMergedPolygon->RemoveAllContours();
  134. // Add the anchor pad shape in aMergedPolygon, others in aux_polyset:
  135. // The anchor pad is always at 0,0
  136. VECTOR2I padSize = GetSize( aLayer );
  137. switch( GetAnchorPadShape( aLayer ) )
  138. {
  139. case PAD_SHAPE::RECTANGLE:
  140. {
  141. SHAPE_RECT rect( -padSize.x / 2, -padSize.y / 2, padSize.x, padSize.y );
  142. aMergedPolygon->AddOutline( rect.Outline() );
  143. }
  144. break;
  145. default:
  146. case PAD_SHAPE::CIRCLE:
  147. TransformCircleToPolygon( *aMergedPolygon, VECTOR2I( 0, 0 ), padSize.x / 2, maxError,
  148. aErrorLoc );
  149. break;
  150. }
  151. addPadPrimitivesToPolygon( aLayer, aMergedPolygon, maxError, aErrorLoc );
  152. }