int ged_edmater(struct rt_wdb *wdbp, int argc, const char *argv[]) { FILE *fp; int i; int status; const char **av; static const char *usage = "comb(s)"; char tmpfil[MAXPATHLEN]; GED_CHECK_DATABASE_OPEN(wdbp, GED_ERROR); GED_CHECK_READ_ONLY(wdbp, GED_ERROR); /* must be wanting help */ if (argc == 1) { wdbp->wdb_result_flags |= GED_RESULT_FLAGS_HELP_BIT; bu_vls_printf(&wdbp->wdb_result_str, "Usage: %s %s", argv[0], usage); return GED_OK; } fp = bu_temp_file(tmpfil, MAXPATHLEN); if (!fp) return TCL_ERROR; av = (const char **)bu_malloc(sizeof(char *)*(argc + 2), "f_edmater: av"); av[0] = "wmater"; av[1] = tmpfil; for(i = 2; i < argc + 1; ++i) av[i] = argv[i-1]; av[i] = NULL; if (ged_wmater(wdbp, argc + 1, av) == TCL_ERROR) { (void)unlink(tmpfil); bu_free((genptr_t)av, "f_edmater: av"); return TCL_ERROR; } (void)fclose(fp); if (ged_editit(tmpfil)) { av[0] = "rmater"; av[2] = NULL; status = ged_rmater(wdbp, 2, av); } else { status = TCL_ERROR; } (void)unlink(tmpfil); bu_free((genptr_t)av, "ged_edmater: av"); return status; }
/* * used by the 'color' command when provided the -e option */ static int edcolor(struct ged *gedp, int argc, const char *argv[]) { struct mater *mp; struct mater *zot; FILE *fp; int c; char line[128]; static char hdr[] = "LOW\tHIGH\tRed\tGreen\tBlue\n"; char tmpfil[MAXPATHLEN]; char *editstring = NULL; GED_CHECK_DATABASE_OPEN(gedp, GED_ERROR); GED_CHECK_READ_ONLY(gedp, GED_ERROR); GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR); bu_optind = 1; /* First, grab the editstring off of the argv list */ while ((c = bu_getopt(argc, (char * const *)argv, "E:")) != -1) { switch (c) { case 'E' : editstring = bu_optarg; break; default : break; } } argc -= bu_optind - 1; argv += bu_optind - 1; /* initialize result */ bu_vls_trunc(gedp->ged_result_str, 0); fp = bu_temp_file(tmpfil, MAXPATHLEN); if (fp == NULL) { bu_vls_printf(gedp->ged_result_str, "%s: could not create tmp file", argv[0]); return GED_ERROR; } fprintf(fp, "%s", hdr); for (mp = rt_material_head(); mp != MATER_NULL; mp = mp->mt_forw) { fprintf(fp, "%d\t%d\t%3d\t%3d\t%3d", mp->mt_low, mp->mt_high, mp->mt_r, mp->mt_g, mp->mt_b); fprintf(fp, "\n"); } (void)fclose(fp); if (!_ged_editit(editstring, (const char *)tmpfil)) { bu_vls_printf(gedp->ged_result_str, "%s: editor returned bad status. Aborted\n", argv[0]); return GED_ERROR; } /* Read file and process it */ if ((fp = fopen(tmpfil, "r")) == NULL) { perror(tmpfil); return GED_ERROR; } if (bu_fgets(line, sizeof (line), fp) == NULL || line[0] != hdr[0]) { bu_vls_printf(gedp->ged_result_str, "%s: Header line damaged, aborting\n", argv[0]); (void)fclose(fp); return GED_ERROR; } if (db_version(gedp->ged_wdbp->dbip) < 5) { /* Zap all the current records, both in core and on disk */ while (rt_material_head() != MATER_NULL) { zot = rt_material_head(); rt_new_material_head(zot->mt_forw); color_zaprec(gedp, zot); bu_free((void *)zot, "mater rec"); } while (bu_fgets(line, sizeof (line), fp) != NULL) { int cnt; int low, hi, r, g, b; /* character-separated numbers (ideally a space) */ cnt = sscanf(line, "%d%*c%d%*c%d%*c%d%*c%d", &low, &hi, &r, &g, &b); if (cnt != 9) { bu_vls_printf(gedp->ged_result_str, "%s: Discarding %s\n", argv[0], line); continue; } BU_ALLOC(mp, struct mater); mp->mt_low = low; mp->mt_high = hi; mp->mt_r = r; mp->mt_g = g; mp->mt_b = b; mp->mt_daddr = MATER_NO_ADDR; rt_insert_color(mp); color_putrec(gedp, mp); } } else { struct bu_vls vls = BU_VLS_INIT_ZERO; /* free colors in rt_material_head */ rt_color_free(); while (bu_fgets(line, sizeof (line), fp) != NULL) { int cnt; int low, hi, r, g, b; /* character-separated numbers (ideally a space) */ cnt = sscanf(line, "%d%*c%d%*c%d%*c%d%*c%d", &low, &hi, &r, &g, &b); /* check to see if line is reasonable */ if (cnt != 5) { bu_vls_printf(gedp->ged_result_str, "%s: Discarding %s\n", argv[0], line); continue; } bu_vls_printf(&vls, "{%d %d %d %d %d} ", low, hi, r, g, b); } db5_update_attribute("_GLOBAL", "regionid_colortable", bu_vls_addr(&vls), gedp->ged_wdbp->dbip); db5_import_color_table(bu_vls_addr(&vls)); bu_vls_free(&vls); } (void)fclose(fp); bu_file_delete(tmpfil); /* if there are drawables, update their colors */ if (gedp->ged_gdp) ged_color_soltab(gedp->ged_gdp->gd_headDisplay); return GED_OK; }
/* * Called from db_walk_tree(). * * This routine must be prepared to run in parallel. */ union tree * do_nmg_region_end(struct db_tree_state *tsp, const struct db_full_path *pathp, union tree *curtree, void *UNUSED(client_data)) { union tree *result; struct nmgregion *r; struct bu_list vhead; struct directory *dp; int dependent; size_t i; RT_CK_TESS_TOL(tsp->ts_ttol); BN_CK_TOL(tsp->ts_tol); NMG_CK_MODEL(*tsp->ts_m); BU_LIST_INIT(&vhead); if (RT_G_DEBUG&DEBUG_TREEWALK || verbose) { char *sofar = db_path_to_string(pathp); bu_log("\ndo_nmg_region_end(%d %d%%) %s\n", regions_tried, regions_tried>0 ? (regions_done * 100) / regions_tried : 0, sofar); bu_free(sofar, "path string"); } if (curtree->tr_op == OP_NOP) return curtree; regions_tried++; if (verbose) bu_log("\ndoing boolean tree evaluate...\n"); /* evaluate the boolean */ result = process_boolean(tsp, curtree, pathp); if (result) r = result->tr_d.td_r; else r = (struct nmgregion *)NULL; if (verbose) bu_log("\nfinished boolean tree evaluate...\n"); regions_done++; if (r != NULL) { dp = DB_FULL_PATH_CUR_DIR(pathp); if (multi_file) { /* Open the output file */ if (output_file == NULL) fp_dir = stdout; else { char *multi_name; size_t len; int unique = 0; char suffix[SUFFIX_LEN+1]; /* construct a unique file name */ len = strlen(output_file) + strlen(dp->d_namep) + 6 + SUFFIX_LEN; multi_name = (char *)bu_malloc(sizeof(char)*len, "multi_name"); snprintf(multi_name, len, "%s/%s.igs", output_file, dp->d_namep); bu_strlcpy(suffix, "a", sizeof(suffix)); suffix[0]--; while (!unique) { if (bu_file_readable(multi_name)) { unique = 1; break; } /* not unique, try adding a suffix */ len = strlen(suffix); i = len - 1; suffix[i]++; while (suffix[i] > 'z' && i > 0) { suffix[i] = 'a'; i--; suffix[i]++; } if (suffix[0] > 'z' && len < SUFFIX_LEN) { for (i = 0; i <= len; i++) suffix[i] = 'a'; } else if (suffix[0] > 'z' && len >= SUFFIX_LEN) { bu_log("too many files with the same name (%s)\n", dp->d_namep); bu_exit(1, "Cannot create a unique filename, \n"); } snprintf(multi_name, len, "%s/%s%s.igs", output_file, dp->d_namep, suffix); } if ((fp_dir = fopen(multi_name, "wb")) == NULL) { perror("g-iges"); bu_exit(1, "Cannot open output file: %s\n", multi_name); } } /* Open the temporary file for the parameter section */ if ((fp_param = bu_temp_file(NULL, 0)) == NULL) { perror("g-iges"); bu_exit(1, "Cannot open temporary file\n"); } /* let the IGES routines know the selected tolerances and the database pointer */ iges_init(&tol, &ttol, verbose, DBIP); /* Write start and global sections of the IGES file */ w_start_global(fp_dir, fp_param, db_name, prog_name, output_file, __DATE__, brlcad_version()); } if (mode == FACET_MODE) { dependent = 1; for (i = 0; i < no_of_indeps; i++) { if (!bu_strncmp(dp->d_namep, independent[i], NAMESIZE+1)) { dependent = 0; break; } } dp->d_uses = (-nmgregion_to_iges(dp->d_namep, r, dependent, fp_dir, fp_param)); } else if (mode == TRIMMED_SURF_MODE) dp->d_uses = (-nmgregion_to_tsurf(dp->d_namep, r, fp_dir, fp_param)); /* NMG region is no longer necessary */ nmg_kr(r); if (multi_file) { char copy_buffer[CP_BUF_SIZE] = {0}; /* Copy the parameter section from the temporary file to the output file */ if ((bu_fseek(fp_param, 0, 0))) { perror("g-iges"); bu_exit(1, "Cannot seek to start of temporary file\n"); } while ((i = fread(copy_buffer, 1, CP_BUF_SIZE, fp_param))) if (fwrite(copy_buffer, 1, i, fp_dir) != i) { perror("g-iges"); bu_exit(1, "Error in copying parameter data to %s\n", output_file); } /* Write the terminate section */ w_terminate(fp_dir); fclose(fp_dir); fclose(fp_param); } } /* * Dispose of original tree, so that all associated dynamic * memory is released now, not at the end of all regions. * A return of TREE_NULL from this routine signals an error, * so we need to cons up an OP_NOP node to return. */ db_free_tree(curtree, &rt_uniresource); /* Does an nmg_kr() */ BU_ALLOC(curtree, union tree); RT_TREE_INIT(curtree); curtree->tr_op = OP_NOP; return curtree; }
int main(int argc, char *argv[]) { size_t i; int ret; int c; double percent; char copy_buffer[CP_BUF_SIZE] = {0}; struct directory *dp; bu_setprogname(argv[0]); bu_setlinebuf(stderr); bu_log("%s", brlcad_ident("BRL-CAD to IGES Translator")); bu_log("Please direct bug reports to <*****@*****.**>\n\n"); tree_state = rt_initial_tree_state; /* struct copy */ tree_state.ts_tol = &tol; tree_state.ts_ttol = &ttol; tree_state.ts_m = &the_model; ttol.magic = RT_TESS_TOL_MAGIC; /* Defaults, updated by command line options. */ ttol.abs = 0.0; ttol.rel = 0.01; ttol.norm = 0.0; /* FIXME: These need to be improved */ tol.magic = BN_TOL_MAGIC; tol.dist = 0.0005; tol.dist_sq = tol.dist * tol.dist; tol.perp = 1e-6; tol.para = 1 - tol.perp; the_model = nmg_mm(); BU_LIST_INIT(&RTG.rtg_vlfree); /* for vlist macros */ rt_init_resource(&rt_uniresource, 0, NULL); prog_name = argv[0]; /* Get command line arguments. */ while ((c = bu_getopt(argc, argv, "ftsmd:a:n:o:p:r:vx:P:X:")) != -1) { switch (c) { case 'f': /* Select facetized output */ mode = FACET_MODE; multi_file = 0; break; case 't': mode = TRIMMED_SURF_MODE; multi_file = 0; break; case 'm': /* multi-file mode */ multi_file = 1; mode = TRIMMED_SURF_MODE; break; case 's': /* Select NURB output */ do_nurbs = 1; break; case 'v': verbose++; break; case 'a': /* Absolute tolerance. */ ttol.abs = atof(bu_optarg); break; case 'r': /* Relative tolerance. */ ttol.rel = atof(bu_optarg); break; case 'n': /* Surface normal tolerance. */ ttol.norm = atof(bu_optarg); break; case 'd': /* distance tolerance */ tol.dist = atof(bu_optarg); tol.dist_sq = tol.dist * tol.dist; break; case 'x': sscanf(bu_optarg, "%x", (unsigned int *)&RTG.debug); break; case 'X': sscanf(bu_optarg, "%x", (unsigned int *)&RTG.NMG_debug); NMG_debug = RTG.NMG_debug; break; case 'o': /* Output file name. */ output_file = bu_optarg; break; case 'P': ncpu = atoi(bu_optarg); break; default: usage(argv[0]); break; } } if (bu_optind+1 >= argc) { usage(argv[0]); } /* Open BRL-CAD database */ argc -= bu_optind; argv += bu_optind; db_name = argv[0]; if ((DBIP = db_open(db_name, DB_OPEN_READONLY)) == DBI_NULL) { perror("g-iges"); bu_exit(1, "ERROR: unable to open geometry database file (%s)\n", db_name); } /* Scan the database */ if (db_dirbuild(DBIP)) { bu_exit(1, "db_dirbuild failed\n"); } if (!multi_file) { /* let the IGES routines know the selected tolerances and the database pointer */ iges_init(&tol, &ttol, verbose, DBIP); /* Open the output file */ if (output_file == NULL) fp_dir = stdout; else { if ((fp_dir = fopen(output_file, "wb")) == NULL) { perror(output_file); bu_exit(1, "Cannot open output file: %s\n", output_file); } } /* Open the temporary file for the parameter section */ if ((fp_param = bu_temp_file(NULL, 0)) == NULL) { perror("g-iges"); bu_exit(1, "Cannot open temporary file\n"); } /* Write start and global sections of the IGES file */ w_start_global(fp_dir, fp_param, argv[0], prog_name, output_file, __DATE__, brlcad_version()); } else { if (!bu_file_directory(output_file)) { bu_exit(1, "-o option must provide a directory, %s is not a directory\n", output_file); } } /* Count object references */ /* for (i = 1; i < argc; i++) { dp = db_lookup(DBIP, argv[i], 1); db_functree(DBIP, dp, count_refs, 0, NULL); } */ /* tree tops must have independent status, so we need to remember them */ independent = (argv+1); no_of_indeps = (size_t)argc-1; if (mode == FACET_MODE) { /* Walk indicated tree(s). Each region will be output * as a single manifold solid BREP object */ ret = db_walk_tree(DBIP, argc-1, (const char **)(argv+1), ncpu, &tree_state, 0, /* take all regions */ do_nmg_region_end, nmg_booltree_leaf_tess, (void *)NULL); /* in librt/nmg_bool.c */ if (ret) bu_exit(1, "g-iges: Could not facetize anything!"); if (!multi_file) { /* Now walk the same trees again, but only output groups */ for (i = 1; i < (size_t)argc; i++) { char *ptr; ptr = strrchr(argv[i], '/'); if (ptr != NULL) { ptr++; } else { ptr = argv[i]; } dp = db_lookup(DBIP, ptr, 1); if (!dp) { bu_log("WARNING: Unable to locate %s in %s\n, skipping\n", ptr, db_name); continue; } db_functree(DBIP, dp, csg_comb_func, 0, &rt_uniresource, NULL); } } } else if (mode == CSG_MODE) { /* Walk indicated tree(s). Each combination and solid will be output * as a CSG object, unless there is no IGES equivalent (then the * solid will be tessellated and output as a BREP object) */ for (i = 1; i < (size_t)argc; i++) { dp = db_lookup(DBIP, argv[i], 1); if (!dp) { bu_log("WARNING: Unable to locate %s in %s\n, skipping\n", argv[i], db_name); continue; } db_functree(DBIP, dp, csg_comb_func, csg_leaf_func, &rt_uniresource, NULL); } } else if (mode == TRIMMED_SURF_MODE) { /* Walk the indicated tree(s). Each region is output as a collection * of trimmed NURBS */ ret = db_walk_tree(DBIP, argc-1, (const char **)(argv+1), ncpu, &tree_state, 0, /* take all regions */ do_nmg_region_end, nmg_booltree_leaf_tess, (void *)NULL); /* in librt/nmg_bool.c */ if (ret) bu_exit(1, "g-iges: Could not facetize anything!"); } if (!multi_file) { /* Copy the parameter section from the temporary file to the output file */ if ((bu_fseek(fp_param, 0, 0))) { perror("g-iges"); bu_exit(1, "Cannot seek to start of temporary file\n"); } while ((i = fread(copy_buffer, 1, CP_BUF_SIZE, fp_param))) if (fwrite(copy_buffer, 1, i, fp_dir) != i) { perror("g-iges"); bu_exit(1, "Error in copying parameter data to %s\n", output_file); } /* Write the terminate section */ w_terminate(fp_dir); } /* Print some statistics */ Print_stats(stdout); /* report on the success rate for facetizing regions */ if (mode == FACET_MODE || mode == TRIMMED_SURF_MODE) { percent = 0; if (regions_tried>0) percent = ((double)regions_done * 100) / regions_tried; bu_log("Tried %d regions, %d converted to nmg's successfully. %g%%\n", regions_tried, regions_done, percent); } /* re-iterate warnings */ if (scale_error || solid_error || comb_error) bu_log("WARNING: the IGES file produced has errors:\n"); if (scale_error) bu_log("\t%d scaled objects found, written to IGES file without being scaled\n", scale_error); if (solid_error) bu_log("\t%d solids were not converted to IGES format\n", solid_error); if (comb_error) bu_log("\t%d combinations were not converted to IGES format\n", comb_error); return 0; }
int main(int argc, char **argv) { int c; int i; struct pshell *psh; struct pbar *pbp; struct wmember head; struct wmember all_head; char *nastran_file = "Converted from NASTRAN file (stdin)"; bu_setprogname(argv[0]); fpin = stdin; units = INCHES; /* FIXME: These need to be improved */ tol.magic = BN_TOL_MAGIC; tol.dist = 0.0005; tol.dist_sq = tol.dist * tol.dist; tol.perp = 1e-6; tol.para = 1 - tol.perp; while ((c=bu_getopt(argc, argv, "x:X:t:ni:o:mh?")) != -1) { switch (c) { case 'x': sscanf(bu_optarg, "%x", (unsigned int *)&RTG.debug); bu_printb("librt RT_G_DEBUG", RT_G_DEBUG, DEBUG_FORMAT); bu_log("\n"); break; case 'X': sscanf(bu_optarg, "%x", (unsigned int *)&RTG.NMG_debug); bu_printb("librt RTG.NMG_debug", RTG.NMG_debug, NMG_DEBUG_FORMAT); bu_log("\n"); break; case 't': /* calculational tolerance */ tol.dist = atof(bu_optarg); tol.dist_sq = tol.dist * tol.dist; break; case 'n': polysolids = 0; break; case 'm': units = MM; break; case 'i': fpin = fopen(bu_optarg, "rb"); if (fpin == (FILE *)NULL) { bu_log("Cannot open NASTRAN file (%s) for reading!\n", bu_optarg); bu_exit(1, Usage, argv[0]); } nastran_file = bu_optarg; break; case 'o': output_file = bu_optarg; break; default: bu_exit(1, Usage, argv[0]); } } fpout = wdb_fopen(output_file); if (fpout == NULL) { bu_log("Cannot open BRL-CAD file (%s) for writing!\n", output_file); bu_exit(1, Usage, argv[0]); } if (!fpin || !fpout) { bu_exit(1, Usage, argv[0]); } line = (char *)bu_malloc(MAX_LINE_SIZE, "line"); next_line = (char *)bu_malloc(MAX_LINE_SIZE, "next_line"); prev_line = (char *)bu_malloc(MAX_LINE_SIZE, "prev_line"); curr_rec = (char **)bu_calloc(NO_OF_FIELDS, sizeof(char *), "curr_rec"); for (i=0; i<NO_OF_FIELDS; i++) curr_rec[i] = (char *)bu_malloc(sizeof(char)*FIELD_LENGTH, "curr_rec[i]"); prev_rec = (char **)bu_calloc(NO_OF_FIELDS, sizeof(char *), "prev_rec"); for (i=0; i<NO_OF_FIELDS; i++) prev_rec[i] = (char *)bu_malloc(sizeof(char)*FIELD_LENGTH, "prev_rec[i]"); /* first pass, find start of NASTRAN "bulk data" */ start_off = (-1); bulk_data_start_line = 0; while (bu_fgets(line, MAX_LINE_SIZE, fpin)) { bulk_data_start_line++; if (bu_strncmp(line, "BEGIN BULK", 10)) continue; start_off = bu_ftell(fpin); break; } if (start_off < 0) { bu_log("Cannot find start of bulk data in NASTRAN file!\n"); bu_exit(1, Usage, argv[0]); } /* convert BULK data deck into something reasonable */ fptmp = bu_temp_file(NULL, 0); if (fptmp == NULL) { perror(argv[0]); bu_exit(1, "Cannot open temporary file\n"); } convert_input(); /* initialize some lists */ BU_LIST_INIT(&coord_head.l); BU_LIST_INIT(&pbar_head.l); BU_LIST_INIT(&pshell_head.l); BU_LIST_INIT(&all_head.l); nmg_model = (struct model *)NULL; /* count grid points */ bu_fseek(fptmp, 0, SEEK_SET); while (bu_fgets(line, MAX_LINE_SIZE, fptmp)) { if (!bu_strncmp(line, "GRID", 4)) grid_count++; } if (!grid_count) { bu_exit(1, "No geometry in this NASTRAN file!\n"); } /* get default values and properties */ bu_fseek(fptmp, 0, SEEK_SET); while (get_next_record(fptmp, 1, 0)) { if (!bu_strncmp(curr_rec[0], "BAROR", 5)) { /* get BAR defaults */ bar_def_pid = atoi(curr_rec[2]); } else if (!bu_strncmp(curr_rec[0], "PBAR", 4)) { struct pbar *pb; BU_ALLOC(pb, struct pbar); pb->pid = atoi(curr_rec[1]); pb->mid = atoi(curr_rec[2]); pb->area = atof(curr_rec[3]); BU_LIST_INIT(&pb->head.l); BU_LIST_INSERT(&pbar_head.l, &pb->l); } else if (!bu_strncmp(curr_rec[0], "PSHELL", 6)) { BU_ALLOC(psh, struct pshell); psh->s = (struct shell *)NULL; psh->pid = atoi(curr_rec[1]); psh->mid = atoi(curr_rec[2]); psh->thick = atof(curr_rec[3]); BU_LIST_INSERT(&pshell_head.l, &psh->l); pshell_count++; } } /* allocate storage for grid points */ g_pts = (struct grid_point *)bu_calloc(grid_count, sizeof(struct grid_point), "grid points"); /* get all grid points */ bu_fseek(fptmp, 0, SEEK_SET); while (get_next_record(fptmp, 1, 0)) { int gid; int cid; double tmp[3]; if (bu_strncmp(curr_rec[0], "GRID", 4)) continue; gid = atoi(curr_rec[1]); cid = atoi(curr_rec[2]); for (i=0; i<3; i++) { tmp[i] = atof(curr_rec[i+3]); } g_pts[grid_used].gid = gid; g_pts[grid_used].cid = cid; g_pts[grid_used].v = (struct vertex **)bu_calloc(pshell_count + 1, sizeof(struct vertex *), "g_pts vertex array"); VMOVE(g_pts[grid_used].pt, tmp); grid_used++; } /* find coordinate systems */ bu_fseek(fptmp, 0, SEEK_SET); while (get_next_record(fptmp, 1, 0)) { if (bu_strncmp(curr_rec[0], "CORD", 4)) continue; get_coord_sys(); } /* convert everything to BRL-CAD coordinate system */ i = 0; while (convert_all_cs() || convert_all_pts()) { i++; if (i > 10) { bu_exit(1, "Cannot convert to default coordinate system, check for circular definition\n"); } } mk_id(fpout, nastran_file); /* get elements */ bu_fseek(fptmp, 0, SEEK_SET); while (get_next_record(fptmp, 1, 0)) { if (!bu_strncmp(curr_rec[0], "CBAR", 4)) get_cbar(); else if (!bu_strncmp(curr_rec[0], "CROD", 4)) get_cbar(); else if (!bu_strncmp(curr_rec[0], "CTRIA3", 6)) get_ctria3(); else if (!bu_strncmp(curr_rec[0], "CQUAD4", 6)) get_cquad4(); } if (nmg_model) { nmg_rebound(nmg_model, &tol); if (polysolids) mk_bot_from_nmg(fpout, "pshell.0", nmg_shell); else mk_nmg(fpout, "pshell.0", nmg_model); } BU_LIST_INIT(&head.l); for (BU_LIST_FOR(psh, pshell, &pshell_head.l)) { struct model *m; char name[NAMESIZE+1]; if (!psh->s) continue; m = nmg_find_model(&psh->s->l.magic); nmg_rebound(m, &tol); nmg_fix_normals(psh->s, &tol); if (psh->thick > tol.dist) { nmg_model_face_fuse(m, &tol); nmg_hollow_shell(psh->s, psh->thick*conv[units], 1, &tol); } sprintf(name, "pshell.%d", psh->pid); if (polysolids) mk_bot_from_nmg(fpout, name, psh->s); else mk_nmg(fpout, name, m); mk_addmember(name, &head.l, NULL, WMOP_UNION); } if (BU_LIST_NON_EMPTY(&head.l)) { mk_lfcomb(fpout, "shells", &head, 0); mk_addmember("shells", &all_head.l, NULL, WMOP_UNION); } BU_LIST_INIT(&head.l); for (BU_LIST_FOR(pbp, pbar, &pbar_head.l)) { char name[NAMESIZE+1]; if (BU_LIST_IS_EMPTY(&pbp->head.l)) continue; sprintf(name, "pbar_group.%d", pbp->pid); mk_lfcomb(fpout, name, &pbp->head, 0); mk_addmember(name, &head.l, NULL, WMOP_UNION); } if (BU_LIST_NON_EMPTY(&head.l)) { mk_lfcomb(fpout, "pbars", &head, 0); mk_addmember("pbars", &all_head.l, NULL, WMOP_UNION); } if (BU_LIST_NON_EMPTY(&all_head.l)) { mk_lfcomb(fpout, "all", &all_head, 0); } wdb_close(fpout); return 0; }
int main(int argc, char *argv[]) { char _template[512] = {0}; char buf[SIZE] = {0}; FILE *fp = NULL; long count = 0; int tfd = 0; int ret = 0; if ((BU_STR_EQUAL(argv[1],"-h") || BU_STR_EQUAL(argv[1],"-?")) && argc == 2) { bu_log("Usage: %s (takes no arguments)\n",argv[0]); exit(1); } if (argc > 1) { bu_log("%s: unrecognized argument(s)\n", argv[0]); bu_log(" Program continues running:\n", argv[0]); } if ((count = bu_mread(0, buf, sizeof(buf))) < (long)sizeof(buf)) { if (count < 0) { perror("buffer: mem read"); exit(1); } /* Entire input sequence fit into buf */ if (write(1, buf, count) != count) { perror("buffer: stdout write 1"); exit(1); } exit(0); } /* Create temporary file to hold data, get r/w file descriptor */ fp = bu_temp_file(_template, 512); if (fp == NULL || (tfd = fileno(fp)) < 0) { perror(_template); goto err; } /* Stash away first buffer full */ if (write(tfd, buf, count) != count) { perror("buffer: tmp write1"); goto err; } /* Continue reading and writing additional buffer loads to temp file */ while ((count = bu_mread(0, buf, sizeof(buf))) > 0) { if (write(tfd, buf, count) != count) { perror("buffer: tmp write2"); goto err; } } if (count < 0) { perror("buffer: read"); goto err; } /* All input read, regurgitate it all on stdout */ if (lseek(tfd, 0, 0) < 0) { perror("buffer: lseek"); goto err; } while ((count = bu_mread(tfd, buf, sizeof(buf))) > 0) { if (write(1, buf, count) != count) { perror("buffer: stdout write 2"); goto err; } } if (count < 0) { perror("buffer: tmp read"); goto err; } ret = 0; goto clean; err: ret = 1; clean: /* clean up */ if (fp) { fclose(fp); fp = NULL; } bu_file_delete(_template); return ret; }
int ged_edcodes(struct ged *gedp, int argc, const char *argv[]) { int i; int nflag = 0; int status; int sort_by_ident=0; int sort_by_region=0; int c; char **av; FILE *fp = NULL; char tmpfil[MAXPATHLEN] = {0}; const char *editstring = NULL; static const char *usage = "[-i|-n|-r|-E editor] object(s)"; GED_CHECK_DATABASE_OPEN(gedp, GED_ERROR); GED_CHECK_READ_ONLY(gedp, GED_ERROR); GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR); /* initialize result */ bu_vls_trunc(gedp->ged_result_str, 0); /* must be wanting help */ if (argc == 1) { bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage); return GED_HELP; } bu_optind = 1; while ((c = bu_getopt(argc, (char * const *)argv, "E:inr")) != -1) { switch (c) { case 'E' : editstring = bu_optarg; break; case 'i': sort_by_ident = 1; break; case 'n': nflag = 1; break; case 'r': sort_by_region = 1; break; } } if ((nflag + sort_by_ident + sort_by_region) > 1) { bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage); return GED_ERROR; } argc -= bu_optind - 1; argv += bu_optind - 1; if (nflag) { struct directory *dp; for (i = 1; i < argc; ++i) { if ((dp = db_lookup(gedp->ged_wdbp->dbip, argv[i], LOOKUP_NOISY)) != RT_DIR_NULL) { status = edcodes_collect_regnames(gedp, dp, 0); if (status != EDCODES_OK) { if (status == EDCODES_HALT) bu_vls_printf(gedp->ged_result_str, "%s: nesting is too deep\n", argv[0]); return GED_ERROR; } } } return GED_OK; } fp = bu_temp_file(tmpfil, MAXPATHLEN); if (!fp) return GED_ERROR; av = (char **)bu_malloc(sizeof(char *)*(argc + 2), "ged_edcodes av"); av[0] = "wcodes"; av[1] = tmpfil; for (i = 2; i < argc + 1; ++i) av[i] = (char *)argv[i-1]; av[i] = NULL; (void)fclose(fp); if (ged_wcodes(gedp, argc + 1, (const char **)av) == GED_ERROR) { bu_file_delete(tmpfil); bu_free((void *)av, "ged_edcodes av"); return GED_ERROR; } if (sort_by_ident || sort_by_region) { char **line_array; char aline[RT_MAXLINE]; FILE *f_srt; int line_count=0; int j; if ((f_srt=fopen(tmpfil, "r+")) == NULL) { bu_vls_printf(gedp->ged_result_str, "%s: Failed to open temp file for sorting\n", argv[0]); bu_file_delete(tmpfil); return GED_ERROR; } /* count lines */ while (bu_fgets(aline, RT_MAXLINE, f_srt)) { line_count++; } /* build array of lines */ line_array = (char **)bu_calloc(line_count, sizeof(char *), "edcodes line array"); /* read lines and save into the array */ rewind(f_srt); line_count = 0; while (bu_fgets(aline, RT_MAXLINE, f_srt)) { line_array[line_count] = bu_strdup(aline); line_count++; } /* sort the array of lines */ if (sort_by_ident) { bu_sort((void *)line_array, line_count, sizeof(char *), edcodes_id_compare, NULL); } else { bu_sort((void *)line_array, line_count, sizeof(char *), edcodes_reg_compare, NULL); } /* rewrite the temp file using the sorted lines */ rewind(f_srt); for (j = 0; j < line_count; j++) { fprintf(f_srt, "%s", line_array[j]); bu_free(line_array[j], "ged_edcodes line array element"); } bu_free((char *)line_array, "ged_edcodes line array"); fclose(f_srt); } if (_ged_editit(editstring, tmpfil)) { av[0] = "rcodes"; av[2] = NULL; status = ged_rcodes(gedp, 2, (const char **)av); } else status = GED_ERROR; bu_file_delete(tmpfil); bu_free((void *)av, "ged_edcodes av"); return status; }