source: git/src/cad3d.c @ bef66af

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

Added SVG support from John Pybus

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

  • Property mode set to 100644
File size: 24.5 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 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 void
397svg_header(void)
398{
399   size_t i;
400   htab = osmalloc(HTAB_SIZE * ossizeof(point *));
401   for (i = 0; i < HTAB_SIZE; ++i) htab[i] = NULL;
402   fprintf(fh, "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n");
403   fprintf(fh, "<svg width=\"%.3f%s\" height=\"%.3f%s\""
404               " viewBox=\"0 0 %0.3f %0.3f\">\n",
405           (max_x - min_x) * factor, unit, (max_y - min_y) * factor, unit,
406           (max_x - min_x) * factor, (max_y - min_y) * factor );
407   fprintf(fh, "<g transform=\"translate(%.3f %.3f)\">\n",
408           min_x * -factor, max_y * factor);
409}
410
411static bool to_close = 0;
412static bool close_g = 0;
413
414static void
415svg_start_pass(int layer)
416{
417   if (to_close) {
418      fprintf(fh, "\"/>\n");
419      to_close = 0;
420   }
421   if (close_g) {
422      fprintf(fh, "</g>\n");
423   }
424   close_g = 1;
425
426   fprintf(fh, "<g id=\"%s\"", layer_names[layer]);
427   if (layer & LEGS)
428      fprintf(fh, " style=\"stroke:black;fill:none;stroke-width:0.4\"");
429   else if (layer & STNS)
430      fprintf(fh, " style=\"stroke:black;fill:none;stroke-width:0.05\"");
431   else if (layer & LABELS)
432      fprintf(fh, " style=\"font-size:%.3f\"", text_height);
433   fprintf(fh, ">\n");
434}
435
436static void
437svg_move(const img_point *p)
438{
439   if (to_close) {
440      fprintf(fh, "\"/>\n");
441   }
442   fprintf(fh, "<path d=\"M %.3f %.3f", p->x * factor, p->y * -factor);
443   to_close = 1;
444}
445
446static void
447svg_line(const img_point *p1, const img_point *p, bool fSurface)
448{
449   fSurface = fSurface; /* unused */
450   p1 = p1; /* unused */
451   fprintf(fh, "L%.3f %.3f", p->x * factor, p->y * -factor);
452   to_close = 1;
453}
454
455static void
456svg_label(const img_point *p, const char *s, bool fSurface)
457{
458   fSurface = fSurface; /* unused */
459   fprintf(fh, "<text transform=\"translate(%.3f %.3f)\">",
460           p->x * factor, p->y * -factor);
461   fprintf(fh, s);
462   fprintf(fh, "</text>\n");
463   set_name(p, s);
464}
465
466static void
467svg_cross(const img_point *p, bool fSurface)
468{
469   fSurface = fSurface; /* unused */
470   fprintf(fh, "<circle id=\"%s\" cx=\"%.3f\" cy=\"%.3f\" r=\"%.3f\"/>\n",
471           find_name(p), p->x * factor, p->y * -factor, MARKER_SIZE * SQRT_2);
472   fprintf(fh, "<path d=\"M%.3f %.3fL%.3f %.3fM%.3f %.3fL%.3f %.3f\"/>\n",
473           p->x * factor - MARKER_SIZE, p->y * -factor - MARKER_SIZE,
474           p->x * factor + MARKER_SIZE, p->y * -factor + MARKER_SIZE,
475           p->x * factor + MARKER_SIZE, p->y * -factor - MARKER_SIZE,
476           p->x * factor - MARKER_SIZE, p->y * -factor + MARKER_SIZE );
477}
478
479static void
480svg_footer(void) ////
481{
482   if (to_close) {
483      fprintf(fh, "\"/>\n");
484      to_close = 0;
485   }
486   fprintf(fh, "</g>\n");
487   fprintf(fh, "</g>\n</svg>");
488}
489
490static void
491plt_header(void)
492{
493   size_t i;
494   htab = osmalloc(HTAB_SIZE * ossizeof(point *));
495   for (i = 0; i < HTAB_SIZE; ++i) htab[i] = NULL;
496   /* Survex is E, N, Alt - PLT file is N, E, Alt */
497   fprintf(fh, "Z %.3f %.3f %.3f %.3f %.3f %.3f\r\n",
498           min_y / METRES_PER_FOOT, max_y / METRES_PER_FOOT,
499           min_x / METRES_PER_FOOT, max_x / METRES_PER_FOOT,
500           min_z / METRES_PER_FOOT, max_z / METRES_PER_FOOT);
501   fprintf(fh, "N%s D 1 1 1 C%s\r\n", survey ? survey : "X", pimg->title);
502}
503
504static void
505plt_start_pass(int layer)
506{
507   layer = layer;
508}
509
510static void
511plt_move(const img_point *p)
512{
513   /* Survex is E, N, Alt - PLT file is N, E, Alt */
514   fprintf(fh, "M %.3f %.3f %.3f ",
515           p->y / METRES_PER_FOOT, p->x / METRES_PER_FOOT, p->z / METRES_PER_FOOT);
516   /* dummy passage dimensions are required to avoid compass bug */
517   fprintf(fh, "S%s P -9 -9 -9 -9\r\n", find_name(p));
518}
519
520static void
521plt_line(const img_point *p1, const img_point *p, bool fSurface)
522{
523   fSurface = fSurface; /* unused */
524   p1 = p1; /* unused */
525   /* Survex is E, N, Alt - PLT file is N, E, Alt */
526   fprintf(fh, "D %.3f %.3f %.3f ",
527           p->y / METRES_PER_FOOT, p->x / METRES_PER_FOOT, p->z / METRES_PER_FOOT);
528   /* dummy passage dimensions are required to avoid compass bug */
529   fprintf(fh, "S%s P -9 -9 -9 -9\r\n", find_name(p));
530}
531
532static void
533plt_label(const img_point *p, const char *s, bool fSurface)
534{
535   fSurface = fSurface; /* unused */
536   /* FIXME: also ctrl characters - ought to remap them, not give up */
537   if (strchr(s, ' ')) {
538      fprintf(stderr, "PLT format can't cope with spaces in station names\n");
539      exit(1);
540   }
541   set_name(p, s);
542}
543
544static void
545plt_cross(const img_point *p, bool fSurface)
546{
547   fSurface = fSurface; /* unused */
548   p = p; /* unused */
549}
550
551static void
552plt_footer(void)
553{
554   /* Survex is E, N, Alt - PLT file is N, E, Alt */
555   fprintf(fh, "X %.3f %.3f %.3f %.3f %.3f %.3f\r\n",
556           min_y / METRES_PER_FOOT, max_y / METRES_PER_FOOT,
557           min_x / METRES_PER_FOOT, max_x / METRES_PER_FOOT,
558           min_z / METRES_PER_FOOT, max_z / METRES_PER_FOOT);
559   /* Yucky DOS "end of textfile" marker */
560   putc('\x1a', fh);
561}
562
563static int dxf_passes[] = { LEGS|STNS|LABELS, 0 };
564static int sketch_passes[] = { LEGS, STNS, LABELS, 0 };
565static int plt_passes[] = { LABELS, LEGS, 0 };
566static int svg_passes[] = { LEGS, LABELS, STNS, 0 };
567
568int
569main(int argc, char **argv)
570{
571   char *fnm_3d, *fnm_out;
572   unsigned char labels, crosses, legs;
573   int item;
574   int fSeenMove = 0;
575   img_point p, p1;
576   int elevation = 0;
577   double elev_angle = 0;
578   double s = 0, c = 0;
579   enum { FMT_DXF = 0, FMT_SKETCH, FMT_PLT, FMT_SVG, FMT_AUTO } format;
580   static const char *extensions[] = { "dxf", "sk", "plt", "svg" };
581   int *pass;
582
583   void (*header)(void);
584   void (*start_pass)(int);
585   void (*move)(const img_point *);
586   void (*line)(const img_point *, const img_point *, bool);
587   void (*label)(const img_point *, const char *, bool);
588   void (*cross)(const img_point *, bool);
589   void (*footer)(void);
590   const char *mode = "w"; /* default to text output */
591
592   /* TRANSLATE */
593   static const struct option long_opts[] = {
594        /* const char *name; int has_arg (0 no_argument, 1 required, 2 options_*); int *flag; int val */
595        {"survey", required_argument, 0, 's'},
596        {"no-crosses", no_argument, 0, 'c'},
597        {"no-station-names", no_argument, 0, 'n'},
598        {"no-legs", no_argument, 0, 'l'},
599        {"grid", optional_argument, 0, 'g'},
600        {"text-height", required_argument, 0, 't'},
601        {"marker-size", required_argument, 0, 'm'},
602        {"elevation", required_argument, 0, 'e'},
603        {"reduction", required_argument, 0, 'r'},
604        {"dxf", no_argument, 0, 'D'},
605        {"sketch", no_argument, 0, 'S'},
606        {"plt", no_argument, 0, 'P'},
607        {"svg", no_argument, 0, 'V'},
608        {"help", no_argument, 0, HLP_HELP},
609        {"version", no_argument, 0, HLP_VERSION},
610        {0,0,0,0}
611   };
612
613#define short_opts "s:cnlg::t:m:e:r:DSPV"
614
615   /* TRANSLATE */
616   static struct help_msg help[] = {
617        {HLP_ENCODELONG(0), "only load the sub-survey with this prefix"},
618        {HLP_ENCODELONG(1), "do not generate station markers"},
619        {HLP_ENCODELONG(2), "do not generate station labels"},
620        {HLP_ENCODELONG(3), "do not generate the survey legs"},
621        {HLP_ENCODELONG(4), "generate grid (default "STRING(GRID_SPACING)"m)"},
622        {HLP_ENCODELONG(5), "station labels text height (default "STRING(TEXT_HEIGHT)")"},
623        {HLP_ENCODELONG(6), "station marker size (default "STRING(MARKER_SIZE)")"},
624        {HLP_ENCODELONG(7), "produce an elevation view"},
625        {HLP_ENCODELONG(8), "factor to scale down by (default 500)"},
626        {HLP_ENCODELONG(9), "produce DXF output"},
627        {HLP_ENCODELONG(10), "produce Sketch output"},
628        {HLP_ENCODELONG(11), "produce Compass PLT output for Carto"},
629        {HLP_ENCODELONG(12), "produce SVG output"},
630        {0,0}
631   };
632
633   msg_init(argv);
634
635   /* Defaults */
636   crosses = 1;
637   labels = 1;
638   legs = 1;
639   grid = 0;
640   text_height = TEXT_HEIGHT;
641   marker_size = MARKER_SIZE;
642   format = FMT_AUTO;
643
644   cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 1, 2);
645   while (1) {
646      int opt = cmdline_getopt();
647      if (opt == EOF) break;
648      switch (opt) {
649       case 'e': /* Elevation */
650         elevation = 1;
651         elev_angle = cmdline_double_arg();
652         break;
653       case 'c': /* Crosses */
654         crosses = 0;
655         break;
656       case 'n': /* Labels */
657         labels = 0;
658         break;
659       case 'l': /* Legs */
660         legs = 0;
661         break;
662       case 'g': /* Grid */
663         if (optarg) {
664            grid = cmdline_double_arg();
665         } else {
666            grid = (double)GRID_SPACING;
667         }
668         break;
669       case 'r': /* Reduction factor */
670         scale = cmdline_double_arg();
671         break;
672       case 't': /* Text height */
673         text_height = cmdline_double_arg();
674#ifdef DEBUG_CAD3D
675         printf("Text Height: `%s' input, converted to %6.2f\n", optarg, text_height);
676#endif
677         break;
678       case 'm': /* Marker size */
679         marker_size = cmdline_double_arg();
680#ifdef DEBUG_CAD3D
681         printf("Marker Size: `%s', converted to %6.2f\n", optarg, marker_size);
682#endif
683         break;
684       case 'D':
685         format = FMT_DXF;
686         break;
687       case 'S':
688         format = FMT_SKETCH;
689         break;
690       case 'P':
691         format = FMT_PLT;
692         break;
693       case 'V':
694         format = FMT_SVG;
695         break;
696       case 's':
697         survey = optarg;
698         break;
699#ifdef DEBUG_CAD3D
700       default:
701         printf("Internal Error: 'getopt' returned '%c' %d\n", opt, opt);
702#endif
703      }
704   }
705
706   fnm_3d = argv[optind++];
707   if (argv[optind]) {
708      fnm_out = argv[optind];
709      if (format == FMT_AUTO) {
710         size_t i;
711         size_t len = strlen(fnm_out);
712         format = FMT_DEFAULT;
713         for (i = 0; i < FMT_AUTO; ++i) {
714            size_t l = strlen(extensions[i]);
715            if (len > l + 1 && fnm_out[len - l - 1] == FNM_SEP_EXT &&
716                strcasecmp(fnm_out + len - l, extensions[i]) == 0) {
717               format = i;
718               break;
719            }
720         }
721      }
722   } else {
723      char *baseleaf = baseleaf_from_fnm(fnm_3d);
724      if (format == FMT_AUTO) format = FMT_DEFAULT;
725      /* note : memory allocated by fnm_out gets leaked in this case... */
726      fnm_out = add_ext(baseleaf, extensions[format]);
727      osfree(baseleaf);
728   }
729
730   switch (format) {
731    case FMT_DXF:
732      header = dxf_header;
733      start_pass = dxf_start_pass;
734      move = dxf_move;
735      line = dxf_line;
736      label = dxf_label;
737      cross = dxf_cross;
738      footer = dxf_footer;
739      pass = dxf_passes;
740      break;
741    case FMT_SKETCH:
742      header = sketch_header;
743      start_pass = sketch_start_pass;
744      move = sketch_move;
745      line = sketch_line;
746      label = sketch_label;
747      cross = sketch_cross;
748      footer = sketch_footer;
749      pass = sketch_passes;
750      factor = POINTS_PER_MM * 1000.0 / scale;
751      mode = "wb"; /* Binary file output */
752      break;
753    case FMT_PLT:
754      header = plt_header;
755      start_pass = plt_start_pass;
756      move = plt_move;
757      line = plt_line;
758      label = plt_label;
759      cross = plt_cross;
760      footer = plt_footer;
761      pass = plt_passes;
762      mode = "wb"; /* Binary file output */
763      break;
764    case FMT_SVG:
765      header = svg_header;
766      start_pass = svg_start_pass;
767      move = svg_move;
768      line = svg_line;
769      label = svg_label;
770      cross = svg_cross;
771      footer = svg_footer;
772      pass = svg_passes;
773      factor = 1000.0 / scale;
774      mode = "wb"; /* Binary file output */
775      break;
776    default:
777      exit(1);
778   }
779
780   pimg = img_open_survey(fnm_3d, survey);
781   if (!pimg) fatalerror(img_error(), fnm_3d);
782
783   fh = safe_fopen(fnm_out, mode);
784
785   if (elevation) {
786      s = sin(rad(elev_angle));
787      c = cos(rad(elev_angle));
788   }
789
790   /* Get drawing corners */
791   min_x = min_y = min_z = HUGE_VAL;
792   max_x = max_y = max_z = -HUGE_VAL;
793   do {
794      item = img_read_item(pimg, &p);
795
796      if (elevation) {
797         double xnew = p.x * c - p.y * s;
798         double znew = - p.x * s - p.y * c;
799         p.y = p.z;
800         p.z = znew;
801         p.x = xnew;
802      }
803
804      switch (item) {
805       case img_MOVE: case img_LINE: case img_LABEL:
806         if (p.x < min_x) min_x = p.x;
807         if (p.x > max_x) max_x = p.x;
808         if (p.y < min_y) min_y = p.y;
809         if (p.y > max_y) max_y = p.y;
810         if (p.z < min_z) min_z = p.z;
811         if (p.z > max_z) max_z = p.z;
812         break;
813      }
814   } while (item != img_STOP);
815
816   if (grid > 0) {
817      min_x -= grid / 2;
818      max_x += grid / 2;
819      min_y -= grid / 2;
820      max_y += grid / 2;
821   }
822
823   /* handle empty file gracefully */
824   if (min_x > max_x) {
825      min_x = min_y = min_z = 0;
826      max_x = max_y = max_z = 0;
827   }
828
829   /* Header */
830   header();
831
832   p1.x = p1.y = p1.z = 0; /* avoid compiler warning */
833
834   while (*pass) {
835      if (((*pass & LEGS) && legs) ||
836          ((*pass & STNS) && crosses) ||
837          ((*pass & LABELS) && labels)) {
838         img_rewind(pimg);
839         start_pass(*pass);
840         do {
841            item = img_read_item(pimg, &p);
842
843            if (format == FMT_SKETCH) {
844               p.x -= min_x;
845               p.y -= min_y;
846               p.z -= min_z;
847            }
848
849            if (elevation) {
850               double xnew = p.x * c - p.y * s;
851               double znew = - p.x * s - p.y * c;
852               p.y = p.z;
853               p.z = znew;
854               p.x = xnew;
855            }
856
857            switch (item) {
858             case img_BAD:
859               img_close(pimg);
860               fatalerror(/*Bad 3d image file `%s'*/106, fnm_3d);
861               break;
862             case img_LINE:
863#ifdef DEBUG_CAD3D
864               printf("line to %9.2f %9.2f %9.2f\n", p.x, p.y, p.z);
865#endif
866               if (!fSeenMove) {
867#ifdef DEBUG_CAD3D
868                  printf("Something is wrong -- img_LINE before any img_MOVE!\n");
869#endif
870                  img_close(pimg);
871                  fatalerror(/*Bad 3d image file `%s'*/106, fnm_3d);
872               }
873               if ((*pass & LEGS) && legs)
874                  line(&p1, &p, pimg->flags & img_FLAG_SURFACE);
875               p1 = p;
876               break;
877             case img_MOVE:
878#ifdef DEBUG_CAD3D
879               printf("move to %9.2f %9.2f %9.2f\n",x,y,z);
880#endif
881               if ((*pass & LEGS) && legs) move(&p);
882               fSeenMove = 1;
883               p1 = p;
884               break;
885             case img_LABEL:
886#ifdef DEBUG_CAD3D
887               printf("label `%s' at %9.2f %9.2f %9.2f\n",pimg->label,x,y,z);
888#endif
889               /* Use !UNDERGROUND as the criterion - we want stations where
890                * a surface and underground survey meet to be in the
891                * underground layer */
892               if ((*pass & LABELS) && labels)
893                  label(&p, pimg->label,
894                        !(pimg->flags & img_SFLAG_UNDERGROUND));
895               if ((*pass & STNS) && crosses)
896                  cross(&p, !(pimg->flags & img_SFLAG_UNDERGROUND));
897               break;
898#ifdef DEBUG_CAD3D
899             case img_STOP:
900               printf("stop\n");
901               break;
902             default:
903               printf("other info tag (code %d) ignored\n",item);
904#endif
905            }
906         } while (item != img_STOP);
907      }
908      pass++;
909   }
910   img_close(pimg);
911   footer();
912   safe_fclose(fh);
913   return 0;
914}
Note: See TracBrowser for help on using the repository browser.