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
|
|
|
|
*/
|
|
|
|
|
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>
|
2020-10-24 01:38:50 +00:00
|
|
|
#include <locale_io.h>
|
2023-09-09 04:10:57 +00:00
|
|
|
#include <string_utils.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>
|
2021-09-14 18:26:03 +00:00
|
|
|
#include <dialogs/html_message_box.h>
|
2018-02-27 02:15:32 +00:00
|
|
|
#include <view/view.h>
|
2021-05-01 07:50:29 +00:00
|
|
|
#include <wx/filedlg.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
|
|
|
|
Fix source comment/doc typos (follow-up)
Found via `codespell -q 3 -S *.po,./thirdparty -L aactual,acount,aline,alocation,alog,anormal,anumber,aother,apoints,aparent,aray,dout,einstance,modul,ot,overide,serie,te,,tesselate,tesselator,tht`
2021-07-03 22:37:31 +00:00
|
|
|
// Convert a JSON string, that uses escaped sequence of 4 hexadecimal digits
|
2019-07-14 08:19:53 +00:00
|
|
|
// 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 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;
|
|
|
|
|
2022-02-05 19:31:22 +00:00
|
|
|
if( data.Contains( wxT( "{" ) ) )
|
2018-03-16 18:32:49 +00:00
|
|
|
json_format = true;
|
|
|
|
|
|
|
|
if( json_format )
|
2017-08-19 16:28:11 +00:00
|
|
|
{
|
2023-04-11 08:53:35 +00:00
|
|
|
while( ( line = jobfileReader.ReadLine() ) != nullptr )
|
2018-03-16 18:32:49 +00:00
|
|
|
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 )
|
|
|
|
{
|
Fix source comment/doc typos (follow-up)
Found via `codespell -q 3 -S *.po,./thirdparty -L aactual,acount,aline,alocation,alog,anormal,anumber,aother,apoints,aparent,aray,dout,einstance,modul,ot,overide,serie,te,,tesselate,tesselator,tht`
2021-07-03 22:37:31 +00:00
|
|
|
// Convert a JSON string, that uses a escaped sequence of 4 hexadecimal digits
|
2019-07-14 08:19:53 +00:00
|
|
|
// to encode unicode chars
|
|
|
|
// Our json11 library returns in this case a UTF8 sequence. Just convert it to
|
|
|
|
// a wxString.
|
2023-09-09 04:10:57 +00:00
|
|
|
wxString wstr = From_UTF8( name.c_str() );
|
2019-07-14 08:19:53 +00:00
|
|
|
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();
|
|
|
|
|
2021-08-16 17:38:04 +00:00
|
|
|
// 0 = Gerber file type
|
|
|
|
std::vector<int> fileTypesVec( gbrfiles.Count(), 0 );
|
|
|
|
success = LoadListOfGerberAndDrillFiles( currentPath, gbrfiles, &fileTypesVec );
|
2021-07-24 13:11:11 +00:00
|
|
|
|
|
|
|
Zoom_Automatique( false );
|
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
|
|
|
|
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
|
|
|
}
|