~siretart/ubuntu/utopic/blender/libav10

« back to all changes in this revision

Viewing changes to source/blender/blenkernel/intern/exotic.c

  • Committer: Package Import Robot
  • Author(s): Matteo F. Vescovi
  • Date: 2012-07-23 08:54:18 UTC
  • mfrom: (14.2.16 sid)
  • mto: (14.2.19 sid)
  • mto: This revision was merged to the branch mainline in revision 42.
  • Revision ID: package-import@ubuntu.com-20120723085418-9foz30v6afaf5ffs
Tags: 2.63a-2
* debian/: Cycles support added (Closes: #658075)
  For now, this top feature has been enabled only
  on [any-amd64 any-i386] architectures because
  of OpenImageIO failing on all others
* debian/: scripts installation path changed
  from /usr/lib to /usr/share:
  + debian/patches/: patchset re-worked for path changing
  + debian/control: "Breaks" field added on yafaray-exporter

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  exotic.c   
2
 
 *
3
 
 * ***** BEGIN GPL LICENSE BLOCK *****
4
 
 *
5
 
 * This program is free software; you can redistribute it and/or
6
 
 * modify it under the terms of the GNU General Public License
7
 
 * as published by the Free Software Foundation; either version 2
8
 
 * of the License, or (at your option) any later version.
9
 
 *
10
 
 * This program is distributed in the hope that it will be useful,
11
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 * GNU General Public License for more details.
14
 
 *
15
 
 * You should have received a copy of the GNU General Public License
16
 
 * along with this program; if not, write to the Free Software Foundation,
17
 
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
 
 *
19
 
 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20
 
 * All rights reserved.
21
 
 *
22
 
 *
23
 
 * Contributor(s): 
24
 
 * - Martin DeMello
25
 
 *   Added dxf_read_arc, dxf_read_ellipse and dxf_read_lwpolyline
26
 
 *   Copyright (C) 2004 by Etheract Software Labs
27
 
 *
28
 
 * - Blender Foundation
29
 
 *
30
 
 * ***** END GPL LICENSE BLOCK *****/
31
 
 
32
 
#include "BLI_storage.h"
33
 
 
34
 
#include <ctype.h> /* isdigit, isspace */
35
 
#include <math.h>
36
 
#include <stdio.h>
37
 
#include <stdlib.h>
38
 
#include <fcntl.h>
39
 
#include <string.h>
40
 
#include <errno.h>
41
 
 
42
 
#ifndef _WIN32 
43
 
#include <unistd.h>
44
 
#else
45
 
#include <io.h>
46
 
#define open _open
47
 
#define read _read
48
 
#define close _close
49
 
#define write _write
50
 
#endif
51
 
 
52
 
#include "MEM_guardedalloc.h"
53
 
 
54
 
#include "DNA_object_types.h"
55
 
#include "DNA_mesh_types.h"
56
 
#include "DNA_meshdata_types.h"
57
 
#include "DNA_material_types.h"
58
 
#include "DNA_curve_types.h"
59
 
#include "DNA_camera_types.h"
60
 
#include "DNA_scene_types.h"
61
 
 
62
 
#include "BKE_utildefines.h"
63
 
#include "BLI_blenlib.h"
64
 
#include "BLI_math.h"
65
 
 
66
 
#include "BKE_blender.h"
67
 
#include "BKE_global.h"
68
 
#include "BKE_main.h"
69
 
#include "BKE_mesh.h"
70
 
#include "BKE_library.h"
71
 
#include "BKE_object.h"
72
 
#include "BKE_material.h"
73
 
#include "BKE_report.h"
74
 
 
75
 
#include "BKE_displist.h"
76
 
#include "BKE_DerivedMesh.h"
77
 
#include "BKE_curve.h"
78
 
 
79
 
#ifndef DISABLE_PYTHON
80
 
#include "BPY_extern.h"
81
 
#endif
82
 
 
83
 
#include "zlib.h"
84
 
 
85
 
static int is_dxf(char *str);
86
 
static void dxf_read(Scene *scene, char *filename);
87
 
static int is_stl(char *str);
88
 
 
89
 
static int is_stl_ascii(char *str)
90
 
{       
91
 
        FILE *fpSTL;
92
 
        char buffer[1000];
93
 
        int  numread, i;
94
 
 
95
 
        fpSTL = fopen(str, "rb");
96
 
        if ( (numread = fread( (void *) buffer, sizeof(char), 1000, fpSTL)) <= 0 )
97
 
          { fclose(fpSTL); return 0; }
98
 
 
99
 
        for (i=0; i < numread; ++i) {
100
 
          /* if bit 8 is set we assume binary */
101
 
          if (buffer[i] & 0x80)
102
 
                { fclose(fpSTL); return 0; }
103
 
        }
104
 
 
105
 
        buffer[5] = '\0';
106
 
        if ( !(strstr(buffer, "solid")) && !(strstr(buffer, "SOLID")) ) 
107
 
          { fclose(fpSTL); return 0; }
108
 
 
109
 
        fclose(fpSTL);
110
 
        
111
 
        return 1;
112
 
}
113
 
 
114
 
static int is_stl(char *str)
115
 
{
116
 
        int i;
117
 
        i = strlen(str) - 3;
118
 
        if ( (str[i] !='s') && (str[i] !='S'))
119
 
                return 0;
120
 
        i++;
121
 
        if ( (str[i] !='t') && (str[i] !='T'))
122
 
                return 0;
123
 
        i++;
124
 
        if ( (str[i] !='l') && (str[i] !='L'))
125
 
                return 0;
126
 
 
127
 
        return 1;
128
 
}
129
 
 
130
 
#define READSTLVERT {                                   \
131
 
  if (fread(mvert->co, sizeof(float), 3, fpSTL) != 3) { \
132
 
        char error_msg[255];                                \
133
 
        MEM_freeN(vertdata);                                \
134
 
        MEM_freeN(facedata);                                \
135
 
        fclose(fpSTL);                                      \
136
 
        sprintf(error_msg, "Problems reading face %d!", i); \
137
 
        return;                                             \
138
 
  }                                                     \
139
 
  else {                                                \
140
 
        if (ENDIAN_ORDER==B_ENDIAN) {                       \
141
 
          SWITCH_INT(mvert->co[0]);                         \
142
 
          SWITCH_INT(mvert->co[1]);                         \
143
 
          SWITCH_INT(mvert->co[2]);                         \
144
 
        }                                                   \
145
 
  }                                                     \
146
 
}
147
 
 
148
 
static void simple_vertex_normal_blend(short *no, short *ble)
149
 
{
150
 
        if(no[0]==0 && no[1]==0 && no[2]==0) {
151
 
                VECCOPY(no, ble);
152
 
        }
153
 
        else {
154
 
                no[0]= (2*no[0] + ble[0])/3;
155
 
                no[1]= (2*no[1] + ble[1])/3;
156
 
                no[2]= (2*no[2] + ble[2])/3;
157
 
        }
158
 
}
159
 
 
160
 
static void mesh_add_normals_flags(Mesh *me)
161
 
{
162
 
        MVert *v1, *v2, *v3, *v4;
163
 
        MFace *mface;
164
 
        float nor[3];
165
 
        int a;
166
 
        short sno[3];
167
 
        
168
 
        mface= me->mface;
169
 
        for(a=0; a<me->totface; a++, mface++) {
170
 
                v1= me->mvert+mface->v1;
171
 
                v2= me->mvert+mface->v2;
172
 
                v3= me->mvert+mface->v3;
173
 
                v4= me->mvert+mface->v4;
174
 
                
175
 
                normal_tri_v3( nor,v1->co, v2->co, v3->co);
176
 
                sno[0]= 32767.0*nor[0];
177
 
                sno[1]= 32767.0*nor[1];
178
 
                sno[2]= 32767.0*nor[2];
179
 
                
180
 
                simple_vertex_normal_blend(v1->no, sno);
181
 
                simple_vertex_normal_blend(v2->no, sno);
182
 
                simple_vertex_normal_blend(v3->no, sno);
183
 
                if(mface->v4) {
184
 
                        simple_vertex_normal_blend(v4->no, sno);
185
 
                }
186
 
                mface->edcode= ME_V1V2|ME_V2V3;
187
 
        }       
188
 
}
189
 
 
190
 
static void read_stl_mesh_binary(Scene *scene, char *str)
191
 
{
192
 
        FILE   *fpSTL;
193
 
        Object *ob;
194
 
        Mesh   *me;
195
 
        MVert  *mvert, *vertdata;
196
 
        MFace  *mface, *facedata;
197
 
        unsigned int numfacets = 0, i, j, vertnum;
198
 
        unsigned int maxmeshsize, nummesh, lastmeshsize;
199
 
        unsigned int totvert, totface;
200
 
        ReportList *reports= NULL; /* XXX */
201
 
 
202
 
        fpSTL= fopen(str, "rb");
203
 
        if(fpSTL==NULL) {
204
 
                BKE_reportf(reports, RPT_ERROR, "Can't read file: %s.", strerror(errno));
205
 
                return;
206
 
        }
207
 
 
208
 
        if(fseek(fpSTL, 80, SEEK_SET) != 0) {
209
 
                BKE_reportf(reports, RPT_ERROR, "Failed reading file: %s.", strerror(errno));
210
 
                fclose(fpSTL);
211
 
                return;
212
 
        }
213
 
 
214
 
        if(fread(&numfacets, 4*sizeof(char), 1, fpSTL) != 1) {
215
 
                if(feof(fpSTL))
216
 
                        BKE_reportf(reports, RPT_ERROR, "Failed reading file: premature end of file.");
217
 
                else
218
 
                        BKE_reportf(reports, RPT_ERROR, "Failed reading file: %s.", strerror(errno));
219
 
                fclose(fpSTL);
220
 
                return;
221
 
        }
222
 
        if (ENDIAN_ORDER==B_ENDIAN) {
223
 
                SWITCH_INT(numfacets);
224
 
        }
225
 
 
226
 
        maxmeshsize = MESH_MAX_VERTS/3;
227
 
 
228
 
        nummesh      = (numfacets / maxmeshsize) + 1;
229
 
        lastmeshsize = numfacets % maxmeshsize;
230
 
 
231
 
        if (numfacets) {
232
 
                for (j=0; j < nummesh; ++j) {
233
 
                        /* new object */
234
 
                        if (j == nummesh-1) {
235
 
                                totface = lastmeshsize;
236
 
                        }
237
 
                        else {
238
 
                                totface = maxmeshsize;
239
 
                        }
240
 
                        totvert = 3 * totface;
241
 
        
242
 
                        vertdata = MEM_callocN(totvert*sizeof(MVert), "mverts");
243
 
                        facedata = MEM_callocN(totface*sizeof(MFace), "mface");
244
 
 
245
 
                        vertnum = 0;
246
 
                        mvert= vertdata;
247
 
                        mface = facedata;
248
 
                        for (i=0; i < totface; i++) {
249
 
                                fseek(fpSTL, 12, SEEK_CUR); /* skip the face normal */
250
 
                                READSTLVERT;
251
 
                                mvert++;
252
 
                                READSTLVERT;
253
 
                                mvert++;
254
 
                                READSTLVERT;
255
 
                                mvert++;
256
 
 
257
 
                                mface->v1 = vertnum++;
258
 
                                mface->v2 = vertnum++;
259
 
                                mface->v3 = vertnum++;
260
 
                                mface++;
261
 
 
262
 
                                fseek(fpSTL, 2, SEEK_CUR);
263
 
                        }
264
 
 
265
 
                        ob= add_object(scene, OB_MESH);
266
 
                        me= ob->data;
267
 
                        me->totvert = totvert;
268
 
                        me->totface = totface;
269
 
                        me->mvert = CustomData_add_layer(&me->vdata, CD_MVERT, CD_ASSIGN,
270
 
                                                                                         vertdata, totvert);
271
 
                        me->mface = CustomData_add_layer(&me->fdata, CD_MFACE, CD_ASSIGN,
272
 
                                                                                         facedata, totface);
273
 
 
274
 
                        mesh_add_normals_flags(me);
275
 
                        make_edges(me, 0);
276
 
                }
277
 
                //XXX waitcursor(1);
278
 
        }
279
 
        fclose(fpSTL);
280
 
 
281
 
}
282
 
#undef READSTLVERT
283
 
 
284
 
#define STLALLOCERROR { \
285
 
        char error_msg[255]; \
286
 
        fclose(fpSTL); \
287
 
        sprintf(error_msg, "Can't allocate storage for %d faces!", \
288
 
                        numtenthousand * 10000); \
289
 
        return; \
290
 
}
291
 
 
292
 
#define STLBAILOUT(message) { \
293
 
        char error_msg[255]; \
294
 
        fclose(fpSTL); \
295
 
        free(vertdata); \
296
 
        sprintf(error_msg, "Line %d: %s", linenum, message); \
297
 
        return; \
298
 
}
299
 
 
300
 
#define STLREADLINE { \
301
 
        if (!fgets(buffer, 2048, fpSTL)) STLBAILOUT("Can't read line!"); \
302
 
        linenum++; \
303
 
}
304
 
 
305
 
#define STLREADVERT { \
306
 
        STLREADLINE; \
307
 
        if ( !(cp = strstr(buffer, "vertex")) && \
308
 
                 !(cp = strstr(buffer, "VERTEX")) ) STLBAILOUT("Bad vertex!"); \
309
 
        vp = vertdata + 3 * totvert; \
310
 
        if (sscanf(cp + 6, "%f %f %f", vp, vp+1, vp+2) != 3) \
311
 
                STLBAILOUT("Bad vertex!"); \
312
 
        ++totvert; \
313
 
}
314
 
static void read_stl_mesh_ascii(Scene *scene, char *str)
315
 
{
316
 
        FILE   *fpSTL;
317
 
        char   buffer[2048], *cp;
318
 
        Object *ob;
319
 
        Mesh   *me;
320
 
        MVert  *mvert;
321
 
        MFace  *mface;
322
 
        float  *vertdata, *vp;
323
 
        unsigned int numtenthousand, linenum;
324
 
        unsigned int i, vertnum;
325
 
        unsigned int totvert, totface;
326
 
        ReportList *reports= NULL; /* XXX */
327
 
 
328
 
        /* ASCII stl sucks ... we don't really know how many faces there
329
 
           are until the file is done, so lets allocate faces 10000 at a time */
330
 
 
331
 
        fpSTL= fopen(str, "r");
332
 
        if(fpSTL==NULL) {
333
 
                BKE_reportf(reports, RPT_ERROR, "Can't read file: %s.", strerror(errno));
334
 
                return;
335
 
        }
336
 
        
337
 
        /* we'll use the standard malloc/realloc for now ... 
338
 
         * lets allocate enough storage to hold 10000 triangles,
339
 
         * i.e. 30000 verts, i.e., 90000 floats.
340
 
         */
341
 
        numtenthousand = 1;
342
 
        vertdata = malloc(numtenthousand*3*30000*sizeof(float));        // uses realloc!
343
 
        if (!vertdata) { STLALLOCERROR; }
344
 
 
345
 
        linenum = 1;
346
 
        /* Get rid of the first line */
347
 
        STLREADLINE;
348
 
 
349
 
        totvert = 0;
350
 
        totface = 0;
351
 
        while(1) {
352
 
                /* Read in the next line */
353
 
                STLREADLINE;
354
 
 
355
 
                /* lets check if this is the end of the file */
356
 
                if ( strstr(buffer, "endsolid") || strstr(buffer, "ENDSOLID") ) 
357
 
                        break;
358
 
 
359
 
                /* Well, guess that wasn't the end, so lets make
360
 
                 * sure we have enough storage for some more faces
361
 
                 */
362
 
                if ( (totface) && ( (totface % 10000) == 0 ) ) {
363
 
                        ++numtenthousand;
364
 
                        vertdata = realloc(vertdata, 
365
 
                                                           numtenthousand*3*30000*sizeof(float));
366
 
                        if (!vertdata) { STLALLOCERROR; }
367
 
                }
368
 
                
369
 
                /* Don't read normal, but check line for proper syntax anyway
370
 
                 */
371
 
                if ( !(cp = strstr(buffer, "facet")) && 
372
 
                         !(cp = strstr(buffer, "FACET")) ) STLBAILOUT("Bad normal line!");
373
 
                if ( !(strstr(cp+5, "normal")) && 
374
 
                         !(strstr(cp+5, "NORMAL")) )       STLBAILOUT("Bad normal line!");
375
 
 
376
 
                /* Read in what should be the outer loop line 
377
 
                 */
378
 
                STLREADLINE;
379
 
                if ( !(cp = strstr(buffer, "outer")) &&
380
 
                         !(cp = strstr(buffer, "OUTER")) ) STLBAILOUT("Bad outer loop!");
381
 
                if ( !(strstr(cp+5, "loop")) &&
382
 
                         !(strstr(cp+5, "LOOP")) )         STLBAILOUT("Bad outer loop!");
383
 
 
384
 
                /* Read in the face */
385
 
                STLREADVERT;
386
 
                STLREADVERT;
387
 
                STLREADVERT;
388
 
 
389
 
                /* Read in what should be the endloop line 
390
 
                 */
391
 
                STLREADLINE;
392
 
                if ( !strstr(buffer, "endloop") && !strstr(buffer, "ENDLOOP") ) 
393
 
                        STLBAILOUT("Bad endloop!");
394
 
 
395
 
                /* Read in what should be the endfacet line 
396
 
                 */
397
 
                STLREADLINE;
398
 
                if ( !strstr(buffer, "endfacet") && !strstr(buffer, "ENDFACET") ) 
399
 
                        STLBAILOUT("Bad endfacet!");
400
 
 
401
 
                /* Made it this far? Increment face count */
402
 
                ++totface;
403
 
        }
404
 
        fclose(fpSTL);
405
 
 
406
 
        /* OK, lets create our mesh */
407
 
        ob = add_object(scene, OB_MESH);
408
 
        me = ob->data;
409
 
 
410
 
        me->totface = totface;
411
 
        me->totvert = totvert;
412
 
        me->mvert = CustomData_add_layer(&me->vdata, CD_MVERT, CD_CALLOC,
413
 
                                                                         NULL, totvert);
414
 
        me->mface = CustomData_add_layer(&me->fdata, CD_MFACE, CD_CALLOC,
415
 
                                                                         NULL, totface);
416
 
 
417
 
        /* Copy vert coords and create topology */
418
 
        mvert = me->mvert;
419
 
        mface = me->mface;
420
 
        vertnum = 0;
421
 
        for (i=0; i < totface; ++i) {
422
 
                memcpy(mvert->co, vertdata+3*vertnum, 3*sizeof(float) );
423
 
                mface->v1 = vertnum;
424
 
                mvert++;
425
 
                vertnum++;
426
 
 
427
 
                memcpy(mvert->co, vertdata+3*vertnum, 3*sizeof(float) );
428
 
                mface->v2 = vertnum;
429
 
                mvert++;
430
 
                vertnum++;
431
 
 
432
 
                memcpy(mvert->co, vertdata+3*vertnum, 3*sizeof(float) );
433
 
                mface->v3 = vertnum;
434
 
                mvert++;
435
 
                vertnum++;
436
 
 
437
 
                mface++;
438
 
        }
439
 
        free(vertdata);
440
 
 
441
 
        mesh_add_normals_flags(me);
442
 
        make_edges(me, 0);
443
 
 
444
 
        //XXX waitcursor(1);
445
 
}
446
 
 
447
 
#undef STLALLOCERROR
448
 
#undef STLBAILOUT
449
 
#undef STLREADLINE
450
 
#undef STLREADVERT
451
 
 
452
 
/* ***************** INVENTOR ******************* */
453
 
 
454
 
 
455
 
#define IV_MAXSTACK 3000000
456
 
#define IV_MAXFIELD 10
457
 
#define IV_MAXCOL 16
458
 
 
459
 
static float *iv_data_stack;
460
 
static float ivcolors[IV_MAXCOL][3];
461
 
static Object *ivsurf;
462
 
static ListBase ivbase;
463
 
 
464
 
struct IvNode {
465
 
        struct IvNode *next, *prev;
466
 
        char *nodename;
467
 
        char *fieldname[IV_MAXFIELD];
468
 
        int datalen[IV_MAXFIELD];
469
 
        float *data[IV_MAXFIELD];
470
 
};
471
 
 
472
 
static int iv_curcol=0;
473
 
 
474
 
static int iv_colornumber(struct IvNode *iv)
475
 
{
476
 
        float *fp, fr = 0.0, fg = 0.0, fb = 0.0;
477
 
        int a;
478
 
        char *cp;
479
 
        
480
 
        /* search back to last material */
481
 
        while(iv) {
482
 
                if( strcmp(iv->nodename, "Material")==0) {
483
 
                        fp= iv->data[0];
484
 
                        if(fp==0) fp= iv->data[1];
485
 
                        if(fp) {
486
 
                                fr= fp[0];
487
 
                                fg= fp[1];
488
 
                                fb= fp[2];
489
 
                        }
490
 
                        break;
491
 
                }
492
 
                else if( strcmp(iv->nodename, "BaseColor")==0) {
493
 
                        fp= iv->data[0];
494
 
                        fr= fp[0];
495
 
                        fg= fp[1];
496
 
                        fb= fp[2];
497
 
                        break;
498
 
                }
499
 
                else if( strcmp(iv->nodename, "PackedColor")==0) {
500
 
                        cp= (char *)iv->data[0];
501
 
                        fr= cp[3]/255.0f;
502
 
                        fg= cp[2]/255.0f;
503
 
                        fb= cp[1]/255.0f;
504
 
                        break;
505
 
                }
506
 
                iv= iv->prev;
507
 
                
508
 
        }
509
 
        if(iv==0) return 0;
510
 
        if(iv->datalen[0]<3) return 0;
511
 
        
512
 
        for(a=0; a<iv_curcol; a++) {
513
 
        
514
 
                if(ivcolors[a][0]== fr)
515
 
                        if(ivcolors[a][1]== fg)
516
 
                                if(ivcolors[a][2]== fb) return a+1
517
 
                                ;
518
 
        }
519
 
        
520
 
        if(a>=IV_MAXCOL) a= IV_MAXCOL-1;
521
 
        iv_curcol= a+1;
522
 
        ivcolors[a][0]= fr;
523
 
        ivcolors[a][1]= fg;
524
 
        ivcolors[a][2]= fb;
525
 
        
526
 
        return iv_curcol;
527
 
}
528
 
 
529
 
static int iv_finddata(struct IvNode *iv, char *field, int fieldnr)
530
 
{
531
 
        /* search for "field", count data size and make datablock. return skipdata */
532
 
        float *fp;
533
 
        int len, stackcount, skipdata=0;
534
 
        char *cpa, terminator, str[64];
535
 
        intptr_t i;
536
 
        
537
 
        len= strlen(field);
538
 
 
539
 
        cpa= iv->nodename+1;
540
 
        while( *cpa != '}' ) {
541
 
                
542
 
                if( *cpa == *field ) {
543
 
                        if( strncmp(cpa, field, len)==0 ) {
544
 
                                iv->fieldname[fieldnr]= cpa;
545
 
                                
546
 
                                /* read until first character */
547
 
                                cpa+= len;
548
 
                                skipdata+= len;
549
 
                                *cpa= 0;
550
 
                                cpa++;
551
 
                                skipdata++;
552
 
                                
553
 
                                while( *cpa==32 || *cpa==13 || *cpa==10 || *cpa==9) cpa++;
554
 
                                if( *cpa=='[' ) {
555
 
                                        terminator= ']';
556
 
                                        cpa++;
557
 
                                        skipdata++;
558
 
                                }
559
 
                                else terminator= 13;
560
 
                                
561
 
                                stackcount= 0;
562
 
                                fp= iv_data_stack;
563
 
                                
564
 
                                while( *cpa!=terminator && *cpa != '}' ) {
565
 
                                        
566
 
                                        /* in fact, isdigit should include the dot and minus */
567
 
                                        if( (isdigit(*cpa) || *cpa=='.' || *cpa=='-') && (isspace(cpa[-1]) || cpa[-1]==0 || cpa[-1]==',') ) {
568
 
                                                if(cpa[1]=='x') {
569
 
                                                        memcpy(str, cpa, 16);
570
 
                                                        str[16]= 0;
571
 
                                                        
572
 
                                                        sscanf(str, "%x", (int *)fp);
573
 
                                                }
574
 
                                                else {
575
 
                                                        /* atof doesn't stop after the first float
576
 
                                                         * in a long string at Windows... so we copy 
577
 
                                                         * the float to a new string then atof... */
578
 
                                                        char *cpa_temp = strpbrk(cpa, ", \n");
579
 
                                                        i = cpa_temp - cpa;
580
 
                                                        
581
 
                                                        if (i>63) *fp= 0.0;
582
 
                                                        else {
583
 
                                                                memcpy(str, cpa, i);
584
 
                                                                str[i]=0;
585
 
                                                        
586
 
                                                                *fp= (float) atof(str);
587
 
                                                        }
588
 
                                                }
589
 
                                                                                                
590
 
                                                stackcount++;
591
 
                                                if(stackcount>=IV_MAXSTACK) {
592
 
                                                        printf("stackoverflow in IV read\n");
593
 
                                                        break;
594
 
                                                }
595
 
                                                fp++;
596
 
                                        }
597
 
                                        cpa++;
598
 
                                        skipdata++;
599
 
                                }
600
 
                                
601
 
                                iv->datalen[fieldnr]= stackcount;
602
 
                                if(stackcount) {
603
 
                                        iv->data[fieldnr]= MEM_mallocN(sizeof(float)*stackcount, "iv_finddata");
604
 
                                        memcpy(iv->data[fieldnr], iv_data_stack, sizeof(float)*stackcount);
605
 
                                }
606
 
                                else iv->data[fieldnr]= 0;
607
 
                                
608
 
                                return skipdata;
609
 
                        }
610
 
                }
611
 
                cpa++;
612
 
                skipdata++;
613
 
        }
614
 
        
615
 
        return skipdata;
616
 
}
617
 
 
618
 
static void read_iv_index(float *data, float *baseadr, float *index, int nr, int coordtype)
619
 
{
620
 
        /* write in data: baseadr with offset index (and number nr) */
621
 
        float *fp;
622
 
        int ofs;
623
 
        
624
 
        while(nr--) {
625
 
                ofs= (int) *index;
626
 
                fp= baseadr+coordtype*ofs;
627
 
                VECCOPY(data, fp);
628
 
                data+= 3;
629
 
                index++;
630
 
        }
631
 
}
632
 
 
633
 
 
634
 
 
635
 
static void read_inventor(Scene *scene, char *str, struct ListBase *listb)
636
 
{
637
 
        struct IvNode *iv, *ivp, *ivn;
638
 
        char *maindata, *md, *cpa;
639
 
        float *index, *data, *fp;
640
 
        int file, filelen, count, lll, face, nr = 0;
641
 
        int skipdata, ok, a, b, tot, first, colnr, coordtype, polytype, *idata;
642
 
        struct DispList *dl;
643
 
        ReportList *reports= NULL; /* XXX */
644
 
        
645
 
        ivbase.first= ivbase.last= 0;
646
 
        iv_curcol= 0;
647
 
        ivsurf= 0;
648
 
        
649
 
        file= open(str, O_BINARY|O_RDONLY);
650
 
        if(file== -1) {
651
 
                BKE_reportf(reports, RPT_ERROR, "Can't read file: %s.", strerror(errno));
652
 
                return;
653
 
        }
654
 
 
655
 
        filelen= BLI_filesize(file);
656
 
        if(filelen < 1) {
657
 
                close(file);
658
 
                return;
659
 
        }
660
 
        
661
 
        maindata= MEM_mallocN(filelen, "leesInventor");
662
 
        if(read(file, maindata, filelen) < filelen) {
663
 
                BKE_reportf(reports, RPT_ERROR, "Failed reading file: premature end of file.");
664
 
                close(file);
665
 
                return;
666
 
        }
667
 
        close(file);
668
 
 
669
 
        iv_data_stack= MEM_mallocN(sizeof(float)*IV_MAXSTACK, "ivstack");
670
 
 
671
 
        /* preprocess: remove comments */
672
 
        md= maindata+20;
673
 
        count= 20;
674
 
        while(count<filelen) {
675
 
                if( *md=='#' ) {        /* comment */
676
 
                        while( *md!=13 && *md!=10) {    /* enters */
677
 
                                *md= 32;
678
 
                                md++;
679
 
                                count++;
680
 
                                if(count>=filelen) break;
681
 
                        }
682
 
                }
683
 
                md++;
684
 
                count++;        
685
 
        }
686
 
        
687
 
 
688
 
        /* now time to collect: which are the nodes and fields? */
689
 
        md= maindata;
690
 
        count= 0;
691
 
        while(count<filelen) {
692
 
                if( *md=='{' ) {        /* read back */
693
 
                
694
 
                        cpa= md-1;
695
 
                        while( *cpa==32 || *cpa==13 || *cpa==10 || *cpa==9) {   /* remove spaces/enters/tab  */
696
 
                                *cpa= 0;
697
 
                                cpa--;
698
 
                        }               
699
 
                                
700
 
                        while( *cpa>32 && *cpa<128) cpa--;
701
 
                        cpa++;
702
 
                        *md= 0;
703
 
                        
704
 
                        ok= 0;
705
 
                        skipdata= 0;
706
 
                        iv= MEM_callocN(sizeof(struct IvNode), "leesInventor");
707
 
                        iv->nodename= cpa;
708
 
 
709
 
                        if(strcmp(cpa, "Coordinate3")==0 || strcmp(cpa, "Coordinate4")==0) {
710
 
                                skipdata= iv_finddata(iv, "point", 0);
711
 
                                ok= 1;
712
 
                        }
713
 
                        else if(strcmp(cpa, "VertexProperty")==0) {
714
 
                                skipdata= iv_finddata(iv, "vertex", 0);
715
 
                                ok= 1;
716
 
                        }
717
 
                        else if(strcmp(cpa, "IndexedLineSet")==0) {
718
 
                                skipdata= iv_finddata(iv, "coordIndex", 0);
719
 
                                ok= 1;
720
 
                        }
721
 
                        else if(strcmp(cpa, "IndexedTriangleMesh")==0) {
722
 
                                skipdata= iv_finddata(iv, "coordIndex", 0);
723
 
                                ok= 1;
724
 
                        }
725
 
                        else if(strcmp(cpa, "IndexedFaceSet")==0) {
726
 
                                skipdata= iv_finddata(iv, "coordIndex", 0);
727
 
                                ok= 1;
728
 
                        }
729
 
                        else if(strcmp(cpa, "FaceSet")==0) {
730
 
                                skipdata= iv_finddata(iv, "numVertices", 0);
731
 
                                ok= 1;
732
 
                        }
733
 
                        else if(strcmp(cpa, "Material")==0) {
734
 
                                iv_finddata(iv, "diffuseColor", 0);
735
 
                                iv_finddata(iv, "ambientColor", 1);
736
 
                                ok= 1;
737
 
                        }
738
 
                        else if(strcmp(cpa, "BaseColor")==0) {
739
 
                                iv_finddata(iv, "rgb", 0);
740
 
                                ok= 1;
741
 
                        }
742
 
                        else if(strcmp(cpa, "PackedColor")==0) {
743
 
                                iv_finddata(iv, "rgba", 0);
744
 
                                ok= 1;
745
 
                        }
746
 
                        else if(strcmp(cpa, "QuadMesh")==0) {
747
 
                                iv_finddata(iv, "verticesPerColumn", 0);
748
 
                                iv_finddata(iv, "verticesPerRow", 1);
749
 
                                
750
 
                                ok= 1;
751
 
                        }
752
 
                        else if(strcmp(cpa, "IndexedTriangleStripSet")==0) {
753
 
                                skipdata= iv_finddata(iv, "coordIndex", 0);
754
 
                                ok= 1;
755
 
                        }
756
 
                        else if(strcmp(cpa, "TriangleStripSet")==0) {
757
 
                                skipdata= iv_finddata(iv, "numVertices", 0);
758
 
                                ok= 1;
759
 
                        }
760
 
                        else if(strcmp(cpa, "IndexedNurbsSurface")==0 || strcmp(cpa, "NurbsSurface")==0) {
761
 
                                iv_finddata(iv, "numUControlPoints", 0);
762
 
                                iv_finddata(iv, "numVControlPoints", 1);
763
 
                                iv_finddata(iv, "uKnotVector", 2);
764
 
                                iv_finddata(iv, "vKnotVector", 3);
765
 
                                ok= 1;
766
 
                        }
767
 
                        else {
768
 
                                /* to the end */
769
 
                                while( *md != '}') {
770
 
                                        md++;
771
 
                                        count++;
772
 
                                        if(count<filelen) break;
773
 
                                }
774
 
                        }
775
 
                        
776
 
                        
777
 
                        if(ok) {
778
 
                                BLI_addtail(&ivbase, iv);
779
 
                                md+= skipdata;
780
 
                                count+= skipdata;
781
 
                        }
782
 
                        else MEM_freeN(iv);
783
 
                        
784
 
                }
785
 
                md++;
786
 
                count++;
787
 
        }
788
 
        
789
 
        /* join nodes */
790
 
        iv= ivbase.first;
791
 
        
792
 
        while(iv) {
793
 
                ivn= iv->next;
794
 
                
795
 
                if( strncmp(iv->nodename, "Indexed", 7)==0) {
796
 
                        /* seek back: same name? */
797
 
                        
798
 
                        ivp= iv->prev;
799
 
                        while(ivp) {
800
 
                                if(strcmp(iv->nodename, ivp->nodename)==0) break;
801
 
 
802
 
                                if(strcmp(ivp->nodename, "Coordinate3")==0 || 
803
 
                                   strcmp(ivp->nodename, "Coordinate4")==0 ||
804
 
                                   strcmp(ivp->nodename, "VertexProperty")==0) {
805
 
                                        ivp= 0;
806
 
                                        break;
807
 
                                }
808
 
                                ivp= ivp->prev;
809
 
                        }
810
 
                        
811
 
                        if(ivp) {
812
 
                                /* add iv to ivp */
813
 
                                
814
 
                                tot= iv->datalen[0] + ivp->datalen[0];
815
 
                                if(tot) {
816
 
                                        data= MEM_mallocN(tot*sizeof(float), "samenvoeg iv");
817
 
                                        memcpy(data, ivp->data[0], sizeof(float)*ivp->datalen[0]);
818
 
                                        memcpy(data+ivp->datalen[0], iv->data[0], sizeof(float)*iv->datalen[0]);
819
 
                                        
820
 
                                        ivp->datalen[0]+= iv->datalen[0];
821
 
                                        MEM_freeN(ivp->data[0]);
822
 
                                        ivp->data[0]= data;
823
 
                                        
824
 
                                        BLI_remlink(&ivbase, iv);
825
 
                                        MEM_freeN(iv->data[0]);
826
 
                                        MEM_freeN(iv);
827
 
                                }
828
 
                        }
829
 
                }
830
 
                
831
 
                iv= ivn;
832
 
        }
833
 
 
834
 
        
835
 
        /* convert Nodes to DispLists */
836
 
        iv= ivbase.first;
837
 
        while(iv) {
838
 
                
839
 
                /* printf(" Node: %s\n", iv->nodename); */
840
 
                /* if(iv->fieldname[0]) printf(" Field: %s len %d\n", iv->fieldname[0], iv->datalen[0]); */
841
 
                coordtype= 3;
842
 
                
843
 
                if( strcmp(iv->nodename, "IndexedLineSet")==0 ) {
844
 
                        
845
 
                        colnr= iv_colornumber(iv);
846
 
 
847
 
                        /* seek back to data */
848
 
                        ivp= iv;
849
 
                        while(ivp->prev) {
850
 
                                ivp= ivp->prev;
851
 
                                if( strcmp(ivp->nodename, "Coordinate3")==0 ) {
852
 
                                        coordtype= 3;
853
 
                                        break;
854
 
                                }
855
 
                                if( strcmp(ivp->nodename, "Coordinate4")==0 ) {
856
 
                                        coordtype= 4;
857
 
                                        break;
858
 
                                }
859
 
                        }
860
 
                        if(ivp) {
861
 
                        
862
 
                                /* count the nr of lines */
863
 
                                tot= 0;
864
 
                                index= iv->data[0];
865
 
                                                                lll = iv->datalen[0]-1;
866
 
                                for(a=0; a<lll; a++) {
867
 
                                        if(index[0]!= -1 && index[1]!= -1) tot++;
868
 
                                        index++;
869
 
                                }
870
 
                                
871
 
                                tot*= 2;        /* nr of vertices */
872
 
                                dl= MEM_callocN(sizeof(struct DispList)+tot*3*sizeof(float), "leesInventor1");
873
 
                                BLI_addtail(listb, dl);
874
 
                                dl->type= DL_SEGM;
875
 
                                dl->nr= 2;
876
 
                                dl->parts= tot/2;
877
 
                                dl->col= colnr;
878
 
                                data= (float *)(dl+1);
879
 
                                
880
 
                                index= iv->data[0];
881
 
                                for(a=0; a<lll; a++) {
882
 
                                        if(index[0]!= -1 && index[1]!= -1) {
883
 
                                                read_iv_index(data, ivp->data[0], index, 2, coordtype);
884
 
                                                data+= 6;
885
 
                                        }
886
 
                                        index++;
887
 
                                }
888
 
                        }
889
 
                }
890
 
                else if( strcmp(iv->nodename, "FaceSet")==0 ) {
891
 
                        
892
 
                        colnr= iv_colornumber(iv);
893
 
                
894
 
                        /* seek back to data */
895
 
                        ivp= iv;
896
 
                        while(ivp->prev) {
897
 
                                ivp= ivp->prev;
898
 
                                if( strcmp(ivp->nodename, "Coordinate3")==0 ) {
899
 
                                        coordtype= 3;
900
 
                                        break;
901
 
                                }
902
 
                                if( strcmp(ivp->nodename, "Coordinate4")==0 ) {
903
 
                                        coordtype= 4;
904
 
                                        break;
905
 
                                }
906
 
                        }
907
 
                        
908
 
                        if(ivp) {
909
 
                                /* count triangles */
910
 
                                tot= 0;
911
 
                                
912
 
                                index= iv->data[0];
913
 
                                polytype= (int) index[0];
914
 
                                
915
 
                                for(a=0; a<iv->datalen[0]; a++) {
916
 
                                        if(index[0]== polytype) tot++;  /* one kind? */
917
 
                                        index++;
918
 
                                }
919
 
                                
920
 
                                
921
 
                                tot*= polytype;         /* nr of vertices */
922
 
                                dl= MEM_callocN(sizeof(struct DispList)+tot*3*sizeof(float), "leesInventor4");
923
 
                                BLI_addtail(listb, dl);
924
 
                                dl->type= DL_POLY;
925
 
                                dl->nr= polytype;
926
 
                                dl->parts= tot/polytype;
927
 
                                dl->col= colnr;
928
 
                                data= (float *)(dl+1);
929
 
 
930
 
                                index= ivp->data[0];
931
 
                                first= 1;
932
 
                                for(a=0; a<iv->datalen[0]; a++) {
933
 
                                        
934
 
                                        VECCOPY(data, index);
935
 
                                        data+= 3;
936
 
                                        index+= 3;
937
 
 
938
 
                                        VECCOPY(data, index);
939
 
                                        data+= 3;
940
 
                                        index+= 3;
941
 
 
942
 
                                        VECCOPY(data, index);
943
 
                                        data+= 3;
944
 
                                        index+= 3;
945
 
 
946
 
                                        if(polytype==4) {
947
 
                                                VECCOPY(data, index);
948
 
                                                data+= 3;
949
 
                                                index+= 3;
950
 
                                        }
951
 
                                }
952
 
                        }
953
 
                }
954
 
                else if( strcmp(iv->nodename, "TriangleStripSet")==0 ) {
955
 
                        
956
 
                        colnr= iv_colornumber(iv);
957
 
                
958
 
                        /* seek back to data */
959
 
                        ivp= iv;
960
 
                        while(ivp->prev) {
961
 
                                ivp= ivp->prev;
962
 
                                if( strcmp(ivp->nodename, "Coordinate3")==0 ) {
963
 
                                        coordtype= 3;
964
 
                                        break;
965
 
                                }
966
 
                                if( strcmp(ivp->nodename, "Coordinate4")==0 ) {
967
 
                                        coordtype= 4;
968
 
                                        break;
969
 
                                }
970
 
                        }
971
 
                        
972
 
                        if(ivp) {
973
 
                                /* count triangles */
974
 
                                tot= 0;
975
 
                                face= 0;
976
 
                                
977
 
                                index= iv->data[0];             /* strip size */ 
978
 
                                
979
 
                                for(a=0; a<iv->datalen[0]; a++) {
980
 
                                        tot+= (int) index[0];
981
 
                                        face+= ((int) index[0]) - 2;
982
 
                                        index++;
983
 
                                }
984
 
                                
985
 
                                dl= MEM_callocN(sizeof(struct DispList), "leesInventor4");
986
 
                                dl->verts= MEM_callocN( tot*3*sizeof(float), "dl verts");
987
 
                                dl->index= MEM_callocN( face*3*sizeof(int), "dl index");
988
 
                                
989
 
                                dl->type= DL_INDEX3;
990
 
                                dl->nr= tot;
991
 
                                dl->parts= face;
992
 
 
993
 
                                BLI_addtail(listb, dl);
994
 
                                dl->col= colnr;
995
 
 
996
 
                                index= iv->data[0];             /* strip size */ 
997
 
                                fp= ivp->data[0];               /* vertices */
998
 
                                data= dl->verts;
999
 
                                idata= dl->index;
1000
 
                                first= 0;
1001
 
                                
1002
 
                                for(a=0; a<iv->datalen[0]; a++) {
1003
 
                                        
1004
 
                                        /* vertices */
1005
 
                                        for(b=0; b<index[0]; b++) {
1006
 
                                                VECCOPY(data, fp);
1007
 
                                                data+= 3; 
1008
 
                                                fp+= coordtype;
1009
 
                                        }
1010
 
                                                
1011
 
                                        /* indices */
1012
 
                                                                                lll = index[0] - 2;
1013
 
                                        for(b=0; b<lll; b++) {
1014
 
                                                idata[0]= first;
1015
 
                                                idata[1]= first+1;
1016
 
                                                idata[2]= first+2;
1017
 
                                                first++;
1018
 
                                                idata+= 3;
1019
 
                                        }
1020
 
                                        first+= 2;
1021
 
                                        
1022
 
                                        index++;
1023
 
                                }
1024
 
                        }
1025
 
                }
1026
 
                else if( strcmp(iv->nodename, "IndexedFaceSet")==0 ) {
1027
 
                        
1028
 
                        colnr= iv_colornumber(iv);
1029
 
                
1030
 
                        /* seek back to data */
1031
 
                        ivp= iv;
1032
 
                        while(ivp->prev) {
1033
 
                                ivp= ivp->prev;
1034
 
                                if( strcmp(ivp->nodename, "Coordinate3")==0 ) {
1035
 
                                        coordtype= 3;
1036
 
                                        break;
1037
 
                                }
1038
 
                                if( strcmp(ivp->nodename, "Coordinate4")==0 ) {
1039
 
                                        coordtype= 4;
1040
 
                                        break;
1041
 
                                }
1042
 
                        }
1043
 
                        if(ivp) {
1044
 
                        
1045
 
                                /* count triangles */
1046
 
                                face= 0;
1047
 
                                index= iv->data[0];
1048
 
                                lll = iv->datalen[0]-2;
1049
 
                                for(a=0; a<lll; a++) {
1050
 
                                        if(index[0]!= -1 && index[1]!= -1 && index[2]!= -1) face++;
1051
 
                                        index++;
1052
 
                                }
1053
 
 
1054
 
                                /*number of vertices */
1055
 
                                tot= ivp->datalen[0]/coordtype;
1056
 
 
1057
 
                                if(tot) {
1058
 
                                        dl= MEM_callocN(sizeof(struct DispList), "leesInventor5");
1059
 
                                        BLI_addtail(listb, dl);
1060
 
                                        dl->type= DL_INDEX3;
1061
 
                                        dl->nr= tot;
1062
 
                                        dl->parts= face;
1063
 
                                        dl->col= colnr;
1064
 
        
1065
 
                                        dl->verts= MEM_callocN( tot*3*sizeof(float), "dl verts");
1066
 
                                        dl->index= MEM_callocN(sizeof(int)*3*face, "dl index");
1067
 
        
1068
 
                                        /* vertices */
1069
 
                                        fp= ivp->data[0];
1070
 
                                        data= dl->verts;
1071
 
                                        for(b=tot; b>0; b--) {
1072
 
                                                VECCOPY(data, fp);
1073
 
                                                data+= 3; 
1074
 
                                                fp+= coordtype;
1075
 
                                        }
1076
 
                                        
1077
 
                                        /* indices */
1078
 
                                        index= iv->data[0];
1079
 
                                        idata= dl->index;
1080
 
                                        first= 1;
1081
 
                                        lll=iv->datalen[0]-2;
1082
 
                                        for(a=0; a<lll; a++) {
1083
 
                                                
1084
 
                                                if(index[0]!= -1 && index[1]!= -1 && index[2]!= -1) {
1085
 
        
1086
 
                                                        /* this trick is to fill poly's with more than 3 vertices correctly */
1087
 
                                                        if(first) {
1088
 
                                                                nr= (int) index[0];
1089
 
                                                                first= 0;
1090
 
                                                        }
1091
 
                                                        idata[0]= nr;
1092
 
                                                        idata[1]= (int) index[1];
1093
 
                                                        idata[2]= (int) index[2];
1094
 
                                                        idata+= 3;
1095
 
                                                }
1096
 
                                                else first= 1;
1097
 
                                                
1098
 
                                                index++;
1099
 
                                        }
1100
 
                                }
1101
 
                        }
1102
 
                }
1103
 
                else if( strcmp(iv->nodename, "IndexedTriangleMesh")==0 || 
1104
 
                                 strcmp(iv->nodename, "IndexedTriangleStripSet")==0 ) {
1105
 
                        
1106
 
                        colnr= iv_colornumber(iv);
1107
 
                
1108
 
                        /* seek back to data */
1109
 
                        ivp= iv;
1110
 
                        while(ivp->prev) {
1111
 
                                ivp= ivp->prev;
1112
 
                                if( strcmp(ivp->nodename, "Coordinate3")==0 ) {
1113
 
                                        coordtype= 3;
1114
 
                                        break;
1115
 
                                }
1116
 
                                if( strcmp(ivp->nodename, "Coordinate4")==0 ) {
1117
 
                                        coordtype= 4;
1118
 
                                        break;
1119
 
                                }
1120
 
                        }
1121
 
                        if(ivp) {
1122
 
                        
1123
 
                                /* count triangles */
1124
 
                                face= 0;
1125
 
                                index= iv->data[0];
1126
 
                                                                lll=iv->datalen[0]-2;
1127
 
                                for(a=0; a<lll; a++) {
1128
 
                                        if(index[0]!= -1 && index[1]!= -1 && index[2]!= -1) face++;
1129
 
                                        index++;
1130
 
                                }
1131
 
                                
1132
 
                                /* nr of vertices */
1133
 
                                tot= ivp->datalen[0]/coordtype;
1134
 
                                
1135
 
                                dl= MEM_callocN(sizeof(struct DispList), "leesInventor6");
1136
 
                                BLI_addtail(listb, dl);
1137
 
                                dl->type= DL_INDEX3;
1138
 
                                dl->nr= tot;
1139
 
                                dl->parts= face;
1140
 
                                dl->col= colnr;
1141
 
                                
1142
 
                                dl->verts= MEM_callocN( tot*3*sizeof(float), "dl verts");
1143
 
                                dl->index= MEM_callocN(sizeof(int)*3*face, "dl index");
1144
 
 
1145
 
                                /* vertices */
1146
 
                                fp= ivp->data[0];
1147
 
                                data= dl->verts;
1148
 
                                for(b=tot; b>0; b--) {
1149
 
                                        VECCOPY(data, fp);
1150
 
                                        data+= 3; 
1151
 
                                        fp+= coordtype;
1152
 
                                }
1153
 
                                
1154
 
                                /* indices */
1155
 
                                index= iv->data[0];
1156
 
                                idata= dl->index;
1157
 
                                
1158
 
                                                                lll=iv->datalen[0]-2;
1159
 
                                for(a=lll; a>0; a--) {
1160
 
                                
1161
 
                                        if(index[0]!= -1 && index[1]!= -1 && index[2]!= -1) {
1162
 
                                                idata[0]= (int) index[0];
1163
 
                                                idata[1]= (int) index[1];
1164
 
                                                idata[2]= (int) index[2];
1165
 
                                                idata+= 3;
1166
 
                                        }
1167
 
                                        index++;
1168
 
                                }
1169
 
                        }
1170
 
                }
1171
 
                else if( strcmp(iv->nodename, "QuadMesh")==0 ) {
1172
 
                        
1173
 
                        colnr= iv_colornumber(iv);
1174
 
                
1175
 
                        /* seek back to data */
1176
 
                        ivp= iv;
1177
 
                        while(ivp->prev) {
1178
 
                                ivp= ivp->prev;
1179
 
                                if( strcmp(ivp->nodename, "Coordinate3")==0 ) {
1180
 
                                        coordtype= 3;
1181
 
                                        break;
1182
 
                                }
1183
 
                                if( strcmp(ivp->nodename, "VertexProperty")==0 ) {
1184
 
                                        coordtype= 3;
1185
 
                                        break;
1186
 
                                }
1187
 
                                if( strcmp(ivp->nodename, "Coordinate4")==0 ) {
1188
 
                                        coordtype= 4;
1189
 
                                        break;
1190
 
                                }
1191
 
                        }
1192
 
                        
1193
 
                        if(ivp) {
1194
 
                                tot= (int) (floor(*(iv->data[0])+0.5) * floor(*(iv->data[1])+0.5));
1195
 
 
1196
 
                                if(tot>0) {
1197
 
                                        dl= MEM_callocN(sizeof(struct DispList)+tot*3*sizeof(float), "leesInventor8");
1198
 
                                        BLI_addtail(listb, dl);
1199
 
                                        dl->type= DL_SURF;
1200
 
                                        dl->parts= (int) floor(*(iv->data[0])+0.5);
1201
 
                                        dl->nr= (int) floor(*(iv->data[1])+0.5);
1202
 
                                        dl->col= colnr;
1203
 
                                        data= (float *)(dl+1);
1204
 
                                        memcpy(data, ivp->data[0], tot*3*sizeof(float));
1205
 
                                }
1206
 
                        }
1207
 
                }
1208
 
                else if(strcmp(iv->nodename, "IndexedNurbsSurface")==0 || strcmp(iv->nodename, "NurbsSurface")==0) {
1209
 
                        
1210
 
                        colnr= iv_colornumber(iv);
1211
 
                
1212
 
                        /* sek back to data */
1213
 
                        ivp= iv;
1214
 
                        while(ivp->prev) {
1215
 
                                ivp= ivp->prev;
1216
 
                                if( strcmp(ivp->nodename, "Coordinate3")==0 ) {
1217
 
                                        coordtype= 3;
1218
 
                                        break;
1219
 
                                }
1220
 
                                if( strcmp(ivp->nodename, "Coordinate4")==0 ) {
1221
 
                                        coordtype= 4;
1222
 
                                        break;
1223
 
                                }
1224
 
                        }
1225
 
                        if(ivp) {
1226
 
                                a= (int) *(iv->data[0]);
1227
 
                                b= (int) *(iv->data[1]);
1228
 
                                
1229
 
                                tot= a*b;
1230
 
 
1231
 
                                if( (a>=4 || b>=4) && tot>6) {
1232
 
                                        Object *ob;
1233
 
                                        Curve *cu;
1234
 
                                        Nurb *nu;
1235
 
                                        BPoint *bp;
1236
 
                                        
1237
 
                                        if(ivsurf==0) {
1238
 
                                                ob= add_object(scene, OB_SURF);
1239
 
                                                ivsurf= ob;
1240
 
                                        }
1241
 
                                        else ob= ivsurf;
1242
 
                                        cu= ob->data;
1243
 
                                        nu = (Nurb*) MEM_callocN(sizeof(Nurb),"addNurbprim") ;
1244
 
                                        BLI_addtail(&cu->nurb, nu);
1245
 
                                        nu->type= CU_NURBS;
1246
 
 
1247
 
                                        nu->pntsu= a;
1248
 
                                        nu->pntsv= b;
1249
 
                                        nu->resolu= 2*a;
1250
 
                                        nu->resolv= 2*b;
1251
 
 
1252
 
                                        nu->flagu= 0;
1253
 
                                        nu->flagv= 0;
1254
 
                                        
1255
 
                                        nu->bp = bp =
1256
 
                                                (BPoint*)MEM_callocN(tot * sizeof(BPoint), "addNurbprim3");
1257
 
                                        a= tot;
1258
 
                                        data= ivp->data[0];
1259
 
                                        while(a--) {
1260
 
                                                VECCOPY(bp->vec, data);
1261
 
                                                if(coordtype==4) {
1262
 
                                                        bp->vec[3]= data[3];
1263
 
                                                        mul_v3_fl(bp->vec, 1.0f/data[3]);
1264
 
                                                }
1265
 
                                                else bp->vec[3]= 1.0;
1266
 
                                                data+= coordtype;
1267
 
                                                bp++;
1268
 
                                        }
1269
 
                                        
1270
 
                                        /* iv->datalen[2] / [3] is number of knots */
1271
 
                                        nu->orderu= iv->datalen[2] - nu->pntsu;
1272
 
                                        nu->orderv= iv->datalen[3] - nu->pntsv;
1273
 
                                        
1274
 
                                        nu->knotsu= MEM_mallocN( sizeof(float)*(iv->datalen[2]), "knots");
1275
 
                                        memcpy(nu->knotsu, iv->data[2], sizeof(float)*(iv->datalen[2]));
1276
 
                                        nu->knotsv= MEM_mallocN( sizeof(float)*(iv->datalen[3]), "knots");
1277
 
                                        memcpy(nu->knotsv, iv->data[3], sizeof(float)*(iv->datalen[3]));                                        
1278
 
 
1279
 
                                        switchdirectionNurb(nu);
1280
 
 
1281
 
                                }
1282
 
                                else {
1283
 
                                        dl= MEM_callocN(sizeof(struct DispList)+tot*3*sizeof(float), "leesInventor3");
1284
 
                                        BLI_addtail(listb, dl);
1285
 
                                        dl->type= DL_SURF;
1286
 
                                        dl->nr= (int) *(iv->data[0]);
1287
 
                                        dl->parts= (int) *(iv->data[1]);
1288
 
                                        dl->col= colnr;
1289
 
                                        data= (float *)(dl+1);
1290
 
                                        
1291
 
                                        a= tot;
1292
 
                                        fp= ivp->data[0];
1293
 
                                        while(a--) {
1294
 
                                                VECCOPY(data, fp);
1295
 
                                                fp+= coordtype;
1296
 
                                                data+= 3;
1297
 
                                        }
1298
 
                                }
1299
 
                        }
1300
 
                }
1301
 
                iv= iv->next;
1302
 
        }
1303
 
 
1304
 
        /* free */
1305
 
        iv= ivbase.first;
1306
 
        while(iv) {
1307
 
                for(a=0; a<IV_MAXFIELD; a++) {
1308
 
                        if(iv->data[a]) MEM_freeN(iv->data[a]);
1309
 
                }
1310
 
                iv= iv->next;
1311
 
        }
1312
 
 
1313
 
        BLI_freelistN(&ivbase);
1314
 
        MEM_freeN(maindata);
1315
 
        MEM_freeN(iv_data_stack);
1316
 
        
1317
 
}
1318
 
 
1319
 
/* ************************************************************ */
1320
 
 
1321
 
static void displist_to_mesh(Scene *scene, DispList *dlfirst)
1322
 
{
1323
 
        Object *ob;
1324
 
        Mesh *me;
1325
 
        Material *ma;
1326
 
        DispList *dl;
1327
 
        MVert *mvert;
1328
 
        MFace *mface;
1329
 
        float *data, vec[3], min[3], max[3];
1330
 
        int a, b, startve, *idata, totedge=0, tottria=0, totquad=0, totvert=0, totface, totcol=0, colnr;
1331
 
        int p1, p2, p3, p4;
1332
 
        unsigned int maxvertidx;
1333
 
 
1334
 
        /* count first */
1335
 
        INIT_MINMAX(min, max);
1336
 
 
1337
 
        dl= dlfirst;
1338
 
        while(dl) {
1339
 
        
1340
 
                /* PATCH 1 (polyfill) can't be done, there's no listbase here. do that first! */
1341
 
                /* PATCH 2 */
1342
 
                if(dl->type==DL_SEGM && dl->nr>2) {
1343
 
                        data= (float *)(dl+1);
1344
 
                        if(data[0]==data[3*(dl->nr-1)]) {
1345
 
                                if(data[1]==data[3*(dl->nr-1)+1]) {
1346
 
                                        if(data[2]==data[3*(dl->nr-1)+2]) {
1347
 
                                                dl->type= DL_POLY;
1348
 
                                                dl->nr--;
1349
 
                                        }
1350
 
                                }
1351
 
                        }
1352
 
                }
1353
 
                
1354
 
                /* colors */
1355
 
                if(dl->col > totcol) totcol= dl->col;
1356
 
                
1357
 
                /* size and count */
1358
 
                if(dl->type==DL_SURF) {
1359
 
                        a= dl->nr;
1360
 
                        b= dl->parts;
1361
 
                        if(dl->flag & DL_CYCL_U) a++;
1362
 
                        if(dl->flag & DL_CYCL_V) b++;
1363
 
                        
1364
 
                        totquad+= a*b;
1365
 
 
1366
 
                        totvert+= dl->nr*dl->parts;
1367
 
 
1368
 
                        data= (float *)(dl+1);
1369
 
                        for(a= dl->nr*dl->parts; a>0; a--) {
1370
 
                                DO_MINMAX(data, min, max);
1371
 
                                data+= 3;
1372
 
                        }
1373
 
                }
1374
 
                else if(dl->type==DL_POLY) {
1375
 
                        if(dl->nr==3 || dl->nr==4) {
1376
 
                                if(dl->nr==3) tottria+= dl->parts;
1377
 
                                else totquad+= dl->parts;
1378
 
                                
1379
 
                                totvert+= dl->nr*dl->parts;
1380
 
 
1381
 
                                data= (float *)(dl+1);
1382
 
                                for(a= dl->nr*dl->parts; a>0; a--) {
1383
 
                                        DO_MINMAX(data, min, max);
1384
 
                                        data+= 3;
1385
 
                                }
1386
 
                        }
1387
 
                        else if(dl->nr>4) {
1388
 
                                
1389
 
                                tottria+= dl->nr*dl->parts;
1390
 
                                totvert+= dl->nr*dl->parts;
1391
 
                                
1392
 
                                data= (float *)(dl+1);
1393
 
                                for(a= dl->nr*dl->parts; a>0; a--) {
1394
 
                                        DO_MINMAX(data, min, max);
1395
 
                                        data+= 3;
1396
 
                                }
1397
 
                                
1398
 
                        }
1399
 
                }
1400
 
                else if(dl->type==DL_INDEX3) {
1401
 
                        tottria+= dl->parts;
1402
 
                        totvert+= dl->nr;
1403
 
                        
1404
 
                        data= dl->verts;
1405
 
                        for(a= dl->nr; a>0; a--) {
1406
 
                                DO_MINMAX(data, min, max);
1407
 
                                data+= 3;
1408
 
                        }
1409
 
                }
1410
 
                else if(dl->type==DL_SEGM) {
1411
 
                        
1412
 
                        tottria+= (dl->nr-1)*dl->parts;
1413
 
                        totvert+= dl->nr*dl->parts;
1414
 
                        
1415
 
                        data= (float *)(dl+1);
1416
 
                        for(a= dl->nr*dl->parts; a>0; a--) {
1417
 
                                DO_MINMAX(data, min, max);
1418
 
                                data+= 3;
1419
 
                        }
1420
 
                }
1421
 
 
1422
 
                dl= dl->next;
1423
 
        }
1424
 
 
1425
 
        if(totvert==0) {
1426
 
                return;
1427
 
        }
1428
 
        
1429
 
        vec[0]= (min[0]+max[0])/2;
1430
 
        vec[1]= (min[1]+max[1])/2;
1431
 
        vec[2]= (min[2]+max[2])/2;
1432
 
 
1433
 
        ob= add_object(scene, OB_MESH);
1434
 
        VECCOPY(ob->loc, vec);
1435
 
        where_is_object(scene, ob);
1436
 
 
1437
 
        me= ob->data;
1438
 
        
1439
 
        /* colors */
1440
 
        if(totcol) {
1441
 
                ob->mat= MEM_callocN(sizeof(void *)*totcol, "ob->mat");
1442
 
                ob->matbits= MEM_callocN(sizeof(char)*totcol, "ob->matbits");
1443
 
                me->mat= MEM_callocN(sizeof(void *)*totcol, "me->mat");
1444
 
                me->totcol= totcol;
1445
 
                ob->totcol= (unsigned char) me->totcol;
1446
 
                ob->actcol= 1;
1447
 
        }
1448
 
        
1449
 
        /* materials */
1450
 
        for(a=0; a<totcol; a++) {
1451
 
                ma= G.main->mat.first;
1452
 
                while(ma) {
1453
 
                        if(ma->mtex[0]==0) {
1454
 
                                if(ivcolors[a][0]==ma->r && ivcolors[a][1]==ma->g && ivcolors[a][2]==ma->b) {
1455
 
                                        me->mat[a]= ma;
1456
 
                                        ma->id.us++;
1457
 
                                        break;
1458
 
                                }
1459
 
                        }
1460
 
                        ma= ma->id.next;
1461
 
                }
1462
 
                if(ma==0) {
1463
 
                        ma= add_material("ext");
1464
 
                        me->mat[a]= ma;
1465
 
                        ma->r= ivcolors[a][0];
1466
 
                        ma->g= ivcolors[a][1];
1467
 
                        ma->b= ivcolors[a][2];
1468
 
                        automatname(ma);
1469
 
                }
1470
 
        }
1471
 
        
1472
 
        totface= totquad+tottria+totedge;
1473
 
 
1474
 
        printf("Import: %d vertices %d faces\n", totvert, totface);
1475
 
        
1476
 
        me->totvert= totvert;
1477
 
        me->totface= totface;
1478
 
        me->mvert= CustomData_add_layer(&me->vdata, CD_MVERT, CD_CALLOC,
1479
 
                                                                        NULL, me->totvert);
1480
 
        me->mface= CustomData_add_layer(&me->fdata, CD_MFACE, CD_CALLOC,
1481
 
                                                                        NULL, me->totface);
1482
 
        maxvertidx= totvert-1;
1483
 
        
1484
 
        mvert= me->mvert;
1485
 
        mface= me->mface;
1486
 
 
1487
 
        startve= 0;
1488
 
 
1489
 
        dl= dlfirst;
1490
 
        while(dl) {
1491
 
                
1492
 
                colnr= dl->col;
1493
 
                if(colnr) colnr--;
1494
 
                
1495
 
                if(dl->type==DL_SURF) {
1496
 
                        data= (float *)(dl+1);
1497
 
 
1498
 
                        for(a=dl->parts*dl->nr; a>0; a--) {
1499
 
                                mvert->co[0]= data[0] -vec[0];
1500
 
                                mvert->co[1]= data[1] -vec[1];
1501
 
                                mvert->co[2]= data[2] -vec[2];
1502
 
                                
1503
 
                                data+=3;
1504
 
                                mvert++;
1505
 
                        }
1506
 
 
1507
 
                        for(a=0; a<dl->parts; a++) {
1508
 
                                
1509
 
                                if (surfindex_displist(dl, a, &b, &p1, &p2, &p3, &p4)==0)
1510
 
                                        break;
1511
 
                                
1512
 
                                p1+= startve; 
1513
 
                                p2+= startve; 
1514
 
                                p3+= startve; 
1515
 
                                p4+= startve;
1516
 
 
1517
 
                                for(; b<dl->nr; b++) {
1518
 
                                
1519
 
                                        mface->v1= p1;
1520
 
                                        mface->v2= p2;
1521
 
                                        mface->v3= p4;
1522
 
                                        mface->v4= p3;
1523
 
                                        
1524
 
                                        mface->mat_nr= colnr;
1525
 
                                        test_index_face(mface, NULL, 0, 4);
1526
 
                                        
1527
 
                                        mface++;
1528
 
                                        
1529
 
                                        p4= p3; 
1530
 
                                        p3++;
1531
 
                                        p2= p1; 
1532
 
                                        p1++;
1533
 
                                }
1534
 
                        }
1535
 
                        
1536
 
                        startve += dl->parts*dl->nr;
1537
 
 
1538
 
                }
1539
 
                else if(dl->type==DL_POLY) {
1540
 
                
1541
 
                        if(dl->nr==3 || dl->nr==4) {
1542
 
                                data= (float *)(dl+1);
1543
 
 
1544
 
                                for(a=dl->parts*dl->nr; a>0; a--) {
1545
 
                                        mvert->co[0]= data[0] -vec[0];
1546
 
                                        mvert->co[1]= data[1] -vec[1];
1547
 
                                        mvert->co[2]= data[2] -vec[2];
1548
 
                                        data+=3;
1549
 
                                        mvert++;
1550
 
                                }
1551
 
 
1552
 
                                for(a=0; a<dl->parts; a++) {
1553
 
                                        if(dl->nr==3) {
1554
 
                                                mface->v1= startve+a*dl->nr;
1555
 
                                                mface->v2= startve+a*dl->nr+1;
1556
 
                                                mface->v3= startve+a*dl->nr+2;
1557
 
                                                mface->mat_nr= colnr;
1558
 
                                                test_index_face(mface, NULL, 0, 3);
1559
 
                                                mface++;
1560
 
                                        }
1561
 
                                        else {
1562
 
                                                mface->v1= startve+a*dl->nr;
1563
 
                                                mface->v2= startve+a*dl->nr+1;
1564
 
                                                mface->v3= startve+a*dl->nr+2;
1565
 
                                                mface->v4= startve+a*dl->nr+3;
1566
 
                                                mface->mat_nr= colnr;
1567
 
                                                test_index_face(mface, NULL, 0, 4);
1568
 
                                                mface++;
1569
 
                                        }
1570
 
                                }
1571
 
                                startve += dl->parts*dl->nr;
1572
 
                        }
1573
 
                        else if(dl->nr>4) {
1574
 
                                data= (float *)(dl+1);
1575
 
 
1576
 
                                for(a=dl->parts*dl->nr; a>0; a--) {
1577
 
                                        mvert->co[0]= data[0] -vec[0];
1578
 
                                        mvert->co[1]= data[1] -vec[1];
1579
 
                                        mvert->co[2]= data[2] -vec[2];
1580
 
                                        
1581
 
                                        data+=3;
1582
 
                                        mvert++;
1583
 
                                }
1584
 
 
1585
 
                                for(b=0; b<dl->parts; b++) {
1586
 
                                        for(a=0; a<dl->nr; a++) {
1587
 
                                                mface->v1= startve+a;
1588
 
                                                
1589
 
                                                if(a==dl->nr-1) mface->v2= startve;
1590
 
                                                else mface->v2= startve+a+1;
1591
 
                                                
1592
 
                                                mface->mat_nr= colnr;
1593
 
 
1594
 
                                                mface++;
1595
 
                                        }
1596
 
                                        startve += dl->nr;
1597
 
                                }
1598
 
                        }
1599
 
                }
1600
 
                else if(dl->type==DL_INDEX3) {
1601
 
                        data= dl->verts;
1602
 
                        
1603
 
                        for(a=dl->nr; a>0; a--) {
1604
 
                                mvert->co[0]= data[0] -vec[0];
1605
 
                                mvert->co[1]= data[1] -vec[1];
1606
 
                                mvert->co[2]= data[2] -vec[2];
1607
 
                                data+=3;
1608
 
                                mvert++;
1609
 
                        }
1610
 
 
1611
 
                        idata= dl->index;
1612
 
                        for(b=dl->parts; b>0; b--) {
1613
 
                                mface->v1= startve+idata[0];
1614
 
                                mface->v2= startve+idata[1];
1615
 
                                mface->v3= startve+idata[2];
1616
 
                                mface->mat_nr= colnr;
1617
 
                                
1618
 
                                if (mface->v1>maxvertidx) mface->v1= maxvertidx;
1619
 
                                if (mface->v2>maxvertidx) mface->v2= maxvertidx;
1620
 
                                if (mface->v3>maxvertidx) mface->v3= maxvertidx;
1621
 
 
1622
 
                                test_index_face(mface, NULL, 0, 3);
1623
 
                                mface++;
1624
 
                                idata+= 3;
1625
 
                        }
1626
 
                        startve += dl->nr;
1627
 
                }
1628
 
                else if(dl->type==DL_SEGM) {
1629
 
                        data= (float *)(dl+1);
1630
 
 
1631
 
                        for(a=dl->parts*dl->nr; a>0; a--) {
1632
 
                                mvert->co[0]= data[0] -vec[0];
1633
 
                                mvert->co[1]= data[1] -vec[1];
1634
 
                                mvert->co[2]= data[2] -vec[2];
1635
 
                                data+=3;
1636
 
                                mvert++;
1637
 
                        }
1638
 
 
1639
 
                        for(b=0; b<dl->parts; b++) {
1640
 
                                for(a=0; a<dl->nr-1; a++) {
1641
 
                                        mface->v1= startve+a;
1642
 
                                        mface->v2= startve+a+1;
1643
 
                                        mface->mat_nr= colnr;
1644
 
                                        mface++;
1645
 
                                }
1646
 
                                startve += dl->nr;
1647
 
                        }
1648
 
                }
1649
 
                dl= dl->next;
1650
 
        }
1651
 
 
1652
 
        mesh_add_normals_flags(me);
1653
 
        make_edges(me, 0);
1654
 
}
1655
 
 
1656
 
static void displist_to_objects(Scene *scene, ListBase *lbase)
1657
 
{
1658
 
        DispList *dl, *first, *prev, *next;
1659
 
        ListBase tempbase;
1660
 
        int maxaantal, curcol, totvert=0, vert;
1661
 
        
1662
 
        /* irst this: is still active */
1663
 
        if(ivsurf) {
1664
 
                where_is_object(scene, ivsurf);
1665
 
// XXX          docenter_new();
1666
 
        }
1667
 
 
1668
 
        dl= lbase->first;
1669
 
        while(dl) {
1670
 
                next= dl->next;
1671
 
                
1672
 
                /* PATCH 1: polyfill */
1673
 
                if(dl->type==DL_POLY && dl->nr>4) {
1674
 
                        /* solution: put them together in separate listbase */
1675
 
                        ;
1676
 
                }
1677
 
                /* PATCH 2: poly's of 2 points */
1678
 
                if(dl->type==DL_POLY && dl->nr==2) dl->type= DL_SEGM;
1679
 
                
1680
 
                dl= next;
1681
 
        }
1682
 
 
1683
 
        /* count vertices */
1684
 
 
1685
 
        dl= lbase->first;
1686
 
        while(dl) {
1687
 
 
1688
 
                if(dl->type==DL_SURF) totvert+= dl->nr*dl->parts;
1689
 
                else if(dl->type==DL_POLY) {
1690
 
                        if(dl->nr==3 || dl->nr==4) totvert+= dl->nr*dl->parts;
1691
 
                        else if(dl->nr>4) totvert+= dl->nr*dl->parts;
1692
 
                }
1693
 
                else if(dl->type==DL_INDEX3) totvert+= dl->nr;
1694
 
                else if(dl->type==DL_SEGM) totvert+= dl->nr*dl->parts;
1695
 
 
1696
 
                dl= dl->next;
1697
 
        }
1698
 
 
1699
 
        if(totvert==0) {
1700
 
                
1701
 
                if(ivsurf==0) {}; //XXX error("Found no data");
1702
 
                if(lbase->first) BLI_freelistN(lbase);
1703
 
                
1704
 
                return;
1705
 
        }
1706
 
 
1707
 
        maxaantal= 32000;
1708
 
        
1709
 
        if(totvert>maxaantal) {
1710
 
        
1711
 
                /* try to put colors together */
1712
 
                curcol= 0;
1713
 
                tempbase.first= tempbase.last= 0;
1714
 
 
1715
 
                while(lbase->first) {
1716
 
                        dl= lbase->first;
1717
 
                        while(dl) {
1718
 
                                next= dl->next;
1719
 
                                if(dl->col==curcol) {
1720
 
                                        BLI_remlink(lbase, dl);
1721
 
                                        BLI_addtail(&tempbase, dl);
1722
 
                                        dl->col= 0;
1723
 
                                }
1724
 
                                
1725
 
                                dl= next;
1726
 
                        }
1727
 
                        
1728
 
                        /* in tempbase are all 'curcol' */
1729
 
                        totvert= 0;
1730
 
                        dl= first= tempbase.first;
1731
 
                        while(dl) {
1732
 
                                vert= 0;
1733
 
                                
1734
 
                                if(dl->type==DL_SURF) vert= dl->nr*dl->parts;
1735
 
                                else if(dl->type==DL_POLY) {
1736
 
                                        if(dl->nr==3 || dl->nr==4) vert= dl->nr*dl->parts;
1737
 
                                        else if(dl->nr>4) vert= dl->nr*dl->parts;
1738
 
                                }
1739
 
                                else if(dl->type==DL_INDEX3) totvert+= dl->nr;
1740
 
                                else if(dl->type==DL_SEGM) vert= dl->nr*dl->parts;
1741
 
                                
1742
 
                                totvert+= vert;
1743
 
                                if(totvert > maxaantal || dl->next==0) {
1744
 
                                        if(dl->next==0) {
1745
 
                                                displist_to_mesh(scene, first);
1746
 
                                        }
1747
 
                                        else if(dl->prev) {
1748
 
                                                prev= dl->prev;
1749
 
                                                prev->next= 0;
1750
 
                                                displist_to_mesh(scene, first);
1751
 
                                                prev->next= dl;
1752
 
                                                first= dl;
1753
 
                                                totvert= 0;
1754
 
                                        }
1755
 
                                }
1756
 
                                
1757
 
                                dl= dl->next;
1758
 
                        }
1759
 
                        
1760
 
                        freedisplist(&tempbase);
1761
 
                        
1762
 
                        curcol++;
1763
 
                }
1764
 
        }
1765
 
        else displist_to_mesh(scene, lbase->first);
1766
 
 
1767
 
        freedisplist(lbase);
1768
 
 
1769
 
}
1770
 
 
1771
 
int BKE_read_exotic(Scene *scene, char *name)
1772
 
{
1773
 
        ListBase lbase={0, 0};
1774
 
        int len;
1775
 
        gzFile gzfile;
1776
 
        char str[32];
1777
 
        int *s0 = (int*) str;
1778
 
        int retval = 0;
1779
 
 
1780
 
        // make sure we're not trying to read a directory....
1781
 
 
1782
 
        len= strlen(name);
1783
 
        if (name[len-1] !='/' && name[len-1] != '\\') {
1784
 
                gzfile = gzopen(name,"rb");
1785
 
 
1786
 
                if (NULL == gzfile ) {
1787
 
                        //XXX error("Can't open file: %s", name);
1788
 
                        retval= -1;
1789
 
                } else {
1790
 
                        gzread(gzfile, str, 31);
1791
 
                        gzclose(gzfile);
1792
 
 
1793
 
                        if ((*s0 != FORM) && (strncmp(str, "BLEN", 4) != 0) && !BLI_testextensie(name,".blend.gz")) {
1794
 
 
1795
 
                                //XXX waitcursor(1);
1796
 
                                if(strncmp(str, "#Inventor V1.0", 14)==0) {
1797
 
                                        if( strncmp(str+15, "ascii", 5)==0) {
1798
 
                                                read_inventor(scene, name, &lbase);
1799
 
                                                displist_to_objects(scene, &lbase);                             
1800
 
                                                retval = 1;
1801
 
                                        } else {
1802
 
                                                //XXX error("Can only read Inventor 1.0 ascii");
1803
 
                                        }
1804
 
                                }
1805
 
                                else if((strncmp(str, "#VRML V1.0 asc", 14)==0)) {
1806
 
                                        read_inventor(scene, name, &lbase);
1807
 
                                        displist_to_objects(scene, &lbase);                             
1808
 
                                        retval = 1;
1809
 
                                }
1810
 
                                else if(is_dxf(name)) {
1811
 
                                        dxf_read(scene, name);
1812
 
                                        retval = 1;
1813
 
                                }
1814
 
                                else if(is_stl(name)) {
1815
 
                                        if (is_stl_ascii(name))
1816
 
                                                read_stl_mesh_ascii(scene, name);
1817
 
                                        else
1818
 
                                                read_stl_mesh_binary(scene, name);
1819
 
                                        retval = 1;
1820
 
                                }
1821
 
#ifndef DISABLE_PYTHON
1822
 
                                // TODO: this should not be in the kernel...
1823
 
                                else { // unknown format, call Python importloader 
1824
 
                                        if (BPY_call_importloader(name)) {
1825
 
                                                retval = 1;
1826
 
                                        } else {        
1827
 
                                                //XXX error("Unknown file type or error, check console");
1828
 
                                        }       
1829
 
                                
1830
 
                                }
1831
 
#endif /* DISABLE_PYTHON */
1832
 
                                //XXX waitcursor(0);
1833
 
                        }
1834
 
                }
1835
 
        }
1836
 
        
1837
 
        return (retval);
1838
 
}
1839
 
 
1840
 
 
1841
 
/* ************************ WRITE ************************** */
1842
 
 
1843
 
 
1844
 
char temp_dir[160]= {0, 0};
1845
 
 
1846
 
static void write_vert_stl(Object *ob, MVert *verts, int index, FILE *fpSTL)
1847
 
{
1848
 
        float vert[3];
1849
 
 
1850
 
        VECCOPY(vert, verts[(index)].co);
1851
 
        mul_m4_v3(ob->obmat, vert);
1852
 
 
1853
 
        if (ENDIAN_ORDER==B_ENDIAN) {
1854
 
                SWITCH_INT(vert[0]);
1855
 
                SWITCH_INT(vert[1]);
1856
 
                SWITCH_INT(vert[2]);
1857
 
        }
1858
 
 
1859
 
        fwrite(vert, sizeof(float), 3, fpSTL);
1860
 
}
1861
 
 
1862
 
static int write_derivedmesh_stl(FILE *fpSTL, Object *ob, DerivedMesh *dm)
1863
 
{
1864
 
        MVert *mvert = dm->getVertArray(dm);
1865
 
        MFace *mface = dm->getFaceArray(dm);
1866
 
        int i, numfacets = 0, totface = dm->getNumFaces(dm);
1867
 
        float zero[3] = {0.0f, 0.0f, 0.0f};
1868
 
 
1869
 
        for (i=0; i<totface; i++, mface++) {
1870
 
                fwrite(zero, sizeof(float), 3, fpSTL);
1871
 
                write_vert_stl(ob, mvert, mface->v1, fpSTL);
1872
 
                write_vert_stl(ob, mvert, mface->v2, fpSTL);
1873
 
                write_vert_stl(ob, mvert, mface->v3, fpSTL);
1874
 
                fprintf(fpSTL, "  ");
1875
 
                numfacets++;
1876
 
 
1877
 
                if(mface->v4) { /* quad = 2 tri's */
1878
 
                        fwrite(zero, sizeof(float), 3, fpSTL);
1879
 
                        write_vert_stl(ob, mvert, mface->v1, fpSTL);
1880
 
                        write_vert_stl(ob, mvert, mface->v3, fpSTL);
1881
 
                        write_vert_stl(ob, mvert, mface->v4, fpSTL);
1882
 
                        fprintf(fpSTL, "  ");
1883
 
                        numfacets++;
1884
 
                }
1885
 
        }
1886
 
 
1887
 
        return numfacets;
1888
 
}
1889
 
 
1890
 
static int write_object_stl(FILE *fpSTL, Scene *scene, Object *ob, Mesh *me)
1891
 
{
1892
 
        int  numfacets = 0;
1893
 
        DerivedMesh *dm = mesh_get_derived_final(scene, ob, CD_MASK_BAREMESH);
1894
 
 
1895
 
        numfacets += write_derivedmesh_stl(fpSTL, ob, dm);
1896
 
 
1897
 
        dm->release(dm);
1898
 
 
1899
 
        return numfacets;
1900
 
}
1901
 
 
1902
 
void write_stl(Scene *scene, char *str)
1903
 
{
1904
 
        Object *ob;
1905
 
        Mesh   *me;
1906
 
        Base   *base;
1907
 
        FILE   *fpSTL;
1908
 
        int    numfacets = 0;
1909
 
        ReportList *reports= NULL; /* XXX */
1910
 
        
1911
 
        if(BLI_testextensie(str,".blend")) str[ strlen(str)-6]= 0;
1912
 
        if(BLI_testextensie(str,".ble")) str[ strlen(str)-4]= 0;
1913
 
        if(BLI_testextensie(str,".stl")==0) strcat(str, ".stl");
1914
 
 
1915
 
        if (BLI_exists(str)) {
1916
 
                ; //XXX if(saveover(str)==0)
1917
 
                //XXX   return;
1918
 
        }
1919
 
 
1920
 
        fpSTL= fopen(str, "wb");
1921
 
        
1922
 
        if(fpSTL==NULL) {
1923
 
                BKE_reportf(reports, RPT_ERROR, "Can't open file: %s.", strerror(errno));
1924
 
                return;
1925
 
        }
1926
 
        strcpy(temp_dir, str);
1927
 
        
1928
 
        //XXX waitcursor(1);
1929
 
        
1930
 
        /* The header part of the STL */
1931
 
        /* First 80 characters are a title or whatever you want.
1932
 
           Lets make the first 32 of those spam and the rest the filename.
1933
 
           Those first 80 characters will be followed by 4 bytes
1934
 
           which will be overwritten later with an integer holding
1935
 
           how many facets are written (we set them to ' ' for now).
1936
 
        */
1937
 
        fprintf(fpSTL, "Binary STL output from Blender: %-48.48s    ", str);
1938
 
 
1939
 
        /* Write all selected mesh objects */
1940
 
        base= scene->base.first;
1941
 
        while(base) {
1942
 
                if (base->flag & SELECT) {
1943
 
                        ob = base->object;
1944
 
                        if (ob->type == OB_MESH) {
1945
 
                                me = ob->data;
1946
 
                                if (me)
1947
 
                                        numfacets += write_object_stl(fpSTL, scene, ob, me);
1948
 
                        }
1949
 
                }
1950
 
                base= base->next;
1951
 
        }
1952
 
 
1953
 
        /* time to write the number of facets in the 4 bytes
1954
 
           starting at byte 81
1955
 
        */
1956
 
        fseek(fpSTL, 80, SEEK_SET);
1957
 
 
1958
 
        if (ENDIAN_ORDER==B_ENDIAN) {
1959
 
                                SWITCH_INT(numfacets);
1960
 
                }
1961
 
        fwrite(&numfacets, 4*sizeof(char), 1, fpSTL);
1962
 
 
1963
 
        fclose(fpSTL);
1964
 
        
1965
 
        //XXX waitcursor(0);
1966
 
}
1967
 
 
1968
 
/* ******************************* WRITE VRML ***************************** */
1969
 
 
1970
 
static void replace_chars(char *str1, char *str2)
1971
 
{
1972
 
        int a= strlen(str2);
1973
 
        
1974
 
        str1[a]= 0;
1975
 
        while(a--) {
1976
 
                if(str2[a]=='.' || str2[a]==' ') str1[a]= '_';
1977
 
                else str1[a]= str2[a];
1978
 
        }
1979
 
}
1980
 
 
1981
 
 
1982
 
static void write_material_vrml(FILE *fp, Material *ma)
1983
 
{
1984
 
        char str[32];
1985
 
        
1986
 
        replace_chars(str, ma->id.name+2);
1987
 
        
1988
 
        fprintf(fp, "\tDEF %s\n", str);
1989
 
        fprintf(fp, "\tMaterial {\n");
1990
 
        
1991
 
        fprintf(fp, "\t\tdiffuseColor %f %f %f\n", ma->r, ma->g, ma->b);
1992
 
        fprintf(fp, "\t\tspecularColor %f %f %f\n", ma->specr, ma->specg, ma->specb);
1993
 
        fprintf(fp, "\t\tshininess %f \n", ((float)ma->har)/100.0);
1994
 
        fprintf(fp, "\t\ttransparency %f \n", 1.0-ma->alpha);
1995
 
        
1996
 
        fprintf(fp, "\t}\n");
1997
 
        
1998
 
}
1999
 
 
2000
 
unsigned int *mcol_to_vcol(Mesh *me)
2001
 
{
2002
 
        MFace *mface;
2003
 
        unsigned int *mcol, *mcoln, *mcolmain;
2004
 
        int a;
2005
 
 
2006
 
        if(me->totface==0 || me->mcol==0) return 0;
2007
 
        
2008
 
        mcoln= mcolmain= MEM_mallocN(sizeof(int)*me->totvert, "mcoln");
2009
 
        mcol = (unsigned int *)me->mcol;
2010
 
        mface= me->mface;
2011
 
        
2012
 
        for(a=me->totface; a>0; a--, mface++) {
2013
 
                mcoln[mface->v1]= mcol[0];
2014
 
                mcoln[mface->v2]= mcol[1];
2015
 
                mcoln[mface->v3]= mcol[2];
2016
 
                if(mface->v4) mcoln[mface->v4]= mcol[3];
2017
 
 
2018
 
                mcol+= 4;
2019
 
        }
2020
 
        
2021
 
        return mcolmain;
2022
 
}
2023
 
 
2024
 
void mcol_to_rgba(unsigned int col, float *r, float *g, float *b, float *a)
2025
 
{
2026
 
        char *cp;
2027
 
        
2028
 
        cp = (char *)&col;
2029
 
        
2030
 
        *r= cp[3];
2031
 
        *r /= 255.0;
2032
 
 
2033
 
        *g= cp[2];
2034
 
        *g /= 255.0;
2035
 
 
2036
 
        *b= cp[1];
2037
 
        *b /= 255.0;
2038
 
 
2039
 
        *a= cp[0];
2040
 
        *a /= 255.0;
2041
 
}
2042
 
 
2043
 
static void write_mesh_vrml(FILE *fp, Mesh *me)
2044
 
{
2045
 
        Material *ma;
2046
 
        MVert *mvert;
2047
 
        MFace *mface;
2048
 
        MTFace *tface;
2049
 
        Image *ima;
2050
 
        int a, b, totcol, texind;
2051
 
        char str[32];
2052
 
        
2053
 
        replace_chars(str, me->id.name+2);
2054
 
 
2055
 
        fprintf(fp, "\tDEF %s\n", str);
2056
 
        fprintf(fp, "\tSeparator {\n");
2057
 
        
2058
 
        if(me->mtface) {
2059
 
                ima= ((MTFace *)me->mtface)->tpage;
2060
 
                if(ima) {
2061
 
                        fprintf(fp, "\t\tTexture2 {\n");
2062
 
                        fprintf(fp, "\t\t\tfilename %s\n", ima->name);
2063
 
                        fprintf(fp, "\t\t\twrapS REPEAT \n");
2064
 
                        fprintf(fp, "\t\t\twrapT REPEAT \n");
2065
 
                        fprintf(fp, "\t\t}\n");
2066
 
                }
2067
 
        }
2068
 
        
2069
 
        if(me->mcol) {
2070
 
                unsigned int *mcol, *mcolmain;
2071
 
                float r, g, b, cola;
2072
 
                
2073
 
                fprintf(fp, "\t\tMaterial {\n");
2074
 
                fprintf(fp, "\t\t\tdiffuseColor [\n");
2075
 
                
2076
 
                a= me->totvert;
2077
 
                mcol= mcolmain= mcol_to_vcol(me);
2078
 
                if(mcol) {
2079
 
                        while(a--) {
2080
 
                                mcol_to_rgba(*mcol, &r, &g, &b, &cola);
2081
 
                                fprintf(fp, "\t\t\t\t %f %f %f,\n", r, g, b);
2082
 
                                mcol++;
2083
 
                        }
2084
 
                        MEM_freeN(mcolmain);
2085
 
                }
2086
 
                fprintf(fp, "\t\t\t]\n");
2087
 
                fprintf(fp, "\t\t}\n");
2088
 
 
2089
 
                fprintf(fp, "\t\tMaterialBinding { value PER_VERTEX_INDEXED }\n");
2090
 
        }
2091
 
 
2092
 
 
2093
 
        fprintf(fp, "\t\tCoordinate3 {\n");
2094
 
        fprintf(fp, "\t\t\tpoint [\n");
2095
 
        
2096
 
        a= me->totvert;
2097
 
        mvert= me->mvert;
2098
 
        while(a--) {
2099
 
                fprintf(fp, "\t\t\t\t %f %f %f,\n", mvert->co[0], mvert->co[1], mvert->co[2]);
2100
 
                mvert++;
2101
 
        }
2102
 
        fprintf(fp, "\t\t\t]\n");
2103
 
        fprintf(fp, "\t\t}\n");
2104
 
        
2105
 
        
2106
 
        totcol= me->totcol;
2107
 
        if(totcol==0) totcol= 1;
2108
 
        texind= 0; // index for uv coords
2109
 
        
2110
 
        for(b=0; b<totcol; b++) {
2111
 
                
2112
 
                if(me->mcol==0) {
2113
 
                        if(me->mat) {
2114
 
                                ma= me->mat[b];
2115
 
                                if(ma) {
2116
 
                                        replace_chars(str, ma->id.name+2);
2117
 
 
2118
 
                                        fprintf(fp, "\t\tUSE %s\n\n", str);
2119
 
                                }
2120
 
                        }
2121
 
                }
2122
 
                
2123
 
                if(me->mtface) {
2124
 
                        fprintf(fp, "\t\tTextureCoordinate2 {\n");
2125
 
                        fprintf(fp, "\t\t\tpoint [\n");
2126
 
        
2127
 
                        a= me->totface;
2128
 
                        mface= me->mface;
2129
 
                        tface= me->mtface;
2130
 
                        while(a--) {
2131
 
                                if(mface->mat_nr==b) {
2132
 
                                        fprintf(fp, "\t\t\t\t %f %f,\n", tface->uv[0][0], tface->uv[0][1]); 
2133
 
                                        fprintf(fp, "\t\t\t\t %f %f,\n", tface->uv[1][0], tface->uv[1][1]); 
2134
 
                                        fprintf(fp, "\t\t\t\t %f %f,\n", tface->uv[2][0], tface->uv[2][1]); 
2135
 
                                        if(mface->v4) fprintf(fp, "\t\t\t\t %f %f,\n", tface->uv[3][0], tface->uv[3][1]); 
2136
 
                                }
2137
 
                                mface++;
2138
 
                                tface++;
2139
 
                        }
2140
 
                        fprintf(fp, "\t\t\t]\n");
2141
 
                        fprintf(fp, "\t\t}\n");
2142
 
                }
2143
 
 
2144
 
                fprintf(fp, "\t\tIndexedFaceSet {\n");
2145
 
                fprintf(fp, "\t\t\tcoordIndex [\n");
2146
 
 
2147
 
                a= me->totface;
2148
 
                mface= me->mface;
2149
 
                while(a--) {
2150
 
                        if(mface->mat_nr==b) {
2151
 
                                if(mface->v4) fprintf(fp, "\t\t\t\t %d, %d, %d, %d, -1,\n", mface->v1, mface->v2, mface->v3, mface->v4); 
2152
 
                                else fprintf(fp, "\t\t\t\t %d, %d, %d, -1,\n", mface->v1, mface->v2, mface->v3); 
2153
 
                        }
2154
 
                        mface++;
2155
 
                }
2156
 
                fprintf(fp, "\t\t\t]\n");
2157
 
 
2158
 
                if(me->mtface) {
2159
 
                        fprintf(fp, "\t\t\ttextureCoordIndex [\n");
2160
 
        
2161
 
                        a= me->totface;
2162
 
                        mface= me->mface;
2163
 
                        while(a--) {
2164
 
                                if(mface->mat_nr==b) {
2165
 
                                        if(mface->v4) {
2166
 
                                                fprintf(fp, "\t\t\t\t %d, %d, %d, %d, -1,\n", texind, texind+1, texind+2, texind+3); 
2167
 
                                                texind+= 4;
2168
 
                                        }
2169
 
                                        else {
2170
 
                                                fprintf(fp, "\t\t\t\t %d, %d, %d, -1,\n", texind, texind+1, texind+2); 
2171
 
                                                texind+= 3;
2172
 
                                        }
2173
 
                                }
2174
 
                                mface++;
2175
 
                        }
2176
 
                        fprintf(fp, "\t\t\t]\n");
2177
 
                }
2178
 
                fprintf(fp, "\t\t}\n");
2179
 
        }
2180
 
        
2181
 
        fprintf(fp, "\t}\n");
2182
 
}
2183
 
 
2184
 
static void write_camera_vrml(FILE *fp, Object *ob)
2185
 
{
2186
 
        Camera *cam;
2187
 
        
2188
 
        if(ob==0) return;
2189
 
        invert_m4_m4(ob->imat, ob->obmat);
2190
 
 
2191
 
        fprintf(fp, "\tMatrixTransform {\n");
2192
 
 
2193
 
        fprintf(fp, "\tmatrix \n");
2194
 
 
2195
 
        fprintf(fp, "\t\t%f %f %f %f\n", ob->imat[0][0], ob->imat[0][1], ob->imat[0][2], ob->imat[0][3]);
2196
 
        fprintf(fp, "\t\t%f %f %f %f\n", ob->imat[1][0], ob->imat[1][1], ob->imat[1][2], ob->imat[1][3]);
2197
 
        fprintf(fp, "\t\t%f %f %f %f\n", ob->imat[2][0], ob->imat[2][1], ob->imat[2][2], ob->imat[2][3]);
2198
 
        fprintf(fp, "\t\t%f %f %f %f\n", ob->imat[3][0], ob->imat[3][1], ob->imat[3][2], ob->imat[3][3]);
2199
 
 
2200
 
        fprintf(fp, "\t}\n");
2201
 
 
2202
 
        cam= ob->data;
2203
 
 
2204
 
        fprintf(fp, "\tPerspectiveCamera {\n");
2205
 
        fprintf(fp, "\t\tfocalDistance %f\n", cam->lens/10.0);
2206
 
        
2207
 
        fprintf(fp, "\t}\n");
2208
 
 
2209
 
}
2210
 
 
2211
 
static void write_object_vrml(FILE *fp, Object *ob)
2212
 
{
2213
 
        ID *id;
2214
 
        char str[32];
2215
 
        
2216
 
        fprintf(fp, "\tSeparator {\n");
2217
 
        fprintf(fp, "\t\tMatrixTransform {\n");
2218
 
 
2219
 
        fprintf(fp, "\t\tmatrix \n");
2220
 
 
2221
 
        fprintf(fp, "\t\t\t%f %f %f %f\n", ob->obmat[0][0], ob->obmat[0][1], ob->obmat[0][2], ob->obmat[0][3]);
2222
 
        fprintf(fp, "\t\t\t%f %f %f %f\n", ob->obmat[1][0], ob->obmat[1][1], ob->obmat[1][2], ob->obmat[1][3]);
2223
 
        fprintf(fp, "\t\t\t%f %f %f %f\n", ob->obmat[2][0], ob->obmat[2][1], ob->obmat[2][2], ob->obmat[2][3]);
2224
 
        fprintf(fp, "\t\t\t%f %f %f %f\n", ob->obmat[3][0], ob->obmat[3][1], ob->obmat[3][2], ob->obmat[3][3]);
2225
 
 
2226
 
        fprintf(fp, "\t\t}\n");
2227
 
 
2228
 
        id= ob->data;
2229
 
 
2230
 
        replace_chars(str, id->name+2);
2231
 
 
2232
 
        fprintf(fp, "\t\tUSE %s\n", str);
2233
 
        fprintf(fp, "\t}\n");
2234
 
}
2235
 
 
2236
 
 
2237
 
void write_vrml(Scene *scene, char *str)
2238
 
{
2239
 
        Mesh *me;
2240
 
        Material *ma;
2241
 
        Base *base;
2242
 
        FILE *fp;
2243
 
        
2244
 
        if(BLI_testextensie(str,".blend")) str[ strlen(str)-6]= 0;
2245
 
        if(BLI_testextensie(str,".ble")) str[ strlen(str)-4]= 0;
2246
 
        if(BLI_testextensie(str,".wrl")==0) strcat(str, ".wrl");
2247
 
        //XXX saveover()       if(saveover(str)==0) return;
2248
 
        
2249
 
        fp= fopen(str, "w");
2250
 
        
2251
 
        if(fp==NULL) {
2252
 
                //XXX error("Can't write file");
2253
 
                return;
2254
 
        }
2255
 
        strcpy(temp_dir, str);
2256
 
 
2257
 
        //XXX waitcursor(1);
2258
 
        
2259
 
        /* FIRST: write all the datablocks */
2260
 
        
2261
 
        fprintf(fp, "#VRML V1.0 ascii\n\n# Blender V%d\n\n# 'Switch' is used as a hack, to ensure it is not part of the drawing\n\n", BLENDER_VERSION);
2262
 
        fprintf(fp, "Separator {\n");
2263
 
        fprintf(fp, "Switch {\n");
2264
 
 
2265
 
        ma= G.main->mat.first;
2266
 
        while(ma) {
2267
 
                if(ma->id.us) {
2268
 
                        write_material_vrml(fp, ma);
2269
 
                }
2270
 
                ma= ma->id.next;
2271
 
        }
2272
 
 
2273
 
        /* only write meshes we're using in this scene */
2274
 
        flag_listbase_ids(&G.main->mesh, LIB_DOIT, 0);
2275
 
        
2276
 
        for(base= scene->base.first; base; base= base->next)
2277
 
                if(base->object->type== OB_MESH)
2278
 
                        ((ID *)base->object->data)->flag |= LIB_DOIT;   
2279
 
        
2280
 
        me= G.main->mesh.first;
2281
 
        while(me) {
2282
 
                if(me->id.flag & LIB_DOIT) { /* is the mesh used in this scene ? */
2283
 
                        write_mesh_vrml(fp, me);
2284
 
                }
2285
 
                me= me->id.next;
2286
 
        }
2287
 
        
2288
 
        /* THEN:Hidden Objects */
2289
 
        fprintf(fp, "\n\t# Hidden Objects, in invisible layers\n\n");
2290
 
        base= scene->base.first;
2291
 
        while(base) {
2292
 
                if(base->object->type== OB_MESH) {
2293
 
                        if( (base->lay & scene->lay)==0 ) {
2294
 
                                write_object_vrml(fp, base->object);
2295
 
                        }
2296
 
                }
2297
 
                base= base->next;
2298
 
        }
2299
 
 
2300
 
        fprintf(fp, "}\n");
2301
 
        fprintf(fp, "\n# Visible Objects\n\n");
2302
 
        fprintf(fp, "Separator {\n");
2303
 
        
2304
 
        /* The camera */
2305
 
 
2306
 
        write_camera_vrml(fp, scene->camera);
2307
 
        
2308
 
        /* THEN:The Objects */
2309
 
        
2310
 
        base= scene->base.first;
2311
 
        while(base) {
2312
 
                if(base->object->type== OB_MESH) {
2313
 
                        if(base->lay & scene->lay) {
2314
 
                                write_object_vrml(fp, base->object);
2315
 
                        }
2316
 
                }
2317
 
                base= base->next;
2318
 
        }
2319
 
        
2320
 
        fprintf(fp, "}\n");
2321
 
        fprintf(fp, "}\n");
2322
 
        
2323
 
        fclose(fp);
2324
 
        
2325
 
        //XXX waitcursor(0);
2326
 
}
2327
 
 
2328
 
 
2329
 
/* ******************************* WRITE DXF ***************************** */
2330
 
 
2331
 
#define write_group(id,data) fprintf(fp, "%d\n%s\n", id, data)
2332
 
 
2333
 
/* A completely wacky function to try and make good
2334
 
indexed (AutoCAD index) values out of straight rgb 
2335
 
ones... crazy */
2336
 
 
2337
 
static int rgb_to_dxf_col (float rf, float gf, float bf) 
2338
 
{
2339
 
        int r= (int) (rf*255.0f);
2340
 
        int g= (int) (gf*255.0f);
2341
 
        int b= (int) (bf*255.0f);
2342
 
        float h,s,v;
2343
 
        int ret;
2344
 
        
2345
 
        /* Grayscale value */
2346
 
        if (((int)r/10)==((int)g/10) && ((int)g/10)==((int)b/10)) ret= 250+((int)r/51);
2347
 
        /* A nice chroma value */
2348
 
        else {
2349
 
                rgb_to_hsv (rf,gf,bf,&h,&s,&v);
2350
 
                
2351
 
                ret= (int) (10.0f + (h*239.0f));
2352
 
                CLAMP(ret,10,249);
2353
 
                
2354
 
                /* If its whitish make the index odd */
2355
 
                if (s<.5 || v>.5) if(ret%2) ret++;
2356
 
        }
2357
 
        
2358
 
        return ret;
2359
 
}
2360
 
 
2361
 
/* And its completely wacky complement */
2362
 
 
2363
 
static void dxf_col_to_rgb (int cid, float *rf, float *gf, float *bf)
2364
 
{
2365
 
        float h, s, v;
2366
 
        
2367
 
        /* Grayscale values */
2368
 
        if (cid>=250 && cid <= 255) {
2369
 
                *rf= *gf= *bf= (float) ((cid-250)*51)/255;
2370
 
                CLAMP(*rf, 0.0, 1.0);
2371
 
                CLAMP(*gf, 0.0, 1.0);
2372
 
                CLAMP(*bf, 0.0, 1.0);
2373
 
                
2374
 
        /* Pure values */
2375
 
        } else if (cid<10) {
2376
 
                switch (cid) {
2377
 
                case 1:
2378
 
                        *rf=1.0;
2379
 
                        *gf=0.0;
2380
 
                        *bf=0.0;
2381
 
                        break;
2382
 
                case 2:
2383
 
                        *rf=1.0;
2384
 
                        *gf=1.0;
2385
 
                        *bf=0.0;
2386
 
                        break;
2387
 
                case 3:
2388
 
                        *gf=1.0;
2389
 
                        *rf=0.0;
2390
 
                        *bf=0.0;
2391
 
                        break;
2392
 
                case 4:
2393
 
                        *rf=0.0;
2394
 
                        *gf=1.0;
2395
 
                        *bf=1.0;
2396
 
                        break;
2397
 
                case 5:
2398
 
                        *rf=0.0;
2399
 
                        *gf=0.0;
2400
 
                        *bf=1.0;
2401
 
                        break;
2402
 
                case 6:
2403
 
                        *rf=1.0;
2404
 
                        *gf=0.0;
2405
 
                        *bf=1.0;
2406
 
                        break;
2407
 
                case 7:
2408
 
                default:
2409
 
                        *rf= *gf= *bf= 1.0;
2410
 
                        break;
2411
 
                }
2412
 
        } else {
2413
 
                /* Get chroma values */
2414
 
                        
2415
 
                h= (float) (cid-10)/239;
2416
 
                CLAMP(h, 0.0, 1.0);
2417
 
                
2418
 
                /* If its odd make it a bit whitish */
2419
 
                if (cid%2) { s=.75; v= 0.25; 
2420
 
                } else {  s= 0.25; v= 0.75;}
2421
 
                
2422
 
                hsv_to_rgb (h, s, v, rf, gf, bf);
2423
 
        }
2424
 
}
2425
 
 
2426
 
static void write_mesh_dxf(FILE *fp, Mesh *me)
2427
 
{
2428
 
        Material *ma;
2429
 
        MVert *mvert;
2430
 
        MFace *mface;
2431
 
        int a;
2432
 
        char str[32];
2433
 
        
2434
 
        replace_chars(str, me->id.name+2);
2435
 
 
2436
 
        write_group(0, "BLOCK");
2437
 
        
2438
 
        write_group(2, str); /* The name */
2439
 
                
2440
 
        write_group(8, "Meshes"); /* DXF Layer */
2441
 
        write_group(70, "64"); /* DXF block flags */
2442
 
        
2443
 
        write_group(10, "0.0"); /* X of base */
2444
 
        write_group(20, "0.0"); /* Y of base */
2445
 
        write_group(30, "0.0"); /* Z of base */
2446
 
 
2447
 
        write_group(3, str); /* The name (again) */
2448
 
        
2449
 
        write_group(0, "POLYLINE"); /* Start the mesh */
2450
 
        write_group(66, "1"); /* Vertices follow flag */
2451
 
        write_group(8,"Meshes"); /* DXF Layer */
2452
 
 
2453
 
        if (me->totcol) {
2454
 
                ma= me->mat[0];
2455
 
                if(ma) {
2456
 
                        sprintf(str,"%d",rgb_to_dxf_col(ma->r,ma->g,ma->b));
2457
 
                        write_group(62, str); /* Color index */
2458
 
                }
2459
 
        }
2460
 
 
2461
 
        write_group(70, "64"); /* Polymesh mesh flag */
2462
 
        
2463
 
        fprintf(fp, "71\n%d\n", me->totvert); /* Total vertices */
2464
 
        fprintf(fp, "72\n%d\n", me->totface); /* Total faces */
2465
 
        
2466
 
        /* Write the vertices */
2467
 
        a= me->totvert;
2468
 
        mvert= me->mvert;
2469
 
        while(a--) {
2470
 
                write_group(0, "VERTEX"); /* Start a new vertex */
2471
 
                write_group(8, "Meshes"); /* DXF Layer */
2472
 
                fprintf (fp, "10\n%f\n", mvert->co[0]); /* X cord */
2473
 
                fprintf (fp, "20\n%f\n", mvert->co[1]); /* Y cord */
2474
 
                fprintf (fp, "30\n%f\n", mvert->co[2]); /* Z cord */
2475
 
                write_group(70, "192"); /* Polymesh vertex flag */
2476
 
                                
2477
 
                mvert++;
2478
 
        }
2479
 
 
2480
 
        /* Write the face entries */
2481
 
        a= me->totface;
2482
 
        mface= me->mface;
2483
 
        while(a--) {
2484
 
                write_group(0, "VERTEX"); /* Start a new face */
2485
 
                write_group(8, "Meshes");
2486
 
        
2487
 
                /* Write a face color */
2488
 
                if (me->totcol) {
2489
 
                        ma= me->mat[(int)mface->mat_nr];
2490
 
                        if(ma) {
2491
 
                                sprintf(str,"%d",rgb_to_dxf_col(ma->r,ma->g,ma->b));
2492
 
                                write_group(62, str); /* Color index */
2493
 
                        }
2494
 
                }
2495
 
                else write_group(62, "254"); /* Color Index */
2496
 
 
2497
 
                /* Not sure what this really corresponds too */
2498
 
                write_group(10, "0.0"); /* X of base */
2499
 
                write_group(20, "0.0"); /* Y of base */
2500
 
                write_group(30, "0.0"); /* Z of base */
2501
 
        
2502
 
                write_group(70, "128"); /* Polymesh face flag */
2503
 
        
2504
 
                if(mface->v4) {
2505
 
                        fprintf (fp, "71\n%d\n", mface->v1+1);
2506
 
                        fprintf (fp, "72\n%d\n", mface->v2+1);
2507
 
                        fprintf (fp, "73\n%d\n", mface->v3+1);
2508
 
                        fprintf (fp, "74\n%d\n", mface->v4+1);
2509
 
                } else {
2510
 
                        fprintf (fp, "71\n%d\n", mface->v1+1);
2511
 
                        fprintf (fp, "72\n%d\n", mface->v2+1);
2512
 
                        fprintf (fp, "73\n%d\n", mface->v3+1);
2513
 
                }
2514
 
                mface++;
2515
 
        }
2516
 
 
2517
 
        write_group(0, "SEQEND");       
2518
 
        
2519
 
        write_group(0, "ENDBLK");
2520
 
}
2521
 
 
2522
 
static void write_object_dxf(FILE *fp, Object *ob, int layer)
2523
 
{
2524
 
        ID *id;
2525
 
        char str[32];
2526
 
 
2527
 
        id= ob->data;
2528
 
 
2529
 
        write_group(0, "INSERT"); /* Start an insert group */
2530
 
        
2531
 
        sprintf(str, "%d", layer);
2532
 
        write_group(8, str);
2533
 
 
2534
 
        replace_chars(str, id->name+2);
2535
 
        write_group(2, str);
2536
 
 
2537
 
        fprintf (fp, "10\n%f\n", ob->loc[0]); /* X of base */
2538
 
        fprintf (fp, "20\n%f\n", ob->loc[1]); /* Y of base */
2539
 
        fprintf (fp, "30\n%f\n", ob->loc[2]); /* Z of base */
2540
 
        
2541
 
        fprintf (fp, "41\n%f\n", ob->size[0]); /* X scale */
2542
 
        fprintf (fp, "42\n%f\n", ob->size[1]); /* Y scale */
2543
 
        fprintf (fp, "43\n%f\n", ob->size[2]); /* Z scale */
2544
 
        
2545
 
        fprintf (fp, "50\n%f\n", (float) ob->rot[2]*180/M_PI); /* Can only write the Z rot */
2546
 
}
2547
 
 
2548
 
void write_dxf(struct Scene *scene, char *str)
2549
 
{
2550
 
        Mesh *me;
2551
 
        Base *base;
2552
 
        FILE *fp;
2553
 
        
2554
 
        if(BLI_testextensie(str,".blend")) str[ strlen(str)-6]= 0;
2555
 
        if(BLI_testextensie(str,".ble")) str[ strlen(str)-4]= 0;
2556
 
        if(BLI_testextensie(str,".dxf")==0) strcat(str, ".dxf");
2557
 
 
2558
 
        
2559
 
        if (BLI_exists(str)) {
2560
 
                ; //XXX if(saveover(str)==0)
2561
 
                //      return;
2562
 
        }
2563
 
 
2564
 
        fp= fopen(str, "w");
2565
 
        
2566
 
        if(fp==NULL) {
2567
 
                //XXX error("Can't write file");
2568
 
                return;
2569
 
        }
2570
 
        strcpy(temp_dir, str);
2571
 
        
2572
 
        //XXX waitcursor(1);
2573
 
        
2574
 
        /* The header part of the DXF */
2575
 
        
2576
 
        write_group(0, "SECTION");
2577
 
        write_group(2, "HEADER");
2578
 
        write_group(0, "ENDSEC");
2579
 
 
2580
 
        /* The blocks part of the DXF */
2581
 
        
2582
 
        write_group(0, "SECTION");
2583
 
        write_group(2, "BLOCKS");
2584
 
 
2585
 
    
2586
 
        /* only write meshes we're using in this scene */
2587
 
        flag_listbase_ids(&G.main->mesh, LIB_DOIT, 0);
2588
 
        
2589
 
        for(base= scene->base.first; base; base= base->next)
2590
 
                if(base->object->type== OB_MESH)
2591
 
                        ((ID *)base->object->data)->flag |= LIB_DOIT;   
2592
 
        
2593
 
        /* Write all the meshes */
2594
 
        me= G.main->mesh.first;
2595
 
        while(me) {
2596
 
                if(me->id.flag & LIB_DOIT) { /* is the mesh used in this scene ? */
2597
 
                        write_mesh_dxf(fp, me);
2598
 
                }
2599
 
                me= me->id.next;
2600
 
        }
2601
 
 
2602
 
        write_group(0, "ENDSEC");
2603
 
 
2604
 
        /* The entities part of the DXF */
2605
 
        
2606
 
        write_group(0, "SECTION");
2607
 
        write_group(2, "ENTITIES");
2608
 
 
2609
 
        /* Write all the mesh objects */
2610
 
        base= scene->base.first;
2611
 
        while(base) {
2612
 
                if(base->object->type== OB_MESH) {
2613
 
                        write_object_dxf(fp, base->object, base->lay);
2614
 
                }
2615
 
                base= base->next;
2616
 
        }
2617
 
 
2618
 
        write_group(0, "ENDSEC");
2619
 
        
2620
 
        /* Thats all */
2621
 
        
2622
 
        write_group(0, "EOF");
2623
 
        fclose(fp);
2624
 
        
2625
 
        //XXX waitcursor(0);
2626
 
}
2627
 
 
2628
 
 
2629
 
static int dxf_line= 0;
2630
 
static FILE *dxf_fp= NULL;
2631
 
 
2632
 
/* exotic.c(2863) : note C6311: c:/Program Files/Microsoft Visual
2633
 
 * Studio/VC98/include\ctype.h(268) : see previous definition of
2634
 
 * 'iswspace' */
2635
 
#define ton_iswspace(c) (c==' '||c=='\n'||c=='\t')
2636
 
 
2637
 
static void clean_wspace (char *str) 
2638
 
{
2639
 
        char *from, *to;
2640
 
        char t;
2641
 
        
2642
 
        from= str;
2643
 
        to=str;
2644
 
        
2645
 
        while (*from!=0) {
2646
 
                t= *from;
2647
 
                *to= t;
2648
 
                
2649
 
                if(!ton_iswspace(*from)) to++;
2650
 
                from++;
2651
 
        }
2652
 
        *to=0;
2653
 
}
2654
 
 
2655
 
static int all_wspace(char *str)
2656
 
{
2657
 
        while(*str != 0) {
2658
 
                if (!ton_iswspace(*str)) return 0;
2659
 
                str++;
2660
 
        }
2661
 
 
2662
 
        return 1;
2663
 
}
2664
 
 
2665
 
static int all_digits(char *str)
2666
 
{
2667
 
        while(*str != 0) {
2668
 
                if (!isdigit(*str)) return 0;
2669
 
                str++;
2670
 
        }
2671
 
 
2672
 
        return 1;
2673
 
}
2674
 
 
2675
 
static int dxf_get_layer_col(char *layer) 
2676
 
{
2677
 
        return 1;
2678
 
}
2679
 
 
2680
 
static int dxf_get_layer_num(Scene *scene, char *layer)
2681
 
{
2682
 
        int ret = 0;
2683
 
 
2684
 
        if (all_digits(layer) && atoi(layer)<(1<<20)) ret= atoi(layer);
2685
 
        if (ret == 0) ret = scene->lay;
2686
 
 
2687
 
        return ret;
2688
 
}
2689
 
 
2690
 
static void dos_clean(char *str)
2691
 
{
2692
 
        while (*str) {
2693
 
                if (*str == 0x0d) {
2694
 
                        *str='\n';
2695
 
                        *(++str)= 0;
2696
 
                        break;
2697
 
                }
2698
 
                str++;
2699
 
        }       
2700
 
}
2701
 
 
2702
 
static void myfgets(char *str, int len, FILE *fp)
2703
 
{
2704
 
        char c;
2705
 
        
2706
 
        while(len>0 && (c=getc(dxf_fp)) ) {
2707
 
                *str= c;
2708
 
                str++;
2709
 
                len--;
2710
 
                /* three types of enters, \n \r and \r\n  */
2711
 
                if(c == '\n') break;
2712
 
                if(c=='\r') {
2713
 
                        c= getc(dxf_fp);                                // read the linefeed from stream
2714
 
                        if(c != 10) ungetc(c, dxf_fp);  // put back, if it's not one...
2715
 
                        break;
2716
 
                }
2717
 
        }
2718
 
}
2719
 
 
2720
 
static int read_groupf(char *str) 
2721
 
{
2722
 
        short c;
2723
 
        int ret=-1;
2724
 
        char tmp[256];
2725
 
        
2726
 
        strcpy(str, " ");
2727
 
 
2728
 
        while ((c=getc(dxf_fp)) && ton_iswspace(c));
2729
 
        ungetc(c, dxf_fp);
2730
 
        if (c==EOF) return -1;
2731
 
        
2732
 
        myfgets(tmp, 255, dxf_fp);
2733
 
        
2734
 
        dos_clean(tmp);
2735
 
 
2736
 
        if(sscanf(tmp, "%d\n", &ret)!=1) return -2;
2737
 
                
2738
 
        myfgets(tmp, 255, dxf_fp);
2739
 
 
2740
 
        dos_clean(tmp);
2741
 
 
2742
 
        if (!all_wspace(tmp)) {
2743
 
                if (sscanf(tmp, "%s\n", str)!=1) return -2;
2744
 
        }
2745
 
        
2746
 
        clean_wspace(str);
2747
 
        dxf_line+=2;
2748
 
        
2749
 
        return ret;
2750
 
}
2751
 
 
2752
 
//XXX error() is now printf until we have a callback error
2753
 
#define id_test(id) if(id<0) {char errmsg[128];fclose(dxf_fp); if(id==-1) sprintf(errmsg, "Error inputting dxf, near line %d", dxf_line); else if(id==-2) sprintf(errmsg, "Error reading dxf, near line %d", dxf_line);printf("%s", errmsg); return;}
2754
 
 
2755
 
#define read_group(id,str) {id= read_groupf(str); id_test(id);}
2756
 
 
2757
 
#define group_is(idtst,str) (id==idtst&&strcmp(val,str)==0)
2758
 
#define group_isnt(idtst,str) (id!=idtst||strcmp(val,str)!=0)
2759
 
#define id_check(idtst,str) if(group_isnt(idtst,str)) { fclose(dxf_fp); printf("Error parsing dxf, near line %d", dxf_line); return;}
2760
 
 
2761
 
static int id;
2762
 
static char val[256];
2763
 
 
2764
 
static short error_exit=0;
2765
 
static short hasbumped=0;
2766
 
 
2767
 
static int is_dxf(char *str)
2768
 
{       
2769
 
        dxf_line=0;
2770
 
        
2771
 
        dxf_fp= fopen(str, "r");
2772
 
        if (dxf_fp==NULL) return 0;
2773
 
 
2774
 
        id= read_groupf(val);
2775
 
        if ((id==0 && strcmp(val, "SECTION")==0)||id==999) return 1;
2776
 
        
2777
 
        fclose(dxf_fp);
2778
 
        
2779
 
        return 0;
2780
 
}
2781
 
 
2782
 
/* NOTES ON THE READER */ 
2783
 
/*
2784
 
        --
2785
 
        It turns out that most DXF writers like (LOVE) to
2786
 
        write meshes as a long string of 3DFACE entities.
2787
 
        This means the natural way to read a DXF file
2788
 
        (every entity corresponds to an object) is completely
2789
 
        unusable, reading in 10,000 faces each as an
2790
 
        object just doesn't cut it. Thus the 3DFACE
2791
 
        entry reader holds state, and only finalizes to
2792
 
        an object when a) the layer name changes, b) the
2793
 
        entry type changes, c) we are done reading.
2794
 
 
2795
 
        PS... I decided to do the same thing with LINES, 
2796
 
        apparently the same thing happens sometimes as
2797
 
        well.
2798
 
 
2799
 
        PPS... I decided to do the same thing with everything.
2800
 
        Now it is all really nasty and should be rewritten. 
2801
 
        --
2802
 
        
2803
 
        Added circular and elliptical arcs and lwpolylines.
2804
 
        These are all self-contained and have the size known
2805
 
        in advance, and so I haven't used the held state. -- martin
2806
 
*/
2807
 
 
2808
 
static void dxf_add_mat (Object *ob, Mesh *me, float color[3], char *layer) 
2809
 
{
2810
 
        Material *ma;
2811
 
        
2812
 
        if (!me) return;
2813
 
        
2814
 
        if(ob) {
2815
 
                ob->mat= MEM_callocN(sizeof(void *)*1, "ob->mat");
2816
 
                ob->matbits= MEM_callocN(sizeof(char)*1, "ob->matbits");
2817
 
                ob->actcol= 1;
2818
 
        }
2819
 
 
2820
 
        me->totcol= 1;
2821
 
        me->mat= MEM_callocN(sizeof(void *)*1, "me->mat");
2822
 
        
2823
 
        if (color[0]<0) {
2824
 
                if (strlen(layer)) dxf_col_to_rgb(dxf_get_layer_col(layer), &color[0], &color[1], &color[2]);
2825
 
                color[0]= color[1]= color[2]= 0.8f;
2826
 
        }                                                                                               
2827
 
                                                
2828
 
        ma= G.main->mat.first;
2829
 
        while(ma) {
2830
 
                if(ma->mtex[0]==0) {
2831
 
                        if(color[0]==ma->r && color[1]==ma->g && color[2]==ma->b) {
2832
 
                                me->mat[0]= ma;
2833
 
                                ma->id.us++;
2834
 
                                break;
2835
 
                        }
2836
 
                }
2837
 
                ma= ma->id.next;
2838
 
        }
2839
 
        if(ma==0) {
2840
 
                ma= add_material("ext");
2841
 
                me->mat[0]= ma;
2842
 
                ma->r= color[0];
2843
 
                ma->g= color[1];
2844
 
                ma->b= color[2];
2845
 
                automatname(ma);
2846
 
        }
2847
 
}
2848
 
 
2849
 
        /* General DXF vars */
2850
 
static float cent[3]={0.0, 0.0, 0.0};
2851
 
static char layname[32]="";
2852
 
static char entname[32]="";
2853
 
static float color[3]={-1.0, -1.0, -1.0};
2854
 
static float *vcenter;
2855
 
static float zerovec[3]= {0.0, 0.0, 0.0};
2856
 
 
2857
 
#define reset_vars cent[0]= cent[1]= cent[2]=0.0; strcpy(layname, ""); color[0]= color[1]= color[2]= -1.0
2858
 
 
2859
 
 
2860
 
static void dxf_get_mesh(Scene *scene, Mesh** m, Object** o, int noob)
2861
 
{
2862
 
        Mesh *me = NULL;
2863
 
        Object *ob;
2864
 
        
2865
 
        if (!noob) {
2866
 
                *o = add_object(scene, OB_MESH);
2867
 
                ob = *o;
2868
 
                
2869
 
                if (strlen(entname)) new_id(&G.main->object, (ID *)ob, entname);
2870
 
                else if (strlen(layname)) new_id(&G.main->object, (ID *)ob,  layname);
2871
 
 
2872
 
                if (strlen(layname)) ob->lay= dxf_get_layer_num(scene, layname);
2873
 
                else ob->lay= scene->lay;
2874
 
                // not nice i know... but add_object() sets active base, which needs layer setting too (ton)
2875
 
                scene->basact->lay= ob->lay;
2876
 
 
2877
 
                *m = ob->data;
2878
 
                me= *m;
2879
 
 
2880
 
                vcenter= ob->loc;
2881
 
        } 
2882
 
        else {
2883
 
                *o = NULL;
2884
 
                *m = add_mesh("Mesh");
2885
 
 
2886
 
                me = *m;
2887
 
                ob = *o;
2888
 
                
2889
 
                ((ID *)me)->us=0;
2890
 
 
2891
 
                if (strlen(entname)) new_id(&G.main->mesh, (ID *)me, entname);
2892
 
                else if (strlen(layname)) new_id(&G.main->mesh, (ID *)me, layname);
2893
 
 
2894
 
                vcenter = zerovec;
2895
 
        }
2896
 
        me->totvert=0;
2897
 
        me->totface=0;
2898
 
        me->mvert= CustomData_add_layer(&me->vdata, CD_MVERT, CD_CALLOC, NULL, 0);
2899
 
        me->mface= CustomData_add_layer(&me->fdata, CD_MFACE, CD_CALLOC, NULL, 0);
2900
 
}
2901
 
 
2902
 
static void dxf_read_point(Scene *scene, int noob) {    
2903
 
        /* Blender vars */
2904
 
        Object *ob;
2905
 
        Mesh *me;
2906
 
        MVert *mvert;
2907
 
        
2908
 
        reset_vars;
2909
 
 
2910
 
        read_group(id, val);                                                            
2911
 
        while(id!=0) {
2912
 
                if (id==8) {
2913
 
                        BLI_strncpy(layname, val, sizeof(layname));
2914
 
                } else if (id==10) {
2915
 
                        cent[0]= (float) atof(val);
2916
 
                } else if (id==20) {
2917
 
                        cent[1]= (float) atof(val);
2918
 
                } else if (id==30) {
2919
 
                        cent[2]= (float) atof(val);
2920
 
                } else if (id==60) {
2921
 
                        /* short invisible= atoi(val); */
2922
 
                } else if (id==62) {
2923
 
                        int colorid= atoi(val);
2924
 
                                                        
2925
 
                        CLAMP(colorid, 1, 255);
2926
 
                        dxf_col_to_rgb(colorid, &color[0], &color[1], &color[2]);
2927
 
                }
2928
 
                read_group(id, val);                                                            
2929
 
        }
2930
 
 
2931
 
        dxf_get_mesh(scene, &me, &ob, noob);
2932
 
        me->totvert= 1;
2933
 
        me->mvert= MEM_callocN(me->totvert*sizeof(MVert), "mverts");
2934
 
        CustomData_set_layer(&me->vdata, CD_MVERT, me->mvert);
2935
 
        
2936
 
        dxf_add_mat (ob, me, color, layname);                                   
2937
 
 
2938
 
        mvert= me->mvert;
2939
 
        mvert->co[0]= mvert->co[1]= mvert->co[2]= 0;
2940
 
                
2941
 
        if (ob) VECCOPY(ob->loc, cent);
2942
 
 
2943
 
        hasbumped=1;
2944
 
}
2945
 
 
2946
 
        /* Line state vars */
2947
 
static Object *linehold=NULL;
2948
 
static Mesh *linemhold=NULL;
2949
 
 
2950
 
static char oldllay[32];
2951
 
static short lwasline=0; /* last was face 3d? */
2952
 
 
2953
 
static void dxf_close_line(void)
2954
 
{
2955
 
        linemhold=NULL;
2956
 
        if (linehold==NULL) return;
2957
 
        
2958
 
        linehold=NULL;
2959
 
}
2960
 
 
2961
 
static void dxf_read_line(Scene *scene, int noob) {     
2962
 
        /* Entity specific vars */
2963
 
        float epoint[3]={0.0, 0.0, 0.0};
2964
 
        short vspace=0; /* Whether or not coords are relative */
2965
 
        
2966
 
        /* Blender vars */
2967
 
        Object *ob;
2968
 
        Mesh *me;
2969
 
        MVert *mvert, *vtmp;
2970
 
        MFace *mface, *ftmp;
2971
 
        
2972
 
        reset_vars;
2973
 
 
2974
 
        read_group(id, val);                                                            
2975
 
        while(id!=0) {
2976
 
                if (id==8) {
2977
 
                        BLI_strncpy(layname, val, sizeof(layname));
2978
 
                } else if (id==10) {
2979
 
                        cent[0]= (float) atof(val);
2980
 
                } else if (id==20) {
2981
 
                        cent[1]= (float) atof(val);
2982
 
                } else if (id==30) {
2983
 
                        cent[2]= (float) atof(val);
2984
 
                } else if (id==11) {
2985
 
                        epoint[0]= (float) atof(val);
2986
 
                } else if (id==21) {
2987
 
                        epoint[1]= (float) atof(val);
2988
 
                } else if (id==31) {
2989
 
                        epoint[2]= (float) atof(val);
2990
 
                } else if (id==60) {
2991
 
                        /* short invisible= atoi(val); */
2992
 
                } else if (id==62) {
2993
 
                        int colorid= atoi(val);
2994
 
                                                        
2995
 
                        CLAMP(colorid, 1, 255);
2996
 
                        dxf_col_to_rgb(colorid, &color[0], &color[1], &color[2]);
2997
 
                } else if (id==67) {
2998
 
                        vspace= atoi(val);
2999
 
                }
3000
 
                read_group(id, val);                                                            
3001
 
        }
3002
 
 
3003
 
        /* Check to see if we need to make a new object */
3004
 
 
3005
 
        if(!lwasline || strcmp(layname, oldllay)!=0) 
3006
 
                dxf_close_line();
3007
 
        if(linemhold != NULL && linemhold->totvert>MESH_MAX_VERTS) 
3008
 
                dxf_close_line();
3009
 
                                        
3010
 
        if (linemhold==NULL) {
3011
 
                dxf_get_mesh(scene, &me, &ob, noob);
3012
 
 
3013
 
                if(ob) VECCOPY(ob->loc, cent);
3014
 
 
3015
 
                dxf_add_mat (ob, me, color, layname);
3016
 
 
3017
 
                linehold= ob;
3018
 
                linemhold= me;
3019
 
        } else {
3020
 
                ob= linehold;
3021
 
                me= linemhold;
3022
 
        }
3023
 
 
3024
 
        me->totvert+= 2;
3025
 
        me->totface++;
3026
 
        
3027
 
        vtmp= MEM_callocN(me->totvert*sizeof(MVert), "mverts");
3028
 
        ftmp= MEM_callocN(me->totface*sizeof(MFace), "mface");
3029
 
 
3030
 
        if(me->mvert) {
3031
 
                memcpy(vtmp, me->mvert, (me->totvert-2)*sizeof(MVert));
3032
 
                MEM_freeN(me->mvert);
3033
 
        }
3034
 
        me->mvert= CustomData_set_layer(&me->vdata, CD_MVERT, vtmp);
3035
 
        vtmp=NULL;
3036
 
 
3037
 
        if(me->mface) {
3038
 
                memcpy(ftmp, me->mface, (me->totface-1)*sizeof(MFace));
3039
 
                MEM_freeN(me->mface);
3040
 
        }
3041
 
        me->mface= CustomData_set_layer(&me->fdata, CD_MFACE, ftmp);
3042
 
        ftmp=NULL;
3043
 
        
3044
 
        mvert= &me->mvert[(me->totvert-2)];
3045
 
 
3046
 
        sub_v3_v3v3(mvert->co, cent, vcenter);
3047
 
        mvert++;
3048
 
        if (vspace) { VECCOPY(mvert->co, epoint);
3049
 
        } else sub_v3_v3v3(mvert->co, epoint, vcenter);
3050
 
                
3051
 
        mface= &(((MFace*)me->mface)[me->totface-1]);
3052
 
        mface->v1= me->totvert-2;
3053
 
        mface->v2= me->totvert-1;
3054
 
        mface->mat_nr= 0;
3055
 
 
3056
 
        hasbumped=1;
3057
 
}
3058
 
 
3059
 
                /* 2D Polyline state vars */
3060
 
static Object *p2dhold=NULL;
3061
 
static Mesh *p2dmhold=NULL;
3062
 
static char oldplay[32];
3063
 
static short lwasp2d=0;
3064
 
 
3065
 
static void dxf_close_2dpoly(void)
3066
 
{
3067
 
        p2dmhold= NULL;
3068
 
        if (p2dhold==NULL) return;
3069
 
 
3070
 
        p2dhold=NULL;
3071
 
}
3072
 
 
3073
 
static void dxf_read_ellipse(Scene *scene, int noob) 
3074
 
{
3075
 
 
3076
 
        /*
3077
 
   * The Parameter option of the ELLIPSE command uses the following equation to define an elliptical arc.
3078
 
   *
3079
 
   *    p(u)=c+a*cos(u)+b*sin(u)
3080
 
   *
3081
 
         * The variables a, b, c are determined when you select the endpoints for the
3082
 
         * first axis and the distance for the second axis. a is the negative of 1/2
3083
 
         * of the major axis length, b is the negative of 1/2 the minor axis length,
3084
 
         * and c is the center point (2-D) of the ellipse.
3085
 
   *
3086
 
         * Because this is actually a vector equation and the variable c is actually
3087
 
         * a point with X and Y values, it really should be written as:
3088
 
   *
3089
 
   *   p(u)=(Cx+a*cos(u))*i+(Cy+b*sin(u))*j
3090
 
   *
3091
 
   * where
3092
 
   *
3093
 
   *   Cx is the X value of the point c
3094
 
   *   Cy is the Y value of the point c
3095
 
   *   a is -(1/2 of the major axis length)
3096
 
   *   b is -(1/2 of the minor axis length)
3097
 
   *   i and j represent unit vectors in the X and Y directions
3098
 
         *
3099
 
         * http://astronomy.swin.edu.au/~pbourke/geomformats/dxf2000/ellipse_command39s_parameter_option_dxf_06.htm
3100
 
         * (reproduced with permission)
3101
 
         * 
3102
 
         * NOTE: The start and end angles ('parameters') are in radians, whereas those for the circular arc are 
3103
 
         * in degrees. The 'sense' of u appears to be determined by the extrusion direction (see more detailed comment
3104
 
         * in the code)
3105
 
         *
3106
 
         * TODO: The code is specific to ellipses in the x-y plane right now.
3107
 
         * 
3108
 
         */
3109
 
        
3110
 
        /* Entity specific vars */
3111
 
        float epoint[3]={0.0, 0.0, 0.0};
3112
 
        float center[3]={0.0, 0.0, 0.0};
3113
 
        float extrusion[3]={0.0, 0.0, 1.0}; 
3114
 
        float axis_endpoint[3] = {0.0, 0.0, 0.0}; /* major axis endpoint */
3115
 
        short vspace=0; /* Whether or not coords are relative */
3116
 
        float a, b, x, y, z;
3117
 
        float phid = 0.0f, phi = 0.0f, theta = 0.0f;
3118
 
        float start_angle = 0.0f;
3119
 
        float end_angle = 2*M_PI;
3120
 
        float axis_ratio = 1.0f;
3121
 
        float temp;
3122
 
        int v, tot;
3123
 
        int isArc=0;
3124
 
        /* Blender vars */
3125
 
        Object *ob;
3126
 
        Mesh *me;
3127
 
        MVert *mvert;
3128
 
        MFace *mface;
3129
 
        
3130
 
        reset_vars;
3131
 
        read_group(id, val);                                                            
3132
 
        while(id!=0) {
3133
 
          if (id==8) {
3134
 
                BLI_strncpy(layname, val, sizeof(layname));
3135
 
          } else if (id==10) {
3136
 
                center[0]= (float) atof(val);
3137
 
          } else if (id==20) {
3138
 
                center[1]= (float) atof(val);
3139
 
          } else if (id==30) {
3140
 
                center[2]= (float) atof(val);
3141
 
          } else if (id==11) {
3142
 
                axis_endpoint[0]= (float) atof(val);
3143
 
          } else if (id==21) {
3144
 
                axis_endpoint[1]= (float) atof(val);
3145
 
          } else if (id==31) {
3146
 
                axis_endpoint[2]= (float) atof(val);
3147
 
          } else if (id==40) {
3148
 
                axis_ratio = (float) atof(val);
3149
 
                } else if (id==41) {
3150
 
                        printf("dxf: start = %f", atof(val) * 180/M_PI);
3151
 
                start_angle = -atof(val) + M_PI_2;
3152
 
          } else if (id==42) {
3153
 
                        printf("dxf: end = %f", atof(val) * 180/M_PI);
3154
 
                        end_angle = -atof(val) + M_PI_2; 
3155
 
          } else if (id==62) {
3156
 
                int colorid= atoi(val);
3157
 
                CLAMP(colorid, 1, 255);
3158
 
                dxf_col_to_rgb(colorid, &color[0], &color[1], &color[2]);
3159
 
          } else if (id==67) {
3160
 
                vspace= atoi(val);
3161
 
          } else if (id==100) {
3162
 
                isArc = 1;
3163
 
          } else if (id==210) {
3164
 
                        extrusion[0] = atof(val);
3165
 
                } else if (id==220) {
3166
 
                        extrusion[1] = atof(val);
3167
 
                } else if (id==230) {
3168
 
                        extrusion[2] = atof(val);
3169
 
                }
3170
 
          read_group(id, val);
3171
 
        }
3172
 
 
3173
 
        if(!lwasline || strcmp(layname, oldllay)!=0) dxf_close_line();
3174
 
        if(linemhold != NULL && linemhold->totvert>MESH_MAX_VERTS) 
3175
 
          dxf_close_line();
3176
 
 
3177
 
        /* The 'extrusion direction' seems akin to a face normal, 
3178
 
         * insofar as it determines the direction of increasing phi.
3179
 
         * This is again x-y plane specific; it should be fixed at 
3180
 
         * some point. */
3181
 
        
3182
 
        if (extrusion[2] < 0) {
3183
 
                temp = start_angle;
3184
 
                start_angle = M_PI - end_angle;
3185
 
                end_angle = M_PI - temp;
3186
 
        }
3187
 
        
3188
 
        if(end_angle > start_angle)
3189
 
          end_angle -= 2 * M_PI;
3190
 
 
3191
 
        phi = start_angle;
3192
 
        
3193
 
        x = axis_endpoint[0]; 
3194
 
        y = axis_endpoint[1];
3195
 
        z = axis_endpoint[2];
3196
 
        a = sqrt(x*x + y*y + z*z);
3197
 
        b = a * axis_ratio;
3198
 
 
3199
 
        theta = atan2(y, x);
3200
 
 
3201
 
        x = a * sin(phi);
3202
 
        y = b * cos(phi);       
3203
 
 
3204
 
#ifndef DEBUG_CENTER
3205
 
        epoint[0] = center[0] + x*cos(theta) - y*sin(theta);
3206
 
        epoint[1] = center[1] + x*sin(theta) + y*cos(theta);
3207
 
        epoint[2] = center[2];
3208
 
        
3209
 
 
3210
 
        cent[0]= epoint[0];
3211
 
        cent[1]= epoint[1];
3212
 
        cent[2]= epoint[2];
3213
 
#else
3214
 
        cent[0]= center[0];
3215
 
        cent[1]= center[1];
3216
 
        cent[2]= center[2];
3217
 
#endif
3218
 
        
3219
 
        dxf_get_mesh(scene, &me, &ob, noob);
3220
 
        strcpy(oldllay, layname);               
3221
 
        if(ob) VECCOPY(ob->loc, cent);
3222
 
        dxf_add_mat (ob, me, color, layname);
3223
 
 
3224
 
        tot = 32; /* # of line segments to divide the arc into */
3225
 
 
3226
 
        phid = (end_angle - start_angle)/tot; 
3227
 
 
3228
 
        me->totvert += tot+1;
3229
 
        me->totface += tot+1;
3230
 
        
3231
 
        me->mvert = (MVert*) MEM_callocN(me->totvert*sizeof(MVert), "mverts");
3232
 
        me->mface = (MFace*) MEM_callocN(me->totface*sizeof(MVert), "mface");
3233
 
 
3234
 
        CustomData_set_layer(&me->vdata, CD_MVERT, me->mvert);
3235
 
        CustomData_set_layer(&me->fdata, CD_MFACE, me->mface);
3236
 
 
3237
 
        printf("vertex and face buffers allocated\n");
3238
 
 
3239
 
        for(v = 0; v <= tot; v++) {
3240
 
 
3241
 
                x = a * sin(phi);
3242
 
                y = b * cos(phi);       
3243
 
                epoint[0] = center[0] + x*cos(theta) - y*sin(theta);
3244
 
                epoint[1] = center[1] + x*sin(theta) + y*cos(theta);
3245
 
                epoint[2] = center[2];
3246
 
          
3247
 
                mvert= &me->mvert[v];
3248
 
                
3249
 
                if (vspace) {
3250
 
                        VECCOPY(mvert->co, epoint);
3251
 
                }       else {
3252
 
                        sub_v3_v3v3(mvert->co, epoint, vcenter);
3253
 
                }
3254
 
 
3255
 
                if (v > 0) {
3256
 
                        mface= &(((MFace*)me->mface)[v-1]);
3257
 
                        mface->v1 = v-1;
3258
 
                        mface->v2 = v;
3259
 
                        mface->mat_nr = 0;
3260
 
                }
3261
 
          
3262
 
                hasbumped = 1;
3263
 
 
3264
 
                VECCOPY(cent, epoint);    
3265
 
                phi+=phid;
3266
 
        }
3267
 
}
3268
 
 
3269
 
static void dxf_read_arc(Scene *scene, int noob) 
3270
 
{
3271
 
        /* Entity specific vars */
3272
 
        float epoint[3]={0.0, 0.0, 0.0};
3273
 
        float center[3]={0.0, 0.0, 0.0};
3274
 
        float extrusion[3]={0.0, 0.0, 1.0};
3275
 
        short vspace=0; /* Whether or not coords are relative */
3276
 
        float dia = 0.0f;
3277
 
        float phid = 0.0f, phi = 0.0f;
3278
 
        float start_angle = 0.0f;
3279
 
        float end_angle = 2*M_PI;
3280
 
        float temp;
3281
 
        int v, tot = 32;
3282
 
        int isArc=0;
3283
 
        /* Blender vars */
3284
 
        Object *ob;
3285
 
        Mesh *me;
3286
 
        MVert *mvert;
3287
 
        MFace *mface;
3288
 
        
3289
 
        reset_vars;
3290
 
        read_group(id, val);                                                            
3291
 
        while(id!=0) {
3292
 
          if (id==8) {
3293
 
                BLI_strncpy(layname, val, sizeof(layname));
3294
 
          } else if (id==10) {
3295
 
                center[0]= (float) atof(val);
3296
 
          } else if (id==20) {
3297
 
                center[1]= (float) atof(val);
3298
 
          } else if (id==30) {
3299
 
                center[2]= (float) atof(val);
3300
 
          } else if (id==40) {
3301
 
                dia = (float) atof(val);
3302
 
          } else if (id==62) {
3303
 
                int colorid= atoi(val);
3304
 
            
3305
 
                CLAMP(colorid, 1, 255);
3306
 
                dxf_col_to_rgb(colorid, &color[0], &color[1], &color[2]);
3307
 
          } else if (id==67) {
3308
 
                vspace= atoi(val);
3309
 
          } else if (id==100) {
3310
 
                isArc = 1;
3311
 
          } else if (id==50) {
3312
 
                start_angle = (90 - atoi(val)) * M_PI/180.0;
3313
 
          } else if (id==51) {
3314
 
                end_angle = (90 - atoi(val)) * M_PI/180.0;
3315
 
          } else if (id==210) {
3316
 
                        extrusion[0] = atof(val);
3317
 
                } else if (id==220) {
3318
 
                        extrusion[1] = atof(val);
3319
 
                } else if (id==230) {
3320
 
                        extrusion[2] = atof(val);
3321
 
                }
3322
 
          read_group(id, val);
3323
 
        }
3324
 
 
3325
 
        if(!lwasline || strcmp(layname, oldllay)!=0) dxf_close_line();
3326
 
        if(linemhold != NULL && linemhold->totvert>MESH_MAX_VERTS) 
3327
 
          dxf_close_line();
3328
 
        
3329
 
        /* Same xy-plane-specific extrusion direction code as in read_ellipse
3330
 
         * (read_arc and read_ellipse should ideally be rewritten to share code)
3331
 
         */
3332
 
        
3333
 
        if (extrusion[2] < 0) {
3334
 
                temp = start_angle;
3335
 
                start_angle = M_PI - end_angle;
3336
 
                end_angle = M_PI - temp;
3337
 
        }
3338
 
        
3339
 
        phi = start_angle;
3340
 
        if(end_angle > start_angle)
3341
 
          end_angle -= 2 * M_PI;
3342
 
 
3343
 
        cent[0]= center[0]+dia*sin(phi);
3344
 
        cent[1]= center[1]+dia*cos(phi);
3345
 
        cent[2]= center[2];
3346
 
 
3347
 
        dxf_get_mesh(scene, &me, &ob, noob);
3348
 
        strcpy(oldllay, layname);               
3349
 
        if(ob) VECCOPY(ob->loc, cent);
3350
 
        dxf_add_mat (ob, me, color, layname);
3351
 
 
3352
 
        tot = 32; /* # of line segments to divide the arc into */
3353
 
        phid = (end_angle - start_angle)/tot; /* fix so that arcs have the same 'resolution' as circles? */
3354
 
 
3355
 
        me->totvert += tot+1;
3356
 
        me->totface += tot+1;
3357
 
        
3358
 
        me->mvert = (MVert*) MEM_callocN(me->totvert*sizeof(MVert), "mverts");
3359
 
        me->mface = (MFace*) MEM_callocN(me->totface*sizeof(MVert), "mface");
3360
 
 
3361
 
        CustomData_set_layer(&me->vdata, CD_MVERT, me->mvert);
3362
 
        CustomData_set_layer(&me->fdata, CD_MFACE, me->mface);
3363
 
 
3364
 
        for(v = 0; v <= tot; v++) { 
3365
 
 
3366
 
                epoint[0]= center[0]+dia*sin(phi);
3367
 
                epoint[1]= center[1]+dia*cos(phi);
3368
 
                epoint[2]= center[2];
3369
 
 
3370
 
                mvert= &me->mvert[v];
3371
 
                
3372
 
                if (vspace) {
3373
 
                        VECCOPY(mvert->co, epoint);
3374
 
                } else {
3375
 
                        sub_v3_v3v3(mvert->co, epoint, vcenter);
3376
 
                }
3377
 
 
3378
 
                if (v > 0) {
3379
 
                        mface= &(((MFace*)me->mface)[v-1]);
3380
 
                        mface->v1 = v-1;
3381
 
                        mface->v2 = v;
3382
 
                        mface->mat_nr = 0;
3383
 
                }
3384
 
          
3385
 
                hasbumped=1;
3386
 
 
3387
 
                VECCOPY(cent, epoint);    
3388
 
                phi+=phid;
3389
 
        }
3390
 
}
3391
 
 
3392
 
static void dxf_read_polyline(Scene *scene, int noob) { 
3393
 
        /* Entity specific vars */
3394
 
        short vspace=0; /* Whether or not coords are relative */
3395
 
        int flag=0;
3396
 
        int vflags=0;
3397
 
        int vids[4];
3398
 
        int nverts;
3399
 
        
3400
 
        /* Blender vars */
3401
 
        Object *ob;
3402
 
        Mesh *me;
3403
 
        float vert[3] = {0};
3404
 
        
3405
 
        MVert *mvert, *vtmp;
3406
 
        MFace *mface, *ftmp;
3407
 
        
3408
 
        reset_vars;
3409
 
 
3410
 
        read_group(id, val);                                                            
3411
 
        while(id!=0) {
3412
 
                if (id==8) {
3413
 
                        BLI_strncpy(layname, val, sizeof(layname));
3414
 
                } else if (id==10) {
3415
 
                        cent[0]= (float) atof(val);
3416
 
                } else if (id==20) {
3417
 
                        cent[1]= (float) atof(val);
3418
 
                } else if (id==30) {
3419
 
                        cent[2]= (float) atof(val);
3420
 
                } else if (id==60) {
3421
 
                        /* short invisible= atoi(val); */
3422
 
                } else if (id==62) {
3423
 
                        int colorid= atoi(val);
3424
 
                                                        
3425
 
                        CLAMP(colorid, 1, 255);
3426
 
                        dxf_col_to_rgb(colorid, &color[0], &color[1], &color[2]);
3427
 
                } else if (id==67) {
3428
 
                        vspace= atoi(val);
3429
 
                } else if (id==70) {
3430
 
                        flag= atoi(val);                        
3431
 
                }
3432
 
                read_group(id, val);                                                            
3433
 
        }
3434
 
 
3435
 
        if (flag & 9) { // 1= closed curve, 8= 3d curve
3436
 
                if(!lwasp2d || strcmp(layname, oldplay)!=0) dxf_close_2dpoly();
3437
 
                if(p2dmhold != NULL && p2dmhold->totvert>MESH_MAX_VERTS)
3438
 
                        dxf_close_2dpoly();
3439
 
 
3440
 
                if (p2dmhold==NULL) {
3441
 
                        dxf_get_mesh(scene, &me, &ob, noob);
3442
 
 
3443
 
                        strcpy(oldplay, layname);
3444
 
                                
3445
 
                        if(ob) VECCOPY(ob->loc, cent);
3446
 
                
3447
 
                        dxf_add_mat (ob, me, color, layname);
3448
 
                
3449
 
                        p2dhold= ob;
3450
 
                        p2dmhold= me;
3451
 
                } 
3452
 
                else {
3453
 
                        ob= p2dhold;
3454
 
                        me= p2dmhold;
3455
 
                }
3456
 
                
3457
 
                nverts=0;
3458
 
                while (group_is(0, "VERTEX")) {
3459
 
                        read_group(id, val);
3460
 
                        while(id!=0) {
3461
 
                                if (id==10) {
3462
 
                                        vert[0]= (float) atof(val);
3463
 
                                } else if (id==20) {
3464
 
                                        vert[1]= (float) atof(val);
3465
 
                                } else if (id==30) {
3466
 
                                        vert[2]= (float) atof(val);
3467
 
                                }
3468
 
                                read_group(id, val);
3469
 
                        }
3470
 
                        nverts++;
3471
 
                        me->totvert++;
3472
 
                        
3473
 
                        vtmp= MEM_callocN(me->totvert*sizeof(MVert), "mverts");
3474
 
                        
3475
 
                        if (me->mvert) {
3476
 
                                memcpy (vtmp, me->mvert, (me->totvert-1)*sizeof(MVert));
3477
 
                                MEM_freeN(me->mvert);
3478
 
                        }
3479
 
                        me->mvert= CustomData_set_layer(&me->vdata, CD_MVERT, vtmp);
3480
 
                        vtmp= NULL;
3481
 
                        
3482
 
                        mvert= &me->mvert[me->totvert-1];
3483
 
                        
3484
 
                        if (vspace) { VECCOPY(mvert->co, vert);
3485
 
                        } else sub_v3_v3v3(mvert->co, vert, vcenter);
3486
 
                }
3487
 
                
3488
 
                /* make edges */
3489
 
                if(nverts>1) {
3490
 
                        int a, oldtotface;
3491
 
                        
3492
 
                        oldtotface= me->totface;
3493
 
                        me->totface+= nverts-1;
3494
 
 
3495
 
                        ftmp= MEM_callocN(me->totface*sizeof(MFace), "mface");
3496
 
 
3497
 
                        if(me->mface) {
3498
 
                                memcpy(ftmp, me->mface, oldtotface*sizeof(MFace));
3499
 
                                MEM_freeN(me->mface);
3500
 
                        }
3501
 
                        me->mface= CustomData_set_layer(&me->fdata, CD_MFACE, ftmp);
3502
 
                        ftmp=NULL;
3503
 
 
3504
 
                        mface= me->mface;
3505
 
                        mface+= oldtotface;
3506
 
                        
3507
 
                        for(a=1; a<nverts; a++, mface++) {
3508
 
                                mface->v1= (me->totvert-nverts)+a-1;
3509
 
                                mface->v2= (me->totvert-nverts)+a;
3510
 
                                mface->mat_nr= 0;
3511
 
                        }
3512
 
                }
3513
 
                
3514
 
                lwasp2d=1;
3515
 
        } 
3516
 
        else if (flag&64) {
3517
 
                dxf_get_mesh(scene, &me, &ob, noob);
3518
 
                
3519
 
                if(ob) VECCOPY(ob->loc, cent);
3520
 
        
3521
 
                dxf_add_mat (ob, me, color, layname);
3522
 
 
3523
 
                while (group_is(0, "VERTEX")) {
3524
 
                        vflags= 0;
3525
 
                        vids[0]= vids[1]= vids[2]= vids[3]= 0;
3526
 
                
3527
 
                        vflags=0;
3528
 
                        read_group(id, val);
3529
 
                        while(id!=0) {
3530
 
                                if(id==8) {
3531
 
                                        ; /* Layer def, skip */
3532
 
                                } else if (id==10) {
3533
 
                                        vert[0]= (float) atof(val);
3534
 
                                } else if (id==20) {
3535
 
                                        vert[1]= (float) atof(val);
3536
 
                                } else if (id==30) {
3537
 
                                        vert[2]= (float) atof(val);
3538
 
                                } else if (id==70) {
3539
 
                                        vflags= atoi(val);
3540
 
                                } else if (id==71) {
3541
 
                                        vids[0]= abs(atoi(val));
3542
 
                                } else if (id==72) {
3543
 
                                        vids[1]= abs(atoi(val));
3544
 
                                } else if (id==73) {
3545
 
                                        vids[2]= abs(atoi(val));
3546
 
                                } else if (id==74) {
3547
 
                                        vids[3]= abs(atoi(val));
3548
 
                                }
3549
 
                                read_group(id, val);
3550
 
                        }
3551
 
                        
3552
 
                        if (vflags & 128 && vflags & 64) {
3553
 
                                me->totvert++;
3554
 
                                
3555
 
                                /* If we are nearing the limit scan to the next entry */
3556
 
                                if(me->totvert > MESH_MAX_VERTS) 
3557
 
                                        while(group_isnt(0, "SEQEND")) read_group(id, val);
3558
 
                
3559
 
                                vtmp= MEM_callocN(me->totvert*sizeof(MVert), "mverts");
3560
 
        
3561
 
                                if(me->mvert) {
3562
 
                                        memcpy(vtmp, me->mvert, (me->totvert-1)*sizeof(MVert));
3563
 
                                        MEM_freeN(me->mvert);
3564
 
                                }
3565
 
                                me->mvert= CustomData_set_layer(&me->vdata, CD_MVERT, vtmp);
3566
 
                                vtmp=NULL;
3567
 
                                
3568
 
                                mvert= &me->mvert[(me->totvert-1)];
3569
 
        
3570
 
                                if (vspace) { VECCOPY(mvert->co, vert);
3571
 
                                } else sub_v3_v3v3(mvert->co, vert, vcenter);
3572
 
        
3573
 
                        } else if (vflags & 128) {
3574
 
                                if(vids[2]==0) {
3575
 
                                        //XXX error("(PL) Error parsing dxf, not enough vertices near line %d", dxf_line);
3576
 
                        
3577
 
                                        error_exit=1;
3578
 
                                        fclose(dxf_fp);
3579
 
                                        return;
3580
 
                                }
3581
 
        
3582
 
                                me->totface++;
3583
 
                
3584
 
                                ftmp= MEM_callocN(me->totface*sizeof(MFace), "mfaces");
3585
 
        
3586
 
                                if(me->mface) {
3587
 
                                        memcpy(ftmp, me->mface, (me->totface-1)*sizeof(MFace));
3588
 
                                        MEM_freeN(me->mface);
3589
 
                                }
3590
 
                                me->mface= CustomData_set_layer(&me->fdata, CD_MFACE, ftmp);
3591
 
                                ftmp=NULL;                      
3592
 
                                
3593
 
                                mface= &(((MFace*)me->mface)[me->totface-1]);
3594
 
                                mface->v1= vids[0]-1;
3595
 
                                mface->v2= vids[1]-1;
3596
 
                                mface->v3= vids[2]-1;
3597
 
        
3598
 
                                if(vids[3] && vids[3]!=vids[0]) {
3599
 
                                        mface->v4= vids[3]-1;
3600
 
                                        test_index_face(mface, NULL, 0, 4);
3601
 
                                }
3602
 
                                else test_index_face(mface, NULL, 0, 3);
3603
 
        
3604
 
                                mface->mat_nr= 0;
3605
 
        
3606
 
                        } else {
3607
 
                                //XXX error("Error parsing dxf, unknown polyline information near %d", dxf_line);
3608
 
                        
3609
 
                                error_exit=1;
3610
 
                                fclose(dxf_fp);
3611
 
                                return;
3612
 
                        }
3613
 
        
3614
 
                }       
3615
 
        }
3616
 
}
3617
 
 
3618
 
static void dxf_read_lwpolyline(Scene *scene, int noob) {       
3619
 
        /* Entity specific vars */
3620
 
        short vspace=0; /* Whether or not coords are relative */
3621
 
        int flag=0;
3622
 
        int nverts=0;
3623
 
        int v;
3624
 
        
3625
 
        /* Blender vars */
3626
 
        Object *ob;
3627
 
        Mesh *me;
3628
 
        float vert[3] = {0};
3629
 
        
3630
 
        MVert *mvert;
3631
 
        MFace *mface;
3632
 
        
3633
 
        reset_vars;
3634
 
 
3635
 
        id = -1;
3636
 
 
3637
 
        /* block structure is
3638
 
         * {...}
3639
 
         * 90 => nverts
3640
 
         * 70 => flags
3641
 
         * nverts.times { 10 => x, 20 => y }
3642
 
         */
3643
 
        while(id!=70)   {
3644
 
                read_group(id, val);                                                            
3645
 
                if (id==8) {
3646
 
                        BLI_strncpy(layname, val, sizeof(layname));
3647
 
                } else if (id==38) {
3648
 
                        vert[2]= (float) atof(val);
3649
 
                } else if (id==60) {
3650
 
                        /* short invisible= atoi(val); */
3651
 
                } else if (id==62) {
3652
 
                        int colorid= atoi(val);
3653
 
                                                        
3654
 
                        CLAMP(colorid, 1, 255);
3655
 
                        dxf_col_to_rgb(colorid, &color[0], &color[1], &color[2]);
3656
 
                } else if (id==67) {
3657
 
                        vspace= atoi(val);
3658
 
                } else if (id==70) {
3659
 
                        flag= atoi(val);                        
3660
 
                } else if (id==90) {
3661
 
                        nverts= atoi(val);
3662
 
                }
3663
 
        } 
3664
 
        printf("nverts %d\n", nverts);  
3665
 
        if (nverts == 0)
3666
 
                return;
3667
 
 
3668
 
        dxf_get_mesh(scene, &me, &ob, noob);
3669
 
        strcpy(oldllay, layname);               
3670
 
        if(ob) VECCOPY(ob->loc, cent);
3671
 
        dxf_add_mat (ob, me, color, layname);
3672
 
 
3673
 
        me->totvert += nverts;
3674
 
        me->totface += nverts;
3675
 
 
3676
 
        me->mvert = (MVert*) MEM_callocN(me->totvert*sizeof(MVert), "mverts");
3677
 
        me->mface = (MFace*) MEM_callocN(me->totface*sizeof(MVert), "mface");
3678
 
 
3679
 
        CustomData_set_layer(&me->vdata, CD_MVERT, me->mvert);
3680
 
        CustomData_set_layer(&me->fdata, CD_MFACE, me->mface);
3681
 
 
3682
 
        for (v = 0; v < nverts; v++) {
3683
 
                read_group(id,val);
3684
 
                if (id == 10) {
3685
 
                        vert[0]= (float) atof(val);
3686
 
                } else {
3687
 
                        //XXX error("Error parsing dxf, expected (10, <x>) at line %d", dxf_line);      
3688
 
                }
3689
 
 
3690
 
                read_group(id,val);
3691
 
                if (id == 20) {
3692
 
                        vert[1]= (float) atof(val);
3693
 
                } else {
3694
 
                        //XXX error("Error parsing dxf, expected (20, <y>) at line %d", dxf_line);      
3695
 
                }
3696
 
                
3697
 
                mvert = &me->mvert[v];
3698
 
 
3699
 
                if (vspace) { 
3700
 
                        VECCOPY(mvert->co, vert);
3701
 
                } else {
3702
 
                        sub_v3_v3v3(mvert->co, vert, vcenter);
3703
 
                }
3704
 
 
3705
 
                if (v > 0) {
3706
 
                        mface= &(((MFace*)me->mface)[v-1]);
3707
 
                        mface->v1 = v-1;
3708
 
                        mface->v2 = v;
3709
 
                        mface->mat_nr = 0;
3710
 
                }
3711
 
        }
3712
 
 
3713
 
        /* flag & 1 -> closed polyline 
3714
 
   * TODO: give the polyline actual 2D faces if it is closed */
3715
 
 
3716
 
        if (flag&1) {
3717
 
                if(me->mface) {
3718
 
                        mface= &(((MFace*)me->mface)[nverts - 1]);
3719
 
                        mface->v1 = nverts-1;
3720
 
                        mface->v2 = 0;
3721
 
                        mface->mat_nr = 0;
3722
 
                }
3723
 
        }  
3724
 
}
3725
 
 
3726
 
 
3727
 
        /* 3D Face state vars */
3728
 
static Object *f3dhold=NULL;
3729
 
static Mesh *f3dmhold=NULL;
3730
 
static char oldflay[32];
3731
 
static short lwasf3d=0; /* last was face 3d? */
3732
 
 
3733
 
/* how can this function do anything useful (ton)? */
3734
 
static void dxf_close_3dface(void)
3735
 
{
3736
 
        f3dmhold= NULL;
3737
 
        if (f3dhold==NULL) return;
3738
 
        
3739
 
        f3dhold=NULL;
3740
 
}
3741
 
 
3742
 
static void dxf_read_3dface(Scene *scene, int noob) 
3743
 
{       
3744
 
        /* Entity specific vars */
3745
 
        float vert2[3]={0.0, 0.0, 0.0};
3746
 
        float vert3[3]={0.0, 0.0, 0.0};
3747
 
        float vert4[3]={0.0, 0.0, 0.0};
3748
 
        short vspace=0;
3749
 
 
3750
 
        int nverts=0;
3751
 
        
3752
 
        /* Blender vars */
3753
 
        Object *ob;
3754
 
        Mesh *me;
3755
 
        MVert *mvert, *vtmp;
3756
 
        MFace *mface, *ftmp;
3757
 
        
3758
 
        reset_vars;
3759
 
 
3760
 
        read_group(id, val);                                                            
3761
 
        while(id!=0) {
3762
 
                if (id==8) {
3763
 
                        BLI_strncpy(layname, val, sizeof(layname));
3764
 
                
3765
 
                /* First vert/origin */
3766
 
                } else if (id==10) {
3767
 
                        cent[0]= (float) atof(val);
3768
 
                        if (nverts<1)nverts++;
3769
 
                } else if (id==20) {
3770
 
                        cent[1]= (float) atof(val);
3771
 
                        if (nverts<1)nverts++;
3772
 
                } else if (id==30) {
3773
 
                        cent[2]= (float) atof(val);
3774
 
                        if (nverts<1)nverts++;
3775
 
                        
3776
 
                /* Second vert */
3777
 
                } else if (id==11) {
3778
 
                        vert2[0]= (float) atof(val);
3779
 
                        if (nverts<2)nverts++;
3780
 
                } else if (id==21) {
3781
 
                        vert2[1]= (float) atof(val);
3782
 
                        if (nverts<2)nverts++;
3783
 
                } else if (id==31) {
3784
 
                        vert2[2]= (float) atof(val);
3785
 
                        if (nverts<2)nverts++;
3786
 
                
3787
 
                /* Third vert */
3788
 
                } else if (id==12) {
3789
 
                        vert3[0]= (float) atof(val);
3790
 
                        if (nverts<3)nverts++;
3791
 
                } else if (id==22) {
3792
 
                        vert3[1]= (float) atof(val);
3793
 
                        if (nverts<3)nverts++;
3794
 
                } else if (id==32) {
3795
 
                        vert3[2]= (float) atof(val);
3796
 
                        if (nverts<3)nverts++;
3797
 
                        
3798
 
                /* Fourth vert */
3799
 
                } else if (id==13) {
3800
 
                        vert4[0]= (float) atof(val);
3801
 
                        if (nverts<4)nverts++;
3802
 
                } else if (id==23) {
3803
 
                        vert4[1]= (float) atof(val);
3804
 
                        if (nverts<4)nverts++;
3805
 
                } else if (id==33) {
3806
 
                        vert4[2]= (float) atof(val);
3807
 
                        if (nverts<4)nverts++;
3808
 
                        
3809
 
                /* Other */
3810
 
                } else if (id==60) {
3811
 
                        /* short invisible= atoi(val); */
3812
 
                } else if (id==62) {
3813
 
                        int colorid= atoi(val);
3814
 
                                                        
3815
 
                        CLAMP(colorid, 1, 255);
3816
 
                        dxf_col_to_rgb(colorid, &color[0], &color[1], &color[2]);
3817
 
                } else if (id==67) {
3818
 
                        vspace= atoi(val);
3819
 
                }
3820
 
                read_group(id, val);                                                            
3821
 
        }
3822
 
 
3823
 
        /* Check to see if we need to make a new object */
3824
 
 
3825
 
        if(!lwasf3d || strcmp(layname, oldflay)!=0) dxf_close_3dface();
3826
 
        if(f3dmhold != NULL && f3dmhold->totvert>MESH_MAX_VERTS)
3827
 
                dxf_close_3dface();
3828
 
        
3829
 
        if(nverts<3) {
3830
 
                //XXX error("(3DF) Error parsing dxf, not enough vertices near line %d", dxf_line);
3831
 
                
3832
 
                error_exit=1;
3833
 
                fclose(dxf_fp);
3834
 
                return;
3835
 
        }
3836
 
 
3837
 
        if (f3dmhold==NULL) {
3838
 
                dxf_get_mesh(scene, &me, &ob, noob);
3839
 
                
3840
 
                strcpy(oldflay, layname);
3841
 
                
3842
 
                if(ob) VECCOPY(ob->loc, cent);
3843
 
        
3844
 
                dxf_add_mat (ob, me, color, layname);
3845
 
                
3846
 
                f3dhold= ob;
3847
 
                f3dmhold= me;
3848
 
        } else {
3849
 
                ob= f3dhold;
3850
 
                me= f3dmhold;
3851
 
        }
3852
 
        
3853
 
        me->totvert+= nverts;
3854
 
        me->totface++;
3855
 
        
3856
 
        vtmp= MEM_callocN(me->totvert*sizeof(MVert), "mverts");
3857
 
        ftmp= MEM_callocN(me->totface*sizeof(MFace), "mface");
3858
 
 
3859
 
        if(me->mvert) {
3860
 
                memcpy(vtmp, me->mvert, (me->totvert-nverts)*sizeof(MVert));
3861
 
                MEM_freeN(me->mvert);
3862
 
        }
3863
 
        me->mvert= CustomData_set_layer(&me->vdata, CD_MVERT, vtmp);
3864
 
        vtmp=NULL;
3865
 
 
3866
 
        if(me->mface) {
3867
 
                memcpy(ftmp, me->mface, (me->totface-1)*sizeof(MFace));
3868
 
                MEM_freeN(me->mface);
3869
 
        }
3870
 
        me->mface= CustomData_set_layer(&me->fdata, CD_MFACE, ftmp);
3871
 
        ftmp=NULL;
3872
 
        
3873
 
        mvert= &me->mvert[(me->totvert-nverts)];
3874
 
        sub_v3_v3v3(mvert->co, cent, vcenter);
3875
 
                                                
3876
 
        mvert++;
3877
 
        if (vspace) { VECCOPY(mvert->co, vert2);
3878
 
        } else sub_v3_v3v3(mvert->co, vert2, vcenter);
3879
 
 
3880
 
        mvert++;
3881
 
        if (vspace) { VECCOPY(mvert->co, vert3);
3882
 
        } else sub_v3_v3v3(mvert->co, vert3, vcenter);
3883
 
 
3884
 
        if (nverts==4) {
3885
 
                mvert++;
3886
 
                if (vspace) { VECCOPY(mvert->co, vert4);
3887
 
                } else sub_v3_v3v3(mvert->co, vert4, vcenter);          
3888
 
        }
3889
 
 
3890
 
        mface= &(((MFace*)me->mface)[me->totface-1]);
3891
 
        mface->v1= (me->totvert-nverts)+0;
3892
 
        mface->v2= (me->totvert-nverts)+1;
3893
 
        mface->v3= (me->totvert-nverts)+2;
3894
 
 
3895
 
        if (nverts==4)
3896
 
                mface->v4= (me->totvert-nverts)+3;
3897
 
 
3898
 
        mface->mat_nr= 0;
3899
 
 
3900
 
        test_index_face(mface, NULL, 0, nverts);
3901
 
 
3902
 
        hasbumped=1;
3903
 
}
3904
 
 
3905
 
static void dxf_read(Scene *scene, char *filename)
3906
 
{
3907
 
        Mesh *lastMe = G.main->mesh.last;
3908
 
 
3909
 
        /* clear ugly global variables, that can hang because on error the code
3910
 
           below returns... tsk (ton) */
3911
 
        dxf_line=0;
3912
 
        dxf_close_3dface();
3913
 
        dxf_close_2dpoly();
3914
 
        dxf_close_line();
3915
 
        
3916
 
        dxf_fp= fopen(filename, "r");
3917
 
        if (dxf_fp==NULL) return;
3918
 
        
3919
 
        while (1) {     
3920
 
                read_group(id, val);
3921
 
                if (group_is(0, "EOF")) break;
3922
 
                
3923
 
                if (id==999) continue;
3924
 
                id_check(0, "SECTION");
3925
 
        
3926
 
                read_group(id, val);
3927
 
                if (group_is(2, "HEADER")) {            
3928
 
                } else if (group_is(2, "TABLES")) {
3929
 
                } else if (group_is(2, "OBJECTS")) {
3930
 
                } else if (group_is(2, "CLASSES")) {
3931
 
                } else if (group_is(2, "BLOCKS")) {     
3932
 
                        while(1) {
3933
 
                                read_group(id, val);
3934
 
                                if (group_is(0, "BLOCK")) {
3935
 
                                        while(group_isnt(0, "ENDBLK")) {
3936
 
                                                read_group(id, val);
3937
 
 
3938
 
                                                if(id==2) {
3939
 
                                                        BLI_strncpy(entname, val, sizeof(entname));
3940
 
                                                } else if (id==3) {
3941
 
                                                        /* Now the object def should follow */
3942
 
                                                        if(strlen(entname)==0) {
3943
 
                                                                //XXX error("Error parsing dxf, no mesh name near %d", dxf_line);
3944
 
                                                                fclose(dxf_fp);
3945
 
                                                                return;
3946
 
                                                        }
3947
 
                                                
3948
 
                                                        /* Now the object def should follow */
3949
 
                                                        while(group_isnt(0, "ENDBLK")) {
3950
 
                                                                read_group(id, val);
3951
 
 
3952
 
                                                                if(group_is(0, "POLYLINE")) {
3953
 
                                                                        dxf_read_polyline(scene, 1);
3954
 
                                                                        if(error_exit) return;
3955
 
                                                                        lwasf3d=0;
3956
 
                                                                        lwasline=0;
3957
 
 
3958
 
                                                                        while(group_isnt(0, "SEQEND")) read_group(id, val);                                             
3959
 
                                                                        
3960
 
                                                                }       else if(group_is(0, "LWPOLYLINE")) {
3961
 
                                                                        dxf_read_lwpolyline(scene, 1);
3962
 
                                                                        if(error_exit) return;
3963
 
                                                                        lwasf3d=0;
3964
 
                                                                        lwasline=0;
3965
 
 
3966
 
                                                                        while(group_isnt(0, "SEQEND")) read_group(id, val);                                             
3967
 
                                                                } else if(group_is(0, "ATTRIB")) {
3968
 
                                                                        while(group_isnt(0, "SEQEND")) read_group(id, val);                                             
3969
 
                                                                        lwasf3d=0;
3970
 
                                                                        lwasp2d=0;
3971
 
                                                                        lwasline=0;
3972
 
                                                                } else if(group_is(0, "POINT")) {
3973
 
                                                                        dxf_read_point(scene, 1);
3974
 
                                                                        if(error_exit) return;
3975
 
                                                                        lwasf3d=0;
3976
 
                                                                        lwasp2d=0;
3977
 
                                                                        lwasline=0;
3978
 
                                                                } else if(group_is(0, "LINE")) {
3979
 
                                                                        dxf_read_line(scene, 1);
3980
 
                                                                        if(error_exit) return;
3981
 
                                                                        lwasline=1;
3982
 
                                                                        lwasp2d=0;
3983
 
                                                                        lwasf3d=0;
3984
 
                                                                } else if(group_is(0, "3DFACE")) {
3985
 
                                                                        dxf_read_3dface(scene, 1);
3986
 
                                                                        if(error_exit) return;
3987
 
                                                                        lwasf3d=1;
3988
 
                                                                        lwasp2d=0;
3989
 
                                                                        lwasline=0;
3990
 
                                                                } else if (group_is(0, "CIRCLE")) {
3991
 
                                                                        dxf_read_arc(scene, 1);
3992
 
                                                                } else if (group_is(0, "ELLIPSE")) {
3993
 
                                                                        dxf_read_ellipse(scene, 1);
3994
 
                                                                } else if (group_is(0, "ENDBLK")) { 
3995
 
                                                                        break;
3996
 
                                                                }
3997
 
                                                        }
3998
 
                                                } else if (group_is(0, "ENDBLK")) {
3999
 
                                                        break;
4000
 
                                                }
4001
 
                                        }
4002
 
                                        while(id!=0) read_group(id, val); 
4003
 
 
4004
 
                                } else if(group_is(0, "ENDSEC")) {
4005
 
                                        break;
4006
 
                                }
4007
 
                        }
4008
 
                } else if (group_is(2, "ENTITIES")) {                   
4009
 
                        while(group_isnt(0, "ENDSEC")) {
4010
 
                                char obname[32]="";
4011
 
                                char layname[32]="";
4012
 
                                float cent[3]={0.0, 0.0, 0.0};
4013
 
                                float obsize[3]={1.0, 1.0, 1.0};
4014
 
                                float obrot[3]={0.0, 0.0, 0.0};
4015
 
                                
4016
 
                                if(!hasbumped) read_group(id, val);
4017
 
                                hasbumped=0;
4018
 
                                if (group_is(0, "INSERT")) {
4019
 
                                        Base *base;
4020
 
                                        Object *ob;
4021
 
                                        void *obdata;
4022
 
                                        
4023
 
                                        read_group(id, val);
4024
 
 
4025
 
                                        while(id!=0) {
4026
 
                                                if(id==2) {
4027
 
                                                        BLI_strncpy(obname, val, sizeof(obname));
4028
 
                                                } else if (id==8) {
4029
 
                                                        BLI_strncpy(layname, val, sizeof(layname));
4030
 
                                                } else if (id==10) {
4031
 
                                                        cent[0]= (float) atof(val);
4032
 
                                                } else if (id==20) {
4033
 
                                                        cent[1]= (float) atof(val);
4034
 
                                                } else if (id==30) {
4035
 
                                                        cent[2]= (float) atof(val);
4036
 
                                                } else if (id==41) {
4037
 
                                                        obsize[0]= (float) atof(val);
4038
 
                                                } else if (id==42) {
4039
 
                                                        obsize[1]= (float) atof(val);
4040
 
                                                } else if (id==43) {
4041
 
                                                        obsize[2]= (float) atof(val);
4042
 
                                                } else if (id==50) {
4043
 
                                                        obrot[2]= (float) (atof(val)*M_PI/180.0);
4044
 
                                                } else if (id==60) {
4045
 
                                                        /* short invisible= atoi(val); */
4046
 
                                                }
4047
 
                                                
4048
 
                                                read_group(id, val);
4049
 
 
4050
 
                                        }
4051
 
                        
4052
 
                                        if(strlen(obname)==0) {
4053
 
                                                //XXX error("Error parsing dxf, no object name near %d", dxf_line);
4054
 
                                                fclose(dxf_fp);
4055
 
                                                return;
4056
 
                                        }
4057
 
                                        
4058
 
                                        obdata= find_id("ME", obname);
4059
 
        
4060
 
                                        if (obdata) {
4061
 
                                                ob= alloc_libblock(&G.main->object, ID_OB, obname);
4062
 
        
4063
 
                                                ob->type= OB_MESH;
4064
 
        
4065
 
                                                ob->dt= OB_TEXTURE;
4066
 
 
4067
 
                                                ob->trackflag= OB_POSY;
4068
 
                                                ob->upflag= OB_POSZ;
4069
 
 
4070
 
                                                ob->ipoflag = OB_OFFS_OB+OB_OFFS_PARENT;
4071
 
        
4072
 
                                                ob->dupon= 1; ob->dupoff= 0;
4073
 
                                                ob->dupsta= 1; ob->dupend= 100;
4074
 
                                                ob->recalc= OB_RECALC_ALL;      /* needed because of weird way of adding libdata directly */
4075
 
                                                
4076
 
                                                ob->data= obdata;
4077
 
                                                ((ID*)ob->data)->us++;
4078
 
                                                
4079
 
                                                VECCOPY(ob->loc, cent);
4080
 
                                                VECCOPY(ob->size, obsize);
4081
 
                                                VECCOPY(ob->rot, obrot);
4082
 
                                                
4083
 
                                                ob->mat= MEM_callocN(sizeof(void *)*1, "ob->mat");
4084
 
                                                ob->matbits= MEM_callocN(sizeof(char)*1, "ob->matbits");
4085
 
                                                ob->totcol= (unsigned char) ((Mesh*)ob->data)->totcol;
4086
 
                                                ob->actcol= 1;
4087
 
 
4088
 
                                                /* note: materials are either linked to mesh or object, if both then 
4089
 
                                                        you have to increase user counts. below line is not needed.
4090
 
                                                        I leave it commented out here as warning (ton) */
4091
 
                                                //for (i=0; i<ob->totcol; i++) ob->mat[i]= ((Mesh*)ob->data)->mat[i];
4092
 
                                                
4093
 
                                                if (strlen(layname)) ob->lay= dxf_get_layer_num(scene, layname);
4094
 
                                                else ob->lay= scene->lay;
4095
 
        
4096
 
                                                /* link to scene */
4097
 
                                                base= MEM_callocN( sizeof(Base), "add_base");
4098
 
                                                BLI_addhead(&scene->base, base);
4099
 
                
4100
 
                                                base->lay= ob->lay;
4101
 
                
4102
 
                                                base->object= ob;
4103
 
                                        }
4104
 
 
4105
 
                                        hasbumped=1;
4106
 
 
4107
 
                                        lwasf3d=0;
4108
 
                                        lwasp2d=0;
4109
 
                                        lwasline=0;
4110
 
                                } else if(group_is(0, "POLYLINE")) {
4111
 
                                        dxf_read_polyline(scene, 0);
4112
 
                                        if(error_exit) return;
4113
 
                                        lwasf3d=0;
4114
 
                                        lwasline=0;
4115
 
 
4116
 
                                        while(group_isnt(0, "SEQEND")) read_group(id, val);                                             
4117
 
 
4118
 
                                } else if(group_is(0, "LWPOLYLINE")) {
4119
 
                                        dxf_read_lwpolyline(scene, 0);
4120
 
                                        if(error_exit) return;
4121
 
                                        lwasf3d=0;
4122
 
                                        lwasline=0;
4123
 
                                        //while(group_isnt(0, "SEQEND")) read_group(id, val);                                           
4124
 
                                        
4125
 
                                } else if(group_is(0, "ATTRIB")) {
4126
 
                                        while(group_isnt(0, "SEQEND")) read_group(id, val);                                             
4127
 
                                        lwasf3d=0;
4128
 
                                        lwasp2d=0;
4129
 
                                        lwasline=0;
4130
 
                                } else if(group_is(0, "POINT")) {
4131
 
                                        dxf_read_point(scene, 0);
4132
 
                                        if(error_exit) return;
4133
 
                                        lwasf3d=0;
4134
 
                                        lwasp2d=0;
4135
 
                                        lwasline=0;
4136
 
                                } else if(group_is(0, "LINE")) {
4137
 
                                        dxf_read_line(scene, 0);
4138
 
                                        if(error_exit) return;
4139
 
                                        lwasline=1;
4140
 
                                        lwasp2d=0;
4141
 
                                        lwasf3d=0;
4142
 
                                } else if(group_is(0, "3DFACE")) {
4143
 
                                        dxf_read_3dface(scene, 0);
4144
 
                                        if(error_exit) return;
4145
 
                                        lwasline=0;
4146
 
                                        lwasp2d=0;
4147
 
                                        lwasf3d=1;
4148
 
                                } else if (group_is(0, "CIRCLE") || group_is(0, "ARC")) {
4149
 
                                  dxf_read_arc(scene, 0);
4150
 
                                } else if (group_is(0, "ELLIPSE")) {
4151
 
                                  dxf_read_ellipse(scene, 0);
4152
 
                                } else if(group_is(0, "ENDSEC")) {
4153
 
                                        break;
4154
 
                                }
4155
 
                        }
4156
 
                }
4157
 
        
4158
 
                while(group_isnt(0, "ENDSEC")) read_group(id, val);
4159
 
        }               
4160
 
        id_check(0, "EOF");
4161
 
        
4162
 
        fclose (dxf_fp);
4163
 
        
4164
 
        /* Close any remaining state held stuff */
4165
 
        dxf_close_3dface();
4166
 
        dxf_close_2dpoly();
4167
 
        dxf_close_line();
4168
 
 
4169
 
        if (lastMe) {
4170
 
                lastMe = lastMe->id.next;
4171
 
        } else {
4172
 
                lastMe = G.main->mesh.first;
4173
 
        }
4174
 
        for (; lastMe; lastMe=lastMe->id.next) {
4175
 
                mesh_add_normals_flags(lastMe);
4176
 
                make_edges(lastMe, 0);
4177
 
        }
4178
 
}