main.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include <iostream>
  2. #include <ostream>
  3. #include <libslic3r/TriangleMesh.hpp>
  4. #include <boost/filesystem.hpp>
  5. void print_arrange_polygons(const std::string &dirpath, std::ostream &out)
  6. {
  7. using namespace Slic3r;
  8. boost::filesystem::path p = dirpath; //"/home/quarky/Workspace/printing/Original-Prusa-i3-MK3/Printed-Parts/stl/";
  9. if (!boost::filesystem::exists(p) || !boost::filesystem::is_directory(p))
  10. return;
  11. for (const auto& entry : boost::filesystem::directory_iterator(p)) {
  12. if (!boost::filesystem::is_regular_file(entry)) {
  13. continue;
  14. }
  15. TriangleMesh mesh;
  16. mesh.ReadSTLFile(entry.path().c_str());
  17. ExPolygons outline = mesh.horizontal_projection();
  18. out << "// " << entry.path().filename() << ": " << std::endl;
  19. for (const ExPolygon &expoly : outline) {
  20. out << "MyPoly{\n"; // Start of polygon
  21. out << "\t{\n"; // Start of contour
  22. for (const auto& point : expoly.contour.points) {
  23. out << " {" << point.x() << ", " << point.y() << "},\n"; // Print point coordinates
  24. }
  25. out << " },\n"; // End of contour
  26. out << " {\n"; // start of holes
  27. for (const auto& hole : expoly.holes) {
  28. out << " {\n"; // Start of hole
  29. for (const auto& point : hole.points) {
  30. out << " {" << point.x() << ", " << point.y() << "},\n"; // Print point coordinates
  31. }
  32. out << " },\n"; // End of hole Polygon
  33. }
  34. out << " }\n"; // end of holes Polygons
  35. out << "},\n"; // End of ExPolygon
  36. }
  37. }
  38. }
  39. void print_arrange_items(const std::string &dirpath, std::ostream &out)
  40. {
  41. using namespace Slic3r;
  42. boost::filesystem::path p = dirpath;
  43. if (!boost::filesystem::exists(p) || !boost::filesystem::is_directory(p))
  44. return;
  45. for (const auto& entry : boost::filesystem::directory_iterator(p)) {
  46. if (!boost::filesystem::is_regular_file(entry)) {
  47. continue;
  48. }
  49. TriangleMesh mesh;
  50. mesh.ReadSTLFile(entry.path().c_str());
  51. ExPolygons outline = mesh.horizontal_projection();
  52. out << "ExPolygons{ " << "// " << entry.path().filename() << ":\n";
  53. for (const ExPolygon &expoly : outline) {
  54. out << " MyPoly{\n"; // Start of polygon
  55. out << " {\n"; // Start of contour
  56. for (const auto& point : expoly.contour.points) {
  57. out << " {" << point.x() << ", " << point.y() << "},\n"; // Print point coordinates
  58. }
  59. out << " },\n"; // End of contour
  60. out << " {\n"; // start of holes
  61. for (const auto& hole : expoly.holes) {
  62. out << " {\n"; // Start of hole
  63. for (const auto& point : hole.points) {
  64. out << " {" << point.x() << ", " << point.y() << "},\n"; // Print point coordinates
  65. }
  66. out << " },\n"; // End of hole Polygon
  67. }
  68. out << " }\n"; // end of holes Polygons
  69. out << " },\n"; // End of ExPolygon
  70. }
  71. out << "},\n";
  72. }
  73. }
  74. int main(int argc, const char *argv[])
  75. {
  76. if (argc <= 1)
  77. return -1;
  78. std::string dirpath = argv[1];
  79. print_arrange_items(dirpath, std::cout);
  80. return 0;
  81. }