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.

314 lines
8.9 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  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. #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( const SHAPE_POLY_SET& aPoly, int aThickness, bool aFilled )
  37. {
  38. // If aPoly has holes, convert it to a polygon with no holes.
  39. SHAPE_POLY_SET poly_no_hole;
  40. poly_no_hole.Append( aPoly );
  41. poly_no_hole.Fracture( SHAPE_POLY_SET::PM_STRICTLY_SIMPLE );
  42. PCB_SHAPE* item = new PCB_SHAPE();
  43. item->SetShape( SHAPE_T::POLY );
  44. item->SetFilled( aFilled );
  45. item->SetPolyShape( poly_no_hole );
  46. item->SetWidth( aThickness );
  47. item->SetParent( this );
  48. m_editPrimitives.emplace_back( item );
  49. SetDirty();
  50. }
  51. void PAD::AddPrimitivePoly( const std::vector<wxPoint>& aPoly, int aThickness, bool aFilled )
  52. {
  53. PCB_SHAPE* item = new PCB_SHAPE();
  54. item->SetShape( SHAPE_T::POLY );
  55. item->SetFilled( aFilled );
  56. item->SetPolyPoints( aPoly );
  57. item->SetWidth( aThickness );
  58. item->SetParent( this );
  59. m_editPrimitives.emplace_back( item );
  60. SetDirty();
  61. }
  62. void PAD::AddPrimitiveSegment( const wxPoint& aStart, const wxPoint& aEnd, int aThickness )
  63. {
  64. PCB_SHAPE* item = new PCB_SHAPE();
  65. item->SetShape( SHAPE_T::SEGMENT );
  66. item->SetFilled( false );
  67. item->SetStart( aStart );
  68. item->SetEnd( aEnd );
  69. item->SetWidth( aThickness );
  70. item->SetParent( this );
  71. m_editPrimitives.emplace_back( item );
  72. SetDirty();
  73. }
  74. void PAD::AddPrimitiveArc( const wxPoint& aCenter, const wxPoint& aStart, int aArcAngle,
  75. int aThickness )
  76. {
  77. PCB_SHAPE* item = new PCB_SHAPE();
  78. item->SetShape( SHAPE_T::ARC );
  79. item->SetFilled( false );
  80. item->SetCenter( aCenter );
  81. item->SetArcStart( aStart );
  82. item->SetAngle( aArcAngle );
  83. item->SetWidth( aThickness );
  84. item->SetParent( this );
  85. m_editPrimitives.emplace_back( item );
  86. SetDirty();
  87. }
  88. void PAD::AddPrimitiveCurve( const wxPoint& aStart, const wxPoint& aEnd, const wxPoint& aCtrl1,
  89. const wxPoint& aCtrl2, int aThickness )
  90. {
  91. PCB_SHAPE* item = new PCB_SHAPE();
  92. item->SetShape( SHAPE_T::BEZIER );
  93. item->SetFilled( false );
  94. item->SetStart( aStart );
  95. item->SetEnd( aEnd );
  96. item->SetBezierC1( aCtrl1 );
  97. item->SetBezierC2( aCtrl2 );
  98. item->SetWidth( aThickness );
  99. item->SetParent( this );
  100. m_editPrimitives.emplace_back( item );
  101. SetDirty();
  102. }
  103. void PAD::AddPrimitiveCircle( const wxPoint& aCenter, int aRadius, int aThickness, bool aFilled )
  104. {
  105. PCB_SHAPE* item = new PCB_SHAPE();
  106. item->SetShape( SHAPE_T::CIRCLE );
  107. item->SetFilled( aFilled );
  108. item->SetStart( aCenter );
  109. item->SetEnd( wxPoint( aCenter.x + aRadius, aCenter.y ) );
  110. item->SetWidth( aThickness );
  111. item->SetParent( this );
  112. m_editPrimitives.emplace_back( item );
  113. SetDirty();
  114. }
  115. void PAD::AddPrimitiveRect( const wxPoint& aStart, const wxPoint& aEnd, int aThickness,
  116. bool aFilled)
  117. {
  118. PCB_SHAPE* item = new PCB_SHAPE();
  119. item->SetShape( SHAPE_T::RECT );
  120. item->SetFilled( aFilled );
  121. item->SetStart( aStart );
  122. item->SetEnd( aEnd );
  123. item->SetWidth( aThickness );
  124. item->SetParent( this );
  125. m_editPrimitives.emplace_back( item );
  126. SetDirty();
  127. }
  128. void PAD::ReplacePrimitives( const std::vector<std::shared_ptr<PCB_SHAPE>>& aPrimitivesList )
  129. {
  130. // clear old list
  131. DeletePrimitivesList();
  132. // Import to the given shape list
  133. if( aPrimitivesList.size() )
  134. AppendPrimitives( aPrimitivesList );
  135. SetDirty();
  136. }
  137. void PAD::AppendPrimitives( const std::vector<std::shared_ptr<PCB_SHAPE>>& aPrimitivesList )
  138. {
  139. // Add duplicates of aPrimitivesList to the pad primitives list:
  140. for( const std::shared_ptr<PCB_SHAPE>& prim : aPrimitivesList )
  141. AddPrimitive( new PCB_SHAPE( *prim ) );
  142. SetDirty();
  143. }
  144. void PAD::AddPrimitive( PCB_SHAPE* aPrimitive )
  145. {
  146. aPrimitive->SetParent( this );
  147. m_editPrimitives.emplace_back( aPrimitive );
  148. SetDirty();
  149. }
  150. // clear the basic shapes list and associated data
  151. void PAD::DeletePrimitivesList()
  152. {
  153. m_editPrimitives.clear();
  154. SetDirty();
  155. }
  156. void PAD::addPadPrimitivesToPolygon( SHAPE_POLY_SET* aMergedPolygon, PCB_LAYER_ID aLayer,
  157. int aError, ERROR_LOC aErrorLoc ) const
  158. {
  159. SHAPE_POLY_SET polyset;
  160. for( const std::shared_ptr<PCB_SHAPE>& primitive : m_editPrimitives )
  161. primitive->TransformShapeWithClearanceToPolygon( polyset, aLayer, 0, aError, aErrorLoc );
  162. polyset.Simplify( SHAPE_POLY_SET::PM_FAST );
  163. // Merge all polygons with the initial pad anchor shape
  164. if( polyset.OutlineCount() )
  165. {
  166. aMergedPolygon->BooleanAdd( polyset, SHAPE_POLY_SET::PM_STRICTLY_SIMPLE );
  167. aMergedPolygon->Fracture( SHAPE_POLY_SET::PM_STRICTLY_SIMPLE );
  168. }
  169. }
  170. void PAD::MergePrimitivesAsPolygon( SHAPE_POLY_SET* aMergedPolygon, PCB_LAYER_ID aLayer,
  171. ERROR_LOC aErrorLoc ) const
  172. {
  173. const BOARD* board = GetBoard();
  174. int maxError = board ? board->GetDesignSettings().m_MaxError : ARC_HIGH_DEF;
  175. aMergedPolygon->RemoveAllContours();
  176. // Add the anchor pad shape in aMergedPolygon, others in aux_polyset:
  177. // The anchor pad is always at 0,0
  178. switch( GetAnchorPadShape() )
  179. {
  180. case PAD_SHAPE::RECT:
  181. {
  182. SHAPE_RECT rect( -GetSize().x / 2, -GetSize().y / 2, GetSize().x, GetSize().y );
  183. aMergedPolygon->AddOutline( rect.Outline() );
  184. }
  185. break;
  186. default:
  187. case PAD_SHAPE::CIRCLE:
  188. TransformCircleToPolygon( *aMergedPolygon, wxPoint( 0, 0 ), GetSize().x / 2, maxError,
  189. ERROR_INSIDE );
  190. break;
  191. }
  192. addPadPrimitivesToPolygon( aMergedPolygon, aLayer, maxError, aErrorLoc );
  193. }
  194. bool PAD::GetBestAnchorPosition( VECTOR2I& aPos )
  195. {
  196. SHAPE_POLY_SET poly;
  197. addPadPrimitivesToPolygon( &poly, UNDEFINED_LAYER, ARC_LOW_DEF, ERROR_INSIDE );
  198. if( poly.OutlineCount() > 1 )
  199. return false;
  200. const int minSteps = 10;
  201. const int maxSteps = 50;
  202. int stepsX, stepsY;
  203. auto bbox = poly.BBox();
  204. if( bbox.GetWidth() < bbox.GetHeight() )
  205. {
  206. stepsX = minSteps;
  207. stepsY = minSteps * (double) bbox.GetHeight() / (double )(bbox.GetWidth() + 1);
  208. }
  209. else
  210. {
  211. stepsY = minSteps;
  212. stepsX = minSteps * (double) bbox.GetWidth() / (double )(bbox.GetHeight() + 1);
  213. }
  214. stepsX = std::max(minSteps, std::min( maxSteps, stepsX ) );
  215. stepsY = std::max(minSteps, std::min( maxSteps, stepsY ) );
  216. VECTOR2I center = bbox.Centre();
  217. int64_t minDist = std::numeric_limits<int64_t>::max();
  218. int64_t minDistEdge;
  219. if( GetAnchorPadShape() == PAD_SHAPE::CIRCLE )
  220. {
  221. minDistEdge = GetSize().x;
  222. }
  223. else
  224. {
  225. minDistEdge = std::max( GetSize().x, GetSize().y );
  226. }
  227. OPT<VECTOR2I> bestAnchor( []()->OPT<VECTOR2I> { return NULLOPT; }() );
  228. for( int y = 0; y < stepsY ; y++ )
  229. {
  230. for( int x = 0; x < stepsX; x++ )
  231. {
  232. VECTOR2I p = bbox.GetPosition();
  233. p.x += rescale( x, bbox.GetWidth(), (stepsX - 1) );
  234. p.y += rescale( y, bbox.GetHeight(), (stepsY - 1) );
  235. if( poly.Contains(p) )
  236. {
  237. int dist = (center - p).EuclideanNorm();
  238. int distEdge = poly.COutline(0).Distance( p, true );
  239. if( distEdge >= minDistEdge )
  240. {
  241. if( dist < minDist )
  242. {
  243. bestAnchor = p;
  244. minDist = dist;
  245. }
  246. }
  247. }
  248. }
  249. }
  250. if( bestAnchor )
  251. {
  252. aPos = *bestAnchor;
  253. return true;
  254. }
  255. return false;
  256. }