/* * Write the nmg to a BRL-CAD style data base. */ int create_brlcad_db(struct rt_wdb *fpout, struct model *m, char *reg_name, char *grp_name) { char *rname, *sname; int empty_model; struct shell *s; struct nmgregion *r; rname = (char *)bu_malloc(sizeof(reg_name) + 3, "rname"); /* Region name. */ sname = (char *)bu_malloc(sizeof(reg_name) + 3, "sname"); /* Solid name. */ snprintf(sname, sizeof(reg_name) + 2, "s.%s", reg_name); empty_model = nmg_kill_zero_length_edgeuses(m); if (empty_model) { bu_log("Warning: skipping empty model."); return 0; } nmg_rebound(m, &tol); r = BU_LIST_FIRST(nmgregion, &m->r_hd); s = BU_LIST_FIRST(shell, &r->s_hd); mk_bot_from_nmg(fpout, sname, s); /* Make BOT object. */ snprintf(rname, sizeof(reg_name) + 2, "r.%s", reg_name); mk_comb1(fpout, rname, sname, 1); /* Put object in a region. */ if (grp_name) { mk_comb1(fpout, grp_name, rname, 1); /* Region in group. */ } return 0; }
static void nmg_conv(struct rt_db_internal *intern, const char *name) { struct model *m; struct nmgregion *r; struct shell *s; RT_CK_DB_INTERNAL(intern); m = (struct model *)intern->idb_ptr; NMG_CK_MODEL(m); r = BU_LIST_FIRST(nmgregion, &m->r_hd); if (r && BU_LIST_NEXT(nmgregion, &r->l) != (struct nmgregion *)&m->r_hd) bu_exit(1, "ERROR: this code works only for NMG models with one region!\n"); s = BU_LIST_FIRST(shell, &r->s_hd); if (s && BU_LIST_NEXT(shell, &s->l) != (struct shell *)&r->s_hd) bu_exit(1, "ERROR: this code works only for NMG models with one shell!\n"); if (!s) { bu_log("WARNING: NMG has no shells\n"); return; } if (!BU_SETJUMP) { /* try */ mk_bot_from_nmg(fdout, name, s); } else { /* catch */ BU_UNSETJUMP; bu_log("Failed to convert %s\n", name); return; } BU_UNSETJUMP; if (verbose) bu_log("Converted %s to a Bot solid\n", name); }
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 extrude(int entityno) { fastf_t length; /* extrusion length */ vect_t edir; /* a unit vector (direction of extrusion */ vect_t evect; /* Scaled vector for extrusion */ int sol_num; /* IGES solid type number */ int curve; /* pointer to directory entry for base curve */ struct ptlist *curv_pts; /* List of points along curve */ int i; /* Default values */ VSET(edir, 0.0, 0.0, 1.0); /* Acquiring Data */ if (dir[entityno]->param <= pstart) { bu_log("Illegal parameter pointer for entity D%07d (%s)\n" , dir[entityno]->direct, dir[entityno]->name); return 0; } Readrec(dir[entityno]->param); Readint(&sol_num, ""); /* Read pointer to directory entry for curve to be extruded */ Readint(&curve, ""); /* Convert this to a "dir" index */ curve = (curve-1)/2; Readcnv(&length, ""); Readflt(&edir[X], ""); Readflt(&edir[Y], ""); Readflt(&edir[Z], ""); if (length <= 0.0) { bu_log("Illegal parameters for entity D%07d (%s)\n" , dir[entityno]->direct, dir[entityno]->name); return 0; } /* * Unitize direction vector */ VUNITIZE(edir); /* Scale vector */ VSCALE(evect, edir, length); /* Switch based on type of curve to be extruded */ switch (dir[curve]->type) { case 100: /* circular arc */ return Extrudcirc(entityno, curve, evect); case 104: /* conic arc */ return Extrudcon(entityno, curve, evect); case 102: /* composite curve */ case 106: /* copius data */ case 112: /* parametric spline */ case 126: { /* B-spline */ int npts; struct model *m; struct nmgregion *r; struct shell *s; struct faceuse *fu; struct loopuse *lu; struct edgeuse *eu; struct ptlist *pt_ptr; npts = Getcurve(curve, &curv_pts); if (npts < 3) return 0; m = nmg_mm(); r = nmg_mrsv(m); s = BU_LIST_FIRST(shell, &r->s_hd); fu = nmg_cface(s, (struct vertex **)NULL, npts-1); pt_ptr = curv_pts; lu = BU_LIST_FIRST(loopuse, &fu->lu_hd); for (BU_LIST_FOR(eu, edgeuse, &lu->down_hd)) { struct vertex *v; v = eu->vu_p->v_p; nmg_vertex_gv(v, pt_ptr->pt); pt_ptr = pt_ptr->next; } if (nmg_calc_face_g(fu)) { bu_log("Extrude: Failed to calculate face geometry\n"); nmg_km(m); bu_free((char *)curv_pts, "curve_pts"); return 0; } if (nmg_extrude_face(fu, evect, &tol)) { bu_log("Extrude: extrusion failed\n"); nmg_km(m); bu_free((char *)curv_pts, "curve_pts"); return 0; } mk_bot_from_nmg(fdout, dir[entityno]->name, s); nmg_km(m); bu_free((char *)curv_pts, "curve_pts"); return 1; } default: i = (-1); while (dir[curve]->type != typecount[++i].type && i < ntypes); bu_log("Extrusions of %s are not allowed\n", typecount[i].name); break; } return 0; }
int brep(int entityno) { int sol_num; /* IGES solid type number */ int shell_de; /* Directory sequence number for a shell */ int orient; /* Orientation of shell */ int *void_shell_de; /* Directory sequence number for an void shell */ int *void_orient; /* Orientation of void shell */ int num_of_voids; /* Number of inner void shells */ struct model *m; /* NMG model */ struct nmgregion *r; /* NMG region */ struct shell **void_shells; /* List of void shells */ struct shell *s_outer; /* Outer shell */ struct iges_vertex_list *v_list; struct iges_edge_list *e_list; int i; /* Acquiring Data */ if (dir[entityno]->param <= pstart) { bu_log("Illegal parameter pointer for entity D%07d (%s)\n" , dir[entityno]->direct , dir[entityno]->name); return 0; } Readrec(dir[entityno]->param); Readint(&sol_num , ""); Readint(&shell_de , ""); Readint(&orient , ""); Readint(&num_of_voids , ""); if (num_of_voids) { void_shell_de = (int *)bu_calloc(num_of_voids , sizeof(int) , "BREP: void shell DE's"); void_orient = (int *)bu_calloc(num_of_voids , sizeof(int) , "BREP: void shell orients"); void_shells = (struct shell **)bu_calloc(num_of_voids , sizeof(struct shell *) , "BREP: void shell pointers"); for (i = 0; i < num_of_voids; i++) { Readint(&void_shell_de[i] , ""); Readint(&void_orient[i] , ""); } } else { void_shell_de = NULL; void_orient = NULL; void_shells = NULL; } /* start building */ m = nmg_mmr(); r = BU_LIST_FIRST(nmgregion, &m->r_hd); /* Put outer shell in region */ if ((s_outer = Get_outer_shell(r , (shell_de - 1)/2)) == (struct shell *)NULL) goto err; ON_Brep* outer = ON_Brep::New(); if (Get_outer_brep(outer, (shell_de - 1)/2, orient)) goto err; /* Put voids in */ for (i = 0; i < num_of_voids; i++) { if ((void_shells[i] = Add_inner_shell(r, (void_shell_de[i] - 1)/2)) == (struct shell *)NULL) goto err; } /* orient loops */ Orient_loops(r); /* orient shells */ nmg_fix_normals(s_outer , &tol); for (i = 0; i < num_of_voids; i++) { nmg_fix_normals(void_shells[i] , &tol); nmg_invert_shell(void_shells[i]); } if (do_bots) { /* Merge all shells into one */ for (i = 0; i < num_of_voids; i++) nmg_js(s_outer, void_shells[i], &tol); /* write out BOT */ if (mk_bot_from_nmg(fdout, dir[entityno]->name, s_outer)) goto err; } else { /* Compute "geometry" for region and shell */ nmg_region_a(r , &tol); /* Write NMG solid */ if (mk_nmg(fdout , dir[entityno]->name , m)) goto err; } if (num_of_voids) { bu_free((char *)void_shell_de , "BREP: void shell DE's"); bu_free((char *)void_orient , "BREP: void shell orients"); bu_free((char *)void_shells , "brep: void shell list"); } v_list = vertex_root; while (v_list != NULL) { bu_free((char *)v_list->i_verts , "brep: iges_vertex"); bu_free((char *)v_list , "brep: vertex list"); v_list = v_list->next; } vertex_root = NULL; e_list = edge_root; while (e_list != NULL) { bu_free((char *)e_list->i_edge , "brep:iges_edge"); bu_free((char *)e_list , "brep: edge list"); e_list = e_list->next; } edge_root = NULL; return 1; err : if (num_of_voids) { bu_free((char *)void_shell_de , "BREP: void shell DE's"); bu_free((char *)void_orient , "BREP: void shell orients"); bu_free((char *)void_shells , "brep: void shell list"); } nmg_km(m); return 0; }