Some refactoring around Gerber files extension checking.

Fixes a case where most Protel inner layer files couldn't be drag and dropped.
This commit is contained in:
Alex 2022-11-18 09:55:34 +05:00
parent 95b8fe26b4
commit 53b9baa5ba
9 changed files with 42 additions and 80 deletions

View File

@ -1131,14 +1131,14 @@ void EDA_BASE_FRAME::OnDropFiles( wxDropFilesEvent& aEvent )
for( int nb = 0; nb < aEvent.GetNumberOfFiles(); nb++ )
{
const wxFileName fn = wxFileName( files[nb] );
for( const auto& [ext, tool] : m_acceptedExts )
{
if( IsExtensionAccepted( fn.GetExt(), { ext.ToStdString() } ) )
{
m_AcceptedFiles.emplace( m_AcceptedFiles.end(), fn );
break;
}
}
wxString ext = fn.GetExt();
// Alias all gerber files as GerberFileExtension
if( IsGerberFileExtension( ext ) )
ext = GerberFileExtension;
if( m_acceptedExts.find( ext.ToStdString() ) != m_acceptedExts.end() )
m_AcceptedFiles.emplace_back( fn );
}
DoWithAcceptedFiles();

View File

@ -184,37 +184,14 @@ const std::string TextFileExtension( "txt" );
const std::string MarkdownFileExtension( "md" );
const std::string CsvFileExtension( "csv" );
/**
* Gerber files extensions Kicad is to open
*/
const std::vector<std::string> GerberFileExtensions =
const wxString GerberFileExtensionsRegex( "(gbr|gko|pho|(g[tb][alops])|(gm?\\d\\d*)|(gp[tb]))" );
bool IsGerberFileExtension( const wxString& ext )
{
GerberFileExtension,
"gbl", "gbo", "gbp", "gbs", "gko",
"gm1", "gm2", "gm3", "gm4", "gm5", "gm6", "gm7", "gm8", "gm9",
"g1", "g3",
"gpt", "gpb", "gtl", "gto", "gtp", "gts", "pho", "pos"
};
static wxRegEx gerberRE( GerberFileExtensionsRegex, wxRE_ICASE );
const wxString GerberFileExtensionWildCard( "((gbr|(gb|gt)[alops]|g[0-9]{1,2}|gm[0-9]{1,2}|gko)|pho)" );
bool IsExtensionAccepted( const wxString& aExt, const std::vector<std::string> acceptedExts )
{
for( auto extPtr = acceptedExts.begin(); extPtr != acceptedExts.end(); extPtr++ )
{
if( aExt.ToStdString() == *extPtr )
return true;
}
return false;
}
bool IsProtelExtension( const wxString& ext )
{
static wxRegEx protelRE( wxT( "(gm1)|(g[tb][lapos])|(g\\d\\d*)" ), wxRE_ICASE );
return protelRE.Matches( ext );
return gerberRE.Matches( ext );
}
@ -343,12 +320,6 @@ wxString NetlistFileWildcard()
}
wxString GerberFileWildcard()
{
return _( "Gerber files" ) + AddFileExtListToFilter( { "pho" } );
}
wxString LegacyPcbFileWildcard()
{
return _( "KiCad printed circuit board files" ) + AddFileExtListToFilter( { "brd" } );

View File

@ -199,10 +199,7 @@ bool GERBVIEW_FRAME::LoadGerberFiles( const wxString& aFileName )
* 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( "|" );
filetypes = _( "Gerber files" ) + AddFileExtListToFilter( { "G*", "PHO" } ) + wxT( "|" );
/* Special gerber filetypes */
filetypes += _( "Top layer" ) + AddFileExtListToFilter( { "GTL" } ) + wxT( "|" );
@ -680,7 +677,7 @@ void GERBVIEW_FRAME::DoWithAcceptedFiles()
for( const wxFileName& file : m_AcceptedFiles )
{
if( IsExtensionAccepted( file.GetExt(), { ArchiveFileExtension } ) )
if( file.GetExt() == ArchiveFileExtension )
{
wxString fn = file.GetFullPath();
// Open zip archive in editor
@ -695,5 +692,5 @@ void GERBVIEW_FRAME::DoWithAcceptedFiles()
// Open files in editor
if( !gerbFn.IsEmpty() )
m_toolManager->RunAction( *m_acceptedExts.at( DrillFileExtension ), true, &gerbFn );
m_toolManager->RunAction( *m_acceptedExts.at( GerberFileExtension ), true, &gerbFn );
}

View File

@ -173,7 +173,7 @@ void IFACE::SaveFileAs( const wxString& aProjectBasePath, const wxString& aProje
destFile.SetPath( destPath );
}
if( ext == wxT( "gbr" ) || IsProtelExtension( ext ) )
if( IsGerberFileExtension( ext ) )
{
wxString destFileName = destFile.GetName();
@ -185,7 +185,7 @@ void IFACE::SaveFileAs( const wxString& aProjectBasePath, const wxString& aProje
KiCopyFile( aSrcFilePath, destFile.GetFullPath(), aErrors );
}
else if( ext == wxT( "gbrjob" ) )
else if( ext == GerberJobFileExtension )
{
if( destFile.GetName() == aProjectName + wxT( "-job" ) )
destFile.SetName( aNewProjectName + wxT( "-job" ) );
@ -246,7 +246,7 @@ void IFACE::SaveFileAs( const wxString& aProjectBasePath, const wxString& aProje
aErrors += msg;
}
}
else if( ext == wxT( "drl" ) )
else if( ext == DrillFileExtension )
{
wxString destFileName = destFile.GetName();

View File

@ -179,9 +179,9 @@ GERBVIEW_FRAME::GERBVIEW_FRAME( KIWAY* aKiway, wxWindow* aParent )
m_LayersManager->ReFillRender(); // Update colors in Render after the config is read
// Drag and drop
// Note that all gerber files are aliased as GerberFileExtension
m_acceptedExts.emplace( GerberFileExtension, &GERBVIEW_ACTIONS::loadGerbFiles );
m_acceptedExts.emplace( ArchiveFileExtension, &GERBVIEW_ACTIONS::loadZipFile );
for( const auto& ext : GerberFileExtensions )
m_acceptedExts.emplace( ext, &GERBVIEW_ACTIONS::loadGerbFiles );
m_acceptedExts.emplace( DrillFileExtension, &GERBVIEW_ACTIONS::loadGerbFiles );
DragAcceptFiles( true );

View File

@ -170,19 +170,9 @@ extern const std::string TextFileExtension;
extern const std::string MarkdownFileExtension;
extern const std::string CsvFileExtension;
extern const std::vector<std::string> GerberFileExtensions;
extern const wxString GerberFileExtensionWildCard;
extern const wxString GerberFileExtensionsRegex;
/**
* Checks if the file extension is in accepted extensions
* @param aExt is the extension to test
* @param acceptedExts Array with extensions to test against
*
* @return true if the extension is in array
*/
bool IsExtensionAccepted( const wxString& aExt, const std::vector<std::string> acceptedExts );
bool IsProtelExtension( const wxString& ext );
bool IsGerberFileExtension( const wxString& ext );
/**
* @}
@ -217,7 +207,6 @@ extern wxString LegacySchematicFileWildcard();
extern wxString BoardFileWildcard();
extern wxString OrCadPcb2NetlistFileWildcard();
extern wxString NetlistFileWildcard();
extern wxString GerberFileWildcard();
extern wxString HtmlFileWildcard();
extern wxString CsvFileWildcard();
extern wxString LegacyPcbFileWildcard();

View File

@ -231,18 +231,23 @@ KICAD_MANAGER_FRAME::KICAD_MANAGER_FRAME( wxWindow* parent, const wxString& titl
// Init for dropping files
m_acceptedExts.emplace( ProjectFileExtension, &KICAD_MANAGER_ACTIONS::loadProject );
m_acceptedExts.emplace( LegacyProjectFileExtension, &KICAD_MANAGER_ACTIONS::loadProject );
for( const auto& ext : GerberFileExtensions )
m_acceptedExts.emplace( ext, &KICAD_MANAGER_ACTIONS::viewDroppedGerbers );
// Gerber files
// Note that all gerber files are aliased as GerberFileExtension
m_acceptedExts.emplace( GerberFileExtension, &KICAD_MANAGER_ACTIONS::viewDroppedGerbers );
m_acceptedExts.emplace( GerberJobFileExtension, &KICAD_MANAGER_ACTIONS::viewDroppedGerbers );
m_acceptedExts.emplace( DrillFileExtension, &KICAD_MANAGER_ACTIONS::viewDroppedGerbers );
// Eagle files import
m_acceptedExts.emplace( EagleSchematicFileExtension,
&KICAD_MANAGER_ACTIONS::importNonKicadProj );
m_acceptedExts.emplace( EaglePcbFileExtension, &KICAD_MANAGER_ACTIONS::importNonKicadProj );
// Cadstar files import
m_acceptedExts.emplace( CadstarSchematicFileExtension,
&KICAD_MANAGER_ACTIONS::importNonKicadProj );
m_acceptedExts.emplace( CadstarPcbFileExtension, &KICAD_MANAGER_ACTIONS::importNonKicadProj );
DragAcceptFiles( true );
// Ensure the window is on top
@ -422,11 +427,13 @@ void KICAD_MANAGER_FRAME::DoWithAcceptedFiles()
// If not, open files in dedicated app.
for( const wxFileName& fileName : m_AcceptedFiles )
{
if( IsExtensionAccepted( fileName.GetExt(),
{ ProjectFileExtension, LegacyProjectFileExtension } ) )
wxString ext = fileName.GetExt();
if( ext == ProjectFileExtension || ext == LegacyProjectFileExtension )
{
wxString fn = fileName.GetFullPath();
m_toolManager->RunAction( *m_acceptedExts.at( fileName.GetExt() ), true, &fn );
return;
}
}
@ -435,13 +442,12 @@ void KICAD_MANAGER_FRAME::DoWithAcceptedFiles()
wxString gerberFiles;
// Gerbview editor should be able to open Gerber and drill files
std::vector<std::string> gerberExts( GerberFileExtensions );
gerberExts.push_back( GerberJobFileExtension );
gerberExts.push_back( DrillFileExtension );
for( const wxFileName& fileName : m_AcceptedFiles )
{
if( IsExtensionAccepted( fileName.GetExt(), gerberExts ) )
wxString ext = fileName.GetExt();
if( ext == GerberJobFileExtension || ext == DrillFileExtension
|| IsGerberFileExtension( ext ) )
{
gerberFiles += wxT( '\"' );
gerberFiles += fileName.GetFullPath() + wxT( '\"' );

View File

@ -258,7 +258,7 @@ wxString PROJECT_TREE_PANE::GetFileExt( TREE_FILE_TYPE type )
case TREE_FILE_TYPE::SEXPR_SCHEMATIC: return KiCadSchematicFileExtension;
case TREE_FILE_TYPE::LEGACY_PCB: return LegacyPcbFileExtension;
case TREE_FILE_TYPE::SEXPR_PCB: return KiCadPcbFileExtension;
case TREE_FILE_TYPE::GERBER: return GerberFileExtensionWildCard;
case TREE_FILE_TYPE::GERBER: return GerberFileExtensionsRegex;
case TREE_FILE_TYPE::GERBER_JOB_FILE: return GerberJobFileExtension;
case TREE_FILE_TYPE::HTML: return HtmlFileExtension;
case TREE_FILE_TYPE::PDF: return PdfFileExtension;

View File

@ -527,10 +527,9 @@ public:
pleditor->SaveFileAs( m_projectDirPath, m_projectName, m_newProjectDirPath,
m_newProjectName, aSrcFilePath, m_errors );
}
else if( ext == GerberFileExtension
|| ext == GerberJobFileExtension
else if( ext == GerberJobFileExtension
|| ext == DrillFileExtension
|| IsProtelExtension( ext ) )
|| IsGerberFileExtension(ext) )
{
KIFACE* gerbview = m_frame->Kiway().KiFACE( KIWAY::FACE_GERBVIEW );
gerbview->SaveFileAs( m_projectDirPath, m_projectName, m_newProjectDirPath,