diff --git a/src/Mod/Import/App/AppImportPy.cpp b/src/Mod/Import/App/AppImportPy.cpp index a5ad694a30..61edca60d2 100644 --- a/src/Mod/Import/App/AppImportPy.cpp +++ b/src/Mod/Import/App/AppImportPy.cpp @@ -30,7 +30,6 @@ #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wextra-semi" #endif -#include #include #include #include @@ -39,8 +38,6 @@ #include #include #include -#include -#include #include #include #include @@ -74,7 +71,9 @@ #include "ImportOCAF2.h" #include "ReaderGltf.h" +#include "ReaderStep.h" #include "WriterGltf.h" +#include "WriterStep.h" namespace Import { @@ -177,25 +176,8 @@ private: if (file.hasExtension({"stp", "step"})) { try { - STEPCAFControl_Reader aReader; - 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 STEP file"); - } - -#if OCC_VERSION_HEX < 0x070500 - Handle(Message_ProgressIndicator) pi = new Part::ProgressIndicator(100); - aReader.Reader().WS()->MapReader()->SetProgress(pi); - pi->NewScope(100, "Reading STEP file..."); - pi->Show(); -#endif - aReader.Transfer(hDoc); -#if OCC_VERSION_HEX < 0x070500 - pi->EndScope(); -#endif + Import::ReaderStep reader(file); + reader.read(hDoc); } catch (OSD_Exception& e) { Base::Console().Error("%s\n", e.GetMessageString()); @@ -389,35 +371,8 @@ private: Base::FileInfo file(Utf8Name.c_str()); if (file.hasExtension({"stp", "step"})) { - STEPCAFControl_Writer writer; - Part::Interface::writeStepAssembly(Part::Interface::Assembly::On); - writer.Transfer(hDoc, STEPControl_AsIs); - - APIHeaderSection_MakeHeader makeHeader(writer.ChangeWriter().Model()); - Base::Reference hGrp = App::GetApplication() - .GetUserParameter() - .GetGroup("BaseApp") - ->GetGroup("Preferences") - ->GetGroup("Mod/Part") - ->GetGroup("STEP"); - - // Don't set name because STEP doesn't support UTF-8 - // https://forum.freecad.org/viewtopic.php?f=8&t=52967 - makeHeader.SetAuthorValue( - 1, - new TCollection_HAsciiString(hGrp->GetASCII("Author", "Author").c_str())); - makeHeader.SetOrganizationValue( - 1, - new TCollection_HAsciiString(hGrp->GetASCII("Company").c_str())); - makeHeader.SetOriginatingSystem( - new TCollection_HAsciiString(App::Application::getExecutableName().c_str())); - makeHeader.SetDescriptionValue(1, new TCollection_HAsciiString("FreeCAD Model")); - IFSelect_ReturnStatus ret = writer.Write(name8bit.c_str()); - if (ret == IFSelect_RetError || ret == IFSelect_RetFail - || ret == IFSelect_RetStop) { - PyErr_Format(PyExc_IOError, "Cannot open file '%s'", Utf8Name.c_str()); - throw Py::Exception(); - } + Import::WriterStep writer(file); + writer.write(hDoc); } else if (file.hasExtension({"igs", "iges"})) { IGESControl_Controller::Init(); diff --git a/src/Mod/Import/App/CMakeLists.txt b/src/Mod/Import/App/CMakeLists.txt index ded2ae2886..fa8516da9a 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 + ReaderStep.cpp + ReaderStep.h StepShapePy.xml StepShape.h StepShape.cpp @@ -45,6 +47,8 @@ SET(Import_SRCS PreCompiled.h WriterGltf.cpp WriterGltf.h + WriterStep.cpp + WriterStep.h dxf/ImpExpDxf.cpp dxf/ImpExpDxf.h dxf/dxf.cpp diff --git a/src/Mod/Import/App/ReaderStep.cpp b/src/Mod/Import/App/ReaderStep.cpp new file mode 100644 index 0000000000..bacab3e608 --- /dev/null +++ b/src/Mod/Import/App/ReaderStep.cpp @@ -0,0 +1,67 @@ +// 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 +#endif + +#include "ReaderStep.h" +#include +#include +#include + +using namespace Import; + +ReaderStep::ReaderStep(const Base::FileInfo& file) // NOLINT + : file {file} +{} + +void ReaderStep::read(Handle(TDocStd_Document) hDoc) // NOLINT +{ + std::string utf8Name = file.filePath(); + std::string name8bit = Part::encodeFilename(utf8Name); + STEPCAFControl_Reader aReader; + aReader.SetColorMode(true); + aReader.SetNameMode(true); + aReader.SetLayerMode(true); + aReader.SetSHUOMode(true); + if (aReader.ReadFile(name8bit.c_str()) != IFSelect_RetDone) { + throw Base::FileException("Cannot read STEP file"); + } + +#if OCC_VERSION_HEX < 0x070500 + Handle(Message_ProgressIndicator) pi = new Part::ProgressIndicator(100); + aReader.Reader().WS()->MapReader()->SetProgress(pi); + pi->NewScope(100, "Reading STEP file..."); + pi->Show(); +#endif + aReader.Transfer(hDoc); +#if OCC_VERSION_HEX < 0x070500 + pi->EndScope(); +#endif +} diff --git a/src/Mod/Import/App/ReaderStep.h b/src/Mod/Import/App/ReaderStep.h new file mode 100644 index 0000000000..febd1760a3 --- /dev/null +++ b/src/Mod/Import/App/ReaderStep.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_STEP_H +#define IMPORT_READER_STEP_H + +#include +#include +#include + +namespace Import +{ + +class ImportExport ReaderStep +{ +public: + explicit ReaderStep(const Base::FileInfo& file); + + void read(Handle(TDocStd_Document) hDoc); + +private: + Base::FileInfo file; +}; + +} // namespace Import + +#endif // IMPORT_READER_STEP_H diff --git a/src/Mod/Import/App/WriterStep.cpp b/src/Mod/Import/App/WriterStep.cpp new file mode 100644 index 0000000000..3a05ec6c22 --- /dev/null +++ b/src/Mod/Import/App/WriterStep.cpp @@ -0,0 +1,72 @@ +// 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 +#endif + +#include "WriterStep.h" +#include +#include +#include + +using namespace Import; + +WriterStep::WriterStep(const Base::FileInfo& file) // NOLINT + : file {file} +{} + +void WriterStep::write(Handle(TDocStd_Document) hDoc) const // NOLINT +{ + STEPCAFControl_Writer writer; + Part::Interface::writeStepAssembly(Part::Interface::Assembly::On); + writer.Transfer(hDoc, STEPControl_AsIs); + + APIHeaderSection_MakeHeader makeHeader(writer.ChangeWriter().Model()); + Base::Reference hGrp = App::GetApplication() + .GetUserParameter() + .GetGroup("BaseApp") + ->GetGroup("Preferences") + ->GetGroup("Mod/Part") + ->GetGroup("STEP"); + + // Don't set name because STEP doesn't support UTF-8 + // https://forum.freecad.org/viewtopic.php?f=8&t=52967 + makeHeader.SetAuthorValue( + 1, + new TCollection_HAsciiString(hGrp->GetASCII("Author", "Author").c_str())); + makeHeader.SetOrganizationValue( + 1, + new TCollection_HAsciiString(hGrp->GetASCII("Company").c_str())); + makeHeader.SetOriginatingSystem( + new TCollection_HAsciiString(App::Application::getExecutableName().c_str())); + makeHeader.SetDescriptionValue(1, new TCollection_HAsciiString("FreeCAD Model")); + IFSelect_ReturnStatus ret = writer.Write(name8bit.c_str()); + if (ret == IFSelect_RetError || ret == IFSelect_RetFail || ret == IFSelect_RetStop) { + std::string utf8Name = file.filePath(); + throw Base::FileException("Cannot open file '%s'", utf8Name.c_str()); + } +} diff --git a/src/Mod/Import/App/WriterStep.h b/src/Mod/Import/App/WriterStep.h new file mode 100644 index 0000000000..b709855d1b --- /dev/null +++ b/src/Mod/Import/App/WriterStep.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_STEP_H +#define IMPORT_WRITER_STEP_H + +#include +#include +#include + +namespace Import +{ + +class ImportExport WriterStep +{ +public: + WriterStep(const Base::FileInfo& file); + + void write(Handle(TDocStd_Document) hDoc) const; + +private: + std::string name8bit; + Base::FileInfo file; +}; +} // namespace Import + +#endif // IMPORT_WRITER_GLTF_H diff --git a/src/Mod/Import/Gui/AppImportGuiPy.cpp b/src/Mod/Import/Gui/AppImportGuiPy.cpp index 0228cdb37f..ce44a8b9d9 100644 --- a/src/Mod/Import/Gui/AppImportGuiPy.cpp +++ b/src/Mod/Import/Gui/AppImportGuiPy.cpp @@ -45,7 +45,6 @@ #pragma clang diagnostic ignored "-Wextra-semi" #endif -#include #include #include #include @@ -54,8 +53,6 @@ #include #include #include -#include -#include #include #include #include @@ -106,6 +103,8 @@ #include #include #include +#include +#include #include #include #include @@ -453,25 +452,8 @@ private: } try { - STEPCAFControl_Reader aReader; - aReader.SetColorMode(true); - aReader.SetNameMode(true); - aReader.SetLayerMode(true); - aReader.SetSHUOMode(true); - if (aReader.ReadFile((const char*)name8bit.c_str()) != IFSelect_RetDone) { - throw Py::Exception(PyExc_IOError, "cannot read STEP file"); - } - -#if OCC_VERSION_HEX < 0x070500 - Handle(Message_ProgressIndicator) pi = new Part::ProgressIndicator(100); - aReader.Reader().WS()->MapReader()->SetProgress(pi); - pi->NewScope(100, "Reading STEP file..."); - pi->Show(); -#endif - aReader.Transfer(hDoc); -#if OCC_VERSION_HEX < 0x070500 - pi->EndScope(); -#endif + Import::ReaderStep reader(file); + reader.read(hDoc); } catch (OSD_Exception& e) { Base::Console().Error("%s\n", e.GetMessageString()); @@ -716,46 +698,16 @@ private: Base::FileInfo file(Utf8Name.c_str()); if (file.hasExtension({"stp", "step"})) { - ParameterGrp::handle hGrp_stp = App::GetApplication().GetParameterGroupByPath( + ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath( "User parameter:BaseApp/Preferences/Mod/Part/STEP"); - std::string scheme = - hGrp_stp->GetASCII("Scheme", Part::Interface::writeStepScheme()); + std::string scheme = hGrp->GetASCII("Scheme", Part::Interface::writeStepScheme()); std::list supported = Part::supportedSTEPSchemes(); if (std::find(supported.begin(), supported.end(), scheme) != supported.end()) { Part::Interface::writeStepScheme(scheme.c_str()); } - STEPCAFControl_Writer writer; - Part::Interface::writeStepAssembly(Part::Interface::Assembly::On); - writer.Transfer(hDoc, STEPControl_AsIs); - - // edit STEP header - APIHeaderSection_MakeHeader makeHeader(writer.ChangeWriter().Model()); - - Base::Reference hGrp = App::GetApplication() - .GetUserParameter() - .GetGroup("BaseApp") - ->GetGroup("Preferences") - ->GetGroup("Mod/Part") - ->GetGroup("STEP"); - - // Don't set name because STEP doesn't support UTF-8 - // https://forum.freecad.org/viewtopic.php?f=8&t=52967 - makeHeader.SetAuthorValue( - 1, - new TCollection_HAsciiString(hGrp->GetASCII("Author", "Author").c_str())); - makeHeader.SetOrganizationValue( - 1, - new TCollection_HAsciiString(hGrp->GetASCII("Company").c_str())); - makeHeader.SetOriginatingSystem( - new TCollection_HAsciiString(App::Application::getExecutableName().c_str())); - makeHeader.SetDescriptionValue(1, new TCollection_HAsciiString("FreeCAD Model")); - IFSelect_ReturnStatus ret = writer.Write(name8bit.c_str()); - if (ret == IFSelect_RetError || ret == IFSelect_RetFail - || ret == IFSelect_RetStop) { - PyErr_Format(PyExc_IOError, "Cannot open file '%s'", Utf8Name.c_str()); - throw Py::Exception(); - } + Import::WriterStep writer(file); + writer.write(hDoc); } else if (file.hasExtension({"igs", "iges"})) { IGESControl_Controller::Init(); @@ -807,25 +759,8 @@ private: hApp->NewDocument(TCollection_ExtendedString("MDTV-CAF"), hDoc); if (file.hasExtension({"stp", "step"})) { - STEPCAFControl_Reader aReader; - aReader.SetColorMode(true); - aReader.SetNameMode(true); - aReader.SetLayerMode(true); - aReader.SetSHUOMode(true); - if (aReader.ReadFile((Standard_CString)Name) != IFSelect_RetDone) { - throw Py::Exception(PyExc_IOError, "cannot read STEP file"); - } - -#if OCC_VERSION_HEX < 0x070500 - Handle(Message_ProgressIndicator) pi = new Part::ProgressIndicator(100); - aReader.Reader().WS()->MapReader()->SetProgress(pi); - pi->NewScope(100, "Reading STEP file..."); - pi->Show(); -#endif - aReader.Transfer(hDoc); -#if OCC_VERSION_HEX < 0x070500 - pi->EndScope(); -#endif + Import::ReaderStep reader(file); + reader.read(hDoc); } else if (file.hasExtension({"igs", "iges"})) { Base::Reference hGrp = App::GetApplication()