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-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 
29 #include <fctsys.h>
30 #include <wx/filename.h>
32 
33 #include <gerbview.h>
34 #include <richio.h>
35 #include <class_drawpanel.h>
36 #include <gerber_file_image.h>
37 #include <gerber_file_image_list.h>
38 #include <gerbview_frame.h>
39 #include <reporter.h>
40 #include <gbr_metadata.h>
41 #include <html_messagebox.h>
42 #include <view/view.h>
43 
44 #include "json11.hpp" // A light JSON parser
45 
78 {
79 public:
80  GERBER_JOBFILE_READER( const wxString& aFileName, REPORTER* aReporter )
81  {
82  m_filename = aFileName;
83  m_reporter = aReporter;
84  }
85 
87 
88  bool ReadGerberJobFile();
89  wxArrayString& GetGerberFiles() { return m_GerberFiles; }
90 
91 private:
93  wxFileName m_filename;
94  wxArrayString m_GerberFiles; // List of gerber files in job
95 };
96 
97 
99 {
100  // Read the gerber file */
101  FILE* jobFile = wxFopen( m_filename.GetFullPath(), wxT( "rt" ) );
102 
103  if( jobFile == nullptr )
104  return false;
105 
106  LOCALE_IO toggleIo;
107 
108  FILE_LINE_READER jobfileReader( jobFile, m_filename.GetFullPath() ); // Will close jobFile
109 
110  wxString msg;
111  wxString data;
112 
113  // detect the file format: old (deprecated) gerber format of official JSON format
114  bool json_format = false;
115 
116  char* line = jobfileReader.ReadLine();
117 
118  if( !line ) // end of file
119  return false;
120 
121  data = line;
122 
123  if( data.Contains( "{" ) )
124  json_format = true;
125 
126  if( json_format )
127  {
128  while( ( line = jobfileReader.ReadLine() ) )
129  data << '\n' << line;
130 
131  std::string err;
132  json11::Json json_parser = json11::Json::parse( TO_UTF8( data ), err );
133 
134  if( !err.empty() )
135  return false;
136 
137  for( auto& entry : json_parser["FilesAttributes"].array_items() )
138  {
139  //wxLogMessage( entry.dump().c_str() );
140  std::string name = entry["Path"].string_value();
141  //wxLogMessage( name.c_str() );
142  m_GerberFiles.Add( FormatStringFromGerber( name ) );
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 bool GERBVIEW_FRAME::LoadGerberJobFile( const wxString& aFullFileName )
159 {
160  wxFileName filename = aFullFileName;
161  wxString currentPath;
162  bool success = true;
163 
164  if( !filename.IsOk() )
165  {
166  // Use the current working directory if the file name path does not exist.
167  if( filename.DirExists() )
168  currentPath = filename.GetPath();
169  else
170  currentPath = m_mruPath;
171 
172  wxFileDialog dlg( this, _( "Open Gerber Job File" ),
173  currentPath,
174  filename.GetFullName(),
176  wxFD_OPEN | wxFD_FILE_MUST_EXIST | wxFD_CHANGE_DIR );
177 
178  if( dlg.ShowModal() == wxID_CANCEL )
179  return false;
180 
181  filename = dlg.GetPath();
182  currentPath = filename.GetPath();
183  m_mruPath = currentPath;
184  }
185  else
186  {
187  currentPath = filename.GetPath();
188  m_mruPath = currentPath;
189  }
190 
191  wxString msg;
192  WX_STRING_REPORTER reporter( &msg );
193 
194  if( filename.IsOk() )
195  {
196  GERBER_JOBFILE_READER gbjReader( filename.GetFullPath(), &reporter );
197 
198  if( gbjReader.ReadGerberJobFile() )
199  {
200  // Update the list of recent drill files.
201  UpdateFileHistory( filename.GetFullPath(), &m_jobFileHistory );
202 
203  Clear_DrawLayers( false );
204  ClearMsgPanel();
205 
206  wxArrayString& gbrfiles = gbjReader.GetGerberFiles();
207 
208  success = loadListOfGerberFiles( currentPath, gbrfiles );
209  }
210  }
211 
212  SortLayersByX2Attributes();
213 
214  SetActiveLayer( 0 );
215 
216  if( !msg.IsEmpty() )
217  {
218  wxSafeYield(); // Allows slice of time to redraw the screen
219  // to refresh widgets, before displaying messages
220  HTML_MESSAGE_BOX mbox( this, _( "Messages" ) );
221  mbox.ListSet( msg );
222  mbox.ShowModal();
223  }
224 
225  return success;
226 }
227 
228 
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
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown...
Definition: common.h:179
this class read and parse a Gerber job file to extract useful info for GerbView
Class REPORTER is a pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:61
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:47
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:109
void ListSet(const wxString &aList)
Function ListSet Add a list of items.
Subclass of DIALOG_DISPLAY_HTML_TEXT_BASE, which is generated by wxFormBuilder.
The common library.
wxArrayString & GetGerberFiles()
read a .gbrjob file
Class HTML_MESSAGE_BOX.
string & err
Definition: json11.cpp:598
Class WX_STRING_REPORTER is a wrapper for reporting to a wxString object.
Definition: reporter.h:163
wxString GerberJobFileWildcard()
const char * name
Definition: DXF_plotter.cpp:61
wxArrayString m_GerberFiles
wxString FormatStringFromGerber(const wxString &aString)
This helper function make the inverse conversion of formatStringToGerber() It converts a "normalized"...