From 53b9baa5bad5fb9ef91a140a045a6b028b9b797c Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 18 Nov 2022 09:55:34 +0500 Subject: [PATCH] Some refactoring around Gerber files extension checking. Fixes a case where most Protel inner layer files couldn't be drag and dropped. --- common/eda_base_frame.cpp | 16 +++++------ common/wildcards_and_files_ext.cpp | 41 ++++----------------------- gerbview/files.cpp | 9 ++---- gerbview/gerbview.cpp | 6 ++-- gerbview/gerbview_frame.cpp | 4 +-- include/wildcards_and_files_ext.h | 15 ++-------- kicad/kicad_manager_frame.cpp | 24 ++++++++++------ kicad/project_tree_pane.cpp | 2 +- kicad/tools/kicad_manager_control.cpp | 5 ++-- 9 files changed, 42 insertions(+), 80 deletions(-) diff --git a/common/eda_base_frame.cpp b/common/eda_base_frame.cpp index d2036d6f74..3797435185 100644 --- a/common/eda_base_frame.cpp +++ b/common/eda_base_frame.cpp @@ -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(); diff --git a/common/wildcards_and_files_ext.cpp b/common/wildcards_and_files_ext.cpp index 7c82ae5b9c..fcab884659 100644 --- a/common/wildcards_and_files_ext.cpp +++ b/common/wildcards_and_files_ext.cpp @@ -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 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 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" } ); diff --git a/gerbview/files.cpp b/gerbview/files.cpp index bc3293e0b6..bead1e41b4 100644 --- a/gerbview/files.cpp +++ b/gerbview/files.cpp @@ -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 ); } \ No newline at end of file diff --git a/gerbview/gerbview.cpp b/gerbview/gerbview.cpp index efe89cb30c..37d441e66c 100644 --- a/gerbview/gerbview.cpp +++ b/gerbview/gerbview.cpp @@ -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(); diff --git a/gerbview/gerbview_frame.cpp b/gerbview/gerbview_frame.cpp index a334fbfe67..9418e3416a 100644 --- a/gerbview/gerbview_frame.cpp +++ b/gerbview/gerbview_frame.cpp @@ -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 ); diff --git a/include/wildcards_and_files_ext.h b/include/wildcards_and_files_ext.h index 734dda9aac..38685ae2d1 100644 --- a/include/wildcards_and_files_ext.h +++ b/include/wildcards_and_files_ext.h @@ -170,19 +170,9 @@ extern const std::string TextFileExtension; extern const std::string MarkdownFileExtension; extern const std::string CsvFileExtension; -extern const std::vector 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 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(); diff --git a/kicad/kicad_manager_frame.cpp b/kicad/kicad_manager_frame.cpp index b0f517e98a..7ee8090858 100644 --- a/kicad/kicad_manager_frame.cpp +++ b/kicad/kicad_manager_frame.cpp @@ -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 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( '\"' ); diff --git a/kicad/project_tree_pane.cpp b/kicad/project_tree_pane.cpp index f54161fd7b..a6de4070f1 100644 --- a/kicad/project_tree_pane.cpp +++ b/kicad/project_tree_pane.cpp @@ -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; diff --git a/kicad/tools/kicad_manager_control.cpp b/kicad/tools/kicad_manager_control.cpp index 03da619455..9b9d9a36ee 100644 --- a/kicad/tools/kicad_manager_control.cpp +++ b/kicad/tools/kicad_manager_control.cpp @@ -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,