KiCad PCB EDA Suite
job_file_reader.cpp
Go to the documentation of this file.
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 
29 #include <fctsys.h>
30 #include <wx/filename.h>
32 #include <gerbview.h>
33 #include <richio.h>
34 #include <gerber_file_image.h>
35 #include <gerber_file_image_list.h>
36 #include <gerbview_frame.h>
37 #include <reporter.h>
38 #include <gbr_metadata.h>
39 #include <html_messagebox.h>
40 #include <view/view.h>
41 #include "json11.hpp" // A light JSON parser
42 
75 {
76 public:
77  GERBER_JOBFILE_READER( const wxString& aFileName, REPORTER* aReporter )
78  {
79  m_filename = aFileName;
80  m_reporter = aReporter;
81  }
82 
84 
85  bool ReadGerberJobFile();
86  wxArrayString& GetGerberFiles() { return m_GerberFiles; }
87 
88 private:
90  wxFileName m_filename;
91  wxArrayString m_GerberFiles; // List of gerber files in job
92 
93  // Convert a JSON string, that uses escaped sequence of 4 hexdecimal digits
94  // to encode unicode chars when not ASCII7 codes
95  // json11 converts this sequence to UTF8 string
96  wxString formatStringFromJSON( const std::string& name );
97 };
98 
99 
101 {
102  // Read the gerber file */
103  FILE* jobFile = wxFopen( m_filename.GetFullPath(), wxT( "rt" ) );
104 
105  if( jobFile == nullptr )
106  return false;
107 
108  LOCALE_IO toggleIo;
109 
110  FILE_LINE_READER jobfileReader( jobFile, m_filename.GetFullPath() ); // Will close jobFile
111 
112  wxString msg;
113  wxString data;
114 
115  // detect the file format: old (deprecated) gerber format of official JSON format
116  bool json_format = false;
117 
118  char* line = jobfileReader.ReadLine();
119 
120  if( !line ) // end of file
121  return false;
122 
123  data = line;
124 
125  if( data.Contains( "{" ) )
126  json_format = true;
127 
128  if( json_format )
129  {
130  while( ( line = jobfileReader.ReadLine() ) )
131  data << '\n' << line;
132 
133  std::string err;
134  json11::Json json_parser = json11::Json::parse( TO_UTF8( data ), err );
135 
136  if( !err.empty() )
137  return false;
138 
139  for( auto& entry : json_parser["FilesAttributes"].array_items() )
140  {
141  std::string name = entry["Path"].string_value();
143  }
144  }
145  else
146  {
147  if( m_reporter )
148  m_reporter->ReportTail( _( "This job file uses an outdated format. Please, recreate it." ),
150 
151  return false;
152  }
153 
154  return true;
155 }
156 
157 
158 wxString GERBER_JOBFILE_READER::formatStringFromJSON( const std::string& name )
159 {
160  // Convert a JSON string, that uses a escaped sequence of 4 hexdecimal digits
161  // to encode unicode chars
162  // Our json11 library returns in this case a UTF8 sequence. Just convert it to
163  // a wxString.
164  wxString wstr = FROM_UTF8( name.c_str() );
165  return wstr;
166 }
167 
168 
169 
170 bool GERBVIEW_FRAME::LoadGerberJobFile( const wxString& aFullFileName )
171 {
172  wxFileName filename = aFullFileName;
173  wxString currentPath;
174  bool success = true;
175 
176  if( !filename.IsOk() )
177  {
178  // Use the current working directory if the file name path does not exist.
179  if( filename.DirExists() )
180  currentPath = filename.GetPath();
181  else
182  currentPath = m_mruPath;
183 
184  wxFileDialog dlg( this, _( "Open Gerber Job File" ),
185  currentPath,
186  filename.GetFullName(),
188  wxFD_OPEN | wxFD_FILE_MUST_EXIST | wxFD_CHANGE_DIR );
189 
190  if( dlg.ShowModal() == wxID_CANCEL )
191  return false;
192 
193  filename = dlg.GetPath();
194  currentPath = filename.GetPath();
195  m_mruPath = currentPath;
196  }
197  else
198  {
199  currentPath = filename.GetPath();
200  m_mruPath = currentPath;
201  }
202 
203  wxString msg;
204  WX_STRING_REPORTER reporter( &msg );
205 
206  if( filename.IsOk() )
207  {
208  GERBER_JOBFILE_READER gbjReader( filename.GetFullPath(), &reporter );
209 
210  if( gbjReader.ReadGerberJobFile() )
211  {
212  // Update the list of recent drill files.
213  UpdateFileHistory( filename.GetFullPath(), &m_jobFileHistory );
214 
215  Clear_DrawLayers( false );
216  ClearMsgPanel();
217 
218  wxArrayString& gbrfiles = gbjReader.GetGerberFiles();
219 
220  success = loadListOfGerberAndDrillFiles( currentPath, gbrfiles );
221  }
222  }
223 
225 
226  SetActiveLayer( 0 );
227 
228  if( !msg.IsEmpty() )
229  {
230  wxSafeYield(); // Allows slice of time to redraw the screen
231  // to refresh widgets, before displaying messages
232  HTML_MESSAGE_BOX mbox( this, _( "Messages" ) );
233  mbox.ListSet( msg );
234  mbox.ShowModal();
235  }
236 
237  return success;
238 }
a class to handle special data (items attributes) during plot.
char * ReadLine() override
Function ReadLine reads a line of text into the buffer and increments the line number counter.
Definition: richio.cpp:194
void SortLayersByX2Attributes()
bool Clear_DrawLayers(bool query)
wxString m_mruPath
static wxString FROM_UTF8(const char *cstring)
function FROM_UTF8 converts a UTF8 encoded C string to a wxString for all wxWidgets build modes.
Definition: macros.h:62
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: common.h:154
FILE_HISTORY m_jobFileHistory
this class read and parse a Gerber job file to extract useful info for GerbView
void SetActiveLayer(int aLayer, bool doLayerWidgetUpdate=true)
Function SetActiveLayer will change the currently active layer to aLayer and also update the GERBER_L...
bool loadListOfGerberAndDrillFiles(const wxString &aPath, const wxArrayString &aFilenameList, const std::vector< int > *aFileType=nullptr)
Loads a list of Gerber and NC drill files and updates the view based on them.
Class REPORTER is a pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:61
void UpdateFileHistory(const wxString &FullFileName, wxFileHistory *aFileHistory=NULL)
Update the list of recently opened files.
bool LoadGerberJobFile(const wxString &aFileName)
Load a Gerber job file, and load gerber files found in job files.
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:48
Class FILE_LINE_READER is a LINE_READER that reads from an open file.
Definition: richio.h:180
GERBER_JOBFILE_READER(const wxString &aFileName, REPORTER *aReporter)
virtual REPORTER & ReportTail(const wxString &aText, SEVERITY aSeverity=RPT_UNDEFINED)
Function ReportTail Places the report at the end of the list, for objects that support report orderin...
Definition: reporter.h:111
void ListSet(const wxString &aList)
Add a list of items.
Subclass of DIALOG_DISPLAY_HTML_TEXT_BASE, which is generated by wxFormBuilder.
Definition of file extensions used in Kicad.
wxArrayString & GetGerberFiles()
read a .gbrjob file
Class HTML_MESSAGE_BOX.
virtual void ClearMsgPanel()
Clear all messages from the message panel.
#define _(s)
string & err
Definition: json11.cpp:650
Class WX_STRING_REPORTER is a wrapper for reporting to a wxString object.
Definition: reporter.h:165
wxString GerberJobFileWildcard()
const char * name
Definition: DXF_plotter.cpp:61
wxArrayString m_GerberFiles
wxString formatStringFromJSON(const std::string &name)