From 1d3159c1cbbcf7bd6dc77a0706235d78622c4467 Mon Sep 17 00:00:00 2001 From: Seth Hillbrand Date: Sat, 27 Feb 2021 16:41:14 -0800 Subject: [PATCH] Add support for PKZIP-based stpZ files FreeCAD uses gzip-based stpZ files but many programs will compress using the archive format of PKZIP (e.g. WinZIP). This handles the archive format, taking the first file from the archive, which by the standard should be the STEP file Fixes https://gitlab.com/kicad/code/kicad/issues/5376 --- plugins/3d/oce/loadmodel.cpp | 26 ++++++++++++++++++++++---- utils/kicad2step/pcb/oce_utils.cpp | 30 ++++++++++++++++++++++-------- 2 files changed, 44 insertions(+), 12 deletions(-) diff --git a/plugins/3d/oce/loadmodel.cpp b/plugins/3d/oce/loadmodel.cpp index 335da6e47a..a9d13a792c 100644 --- a/plugins/3d/oce/loadmodel.cpp +++ b/plugins/3d/oce/loadmodel.cpp @@ -33,6 +33,7 @@ #include #include #include +#include #include @@ -473,6 +474,7 @@ bool readSTEPZ( Handle(TDocStd_Document)& m_doc, const char* aFileName ) return false; { + bool success = false; wxFFileOutputStream ofile( outFile.GetFullPath() ); if( !ofile.IsOk() ) @@ -486,17 +488,33 @@ bool readSTEPZ( Handle(TDocStd_Document)& m_doc, const char* aFileName ) try { expanded = gzip::decompress( buffer, size ); + success = true; } catch(...) + {} + + if( expanded.empty() ) { - delete[] buffer; - return false; + ifile.Reset(); + ifile.SeekI( 0 ); + wxZipInputStream izipfile( ifile ); + std::unique_ptr zip_file( izipfile.GetNextEntry() ); + + if( zip_file && !zip_file->IsDir() && izipfile.CanRead() ) + { + izipfile.Read( ofile ); + success = true; + } + } + else + { + ofile.Write( expanded.data(), expanded.size() ); } delete[] buffer; - - ofile.Write( expanded.data(), expanded.size() ); ofile.Close(); + + return success; } bool retval = readSTEP( m_doc, outFile.GetFullPath().mb_str() ); diff --git a/utils/kicad2step/pcb/oce_utils.cpp b/utils/kicad2step/pcb/oce_utils.cpp index f61a9eafa6..e60d40e5b5 100644 --- a/utils/kicad2step/pcb/oce_utils.cpp +++ b/utils/kicad2step/pcb/oce_utils.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #include @@ -1020,14 +1021,11 @@ bool PCBMODEL::getModelLabel( const std::string& aFileName, TRIPLET aScale, TDF_ } { + bool success = false; wxFFileOutputStream ofile( outFile.GetFullPath() ); if( !ofile.IsOk() ) - { - ReportMessage( wxString::Format( "readSTEP() failed on filename %s\n", - outFile.GetFullPath() ) ); return false; - } char *buffer = new char[size]; @@ -1037,17 +1035,33 @@ bool PCBMODEL::getModelLabel( const std::string& aFileName, TRIPLET aScale, TDF_ try { expanded = gzip::decompress( buffer, size ); + success = true; } catch(...) + {} + + if( expanded.empty() ) { - delete[] buffer; - return false; + ifile.Reset(); + ifile.SeekI( 0 ); + wxZipInputStream izipfile( ifile ); + std::unique_ptr zip_file( izipfile.GetNextEntry() ); + + if( zip_file && !zip_file->IsDir() && izipfile.CanRead() ) + { + izipfile.Read( ofile ); + success = true; + } + } + else + { + ofile.Write( expanded.data(), expanded.size() ); } delete[] buffer; - - ofile.Write( expanded.data(), expanded.size() ); ofile.Close(); + + return success; } return getModelLabel( outFile.GetFullPath().ToStdString(), aScale, aLabel );