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