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.

302 lines
8.3 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 (C) 1992-2019 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. /**
  25. * @file class_pad_custom_shape_functions.cpp
  26. * D_PAD functions specific to custom shaped pads.
  27. */
  28. #include <fctsys.h>
  29. #include <trigo.h>
  30. #include <pcbnew.h>
  31. #include <class_board.h>
  32. #include <class_board_item.h>
  33. #include <class_drawsegment.h>
  34. #include <class_pad.h>
  35. #include <convert_basic_shapes_to_polygon.h>
  36. #include <geometry/shape_rect.h>
  37. /*
  38. * Has meaning only for free shape pads.
  39. * add a free shape to the shape list.
  40. * the shape is a polygon (can be with thick outline), segment, circle or arc
  41. */
  42. void D_PAD::AddPrimitivePoly( const SHAPE_POLY_SET& aPoly, int aThickness )
  43. {
  44. std::vector<wxPoint> points;
  45. // If aPoly has holes, convert it to a polygon with no holes.
  46. SHAPE_POLY_SET poly_no_hole;
  47. poly_no_hole.Append( aPoly );
  48. poly_no_hole.Fracture( SHAPE_POLY_SET::PM_STRICTLY_SIMPLE );
  49. for( auto iter = poly_no_hole.CIterate(); iter; iter++ )
  50. points.emplace_back( iter->x, iter->y );
  51. AddPrimitivePoly( points, aThickness );
  52. }
  53. void D_PAD::AddPrimitivePoly( const std::vector<wxPoint>& aPoly, int aThickness )
  54. {
  55. DRAWSEGMENT* item = new DRAWSEGMENT();
  56. item->SetShape( S_POLYGON );
  57. item->SetPolyPoints( aPoly );
  58. item->SetWidth( aThickness );
  59. m_editPrimitives.emplace_back( item );
  60. m_shapesDirty = true;
  61. }
  62. void D_PAD::AddPrimitiveSegment( const wxPoint& aStart, const wxPoint& aEnd, int aThickness )
  63. {
  64. DRAWSEGMENT* item = new DRAWSEGMENT();
  65. item->SetStart( aStart );
  66. item->SetEnd( aEnd );
  67. item->SetWidth( aThickness );
  68. m_editPrimitives.emplace_back( item );
  69. m_shapesDirty = true;
  70. }
  71. void D_PAD::AddPrimitiveArc( const wxPoint& aCenter, const wxPoint& aStart, int aArcAngle,
  72. int aThickness )
  73. {
  74. DRAWSEGMENT* item = new DRAWSEGMENT();
  75. item->SetShape( S_ARC );
  76. item->SetCenter( aCenter );
  77. item->SetArcStart( aStart );
  78. item->SetAngle( aArcAngle );
  79. item->SetWidth( aThickness );
  80. m_editPrimitives.emplace_back( item );
  81. m_shapesDirty = true;
  82. }
  83. void D_PAD::AddPrimitiveCurve( const wxPoint& aStart, const wxPoint& aEnd, const wxPoint& aCtrl1,
  84. const wxPoint& aCtrl2, int aThickness )
  85. {
  86. DRAWSEGMENT* item = new DRAWSEGMENT();
  87. item->SetShape( S_CURVE );
  88. item->SetStart( aStart );
  89. item->SetEnd( aEnd );
  90. item->SetBezControl1( aCtrl1 );
  91. item->SetBezControl2( aCtrl2 );
  92. item->SetWidth( aThickness );
  93. m_editPrimitives.emplace_back( item );
  94. m_shapesDirty = true;
  95. }
  96. void D_PAD::AddPrimitiveCircle( const wxPoint& aCenter, int aRadius, int aThickness )
  97. {
  98. DRAWSEGMENT* item = new DRAWSEGMENT();
  99. item->SetShape( S_CIRCLE );
  100. item->SetStart( aCenter );
  101. item->SetEnd( wxPoint( aCenter.x + aRadius, aCenter.y ) );
  102. item->SetWidth( aThickness );
  103. m_editPrimitives.emplace_back( item );
  104. m_shapesDirty = true;
  105. }
  106. void D_PAD::AddPrimitiveRect( const wxPoint& aStart, const wxPoint& aEnd, int aThickness )
  107. {
  108. DRAWSEGMENT* item = new DRAWSEGMENT();
  109. item->SetShape( S_RECT );
  110. item->SetStart( aStart );
  111. item->SetEnd( aEnd );
  112. item->SetWidth( aThickness );
  113. m_editPrimitives.emplace_back( item );
  114. m_shapesDirty = true;
  115. }
  116. void D_PAD::SetPrimitives( const std::vector<std::shared_ptr<DRAWSEGMENT>>& aPrimitivesList )
  117. {
  118. // clear old list
  119. m_editPrimitives.clear();
  120. // Import to the basic shape list
  121. if( aPrimitivesList.size() )
  122. m_editPrimitives = aPrimitivesList;
  123. m_shapesDirty = true;
  124. }
  125. void D_PAD::AddPrimitives( const std::vector<std::shared_ptr<DRAWSEGMENT>>& aPrimitivesList )
  126. {
  127. for( const std::shared_ptr<DRAWSEGMENT>& prim : aPrimitivesList )
  128. m_editPrimitives.push_back( prim );
  129. m_shapesDirty = true;
  130. }
  131. void D_PAD::AddPrimitive( DRAWSEGMENT* aPrimitive )
  132. {
  133. m_editPrimitives.emplace_back( aPrimitive );
  134. m_shapesDirty = true;
  135. }
  136. // clear the basic shapes list and associated data
  137. void D_PAD::DeletePrimitivesList()
  138. {
  139. m_editPrimitives.clear();
  140. m_shapesDirty = true;
  141. }
  142. void D_PAD::addPadPrimitivesToPolygon( SHAPE_POLY_SET* aMergedPolygon, int aError ) const
  143. {
  144. SHAPE_POLY_SET polyset;
  145. for( const std::shared_ptr<DRAWSEGMENT>& primitive : m_editPrimitives )
  146. primitive->TransformShapeWithClearanceToPolygon( polyset, 0, aError );
  147. polyset.Simplify( SHAPE_POLY_SET::PM_FAST );
  148. // Merge all polygons with the initial pad anchor shape
  149. if( polyset.OutlineCount() )
  150. {
  151. aMergedPolygon->BooleanAdd( polyset, SHAPE_POLY_SET::PM_STRICTLY_SIMPLE );
  152. aMergedPolygon->Fracture( SHAPE_POLY_SET::PM_STRICTLY_SIMPLE );
  153. }
  154. }
  155. void D_PAD::MergePrimitivesAsPolygon( SHAPE_POLY_SET* aMergedPolygon ) const
  156. {
  157. auto board = GetBoard();
  158. int maxError = ARC_HIGH_DEF;
  159. if( board )
  160. maxError = board->GetDesignSettings().m_MaxError;
  161. aMergedPolygon->RemoveAllContours();
  162. // Add the anchor pad shape in aMergedPolygon, others in aux_polyset:
  163. // The anchor pad is always at 0,0
  164. switch( GetAnchorPadShape() )
  165. {
  166. case PAD_SHAPE_RECT:
  167. {
  168. SHAPE_RECT rect( -GetSize().x / 2, -GetSize().y / 2, GetSize().x, GetSize().y );
  169. aMergedPolygon->AddOutline( rect.Outline() );
  170. }
  171. break;
  172. default:
  173. case PAD_SHAPE_CIRCLE:
  174. TransformCircleToPolygon( *aMergedPolygon, wxPoint( 0, 0 ), GetSize().x / 2, maxError );
  175. break;
  176. }
  177. addPadPrimitivesToPolygon( aMergedPolygon, maxError );
  178. }
  179. bool D_PAD::GetBestAnchorPosition( VECTOR2I& aPos )
  180. {
  181. SHAPE_POLY_SET poly;
  182. addPadPrimitivesToPolygon( &poly, ARC_LOW_DEF );
  183. if( poly.OutlineCount() > 1 )
  184. return false;
  185. const int minSteps = 10;
  186. const int maxSteps = 50;
  187. int stepsX, stepsY;
  188. auto bbox = poly.BBox();
  189. if( bbox.GetWidth() < bbox.GetHeight() )
  190. {
  191. stepsX = minSteps;
  192. stepsY = minSteps * (double) bbox.GetHeight() / (double )(bbox.GetWidth() + 1);
  193. }
  194. else
  195. {
  196. stepsY = minSteps;
  197. stepsX = minSteps * (double) bbox.GetWidth() / (double )(bbox.GetHeight() + 1);
  198. }
  199. stepsX = std::max(minSteps, std::min( maxSteps, stepsX ) );
  200. stepsY = std::max(minSteps, std::min( maxSteps, stepsY ) );
  201. VECTOR2I center = bbox.Centre();
  202. int64_t minDist = std::numeric_limits<int64_t>::max();
  203. int64_t minDistEdge;
  204. if( GetAnchorPadShape() == PAD_SHAPE_CIRCLE )
  205. {
  206. minDistEdge = GetSize().x;
  207. }
  208. else
  209. {
  210. minDistEdge = std::max( GetSize().x, GetSize().y );
  211. }
  212. OPT<VECTOR2I> bestAnchor( []()->OPT<VECTOR2I> { return NULLOPT; }() );
  213. for( int y = 0; y < stepsY ; y++ )
  214. {
  215. for( int x = 0; x < stepsX; x++ )
  216. {
  217. VECTOR2I p = bbox.GetPosition();
  218. p.x += rescale( x, bbox.GetWidth(), (stepsX - 1) );
  219. p.y += rescale( y, bbox.GetHeight(), (stepsY - 1) );
  220. if( poly.Contains(p) )
  221. {
  222. int dist = (center - p).EuclideanNorm();
  223. int distEdge = poly.COutline(0).Distance( p, true );
  224. if( distEdge >= minDistEdge )
  225. {
  226. if( dist < minDist )
  227. {
  228. bestAnchor = p;
  229. minDist = dist;
  230. }
  231. }
  232. }
  233. }
  234. }
  235. if( bestAnchor )
  236. {
  237. aPos = *bestAnchor;
  238. return true;
  239. }
  240. return false;
  241. }