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