star-cad

Geometric operators for computer-aided design
git clone git://git.meso-star.fr/star-cad.git
Log | Files | Refs | README | LICENSE

commit 420f9185bc1446e267e621f3f125cd7302354c16
parent 3e45e459760fcef1a34fa401a8b11055ffc11148
Author: Christophe Coustet <christophe.coustet@meso-star.com>
Date:   Fri, 20 Oct 2023 10:31:23 +0200

Change nul triangles detected when writing an STL file from error to warning

Diffstat:
Msrc/scad.c | 41++++++++++++++++++++++++++++++-----------
1 file changed, 30 insertions(+), 11 deletions(-)

diff --git a/src/scad.c b/src/scad.c @@ -53,6 +53,7 @@ write_ascii_stl { res_T res = RES_OK; size_t i; + size_t actual_trg_count = 0; FILE* stl_file = NULL; ASSERT(filename && (coords || count == 0)); @@ -75,11 +76,7 @@ write_ascii_stl d3_sub(edge1, vtx[1], vtx[0]); d3_sub(edge2, vtx[2], vtx[0]); d3_cross(tmp, edge1, edge2); - if(d3_eq(tmp, zero)) { - log_error(get_device(), "Error: nul triangle in exported geometry.\n"); - res = RES_BAD_ARG; - goto error; - } + if(d3_eq(tmp, zero)) continue; d3_normalize(n, tmp); OKP(fprintf(stl_file, " facet normal %g %g %g\n", SPLIT3(n))); @@ -89,8 +86,16 @@ write_ascii_stl } OKP(fprintf(stl_file, " endloop\n")); OKP(fprintf(stl_file, " endfacet\n")); + actual_trg_count++; } OKP(fprintf(stl_file, "endsolid \n")); + if(actual_trg_count != count) { + long long unsigned del_count = count - actual_trg_count; + ASSERT(actual_trg_count < count); + log_warning(get_device(), + "In file '%s': %llu nul triangle(s) removed in exported geometry.\n", + filename, del_count); + } exit: if(stl_file) fclose(stl_file); @@ -109,9 +114,10 @@ write_binary_stl { res_T res = RES_OK; unsigned i; - unsigned trg_count; + unsigned trg_count, actual_trg_count = 0; char header[80] = "Binary STL"; FILE* stl_file = NULL; + long int offset; ASSERT(filename && (coords || count == 0)); @@ -127,6 +133,7 @@ write_binary_stl } trg_count = (unsigned)count; + offset = ftell(stl_file); if(1 != fwrite(&trg_count, 4, 1, stl_file)) { res = RES_IO_ERR; goto error; @@ -145,17 +152,29 @@ write_binary_stl f3_sub(edge1, trg.vrtx[1], trg.vrtx[0]); f3_sub(edge2, trg.vrtx[2], trg.vrtx[0]); f3_cross(tmp, edge1, edge2); - if(f3_eq(tmp, zero)) { - log_error(get_device(), "Error: nul triangle in exported geometry.\n"); - res = RES_BAD_ARG; - goto error; - } + if(f3_eq(tmp, zero)) continue; f3_normalize(trg.n, tmp); trg.attrib = 0; if(1 != fwrite(&trg, 50, 1, stl_file)) { res = RES_IO_ERR; goto error; } + actual_trg_count++; + } + if(actual_trg_count != trg_count) { + ASSERT(actual_trg_count < trg_count); + log_warning(get_device(), + "In file '%s': %u nul triangle(s) removed in exported geometry.\n", + filename, trg_count - actual_trg_count); + /* Need to change count */ + if(0 != fseek(stl_file, offset, SEEK_SET)) { + res = RES_IO_ERR; + goto error; + } + if(1 != fwrite(&actual_trg_count, 4, 1, stl_file)) { + res = RES_IO_ERR; + goto error; + } } exit: