| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408 |
- #ifndef MODELIO_H
- #define MODELIO_H
- #include "BaseIO.h"
- #include <BinXCAFDrivers.hxx>
- #include <XCAFApp_Application.hxx>
- #include <TDF_Label.hxx>
- #include <TDF_Tool.hxx>
- #include <TDF_Attribute.hxx>
- #include <TDF_LabelMap.hxx>
- #include <TDF_LabelSequence.hxx>
- #include <IFSelect_ReturnStatus.hxx>
- #include <Interface_Static.hxx>
- #include <Interface_EntityIterator.hxx>
- #include <Interface_Check.hxx>
- #include <Standard_CString.hxx>
- #include <Standard_Boolean.hxx>
- #include <Standard_Integer.hxx>
- #include <Standard_OStream.hxx>
- #include <XCAFDoc_Color.hxx>
- #include <STEPControl_StepModelType.hxx>
- #include <UnitsMethods.hxx>
- #include <Prs3d_Drawer.hxx>
- #include <IGESCAFControl_Reader.hxx>
- #include <IGESCAFControl_Writer.hxx>
- #include <STEPCAFControl_Reader.hxx>
- #include <STEPCAFControl_Writer.hxx>
- #include <RWGltf_CafReader.hxx>
- #include <RWGltf_CafWriter.hxx>
- #include <StlAPI_Writer.hxx>
- #include <StlAPI_Reader.hxx>
- enum GeomType
- {
- GeomSTP,
- GeomIGS,
- GeomSTL,
- GeomBREP,
- GeomGLTF
- };
- class ModelIO : public BaseIO
- {
- public:
- ModelIO() {}
- bool Read(const string &fileName, const GeomType &modelType);
- bool Write(const string &fileName, const GeomType &modelType);
- private:
- bool ReadIGS(const string &fileName);
- bool ReadSTP(const string &fileName);
- bool ReadBREP(const string &filename);
- bool ReadSTL(const string &filename);
- bool ReadGLTF(const string &filename);
- bool WriteSTP(const string &fileName);
- bool WriteIGS(const string &fileName);
- bool WriteBREP(const string &fileName);
- bool WriteSTL(const string &fileName);
- bool WriteGLTF(const string &fileName);
- GeomType modelFormat;
- };
- inline bool ModelIO::Read(const string &fileName, const GeomType &modelType)
- {
- modelFormat = modelType;
- switch (modelType)
- {
- case GeomSTP:
- return ReadSTP(fileName);
- case GeomIGS:
- return ReadIGS(fileName);
- case GeomSTL:
- return ReadSTL(fileName);
- case GeomBREP:
- return ReadBREP(fileName);
- case GeomGLTF:
- return ReadGLTF(fileName);
- default:
- break;
- }
- return false;
- }
- inline bool ModelIO::Write(const string &fileName, const GeomType &modelType)
- {
- switch (modelType)
- {
- case GeomSTP:
- return WriteSTP(fileName);
- case GeomIGS:
- return WriteIGS(fileName);
- case GeomSTL:
- return WriteSTL(fileName);
- case GeomBREP:
- return WriteBREP(fileName);
- case GeomGLTF:
- return WriteGLTF(fileName);
- default:
- break;
- }
- return false;
- }
- inline bool ModelIO::ReadIGS(const string &fileName)
- {
- IGESCAFControl_Reader aIgesReader;
- aIgesReader.SetColorMode(true);
- aIgesReader.SetNameMode(true);
- IFSelect_ReturnStatus status = aIgesReader.ReadFile(fileName.c_str());
- Handle(XCAFApp_Application) anApp = XCAFApp_Application::GetApplication();
- BinXCAFDrivers::DefineFormat(anApp);
- anApp->NewDocument("BinXCAF", doc);
- if (status == IFSelect_RetDone)
- {
- aIgesReader.Transfer(doc);
- return true;
- }
- return false;
- }
- inline bool ModelIO::ReadSTP(const string &fileName)
- {
- Handle(XCAFApp_Application) anApp = XCAFApp_Application::GetApplication();
- BinXCAFDrivers::DefineFormat(anApp);
- anApp->NewDocument("BinXCAF", doc);
- STEPCAFControl_Reader aStepReader;
- aStepReader.SetColorMode(true);
- aStepReader.SetNameMode(true);
- IFSelect_ReturnStatus status = aStepReader.ReadFile(fileName.c_str());
- //--------------
- //TopoDS_Shape shape_Step;
- //STEPControl_Reader aReader_Step = aStepReader.Reader();
- // int j = 1;
- //for (Standard_Integer i = 1; i <= aReader_Step.NbRootsForTransfer(); i++)
- // aReader_Step.TransferRoot(i);
- //for (Standard_Integer i = 1; i <= aReader_Step.NbShapes(); i++)
- // shape_Step = aReader_Step.Shape(i);
- //TopTools_IndexedMapOfShape solids_map, shells_map, faces_map, wires_map, edges_map, vertices_map;
- //TopExp_Explorer exp_solids, exp_shells, exp_faces, exp_wires, exp_edges, exp_vertices;
- //for (exp_faces.Init(shape_Step, TopAbs_FACE); exp_faces.More(); exp_faces.Next())
- //{
- // TopoDS_Face face = TopoDS::Face(exp_faces.Current().Composed(shape_Step.Orientation()));
- // cout << face.HashCode()
- // j++;
- //}
- //--------------
- if (status == IFSelect_RetDone)
- {
- aStepReader.Transfer(doc);
- return true;
- }
- return false;
- }
- inline bool ModelIO::ReadBREP(const string &filename)
- {
- TopoDS_Shape aShape;
- BRep_Builder aBuilder;
- if (!BRepTools::Read(aShape, filename.c_str(), aBuilder))
- {
- return false;
- }
- doc = new TDocStd_Document("BRep");
- Handle(XCAFDoc_ShapeTool) ST = XCAFDoc_DocumentTool::ShapeTool(doc->Main());
- ST->AddShape(aShape);
- return true;
- }
- inline bool ModelIO::ReadSTL(const string &filename)
- {
- TopoDS_Shape aShape;
- StlAPI_Reader anStlReader;
- if (!anStlReader.Read(aShape, filename.c_str()))
- {
- return false;
- }
- doc = new TDocStd_Document("STL");
- Handle(XCAFDoc_ShapeTool) ST = XCAFDoc_DocumentTool::ShapeTool(doc->Main());
- Handle(XCAFDoc_ColorTool) CT = XCAFDoc_DocumentTool::ColorTool(doc->Main());
- Quantity_ColorRGBA aColor(0.644479692f, 0.644479692f, 1.00000000f, 1.00000000f);
- TDF_Label aLabel = ST->AddShape(aShape);
- CT->SetColor(aShape, aColor, XCAFDoc_ColorSurf);
- TDataStd_Name::Set(aLabel, "STL_Shape");
- return true;
- }
- inline bool ModelIO::ReadGLTF(const string &filename)
- {
- Handle(XCAFApp_Application) anApp = XCAFApp_Application::GetApplication();
- BinXCAFDrivers::DefineFormat(anApp);
- anApp->NewDocument("BinXCAF", doc);
- RWGltf_CafReader aReader;
- Standard_Real aSystemUnitFactor = UnitsMethods::GetCasCadeLengthUnit() * 0.001;
- aReader.SetSystemLengthUnit(aSystemUnitFactor);
- aReader.SetSystemCoordinateSystem(RWMesh_CoordinateSystem_Zup);
- aReader.SetDocument(doc);
- aReader.SetParallel(Standard_True);
- Message_ProgressRange theProgress;
- if (aReader.Perform(filename.c_str(), theProgress))
- {
- Handle(XCAFDoc_ColorTool) CT = XCAFDoc_DocumentTool::ColorTool(doc->Main());
- Quantity_ColorRGBA aColor(0.644479692, 0.644479692, 1.00000000, 1.00000000);
- CT->SetColor(doc->Main(), aColor, XCAFDoc_ColorSurf);
- return true;
- }
- else
- {
- return false;
- }
- }
- // ------------------------------------------
- // Write Files
- // ------------------------------------------
- inline bool ModelIO::WriteSTP(const string &fileName)
- {
- if (modelFormat == GeomGLTF)
- {
- return false;
- }
- STEPControl_StepModelType mode = STEPControl_AsIs;
- STEPCAFControl_Writer aWriter;
- aWriter.SetColorMode(true);
- aWriter.SetNameMode(true);
- // Translating document (conversion) to STEP
- if (!aWriter.Transfer(doc, mode)) {
- return false;
- }
- // Writing the File
- IFSelect_ReturnStatus status = aWriter.Write(fileName.c_str());
- // ¼ì²éÊÇ·ñ³É¹¦Ð´Èë
- if (status != IFSelect_RetDone) {
- return false;
- }
- return true;
- }
- inline bool ModelIO::WriteIGS(const string &fileName)
- {
- if (modelFormat == GeomGLTF)
- {
- return false;
- }
- IGESCAFControl_Writer aWriter;
- aWriter.SetColorMode(true);
- aWriter.SetNameMode(true);
- return aWriter.Perform(doc, fileName.c_str());
- }
- inline bool ModelIO::WriteBREP(const string &fileName)
- {
- Handle(XCAFDoc_ShapeTool) aShapeTool = XCAFDoc_DocumentTool::ShapeTool(doc->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))
- {
- aBuildTool.Add(aCompound, aRootShape);
- }
- }
- return BRepTools::Write(aCompound, fileName.c_str());
- }
- inline bool ModelIO::WriteSTL(const string &fileName)
- {
- cout << "WriteSTL " << endl;
- Handle(XCAFDoc_ShapeTool) aShapeTool = XCAFDoc_DocumentTool::ShapeTool(doc->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))
- {
- aBuildTool.Add(aCompound, aRootShape);
- }
- }
- // perform meshing
- Handle(Prs3d_Drawer) aDrawer = new Prs3d_Drawer(); // holds visualization defaults
- BRepMesh_IncrementalMesh anAlgo;
- anAlgo.ChangeParameters().Deflection = 0.2;
- anAlgo.ChangeParameters().Angle = 20.0 * M_PI / 180.0; // 20 degrees
- anAlgo.ChangeParameters().InParallel = true;
- anAlgo.SetShape(aCompound);
- anAlgo.Perform();
-
- StlAPI_Writer anStlWriter;
- //anStlWriter.ASCIIMode() = false;
- cout << "Start Writing STL." << endl;
- if (anStlWriter.Write(aCompound, fileName.c_str()))
- {
- return true;
- }
- return false;
- }
- inline bool ModelIO::WriteGLTF(const string &fileName)
- {
- Handle(XCAFDoc_ShapeTool) aShapeTool = XCAFDoc_DocumentTool::ShapeTool(doc->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))
- {
- aBuildTool.Add(aCompound, aRootShape);
- }
- }
- // perform meshing
- Handle(Prs3d_Drawer) aDrawer = new Prs3d_Drawer(); // holds visualization defaults
- BRepMesh_IncrementalMesh anAlgo;
- anAlgo.ChangeParameters().Deflection = 0.2;
- anAlgo.ChangeParameters().Angle = 20.0 * M_PI / 180.0; // 20 degrees
- anAlgo.ChangeParameters().InParallel = true;
- anAlgo.SetShape(aCompound);
- anAlgo.Perform();
- TColStd_IndexedDataMapOfStringString aMetadata;
- Message_ProgressRange theProgress;
- RWGltf_CafWriter aGltfWriter(fileName.c_str(), true);
- // STEP reader translates into mm units by default
- aGltfWriter.ChangeCoordinateSystemConverter().SetInputLengthUnit(0.001);
- aGltfWriter.ChangeCoordinateSystemConverter().SetInputCoordinateSystem(RWMesh_CoordinateSystem_Zup);
- if (aGltfWriter.Perform(doc, aMetadata, theProgress))
- {
- return true;
- }
- return false;
- }
- #endif
|