source: git/src/cad3d.c @ 45af761

RELEASE/1.2debug-cidebug-ci-sanitisersstereowalls-data
Last change on this file since 45af761 was 45af761, checked in by Olly Betts <olly@…>, 13 years ago

lib/,src/: Extract all the help strings in --help output and make
them available for translation.

git-svn-id: file:///home/survex-svn/survex/trunk@3741 4b37db11-9a0c-4f06-9ece-9ab7cdaee568

  • Property mode set to 100644
File size: 24.7 KB
Line 
1/* cad3d.c
2 * Converts a .3d file to CAD-like formats (DXF, Sketch) and also Compass PLT.
3 * Also useful as an example of how to use the img code in your own programs
4 */
5
6/* Copyright (C) 1994-2004,2008,2010 Olly Betts
7 * Copyright (C) 2004 John Pybus (SVG Output code)
8 *
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.
13 *
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.
18 *
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
22 */
23
24/* #define DEBUG_CAD3D */
25
26#ifdef HAVE_CONFIG_H
27#include <config.h>
28#endif
29
30#include <float.h>
31#include <math.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35
36#include "cmdline.h"
37#include "debug.h"
38#include "filename.h"
39#include "hash.h"
40#include "img.h"
41#include "message.h"
42#include "useful.h"
43
44/* default values - can be overridden with --htext and --msize respectively */
45#define TEXT_HEIGHT     0.6
46#define MARKER_SIZE     0.8
47
48#define GRID_SPACING    100
49
50#define POINTS_PER_INCH 72.0
51#define POINTS_PER_MM (POINTS_PER_INCH / MM_PER_INCH)
52
53#define SQRT_2          1.41421356237309504880168872420969
54
55#define LEGS 1
56#define STNS 2
57#define LABELS 4
58
59/* default to DXF */
60#define FMT_DEFAULT FMT_DXF
61
62static FILE *fh;
63
64/* bounds */
65static double min_x, min_y, min_z, max_x, max_y, max_z;
66
67static double text_height; /* for station labels */
68static double marker_size; /* for station markers */
69static double grid; /* grid spacing (or 0 for no grid) */
70static double scale = 500.0;
71static double factor;
72static const char *unit = "mm";
73
74static img *pimg;
75static const char *survey = NULL;
76
77static void
78dxf_header(void)
79{
80   fprintf(fh, "0\nSECTION\n"
81               "2\nHEADER\n");
82   fprintf(fh, "9\n$EXTMIN\n"); /* lower left corner of drawing */
83   fprintf(fh, "10\n%#-.6f\n", min_x); /* x */
84   fprintf(fh, "20\n%#-.6f\n", min_y); /* y */
85   fprintf(fh, "30\n%#-.6f\n", min_z); /* min z */
86   fprintf(fh, "9\n$EXTMAX\n"); /* upper right corner of drawing */
87   fprintf(fh, "10\n%#-.6f\n", max_x); /* x */
88   fprintf(fh, "20\n%#-.6f\n", max_y); /* y */
89   fprintf(fh, "30\n%#-.6f\n", max_z); /* max z */
90   fprintf(fh, "9\n$PDMODE\n70\n3\n"); /* marker style as CROSS */
91   fprintf(fh, "9\n$PDSIZE\n40\n%6.2f\n", marker_size); /* marker size */
92   fprintf(fh, "0\nENDSEC\n");
93
94   fprintf(fh, "0\nSECTION\n"
95               "2\nTABLES\n");
96   fprintf(fh, "0\nTABLE\n" /* Define CONTINUOUS and DASHED line types. */
97               "2\nLTYPE\n"
98               "70\n10\n"
99               "0\nLTYPE\n"
100               "2\nCONTINUOUS\n"
101               "70\n64\n"
102               "3\nContinuous\n"
103               "72\n65\n"
104               "73\n0\n"
105               "40\n0.0\n"
106               "0\nLTYPE\n"
107               "2\nDASHED\n"
108               "70\n64\n"
109               "3\nDashed\n"
110               "72\n65\n"
111               "73\n2\n"
112               "40\n2.5\n"
113               "49\n1.25\n"
114               "49\n-1.25\n"
115               "0\nENDTAB\n");
116   fprintf(fh, "0\nTABLE\n"
117               "2\nLAYER\n");
118   fprintf(fh, "70\n10\n"); /* max # off layers in this DXF file : 10 */
119   /* First Layer: CentreLine */
120   fprintf(fh, "0\nLAYER\n2\nCentreLine\n");
121   fprintf(fh, "70\n64\n"); /* shows layer is referenced by entities */
122   fprintf(fh, "62\n5\n"); /* color: kept the same used by SpeleoGen */
123   fprintf(fh, "6\nCONTINUOUS\n"); /* linetype */
124   /* Next Layer: Stations */
125   fprintf(fh, "0\nLAYER\n2\nStations\n");
126   fprintf(fh, "70\n64\n"); /* shows layer is referenced by entities */
127   fprintf(fh, "62\n7\n"); /* color: kept the same used by SpeleoGen */
128   fprintf(fh, "6\nCONTINUOUS\n"); /* linetype */
129   /* Next Layer: Labels */
130   fprintf(fh, "0\nLAYER\n2\nLabels\n");
131   fprintf(fh, "70\n64\n"); /* shows layer is referenced by entities */
132   fprintf(fh, "62\n7\n"); /* color: kept the same used by SpeleoGen */
133   fprintf(fh, "6\nCONTINUOUS\n"); /* linetype */
134   /* Next Layer: Surface */
135   fprintf(fh, "0\nLAYER\n2\nSurface\n");
136   fprintf(fh, "70\n64\n"); /* shows layer is referenced by entities */
137   fprintf(fh, "62\n5\n"); /* color */
138   fprintf(fh, "6\nDASHED\n"); /* linetype */
139   /* Next Layer: SurfaceStations */
140   fprintf(fh, "0\nLAYER\n2\nSurfaceStations\n");
141   fprintf(fh, "70\n64\n"); /* shows layer is referenced by entities */
142   fprintf(fh, "62\n7\n"); /* color */
143   fprintf(fh, "6\nCONTINUOUS\n"); /* linetype */
144   /* Next Layer: SurfaceLabels */
145   fprintf(fh, "0\nLAYER\n2\nSurfaceLabels\n");
146   fprintf(fh, "70\n64\n"); /* shows layer is referenced by entities */
147   fprintf(fh, "62\n7\n"); /* color */
148   fprintf(fh, "6\nCONTINUOUS\n"); /* linetype */
149   if (grid > 0) {
150      /* Next Layer: Grid */
151      fprintf(fh, "0\nLAYER\n2\nGrid\n");
152      fprintf(fh, "70\n64\n"); /* shows layer is referenced by entities */
153      fprintf(fh, "62\n7\n"); /* color: kept the same used by SpeleoGen */
154      fprintf(fh, "6\nCONTINUOUS\n"); /* linetype */
155   }
156   fprintf(fh, "0\nENDTAB\n"
157               "0\nENDSEC\n");
158
159   fprintf(fh, "0\nSECTION\n"
160               "2\nENTITIES\n");
161
162   if (grid > 0) {
163      double x, y;
164      x = floor(min_x / grid) * grid + grid;
165      y = floor(min_y / grid) * grid + grid;
166#ifdef DEBUG_CAD3D
167      printf("x_min: %f  y_min: %f\n", x, y);
168#endif
169      while (x < max_x) {
170         /* horizontal line */
171         fprintf(fh, "0\nLINE\n");
172         fprintf(fh, "8\nGrid\n"); /* Layer */
173         fprintf(fh, "10\n%6.2f\n", x);
174         fprintf(fh, "20\n%6.2f\n", min_y);
175         fprintf(fh, "30\n0\n");
176         fprintf(fh, "11\n%6.2f\n", x);
177         fprintf(fh, "21\n%6.2f\n", max_y);
178         fprintf(fh, "31\n0\n");
179         x += grid;
180      }
181      while (y < max_y) {
182         /* vertical line */
183         fprintf(fh, "0\nLINE\n");
184         fprintf(fh, "8\nGrid\n"); /* Layer */
185         fprintf(fh, "10\n%6.2f\n", min_x);
186         fprintf(fh, "20\n%6.2f\n", y);
187         fprintf(fh, "30\n0\n");
188         fprintf(fh, "11\n%6.2f\n", max_x);
189         fprintf(fh, "21\n%6.2f\n", y);
190         fprintf(fh, "31\n0\n");
191         y += grid;
192      }
193   }
194}
195
196static void
197dxf_start_pass(int layer)
198{
199   layer = layer;
200}
201
202static void
203dxf_move(const img_point *p)
204{
205   p = p; /* unused */
206}
207
208static void
209dxf_line(const img_point *p1, const img_point *p, bool fSurface)
210{
211   fprintf(fh, "0\nLINE\n");
212   fprintf(fh, fSurface ? "8\nSurface\n" : "8\nCentreLine\n"); /* Layer */
213   fprintf(fh, "10\n%6.2f\n", p1->x);
214   fprintf(fh, "20\n%6.2f\n", p1->y);
215   fprintf(fh, "30\n%6.2f\n", p1->z);
216   fprintf(fh, "11\n%6.2f\n", p->x);
217   fprintf(fh, "21\n%6.2f\n", p->y);
218   fprintf(fh, "31\n%6.2f\n", p->z);
219}
220
221static void
222dxf_label(const img_point *p, const char *s, bool fSurface)
223{
224   /* write station label to dxf file */
225   fprintf(fh, "0\nTEXT\n");
226   fprintf(fh, fSurface ? "8\nSurfaceLabels\n" : "8\nLabels\n"); /* Layer */
227   fprintf(fh, "10\n%6.2f\n", p->x);
228   fprintf(fh, "20\n%6.2f\n", p->y);
229   fprintf(fh, "30\n%6.2f\n", p->z);
230   fprintf(fh, "40\n%6.2f\n", text_height);
231   fprintf(fh, "1\n%s\n", s);
232}
233
234static void
235dxf_cross(const img_point *p, bool fSurface)
236{
237   /* write station marker to dxf file */
238   fprintf(fh, "0\nPOINT\n");
239   fprintf(fh, fSurface ? "8\nSurfaceStations\n" : "8\nStations\n"); /* Layer */
240   fprintf(fh, "10\n%6.2f\n", p->x);
241   fprintf(fh, "20\n%6.2f\n", p->y);
242   fprintf(fh, "30\n%6.2f\n", p->z);
243}
244
245static void
246dxf_footer(void)
247{
248   fprintf(fh, "000\nENDSEC\n");
249   fprintf(fh, "000\nEOF\n");
250}
251
252static void
253sketch_header(void)
254{
255   fprintf(fh, "##Sketch 1 2\n"); /* Sketch file version */
256   fprintf(fh, "document()\n");
257   fprintf(fh, "layout((%.3f,%.3f),0)\n",
258           (max_x - min_x) * factor, (max_y - min_y) * factor);
259}
260
261static const char *layer_names[] = {
262   NULL,
263   "Legs",
264   "Stations",
265   NULL,
266   "Labels"
267};
268
269static void
270sketch_start_pass(int layer)
271{
272   fprintf(fh, "layer('%s',1,1,0,0,(0,0,0))\n", layer_names[layer]);
273}
274
275static void
276sketch_move(const img_point *p)
277{
278   fprintf(fh, "b()\n");
279   fprintf(fh, "bs(%.3f,%.3f,%.3f)\n", p->x * factor, p->y * factor, 0.0);
280}
281
282static void
283sketch_line(const img_point *p1, const img_point *p, bool fSurface)
284{
285   fSurface = fSurface; /* unused */
286   p1 = p1; /* unused */
287   fprintf(fh, "bs(%.3f,%.3f,%.3f)\n", p->x * factor, p->y * factor, 0.0);
288}
289
290static void
291sketch_label(const img_point *p, const char *s, bool fSurface)
292{
293   fSurface = fSurface; /* unused */
294   fprintf(fh, "fp((0,0,0))\n");
295   fprintf(fh, "le()\n");
296   fprintf(fh, "Fn('Times-Roman')\n");
297   fprintf(fh, "Fs(5)\n");
298   fprintf(fh, "txt('");
299   while (*s) {
300      int ch = *s++;
301      if (ch == '\'' || ch == '\\') PUTC('\\', fh);
302      PUTC(ch, fh);
303   }
304   fprintf(fh, "',(%.3f,%.3f))\n", p->x * factor, p->y * factor);
305}
306
307static void
308sketch_cross(const img_point *p, bool fSurface)
309{
310   fSurface = fSurface; /* unused */
311   fprintf(fh, "b()\n");
312   fprintf(fh, "bs(%.3f,%.3f,%.3f)\n",
313           p->x * factor - MARKER_SIZE, p->y * factor - MARKER_SIZE, 0.0);
314   fprintf(fh, "bs(%.3f,%.3f,%.3f)\n",
315           p->x * factor + MARKER_SIZE, p->y * factor + MARKER_SIZE, 0.0);
316   fprintf(fh, "bn()\n");
317   fprintf(fh, "bs(%.3f,%.3f,%.3f)\n",
318           p->x * factor + MARKER_SIZE, p->y * factor - MARKER_SIZE, 0.0);
319   fprintf(fh, "bs(%.3f,%.3f,%.3f)\n",
320           p->x * factor - MARKER_SIZE, p->y * factor + MARKER_SIZE, 0.0);
321}
322
323static void
324sketch_footer(void)
325{
326   fprintf(fh, "guidelayer('Guide Lines',1,0,0,1,(0,0,1))\n");
327   if (grid) {
328      fprintf(fh, "grid((0,0,%.3f,%.3f),1,(0,0,1),'Grid')\n",
329              grid * factor, grid * factor);
330   }
331}
332
333typedef struct point {
334   img_point p;
335   const char *label;
336   struct point *next;
337} point;
338
339#define HTAB_SIZE 0x2000
340
341static point **htab;
342
343static void
344set_name(const img_point *p, const char *s)
345{
346   int hash;
347   point *pt;
348   union {
349      char data[sizeof(int) * 3];
350      int x[3];
351   } u;
352
353   u.x[0] = (int)(p->x * 100);
354   u.x[1] = (int)(p->y * 100);
355   u.x[2] = (int)(p->z * 100);
356   hash = (hash_data(u.data, sizeof(int) * 3) & (HTAB_SIZE - 1));
357   for (pt = htab[hash]; pt; pt = pt->next) {
358      if (pt->p.x == p->x && pt->p.y == p->y && pt->p.z == p->z) {
359         /* already got name for these coordinates */
360         /* FIXME: what about multiple names for the same station? */
361         return;
362      }
363   }
364
365   pt = osnew(point);
366   pt->label = osstrdup(s);
367   pt->p = *p;
368   pt->next = htab[hash];
369   htab[hash] = pt;
370
371   return;
372}
373
374static const char *
375find_name(const img_point *p)
376{
377   int hash;
378   point *pt;
379   union {
380      char data[sizeof(int) * 3];
381      int x[3];
382   } u;
383   SVX_ASSERT(p);
384
385   u.x[0] = (int)(p->x * 100);
386   u.x[1] = (int)(p->y * 100);
387   u.x[2] = (int)(p->z * 100);
388   hash = (hash_data(u.data, sizeof(int) * 3) & (HTAB_SIZE - 1));
389   for (pt = htab[hash]; pt; pt = pt->next) {
390      if (pt->p.x == p->x && pt->p.y == p->y && pt->p.z == p->z)
391         return pt->label;
392   }
393   return "?";
394}
395
396static bool to_close = 0;
397static bool close_g = 0;
398
399static void
400svg_header(void)
401{
402   size_t i;
403   htab = osmalloc(HTAB_SIZE * ossizeof(point *));
404   for (i = 0; i < HTAB_SIZE; ++i) htab[i] = NULL;
405   fprintf(fh, "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n");
406   fprintf(fh, "<svg width=\"%.3f%s\" height=\"%.3f%s\""
407               " viewBox=\"0 0 %0.3f %0.3f\">\n",
408           (max_x - min_x) * factor, unit, (max_y - min_y) * factor, unit,
409           (max_x - min_x) * factor, (max_y - min_y) * factor );
410   fprintf(fh, "<g transform=\"translate(%.3f %.3f)\">\n",
411           min_x * -factor, max_y * factor);
412   to_close = 0;
413   close_g = 0;
414}
415
416static void
417svg_start_pass(int layer)
418{
419   if (to_close) {
420      fprintf(fh, "\"/>\n");
421      to_close = 0;
422   }
423   if (close_g) {
424      fprintf(fh, "</g>\n");
425   }
426   close_g = 1;
427
428   fprintf(fh, "<g id=\"%s\"", layer_names[layer]);
429   if (layer & LEGS)
430      fprintf(fh, " style=\"stroke:black;fill:none;stroke-width:0.4\"");
431   else if (layer & STNS)
432      fprintf(fh, " style=\"stroke:black;fill:none;stroke-width:0.05\"");
433   else if (layer & LABELS)
434      fprintf(fh, " style=\"font-size:%.3f\"", text_height);
435   fprintf(fh, ">\n");
436}
437
438static void
439svg_move(const img_point *p)
440{
441   if (to_close) {
442      fprintf(fh, "\"/>\n");
443   }
444   fprintf(fh, "<path d=\"M%.3f %.3f", p->x * factor, p->y * -factor);
445   to_close = 1;
446}
447
448static void
449svg_line(const img_point *p1, const img_point *p, bool fSurface)
450{
451   fSurface = fSurface; /* unused */
452   p1 = p1; /* unused */
453   fprintf(fh, "L%.3f %.3f", p->x * factor, p->y * -factor);
454   to_close = 1;
455}
456
457static void
458svg_label(const img_point *p, const char *s, bool fSurface)
459{
460   fSurface = fSurface; /* unused */
461   fprintf(fh, "<text transform=\"translate(%.3f %.3f)\">",
462           p->x * factor, p->y * -factor);
463   fputs(s, fh);
464   fputs("</text>\n", fh);
465   set_name(p, s);
466}
467
468static void
469svg_cross(const img_point *p, bool fSurface)
470{
471   fSurface = fSurface; /* unused */
472   fprintf(fh, "<circle id=\"%s\" cx=\"%.3f\" cy=\"%.3f\" r=\"%.3f\"/>\n",
473           find_name(p), p->x * factor, p->y * -factor, MARKER_SIZE * SQRT_2);
474   fprintf(fh, "<path d=\"M%.3f %.3fL%.3f %.3fM%.3f %.3fL%.3f %.3f\"/>\n",
475           p->x * factor - MARKER_SIZE, p->y * -factor - MARKER_SIZE,
476           p->x * factor + MARKER_SIZE, p->y * -factor + MARKER_SIZE,
477           p->x * factor + MARKER_SIZE, p->y * -factor - MARKER_SIZE,
478           p->x * factor - MARKER_SIZE, p->y * -factor + MARKER_SIZE );
479}
480
481static void
482svg_footer(void)
483{
484   if (to_close) {
485      fprintf(fh, "\"/>\n");
486      to_close = 0;
487   }
488   if (close_g) {
489      fprintf(fh, "</g>\n");
490   }
491   close_g = 1;
492   fprintf(fh, "</g>\n</svg>");
493}
494
495static void
496plt_header(void)
497{
498   size_t i;
499   htab = osmalloc(HTAB_SIZE * ossizeof(point *));
500   for (i = 0; i < HTAB_SIZE; ++i) htab[i] = NULL;
501   /* Survex is E, N, Alt - PLT file is N, E, Alt */
502   fprintf(fh, "Z %.3f %.3f %.3f %.3f %.3f %.3f\r\n",
503           min_y / METRES_PER_FOOT, max_y / METRES_PER_FOOT,
504           min_x / METRES_PER_FOOT, max_x / METRES_PER_FOOT,
505           min_z / METRES_PER_FOOT, max_z / METRES_PER_FOOT);
506   fprintf(fh, "N%s D 1 1 1 C%s\r\n", survey ? survey : "X", pimg->title);
507}
508
509static void
510plt_start_pass(int layer)
511{
512   layer = layer;
513}
514
515static void
516plt_move(const img_point *p)
517{
518   /* Survex is E, N, Alt - PLT file is N, E, Alt */
519   fprintf(fh, "M %.3f %.3f %.3f ",
520           p->y / METRES_PER_FOOT, p->x / METRES_PER_FOOT, p->z / METRES_PER_FOOT);
521   /* dummy passage dimensions are required to avoid compass bug */
522   fprintf(fh, "S%s P -9 -9 -9 -9\r\n", find_name(p));
523}
524
525static void
526plt_line(const img_point *p1, const img_point *p, bool fSurface)
527{
528   fSurface = fSurface; /* unused */
529   p1 = p1; /* unused */
530   /* Survex is E, N, Alt - PLT file is N, E, Alt */
531   fprintf(fh, "D %.3f %.3f %.3f ",
532           p->y / METRES_PER_FOOT, p->x / METRES_PER_FOOT, p->z / METRES_PER_FOOT);
533   /* dummy passage dimensions are required to avoid compass bug */
534   fprintf(fh, "S%s P -9 -9 -9 -9\r\n", find_name(p));
535}
536
537static void
538plt_label(const img_point *p, const char *s, bool fSurface)
539{
540   fSurface = fSurface; /* unused */
541   /* FIXME: also ctrl characters - ought to remap them, not give up */
542   if (strchr(s, ' ')) {
543      fprintf(stderr, "PLT format can't cope with spaces in station names\n");
544      exit(1);
545   }
546   set_name(p, s);
547}
548
549static void
550plt_cross(const img_point *p, bool fSurface)
551{
552   fSurface = fSurface; /* unused */
553   p = p; /* unused */
554}
555
556static void
557plt_footer(void)
558{
559   /* Survex is E, N, Alt - PLT file is N, E, Alt */
560   fprintf(fh, "X %.3f %.3f %.3f %.3f %.3f %.3f\r\n",
561           min_y / METRES_PER_FOOT, max_y / METRES_PER_FOOT,
562           min_x / METRES_PER_FOOT, max_x / METRES_PER_FOOT,
563           min_z / METRES_PER_FOOT, max_z / METRES_PER_FOOT);
564   /* Yucky DOS "end of textfile" marker */
565   PUTC('\x1a', fh);
566}
567
568static int dxf_passes[] = { LEGS|STNS|LABELS, 0 };
569static int sketch_passes[] = { LEGS, STNS, LABELS, 0 };
570static int plt_passes[] = { LABELS, LEGS, 0 };
571static int svg_passes[] = { LEGS, LABELS, STNS, 0 };
572
573int
574main(int argc, char **argv)
575{
576   char *fnm_3d, *fnm_out;
577   unsigned char labels, crosses, legs;
578   int item;
579   int fSeenMove = 0;
580   img_point p, p1;
581   int elevation = 0;
582   double elev_angle = 0;
583   double s = 0, c = 0;
584   enum { FMT_DXF = 0, FMT_SKETCH, FMT_PLT, FMT_SVG, FMT_AUTO } format;
585   static const char *extensions[] = { "dxf", "sk", "plt", "svg" };
586   int *pass;
587
588   void (*header)(void);
589   void (*start_pass)(int);
590   void (*move)(const img_point *);
591   void (*line)(const img_point *, const img_point *, bool);
592   void (*label)(const img_point *, const char *, bool);
593   void (*cross)(const img_point *, bool);
594   void (*footer)(void);
595   const char *mode = "w"; /* default to text output */
596
597   /* TRANSLATE */
598   static const struct option long_opts[] = {
599        /* const char *name; int has_arg (0 no_argument, 1 required, 2 options_*); int *flag; int val */
600        {"survey", required_argument, 0, 's'},
601        {"no-crosses", no_argument, 0, 'c'},
602        {"no-station-names", no_argument, 0, 'n'},
603        {"no-legs", no_argument, 0, 'l'},
604        {"grid", optional_argument, 0, 'g'},
605        {"text-height", required_argument, 0, 't'},
606        {"marker-size", required_argument, 0, 'm'},
607        {"elevation", required_argument, 0, 'e'},
608        {"reduction", required_argument, 0, 'r'},
609        {"dxf", no_argument, 0, 'D'},
610        {"sketch", no_argument, 0, 'S'},
611        {"plt", no_argument, 0, 'P'},
612        {"svg", no_argument, 0, 'V'},
613        {"help", no_argument, 0, HLP_HELP},
614        {"version", no_argument, 0, HLP_VERSION},
615        {0,0,0,0}
616   };
617
618#define short_opts "s:cnlg::t:m:e:r:DSPV"
619
620   /* TRANSLATE */
621   static struct help_msg help[] = {
622        /*                      <-- */
623        {HLP_ENCODELONG(0),   /*only load the sub-survey with this prefix*/199, 0},
624        {HLP_ENCODELONG(1),   /*do not generate station markers*/100, 0},
625        {HLP_ENCODELONG(2),   /*do not generate station labels*/101, 0},
626        {HLP_ENCODELONG(3),   /*do not generate the survey legs*/102, 0},
627        {HLP_ENCODELONG(4),   /*generate grid (default %sm)*/148, STRING(GRID_SPACING)},
628        {HLP_ENCODELONG(5),   /*station labels text height (default %s)*/149, STRING(TEXT_HEIGHT)},
629        {HLP_ENCODELONG(6),   /*station marker size (default %s)*/152, STRING(MARKER_SIZE)},
630        {HLP_ENCODELONG(7),   /*produce an elevation view*/103, 0},
631        {HLP_ENCODELONG(8),   /*factor to scale down by (default %s)*/155, "500"},
632        {HLP_ENCODELONG(9),   /*produce DXF output*/156, 0},
633        {HLP_ENCODELONG(10),  /*produce Sketch output*/158, 0},
634        {HLP_ENCODELONG(11),  /*produce Compass PLT output for Carto*/159, 0},
635        {HLP_ENCODELONG(12),  /*produce SVG output*/160, 0},
636        {0, 0, 0}
637   };
638
639   msg_init(argv);
640
641   /* Defaults */
642   crosses = 1;
643   labels = 1;
644   legs = 1;
645   grid = 0;
646   text_height = TEXT_HEIGHT;
647   marker_size = MARKER_SIZE;
648   format = FMT_AUTO;
649
650   cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 1, 2);
651   while (1) {
652      int opt = cmdline_getopt();
653      if (opt == EOF) break;
654      switch (opt) {
655       case 'e': /* Elevation */
656         elevation = 1;
657         elev_angle = cmdline_double_arg();
658         break;
659       case 'c': /* Crosses */
660         crosses = 0;
661         break;
662       case 'n': /* Labels */
663         labels = 0;
664         break;
665       case 'l': /* Legs */
666         legs = 0;
667         break;
668       case 'g': /* Grid */
669         if (optarg) {
670            grid = cmdline_double_arg();
671         } else {
672            grid = (double)GRID_SPACING;
673         }
674         break;
675       case 'r': /* Reduction factor */
676         scale = cmdline_double_arg();
677         break;
678       case 't': /* Text height */
679         text_height = cmdline_double_arg();
680#ifdef DEBUG_CAD3D
681         printf("Text Height: `%s' input, converted to %6.2f\n", optarg, text_height);
682#endif
683         break;
684       case 'm': /* Marker size */
685         marker_size = cmdline_double_arg();
686#ifdef DEBUG_CAD3D
687         printf("Marker Size: `%s', converted to %6.2f\n", optarg, marker_size);
688#endif
689         break;
690       case 'D':
691         format = FMT_DXF;
692         break;
693       case 'S':
694         format = FMT_SKETCH;
695         break;
696       case 'P':
697         format = FMT_PLT;
698         break;
699       case 'V':
700         format = FMT_SVG;
701         break;
702       case 's':
703         survey = optarg;
704         break;
705#ifdef DEBUG_CAD3D
706       default:
707         printf("Internal Error: 'getopt' returned '%c' %d\n", opt, opt);
708#endif
709      }
710   }
711
712   fnm_3d = argv[optind++];
713   if (argv[optind]) {
714      fnm_out = argv[optind];
715      if (format == FMT_AUTO) {
716         size_t i;
717         size_t len = strlen(fnm_out);
718         format = FMT_DEFAULT;
719         for (i = 0; i < FMT_AUTO; ++i) {
720            size_t l = strlen(extensions[i]);
721            if (len > l + 1 && fnm_out[len - l - 1] == FNM_SEP_EXT &&
722                strcasecmp(fnm_out + len - l, extensions[i]) == 0) {
723               format = i;
724               break;
725            }
726         }
727      }
728   } else {
729      char *baseleaf = baseleaf_from_fnm(fnm_3d);
730      if (format == FMT_AUTO) format = FMT_DEFAULT;
731      /* note : memory allocated by fnm_out gets leaked in this case... */
732      fnm_out = add_ext(baseleaf, extensions[format]);
733      osfree(baseleaf);
734   }
735
736   switch (format) {
737    case FMT_DXF:
738      header = dxf_header;
739      start_pass = dxf_start_pass;
740      move = dxf_move;
741      line = dxf_line;
742      label = dxf_label;
743      cross = dxf_cross;
744      footer = dxf_footer;
745      pass = dxf_passes;
746      break;
747    case FMT_SKETCH:
748      header = sketch_header;
749      start_pass = sketch_start_pass;
750      move = sketch_move;
751      line = sketch_line;
752      label = sketch_label;
753      cross = sketch_cross;
754      footer = sketch_footer;
755      pass = sketch_passes;
756      factor = POINTS_PER_MM * 1000.0 / scale;
757      mode = "wb"; /* Binary file output */
758      break;
759    case FMT_PLT:
760      header = plt_header;
761      start_pass = plt_start_pass;
762      move = plt_move;
763      line = plt_line;
764      label = plt_label;
765      cross = plt_cross;
766      footer = plt_footer;
767      pass = plt_passes;
768      mode = "wb"; /* Binary file output */
769      break;
770    case FMT_SVG:
771      header = svg_header;
772      start_pass = svg_start_pass;
773      move = svg_move;
774      line = svg_line;
775      label = svg_label;
776      cross = svg_cross;
777      footer = svg_footer;
778      pass = svg_passes;
779      factor = 1000.0 / scale;
780      mode = "wb"; /* Binary file output */
781      break;
782    default:
783      exit(1);
784   }
785
786   pimg = img_open_survey(fnm_3d, survey);
787   if (!pimg) fatalerror(img_error(), fnm_3d);
788
789   fh = safe_fopen(fnm_out, mode);
790
791   if (elevation) {
792      s = sin(rad(elev_angle));
793      c = cos(rad(elev_angle));
794   }
795
796   /* Get drawing corners */
797   min_x = min_y = min_z = HUGE_VAL;
798   max_x = max_y = max_z = -HUGE_VAL;
799   do {
800      item = img_read_item(pimg, &p);
801
802      if (elevation) {
803         double xnew = p.x * c - p.y * s;
804         double znew = - p.x * s - p.y * c;
805         p.y = p.z;
806         p.z = znew;
807         p.x = xnew;
808      }
809
810      switch (item) {
811       case img_MOVE: case img_LINE: case img_LABEL:
812         if (p.x < min_x) min_x = p.x;
813         if (p.x > max_x) max_x = p.x;
814         if (p.y < min_y) min_y = p.y;
815         if (p.y > max_y) max_y = p.y;
816         if (p.z < min_z) min_z = p.z;
817         if (p.z > max_z) max_z = p.z;
818         break;
819      }
820   } while (item != img_STOP);
821
822   if (grid > 0) {
823      min_x -= grid / 2;
824      max_x += grid / 2;
825      min_y -= grid / 2;
826      max_y += grid / 2;
827   }
828
829   /* handle empty file gracefully */
830   if (min_x > max_x) {
831      min_x = min_y = min_z = 0;
832      max_x = max_y = max_z = 0;
833   }
834
835   /* Header */
836   header();
837
838   p1.x = p1.y = p1.z = 0; /* avoid compiler warning */
839
840   while (*pass) {
841      if (((*pass & LEGS) && legs) ||
842          ((*pass & STNS) && crosses) ||
843          ((*pass & LABELS) && labels)) {
844         img_rewind(pimg);
845         start_pass(*pass);
846         do {
847            item = img_read_item(pimg, &p);
848
849            if (format == FMT_SKETCH) {
850               p.x -= min_x;
851               p.y -= min_y;
852               p.z -= min_z;
853            }
854
855            if (elevation) {
856               double xnew = p.x * c - p.y * s;
857               double znew = - p.x * s - p.y * c;
858               p.y = p.z;
859               p.z = znew;
860               p.x = xnew;
861            }
862
863            switch (item) {
864             case img_BAD:
865               img_close(pimg);
866               fatalerror(/*Bad 3d image file `%s'*/106, fnm_3d);
867               break;
868             case img_LINE:
869#ifdef DEBUG_CAD3D
870               printf("line to %9.2f %9.2f %9.2f\n", p.x, p.y, p.z);
871#endif
872               if (!fSeenMove) {
873#ifdef DEBUG_CAD3D
874                  printf("Something is wrong -- img_LINE before any img_MOVE!\n");
875#endif
876                  img_close(pimg);
877                  fatalerror(/*Bad 3d image file `%s'*/106, fnm_3d);
878               }
879               if ((*pass & LEGS) && legs)
880                  line(&p1, &p, pimg->flags & img_FLAG_SURFACE);
881               p1 = p;
882               break;
883             case img_MOVE:
884#ifdef DEBUG_CAD3D
885               printf("move to %9.2f %9.2f %9.2f\n",x,y,z);
886#endif
887               if ((*pass & LEGS) && legs) move(&p);
888               fSeenMove = 1;
889               p1 = p;
890               break;
891             case img_LABEL:
892#ifdef DEBUG_CAD3D
893               printf("label `%s' at %9.2f %9.2f %9.2f\n",pimg->label,x,y,z);
894#endif
895               /* Use !UNDERGROUND as the criterion - we want stations where
896                * a surface and underground survey meet to be in the
897                * underground layer */
898               if ((*pass & LABELS) && labels)
899                  label(&p, pimg->label,
900                        !(pimg->flags & img_SFLAG_UNDERGROUND));
901               if ((*pass & STNS) && crosses)
902                  cross(&p, !(pimg->flags & img_SFLAG_UNDERGROUND));
903               break;
904#ifdef DEBUG_CAD3D
905             case img_STOP:
906               printf("stop\n");
907               break;
908             default:
909               printf("other info tag (code %d) ignored\n",item);
910#endif
911            }
912         } while (item != img_STOP);
913      }
914      pass++;
915   }
916   img_close(pimg);
917   footer();
918   safe_fclose(fh);
919   return 0;
920}
Note: See TracBrowser for help on using the repository browser.