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.

222 lines
6.6 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2007-2016 Jean-Pierre Charras jp.charras at wanadoo.fr
  5. * Copyright (C) 1992-2016 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #include <kicad_string.h>
  25. #include <gerbview.h>
  26. #include <gerbview_frame.h>
  27. #include <gerber_file_image.h>
  28. #include <gerber_file_image_list.h>
  29. #include <view/view.h>
  30. #include <html_messagebox.h>
  31. #include <macros.h>
  32. /* Read a gerber file, RS274D, RS274X or RS274X2 format.
  33. */
  34. bool GERBVIEW_FRAME::Read_GERBER_File( const wxString& GERBER_FullFileName )
  35. {
  36. wxString msg;
  37. int layer = GetActiveLayer();
  38. GERBER_FILE_IMAGE_LIST* images = GetImagesList();
  39. GERBER_FILE_IMAGE* gerber = GetGbrImage( layer );
  40. if( gerber != NULL )
  41. {
  42. Erase_Current_DrawLayer( false );
  43. }
  44. gerber = new GERBER_FILE_IMAGE( layer );
  45. // Read the gerber file. The image will be added only if it can be read
  46. // to avoid broken data.
  47. bool success = gerber->LoadGerberFile( GERBER_FullFileName );
  48. if( !success )
  49. {
  50. delete gerber;
  51. msg.Printf( _( "File \"%s\" not found" ), GERBER_FullFileName );
  52. ShowInfoBarError( msg );
  53. return false;
  54. }
  55. images->AddGbrImage( gerber, layer );
  56. // Display errors list
  57. if( gerber->GetMessages().size() > 0 )
  58. {
  59. HTML_MESSAGE_BOX dlg( this, _("Errors") );
  60. dlg.ListSet(gerber->GetMessages());
  61. dlg.ShowModal();
  62. }
  63. /* if the gerber file has items using D codes but missing D codes definitions,
  64. * it can be a deprecated RS274D file (i.e. without any aperture information),
  65. * or has missing definitions,
  66. * warn the user:
  67. */
  68. if( gerber->GetItemsCount() && gerber->m_Has_MissingDCode )
  69. {
  70. if( !gerber->m_Has_DCode )
  71. msg = _("Warning: this file has no D-Code definition\n"
  72. "Therefore the size of some items is undefined");
  73. else
  74. msg = _("Warning: this file has some missing D-Code definitions\n"
  75. "Therefore the size of some items is undefined");
  76. wxMessageBox( msg );
  77. }
  78. if( GetCanvas() )
  79. {
  80. if( gerber->m_ImageNegative )
  81. {
  82. // TODO: find a way to handle negative images
  83. // (maybe convert geometry into positives?)
  84. }
  85. for( auto item : gerber->GetItems() )
  86. GetCanvas()->GetView()->Add( (KIGFX::VIEW_ITEM*) item );
  87. }
  88. return true;
  89. }
  90. // size of a single line of text from a gerber file.
  91. // warning: some files can have *very long* lines, so the buffer must be large.
  92. #define GERBER_BUFZ 1000000
  93. // A large buffer to store one line
  94. static char lineBuffer[GERBER_BUFZ+1];
  95. bool GERBER_FILE_IMAGE::LoadGerberFile( const wxString& aFullFileName )
  96. {
  97. int G_command = 0; // command number for G commands like G04
  98. int D_commande = 0; // command number for D commands like D02
  99. char* text;
  100. ClearMessageList( );
  101. ResetDefaultValues();
  102. // Read the gerber file */
  103. m_Current_File = wxFopen( aFullFileName, wxT( "rt" ) );
  104. if( m_Current_File == 0 )
  105. return false;
  106. m_FileName = aFullFileName;
  107. LOCALE_IO toggleIo;
  108. wxString msg;
  109. while( true )
  110. {
  111. if( fgets( lineBuffer, GERBER_BUFZ, m_Current_File ) == NULL )
  112. break;
  113. m_LineNum++;
  114. text = StrPurge( lineBuffer );
  115. while( text && *text )
  116. {
  117. switch( *text )
  118. {
  119. case ' ':
  120. case '\r':
  121. case '\n':
  122. text++;
  123. break;
  124. case '*': // End command
  125. m_CommandState = END_BLOCK;
  126. text++;
  127. break;
  128. case 'M': // End file
  129. m_CommandState = CMD_IDLE;
  130. while( *text )
  131. text++;
  132. break;
  133. case 'G': /* Line type Gxx : command */
  134. G_command = GCodeNumber( text );
  135. Execute_G_Command( text, G_command );
  136. break;
  137. case 'D': /* Line type Dxx : Tool selection (xx > 0) or
  138. * command if xx = 0..9 */
  139. D_commande = DCodeNumber( text );
  140. Execute_DCODE_Command( text, D_commande );
  141. break;
  142. case 'X':
  143. case 'Y': /* Move or draw command */
  144. m_CurrentPos = ReadXYCoord( text );
  145. if( *text == '*' ) // command like X12550Y19250*
  146. {
  147. Execute_DCODE_Command( text, m_Last_Pen_Command );
  148. }
  149. break;
  150. case 'I':
  151. case 'J': /* Auxiliary Move command */
  152. m_IJPos = ReadIJCoord( text );
  153. if( *text == '*' ) // command like X35142Y15945J504*
  154. {
  155. Execute_DCODE_Command( text, m_Last_Pen_Command );
  156. }
  157. break;
  158. case '%':
  159. if( m_CommandState != ENTER_RS274X_CMD )
  160. {
  161. m_CommandState = ENTER_RS274X_CMD;
  162. ReadRS274XCommand( lineBuffer, GERBER_BUFZ, text );
  163. }
  164. else //Error
  165. {
  166. AddMessageToList( "Expected RS274X Command" );
  167. m_CommandState = CMD_IDLE;
  168. text++;
  169. }
  170. break;
  171. default:
  172. msg.Printf( "Unexpected char 0x%2.2X &lt;%c&lt;", *text, *text );
  173. AddMessageToList( msg );
  174. text++;
  175. break;
  176. }
  177. }
  178. }
  179. fclose( m_Current_File );
  180. m_InUse = true;
  181. return true;
  182. }