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.

1400 lines
54 KiB

* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
12 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2009-2018 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 1992-2018 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 board_items_to_polygon_shape_transform.cpp
  26. * @brief function to convert shapes of items ( pads, tracks... ) to polygons
  27. */
  28. /* Function to convert pad and track shapes to polygons
  29. * Used to fill zones areas and in 3D viewer
  30. */
  31. #include <vector>
  32. #include <fctsys.h>
  33. #include <bezier_curves.h>
  34. #include <base_units.h> // for IU_PER_MM
  35. #include <draw_graphic_text.h>
  36. #include <pcbnew.h>
  37. #include <pcb_edit_frame.h>
  38. #include <trigo.h>
  39. #include <class_board.h>
  40. #include <class_pad.h>
  41. #include <class_track.h>
  42. #include <class_drawsegment.h>
  43. #include <class_pcb_text.h>
  44. #include <class_zone.h>
  45. #include <class_module.h>
  46. #include <class_edge_mod.h>
  47. #include <convert_basic_shapes_to_polygon.h>
  48. #include <geometry/geometry_utils.h>
  49. // A helper struct for the callback function
  50. // These variables are parameters used in addTextSegmToPoly.
  51. // But addTextSegmToPoly is a call-back function,
  52. // so we cannot send them as arguments.
  53. struct TSEGM_2_POLY_PRMS {
  54. int m_textWidth;
  55. int m_textCircle2SegmentCount;
  56. SHAPE_POLY_SET* m_cornerBuffer;
  57. };
  58. TSEGM_2_POLY_PRMS prms;
  59. // The max error is the distance between the middle of a segment, and the circle
  60. // for circle/arc to segment approximation.
  61. // Warning: too small values can create very long calculation time in zone filling
  62. // 0.05 to 0.01 mm is a reasonable value
  63. double s_error_max = Millimeter2iu( 0.02 );
  64. // This is a call back function, used by DrawGraphicText to draw the 3D text shape:
  65. static void addTextSegmToPoly( int x0, int y0, int xf, int yf, void* aData )
  66. {
  67. TSEGM_2_POLY_PRMS* prm = static_cast<TSEGM_2_POLY_PRMS*>( aData );
  68. TransformRoundedEndsSegmentToPolygon( *prm->m_cornerBuffer,
  69. wxPoint( x0, y0), wxPoint( xf, yf ),
  70. prm->m_textCircle2SegmentCount, prm->m_textWidth );
  71. }
  72. void BOARD::ConvertBrdLayerToPolygonalContours( PCB_LAYER_ID aLayer, SHAPE_POLY_SET& aOutlines )
  73. {
  74. // Number of segments to convert a circle to a polygon
  75. const int segcountforcircle = ARC_APPROX_SEGMENTS_COUNT_HIGH_DEF;
  76. double correctionFactor = GetCircletoPolyCorrectionFactor( segcountforcircle );
  77. // convert tracks and vias:
  78. for( TRACK* track = m_Track; track != NULL; track = track->Next() )
  79. {
  80. if( !track->IsOnLayer( aLayer ) )
  81. continue;
  82. track->TransformShapeWithClearanceToPolygon( aOutlines,
  83. 0, segcountforcircle, correctionFactor );
  84. }
  85. // convert pads
  86. for( MODULE* module = m_Modules; module != NULL; module = module->Next() )
  87. {
  88. module->TransformPadsShapesWithClearanceToPolygon( aLayer,
  89. aOutlines, 0, segcountforcircle, correctionFactor );
  90. // Micro-wave modules may have items on copper layers
  91. module->TransformGraphicShapesWithClearanceToPolygonSet( aLayer,
  92. aOutlines, 0, segcountforcircle, correctionFactor );
  93. }
  94. // convert copper zones
  95. for( int ii = 0; ii < GetAreaCount(); ii++ )
  96. {
  97. ZONE_CONTAINER* zone = GetArea( ii );
  98. PCB_LAYER_ID zonelayer = zone->GetLayer();
  99. if( zonelayer == aLayer )
  100. zone->TransformSolidAreasShapesToPolygonSet(
  101. aOutlines, segcountforcircle, correctionFactor );
  102. }
  103. // convert graphic items on copper layers (texts)
  104. for( BOARD_ITEM* item = m_Drawings; item; item = item->Next() )
  105. {
  106. if( !item->IsOnLayer( aLayer ) )
  107. continue;
  108. switch( item->Type() )
  109. {
  110. case PCB_LINE_T: // should not exist on copper layers
  111. ( (DRAWSEGMENT*) item )->TransformShapeWithClearanceToPolygon(
  112. aOutlines, 0, segcountforcircle, correctionFactor );
  113. break;
  114. case PCB_TEXT_T:
  115. ( (TEXTE_PCB*) item )->TransformShapeWithClearanceToPolygonSet(
  116. aOutlines, 0, segcountforcircle, correctionFactor );
  117. break;
  118. default:
  119. break;
  120. }
  121. }
  122. }
  123. void MODULE::TransformPadsShapesWithClearanceToPolygon( PCB_LAYER_ID aLayer,
  124. SHAPE_POLY_SET& aCornerBuffer,
  125. int aInflateValue,
  126. int aCircleToSegmentsCount,
  127. double aCorrectionFactor,
  128. bool aSkipNPTHPadsWihNoCopper ) const
  129. {
  130. D_PAD* pad = PadsList();
  131. wxSize margin;
  132. for( ; pad != NULL; pad = pad->Next() )
  133. {
  134. if( aLayer != UNDEFINED_LAYER && !pad->IsOnLayer(aLayer) )
  135. continue;
  136. // NPTH pads are not drawn on layers if the shape size and pos is the same
  137. // as their hole:
  138. if( aSkipNPTHPadsWihNoCopper && pad->GetAttribute() == PAD_ATTRIB_HOLE_NOT_PLATED )
  139. {
  140. if( pad->GetDrillSize() == pad->GetSize() && pad->GetOffset() == wxPoint( 0, 0 ) )
  141. {
  142. switch( pad->GetShape() )
  143. {
  144. case PAD_SHAPE_CIRCLE:
  145. if( pad->GetDrillShape() == PAD_DRILL_SHAPE_CIRCLE )
  146. continue;
  147. break;
  148. case PAD_SHAPE_OVAL:
  149. if( pad->GetDrillShape() != PAD_DRILL_SHAPE_CIRCLE )
  150. continue;
  151. break;
  152. default:
  153. break;
  154. }
  155. }
  156. }
  157. switch( aLayer )
  158. {
  159. case F_Mask:
  160. case B_Mask:
  161. margin.x = margin.y = pad->GetSolderMaskMargin() + aInflateValue;
  162. break;
  163. case F_Paste:
  164. case B_Paste:
  165. margin = pad->GetSolderPasteMargin();
  166. margin.x += aInflateValue;
  167. margin.y += aInflateValue;
  168. break;
  169. default:
  170. margin.x = margin.y = aInflateValue;
  171. break;
  172. }
  173. pad->BuildPadShapePolygon( aCornerBuffer, margin,
  174. aCircleToSegmentsCount, aCorrectionFactor );
  175. }
  176. }
  177. /* generate shapes of graphic items (outlines) on layer aLayer as polygons,
  178. * and adds these polygons to aCornerBuffer
  179. * aCornerBuffer = the buffer to store polygons
  180. * aInflateValue = a value to inflate shapes
  181. * aCircleToSegmentsCount = number of segments to approximate a circle
  182. * aCorrectionFactor = the correction to apply to the circle radius
  183. * to generate the polygon.
  184. * if aCorrectionFactor = 1.0, the polygon is inside the circle
  185. * the radius of circle approximated by segments is
  186. * initial radius * aCorrectionFactor
  187. */
  188. void MODULE::TransformGraphicShapesWithClearanceToPolygonSet(
  189. PCB_LAYER_ID aLayer,
  190. SHAPE_POLY_SET& aCornerBuffer,
  191. int aInflateValue,
  192. int aCircleToSegmentsCount,
  193. double aCorrectionFactor,
  194. int aCircleToSegmentsCountForTexts,
  195. bool aIncludeText ) const
  196. {
  197. std::vector<TEXTE_MODULE *> texts; // List of TEXTE_MODULE to convert
  198. EDGE_MODULE* outline;
  199. for( EDA_ITEM* item = GraphicalItemsList(); item != NULL; item = item->Next() )
  200. {
  201. switch( item->Type() )
  202. {
  203. case PCB_MODULE_TEXT_T:
  204. {
  205. TEXTE_MODULE* text = static_cast<TEXTE_MODULE*>( item );
  206. if( ( aLayer != UNDEFINED_LAYER && text->GetLayer() == aLayer )
  207. && text->IsVisible() )
  208. texts.push_back( text );
  209. break;
  210. }
  211. case PCB_MODULE_EDGE_T:
  212. outline = (EDGE_MODULE*) item;
  213. if( aLayer != UNDEFINED_LAYER && outline->GetLayer() != aLayer )
  214. break;
  215. outline->TransformShapeWithClearanceToPolygon( aCornerBuffer, 0,
  216. aCircleToSegmentsCount, aCorrectionFactor );
  217. break;
  218. default:
  219. break;
  220. }
  221. }
  222. if( !aIncludeText )
  223. return;
  224. // Convert texts sur modules
  225. if( Reference().GetLayer() == aLayer && Reference().IsVisible() )
  226. texts.push_back( &Reference() );
  227. if( Value().GetLayer() == aLayer && Value().IsVisible() )
  228. texts.push_back( &Value() );
  229. prms.m_cornerBuffer = &aCornerBuffer;
  230. // To allow optimization of circles approximated by segments,
  231. // aCircleToSegmentsCountForTexts, when not 0, is used.
  232. // if 0 (default value) the aCircleToSegmentsCount is used
  233. prms.m_textCircle2SegmentCount = aCircleToSegmentsCountForTexts ?
  234. aCircleToSegmentsCountForTexts : aCircleToSegmentsCount;
  235. for( unsigned ii = 0; ii < texts.size(); ii++ )
  236. {
  237. TEXTE_MODULE *textmod = texts[ii];
  238. prms.m_textWidth = textmod->GetThickness() + ( 2 * aInflateValue );
  239. wxSize size = textmod->GetTextSize();
  240. if( textmod->IsMirrored() )
  241. size.x = -size.x;
  242. DrawGraphicText( NULL, NULL, textmod->GetTextPos(), BLACK,
  243. textmod->GetShownText(), textmod->GetDrawRotation(), size,
  244. textmod->GetHorizJustify(), textmod->GetVertJustify(),
  245. textmod->GetThickness(), textmod->IsItalic(),
  246. true, addTextSegmToPoly, &prms );
  247. }
  248. }
  249. // Same as function TransformGraphicShapesWithClearanceToPolygonSet but
  250. // this only render text
  251. void MODULE::TransformGraphicTextWithClearanceToPolygonSet(
  252. PCB_LAYER_ID aLayer,
  253. SHAPE_POLY_SET& aCornerBuffer,
  254. int aInflateValue,
  255. int aCircleToSegmentsCount,
  256. double aCorrectionFactor,
  257. int aCircleToSegmentsCountForTexts ) const
  258. {
  259. std::vector<TEXTE_MODULE *> texts; // List of TEXTE_MODULE to convert
  260. for( EDA_ITEM* item = GraphicalItemsList(); item != NULL; item = item->Next() )
  261. {
  262. switch( item->Type() )
  263. {
  264. case PCB_MODULE_TEXT_T:
  265. {
  266. TEXTE_MODULE* text = static_cast<TEXTE_MODULE*>( item );
  267. if( text->GetLayer() == aLayer && text->IsVisible() )
  268. texts.push_back( text );
  269. break;
  270. }
  271. case PCB_MODULE_EDGE_T:
  272. // This function does not render this
  273. break;
  274. default:
  275. break;
  276. }
  277. }
  278. // Convert texts sur modules
  279. if( Reference().GetLayer() == aLayer && Reference().IsVisible() )
  280. texts.push_back( &Reference() );
  281. if( Value().GetLayer() == aLayer && Value().IsVisible() )
  282. texts.push_back( &Value() );
  283. prms.m_cornerBuffer = &aCornerBuffer;
  284. // To allow optimization of circles approximated by segments,
  285. // aCircleToSegmentsCountForTexts, when not 0, is used.
  286. // if 0 (default value) the aCircleToSegmentsCount is used
  287. prms.m_textCircle2SegmentCount = aCircleToSegmentsCountForTexts ?
  288. aCircleToSegmentsCountForTexts : aCircleToSegmentsCount;
  289. for( unsigned ii = 0; ii < texts.size(); ii++ )
  290. {
  291. TEXTE_MODULE *textmod = texts[ii];
  292. prms.m_textWidth = textmod->GetThickness() + ( 2 * aInflateValue );
  293. wxSize size = textmod->GetTextSize();
  294. if( textmod->IsMirrored() )
  295. size.x = -size.x;
  296. DrawGraphicText( NULL, NULL, textmod->GetTextPos(), BLACK,
  297. textmod->GetShownText(), textmod->GetDrawRotation(), size,
  298. textmod->GetHorizJustify(), textmod->GetVertJustify(),
  299. textmod->GetThickness(), textmod->IsItalic(),
  300. true, addTextSegmToPoly, &prms );
  301. }
  302. }
  303. /* Function TransformSolidAreasShapesToPolygonSet
  304. * Convert solid areas full shapes to polygon set
  305. * (the full shape is the polygon area with a thick outline)
  306. * Used in 3D view
  307. * Arcs (ends of segments) are approximated by segments
  308. * aCornerBuffer = a buffer to store the polygons
  309. * aCircleToSegmentsCount = the number of segments to approximate a circle
  310. * aCorrectionFactor = the correction to apply to arcs radius to roughly
  311. * keep arc radius when approximated by segments
  312. */
  313. void ZONE_CONTAINER::TransformSolidAreasShapesToPolygonSet(
  314. SHAPE_POLY_SET& aCornerBuffer,
  315. int aCircleToSegmentsCount,
  316. double aCorrectionFactor ) const
  317. {
  318. if( GetFilledPolysList().IsEmpty() )
  319. return;
  320. // add filled areas polygons
  321. aCornerBuffer.Append( m_FilledPolysList );
  322. // add filled areas outlines, which are drawn with thick lines
  323. for( int i = 0; i < m_FilledPolysList.OutlineCount(); i++ )
  324. {
  325. const SHAPE_LINE_CHAIN& path = m_FilledPolysList.COutline( i );
  326. for( int j = 0; j < path.PointCount(); j++ )
  327. {
  328. const VECTOR2I& a = path.CPoint( j );
  329. const VECTOR2I& b = path.CPoint( j + 1 );
  330. TransformRoundedEndsSegmentToPolygon( aCornerBuffer, wxPoint( a.x, a.y ), wxPoint( b.x, b.y ),
  331. aCircleToSegmentsCount,
  332. GetMinThickness() );
  333. }
  334. }
  335. }
  336. /**
  337. * Function TransformBoundingBoxWithClearanceToPolygon
  338. * Convert the text bounding box to a rectangular polygon
  339. * Used in filling zones calculations
  340. * Circles and arcs are approximated by segments
  341. * @param aCornerBuffer = a buffer to store the polygon
  342. * @param aClearanceValue = the clearance around the text bounding box
  343. */
  344. void EDA_TEXT::TransformBoundingBoxWithClearanceToPolygon(
  345. SHAPE_POLY_SET* aCornerBuffer,
  346. int aClearanceValue ) const
  347. {
  348. // Oh dear. When in UTF-8 mode, wxString puts string iterators in a linked list, and
  349. // that linked list is not thread-safe.
  350. std::lock_guard<std::mutex> guard( m_mutex );
  351. if( GetText().Length() == 0 )
  352. return;
  353. wxPoint corners[4]; // Buffer of polygon corners
  354. EDA_RECT rect = GetTextBox( -1 );
  355. rect.Inflate( aClearanceValue );
  356. corners[0].x = rect.GetOrigin().x;
  357. corners[0].y = rect.GetOrigin().y;
  358. corners[1].y = corners[0].y;
  359. corners[1].x = rect.GetRight();
  360. corners[2].x = corners[1].x;
  361. corners[2].y = rect.GetBottom();
  362. corners[3].y = corners[2].y;
  363. corners[3].x = corners[0].x;
  364. aCornerBuffer->NewOutline();
  365. for( int ii = 0; ii < 4; ii++ )
  366. {
  367. // Rotate polygon
  368. RotatePoint( &corners[ii].x, &corners[ii].y, GetTextPos().x, GetTextPos().y, GetTextAngle() );
  369. aCornerBuffer->Append( corners[ii].x, corners[ii].y );
  370. }
  371. }
  372. /* Function TransformShapeWithClearanceToPolygonSet
  373. * Convert the text shape to a set of polygons (one by segment)
  374. * Used in filling zones calculations and 3D view
  375. * Circles and arcs are approximated by segments
  376. * aCornerBuffer = SHAPE_POLY_SET to store the polygon corners
  377. * aClearanceValue = the clearance around the text
  378. * aCircleToSegmentsCount = the number of segments to approximate a circle
  379. * aCorrectionFactor = the correction to apply to circles radius to keep
  380. * clearance when the circle is approximated by segment bigger or equal
  381. * to the real clearance value (usually near from 1.0)
  382. */
  383. void TEXTE_PCB::TransformShapeWithClearanceToPolygonSet(
  384. SHAPE_POLY_SET& aCornerBuffer,
  385. int aClearanceValue,
  386. int aCircleToSegmentsCount,
  387. double aCorrectionFactor ) const
  388. {
  389. wxSize size = GetTextSize();
  390. if( IsMirrored() )
  391. size.x = -size.x;
  392. prms.m_cornerBuffer = &aCornerBuffer;
  393. prms.m_textWidth = GetThickness() + ( 2 * aClearanceValue );
  394. prms.m_textCircle2SegmentCount = aCircleToSegmentsCount;
  395. COLOR4D color = COLOR4D::BLACK; // not actually used, but needed by DrawGraphicText
  396. if( IsMultilineAllowed() )
  397. {
  398. wxArrayString strings_list;
  399. wxStringSplit( GetShownText(), strings_list, '\n' );
  400. std::vector<wxPoint> positions;
  401. positions.reserve( strings_list.Count() );
  402. GetPositionsOfLinesOfMultilineText( positions, strings_list.Count() );
  403. for( unsigned ii = 0; ii < strings_list.Count(); ii++ )
  404. {
  405. wxString txt = strings_list.Item( ii );
  406. DrawGraphicText( NULL, NULL, positions[ii], color,
  407. txt, GetTextAngle(), size,
  408. GetHorizJustify(), GetVertJustify(),
  409. GetThickness(), IsItalic(),
  410. true, addTextSegmToPoly, &prms );
  411. }
  412. }
  413. else
  414. {
  415. DrawGraphicText( NULL, NULL, GetTextPos(), color,
  416. GetShownText(), GetTextAngle(), size,
  417. GetHorizJustify(), GetVertJustify(),
  418. GetThickness(), IsItalic(),
  419. true, addTextSegmToPoly, &prms );
  420. }
  421. }
  422. /**
  423. * Function TransformShapeWithClearanceToPolygon
  424. * Convert the track shape to a closed polygon
  425. * Used in filling zones calculations
  426. * Circles and arcs are approximated by segments
  427. * @param aCornerBuffer = a buffer to store the polygon
  428. * @param aClearanceValue = the clearance around the pad
  429. * @param aCircleToSegmentsCount = the number of segments to approximate a circle
  430. * @param aCorrectionFactor = the correction to apply to circles radius to keep
  431. * clearance when the circle is approxiamted by segment bigger or equal
  432. * to the real clearance value (usually near from 1.0)
  433. * @param ignoreLineWidth = used for edge cut items where the line width is only
  434. * for visualization
  435. */
  436. void DRAWSEGMENT::TransformShapeWithClearanceToPolygon( SHAPE_POLY_SET& aCornerBuffer,
  437. int aClearanceValue,
  438. int aCircleToSegmentsCount,
  439. double aCorrectionFactor,
  440. bool ignoreLineWidth ) const
  441. {
  442. // The full width of the lines to create:
  443. int linewidth = ignoreLineWidth ? 0 : m_Width;
  444. linewidth += 2 * aClearanceValue;
  445. // Creating a reliable clearance shape for circles and arcs is not so easy, due to
  446. // the error created by segment approximation.
  447. // for a cicle this is not so hard: create a polygon from a circle slightly bigger:
  448. // thickness = linewidth + s_error_max, and radius = initial radius + s_error_max/2
  449. // giving a shape with a suitable internal radius and external radius
  450. // For an arc this is more tricky: TODO
  451. if( m_Shape == S_CIRCLE || m_Shape == S_ARC )
  452. {
  453. int segCount = GetArcToSegmentCount( GetRadius(), s_error_max, 360.0 );
  454. if( segCount > aCircleToSegmentsCount )
  455. aCircleToSegmentsCount = segCount;
  456. }
  457. switch( m_Shape )
  458. {
  459. case S_CIRCLE:
  460. TransformRingToPolygon( aCornerBuffer, GetCenter(), GetRadius() + (s_error_max/2),
  461. aCircleToSegmentsCount, linewidth + s_error_max ) ;
  462. break;
  463. case S_ARC:
  464. TransformArcToPolygon( aCornerBuffer, GetCenter(),
  465. GetArcStart(), m_Angle,
  466. aCircleToSegmentsCount, linewidth );
  467. break;
  468. case S_SEGMENT:
  469. TransformOvalClearanceToPolygon( aCornerBuffer, m_Start, m_End, linewidth,
  470. aCircleToSegmentsCount, aCorrectionFactor );
  471. break;
  472. case S_POLYGON:
  473. if( IsPolyShapeValid() )
  474. {
  475. // The polygon is expected to be a simple polygon
  476. // not self intersecting, no hole.
  477. MODULE* module = GetParentModule(); // NULL for items not in footprints
  478. double orientation = module ? module->GetOrientation() : 0.0;
  479. wxPoint offset;
  480. if( module )
  481. offset = module->GetPosition();
  482. // Build the polygon with the actual position and orientation:
  483. std::vector< wxPoint> poly;
  484. poly = BuildPolyPointsList();
  485. for( unsigned ii = 0; ii < poly.size(); ii++ )
  486. {
  487. RotatePoint( &poly[ii], orientation );
  488. poly[ii] += offset;
  489. }
  490. // If the polygon is not filled, treat it as a closed set of lines
  491. if( !IsPolygonFilled() )
  492. {
  493. for( size_t ii = 1; ii < poly.size(); ii++ )
  494. {
  495. TransformOvalClearanceToPolygon( aCornerBuffer, poly[ii - 1], poly[ii],
  496. linewidth, aCircleToSegmentsCount, aCorrectionFactor );
  497. }
  498. TransformOvalClearanceToPolygon( aCornerBuffer, poly.back(), poly.front(),
  499. linewidth, aCircleToSegmentsCount, aCorrectionFactor );
  500. break;
  501. }
  502. // Generate polygons for the outline + clearance
  503. // This code is compatible with a polygon with holes linked to external outline
  504. // by overlapping segments.
  505. // Insert the initial polygon:
  506. aCornerBuffer.NewOutline();
  507. for( unsigned ii = 0; ii < poly.size(); ii++ )
  508. aCornerBuffer.Append( poly[ii].x, poly[ii].y );
  509. if( linewidth ) // Add thick outlines
  510. {
  511. wxPoint corner1( poly[poly.size()-1] );
  512. for( unsigned ii = 0; ii < poly.size(); ii++ )
  513. {
  514. wxPoint corner2( poly[ii] );
  515. if( corner2 != corner1 )
  516. {
  517. TransformRoundedEndsSegmentToPolygon( aCornerBuffer,
  518. corner1, corner2, aCircleToSegmentsCount, linewidth );
  519. }
  520. corner1 = corner2;
  521. }
  522. }
  523. }
  524. break;
  525. case S_CURVE: // Bezier curve
  526. {
  527. std::vector<wxPoint> ctrlPoints = { m_Start, m_BezierC1, m_BezierC2, m_End };
  528. BEZIER_POLY converter( ctrlPoints );
  529. std::vector< wxPoint> poly;
  530. converter.GetPoly( poly, m_Width );
  531. for( unsigned ii = 1; ii < poly.size(); ii++ )
  532. {
  533. TransformRoundedEndsSegmentToPolygon( aCornerBuffer,
  534. poly[ii-1], poly[ii], aCircleToSegmentsCount, linewidth );
  535. }
  536. }
  537. break;
  538. default:
  539. break;
  540. }
  541. }
  542. /**
  543. * Function TransformShapeWithClearanceToPolygon
  544. * Convert the track shape to a closed polygon
  545. * Used in filling zones calculations
  546. * Circles (vias) and arcs (ends of tracks) are approximated by segments
  547. * @param aCornerBuffer = a buffer to store the polygon
  548. * @param aClearanceValue = the clearance around the pad
  549. * @param aCircleToSegmentsCount = the number of segments to approximate a circle
  550. * @param aCorrectionFactor = the correction to apply to circles radius to keep
  551. * clearance when the circle is approximated by segment bigger or equal
  552. * to the real clearance value (usually near from 1.0)
  553. * @param ignoreLineWidth = used for edge cut items where the line width is only
  554. * for visualization
  555. */
  556. void TRACK::TransformShapeWithClearanceToPolygon( SHAPE_POLY_SET& aCornerBuffer,
  557. int aClearanceValue,
  558. int aCircleToSegmentsCount,
  559. double aCorrectionFactor,
  560. bool ignoreLineWidth ) const
  561. {
  562. wxASSERT_MSG( !ignoreLineWidth, "IgnoreLineWidth has no meaning for tracks." );
  563. switch( Type() )
  564. {
  565. case PCB_VIA_T:
  566. {
  567. int radius = (m_Width / 2) + aClearanceValue;
  568. radius = KiROUND( radius * aCorrectionFactor );
  569. TransformCircleToPolygon( aCornerBuffer, m_Start, radius, aCircleToSegmentsCount );
  570. }
  571. break;
  572. default:
  573. TransformOvalClearanceToPolygon( aCornerBuffer, m_Start, m_End,
  574. m_Width + ( 2 * aClearanceValue),
  575. aCircleToSegmentsCount,
  576. aCorrectionFactor );
  577. break;
  578. }
  579. }
  580. /* Function TransformShapeWithClearanceToPolygon
  581. * Convert the pad shape to a closed polygon
  582. * Used in filling zones calculations and 3D view generation
  583. * Circles and arcs are approximated by segments
  584. * aCornerBuffer = a SHAPE_POLY_SET to store the polygon corners
  585. * aClearanceValue = the clearance around the pad
  586. * aCircleToSegmentsCount = the number of segments to approximate a circle
  587. * aCorrectionFactor = the correction to apply to circles radius to keep
  588. * clearance when the circle is approximated by segment bigger or equal
  589. * to the real clearance value (usually near from 1.0)
  590. * @param ignoreLineWidth = used for edge cut items where the line width is only
  591. * for visualization
  592. */
  593. void D_PAD::TransformShapeWithClearanceToPolygon( SHAPE_POLY_SET& aCornerBuffer,
  594. int aClearanceValue,
  595. int aCircleToSegmentsCount,
  596. double aCorrectionFactor,
  597. bool ignoreLineWidth ) const
  598. {
  599. wxASSERT_MSG( !ignoreLineWidth, "IgnoreLineWidth has no meaning for pads." );
  600. double angle = m_Orient;
  601. int dx = (m_Size.x / 2) + aClearanceValue;
  602. int dy = (m_Size.y / 2) + aClearanceValue;
  603. wxPoint padShapePos = ShapePos(); /* Note: for pad having a shape offset,
  604. * the pad position is NOT the shape position */
  605. switch( GetShape() )
  606. {
  607. case PAD_SHAPE_CIRCLE:
  608. dx = KiROUND( dx * aCorrectionFactor );
  609. TransformCircleToPolygon( aCornerBuffer, padShapePos, dx,
  610. aCircleToSegmentsCount );
  611. break;
  612. case PAD_SHAPE_OVAL:
  613. // An oval pad has the same shape as a segment with rounded ends
  614. {
  615. int width;
  616. wxPoint shape_offset;
  617. if( dy > dx ) // Oval pad X/Y ratio for choosing translation axis
  618. {
  619. shape_offset.y = dy - dx;
  620. width = dx * 2;
  621. }
  622. else //if( dy <= dx )
  623. {
  624. shape_offset.x = dy - dx;
  625. width = dy * 2;
  626. }
  627. RotatePoint( &shape_offset, angle );
  628. wxPoint start = padShapePos - shape_offset;
  629. wxPoint end = padShapePos + shape_offset;
  630. TransformOvalClearanceToPolygon( aCornerBuffer, start, end, width,
  631. aCircleToSegmentsCount, aCorrectionFactor );
  632. }
  633. break;
  634. case PAD_SHAPE_TRAPEZOID:
  635. case PAD_SHAPE_RECT:
  636. {
  637. wxPoint corners[4];
  638. BuildPadPolygon( corners, wxSize( 0, 0 ), angle );
  639. SHAPE_POLY_SET outline;
  640. outline.NewOutline();
  641. for( int ii = 0; ii < 4; ii++ )
  642. {
  643. corners[ii] += padShapePos;
  644. outline.Append( corners[ii].x, corners[ii].y );
  645. }
  646. int rounding_radius = int( aClearanceValue * aCorrectionFactor );
  647. outline.Inflate( rounding_radius, aCircleToSegmentsCount );
  648. aCornerBuffer.Append( outline );
  649. }
  650. break;
  651. case PAD_SHAPE_CHAMFERED_RECT:
  652. case PAD_SHAPE_ROUNDRECT:
  653. {
  654. SHAPE_POLY_SET outline;
  655. int clearance = int( aClearanceValue * aCorrectionFactor );
  656. int rounding_radius = GetRoundRectCornerRadius() + clearance;
  657. wxSize shapesize( m_Size );
  658. shapesize.x += clearance*2;
  659. shapesize.y += clearance*2;
  660. bool doChamfer = GetShape() == PAD_SHAPE_CHAMFERED_RECT;
  661. TransformRoundChamferedRectToPolygon( outline, padShapePos, shapesize, angle,
  662. rounding_radius,
  663. doChamfer ? GetChamferRectRatio() : 0.0,
  664. doChamfer ? GetChamferPositions() : 0,
  665. aCircleToSegmentsCount );
  666. aCornerBuffer.Append( outline );
  667. }
  668. break;
  669. case PAD_SHAPE_CUSTOM:
  670. {
  671. int clearance = KiROUND( aClearanceValue * aCorrectionFactor );
  672. SHAPE_POLY_SET outline; // Will contain the corners in board coordinates
  673. outline.Append( m_customShapeAsPolygon );
  674. CustomShapeAsPolygonToBoardPosition( &outline, GetPosition(), GetOrientation() );
  675. outline.Inflate( clearance, aCircleToSegmentsCount );
  676. aCornerBuffer.Append( outline );
  677. }
  678. break;
  679. }
  680. }
  681. /*
  682. * Function BuildPadShapePolygon
  683. * Build the Corner list of the polygonal shape,
  684. * depending on shape, extra size (clearance ...) pad and orientation
  685. * Note: for Round and oval pads this function is equivalent to
  686. * TransformShapeWithClearanceToPolygon, but not for other shapes
  687. */
  688. void D_PAD::BuildPadShapePolygon( SHAPE_POLY_SET& aCornerBuffer,
  689. wxSize aInflateValue, int aSegmentsPerCircle,
  690. double aCorrectionFactor ) const
  691. {
  692. wxPoint corners[4];
  693. wxPoint padShapePos = ShapePos(); /* Note: for pad having a shape offset,
  694. * the pad position is NOT the shape position */
  695. switch( GetShape() )
  696. {
  697. case PAD_SHAPE_CIRCLE:
  698. case PAD_SHAPE_OVAL:
  699. case PAD_SHAPE_ROUNDRECT:
  700. case PAD_SHAPE_CHAMFERED_RECT:
  701. {
  702. // We are using TransformShapeWithClearanceToPolygon to build the shape.
  703. // Currently, this method uses only the same inflate value for X and Y dirs.
  704. // so because here this is not the case, we use a inflated dummy pad to build
  705. // the polygonal shape
  706. // TODO: remove this dummy pad when TransformShapeWithClearanceToPolygon will use
  707. // a wxSize to inflate the pad size
  708. D_PAD dummy( *this );
  709. dummy.SetSize( GetSize() + aInflateValue + aInflateValue );
  710. dummy.TransformShapeWithClearanceToPolygon( aCornerBuffer, 0,
  711. aSegmentsPerCircle, aCorrectionFactor );
  712. }
  713. break;
  714. case PAD_SHAPE_TRAPEZOID:
  715. case PAD_SHAPE_RECT:
  716. aCornerBuffer.NewOutline();
  717. BuildPadPolygon( corners, aInflateValue, m_Orient );
  718. for( int ii = 0; ii < 4; ii++ )
  719. {
  720. corners[ii] += padShapePos; // Shift origin to position
  721. aCornerBuffer.Append( corners[ii].x, corners[ii].y );
  722. }
  723. break;
  724. case PAD_SHAPE_CUSTOM:
  725. // for a custom shape, that is in fact a polygon (with holes), we can use only a inflate value.
  726. // so use ( aInflateValue.x + aInflateValue.y ) / 2 as polygon inflate value.
  727. // (different values for aInflateValue.x and aInflateValue.y has no sense for a custom pad)
  728. TransformShapeWithClearanceToPolygon( aCornerBuffer,
  729. ( aInflateValue.x + aInflateValue.y ) / 2,
  730. aSegmentsPerCircle, aCorrectionFactor );
  731. break;
  732. }
  733. }
  734. /*
  735. * Function BuildPadDrillShapePolygon
  736. * Build the Corner list of the polygonal drill shape,
  737. * depending on shape pad hole and orientation
  738. * return false if the pad has no hole, true otherwise
  739. */
  740. bool D_PAD::BuildPadDrillShapePolygon( SHAPE_POLY_SET& aCornerBuffer,
  741. int aInflateValue, int aSegmentsPerCircle ) const
  742. {
  743. wxSize drillsize = GetDrillSize();
  744. if( !drillsize.x || !drillsize.y )
  745. return false;
  746. if( drillsize.x == drillsize.y ) // usual round hole
  747. {
  748. TransformCircleToPolygon( aCornerBuffer, GetPosition(),
  749. (drillsize.x / 2) + aInflateValue, aSegmentsPerCircle );
  750. }
  751. else // Oblong hole
  752. {
  753. wxPoint start, end;
  754. int width;
  755. GetOblongDrillGeometry( start, end, width );
  756. width += aInflateValue * 2;
  757. TransformRoundedEndsSegmentToPolygon( aCornerBuffer,
  758. GetPosition() + start, GetPosition() + end, aSegmentsPerCircle, width );
  759. }
  760. return true;
  761. }
  762. /**
  763. * Function CreateThermalReliefPadPolygon
  764. * Add holes around a pad to create a thermal relief
  765. * copper thickness is min (dx/2, aCopperWitdh) or min (dy/2, aCopperWitdh)
  766. * @param aCornerBuffer = a buffer to store the polygon
  767. * @param aPad = the current pad used to create the thermal shape
  768. * @param aThermalGap = gap in thermal shape
  769. * @param aCopperThickness = stubs thickness in thermal shape
  770. * @param aMinThicknessValue = min copper thickness allowed
  771. * @param aCircleToSegmentsCount = the number of segments to approximate a circle
  772. * @param aCorrectionFactor = the correction to apply to circles radius to keep
  773. * @param aThermalRot = for rond pads the rotation of thermal stubs (450 usually for 45 deg.)
  774. */
  775. /* thermal reliefs are created as 4 polygons.
  776. * each corner of a polygon if calculated for a pad at position 0, 0, orient 0,
  777. * and then moved and rotated acroding to the pad position and orientation
  778. */
  779. /*
  780. * Note 1: polygons are drawm using outlines witk a thickness = aMinThicknessValue
  781. * so shapes must take in account this outline thickness
  782. *
  783. * Note 2:
  784. * Trapezoidal pads are not considered here because they are very special case
  785. * and are used in microwave applications and they *DO NOT* have a thermal relief that
  786. * change the shape by creating stubs and destroy their properties.
  787. */
  788. void CreateThermalReliefPadPolygon( SHAPE_POLY_SET& aCornerBuffer,
  789. const D_PAD& aPad,
  790. int aThermalGap,
  791. int aCopperThickness,
  792. int aMinThicknessValue,
  793. int aCircleToSegmentsCount,
  794. double aCorrectionFactor,
  795. double aThermalRot )
  796. {
  797. wxPoint corner, corner_end;
  798. wxPoint padShapePos = aPad.ShapePos(); // Note: for pad having a shape offset,
  799. // the pad position is NOT the shape position
  800. wxSize copper_thickness;
  801. double delta = 3600.0 / aCircleToSegmentsCount; // rot angle in 0.1 degree
  802. /* Keep in account the polygon outline thickness
  803. * aThermalGap must be increased by aMinThicknessValue/2 because drawing external outline
  804. * with a thickness of aMinThicknessValue will reduce gap by aMinThicknessValue/2
  805. */
  806. aThermalGap += aMinThicknessValue / 2;
  807. /* Keep in account the polygon outline thickness
  808. * copper_thickness must be decreased by aMinThicknessValue because drawing outlines
  809. * with a thickness of aMinThicknessValue will increase real thickness by aMinThicknessValue
  810. */
  811. int dx = aPad.GetSize().x / 2;
  812. int dy = aPad.GetSize().y / 2;
  813. copper_thickness.x = std::min( aPad.GetSize().x, aCopperThickness ) - aMinThicknessValue;
  814. copper_thickness.y = std::min( aPad.GetSize().y, aCopperThickness ) - aMinThicknessValue;
  815. if( copper_thickness.x < 0 )
  816. copper_thickness.x = 0;
  817. if( copper_thickness.y < 0 )
  818. copper_thickness.y = 0;
  819. switch( aPad.GetShape() )
  820. {
  821. case PAD_SHAPE_CIRCLE: // Add 4 similar holes
  822. {
  823. /* we create 4 copper holes and put them in position 1, 2, 3 and 4
  824. * here is the area of the rectangular pad + its thermal gap
  825. * the 4 copper holes remove the copper in order to create the thermal gap
  826. * 4 ------ 1
  827. * | |
  828. * | |
  829. * | |
  830. * | |
  831. * 3 ------ 2
  832. * holes 2, 3, 4 are the same as hole 1, rotated 90, 180, 270 deg
  833. */
  834. // Build the hole pattern, for the hole in the X >0, Y > 0 plane:
  835. // The pattern roughtly is a 90 deg arc pie
  836. std::vector <wxPoint> corners_buffer;
  837. // Radius of outer arcs of the shape corrected for arc approximation by lines
  838. int outer_radius = KiROUND( (dx + aThermalGap) * aCorrectionFactor );
  839. // Crosspoint of thermal spoke sides, the first point of polygon buffer
  840. corners_buffer.push_back( wxPoint( copper_thickness.x / 2, copper_thickness.y / 2 ) );
  841. // Add an intermediate point on spoke sides, to allow a > 90 deg angle between side
  842. // and first seg of arc approx
  843. corner.x = copper_thickness.x / 2;
  844. int y = outer_radius - (aThermalGap / 4);
  845. corner.y = KiROUND( sqrt( ( (double) y * y - (double) corner.x * corner.x ) ) );
  846. if( aThermalRot != 0 )
  847. corners_buffer.push_back( corner );
  848. // calculate the starting point of the outter arc
  849. corner.x = copper_thickness.x / 2;
  850. corner.y = KiROUND( sqrt( ( (double) outer_radius * outer_radius ) -
  851. ( (double) corner.x * corner.x ) ) );
  852. RotatePoint( &corner, 90 ); // 9 degrees is the spoke fillet size
  853. // calculate the ending point of the outter arc
  854. corner_end.x = corner.y;
  855. corner_end.y = corner.x;
  856. // calculate intermediate points (y coordinate from corner.y to corner_end.y
  857. while( (corner.y > corner_end.y) && (corner.x < corner_end.x) )
  858. {
  859. corners_buffer.push_back( corner );
  860. RotatePoint( &corner, delta );
  861. }
  862. corners_buffer.push_back( corner_end );
  863. /* add an intermediate point, to avoid angles < 90 deg between last arc approx line
  864. * and radius line
  865. */
  866. corner.x = corners_buffer[1].y;
  867. corner.y = corners_buffer[1].x;
  868. corners_buffer.push_back( corner );
  869. // Now, add the 4 holes ( each is the pattern, rotated by 0, 90, 180 and 270 deg
  870. // aThermalRot = 450 (45.0 degrees orientation) work fine.
  871. double angle_pad = aPad.GetOrientation(); // Pad orientation
  872. double th_angle = aThermalRot;
  873. for( unsigned ihole = 0; ihole < 4; ihole++ )
  874. {
  875. aCornerBuffer.NewOutline();
  876. for( unsigned ii = 0; ii < corners_buffer.size(); ii++ )
  877. {
  878. corner = corners_buffer[ii];
  879. RotatePoint( &corner, th_angle + angle_pad ); // Rotate by segment angle and pad orientation
  880. corner += padShapePos;
  881. aCornerBuffer.Append( corner.x, corner.y );
  882. }
  883. th_angle += 900; // Note: th_angle in in 0.1 deg.
  884. }
  885. }
  886. break;
  887. case PAD_SHAPE_OVAL:
  888. {
  889. // Oval pad support along the lines of round and rectangular pads
  890. std::vector <wxPoint> corners_buffer; // Polygon buffer as vector
  891. dx = (aPad.GetSize().x / 2) + aThermalGap; // Cutout radius x
  892. dy = (aPad.GetSize().y / 2) + aThermalGap; // Cutout radius y
  893. wxPoint shape_offset;
  894. // We want to calculate an oval shape with dx > dy.
  895. // if this is not the case, exchange dx and dy, and rotate the shape 90 deg.
  896. int supp_angle = 0;
  897. if( dx < dy )
  898. {
  899. std::swap( dx, dy );
  900. supp_angle = 900;
  901. std::swap( copper_thickness.x, copper_thickness.y );
  902. }
  903. int deltasize = dx - dy; // = distance between shape position and the 2 demi-circle ends centre
  904. // here we have dx > dy
  905. // Radius of outer arcs of the shape:
  906. int outer_radius = dy; // The radius of the outer arc is radius end + aThermalGap
  907. // Some coordinate fiddling, depending on the shape offset direction
  908. shape_offset = wxPoint( deltasize, 0 );
  909. // Crosspoint of thermal spoke sides, the first point of polygon buffer
  910. corner.x = copper_thickness.x / 2;
  911. corner.y = copper_thickness.y / 2;
  912. corners_buffer.push_back( corner );
  913. // Arc start point calculation, the intersecting point of cutout arc and thermal spoke edge
  914. // If copper thickness is more than shape offset, we need to calculate arc intercept point.
  915. if( copper_thickness.x > deltasize )
  916. {
  917. corner.x = copper_thickness.x / 2;
  918. corner.y = KiROUND( sqrt( ( (double) outer_radius * outer_radius ) -
  919. ( (double) ( corner.x - delta ) * ( corner.x - deltasize ) ) ) );
  920. corner.x -= deltasize;
  921. /* creates an intermediate point, to have a > 90 deg angle
  922. * between the side and the first segment of arc approximation
  923. */
  924. wxPoint intpoint = corner;
  925. intpoint.y -= aThermalGap / 4;
  926. corners_buffer.push_back( intpoint + shape_offset );
  927. RotatePoint( &corner, 90 ); // 9 degrees of thermal fillet
  928. }
  929. else
  930. {
  931. corner.x = copper_thickness.x / 2;
  932. corner.y = outer_radius;
  933. corners_buffer.push_back( corner );
  934. }
  935. // Add an intermediate point on spoke sides, to allow a > 90 deg angle between side
  936. // and first seg of arc approx
  937. wxPoint last_corner;
  938. last_corner.y = copper_thickness.y / 2;
  939. int px = outer_radius - (aThermalGap / 4);
  940. last_corner.x =
  941. KiROUND( sqrt( ( ( (double) px * px ) - (double) last_corner.y * last_corner.y ) ) );
  942. // Arc stop point calculation, the intersecting point of cutout arc and thermal spoke edge
  943. corner_end.y = copper_thickness.y / 2;
  944. corner_end.x =
  945. KiROUND( sqrt( ( (double) outer_radius *
  946. outer_radius ) - ( (double) corner_end.y * corner_end.y ) ) );
  947. RotatePoint( &corner_end, -90 ); // 9 degrees of thermal fillet
  948. // calculate intermediate arc points till limit is reached
  949. while( (corner.y > corner_end.y) && (corner.x < corner_end.x) )
  950. {
  951. corners_buffer.push_back( corner + shape_offset );
  952. RotatePoint( &corner, delta );
  953. }
  954. //corners_buffer.push_back(corner + shape_offset); // TODO: about one mil geometry error forms somewhere.
  955. corners_buffer.push_back( corner_end + shape_offset );
  956. corners_buffer.push_back( last_corner + shape_offset ); // Enabling the line above shows intersection point.
  957. /* Create 2 holes, rotated by pad rotation.
  958. */
  959. double angle = aPad.GetOrientation() + supp_angle;
  960. for( int irect = 0; irect < 2; irect++ )
  961. {
  962. aCornerBuffer.NewOutline();
  963. for( unsigned ic = 0; ic < corners_buffer.size(); ic++ )
  964. {
  965. wxPoint cpos = corners_buffer[ic];
  966. RotatePoint( &cpos, angle );
  967. cpos += padShapePos;
  968. aCornerBuffer.Append( cpos.x, cpos.y );
  969. }
  970. angle = AddAngles( angle, 1800 ); // this is calculate hole 3
  971. }
  972. // Create holes, that are the mirrored from the previous holes
  973. for( unsigned ic = 0; ic < corners_buffer.size(); ic++ )
  974. {
  975. wxPoint swap = corners_buffer[ic];
  976. swap.x = -swap.x;
  977. corners_buffer[ic] = swap;
  978. }
  979. // Now add corner 4 and 2 (2 is the corner 4 rotated by 180 deg
  980. angle = aPad.GetOrientation() + supp_angle;
  981. for( int irect = 0; irect < 2; irect++ )
  982. {
  983. aCornerBuffer.NewOutline();
  984. for( unsigned ic = 0; ic < corners_buffer.size(); ic++ )
  985. {
  986. wxPoint cpos = corners_buffer[ic];
  987. RotatePoint( &cpos, angle );
  988. cpos += padShapePos;
  989. aCornerBuffer.Append( cpos.x, cpos.y );
  990. }
  991. angle = AddAngles( angle, 1800 );
  992. }
  993. }
  994. break;
  995. case PAD_SHAPE_CHAMFERED_RECT:
  996. case PAD_SHAPE_ROUNDRECT: // thermal shape is the same for rectangular shapes.
  997. case PAD_SHAPE_RECT:
  998. {
  999. /* we create 4 copper holes and put them in position 1, 2, 3 and 4
  1000. * here is the area of the rectangular pad + its thermal gap
  1001. * the 4 copper holes remove the copper in order to create the thermal gap
  1002. * 1 ------ 4
  1003. * | |
  1004. * | |
  1005. * | |
  1006. * | |
  1007. * 2 ------ 3
  1008. * hole 3 is the same as hole 1, rotated 180 deg
  1009. * hole 4 is the same as hole 2, rotated 180 deg and is the same as hole 1, mirrored
  1010. */
  1011. // First, create a rectangular hole for position 1 :
  1012. // 2 ------- 3
  1013. // | |
  1014. // | |
  1015. // | |
  1016. // 1 -------4
  1017. // Modified rectangles with one corner rounded. TODO: merging with oval thermals
  1018. // and possibly round too.
  1019. std::vector <wxPoint> corners_buffer; // Polygon buffer as vector
  1020. dx = (aPad.GetSize().x / 2) + aThermalGap; // Cutout radius x
  1021. dy = (aPad.GetSize().y / 2) + aThermalGap; // Cutout radius y
  1022. // calculation is optimized for pad shape with dy >= dx (vertical rectangle).
  1023. // if it is not the case, just rotate this shape 90 degrees:
  1024. double angle = aPad.GetOrientation();
  1025. wxPoint corner_origin_pos( -aPad.GetSize().x / 2, -aPad.GetSize().y / 2 );
  1026. if( dy < dx )
  1027. {
  1028. std::swap( dx, dy );
  1029. std::swap( copper_thickness.x, copper_thickness.y );
  1030. std::swap( corner_origin_pos.x, corner_origin_pos.y );
  1031. angle += 900.0;
  1032. }
  1033. // Now calculate the hole pattern in position 1 ( top left pad corner )
  1034. // The first point of polygon buffer is left lower corner, second the crosspoint of
  1035. // thermal spoke sides, the third is upper right corner and the rest are rounding
  1036. // vertices going anticlockwise. Note the inverted Y-axis in corners_buffer y coordinates.
  1037. wxPoint arc_end_point( -dx, -(aThermalGap / 4 + copper_thickness.y / 2) );
  1038. corners_buffer.push_back( arc_end_point ); // Adds small miters to zone
  1039. corners_buffer.push_back( wxPoint( -(dx - aThermalGap / 4), -copper_thickness.y / 2 ) ); // fill and spoke corner
  1040. corners_buffer.push_back( wxPoint( -copper_thickness.x / 2, -copper_thickness.y / 2 ) );
  1041. corners_buffer.push_back( wxPoint( -copper_thickness.x / 2, -(dy - aThermalGap / 4) ) );
  1042. // The first point to build the rounded corner:
  1043. wxPoint arc_start_point( -(aThermalGap / 4 + copper_thickness.x / 2) , -dy );
  1044. corners_buffer.push_back( arc_start_point );
  1045. int rounding_radius = KiROUND( aThermalGap * aCorrectionFactor ); // Corner rounding radius
  1046. // Calculate arc angle parameters.
  1047. // the start angle id near 900 decidegrees, the final angle is near 1800.0 decidegrees.
  1048. double arc_increment = 3600.0 / aCircleToSegmentsCount;
  1049. // the arc_angle_start is 900.0 or slighly more, depending on the actual arc starting point
  1050. double arc_angle_start = atan2( -arc_start_point.y -corner_origin_pos.y, arc_start_point.x - corner_origin_pos.x ) * 1800/M_PI;
  1051. if( arc_angle_start < 900.0 )
  1052. arc_angle_start = 900.0;
  1053. bool first_point = true;
  1054. for( double curr_angle = arc_angle_start; ; curr_angle += arc_increment )
  1055. {
  1056. wxPoint corner_position = wxPoint( rounding_radius, 0 );
  1057. RotatePoint( &corner_position, curr_angle ); // Rounding vector rotation
  1058. corner_position += corner_origin_pos; // Rounding vector + Pad corner offset
  1059. // The arc angle is <= 90 degrees, therefore the arc is finished if the x coordinate
  1060. // decrease or the y coordinate is smaller than the y end point
  1061. if( !first_point &&
  1062. ( corner_position.x >= corners_buffer.back().x || corner_position.y > arc_end_point.y ) )
  1063. break;
  1064. first_point = false;
  1065. // Note: for hole in position 1, arc x coordinate is always < x starting point
  1066. // and arc y coordinate is always <= y ending point
  1067. if( corner_position != corners_buffer.back() // avoid duplicate corners.
  1068. && corner_position.x <= arc_start_point.x ) // skip current point at the right of the starting point
  1069. corners_buffer.push_back( corner_position );
  1070. }
  1071. for( int irect = 0; irect < 2; irect++ )
  1072. {
  1073. aCornerBuffer.NewOutline();
  1074. for( unsigned ic = 0; ic < corners_buffer.size(); ic++ )
  1075. {
  1076. wxPoint cpos = corners_buffer[ic];
  1077. RotatePoint( &cpos, angle ); // Rotate according to module orientation
  1078. cpos += padShapePos; // Shift origin to position
  1079. aCornerBuffer.Append( cpos.x, cpos.y );
  1080. }
  1081. angle = AddAngles( angle, 1800 ); // this is calculate hole 3
  1082. }
  1083. // Create holes, that are the mirrored from the previous holes
  1084. for( unsigned ic = 0; ic < corners_buffer.size(); ic++ )
  1085. {
  1086. wxPoint swap = corners_buffer[ic];
  1087. swap.x = -swap.x;
  1088. corners_buffer[ic] = swap;
  1089. }
  1090. // Now add corner 4 and 2 (2 is the corner 4 rotated by 180 deg
  1091. for( int irect = 0; irect < 2; irect++ )
  1092. {
  1093. aCornerBuffer.NewOutline();
  1094. for( unsigned ic = 0; ic < corners_buffer.size(); ic++ )
  1095. {
  1096. wxPoint cpos = corners_buffer[ic];
  1097. RotatePoint( &cpos, angle );
  1098. cpos += padShapePos;
  1099. aCornerBuffer.Append( cpos.x, cpos.y );
  1100. }
  1101. angle = AddAngles( angle, 1800 );
  1102. }
  1103. }
  1104. break;
  1105. case PAD_SHAPE_TRAPEZOID:
  1106. {
  1107. SHAPE_POLY_SET antipad; // The full antipad area
  1108. // We need a length to build the stubs of the thermal reliefs
  1109. // the value is not very important. The pad bounding box gives a reasonable value
  1110. EDA_RECT bbox = aPad.GetBoundingBox();
  1111. int stub_len = std::max( bbox.GetWidth(), bbox.GetHeight() );
  1112. aPad.TransformShapeWithClearanceToPolygon( antipad, aThermalGap,
  1113. aCircleToSegmentsCount, aCorrectionFactor );
  1114. SHAPE_POLY_SET stub; // A basic stub ( a rectangle)
  1115. SHAPE_POLY_SET stubs; // the full stubs shape
  1116. // We now substract the stubs (connections to the copper zone)
  1117. //ClipperLib::Clipper clip_engine;
  1118. // Prepare a clipping transform
  1119. //clip_engine.AddPath( antipad, ClipperLib::ptSubject, true );
  1120. // Create stubs and add them to clipper engine
  1121. wxPoint stubBuffer[4];
  1122. stubBuffer[0].x = stub_len;
  1123. stubBuffer[0].y = copper_thickness.y/2;
  1124. stubBuffer[1] = stubBuffer[0];
  1125. stubBuffer[1].y = -copper_thickness.y/2;
  1126. stubBuffer[2] = stubBuffer[1];
  1127. stubBuffer[2].x = -stub_len;
  1128. stubBuffer[3] = stubBuffer[2];
  1129. stubBuffer[3].y = copper_thickness.y/2;
  1130. stub.NewOutline();
  1131. for( unsigned ii = 0; ii < arrayDim( stubBuffer ); ii++ )
  1132. {
  1133. wxPoint cpos = stubBuffer[ii];
  1134. RotatePoint( &cpos, aPad.GetOrientation() );
  1135. cpos += padShapePos;
  1136. stub.Append( cpos.x, cpos.y );
  1137. }
  1138. stubs.Append( stub );
  1139. stubBuffer[0].y = stub_len;
  1140. stubBuffer[0].x = copper_thickness.x/2;
  1141. stubBuffer[1] = stubBuffer[0];
  1142. stubBuffer[1].x = -copper_thickness.x/2;
  1143. stubBuffer[2] = stubBuffer[1];
  1144. stubBuffer[2].y = -stub_len;
  1145. stubBuffer[3] = stubBuffer[2];
  1146. stubBuffer[3].x = copper_thickness.x/2;
  1147. stub.RemoveAllContours();
  1148. stub.NewOutline();
  1149. for( unsigned ii = 0; ii < arrayDim( stubBuffer ); ii++ )
  1150. {
  1151. wxPoint cpos = stubBuffer[ii];
  1152. RotatePoint( &cpos, aPad.GetOrientation() );
  1153. cpos += padShapePos;
  1154. stub.Append( cpos.x, cpos.y );
  1155. }
  1156. stubs.Append( stub );
  1157. stubs.Simplify( SHAPE_POLY_SET::PM_FAST );
  1158. antipad.BooleanSubtract( stubs, SHAPE_POLY_SET::PM_FAST );
  1159. aCornerBuffer.Append( antipad );
  1160. break;
  1161. }
  1162. default:
  1163. ;
  1164. }
  1165. }
  1166. void ZONE_CONTAINER::TransformShapeWithClearanceToPolygon( SHAPE_POLY_SET& aCornerBuffer,
  1167. int aClearanceValue,
  1168. int aCircleToSegmentsCount,
  1169. double aCorrectionFactor,
  1170. bool ignoreLineWidth ) const
  1171. {
  1172. wxASSERT_MSG( !ignoreLineWidth, "IgnoreLineWidth has no meaning for zones." );
  1173. aCornerBuffer = m_FilledPolysList;
  1174. aCornerBuffer.Simplify( SHAPE_POLY_SET::PM_STRICTLY_SIMPLE );
  1175. }