source: git/src/cad3d.c @ 6df03c1

RELEASE/1.1RELEASE/1.2debug-cidebug-ci-sanitisersfaster-cavernloglog-selectstereowalls-datawalls-data-hanging-as-warningwarn-only-for-hanging-survey
Last change on this file since 6df03c1 was 63dc4eb, checked in by Olly Betts <olly@…>, 21 years ago

Synchronised with 1.0 branch.

git-svn-id: file:///home/survex-svn/survex/branches/survex-1_1@2472 4b37db11-9a0c-4f06-9ece-9ab7cdaee568

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