Skip to content

Commit

Permalink
Mesh: Workaround to load 3mf files not supported by zipios library
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Nov 23, 2024
1 parent 45134ce commit 79e2b53
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/Base/Tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
8 changes: 8 additions & 0 deletions src/Base/Tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions src/Ext/freecad/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ set(EXT_FILES
sketcher.py
UiTools.py
utils.py
utils_zip.py
)

foreach (it ${EXT_FILES})
Expand Down
18 changes: 18 additions & 0 deletions src/Ext/freecad/utils_zip.py
Original file line number Diff line number Diff line change
@@ -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()
33 changes: 32 additions & 1 deletion src/Mod/Mesh/App/Core/MeshIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

// --------------------------------------------------------------
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 79e2b53

Please sign in to comment.