BRL-CAD
copyeval.c
Go to the documentation of this file.
1 /* C O P Y E V A L . 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/copyeval.c
21  *
22  * The copyeval command.
23  *
24  */
25 
26 #include "common.h"
27 
28 #include <string.h>
29 
30 #include "bu/cmd.h"
31 
32 #include "./ged_private.h"
33 
34 
35 int
36 ged_copyeval(struct ged *gedp, int argc, const char *argv[])
37 {
38  static const char *usage = "path_to_old_prim new_prim";
39  struct _ged_trace_data gtd;
40  struct directory *dp;
41  struct rt_db_internal *ip;
42  struct rt_db_internal internal, new_int;
43 
44  char *tok;
45  int endpos = 0;
46  int i;
47  mat_t start_mat;
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  if (argc != 3) {
63  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
64  return GED_ERROR;
65  }
66 
67  /* initialize gtd */
68  gtd.gtd_gedp = gedp;
69  gtd.gtd_flag = _GED_CPEVAL;
70  gtd.gtd_prflag = 0;
71 
72  /* check if new solid name already exists in description */
73  GED_CHECK_EXISTS(gedp, argv[2], LOOKUP_QUIET, GED_ERROR);
74 
75  MAT_IDN(start_mat);
76 
77  /* build directory pointer array for desired path */
78  if (strchr(argv[1], '/')) {
79  tok = strtok((char *)argv[1], "/");
80  while (tok) {
81  GED_DB_LOOKUP(gedp, gtd.gtd_obj[endpos], tok, LOOKUP_NOISY, GED_ERROR & GED_QUIET);
82  endpos++;
83  tok = strtok((char *)NULL, "/");
84  }
85  } else {
86  GED_DB_LOOKUP(gedp, gtd.gtd_obj[endpos], argv[1], LOOKUP_NOISY, GED_ERROR & GED_QUIET);
87  endpos++;
88  }
89 
90  if (endpos < 1) {
91  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
92  return GED_ERROR;
93  }
94 
95  gtd.gtd_objpos = endpos - 1;
96 
97  GED_DB_GET_INTERNAL(gedp, &internal, gtd.gtd_obj[endpos - 1], bn_mat_identity, &rt_uniresource, GED_ERROR);
98 
99  if (endpos > 1) {
100  /* Make sure that final component in path is a solid */
101  if (internal.idb_type == ID_COMBINATION) {
102  rt_db_free_internal(&internal);
103  bu_vls_printf(gedp->ged_result_str, "final component on path must be a primitive!\n");
104  return GED_ERROR;
105  }
106 
107  /* Accumulate the matrices */
108  ged_trace(gtd.gtd_obj[0], 0, start_mat, &gtd, 1);
109 
110  if (gtd.gtd_prflag == 0) {
111  bu_vls_printf(gedp->ged_result_str, "PATH: ");
112 
113  for (i = 0; i < gtd.gtd_objpos; i++)
114  bu_vls_printf(gedp->ged_result_str, "/%s", gtd.gtd_obj[i]->d_namep);
115 
116  bu_vls_printf(gedp->ged_result_str, " NOT FOUND\n");
117  rt_db_free_internal(&internal);
118  return GED_ERROR;
119  }
120 
121  /* Have found the desired path - wdb_xform is the transformation matrix */
122  /* wdb_xform matrix calculated in wdb_trace() */
123 
124  /* create the new solid */
125  RT_DB_INTERNAL_INIT(&new_int);
126  if (rt_generic_xform(&new_int, gtd.gtd_xform,
127  &internal, 0, gedp->ged_wdbp->dbip, &rt_uniresource)) {
128  rt_db_free_internal(&internal);
129  bu_vls_printf(gedp->ged_result_str, "ged_copyeval: rt_generic_xform failed\n");
130  return GED_ERROR;
131  }
132 
133  ip = &new_int;
134  } else
135  ip = &internal;
136 
137  /* should call GED_DB_DIRADD() but need to deal with freeing the
138  * internals on failure.
139  */
140  dp=db_diradd(gedp->ged_wdbp->dbip, argv[2], RT_DIR_PHONY_ADDR, 0, gtd.gtd_obj[endpos-1]->d_flags, (void *)&ip->idb_type);
141  if (dp == RT_DIR_NULL) {
142  rt_db_free_internal(&internal);
143  if (ip == &new_int)
144  rt_db_free_internal(&new_int);
145  bu_vls_printf(gedp->ged_result_str, "An error has occurred while adding a new object to the database.");
146  return GED_ERROR;
147  }
148 
149  /* should call GED_DB_DIRADD() but need to deal with freeing the
150  * internals on failure.
151  */
152  if (rt_db_put_internal(dp, gedp->ged_wdbp->dbip, ip, &rt_uniresource) < 0) {
153  /* if (ip == &new_int) then new_int gets freed by the rt_db_put_internal above
154  * regardless of whether it succeeds or not. At this point only internal needs
155  * to be freed. On the other hand if (ip == &internal), the internal gets freed
156  * freed by the rt_db_put_internal above. In this case memory for new_int has
157  * not been allocated.
158  */
159  if (ip == &new_int)
160  rt_db_free_internal(&internal);
161 
162  bu_vls_printf(gedp->ged_result_str, "Database write error, aborting");
163  return GED_ERROR;
164  }
165 
166  /* see previous comment */
167  if (ip == &new_int)
168  rt_db_free_internal(&internal);
169 
170  return GED_OK;
171 }
172 
173 
174 /*
175  * Local Variables:
176  * tab-width: 8
177  * mode: C
178  * indent-tabs-mode: t
179  * c-file-style: "stroustrup"
180  * End:
181  * ex: shiftwidth=4 tabstop=8
182  */
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
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
#define GED_DB_LOOKUP(_gedp, _dp, _name, _noisy, _flags)
Definition: ged.h:223
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 rt_wdb * ged_wdbp
Definition: ged.h:340
Header file for the BRL-CAD common definitions.
#define ID_COMBINATION
Combination Record.
Definition: raytrace.h:499
#define GED_ERROR
Definition: ged.h:61
char * strchr(const char *sp, int c)
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 _GED_CPEVAL
Definition: ged_private.h:52
#define RT_DB_INTERNAL_INIT(_p)
Definition: raytrace.h:199
#define LOOKUP_QUIET
Definition: raytrace.h:893
char * strtok(char *s, const char *delim)
#define GED_QUIET
Definition: ged.h:64
#define RT_DIR_PHONY_ADDR
Special marker for d_addr field.
Definition: raytrace.h:879
void ged_trace(struct directory *dp, int pathpos, const mat_t old_xlate, struct _ged_trace_data *gtdp, int verbose)
Definition: trace.c:82
#define GED_CHECK_EXISTS(_gedp, _name, _noisy, _flags)
Definition: ged.h:171
struct ged * gtd_gedp
Definition: ged.h:1914
struct bu_vls * ged_result_str
Definition: ged.h:357
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
mat_t gtd_xform
Definition: ged.h:1917
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
#define GED_HELP
Definition: ged.h:62
#define GED_DB_GET_INTERNAL(_gedp, _intern, _dp, _mat, _resource, _flags)
Definition: ged.h:233
#define idb_type
Definition: raytrace.h:198
int gtd_flag
Definition: ged.h:1920
#define GED_CHECK_READ_ONLY(_gedp, _flags)
Definition: ged.h:181
int d_flags
flags
Definition: raytrace.h:869
int gtd_prflag
Definition: ged.h:1919
int rt_generic_xform(struct rt_db_internal *op, const mat_t mat, struct rt_db_internal *ip, int avail, struct db_i *dbip, struct resource *resp)
Definition: generic.c:50
int ged_copyeval(struct ged *gedp, int argc, const char *argv[])
Definition: copyeval.c:36
struct directory * gtd_obj[_GED_MAX_LEVELS]
Definition: ged.h:1916
void rt_db_free_internal(struct rt_db_internal *ip)
Definition: dir.c:216
int gtd_objpos
Definition: ged.h:1918