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.2 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2007-2017 Jean-Pierre Charras jp.charras at wanadoo.fr
  5. * Copyright (C) 1992-2017 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. /**
  25. * @file job_file_reader.cpp
  26. */
  27. #include <fctsys.h>
  28. #include <wx/filename.h>
  29. #include <wildcards_and_files_ext.h>
  30. #include <gerbview.h>
  31. #include <richio.h>
  32. #include <gerber_file_image.h>
  33. #include <gerber_file_image_list.h>
  34. #include <gerbview_frame.h>
  35. #include <reporter.h>
  36. #include <gbr_metadata.h>
  37. #include <html_messagebox.h>
  38. #include <view/view.h>
  39. #include "json11.hpp" // A light JSON parser
  40. /**
  41. * this class read and parse a Gerber job file to extract useful info
  42. * for GerbView
  43. *
  44. * In a gerber job file, old (deprecated) format, data lines start by
  45. * %TF. (usual Gerber X2 info)
  46. * %TJ.B. (board info)
  47. * %TJ.D. (design info)
  48. * %TJ.L. (layers info)
  49. * some others are not yet handled by Kicad
  50. * M02* is the last line
  51. * In a gerber job file, JSON format, first lines are
  52. * {
  53. * "Header":
  54. * and the block ( a JSON array) containing the filename of files to load is
  55. * "FilesAttributes":
  56. * [
  57. * {
  58. * "Path": "interf_u-Composant.gbr",
  59. * "FileFunction": "Copper,L1,Top",
  60. * "FilePolarity": "Positive"
  61. * },
  62. * {
  63. * "Path": "interf_u-In1.Cu.gbr",
  64. * "FileFunction": "Copper,L2,Inr",
  65. * "FilePolarity": "Positive"
  66. * },
  67. * ],
  68. */
  69. class GERBER_JOBFILE_READER
  70. {
  71. public:
  72. GERBER_JOBFILE_READER( const wxString& aFileName, REPORTER* aReporter )
  73. {
  74. m_filename = aFileName;
  75. m_reporter = aReporter;
  76. }
  77. ~GERBER_JOBFILE_READER() {}
  78. bool ReadGerberJobFile(); /// read a .gbrjob file
  79. wxArrayString& GetGerberFiles() { return m_GerberFiles; }
  80. private:
  81. REPORTER* m_reporter;
  82. wxFileName m_filename;
  83. wxArrayString m_GerberFiles; // List of gerber files in job
  84. };
  85. bool GERBER_JOBFILE_READER::ReadGerberJobFile()
  86. {
  87. // Read the gerber file */
  88. FILE* jobFile = wxFopen( m_filename.GetFullPath(), wxT( "rt" ) );
  89. if( jobFile == nullptr )
  90. return false;
  91. LOCALE_IO toggleIo;
  92. FILE_LINE_READER jobfileReader( jobFile, m_filename.GetFullPath() ); // Will close jobFile
  93. wxString msg;
  94. wxString data;
  95. // detect the file format: old (deprecated) gerber format of official JSON format
  96. bool json_format = false;
  97. char* line = jobfileReader.ReadLine();
  98. if( !line ) // end of file
  99. return false;
  100. data = line;
  101. if( data.Contains( "{" ) )
  102. json_format = true;
  103. if( json_format )
  104. {
  105. while( ( line = jobfileReader.ReadLine() ) )
  106. data << '\n' << line;
  107. std::string err;
  108. json11::Json json_parser = json11::Json::parse( TO_UTF8( data ), err );
  109. if( !err.empty() )
  110. return false;
  111. for( auto& entry : json_parser["FilesAttributes"].array_items() )
  112. {
  113. //wxLogMessage( entry.dump().c_str() );
  114. std::string name = entry["Path"].string_value();
  115. //wxLogMessage( name.c_str() );
  116. m_GerberFiles.Add( FormatStringFromGerber( name ) );
  117. }
  118. }
  119. else
  120. {
  121. if( m_reporter )
  122. m_reporter->ReportTail( _( "This job file uses an outdated format. Please, recreate it." ),
  123. REPORTER::RPT_WARNING );
  124. return false;
  125. }
  126. return true;
  127. }
  128. bool GERBVIEW_FRAME::LoadGerberJobFile( const wxString& aFullFileName )
  129. {
  130. wxFileName filename = aFullFileName;
  131. wxString currentPath;
  132. bool success = true;
  133. if( !filename.IsOk() )
  134. {
  135. // Use the current working directory if the file name path does not exist.
  136. if( filename.DirExists() )
  137. currentPath = filename.GetPath();
  138. else
  139. currentPath = m_mruPath;
  140. wxFileDialog dlg( this, _( "Open Gerber Job File" ),
  141. currentPath,
  142. filename.GetFullName(),
  143. GerberJobFileWildcard(),
  144. wxFD_OPEN | wxFD_FILE_MUST_EXIST | wxFD_CHANGE_DIR );
  145. if( dlg.ShowModal() == wxID_CANCEL )
  146. return false;
  147. filename = dlg.GetPath();
  148. currentPath = filename.GetPath();
  149. m_mruPath = currentPath;
  150. }
  151. else
  152. {
  153. currentPath = filename.GetPath();
  154. m_mruPath = currentPath;
  155. }
  156. wxString msg;
  157. WX_STRING_REPORTER reporter( &msg );
  158. if( filename.IsOk() )
  159. {
  160. GERBER_JOBFILE_READER gbjReader( filename.GetFullPath(), &reporter );
  161. if( gbjReader.ReadGerberJobFile() )
  162. {
  163. // Update the list of recent drill files.
  164. UpdateFileHistory( filename.GetFullPath(), &m_jobFileHistory );
  165. Clear_DrawLayers( false );
  166. ClearMsgPanel();
  167. wxArrayString& gbrfiles = gbjReader.GetGerberFiles();
  168. success = loadListOfGerberAndDrillFiles( currentPath, gbrfiles );
  169. }
  170. }
  171. SortLayersByX2Attributes();
  172. SetActiveLayer( 0 );
  173. if( !msg.IsEmpty() )
  174. {
  175. wxSafeYield(); // Allows slice of time to redraw the screen
  176. // to refresh widgets, before displaying messages
  177. HTML_MESSAGE_BOX mbox( this, _( "Messages" ) );
  178. mbox.ListSet( msg );
  179. mbox.ShowModal();
  180. }
  181. return success;
  182. }