
Wed, 04/27/2022 - 11:35
Convert Stp to gltf crashed:
//
// TL3DModelConverter.cpp
// OpenCVDemo
//
// Created by zoulin on 2022/4/25.
//
#include "TL3DModelConverter.hpp"
#include <STEPCAFControl_Reader.hxx>
#include <STEPCAFControl_Controller.hxx>
#include <XCAFApp_Application.hxx>
#include <TopoDS_Compound.hxx>
#include <TDocStd_Document.hxx>
#include <XCAFDoc_ShapeTool.hxx>
#include <TDF_LabelSequence.hxx>
#include <BRep_Builder.hxx>
#include <TopoDS_Shape.hxx>
// Mesh Methods
#include <Prs3d_Drawer.hxx>
#include <BRepMesh_IncrementalMesh.hxx>
// Gltf Methods
#include <Prs3d.hxx>
#include <Prs3d_Drawer.hxx>
#include <StdPrs_ToolTriangulatedShape.hxx>
#include <TColStd_IndexedDataMapOfStringString.hxx>
#include <RWGltf_CafWriter.hxx>
#include <RWMesh_CoordinateSystem.hxx>
#include <XCAFDoc_DocumentTool.hxx>
#include <iostream>
using namespace std;
int step2glbFormat(const string& in, const string& out)
{
// read / create / fill in the document
Handle(TDocStd_Document) theXdeDoc; // created in advance
if (!theXdeDoc.IsNull()) {
theXdeDoc.Nullify();
}
Handle(XCAFApp_Application) anApp = XCAFApp_Application::GetApplication();
anApp->NewDocument("MDTV-XCAF", theXdeDoc);
STEPCAFControl_Reader aStepReader;
if (aStepReader.ReadFile(in.c_str()) != IFSelect_RetDone) {
// parse error
cout << "Read file parse error." << endl;
return -1;
}
if (!aStepReader.Transfer(theXdeDoc)) {
// translation error
cout << "translation error." << endl;
return -1;
}
if (!XCAFDoc_DocumentTool::IsXCAFDocument(theXdeDoc)) {
//XDE doc error
cout << "XDE doc error." << endl;
return -1;
}
// collect document roots into temporary compound
Handle(XCAFDoc_ShapeTool) aShapeTool = XCAFDoc_DocumentTool::ShapeTool(theXdeDoc->Main());
TDF_LabelSequence aRootLabels;
aShapeTool->GetFreeShapes(aRootLabels);
TopoDS_Compound aCompound;
BRep_Builder aBuildTool;
aBuildTool.MakeCompound(aCompound);
for (TDF_LabelSequence::Iterator aRootIter(aRootLabels); aRootIter.More(); aRootIter.Next())
{
const TDF_Label& aRootLabel = aRootIter.Value();
TopoDS_Shape aRootShape;
if (XCAFDoc_ShapeTool::GetShape(aRootLabel, aRootShape))
{
cout << "Build tool add a compound." << endl;
aBuildTool.Add(aCompound, aRootShape);
}
}
// perform meshing
Handle(Prs3d_Drawer) aDrawer = new Prs3d_Drawer(); // holds visualization defaults
aDrawer->ClearLocalAttributes();
aDrawer->SetupOwnShadingAspect();
BRepMesh_IncrementalMesh anAlgo;
//anAlgo.ChangeParameters().Deflection = Prs3d::GetDeflection(aCompound, aDrawer);
anAlgo.ChangeParameters().Deflection = StdPrs_ToolTriangulatedShape::GetDeflection(aCompound, aDrawer); //crash statement
anAlgo.ChangeParameters().Angle = 20.0 * M_PI / 180.0; // 20 degrees
anAlgo.ChangeParameters().InParallel = true;
anAlgo.SetShape (aCompound);
anAlgo.Perform();
// write or export the document
TColStd_IndexedDataMapOfStringString aMetadata;
RWGltf_CafWriter aGltfWriter(out.c_str(), Standard_True);
// STEP reader translates into mm units by default
aGltfWriter.ChangeCoordinateSystemConverter().SetInputLengthUnit (0.001);
aGltfWriter.ChangeCoordinateSystemConverter().SetInputCoordinateSystem(RWMesh_CoordinateSystem_Zup);
aGltfWriter.SetTransformationFormat(RWGltf_WriterTrsfFormat_Compact);
if (!aGltfWriter.Perform(theXdeDoc, aMetadata, Message_ProgressRange())) {
// export error
std::cout << "export error\n" << endl;
return -1;
}
return 0;
}
how to fix this crash?
Attachments: