Remove debug code from export code
[survex.git] / src / cad3d.cc
blob7d9369b964f3c431e09e91180ba07dadd5263676
1 /* cad3d.cc
2 * Converts a .3d file to CAD-like formats (DXF, Skencil, SVG) and also Compass
3 * PLT.
4 */
6 /* Copyright (C) 1994-2004,2008,2010,2011,2013,2014,2018 Olly Betts
7 * Copyright (C) 2004 John Pybus (SVG Output code)
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
28 #include <math.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
33 #include "export.h"
34 #include "mainfrm.h"
36 #include "cmdline.h"
37 #include "filename.h"
38 #include "img_hosted.h"
39 #include "message.h"
40 #include "str.h"
41 #include "useful.h"
43 /* default to DXF */
44 #define FMT_DEFAULT FMT_DXF
46 int
47 main(int argc, char **argv)
49 double pan = 0;
50 double tilt = -90.0;
51 export_format format;
52 bool format_set = false;
53 int show_mask = 0;
54 const char *survey = NULL;
55 double grid = 0.0; /* grid spacing (or 0 for no grid) */
56 double text_height = DEFAULT_TEXT_HEIGHT; /* for station labels */
57 double marker_size = DEFAULT_MARKER_SIZE; /* for station markers */
58 double scale = 500.0;
60 /* Defaults */
61 show_mask |= STNS;
62 show_mask |= LABELS;
63 show_mask |= LEGS;
64 show_mask |= SURF;
66 /* TRANSLATE */
67 static const struct option long_opts[] = {
68 /* const char *name; int has_arg (0 no_argument, 1 required, 2 options_*); int *flag; int val */
69 {"survey", required_argument, 0, 's'},
70 {"no-crosses", no_argument, 0, 'c'},
71 {"no-station-names", no_argument, 0, 'n'},
72 {"no-legs", no_argument, 0, 'l'},
73 {"grid", optional_argument, 0, 'g'},
74 {"text-height", required_argument, 0, 't'},
75 {"marker-size", required_argument, 0, 'm'},
76 {"elevation", required_argument, 0, 'e'},
77 {"reduction", required_argument, 0, 'r'},
78 {"dxf", no_argument, 0, 'D'},
79 {"skencil", no_argument, 0, 'S'},
80 {"plt", no_argument, 0, 'P'},
81 {"svg", no_argument, 0, 'V'},
82 {"help", no_argument, 0, HLP_HELP},
83 {"version", no_argument, 0, HLP_VERSION},
84 /* Old name for --skencil: */
85 {"sketch", no_argument, 0, 'S'},
86 {0,0,0,0}
89 #define short_opts "s:cnlg::t:m:e:r:DSPV"
91 /* TRANSLATE */
92 static struct help_msg help[] = {
93 /* <-- */
94 {HLP_ENCODELONG(0), /*only load the sub-survey with this prefix*/199, 0},
95 {HLP_ENCODELONG(1), /*do not generate station markers*/100, 0},
96 {HLP_ENCODELONG(2), /*do not generate station labels*/101, 0},
97 {HLP_ENCODELONG(3), /*do not generate survey legs*/102, 0},
98 {HLP_ENCODELONG(4), /*generate grid (default %sm)*/148, STRING(DEFAULT_GRID_SPACING)},
99 {HLP_ENCODELONG(5), /*station labels text height (default %s)*/149, STRING(DEFAULT_TEXT_HEIGHT)},
100 {HLP_ENCODELONG(6), /*station marker size (default %s)*/152, STRING(DEFAULT_MARKER_SIZE)},
101 {HLP_ENCODELONG(7), /*produce an elevation view*/103, 0},
102 {HLP_ENCODELONG(8), /*factor to scale down by (default %s)*/155, "500"},
103 {HLP_ENCODELONG(9), /*produce DXF output*/156, 0},
104 /* TRANSLATORS: "Skencil" is the name of a software package, so should not be
105 * translated. */
106 {HLP_ENCODELONG(10), /*produce Skencil output*/158, 0},
107 /* TRANSLATORS: "Compass" and "Carto" are the names of software packages,
108 * so should not be translated. */
109 {HLP_ENCODELONG(11), /*produce Compass PLT output for Carto*/159, 0},
110 {HLP_ENCODELONG(12), /*produce SVG output*/160, 0},
111 {0, 0, 0}
114 msg_init(argv);
116 cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 1, 2);
117 while (1) {
118 int opt = cmdline_getopt();
119 if (opt == EOF) break;
120 switch (opt) {
121 case 'e': /* Elevation */
122 tilt = cmdline_double_arg();
123 break;
124 case 'c': /* Crosses */
125 show_mask &= ~STNS;
126 break;
127 case 'n': /* Labels */
128 show_mask &= ~LABELS;
129 break;
130 case 'l': /* Legs */
131 show_mask &= ~LEGS;
132 break;
133 case 'g': /* Grid */
134 if (optarg) {
135 grid = cmdline_double_arg();
136 } else {
137 grid = (double)DEFAULT_GRID_SPACING;
139 break;
140 case 'r': /* Reduction factor */
141 scale = cmdline_double_arg();
142 break;
143 case 't': /* Text height */
144 text_height = cmdline_double_arg();
145 break;
146 case 'm': /* Marker size */
147 marker_size = cmdline_double_arg();
148 break;
149 case 'D':
150 format = FMT_DXF;
151 format_set = true;
152 break;
153 case 'S':
154 format = FMT_SK;
155 format_set = true;
156 break;
157 case 'P':
158 format = FMT_PLT;
159 format_set = true;
160 break;
161 case 'V':
162 format = FMT_SVG;
163 format_set = true;
164 break;
165 case 's':
166 survey = optarg;
167 break;
171 const char* fnm_in = argv[optind++];
172 const char* fnm_out = argv[optind];
173 if (fnm_out) {
174 if (!format_set) {
175 size_t i;
176 size_t len = strlen(fnm_out);
177 format = FMT_DEFAULT;
178 for (i = 0; i < FMT_MAX_PLUS_ONE_; ++i) {
179 size_t l = strlen(extension[i]);
180 if (len > l + 1 &&
181 strcasecmp(fnm_out + len - l, extension[i]) == 0) {
182 format = export_format(i);
183 break;
187 } else {
188 char *baseleaf = baseleaf_from_fnm(fnm_in);
189 if (!format_set) format = FMT_DEFAULT;
190 /* note : memory allocated by fnm_out gets leaked in this case... */
191 fnm_out = add_ext(baseleaf, extension[format]);
192 osfree(baseleaf);
195 Model model;
196 int err = model.Load(fnm_in, survey);
197 if (err) fatalerror(err, fnm_in);
199 if (!Export(fnm_out, model.GetSurveyTitle(),
200 model.GetDateString(),
201 model,
202 pan, tilt, show_mask, format,
203 model.GetCSProj(),
204 grid, text_height, marker_size,
205 scale)) {
206 fatalerror(/*Couldn’t write file “%s”*/402, fnm_out);
208 return 0;