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.

462 lines
13 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
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2016 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 2011 Wayne Stambaugh <stambaughw@verizon.net>
  6. * Copyright (C) 1992-2016 KiCad Developers, see AUTHORS.txt for contributors.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, you may find one here:
  20. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  21. * or you may search the http://www.gnu.org website for the version 2 license,
  22. * or you may write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  24. */
  25. /**
  26. * @file dcode.cpp
  27. * @brief D_CODE class implementation
  28. */
  29. #include <fctsys.h>
  30. #include <common.h>
  31. #include <class_drawpanel.h>
  32. #include <trigo.h>
  33. #include <gerbview_frame.h>
  34. #include <class_gerber_file_image.h>
  35. #include <convert_to_biu.h>
  36. #include <convert_basic_shapes_to_polygon.h>
  37. #define DCODE_DEFAULT_SIZE Millimeter2iu( 0.1 )
  38. /* Format Gerber: NOTES:
  39. * Tools and D_CODES
  40. * tool number (identification of shapes)
  41. * 1 to 999
  42. *
  43. * D_CODES:
  44. * D01 ... D9 = command codes:
  45. * D01 = activating light (pen down) while moving
  46. * D02 = light extinction (pen up) while moving
  47. * D03 = Flash
  48. * D04 to D09 = non used
  49. * D10 ... D999 = Identification Tool (Shape id)
  50. *
  51. * For tools defining a shape):
  52. * DCode min = D10
  53. * DCode max = 999
  54. */
  55. /***************/
  56. /* Class DCODE */
  57. /***************/
  58. D_CODE::D_CODE( int num_dcode )
  59. {
  60. m_Num_Dcode = num_dcode;
  61. Clear_D_CODE_Data();
  62. }
  63. D_CODE::~D_CODE()
  64. {
  65. }
  66. void D_CODE::Clear_D_CODE_Data()
  67. {
  68. m_Size.x = DCODE_DEFAULT_SIZE;
  69. m_Size.y = DCODE_DEFAULT_SIZE;
  70. m_Shape = APT_CIRCLE;
  71. m_Drill.x = m_Drill.y = 0;
  72. m_DrillShape = APT_DEF_NO_HOLE;
  73. m_InUse = false;
  74. m_Defined = false;
  75. m_Macro = NULL;
  76. m_Rotation = 0.0;
  77. m_EdgesCount = 0;
  78. m_Polygon.RemoveAllContours();
  79. }
  80. const wxChar* D_CODE::ShowApertureType( APERTURE_T aType )
  81. {
  82. const wxChar* ret;
  83. switch( aType )
  84. {
  85. case APT_CIRCLE:
  86. ret = wxT( "Round" ); break;
  87. case APT_RECT:
  88. ret = wxT( "Rect" ); break;
  89. case APT_OVAL:
  90. ret = wxT( "Oval" ); break;
  91. case APT_POLYGON:
  92. ret = wxT( "Poly" ); break;
  93. case APT_MACRO:
  94. ret = wxT( "Macro" ); break;
  95. default:
  96. ret = wxT( "???" ); break;
  97. }
  98. return ret;
  99. }
  100. int D_CODE::GetShapeDim( GERBER_DRAW_ITEM* aParent )
  101. {
  102. int dim = -1;
  103. switch( m_Shape )
  104. {
  105. case APT_CIRCLE:
  106. dim = m_Size.x;
  107. break;
  108. case APT_RECT:
  109. case APT_OVAL:
  110. dim = std::min( m_Size.x, m_Size.y );
  111. break;
  112. case APT_POLYGON:
  113. dim = std::min( m_Size.x, m_Size.y );
  114. break;
  115. case APT_MACRO:
  116. if( m_Macro )
  117. dim = m_Macro->GetShapeDim( aParent );
  118. break;
  119. default:
  120. break;
  121. }
  122. return dim;
  123. }
  124. void D_CODE::DrawFlashedShape( GERBER_DRAW_ITEM* aParent,
  125. EDA_RECT* aClipBox, wxDC* aDC, COLOR4D aColor,
  126. wxPoint aShapePos, bool aFilledShape )
  127. {
  128. int radius;
  129. switch( m_Shape )
  130. {
  131. case APT_MACRO:
  132. GetMacro()->DrawApertureMacroShape( aParent, aClipBox, aDC, aColor,
  133. aShapePos, aFilledShape);
  134. break;
  135. case APT_CIRCLE:
  136. radius = m_Size.x >> 1;
  137. if( !aFilledShape )
  138. GRCircle( aClipBox, aDC, aParent->GetABPosition(aShapePos), radius, 0, aColor );
  139. else
  140. if( m_DrillShape == APT_DEF_NO_HOLE )
  141. {
  142. GRFilledCircle( aClipBox, aDC, aParent->GetABPosition(aShapePos),
  143. radius, aColor );
  144. }
  145. else if( m_DrillShape == APT_DEF_ROUND_HOLE ) // round hole in shape
  146. {
  147. int width = (m_Size.x - m_Drill.x ) / 2;
  148. GRCircle( aClipBox, aDC, aParent->GetABPosition(aShapePos),
  149. radius - (width / 2), width, aColor );
  150. }
  151. else // rectangular hole
  152. {
  153. if( m_Polygon.OutlineCount() == 0 )
  154. ConvertShapeToPolygon();
  155. DrawFlashedPolygon( aParent, aClipBox, aDC, aColor, aFilledShape, aShapePos );
  156. }
  157. break;
  158. case APT_RECT:
  159. {
  160. wxPoint start;
  161. start.x = aShapePos.x - m_Size.x / 2;
  162. start.y = aShapePos.y - m_Size.y / 2;
  163. wxPoint end = start + m_Size;
  164. start = aParent->GetABPosition( start );
  165. end = aParent->GetABPosition( end );
  166. if( !aFilledShape )
  167. {
  168. GRRect( aClipBox, aDC, start.x, start.y, end.x, end.y, 0, aColor );
  169. }
  170. else if( m_DrillShape == APT_DEF_NO_HOLE )
  171. {
  172. GRFilledRect( aClipBox, aDC, start.x, start.y, end.x, end.y, 0, aColor, aColor );
  173. }
  174. else
  175. {
  176. if( m_Polygon.OutlineCount() == 0 )
  177. ConvertShapeToPolygon();
  178. DrawFlashedPolygon( aParent, aClipBox, aDC, aColor, aFilledShape, aShapePos );
  179. }
  180. }
  181. break;
  182. case APT_OVAL:
  183. {
  184. wxPoint start = aShapePos;
  185. wxPoint end = aShapePos;
  186. if( m_Size.x > m_Size.y ) // horizontal oval
  187. {
  188. int delta = (m_Size.x - m_Size.y) / 2;
  189. start.x -= delta;
  190. end.x += delta;
  191. radius = m_Size.y;
  192. }
  193. else // horizontal oval
  194. {
  195. int delta = (m_Size.y - m_Size.x) / 2;
  196. start.y -= delta;
  197. end.y += delta;
  198. radius = m_Size.x;
  199. }
  200. start = aParent->GetABPosition( start );
  201. end = aParent->GetABPosition( end );
  202. if( !aFilledShape )
  203. {
  204. GRCSegm( aClipBox, aDC, start.x, start.y, end.x, end.y, radius, aColor );
  205. }
  206. else if( m_DrillShape == APT_DEF_NO_HOLE )
  207. {
  208. GRFillCSegm( aClipBox, aDC, start.x, start.y, end.x, end.y, radius, aColor );
  209. }
  210. else
  211. {
  212. if( m_Polygon.OutlineCount() == 0 )
  213. ConvertShapeToPolygon();
  214. DrawFlashedPolygon( aParent, aClipBox, aDC, aColor, aFilledShape, aShapePos );
  215. }
  216. }
  217. break;
  218. case APT_POLYGON:
  219. if( m_Polygon.OutlineCount() == 0 )
  220. ConvertShapeToPolygon();
  221. DrawFlashedPolygon( aParent, aClipBox, aDC, aColor, aFilledShape, aShapePos );
  222. break;
  223. }
  224. }
  225. void D_CODE::DrawFlashedPolygon( GERBER_DRAW_ITEM* aParent,
  226. EDA_RECT* aClipBox, wxDC* aDC,
  227. COLOR4D aColor, bool aFilled,
  228. const wxPoint& aPosition )
  229. {
  230. if( m_Polygon.OutlineCount() == 0 )
  231. return;
  232. int pointCount = m_Polygon.VertexCount();
  233. std::vector<wxPoint> points;
  234. points.reserve( pointCount );
  235. for( int ii = 0; ii < pointCount; ii++ )
  236. {
  237. wxPoint p( m_Polygon.Vertex( ii ).x, m_Polygon.Vertex( ii ).y );
  238. points[ii] = p + aPosition;
  239. points[ii] = aParent->GetABPosition( points[ii] );
  240. }
  241. GRClosedPoly( aClipBox, aDC, pointCount, &points[0], aFilled, aColor, aColor );
  242. }
  243. #define SEGS_CNT 64 // number of segments to approximate a circle
  244. // A helper function for D_CODE::ConvertShapeToPolygon(). Add a hole to a polygon
  245. static void addHoleToPolygon( SHAPE_POLY_SET* aPolygon,
  246. APERTURE_DEF_HOLETYPE aHoleShape,
  247. wxSize aSize,
  248. wxPoint aAnchorPos );
  249. void D_CODE::ConvertShapeToPolygon()
  250. {
  251. wxPoint initialpos;
  252. wxPoint currpos;
  253. m_Polygon.RemoveAllContours();
  254. switch( m_Shape )
  255. {
  256. case APT_CIRCLE: // creates only a circle with rectangular hole
  257. TransformCircleToPolygon( m_Polygon, initialpos, m_Size.x >> 1, SEGS_CNT );
  258. addHoleToPolygon( &m_Polygon, m_DrillShape, m_Drill, initialpos );
  259. break;
  260. case APT_RECT:
  261. m_Polygon.NewOutline();
  262. currpos.x = m_Size.x / 2;
  263. currpos.y = m_Size.y / 2;
  264. initialpos = currpos;
  265. m_Polygon.Append( VECTOR2I( currpos ) );
  266. currpos.x -= m_Size.x;
  267. m_Polygon.Append( VECTOR2I( currpos ) );
  268. currpos.y -= m_Size.y;
  269. m_Polygon.Append( VECTOR2I( currpos ) );
  270. currpos.x += m_Size.x;
  271. m_Polygon.Append( VECTOR2I( currpos ) );
  272. currpos.y += m_Size.y;
  273. m_Polygon.Append( VECTOR2I( currpos ) ); // close polygon
  274. m_Polygon.Append( VECTOR2I( initialpos ) );
  275. addHoleToPolygon( &m_Polygon, m_DrillShape, m_Drill, initialpos );
  276. break;
  277. case APT_OVAL:
  278. {
  279. m_Polygon.NewOutline();
  280. int delta, radius;
  281. // we create an horizontal oval shape. then rotate if needed
  282. if( m_Size.x > m_Size.y ) // horizontal oval
  283. {
  284. delta = (m_Size.x - m_Size.y) / 2;
  285. radius = m_Size.y / 2;
  286. }
  287. else // vertical oval
  288. {
  289. delta = (m_Size.y - m_Size.x) / 2;
  290. radius = m_Size.x / 2;
  291. }
  292. currpos.y = radius;
  293. initialpos = currpos;
  294. m_Polygon.Append( VECTOR2I( currpos ) );
  295. // build the right arc of the shape
  296. unsigned ii = 0;
  297. for( ; ii <= SEGS_CNT / 2; ii++ )
  298. {
  299. currpos = initialpos;
  300. RotatePoint( &currpos, ii * 3600.0 / SEGS_CNT );
  301. currpos.x += delta;
  302. m_Polygon.Append( VECTOR2I( currpos ) );
  303. }
  304. // build the left arc of the shape
  305. for( ii = SEGS_CNT / 2; ii <= SEGS_CNT; ii++ )
  306. {
  307. currpos = initialpos;
  308. RotatePoint( &currpos, ii * 3600.0 / SEGS_CNT );
  309. currpos.x -= delta;
  310. m_Polygon.Append( VECTOR2I( currpos ) );
  311. }
  312. m_Polygon.Append( VECTOR2I( initialpos ) ); // close outline
  313. if( m_Size.y > m_Size.x ) // vertical oval, rotate polygon.
  314. {
  315. for( auto it = m_Polygon.Iterate( 0 ); it; ++it )
  316. it->Rotate( -M_PI / 2 );
  317. }
  318. addHoleToPolygon( &m_Polygon, m_DrillShape, m_Drill, initialpos );
  319. }
  320. break;
  321. case APT_POLYGON:
  322. m_Polygon.NewOutline();
  323. currpos.x = m_Size.x >> 1; // first point is on X axis
  324. initialpos = currpos;
  325. // rs274x said: m_EdgesCount = 3 ... 12
  326. if( m_EdgesCount < 3 )
  327. m_EdgesCount = 3;
  328. if( m_EdgesCount > 12 )
  329. m_EdgesCount = 12;
  330. for( int ii = 0; ii < m_EdgesCount; ii++ )
  331. {
  332. currpos = initialpos;
  333. RotatePoint( &currpos, ii * 3600.0 / m_EdgesCount );
  334. m_Polygon.Append( VECTOR2I( currpos ) );
  335. }
  336. addHoleToPolygon( &m_Polygon, m_DrillShape, m_Drill, initialpos );
  337. if( m_Rotation ) // vertical oval, rotate polygon.
  338. {
  339. int angle = KiROUND( m_Rotation * 10 );
  340. for( auto it = m_Polygon.Iterate( 0 ); it; ++it )
  341. it->Rotate( -angle );
  342. }
  343. break;
  344. case APT_MACRO:
  345. // TODO
  346. break;
  347. }
  348. }
  349. // The helper function for D_CODE::ConvertShapeToPolygon().
  350. // Add a hole to a polygon
  351. static void addHoleToPolygon( SHAPE_POLY_SET* aPolygon,
  352. APERTURE_DEF_HOLETYPE aHoleShape,
  353. wxSize aSize,
  354. wxPoint aAnchorPos )
  355. {
  356. wxPoint currpos;
  357. SHAPE_POLY_SET holeBuffer;
  358. if( aHoleShape == APT_DEF_ROUND_HOLE )
  359. {
  360. TransformCircleToPolygon( holeBuffer, wxPoint( 0, 0 ), aSize.x / 2, SEGS_CNT );
  361. }
  362. else if( aHoleShape == APT_DEF_RECT_HOLE )
  363. {
  364. holeBuffer.NewOutline();
  365. currpos.x = aSize.x / 2;
  366. currpos.y = aSize.y / 2;
  367. holeBuffer.Append( VECTOR2I( currpos ) ); // link to hole and begin hole
  368. currpos.x -= aSize.x;
  369. holeBuffer.Append( VECTOR2I( currpos ) );
  370. currpos.y -= aSize.y;
  371. holeBuffer.Append( VECTOR2I( currpos ) );
  372. currpos.x += aSize.x;
  373. holeBuffer.Append( VECTOR2I( currpos ) );
  374. currpos.y += aSize.y;
  375. holeBuffer.Append( VECTOR2I( currpos ) ); // close hole
  376. }
  377. aPolygon->BooleanSubtract( holeBuffer, SHAPE_POLY_SET::PM_FAST );
  378. // Needed for legacy canvas only
  379. aPolygon->Fracture( SHAPE_POLY_SET::PM_FAST );
  380. }