BRL-CAD
cpi.c
Go to the documentation of this file.
1 /* C P I . 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/cpi.c
21  *
22  * The cpi 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"
33 
34 #include "./ged_private.h"
35 
36 
37 int
38 ged_cpi(struct ged *gedp, int argc, const char *argv[])
39 {
40  struct directory *proto;
41  struct directory *dp;
42  struct rt_db_internal internal;
43  struct rt_tgc_internal *tgc_ip;
44  int id;
45  static const char *usage = "from to";
46 
49  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
50 
51  /* initialize result */
52  bu_vls_trunc(gedp->ged_result_str, 0);
53 
54  /* must be wanting help */
55  if (argc == 1) {
56  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
57  return GED_HELP;
58  }
59 
60  if (argc != 3) {
61  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
62  return GED_ERROR;
63  }
64 
65  if ((proto = db_lookup(gedp->ged_wdbp->dbip, argv[1], LOOKUP_NOISY)) == RT_DIR_NULL) {
66  bu_vls_printf(gedp->ged_result_str, "%s: %s does not exist!!\n", argv[0], argv[1]);
67  return GED_ERROR;
68  }
69 
70  if (db_lookup(gedp->ged_wdbp->dbip, argv[2], LOOKUP_QUIET) != RT_DIR_NULL) {
71  bu_vls_printf(gedp->ged_result_str, "%s: %s already exists!!\n", argv[0], argv[2]);
72  return GED_ERROR;
73  }
74 
75  if ((id = rt_db_get_internal(&internal, proto, gedp->ged_wdbp->dbip, (fastf_t *)NULL, &rt_uniresource)) < 0) {
76  bu_vls_printf(gedp->ged_result_str, "%s: Database read error, aborting\n", argv[0]);
77  return GED_ERROR;
78  }
79  /* make sure it is a TGC */
80  if (id != ID_TGC) {
81  bu_vls_printf(gedp->ged_result_str, "%s: %s is not a cylinder\n", argv[0], argv[1]);
82  rt_db_free_internal(&internal);
83  return GED_ERROR;
84  }
85  tgc_ip = (struct rt_tgc_internal *)internal.idb_ptr;
86 
87  /* translate to end of "original" cylinder */
88  VADD2(tgc_ip->v, tgc_ip->v, tgc_ip->h);
89 
90  dp = db_diradd(gedp->ged_wdbp->dbip, argv[2], RT_DIR_PHONY_ADDR, 0, proto->d_flags, &proto->d_minor_type);
91  if (dp == RT_DIR_NULL) {
92  bu_vls_printf(gedp->ged_result_str, "%s: An error has occurred while adding a new object to the database.\n", argv[0]);
93  return GED_ERROR;
94  }
95 
96  if (rt_db_put_internal(dp, gedp->ged_wdbp->dbip, &internal, &rt_uniresource) < 0) {
97  bu_vls_printf(gedp->ged_result_str, "%s: Database write error, aborting\n", argv[0]);
98  return GED_ERROR;
99  }
100 
101  return GED_OK;
102 }
103 
104 
105 /*
106  * Local Variables:
107  * tab-width: 8
108  * mode: C
109  * indent-tabs-mode: t
110  * c-file-style: "stroustrup"
111  * End:
112  * ex: shiftwidth=4 tabstop=8
113  */
void usage(struct ged *gedp)
Definition: coil.c:315
#define GED_OK
Definition: ged.h:55
int rt_db_get_internal(struct rt_db_internal *ip, const struct directory *dp, const struct db_i *dbip, const mat_t mat, struct resource *resp)
Definition: dir.c:76
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
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
int ged_cpi(struct ged *gedp, int argc, const char *argv[])
Definition: cpi.c:38
struct rt_wdb * ged_wdbp
Definition: ged.h:340
Header file for the BRL-CAD common definitions.
#define GED_ERROR
Definition: ged.h:61
unsigned char d_minor_type
object minor type
Definition: raytrace.h:871
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 LOOKUP_QUIET
Definition: raytrace.h:893
#define RT_DIR_PHONY_ADDR
Special marker for d_addr field.
Definition: raytrace.h:879
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
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 ID_TGC
Generalized Truncated General Cone.
Definition: raytrace.h:460
#define GED_CHECK_READ_ONLY(_gedp, _flags)
Definition: ged.h:181
int d_flags
flags
Definition: raytrace.h:869
double fastf_t
Definition: defines.h:300
void rt_db_free_internal(struct rt_db_internal *ip)
Definition: dir.c:216