3 #include "WasmOcctView.h"
6 #include <Message_Messenger.hxx>
7 #include <Message_PrinterSystemLog.hxx>
8 #include <OSD_MemInfo.hxx>
9 #include <OSD_Parallel.hxx>
11 #include <AIS_Shape.hxx>
12 #include <BRepTools.hxx>
13 #include <BRep_Builder.hxx>
14 #include <Standard_ArrayStreamBuffer.hxx>
16 #include <emscripten.h>
17 #include <emscripten/html5.h>
19 //! Global viewer instance.
20 static WasmOcctView aViewer;
22 //! File data read event.
23 extern "C" void onFileDataRead (void* theOpaque, void* theBuffer, int theDataLen)
25 const char* aName = theOpaque != NULL ? (const char* )theOpaque : "";
27 AIS_ListOfInteractive aShapes;
28 aViewer.Context()->DisplayedObjects (AIS_KOI_Shape, -1, aShapes);
29 for (AIS_ListOfInteractive::Iterator aShapeIter (aShapes); aShapeIter.More(); aShapeIter.Next())
31 aViewer.Context()->Remove (aShapeIter.Value(), false);
35 Standard_ArrayStreamBuffer aStreamBuffer ((const char* )theBuffer, theDataLen);
36 std::istream aStream (&aStreamBuffer);
38 BRep_Builder aBuilder;
39 BRepTools::Read (aShape, aStream, aBuilder);
41 Handle(AIS_Shape) aShapePrs = new AIS_Shape (aShape);
42 aShapePrs->SetMaterial (Graphic3d_NOM_SILVER);
43 aViewer.Context()->Display (aShapePrs, AIS_Shaded, 0, false);
44 aViewer.View()->FitAll (0.01, false);
45 aViewer.View()->Redraw();
46 Message::DefaultMessenger()->Send (TCollection_AsciiString("Loaded file ") + aName, Message_Info);
47 Message::DefaultMessenger()->Send (OSD_MemInfo::PrintInfo(), Message_Trace);
50 //! File read error event.
51 static void onFileReadFailed (void* theOpaque)
53 const char* aName = (const char* )theOpaque;
54 Message::DefaultMessenger()->Send (TCollection_AsciiString("Error: unable to load file ") + aName, Message_Fail);
59 Message::DefaultMessenger()->Printers().First()->SetTraceLevel (Message_Trace);
60 Handle(Message_PrinterSystemLog) aJSConsolePrinter = new Message_PrinterSystemLog ("webgl-sample", Message_Trace);
61 Message::DefaultMessenger()->AddPrinter (aJSConsolePrinter); // open JavaScript console within the Browser to see this output
62 Message::DefaultMessenger()->Send (TCollection_AsciiString("NbLogicalProcessors: ") + OSD_Parallel::NbLogicalProcessors(), Message_Trace);
64 Message::DefaultMessenger()->Send (OSD_MemInfo::PrintInfo(), Message_Trace);
67 emscripten_async_wget_data ("samples/Ball.brep", (void* )"samples/Ball.brep", onFileDataRead, onFileReadFailed);