From a879e772eade7a6da41aed05a78b10c48ce69a18 Mon Sep 17 00:00:00 2001 From: Clifford Yapp <238416+starseeker@users.noreply.github.com> Date: Fri, 19 Jan 2024 15:54:42 -0500 Subject: [PATCH] Set up to structurally allow the possibility of passing in multiple directory pointers to the subcommands. --- src/libged/brep/brep.cpp | 118 +++++++++++++++++++++++++++++-------- src/libged/brep/ged_brep.h | 2 +- src/libged/brep/tikz.cpp | 14 +++-- 3 files changed, 103 insertions(+), 31 deletions(-) diff --git a/src/libged/brep/brep.cpp b/src/libged/brep/brep.cpp index 088c9930cfe..828e9b0d0c5 100644 --- a/src/libged/brep/brep.cpp +++ b/src/libged/brep/brep.cpp @@ -173,19 +173,24 @@ _brep_cmd_boolean(void *bs, int argc, const char **argv) return BRLCAD_ERROR; } + if (BU_PTBL_LEN(&gb->dps) != 1) { + bu_vls_printf(gb->gedp->ged_result_str, ": multiple objects not currently supported for boolean subcommand\n"); + return BRLCAD_ERROR; + } + struct directory *dp = (struct directory *)BU_PTBL_GET(&gb->dps, 0); + if (argc != 4) { bu_vls_printf(gb->gedp->ged_result_str, "brep bool \n"); return BRLCAD_ERROR; } - // We've already looked up the first sold, get the second struct directory *dp2 = db_lookup(gedp->dbip, argv[2], LOOKUP_NOISY); if (dp2 == RT_DIR_NULL) { bu_vls_printf(gedp->ged_result_str, ": %s is not a solid or does not exist in database", argv[3]); return BRLCAD_ERROR; } else { - int real_flag = (gb->dp->d_addr == RT_DIR_PHONY_ADDR) ? 0 : 1; + int real_flag = (dp->d_addr == RT_DIR_PHONY_ADDR) ? 0 : 1; if (!real_flag) { /* solid doesn't exist */ bu_vls_printf(gedp->ged_result_str, ": %s is not a real solid", argv[2]); @@ -316,8 +321,14 @@ _brep_cmd_bots(void *bs, int argc, const char **argv) return BRLCAD_ERROR; } + if (BU_PTBL_LEN(&gb->dps) != 1) { + bu_vls_printf(gb->gedp->ged_result_str, ": multiple object specification not currently supported for bots subcommand\n"); + return BRLCAD_ERROR; + } + struct directory *gdp = (struct directory *)BU_PTBL_GET(&gb->dps, 0); + const char **obj_names = (const char **)bu_calloc(argc, sizeof(char *), "new argv"); - obj_names[0] = gb->dp->d_namep; + obj_names[0] = gdp->d_namep; for (int iav = 0; iav < argc; iav++) { obj_names[iav+1] = argv[iav]; } @@ -430,6 +441,12 @@ _brep_cmd_brep(void *bs, int argc, const char **argv) struct _ged_brep_info *gb = (struct _ged_brep_info *)bs; + if (BU_PTBL_LEN(&gb->dps) != 1) { + bu_vls_printf(gb->gedp->ged_result_str, ": multiple object specification not currently supported for brep subcommand\n"); + return BRLCAD_ERROR; + } + struct directory *gdp = (struct directory *)BU_PTBL_GET(&gb->dps, 0); + argc--;argv++; int no_evaluation = 0; @@ -465,7 +482,7 @@ _brep_cmd_brep(void *bs, int argc, const char **argv) // brep_conversion_comb frees the intern, so make a new copy specifically for it to avoid // a double-free with the top level cleanup of gb->intern struct rt_db_internal intern; - GED_DB_GET_INTERNAL(gedp, &intern, gb->dp, bn_mat_identity, &rt_uniresource, BRLCAD_ERROR); + GED_DB_GET_INTERNAL(gedp, &intern, gdp, bn_mat_identity, &rt_uniresource, BRLCAD_ERROR); RT_CK_DB_INTERNAL(&intern); struct rt_wdb *wdbp = wdb_dbopen(gedp->dbip, RT_WDB_TYPE_DB_DEFAULT); @@ -526,6 +543,13 @@ _brep_cmd_csg(void *bs, int argc, const char **argv) } struct _ged_brep_info *gb = (struct _ged_brep_info *)bs; + + if (BU_PTBL_LEN(&gb->dps) != 1) { + bu_vls_printf(gb->gedp->ged_result_str, ": multiple object specification not currently supported for brep subcommand\n"); + return BRLCAD_ERROR; + } + struct directory *gdp = (struct directory *)BU_PTBL_GET(&gb->dps, 0); + struct ged *gedp = gb->gedp; struct bu_vls bname_csg; @@ -538,7 +562,7 @@ _brep_cmd_csg(void *bs, int argc, const char **argv) } bu_vls_free(&bname_csg); - return _ged_brep_to_csg(gedp, gb->dp->d_namep, gb->verbosity); + return _ged_brep_to_csg(gedp, gdp->d_namep, gb->verbosity); } extern "C" int @@ -553,6 +577,12 @@ _brep_cmd_dump(void *bs, int argc, const char **argv) struct _ged_brep_info *gb = (struct _ged_brep_info *)bs; struct ged *gedp = gb->gedp; + if (BU_PTBL_LEN(&gb->dps) != 1) { + bu_vls_printf(gb->gedp->ged_result_str, ": multiple object specification not currently supported for dump subcommand\n"); + return BRLCAD_ERROR; + } + struct directory *gdp = (struct directory *)BU_PTBL_GET(&gb->dps, 0); + argc--; argv++; if (!argc) { @@ -575,7 +605,7 @@ _brep_cmd_dump(void *bs, int argc, const char **argv) struct bu_ptbl breps = BU_PTBL_INIT_ZERO; const char *brep_search = "-type brep"; db_update_nref(gedp->dbip, &rt_uniresource); - (void)db_search(&breps, DB_SEARCH_TREE, brep_search, 1, &gb->dp, gedp->dbip, NULL); + (void)db_search(&breps, DB_SEARCH_TREE, brep_search, 1, &gdp, gedp->dbip, NULL); for (size_t i = 0; i < BU_PTBL_LEN(&breps); i++) { struct db_full_path *fp = (struct db_full_path *)BU_PTBL_GET(&breps, i); mat_t m; @@ -725,6 +755,13 @@ _brep_cmd_intersect(void *bs, int argc, const char **argv) } struct _ged_brep_info *gb = (struct _ged_brep_info *)bs; + + if (BU_PTBL_LEN(&gb->dps) != 1) { + bu_vls_printf(gb->gedp->ged_result_str, ": multiple object specification not currently supported for intersect subcommand\n"); + return BRLCAD_ERROR; + } + struct directory *gdp = (struct directory *)BU_PTBL_GET(&gb->dps, 0); + struct ged *gedp = gb->gedp; if (gb->intern.idb_minor_type != DB5_MINORTYPE_BRLCAD_BREP) { bu_vls_printf(gb->gedp->ged_result_str, ": object %s is not of type brep\n", gb->solid_name.c_str()); @@ -750,7 +787,7 @@ _brep_cmd_intersect(void *bs, int argc, const char **argv) bu_vls_printf(gedp->ged_result_str, ": %s is not a solid or does not exist in database", argv[3]); return BRLCAD_ERROR; } else { - int real_flag = (gb->dp->d_addr == RT_DIR_PHONY_ADDR) ? 0 : 1; + int real_flag = (gdp->d_addr == RT_DIR_PHONY_ADDR) ? 0 : 1; if (!real_flag) { /* solid doesn't exist */ bu_vls_printf(gedp->ged_result_str, ": %s is not a real solid", argv[1]); @@ -827,6 +864,13 @@ _brep_cmd_plate_mode(void *bs, int argc, const char **argv) struct _ged_brep_info *gb = (struct _ged_brep_info *)bs; + if (BU_PTBL_LEN(&gb->dps) != 1) { + bu_vls_printf(gb->gedp->ged_result_str, ": multiple object specification not currently supported for plate_mode subcommand\n"); + return BRLCAD_ERROR; + } + struct directory *gdp = (struct directory *)BU_PTBL_GET(&gb->dps, 0); + + if (gb->intern.idb_minor_type != DB5_MINORTYPE_BRLCAD_BREP) { bu_vls_printf(gb->gedp->ged_result_str, ": object %s is not of type brep\n", gb->solid_name.c_str()); return BRLCAD_ERROR; @@ -860,14 +904,14 @@ _brep_cmd_plate_mode(void *bs, int argc, const char **argv) double local2base = gb->gedp->dbip->dbi_local2base; // Make sure we can get attributes - if (db5_get_attributes(gb->gedp->dbip, &avs, gb->dp)) { + if (db5_get_attributes(gb->gedp->dbip, &avs, gdp)) { bu_vls_printf(gb->gedp->ged_result_str, "Error setting plate mode value\n"); return BRLCAD_ERROR; }; if (BU_STR_EQUIV(val, "cos")) { (void)bu_avs_add(&avs, "_plate_mode_nocos", "0"); - if (db5_replace_attributes(gb->dp, &avs, gb->gedp->dbip)) { + if (db5_replace_attributes(gdp, &avs, gb->gedp->dbip)) { bu_vls_printf(gb->gedp->ged_result_str, "Error setting plate mode value\n"); return BRLCAD_ERROR; } else { @@ -878,7 +922,7 @@ _brep_cmd_plate_mode(void *bs, int argc, const char **argv) if (BU_STR_EQUIV(val, "nocos")) { (void)bu_avs_add(&avs, "_plate_mode_nocos", "1"); - if (db5_replace_attributes(gb->dp, &avs, gb->gedp->dbip)) { + if (db5_replace_attributes(gdp, &avs, gb->gedp->dbip)) { bu_vls_printf(gb->gedp->ged_result_str, "Error setting plate mode value\n"); return BRLCAD_ERROR; } else { @@ -904,7 +948,7 @@ _brep_cmd_plate_mode(void *bs, int argc, const char **argv) ss << std::fixed << std::setprecision(std::numeric_limits::max_digits10) << pthicknessmm; std::string sd = ss.str(); (void)bu_avs_add(&avs, "_plate_mode_thickness", sd.c_str()); - if (db5_replace_attributes(gb->dp, &avs, gb->gedp->dbip)) { + if (db5_replace_attributes(gdp, &avs, gb->gedp->dbip)) { bu_vls_printf(gb->gedp->ged_result_str, "Error setting plate mode value\n"); return BRLCAD_ERROR; } else { @@ -973,6 +1017,13 @@ _brep_cmd_selection(void *bs, int argc, const char **argv) } struct _ged_brep_info *gb = (struct _ged_brep_info *)bs; + + if (BU_PTBL_LEN(&gb->dps) != 1) { + bu_vls_printf(gb->gedp->ged_result_str, ": multiple object specification not currently supported for selection subcommand\n"); + return BRLCAD_ERROR; + } + struct directory *gdp = (struct directory *)BU_PTBL_GET(&gb->dps, 0); + struct ged *gedp = gb->gedp; if (gb->intern.idb_minor_type != DB5_MINORTYPE_BRLCAD_BREP) { bu_vls_printf(gb->gedp->ged_result_str, ": object %s is not of type brep\n", gb->solid_name.c_str()); @@ -1087,7 +1138,7 @@ _brep_cmd_selection(void *bs, int argc, const char **argv) return BRLCAD_ERROR; } } - GED_DB_PUT_INTERNAL(gedp, gb->dp, &gb->intern, &rt_uniresource, BRLCAD_ERROR); + GED_DB_PUT_INTERNAL(gedp, gdp, &gb->intern, &rt_uniresource, BRLCAD_ERROR); } return BRLCAD_OK; } @@ -1157,6 +1208,13 @@ _brep_cmd_split(void *bs, int argc, const char **argv) struct _ged_brep_info *gb = (struct _ged_brep_info *)bs; struct ged *gedp = gb->gedp; + if (BU_PTBL_LEN(&gb->dps) != 1) { + bu_vls_printf(gb->gedp->ged_result_str, ": multiple object specification not currently supported for split subcommand\n"); + return BRLCAD_ERROR; + } + struct directory *gdp = (struct directory *)BU_PTBL_GET(&gb->dps, 0); + + if (gb->intern.idb_minor_type != DB5_MINORTYPE_BRLCAD_BREP) { bu_vls_printf(gb->gedp->ged_result_str, ": object %s is not of type brep\n", gb->solid_name.c_str()); return BRLCAD_ERROR; @@ -1244,7 +1302,7 @@ _brep_cmd_split(void *bs, int argc, const char **argv) bu_vls_free(&ocomb); return BRLCAD_ERROR; } - if (db5_get_attributes(gb->gedp->dbip, &avs, gb->dp)) { + if (db5_get_attributes(gb->gedp->dbip, &avs, gdp)) { bu_vls_printf(gedp->ged_result_str, ": failed to get attributes from face brep %s", bu_vls_cstr(&fbrep_name)); bu_vls_free(&fbrep_name); bu_vls_free(&ocomb); @@ -1290,7 +1348,7 @@ _brep_cmd_split(void *bs, int argc, const char **argv) bu_vls_free(&ocomb); return BRLCAD_ERROR; } - if (db5_get_attributes(gb->gedp->dbip, &avs, gb->dp)) { + if (db5_get_attributes(gb->gedp->dbip, &avs, gdp)) { bu_vls_printf(gedp->ged_result_str, ": failed to get attributes from brep"); bu_vls_free(&ocomb); return BRLCAD_ERROR; @@ -1539,15 +1597,15 @@ ged_brep_core(struct ged *gedp, int argc, const char *argv[]) gb.wdbp = wdb_dbopen(gb.gedp->dbip, RT_WDB_TYPE_DB_DEFAULT); gb.cmds = _brep_cmds; gb.solid_name = std::string(argv[0]); - gb.dp = db_lookup(gedp->dbip, gb.solid_name.c_str(), LOOKUP_NOISY); - if (gb.dp == RT_DIR_NULL) { - bu_vls_printf(gedp->ged_result_str, ": %s is not a solid or does not exist in database", gb.solid_name.c_str()); - if (color) { - BU_PUT(color, struct bu_color); - } - return BRLCAD_ERROR; - } else { - int real_flag = (gb.dp->d_addr == RT_DIR_PHONY_ADDR) ? 0 : 1; + + // TODO - we need to allow for specifying a glob pattern of multiple objects + // here. + bu_ptbl_init(&gb.dps, 8, "dp table"); + + // First, see if we have an exact match for a database name. + struct directory *gdp = db_lookup(gedp->dbip, gb.solid_name.c_str(), LOOKUP_QUIET); + if (gdp != RT_DIR_NULL) { + int real_flag = (gdp->d_addr == RT_DIR_PHONY_ADDR) ? 0 : 1; if (!real_flag) { /* solid doesn't exist */ bu_vls_printf(gedp->ged_result_str, ": %s is not a real solid", gb.solid_name.c_str()); @@ -1556,10 +1614,14 @@ ged_brep_core(struct ged *gedp, int argc, const char *argv[]) } return BRLCAD_ERROR; } - } + bu_ptbl_ins(&gb.dps, (long *)gdp); - GED_DB_GET_INTERNAL(gedp, &gb.intern, gb.dp, bn_mat_identity, &rt_uniresource, BRLCAD_ERROR); - RT_CK_DB_INTERNAL(&gb.intern); + // Single object - go ahead and do the intern unpack now. + GED_DB_GET_INTERNAL(gedp, &gb.intern, gdp, bn_mat_identity, &rt_uniresource, BRLCAD_ERROR); + RT_CK_DB_INTERNAL(&gb.intern); + } else { + // TODO - Not an object name - see if we got a glob pattern + } gb.vbp = bv_vlblock_init(&RTG.rtg_vlfree, 32); gb.color = color; @@ -1571,6 +1633,9 @@ ged_brep_core(struct ged *gedp, int argc, const char *argv[]) int ret; if (bu_cmd(_brep_cmds, argc, argv, 0, (void *)&gb, &ret) == BRLCAD_OK) { + bv_vlblock_free(gb.vbp); + gb.vbp = (struct bv_vlblock *)NULL; + bu_ptbl_free(&gb.dps); rt_db_free_internal(&gb.intern); return ret; } else { @@ -1579,6 +1644,7 @@ ged_brep_core(struct ged *gedp, int argc, const char *argv[]) bv_vlblock_free(gb.vbp); gb.vbp = (struct bv_vlblock *)NULL; + bu_ptbl_free(&gb.dps); rt_db_free_internal(&gb.intern); return BRLCAD_ERROR; } diff --git a/src/libged/brep/ged_brep.h b/src/libged/brep/ged_brep.h index 234b08d7820..13e7c4cf879 100644 --- a/src/libged/brep/ged_brep.h +++ b/src/libged/brep/ged_brep.h @@ -81,7 +81,7 @@ struct _ged_brep_info { struct ged *gedp = NULL; struct rt_wdb *wdbp = NULL; struct rt_db_internal intern; - struct directory *dp = NULL; + struct bu_ptbl dps; struct bv_vlblock *vbp = NULL; struct bu_color *color = NULL; int verbosity; diff --git a/src/libged/brep/tikz.cpp b/src/libged/brep/tikz.cpp index b2e8c80aae5..27cd789ac72 100644 --- a/src/libged/brep/tikz.cpp +++ b/src/libged/brep/tikz.cpp @@ -134,6 +134,12 @@ brep_tikz(struct _ged_brep_info *gb, const char *outfile) struct ged *gedp = gb->gedp; struct rt_brep_internal *brep_ip = NULL; + if (BU_PTBL_LEN(&gb->dps) != 1) { + bu_log("tikz subcommand currently supports processing only a single object at a time."); + return BRLCAD_ERROR; + } + struct directory *dp = (struct directory *)BU_PTBL_GET(&gb->dps, 0); + bu_vls_printf(&tikz, "\\documentclass{article}\n"); bu_vls_printf(&tikz, "\\usepackage{tikz}\n"); bu_vls_printf(&tikz, "\\usepackage{tikz-3dplot}\n\n"); @@ -151,7 +157,7 @@ brep_tikz(struct _ged_brep_info *gb, const char *outfile) struct bu_ptbl breps = BU_PTBL_INIT_ZERO; const char *brep_search = "-type brep"; db_update_nref(gedp->dbip, &rt_uniresource); - (void)db_search(&breps, DB_SEARCH_TREE|DB_SEARCH_RETURN_UNIQ_DP, brep_search, 1, &gb->dp, gedp->dbip, NULL); + (void)db_search(&breps, DB_SEARCH_TREE|DB_SEARCH_RETURN_UNIQ_DP, brep_search, 1, &dp, gedp->dbip, NULL); for(size_t i = 0; i < BU_PTBL_LEN(&breps); i++) { struct rt_db_internal bintern; struct rt_brep_internal *b_ip = NULL; @@ -169,14 +175,14 @@ brep_tikz(struct _ged_brep_info *gb, const char *outfile) bu_vls_printf(&tikz, "\\begin{tikzpicture}[scale=%f,tdplot_main_coords]\n", scale); - if (gb->dp->d_flags & RT_DIR_COMB) { + if (dp->d_flags & RT_DIR_COMB) { // Assign a default color bu_vls_sprintf(&color, "color={rgb:red,255;green,0;blue,0}"); - tikz_comb(gedp, &tikz, gb->dp, &color, &cnt); + tikz_comb(gedp, &tikz, dp, &color, &cnt); } else { ON_String s; if (gb->intern.idb_minor_type != DB5_MINORTYPE_BRLCAD_BREP) { - bu_vls_printf(gedp->ged_result_str, "%s is not a B-Rep - aborting\n", gb->dp->d_namep); + bu_vls_printf(gedp->ged_result_str, "%s is not a B-Rep - aborting\n", dp->d_namep); return 1; } else { brep_ip = (struct rt_brep_internal *)gb->intern.idb_ptr;