BRL-CAD
mater.c
Go to the documentation of this file.
1 /* M A T E R . 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/mater.c
21  *
22  * The mater command.
23  *
24  */
25 
26 #include "ged.h"
27 
28 #include <string.h>
29 
30 
31 int
32 ged_mater(struct ged *gedp, int argc, const char *argv[])
33 {
34  struct bu_attribute_value_set avs;
35  static const char *usage = "object_name shader r [g b] inherit";
36  static const char *prompt[] = {
37  "Name of combination to edit? ", /* unused */
38  "Specify shader. Enclose spaces within quotes. E.g., \"light invisible=1\"\nShader? ('del' to delete, '.' to skip) ",
39  "R, G, B color values (0 to 255)? ('del' to delete, '.' to skip) ",
40  "G component color value? ('.' to skip) ",
41  "B component color value? ('.' to skip) ",
42  "Should this object's shader override lower nodes? (y/n or '.' to skip) "
43  };
44 
45  struct directory *dp = NULL;
46  int r=0, g=0, b=0;
47  struct rt_comb_internal *comb = NULL;
48  struct rt_db_internal intern;
49  struct bu_vls vls = BU_VLS_INIT_ZERO;
50  int offset = 0;
51 
54  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
55 
56  /* initialize result */
57  bu_vls_trunc(gedp->ged_result_str, 0);
58 
59  /* must be wanting help */
60  if (argc == 1) {
61  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
62  return GED_HELP;
63  }
64 
65  GED_DB_LOOKUP(gedp, dp, argv[1], LOOKUP_NOISY, GED_ERROR);
66  GED_CHECK_COMB(gedp, dp, GED_ERROR);
67  GED_DB_GET_INTERNAL(gedp, &intern, dp, (fastf_t *)NULL, &rt_uniresource, GED_ERROR);
68 
69  comb = (struct rt_comb_internal *)intern.idb_ptr;
70  RT_CK_COMB(comb);
71 
72  /* deleting the color means we don't need to prompt for the green
73  * and blue color channels, so offset our argument indices.
74  */
75  if (argc > 3) {
76  struct bu_vls color = BU_VLS_INIT_ZERO;
77  bu_vls_strcat(&color, argv[3]);
78  bu_vls_trimspace(&color);
79  if (bu_strncmp(bu_vls_addr(&color), "del", 3) == 0) {
80  offset=2;
81  }
82  bu_vls_free(&color);
83  }
84 
85  /* need more arguments */
86  if ((!offset && argc < 7) || (offset && argc < 5)) {
87  /* help, let them know the old value */
88  if (argc == 2) {
89  bu_vls_printf(gedp->ged_result_str, "Current shader string = %s\n", bu_vls_addr(&comb->shader));
90  } else if (argc == 3) {
91  if (!comb->rgb_valid)
92  bu_vls_printf(gedp->ged_result_str, "Current color = (No color specified)\n");
93  else
94  bu_vls_printf(gedp->ged_result_str, "Current color = %d %d %d\n", V3ARGS(comb->rgb));
95  } else if (!offset && argc == 4) {
96  if (comb->rgb_valid)
97  bu_vls_printf(gedp->ged_result_str, "Current green color value = %d\n", comb->rgb[1]);
98  } else if (!offset && argc == 5) {
99  if (comb->rgb_valid)
100  bu_vls_printf(gedp->ged_result_str, "Current blue color value = %d\n", comb->rgb[2]);
101  } else if ((!offset && argc == 6) || (offset && argc == 4)) {
102  if (comb->inherit)
103  bu_vls_printf(gedp->ged_result_str, "Current inheritance = 1: this node overrides lower nodes\n");
104  else
105  bu_vls_printf(gedp->ged_result_str, "Current inheritance = 0: lower nodes (towards leaves) override\n");
106  }
107 
108  bu_vls_printf(gedp->ged_result_str, "%s", prompt[argc+offset-1]);
109  return GED_MORE;
110  }
111 
112 
113  /* too much */
114  if ((!offset && argc > 7) || (offset && argc > 5)) {
115  bu_vls_printf(gedp->ged_result_str, "Too many arguments.\nUsage: %s %s", argv[0], usage);
116  return GED_ERROR;
117  }
118 
119  /* Material */
120  bu_vls_strcat(&vls, argv[2]);
121  bu_vls_trimspace(&vls);
122  if (bu_vls_strlen(&vls) == 0 || bu_strncmp(bu_vls_addr(&vls), "del", 3) == 0) {
123  /* delete the current shader string */
124  bu_vls_free(&comb->shader);
125  } else {
126  if (!BU_STR_EQUAL(bu_vls_addr(&vls), ".")) {
127  bu_vls_trunc(&comb->shader, 0);
128  if (bu_shader_to_list(bu_vls_addr(&vls), &comb->shader)) {
129  bu_vls_printf(gedp->ged_result_str, "Problem with shader string [%s]", argv[2]);
130  rt_db_free_internal(&intern);
131  bu_vls_free(&vls);
132  return GED_ERROR;
133  }
134  }
135  }
136  bu_vls_free(&vls);
137 
138  /* Color */
139  if (offset) {
140  /* means argv[3] == "del" so remove the color */
141  comb->rgb_valid = 0;
142  comb->rgb[0] = comb->rgb[1] = comb->rgb[2] = 0;
143  } else {
144  struct bu_vls rgb = BU_VLS_INIT_ZERO;
145 
146  bu_vls_strcpy(&rgb, argv[3]); /* RED */
147  bu_vls_trimspace(&rgb);
148 
149  if (BU_STR_EQUAL(bu_vls_addr(&rgb), ".")) {
150  if (!comb->rgb_valid) {
151  bu_vls_printf(gedp->ged_result_str, "Color is not set, cannot skip by using existing RED color value");
152  rt_db_free_internal(&intern);
153  bu_vls_free(&rgb);
154  return GED_ERROR;
155  }
156  } else {
157  if (sscanf(bu_vls_addr(&rgb), "%d", &r) != 1 || r < 0 || 255 < r) {
158  bu_vls_printf(gedp->ged_result_str, "Bad color value [%s]", argv[3]);
159  rt_db_free_internal(&intern);
160  bu_vls_free(&rgb);
161  return GED_ERROR;
162  }
163  comb->rgb[0] = r;
164  }
165 
166  bu_vls_strcpy(&rgb, argv[4]); /* GRN */
167  bu_vls_trimspace(&rgb);
168 
169  if (BU_STR_EQUAL(bu_vls_addr(&rgb), ".")) {
170  if (!comb->rgb_valid) {
171  bu_vls_printf(gedp->ged_result_str, "Color is not set, cannot skip by using existing GREEN color value");
172  rt_db_free_internal(&intern);
173  bu_vls_free(&rgb);
174  return GED_ERROR;
175  }
176  } else {
177  if (sscanf(bu_vls_addr(&rgb), "%d", &g) != 1 || g < 0 || 255 < g) {
178  bu_vls_printf(gedp->ged_result_str, "Bad color value [%s]", argv[4]);
179  rt_db_free_internal(&intern);
180  bu_vls_free(&rgb);
181  return GED_ERROR;
182  }
183  comb->rgb[1] = g;
184  }
185 
186  bu_vls_strcpy(&rgb, argv[5]); /* BLU */
187  bu_vls_trimspace(&rgb);
188 
189  if (BU_STR_EQUAL(bu_vls_addr(&rgb), ".")) {
190  if (!comb->rgb_valid) {
191  bu_vls_printf(gedp->ged_result_str, "Color is not set, cannot skip by using existing BLUE color value");
192  rt_db_free_internal(&intern);
193  bu_vls_free(&rgb);
194  return GED_ERROR;
195  }
196  } else {
197  if (sscanf(bu_vls_addr(&rgb), "%d", &b) != 1 || b < 0 || 255 < b) {
198  bu_vls_printf(gedp->ged_result_str, "Bad color value [%s]", argv[5]);
199  rt_db_free_internal(&intern);
200  bu_vls_free(&rgb);
201  return GED_ERROR;
202  }
203  comb->rgb[2] = b;
204  }
205 
206  bu_vls_free(&rgb);
207  comb->rgb_valid = 1;
208  }
209 
210  if (!BU_STR_EQUAL(argv[argc - 1], ".")) {
211  comb->inherit = bu_str_true(argv[argc - 1]);
212  if (comb->inherit > 1) {
213  bu_vls_printf(gedp->ged_result_str, "Inherit value should be 0 or 1");
214  rt_db_free_internal(&intern);
215  return GED_ERROR;
216  }
217  }
218 
219  bu_avs_init_empty(&avs);
220  if (db5_get_attributes(gedp->ged_wdbp->dbip, &avs, dp)) {
221  bu_vls_printf(gedp->ged_result_str, "Cannot get attributes for object %s\n", dp->d_namep);
222  bu_avs_free(&avs);
223  return GED_ERROR;
224  }
225  db5_standardize_avs(&avs);
226  db5_sync_comb_to_attr(&avs, comb);
227  db5_standardize_avs(&avs);
228 
229  GED_DB_PUT_INTERNAL(gedp, dp, &intern, &rt_uniresource, GED_ERROR);
230 
231  if (db5_update_attributes(dp, &avs, gedp->ged_wdbp->dbip)) {
232  bu_vls_printf(gedp->ged_result_str, "ERROR: failed to update attributes\n");
233  bu_avs_free(&avs);
234  return GED_ERROR;
235  }
236 
237  bu_avs_free(&avs);
238 
239  return GED_OK;
240 }
241 
242 
243 /*
244  * Local Variables:
245  * tab-width: 8
246  * mode: C
247  * indent-tabs-mode: t
248  * c-file-style: "stroustrup"
249  * End:
250  * ex: shiftwidth=4 tabstop=8
251  */
#define GED_CHECK_COMB(_gedp, _dp, _flags)
Definition: ged.h:104
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
size_t db5_standardize_avs(struct bu_attribute_value_set *avs)
Definition: db5_attr.c:223
int bu_shader_to_list(const char *in, struct bu_vls *vls)
Definition: parse.c:1933
void bu_avs_init_empty(struct bu_attribute_value_set *avp)
Definition: avs.c:36
int db5_update_attributes(struct directory *dp, struct bu_attribute_value_set *avsp, struct db_i *dbip)
Definition: attributes.c:285
Definition: ged.h:338
struct db_i * dbip
Definition: raytrace.h:1266
#define GED_DB_LOOKUP(_gedp, _dp, _name, _noisy, _flags)
Definition: ged.h:223
void bu_vls_strcat(struct bu_vls *vp, const char *s)
Definition: vls.c:368
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
#define RT_CK_COMB(_p)
Definition: raytrace.h:955
struct rt_wdb * ged_wdbp
Definition: ged.h:340
unsigned char rgb[3]
Definition: raytrace.h:948
#define GED_ERROR
Definition: ged.h:61
#define GED_DB_PUT_INTERNAL(_gedp, _dp, _intern, _resource, _flags)
Definition: ged.h:243
void bu_vls_free(struct bu_vls *vp)
Definition: vls.c:248
int bu_strncmp(const char *string1, const char *string2, size_t n)
Definition: str.c:191
struct resource rt_uniresource
default. Defined in librt/globals.c
Definition: globals.c:41
#define GED_CHECK_DATABASE_OPEN(_gedp, _flags)
Definition: ged.h:114
#define V3ARGS(a)
Definition: color.c:56
struct bu_vls shader
Definition: raytrace.h:950
size_t bu_vls_strlen(const struct bu_vls *vp)
Definition: vls.c:189
char * bu_vls_addr(const struct bu_vls *vp)
Definition: vls.c:111
struct bu_vls * ged_result_str
Definition: ged.h:357
int ged_mater(struct ged *gedp, int argc, const char *argv[])
Definition: mater.c:32
void db5_sync_comb_to_attr(struct bu_attribute_value_set *avs, const struct rt_comb_internal *comb)
Definition: db5_attr.c:447
void * idb_ptr
Definition: raytrace.h:195
void bu_vls_printf(struct bu_vls *vls, const char *fmt,...) _BU_ATTR_PRINTF23
Definition: vls.c:694
#define LOOKUP_NOISY
Definition: raytrace.h:892
#define GED_HELP
Definition: ged.h:62
#define GED_DB_GET_INTERNAL(_gedp, _intern, _dp, _mat, _resource, _flags)
Definition: ged.h:233
void bu_vls_trimspace(struct bu_vls *vp)
Definition: vls.c:678
void bu_vls_strcpy(struct bu_vls *vp, const char *s)
Definition: vls.c:310
char rgb_valid
!0 ==> rgb[] has valid color
Definition: raytrace.h:947
int bu_str_true(const char *str)
Definition: booleanize.c:32
#define BU_VLS_INIT_ZERO
Definition: vls.h:84
#define GED_CHECK_READ_ONLY(_gedp, _flags)
Definition: ged.h:181
int db5_get_attributes(const struct db_i *dbip, struct bu_attribute_value_set *avs, const struct directory *dp)
Definition: db5_io.c:1027
Definition: vls.h:56
double fastf_t
Definition: defines.h:300
void bu_avs_free(struct bu_attribute_value_set *avp)
Definition: avs.c:235
void rt_db_free_internal(struct rt_db_internal *ip)
Definition: dir.c:216
#define GED_MORE
Definition: ged.h:63
#define BU_STR_EQUAL(s1, s2)
Definition: str.h:126