File: dxf-import.c

package info (click to toggle)
dia 0.97.3%2Bgit20160930-8.1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 54,328 kB
  • sloc: ansic: 155,065; xml: 16,326; python: 6,641; cpp: 4,935; makefile: 3,833; sh: 540; perl: 137; sed: 19
file content (1396 lines) | stat: -rw-r--r-- 43,367 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
/* -*- Mode: C; c-basic-offset: 4 -*- */
/* Dia -- an diagram creation/manipulation program
 * Copyright (C) 1998 Alexander Larsson
 *
 * dxf-import.c: dxf import filter for dia
 * Copyright (C) 2000 Steffen Macke
 * Copyright (C) 2002 Angus Ainslie 
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
 
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <glib.h>
#include <glib/gstdio.h>

#include "intl.h"
#include "geometry.h"
#include "diarenderer.h"
#include "filter.h"
#include "object.h"
#include "properties.h"
#include "propinternals.h"
#include "autocad_pal.h"

#include "group.h"
#include "create.h"
#include "attributes.h"

static real coord_scale = 1.0, measure_scale = 1.0;
static real text_scale = 1.0;

#define WIDTH_SCALE (coord_scale * measure_scale)
#define DEFAULT_LINE_WIDTH 0.001

Point extent_min, extent_max;
Point limit_min, limit_max;

/* maximum line length */
#define DXF_LINE_LENGTH 257

typedef struct _DxfLayerData 
{
   char layerName[256];
   int acad_colour;
} DxfLayerData;

typedef struct _DxfData
{
    int  code;
    char codeline[DXF_LINE_LENGTH];
    char value[DXF_LINE_LENGTH];
} DxfData;

static gboolean import_dxf(const gchar *filename, DiagramData *dia, DiaContext *ctx, void* user_data);
static gboolean read_dxf_codes(FILE *filedxf, DxfData *data);
static DiaObject *read_entity_line_dxf(FILE *filedxf, DxfData *data, DiagramData *dia);
static DiaObject *read_entity_circle_dxf(FILE *filedxf, DxfData *data, DiagramData *dia);
static DiaObject *read_entity_ellipse_dxf(FILE *filedxf, DxfData *data, DiagramData *dia);
static DiaObject *read_entity_arc_dxf(FILE *filedxf, DxfData *data, DiagramData *dia);
static DiaObject *read_entity_solid_dxf(FILE *filedxf, DxfData *data, DiagramData *dia);
static DiaObject *read_entity_polyline_dxf(FILE *filedxf, DxfData *data, DiagramData *dia);
static DiaObject *read_entity_text_dxf(FILE *filedxf, DxfData *data, DiagramData *dia);
static void read_entity_measurement_dxf(FILE *filedxf, DxfData *data, DiagramData *dia);
static void read_entity_scale_dxf(FILE *filedxf, DxfData *data, DiagramData *dia);
static void read_entity_textsize_dxf(FILE *filedxf, DxfData *data, DiagramData *dia);
static void read_table_layer_dxf(FILE *filedxf, DxfData *data, DiagramData *dia);
static void read_section_header_dxf(FILE *filedxf, DxfData *data, DiagramData *dia);
static void read_section_classes_dxf(FILE *filedxf, DxfData *data, DiagramData *dia);
static void read_section_tables_dxf(FILE *filedxf, DxfData *data, DiagramData *dia);
static void read_section_entities_dxf(FILE *filedxf, DxfData *data, DiagramData *dia);
static void read_section_blocks_dxf(FILE *filedxf, DxfData *data, DiagramData *dia);
static Layer *layer_find_by_name(char *layername, DiagramData *dia);
static LineStyle get_dia_linestyle_dxf(char *dxflinestyle);

GHashTable *_color_by_layer_ht = NULL;

static void
_dxf_color_set_by_layer (const Layer *layer, int color_index)
{
  if (!_color_by_layer_ht) /* lazy creation */
    _color_by_layer_ht = g_hash_table_new (g_direct_hash, g_direct_equal); 
  g_hash_table_insert (_color_by_layer_ht, (void *)layer, GINT_TO_POINTER (color_index));
} 
static int
_dxf_color_get_by_layer (const Layer *layer)
{
  int color_index;

  if (!_color_by_layer_ht)
    return 0;
  color_index = GPOINTER_TO_INT (g_hash_table_lookup (_color_by_layer_ht, layer));
  if (color_index > 0)
    return color_index;
  return 0;
}
static void
_color_init_from_rgb (Color *color, RGB_t rgb)
{
  color->red   = rgb.r / 255.0;
  color->green = rgb.g / 255.0;
  color->blue  = rgb.b / 255.0;
  color->alpha = 1.0;
}

/* returns the layer with the given name */
/* TODO: merge this with other layer code? */
static Layer *
layer_find_by_name(char *layername, DiagramData *dia) 
{
    Layer *matching_layer, *layer;
    guint i;
	
    matching_layer = NULL;
   
    for (i=0; i<dia->layers->len; i++) {
        layer = (Layer *)g_ptr_array_index(dia->layers, i);
        if(strcmp(layer->name, layername) == 0) {
            matching_layer = layer;
            break;
        }
    }

   if( matching_layer == NULL )
     {
	matching_layer = new_layer(g_strdup( layername ), dia);
	data_add_layer(dia, matching_layer);
     }

    return matching_layer;
}

/* returns the matching dia linestyle for a given dxf linestyle */
/* if no matching style is found, LINESTYLE solid is returned as a default */
static LineStyle
get_dia_linestyle_dxf(char *dxflinestyle) 
{
    if (strcmp(dxflinestyle, "DASHED") == 0)
        return LINESTYLE_DASHED;
    if (strcmp(dxflinestyle, "DASHDOT") == 0)
        return LINESTYLE_DASH_DOT;
    if (strcmp(dxflinestyle, "DOT") == 0)
        return LINESTYLE_DOTTED;
    if (strcmp(dxflinestyle, "DIVIDE") == 0)
        return LINESTYLE_DASH_DOT_DOT;

    return LINESTYLE_SOLID;
}

/* reads a line entity from the dxf file and creates a line object in dia*/
static DiaObject *
read_entity_line_dxf(FILE *filedxf, DxfData *data, DiagramData *dia)
{
    /* line data */
    Point start, end;
    
    DiaObjectType *otype = object_get_type("Standard - Line");  
    Handle *h1, *h2;
    
    DiaObject *line_obj;
    Color line_colour;
    RGB_t color = { 0, };
    GPtrArray *props;

    real line_width = DEFAULT_LINE_WIDTH;
    LineStyle style = LINESTYLE_SOLID;
    Layer *layer = dia->active_layer;
    
    end.x=0;
    end.y=0;

    props = g_ptr_array_new();

    do {
        if(read_dxf_codes(filedxf, data) == FALSE){
            return( NULL );
        }
        switch(data->code){
        case 6:	 style = get_dia_linestyle_dxf(data->value);
            break;		
        case  8: 
	    layer = layer_find_by_name(data->value, dia);
	    color = pal_get_rgb (_dxf_color_get_by_layer (layer));
            break;
        case 10:
            start.x = g_ascii_strtod(data->value, NULL) * coord_scale * measure_scale;
            break;
        case 11: 
            end.x = g_ascii_strtod(data->value, NULL) * coord_scale * measure_scale;
            break;
        case 20: 
            start.y = (-1)*g_ascii_strtod(data->value, NULL) * coord_scale * measure_scale;
            break;
        case 21: 
            end.y = (-1)*g_ascii_strtod(data->value, NULL) * coord_scale * measure_scale;
            break;
        case 39: 
            line_width = g_ascii_strtod(data->value, NULL) * WIDTH_SCALE;
	   /*printf( "line width %f\n", line_width ); */
            break;
	case 62 :
            color = pal_get_rgb (atoi(data->value));
            break;
        }	
    } while(data->code != 0);

    _color_init_from_rgb (&line_colour, color);
    line_obj = otype->ops->create(&start, otype->default_user_data,
                                  &h1, &h2);

    prop_list_add_point (props, "start_point", &start);
    prop_list_add_point (props, "end_point", &end);
    prop_list_add_line_colour (props, &line_colour);
    prop_list_add_line_width (props, line_width);
    prop_list_add_line_style (props, style, 1.0);

    line_obj->ops->set_props(line_obj, props);

    prop_list_free(props);

    if (layer)
      layer_add_object(layer, line_obj);
    else
      return line_obj;
    /* don't add it it twice */
    return NULL;
}

/* reads a solid entity from the dxf file and creates a polygon object in dia*/
static DiaObject *
read_entity_solid_dxf(FILE *filedxf, DxfData *data, DiagramData *dia)
{
   /* polygon data */
   Point p[4];
   
   DiaObjectType *otype = object_get_type("Standard - Polygon");  
   Handle *h1, *h2;
   
   DiaObject *polygon_obj;
   MultipointCreateData *pcd;

   Color fill_colour;

   GPtrArray *props;
   
   real line_width = 0.001;
   LineStyle style = LINESTYLE_SOLID;
   Layer *layer = dia->active_layer;
   RGB_t color  = { 127, 127, 127 };
   
/*   printf( "Solid " ); */

   memset(p, 0, sizeof(p));

    do {
        if(read_dxf_codes(filedxf, data) == FALSE){
            return( NULL );
        }
        switch(data->code){
        case 6:	 
	   style = get_dia_linestyle_dxf(data->value);
	   break;		
        case  8: 
	   layer = layer_find_by_name(data->value, dia);
	   color = pal_get_rgb (_dxf_color_get_by_layer (layer));
	   /*printf( "layer: %s ", data->value );*/
	   break;
        case 10:
	   p[0].x = g_ascii_strtod(data->value, NULL) * coord_scale * measure_scale;
	   /*printf( "P0.x: %f ", p[0].x );*/
	   break;
        case 11: 
            p[1].x = g_ascii_strtod(data->value, NULL) * coord_scale * measure_scale;
	   /*printf( "P1.x: %f ", p[1].x );*/
            break;
        case 12: /* bot only swapped, but special for only 3 points given */
            p[2].x = p[3].x = g_ascii_strtod(data->value, NULL) * coord_scale * measure_scale;
	   /*printf( "P2.x: %f ", p[2].x );*/
            break;
        case 13: /* SOLID order swapped compared to Dia's */
            p[2].x = g_ascii_strtod(data->value, NULL) * coord_scale * measure_scale;
	   /*printf( "P3.x: %f ", p[3].x );*/
            break;
        case 20: 
            p[0].y = (-1)*g_ascii_strtod(data->value, NULL) * coord_scale * measure_scale;
	   /*printf( "P0.y: %f ", p[0].y );*/
            break;
        case 21: 
            p[1].y = (-1)*g_ascii_strtod(data->value, NULL) * coord_scale * measure_scale;
	   /*printf( "P1.y: %f ", p[1].y );*/
            break;
        case 22: 
            p[2].y = p[3].y = (-1)*g_ascii_strtod(data->value, NULL) * coord_scale * measure_scale;
	   /*printf( "P2.y: %f ", p[2].y );*/
            break;
        case 23: 
            p[2].y = (-1)*g_ascii_strtod(data->value, NULL) * coord_scale * measure_scale;
	   /*printf( "P3.y: %f\n", p[3].y );*/
            break;
        case 39: 
            line_width = g_ascii_strtod(data->value, NULL) * WIDTH_SCALE;
	   /*printf( "width %f\n", line_width );*/
            break;
        case 62: 
            color = pal_get_rgb (atoi(data->value));
            break;
        }	
    } while(data->code != 0);

   pcd = g_new( MultipointCreateData, 1);
   
   if( p[2].x != p[3].x || p[2].y != p[3].y )
     pcd->num_points = 4;
   else
     pcd->num_points = 3;
   
   pcd->points = g_new( Point, pcd->num_points );
   
   memcpy( pcd->points, p, sizeof( Point ) * pcd->num_points );

   polygon_obj = otype->ops->create( NULL, pcd, &h1, &h2 );

    _color_init_from_rgb (&fill_colour, color);

   props = g_ptr_array_new ();

   prop_list_add_line_colour (props, &fill_colour);
   prop_list_add_line_width (props, line_width);
   prop_list_add_line_style (props, style, 1.0);
   prop_list_add_fill_colour (props, &fill_colour);
   prop_list_add_show_background (props, TRUE);

   polygon_obj->ops->set_props( polygon_obj, props );

   prop_list_free(props);

   if (layer)
      layer_add_object( layer, polygon_obj );
   else
      return polygon_obj;

   return NULL;
}

static int
is_equal( double a, double b )
{
   double epsilon = 0.00001;
   
   if( a == b )
     return( 1 );
   
   if(( a + epsilon ) > b && ( a - epsilon ) < b )
     return( 1 );
   
   return( 0 );
}

/* reads a polyline entity from the dxf file and creates a polyline object in dia*/
static DiaObject *
read_entity_polyline_dxf(FILE *filedxf, DxfData *data, DiagramData *dia)
{
    int i;
   
        /* polygon data */
    Point *p = NULL, start, end, center;
    
    DiaObjectType *otype = object_get_type("Standard - PolyLine");
    Handle *h1, *h2;
    
    DiaObject *polyline_obj;
    MultipointCreateData *pcd;

    Color line_colour;

    GPtrArray *props;

    real line_width = DEFAULT_LINE_WIDTH;
    real radius, start_angle = 0;
    LineStyle style = LINESTYLE_SOLID;
    Layer *layer = dia->active_layer;
    RGB_t color = { 0, };
    unsigned char closed = 0;
    int points = 0;
    real bulge = 0.0;
    int bulge_end = -1;
    gboolean bulge_x_avail = FALSE, bulge_y_avail = FALSE;

    do {
        if(read_dxf_codes(filedxf, data) == FALSE){
            return( NULL );
        }
        switch(data->code){
            case 0:
                if( !strcmp( data->value, "VERTEX" ))
                {
                    points++;
		
                    p = g_realloc( p, sizeof( Point ) * points );
		
                        /*printf( "Vertex %d\n", points );*/
		  
                }
		break;	   
            case 6:	 
                style = get_dia_linestyle_dxf(data->value);
                break;		
            case  8: 
                layer = layer_find_by_name(data->value, dia);
	        color = pal_get_rgb (_dxf_color_get_by_layer (layer));
                    /*printf( "layer: %s ", data->value );*/
                break;
            case 10:
                if( points != 0 )
                {
                    p[points-1].x = g_ascii_strtod(data->value, NULL) * coord_scale * measure_scale;
                        /*printf( "P[%d].x: %f ", points-1, p[points-1].x );*/
		    bulge_x_avail = (bulge_end == points);
                }
                break;
            case 20: 
                if( points != 0 )
                {
                    p[points-1].y = (-1)*g_ascii_strtod(data->value, NULL) * coord_scale * measure_scale;
                        /*printf( "P[%d].y: %f\n", points-1, p[points-1].y );*/
		    bulge_y_avail = (bulge_end == points);
                }
                break;
            case 39: 
                line_width = g_ascii_strtod(data->value, NULL) * WIDTH_SCALE;
                    /*printf( "width %f\n", line_width );*/
                break;
	    case 40: /* default starting width */
	    case 41: /* default ending width */
	        line_width = g_ascii_strtod(data->value, NULL) * WIDTH_SCALE;
		break;
            case 42:
                bulge = g_ascii_strtod(data->value, NULL);
		/* The bulge is meant to be _between_ two VERTEX, here: p[points-1] and p[points,]
		 * but we have not yet read the end point; so just remember the point to 'bulge to' */
		bulge_end = points+1;
		bulge_x_avail = bulge_y_avail = FALSE;
                break;
            case 62: 
                color = pal_get_rgb (atoi(data->value));
                break;
            case 70:
                closed = 1 & atoi( data->value );
                    /*printf( "closed %d %s", closed, data->value );*/
                break;
        }
	if (points == bulge_end && bulge_x_avail && bulge_y_avail) {
	    /* turn the last segment into a bulge */

                p = g_realloc( p, sizeof( Point ) * ( points + 10 ));

                if (points < 2)
                    continue;
                start = p[points-2];
                end = p[points-1];
	   
                radius = sqrt( pow( end.x - start.x, 2 ) + pow( end.y - start.y, 2 ))/2;

                center.x = start.x + ( end.x - start.x )/2;
                center.y = start.y + ( end.y - start.y )/2;
	   
                if( is_equal( start.x, end.x ))
                {
                    if( is_equal( start.y, end.y ))
                    {
                        continue; /* better than complaining? */
                        g_warning("Bad vertex bulge");
                    }
                    else if( start.y > center.y )
                    {
                            /*start_angle = 90.0;*/
                        start_angle = M_PI/2;
                    }
                    else
                    {
                            /*start_angle = 270.0;*/
                        start_angle = M_PI * 1.5;
                    }
                }
                else if( is_equal( start.y, end.y ))
                {
                    if( is_equal( start.x, end.x ))
                    {
                        continue;
                        g_warning("Bad vertex bulge");
                    }
                    else if( start.x > center.x )
                    {
                        start_angle = 0.0;
                    }
                    else
                    {
                        start_angle = M_PI;
                    }
                }
                else
                {
                    start_angle = atan( center.y - start.y /center.x - start.x );
                }
	   
                    /*printf( "start x %f end x %f center x %f\n", start.x, end.x, center.x );
                      printf( "start y %f end y %f center y %f\n", start.y, end.y, center.y );
                      printf( "bulge %s %f startx_angle %f\n", data->value, radius, start_angle );*/
	   
                for( i=(points-1); i<(points+9); i++ )
                {
                    p[i].x = center.x + cos( start_angle ) * radius;
                    p[i].y = center.y + sin( start_angle ) * radius;
                    start_angle += (-M_PI/10.0 * bulge);
                        /*printf( "i %d x %f y %f\n", i, p[i].x, p[i].y );*/
                }
                points += 10;
	   
                p[points-1] = end;
	  
	}
    } while( strcmp( data->value, "SEQEND" ));
   
    if( points == 0 )
    {
        fprintf(stderr,  "No vertexes defined\n" );
        return( NULL );
    }
   
    pcd = g_new( MultipointCreateData, 1);
   
    if( closed )
    {
	otype = object_get_type("Standard - Polygon");
    }
   
    pcd->num_points = points;
    pcd->points = g_new( Point, pcd->num_points );
   
    memcpy( pcd->points, p, sizeof( Point ) * pcd->num_points );
   
    g_free( p );

    polyline_obj = otype->ops->create( NULL, pcd, &h1, &h2 );

    _color_init_from_rgb (&line_colour, color);

    props = g_ptr_array_new ();

    prop_list_add_line_colour (props, &line_colour);
    prop_list_add_line_width (props, line_width);
    prop_list_add_line_style (props, style, 1.0);

    polyline_obj->ops->set_props( polyline_obj, props );

    prop_list_free(props);

    if (layer)
       layer_add_object( layer, polyline_obj );
    else
       return polyline_obj;
       
    return NULL; /* don't add it twice */
}

/* reads a circle entity from the dxf file and creates a circle object in dia*/
static DiaObject *
read_entity_circle_dxf(FILE *filedxf, DxfData *data, DiagramData *dia)
{
    /* circle data */
    Point center = {0, 0};
    real radius = 1.0;
    
    DiaObjectType *otype = object_get_type("Standard - Ellipse");  
    Handle *h1, *h2;
    
    DiaObject *ellipse_obj;
    RGB_t color  = { 0, };
    Color line_colour;

    GPtrArray *props;

    real line_width = DEFAULT_LINE_WIDTH;
    Layer *layer = dia->active_layer;
    
    do {
        if(read_dxf_codes(filedxf, data) == FALSE){
            return( NULL );
        }
        switch(data->code){
        case  8: 
            layer = layer_find_by_name(data->value, dia);
	    color = pal_get_rgb (_dxf_color_get_by_layer (layer));
            break;
        case 10: 
            center.x = g_ascii_strtod(data->value, NULL) * coord_scale * measure_scale;
            break;
        case 20: 
            center.y = (-1)*g_ascii_strtod(data->value, NULL) * coord_scale * measure_scale;
            break;
        case 39: 
            line_width = g_ascii_strtod(data->value, NULL) * WIDTH_SCALE;
            break;
        case 40: 
            radius = g_ascii_strtod(data->value, NULL) * coord_scale * measure_scale;
            break;
	case 62 :
            color = pal_get_rgb (atoi(data->value));
            break;
        }
        
    } while(data->code != 0);
 
    center.x -= radius;
    center.y -= radius;
    ellipse_obj = otype->ops->create(&center, otype->default_user_data,
                                     &h1, &h2);
	
    _color_init_from_rgb (&line_colour, color);

    props = g_ptr_array_new ();

    prop_list_add_point (props, "elem_corner", &center);
    prop_list_add_real (props, "elem_width", radius * 2.0);
    prop_list_add_real (props, "elem_height", radius * 2.0);
    prop_list_add_line_colour (props, &line_colour);
    prop_list_add_line_width (props, line_width);
    prop_list_add_show_background (props, FALSE);
    
    ellipse_obj->ops->set_props(ellipse_obj, props);
    prop_list_free(props);

    if (layer)
        layer_add_object(layer, ellipse_obj);
    else
        return ellipse_obj;

    return NULL; /* don't add it twice */
}

/* reads a circle entity from the dxf file and creates a circle object in dia*/
static DiaObject *
read_entity_arc_dxf(FILE *filedxf, DxfData *data, DiagramData *dia)
{
    /* arc data */
    Point start, end;
    Point center = {0, 0};
    real radius = 1.0, start_angle = 0.0, end_angle=360.0;
    real curve_distance;

    DiaObjectType *otype = object_get_type("Standard - Arc");  
    Handle *h1, *h2;
  
    DiaObject *arc_obj;
    RGB_t color  = { 0, };
    Color line_colour;
    GPtrArray *props;

    real line_width = DEFAULT_LINE_WIDTH;
    Layer *layer = dia->active_layer;
		
    do {
        if(read_dxf_codes(filedxf, data) == FALSE){
            return( NULL );
        }
        switch(data->code){
        case  8: 
            layer = layer_find_by_name(data->value, dia);
	    color = pal_get_rgb (_dxf_color_get_by_layer (layer));
            break;
        case 10: 
            center.x = g_ascii_strtod(data->value, NULL) * coord_scale * measure_scale;
            break;
        case 20: 
            center.y = (-1)*g_ascii_strtod(data->value, NULL) * coord_scale * measure_scale;
            break;
        case 39: 
            line_width = g_ascii_strtod(data->value, NULL) * WIDTH_SCALE;
            break;
        case 40: 
            radius = g_ascii_strtod(data->value, NULL) * coord_scale * measure_scale;
            break;
        case 50:
            start_angle = g_ascii_strtod(data->value, NULL)*M_PI/180.0;
            break;
        case 51:
            end_angle = g_ascii_strtod(data->value, NULL)*M_PI/180.0;
            break;
	case 62 :
            color = pal_get_rgb (atoi(data->value));
            break;
        }
    } while(data->code != 0);

    /* printf("c.x=%f c.y=%f s",center.x,center.y); */
    start.x = center.x + cos(start_angle) * radius; 
    start.y = center.y - sin(start_angle) * radius;
    end.x = center.x + cos(end_angle) * radius;
    end.y = center.y - sin(end_angle) * radius;
    /*printf("s.x=%f s.y=%f e.x=%f e.y=%f\n",start.x,start.y,end.x,end.y);*/


    if (end_angle < start_angle) end_angle += 2.0*M_PI;
    curve_distance = radius * (1 - cos ((end_angle - start_angle)/2));

    /*printf("start_angle: %f end_angle: %f radius:%f  curve_distance:%f\n",
      start_angle,end_angle,radius,curve_distance);*/
   
    arc_obj = otype->ops->create(&center, otype->default_user_data,
                                     &h1, &h2);
    
    _color_init_from_rgb (&line_colour, color);

    props = g_ptr_array_new ();
    prop_list_add_point (props, "start_point", &start);
    prop_list_add_point (props, "end_point", &end);
    prop_list_add_real (props, "curve_distance", curve_distance);
    prop_list_add_line_colour (props, &line_colour);
    prop_list_add_line_width (props, line_width);
    
    arc_obj->ops->set_props(arc_obj, props);
    prop_list_free(props);
   
    if (layer)
        layer_add_object(layer, arc_obj);
    else
        return arc_obj ;

    return NULL; /* don't add it twice */
}

/* reads an ellipse entity from the dxf file and creates an ellipse object in dia*/
static DiaObject *
read_entity_ellipse_dxf(FILE *filedxf, DxfData *data, DiagramData *dia)
{
    /* ellipse data */
    Point center = {0, 0};
    real width = 1.0;
    real ratio_width_height = 1.0;
    
    DiaObjectType *otype = object_get_type("Standard - Ellipse");
    Handle *h1, *h2;
    
    DiaObject *ellipse_obj; 
    RGB_t color = { 0, };
    Color line_colour;
    GPtrArray *props;

    real line_width = DEFAULT_LINE_WIDTH;
    Layer *layer = dia->active_layer;
    
    do {
        if(read_dxf_codes(filedxf, data) == FALSE){
            return( NULL );
        }
        switch(data->code){
        case  8: 
            layer = layer_find_by_name(data->value, dia);
	    color = pal_get_rgb (_dxf_color_get_by_layer (layer));
            break;
        case 10: 
            center.x = g_ascii_strtod(data->value, NULL) * coord_scale * measure_scale;
            break;
        case 11: 
            ratio_width_height = g_ascii_strtod(data->value, NULL) * coord_scale * measure_scale;
            break;
        case 20: 
            center.y = (-1)*g_ascii_strtod(data->value, NULL) * coord_scale * measure_scale;
            break;
        case 39: 
            line_width = g_ascii_strtod(data->value, NULL) * WIDTH_SCALE;
            break;
        case 40: 
            width = g_ascii_strtod(data->value, NULL) * WIDTH_SCALE; /* XXX what scale */
            break;
	case 62 :
            color = pal_get_rgb (atoi(data->value));
            break;
        }
    } while(data->code != 0);
 
    center.x -= width;
    center.y -= (width*ratio_width_height);
    ellipse_obj = otype->ops->create(&center, otype->default_user_data,
                                     &h1, &h2);

    _color_init_from_rgb (&line_colour, color);

    props = g_ptr_array_new ();

    prop_list_add_point (props, "elem_corner", &center);
    prop_list_add_real (props, "elem_width", width);
    prop_list_add_real (props, "elem_height", width * ratio_width_height);
    prop_list_add_line_colour (props, &line_colour);
    prop_list_add_line_width (props, line_width);
    prop_list_add_show_background (props, FALSE);
    
    ellipse_obj->ops->set_props(ellipse_obj, props);
    prop_list_free(props);

    if (layer)
        layer_add_object(layer, ellipse_obj);
    else
        return ellipse_obj;

    return NULL; /* don't add it twice */
}

static PropDescription dxf_text_prop_descs[] = {
    { "text", PROP_TYPE_TEXT },
    PROP_DESC_END};

static DiaObject *
read_entity_text_dxf(FILE *filedxf, DxfData *data, DiagramData *dia)
{
    RGB_t color = { 0, };

    /* text data */
    Point location = {0, 0};
    real height = text_scale * coord_scale * measure_scale;
    real y_offset = 0;
    Alignment textalignment = ALIGN_LEFT;
    char *textvalue = NULL, *textp;
    
    DiaObjectType *otype = object_get_type("Standard - Text");
    Handle *h1, *h2;
    
    DiaObject *text_obj;
    Color text_colour;

    TextProperty *tprop;
    GPtrArray *props;

    Layer *layer = dia->active_layer;

    do {
        if (read_dxf_codes(filedxf, data) == FALSE) {
            return( NULL );
        }
        switch (data->code) {
        case  1: 
	   textvalue = g_strdup(data->value);
	   textp = textvalue;
	   /* FIXME - poor tab to space converter */
	   do 
	     {
		if( textp[0] == '^' && textp[1] == 'I' )
		  {
		     textp[0] = ' ';
		     textp[1] = ' ';
		     textp++;
		  }
		
	     }
	   while( *(++textp) != '\0' );
		
	   /*printf( "Found text: %s\n", textvalue );*/
            break;
        case  8: 
	    layer = layer_find_by_name(data->value, dia);
	    color = pal_get_rgb (_dxf_color_get_by_layer (layer));
            break;
        case 10: 
            location.x = g_ascii_strtod(data->value, NULL) * coord_scale * measure_scale;
	   /*printf( "Found text location x: %f\n", location.x );*/
            break;
        case 11:
            location.x = g_ascii_strtod(data->value, NULL) * coord_scale * measure_scale;
	   /*printf( "Found text location x: %f\n", location.x );*/
            break;
        case 20:
            location.y = (-1)*g_ascii_strtod(data->value, NULL) * coord_scale * measure_scale;
	   /*printf( "Found text location y: %f\n", location.y );*/
            break;
        case 21:
            location.y = (-1)*g_ascii_strtod(data->value, NULL) * coord_scale * measure_scale;
	   /*location.y = (-1)*g_ascii_strtod(data->value, NULL) / text_scale;*/
	   /*printf( "Found text location y: %f\n", location.y );*/
            break;
        case 40: 
            height = g_ascii_strtod(data->value, NULL) * text_scale * coord_scale * measure_scale;
	   /*printf( "text height %f\n", height );*/
            break;
	 case 62: 
	    color = pal_get_rgb (atoi(data->value));
            break;
        case 72: 
	   switch(atoi(data->value))
	     {
	      case 0:
		textalignment = ALIGN_LEFT;
                break;
	      case 1: 
		textalignment = ALIGN_CENTER;
                break;
	      case 2: 
		textalignment = ALIGN_RIGHT;
                break;	
	      case 3:
		/* FIXME - it's not clear what these are */
                break;
	      case 4: 
		/* FIXME - it's not clear what these are */
                break;	
	      case 5: 
		/* FIXME - it's not clear what these are */
                break;	
            }
	   break;
        case 73: 
	   switch(atoi(data->value))
	     {
	      case 0:
	      case 1:
		/* FIXME - not really the same vertical alignment */
		/* 0 = baseline */
		/* 1 = bottom */
		y_offset = 0;
                break;
	      case 2: 
		/* 2 = middle */
		y_offset = 0.5;
                break;	
	      case 3:
		/* 3 = top */
		y_offset = 1;
                break;	
            }
	   break;
        }
    } while(data->code != 0);
  
    location.y += y_offset * height;
    _color_init_from_rgb (&text_colour, color);
    text_obj = otype->ops->create(&location, otype->default_user_data,
                                  &h1, &h2);
    props = prop_list_from_descs(dxf_text_prop_descs,pdtpp_true);
    g_assert(props->len == 1);

    tprop = g_ptr_array_index(props,0);
    g_free(tprop->text_data);
    tprop->text_data = textvalue;
    tprop->attr.alignment = textalignment;
    tprop->attr.position.x = location.x;
    tprop->attr.position.y = location.y;

    attributes_get_default_font(&tprop->attr.font, &tprop->attr.height);
    tprop->attr.color = text_colour;
    tprop->attr.height = height;
        
    text_obj->ops->set_props(text_obj, props);
    prop_list_free(props);
   
    if (layer)
        layer_add_object(layer, text_obj);
    else
        return text_obj;

    return NULL; /* don't add it twice */
}

/* reads the layer table from the dxf file and creates the layers */
static void
read_table_layer_dxf(FILE *filedxf, DxfData *data, DiagramData *dia)
{
     Layer *layer = NULL;
     int color_index;

     do {
	if (read_dxf_codes(filedxf, data) == FALSE)
	    return;
	switch (data->code) {
	case 2 : /* layer name */
	    layer = layer_find_by_name( data->value, dia );
	    break;
	case 62 : /* Color number, if negative layer is off */
	    color_index = atoi(data->value);
	    if (layer && color_index < 0)
		layer->visible = FALSE;
	    else
	        _dxf_color_set_by_layer (layer, color_index);
	    break;
	} 
     } while ((data->code != 0) || (strcmp(data->value, "ENDTAB") != 0));
}

/* reads a scale entity from the dxf file */
static void
read_entity_scale_dxf(FILE *filedxf, DxfData *data, DiagramData *dia)
{
   if(read_dxf_codes(filedxf, data) == FALSE)
      return;

   switch(data->code)
     {
      case 40: 
	coord_scale = g_ascii_strtod(data->value, NULL);
	if (coord_scale > 0.0)
	  coord_scale = 1.0 / coord_scale;
	g_message("Scale: %f", coord_scale );
	break;
      
      default:
	break;
     }	
   
}

static void
read_entitiy_lengthunit_dxf(FILE *filedxf, DxfData *data, DiagramData *dia)
{
   real e; /* undocumented ... */

   if(read_dxf_codes(filedxf, data) == FALSE)
      return;

   switch(data->code)
     {
      case 70: 
	/* 1 = Scientific; 2 = Decimal; 3 = Engineering;
         * 4 = Architectural; 5 = Fractional; 6 = Windows desktop 
	 */
	e = atoi(data->value);
	g_message("LengthUnit: %f:%f", e, coord_scale );
	break;
      
      default:
	break;
     }	
   
}

/* reads a scale entity from the dxf file */
static void 
read_entity_measurement_dxf(FILE *filedxf, DxfData *data, DiagramData *dia)
{
   if(read_dxf_codes(filedxf, data) == FALSE)
      return;

   switch(data->code)
     {
      case 70:
	/* value 0 = English, 1 = Metric */
	if( atoi( data->value ) == 0 )
	  measure_scale = 2.54;
	else
	  measure_scale = 1.0;
	/*printf( "Measure Scale: %f\n", measure_scale );*/
	break;
      
      default:
	break;
     }	
   
}

/* reads a textsize entity from the dxf file */
static void
read_entity_textsize_dxf(FILE *filedxf, DxfData *data, DiagramData *dia)
{
   if(read_dxf_codes(filedxf, data) == FALSE)
     return;
     
   switch(data->code)
     {
      case 40:
	text_scale = g_ascii_strtod(data->value, NULL);
	if (text_scale > 0.0)
	  text_scale = 1.0 / text_scale;
	/*printf( "Text Size: %f\n", text_scale );*/
	break;		
      default:
	break;
     }	

}

/* reads the headers section of the dxf file */
static void
read_section_header_dxf(FILE *filedxf, DxfData *data, DiagramData *dia) 
{
    if(read_dxf_codes(filedxf, data) == FALSE){
        return;
    }
    do {
       if((data->code == 9) && (strcmp(data->value, "$DIMSCALE") == 0)) {
	  read_entity_scale_dxf(filedxf, data, dia);
	} else if((data->code == 9) && (strcmp(data->value, "DIMLUNIT") == 0)) {
	  /* nothing documented */
	  read_entitiy_lengthunit_dxf(filedxf, data, dia);
        } else if((data->code == 9) && (strcmp(data->value, "$TEXTSIZE") == 0)) {
	  read_entity_textsize_dxf(filedxf, data, dia);
        } else if((data->code == 9) && (strcmp(data->value, "$MEASUREMENT") == 0)) {
	  read_entity_measurement_dxf(filedxf, data, dia);
        } else {
	   if(read_dxf_codes(filedxf, data) == FALSE){
	      return;
	   }
	   
	}
    } while ((data->code != 0) || (strcmp(data->value, "ENDSEC") != 0));
}

/* reads the classes section of the dxf file */
static void
read_section_classes_dxf(FILE *filedxf, DxfData *data, DiagramData *dia) 
{
    if(read_dxf_codes(filedxf, data) == FALSE){
        return;
    }
    do {
       if((data->code == 9) && (strcmp(data->value, "$LTSCALE") == 0)) {
	  read_entity_scale_dxf(filedxf, data, dia);
        } else if((data->code == 9) && (strcmp(data->value, "$TEXTSIZE") == 0)) {
	  read_entity_textsize_dxf(filedxf, data, dia);
        } else {
	   if(read_dxf_codes(filedxf, data) == FALSE){
	      return;
	   }
	   
	}
    } while ((data->code != 0) || (strcmp(data->value, "ENDSEC") != 0));
}

/* reads the tables section of the dxf file */
static void
read_section_tables_dxf(FILE *filedxf, DxfData *data, DiagramData *dia) 
{
    if(read_dxf_codes(filedxf, data) == FALSE){
        return;
    }
    do {
        if((data->code == 0) && (strcmp(data->value, "LAYER") == 0)) {
            read_table_layer_dxf(filedxf, data, dia);
        }
        else {
            if(read_dxf_codes(filedxf, data) == FALSE){
                return;
            }
        }		
    } while ((data->code != 0) || (strcmp(data->value, "ENDSEC") != 0));
}

/* reads the entities section of the dxf file */
static void
read_section_entities_dxf(FILE *filedxf, DxfData *data, DiagramData *dia) 
{
    if (read_dxf_codes(filedxf, data) == FALSE){
        return;		
    }
    do {  
        if((data->code == 0) && (strcmp(data->value, "LINE") == 0)) {
            read_entity_line_dxf(filedxf, data, dia);
        } else if((data->code == 0) && (strcmp(data->value, "VERTEX") == 0)) {
            read_entity_line_dxf(filedxf, data, dia);
        } else if((data->code == 0) && (strcmp(data->value, "SOLID") == 0)) {
            read_entity_solid_dxf(filedxf, data, dia);
        } else if((data->code == 0) && (strcmp(data->value, "POLYLINE") == 0)) {
            read_entity_polyline_dxf(filedxf, data, dia);
        } else if((data->code == 0) && (strcmp(data->value, "CIRCLE") == 0)) {
            read_entity_circle_dxf(filedxf, data, dia);
        } else if((data->code == 0) && (strcmp(data->value, "ELLIPSE") == 0)) {
            read_entity_ellipse_dxf(filedxf, data, dia);
        } else if((data->code == 0) && (strcmp(data->value, "TEXT") == 0)) {
            read_entity_text_dxf(filedxf, data, dia);
        } else if((data->code == 0) && (strcmp(data->value, "ARC") == 0)) {
               read_entity_arc_dxf(filedxf,data,dia);
        } else {
            if(read_dxf_codes(filedxf, data) == FALSE) {
                return;
            }
        }
    } while((data->code != 0) || (strcmp(data->value, "ENDSEC") != 0));
}

/* reads the blocks section of the dxf file */
static void
read_section_blocks_dxf(FILE *filedxf, DxfData *data, DiagramData *dia) 
{
    int group_items = 0, group = 0;
    GList *group_list = NULL;
    DiaObject *obj = NULL;
    Layer *group_layer = NULL;
   
    if (read_dxf_codes(filedxf, data) == FALSE){
        return;		
    }
    do {  
        if((data->code == 0) && (strcmp(data->value, "LINE") == 0)) {
            obj = read_entity_line_dxf(filedxf, data, dia);
        } else if((data->code == 0) && (strcmp(data->value, "SOLID") == 0)) {
            obj = read_entity_solid_dxf(filedxf, data, dia);
        } else if((data->code == 0) && (strcmp(data->value, "VERTEX") == 0)) {
            read_entity_line_dxf(filedxf, data, dia);
        } else if((data->code == 0) && (strcmp(data->value, "POLYLINE") == 0)) {
            obj = read_entity_polyline_dxf(filedxf, data, dia);
        } else if((data->code == 0) && (strcmp(data->value, "CIRCLE") == 0)) {
            obj = read_entity_circle_dxf(filedxf, data, dia);
        } else if((data->code == 0) && (strcmp(data->value, "ELLIPSE") == 0)) {
            obj = read_entity_ellipse_dxf(filedxf, data, dia);
        } else if((data->code == 0) && (strcmp(data->value, "TEXT") == 0)) {
            obj = read_entity_text_dxf(filedxf, data, dia);
        } else if((data->code == 0) && (strcmp(data->value, "ARC") == 0)) {
            obj = read_entity_arc_dxf(filedxf,data,dia);
        } else if((data->code == 0) && (strcmp(data->value, "BLOCK") == 0)) {
                /* printf("Begin group\n" ); */
	 
            group = TRUE;
            group_items = 0;
            group_list = NULL;
            group_layer = NULL;
	 
            do {
                if(read_dxf_codes(filedxf, data) == FALSE)
                    return;

                if(data->code == 8) {
                    group_layer = layer_find_by_name( data->value, dia );
		    data_set_active_layer (dia, group_layer);
		}

            } while(data->code != 0);
	
        } else if((data->code == 0) && (strcmp(data->value, "ENDBLK") == 0)) {
                /* printf( "End group %d\n", group_items ); */

            if( group && group_items > 0 && group_list != NULL )
            {
                obj = group_create( group_list );
                if( NULL == group_layer )
                    layer_add_object( dia->active_layer, obj );
                else
                    layer_add_object( group_layer, obj );
            }
	 
            group = FALSE;
            group_items = 0;
            group_list = NULL;
            obj = NULL;
				  
            if(read_dxf_codes(filedxf, data) == FALSE)
                return;
	 
        } else {
            if(read_dxf_codes(filedxf, data) == FALSE) {
                return;
            }
        }
      
        if( group && obj != NULL )
        {
            group_items++;

            group_list = g_list_prepend( group_list, obj );
	   
            obj = NULL;
        }
      
    } while((data->code != 0) || (strcmp(data->value, "ENDSEC") != 0));
}

/* imports the given dxf-file, returns TRUE if successful */
static gboolean
import_dxf(const gchar *filename, DiagramData *dia, DiaContext *ctx, void* user_data)
{
    FILE *filedxf;
    DxfData *data;
    
    filedxf = g_fopen(filename,"r");
    if(filedxf == NULL){
        dia_context_add_message(ctx, _("Couldn't open: '%s' for reading.\n"),
				dia_context_get_filename (ctx));
        return FALSE;
    }
    
    data = g_new(DxfData, 1);
    
    do {
        if(read_dxf_codes(filedxf, data) == FALSE) {
            g_free(data);
	    dia_context_add_message(ctx, _("read_dxf_codes failed on '%s'"),
			            dia_context_get_filename(ctx) );
            return FALSE;
        }
        else {
            if (0 == data->code && strstr(data->codeline, "AutoCAD Binary DXF")) {
                g_free(data);
	        dia_context_add_message(ctx, _("Binary DXF from '%s' not supported"),
			                dia_context_get_filename(ctx));
                return FALSE;
            }
	    if (0 == data->code) {
                if(strcmp(data->value, "SECTION") == 0) {
		  /* don't think we need to do anything */
                } else if(strcmp(data->value, "ENDSEC") == 0) {
		  /* don't think we need to do anything */
                } else if(strcmp(data->value, "EOF") == 0) {
		  /* handled below */
		} else {
		  g_printerr ("DXF 0:%s not handled\n", data->value);
		}
            } else if(data->code == 2) {
                if(strcmp(data->value, "ENTITIES") == 0) {
		   /*printf( "reading section entities\n" );*/
                    read_section_entities_dxf(filedxf, data, dia);
                }
                else if(strcmp(data->value, "BLOCKS") == 0) {
		   /*printf( "reading section BLOCKS\n" );*/
                    read_section_blocks_dxf(filedxf, data, dia);
                }
                else if(strcmp(data->value, "CLASSES") == 0) {
		   /*printf( "reading section CLASSES\n" );*/
                    read_section_classes_dxf(filedxf, data, dia);
                }
                else if(strcmp(data->value, "HEADER") == 0) {
		   /*printf( "reading section HEADER\n" );*/
                    read_section_header_dxf(filedxf, data, dia);
                }
                else if(strcmp(data->value, "TABLES") == 0) {
		  /*printf( "reading section tables\n" );*/
                    read_section_tables_dxf(filedxf, data, dia);
                }
	        else if(strcmp(data->value, "OBJECTS") == 0) {
		  /*printf( "reading section objects\n" );*/
                    read_section_entities_dxf(filedxf, data, dia);
		}
            } else if(data->code == 999) {
	      /* Don't complain on comments, but silently ignore */
	    }
	   else
	     g_warning("Unknown dxf code %d", data->code);
        }
    }while((data->code != 0) || (strcmp(data->value, "EOF") != 0));
    
    g_free(data);
    if (_color_by_layer_ht) {
        g_hash_table_destroy (_color_by_layer_ht);
        _color_by_layer_ht = NULL;
    }
    return TRUE;
}

/* reads a code/value pair from the DXF file */
static gboolean 
read_dxf_codes(FILE *filedxf, DxfData *data)
{
    int i;
    char *c;
    
    if(fgets(data->codeline, DXF_LINE_LENGTH, filedxf) == NULL){
        return FALSE;
    }
    data->code = atoi(data->codeline);
    if(fgets(data->value, DXF_LINE_LENGTH, filedxf) == NULL){
        return FALSE;
    }
    c=data->value;
    for(i=0; i < DXF_LINE_LENGTH; i++){		
        if((c[i] == '\n')||(c[i] == '\r')){
            c[i] = 0;
            break;
        }
    }
    return TRUE;	
}

/* interface from filter.h */

static const gchar *extensions[] = {"dxf", NULL };
DiaImportFilter dxf_import_filter = {
	N_("Drawing Interchange File"),
	extensions,
	import_dxf
};