BRL-CAD
units.c
Go to the documentation of this file.
1 /* U N I T 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/units.c
21  *
22  * The units 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_units(struct ged *gedp, int argc, const char *argv[])
38 {
39  double loc2mm;
40  const char *str;
41  int sflag = 0;
42  static const char *usage = "[-s] [mm|cm|m|in|ft|...]";
43 
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  if (argc > 2) {
51  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
52  return GED_ERROR;
53  }
54 
55  if (argc == 2) {
56  if (BU_STR_EQUAL(argv[1], "-s")) {
57  --argc;
58  ++argv;
59 
60  sflag = 1;
61  } else if (BU_STR_EQUAL(argv[1], "-t")) {
62  struct bu_vls *vlsp = bu_units_strings_vls();
63 
64  bu_vls_printf(gedp->ged_result_str, "%s", bu_vls_addr(vlsp));
65  bu_vls_free(vlsp);
66  bu_free(vlsp, "ged_units: vlsp");
67 
68  return GED_OK;
69  }
70  }
71 
72  /* Get units */
73  if (argc == 1) {
75  if (!str) str = "Unknown_unit";
76 
77  if (sflag)
78  bu_vls_printf(gedp->ged_result_str, "%s", str);
79  else
80  bu_vls_printf(gedp->ged_result_str, "You are editing in '%s'. 1 %s = %g mm \n",
81  str, str, gedp->ged_wdbp->dbip->dbi_local2base);
82 
83  return GED_OK;
84  }
85 
86  /* Set units */
87  /* Allow inputs of the form "25cm" or "3ft" */
88  if ((loc2mm = bu_mm_value(argv[1])) <= 0) {
90  "%s: unrecognized unit\nvalid units: <um|mm|cm|m|km|in|ft|yd|mi>\n",
91  argv[1]);
92  return GED_ERROR;
93  }
94 
95  if (db_update_ident(gedp->ged_wdbp->dbip, gedp->ged_wdbp->dbip->dbi_title, loc2mm) < 0) {
96  bu_vls_printf(gedp->ged_result_str, "Warning: unable to stash working units into database\n");
97  }
98 
99  gedp->ged_wdbp->dbip->dbi_local2base = loc2mm;
100  gedp->ged_wdbp->dbip->dbi_base2local = 1.0 / loc2mm;
101 
103  if (!str) str = "Unknown_unit";
104  bu_vls_printf(gedp->ged_result_str, "You are now editing in '%s'. 1 %s = %g mm \n",
105  str, str, gedp->ged_wdbp->dbip->dbi_local2base);
106 
107  return GED_OK;
108 }
109 
110 
111 /*
112  * Local Variables:
113  * tab-width: 8
114  * mode: C
115  * indent-tabs-mode: t
116  * c-file-style: "stroustrup"
117  * End:
118  * ex: shiftwidth=4 tabstop=8
119  */
void usage(struct ged *gedp)
Definition: coil.c:315
#define GED_OK
Definition: ged.h:55
Definition: ged.h:338
struct db_i * dbip
Definition: raytrace.h:1266
const char * bu_units_string(register const double mm)
Definition: units.c:256
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.
double bu_mm_value(const char *s)
Definition: units.c:355
#define GED_ERROR
Definition: ged.h:61
void bu_vls_free(struct bu_vls *vp)
Definition: vls.c:248
#define GED_CHECK_DATABASE_OPEN(_gedp, _flags)
Definition: ged.h:114
char * dbi_title
title from IDENT rec
Definition: raytrace.h:809
struct bu_vls * bu_units_strings_vls()
Definition: units.c:291
int db_update_ident(struct db_i *dbip, const char *title, double local2mm)
Definition: db_scan.c:367
char * bu_vls_addr(const struct bu_vls *vp)
Definition: vls.c:111
struct bu_vls * ged_result_str
Definition: ged.h:357
double dbi_base2local
unit conversion factors
Definition: raytrace.h:808
void bu_vls_printf(struct bu_vls *vls, const char *fmt,...) _BU_ATTR_PRINTF23
Definition: vls.c:694
void bu_free(void *ptr, const char *str)
Definition: malloc.c:328
double dbi_local2base
local2mm
Definition: raytrace.h:807
int ged_units(struct ged *gedp, int argc, const char *argv[])
Definition: units.c:37
Definition: vls.h:56
#define BU_STR_EQUAL(s1, s2)
Definition: str.h:126