js_ext_comx_cascade_core.cc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #include <comx_napi.hxx>
  2. #include <type_cast.hxx>
  3. using namespace KMAS::type;
  4. #include <map>
  5. #include <set>
  6. #include <string>
  7. #include <vector>
  8. using namespace std;
  9. #include <base/function.hxx>
  10. #include <base/type_cast.hxx>
  11. using namespace KMAS::type;
  12. // Open Cascade library.
  13. #include <BRepBuilderAPI_MakeEdge.hxx>
  14. #include <BRepBuilderAPI_MakeFace.hxx>
  15. #include <BRepBuilderAPI_MakeWire.hxx>
  16. #include <BRepPrimAPI_MakeBox.hxx>
  17. #include <BRepPrimAPI_MakeCone.hxx>
  18. #include <BRepPrimAPI_MakeCylinder.hxx>
  19. #include <BRepPrimApI_MakeSphere.hxx>
  20. #include <BRepTools.hxx>
  21. #include <BRep_ListIteratorOfListOfCurveRepresentation.hxx>
  22. #include <BRep_Tool.hxx>
  23. #include <Brep_TEdge.hxx>
  24. #include <Standard.hxx>
  25. #include <Standard_DefineAlloc.hxx>
  26. #include <Standard_Handle.hxx>
  27. #include <Standard_TypeDef.hxx>
  28. #include <TopTools_HSequenceOfShape.hxx>
  29. #include <TopTools_ListIteratorOfListOfShape.hxx>
  30. #include <TopoDS.hxx>
  31. #include <TopoDS_Edge.hxx>
  32. #include <TopoDS_Face.hxx>
  33. #include <TopoDS_Wire.hxx>
  34. #include <gp_Pln.hxx>
  35. #include <gp_Pnt.hxx>
  36. // #include <BRepMesh.hxx>
  37. #include <BRepMesh_IncrementalMesh.hxx>
  38. #include <Poly_Triangulation.hxx>
  39. #include <TShort_Array1OfShortReal.hxx>
  40. #include <TopExp.hxx>
  41. #include <TopExp_Explorer.hxx>
  42. #include <Poly.hxx>
  43. #include <Poly_Connect.hxx>
  44. #include <Poly_Triangulation.hxx>
  45. #include <Geom2dAdaptor.hxx>
  46. #include <Geom2dAdaptor_curve.hxx>
  47. #include <GeomAdaptor.hxx>
  48. #include <GeomAdaptor_curve.hxx>
  49. #include <BRepBuilderAPI_NurbsConvert.hxx>
  50. #include <Geom2dConvert.hxx>
  51. #include <Geom2d_TrimmedCurve.hxx>
  52. #include <Geom_Plane.hxx>
  53. #include <BRepLib_FindSurface.hxx>
  54. #include <GeomConvert.hxx>
  55. #include <Geom_BSplineSurface.hxx>
  56. #include <BRepLib.hxx>
  57. #include <GeomLib.hxx>
  58. #include <IGESCAFControl_Reader.hxx>
  59. #include <Quantity_Color.hxx>
  60. #include <STEPCAFControl_Reader.hxx>
  61. #include <TDF_ChildIterator.hxx>
  62. #include <TDF_Label.hxx>
  63. #include <TDocStd_Document.hxx>
  64. #include <XCAFApp_Application.hxx>
  65. #include <XCAFDoc_ColorTool.hxx>
  66. #include <XCAFDoc_ColorType.hxx>
  67. #include <XCAFDoc_DocumentTool.hxx>
  68. #include <XCAFDoc_ShapeTool.hxx>
  69. #include <ModelIO/ModelIO.h>
  70. TopTools_HSequenceOfShape
  71. g_aHSequenceOfShape; // new TopTools_HSequenceOfShape();
  72. TopTools_IndexedMapOfShape g_mapFace;
  73. TopTools_IndexedMapOfShape g_mapEdge;
  74. NCollection_IndexedDataMap<TopoDS_Shape, Quantity_Color> aShapeGroup;
  75. ModelIO g_modelIO;
  76. ////////////////////////////////////////////////////////////////////////////
  77. // system pre-define segment, please don't modify the following codes.
  78. JS_EXT_DATA_DECLARE()
  79. ///////////////////////////////////////////////////////////////////////////
  80. // implement GetExEntry function
  81. #define GETENTRYEX_FUNC_USAGE \
  82. "GetEntryEx Usage: var exEntry = comx.occore.GetEntryEx();"
  83. JS_EXT_FUNC_BEGIN(GetEntryEx, 0, GETENTRYEX_FUNC_USAGE) {
  84. unsigned long long ulRet = (unsigned long long)((void *)&g_modelIO);
  85. string ret_val = type_cast<string>(ulRet);
  86. JS_EXT_FUNC_ASSIGN_RET(ret_val);
  87. }
  88. JS_EXT_FUNC_END()
  89. ///////////////////////////////////////////////////////////////////////////
  90. // implement GetEntry function
  91. #define GETENTRY_FUNC_USAGE \
  92. "GetEntry Usage: var entry = comx.occore.GetEntry();"
  93. JS_EXT_FUNC_BEGIN(GetEntry, 0, GETENTRY_FUNC_USAGE) {
  94. // para_type p0 = JS_EXT_PARA(para_type, 0);
  95. unsigned long long ulRet =
  96. (unsigned long long)((void *)&g_aHSequenceOfShape);
  97. string ret_val = type_cast<string>(ulRet);
  98. JS_EXT_FUNC_ASSIGN_RET(ret_val);
  99. }
  100. JS_EXT_FUNC_END()
  101. ///////////////////////////////////////////////////////////////////////////
  102. // implement IsEmpty function
  103. #define ISEMPTY_FUNC_USAGE \
  104. "IsEmpty Usage: var is_empty = comx.occore.IsEmpty();"
  105. JS_EXT_FUNC_BEGIN(IsEmpty, 0, ISEMPTY_FUNC_USAGE) {
  106. // para_type p0 = JS_EXT_PARA(para_type, 0);
  107. string ret_val = type_cast<string>(g_aHSequenceOfShape.IsEmpty());
  108. JS_EXT_FUNC_ASSIGN_RET(ret_val);
  109. }
  110. JS_EXT_FUNC_END()
  111. ///////////////////////////////////////////////////////////////////////////
  112. // implement Clear function
  113. #define CLEAR_FUNC_USAGE "Clear Usage: comx.occore.Clear();"
  114. JS_EXT_FUNC_BEGIN(Clear, 0, CLEAR_FUNC_USAGE) {
  115. // para_type p0 = JS_EXT_PARA(para_type, 0);
  116. g_aHSequenceOfShape.Clear();
  117. string ret_val = type_cast<string>("undefined");
  118. JS_EXT_FUNC_ASSIGN_RET(ret_val);
  119. }
  120. JS_EXT_FUNC_END()
  121. ///////////////////////////////////////////////////////////////////////////
  122. // implement GetBrepInfo function
  123. #define GetBrepInfo_FUNC_USAGE \
  124. "GetBrepInfo Usage: var res = comx.occore.GetBrepInfo(face_id);"
  125. JS_EXT_FUNC_BEGIN(GetBrepInfo, 1, GetBrepInfo_FUNC_USAGE) {
  126. Standard_Integer id = JS_EXT_PARA(Standard_Integer, 0);
  127. stringstream sstr;
  128. if (id > g_mapFace.Extent()) {
  129. const TopoDS_Shape &objEdge =
  130. g_mapEdge.FindKey(id - g_mapFace.Extent());
  131. BRepTools::Write(objEdge, sstr);
  132. } else {
  133. const TopoDS_Shape &objFace = g_mapFace.FindKey(id);
  134. /*const TopoDS_Face &F = TopoDS::Face(objFace);
  135. Handle(Geom_Surface) S = BRep_Tool::Surface(F);
  136. Handle(Geom_BSplineSurface) BSurface =
  137. GeomConvert::SurfaceToBSplineSurface(S); BRep_Builder builder;
  138. TopoDS_Face Face;
  139. builder.MakeFace(Face, BSurface, 0.1);*/
  140. TopoDS_Face F = TopoDS::Face(objFace);
  141. BRepBuilderAPI_NurbsConvert nurbs(F);
  142. Handle(Geom_Surface) geom_extrusion =
  143. BRepLib_FindSurface(nurbs).Surface();
  144. Handle(Geom_BSplineSurface) geombspline_extrusion =
  145. GeomConvert::SurfaceToBSplineSurface(geom_extrusion);
  146. BRep_Builder builder;
  147. TopoDS_Face Face;
  148. builder.MakeFace(Face, geombspline_extrusion,
  149. Precision::Confusion());
  150. BRepTools::Write(/*objFace*/ Face, sstr);
  151. for (TopExp_Explorer edgeExp(objFace, TopAbs_EDGE);
  152. edgeExp.More(); edgeExp.Next()) {
  153. const TopoDS_Shape &objEdge = edgeExp.Current();
  154. /*const TopoDS_Edge E = TopoDS::Edge(objEdge);
  155. BRepBuilderAPI_NurbsConvert nurbs(E);
  156. Handle(Geom_Curve) geom_extrusion = nurbs->*/
  157. BRepTools::Write(objEdge, sstr);
  158. }
  159. }
  160. string ret_val = type_cast<string>(sstr.str());
  161. JS_EXT_FUNC_ASSIGN_RET(ret_val);
  162. }
  163. JS_EXT_FUNC_END()
  164. // 0x4cc0c89c-0x0090-0x47e3-0xb7-0x63-0xc1-0xaf-0xf1-0xe7-0x99-0x4e
  165. // please don't modify or delete the previous line codes.
  166. ////////////////////////////////////////////////////////////////////////////
  167. // please set your js.ext namespace in the following codes.
  168. #define JS_EXT_NS "comx.occore"
  169. ////////////////////////////////////////////////////////////////////////////
  170. // entry segment, please replace your function name in the following codes.
  171. JS_EXT_ENTRY_BEGIN()
  172. JS_EXT_ENTRY(GetEntryEx)
  173. JS_EXT_ENTRY(GetEntry)
  174. JS_EXT_ENTRY(IsEmpty)
  175. JS_EXT_ENTRY(Clear)
  176. JS_EXT_ENTRY(GetBrepInfo)
  177. JS_EXT_ENTRY_END()
  178. JS_EXT_MAIN_BEGIN(JS_EXT_NS, 5)
  179. JS_EXT_FUNC_REG(GetEntryEx)
  180. JS_EXT_FUNC_REG(GetEntry)
  181. JS_EXT_FUNC_REG(IsEmpty)
  182. JS_EXT_FUNC_REG(Clear)
  183. JS_EXT_FUNC_REG(GetBrepInfo)
  184. JS_EXT_MAIN_END()