2017-08-19 16:28:11 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2019-07-14 08:19:53 +00:00
|
|
|
* Copyright (C) 2007-2019 Jean-Pierre Charras jp.charras at wanadoo.fr
|
|
|
|
* Copyright (C) 1992-2019 KiCad Developers, see AUTHORS.txt for contributors.
|
2017-08-19 16:28:11 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file job_file_reader.cpp
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <fctsys.h>
|
2019-12-23 18:05:52 +00:00
|
|
|
#include <nlohmann/json.hpp>
|
2017-08-19 16:28:11 +00:00
|
|
|
#include <wx/filename.h>
|
2019-12-23 18:05:52 +00:00
|
|
|
|
2017-11-12 00:31:38 +00:00
|
|
|
#include <wildcards_and_files_ext.h>
|
2017-08-19 16:28:11 +00:00
|
|
|
#include <gerbview.h>
|
|
|
|
#include <richio.h>
|
2018-01-29 12:26:58 +00:00
|
|
|
#include <gerber_file_image.h>
|
|
|
|
#include <gerber_file_image_list.h>
|
2017-08-19 16:28:11 +00:00
|
|
|
#include <gerbview_frame.h>
|
|
|
|
#include <reporter.h>
|
2018-05-17 05:49:38 +00:00
|
|
|
#include <gbr_metadata.h>
|
2017-08-19 16:28:11 +00:00
|
|
|
#include <html_messagebox.h>
|
2018-02-27 02:15:32 +00:00
|
|
|
#include <view/view.h>
|
2019-12-23 18:05:52 +00:00
|
|
|
|
|
|
|
using json = nlohmann::json;
|
2017-08-19 16:28:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* this class read and parse a Gerber job file to extract useful info
|
|
|
|
* for GerbView
|
|
|
|
*
|
2018-03-16 18:32:49 +00:00
|
|
|
* In a gerber job file, old (deprecated) format, data lines start by
|
2017-08-19 16:28:11 +00:00
|
|
|
* %TF. (usual Gerber X2 info)
|
|
|
|
* %TJ.B. (board info)
|
|
|
|
* %TJ.D. (design info)
|
|
|
|
* %TJ.L. (layers info)
|
|
|
|
* some others are not yet handled by Kicad
|
|
|
|
* M02* is the last line
|
2018-03-16 18:32:49 +00:00
|
|
|
|
|
|
|
* In a gerber job file, JSON format, first lines are
|
|
|
|
* {
|
|
|
|
* "Header":
|
|
|
|
* and the block ( a JSON array) containing the filename of files to load is
|
|
|
|
* "FilesAttributes":
|
|
|
|
* [
|
|
|
|
* {
|
|
|
|
* "Path": "interf_u-Composant.gbr",
|
|
|
|
* "FileFunction": "Copper,L1,Top",
|
|
|
|
* "FilePolarity": "Positive"
|
|
|
|
* },
|
|
|
|
* {
|
|
|
|
* "Path": "interf_u-In1.Cu.gbr",
|
|
|
|
* "FileFunction": "Copper,L2,Inr",
|
|
|
|
* "FilePolarity": "Positive"
|
|
|
|
* },
|
|
|
|
* ],
|
2017-08-19 16:28:11 +00:00
|
|
|
*/
|
2018-03-16 18:32:49 +00:00
|
|
|
|
2017-08-19 16:28:11 +00:00
|
|
|
class GERBER_JOBFILE_READER
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
GERBER_JOBFILE_READER( const wxString& aFileName, REPORTER* aReporter )
|
|
|
|
{
|
|
|
|
m_filename = aFileName;
|
|
|
|
m_reporter = aReporter;
|
|
|
|
}
|
|
|
|
|
|
|
|
~GERBER_JOBFILE_READER() {}
|
|
|
|
|
2018-04-23 11:04:21 +00:00
|
|
|
bool ReadGerberJobFile(); /// read a .gbrjob file
|
2017-08-19 16:28:11 +00:00
|
|
|
wxArrayString& GetGerberFiles() { return m_GerberFiles; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
REPORTER* m_reporter;
|
|
|
|
wxFileName m_filename;
|
|
|
|
wxArrayString m_GerberFiles; // List of gerber files in job
|
2019-07-14 08:19:53 +00:00
|
|
|
|
|
|
|
// Convert a JSON string, that uses escaped sequence of 4 hexdecimal digits
|
|
|
|
// to encode unicode chars when not ASCII7 codes
|
|
|
|
// json11 converts this sequence to UTF8 string
|
|
|
|
wxString formatStringFromJSON( const std::string& name );
|
2017-08-19 16:28:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
bool GERBER_JOBFILE_READER::ReadGerberJobFile()
|
|
|
|
{
|
|
|
|
// Read the gerber file */
|
|
|
|
FILE* jobFile = wxFopen( m_filename.GetFullPath(), wxT( "rt" ) );
|
|
|
|
|
|
|
|
if( jobFile == nullptr )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
LOCALE_IO toggleIo;
|
|
|
|
|
|
|
|
FILE_LINE_READER jobfileReader( jobFile, m_filename.GetFullPath() ); // Will close jobFile
|
|
|
|
|
|
|
|
wxString msg;
|
|
|
|
wxString data;
|
|
|
|
|
2018-04-23 11:04:21 +00:00
|
|
|
// detect the file format: old (deprecated) gerber format of official JSON format
|
2018-03-16 18:32:49 +00:00
|
|
|
bool json_format = false;
|
|
|
|
|
|
|
|
char* line = jobfileReader.ReadLine();
|
|
|
|
|
|
|
|
if( !line ) // end of file
|
|
|
|
return false;
|
|
|
|
|
|
|
|
data = line;
|
|
|
|
|
2018-04-23 11:04:21 +00:00
|
|
|
if( data.Contains( "{" ) )
|
2018-03-16 18:32:49 +00:00
|
|
|
json_format = true;
|
|
|
|
|
|
|
|
if( json_format )
|
2017-08-19 16:28:11 +00:00
|
|
|
{
|
2018-03-16 18:32:49 +00:00
|
|
|
while( ( line = jobfileReader.ReadLine() ) )
|
|
|
|
data << '\n' << line;
|
2017-08-19 16:28:11 +00:00
|
|
|
|
2019-12-23 18:05:52 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
json js = json::parse( TO_UTF8( data ) );
|
2017-08-19 16:28:11 +00:00
|
|
|
|
2019-12-23 18:05:52 +00:00
|
|
|
for( json& entry : js["FilesAttributes"] )
|
|
|
|
{
|
|
|
|
std::string name = entry["Path"].get<std::string>();
|
|
|
|
m_GerberFiles.Add( formatStringFromJSON( name ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch( ... )
|
2017-08-19 16:28:11 +00:00
|
|
|
{
|
2019-12-23 18:05:52 +00:00
|
|
|
return false;
|
2017-08-19 16:28:11 +00:00
|
|
|
}
|
2018-03-16 18:32:49 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-04-23 11:04:21 +00:00
|
|
|
if( m_reporter )
|
2020-02-28 00:05:40 +00:00
|
|
|
m_reporter->ReportTail( _( "This job file uses an outdated format. Please recreate it." ),
|
2020-03-04 09:48:18 +00:00
|
|
|
RPT_SEVERITY_WARNING );
|
2017-08-19 16:28:11 +00:00
|
|
|
|
2018-04-23 11:04:21 +00:00
|
|
|
return false;
|
2017-08-19 16:28:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-14 08:19:53 +00:00
|
|
|
wxString GERBER_JOBFILE_READER::formatStringFromJSON( const std::string& name )
|
|
|
|
{
|
|
|
|
// Convert a JSON string, that uses a escaped sequence of 4 hexdecimal digits
|
|
|
|
// to encode unicode chars
|
|
|
|
// Our json11 library returns in this case a UTF8 sequence. Just convert it to
|
|
|
|
// a wxString.
|
|
|
|
wxString wstr = FROM_UTF8( name.c_str() );
|
|
|
|
return wstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-08-19 16:28:11 +00:00
|
|
|
bool GERBVIEW_FRAME::LoadGerberJobFile( const wxString& aFullFileName )
|
|
|
|
{
|
|
|
|
wxFileName filename = aFullFileName;
|
|
|
|
wxString currentPath;
|
2018-02-21 02:58:53 +00:00
|
|
|
bool success = true;
|
2017-08-19 16:28:11 +00:00
|
|
|
|
|
|
|
if( !filename.IsOk() )
|
|
|
|
{
|
|
|
|
// Use the current working directory if the file name path does not exist.
|
|
|
|
if( filename.DirExists() )
|
|
|
|
currentPath = filename.GetPath();
|
|
|
|
else
|
|
|
|
currentPath = m_mruPath;
|
|
|
|
|
|
|
|
wxFileDialog dlg( this, _( "Open Gerber Job File" ),
|
|
|
|
currentPath,
|
|
|
|
filename.GetFullName(),
|
2017-11-12 00:31:38 +00:00
|
|
|
GerberJobFileWildcard(),
|
2017-08-19 16:28:11 +00:00
|
|
|
wxFD_OPEN | wxFD_FILE_MUST_EXIST | wxFD_CHANGE_DIR );
|
|
|
|
|
|
|
|
if( dlg.ShowModal() == wxID_CANCEL )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
filename = dlg.GetPath();
|
2018-02-26 01:11:12 +00:00
|
|
|
currentPath = filename.GetPath();
|
2017-08-19 16:28:11 +00:00
|
|
|
m_mruPath = currentPath;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
currentPath = filename.GetPath();
|
|
|
|
m_mruPath = currentPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxString msg;
|
|
|
|
WX_STRING_REPORTER reporter( &msg );
|
|
|
|
|
|
|
|
if( filename.IsOk() )
|
|
|
|
{
|
|
|
|
GERBER_JOBFILE_READER gbjReader( filename.GetFullPath(), &reporter );
|
|
|
|
|
|
|
|
if( gbjReader.ReadGerberJobFile() )
|
|
|
|
{
|
|
|
|
// Update the list of recent drill files.
|
|
|
|
UpdateFileHistory( filename.GetFullPath(), &m_jobFileHistory );
|
|
|
|
|
|
|
|
Clear_DrawLayers( false );
|
|
|
|
ClearMsgPanel();
|
|
|
|
|
|
|
|
wxArrayString& gbrfiles = gbjReader.GetGerberFiles();
|
|
|
|
|
2018-11-26 11:49:13 +00:00
|
|
|
success = loadListOfGerberAndDrillFiles( currentPath, gbrfiles );
|
2017-08-19 16:28:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-28 02:33:41 +00:00
|
|
|
SortLayersByX2Attributes();
|
2018-02-27 02:15:32 +00:00
|
|
|
|
2018-03-08 23:19:38 +00:00
|
|
|
SetActiveLayer( 0 );
|
|
|
|
|
2017-08-19 16:28:11 +00:00
|
|
|
if( !msg.IsEmpty() )
|
|
|
|
{
|
|
|
|
wxSafeYield(); // Allows slice of time to redraw the screen
|
|
|
|
// to refresh widgets, before displaying messages
|
|
|
|
HTML_MESSAGE_BOX mbox( this, _( "Messages" ) );
|
|
|
|
mbox.ListSet( msg );
|
|
|
|
mbox.ShowModal();
|
|
|
|
}
|
|
|
|
|
2018-02-21 02:58:53 +00:00
|
|
|
return success;
|
2017-08-19 16:28:11 +00:00
|
|
|
}
|