Import: move IGES reader & writer to own classes

This commit is contained in:
wmayer
2023-09-30 23:13:33 +02:00
committed by wwmayer
parent 4092677f24
commit 70ca90f46f
9 changed files with 260 additions and 141 deletions

View File

@@ -30,12 +30,6 @@
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wextra-semi"
#endif
#include <IGESCAFControl_Reader.hxx>
#include <IGESCAFControl_Writer.hxx>
#include <IGESControl_Controller.hxx>
#include <IGESData_GlobalSection.hxx>
#include <IGESData_IGESModel.hxx>
#include <IGESToBRep_Actor.hxx>
#include <Interface_Static.hxx>
#include <OSD_Exception.hxx>
#include <Standard_Version.hxx>
@@ -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<ParameterGrp> 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);

View File

@@ -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

View File

@@ -0,0 +1,85 @@
// SPDX-License-Identifier: LGPL-2.1-or-later
/***************************************************************************
* Copyright (c) 2023 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* 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 *
* <https://www.gnu.org/licenses/>. *
* *
**************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
#include <IGESControl_Controller.hxx>
#include <IGESCAFControl_Reader.hxx>
#include <IGESData_GlobalSection.hxx>
#include <IGESData_IGESModel.hxx>
#include <IGESToBRep_Actor.hxx>
#include <Standard_Version.hxx>
#include <Transfer_TransientProcess.hxx>
#include <XSControl_TransferReader.hxx>
#include <XSControl_WorkSession.hxx>
#endif
#include "ReaderIges.h"
#include <Base/Exception.h>
#include <App/Application.h>
#include <Mod/Part/App/encodeFilename.h>
#include <Mod/Part/App/ProgressIndicator.h>
using namespace Import;
ReaderIges::ReaderIges(const Base::FileInfo& file) // NOLINT
: file {file}
{}
void ReaderIges::read(Handle(TDocStd_Document) hDoc) // NOLINT
{
Base::Reference<ParameterGrp> 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);
}

View File

@@ -0,0 +1,47 @@
// SPDX-License-Identifier: LGPL-2.1-or-later
/***************************************************************************
* Copyright (c) 2023 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* 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 *
* <https://www.gnu.org/licenses/>. *
* *
**************************************************************************/
#ifndef IMPORT_READER_IGES_H
#define IMPORT_READER_IGES_H
#include <Mod/Import/ImportGlobal.h>
#include <Base/FileInfo.h>
#include <TDocStd_Document.hxx>
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

View File

@@ -26,6 +26,7 @@
#ifndef _PreComp_
#include <Standard_Version.hxx>
#include <STEPCAFControl_Reader.hxx>
#include <Transfer_TransientProcess.hxx>
#include <XSControl_TransferReader.hxx>
#include <XSControl_WorkSession.hxx>
#endif

View File

@@ -0,0 +1,60 @@
// SPDX-License-Identifier: LGPL-2.1-or-later
/***************************************************************************
* Copyright (c) 2023 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* 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 *
* <https://www.gnu.org/licenses/>. *
* *
**************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
#include <IGESControl_Controller.hxx>
#include <IGESCAFControl_Writer.hxx>
#include <IGESData_GlobalSection.hxx>
#include <IGESData_IGESModel.hxx>
#include <IGESToBRep_Actor.hxx>
#endif
#include "WriterIges.h"
#include <Base/Exception.h>
#include <App/Application.h>
#include <Mod/Part/App/Interface.h>
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());
}
}

View File

@@ -0,0 +1,47 @@
// SPDX-License-Identifier: LGPL-2.1-or-later
/***************************************************************************
* Copyright (c) 2023 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* 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 *
* <https://www.gnu.org/licenses/>. *
* *
**************************************************************************/
#ifndef IMPORT_WRITER_IGES_H
#define IMPORT_WRITER_IGES_H
#include <Mod/Import/ImportGlobal.h>
#include <Base/FileInfo.h>
#include <TDocStd_Document.hxx>
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

View File

@@ -25,6 +25,7 @@
#include "PreCompiled.h"
#ifndef _PreComp_
#include <APIHeaderSection_MakeHeader.hxx>
#include <NCollection_Vector.hxx>
#include <STEPCAFControl_Writer.hxx>
#endif

View File

@@ -45,12 +45,6 @@
#pragma clang diagnostic ignored "-Wextra-semi"
#endif
#include <IGESCAFControl_Reader.hxx>
#include <IGESCAFControl_Writer.hxx>
#include <IGESControl_Controller.hxx>
#include <IGESData_GlobalSection.hxx>
#include <IGESData_IGESModel.hxx>
#include <IGESToBRep_Actor.hxx>
#include <Interface_Static.hxx>
#include <OSD_Exception.hxx>
#include <Standard_Version.hxx>
@@ -102,8 +96,10 @@
#include <Gui/ViewProviderLink.h>
#include <Mod/Import/App/ImportOCAF2.h>
#include <Mod/Import/App/ReaderGltf.h>
#include <Mod/Import/App/WriterGltf.h>
#include <Mod/Import/App/ReaderIges.h>
#include <Mod/Import/App/ReaderStep.h>
#include <Mod/Import/App/WriterGltf.h>
#include <Mod/Import/App/WriterIges.h>
#include <Mod/Import/App/WriterStep.h>
#include <Mod/Part/App/ImportIges.h>
#include <Mod/Part/App/ImportStep.h>
@@ -464,39 +460,9 @@ private:
}
}
else if (file.hasExtension({"igs", "iges"})) {
Base::Reference<ParameterGrp> 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<ParameterGrp> 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");