From 75b313be2889acb72069ec42d39ad7df086a9a9c Mon Sep 17 00:00:00 2001 From: wmayer Date: Sat, 30 Sep 2023 23:13:33 +0200 Subject: [PATCH] Import: move IGES reader & writer to own classes --- src/Mod/Import/App/AppImportPy.cpp | 61 ++--------------- src/Mod/Import/App/CMakeLists.txt | 4 ++ src/Mod/Import/App/ReaderIges.cpp | 85 ++++++++++++++++++++++++ src/Mod/Import/App/ReaderIges.h | 47 +++++++++++++ src/Mod/Import/App/ReaderStep.cpp | 1 + src/Mod/Import/App/WriterIges.cpp | 60 +++++++++++++++++ src/Mod/Import/App/WriterIges.h | 47 +++++++++++++ src/Mod/Import/App/WriterStep.cpp | 1 + src/Mod/Import/Gui/AppImportGuiPy.cpp | 95 +++------------------------ 9 files changed, 260 insertions(+), 141 deletions(-) create mode 100644 src/Mod/Import/App/ReaderIges.cpp create mode 100644 src/Mod/Import/App/ReaderIges.h create mode 100644 src/Mod/Import/App/WriterIges.cpp create mode 100644 src/Mod/Import/App/WriterIges.h diff --git a/src/Mod/Import/App/AppImportPy.cpp b/src/Mod/Import/App/AppImportPy.cpp index 61edca60d2..1769b4fa22 100644 --- a/src/Mod/Import/App/AppImportPy.cpp +++ b/src/Mod/Import/App/AppImportPy.cpp @@ -30,12 +30,6 @@ #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wextra-semi" #endif -#include -#include -#include -#include -#include -#include #include #include #include @@ -71,8 +65,10 @@ #include "ImportOCAF2.h" #include "ReaderGltf.h" +#include "ReaderIges.h" #include "ReaderStep.h" #include "WriterGltf.h" +#include "WriterIges.h" #include "WriterStep.h" namespace Import @@ -188,40 +184,9 @@ private: } } else if (file.hasExtension({"igs", "iges"})) { - Base::Reference hGrp = App::GetApplication() - .GetUserParameter() - .GetGroup("BaseApp") - ->GetGroup("Preferences") - ->GetGroup("Mod/Part") - ->GetGroup("IGES"); - try { - IGESControl_Controller::Init(); - IGESCAFControl_Reader aReader; - // http://www.opencascade.org/org/forum/thread_20603/?forum=3 - aReader.SetReadVisible( - hGrp->GetBool("SkipBlankEntities", true) ? Standard_True : Standard_False); - aReader.SetColorMode(true); - aReader.SetNameMode(true); - aReader.SetLayerMode(true); - if (aReader.ReadFile((Standard_CString)(name8bit.c_str())) - != IFSelect_RetDone) { - throw Py::Exception(PyExc_IOError, "cannot read IGES file"); - } - -#if OCC_VERSION_HEX < 0x070500 - Handle(Message_ProgressIndicator) pi = new Part::ProgressIndicator(100); - aReader.WS()->MapReader()->SetProgress(pi); - pi->NewScope(100, "Reading IGES file..."); - pi->Show(); -#endif - aReader.Transfer(hDoc); -#if OCC_VERSION_HEX < 0x070500 - pi->EndScope(); -#endif - // http://opencascade.blogspot.de/2009/03/unnoticeable-memory-leaks-part-2.html - Handle(IGESToBRep_Actor)::DownCast(aReader.WS()->TransferReader()->Actor()) - ->SetModel(new IGESData_IGESModel); + Import::ReaderIges reader(file); + reader.read(hDoc); } catch (OSD_Exception& e) { Base::Console().Error("%s\n", e.GetMessageString()); @@ -375,22 +340,8 @@ private: writer.write(hDoc); } else if (file.hasExtension({"igs", "iges"})) { - IGESControl_Controller::Init(); - IGESCAFControl_Writer writer; - IGESData_GlobalSection header = writer.Model()->GlobalSection(); - header.SetAuthorName( - new TCollection_HAsciiString(Part::Interface::writeIgesHeaderAuthor())); - header.SetCompanyName( - new TCollection_HAsciiString(Part::Interface::writeIgesHeaderCompany())); - header.SetSendName( - new TCollection_HAsciiString(Part::Interface::writeIgesHeaderProduct())); - writer.Model()->SetGlobalSection(header); - writer.Transfer(hDoc); - Standard_Boolean ret = writer.Write(name8bit.c_str()); - if (!ret) { - PyErr_Format(PyExc_IOError, "Cannot open file '%s'", Utf8Name.c_str()); - throw Py::Exception(); - } + Import::WriterIges writer(file); + writer.write(hDoc); } else if (file.hasExtension({"glb", "gltf"})) { Import::WriterGltf writer(name8bit, file); diff --git a/src/Mod/Import/App/CMakeLists.txt b/src/Mod/Import/App/CMakeLists.txt index fa8516da9a..2d31ab038b 100644 --- a/src/Mod/Import/App/CMakeLists.txt +++ b/src/Mod/Import/App/CMakeLists.txt @@ -37,6 +37,8 @@ SET(Import_SRCS #ImportOCAFAssembly.h ReaderGltf.cpp ReaderGltf.h + ReaderIges.cpp + ReaderIges.h ReaderStep.cpp ReaderStep.h StepShapePy.xml @@ -47,6 +49,8 @@ SET(Import_SRCS PreCompiled.h WriterGltf.cpp WriterGltf.h + WriterIges.cpp + WriterIges.h WriterStep.cpp WriterStep.h dxf/ImpExpDxf.cpp diff --git a/src/Mod/Import/App/ReaderIges.cpp b/src/Mod/Import/App/ReaderIges.cpp new file mode 100644 index 0000000000..6f0373eb4c --- /dev/null +++ b/src/Mod/Import/App/ReaderIges.cpp @@ -0,0 +1,85 @@ +// SPDX-License-Identifier: LGPL-2.1-or-later + +/*************************************************************************** + * Copyright (c) 2023 Werner Mayer * + * * + * This file is part of FreeCAD. * + * * + * FreeCAD is free software: you can redistribute it and/or modify it * + * under the terms of the GNU Lesser General Public License as * + * published by the Free Software Foundation, either version 2.1 of the * + * License, or (at your option) any later version. * + * * + * FreeCAD is distributed in the hope that it will be useful, but * + * WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with FreeCAD. If not, see * + * . * + * * + **************************************************************************/ + + +#include "PreCompiled.h" +#ifndef _PreComp_ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#endif + +#include "ReaderIges.h" +#include +#include +#include +#include + +using namespace Import; + +ReaderIges::ReaderIges(const Base::FileInfo& file) // NOLINT + : file {file} +{} + +void ReaderIges::read(Handle(TDocStd_Document) hDoc) // NOLINT +{ + Base::Reference hGrp = App::GetApplication() + .GetUserParameter() + .GetGroup("BaseApp") + ->GetGroup("Preferences") + ->GetGroup("Mod/Part") + ->GetGroup("IGES"); + std::string utf8Name = file.filePath(); + std::string name8bit = Part::encodeFilename(utf8Name); + + IGESControl_Controller::Init(); + IGESCAFControl_Reader aReader; + // http://www.opencascade.org/org/forum/thread_20603/?forum=3 + aReader.SetReadVisible(hGrp->GetBool("SkipBlankEntities", true)); + aReader.SetColorMode(true); + aReader.SetNameMode(true); + aReader.SetLayerMode(true); + if (aReader.ReadFile(name8bit.c_str()) != IFSelect_RetDone) { + throw Base::FileException("cannot read IGES file", utf8Name.c_str()); + } + +#if OCC_VERSION_HEX < 0x070500 + Handle(Message_ProgressIndicator) pi = new Part::ProgressIndicator(100); + aReader.WS()->MapReader()->SetProgress(pi); + pi->NewScope(100, "Reading IGES file..."); + pi->Show(); +#endif + aReader.Transfer(hDoc); +#if OCC_VERSION_HEX < 0x070500 + pi->EndScope(); +#endif + // http://opencascade.blogspot.de/2009/03/unnoticeable-memory-leaks-part-2.html + Handle(IGESToBRep_Actor)::DownCast(aReader.WS()->TransferReader()->Actor()) + ->SetModel(new IGESData_IGESModel); +} diff --git a/src/Mod/Import/App/ReaderIges.h b/src/Mod/Import/App/ReaderIges.h new file mode 100644 index 0000000000..7e6cddf37b --- /dev/null +++ b/src/Mod/Import/App/ReaderIges.h @@ -0,0 +1,47 @@ +// SPDX-License-Identifier: LGPL-2.1-or-later + +/*************************************************************************** + * Copyright (c) 2023 Werner Mayer * + * * + * This file is part of FreeCAD. * + * * + * FreeCAD is free software: you can redistribute it and/or modify it * + * under the terms of the GNU Lesser General Public License as * + * published by the Free Software Foundation, either version 2.1 of the * + * License, or (at your option) any later version. * + * * + * FreeCAD is distributed in the hope that it will be useful, but * + * WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with FreeCAD. If not, see * + * . * + * * + **************************************************************************/ + +#ifndef IMPORT_READER_IGES_H +#define IMPORT_READER_IGES_H + +#include +#include +#include + +namespace Import +{ + +class ImportExport ReaderIges +{ +public: + explicit ReaderIges(const Base::FileInfo& file); + + void read(Handle(TDocStd_Document) hDoc); + +private: + Base::FileInfo file; +}; + +} // namespace Import + +#endif // IMPORT_READER_IGES_H diff --git a/src/Mod/Import/App/ReaderStep.cpp b/src/Mod/Import/App/ReaderStep.cpp index bacab3e608..3fb24a5dab 100644 --- a/src/Mod/Import/App/ReaderStep.cpp +++ b/src/Mod/Import/App/ReaderStep.cpp @@ -26,6 +26,7 @@ #ifndef _PreComp_ #include #include +#include #include #include #endif diff --git a/src/Mod/Import/App/WriterIges.cpp b/src/Mod/Import/App/WriterIges.cpp new file mode 100644 index 0000000000..d77ca1e161 --- /dev/null +++ b/src/Mod/Import/App/WriterIges.cpp @@ -0,0 +1,60 @@ +// SPDX-License-Identifier: LGPL-2.1-or-later + +/*************************************************************************** + * Copyright (c) 2023 Werner Mayer * + * * + * This file is part of FreeCAD. * + * * + * FreeCAD is free software: you can redistribute it and/or modify it * + * under the terms of the GNU Lesser General Public License as * + * published by the Free Software Foundation, either version 2.1 of the * + * License, or (at your option) any later version. * + * * + * FreeCAD is distributed in the hope that it will be useful, but * + * WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with FreeCAD. If not, see * + * . * + * * + **************************************************************************/ + + +#include "PreCompiled.h" +#ifndef _PreComp_ +#include +#include +#include +#include +#include +#endif + +#include "WriterIges.h" +#include +#include +#include + +using namespace Import; + +WriterIges::WriterIges(const Base::FileInfo& file) // NOLINT + : file {file} +{} + +void WriterIges::write(Handle(TDocStd_Document) hDoc) const // NOLINT +{ + IGESControl_Controller::Init(); + IGESCAFControl_Writer writer; + IGESData_GlobalSection header = writer.Model()->GlobalSection(); + header.SetAuthorName(new TCollection_HAsciiString(Part::Interface::writeIgesHeaderAuthor())); + header.SetCompanyName(new TCollection_HAsciiString(Part::Interface::writeIgesHeaderCompany())); + header.SetSendName(new TCollection_HAsciiString(Part::Interface::writeIgesHeaderProduct())); + writer.Model()->SetGlobalSection(header); + writer.Transfer(hDoc); + Standard_Boolean ret = writer.Write(name8bit.c_str()); + if (!ret) { + std::string utf8Name = file.filePath(); + throw Base::FileException("Cannot open file '%s'", utf8Name.c_str()); + } +} diff --git a/src/Mod/Import/App/WriterIges.h b/src/Mod/Import/App/WriterIges.h new file mode 100644 index 0000000000..082d0455b0 --- /dev/null +++ b/src/Mod/Import/App/WriterIges.h @@ -0,0 +1,47 @@ +// SPDX-License-Identifier: LGPL-2.1-or-later + +/*************************************************************************** + * Copyright (c) 2023 Werner Mayer * + * * + * This file is part of FreeCAD. * + * * + * FreeCAD is free software: you can redistribute it and/or modify it * + * under the terms of the GNU Lesser General Public License as * + * published by the Free Software Foundation, either version 2.1 of the * + * License, or (at your option) any later version. * + * * + * FreeCAD is distributed in the hope that it will be useful, but * + * WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with FreeCAD. If not, see * + * . * + * * + **************************************************************************/ + +#ifndef IMPORT_WRITER_IGES_H +#define IMPORT_WRITER_IGES_H + +#include +#include +#include + +namespace Import +{ + +class ImportExport WriterIges +{ +public: + WriterIges(const Base::FileInfo& file); + + void write(Handle(TDocStd_Document) hDoc) const; + +private: + std::string name8bit; + Base::FileInfo file; +}; +} // namespace Import + +#endif // IMPORT_WRITER_IGES_H diff --git a/src/Mod/Import/App/WriterStep.cpp b/src/Mod/Import/App/WriterStep.cpp index 3a05ec6c22..e5e94add37 100644 --- a/src/Mod/Import/App/WriterStep.cpp +++ b/src/Mod/Import/App/WriterStep.cpp @@ -25,6 +25,7 @@ #include "PreCompiled.h" #ifndef _PreComp_ #include +#include #include #endif diff --git a/src/Mod/Import/Gui/AppImportGuiPy.cpp b/src/Mod/Import/Gui/AppImportGuiPy.cpp index ce44a8b9d9..266b858c46 100644 --- a/src/Mod/Import/Gui/AppImportGuiPy.cpp +++ b/src/Mod/Import/Gui/AppImportGuiPy.cpp @@ -45,12 +45,6 @@ #pragma clang diagnostic ignored "-Wextra-semi" #endif -#include -#include -#include -#include -#include -#include #include #include #include @@ -102,8 +96,10 @@ #include #include #include -#include +#include #include +#include +#include #include #include #include @@ -464,39 +460,9 @@ private: } } else if (file.hasExtension({"igs", "iges"})) { - Base::Reference hGrp = App::GetApplication() - .GetUserParameter() - .GetGroup("BaseApp") - ->GetGroup("Preferences") - ->GetGroup("Mod/Part") - ->GetGroup("IGES"); - try { - IGESControl_Controller::Init(); - IGESCAFControl_Reader aReader; - // http://www.opencascade.org/org/forum/thread_20603/?forum=3 - aReader.SetReadVisible( - hGrp->GetBool("SkipBlankEntities", true) ? Standard_True : Standard_False); - aReader.SetColorMode(true); - aReader.SetNameMode(true); - aReader.SetLayerMode(true); - if (aReader.ReadFile((const char*)name8bit.c_str()) != IFSelect_RetDone) { - throw Py::Exception(PyExc_IOError, "cannot read IGES file"); - } - -#if OCC_VERSION_HEX < 0x070500 - Handle(Message_ProgressIndicator) pi = new Part::ProgressIndicator(100); - aReader.WS()->MapReader()->SetProgress(pi); - pi->NewScope(100, "Reading IGES file..."); - pi->Show(); -#endif - aReader.Transfer(hDoc); -#if OCC_VERSION_HEX < 0x070500 - pi->EndScope(); -#endif - // http://opencascade.blogspot.de/2009/03/unnoticeable-memory-leaks-part-2.html - Handle(IGESToBRep_Actor)::DownCast(aReader.WS()->TransferReader()->Actor()) - ->SetModel(new IGESData_IGESModel); + Import::ReaderIges reader(file); + reader.read(hDoc); } catch (OSD_Exception& e) { Base::Console().Error("%s\n", e.GetMessageString()); @@ -710,22 +676,8 @@ private: writer.write(hDoc); } else if (file.hasExtension({"igs", "iges"})) { - IGESControl_Controller::Init(); - IGESCAFControl_Writer writer; - IGESData_GlobalSection header = writer.Model()->GlobalSection(); - header.SetAuthorName( - new TCollection_HAsciiString(Part::Interface::writeIgesHeaderAuthor())); - header.SetCompanyName( - new TCollection_HAsciiString(Part::Interface::writeIgesHeaderCompany())); - header.SetSendName( - new TCollection_HAsciiString(Part::Interface::writeIgesHeaderProduct())); - writer.Model()->SetGlobalSection(header); - writer.Transfer(hDoc); - Standard_Boolean ret = writer.Write((const char*)name8bit.c_str()); - if (!ret) { - PyErr_Format(PyExc_IOError, "Cannot open file '%s'", Utf8Name.c_str()); - throw Py::Exception(); - } + Import::WriterIges writer(file); + writer.write(hDoc); } else if (file.hasExtension({"glb", "gltf"})) { Import::WriterGltf writer(name8bit, file); @@ -763,37 +715,8 @@ private: reader.read(hDoc); } else if (file.hasExtension({"igs", "iges"})) { - Base::Reference hGrp = App::GetApplication() - .GetUserParameter() - .GetGroup("BaseApp") - ->GetGroup("Preferences") - ->GetGroup("Mod/Part") - ->GetGroup("IGES"); - IGESControl_Controller::Init(); - IGESCAFControl_Reader aReader; - // http://www.opencascade.org/org/forum/thread_20603/?forum=3 - aReader.SetReadVisible(hGrp->GetBool("SkipBlankEntities", true) ? Standard_True - : Standard_False); - aReader.SetColorMode(true); - aReader.SetNameMode(true); - aReader.SetLayerMode(true); - if (aReader.ReadFile((Standard_CString)Name) != IFSelect_RetDone) { - throw Py::Exception(PyExc_IOError, "cannot read IGES file"); - } - -#if OCC_VERSION_HEX < 0x070500 - Handle(Message_ProgressIndicator) pi = new Part::ProgressIndicator(100); - aReader.WS()->MapReader()->SetProgress(pi); - pi->NewScope(100, "Reading IGES file..."); - pi->Show(); -#endif - aReader.Transfer(hDoc); -#if OCC_VERSION_HEX < 0x070500 - pi->EndScope(); -#endif - // http://opencascade.blogspot.de/2009/03/unnoticeable-memory-leaks-part-2.html - Handle(IGESToBRep_Actor)::DownCast(aReader.WS()->TransferReader()->Actor()) - ->SetModel(new IGESData_IGESModel); + Import::ReaderIges reader(file); + reader.read(hDoc); } else { throw Py::Exception(PyExc_IOError, "no supported file format");