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++ ) for( int nb = 0; nb < aEvent.GetNumberOfFiles(); nb++ )
{ {
const wxFileName fn = wxFileName( files[nb] ); const wxFileName fn = wxFileName( files[nb] );
for( const auto& [ext, tool] : m_acceptedExts ) wxString ext = fn.GetExt();
{
if( IsExtensionAccepted( fn.GetExt(), { ext.ToStdString() } ) ) // Alias all gerber files as GerberFileExtension
{ if( IsGerberFileExtension( ext ) )
m_AcceptedFiles.emplace( m_AcceptedFiles.end(), fn ); ext = GerberFileExtension;
break;
} if( m_acceptedExts.find( ext.ToStdString() ) != m_acceptedExts.end() )
} m_AcceptedFiles.emplace_back( fn );
} }
DoWithAcceptedFiles(); DoWithAcceptedFiles();

View File

@ -184,37 +184,14 @@ const std::string TextFileExtension( "txt" );
const std::string MarkdownFileExtension( "md" ); const std::string MarkdownFileExtension( "md" );
const std::string CsvFileExtension( "csv" ); const std::string CsvFileExtension( "csv" );
/** const wxString GerberFileExtensionsRegex( "(gbr|gko|pho|(g[tb][alops])|(gm?\\d\\d*)|(gp[tb]))" );
* Gerber files extensions Kicad is to open
*/
const std::vector<std::string> GerberFileExtensions = bool IsGerberFileExtension( const wxString& ext )
{ {
GerberFileExtension, static wxRegEx gerberRE( GerberFileExtensionsRegex, wxRE_ICASE );
"gbl", "gbo", "gbp", "gbs", "gko",
"gm1", "gm2", "gm3", "gm4", "gm5", "gm6", "gm7", "gm8", "gm9",
"g1", "g3",
"gpt", "gpb", "gtl", "gto", "gtp", "gts", "pho", "pos"
};
const wxString GerberFileExtensionWildCard( "((gbr|(gb|gt)[alops]|g[0-9]{1,2}|gm[0-9]{1,2}|gko)|pho)" ); return gerberRE.Matches( ext );
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 );
} }
@ -343,12 +320,6 @@ wxString NetlistFileWildcard()
} }
wxString GerberFileWildcard()
{
return _( "Gerber files" ) + AddFileExtListToFilter( { "pho" } );
}
wxString LegacyPcbFileWildcard() wxString LegacyPcbFileWildcard()
{ {
return _( "KiCad printed circuit board files" ) + AddFileExtListToFilter( { "brd" } ); 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 * Now (2014) Ucamco (the company which manages the Gerber format) encourages use of .gbr
* only and the Gerber X2 file format. * only and the Gerber X2 file format.
*/ */
filetypes = _( "Gerber files (.g* .lgr .pho)" ); filetypes = _( "Gerber files" ) + AddFileExtListToFilter( { "G*", "PHO" } ) + wxT( "|" );
filetypes << wxT( "|" );
filetypes += wxT( "*.g*;*.G*;*.pho;*.PHO" );
filetypes << wxT( "|" );
/* Special gerber filetypes */ /* Special gerber filetypes */
filetypes += _( "Top layer" ) + AddFileExtListToFilter( { "GTL" } ) + wxT( "|" ); filetypes += _( "Top layer" ) + AddFileExtListToFilter( { "GTL" } ) + wxT( "|" );
@ -680,7 +677,7 @@ void GERBVIEW_FRAME::DoWithAcceptedFiles()
for( const wxFileName& file : m_AcceptedFiles ) for( const wxFileName& file : m_AcceptedFiles )
{ {
if( IsExtensionAccepted( file.GetExt(), { ArchiveFileExtension } ) ) if( file.GetExt() == ArchiveFileExtension )
{ {
wxString fn = file.GetFullPath(); wxString fn = file.GetFullPath();
// Open zip archive in editor // Open zip archive in editor
@ -695,5 +692,5 @@ void GERBVIEW_FRAME::DoWithAcceptedFiles()
// Open files in editor // Open files in editor
if( !gerbFn.IsEmpty() ) 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 ); destFile.SetPath( destPath );
} }
if( ext == wxT( "gbr" ) || IsProtelExtension( ext ) ) if( IsGerberFileExtension( ext ) )
{ {
wxString destFileName = destFile.GetName(); wxString destFileName = destFile.GetName();
@ -185,7 +185,7 @@ void IFACE::SaveFileAs( const wxString& aProjectBasePath, const wxString& aProje
KiCopyFile( aSrcFilePath, destFile.GetFullPath(), aErrors ); KiCopyFile( aSrcFilePath, destFile.GetFullPath(), aErrors );
} }
else if( ext == wxT( "gbrjob" ) ) else if( ext == GerberJobFileExtension )
{ {
if( destFile.GetName() == aProjectName + wxT( "-job" ) ) if( destFile.GetName() == aProjectName + wxT( "-job" ) )
destFile.SetName( aNewProjectName + wxT( "-job" ) ); destFile.SetName( aNewProjectName + wxT( "-job" ) );
@ -246,7 +246,7 @@ void IFACE::SaveFileAs( const wxString& aProjectBasePath, const wxString& aProje
aErrors += msg; aErrors += msg;
} }
} }
else if( ext == wxT( "drl" ) ) else if( ext == DrillFileExtension )
{ {
wxString destFileName = destFile.GetName(); 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 m_LayersManager->ReFillRender(); // Update colors in Render after the config is read
// Drag and drop // 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 ); 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 ); m_acceptedExts.emplace( DrillFileExtension, &GERBVIEW_ACTIONS::loadGerbFiles );
DragAcceptFiles( true ); DragAcceptFiles( true );

View File

@ -170,19 +170,9 @@ extern const std::string TextFileExtension;
extern const std::string MarkdownFileExtension; extern const std::string MarkdownFileExtension;
extern const std::string CsvFileExtension; extern const std::string CsvFileExtension;
extern const std::vector<std::string> GerberFileExtensions; extern const wxString GerberFileExtensionsRegex;
extern const wxString GerberFileExtensionWildCard;
/** bool IsGerberFileExtension( const wxString& ext );
* 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 );
/** /**
* @} * @}
@ -217,7 +207,6 @@ extern wxString LegacySchematicFileWildcard();
extern wxString BoardFileWildcard(); extern wxString BoardFileWildcard();
extern wxString OrCadPcb2NetlistFileWildcard(); extern wxString OrCadPcb2NetlistFileWildcard();
extern wxString NetlistFileWildcard(); extern wxString NetlistFileWildcard();
extern wxString GerberFileWildcard();
extern wxString HtmlFileWildcard(); extern wxString HtmlFileWildcard();
extern wxString CsvFileWildcard(); extern wxString CsvFileWildcard();
extern wxString LegacyPcbFileWildcard(); extern wxString LegacyPcbFileWildcard();

View File

@ -231,18 +231,23 @@ KICAD_MANAGER_FRAME::KICAD_MANAGER_FRAME( wxWindow* parent, const wxString& titl
// Init for dropping files // Init for dropping files
m_acceptedExts.emplace( ProjectFileExtension, &KICAD_MANAGER_ACTIONS::loadProject ); m_acceptedExts.emplace( ProjectFileExtension, &KICAD_MANAGER_ACTIONS::loadProject );
m_acceptedExts.emplace( LegacyProjectFileExtension, &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( GerberJobFileExtension, &KICAD_MANAGER_ACTIONS::viewDroppedGerbers );
m_acceptedExts.emplace( DrillFileExtension, &KICAD_MANAGER_ACTIONS::viewDroppedGerbers ); m_acceptedExts.emplace( DrillFileExtension, &KICAD_MANAGER_ACTIONS::viewDroppedGerbers );
// Eagle files import // Eagle files import
m_acceptedExts.emplace( EagleSchematicFileExtension, m_acceptedExts.emplace( EagleSchematicFileExtension,
&KICAD_MANAGER_ACTIONS::importNonKicadProj ); &KICAD_MANAGER_ACTIONS::importNonKicadProj );
m_acceptedExts.emplace( EaglePcbFileExtension, &KICAD_MANAGER_ACTIONS::importNonKicadProj ); m_acceptedExts.emplace( EaglePcbFileExtension, &KICAD_MANAGER_ACTIONS::importNonKicadProj );
// Cadstar files import // Cadstar files import
m_acceptedExts.emplace( CadstarSchematicFileExtension, m_acceptedExts.emplace( CadstarSchematicFileExtension,
&KICAD_MANAGER_ACTIONS::importNonKicadProj ); &KICAD_MANAGER_ACTIONS::importNonKicadProj );
m_acceptedExts.emplace( CadstarPcbFileExtension, &KICAD_MANAGER_ACTIONS::importNonKicadProj ); m_acceptedExts.emplace( CadstarPcbFileExtension, &KICAD_MANAGER_ACTIONS::importNonKicadProj );
DragAcceptFiles( true ); DragAcceptFiles( true );
// Ensure the window is on top // Ensure the window is on top
@ -422,11 +427,13 @@ void KICAD_MANAGER_FRAME::DoWithAcceptedFiles()
// If not, open files in dedicated app. // If not, open files in dedicated app.
for( const wxFileName& fileName : m_AcceptedFiles ) for( const wxFileName& fileName : m_AcceptedFiles )
{ {
if( IsExtensionAccepted( fileName.GetExt(), wxString ext = fileName.GetExt();
{ ProjectFileExtension, LegacyProjectFileExtension } ) )
if( ext == ProjectFileExtension || ext == LegacyProjectFileExtension )
{ {
wxString fn = fileName.GetFullPath(); wxString fn = fileName.GetFullPath();
m_toolManager->RunAction( *m_acceptedExts.at( fileName.GetExt() ), true, &fn ); m_toolManager->RunAction( *m_acceptedExts.at( fileName.GetExt() ), true, &fn );
return; return;
} }
} }
@ -435,13 +442,12 @@ void KICAD_MANAGER_FRAME::DoWithAcceptedFiles()
wxString gerberFiles; wxString gerberFiles;
// Gerbview editor should be able to open Gerber and drill files // 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 ) 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 += wxT( '\"' );
gerberFiles += fileName.GetFullPath() + 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::SEXPR_SCHEMATIC: return KiCadSchematicFileExtension;
case TREE_FILE_TYPE::LEGACY_PCB: return LegacyPcbFileExtension; case TREE_FILE_TYPE::LEGACY_PCB: return LegacyPcbFileExtension;
case TREE_FILE_TYPE::SEXPR_PCB: return KiCadPcbFileExtension; 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::GERBER_JOB_FILE: return GerberJobFileExtension;
case TREE_FILE_TYPE::HTML: return HtmlFileExtension; case TREE_FILE_TYPE::HTML: return HtmlFileExtension;
case TREE_FILE_TYPE::PDF: return PdfFileExtension; case TREE_FILE_TYPE::PDF: return PdfFileExtension;

View File

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