BRL-CAD
region.c
Go to the documentation of this file.
1 /* R E G I O N . 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/region.c
21  *
22  * The region command.
23  *
24  */
25 
26 #include "common.h"
27 
28 #include <string.h>
29 
30 #include "bu/cmd.h"
31 #include "wdb.h"
32 
33 #include "./ged_private.h"
34 
35 
36 int
37 ged_region(struct ged *gedp, int argc, const char *argv[])
38 {
39  struct directory *dp;
40  int i;
41  int ident, air;
42  db_op_t oper;
43  static const char *usage = "reg_name <op obj ...>";
44 
47  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
48 
49  /* initialize result */
50  bu_vls_trunc(gedp->ged_result_str, 0);
51 
52  /* must be wanting help */
53  if (argc == 1) {
54  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
55  return GED_HELP;
56  }
57 
58  if (argc < 4) {
59  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
60  return GED_ERROR;
61  }
62 
63  ident = gedp->ged_wdbp->wdb_item_default;
64  air = gedp->ged_wdbp->wdb_air_default;
65 
66  /* Check for even number of arguments */
67  if (argc & 01) {
68  bu_vls_printf(gedp->ged_result_str, "error in number of args!");
69  return GED_ERROR;
70  }
71 
72  if (db_lookup(gedp->ged_wdbp->dbip, argv[1], LOOKUP_QUIET) == RT_DIR_NULL) {
73  /* will attempt to create the region */
74  if (gedp->ged_wdbp->wdb_item_default) {
75  gedp->ged_wdbp->wdb_item_default++;
76  bu_vls_printf(gedp->ged_result_str, "Defaulting item number to %d\n",
77  gedp->ged_wdbp->wdb_item_default);
78  }
79  }
80 
81  /* Get operation and solid name for each solid */
82  for (i = 2; i < argc; i += 2) {
83  if ((dp = db_lookup(gedp->ged_wdbp->dbip, argv[i+1], LOOKUP_NOISY)) == RT_DIR_NULL) {
84  bu_vls_printf(gedp->ged_result_str, "skipping %s\n", argv[i+1]);
85  continue;
86  }
87 
88  oper = db_str2op(argv[i]);
89  if (oper == DB_OP_NULL) {
90  bu_vls_printf(gedp->ged_result_str, "bad operation: %c (0x%x) skip member: %s\n", argv[i][0], argv[i][0], dp->d_namep);
91  continue;
92  }
93 
94  /* Adding region to region */
95  if (dp->d_flags & RT_DIR_REGION) {
96  bu_vls_printf(gedp->ged_result_str, "Note: %s is a region\n", dp->d_namep);
97  }
98 
99  if (_ged_combadd(gedp, dp, (char *)argv[1], 1, oper, ident, air) == RT_DIR_NULL) {
100  bu_vls_printf(gedp->ged_result_str, "error in combadd");
101  return GED_ERROR;
102  }
103  }
104 
105  if (db_lookup(gedp->ged_wdbp->dbip, argv[1], LOOKUP_QUIET) == RT_DIR_NULL) {
106  /* failed to create region */
107  if (gedp->ged_wdbp->wdb_item_default > 1)
108  gedp->ged_wdbp->wdb_item_default--;
109  return GED_ERROR;
110  }
111 
112  return GED_OK;
113 }
114 
115 
116 /*
117  * Local Variables:
118  * tab-width: 8
119  * mode: C
120  * indent-tabs-mode: t
121  * c-file-style: "stroustrup"
122  * End:
123  * ex: shiftwidth=4 tabstop=8
124  */
int ged_region(struct ged *gedp, int argc, const char *argv[])
Definition: region.c:37
int wdb_item_default
GIFT region ID.
Definition: raytrace.h:1278
void usage(struct ged *gedp)
Definition: coil.c:315
#define GED_OK
Definition: ged.h:55
char * d_namep
pointer to name string
Definition: raytrace.h:859
Definition: ged.h:338
struct db_i * dbip
Definition: raytrace.h:1266
void bu_vls_trunc(struct bu_vls *vp, int len)
Definition: vls.c:198
#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.
db_op_t db_str2op(const char *str)
Definition: op.c:31
#define RT_DIR_REGION
region
Definition: raytrace.h:885
#define GED_ERROR
Definition: ged.h:61
#define GED_CHECK_DATABASE_OPEN(_gedp, _flags)
Definition: ged.h:114
db_op_t
Definition: op.h:33
#define LOOKUP_QUIET
Definition: raytrace.h:893
struct bu_vls * ged_result_str
Definition: ged.h:357
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 LOOKUP_NOISY
Definition: raytrace.h:892
Definition: op.h:34
#define GED_HELP
Definition: ged.h:62
int wdb_air_default
Definition: raytrace.h:1279
struct directory * _ged_combadd(struct ged *gedp, struct directory *objp, char *combname, int region_flag, db_op_t relation, int ident, int air)
Definition: comb.c:680
#define GED_CHECK_READ_ONLY(_gedp, _flags)
Definition: ged.h:181
int d_flags
flags
Definition: raytrace.h:869