BRL-CAD
copy.c
Go to the documentation of this file.
1 /* C O P Y . 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/copy.c
21  *
22  * The copy command.
23  *
24  */
25 
26 #include "common.h"
27 
28 #include <string.h>
29 
30 #include "bu/cmd.h"
31 #include "bu/units.h"
32 
33 #include "./ged_private.h"
34 
35 
36 int
37 ged_copy(struct ged *gedp, int argc, const char *argv[])
38 {
39  struct directory *from_dp;
40  struct bu_external external;
41  static const char *usage = "from to";
42 
45  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
46 
47  /* initialize result */
48  bu_vls_trunc(gedp->ged_result_str, 0);
49 
50  /* must be wanting help */
51  if (argc == 1) {
52  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
53  return GED_HELP;
54  }
55 
56  if (argc != 3) {
57  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
58  return GED_ERROR;
59  }
60 
61  GED_DB_LOOKUP(gedp, from_dp, argv[1], LOOKUP_NOISY, GED_ERROR & GED_QUIET);
62  GED_CHECK_EXISTS(gedp, argv[2], LOOKUP_QUIET, GED_ERROR);
63 
64  if (db_get_external(&external, from_dp, gedp->ged_wdbp->dbip)) {
65  bu_vls_printf(gedp->ged_result_str, "Database read error, aborting\n");
66  return GED_ERROR;
67  }
68 
69  if (wdb_export_external(gedp->ged_wdbp, &external, argv[2],
70  from_dp->d_flags, from_dp->d_minor_type) < 0) {
71  bu_free_external(&external);
73  "Failed to write new object (%s) to database - aborting!!\n",
74  argv[2]);
75  return GED_ERROR;
76  }
77 
78  bu_free_external(&external);
79 
80  return GED_OK;
81 }
82 
83 
84 int
85 ged_dbcopy(struct ged *from_gedp, struct ged *to_gedp, const char *from, const char *to, int fflag)
86 {
87  struct directory *from_dp;
88  struct bu_external external;
89 
93 
94  /* initialize result */
95  bu_vls_trunc(from_gedp->ged_result_str, 0);
96  bu_vls_trunc(to_gedp->ged_result_str, 0);
97 
98  GED_DB_LOOKUP(from_gedp, from_dp, from, LOOKUP_NOISY, GED_ERROR & GED_QUIET);
99 
100  if (!fflag && db_lookup(to_gedp->ged_wdbp->dbip, to, LOOKUP_QUIET) != RT_DIR_NULL) {
101  bu_vls_printf(from_gedp->ged_result_str, "%s already exists.", to);
102  return GED_ERROR;
103  }
104 
105  if (db_get_external(&external, from_dp, from_gedp->ged_wdbp->dbip)) {
106  bu_vls_printf(from_gedp->ged_result_str, "Database read error, aborting\n");
107  return GED_ERROR;
108  }
109 
110  if (wdb_export_external(to_gedp->ged_wdbp, &external, to,
111  from_dp->d_flags, from_dp->d_minor_type) < 0) {
112  bu_free_external(&external);
113  bu_vls_printf(from_gedp->ged_result_str,
114  "Failed to write new object (%s) to database - aborting!!\n",
115  to);
116  return GED_ERROR;
117  }
118 
119  bu_free_external(&external);
120 
121  /* Need to do something extra for _GLOBAL */
122  if (db_version(to_gedp->ged_wdbp->dbip) > 4 && BU_STR_EQUAL(to, DB5_GLOBAL_OBJECT_NAME)) {
123  struct directory *to_dp;
124  struct bu_attribute_value_set avs;
125  const char *val;
126 
127  GED_DB_LOOKUP(to_gedp, to_dp, to, LOOKUP_NOISY, GED_ERROR & GED_QUIET);
128 
129  bu_avs_init_empty(&avs);
130  if (db5_get_attributes(to_gedp->ged_wdbp->dbip, &avs, to_dp)) {
131  bu_vls_printf(from_gedp->ged_result_str, "Cannot get attributes for object %s\n", to_dp->d_namep);
132  return GED_ERROR;
133  }
134 
135  if ((val = bu_avs_get(&avs, "title")) != NULL)
136  to_gedp->ged_wdbp->dbip->dbi_title = strdup(val);
137 
138  if ((val = bu_avs_get(&avs, "units")) != NULL) {
139  double loc2mm;
140 
141  if ((loc2mm = bu_mm_value(val)) > 0) {
142  to_gedp->ged_wdbp->dbip->dbi_local2base = loc2mm;
143  to_gedp->ged_wdbp->dbip->dbi_base2local = 1.0 / loc2mm;
144  }
145  }
146 
147  if ((val = bu_avs_get(&avs, "regionid_colortable")) != NULL) {
148  rt_color_free();
149  db5_import_color_table((char *)val);
150  }
151 
152  bu_avs_free(&avs);
153  }
154 
155  return GED_OK;
156 }
157 
158 
159 /*
160  * Local Variables:
161  * tab-width: 8
162  * mode: C
163  * indent-tabs-mode: t
164  * c-file-style: "stroustrup"
165  * End:
166  * ex: shiftwidth=4 tabstop=8
167  */
Definition: db_flip.c:35
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
void bu_avs_init_empty(struct bu_attribute_value_set *avp)
Definition: avs.c:36
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_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
int db_version(struct db_i *dbip)
Definition: db5_scan.c:414
void bu_free_external(struct bu_external *ep)
struct rt_wdb * ged_wdbp
Definition: ged.h:340
Header file for the BRL-CAD common definitions.
double bu_mm_value(const char *s)
Definition: units.c:355
#define GED_ERROR
Definition: ged.h:61
const char * bu_avs_get(const struct bu_attribute_value_set *avp, const char *attribute)
Definition: avs.c:172
unsigned char d_minor_type
object minor type
Definition: raytrace.h:871
#define GED_CHECK_DATABASE_OPEN(_gedp, _flags)
Definition: ged.h:114
char * dbi_title
title from IDENT rec
Definition: raytrace.h:809
int db_get_external(struct bu_external *ep, const struct directory *dp, const struct db_i *dbip)
int wdb_export_external(struct rt_wdb *wdbp, struct bu_external *ep, const char *name, int flags, unsigned char minor_type)
Definition: wdb.c:105
#define LOOKUP_QUIET
Definition: raytrace.h:893
#define GED_QUIET
Definition: ged.h:64
int ged_copy(struct ged *gedp, int argc, const char *argv[])
Definition: copy.c:37
#define GED_CHECK_EXISTS(_gedp, _name, _noisy, _flags)
Definition: ged.h:171
struct bu_vls * ged_result_str
Definition: ged.h:357
double dbi_base2local
unit conversion factors
Definition: raytrace.h:808
void db5_import_color_table(char *cp)
Definition: db5_io.c:996
int ged_dbcopy(struct ged *from_gedp, struct ged *to_gedp, const char *from, const char *to, int fflag)
Definition: copy.c:85
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
char * strdup(const char *cp)
double dbi_local2base
local2mm
Definition: raytrace.h:807
#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
int d_flags
flags
Definition: raytrace.h:869
void bu_avs_free(struct bu_attribute_value_set *avp)
Definition: avs.c:235
void rt_color_free(void)
Definition: mater.c:294
#define BU_STR_EQUAL(s1, s2)
Definition: str.h:126