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-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>
|
2020-10-25 13:14:52 +00:00
|
|
|
#include <dialogs/html_messagebox.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-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-05-01 16:46:18 +00:00
|
|
|
#define MSG_NOT_LOADED _( "\n<b>Not loaded:</b> <i>%s</i>" )
|
|
|
|
#define MSG_OOM _( "\n<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
|
|
|
{
|
2014-07-14 18:59:41 +00:00
|
|
|
Erase_Current_DrawLayer( false );
|
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() )
|
|
|
|
{
|
2014-07-14 18:59:41 +00:00
|
|
|
Erase_Current_DrawLayer( false );
|
2011-03-17 19:14:45 +00:00
|
|
|
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() )
|
|
|
|
{
|
|
|
|
Erase_Current_DrawLayer( false );
|
|
|
|
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
|
|
|
|
2011-03-12 09:50:21 +00:00
|
|
|
bool GERBVIEW_FRAME::LoadGerberFiles( const wxString& aFullFileName )
|
2007-05-06 16:03:28 +00:00
|
|
|
{
|
2019-04-01 16:46:11 +00:00
|
|
|
static int lastGerberFileWildcard = 0;
|
2009-11-09 14:00:22 +00:00
|
|
|
wxString filetypes;
|
2010-12-15 20:15:24 +00:00
|
|
|
wxArrayString filenamesList;
|
2010-05-17 20:35:46 +00:00
|
|
|
wxFileName filename = aFullFileName;
|
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
|
|
|
{
|
2009-12-30 18:06:12 +00:00
|
|
|
/* Standard gerber filetypes
|
|
|
|
* (See http://en.wikipedia.org/wiki/Gerber_File)
|
2020-05-03 22:02:07 +00: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 16:42:06 +00:00
|
|
|
filetypes = _( "Gerber files (.g* .lgr .pho)" );
|
2009-12-30 18:06:12 +00:00
|
|
|
filetypes << wxT("|");
|
2014-06-30 10:00:21 +00: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 */
|
|
|
|
filetypes += _( "Top layer (*.GTL)|*.GTL;*.gtl|" );
|
|
|
|
filetypes += _( "Bottom layer (*.GBL)|*.GBL;*.gbl|" );
|
|
|
|
filetypes += _( "Bottom solder resist (*.GBS)|*.GBS;*.gbs|" );
|
|
|
|
filetypes += _( "Top solder resist (*.GTS)|*.GTS;*.gts|" );
|
|
|
|
filetypes += _( "Bottom overlay (*.GBO)|*.GBO;*.gbo|" );
|
|
|
|
filetypes += _( "Top overlay (*.GTO)|*.GTO;*.gto|" );
|
|
|
|
filetypes += _( "Bottom paste (*.GBP)|*.GBP;*.gbp|" );
|
|
|
|
filetypes += _( "Top paste (*.GTP)|*.GTP;*.gtp|" );
|
|
|
|
filetypes += _( "Keep-out layer (*.GKO)|*.GKO;*.gko|" );
|
|
|
|
filetypes += _( "Mechanical layers (*.GMx)|*.GM1;*.gm1;*.GM2;*.gm2;*.GM3;*.gm3|" );
|
|
|
|
filetypes += _( "Top Pad Master (*.GPT)|*.GPT;*.gpt|" );
|
|
|
|
filetypes += _( "Bottom Pad Master (*.GPB)|*.GPB;*.gpb|" );
|
|
|
|
|
2016-09-07 08:28:16 +00:00
|
|
|
// All filetypes
|
2019-01-08 11:57:39 +00:00
|
|
|
filetypes += AllFilesWildcard();
|
2009-11-09 14:00:22 +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();
|
|
|
|
}
|
|
|
|
|
2020-05-03 22:02:07 +00:00
|
|
|
wxFileDialog dlg( this, _( "Open Gerber File(s)" ), currentPath, filename.GetFullName(),
|
2009-05-28 20:46:16 +00:00
|
|
|
filetypes,
|
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
|
|
|
|
{
|
|
|
|
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-28 01:59:47 +00:00
|
|
|
Erase_Current_DrawLayer( false );
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
bool success = LoadListOfGerberAndDrillFiles( currentPath, filenamesList );
|
|
|
|
|
|
|
|
// Auto zoom is only applied if there is only one file loaded
|
2021-07-25 10:51:07 +00:00
|
|
|
if( isFirstFile )
|
2021-07-25 03:41:05 +00:00
|
|
|
{
|
|
|
|
Zoom_Automatique( false );
|
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
2018-02-21 02:58:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-08 23:51:05 +00:00
|
|
|
bool GERBVIEW_FRAME::LoadListOfGerberAndDrillFiles( const wxString& aPath,
|
2018-11-26 11:49:13 +00:00
|
|
|
const wxArrayString& aFilenameList,
|
|
|
|
const std::vector<int>* aFileType )
|
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();
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
progress = std::make_unique<WX_PROGRESS_REPORTER>( this,
|
2021-06-26 11:33:37 +00:00
|
|
|
_( "Loading Gerber files..." ), 1,
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2017-09-17 22:43:20 +00:00
|
|
|
SetActiveLayer( layer, false );
|
2011-01-20 16:34:57 +00:00
|
|
|
|
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-05-01 16:46:18 +00:00
|
|
|
if( aFileType && ( *aFileType )[ii] == 1 )
|
2020-07-10 11:59:35 +00:00
|
|
|
{
|
2021-05-01 16:46:18 +00:00
|
|
|
LoadExcellonFiles( filename.GetFullPath() );
|
|
|
|
layer = GetActiveLayer(); // Loading NC drill file changes the active layer
|
2020-07-10 11:59:35 +00:00
|
|
|
}
|
2021-05-01 16:46:18 +00:00
|
|
|
else
|
2011-03-02 15:24:39 +00:00
|
|
|
{
|
2021-05-01 16:46:18 +00:00
|
|
|
if( filename.GetExt() == GerberJobFileExtension.c_str() )
|
2017-01-26 12:16:49 +00:00
|
|
|
{
|
2021-05-01 16:46:18 +00:00
|
|
|
//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() );
|
2018-11-26 11:49:13 +00:00
|
|
|
success = false;
|
2021-05-01 16:46:18 +00:00
|
|
|
reporter.Report( txt, RPT_SEVERITY_ERROR );
|
|
|
|
}
|
|
|
|
else if( Read_GERBER_File( filename.GetFullPath() ) )
|
|
|
|
{
|
|
|
|
UpdateFileHistory( m_lastFileName );
|
2018-11-26 11:49:13 +00:00
|
|
|
|
2021-05-01 16:46:18 +00:00
|
|
|
layer = getNextAvailableLayer( layer );
|
|
|
|
|
|
|
|
if( layer == NO_AVAILABLE_LAYERS && ii < aFilenameList.GetCount() - 1 )
|
2018-11-26 11:49:13 +00:00
|
|
|
{
|
2021-05-01 16:46:18 +00:00
|
|
|
success = false;
|
|
|
|
reporter.Report( MSG_NO_MORE_LAYER, RPT_SEVERITY_ERROR );
|
|
|
|
|
|
|
|
// Report the name of not loaded files:
|
|
|
|
ii += 1;
|
|
|
|
while( ii < aFilenameList.GetCount() )
|
|
|
|
{
|
|
|
|
filename = aFilenameList[ii++];
|
|
|
|
wxString txt =
|
|
|
|
wxString::Format( MSG_NOT_LOADED, filename.GetFullName() );
|
|
|
|
reporter.Report( txt, RPT_SEVERITY_ERROR );
|
|
|
|
}
|
|
|
|
break;
|
2018-11-26 11:49:13 +00:00
|
|
|
}
|
2011-03-02 15:24:39 +00:00
|
|
|
|
2021-05-01 16:46:18 +00:00
|
|
|
SetActiveLayer( layer, false );
|
|
|
|
}
|
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 );
|
|
|
|
|
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
|
|
|
|
|
|
|
// TODO: it would be nice if we could set the active layer to one of the
|
|
|
|
// ones that was just loaded, but to maintain the previous user experience
|
|
|
|
// we need to set it to a blank layer in case they load another file.
|
|
|
|
// We can't start with the next available layer when loading files because
|
|
|
|
// some users expect the behavior of overwriting the active layer on load.
|
|
|
|
SetActiveLayer( getNextAvailableLayer( layer ), true );
|
|
|
|
|
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
|
|
|
|
2011-03-16 20:51:20 +00:00
|
|
|
bool GERBVIEW_FRAME::LoadExcellonFiles( const wxString& aFullFileName )
|
|
|
|
{
|
|
|
|
wxString filetypes;
|
|
|
|
wxArrayString filenamesList;
|
|
|
|
wxFileName filename = aFullFileName;
|
|
|
|
wxString currentPath;
|
|
|
|
|
|
|
|
if( !filename.IsOk() )
|
|
|
|
{
|
2017-11-12 00:31:38 +00:00
|
|
|
filetypes = DrillFileWildcard();
|
|
|
|
filetypes << wxT( "|" );
|
|
|
|
|
2011-03-16 20:51:20 +00:00
|
|
|
/* All filetypes */
|
2019-01-08 11:57:39 +00:00
|
|
|
filetypes += AllFilesWildcard();
|
2011-03-16 20:51:20 +00:00
|
|
|
|
|
|
|
/* Use the current working directory if the file name path does not exist. */
|
|
|
|
if( filename.DirExists() )
|
|
|
|
currentPath = filename.GetPath();
|
|
|
|
else
|
2015-09-25 19:38:09 +00:00
|
|
|
currentPath = m_mruPath;
|
2011-03-16 20:51:20 +00:00
|
|
|
|
2018-11-26 09:55:36 +00:00
|
|
|
wxFileDialog dlg( this, _( "Open NC (Excellon) Drill File(s)" ),
|
2017-01-26 17:15:46 +00:00
|
|
|
currentPath, filename.GetFullName(), filetypes,
|
2011-03-16 20:51:20 +00: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 19:38:09 +00:00
|
|
|
m_mruPath = currentPath;
|
2011-03-16 20:51:20 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
filenamesList.Add( aFullFileName );
|
|
|
|
currentPath = filename.GetPath();
|
2015-09-25 19:38:09 +00:00
|
|
|
m_mruPath = currentPath;
|
2011-03-16 20:51:20 +00:00
|
|
|
}
|
|
|
|
|
2017-01-26 12:16:49 +00:00
|
|
|
// Read Excellon drill files: each file is loaded on a new GerbView layer
|
|
|
|
bool success = true;
|
2017-09-17 22:43:20 +00:00
|
|
|
int layer = GetActiveLayer();
|
2011-03-16 20:51:20 +00:00
|
|
|
|
2017-01-26 12:16:49 +00:00
|
|
|
// Manage errors when loading files
|
|
|
|
wxString msg;
|
|
|
|
WX_STRING_REPORTER reporter( &msg );
|
|
|
|
|
2011-03-16 20:51:20 +00:00
|
|
|
for( unsigned ii = 0; ii < filenamesList.GetCount(); ii++ )
|
|
|
|
{
|
2016-04-29 11:10:31 +00:00
|
|
|
filename = filenamesList[ii];
|
2011-03-16 20:51:20 +00:00
|
|
|
|
|
|
|
if( !filename.IsAbsolute() )
|
|
|
|
filename.SetPath( currentPath );
|
|
|
|
|
2012-08-29 16:59:50 +00:00
|
|
|
m_lastFileName = filename.GetFullPath();
|
2011-03-16 20:51:20 +00:00
|
|
|
|
2017-09-17 22:43:20 +00:00
|
|
|
SetActiveLayer( layer, false );
|
2011-03-16 20:51:20 +00:00
|
|
|
|
|
|
|
if( Read_EXCELLON_File( filename.GetFullPath() ) )
|
|
|
|
{
|
2015-09-25 19:38:09 +00:00
|
|
|
// Update the list of recent drill files.
|
2018-11-26 11:49:13 +00:00
|
|
|
UpdateFileHistory( filename.GetFullPath(), &m_drillFileHistory );
|
2011-03-17 19:14:45 +00:00
|
|
|
|
2011-03-16 20:51:20 +00:00
|
|
|
layer = getNextAvailableLayer( layer );
|
|
|
|
|
2017-01-26 12:16:49 +00:00
|
|
|
if( layer == NO_AVAILABLE_LAYERS && ii < filenamesList.GetCount()-1 )
|
2011-03-16 20:51:20 +00:00
|
|
|
{
|
2017-01-26 12:16:49 +00:00
|
|
|
success = false;
|
2020-03-04 09:48:18 +00:00
|
|
|
reporter.Report( MSG_NO_MORE_LAYER, RPT_SEVERITY_ERROR );
|
2017-01-26 12:16:49 +00:00
|
|
|
|
|
|
|
// Report the name of not loaded files:
|
|
|
|
ii += 1;
|
|
|
|
while( ii < filenamesList.GetCount() )
|
|
|
|
{
|
|
|
|
filename = filenamesList[ii++];
|
2019-04-10 13:40:01 +00:00
|
|
|
wxString txt = wxString::Format( MSG_NOT_LOADED, filename.GetFullName() );
|
2020-03-04 09:48:18 +00:00
|
|
|
reporter.Report( txt, RPT_SEVERITY_ERROR );
|
2017-01-26 12:16:49 +00:00
|
|
|
}
|
2011-03-16 20:51:20 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-09-17 22:43:20 +00:00
|
|
|
SetActiveLayer( layer, false );
|
2011-03-16 20:51:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-26 12:16:49 +00:00
|
|
|
if( !success )
|
|
|
|
{
|
|
|
|
HTML_MESSAGE_BOX mbox( this, _( "Errors" ) );
|
|
|
|
mbox.ListSet( msg );
|
|
|
|
mbox.ShowModal();
|
|
|
|
}
|
|
|
|
|
2011-03-16 20:51:20 +00:00
|
|
|
Zoom_Automatique( false );
|
|
|
|
|
|
|
|
// Synchronize layers tools with actual active layer:
|
2014-11-22 11:52:57 +00:00
|
|
|
ReFillLayerWidget();
|
2017-09-17 22:43:20 +00:00
|
|
|
SetActiveLayer( GetActiveLayer() );
|
2011-04-05 17:49:14 +00:00
|
|
|
m_LayersManager->UpdateLayerIcons();
|
2010-12-17 20:34:29 +00:00
|
|
|
syncLayerBox();
|
|
|
|
|
2017-01-26 12:16:49 +00:00
|
|
|
return success;
|
2007-05-06 16:03:28 +00:00
|
|
|
}
|
2017-01-26 09:45:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
bool GERBVIEW_FRAME::unarchiveFiles( const wxString& aFullFileName, REPORTER* aReporter )
|
|
|
|
{
|
2017-03-31 08:55:13 +00:00
|
|
|
wxString msg;
|
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.
|
2018-04-08 10:28:59 +00:00
|
|
|
// Gerber files ext is usually "gbr", but can be also another value, starting by "g"
|
2017-01-26 10:36:01 +00:00
|
|
|
// old gerber files ext from kicad is .pho
|
2017-01-26 09:45:28 +00:00
|
|
|
// drill files do not have a well defined ext
|
2017-01-26 17:15:46 +00:00
|
|
|
// It is .drl in kicad, but .txt in Altium for instance
|
|
|
|
// Allows only .drl for drill files.
|
2017-03-31 08:55:13 +00:00
|
|
|
if( curr_ext[0] != 'g' && curr_ext != "pho" && curr_ext != "drl" )
|
2017-01-26 09:45:28 +00:00
|
|
|
{
|
|
|
|
if( aReporter )
|
|
|
|
{
|
2021-06-16 22:35:00 +00:00
|
|
|
msg.Printf( _( "Skipped file '%s' (unknown type).\n" ), 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
|
|
|
}
|
|
|
|
|
2020-07-10 11:59:35 +00:00
|
|
|
if( curr_ext == GerberJobFileExtension.c_str() )
|
|
|
|
{
|
|
|
|
//We cannot read a gerber job file as a gerber plot file: skip it
|
|
|
|
if( aReporter )
|
|
|
|
{
|
2021-06-16 22:35:00 +00:00
|
|
|
msg.Printf( _( "Skipped file '%s' (gerber job file).\n" ), entry->GetName() );
|
2020-07-10 11:59:35 +00:00
|
|
|
aReporter->Report( msg, RPT_SEVERITY_WARNING );
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
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-06-16 22:35:00 +00:00
|
|
|
msg.Printf( _( "<b>Unable to create temporary file '%s'.</b>\n"),
|
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;
|
|
|
|
|
|
|
|
if( curr_ext[0] == 'g' || curr_ext == "pho" )
|
|
|
|
{
|
|
|
|
// Read gerber files: each file is loaded on a new GerbView layer
|
|
|
|
read_ok = Read_GERBER_File( unzipped_tempfile );
|
|
|
|
}
|
|
|
|
else // if( curr_ext == "drl" )
|
|
|
|
{
|
|
|
|
read_ok = Read_EXCELLON_File( unzipped_tempfile );
|
|
|
|
}
|
|
|
|
|
|
|
|
delete entry;
|
|
|
|
|
|
|
|
// The unzipped file is only a temporary file, delete it.
|
|
|
|
wxRemoveFile( unzipped_tempfile );
|
|
|
|
|
|
|
|
if( !read_ok )
|
|
|
|
{
|
|
|
|
success = false;
|
|
|
|
|
|
|
|
if( aReporter )
|
|
|
|
{
|
2020-02-28 00:05:40 +00:00
|
|
|
msg.Printf( _("<b>unzipped file %s read error</b>\n"), 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 )
|
|
|
|
gerber_image->m_FileName = fname;
|
2017-03-31 08:55:13 +00:00
|
|
|
|
|
|
|
layer = getNextAvailableLayer( layer );
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|