KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 The 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 <json_common.h>
30#include <wx/filename.h>
31
33#include <gerbview.h>
34#include <richio.h>
35#include <string_utils.h>
36#include <gerber_file_image.h>
38#include <gerbview_frame.h>
39#include <reporter.h>
40#include <gbr_metadata.h>
42#include <view/view.h>
43#include <wx/filedlg.h>
44
45
46using json = nlohmann::json;
47
80{
81public:
82 GERBER_JOBFILE_READER( const wxString& aFileName, REPORTER* aReporter )
83 {
84 m_filename = aFileName;
85 m_reporter = aReporter;
86 }
87
89
90 bool ReadGerberJobFile();
91 wxArrayString& GetGerberFiles() { return m_GerberFiles; }
92
93private:
95 wxFileName m_filename;
96 wxArrayString m_GerberFiles; // List of gerber files in job
97
98 // Convert a JSON string, that uses escaped sequence of 4 hexadecimal digits
99 // to encode unicode chars when not ASCII7 codes
100 // json11 converts this sequence to UTF8 string
101 wxString formatStringFromJSON( const std::string& name );
102};
103
104
106{
107 // Read the gerber file */
108 FILE* jobFile = wxFopen( m_filename.GetFullPath(), wxT( "rt" ) );
109
110 if( jobFile == nullptr )
111 return false;
112
113 FILE_LINE_READER jobfileReader( jobFile, m_filename.GetFullPath() ); // Will close jobFile
114
115 wxString data;
116
117 // detect the file format: old (deprecated) gerber format of official JSON format
118 bool json_format = false;
119
120 char* line = jobfileReader.ReadLine();
121
122 if( !line ) // end of file
123 return false;
124
125 data = line;
126
127 if( data.Contains( wxT( "{" ) ) )
128 json_format = true;
129
130 if( json_format )
131 {
132 while( ( line = jobfileReader.ReadLine() ) != nullptr )
133 data << '\n' << line;
134
135 try
136 {
137 json js = json::parse( TO_UTF8( data ) );
138
139 for( json& entry : js["FilesAttributes"] )
140 {
141 std::string name = entry["Path"].get<std::string>();
143 }
144 }
145 catch( ... )
146 {
147 return false;
148 }
149 }
150 else
151 {
152 if( m_reporter )
153 m_reporter->ReportTail( _( "This job file uses an outdated format. Please recreate it." ),
155
156 return false;
157 }
158
159 return true;
160}
161
162
164{
165 // Convert a JSON string, that uses a escaped sequence of 4 hexadecimal digits
166 // to encode unicode chars
167 // Our json11 library returns in this case a UTF8 sequence. Just convert it to
168 // a wxString.
169 wxString wstr = From_UTF8( name.c_str() );
170 return wstr;
171}
172
173
174
175bool GERBVIEW_FRAME::LoadGerberJobFile( const wxString& aFullFileName )
176{
177 wxFileName filename = aFullFileName;
178 wxString currentPath;
179 bool success = true;
180
181 if( !filename.IsOk() )
182 {
183 // Use the current working directory if the file name path does not exist.
184 if( filename.DirExists() )
185 currentPath = filename.GetPath();
186 else
187 currentPath = m_mruPath;
188
189 wxFileDialog dlg( this, _( "Open Gerber Job File" ),
190 currentPath,
191 filename.GetFullName(),
193 wxFD_OPEN | wxFD_FILE_MUST_EXIST | wxFD_CHANGE_DIR );
194
195 if( dlg.ShowModal() == wxID_CANCEL )
196 return false;
197
198 filename = dlg.GetPath();
199 currentPath = filename.GetPath();
200 m_mruPath = currentPath;
201 }
202 else
203 {
204 currentPath = filename.GetPath();
205 m_mruPath = currentPath;
206 }
207
208 WX_STRING_REPORTER reporter;
209
210 if( filename.IsOk() )
211 {
212 GERBER_JOBFILE_READER gbjReader( filename.GetFullPath(), &reporter );
213
214 if( gbjReader.ReadGerberJobFile() )
215 {
216 // Update the list of recent drill files.
217 UpdateFileHistory( filename.GetFullPath(), &m_jobFileHistory );
218
219 Clear_DrawLayers( false );
221
222 wxArrayString& gbrfiles = gbjReader.GetGerberFiles();
223
224 // 0 = Gerber file type
225 std::vector<int> fileTypesVec( gbrfiles.Count(), 0 );
226 success = LoadListOfGerberAndDrillFiles( currentPath, gbrfiles, &fileTypesVec );
227
228 Zoom_Automatique( false );
229 }
230 }
231
233
234 if( reporter.HasMessage() )
235 {
236 wxSafeYield(); // Allows slice of time to redraw the screen
237 // to refresh widgets, before displaying messages
238 HTML_MESSAGE_BOX mbox( this, _( "Messages" ) );
239 mbox.ListSet( reporter.GetMessages() );
240 mbox.ShowModal();
241 }
242
243 return success;
244}
const char * name
Definition: DXF_plotter.cpp:62
int ShowModal() override
void UpdateFileHistory(const wxString &FullFileName, FILE_HISTORY *aFileHistory=nullptr)
Update the list of recently opened files.
wxString m_mruPath
virtual void ClearMsgPanel()
Clear all messages from the message panel.
virtual void Zoom_Automatique(bool aWarpPointer)
Redraw the screen with best zoom level and the best centering that shows all the page or the board.
A LINE_READER that reads from an open file.
Definition: richio.h:185
char * ReadLine() override
Read a line of text into the buffer and increments the line number counter.
Definition: richio.cpp:249
this class read and parse a Gerber job file to extract useful info for GerbView
GERBER_JOBFILE_READER(const wxString &aFileName, REPORTER *aReporter)
wxArrayString m_GerberFiles
wxArrayString & GetGerberFiles()
read a .gbrjob file
wxString formatStringFromJSON(const std::string &name)
void SortLayersByX2Attributes()
bool LoadGerberJobFile(const wxString &aFileName)
Load a Gerber job file, and load gerber files found in job files.
FILE_HISTORY m_jobFileHistory
bool Clear_DrawLayers(bool query)
bool LoadListOfGerberAndDrillFiles(const wxString &aPath, const wxArrayString &aFilenameList, std::vector< int > *aFileType)
Load a list of Gerber and NC drill files and updates the view based on them.
void ListSet(const wxString &aList)
Add a list of items.
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:73
virtual REPORTER & ReportTail(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Places the report at the end of the list, for objects that support report ordering.
Definition: reporter.h:112
A wrapper for reporting to a wxString object.
Definition: reporter.h:190
bool HasMessage() const override
Returns true if the reporter client is non-empty.
Definition: reporter.cpp:96
const wxString & GetMessages() const
Definition: reporter.cpp:83
#define _(s)
Handle special data (items attributes) during plot.
nlohmann::json json
Definition: gerbview.cpp:50
static wxString GerberJobFileWildcard()
nlohmann::json json
@ RPT_SEVERITY_WARNING
wxString From_UTF8(const char *cstring)
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: string_utils.h:429
Definition of file extensions used in Kicad.