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.

784 lines
24 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
6 years ago
  1. /**
  2. * @file rs274d.cpp
  3. * @brief functions to read the rs274d commands from a rs274d/rs274x file
  4. */
  5. /*
  6. * This program source code file is part of KiCad, a free EDA CAD application.
  7. *
  8. * Copyright (C) 1992-2018 KiCad Developers, see AUTHORS.txt for contributors.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version 2
  13. * of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, you may find one here:
  22. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  23. * or you may search the http://www.gnu.org website for the version 2 license,
  24. * or you may write to the Free Software Foundation, Inc.,
  25. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  26. */
  27. #include <gerbview.h>
  28. #include <gerbview_frame.h>
  29. #include <trigo.h>
  30. #include <gerber_file_image.h>
  31. #include <X2_gerber_attributes.h>
  32. #include <cmath>
  33. /* Gerber: NOTES about some important commands found in RS274D and RS274X (G codes).
  34. * Some are now deprecated, but deprecated commands must be known by the Gerber reader
  35. * Gn =
  36. * G01 linear interpolation (linear trace)
  37. * G02, G20, G21 Circular interpolation, clockwise
  38. * G03, G30, G31 Circular interpolation, counterclockwise
  39. * G04 = comment. Since Sept 2014, file attributes and other X2 attributes can be found here
  40. * if the line starts by G04 #@!
  41. * G06 parabolic interpolation
  42. * G07 Cubic Interpolation
  43. * G10 linear interpolation (scale x10)
  44. * G11 linear interpolation (0.1x range)
  45. * G12 linear interpolation (0.01x scale)
  46. * G36 Start polygon mode (called a region, because the "polygon" can include arcs)
  47. * G37 Stop polygon mode (and close it)
  48. * G54 Selection Tool (outdated)
  49. * G60 linear interpolation (scale x100)
  50. * G70 Select Units = Inches
  51. * G71 Select Units = Millimeters
  52. * G74 enable 90 deg mode for arcs (CW or CCW)
  53. * G75 enable 360 degrees for arcs (CW or CCW)
  54. * G90 mode absolute coordinates
  55. *
  56. * X, Y
  57. * X and Y are followed by + or - and m + n digits (not separated)
  58. * m = integer part
  59. * n = part after the comma
  60. *ic formats: m = 2, n = 3 (size 2.3)
  61. * m = 3, n = 4 (size 3.4)
  62. * eg
  63. * GxxX00345Y-06123*
  64. *
  65. * Tools and D_CODES
  66. * Tool number (identification of shapes)
  67. * 10 to 999
  68. * D_CODES:
  69. * D01 ... D9 = command codes:
  70. * D01 = activating light (pen down) when placement
  71. * D02 = light extinction (pen up) when placement
  72. * D03 = Flash
  73. * D09 = VAPE Flash (I never see this command in gerber file)
  74. * D51 = G54 preceded by -> Select VAPE
  75. *
  76. * D10 ... D999 = Identification Tool: tool selection
  77. */
  78. /* Local Functions (are lower case since they are private to this source file)
  79. **/
  80. /**
  81. * Function fillFlashedGBRITEM
  82. * initializes a given GBRITEM so that it can draw a circle which is filled and
  83. * has no pen border.
  84. *
  85. * @param aGbrItem The GBRITEM to fill in.
  86. * @param aAperture the associated type of aperture
  87. * @param Dcode_index The DCODE value, like D14
  88. * @param aPos The center point of the flash
  89. * @param aSize The diameter of the round flash
  90. * @param aLayerNegative = true if the current layer is negative
  91. */
  92. void fillFlashedGBRITEM( GERBER_DRAW_ITEM* aGbrItem,
  93. APERTURE_T aAperture,
  94. int Dcode_index,
  95. const wxPoint& aPos,
  96. wxSize aSize,
  97. bool aLayerNegative )
  98. {
  99. aGbrItem->m_Size = aSize;
  100. aGbrItem->m_Start = aPos;
  101. aGbrItem->m_End = aGbrItem->m_Start;
  102. aGbrItem->m_DCode = Dcode_index;
  103. aGbrItem->SetLayerPolarity( aLayerNegative );
  104. aGbrItem->m_Flashed = true;
  105. aGbrItem->SetNetAttributes( aGbrItem->m_GerberImageFile->m_NetAttributeDict );
  106. switch( aAperture )
  107. {
  108. case APT_POLYGON: // flashed regular polygon
  109. aGbrItem->m_Shape = GBR_SPOT_POLY;
  110. break;
  111. case APT_CIRCLE:
  112. aGbrItem->m_Shape = GBR_SPOT_CIRCLE;
  113. aGbrItem->m_Size.y = aGbrItem->m_Size.x;
  114. break;
  115. case APT_OVAL:
  116. aGbrItem->m_Shape = GBR_SPOT_OVAL;
  117. break;
  118. case APT_RECT:
  119. aGbrItem->m_Shape = GBR_SPOT_RECT;
  120. break;
  121. case APT_MACRO:
  122. aGbrItem->m_Shape = GBR_SPOT_MACRO;
  123. // Cache the bounding box for aperture macros
  124. aGbrItem->GetDcodeDescr()->GetMacro()->GetApertureMacroShape( aGbrItem, aPos );
  125. break;
  126. }
  127. }
  128. /**
  129. * Function fillLineGBRITEM
  130. * initializes a given GBRITEM so that it can draw a linear D code.
  131. *
  132. * @param aGbrItem The GERBER_DRAW_ITEM to fill in.
  133. * @param Dcode_index The DCODE value, like D14
  134. * @param aStart The starting point of the line
  135. * @param aEnd The ending point of the line
  136. * @param aPenSize The size of the flash. Note rectangular shapes are legal.
  137. * @param aLayerNegative = true if the current layer is negative
  138. */
  139. void fillLineGBRITEM( GERBER_DRAW_ITEM* aGbrItem,
  140. int Dcode_index,
  141. const wxPoint& aStart,
  142. const wxPoint& aEnd,
  143. wxSize aPenSize,
  144. bool aLayerNegative )
  145. {
  146. aGbrItem->m_Flashed = false;
  147. aGbrItem->m_Size = aPenSize;
  148. aGbrItem->m_Start = aStart;
  149. aGbrItem->m_End = aEnd;
  150. aGbrItem->m_DCode = Dcode_index;
  151. aGbrItem->SetLayerPolarity( aLayerNegative );
  152. aGbrItem->SetNetAttributes( aGbrItem->m_GerberImageFile->m_NetAttributeDict );
  153. }
  154. /**
  155. * Function fillArcGBRITEM
  156. * initializes a given GBRITEM so that it can draw an arc G code.
  157. * <p>
  158. * if multiquadrant == true : arc can be 0 to 360 degrees
  159. * and \a rel_center is the center coordinate relative to start point.
  160. * <p>
  161. * if multiquadrant == false arc can be only 0 to 90 deg,
  162. * and only in the same quadrant :
  163. * <ul>
  164. * <li> absolute angle 0 to 90 (quadrant 1) or
  165. * <li> absolute angle 90 to 180 (quadrant 2) or
  166. * <li> absolute angle 180 to 270 (quadrant 3) or
  167. * <li> absolute angle 270 to 0 (quadrant 4)
  168. * </ul><p>
  169. * @param aGbrItem is the GBRITEM to fill in.
  170. * @param Dcode_index is the DCODE value, like D14
  171. * @param aStart is the starting point
  172. * @param aEnd is the ending point
  173. * @param aRelCenter is the center coordinate relative to start point,
  174. * given in ABSOLUTE VALUE and the sign of values x et y de rel_center
  175. * must be calculated from the previously given constraint: arc only in the same quadrant.
  176. * @param aClockwise true if arc must be created clockwise
  177. * @param aPenSize The size of the flash. Note rectangular shapes are legal.
  178. * @param aMultiquadrant = true to create arcs upto 360 deg,
  179. * false when arc is inside one quadrant
  180. * @param aLayerNegative = true if the current layer is negative
  181. */
  182. void fillArcGBRITEM( GERBER_DRAW_ITEM* aGbrItem, int Dcode_index,
  183. const wxPoint& aStart, const wxPoint& aEnd,
  184. const wxPoint& aRelCenter, wxSize aPenSize,
  185. bool aClockwise, bool aMultiquadrant,
  186. bool aLayerNegative )
  187. {
  188. wxPoint center, delta;
  189. aGbrItem->m_Shape = GBR_ARC;
  190. aGbrItem->m_Size = aPenSize;
  191. aGbrItem->m_Flashed = false;
  192. if( aGbrItem->m_GerberImageFile )
  193. aGbrItem->SetNetAttributes( aGbrItem->m_GerberImageFile->m_NetAttributeDict );
  194. if( aMultiquadrant )
  195. center = aStart + aRelCenter;
  196. else
  197. {
  198. // in single quadrant mode the relative coordinate aRelCenter is always >= 0
  199. // So we must recalculate the actual sign of aRelCenter.x and aRelCenter.y
  200. center = aRelCenter;
  201. // calculate arc end coordinate relative to the starting point,
  202. // because center is relative to the center point
  203. delta = aEnd - aStart;
  204. // now calculate the relative to aStart center position, for a draw function
  205. // that use trigonometric arc angle (or counter-clockwise)
  206. /* Quadrants:
  207. * Y
  208. * 2 | 1
  209. * -------X
  210. * 3 | 4
  211. * C = actual relative arc center, S = arc start (axis origin) E = relative arc end
  212. */
  213. if( (delta.x >= 0) && (delta.y >= 0) )
  214. {
  215. /* Quadrant 1 (trigo or cclockwise):
  216. * C | E
  217. * ---S---
  218. * 3 | 4
  219. */
  220. center.x = -center.x;
  221. }
  222. else if( (delta.x >= 0) && (delta.y < 0) )
  223. {
  224. /* Quadrant 4 (trigo or cclockwise):
  225. * 2 | C
  226. * ---S---
  227. * 3 | E
  228. */
  229. // Nothing to do
  230. }
  231. else if( (delta.x < 0) && (delta.y >= 0) )
  232. {
  233. /* Quadrant 2 (trigo or cclockwise):
  234. * E | 1
  235. * ---S---
  236. * C | 4
  237. */
  238. center.x = -center.x;
  239. center.y = -center.y;
  240. }
  241. else
  242. {
  243. /* Quadrant 3 (trigo or cclockwise):
  244. * 2 | 1
  245. * ---S---
  246. * E | C
  247. */
  248. center.y = -center.y;
  249. }
  250. // Due to your draw arc function, we need this:
  251. if( !aClockwise )
  252. center = - center;
  253. // Calculate actual arc center coordinate:
  254. center += aStart;
  255. }
  256. if( aClockwise )
  257. {
  258. aGbrItem->m_Start = aStart;
  259. aGbrItem->m_End = aEnd;
  260. }
  261. else
  262. {
  263. aGbrItem->m_Start = aEnd;
  264. aGbrItem->m_End = aStart;
  265. }
  266. aGbrItem->m_ArcCentre = center;
  267. aGbrItem->m_DCode = Dcode_index;
  268. aGbrItem->SetLayerPolarity( aLayerNegative );
  269. }
  270. /**
  271. * Function fillArcPOLY
  272. * creates an arc G code when found in poly outlines.
  273. * <p>
  274. * if multiquadrant == true : arc can be 0 to 360 degrees
  275. * and \a rel_center is the center coordinate relative to start point.
  276. * <p>
  277. * if multiquadrant == false arc can be only 0 to 90 deg,
  278. * and only in the same quadrant :
  279. * <ul>
  280. * <li> absolute angle 0 to 90 (quadrant 1) or
  281. * <li> absolute angle 90 to 180 (quadrant 2) or
  282. * <li> absolute angle 180 to 270 (quadrant 3) or
  283. * <li> absolute angle 270 to 0 (quadrant 4)
  284. * </ul><p>
  285. * @param aGbrItem is the GBRITEM to fill in.
  286. * @param aStart is the starting point
  287. * @param aEnd is the ending point
  288. * @param rel_center is the center coordinate relative to start point,
  289. * given in ABSOLUTE VALUE and the sign of values x et y de rel_center
  290. * must be calculated from the previously given constraint: arc only in the
  291. * same quadrant.
  292. * @param aClockwise true if arc must be created clockwise
  293. * @param aMultiquadrant = true to create arcs upto 360 deg,
  294. * false when arc is inside one quadrant
  295. * @param aLayerNegative = true if the current layer is negative
  296. */
  297. static void fillArcPOLY( GERBER_DRAW_ITEM* aGbrItem,
  298. const wxPoint& aStart, const wxPoint& aEnd,
  299. const wxPoint& rel_center,
  300. bool aClockwise, bool aMultiquadrant,
  301. bool aLayerNegative )
  302. {
  303. /* in order to calculate arc parameters, we use fillArcGBRITEM
  304. * so we muse create a dummy track and use its geometric parameters
  305. */
  306. static GERBER_DRAW_ITEM dummyGbrItem( NULL );
  307. aGbrItem->SetLayerPolarity( aLayerNegative );
  308. fillArcGBRITEM( &dummyGbrItem, 0,
  309. aStart, aEnd, rel_center, wxSize(0, 0),
  310. aClockwise, aMultiquadrant, aLayerNegative );
  311. aGbrItem->SetNetAttributes( aGbrItem->m_GerberImageFile->m_NetAttributeDict );
  312. wxPoint center;
  313. center = dummyGbrItem.m_ArcCentre;
  314. // Calculate coordinates relative to arc center;
  315. wxPoint start = dummyGbrItem.m_Start - center;
  316. wxPoint end = dummyGbrItem.m_End - center;
  317. /* Calculate angle arc
  318. * angles are in 0.1 deg
  319. * angle is trigonometrical (counter-clockwise),
  320. * and axis is the X,Y gerber coordinates
  321. */
  322. double start_angle = ArcTangente( start.y, start.x );
  323. double end_angle = ArcTangente( end.y, end.x );
  324. // dummyTrack has right geometric parameters, but
  325. // fillArcGBRITEM calculates arc parameters for a draw function that expects
  326. // start_angle < end_angle. So ensure this is the case here:
  327. // Due to the fact atan2 returns angles between -180 to + 180 degrees,
  328. // this is not always the case ( a modulo 360.0 degrees can be lost )
  329. if( start_angle > end_angle )
  330. end_angle += 3600;
  331. double arc_angle = start_angle - end_angle;
  332. // Approximate arc by 36 segments per 360 degree
  333. const int increment_angle = 3600 / 36;
  334. int count = std::abs( arc_angle / increment_angle );
  335. if( aGbrItem->m_Polygon.OutlineCount() == 0 )
  336. aGbrItem->m_Polygon.NewOutline();
  337. // calculate polygon corners
  338. // when arc is counter-clockwise, dummyGbrItem arc goes from end to start
  339. // and we must always create a polygon from start to end.
  340. wxPoint start_arc = start;
  341. for( int ii = 0; ii <= count; ii++ )
  342. {
  343. double rot;
  344. wxPoint end_arc = start;
  345. if( aClockwise )
  346. rot = ii * increment_angle; // rot is in 0.1 deg
  347. else
  348. rot = (count - ii) * increment_angle; // rot is in 0.1 deg
  349. if( ii < count )
  350. RotatePoint( &end_arc, -rot );
  351. else // last point
  352. end_arc = aClockwise ? end : start;
  353. aGbrItem->m_Polygon.Append( VECTOR2I( end_arc + center ) );
  354. start_arc = end_arc;
  355. }
  356. }
  357. /* Read the Gnn sequence and returns the value nn.
  358. */
  359. int GERBER_FILE_IMAGE::GCodeNumber( char*& Text )
  360. {
  361. int ii = 0;
  362. char* text;
  363. char line[1024];
  364. if( Text == NULL )
  365. return 0;
  366. Text++;
  367. text = line;
  368. while( IsNumber( *Text ) )
  369. {
  370. *(text++) = *(Text++);
  371. }
  372. *text = 0;
  373. ii = atoi( line );
  374. return ii;
  375. }
  376. /* Get the sequence Dnn and returns the value nn
  377. */
  378. int GERBER_FILE_IMAGE::DCodeNumber( char*& Text )
  379. {
  380. int ii = 0;
  381. char* text;
  382. char line[1024];
  383. if( Text == NULL )
  384. return 0;
  385. Text++;
  386. text = line;
  387. while( IsNumber( *Text ) )
  388. *(text++) = *(Text++);
  389. *text = 0;
  390. ii = atoi( line );
  391. return ii;
  392. }
  393. bool GERBER_FILE_IMAGE::Execute_G_Command( char*& text, int G_command )
  394. {
  395. // D( printf( "%22s: G_CODE<%d>\n", __func__, G_command ); )
  396. switch( G_command )
  397. {
  398. case GC_PHOTO_MODE: // can starts a D03 flash command: redundant, can
  399. // be safely ignored
  400. break;
  401. case GC_LINEAR_INTERPOL_1X:
  402. m_Iterpolation = GERB_INTERPOL_LINEAR_1X;
  403. break;
  404. case GC_CIRCLE_NEG_INTERPOL:
  405. m_Iterpolation = GERB_INTERPOL_ARC_NEG;
  406. break;
  407. case GC_CIRCLE_POS_INTERPOL:
  408. m_Iterpolation = GERB_INTERPOL_ARC_POS;
  409. break;
  410. case GC_COMMENT:
  411. // Skip comment, but only if the line does not start by "G04 #@! "
  412. // which is a metadata, i.e. a X2 command inside the comment.
  413. // this comment is called a "structured comment"
  414. if( strncmp( text, " #@! ", 5 ) == 0 )
  415. {
  416. text += 5;
  417. // The string starting at text is the same as the X2 attribute,
  418. // but a X2 attribute ends by '%'. So we build the X2 attribute string
  419. std::string x2buf;
  420. while( *text && (*text != '*') )
  421. {
  422. x2buf += *text;
  423. text++;
  424. }
  425. // add the end of X2 attribute string
  426. x2buf += "*%";
  427. x2buf += '\0';
  428. char* cptr = (char*)x2buf.data();
  429. int code_command = ReadXCommandID( cptr );
  430. ExecuteRS274XCommand( code_command, NULL, 0, cptr );
  431. }
  432. while( *text && (*text != '*') )
  433. text++;
  434. break;
  435. case GC_SELECT_TOOL:
  436. {
  437. int D_commande = DCodeNumber( text );
  438. if( D_commande < FIRST_DCODE )
  439. return false;
  440. if( D_commande > (TOOLS_MAX_COUNT - 1) )
  441. D_commande = TOOLS_MAX_COUNT - 1;
  442. m_Current_Tool = D_commande;
  443. D_CODE* pt_Dcode = GetDCODE( D_commande );
  444. if( pt_Dcode )
  445. pt_Dcode->m_InUse = true;
  446. break;
  447. }
  448. case GC_SPECIFY_INCHES:
  449. m_GerbMetric = false; // false = Inches, true = metric
  450. break;
  451. case GC_SPECIFY_MILLIMETERS:
  452. m_GerbMetric = true; // false = Inches, true = metric
  453. break;
  454. case GC_TURN_OFF_360_INTERPOL: // disable Multi cadran arc and Arc interpol
  455. m_360Arc_enbl = false;
  456. m_Iterpolation = GERB_INTERPOL_LINEAR_1X; // not sure it should be done
  457. m_AsArcG74G75Cmd = true;
  458. break;
  459. case GC_TURN_ON_360_INTERPOL:
  460. m_360Arc_enbl = true;
  461. m_AsArcG74G75Cmd = true;
  462. break;
  463. case GC_SPECIFY_ABSOLUES_COORD:
  464. m_Relative = false; // false = absolute Coord, true = relative
  465. // Coord
  466. break;
  467. case GC_SPECIFY_RELATIVEES_COORD:
  468. m_Relative = true; // false = absolute Coord, true = relative
  469. // Coord
  470. break;
  471. case GC_TURN_ON_POLY_FILL:
  472. m_PolygonFillMode = true;
  473. m_Exposure = false;
  474. break;
  475. case GC_TURN_OFF_POLY_FILL:
  476. if( m_Exposure && GetLastItemInList() ) // End of polygon
  477. {
  478. GERBER_DRAW_ITEM * gbritem = GetLastItemInList();
  479. if( gbritem->m_Polygon.VertexCount() )
  480. gbritem->m_Polygon.Append( gbritem->m_Polygon.CVertex( 0 ) );
  481. StepAndRepeatItem( *gbritem );
  482. }
  483. m_Exposure = false;
  484. m_PolygonFillMode = false;
  485. m_PolygonFillModeState = 0;
  486. m_Iterpolation = GERB_INTERPOL_LINEAR_1X; // not sure it should be done
  487. break;
  488. case GC_MOVE: // Non existent
  489. default:
  490. {
  491. wxString msg;
  492. msg.Printf( wxT( "G%0.2d command not handled" ), G_command );
  493. AddMessageToList( msg );
  494. return false;
  495. }
  496. }
  497. return true;
  498. }
  499. bool GERBER_FILE_IMAGE::Execute_DCODE_Command( char*& text, int D_commande )
  500. {
  501. wxSize size( 15, 15 );
  502. APERTURE_T aperture = APT_CIRCLE;
  503. GERBER_DRAW_ITEM* gbritem;
  504. int dcode = 0;
  505. D_CODE* tool = NULL;
  506. wxString msg;
  507. if( D_commande >= FIRST_DCODE ) // This is a "Set tool" command
  508. {
  509. if( D_commande > (TOOLS_MAX_COUNT - 1) )
  510. D_commande = TOOLS_MAX_COUNT - 1;
  511. // remember which tool is selected, nothing is done with it in this
  512. // call
  513. m_Current_Tool = D_commande;
  514. D_CODE* pt_Dcode = GetDCODE( D_commande );
  515. if( pt_Dcode )
  516. pt_Dcode->m_InUse = true;
  517. else
  518. m_Has_MissingDCode = true;
  519. return true;
  520. }
  521. else // D_commande = 0..9: this is a pen command (usually D1, D2 or D3)
  522. {
  523. m_Last_Pen_Command = D_commande;
  524. }
  525. if( m_PolygonFillMode ) // Enter a polygon description:
  526. {
  527. switch( D_commande )
  528. {
  529. case 1: // code D01 Draw line, exposure ON
  530. if( !m_Exposure ) // Start a new polygon outline:
  531. {
  532. m_Exposure = true;
  533. gbritem = new GERBER_DRAW_ITEM( this );
  534. AddItemToList( gbritem );
  535. gbritem->m_Shape = GBR_POLYGON;
  536. gbritem->m_Flashed = false;
  537. gbritem->m_DCode = 0; // No DCode for a Polygon (Region in Gerber dialect)
  538. if( gbritem->m_GerberImageFile )
  539. {
  540. gbritem->SetNetAttributes( gbritem->m_GerberImageFile->m_NetAttributeDict );
  541. gbritem->m_AperFunction = gbritem->m_GerberImageFile->m_AperFunction;
  542. }
  543. }
  544. switch( m_Iterpolation )
  545. {
  546. case GERB_INTERPOL_ARC_NEG:
  547. case GERB_INTERPOL_ARC_POS:
  548. // Before any arc command, a G74 or G75 command must be set.
  549. // Otherwise the Gerber file is invalid
  550. if( !m_AsArcG74G75Cmd )
  551. {
  552. AddMessageToList( _( "Invalid Gerber file: missing G74 or G75 arc command" ) );
  553. // Disable further warning messages:
  554. m_AsArcG74G75Cmd = true;
  555. }
  556. gbritem = GetLastItemInList();
  557. fillArcPOLY( gbritem, m_PreviousPos,
  558. m_CurrentPos, m_IJPos,
  559. ( m_Iterpolation == GERB_INTERPOL_ARC_NEG ) ? false : true,
  560. m_360Arc_enbl, GetLayerParams().m_LayerNegative );
  561. break;
  562. default:
  563. gbritem = GetLastItemInList();
  564. gbritem->m_Start = m_PreviousPos; // m_Start is used as temporary storage
  565. if( gbritem->m_Polygon.OutlineCount() == 0 )
  566. {
  567. gbritem->m_Polygon.NewOutline();
  568. gbritem->m_Polygon.Append( VECTOR2I( gbritem->m_Start ) );
  569. }
  570. gbritem->m_End = m_CurrentPos; // m_End is used as temporary storage
  571. gbritem->m_Polygon.Append( VECTOR2I( gbritem->m_End ) );
  572. break;
  573. }
  574. m_PreviousPos = m_CurrentPos;
  575. m_PolygonFillModeState = 1;
  576. break;
  577. case 2: // code D2: exposure OFF (i.e. "move to")
  578. if( m_Exposure && GetLastItemInList() ) // End of polygon
  579. {
  580. gbritem = GetLastItemInList();
  581. gbritem->m_Polygon.Append( gbritem->m_Polygon.CVertex( 0 ) );
  582. StepAndRepeatItem( *gbritem );
  583. }
  584. m_Exposure = false;
  585. m_PreviousPos = m_CurrentPos;
  586. m_PolygonFillModeState = 0;
  587. break;
  588. default:
  589. return false;
  590. }
  591. }
  592. else
  593. {
  594. switch( D_commande )
  595. {
  596. case 1: // code D01 Draw line, exposure ON
  597. m_Exposure = true;
  598. tool = GetDCODE( m_Current_Tool );
  599. if( tool )
  600. {
  601. size = tool->m_Size;
  602. dcode = tool->m_Num_Dcode;
  603. aperture = tool->m_Shape;
  604. }
  605. switch( m_Iterpolation )
  606. {
  607. case GERB_INTERPOL_LINEAR_1X:
  608. gbritem = new GERBER_DRAW_ITEM( this );
  609. AddItemToList( gbritem );
  610. fillLineGBRITEM( gbritem, dcode, m_PreviousPos,
  611. m_CurrentPos, size, GetLayerParams().m_LayerNegative );
  612. StepAndRepeatItem( *gbritem );
  613. break;
  614. case GERB_INTERPOL_ARC_NEG:
  615. case GERB_INTERPOL_ARC_POS:
  616. gbritem = new GERBER_DRAW_ITEM( this );
  617. AddItemToList( gbritem );
  618. if( m_LastCoordIsIJPos )
  619. {
  620. fillArcGBRITEM( gbritem, dcode, m_PreviousPos,
  621. m_CurrentPos, m_IJPos, size,
  622. ( m_Iterpolation == GERB_INTERPOL_ARC_NEG ) ?
  623. false : true, m_360Arc_enbl, GetLayerParams().m_LayerNegative );
  624. m_LastCoordIsIJPos = false;
  625. }
  626. else
  627. {
  628. fillLineGBRITEM( gbritem, dcode, m_PreviousPos,
  629. m_CurrentPos, size, GetLayerParams().m_LayerNegative );
  630. }
  631. StepAndRepeatItem( *gbritem );
  632. break;
  633. default:
  634. msg.Printf( wxT( "RS274D: DCODE Command: interpol error (type %X)" ),
  635. m_Iterpolation );
  636. AddMessageToList( msg );
  637. break;
  638. }
  639. m_PreviousPos = m_CurrentPos;
  640. break;
  641. case 2: // code D2: exposure OFF (i.e. "move to")
  642. m_Exposure = false;
  643. m_PreviousPos = m_CurrentPos;
  644. break;
  645. case 3: // code D3: flash aperture
  646. tool = GetDCODE( m_Current_Tool );
  647. if( tool )
  648. {
  649. size = tool->m_Size;
  650. dcode = tool->m_Num_Dcode;
  651. aperture = tool->m_Shape;
  652. }
  653. gbritem = new GERBER_DRAW_ITEM( this );
  654. AddItemToList( gbritem );
  655. fillFlashedGBRITEM( gbritem, aperture, dcode, m_CurrentPos,
  656. size, GetLayerParams().m_LayerNegative );
  657. StepAndRepeatItem( *gbritem );
  658. m_PreviousPos = m_CurrentPos;
  659. break;
  660. default:
  661. return false;
  662. }
  663. }
  664. return true;
  665. }