static int gcv_vrml_write(const char *path, struct db_i *vdbip, const struct gcv_opts *UNUSED(options)) { size_t i; struct plate_mode pm; size_t num_objects = 0; char **object_names = NULL; out_file = path; dbip = vdbip; bu_setlinebuf(stderr); the_model = nmg_mm(); 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; tol.magic = BN_TOL_MAGIC; tol.dist = 0.005; tol.dist_sq = tol.dist * tol.dist; tol.perp = 1e-6; tol.para = 1 - tol.perp; /* NOTE: For visualization purposes, in the debug plot files */ { /* WTF: This value is specific to the Bradley */ nmg_eue_dist = 2.0; } BU_LIST_INIT(&RTG.rtg_vlfree); /* for vlist macros */ if ((bot_dump == 1) && (eval_all == 1)) { bu_log("BOT Dump and Evaluate All are mutually exclusive\n"); return 0; } if (out_file == NULL) { fp_out = stdout; setmode(fileno(fp_out), O_BINARY); } else { if ((fp_out = fopen(out_file, "wb")) == NULL) { perror("g-vrml"); bu_log("Cannot open %s\n", out_file); return 0; } } fprintf(fp_out, "#VRML V2.0 utf8\n"); fprintf(fp_out, "#Units are %s\n", units); /* NOTE: We may want to inquire about bounding boxes for the * various groups and add Viewpoints nodes that point the camera * to the center and orient for Top, Side, etc. Views. We will add * some default Material Color definitions (for thousands groups) * before we start defining the geometry. */ fprintf(fp_out, "Shape { appearance Appearance { material DEF Material_999 Material { diffuseColor 0.78 0.78 0.78 } } }\n"); fprintf(fp_out, "Shape { appearance Appearance { material DEF Material_1999 Material { diffuseColor 0.88 0.29 0.29 } } }\n"); fprintf(fp_out, "Shape { appearance Appearance { material DEF Material_2999 Material { diffuseColor 0.82 0.53 0.54 } } }\n"); fprintf(fp_out, "Shape { appearance Appearance { material DEF Material_3999 Material { diffuseColor 0.39 0.89 0.00 } } }\n"); fprintf(fp_out, "Shape { appearance Appearance { material DEF Material_4999 Material { diffuseColor 1.00 0.00 0.00 } } }\n"); fprintf(fp_out, "Shape { appearance Appearance { material DEF Material_5999 Material { diffuseColor 0.82 0.00 0.82 } } }\n"); fprintf(fp_out, "Shape { appearance Appearance { material DEF Material_6999 Material { diffuseColor 0.62 0.62 0.62 } } }\n"); fprintf(fp_out, "Shape { appearance Appearance { material DEF Material_7999 Material { diffuseColor 0.49 0.49 0.49 } } }\n"); fprintf(fp_out, "Shape { appearance Appearance { material DEF Material_8999 Material { diffuseColor 0.18 0.31 0.31 } } }\n"); fprintf(fp_out, "Shape { appearance Appearance { material DEF Material_9999 Material { diffuseColor 0.00 0.41 0.82 } } }\n"); /* I had hoped to create a separate sub-tree (using the Transform * node) for each group name argument however, it appears they are * all handled at the same time so I will only have one Transform * for the complete conversion. Later on switch nodes may be added * to turn on and off the groups (via ROUTE nodes). */ fprintf(fp_out, "Transform {\n"); fprintf(fp_out, "\tchildren [\n"); pm.num_bots = 0; pm.num_nonbots = 0; if (!eval_all) { pm.array_size = 5; pm.bots = (struct rt_bot_internal **)bu_calloc(pm.array_size, sizeof(struct rt_bot_internal *), "pm.bots"); } /* get toplevel objects */ { struct directory **results; db_update_nref(dbip, &rt_uniresource); num_objects = db_ls(dbip, DB_LS_TOPS, NULL, &results); object_names = db_dpv_to_argv(results); bu_free(results, "tops"); } if (eval_all) { (void)db_walk_tree(dbip, num_objects, (const char **)(object_names), 1, /* ncpu */ &tree_state, 0, nmg_region_end, nmg_booltree_leaf_tess, (void *)&pm); /* in librt/nmg_bool.c */ goto out; } if (bot_dump) { (void)db_walk_tree(dbip, num_objects, (const char **)(object_names), 1, /* ncpu */ &tree_state, 0, do_region_end2, leaf_tess2, (void *)&pm); /* in librt/nmg_bool.c */ goto out; } for (i = 0; i < num_objects; i++) { struct directory *dp; dp = db_lookup(dbip, object_names[i], LOOKUP_QUIET); if (dp == RT_DIR_NULL) { bu_log("Cannot find %s\n", object_names[i]); continue; } /* light source must be a combination */ if (!(dp->d_flags & RT_DIR_COMB)) { continue; } fprintf(fp_out, "# Includes group %s\n", object_names[i]); /* walk trees selecting only light source regions */ (void)db_walk_tree(dbip, 1, (const char **)(&object_names[i]), 1, /* ncpu */ &tree_state, select_lights, do_region_end1, leaf_tess1, (void *)&pm); /* in librt/nmg_bool.c */ } /* Walk indicated tree(s). Each non-light-source region will be output separately */ (void)db_walk_tree(dbip, num_objects, (const char **)(object_names), 1, /* ncpu */ &tree_state, select_non_lights, do_region_end1, leaf_tess1, (void *)&pm); /* in librt/nmg_bool.c */ /* Release dynamic storage */ nmg_km(the_model); if (!eval_all) { bu_free(pm.bots, "pm.bots"); } out: bu_free(object_names, "object_names"); /* Now we need to close each group set */ fprintf(fp_out, "\t]\n}\n"); bu_log("\nTotal of %d regions converted of %d regions attempted.\n", regions_converted, regions_tried); if (regions_converted != regions_tried) { bu_log("Of the %d which failed conversion, %d of these failed due to conversion error.\n", regions_tried - regions_converted, bomb_cnt); } fclose(fp_out); bu_log("Done.\n"); return 1; }
int main(int argc, char *argv[]) { int ret = 0; int convert_tops_list = 0; struct directory **paths; int path_cnt = 0; AP203_Contents *sc = new AP203_Contents; // process command line arguments int c; char *output_file = (char *)NULL; while ((c = bu_getopt(argc, argv, "o:")) != -1) { switch (c) { case 'o': output_file = bu_optarg; break; default: usage(); bu_exit(1, NULL); break; } } if (bu_optind >= argc || output_file == (char *)NULL) { usage(); bu_exit(1, NULL); } argc -= bu_optind; argv += bu_optind; /* check our inputs/outputs */ if (bu_file_exists(output_file, NULL)) { bu_exit(1, "ERROR: refusing to overwrite existing output file:\"%s\". Please remove file or change output file name and try again.", output_file); } if (!bu_file_exists(argv[0], NULL) && !BU_STR_EQUAL(argv[0], "-")) { bu_exit(2, "ERROR: unable to read input \"%s\" .g file", argv[0]); } if (argc < 2) { convert_tops_list = 1; } std::string iflnm = argv[0]; std::string oflnm = output_file; /* load .g file */ BRLCADWrapper *dotg = new BRLCADWrapper(); if (!dotg) { std::cerr << "ERROR: unable to create BRL-CAD instance" << std::endl; return 3; } if (!dotg->load(iflnm)) { std::cerr << "ERROR: unable to open BRL-CAD input file [" << oflnm << "]" << std::endl; delete dotg; return 2; } struct db_i *dbip = dotg->GetDBIP(); struct rt_wdb *wdbp = wdb_dbopen(dbip, RT_WDB_TYPE_DB_DISK); if (convert_tops_list) { /* Need db_update_nref for DB_LS_TOPS to work */ db_update_nref(dbip, &rt_uniresource); path_cnt = db_ls(dbip, DB_LS_TOPS, &paths); if (!path_cnt) { std::cerr << "ERROR: no objects found in .g file" << "\n" << std::endl; delete dotg; return 1; } } else { int i = 1; paths = (struct directory **)bu_malloc(sizeof(struct directory *) * argc, "dp array"); while (i < argc) { bu_log("%d: %s\n", i, argv[i]); struct directory *dp = db_lookup(dbip, argv[i], LOOKUP_QUIET); if (dp == RT_DIR_NULL) { std::cerr << "ERROR: cannot find " << argv[i] << "\n" << std::endl; delete dotg; bu_free(paths, "free path memory"); return 1; } else { paths[i-1] = dp; path_cnt++; i++; } } paths[i-1] = RT_DIR_NULL; } struct bu_vls scratch_string; bu_vls_init(&scratch_string); Registry *registry = new Registry(SchemaInit); InstMgr instance_list; STEPfile *sfile = new STEPfile(*registry, instance_list); registry->ResetSchemas(); registry->ResetEntities(); /* Populate the header instances */ InstMgr *header_instances = sfile->HeaderInstances(); /* 1 - Populate File_Name */ SdaiFile_name * fn = (SdaiFile_name *)sfile->HeaderDefaultFileName(); bu_vls_sprintf(&scratch_string, "'%s'", output_file); fn->name_(bu_vls_addr(&scratch_string)); fn->time_stamp_(""); StringAggregate_ptr author_tmp = new StringAggregate; author_tmp->AddNode(new StringNode("''")); fn->author_(author_tmp); StringAggregate_ptr org_tmp = new StringAggregate; org_tmp->AddNode(new StringNode("''")); fn->organization_(org_tmp); fn->preprocessor_version_("'BRL-CAD g-step exporter'"); fn->originating_system_("''"); fn->authorization_("''"); header_instances->Append((SDAI_Application_instance *)fn, completeSE); /* 2 - Populate File_Description */ SdaiFile_description * fd = (SdaiFile_description *)sfile->HeaderDefaultFileDescription(); StringAggregate_ptr description_tmp = new StringAggregate; description_tmp->AddNode(new StringNode("''")); fd->description_(description_tmp); fd->implementation_level_("'2;1'"); header_instances->Append((SDAI_Application_instance *)fd, completeSE); /* 3 - Populate File_Schema */ SdaiFile_schema *fs = (SdaiFile_schema *)sfile->HeaderDefaultFileSchema(); StringAggregate_ptr schema_tmp = new StringAggregate; schema_tmp->AddNode(new StringNode("'CONFIG_CONTROL_DESIGN'")); fs->schema_identifiers_(schema_tmp); header_instances->Append((SDAI_Application_instance *)fs, completeSE); sc->registry = registry; sc->instance_list = &instance_list; sc->default_context = Add_Default_Geometric_Context(sc); sc->application_context = (SdaiApplication_context *)sc->registry->ObjCreate("APPLICATION_CONTEXT"); sc->instance_list->Append((STEPentity *)sc->application_context, completeSE); sc->application_context->application_("'CONFIGURATION CONTROLLED 3D DESIGNS OF MECHANICAL PARTS AND ASSEMBLIES'"); sc->design_context = (SdaiDesign_context *)sc->registry->ObjCreate("DESIGN_CONTEXT"); sc->instance_list->Append((STEPentity *)sc->design_context, completeSE); sc->design_context->name_("''"); sc->design_context->life_cycle_stage_("'design'"); sc->design_context->frame_of_reference_(sc->application_context); sc->solid_to_step = new std::map<struct directory *, STEPentity *>; sc->solid_to_step_shape = new std::map<struct directory *, STEPentity *>; sc->solid_to_step_manifold = new std::map<struct directory *, STEPentity *>; sc->comb_to_step = new std::map<struct directory *, STEPentity *>; sc->comb_to_step_shape = new std::map<struct directory *, STEPentity *>; sc->comb_to_step_manifold = new std::map<struct directory *, STEPentity *>; for (int i = 0; i < path_cnt; i++) { /* Now, add actual DATA */ struct directory *dp = paths[i]; struct rt_db_internal intern; rt_db_get_internal(&intern, dp, dbip, bn_mat_identity, &rt_uniresource); RT_CK_DB_INTERNAL(&intern); Object_To_STEP(dp, &intern, wdbp, sc); rt_db_free_internal(&intern); } /* Write STEP file */ if (!bu_file_exists(output_file, NULL)) { std::ofstream stepout(output_file); sfile->WriteExchangeFile(stepout); } /* Free memory */ header_instances->DeleteInstances(); instance_list.DeleteInstances(); delete dotg; delete registry; delete sfile; delete sc->solid_to_step; delete sc->solid_to_step_shape; delete sc->solid_to_step_manifold; delete sc->comb_to_step; delete sc->comb_to_step_shape; delete sc->comb_to_step_manifold; delete sc; bu_vls_free(&scratch_string); bu_free(paths, "free dp list"); return ret; }
int ged_tops(struct ged *gedp, int argc, const char *argv[]) { struct directory *dp; int i; struct directory **dirp; struct directory **dirp0 = (struct directory **)NULL; int c; int no_decorate = 0; int aflag = 0; int hflag = 0; int pflag = 0; /* static const char *usage = "[-a|-h|-n|-p]"; */ GED_CHECK_DATABASE_OPEN(gedp, GED_ERROR); GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR); /* initialize result */ bu_vls_trunc(gedp->ged_result_str, 0); /* process any options */ bu_optind = 1; /* re-init bu_getopt() */ while ((c = bu_getopt(argc, (char * const *)argv, "ahnp")) != -1) { switch (c) { case 'a': aflag = 1; break; case 'h': hflag = 1; break; case 'n': no_decorate = 1; break; case 'p': pflag = 1; break; default: break; } } argc -= (bu_optind - 1); argv += (bu_optind - 1); /* Can this be executed only sometimes? Perhaps a "dirty bit" on the database? */ db_update_nref(gedp->ged_wdbp->dbip, &rt_uniresource); /* * Find number of possible entries and allocate memory */ dirp = _ged_dir_getspace(gedp->ged_wdbp->dbip, 0); dirp0 = dirp; if (db_version(gedp->ged_wdbp->dbip) < 5) { for (i = 0; i < RT_DBNHASH; i++) for (dp = gedp->ged_wdbp->dbip->dbi_Head[i]; dp != RT_DIR_NULL; dp = dp->d_forw) { if (dp->d_nref == 0) *dirp++ = dp; } } else { for (i = 0; i < RT_DBNHASH; i++) for (dp = gedp->ged_wdbp->dbip->dbi_Head[i]; dp != RT_DIR_NULL; dp = dp->d_forw) { if (dp->d_nref != 0) { continue; } if ((aflag) || (hflag && (dp->d_flags & RT_DIR_HIDDEN)) || (pflag && dp->d_addr == RT_DIR_PHONY_ADDR)) { /* add object because it matches an option */ *dirp++ = dp; } else if (!aflag && !hflag && !pflag && !(dp->d_flags & RT_DIR_HIDDEN) && (dp->d_addr != RT_DIR_PHONY_ADDR)) { /* add non-hidden real object */ *dirp++ = dp; } } } _ged_vls_col_pr4v(gedp->ged_result_str, dirp0, (int)(dirp - dirp0), no_decorate); bu_free((void *)dirp0, "wdb_tops_cmd: wdb_dir_getspace"); return GED_OK; }
int main(int argc, char *argv[]) { int c; struct directory* dp; struct db_i *dbip; /* setup BRL-CAD environment */ bu_setprogname(argv[0]); bu_setlinebuf(stderr); /* process command line arguments */ while ((c = bu_getopt(argc, argv, "vo:ys:fh?")) != -1) { switch (c) { case 'v': verbose++; break; case 'o': out_file = bu_optarg; break; case 'y': yup++; break; case 's': sscanf(bu_optarg, "%f", &scale); break; case 'f': flip_normals++; break; default: print_usage(argv[0]); } } /* param check */ if (bu_optind+1 >= argc) print_usage(argv[0]); /* get database filename and object */ db_file = argv[bu_optind++]; object = argv[bu_optind]; /* open BRL-CAD database */ if ((dbip = db_open(db_file, DB_OPEN_READONLY)) == DBI_NULL) { perror(argv[0]); bu_exit(1, "Cannot open geometry database file %s\n", db_file); } if (db_dirbuild(dbip)) bu_exit(1, "db_dirbuild() failed!\n"); if (verbose) fprintf(stderr, ">> opened db '%s'\n", dbip->dbi_title); /* setup output stream */ if (out_file == NULL) { fp_out = stdout; setmode(fileno(fp_out), O_BINARY); } else { if ((fp_out = fopen(out_file, "wb")) == NULL) { bu_log("Cannot open %s\n", out_file); perror(argv[0]); return 2; } } /* find requested object */ db_update_nref(dbip, &rt_uniresource); dp = db_lookup(dbip, object, 0); if (dp == RT_DIR_NULL) bu_exit(1, "Object %s not found in database!\n", object); /* generate mesh list */ db_functree(dbip, dp, NULL, mesh_tracker, &rt_uniresource, NULL); if (verbose) fprintf(stderr, ">> mesh count: %d\n", mesh_count); /* write out header */ write_header(dbip); /* write out meshes */ write_mesh_data(); /* finish */ dealloc_mesh_list(); db_close(dbip); return 0; }