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.

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