Index: exp_fmt_amf.c =================================================================== --- exp_fmt_amf.c (revision 35973) +++ exp_fmt_amf.c (revision 35974) @@ -132,3 +132,13 @@ verthash_uninit(&verthash); } + +static const stl_fmt_t fmt_amf = { + ".amf", + amf_print_horiz_tri, + amf_print_vert_tri, + amf_print_facet, + amf_new_obj, + amf_print_header, + amf_print_footer +}; Index: exp_fmt_stl.c =================================================================== --- exp_fmt_stl.c (revision 35973) +++ exp_fmt_stl.c (revision 35974) @@ -101,3 +101,13 @@ { fprintf(f, "endsolid\n"); } + +static const stl_fmt_t fmt_stl = { + ".stl", + stl_print_horiz_tri, + stl_print_vert_tri, + stl_print_facet, + stl_new_obj, + stl_print_header, + stl_print_footer +}; Index: export_stl.c =================================================================== --- export_stl.c (revision 35973) +++ export_stl.c (revision 35974) @@ -92,30 +92,6 @@ static rnd_hid_attr_val_t stl_values[NUM_OPTIONS]; -static const rnd_export_opt_t *stl_get_export_options_(rnd_hid_t *hid, int *n, int fmt_amf) -{ - const char *suffix = fmt_amf ? ".amf" : ".stl"; - const char *val = stl_values[HA_stlfile].str; - - if ((PCB != NULL) && ((val == NULL) || (*val == '\0'))) - pcb_derive_default_filename(PCB->hidlib.filename, &stl_values[HA_stlfile], suffix); - - if (n) - *n = NUM_OPTIONS; - return stl_attribute_list; -} - -static const rnd_export_opt_t *stl_get_export_options(rnd_hid_t *hid, int *n) -{ - return stl_get_export_options_(hid, n, 0); -} - -static const rnd_export_opt_t *amf_get_export_options(rnd_hid_t *hid, int *n) -{ - return stl_get_export_options_(hid, n, 1); -} - - static long pa_len(const rnd_polyarea_t *pa) { rnd_pline_t *pl; @@ -346,12 +322,45 @@ static verthash_t verthash; +typedef struct { + const char *suffix; + void (*print_horiz_tri)(FILE *f, fp2t_triangle_t *t, int up, rnd_coord_t z); + void (*print_vert_tri)(FILE *f, rnd_coord_t x1, rnd_coord_t y1, rnd_coord_t x2, rnd_coord_t y2, rnd_coord_t z0, rnd_coord_t z1); + void (*print_facet)(FILE *f, stl_facet_t *head, double mx[16], double mxn[16]); + void (*new_obj)(float r, float g, float b); + void (*print_header)(FILE *f); + void (*print_footer)(FILE *f); +} stl_fmt_t; + +static const rnd_export_opt_t *stl_get_export_options_(rnd_hid_t *hid, int *n, const stl_fmt_t *fmt) +{ + const char *suffix = fmt->suffix; + const char *val = stl_values[HA_stlfile].str; + + if ((PCB != NULL) && ((val == NULL) || (*val == '\0'))) + pcb_derive_default_filename(PCB->hidlib.filename, &stl_values[HA_stlfile], suffix); + + if (n) + *n = NUM_OPTIONS; + return stl_attribute_list; +} + #include "exp_fmt_stl.c" #include "exp_fmt_amf.c" #include "stl_models.c" -static int stl_hid_export_to_file(FILE *f, rnd_hid_attr_val_t *options, rnd_coord_t maxy, rnd_coord_t z0, rnd_coord_t z1, int fmt_amf) +static const rnd_export_opt_t *stl_get_export_options(rnd_hid_t *hid, int *n) { + return stl_get_export_options_(hid, n, &fmt_stl); +} + +static const rnd_export_opt_t *amf_get_export_options(rnd_hid_t *hid, int *n) +{ + return stl_get_export_options_(hid, n, &fmt_amf); +} + +static int stl_hid_export_to_file(FILE *f, rnd_hid_attr_val_t *options, rnd_coord_t maxy, rnd_coord_t z0, rnd_coord_t z1, const stl_fmt_t *fmt) +{ pcb_dynf_t df; pcb_poly_t *brdpoly; size_t mem_req; @@ -422,25 +431,13 @@ fp2t_triangulate(&tri); - if (fmt_amf) { - amf_print_header(f); - amf_new_obj(0, 0.3, 0); - } - else { - stl_print_header(f); - stl_new_obj(0, 0.3, 0); - } + fmt->print_header(f); + fmt->new_obj(0, 0.3, 0); /* write the top and bottom plane */ for(n = 0; n < tri.TriangleCount; n++) { - if (fmt_amf) { - amf_print_horiz_tri(f, tri.Triangles[n], 0, z0); - amf_print_horiz_tri(f, tri.Triangles[n], 1, z1); - } - else { - stl_print_horiz_tri(f, tri.Triangles[n], 0, z0); - stl_print_horiz_tri(f, tri.Triangles[n], 1, z1); - } + fmt->print_horiz_tri(f, tri.Triangles[n], 0, z0); + fmt->print_horiz_tri(f, tri.Triangles[n], 1, z1); } /* write the vertical side */ @@ -456,10 +453,7 @@ px = contours.array[pn], py = contours.array[pn+1]; cx = contours.array[n], cy = contours.array[n+1]; /* rnd_trace(" [%ld <- %ld] c:%f;%f p:%f;%f\n", n, pn, cx/1000000.0, cy/1000000.0, px/1000000.0, py/1000000.0);*/ - if (fmt_amf) - amf_print_vert_tri(f, cx, cy, px, py, z0, z1); - else - stl_print_vert_tri(f, cx, cy, px, py, z0, z1); + fmt->print_vert_tri(f, cx, cy, px, py, z0, z1); } cn += 2; cn_start = cn; @@ -467,12 +461,9 @@ } if (options[HA_models].lng) - stl_models_print(PCB, f, maxy, z0, z1, fmt_amf); + stl_models_print(PCB, f, maxy, z0, z1, fmt); - if (fmt_amf) - amf_print_footer(f); - else - stl_print_footer(f); + fmt->print_footer(f); vtp0_uninit(&cutouts); for(n = 0; n < cutouts.used; n++) @@ -484,7 +475,7 @@ return 0; } -static void stl_do_export_(rnd_hid_t *hid, rnd_hid_attr_val_t *options, int fmt_amf) +static void stl_do_export_(rnd_hid_t *hid, rnd_hid_attr_val_t *options, const stl_fmt_t *fmt) { const char *filename; pcb_cam_t cam; @@ -492,7 +483,7 @@ rnd_coord_t thick; if (!options) { - stl_get_export_options_(hid, 0, fmt_amf); + stl_get_export_options_(hid, 0, fmt); options = stl_values; } @@ -517,9 +508,9 @@ } if (options[HA_zcent].lng) - stl_hid_export_to_file(f, options, PCB->hidlib.size_y, -thick/2, +thick/2, fmt_amf); + stl_hid_export_to_file(f, options, PCB->hidlib.size_y, -thick/2, +thick/2, fmt); else - stl_hid_export_to_file(f, options, PCB->hidlib.size_y, 0, thick, fmt_amf); + stl_hid_export_to_file(f, options, PCB->hidlib.size_y, 0, thick, fmt); fclose(f); pcb_cam_end(&cam); @@ -527,12 +518,12 @@ static void stl_do_export(rnd_hid_t *hid, rnd_hid_attr_val_t *options) { - stl_do_export_(hid, options, 0); + stl_do_export_(hid, options, &fmt_stl); } static void amf_do_export(rnd_hid_t *hid, rnd_hid_attr_val_t *options) { - stl_do_export_(hid, options, 1); + stl_do_export_(hid, options, &fmt_amf); } static int stl_parse_arguments(rnd_hid_t *hid, int *argc, char ***argv) Index: stl_models.c =================================================================== --- stl_models.c (revision 35973) +++ stl_models.c (revision 35974) @@ -111,7 +111,7 @@ } } -void stl_solid_print_facets(FILE *f, stl_facet_t *head, double rotx, double roty, double rotz, double xlatex, double xlatey, double xlatez, int is_amf) +void stl_solid_print_facets(FILE *f, stl_facet_t *head, double rotx, double roty, double rotz, double xlatex, double xlatey, double xlatez, const stl_fmt_t *fmt) { double mxn[16], mx[16], tmp[16], tmp2[16]; @@ -126,12 +126,8 @@ memcpy(mx, tmp2, sizeof(tmp2)); } - for(; head != NULL; head = head->next) { - if (is_amf) - amf_print_facet(f, head, mx, mxn); - else - stl_print_facet(f, head, mx, mxn); - } + for(; head != NULL; head = head->next) + fmt->print_facet(f, head, mx, mxn); } #ifndef STL_TESTER @@ -164,7 +160,7 @@ #include "model_load_stl.c" #include "model_load_amf.c" -static void stl_model_place(rnd_hidlib_t *hl, FILE *outf, 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, rnd_coord_t z0, rnd_coord_t z1, int fmt_amf) +static void stl_model_place(rnd_hidlib_t *hl, FILE *outf, 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, rnd_coord_t z0, rnd_coord_t z1, const stl_fmt_t *fmt) { stl_facet_t *head = NULL; double uxlate[3] = {0,0,0}, xlate[3], urot[3] = {0,0,0}, rot[3]; @@ -202,11 +198,11 @@ rot[1] = (on_bottom ? M_PI : 0) + urot[1] / RND_RAD_TO_DEG; rot[2] = rotdeg / RND_RAD_TO_DEG + urot[2] / RND_RAD_TO_DEG; - stl_solid_print_facets(outf, head, rot[0], rot[1], rot[2], xlate[0], xlate[1], xlate[2], fmt_amf); + stl_solid_print_facets(outf, head, rot[0], rot[1], rot[2], xlate[0], xlate[1], xlate[2], fmt); } -void stl_models_print(pcb_board_t *pcb, FILE *outf, double maxy, rnd_coord_t z0, rnd_coord_t z1, int fmt_amf) +void stl_models_print(pcb_board_t *pcb, FILE *outf, double maxy, rnd_coord_t z0, rnd_coord_t z1, const stl_fmt_t *fmt) { htsp_t models; const char *mod; @@ -238,14 +234,11 @@ srot = pcb_attribute_get(&subc->Attributes, "stl-rotate"); if (first) { - if (fmt_amf) - amf_new_obj(0, 0, 0); - else - stl_new_obj(0, 0, 0); + fmt->new_obj(0, 0, 0); first = 0; } - stl_model_place(&pcb->hidlib, outf, &models, mod, ox, oy, rot, on_bottom, sxlate, srot, maxy, z0, z1, fmt_amf); + stl_model_place(&pcb->hidlib, outf, &models, mod, ox, oy, rot, on_bottom, sxlate, srot, maxy, z0, z1, fmt); } } PCB_END_LOOP;