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.

225 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 <locale_io.h>
  26. #include <gerbview.h>
  27. #include <gerbview_frame.h>
  28. #include <gerber_file_image.h>
  29. #include <gerber_file_image_list.h>
  30. #include <view/view.h>
  31. #include <dialogs/html_messagebox.h>
  32. #include <macros.h>
  33. #include <wx/msgdlg.h>
  34. /* Read a gerber file, RS274D, RS274X or RS274X2 format.
  35. */
  36. bool GERBVIEW_FRAME::Read_GERBER_File( const wxString& GERBER_FullFileName )
  37. {
  38. wxString msg;
  39. int layer = GetActiveLayer();
  40. GERBER_FILE_IMAGE_LIST* images = GetImagesList();
  41. GERBER_FILE_IMAGE* gerber = GetGbrImage( layer );
  42. if( gerber != NULL )
  43. {
  44. Erase_Current_DrawLayer( false );
  45. }
  46. gerber = new GERBER_FILE_IMAGE( layer );
  47. // Read the gerber file. The image will be added only if it can be read
  48. // to avoid broken data.
  49. bool success = gerber->LoadGerberFile( GERBER_FullFileName );
  50. if( !success )
  51. {
  52. delete gerber;
  53. msg.Printf( _( "File \"%s\" not found" ), GERBER_FullFileName );
  54. ShowInfoBarError( msg );
  55. return false;
  56. }
  57. images->AddGbrImage( gerber, layer );
  58. // Display errors list
  59. if( gerber->GetMessages().size() > 0 )
  60. {
  61. HTML_MESSAGE_BOX dlg( this, _("Errors") );
  62. dlg.ListSet(gerber->GetMessages());
  63. dlg.ShowModal();
  64. }
  65. /* if the gerber file has items using D codes but missing D codes definitions,
  66. * it can be a deprecated RS274D file (i.e. without any aperture information),
  67. * or has missing definitions,
  68. * warn the user:
  69. */
  70. if( gerber->GetItemsCount() && gerber->m_Has_MissingDCode )
  71. {
  72. if( !gerber->m_Has_DCode )
  73. msg = _("Warning: this file has no D-Code definition\n"
  74. "Therefore the size of some items is undefined");
  75. else
  76. msg = _("Warning: this file has some missing D-Code definitions\n"
  77. "Therefore the size of some items is undefined");
  78. wxMessageBox( msg );
  79. }
  80. if( GetCanvas() )
  81. {
  82. if( gerber->m_ImageNegative )
  83. {
  84. // TODO: find a way to handle negative images
  85. // (maybe convert geometry into positives?)
  86. }
  87. for( auto item : gerber->GetItems() )
  88. GetCanvas()->GetView()->Add( (KIGFX::VIEW_ITEM*) item );
  89. }
  90. return true;
  91. }
  92. // size of a single line of text from a gerber file.
  93. // warning: some files can have *very long* lines, so the buffer must be large.
  94. #define GERBER_BUFZ 1000000
  95. // A large buffer to store one line
  96. static char lineBuffer[GERBER_BUFZ+1];
  97. bool GERBER_FILE_IMAGE::LoadGerberFile( const wxString& aFullFileName )
  98. {
  99. int G_command = 0; // command number for G commands like G04
  100. int D_commande = 0; // command number for D commands like D02
  101. char* text;
  102. ClearMessageList( );
  103. ResetDefaultValues();
  104. // Read the gerber file */
  105. m_Current_File = wxFopen( aFullFileName, wxT( "rt" ) );
  106. if( m_Current_File == 0 )
  107. return false;
  108. m_FileName = aFullFileName;
  109. LOCALE_IO toggleIo;
  110. wxString msg;
  111. while( true )
  112. {
  113. if( fgets( lineBuffer, GERBER_BUFZ, m_Current_File ) == NULL )
  114. break;
  115. m_LineNum++;
  116. text = StrPurge( lineBuffer );
  117. while( text && *text )
  118. {
  119. switch( *text )
  120. {
  121. case ' ':
  122. case '\r':
  123. case '\n':
  124. text++;
  125. break;
  126. case '*': // End command
  127. m_CommandState = END_BLOCK;
  128. text++;
  129. break;
  130. case 'M': // End file
  131. m_CommandState = CMD_IDLE;
  132. while( *text )
  133. text++;
  134. break;
  135. case 'G': /* Line type Gxx : command */
  136. G_command = GCodeNumber( text );
  137. Execute_G_Command( text, G_command );
  138. break;
  139. case 'D': /* Line type Dxx : Tool selection (xx > 0) or
  140. * command if xx = 0..9 */
  141. D_commande = DCodeNumber( text );
  142. Execute_DCODE_Command( text, D_commande );
  143. break;
  144. case 'X':
  145. case 'Y': /* Move or draw command */
  146. m_CurrentPos = ReadXYCoord( text );
  147. if( *text == '*' ) // command like X12550Y19250*
  148. {
  149. Execute_DCODE_Command( text, m_Last_Pen_Command );
  150. }
  151. break;
  152. case 'I':
  153. case 'J': /* Auxiliary Move command */
  154. m_IJPos = ReadIJCoord( text );
  155. if( *text == '*' ) // command like X35142Y15945J504*
  156. {
  157. Execute_DCODE_Command( text, m_Last_Pen_Command );
  158. }
  159. break;
  160. case '%':
  161. if( m_CommandState != ENTER_RS274X_CMD )
  162. {
  163. m_CommandState = ENTER_RS274X_CMD;
  164. ReadRS274XCommand( lineBuffer, GERBER_BUFZ, text );
  165. }
  166. else //Error
  167. {
  168. AddMessageToList( "Expected RS274X Command" );
  169. m_CommandState = CMD_IDLE;
  170. text++;
  171. }
  172. break;
  173. default:
  174. msg.Printf( "Unexpected char 0x%2.2X &lt;%c&lt;", *text, *text );
  175. AddMessageToList( msg );
  176. text++;
  177. break;
  178. }
  179. }
  180. }
  181. fclose( m_Current_File );
  182. m_InUse = true;
  183. return true;
  184. }