Mesh: Workaround to load 3mf files not supported by zipios library

This commit is contained in:
wmayer
2024-08-17 19:36:04 +02:00
committed by wwmayer
parent 3595c301b7
commit 8f18cfaf3a
5 changed files with 73 additions and 1 deletions

View File

@@ -393,3 +393,17 @@ std::vector<std::string> Base::Tools::splitSubName(const std::string& subname)
return subNames;
}
// ------------------------------------------------------------------------------------------------
void Base::ZipTools::rewrite(const std::string& source, const std::string& target)
{
Base::PyGILStateLocker lock;
PyObject* module = PyImport_ImportModule("freecad.utils_zip");
if (!module) {
throw Py::Exception();
}
Py::Module commands(module, true);
commands.callMemberFunction("rewrite", Py::TupleN(Py::String(source), Py::String(target)));
}

View File

@@ -329,6 +329,14 @@ struct BaseExport Tools
static std::vector<std::string> splitSubName(const std::string& subname);
};
struct BaseExport ZipTools
{
/**
* @brief rewrite Rewrite a zip file under a new name.
*/
static void rewrite(const std::string& source, const std::string& target);
};
} // namespace Base

View File

@@ -35,6 +35,7 @@ set(EXT_FILES
sketcher.py
UiTools.py
utils.py
utils_zip.py
)
foreach (it ${EXT_FILES})

View File

@@ -0,0 +1,18 @@
# (c) 2024 Werner Mayer LGPL
__author__ = "Werner Mayer"
__url__ = "https://www.freecad.org"
__doc__ = "Helper module to convert zip files"
import zipfile
def rewrite(source: str, target: str):
source_zip = zipfile.ZipFile(source, "r")
target_zip = zipfile.ZipFile(target, "w")
for name in source_zip.namelist():
target_zip.writestr(name, source_zip.open(name).read())
source_zip.close()
target_zip.close()

View File

@@ -108,6 +108,31 @@ struct QUAD
int iV[4];
};
class ZipFixer
{
public:
ZipFixer(const char* filename)
: tmp {Base::FileInfo::getTempFileName()}
{
Base::ZipTools::rewrite(filename, tmp.filePath().c_str());
str.open(tmp, std::ios::in | std::ios::binary);
}
~ZipFixer()
{
tmp.deleteFile();
}
Base::ifstream& getStream()
{
return str;
}
private:
Base::FileInfo tmp;
Base::ifstream str;
};
} // namespace MeshCore
// --------------------------------------------------------------
@@ -227,7 +252,13 @@ bool MeshInput::LoadAny(const char* FileName)
ok = LoadSMF(str);
}
else if (fi.hasExtension("3mf")) {
ok = Load3MF(str);
try {
ok = Load3MF(str);
}
catch (const zipios::FCollException&) {
ZipFixer zip(FileName);
ok = Load3MF(zip.getStream());
}
}
else if (fi.hasExtension("off")) {
ok = LoadOFF(str);