Index: trunk/src_plugins/export_stl/export_stl.c =================================================================== --- trunk/src_plugins/export_stl/export_stl.c (revision 32435) +++ trunk/src_plugins/export_stl/export_stl.c (revision 32436) @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include "data.h" @@ -41,6 +42,7 @@ #include "layer.h" #include "obj_pstk_inlines.h" #include "plug_io.h" +#include "conf_core.h" #include "../lib_polyhelp/topoly.h" #include "../lib_polyhelp/triangulate.h" Index: trunk/src_plugins/export_stl/stl_models.c =================================================================== --- trunk/src_plugins/export_stl/stl_models.c (revision 32435) +++ trunk/src_plugins/export_stl/stl_models.c (revision 32436) @@ -59,21 +59,16 @@ } } -stl_facet_t *stl_solid_load(rnd_hidlib_t *hl, const char *fn) +stl_facet_t *stl_solid_fload(rnd_hidlib_t *hl, FILE *f) { - FILE *f; stl_facet_t *head = NULL, *tail = NULL, *t; char *cmd, line[512]; - f = rnd_fopen(hl, fn, "r"); - if (f == NULL) - return NULL; - /* find the 'solid' header */ cmd = stl_getline(line, sizeof(line), f); if ((cmd == NULL) || (strncmp(cmd, "solid", 5) != 0)) { rnd_message(RND_MSG_ERROR, "Invalid stl file: first line is not a 'solid'\n"); - goto err0; + return NULL; } for(;;) { @@ -82,7 +77,7 @@ cmd = stl_getline(line, sizeof(line), f); if (cmd == NULL) { rnd_message(RND_MSG_ERROR, "Invalid stl file: premature end of file in solid\n"); - goto err1; + goto error; } if (strncmp(cmd, "endsolid", 8) == 0) break; /* normal end of file */ @@ -89,7 +84,7 @@ if (strncmp(cmd, "facet normal", 12) != 0) { rnd_message(RND_MSG_ERROR, "Invalid stl file: expected facet, got %s\n", cmd); - goto err1; + goto error; } t = malloc(sizeof(stl_facet_t)); @@ -104,13 +99,13 @@ cmd += 12; if (sscanf(cmd, "%lf %lf %lf", &t->n[0], &t->n[1], &t->n[2]) != 3) { rnd_message(RND_MSG_ERROR, "Invalid stl file: wrong facet normals '%s'\n", cmd); - goto err1; + goto error; } cmd = stl_getline(line, sizeof(line), f); if (strncmp(cmd, "outer loop", 10) != 0) { rnd_message(RND_MSG_ERROR, "Invalid stl file: expected outer loop, got %s\n", cmd); - goto err1; + goto error; } for(n = 0; n < 3; n++) { @@ -117,12 +112,12 @@ cmd = stl_getline(line, sizeof(line), f); if (strncmp(cmd, "vertex", 6) != 0) { rnd_message(RND_MSG_ERROR, "Invalid stl file: expected vertex, got %s\n", cmd); - goto err1; + goto error; } cmd+=6; if (sscanf(cmd, "%lf %lf %lf", &t->vx[n], &t->vy[n], &t->vz[n]) != 3) { rnd_message(RND_MSG_ERROR, "Invalid stl file: wrong facet vertex '%s'\n", cmd); - goto err1; + goto error; } } @@ -129,22 +124,20 @@ cmd = stl_getline(line, sizeof(line), f); if (strncmp(cmd, "endloop", 7) != 0) { rnd_message(RND_MSG_ERROR, "Invalid stl file: expected endloop, got %s\n", cmd); - goto err1; + goto error; } cmd = stl_getline(line, sizeof(line), f); if (strncmp(cmd, "endfacet", 8) != 0) { rnd_message(RND_MSG_ERROR, "Invalid stl file: expected endfacet, got %s\n", cmd); - goto err1; + goto error; } } - fclose(f); - err0:; return head; - err1:; + error:; stl_solid_free(head); fclose(f); return NULL; @@ -246,10 +239,32 @@ #ifndef STL_TESTER -static void stl_model_place(htsp_t *models, const char *mod, rnd_coord_t ox, rnd_coord_t oy, double rotdeg, int on_bottom, const char *user_xlate, const char *user_rot, double maxy) +static void stl_model_place(rnd_hidlib_t *hl, htsp_t *models, const char *name, rnd_coord_t ox, rnd_coord_t oy, double rotdeg, int on_bottom, const char *user_xlate, const char *user_rot, double maxy) { + stl_facet_t *head = NULL; double xlate[3], rot[3]; + if (!htsp_has(models, name)) { + char *full_path; + FILE *f = rnd_fopen_first(&PCB->hidlib, &conf_core.rc.library_search_paths, name, "r", &full_path, rnd_true); + if (f != NULL) { + head = stl_solid_fload(hl, f); + if (head == NULL) + rnd_message(RND_MSG_ERROR, "STL model failed to load: %s\n", full_path); + } + else + rnd_message(RND_MSG_ERROR, "STL model not found: %s\n", name); +printf("model: %s -> %s\n", name, full_path); + free(full_path); + fclose(f); + htsp_set(models, rnd_strdup(name), head); + } + else + head = htsp_get(models, name); + + if (head == NULL) + return; + xlate[0] = ox; xlate[1] = maxy - oy; xlate[2] = 0; @@ -257,6 +272,7 @@ rot[0] = 0; rot[1] = on_bottom ? M_PI : 0; rot[2] = rotdeg / RND_RAD_TO_DEG; + } @@ -286,12 +302,14 @@ sxlate = pcb_attribute_get(&subc->Attributes, "stl::translate"); srot = pcb_attribute_get(&subc->Attributes, "stl::rotate"); - stl_model_place(&models, mod, ox, oy, rot, on_bottom, sxlate, srot, maxy); + stl_model_place(&pcb->hidlib, &models, mod, ox, oy, rot, on_bottom, sxlate, srot, maxy); } } PCB_END_LOOP; - for (e = htsp_first(&models); e; e = htsp_next(&models, e)) - free(e->value); + for (e = htsp_first(&models); e; e = htsp_next(&models, e)) { + free(e->key); + stl_solid_free((stl_facet_t *)e->value); + } htsp_uninit(&models); Index: trunk/src_plugins/export_stl/test_load/main.c =================================================================== --- trunk/src_plugins/export_stl/test_load/main.c (revision 32435) +++ trunk/src_plugins/export_stl/test_load/main.c (revision 32436) @@ -3,6 +3,22 @@ #define STL_TESTER #include "../stl_models.c" +stl_facet_t *stl_solid_load(rnd_hidlib_t *hl, const char *fn) +{ + FILE *f; + stl_facet_t *res; + + f = rnd_fopen(hl, fn, "r"); + if (f == NULL) + return NULL; + + res = stl_solid_fload(hl, fn); + + fclose(f); + + return res; +} + int main() { FILE *f;