Skip to content

Commit

Permalink
Resolved Issues With Corrupted STL Files When Exporting to stdout (op…
Browse files Browse the repository at this point in the history
…enscad#5470)

---------

Co-authored-by: Marius Kintel <[email protected]>
  • Loading branch information
IamLRBA and kintel authored Dec 4, 2024
1 parent fc3aa54 commit c190dac
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 19 deletions.
49 changes: 30 additions & 19 deletions src/io/export_stl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -293,29 +293,40 @@ void export_stl(const std::shared_ptr<const Geometry>& geom, std::ostream& outpu
{
// FIXME: In lazy union mode, should we export multiple solids?
if (binary) {
std::ostringstream buffer; // Using a memory buffer
char header[80] = "OpenSCAD Model\n";
output.write(header, sizeof(header));
char tmp_triangle_count[4] = {0, 0, 0, 0}; // We must fill this in below.
output.write(tmp_triangle_count, 4);
} else {
setlocale(LC_NUMERIC, "C"); // Ensure radix is . (not ,) in output
output << "solid OpenSCAD_Model\n";
}
buffer.write(header, sizeof(header));

// Placeholder for triangle count
uint32_t triangle_count = 0;
char tmp_triangle_count[4] = {0, 0, 0, 0};
buffer.write(tmp_triangle_count, 4);

// Writing triangles and counting them
triangle_count = append_stl(geom, buffer, binary);

if (triangle_count > 4294967295) {
LOG(message_group::Export_Error, "Triangle count exceeded 4294967295, so the STL file is not valid");
}

uint64_t triangle_count = append_stl(geom, output, binary);
// Updating the triangle count in the buffer
char triangle_count_bytes[4] = {
static_cast<char>(triangle_count & 0xff),
static_cast<char>((triangle_count >> 8) & 0xff),
static_cast<char>((triangle_count >> 16) & 0xff),
static_cast<char>((triangle_count >> 24) & 0xff)};
buffer.seekp(80, std::ios_base::beg);
buffer.write(triangle_count_bytes, 4);

// Flushing the buffer to the output stream
output << buffer.str();

if (binary) {
// Fill in triangle count.
output.seekp(80, std::ios_base::beg);
output.put(triangle_count & 0xff);
output.put((triangle_count >> 8) & 0xff);
output.put((triangle_count >> 16) & 0xff);
output.put((triangle_count >> 24) & 0xff);
if (triangle_count > 4294967295) {
LOG(message_group::Export_Error, "Triangle count exceeded 4294967295, so the stl file is not valid");
}
} else {
// ASCII mode: Write directly to the output stream
setlocale(LC_NUMERIC, "C"); // Ensure radix is . (not ,) in output
output << "solid OpenSCAD_Model\n";
uint64_t triangle_count = append_stl(geom, output, binary);
output << "endsolid OpenSCAD_Model\n";
setlocale(LC_NUMERIC, ""); // Set default locale
setlocale(LC_NUMERIC, ""); // Restore default locale
}
}
5 changes: 5 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1137,9 +1137,14 @@ ${TEST_SCAD_DIR}/misc/rotate_extrude-hole.scad

# Export tests (compare actually exported files)
add_cmdline_test(stlexport EXPERIMENTAL OPENSCAD SUFFIX stl FILES ${EXPORT_STL_TEST_FILES} ARGS --enable=predictible-output --render)
add_cmdline_test(stlexport-stdout EXPERIMENTAL OPENSCAD SUFFIX stl FILES ${EXPORT_STL_TEST_FILES} STDIO EXPECTEDDIR stlexport ARGS --enable=predictible-output --render --export-format asciistl)
if (ENABLE_MANIFOLD)
add_cmdline_test(manifold-stlexport EXPERIMENTAL OPENSCAD SUFFIX stl FILES ${EXPORT_STL_TEST_FILES} EXPECTEDDIR stlexport ARGS --enable=predictible-output --backend=manifold --render)
endif()

add_cmdline_test(binstlexport EXPERIMENTAL OPENSCAD SUFFIX stl FILES ${EXPORT_STL_TEST_FILES} ARGS --enable=predictible-output --render --export-format binstl)
add_cmdline_test(binstlexport-stdout EXPERIMENTAL OPENSCAD SUFFIX stl FILES ${EXPORT_STL_TEST_FILES} STDIO EXPECTEDDIR binstlexport ARGS --enable=predictible-output --render --export-format binstl)

add_cmdline_test(objexport EXPERIMENTAL OPENSCAD SUFFIX obj FILES ${EXPORT_OBJ_TEST_FILES} ARGS --render --enable=predictible-output)
if (LIB3MF_FOUND)
add_cmdline_test(3mfexport EXPERIMENTAL OPENSCAD SUFFIX 3mf FILES ${EXPORT_3MF_TEST_FILES} ARGS --enable=predictible-output)
Expand Down
Binary file not shown.

0 comments on commit c190dac

Please sign in to comment.