2012-02-16 21:03:33 +01:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2017-01-26 18:15:46 +01:00
|
|
|
* Copyright (C) 2017 Jean-Pierre Charras, jp.charras at wanadoo.fr
|
2019-04-01 12:46:11 -04:00
|
|
|
* Copyright (C) 2004-2019 KiCad Developers, see AUTHORS.txt for contributors.
|
2012-02-16 21:03:33 +01: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-06-07 18:29:40 -04:00
|
|
|
#include <wx/filedlg.h>
|
2017-01-26 10:45:28 +01:00
|
|
|
#include <wx/wfstream.h>
|
|
|
|
#include <wx/zipstrm.h>
|
|
|
|
#include <reporter.h>
|
2021-09-14 19:26:03 +01:00
|
|
|
#include <dialogs/html_message_box.h>
|
2014-06-27 19:07:42 +02:00
|
|
|
#include <gerbview_frame.h>
|
2012-01-22 22:33:36 -06:00
|
|
|
#include <gerbview_id.h>
|
2018-01-29 13:26:58 +01:00
|
|
|
#include <gerber_file_image.h>
|
2018-02-20 21:58:53 -05:00
|
|
|
#include <gerber_file_image_list.h>
|
2018-11-26 12:49:13 +01:00
|
|
|
#include <excellon_image.h>
|
2012-02-16 21:03:33 +01:00
|
|
|
#include <wildcards_and_files_ext.h>
|
2021-08-24 12:00:46 -04:00
|
|
|
#include <view/view.h>
|
2021-08-14 21:05:21 +01:00
|
|
|
#include <widgets/wx_progress_reporters.h>
|
2020-11-16 07:13:08 -05:00
|
|
|
#include "widgets/gerbview_layer_widget.h"
|
2007-05-06 16:03:28 +00:00
|
|
|
|
2017-01-26 18:15:46 +01: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 09:51:49 -04: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 14:40:01 +01:00
|
|
|
|
2011-03-17 20:14:45 +01:00
|
|
|
void GERBVIEW_FRAME::OnGbrFileHistory( wxCommandEvent& event )
|
2009-01-17 20:31:19 +00:00
|
|
|
{
|
|
|
|
wxString fn;
|
|
|
|
|
2011-03-17 20:14:45 +01:00
|
|
|
fn = GetFileFromHistory( event.GetId(), _( "Gerber files" ) );
|
2009-01-17 20:31:19 +00:00
|
|
|
|
2011-03-17 20:14:45 +01:00
|
|
|
if( !fn.IsEmpty() )
|
2009-01-17 20:31:19 +00:00
|
|
|
{
|
2010-12-15 21:15:24 +01: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 16:32:56 -04:00
|
|
|
|
2011-03-17 20:14:45 +01: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 18:28:11 +02:00
|
|
|
|
2020-02-25 15:46:56 +00:00
|
|
|
void GERBVIEW_FRAME::OnClearDrlFileHistory( wxCommandEvent& aEvent )
|
|
|
|
{
|
|
|
|
ClearFileHistory( &m_drillFileHistory );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-26 10:45:28 +01: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 18:28:11 +02:00
|
|
|
void GERBVIEW_FRAME::OnJobFileHistory( wxCommandEvent& event )
|
|
|
|
{
|
2019-04-10 14:40:01 +01:00
|
|
|
wxString filename = GetFileFromHistory( event.GetId(), _( "Job files" ), &m_jobFileHistory );
|
2017-08-19 18:28:11 +02: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
|
|
|
|
2011-03-12 10:50:21 +01:00
|
|
|
bool GERBVIEW_FRAME::LoadGerberFiles( const wxString& aFullFileName )
|
2007-05-06 16:03:28 +00:00
|
|
|
{
|
2019-04-01 12:46:11 -04:00
|
|
|
static int lastGerberFileWildcard = 0;
|
2009-11-09 14:00:22 +00:00
|
|
|
wxString filetypes;
|
2010-12-15 21:15:24 +01:00
|
|
|
wxArrayString filenamesList;
|
2010-05-17 16:35:46 -04:00
|
|
|
wxFileName filename = aFullFileName;
|
2010-12-15 21:15:24 +01:00
|
|
|
wxString currentPath;
|
2007-09-25 15:10:01 +00:00
|
|
|
|
2011-03-02 10:24:39 -05:00
|
|
|
if( !filename.IsOk() )
|
2007-09-25 15:10:01 +00:00
|
|
|
{
|
2009-12-30 18:06:12 +00:00
|
|
|
/* Standard gerber filetypes
|
|
|
|
* (See http://en.wikipedia.org/wiki/Gerber_File)
|
2020-05-03 23:02:07 +01:00
|
|
|
* 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.
|
2009-12-30 18:06:12 +00:00
|
|
|
*/
|
2010-10-17 18:42:06 +02:00
|
|
|
filetypes = _( "Gerber files (.g* .lgr .pho)" );
|
2009-12-30 18:06:12 +00:00
|
|
|
filetypes << wxT("|");
|
2014-06-30 12:00:21 +02:00
|
|
|
filetypes += wxT("*.g*;*.G*;*.pho;*.PHO" );
|
2009-12-30 18:06:12 +00:00
|
|
|
filetypes << wxT("|");
|
2009-11-09 14:00:22 +00:00
|
|
|
|
|
|
|
/* Special gerber filetypes */
|
2021-09-09 10:18:08 -07:00
|
|
|
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( "|" );
|
2009-11-09 14:00:22 +00:00
|
|
|
|
2016-09-07 10:28:16 +02:00
|
|
|
// All filetypes
|
2019-01-08 11:57:39 +00:00
|
|
|
filetypes += AllFilesWildcard();
|
2009-11-09 14:00:22 +00:00
|
|
|
|
2016-09-07 10:28:16 +02:00
|
|
|
// Use the current working directory if the file name path does not exist.
|
2010-05-17 16:35:46 -04:00
|
|
|
if( filename.DirExists() )
|
2010-12-15 21:15:24 +01:00
|
|
|
currentPath = filename.GetPath();
|
|
|
|
else
|
2016-09-07 10:28:16 +02:00
|
|
|
{
|
2015-09-25 15:38:09 -04:00
|
|
|
currentPath = m_mruPath;
|
2009-05-28 20:46:16 +00:00
|
|
|
|
2016-09-07 10:28:16 +02: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();
|
|
|
|
}
|
|
|
|
|
2020-05-03 23:02:07 +01:00
|
|
|
wxFileDialog dlg( this, _( "Open Gerber File(s)" ), currentPath, filename.GetFullName(),
|
2009-05-28 20:46:16 +00:00
|
|
|
filetypes,
|
2010-12-15 21:15:24 +01:00
|
|
|
wxFD_OPEN | wxFD_FILE_MUST_EXIST | wxFD_MULTIPLE | wxFD_CHANGE_DIR );
|
2019-04-01 12:46:11 -04: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 12:46:11 -04:00
|
|
|
lastGerberFileWildcard = dlg.GetFilterIndex();
|
2011-03-02 10:24:39 -05:00
|
|
|
dlg.GetPaths( filenamesList );
|
2018-02-19 13:48:05 +00:00
|
|
|
m_mruPath = currentPath = dlg.GetDirectory();
|
2010-12-15 21:15:24 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
filenamesList.Add( aFullFileName );
|
2018-02-19 13:48:05 +00:00
|
|
|
m_mruPath = currentPath = filename.GetPath();
|
2009-04-05 20:49:15 +00:00
|
|
|
}
|
2007-09-25 15:10:01 +00:00
|
|
|
|
2018-02-27 20:59:47 -05:00
|
|
|
|
2017-09-24 21:12:58 -04:00
|
|
|
// Set the busy cursor
|
|
|
|
wxBusyCursor wait;
|
|
|
|
|
2021-07-25 13:41:05 +10:00
|
|
|
bool isFirstFile = GetImagesList()->GetLoadedImageCount() == 0;
|
|
|
|
|
|
|
|
bool success = LoadListOfGerberAndDrillFiles( currentPath, filenamesList );
|
|
|
|
|
|
|
|
// Auto zoom is only applied if there is only one file loaded
|
2021-07-25 20:51:07 +10:00
|
|
|
if( isFirstFile )
|
2021-07-25 13:41:05 +10:00
|
|
|
{
|
|
|
|
Zoom_Automatique( false );
|
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
2018-02-20 21:58:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-09 00:51:05 +01:00
|
|
|
bool GERBVIEW_FRAME::LoadListOfGerberAndDrillFiles( const wxString& aPath,
|
2018-11-26 12:49:13 +01:00
|
|
|
const wxArrayString& aFilenameList,
|
|
|
|
const std::vector<int>* aFileType )
|
2018-02-20 21:58:53 -05:00
|
|
|
{
|
|
|
|
wxFileName filename;
|
|
|
|
|
2011-09-30 14:15:37 -04:00
|
|
|
// Read gerber files: each file is loaded on a new GerbView layer
|
2017-01-26 13:16:49 +01:00
|
|
|
bool success = true;
|
2017-09-17 18:43:20 -04:00
|
|
|
int layer = GetActiveLayer();
|
2021-08-04 14:22:47 -04:00
|
|
|
int firstLoadedLayer = NO_AVAILABLE_LAYERS;
|
2019-04-10 14:40:01 +01:00
|
|
|
LSET visibility = GetVisibleLayers();
|
2011-01-20 11:34:57 -05:00
|
|
|
|
2017-01-26 13:16:49 +01:00
|
|
|
// Manage errors when loading files
|
|
|
|
wxString msg;
|
|
|
|
WX_STRING_REPORTER reporter( &msg );
|
|
|
|
|
2019-08-25 20:32:40 +02:00
|
|
|
// Create progress dialog (only used if more than 1 file to load
|
2018-02-20 21:58:53 -05:00
|
|
|
std::unique_ptr<WX_PROGRESS_REPORTER> progress = nullptr;
|
|
|
|
|
|
|
|
for( unsigned ii = 0; ii < aFilenameList.GetCount(); ii++ )
|
2010-12-15 21:15:24 +01:00
|
|
|
{
|
2019-03-24 17:32:57 +01: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 10:48:18 +01:00
|
|
|
reporter.Report( warning, RPT_SEVERITY_WARNING );
|
2019-03-24 17:32:57 +01:00
|
|
|
success = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-08-04 14:22:47 -04: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 20:32:40 +02:00
|
|
|
m_lastFileName = filename.GetFullPath();
|
|
|
|
|
|
|
|
if( !progress && ( aFilenameList.GetCount() > 1 ) )
|
2018-02-20 21:58:53 -05:00
|
|
|
{
|
|
|
|
progress = std::make_unique<WX_PROGRESS_REPORTER>( this,
|
2021-06-26 12:33:37 +01:00
|
|
|
_( "Loading Gerber files..." ), 1,
|
|
|
|
false );
|
2018-02-20 21:58:53 -05:00
|
|
|
progress->SetMaxProgress( aFilenameList.GetCount() - 1 );
|
2021-06-26 12:33:37 +01:00
|
|
|
progress->Report( wxString::Format( _("Loading %u/%zu %s..." ),
|
|
|
|
ii+1,
|
|
|
|
aFilenameList.GetCount(),
|
|
|
|
m_lastFileName ) );
|
2018-02-20 21:58:53 -05:00
|
|
|
}
|
|
|
|
else if( progress )
|
|
|
|
{
|
2021-06-26 12:33:37 +01:00
|
|
|
progress->Report( wxString::Format( _("Loading %u/%zu %s..." ),
|
|
|
|
ii+1,
|
|
|
|
aFilenameList.GetCount(),
|
|
|
|
m_lastFileName ) );
|
2018-02-20 21:58:53 -05:00
|
|
|
progress->KeepRefreshing();
|
|
|
|
}
|
|
|
|
|
2011-01-20 11:34:57 -05:00
|
|
|
|
2021-08-04 14:22:47 -04:00
|
|
|
// Make sure we have a layer available to load into
|
2021-08-16 09:52:25 -04:00
|
|
|
layer = getNextAvailableLayer();
|
2021-08-04 14:22:47 -04:00
|
|
|
|
2021-08-15 14:48:13 -04:00
|
|
|
if( layer == NO_AVAILABLE_LAYERS )
|
2021-08-04 14:22:47 -04: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 14:40:01 +01:00
|
|
|
visibility[ layer ] = true;
|
2018-02-27 21:33:41 -05:00
|
|
|
|
2021-05-01 12:46:18 -04:00
|
|
|
try
|
2010-12-15 21:15:24 +01:00
|
|
|
{
|
2021-05-01 12:46:18 -04:00
|
|
|
if( aFileType && ( *aFileType )[ii] == 1 )
|
2020-07-10 13:59:35 +02:00
|
|
|
{
|
2021-08-04 14:22:47 -04:00
|
|
|
if( LoadExcellonFiles( filename.GetFullPath() ) )
|
|
|
|
{
|
|
|
|
UpdateFileHistory( filename.GetFullPath(), &m_drillFileHistory );
|
|
|
|
|
|
|
|
// Select the first added layer by default when done loading
|
|
|
|
if( firstLoadedLayer == NO_AVAILABLE_LAYERS )
|
|
|
|
{
|
|
|
|
firstLoadedLayer = layer;
|
|
|
|
}
|
|
|
|
}
|
2020-07-10 13:59:35 +02:00
|
|
|
}
|
2021-05-01 12:46:18 -04:00
|
|
|
else
|
2011-03-02 10:24:39 -05:00
|
|
|
{
|
2021-08-04 14:22:47 -04:00
|
|
|
if( Read_GERBER_File( filename.GetFullPath() ) )
|
2017-01-26 13:16:49 +01:00
|
|
|
{
|
2021-08-04 14:22:47 -04:00
|
|
|
UpdateFileHistory( filename.GetFullPath() );
|
2021-05-01 12:46:18 -04:00
|
|
|
|
2021-08-04 14:22:47 -04:00
|
|
|
if( firstLoadedLayer == NO_AVAILABLE_LAYERS )
|
2018-11-26 12:49:13 +01:00
|
|
|
{
|
2021-08-04 14:22:47 -04:00
|
|
|
firstLoadedLayer = layer;
|
2018-11-26 12:49:13 +01:00
|
|
|
}
|
2021-05-01 12:46:18 -04:00
|
|
|
}
|
2018-11-26 12:49:13 +01:00
|
|
|
}
|
2010-12-15 21:15:24 +01:00
|
|
|
}
|
2021-05-01 12:46:18 -04: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-20 21:58:53 -05:00
|
|
|
|
|
|
|
if( progress )
|
|
|
|
progress->AdvanceProgress();
|
2010-12-15 21:15:24 +01:00
|
|
|
}
|
2007-09-25 15:10:01 +00:00
|
|
|
|
2017-01-26 13:16:49 +01:00
|
|
|
if( !success )
|
|
|
|
{
|
2018-02-20 21:58:53 -05:00
|
|
|
wxSafeYield(); // Allows slice of time to redraw the screen
|
|
|
|
// to refresh widgets, before displaying messages
|
2017-01-26 13:16:49 +01:00
|
|
|
HTML_MESSAGE_BOX mbox( this, _( "Errors" ) );
|
|
|
|
mbox.ListSet( msg );
|
|
|
|
mbox.ShowModal();
|
|
|
|
}
|
|
|
|
|
2018-02-27 21:33:41 -05:00
|
|
|
SetVisibleLayers( visibility );
|
|
|
|
|
2021-08-04 14:22:47 -04:00
|
|
|
if( firstLoadedLayer != NO_AVAILABLE_LAYERS )
|
|
|
|
SetActiveLayer( firstLoadedLayer, true );
|
|
|
|
|
2010-12-17 21:34:29 +01:00
|
|
|
// Synchronize layers tools with actual active layer:
|
2014-11-22 12:52:57 +01:00
|
|
|
ReFillLayerWidget();
|
2018-03-14 21:51:58 -04:00
|
|
|
|
2011-04-05 19:49:14 +02:00
|
|
|
m_LayersManager->UpdateLayerIcons();
|
2018-02-20 21:58:53 -05:00
|
|
|
syncLayerBox( true );
|
|
|
|
|
2019-06-13 18:28:55 +01:00
|
|
|
GetCanvas()->Refresh();
|
2018-03-03 23:43:16 -05:00
|
|
|
|
2017-01-26 13:16:49 +01:00
|
|
|
return success;
|
2011-03-16 21:51:20 +01:00
|
|
|
}
|
|
|
|
|
2015-09-25 15:38:09 -04:00
|
|
|
|
2011-03-16 21:51:20 +01:00
|
|
|
bool GERBVIEW_FRAME::LoadExcellonFiles( const wxString& aFullFileName )
|
|
|
|
{
|
|
|
|
wxString filetypes;
|
|
|
|
wxArrayString filenamesList;
|
|
|
|
wxFileName filename = aFullFileName;
|
|
|
|
wxString currentPath;
|
|
|
|
|
|
|
|
if( !filename.IsOk() )
|
|
|
|
{
|
2017-11-11 19:31:38 -05:00
|
|
|
filetypes = DrillFileWildcard();
|
|
|
|
filetypes << wxT( "|" );
|
|
|
|
|
2011-03-16 21:51:20 +01:00
|
|
|
/* All filetypes */
|
2019-01-08 11:57:39 +00:00
|
|
|
filetypes += AllFilesWildcard();
|
2011-03-16 21:51:20 +01:00
|
|
|
|
|
|
|
/* Use the current working directory if the file name path does not exist. */
|
|
|
|
if( filename.DirExists() )
|
|
|
|
currentPath = filename.GetPath();
|
|
|
|
else
|
2015-09-25 15:38:09 -04:00
|
|
|
currentPath = m_mruPath;
|
2011-03-16 21:51:20 +01:00
|
|
|
|
2018-11-26 10:55:36 +01:00
|
|
|
wxFileDialog dlg( this, _( "Open NC (Excellon) Drill File(s)" ),
|
2017-01-26 18:15:46 +01:00
|
|
|
currentPath, filename.GetFullName(), filetypes,
|
2011-03-16 21:51:20 +01:00
|
|
|
wxFD_OPEN | wxFD_FILE_MUST_EXIST | wxFD_MULTIPLE | wxFD_CHANGE_DIR );
|
|
|
|
|
|
|
|
if( dlg.ShowModal() == wxID_CANCEL )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
dlg.GetPaths( filenamesList );
|
|
|
|
currentPath = wxGetCwd();
|
2015-09-25 15:38:09 -04:00
|
|
|
m_mruPath = currentPath;
|
2011-03-16 21:51:20 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
filenamesList.Add( aFullFileName );
|
|
|
|
currentPath = filename.GetPath();
|
2015-09-25 15:38:09 -04:00
|
|
|
m_mruPath = currentPath;
|
2011-03-16 21:51:20 +01:00
|
|
|
}
|
|
|
|
|
2017-01-26 13:16:49 +01:00
|
|
|
// Read Excellon drill files: each file is loaded on a new GerbView layer
|
|
|
|
bool success = true;
|
2017-09-17 18:43:20 -04:00
|
|
|
int layer = GetActiveLayer();
|
2011-03-16 21:51:20 +01:00
|
|
|
|
2017-01-26 13:16:49 +01:00
|
|
|
// Manage errors when loading files
|
|
|
|
wxString msg;
|
|
|
|
WX_STRING_REPORTER reporter( &msg );
|
|
|
|
|
2011-03-16 21:51:20 +01:00
|
|
|
for( unsigned ii = 0; ii < filenamesList.GetCount(); ii++ )
|
|
|
|
{
|
2016-04-29 13:10:31 +02:00
|
|
|
filename = filenamesList[ii];
|
2011-03-16 21:51:20 +01:00
|
|
|
|
|
|
|
if( !filename.IsAbsolute() )
|
|
|
|
filename.SetPath( currentPath );
|
|
|
|
|
2012-08-29 18:59:50 +02:00
|
|
|
m_lastFileName = filename.GetFullPath();
|
2011-03-16 21:51:20 +01:00
|
|
|
|
2021-08-16 09:52:25 -04:00
|
|
|
layer = getNextAvailableLayer();
|
2011-03-16 21:51:20 +01:00
|
|
|
|
2021-08-15 14:48:13 -04:00
|
|
|
if( layer == NO_AVAILABLE_LAYERS )
|
2011-03-16 21:51:20 +01:00
|
|
|
{
|
2021-08-15 14:48:13 -04:00
|
|
|
success = false;
|
|
|
|
reporter.Report( MSG_NO_MORE_LAYER, RPT_SEVERITY_ERROR );
|
2011-03-16 21:51:20 +01:00
|
|
|
|
2021-08-15 14:48:13 -04:00
|
|
|
// Report the name of not loaded files:
|
|
|
|
while( ii < filenamesList.GetCount() )
|
2011-03-16 21:51:20 +01:00
|
|
|
{
|
2021-08-15 14:48:13 -04:00
|
|
|
filename = filenamesList[ii++];
|
|
|
|
wxString txt = wxString::Format( MSG_NOT_LOADED, filename.GetFullName() );
|
|
|
|
reporter.Report( txt, RPT_SEVERITY_ERROR );
|
2011-03-16 21:51:20 +01:00
|
|
|
}
|
2021-08-15 14:48:13 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-09-17 18:43:20 -04:00
|
|
|
SetActiveLayer( layer, false );
|
2021-08-15 14:48:13 -04:00
|
|
|
|
|
|
|
if( Read_EXCELLON_File( filename.GetFullPath() ) )
|
|
|
|
{
|
|
|
|
// Update the list of recent drill files.
|
|
|
|
UpdateFileHistory( filename.GetFullPath(), &m_drillFileHistory );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wxString txt = wxString::Format( MSG_NOT_LOADED, filename.GetFullName() );
|
|
|
|
reporter.Report( txt, RPT_SEVERITY_ERROR );
|
|
|
|
}
|
2011-03-16 21:51:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-26 13:16:49 +01:00
|
|
|
if( !success )
|
|
|
|
{
|
|
|
|
HTML_MESSAGE_BOX mbox( this, _( "Errors" ) );
|
|
|
|
mbox.ListSet( msg );
|
|
|
|
mbox.ShowModal();
|
|
|
|
}
|
|
|
|
|
2011-03-16 21:51:20 +01:00
|
|
|
Zoom_Automatique( false );
|
|
|
|
|
|
|
|
// Synchronize layers tools with actual active layer:
|
2014-11-22 12:52:57 +01:00
|
|
|
ReFillLayerWidget();
|
2017-09-17 18:43:20 -04:00
|
|
|
SetActiveLayer( GetActiveLayer() );
|
2011-04-05 19:49:14 +02:00
|
|
|
m_LayersManager->UpdateLayerIcons();
|
2010-12-17 21:34:29 +01:00
|
|
|
syncLayerBox();
|
|
|
|
|
2017-01-26 13:16:49 +01:00
|
|
|
return success;
|
2007-05-06 16:03:28 +00:00
|
|
|
}
|
2017-01-26 10:45:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
bool GERBVIEW_FRAME::unarchiveFiles( const wxString& aFullFileName, REPORTER* aReporter )
|
|
|
|
{
|
2021-08-03 13:52:33 -04:00
|
|
|
bool foundX2Gerbers = false;
|
2017-03-31 10:55:13 +02:00
|
|
|
wxString msg;
|
2021-08-04 14:47:50 -04:00
|
|
|
int firstLoadedLayer = NO_AVAILABLE_LAYERS;
|
2017-01-26 10:45:28 +01:00
|
|
|
|
2017-03-31 10:55:13 +02:00
|
|
|
// Extract the path of aFullFileName. We use it to store temporary files
|
2017-01-26 10:45:28 +01:00
|
|
|
wxFileName fn( aFullFileName );
|
|
|
|
wxString unzipDir = fn.GetPath();
|
|
|
|
|
2017-03-31 10:55:13 +02:00
|
|
|
wxFFileInputStream zipFile( aFullFileName );
|
2017-01-26 10:45:28 +01:00
|
|
|
|
2017-03-31 10:55:13 +02:00
|
|
|
if( !zipFile.IsOk() )
|
2017-01-26 10:45:28 +01:00
|
|
|
{
|
2017-03-31 10:55:13 +02:00
|
|
|
if( aReporter )
|
2017-01-26 10:45:28 +01:00
|
|
|
{
|
2021-06-16 23:35:00 +01:00
|
|
|
msg.Printf( _( "Zip file '%s' cannot be opened." ), aFullFileName );
|
2020-03-04 10:48:18 +01:00
|
|
|
aReporter->Report( msg, RPT_SEVERITY_ERROR );
|
2017-01-26 10:45:28 +01:00
|
|
|
}
|
|
|
|
|
2017-03-31 10:55:13 +02:00
|
|
|
return false;
|
|
|
|
}
|
2017-01-26 10:45:28 +01:00
|
|
|
|
2017-03-31 10:55:13 +02:00
|
|
|
// Update the list of recent zip files.
|
|
|
|
UpdateFileHistory( aFullFileName, &m_zipFileHistory );
|
2017-01-26 10:45:28 +01:00
|
|
|
|
2017-03-31 10:55:13 +02: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 10:45:28 +01:00
|
|
|
|
|
|
|
|
2017-03-31 10:55:13 +02: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 10:45:28 +01:00
|
|
|
|
2017-01-26 18:15:46 +01: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 16:53:15 -04:00
|
|
|
if( curr_ext == GerberJobFileExtension.c_str() )
|
2017-01-26 10:45:28 +01:00
|
|
|
{
|
2021-08-02 16:53:15 -04:00
|
|
|
//We cannot read a gerber job file as a gerber plot file: skip it
|
2017-01-26 10:45:28 +01:00
|
|
|
if( aReporter )
|
|
|
|
{
|
2021-08-16 09:51:49 -04:00
|
|
|
msg.Printf( _( "Skipped file '%s' (gerber job file)." ), entry->GetName() );
|
2020-03-04 10:48:18 +01:00
|
|
|
aReporter->Report( msg, RPT_SEVERITY_WARNING );
|
2017-01-26 10:45:28 +01:00
|
|
|
}
|
2017-03-31 10:55:13 +02:00
|
|
|
|
|
|
|
continue;
|
2017-01-26 10:45:28 +01:00
|
|
|
}
|
|
|
|
|
2021-08-02 16:53:15 -04:00
|
|
|
wxString matchedExt;
|
|
|
|
enum GERBER_ORDER_ENUM order;
|
|
|
|
GERBER_FILE_IMAGE_LIST::GetGerberLayerFromFilename( fname, order, matchedExt );
|
|
|
|
|
2017-09-17 18:43:20 -04:00
|
|
|
int layer = GetActiveLayer();
|
2017-01-26 10:45:28 +01:00
|
|
|
|
2017-03-31 10:55:13 +02:00
|
|
|
if( layer == NO_AVAILABLE_LAYERS )
|
2017-01-26 18:15:46 +01:00
|
|
|
{
|
|
|
|
success = false;
|
|
|
|
|
|
|
|
if( aReporter )
|
|
|
|
{
|
2017-03-31 10:55:13 +02:00
|
|
|
if( !reported_no_more_layer )
|
2020-03-04 10:48:18 +01:00
|
|
|
aReporter->Report( MSG_NO_MORE_LAYER, RPT_SEVERITY_ERROR );
|
2017-03-31 10:55:13 +02:00
|
|
|
|
|
|
|
reported_no_more_layer = true;
|
|
|
|
|
|
|
|
// Report the name of not loaded files:
|
2020-10-15 20:39:55 -04:00
|
|
|
msg.Printf( MSG_NOT_LOADED, entry->GetName() );
|
2020-03-04 10:48:18 +01:00
|
|
|
aReporter->Report( msg, RPT_SEVERITY_ERROR );
|
2017-01-26 18:15:46 +01:00
|
|
|
}
|
|
|
|
|
2017-03-31 10:55:13 +02:00
|
|
|
delete entry;
|
|
|
|
continue;
|
|
|
|
}
|
2017-01-26 10:45:28 +01:00
|
|
|
|
2017-03-31 10:55:13 +02:00
|
|
|
// Create the unzipped temporary file:
|
2017-01-26 10:45:28 +01:00
|
|
|
{
|
2017-03-31 10:55:13 +02:00
|
|
|
wxFFileOutputStream temporary_ofile( unzipped_tempfile );
|
|
|
|
|
|
|
|
if( temporary_ofile.Ok() )
|
|
|
|
temporary_ofile.Write( zipArchive );
|
|
|
|
else
|
2017-01-26 10:45:28 +01:00
|
|
|
{
|
2017-03-31 10:55:13 +02:00
|
|
|
success = false;
|
2017-01-26 11:36:01 +01:00
|
|
|
|
2017-03-31 10:55:13 +02:00
|
|
|
if( aReporter )
|
2017-01-26 11:36:01 +01:00
|
|
|
{
|
2021-08-16 09:51:49 -04:00
|
|
|
msg.Printf( _( "<b>Unable to create temporary file '%s'.</b>" ),
|
2020-02-28 00:05:40 +00:00
|
|
|
unzipped_tempfile );
|
2020-03-04 10:48:18 +01:00
|
|
|
aReporter->Report( msg, RPT_SEVERITY_ERROR );
|
2017-01-26 11:36:01 +01:00
|
|
|
}
|
2017-01-26 10:45:28 +01:00
|
|
|
}
|
2017-03-31 10:55:13 +02:00
|
|
|
}
|
2017-01-26 10:45:28 +01:00
|
|
|
|
2017-03-31 10:55:13 +02:00
|
|
|
bool read_ok = true;
|
|
|
|
|
2021-08-03 11:09:09 -04: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 09:51:49 -04:00
|
|
|
msg.Printf( _( "Skipped file '%s' (unknown type)." ), entry->GetName() );
|
2021-08-03 11:09:09 -04: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 10:55:13 +02:00
|
|
|
{
|
|
|
|
// Read gerber files: each file is loaded on a new GerbView layer
|
|
|
|
read_ok = Read_GERBER_File( unzipped_tempfile );
|
2021-08-24 12:00:46 -04:00
|
|
|
|
|
|
|
if( read_ok )
|
|
|
|
GetCanvas()->GetView()->SetLayerHasNegatives(
|
|
|
|
GERBER_DRAW_LAYER( layer ), GetGbrImage( layer )->HasNegativeItems() );
|
2017-03-31 10:55:13 +02:00
|
|
|
}
|
|
|
|
|
2021-08-04 14:47:50 -04:00
|
|
|
// Select the first added layer by default when done loading
|
|
|
|
if( read_ok && firstLoadedLayer == NO_AVAILABLE_LAYERS )
|
|
|
|
{
|
|
|
|
firstLoadedLayer = layer;
|
|
|
|
}
|
|
|
|
|
2017-03-31 10:55:13 +02: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 09:51:49 -04:00
|
|
|
msg.Printf( _( "<b>unzipped file %s read error</b>" ), unzipped_tempfile );
|
2020-03-04 10:48:18 +01:00
|
|
|
aReporter->Report( msg, RPT_SEVERITY_ERROR );
|
2017-03-31 10:55:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GERBER_FILE_IMAGE* gerber_image = GetGbrImage( layer );
|
2017-05-29 20:47:21 +02:00
|
|
|
|
|
|
|
if( gerber_image )
|
2021-08-03 13:52:33 -04:00
|
|
|
{
|
2017-05-29 20:47:21 +02:00
|
|
|
gerber_image->m_FileName = fname;
|
2021-08-03 13:52:33 -04:00
|
|
|
if( gerber_image->m_IsX2_file )
|
|
|
|
foundX2Gerbers = true;
|
|
|
|
}
|
2017-03-31 10:55:13 +02:00
|
|
|
|
2021-08-16 09:52:25 -04:00
|
|
|
layer = getNextAvailableLayer();
|
2017-09-17 18:43:20 -04:00
|
|
|
SetActiveLayer( layer, false );
|
2017-01-26 18:15:46 +01:00
|
|
|
}
|
2017-01-26 10:45:28 +01:00
|
|
|
}
|
|
|
|
|
2021-08-03 13:52:33 -04:00
|
|
|
if( foundX2Gerbers )
|
|
|
|
SortLayersByX2Attributes();
|
|
|
|
else
|
|
|
|
SortLayersByFileExtension();
|
|
|
|
|
2021-08-04 14:47:50 -04: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 10:45:28 +01:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool GERBVIEW_FRAME::LoadZipArchiveFile( const wxString& aFullFileName )
|
|
|
|
{
|
|
|
|
#define ZipFileExtension "zip"
|
2017-11-11 19:31:38 -05:00
|
|
|
|
2017-01-26 10:45:28 +01: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 23:02:07 +01:00
|
|
|
wxFileDialog dlg( this, _( "Open Zip File" ), currentPath, filename.GetFullName(),
|
|
|
|
ZipFileWildcard(), wxFD_OPEN | wxFD_FILE_MUST_EXIST | wxFD_CHANGE_DIR );
|
2017-01-26 10:45:28 +01: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 18:15:46 +01:00
|
|
|
wxString msg;
|
|
|
|
WX_STRING_REPORTER reporter( &msg );
|
2017-01-26 10:45:28 +01:00
|
|
|
|
2017-01-26 18:15:46 +01:00
|
|
|
if( filename.IsOk() )
|
|
|
|
unarchiveFiles( filename.GetFullPath(), &reporter );
|
2017-01-26 10:45:28 +01:00
|
|
|
|
|
|
|
Zoom_Automatique( false );
|
|
|
|
|
|
|
|
// Synchronize layers tools with actual active layer:
|
|
|
|
ReFillLayerWidget();
|
2017-09-17 18:43:20 -04:00
|
|
|
SetActiveLayer( GetActiveLayer() );
|
2017-01-26 10:45:28 +01:00
|
|
|
m_LayersManager->UpdateLayerIcons();
|
|
|
|
syncLayerBox();
|
|
|
|
|
2017-01-26 18:15:46 +01: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 10:45:28 +01:00
|
|
|
return true;
|
|
|
|
}
|