BRL-CAD
shells.c
Go to the documentation of this file.
1 /* S H E L L S . 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/shells.c
21  *
22  * The shells 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_shells(struct ged *gedp, int argc, const char *argv[])
37 {
38  struct directory *old_dp, *new_dp;
39  struct rt_db_internal old_intern, new_intern;
40  struct model *m_tmp, *m;
41  struct nmgregion *r_tmp, *r;
42  struct shell *s_tmp, *s;
43  int shell_count=0;
44  struct bu_vls shell_name = BU_VLS_INIT_ZERO;
45  long **trans_tbl;
46  static const char *usage = "nmg_model";
47 
50  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
51 
52  /* initialize result */
53  bu_vls_trunc(gedp->ged_result_str, 0);
54 
55  /* must be wanting help */
56  if (argc == 1) {
57  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
58  return GED_HELP;
59  }
60 
61  if (argc != 2) {
62  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
63  return GED_ERROR;
64  }
65 
66  if ((old_dp = db_lookup(gedp->ged_wdbp->dbip, argv[1], LOOKUP_NOISY)) == RT_DIR_NULL)
67  return GED_ERROR;
68 
69  if (rt_db_get_internal(&old_intern, old_dp, gedp->ged_wdbp->dbip, bn_mat_identity, &rt_uniresource) < 0) {
70  bu_vls_printf(gedp->ged_result_str, "rt_db_get_internal() error\n");
71  return GED_ERROR;
72  }
73 
74  if (old_intern.idb_type != ID_NMG) {
75  bu_vls_printf(gedp->ged_result_str, "Object is not an NMG!!!\n");
76  return GED_ERROR;
77  }
78 
79  m = (struct model *)old_intern.idb_ptr;
80  NMG_CK_MODEL(m);
81 
82  for (BU_LIST_FOR(r, nmgregion, &m->r_hd)) {
83  for (BU_LIST_FOR(s, shell, &r->s_hd)) {
84  s_tmp = nmg_dup_shell(s, &trans_tbl, &gedp->ged_wdbp->wdb_tol);
85  bu_free((void *)trans_tbl, "trans_tbl");
86 
87  m_tmp = nmg_mmr();
88  r_tmp = BU_LIST_FIRST(nmgregion, &m_tmp->r_hd);
89 
90  BU_LIST_DEQUEUE(&s_tmp->l);
91  BU_LIST_APPEND(&r_tmp->s_hd, &s_tmp->l);
92  s_tmp->r_p = r_tmp;
93  nmg_m_reindex(m_tmp, 0);
94  nmg_m_reindex(m, 0);
95 
96  bu_vls_printf(&shell_name, "shell.%d", shell_count);
97  while (db_lookup(gedp->ged_wdbp->dbip, bu_vls_addr(&shell_name), 0) != RT_DIR_NULL) {
98  bu_vls_trunc(&shell_name, 0);
99  shell_count++;
100  bu_vls_printf(&shell_name, "shell.%d", shell_count);
101  }
102 
103  /* Export NMG as a new solid */
104  RT_DB_INTERNAL_INIT(&new_intern);
105  new_intern.idb_major_type = DB5_MAJORTYPE_BRLCAD;
106  new_intern.idb_type = ID_NMG;
107  new_intern.idb_meth = &OBJ[ID_NMG];
108  new_intern.idb_ptr = (void *)m_tmp;
109 
110  new_dp=db_diradd(gedp->ged_wdbp->dbip, bu_vls_addr(&shell_name), RT_DIR_PHONY_ADDR, 0, RT_DIR_SOLID, (void *)&new_intern.idb_type);
111  if (new_dp == RT_DIR_NULL) {
112  bu_vls_printf(gedp->ged_result_str, "An error has occurred while adding a new object to the database.\n");
113  return GED_ERROR;
114  }
115 
116  /* make sure the geometry/bounding boxes are up to date */
117  nmg_rebound(m_tmp, &gedp->ged_wdbp->wdb_tol);
118 
119  if (rt_db_put_internal(new_dp, gedp->ged_wdbp->dbip, &new_intern, &rt_uniresource) < 0) {
120  /* Free memory */
121  nmg_km(m_tmp);
122  bu_vls_printf(gedp->ged_result_str, "rt_db_put_internal() failure\n");
123  return GED_ERROR;
124  }
125  /* Internal representation has been freed by rt_db_put_internal */
126  new_intern.idb_ptr = (void *)NULL;
127  }
128  }
129  bu_vls_free(&shell_name);
130 
131  return GED_OK;
132 }
133 
134 
135 /*
136  * Local Variables:
137  * tab-width: 8
138  * mode: C
139  * indent-tabs-mode: t
140  * c-file-style: "stroustrup"
141  * End:
142  * ex: shiftwidth=4 tabstop=8
143  */
void usage(struct ged *gedp)
Definition: coil.c:315
#define GED_OK
Definition: ged.h:55
#define BU_LIST_FOR(p, structure, hp)
Definition: list.h:365
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
const mat_t bn_mat_identity
Matrix and vector functionality.
Definition: mat.c:46
struct db_i * dbip
Definition: raytrace.h:1266
if lu s
Definition: nmg_mod.c:3860
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 model * nmg_mmr(void)
Definition: nmg_mk.c:268
void nmg_rebound(struct model *m, const struct bn_tol *tol)
Definition: nmg_misc.c:2072
struct rt_wdb * ged_wdbp
Definition: ged.h:340
Header file for the BRL-CAD common definitions.
#define BU_LIST_APPEND(old, new)
Definition: list.h:197
#define GED_ERROR
Definition: ged.h:61
int idb_major_type
Definition: raytrace.h:192
void bu_vls_free(struct bu_vls *vp)
Definition: vls.c:248
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 RT_DIR_SOLID
this name is a solid
Definition: raytrace.h:883
#define RT_DB_INTERNAL_INIT(_p)
Definition: raytrace.h:199
const struct rt_functab * idb_meth
for ft_ifree(), etc.
Definition: raytrace.h:194
#define ID_NMG
n-Manifold Geometry solid
Definition: raytrace.h:469
void nmg_km(struct model *m)
Definition: nmg_mk.c:1634
#define RT_DIR_PHONY_ADDR
Special marker for d_addr field.
Definition: raytrace.h:879
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_shells(struct ged *gedp, int argc, const char *argv[])
Definition: shells.c:36
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 * idb_ptr
Definition: raytrace.h:195
struct bn_tol wdb_tol
Definition: raytrace.h:1269
const struct rt_functab OBJ[]
Definition: table.c:159
void nmg_m_reindex(struct model *m, register long int newindex)
Definition: nmg_index.c:298
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
struct shell * nmg_dup_shell(struct shell *s, long int ***trans_tbl, const struct bn_tol *tol)
Definition: nmg_misc.c:2673
#define GED_HELP
Definition: ged.h:62
void bu_free(void *ptr, const char *str)
Definition: malloc.c:328
#define BU_VLS_INIT_ZERO
Definition: vls.h:84
#define BU_LIST_DEQUEUE(cur)
Definition: list.h:209
#define GED_CHECK_READ_ONLY(_gedp, _flags)
Definition: ged.h:181
Definition: vls.h:56
#define BU_LIST_FIRST(structure, hp)
Definition: list.h:312