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, see <https://www.gnu.org/licenses/>.
19 */
20
24
25#include <json_common.h>
26#include <wx/filename.h>
27
29#include <gerbview.h>
30#include <richio.h>
31#include <string_utils.h>
32#include <gerber_file_image.h>
34#include <gerbview_frame.h>
35#include <reporter.h>
36#include <gbr_metadata.h>
38#include <view/view.h>
39#include <wx/filedlg.h>
40#include <kiplatform/ui.h>
41
42
43using json = nlohmann::json;
44
75
77{
78public:
79 GERBER_JOBFILE_READER( const wxString& aFileName, REPORTER* aReporter )
80 {
81 m_filename = aFileName;
82 m_reporter = aReporter;
83 }
84
86
87 bool ReadGerberJobFile();
88 wxArrayString& GetGerberFiles() { return m_GerberFiles; }
89
90private:
92 wxFileName m_filename;
93 wxArrayString m_GerberFiles; // List of gerber files in job
94
95 // Convert a JSON string, that uses escaped sequence of 4 hexadecimal digits
96 // to encode unicode chars when not ASCII7 codes
97 // json11 converts this sequence to UTF8 string
98 wxString formatStringFromJSON( const std::string& name );
99};
100
101
103{
104 // Read the gerber file */
105 FILE* jobFile = wxFopen( m_filename.GetFullPath(), wxT( "rt" ) );
106
107 if( jobFile == nullptr )
108 return false;
109
110 FILE_LINE_READER jobfileReader( jobFile, m_filename.GetFullPath() ); // Will close jobFile
111
112 wxString data;
113
114 // detect the file format: old (deprecated) gerber format of official JSON format
115 bool json_format = false;
116
117 char* line = jobfileReader.ReadLine();
118
119 if( !line ) // end of file
120 return false;
121
122 data = line;
123
124 if( data.Contains( wxT( "{" ) ) )
125 json_format = true;
126
127 if( json_format )
128 {
129 while( ( line = jobfileReader.ReadLine() ) != nullptr )
130 data << '\n' << line;
131
132 try
133 {
134 json js = json::parse( TO_UTF8( data ) );
135
136 for( json& entry : js["FilesAttributes"] )
137 {
138 std::string name = entry["Path"].get<std::string>();
140 }
141 }
142 catch( ... )
143 {
144 return false;
145 }
146 }
147 else
148 {
149 if( m_reporter )
150 m_reporter->ReportTail( _( "This job file uses an outdated format. Please recreate it." ),
152
153 return false;
154 }
155
156 return true;
157}
158
159
161{
162 // Convert a JSON string, that uses a escaped sequence of 4 hexadecimal digits
163 // to encode unicode chars
164 // Our json11 library returns in this case a UTF8 sequence. Just convert it to
165 // a wxString.
166 wxString wstr = From_UTF8( name.c_str() );
167 return wstr;
168}
169
170
171
172bool GERBVIEW_FRAME::LoadGerberJobFile( const wxString& aFullFileName )
173{
174 wxFileName filename = aFullFileName;
175 wxString currentPath;
176 bool success = true;
177
178 if( !filename.IsOk() )
179 {
180 // Use the current working directory if the file name path does not exist.
181 if( filename.DirExists() )
182 currentPath = filename.GetPath();
183 else
184 currentPath = m_mruPath;
185
186 wxFileDialog dlg( this, _( "Open Gerber Job File" ),
187 currentPath,
188 filename.GetFullName(),
190 wxFD_OPEN | wxFD_FILE_MUST_EXIST | wxFD_CHANGE_DIR );
191
193
194 if( dlg.ShowModal() == wxID_CANCEL )
195 return false;
196
197 filename = dlg.GetPath();
198 currentPath = filename.GetPath();
199 m_mruPath = currentPath;
200 }
201 else
202 {
203 currentPath = filename.GetPath();
204 m_mruPath = currentPath;
205 }
206
208
209 if( filename.IsOk() )
210 {
211 GERBER_JOBFILE_READER gbjReader( filename.GetFullPath(), &reporter );
212
213 if( gbjReader.ReadGerberJobFile() )
214 {
215 // Update the list of recent drill files.
216 UpdateFileHistory( filename.GetFullPath(), &m_jobFileHistory );
217
218 Clear_DrawLayers( false );
220
221 wxArrayString& gbrfiles = gbjReader.GetGerberFiles();
222
223 // 0 = Gerber file type
224 std::vector<int> fileTypesVec( gbrfiles.Count(), 0 );
225 success = LoadListOfGerberAndDrillFiles( currentPath, gbrfiles, &fileTypesVec );
226
227 Zoom_Automatique( false );
228 }
229 }
230
232
233 if( reporter.HasMessage() )
234 {
235 wxSafeYield(); // Allows slice of time to redraw the screen
236 // to refresh widgets, before displaying messages
237 HTML_MESSAGE_BOX mbox( this, _( "Messages" ) );
238 mbox.ListSet( reporter.GetMessages() );
239 mbox.ShowModal();
240 }
241
242 return success;
243}
const char * name
int ShowModal() override
void UpdateFileHistory(const wxString &FullFileName, FILE_HISTORY *aFileHistory=nullptr)
Update the list of recently opened files.
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:154
char * ReadLine() override
Read a line of text into the buffer and increments the line number counter.
Definition richio.cpp:208
this class read and parse a Gerber job file to extract useful info for GerbView
GERBER_JOBFILE_READER(const wxString &aFileName, REPORTER *aReporter)
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:71
A wrapper for reporting to a wxString object.
Definition reporter.h:189
#define _(s)
Handle special data (items attributes) during plot.
nlohmann::json json
Definition gerbview.cpp:49
static wxString GerberJobFileWildcard()
void AllowNetworkFileSystems(wxDialog *aDialog)
Configure a file dialog to show network and virtual file systems.
Definition wxgtk/ui.cpp:448
@ 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.
IbisParser parser & reporter
Definition of file extensions used in Kicad.