ModelIO.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. #ifndef MODELIO_H
  2. #define MODELIO_H
  3. #include "BaseIO.h"
  4. #include <BinXCAFDrivers.hxx>
  5. #include <XCAFApp_Application.hxx>
  6. #include <TDF_Label.hxx>
  7. #include <TDF_Tool.hxx>
  8. #include <TDF_Attribute.hxx>
  9. #include <TDF_LabelMap.hxx>
  10. #include <TDF_LabelSequence.hxx>
  11. #include <IFSelect_ReturnStatus.hxx>
  12. #include <Interface_Static.hxx>
  13. #include <Interface_EntityIterator.hxx>
  14. #include <Interface_Check.hxx>
  15. #include <Standard_CString.hxx>
  16. #include <Standard_Boolean.hxx>
  17. #include <Standard_Integer.hxx>
  18. #include <Standard_OStream.hxx>
  19. #include <XCAFDoc_Color.hxx>
  20. #include <STEPControl_StepModelType.hxx>
  21. #include <UnitsMethods.hxx>
  22. #include <Prs3d_Drawer.hxx>
  23. #include <IGESCAFControl_Reader.hxx>
  24. #include <IGESCAFControl_Writer.hxx>
  25. #include <STEPCAFControl_Reader.hxx>
  26. #include <STEPCAFControl_Writer.hxx>
  27. #include <RWGltf_CafReader.hxx>
  28. #include <RWGltf_CafWriter.hxx>
  29. #include <StlAPI_Writer.hxx>
  30. #include <StlAPI_Reader.hxx>
  31. enum GeomType
  32. {
  33. GeomSTP,
  34. GeomIGS,
  35. GeomSTL,
  36. GeomBREP,
  37. GeomGLTF
  38. };
  39. class ModelIO : public BaseIO
  40. {
  41. public:
  42. ModelIO() {}
  43. bool Read(const string &fileName, const GeomType &modelType);
  44. bool Write(const string &fileName, const GeomType &modelType);
  45. private:
  46. bool ReadIGS(const string &fileName);
  47. bool ReadSTP(const string &fileName);
  48. bool ReadBREP(const string &filename);
  49. bool ReadSTL(const string &filename);
  50. bool ReadGLTF(const string &filename);
  51. bool WriteSTP(const string &fileName);
  52. bool WriteIGS(const string &fileName);
  53. bool WriteBREP(const string &fileName);
  54. bool WriteSTL(const string &fileName);
  55. bool WriteGLTF(const string &fileName);
  56. GeomType modelFormat;
  57. };
  58. inline bool ModelIO::Read(const string &fileName, const GeomType &modelType)
  59. {
  60. modelFormat = modelType;
  61. switch (modelType)
  62. {
  63. case GeomSTP:
  64. return ReadSTP(fileName);
  65. case GeomIGS:
  66. return ReadIGS(fileName);
  67. case GeomSTL:
  68. return ReadSTL(fileName);
  69. case GeomBREP:
  70. return ReadBREP(fileName);
  71. case GeomGLTF:
  72. return ReadGLTF(fileName);
  73. default:
  74. break;
  75. }
  76. return false;
  77. }
  78. inline bool ModelIO::Write(const string &fileName, const GeomType &modelType)
  79. {
  80. switch (modelType)
  81. {
  82. case GeomSTP:
  83. return WriteSTP(fileName);
  84. case GeomIGS:
  85. return WriteIGS(fileName);
  86. case GeomSTL:
  87. return WriteSTL(fileName);
  88. case GeomBREP:
  89. return WriteBREP(fileName);
  90. case GeomGLTF:
  91. return WriteGLTF(fileName);
  92. default:
  93. break;
  94. }
  95. return false;
  96. }
  97. inline bool ModelIO::ReadIGS(const string &fileName)
  98. {
  99. IGESCAFControl_Reader aIgesReader;
  100. aIgesReader.SetColorMode(true);
  101. aIgesReader.SetNameMode(true);
  102. IFSelect_ReturnStatus status = aIgesReader.ReadFile(fileName.c_str());
  103. Handle(XCAFApp_Application) anApp = XCAFApp_Application::GetApplication();
  104. BinXCAFDrivers::DefineFormat(anApp);
  105. anApp->NewDocument("BinXCAF", doc);
  106. if (status == IFSelect_RetDone)
  107. {
  108. aIgesReader.Transfer(doc);
  109. return true;
  110. }
  111. return false;
  112. }
  113. inline bool ModelIO::ReadSTP(const string &fileName)
  114. {
  115. Handle(XCAFApp_Application) anApp = XCAFApp_Application::GetApplication();
  116. BinXCAFDrivers::DefineFormat(anApp);
  117. anApp->NewDocument("BinXCAF", doc);
  118. STEPCAFControl_Reader aStepReader;
  119. aStepReader.SetColorMode(true);
  120. aStepReader.SetNameMode(true);
  121. IFSelect_ReturnStatus status = aStepReader.ReadFile(fileName.c_str());
  122. //--------------
  123. //TopoDS_Shape shape_Step;
  124. //STEPControl_Reader aReader_Step = aStepReader.Reader();
  125. // int j = 1;
  126. //for (Standard_Integer i = 1; i <= aReader_Step.NbRootsForTransfer(); i++)
  127. // aReader_Step.TransferRoot(i);
  128. //for (Standard_Integer i = 1; i <= aReader_Step.NbShapes(); i++)
  129. // shape_Step = aReader_Step.Shape(i);
  130. //TopTools_IndexedMapOfShape solids_map, shells_map, faces_map, wires_map, edges_map, vertices_map;
  131. //TopExp_Explorer exp_solids, exp_shells, exp_faces, exp_wires, exp_edges, exp_vertices;
  132. //for (exp_faces.Init(shape_Step, TopAbs_FACE); exp_faces.More(); exp_faces.Next())
  133. //{
  134. // TopoDS_Face face = TopoDS::Face(exp_faces.Current().Composed(shape_Step.Orientation()));
  135. // cout << face.HashCode()
  136. // j++;
  137. //}
  138. //--------------
  139. if (status == IFSelect_RetDone)
  140. {
  141. aStepReader.Transfer(doc);
  142. return true;
  143. }
  144. return false;
  145. }
  146. inline bool ModelIO::ReadBREP(const string &filename)
  147. {
  148. TopoDS_Shape aShape;
  149. BRep_Builder aBuilder;
  150. if (!BRepTools::Read(aShape, filename.c_str(), aBuilder))
  151. {
  152. return false;
  153. }
  154. doc = new TDocStd_Document("BRep");
  155. Handle(XCAFDoc_ShapeTool) ST = XCAFDoc_DocumentTool::ShapeTool(doc->Main());
  156. ST->AddShape(aShape);
  157. return true;
  158. }
  159. inline bool ModelIO::ReadSTL(const string &filename)
  160. {
  161. TopoDS_Shape aShape;
  162. StlAPI_Reader anStlReader;
  163. if (!anStlReader.Read(aShape, filename.c_str()))
  164. {
  165. return false;
  166. }
  167. doc = new TDocStd_Document("STL");
  168. Handle(XCAFDoc_ShapeTool) ST = XCAFDoc_DocumentTool::ShapeTool(doc->Main());
  169. Handle(XCAFDoc_ColorTool) CT = XCAFDoc_DocumentTool::ColorTool(doc->Main());
  170. Quantity_ColorRGBA aColor(0.644479692f, 0.644479692f, 1.00000000f, 1.00000000f);
  171. TDF_Label aLabel = ST->AddShape(aShape);
  172. CT->SetColor(aShape, aColor, XCAFDoc_ColorSurf);
  173. TDataStd_Name::Set(aLabel, "STL_Shape");
  174. return true;
  175. }
  176. inline bool ModelIO::ReadGLTF(const string &filename)
  177. {
  178. Handle(XCAFApp_Application) anApp = XCAFApp_Application::GetApplication();
  179. BinXCAFDrivers::DefineFormat(anApp);
  180. anApp->NewDocument("BinXCAF", doc);
  181. RWGltf_CafReader aReader;
  182. Standard_Real aSystemUnitFactor = UnitsMethods::GetCasCadeLengthUnit() * 0.001;
  183. aReader.SetSystemLengthUnit(aSystemUnitFactor);
  184. aReader.SetSystemCoordinateSystem(RWMesh_CoordinateSystem_Zup);
  185. aReader.SetDocument(doc);
  186. aReader.SetParallel(Standard_True);
  187. Message_ProgressRange theProgress;
  188. if (aReader.Perform(filename.c_str(), theProgress))
  189. {
  190. Handle(XCAFDoc_ColorTool) CT = XCAFDoc_DocumentTool::ColorTool(doc->Main());
  191. Quantity_ColorRGBA aColor(0.644479692, 0.644479692, 1.00000000, 1.00000000);
  192. CT->SetColor(doc->Main(), aColor, XCAFDoc_ColorSurf);
  193. return true;
  194. }
  195. else
  196. {
  197. return false;
  198. }
  199. }
  200. // ------------------------------------------
  201. // Write Files
  202. // ------------------------------------------
  203. inline bool ModelIO::WriteSTP(const string &fileName)
  204. {
  205. if (modelFormat == GeomGLTF)
  206. {
  207. return false;
  208. }
  209. STEPControl_StepModelType mode = STEPControl_AsIs;
  210. STEPCAFControl_Writer aWriter;
  211. aWriter.SetColorMode(true);
  212. aWriter.SetNameMode(true);
  213. // Translating document (conversion) to STEP
  214. if (!aWriter.Transfer(doc, mode)) {
  215. return false;
  216. }
  217. // Writing the File
  218. IFSelect_ReturnStatus status = aWriter.Write(fileName.c_str());
  219. // ¼ì²éÊÇ·ñ³É¹¦Ð´Èë
  220. if (status != IFSelect_RetDone) {
  221. return false;
  222. }
  223. return true;
  224. }
  225. inline bool ModelIO::WriteIGS(const string &fileName)
  226. {
  227. if (modelFormat == GeomGLTF)
  228. {
  229. return false;
  230. }
  231. IGESCAFControl_Writer aWriter;
  232. aWriter.SetColorMode(true);
  233. aWriter.SetNameMode(true);
  234. return aWriter.Perform(doc, fileName.c_str());
  235. }
  236. inline bool ModelIO::WriteBREP(const string &fileName)
  237. {
  238. Handle(XCAFDoc_ShapeTool) aShapeTool = XCAFDoc_DocumentTool::ShapeTool(doc->Main());
  239. TDF_LabelSequence aRootLabels;
  240. aShapeTool->GetFreeShapes(aRootLabels);
  241. TopoDS_Compound aCompound;
  242. BRep_Builder aBuildTool;
  243. aBuildTool.MakeCompound(aCompound);
  244. for (TDF_LabelSequence::Iterator aRootIter(aRootLabels); aRootIter.More(); aRootIter.Next())
  245. {
  246. const TDF_Label& aRootLabel = aRootIter.Value();
  247. TopoDS_Shape aRootShape;
  248. if (XCAFDoc_ShapeTool::GetShape(aRootLabel, aRootShape))
  249. {
  250. aBuildTool.Add(aCompound, aRootShape);
  251. }
  252. }
  253. return BRepTools::Write(aCompound, fileName.c_str());
  254. }
  255. inline bool ModelIO::WriteSTL(const string &fileName)
  256. {
  257. cout << "WriteSTL " << endl;
  258. Handle(XCAFDoc_ShapeTool) aShapeTool = XCAFDoc_DocumentTool::ShapeTool(doc->Main());
  259. TDF_LabelSequence aRootLabels;
  260. aShapeTool->GetFreeShapes(aRootLabels);
  261. TopoDS_Compound aCompound;
  262. BRep_Builder aBuildTool;
  263. aBuildTool.MakeCompound(aCompound);
  264. for (TDF_LabelSequence::Iterator aRootIter(aRootLabels); aRootIter.More(); aRootIter.Next())
  265. {
  266. const TDF_Label& aRootLabel = aRootIter.Value();
  267. TopoDS_Shape aRootShape;
  268. if (XCAFDoc_ShapeTool::GetShape(aRootLabel, aRootShape))
  269. {
  270. aBuildTool.Add(aCompound, aRootShape);
  271. }
  272. }
  273. // perform meshing
  274. Handle(Prs3d_Drawer) aDrawer = new Prs3d_Drawer(); // holds visualization defaults
  275. BRepMesh_IncrementalMesh anAlgo;
  276. anAlgo.ChangeParameters().Deflection = 0.2;
  277. anAlgo.ChangeParameters().Angle = 20.0 * M_PI / 180.0; // 20 degrees
  278. anAlgo.ChangeParameters().InParallel = true;
  279. anAlgo.SetShape(aCompound);
  280. anAlgo.Perform();
  281. StlAPI_Writer anStlWriter;
  282. //anStlWriter.ASCIIMode() = false;
  283. cout << "Start Writing STL." << endl;
  284. if (anStlWriter.Write(aCompound, fileName.c_str()))
  285. {
  286. return true;
  287. }
  288. return false;
  289. }
  290. inline bool ModelIO::WriteGLTF(const string &fileName)
  291. {
  292. Handle(XCAFDoc_ShapeTool) aShapeTool = XCAFDoc_DocumentTool::ShapeTool(doc->Main());
  293. TDF_LabelSequence aRootLabels;
  294. aShapeTool->GetFreeShapes(aRootLabels);
  295. TopoDS_Compound aCompound;
  296. BRep_Builder aBuildTool;
  297. aBuildTool.MakeCompound(aCompound);
  298. for (TDF_LabelSequence::Iterator aRootIter(aRootLabels); aRootIter.More(); aRootIter.Next())
  299. {
  300. const TDF_Label& aRootLabel = aRootIter.Value();
  301. TopoDS_Shape aRootShape;
  302. if (XCAFDoc_ShapeTool::GetShape(aRootLabel, aRootShape))
  303. {
  304. aBuildTool.Add(aCompound, aRootShape);
  305. }
  306. }
  307. // perform meshing
  308. Handle(Prs3d_Drawer) aDrawer = new Prs3d_Drawer(); // holds visualization defaults
  309. BRepMesh_IncrementalMesh anAlgo;
  310. anAlgo.ChangeParameters().Deflection = 0.2;
  311. anAlgo.ChangeParameters().Angle = 20.0 * M_PI / 180.0; // 20 degrees
  312. anAlgo.ChangeParameters().InParallel = true;
  313. anAlgo.SetShape(aCompound);
  314. anAlgo.Perform();
  315. TColStd_IndexedDataMapOfStringString aMetadata;
  316. Message_ProgressRange theProgress;
  317. RWGltf_CafWriter aGltfWriter(fileName.c_str(), true);
  318. // STEP reader translates into mm units by default
  319. aGltfWriter.ChangeCoordinateSystemConverter().SetInputLengthUnit(0.001);
  320. aGltfWriter.ChangeCoordinateSystemConverter().SetInputCoordinateSystem(RWMesh_CoordinateSystem_Zup);
  321. if (aGltfWriter.Perform(doc, aMetadata, theProgress))
  322. {
  323. return true;
  324. }
  325. return false;
  326. }
  327. #endif