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.

904 lines
32 KiB

// Dick Hollenbeck's KiROUND R&D // This provides better project control over rounding to int from double // than wxRound() did. This scheme provides better logging in Debug builds // and it provides for compile time calculation of constants. #include <stdio.h> #include <assert.h> #include <limits.h> //-----<KiROUND KIT>------------------------------------------------------------ /** * KiROUND * rounds a floating point number to an int using * "round halfway cases away from zero". * In Debug build an assert fires if will not fit into an int. */ #if defined( DEBUG ) // DEBUG: a macro to capture line and file, then calls this inline static inline int KiRound( double v, int line, const char* filename ) { v = v < 0 ? v - 0.5 : v + 0.5; if( v > INT_MAX + 0.5 ) { printf( "%s: in file %s on line %d, val: %.16g too ' > 0 ' for int\n", __FUNCTION__, filename, line, v ); } else if( v < INT_MIN - 0.5 ) { printf( "%s: in file %s on line %d, val: %.16g too ' < 0 ' for int\n", __FUNCTION__, filename, line, v ); } return int( v ); } #define KiROUND( v ) KiRound( v, __LINE__, __FILE__ ) #else // RELEASE: a macro so compile can pre-compute constants. #define KiROUND( v ) int( (v) < 0 ? (v) - 0.5 : (v) + 0.5 ) #endif //-----</KiROUND KIT>----------------------------------------------------------- // Only a macro is compile time calculated, an inline function causes a static constructor // in a situation like this. // Therefore the Release build is best done with a MACRO not an inline function. int Computed = KiROUND( 14.3 * 8 ); int main( int argc, char** argv ) { for( double d = double(INT_MAX)-1; d < double(INT_MAX)+8; d += 2.0 ) { int i = KiROUND( d ); printf( "t: %d %.16g\n", i, d ); } return 0; }
14 years ago
// Dick Hollenbeck's KiROUND R&D // This provides better project control over rounding to int from double // than wxRound() did. This scheme provides better logging in Debug builds // and it provides for compile time calculation of constants. #include <stdio.h> #include <assert.h> #include <limits.h> //-----<KiROUND KIT>------------------------------------------------------------ /** * KiROUND * rounds a floating point number to an int using * "round halfway cases away from zero". * In Debug build an assert fires if will not fit into an int. */ #if defined( DEBUG ) // DEBUG: a macro to capture line and file, then calls this inline static inline int KiRound( double v, int line, const char* filename ) { v = v < 0 ? v - 0.5 : v + 0.5; if( v > INT_MAX + 0.5 ) { printf( "%s: in file %s on line %d, val: %.16g too ' > 0 ' for int\n", __FUNCTION__, filename, line, v ); } else if( v < INT_MIN - 0.5 ) { printf( "%s: in file %s on line %d, val: %.16g too ' < 0 ' for int\n", __FUNCTION__, filename, line, v ); } return int( v ); } #define KiROUND( v ) KiRound( v, __LINE__, __FILE__ ) #else // RELEASE: a macro so compile can pre-compute constants. #define KiROUND( v ) int( (v) < 0 ? (v) - 0.5 : (v) + 0.5 ) #endif //-----</KiROUND KIT>----------------------------------------------------------- // Only a macro is compile time calculated, an inline function causes a static constructor // in a situation like this. // Therefore the Release build is best done with a MACRO not an inline function. int Computed = KiROUND( 14.3 * 8 ); int main( int argc, char** argv ) { for( double d = double(INT_MAX)-1; d < double(INT_MAX)+8; d += 2.0 ) { int i = KiROUND( d ); printf( "t: %d %.16g\n", i, d ); } return 0; }
14 years ago
  1. /**
  2. * @file aperture_macro.cpp
  3. */
  4. /*
  5. * This program source code file is part of KiCad, a free EDA CAD application.
  6. *
  7. * Copyright (C) 1992-2017 Jean-Pierre Charras <jp.charras at wanadoo.fr>
  8. * Copyright (C) 2010 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
  9. * Copyright (C) 1992-2020 KiCad Developers, see AUTHORS.txt for contributors.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version 2
  14. * of the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, you may find one here:
  23. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  24. * or you may search the http://www.gnu.org website for the version 2 license,
  25. * or you may write to the Free Software Foundation, Inc.,
  26. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  27. */
  28. #include <trigo.h>
  29. #include <convert_to_biu.h>
  30. #include <convert_basic_shapes_to_polygon.h>
  31. #include <math/util.h> // for KiROUND
  32. #include <gerbview.h>
  33. #include <gerber_file_image.h>
  34. /**
  35. * Function scaletoIU
  36. * converts a distance given in floating point to our internal units
  37. */
  38. extern int scaletoIU( double aCoord, bool isMetric ); // defined it rs274d_read_XY_and_IJ_coordiantes.cpp
  39. /**
  40. * Function mapPt
  41. * translates a point from the aperture macro coordinate system to our
  42. * deci-mils coordinate system.
  43. * @return wxPoint - The GerbView coordinate system vector.
  44. */
  45. static wxPoint mapPt( double x, double y, bool isMetric )
  46. {
  47. wxPoint ret( scaletoIU( x, isMetric ), scaletoIU( y, isMetric ) );
  48. return ret;
  49. }
  50. bool AM_PRIMITIVE::IsAMPrimitiveExposureOn( const GERBER_DRAW_ITEM* aParent ) const
  51. {
  52. /*
  53. * Some but not all primitives use the first parameter as an exposure control.
  54. * Others are always ON.
  55. * In a aperture macro shape, a basic primitive with exposure off is a hole in the shape
  56. * it is NOT a negative shape
  57. */
  58. wxASSERT( params.size() );
  59. switch( primitive_id )
  60. {
  61. case AMP_CIRCLE:
  62. case AMP_LINE2:
  63. case AMP_LINE20:
  64. case AMP_LINE_CENTER:
  65. case AMP_LINE_LOWER_LEFT:
  66. case AMP_OUTLINE:
  67. case AMP_POLYGON:
  68. // All have an exposure parameter and can return a value (0 or 1)
  69. return params[0].GetValue( aParent->GetDcodeDescr() ) != 0;
  70. break;
  71. case AMP_THERMAL: // Exposure is always on
  72. case AMP_MOIRE: // Exposure is always on
  73. case AMP_EOF:
  74. case AMP_UNKNOWN:
  75. default:
  76. return 1; // All have no exposure parameter and are always 0N return true
  77. break;
  78. }
  79. }
  80. // TODO(snh): Remove hard coded count
  81. const int seg_per_circle = 64; // Number of segments to approximate a circle
  82. void AM_PRIMITIVE::DrawBasicShape( const GERBER_DRAW_ITEM* aParent,
  83. SHAPE_POLY_SET& aShapeBuffer,
  84. wxPoint aShapePos )
  85. {
  86. #define TO_POLY_SHAPE { aShapeBuffer.NewOutline(); \
  87. for( unsigned jj = 0; jj < polybuffer.size(); jj++ )\
  88. aShapeBuffer.Append( polybuffer[jj].x, polybuffer[jj].y );\
  89. aShapeBuffer.Append( polybuffer[0].x, polybuffer[0].y );}
  90. // Draw the primitive shape for flashed items.
  91. static std::vector<wxPoint> polybuffer; // create a static buffer to avoid a lot of memory reallocation
  92. polybuffer.clear();
  93. wxPoint curPos = aShapePos;
  94. D_CODE* tool = aParent->GetDcodeDescr();
  95. double rotation;
  96. switch( primitive_id )
  97. {
  98. case AMP_CIRCLE: // Circle, given diameter and position
  99. {
  100. /* Generated by an aperture macro declaration like:
  101. * "1,1,0.3,0.5, 1.0*"
  102. * type (1), exposure, diameter, pos.x, pos.y, <rotation>
  103. * <rotation> is a optional parameter: rotation from origin.
  104. * type is not stored in parameters list, so the first parameter is exposure
  105. */
  106. ConvertShapeToPolygon( aParent, polybuffer );
  107. // shape rotation (if any):
  108. if( params.size() >= 5 )
  109. {
  110. rotation = params[4].GetValue( tool ) * 10.0;
  111. if( rotation != 0)
  112. {
  113. for( unsigned ii = 0; ii < polybuffer.size(); ii++ )
  114. RotatePoint( &polybuffer[ii], -rotation );
  115. }
  116. }
  117. // Move to current position:
  118. for( unsigned ii = 0; ii < polybuffer.size(); ii++ )
  119. {
  120. polybuffer[ii] += curPos;
  121. polybuffer[ii] = aParent->GetABPosition( polybuffer[ii] );
  122. }
  123. TO_POLY_SHAPE;
  124. }
  125. break;
  126. case AMP_LINE2:
  127. case AMP_LINE20: // Line with rectangle ends. (Width, start and end pos + rotation)
  128. {
  129. /* Vector Line, Primitive Code 20.
  130. * A vector line is a rectangle defined by its line width, start and end points.
  131. * The line ends are rectangular.
  132. */
  133. /* Generated by an aperture macro declaration like:
  134. * "2,1,0.3,0,0, 0.5, 1.0,-135*"
  135. * type (2), exposure, width, start.x, start.y, end.x, end.y, rotation
  136. * type is not stored in parameters list, so the first parameter is exposure
  137. */
  138. ConvertShapeToPolygon( aParent, polybuffer );
  139. // shape rotation:
  140. rotation = params[6].GetValue( tool ) * 10.0;
  141. if( rotation != 0)
  142. {
  143. for( unsigned ii = 0; ii < polybuffer.size(); ii++ )
  144. RotatePoint( &polybuffer[ii], -rotation );
  145. }
  146. // Move to current position:
  147. for( unsigned ii = 0; ii < polybuffer.size(); ii++ )
  148. {
  149. polybuffer[ii] += curPos;
  150. polybuffer[ii] = aParent->GetABPosition( polybuffer[ii] );
  151. }
  152. TO_POLY_SHAPE;
  153. }
  154. break;
  155. case AMP_LINE_CENTER:
  156. {
  157. /* Center Line, Primitive Code 21
  158. * A center line primitive is a rectangle defined by its width, height, and center point
  159. */
  160. /* Generated by an aperture macro declaration like:
  161. * "21,1,0.3,0.03,0,0,-135*"
  162. * type (21), exposure, ,width, height, center pos.x, center pos.y, rotation
  163. * type is not stored in parameters list, so the first parameter is exposure
  164. */
  165. ConvertShapeToPolygon( aParent, polybuffer );
  166. // shape rotation:
  167. rotation = params[5].GetValue( tool ) * 10.0;
  168. if( rotation != 0 )
  169. {
  170. for( unsigned ii = 0; ii < polybuffer.size(); ii++ )
  171. RotatePoint( &polybuffer[ii], -rotation );
  172. }
  173. // Move to current position:
  174. for( unsigned ii = 0; ii < polybuffer.size(); ii++ )
  175. {
  176. polybuffer[ii] += curPos;
  177. polybuffer[ii] = aParent->GetABPosition( polybuffer[ii] );
  178. }
  179. TO_POLY_SHAPE;
  180. }
  181. break;
  182. case AMP_LINE_LOWER_LEFT:
  183. {
  184. /* Generated by an aperture macro declaration like:
  185. * "22,1,0.3,0.03,0,0,-135*"
  186. * type (22), exposure, ,width, height, corner pos.x, corner pos.y, rotation
  187. * type is not stored in parameters list, so the first parameter is exposure
  188. */
  189. ConvertShapeToPolygon( aParent, polybuffer );
  190. // shape rotation:
  191. rotation = params[5].GetValue( tool ) * 10.0;
  192. if( rotation != 0)
  193. {
  194. for( unsigned ii = 0; ii < polybuffer.size(); ii++ )
  195. RotatePoint( &polybuffer[ii], -rotation );
  196. }
  197. // Move to current position:
  198. for( unsigned ii = 0; ii < polybuffer.size(); ii++ )
  199. {
  200. polybuffer[ii] += curPos;
  201. polybuffer[ii] = aParent->GetABPosition( polybuffer[ii] );
  202. }
  203. TO_POLY_SHAPE;
  204. }
  205. break;
  206. case AMP_THERMAL:
  207. {
  208. /* Generated by an aperture macro declaration like:
  209. * "7, 0,0,1.0,0.3,0.01,-13*"
  210. * type (7), center.x , center.y, outside diam, inside diam, crosshair thickness, rotation
  211. * type is not stored in parameters list, so the first parameter is center.x
  212. *
  213. * The thermal primitive is a ring (annulus) interrupted by four gaps. Exposure is always on.
  214. */
  215. std::vector<wxPoint> subshape_poly;
  216. curPos += mapPt( params[0].GetValue( tool ), params[1].GetValue( tool ), m_GerbMetric );
  217. ConvertShapeToPolygon( aParent, subshape_poly );
  218. // shape rotation:
  219. rotation = params[5].GetValue( tool ) * 10.0;
  220. // Because a thermal shape has 4 identical sub-shapes, only one is created in subshape_poly.
  221. // We must draw 4 sub-shapes rotated by 90 deg
  222. for( int ii = 0; ii < 4; ii++ )
  223. {
  224. polybuffer = subshape_poly;
  225. double sub_rotation = rotation + 900 * ii;
  226. for( unsigned jj = 0; jj < polybuffer.size(); jj++ )
  227. RotatePoint( &polybuffer[jj], -sub_rotation );
  228. // Move to current position:
  229. for( unsigned jj = 0; jj < polybuffer.size(); jj++ )
  230. {
  231. polybuffer[jj] += curPos;
  232. polybuffer[jj] = aParent->GetABPosition( polybuffer[jj] );
  233. }
  234. TO_POLY_SHAPE;
  235. }
  236. }
  237. break;
  238. case AMP_MOIRE:
  239. {
  240. /* Moire, Primitive Code 6
  241. * The moire primitive is a cross hair centered on concentric rings (annuli).
  242. * Exposure is always on.
  243. */
  244. curPos += mapPt( params[0].GetValue( tool ), params[1].GetValue( tool ),
  245. m_GerbMetric );
  246. /* Generated by an aperture macro declaration like:
  247. * "6,0,0,0.125,.01,0.01,3,0.003,0.150,0"
  248. * type(6), pos.x, pos.y, diam, penwidth, gap, circlecount, crosshair thickness, crosshaire len, rotation
  249. * type is not stored in parameters list, so the first parameter is pos.x
  250. */
  251. int outerDiam = scaletoIU( params[2].GetValue( tool ), m_GerbMetric );
  252. int penThickness = scaletoIU( params[3].GetValue( tool ), m_GerbMetric );
  253. int gap = scaletoIU( params[4].GetValue( tool ), m_GerbMetric );
  254. int numCircles = KiROUND( params[5].GetValue( tool ) );
  255. // Draw circles:
  256. wxPoint center = aParent->GetABPosition( curPos );
  257. // adjust outerDiam by this on each nested circle
  258. int diamAdjust = (gap + penThickness) * 2;
  259. for( int i = 0; i < numCircles; ++i, outerDiam -= diamAdjust )
  260. {
  261. if( outerDiam <= 0 )
  262. break;
  263. // Note: outerDiam is the outer diameter of the ring.
  264. // the ring graphic diameter is (outerDiam - penThickness)
  265. if( outerDiam <= penThickness )
  266. { // No room to draw a ring (no room for the hole):
  267. // draw a circle instead (with no hole), with the right diameter
  268. TransformCircleToPolygon( aShapeBuffer, center, outerDiam / 2, ARC_HIGH_DEF,
  269. ERROR_INSIDE );
  270. }
  271. else
  272. {
  273. TransformRingToPolygon( aShapeBuffer, center, ( outerDiam - penThickness ) / 2,
  274. penThickness, ARC_HIGH_DEF, ERROR_INSIDE );
  275. }
  276. }
  277. // Draw the cross:
  278. ConvertShapeToPolygon( aParent, polybuffer );
  279. rotation = params[8].GetValue( tool ) * 10.0;
  280. for( unsigned ii = 0; ii < polybuffer.size(); ii++ )
  281. {
  282. // shape rotation:
  283. RotatePoint( &polybuffer[ii], -rotation );
  284. // Move to current position:
  285. polybuffer[ii] += curPos;
  286. polybuffer[ii] = aParent->GetABPosition( polybuffer[ii] );
  287. }
  288. TO_POLY_SHAPE;
  289. }
  290. break;
  291. case AMP_OUTLINE:
  292. {
  293. /* Outline, Primitive Code 4
  294. * An outline primitive is an area enclosed by an n-point polygon defined by its start point and n
  295. * subsequent points. The outline must be closed, i.e. the last point must be equal to the start
  296. * point. There must be at least one subsequent point (to close the outline).
  297. * The outline of the primitive is actually the contour (see 2.6) that consists of linear segments
  298. * only, so it must conform to all the requirements described for contours.
  299. * Warning: Make no mistake: n is the number of subsequent points, being the number of
  300. * vertices of the outline or one less than the number of coordinate pairs.
  301. */
  302. /* Generated by an aperture macro declaration like:
  303. * "4,1,3,0.0,0.0,0.0,0.5,0.5,0.5,0.5,0.0,-25"
  304. * type(4), exposure, corners count, corner1.x, corner.1y, ..., corner1.x, corner.1y, rotation
  305. * type is not stored in parameters list, so the first parameter is exposure
  306. */
  307. // params[0] is the exposure and params[1] is the corners count after the first corner
  308. int numCorners = (int) params[1].GetValue( tool );
  309. // the shape rotation is the last param of list, after corners
  310. int last_prm = params.size() - 1;
  311. rotation = params[last_prm].GetValue( tool ) * 10.0;
  312. wxPoint pos;
  313. // Read points.
  314. // Note: numCorners is the polygon corner count, following the first corner
  315. // * the polygon is always closed,
  316. // * therefore the last XY coordinate is the same as the first
  317. int prm_idx = 2; // params[2] is the first X coordinate
  318. for( int i = 0; i <= numCorners; ++i )
  319. {
  320. pos.x = scaletoIU( params[prm_idx].GetValue( tool ), m_GerbMetric );
  321. prm_idx++;
  322. pos.y = scaletoIU( params[prm_idx].GetValue( tool ), m_GerbMetric );
  323. prm_idx++;
  324. polybuffer.push_back(pos);
  325. // Guard: ensure prm_idx < last_prm
  326. // I saw malformed gerber files with numCorners = number
  327. // of coordinates instead of number of coordinates following the first point
  328. if( prm_idx >= last_prm )
  329. break;
  330. }
  331. // rotate polygon and move it to the actual position
  332. // shape rotation:
  333. for( unsigned ii = 0; ii < polybuffer.size(); ii++ )
  334. {
  335. RotatePoint( &polybuffer[ii], -rotation );
  336. }
  337. // Move to current position:
  338. for( unsigned ii = 0; ii < polybuffer.size(); ii++ )
  339. {
  340. polybuffer[ii] += curPos;
  341. polybuffer[ii] = aParent->GetABPosition( polybuffer[ii] );
  342. }
  343. TO_POLY_SHAPE;
  344. }
  345. break;
  346. case AMP_POLYGON:
  347. /* Polygon, Primitive Code 5
  348. * A polygon primitive is a regular polygon defined by the number of vertices n, the center point
  349. * and the diameter of the circumscribed circle
  350. */
  351. /* Generated by an aperture macro declaration like:
  352. * "5,1,0.6,0,0,0.5,25"
  353. * type(5), exposure, vertices count, pox.x, pos.y, diameter, rotation
  354. * type is not stored in parameters list, so the first parameter is exposure
  355. */
  356. curPos += mapPt( params[2].GetValue( tool ), params[3].GetValue( tool ), m_GerbMetric );
  357. // Creates the shape:
  358. ConvertShapeToPolygon( aParent, polybuffer );
  359. // rotate polygon and move it to the actual position
  360. rotation = params[5].GetValue( tool ) * 10.0;
  361. for( unsigned ii = 0; ii < polybuffer.size(); ii++ )
  362. {
  363. RotatePoint( &polybuffer[ii], -rotation );
  364. polybuffer[ii] += curPos;
  365. polybuffer[ii] = aParent->GetABPosition( polybuffer[ii] );
  366. }
  367. TO_POLY_SHAPE;
  368. break;
  369. case AMP_EOF:
  370. // not yet supported, waiting for you.
  371. break;
  372. case AMP_UNKNOWN:
  373. default:
  374. break;
  375. }
  376. }
  377. /**
  378. * Function ConvertShapeToPolygon (virtual)
  379. * convert a shape to an equivalent polygon.
  380. * Arcs and circles are approximated by segments
  381. * Useful when a shape is not a graphic primitive (shape with hole,
  382. * rotated shape ... ) and cannot be easily drawn.
  383. * note for some schapes conbining circles and solid lines (rectangles), only rectangles are converted
  384. * because circles are very easy to draw (no rotation problem) so convert them in polygons,
  385. * and draw them as polygons is not a good idea.
  386. */
  387. void AM_PRIMITIVE::ConvertShapeToPolygon( const GERBER_DRAW_ITEM* aParent,
  388. std::vector<wxPoint>& aBuffer )
  389. {
  390. D_CODE* tool = aParent->GetDcodeDescr();
  391. switch( primitive_id )
  392. {
  393. case AMP_CIRCLE:
  394. {
  395. /* Generated by an aperture macro declaration like:
  396. * "1,1,0.3,0.5, 1.0*"
  397. * type (1), exposure, diameter, pos.x, pos.y, <rotation>
  398. * <rotation> is a optional parameter: rotation from origin.
  399. * type is not stored in parameters list, so the first parameter is exposure
  400. */
  401. wxPoint center = mapPt( params[2].GetValue( tool ), params[3].GetValue( tool ), m_GerbMetric );
  402. int radius = scaletoIU( params[1].GetValue( tool ), m_GerbMetric ) / 2;
  403. wxPoint corner;
  404. const int delta = 3600 / seg_per_circle; // rot angle in 0.1 degree
  405. for( int angle = 0; angle < 3600; angle += delta )
  406. {
  407. corner.x = radius;
  408. corner.y = 0;
  409. RotatePoint( &corner, angle );
  410. corner += center;
  411. aBuffer.push_back( corner );
  412. }
  413. }
  414. break;
  415. case AMP_LINE2:
  416. case AMP_LINE20: // Line with rectangle ends. (Width, start and end pos + rotation)
  417. {
  418. int width = scaletoIU( params[1].GetValue( tool ), m_GerbMetric );
  419. wxPoint start = mapPt( params[2].GetValue( tool ),
  420. params[3].GetValue( tool ), m_GerbMetric );
  421. wxPoint end = mapPt( params[4].GetValue( tool ),
  422. params[5].GetValue( tool ), m_GerbMetric );
  423. wxPoint delta = end - start;
  424. int len = KiROUND( EuclideanNorm( delta ) );
  425. // To build the polygon, we must create a horizontal polygon starting to "start"
  426. // and rotate it to have the end point to "end"
  427. wxPoint currpt;
  428. currpt.y += width / 2; // Upper left
  429. aBuffer.push_back( currpt );
  430. currpt.x = len; // Upper right
  431. aBuffer.push_back( currpt );
  432. currpt.y -= width; // lower right
  433. aBuffer.push_back( currpt );
  434. currpt.x = 0; // lower left
  435. aBuffer.push_back( currpt );
  436. // Rotate rectangle and move it to the actual start point
  437. double angle = ArcTangente( delta.y, delta.x );
  438. for( unsigned ii = 0; ii < 4; ii++ )
  439. {
  440. RotatePoint( &aBuffer[ii], -angle );
  441. aBuffer[ii] += start;
  442. }
  443. }
  444. break;
  445. case AMP_LINE_CENTER:
  446. {
  447. wxPoint size = mapPt( params[1].GetValue( tool ), params[2].GetValue( tool ), m_GerbMetric );
  448. wxPoint pos = mapPt( params[3].GetValue( tool ), params[4].GetValue( tool ), m_GerbMetric );
  449. // Build poly:
  450. pos.x -= size.x / 2;
  451. pos.y -= size.y / 2; // Lower left
  452. aBuffer.push_back( pos );
  453. pos.y += size.y; // Upper left
  454. aBuffer.push_back( pos );
  455. pos.x += size.x; // Upper right
  456. aBuffer.push_back( pos );
  457. pos.y -= size.y; // lower right
  458. aBuffer.push_back( pos );
  459. }
  460. break;
  461. case AMP_LINE_LOWER_LEFT:
  462. {
  463. wxPoint size = mapPt( params[1].GetValue( tool ), params[2].GetValue( tool ), m_GerbMetric );
  464. wxPoint lowerLeft = mapPt( params[3].GetValue( tool ), params[4].GetValue(
  465. tool ), m_GerbMetric );
  466. // Build poly:
  467. aBuffer.push_back( lowerLeft );
  468. lowerLeft.y += size.y; // Upper left
  469. aBuffer.push_back( lowerLeft );
  470. lowerLeft.x += size.x; // Upper right
  471. aBuffer.push_back( lowerLeft );
  472. lowerLeft.y -= size.y; // lower right
  473. aBuffer.push_back( lowerLeft );
  474. }
  475. break;
  476. case AMP_THERMAL:
  477. {
  478. // Only 1/4 of the full shape is built, because the other 3 shapes will be draw from this first
  479. // rotated by 90, 180 and 270 deg.
  480. // params = center.x (unused here), center.y (unused here), outside diam, inside diam, crosshair thickness
  481. int outerRadius = scaletoIU( params[2].GetValue( tool ), m_GerbMetric ) / 2;
  482. int innerRadius = scaletoIU( params[3].GetValue( tool ), m_GerbMetric ) / 2;
  483. // Safety checks to guarantee no divide-by-zero
  484. outerRadius = std::max( 1, outerRadius );
  485. innerRadius = std::max( 1, innerRadius );
  486. int halfthickness = scaletoIU( params[4].GetValue( tool ), m_GerbMetric ) / 2;
  487. double angle_start = RAD2DECIDEG( asin( (double) halfthickness / innerRadius ) );
  488. // Draw shape in the first cadrant (X and Y > 0)
  489. wxPoint pos, startpos;
  490. // Inner arc
  491. startpos.x = innerRadius;
  492. double angle_end = 900 - angle_start;
  493. for( double angle = angle_start; angle < angle_end; angle += 100 )
  494. {
  495. pos = startpos;
  496. RotatePoint( &pos, angle );
  497. aBuffer.push_back( pos );
  498. }
  499. // Last point
  500. pos = startpos;
  501. RotatePoint( &pos, angle_end );
  502. aBuffer.push_back( pos );
  503. // outer arc
  504. startpos.x = outerRadius;
  505. startpos.y = 0;
  506. angle_start = RAD2DECIDEG( asin( (double) halfthickness / outerRadius ) );
  507. angle_end = 900 - angle_start;
  508. // First point, near Y axis, outer arc
  509. for( double angle = angle_end; angle > angle_start; angle -= 100 )
  510. {
  511. pos = startpos;
  512. RotatePoint( &pos, angle );
  513. aBuffer.push_back( pos );
  514. }
  515. // last point
  516. pos = startpos;
  517. RotatePoint( &pos, angle_start );
  518. aBuffer.push_back( pos );
  519. aBuffer.push_back( aBuffer[0] ); // Close poly
  520. }
  521. break;
  522. case AMP_MOIRE: // A cross hair with n concentric circles. Only the cros is build as polygon
  523. // because circles can be drawn easily
  524. {
  525. int crossHairThickness = scaletoIU( params[6].GetValue( tool ), m_GerbMetric );
  526. int crossHairLength = scaletoIU( params[7].GetValue( tool ), m_GerbMetric );
  527. // Create cross. First create 1/4 of the shape.
  528. // Others point are the same, totated by 90, 180 and 270 deg
  529. wxPoint pos( crossHairThickness / 2, crossHairLength / 2 );
  530. aBuffer.push_back( pos );
  531. pos.y = crossHairThickness / 2;
  532. aBuffer.push_back( pos );
  533. pos.x = -crossHairLength / 2;
  534. aBuffer.push_back( pos );
  535. pos.y = -crossHairThickness / 2;
  536. aBuffer.push_back( pos );
  537. // Copy the 4 shape, rotated by 90, 180 and 270 deg
  538. for( int jj = 1; jj <= 3; jj ++ )
  539. {
  540. for( int ii = 0; ii < 4; ii++ )
  541. {
  542. pos = aBuffer[ii];
  543. RotatePoint( &pos, jj*900 );
  544. aBuffer.push_back( pos );
  545. }
  546. }
  547. }
  548. break;
  549. case AMP_OUTLINE:
  550. // already is a polygon. Do nothing
  551. break;
  552. case AMP_POLYGON: // Creates a regular polygon
  553. {
  554. int vertexcount = KiROUND( params[1].GetValue( tool ) );
  555. int radius = scaletoIU( params[4].GetValue( tool ), m_GerbMetric ) / 2;
  556. // rs274x said: vertex count = 3 ... 10, and the first corner is on the X axis
  557. if( vertexcount < 3 )
  558. vertexcount = 3;
  559. if( vertexcount > 10 )
  560. vertexcount = 10;
  561. for( int ii = 0; ii <= vertexcount; ii++ )
  562. {
  563. wxPoint pos( radius, 0);
  564. RotatePoint( &pos, ii * 3600 / vertexcount );
  565. aBuffer.push_back( pos );
  566. }
  567. }
  568. break;
  569. case AMP_COMMENT:
  570. case AMP_UNKNOWN:
  571. case AMP_EOF:
  572. break;
  573. }
  574. }
  575. /** GetShapeDim
  576. * Calculate a value that can be used to evaluate the size of text
  577. * when displaying the D-Code of an item
  578. * due to the complexity of the shape of some primitives
  579. * one cannot calculate the "size" of a shape (only abounding box)
  580. * but here, the "dimension" of the shape is the diameter of the primitive
  581. * or for lines the width of the line
  582. * @param aParent = the parent GERBER_DRAW_ITEM which is actually drawn
  583. * @return a dimension, or -1 if no dim to calculate
  584. */
  585. int AM_PRIMITIVE::GetShapeDim( const GERBER_DRAW_ITEM* aParent )
  586. {
  587. int dim = -1;
  588. D_CODE* tool = aParent->GetDcodeDescr();
  589. switch( primitive_id )
  590. {
  591. case AMP_CIRCLE:
  592. // params = exposure, diameter, pos.x, pos.y
  593. dim = scaletoIU( params[1].GetValue( tool ), m_GerbMetric ); // Diameter
  594. break;
  595. case AMP_LINE2:
  596. case AMP_LINE20: // Line with rectangle ends. (Width, start and end pos + rotation)
  597. dim = scaletoIU( params[1].GetValue( tool ), m_GerbMetric ); // linne width
  598. break;
  599. case AMP_LINE_CENTER:
  600. {
  601. wxPoint size = mapPt( params[1].GetValue( tool ), params[2].GetValue( tool ), m_GerbMetric );
  602. dim = std::min(size.x, size.y);
  603. }
  604. break;
  605. case AMP_LINE_LOWER_LEFT:
  606. {
  607. wxPoint size = mapPt( params[1].GetValue( tool ), params[2].GetValue( tool ), m_GerbMetric );
  608. dim = std::min(size.x, size.y);
  609. }
  610. break;
  611. case AMP_THERMAL:
  612. {
  613. // Only 1/4 of the full shape is built, because the other 3 shapes will be draw from this first
  614. // rotated by 90, 180 and 270 deg.
  615. // params = center.x (unused here), center.y (unused here), outside diam, inside diam, crosshair thickness
  616. dim = scaletoIU( params[2].GetValue( tool ), m_GerbMetric ) / 2; // Outer diam
  617. }
  618. break;
  619. case AMP_MOIRE: // A cross hair with n concentric circles.
  620. dim = scaletoIU( params[7].GetValue( tool ), m_GerbMetric ); // = cross hair len
  621. break;
  622. case AMP_OUTLINE: // a free polygon :
  623. // dim = min side of the bounding box (this is a poor criteria, but what is a good criteria b?)
  624. {
  625. // exposure, corners count, corner1.x, corner.1y, ..., rotation
  626. // note: corners count is the count of corners following corner1
  627. int numPoints = (int) params[1].GetValue( tool );
  628. // Read points. numPoints does not include the starting point, so add 1.
  629. // and calculate the bounding box;
  630. wxSize pos_min, pos_max, pos;
  631. int prm_idx = 2; // params[2] is the first X coordinate
  632. int last_prm = params.size() - 1;
  633. for( int i = 0; i<= numPoints; ++i )
  634. {
  635. pos.x = scaletoIU( params[prm_idx].GetValue( tool ), m_GerbMetric );
  636. prm_idx++;
  637. pos.y = scaletoIU( params[prm_idx].GetValue( tool ), m_GerbMetric );
  638. prm_idx++;
  639. if( i == 0 )
  640. pos_min = pos_max = pos;
  641. else
  642. {
  643. // upper right corner:
  644. if( pos_min.x > pos.x )
  645. pos_min.x = pos.x;
  646. if( pos_min.y > pos.y )
  647. pos_min.y = pos.y;
  648. // lower left corner:
  649. if( pos_max.x < pos.x )
  650. pos_max.x = pos.x;
  651. if( pos_max.y < pos.y )
  652. pos_max.y = pos.y;
  653. }
  654. // Guard: ensure prm_idx < last_prm (last prm is orientation)
  655. // I saw malformed gerber files with numCorners = number
  656. // of coordinates instead of number of coordinates following the first point
  657. if( prm_idx >= last_prm )
  658. break;
  659. }
  660. // calculate dim
  661. wxSize size;
  662. size.x = pos_max.x - pos_min.x;
  663. size.y = pos_max.y - pos_min.y;
  664. dim = std::min( size.x, size.y );
  665. }
  666. break;
  667. case AMP_POLYGON: // Regular polygon
  668. dim = scaletoIU( params[4].GetValue( tool ), m_GerbMetric ) / 2; // Radius
  669. break;
  670. case AMP_COMMENT:
  671. case AMP_UNKNOWN:
  672. case AMP_EOF:
  673. break;
  674. }
  675. return dim;
  676. }
  677. SHAPE_POLY_SET* APERTURE_MACRO::GetApertureMacroShape( const GERBER_DRAW_ITEM* aParent,
  678. wxPoint aShapePos )
  679. {
  680. SHAPE_POLY_SET holeBuffer;
  681. bool hasHole = false;
  682. m_shape.RemoveAllContours();
  683. for( AM_PRIMITIVES::iterator prim_macro = primitives.begin();
  684. prim_macro != primitives.end(); ++prim_macro )
  685. {
  686. if( prim_macro->primitive_id == AMP_COMMENT )
  687. continue;
  688. if( prim_macro->IsAMPrimitiveExposureOn( aParent ) )
  689. prim_macro->DrawBasicShape( aParent, m_shape, aShapePos );
  690. else
  691. {
  692. prim_macro->DrawBasicShape( aParent, holeBuffer, aShapePos );
  693. if( holeBuffer.OutlineCount() ) // we have a new hole in shape: remove the hole
  694. {
  695. m_shape.BooleanSubtract( holeBuffer, SHAPE_POLY_SET::PM_FAST );
  696. holeBuffer.RemoveAllContours();
  697. hasHole = true;
  698. }
  699. }
  700. }
  701. // If a hole is defined inside a polygon, we must fracture the polygon
  702. // to be able to drawn it (i.e link holes by overlapping edges)
  703. if( hasHole )
  704. m_shape.Fracture( SHAPE_POLY_SET::PM_FAST );
  705. m_boundingBox = EDA_RECT( wxPoint( 0, 0 ), wxSize( 1, 1 ) );
  706. auto bb = m_shape.BBox();
  707. wxPoint center( bb.Centre().x, bb.Centre().y );
  708. m_boundingBox.Move( aParent->GetABPosition( center ) );
  709. m_boundingBox.Inflate( bb.GetWidth() / 2, bb.GetHeight() / 2 );
  710. return &m_shape;
  711. }
  712. /*
  713. * Function DrawApertureMacroShape
  714. * Draw the primitive shape for flashed items.
  715. * When an item is flashed, this is the shape of the item
  716. */
  717. void APERTURE_MACRO::DrawApertureMacroShape( GERBER_DRAW_ITEM* aParent,
  718. EDA_RECT* aClipBox, wxDC* aDC,
  719. COLOR4D aColor,
  720. wxPoint aShapePos, bool aFilledShape )
  721. {
  722. SHAPE_POLY_SET* shapeBuffer = GetApertureMacroShape( aParent, aShapePos );
  723. if( shapeBuffer->OutlineCount() == 0 )
  724. return;
  725. for( int ii = 0; ii < shapeBuffer->OutlineCount(); ii++ )
  726. {
  727. SHAPE_LINE_CHAIN& poly = shapeBuffer->Outline( ii );
  728. GRClosedPoly( aClipBox, aDC, poly.PointCount(), (wxPoint*) &poly.CPoint( 0 ), aFilledShape,
  729. aColor, aColor );
  730. }
  731. }
  732. /** GetShapeDim
  733. * Calculate a value that can be used to evaluate the size of text
  734. * when displaying the D-Code of an item
  735. * due to the complexity of a shape using many primitives
  736. * one cannot calculate the "size" of a shape (only abounding box)
  737. * but most of aperture macro are using one or few primitives
  738. * and the "dimension" of the shape is the diameter of the primitive
  739. * (or the max diameter of primitives)
  740. * @return a dimension, or -1 if no dim to calculate
  741. */
  742. int APERTURE_MACRO::GetShapeDim( GERBER_DRAW_ITEM* aParent )
  743. {
  744. int dim = -1;
  745. for( AM_PRIMITIVES::iterator prim_macro = primitives.begin();
  746. prim_macro != primitives.end(); ++prim_macro )
  747. {
  748. int pdim = prim_macro->GetShapeDim( aParent );
  749. if( dim < pdim )
  750. dim = pdim;
  751. }
  752. return dim;
  753. }
  754. /**
  755. * function GetLocalParam
  756. * Usually, parameters are defined inside the aperture primitive
  757. * using immediate mode or defered mode.
  758. * in defered mode the value is defined in a DCODE that want to use the aperture macro.
  759. * But some parameters are defined outside the aperture primitive
  760. * and are local to the aperture macro
  761. * @return the value of a defered parameter defined inside the aperture macro
  762. * @param aParamId = the param id (defined by $3 or $5 ..) to evaluate
  763. */
  764. double APERTURE_MACRO::GetLocalParam( const D_CODE* aDcode, unsigned aParamId ) const
  765. {
  766. // find parameter descr.
  767. const AM_PARAM * param = NULL;
  768. for( unsigned ii = 0; ii < m_localparamStack.size(); ii ++ )
  769. {
  770. if( m_localparamStack[ii].GetIndex() == aParamId )
  771. {
  772. param = &m_localparamStack[ii];
  773. break;
  774. }
  775. }
  776. if ( param == NULL ) // not found
  777. return 0.0;
  778. // Evaluate parameter
  779. double value = param->GetValue( aDcode );
  780. return value;
  781. }