|
4 | 4 | #include "shared.hpp" |
5 | 5 | #include <BRep_Builder.hxx> |
6 | 6 | #include <BRepTools.hxx> |
| 7 | +#include <BRepMesh_IncrementalMesh.hxx> |
7 | 8 | #include <emscripten/bind.h> |
8 | 9 | #include <emscripten/val.h> |
9 | 10 | #include <IGESCAFControl_Reader.hxx> |
|
24 | 25 | #include <StlAPI_Writer.hxx> |
25 | 26 | #include <BRepBuilderAPI_MakeSolid.hxx> |
26 | 27 | #include <BRepBuilderAPI_Sewing.hxx> |
| 28 | +#include "utils.hpp" |
27 | 29 |
|
28 | 30 | using namespace emscripten; |
29 | 31 |
|
@@ -295,6 +297,15 @@ class Converter |
295 | 297 | return sewing.SewedShape(); |
296 | 298 | } |
297 | 299 |
|
| 300 | + static void writeBufferToFile(const std::string &fileName, const Uint8Array &buffer) |
| 301 | + { |
| 302 | + std::vector<uint8_t> input = convertJSArrayToNumberVector<uint8_t>(buffer); |
| 303 | + std::ofstream dummyFile; |
| 304 | + dummyFile.open(fileName, std::ios::binary); |
| 305 | + dummyFile.write((char *)input.data(), input.size()); |
| 306 | + dummyFile.close(); |
| 307 | + } |
| 308 | + |
298 | 309 | public: |
299 | 310 | static std::string convertToBrep(const TopoDS_Shape &input) |
300 | 311 | { |
@@ -339,12 +350,8 @@ class Converter |
339 | 350 |
|
340 | 351 | static std::optional<ShapeNode> convertFromIges(const Uint8Array &buffer) |
341 | 352 | { |
342 | | - std::vector<uint8_t> input = convertJSArrayToNumberVector<uint8_t>(buffer); |
343 | 353 | std::string dummyFileName = "temp.igs"; |
344 | | - std::ofstream dummyFile; |
345 | | - dummyFile.open(dummyFileName, std::ios::binary); |
346 | | - dummyFile.write((char *)input.data(), input.size()); |
347 | | - dummyFile.close(); |
| 354 | + writeBufferToFile(dummyFileName, buffer); |
348 | 355 |
|
349 | 356 | IGESCAFControl_Reader igesCafReader; |
350 | 357 | igesCafReader.SetColorMode(true); |
@@ -394,73 +401,44 @@ class Converter |
394 | 401 |
|
395 | 402 | static std::optional<ShapeNode> convertFromStl(const Uint8Array &buffer) |
396 | 403 | { |
397 | | - std::vector<uint8_t> input = convertJSArrayToNumberVector<uint8_t>(buffer); |
398 | 404 | std::string dummyFileName = "temp.stl"; |
399 | | - std::ofstream dummyFile; |
400 | | - dummyFile.open(dummyFileName, std::ios::binary); |
401 | | - dummyFile.write((char *)input.data(), input.size()); |
402 | | - dummyFile.close(); |
| 405 | + writeBufferToFile(dummyFileName, buffer); |
403 | 406 |
|
404 | 407 | StlAPI_Reader stlReader; |
405 | 408 | TopoDS_Shape shape; |
406 | 409 | if (!stlReader.Read(shape, dummyFileName.c_str())) |
407 | 410 | { |
408 | | - std::remove(dummyFileName.c_str()); |
409 | 411 | return std::nullopt; |
410 | 412 | } |
411 | | - std::remove(dummyFileName.c_str()); |
412 | | - |
413 | | - TopoDS_Shape sewedShape = sewShapes({shape}); |
414 | 413 |
|
415 | 414 | ShapeNode node = { |
416 | | - .shape = sewedShape, |
| 415 | + .shape = shape, |
417 | 416 | .color = std::nullopt, |
418 | 417 | .children = {}, |
419 | | - .name = "STL Shape" |
420 | | - }; |
| 418 | + .name = "STL Shape"}; |
421 | 419 |
|
422 | 420 | return node; |
423 | 421 | } |
424 | 422 |
|
425 | | - static std::string convertToStl(const ShapeArray &input) |
| 423 | + static std::string convertToStl(const TopoDS_Shape &shapeToExport) |
426 | 424 | { |
427 | | - auto shapes = vecFromJSArray<TopoDS_Shape>(input); |
428 | | - if (shapes.empty()) |
429 | | - { |
430 | | - return std::string(); |
431 | | - } |
432 | | - |
433 | | - TopoDS_Shape shapeToExport = shapes.size() == 1 ? shapes[0] : sewShapes(shapes); |
434 | | - |
435 | 425 | std::string dummyFileName = "temp_export.stl"; |
| 426 | + auto lineDeflection = boundingBoxRatio(shapeToExport, 0.05); |
| 427 | + BRepMesh_IncrementalMesh mesh(shapeToExport, lineDeflection, true, 0.2, true); |
436 | 428 | StlAPI_Writer stlWriter; |
437 | 429 | if (!stlWriter.Write(shapeToExport, dummyFileName.c_str())) |
438 | 430 | { |
439 | | - std::remove(dummyFileName.c_str()); |
| 431 | + BRepTools::Clean(shapeToExport, true); |
440 | 432 | return std::string(); |
441 | 433 | } |
| 434 | + BRepTools::Clean(shapeToExport, true); |
442 | 435 |
|
443 | | - // Read the generated file content |
444 | | - std::ifstream file(dummyFileName, std::ios::binary | std::ios::ate); |
445 | | - if (!file.is_open()) |
446 | | - { |
447 | | - std::remove(dummyFileName.c_str()); |
448 | | - return std::string(); |
449 | | - } |
450 | | - |
451 | | - std::streamsize size = file.tellg(); |
452 | | - file.seekg(0, std::ios::beg); |
453 | | - |
454 | | - std::string content(size, '\0'); |
455 | | - if (!file.read(content.data(), size)) |
456 | | - { |
457 | | - std::remove(dummyFileName.c_str()); |
458 | | - return std::string(); |
459 | | - } |
460 | | - |
461 | | - file.close(); |
462 | | - std::remove(dummyFileName.c_str()); |
463 | | - return content; |
| 436 | + std::ifstream in(dummyFileName); |
| 437 | + std::istreambuf_iterator<char> beg(in), end; |
| 438 | + std::string str(beg, end); |
| 439 | + in.close(); |
| 440 | + |
| 441 | + return str; |
464 | 442 | } |
465 | 443 | }; |
466 | 444 |
|
|
0 commit comments