BRL-CAD
bot_split.c
Go to the documentation of this file.
1 /* B O T _ S P L I T . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2008-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 /** @file libged/bot_split.c
21  *
22  * The bot_split command.
23  *
24  */
25 
26 #include "common.h"
27 
28 #include <stdlib.h>
29 #include <ctype.h>
30 #include <string.h>
31 
32 #include "rtgeom.h" /* for rt_bot_split (in raytrace.h) */
33 #include "./ged_private.h"
34 
35 
36 int
37 ged_bot_split(struct ged *gedp, int argc, const char *argv[])
38 {
39  static const char *usage = "bot [bot2 bot3 ...]";
40 
41  int i;
42  struct bu_vls bot_result_list = BU_VLS_INIT_ZERO;
43  struct bu_vls error_str = BU_VLS_INIT_ZERO;
44  struct bu_vls new_bots = BU_VLS_INIT_ZERO;
45  struct directory *dp;
46  struct rt_bot_internal *bot;
47  struct rt_db_internal intern;
48 
51  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
52 
53  /* initialize result */
54  bu_vls_trunc(gedp->ged_result_str, 0);
55 
56  /* must be wanting help */
57  if (argc == 1) {
58  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
59  return GED_HELP;
60  }
61 
62  for (i = 1; i < argc; ++i) {
63  struct rt_bot_list *headRblp = NULL;
64  /* Skip past any path elements */
65  char *obj = (char *)bu_calloc(strlen(argv[i]), sizeof(char), "ged_bot_split obj");
66  bu_basename(obj, argv[i]);
67 
68  if (BU_STR_EQUAL(obj, ".")) {
69  /* malformed path, lookup using exactly what was provided */
70  bu_free(obj, "free bu_basename");
71  obj = bu_strdup(argv[i]);
72  }
73 
74  if ((dp = db_lookup(gedp->ged_wdbp->dbip, obj, LOOKUP_QUIET)) == RT_DIR_NULL) {
75  bu_vls_printf(&error_str, "%s: db_lookup(%s) error\n", argv[0], obj);
76  bu_free(obj, "free obj");
77  continue;
78  }
79 
81 
82  if (intern.idb_major_type != DB5_MAJORTYPE_BRLCAD || intern.idb_minor_type != DB5_MINORTYPE_BRLCAD_BOT) {
83  rt_db_free_internal(&intern);
84  bu_vls_printf(&error_str, "%s: %s is not a BOT solid!\n", argv[0], obj);
85  bu_free(obj, "free obj");
86  continue;
87  }
88 
89  bot = (struct rt_bot_internal *)intern.idb_ptr;
90  headRblp = rt_bot_split(bot);
91 
92  {
93  int ac = 3;
94  const char *av[4];
95  struct rt_db_internal bot_intern;
96  struct rt_bot_list *rblp;
97 
98  av[0] = "make_name";
99  av[1] = "-s";
100  av[2] = "0";
101  av[3] = (char *)0;
102 
103  /* Set make_name's count to 0 */
104  ged_make_name(gedp, ac, av);
105 
106  ac = 2;
107  av[2] = (char *)0;
108 
109  for (BU_LIST_FOR(rblp, rt_bot_list, &headRblp->l)) {
110  /* Get a unique name based on the original name */
111  av[1] = obj;
112  ged_make_name(gedp, ac, av);
113 
114  /* Create the bot */
115  RT_DB_INTERNAL_INIT(&bot_intern);
116  bot_intern.idb_major_type = DB5_MAJORTYPE_BRLCAD;
117  bot_intern.idb_type = ID_BOT;
118  bot_intern.idb_meth = &OBJ[ID_BOT];
119  bot_intern.idb_ptr = (void *)rblp->bot;
120 
121  /* Save new bot name for later use */
122  bu_vls_printf(&new_bots, "%s ", bu_vls_addr(gedp->ged_result_str));
123 
124  dp = db_diradd(gedp->ged_wdbp->dbip, bu_vls_addr(gedp->ged_result_str), RT_DIR_PHONY_ADDR, 0, RT_DIR_SOLID, (void *)&bot_intern.idb_type);
125  if (dp == RT_DIR_NULL) {
126  bu_vls_printf(&error_str, " failed to be added to the database.\n");
127  rt_bot_list_free(headRblp, 0);
128  rt_db_free_internal(&intern);
129  } else {
130  if (rt_db_put_internal(dp, gedp->ged_wdbp->dbip, &bot_intern, &rt_uniresource) < 0) {
131  bu_vls_printf(&error_str, " failed to be added to the database.\n");
132  rt_bot_list_free(headRblp, 0);
133  rt_db_free_internal(&intern);
134  }
135  }
136  }
137 
138  /* Save the name of the original bot and the new bots as a sublist */
139  bu_vls_printf(&bot_result_list, "{%s {%s}} ", obj, bu_vls_addr(&new_bots));
140 
141  bu_vls_trunc(gedp->ged_result_str, 0);
142  bu_vls_trunc(&new_bots, 0);
143  }
144 
145  rt_bot_list_free(headRblp, 0);
146  rt_db_free_internal(&intern);
147  bu_free(obj, "free obj");
148  }
149 
150  bu_vls_trunc(gedp->ged_result_str, 0);
151  bu_vls_printf(gedp->ged_result_str, "%s {%s}", bu_vls_addr(&bot_result_list), bu_vls_addr(&error_str));
152 
153  bu_vls_free(&bot_result_list);
154  bu_vls_free(&new_bots);
155  bu_vls_free(&error_str);
156 
157  return GED_OK;
158 }
159 
160 
161 /*
162  * Local Variables:
163  * tab-width: 8
164  * mode: C
165  * indent-tabs-mode: t
166  * c-file-style: "stroustrup"
167  * End:
168  * ex: shiftwidth=4 tabstop=8
169  */
void usage(struct ged *gedp)
Definition: coil.c:315
#define GED_OK
Definition: ged.h:55
#define BU_LIST_FOR(p, structure, hp)
Definition: list.h:365
int rt_db_put_internal(struct directory *dp, struct db_i *dbip, struct rt_db_internal *ip, struct resource *resp)
Definition: dir.c:136
Definition: ged.h:338
const mat_t bn_mat_identity
Matrix and vector functionality.
Definition: mat.c:46
struct db_i * dbip
Definition: raytrace.h:1266
void bu_vls_trunc(struct bu_vls *vp, int len)
Definition: vls.c:198
#define ID_BOT
Bag o' triangles.
Definition: raytrace.h:488
#define GED_CHECK_ARGC_GT_0(_gedp, _argc, _flags)
Definition: ged.h:202
struct directory * db_lookup(const struct db_i *, const char *name, int noisy)
Definition: db_lookup.c:153
struct rt_wdb * ged_wdbp
Definition: ged.h:340
Header file for the BRL-CAD common definitions.
#define GED_ERROR
Definition: ged.h:61
struct rt_bot_list * rt_bot_split(struct rt_bot_internal *bot)
Definition: bot.c:4966
int idb_major_type
Definition: raytrace.h:192
void bu_vls_free(struct bu_vls *vp)
Definition: vls.c:248
struct resource rt_uniresource
default. Defined in librt/globals.c
Definition: globals.c:41
void rt_bot_list_free(struct rt_bot_list *headRblp, int fbflag)
Definition: bot.c:5200
#define GED_CHECK_DATABASE_OPEN(_gedp, _flags)
Definition: ged.h:114
int ged_bot_split(struct ged *gedp, int argc, const char *argv[])
Definition: bot_split.c:37
void * bu_calloc(size_t nelem, size_t elsize, const char *str)
Definition: malloc.c:321
#define RT_DIR_SOLID
this name is a solid
Definition: raytrace.h:883
#define RT_DB_INTERNAL_INIT(_p)
Definition: raytrace.h:199
#define LOOKUP_QUIET
Definition: raytrace.h:893
const struct rt_functab * idb_meth
for ft_ifree(), etc.
Definition: raytrace.h:194
#define RT_DIR_PHONY_ADDR
Special marker for d_addr field.
Definition: raytrace.h:879
char * bu_vls_addr(const struct bu_vls *vp)
Definition: vls.c:111
struct bu_vls * ged_result_str
Definition: ged.h:357
void bu_basename(char *basename, const char *path)
Definition: basename.c:30
struct directory * db_diradd(struct db_i *, const char *name, off_t laddr, size_t len, int flags, void *ptr)
Definition: db_lookup.c:190
void * idb_ptr
Definition: raytrace.h:195
const struct rt_functab OBJ[]
Definition: table.c:159
void bu_vls_printf(struct bu_vls *vls, const char *fmt,...) _BU_ATTR_PRINTF23
Definition: vls.c:694
#define RT_DIR_NULL
Definition: raytrace.h:875
#define GED_HELP
Definition: ged.h:62
int idb_minor_type
ID_xxx.
Definition: raytrace.h:193
#define GED_DB_GET_INTERNAL(_gedp, _intern, _dp, _mat, _resource, _flags)
Definition: ged.h:233
void bu_free(void *ptr, const char *str)
Definition: malloc.c:328
#define BU_VLS_INIT_ZERO
Definition: vls.h:84
int ged_make_name(struct ged *gedp, int argc, const char *argv[])
Definition: make_name.c:31
#define GED_CHECK_READ_ONLY(_gedp, _flags)
Definition: ged.h:181
Definition: vls.h:56
void rt_db_free_internal(struct rt_db_internal *ip)
Definition: dir.c:216
#define bu_strdup(s)
Definition: str.h:71
#define BU_STR_EQUAL(s1, s2)
Definition: str.h:126