// 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 #if OCC_VERSION_HEX >= 0x070500 #include #include #endif #endif #include "ReaderGltf.h" #include #include #include using namespace Import; ReaderGltf::ReaderGltf(const Base::FileInfo& file) // NOLINT : file {file} {} void ReaderGltf::read(Handle(TDocStd_Document) hDoc) // NOLINT { #if OCC_VERSION_HEX >= 0x070500 const double unit = 0.001; // mm RWGltf_CafReader aReader; aReader.SetSystemLengthUnit(unit); aReader.SetSystemCoordinateSystem(RWMesh_CoordinateSystem_Zup); aReader.SetDocument(hDoc); aReader.SetParallel(true); TCollection_AsciiString filename(file.filePath().c_str()); Standard_Boolean ret = aReader.Perform(filename, Message_ProgressRange()); if (!ret) { std::stringstream str; str << "Cannot read from file '" << "" << file.filePath() << "'"; } Handle(XCAFDoc_ShapeTool) aShapeTool = XCAFDoc_DocumentTool::ShapeTool(hDoc->Main()); TDF_LabelSequence labels; aShapeTool->GetShapes(labels); for (Standard_Integer i = 1; i <= labels.Length(); i++) { auto label = labels.Value(i); TopoDS_Shape shape = aShapeTool->GetShape(label); if (!shape.IsNull()) { aShapeTool->SetShape(label, fixShape(shape)); } } #else throw Base::RuntimeError("gITF support requires OCCT 7.5.0 or later"); #endif } bool ReaderGltf::cleanup() const { return clean; } void ReaderGltf::setCleanup(bool value) { clean = value; } TopoDS_Shape ReaderGltf::fixShape(TopoDS_Shape shape) // NOLINT { // The glTF reader creates a compound of faces that only contains the triangulation // but not the underlying surfaces. This leads to faces without boundaries. // The triangulation is used to create a valid shape. const double tolerance = 0.5; std::vector points; std::vector facets; Part::TopoShape sh(shape); sh.getFaces(points, facets, tolerance); sh.setFaces(points, facets, tolerance); if (cleanup()) { sh.sewShape(); return sh.removeSplitter(); } return sh.getShape(); }