2012-02-16 20:03:33 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2017-01-26 17:15:46 +00:00
|
|
|
* Copyright (C) 2017 Jean-Pierre Charras, jp.charras at wanadoo.fr
|
2019-04-01 16:46:11 +00:00
|
|
|
* Copyright (C) 2004-2019 KiCad Developers, see AUTHORS.txt for contributors.
|
2012-02-16 20:03:33 +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
|
|
|
|
*/
|
|
|
|
|
2021-08-16 17:38:04 +00:00
|
|
|
#include <wx/debug.h>
|
2021-06-07 22:29:40 +00:00
|
|
|
#include <wx/filedlg.h>
|
2017-01-26 09:45:28 +00:00
|
|
|
#include <wx/wfstream.h>
|
|
|
|
#include <wx/zipstrm.h>
|
|
|
|
#include <reporter.h>
|
2021-09-14 18:26:03 +00:00
|
|
|
#include <dialogs/html_message_box.h>
|
2014-06-27 17:07:42 +00:00
|
|
|
#include <gerbview_frame.h>
|
2012-01-23 04:33:36 +00:00
|
|
|
#include <gerbview_id.h>
|
2018-01-29 12:26:58 +00:00
|
|
|
#include <gerber_file_image.h>
|
2018-02-21 02:58:53 +00:00
|
|
|
#include <gerber_file_image_list.h>
|
2018-11-26 11:49:13 +00:00
|
|
|
#include <excellon_image.h>
|
2012-02-16 20:03:33 +00:00
|
|
|
#include <wildcards_and_files_ext.h>
|
2021-08-24 16:00:46 +00:00
|
|
|
#include <view/view.h>
|
2021-08-14 20:05:21 +00:00
|
|
|
#include <widgets/wx_progress_reporters.h>
|
2020-11-16 12:13:08 +00:00
|
|
|
#include "widgets/gerbview_layer_widget.h"
|
2007-05-06 16:03:28 +00:00
|
|
|
|
2017-01-26 17:15:46 +00:00
|
|
|
// HTML Messages used more than one time:
|
2021-02-21 20:32:35 +00:00
|
|
|
#define MSG_NO_MORE_LAYER _( "<b>No more available layers</b> in GerbView to load files" )
|
2021-08-16 13:51:49 +00:00
|
|
|
#define MSG_NOT_LOADED _( "<b>Not loaded:</b> <i>%s</i>" )
|
|
|
|
#define MSG_OOM _( "<b>Memory was exhausted reading:</b> <i>%s</i>" )
|
2007-05-06 16:03:28 +00:00
|
|
|
|
2019-04-10 13:40:01 +00:00
|
|
|
|
2011-03-17 19:14:45 +00:00
|
|
|
void GERBVIEW_FRAME::OnGbrFileHistory( wxCommandEvent& event )
|
2009-01-17 20:31:19 +00:00
|
|
|
{
|
|
|
|
wxString fn;
|
|
|
|
|
2011-03-17 19:14:45 +00:00
|
|
|
fn = GetFileFromHistory( event.GetId(), _( "Gerber files" ) );
|
2009-01-17 20:31:19 +00:00
|
|
|
|
2011-03-17 19:14:45 +00:00
|
|
|
if( !fn.IsEmpty() )
|
2009-01-17 20:31:19 +00:00
|
|
|
{
|
2010-12-15 20:15:24 +00:00
|
|
|
LoadGerberFiles( fn );
|
2009-01-17 20:31:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-25 15:46:56 +00:00
|
|
|
void GERBVIEW_FRAME::OnClearGbrFileHistory( wxCommandEvent& aEvent )
|
|
|
|
{
|
|
|
|
ClearFileHistory();
|
|
|
|
}
|
|
|
|
|
2011-09-26 20:32:56 +00:00
|
|
|
|
2011-03-17 19:14:45 +00:00
|
|
|
void GERBVIEW_FRAME::OnDrlFileHistory( wxCommandEvent& event )
|
|
|
|
{
|
|
|
|
wxString fn;
|
|
|
|
|
|
|
|
fn = GetFileFromHistory( event.GetId(), _( "Drill files" ), &m_drillFileHistory );
|
|
|
|
|
|
|
|
if( !fn.IsEmpty() )
|
|
|
|
{
|
|
|
|
LoadExcellonFiles( fn );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-19 16:28:11 +00:00
|
|
|
|
2020-02-25 15:46:56 +00:00
|
|
|
void GERBVIEW_FRAME::OnClearDrlFileHistory( wxCommandEvent& aEvent )
|
|
|
|
{
|
|
|
|
ClearFileHistory( &m_drillFileHistory );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-26 09:45:28 +00:00
|
|
|
void GERBVIEW_FRAME::OnZipFileHistory( wxCommandEvent& event )
|
|
|
|
{
|
|
|
|
wxString filename;
|
|
|
|
filename = GetFileFromHistory( event.GetId(), _( "Zip files" ), &m_zipFileHistory );
|
|
|
|
|
|
|
|
if( !filename.IsEmpty() )
|
|
|
|
{
|
|
|
|
LoadZipArchiveFile( filename );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-25 15:10:01 +00:00
|
|
|
|
2020-02-25 15:46:56 +00:00
|
|
|
void GERBVIEW_FRAME::OnClearZipFileHistory( wxCommandEvent& aEvent )
|
|
|
|
{
|
|
|
|
ClearFileHistory( &m_zipFileHistory );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-19 16:28:11 +00:00
|
|
|
void GERBVIEW_FRAME::OnJobFileHistory( wxCommandEvent& event )
|
|
|
|
{
|
2019-04-10 13:40:01 +00:00
|
|
|
wxString filename = GetFileFromHistory( event.GetId(), _( "Job files" ), &m_jobFileHistory );
|
2017-08-19 16:28:11 +00:00
|
|
|
|
|
|
|
if( !filename.IsEmpty() )
|
|
|
|
LoadGerberJobFile( filename );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-25 15:46:56 +00:00
|
|
|
void GERBVIEW_FRAME::OnClearJobFileHistory( wxCommandEvent& aEvent )
|
|
|
|
{
|
|
|
|
ClearFileHistory( &m_jobFileHistory );
|
|
|
|
}
|
|
|
|
|
2007-05-06 16:03:28 +00:00
|
|
|
|
2021-08-16 18:08:05 +00:00
|
|
|
bool GERBVIEW_FRAME::LoadFileOrShowDialog( const wxString& aFileName,
|
|
|
|
const wxString& dialogFiletypes,
|
|
|
|
const wxString& dialogTitle, const int filetype )
|
2007-05-06 16:03:28 +00:00
|
|
|
{
|
2019-04-01 16:46:11 +00:00
|
|
|
static int lastGerberFileWildcard = 0;
|
2010-12-15 20:15:24 +00:00
|
|
|
wxArrayString filenamesList;
|
2021-08-16 18:08:05 +00:00
|
|
|
wxFileName filename = aFileName;
|
2010-12-15 20:15:24 +00:00
|
|
|
wxString currentPath;
|
2007-09-25 15:10:01 +00:00
|
|
|
|
2011-03-02 15:24:39 +00:00
|
|
|
if( !filename.IsOk() )
|
2007-09-25 15:10:01 +00:00
|
|
|
{
|
2016-09-07 08:28:16 +00:00
|
|
|
// Use the current working directory if the file name path does not exist.
|
2010-05-17 20:35:46 +00:00
|
|
|
if( filename.DirExists() )
|
2010-12-15 20:15:24 +00:00
|
|
|
currentPath = filename.GetPath();
|
|
|
|
else
|
2016-09-07 08:28:16 +00:00
|
|
|
{
|
2015-09-25 19:38:09 +00:00
|
|
|
currentPath = m_mruPath;
|
2009-05-28 20:46:16 +00:00
|
|
|
|
2016-09-07 08:28:16 +00:00
|
|
|
// On wxWidgets 3.1 (bug?) the path in wxFileDialog is ignored when
|
|
|
|
// finishing by the dir separator. Remove it if any:
|
|
|
|
if( currentPath.EndsWith( '\\' ) || currentPath.EndsWith( '/' ) )
|
|
|
|
currentPath.RemoveLast();
|
|
|
|
}
|
|
|
|
|
2021-08-16 18:08:05 +00:00
|
|
|
wxFileDialog dlg( this, dialogTitle, currentPath, filename.GetFullName(), dialogFiletypes,
|
2010-12-15 20:15:24 +00:00
|
|
|
wxFD_OPEN | wxFD_FILE_MUST_EXIST | wxFD_MULTIPLE | wxFD_CHANGE_DIR );
|
2019-04-01 16:46:11 +00:00
|
|
|
dlg.SetFilterIndex( lastGerberFileWildcard );
|
2008-02-23 01:26:21 +00:00
|
|
|
|
2009-04-05 20:49:15 +00:00
|
|
|
if( dlg.ShowModal() == wxID_CANCEL )
|
|
|
|
return false;
|
2008-02-23 01:26:21 +00:00
|
|
|
|
2019-04-01 16:46:11 +00:00
|
|
|
lastGerberFileWildcard = dlg.GetFilterIndex();
|
2011-03-02 15:24:39 +00:00
|
|
|
dlg.GetPaths( filenamesList );
|
2018-02-19 13:48:05 +00:00
|
|
|
m_mruPath = currentPath = dlg.GetDirectory();
|
2010-12-15 20:15:24 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-08-16 18:08:05 +00:00
|
|
|
filenamesList.Add( aFileName );
|
|
|
|
currentPath = filename.GetPath();
|
|
|
|
m_mruPath = currentPath;
|
2009-04-05 20:49:15 +00:00
|
|
|
}
|
2007-09-25 15:10:01 +00:00
|
|
|
|
2017-09-25 01:12:58 +00:00
|
|
|
// Set the busy cursor
|
|
|
|
wxBusyCursor wait;
|
|
|
|
|
2021-07-25 03:41:05 +00:00
|
|
|
bool isFirstFile = GetImagesList()->GetLoadedImageCount() == 0;
|
|
|
|
|
2021-08-16 18:08:05 +00:00
|
|
|
std::vector<int> fileTypesVec( filenamesList.Count(), filetype );
|
2021-08-16 16:25:58 +00:00
|
|
|
bool success = LoadListOfGerberAndDrillFiles( currentPath, filenamesList, &fileTypesVec );
|
2021-07-25 03:41:05 +00:00
|
|
|
|
2021-08-16 18:42:46 +00:00
|
|
|
// Auto zoom / sort is only applied when no other files have been loaded
|
2021-07-25 10:51:07 +00:00
|
|
|
if( isFirstFile )
|
2021-07-25 03:41:05 +00:00
|
|
|
{
|
2021-08-16 18:42:46 +00:00
|
|
|
SortLayersByFileExtension();
|
2021-07-25 03:41:05 +00:00
|
|
|
Zoom_Automatique( false );
|
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
2018-02-21 02:58:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-16 18:42:46 +00:00
|
|
|
bool GERBVIEW_FRAME::LoadAutodetectedFiles( const wxString& aFileName )
|
|
|
|
{
|
|
|
|
// 2 = autodetect files
|
|
|
|
return LoadFileOrShowDialog( aFileName, AllFilesWildcard(), _( "Open Autodetected File(s)" ),
|
|
|
|
2 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-16 18:08:05 +00:00
|
|
|
bool GERBVIEW_FRAME::LoadGerberFiles( const wxString& aFileName )
|
|
|
|
{
|
|
|
|
wxString filetypes;
|
|
|
|
wxFileName filename = aFileName;
|
|
|
|
|
|
|
|
/* Standard gerber filetypes
|
|
|
|
* (See http://en.wikipedia.org/wiki/Gerber_File)
|
|
|
|
* The .gbr (.pho in legacy files) extension is the default used in Pcbnew; however
|
|
|
|
* there are a lot of other extensions used for gerber files. Because the first letter
|
|
|
|
* is usually g, we accept g* as extension.
|
|
|
|
* (Mainly internal copper layers do not have specific extension, and filenames are like
|
|
|
|
* *.g1, *.g2 *.gb1 ...)
|
|
|
|
* Now (2014) Ucamco (the company which manages the Gerber format) encourages use of .gbr
|
|
|
|
* only and the Gerber X2 file format.
|
|
|
|
*/
|
|
|
|
filetypes = _( "Gerber files (.g* .lgr .pho)" );
|
|
|
|
filetypes << wxT( "|" );
|
|
|
|
filetypes += wxT( "*.g*;*.G*;*.pho;*.PHO" );
|
|
|
|
filetypes << wxT( "|" );
|
|
|
|
|
|
|
|
/* Special gerber filetypes */
|
|
|
|
filetypes += _( "Top layer" ) + AddFileExtListToFilter( { "GTL" } ) + wxT( "|" );
|
|
|
|
filetypes += _( "Bottom layer" ) + AddFileExtListToFilter( { "GBL" } ) + wxT( "|" );
|
|
|
|
filetypes += _( "Bottom solder resist" ) + AddFileExtListToFilter( { "GBS" } ) + wxT( "|" );
|
|
|
|
filetypes += _( "Top solder resist" ) + AddFileExtListToFilter( { "GTS" } ) + wxT( "|" );
|
|
|
|
filetypes += _( "Bottom overlay" ) + AddFileExtListToFilter( { "GBO" } ) + wxT( "|" );
|
|
|
|
filetypes += _( "Top overlay" ) + AddFileExtListToFilter( { "GTO" } ) + wxT( "|" );
|
|
|
|
filetypes += _( "Bottom paste" ) + AddFileExtListToFilter( { "GBP" } ) + wxT( "|" );
|
|
|
|
filetypes += _( "Top paste" ) + AddFileExtListToFilter( { "GTP" } ) + wxT( "|" );
|
|
|
|
filetypes += _( "Keep-out layer" ) + AddFileExtListToFilter( { "GKO" } ) + wxT( "|" );
|
|
|
|
filetypes += _( "Mechanical layers" )
|
|
|
|
+ AddFileExtListToFilter(
|
|
|
|
{ "GM1", "GM2", "GM3", "GM4", "GM5", "GM6", "GM7", "GM8", "GM9" } )
|
|
|
|
+ wxT( "|" );
|
|
|
|
filetypes += _( "Top Pad Master" ) + AddFileExtListToFilter( { "GPT" } ) + wxT( "|" );
|
|
|
|
filetypes += _( "Bottom Pad Master" ) + AddFileExtListToFilter( { "GPB" } ) + wxT( "|" );
|
|
|
|
|
|
|
|
// All filetypes
|
|
|
|
filetypes += AllFilesWildcard();
|
|
|
|
|
|
|
|
// 0 = gerber files
|
|
|
|
return LoadFileOrShowDialog( aFileName, filetypes, _( "Open Gerber File(s)" ), 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool GERBVIEW_FRAME::LoadExcellonFiles( const wxString& aFileName )
|
|
|
|
{
|
|
|
|
wxString filetypes = DrillFileWildcard();
|
|
|
|
filetypes << wxT( "|" );
|
|
|
|
filetypes += AllFilesWildcard();
|
|
|
|
|
|
|
|
// 1 = drill files
|
|
|
|
return LoadFileOrShowDialog( aFileName, filetypes, _( "Open NC (Excellon) Drill File(s)" ), 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-16 17:38:04 +00:00
|
|
|
bool GERBVIEW_FRAME::LoadListOfGerberAndDrillFiles( const wxString& aPath,
|
|
|
|
const wxArrayString& aFilenameList,
|
|
|
|
std::vector<int>* aFileType )
|
2018-02-21 02:58:53 +00:00
|
|
|
{
|
2021-08-16 17:38:04 +00:00
|
|
|
wxCHECK_MSG( aFilenameList.Count() == aFileType->size(), false,
|
|
|
|
"Mismatch in file names and file types count" );
|
|
|
|
|
2018-02-21 02:58:53 +00:00
|
|
|
wxFileName filename;
|
|
|
|
|
2011-09-30 18:15:37 +00:00
|
|
|
// Read gerber files: each file is loaded on a new GerbView layer
|
2017-01-26 12:16:49 +00:00
|
|
|
bool success = true;
|
2017-09-17 22:43:20 +00:00
|
|
|
int layer = GetActiveLayer();
|
2021-08-04 18:22:47 +00:00
|
|
|
int firstLoadedLayer = NO_AVAILABLE_LAYERS;
|
2019-04-10 13:40:01 +00:00
|
|
|
LSET visibility = GetVisibleLayers();
|
2011-01-20 16:34:57 +00:00
|
|
|
|
2017-01-26 12:16:49 +00:00
|
|
|
// Manage errors when loading files
|
|
|
|
wxString msg;
|
|
|
|
WX_STRING_REPORTER reporter( &msg );
|
|
|
|
|
2019-08-25 18:32:40 +00:00
|
|
|
// Create progress dialog (only used if more than 1 file to load
|
2018-02-21 02:58:53 +00:00
|
|
|
std::unique_ptr<WX_PROGRESS_REPORTER> progress = nullptr;
|
|
|
|
|
|
|
|
for( unsigned ii = 0; ii < aFilenameList.GetCount(); ii++ )
|
2010-12-15 20:15:24 +00:00
|
|
|
{
|
2019-03-24 16:32:57 +00:00
|
|
|
filename = aFilenameList[ii];
|
|
|
|
|
|
|
|
if( !filename.IsAbsolute() )
|
|
|
|
filename.SetPath( aPath );
|
|
|
|
|
|
|
|
// Check for non existing files, to avoid creating broken or useless data
|
|
|
|
// and report all in one error list:
|
|
|
|
if( !filename.FileExists() )
|
|
|
|
{
|
|
|
|
wxString warning;
|
|
|
|
warning << "<b>" << _( "File not found:" ) << "</b><br>"
|
|
|
|
<< filename.GetFullPath() << "<br>";
|
2020-03-04 09:48:18 +00:00
|
|
|
reporter.Report( warning, RPT_SEVERITY_WARNING );
|
2019-03-24 16:32:57 +00:00
|
|
|
success = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-08-04 18:22:47 +00:00
|
|
|
if( filename.GetExt() == GerberJobFileExtension.c_str() )
|
|
|
|
{
|
|
|
|
//We cannot read a gerber job file as a gerber plot file: skip it
|
|
|
|
wxString txt;
|
|
|
|
txt.Printf( _( "<b>A gerber job file cannot be loaded as a plot file</b> "
|
|
|
|
"<i>%s</i>" ),
|
|
|
|
filename.GetFullName() );
|
|
|
|
success = false;
|
|
|
|
reporter.Report( txt, RPT_SEVERITY_ERROR );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-25 18:32:40 +00:00
|
|
|
m_lastFileName = filename.GetFullPath();
|
|
|
|
|
|
|
|
if( !progress && ( aFilenameList.GetCount() > 1 ) )
|
2018-02-21 02:58:53 +00:00
|
|
|
{
|
2021-08-16 16:25:58 +00:00
|
|
|
progress = std::make_unique<WX_PROGRESS_REPORTER>( this, _( "Loading files..." ), 1,
|
2021-06-26 11:33:37 +00:00
|
|
|
false );
|
2018-02-21 02:58:53 +00:00
|
|
|
progress->SetMaxProgress( aFilenameList.GetCount() - 1 );
|
2021-06-26 11:33:37 +00:00
|
|
|
progress->Report( wxString::Format( _("Loading %u/%zu %s..." ),
|
|
|
|
ii+1,
|
|
|
|
aFilenameList.GetCount(),
|
|
|
|
m_lastFileName ) );
|
2018-02-21 02:58:53 +00:00
|
|
|
}
|
|
|
|
else if( progress )
|
|
|
|
{
|
2021-06-26 11:33:37 +00:00
|
|
|
progress->Report( wxString::Format( _("Loading %u/%zu %s..." ),
|
|
|
|
ii+1,
|
|
|
|
aFilenameList.GetCount(),
|
|
|
|
m_lastFileName ) );
|
2018-02-21 02:58:53 +00:00
|
|
|
progress->KeepRefreshing();
|
|
|
|
}
|
|
|
|
|
2011-01-20 16:34:57 +00:00
|
|
|
|
2021-08-04 18:22:47 +00:00
|
|
|
// Make sure we have a layer available to load into
|
2021-08-16 13:52:25 +00:00
|
|
|
layer = getNextAvailableLayer();
|
2021-08-04 18:22:47 +00:00
|
|
|
|
2021-08-15 18:48:13 +00:00
|
|
|
if( layer == NO_AVAILABLE_LAYERS )
|
2021-08-04 18:22:47 +00:00
|
|
|
{
|
|
|
|
success = false;
|
|
|
|
reporter.Report( MSG_NO_MORE_LAYER, RPT_SEVERITY_ERROR );
|
|
|
|
|
|
|
|
// Report the name of not loaded files:
|
|
|
|
while( ii < aFilenameList.GetCount() )
|
|
|
|
{
|
|
|
|
filename = aFilenameList[ii++];
|
|
|
|
wxString txt = wxString::Format( MSG_NOT_LOADED, filename.GetFullName() );
|
|
|
|
reporter.Report( txt, RPT_SEVERITY_ERROR );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetActiveLayer( layer, false );
|
2019-04-10 13:40:01 +00:00
|
|
|
visibility[ layer ] = true;
|
2018-02-28 02:33:41 +00:00
|
|
|
|
2021-05-01 16:46:18 +00:00
|
|
|
try
|
2010-12-15 20:15:24 +00:00
|
|
|
{
|
2021-08-16 17:38:04 +00:00
|
|
|
// 2 = Autodetect
|
|
|
|
if( ( *aFileType )[ii] == 2 )
|
2020-07-10 11:59:35 +00:00
|
|
|
{
|
2021-08-16 17:38:04 +00:00
|
|
|
if( EXCELLON_IMAGE::TestFileIsExcellon( filename.GetFullPath() ) )
|
|
|
|
( *aFileType )[ii] = 1;
|
|
|
|
else if( GERBER_FILE_IMAGE::TestFileIsRS274( filename.GetFullPath() ) )
|
|
|
|
( *aFileType )[ii] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch( ( *aFileType )[ii] )
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
|
|
|
|
if( Read_GERBER_File( filename.GetFullPath() ) )
|
2021-08-04 18:22:47 +00:00
|
|
|
{
|
2021-08-16 17:38:04 +00:00
|
|
|
UpdateFileHistory( filename.GetFullPath() );
|
2021-08-04 18:22:47 +00:00
|
|
|
|
|
|
|
if( firstLoadedLayer == NO_AVAILABLE_LAYERS )
|
|
|
|
{
|
|
|
|
firstLoadedLayer = layer;
|
|
|
|
}
|
|
|
|
}
|
2021-08-16 17:38:04 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
|
|
|
|
if( Read_EXCELLON_File( filename.GetFullPath() ) )
|
2017-01-26 12:16:49 +00:00
|
|
|
{
|
2021-08-16 17:38:04 +00:00
|
|
|
UpdateFileHistory( filename.GetFullPath(), &m_drillFileHistory );
|
2021-05-01 16:46:18 +00:00
|
|
|
|
2021-08-16 17:38:04 +00:00
|
|
|
// Select the first added layer by default when done loading
|
2021-08-04 18:22:47 +00:00
|
|
|
if( firstLoadedLayer == NO_AVAILABLE_LAYERS )
|
2018-11-26 11:49:13 +00:00
|
|
|
{
|
2021-08-04 18:22:47 +00:00
|
|
|
firstLoadedLayer = layer;
|
2018-11-26 11:49:13 +00:00
|
|
|
}
|
2021-05-01 16:46:18 +00:00
|
|
|
}
|
2021-08-16 17:38:04 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
wxString txt = wxString::Format( MSG_NOT_LOADED, filename.GetFullName() );
|
|
|
|
reporter.Report( txt, RPT_SEVERITY_ERROR );
|
2018-11-26 11:49:13 +00:00
|
|
|
}
|
2010-12-15 20:15:24 +00:00
|
|
|
}
|
2021-05-01 16:46:18 +00:00
|
|
|
catch( const std::bad_alloc& )
|
|
|
|
{
|
|
|
|
wxString txt = wxString::Format( MSG_OOM, filename.GetFullName() );
|
|
|
|
reporter.Report( txt, RPT_SEVERITY_ERROR );
|
|
|
|
success = false;
|
|
|
|
continue;
|
|
|
|
}
|
2018-02-21 02:58:53 +00:00
|
|
|
|
|
|
|
if( progress )
|
|
|
|
progress->AdvanceProgress();
|
2010-12-15 20:15:24 +00:00
|
|
|
}
|
2007-09-25 15:10:01 +00:00
|
|
|
|
2017-01-26 12:16:49 +00:00
|
|
|
if( !success )
|
|
|
|
{
|
2018-02-21 02:58:53 +00:00
|
|
|
wxSafeYield(); // Allows slice of time to redraw the screen
|
|
|
|
// to refresh widgets, before displaying messages
|
2017-01-26 12:16:49 +00:00
|
|
|
HTML_MESSAGE_BOX mbox( this, _( "Errors" ) );
|
|
|
|
mbox.ListSet( msg );
|
|
|
|
mbox.ShowModal();
|
|
|
|
}
|
|
|
|
|
2018-02-28 02:33:41 +00:00
|
|
|
SetVisibleLayers( visibility );
|
|
|
|
|
2021-08-04 18:22:47 +00:00
|
|
|
if( firstLoadedLayer != NO_AVAILABLE_LAYERS )
|
|
|
|
SetActiveLayer( firstLoadedLayer, true );
|
|
|
|
|
2010-12-17 20:34:29 +00:00
|
|
|
// Synchronize layers tools with actual active layer:
|
2014-11-22 11:52:57 +00:00
|
|
|
ReFillLayerWidget();
|
2018-03-15 01:51:58 +00:00
|
|
|
|
2011-04-05 17:49:14 +00:00
|
|
|
m_LayersManager->UpdateLayerIcons();
|
2018-02-21 02:58:53 +00:00
|
|
|
syncLayerBox( true );
|
|
|
|
|
2019-06-13 17:28:55 +00:00
|
|
|
GetCanvas()->Refresh();
|
2018-03-04 04:43:16 +00:00
|
|
|
|
2017-01-26 12:16:49 +00:00
|
|
|
return success;
|
2011-03-16 20:51:20 +00:00
|
|
|
}
|
|
|
|
|
2015-09-25 19:38:09 +00:00
|
|
|
|
2017-01-26 09:45:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
bool GERBVIEW_FRAME::unarchiveFiles( const wxString& aFullFileName, REPORTER* aReporter )
|
|
|
|
{
|
2021-08-03 17:52:33 +00:00
|
|
|
bool foundX2Gerbers = false;
|
2017-03-31 08:55:13 +00:00
|
|
|
wxString msg;
|
2021-08-04 18:47:50 +00:00
|
|
|
int firstLoadedLayer = NO_AVAILABLE_LAYERS;
|
2017-01-26 09:45:28 +00:00
|
|
|
|
2017-03-31 08:55:13 +00:00
|
|
|
// Extract the path of aFullFileName. We use it to store temporary files
|
2017-01-26 09:45:28 +00:00
|
|
|
wxFileName fn( aFullFileName );
|
|
|
|
wxString unzipDir = fn.GetPath();
|
|
|
|
|
2017-03-31 08:55:13 +00:00
|
|
|
wxFFileInputStream zipFile( aFullFileName );
|
2017-01-26 09:45:28 +00:00
|
|
|
|
2017-03-31 08:55:13 +00:00
|
|
|
if( !zipFile.IsOk() )
|
2017-01-26 09:45:28 +00:00
|
|
|
{
|
2017-03-31 08:55:13 +00:00
|
|
|
if( aReporter )
|
2017-01-26 09:45:28 +00:00
|
|
|
{
|
2021-06-16 22:35:00 +00:00
|
|
|
msg.Printf( _( "Zip file '%s' cannot be opened." ), aFullFileName );
|
2020-03-04 09:48:18 +00:00
|
|
|
aReporter->Report( msg, RPT_SEVERITY_ERROR );
|
2017-01-26 09:45:28 +00:00
|
|
|
}
|
|
|
|
|
2017-03-31 08:55:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-01-26 09:45:28 +00:00
|
|
|
|
2017-03-31 08:55:13 +00:00
|
|
|
// Update the list of recent zip files.
|
|
|
|
UpdateFileHistory( aFullFileName, &m_zipFileHistory );
|
2017-01-26 09:45:28 +00:00
|
|
|
|
2017-03-31 08:55:13 +00:00
|
|
|
// The unzipped file in only a temporary file. Give it a filename
|
|
|
|
// which cannot conflict with an usual filename.
|
|
|
|
// TODO: make Read_GERBER_File() and Read_EXCELLON_File() able to
|
|
|
|
// accept a stream, and avoid using a temp file.
|
|
|
|
wxFileName temp_fn( "$tempfile.tmp" );
|
|
|
|
temp_fn.MakeAbsolute( unzipDir );
|
|
|
|
wxString unzipped_tempfile = temp_fn.GetFullPath();
|
2017-01-26 09:45:28 +00:00
|
|
|
|
|
|
|
|
2017-03-31 08:55:13 +00:00
|
|
|
bool success = true;
|
|
|
|
wxZipInputStream zipArchive( zipFile );
|
|
|
|
wxZipEntry* entry;
|
|
|
|
bool reported_no_more_layer = false;
|
|
|
|
|
|
|
|
while( ( entry = zipArchive.GetNextEntry() ) )
|
|
|
|
{
|
|
|
|
wxString fname = entry->GetName();
|
|
|
|
wxFileName uzfn = fname;
|
|
|
|
wxString curr_ext = uzfn.GetExt().Lower();
|
2017-01-26 09:45:28 +00:00
|
|
|
|
2017-01-26 17:15:46 +00:00
|
|
|
// The archive contains Gerber and/or Excellon drill files. Use the right loader.
|
|
|
|
// However it can contain a few other files (reports, pdf files...),
|
|
|
|
// which will be skipped.
|
2021-08-02 20:53:15 +00:00
|
|
|
if( curr_ext == GerberJobFileExtension.c_str() )
|
2017-01-26 09:45:28 +00:00
|
|
|
{
|
2021-08-02 20:53:15 +00:00
|
|
|
//We cannot read a gerber job file as a gerber plot file: skip it
|
2017-01-26 09:45:28 +00:00
|
|
|
if( aReporter )
|
|
|
|
{
|
2021-08-16 13:51:49 +00:00
|
|
|
msg.Printf( _( "Skipped file '%s' (gerber job file)." ), entry->GetName() );
|
2020-03-04 09:48:18 +00:00
|
|
|
aReporter->Report( msg, RPT_SEVERITY_WARNING );
|
2017-01-26 09:45:28 +00:00
|
|
|
}
|
2017-03-31 08:55:13 +00:00
|
|
|
|
|
|
|
continue;
|
2017-01-26 09:45:28 +00:00
|
|
|
}
|
|
|
|
|
2021-08-02 20:53:15 +00:00
|
|
|
wxString matchedExt;
|
|
|
|
enum GERBER_ORDER_ENUM order;
|
|
|
|
GERBER_FILE_IMAGE_LIST::GetGerberLayerFromFilename( fname, order, matchedExt );
|
|
|
|
|
2017-09-17 22:43:20 +00:00
|
|
|
int layer = GetActiveLayer();
|
2017-01-26 09:45:28 +00:00
|
|
|
|
2017-03-31 08:55:13 +00:00
|
|
|
if( layer == NO_AVAILABLE_LAYERS )
|
2017-01-26 17:15:46 +00:00
|
|
|
{
|
|
|
|
success = false;
|
|
|
|
|
|
|
|
if( aReporter )
|
|
|
|
{
|
2017-03-31 08:55:13 +00:00
|
|
|
if( !reported_no_more_layer )
|
2020-03-04 09:48:18 +00:00
|
|
|
aReporter->Report( MSG_NO_MORE_LAYER, RPT_SEVERITY_ERROR );
|
2017-03-31 08:55:13 +00:00
|
|
|
|
|
|
|
reported_no_more_layer = true;
|
|
|
|
|
|
|
|
// Report the name of not loaded files:
|
2020-10-16 00:39:55 +00:00
|
|
|
msg.Printf( MSG_NOT_LOADED, entry->GetName() );
|
2020-03-04 09:48:18 +00:00
|
|
|
aReporter->Report( msg, RPT_SEVERITY_ERROR );
|
2017-01-26 17:15:46 +00:00
|
|
|
}
|
|
|
|
|
2017-03-31 08:55:13 +00:00
|
|
|
delete entry;
|
|
|
|
continue;
|
|
|
|
}
|
2017-01-26 09:45:28 +00:00
|
|
|
|
2017-03-31 08:55:13 +00:00
|
|
|
// Create the unzipped temporary file:
|
2017-01-26 09:45:28 +00:00
|
|
|
{
|
2017-03-31 08:55:13 +00:00
|
|
|
wxFFileOutputStream temporary_ofile( unzipped_tempfile );
|
|
|
|
|
|
|
|
if( temporary_ofile.Ok() )
|
|
|
|
temporary_ofile.Write( zipArchive );
|
|
|
|
else
|
2017-01-26 09:45:28 +00:00
|
|
|
{
|
2017-03-31 08:55:13 +00:00
|
|
|
success = false;
|
2017-01-26 10:36:01 +00:00
|
|
|
|
2017-03-31 08:55:13 +00:00
|
|
|
if( aReporter )
|
2017-01-26 10:36:01 +00:00
|
|
|
{
|
2021-08-16 13:51:49 +00:00
|
|
|
msg.Printf( _( "<b>Unable to create temporary file '%s'.</b>" ),
|
2020-02-28 00:05:40 +00:00
|
|
|
unzipped_tempfile );
|
2020-03-04 09:48:18 +00:00
|
|
|
aReporter->Report( msg, RPT_SEVERITY_ERROR );
|
2017-01-26 10:36:01 +00:00
|
|
|
}
|
2017-01-26 09:45:28 +00:00
|
|
|
}
|
2017-03-31 08:55:13 +00:00
|
|
|
}
|
2017-01-26 09:45:28 +00:00
|
|
|
|
2017-03-31 08:55:13 +00:00
|
|
|
bool read_ok = true;
|
|
|
|
|
2021-08-03 15:09:09 +00:00
|
|
|
// Try to parse files if we can't tell from file extension
|
|
|
|
if( order == GERBER_ORDER_ENUM::GERBER_LAYER_UNKNOWN )
|
|
|
|
{
|
|
|
|
if( EXCELLON_IMAGE::TestFileIsExcellon( unzipped_tempfile ) )
|
|
|
|
{
|
|
|
|
order = GERBER_ORDER_ENUM::GERBER_DRILL;
|
|
|
|
}
|
|
|
|
else if( GERBER_FILE_IMAGE::TestFileIsRS274( unzipped_tempfile ) )
|
|
|
|
{
|
|
|
|
// If we have no way to know what layer it is, just guess
|
|
|
|
order = GERBER_ORDER_ENUM::GERBER_TOP_COPPER;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( aReporter )
|
|
|
|
{
|
2021-08-16 13:51:49 +00:00
|
|
|
msg.Printf( _( "Skipped file '%s' (unknown type)." ), entry->GetName() );
|
2021-08-03 15:09:09 +00:00
|
|
|
aReporter->Report( msg, RPT_SEVERITY_WARNING );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( order == GERBER_ORDER_ENUM::GERBER_DRILL )
|
|
|
|
{
|
|
|
|
read_ok = Read_EXCELLON_File( unzipped_tempfile );
|
|
|
|
}
|
|
|
|
else if( order != GERBER_ORDER_ENUM::GERBER_LAYER_UNKNOWN )
|
2017-03-31 08:55:13 +00:00
|
|
|
{
|
|
|
|
// Read gerber files: each file is loaded on a new GerbView layer
|
|
|
|
read_ok = Read_GERBER_File( unzipped_tempfile );
|
2021-08-24 16:00:46 +00:00
|
|
|
|
|
|
|
if( read_ok )
|
|
|
|
GetCanvas()->GetView()->SetLayerHasNegatives(
|
|
|
|
GERBER_DRAW_LAYER( layer ), GetGbrImage( layer )->HasNegativeItems() );
|
2017-03-31 08:55:13 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 18:47:50 +00:00
|
|
|
// Select the first added layer by default when done loading
|
|
|
|
if( read_ok && firstLoadedLayer == NO_AVAILABLE_LAYERS )
|
|
|
|
{
|
|
|
|
firstLoadedLayer = layer;
|
|
|
|
}
|
|
|
|
|
2017-03-31 08:55:13 +00:00
|
|
|
delete entry;
|
|
|
|
|
|
|
|
// The unzipped file is only a temporary file, delete it.
|
|
|
|
wxRemoveFile( unzipped_tempfile );
|
|
|
|
|
|
|
|
if( !read_ok )
|
|
|
|
{
|
|
|
|
success = false;
|
|
|
|
|
|
|
|
if( aReporter )
|
|
|
|
{
|
2021-08-16 13:51:49 +00:00
|
|
|
msg.Printf( _( "<b>unzipped file %s read error</b>" ), unzipped_tempfile );
|
2020-03-04 09:48:18 +00:00
|
|
|
aReporter->Report( msg, RPT_SEVERITY_ERROR );
|
2017-03-31 08:55:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GERBER_FILE_IMAGE* gerber_image = GetGbrImage( layer );
|
2017-05-29 18:47:21 +00:00
|
|
|
|
|
|
|
if( gerber_image )
|
2021-08-03 17:52:33 +00:00
|
|
|
{
|
2017-05-29 18:47:21 +00:00
|
|
|
gerber_image->m_FileName = fname;
|
2021-08-03 17:52:33 +00:00
|
|
|
if( gerber_image->m_IsX2_file )
|
|
|
|
foundX2Gerbers = true;
|
|
|
|
}
|
2017-03-31 08:55:13 +00:00
|
|
|
|
2021-08-16 13:52:25 +00:00
|
|
|
layer = getNextAvailableLayer();
|
2017-09-17 22:43:20 +00:00
|
|
|
SetActiveLayer( layer, false );
|
2017-01-26 17:15:46 +00:00
|
|
|
}
|
2017-01-26 09:45:28 +00:00
|
|
|
}
|
|
|
|
|
2021-08-03 17:52:33 +00:00
|
|
|
if( foundX2Gerbers )
|
|
|
|
SortLayersByX2Attributes();
|
|
|
|
else
|
|
|
|
SortLayersByFileExtension();
|
|
|
|
|
2021-08-04 18:47:50 +00:00
|
|
|
// Select the first layer loaded so we don't show another layer on top after
|
|
|
|
if( firstLoadedLayer != NO_AVAILABLE_LAYERS )
|
|
|
|
SetActiveLayer( firstLoadedLayer, true );
|
|
|
|
|
2017-01-26 09:45:28 +00:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool GERBVIEW_FRAME::LoadZipArchiveFile( const wxString& aFullFileName )
|
|
|
|
{
|
|
|
|
#define ZipFileExtension "zip"
|
2017-11-12 00:31:38 +00:00
|
|
|
|
2017-01-26 09:45:28 +00:00
|
|
|
wxFileName filename = aFullFileName;
|
|
|
|
wxString currentPath;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2020-05-03 22:02:07 +00:00
|
|
|
wxFileDialog dlg( this, _( "Open Zip File" ), currentPath, filename.GetFullName(),
|
|
|
|
ZipFileWildcard(), wxFD_OPEN | wxFD_FILE_MUST_EXIST | wxFD_CHANGE_DIR );
|
2017-01-26 09:45:28 +00:00
|
|
|
|
|
|
|
if( dlg.ShowModal() == wxID_CANCEL )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
filename = dlg.GetPath();
|
|
|
|
currentPath = wxGetCwd();
|
|
|
|
m_mruPath = currentPath;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
currentPath = filename.GetPath();
|
|
|
|
m_mruPath = currentPath;
|
|
|
|
}
|
|
|
|
|
2017-01-26 17:15:46 +00:00
|
|
|
wxString msg;
|
|
|
|
WX_STRING_REPORTER reporter( &msg );
|
2017-01-26 09:45:28 +00:00
|
|
|
|
2017-01-26 17:15:46 +00:00
|
|
|
if( filename.IsOk() )
|
|
|
|
unarchiveFiles( filename.GetFullPath(), &reporter );
|
2017-01-26 09:45:28 +00:00
|
|
|
|
|
|
|
Zoom_Automatique( false );
|
|
|
|
|
|
|
|
// Synchronize layers tools with actual active layer:
|
|
|
|
ReFillLayerWidget();
|
2017-09-17 22:43:20 +00:00
|
|
|
SetActiveLayer( GetActiveLayer() );
|
2017-01-26 09:45:28 +00:00
|
|
|
m_LayersManager->UpdateLayerIcons();
|
|
|
|
syncLayerBox();
|
|
|
|
|
2017-01-26 17:15:46 +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();
|
|
|
|
}
|
|
|
|
|
2017-01-26 09:45:28 +00:00
|
|
|
return true;
|
|
|
|
}
|