some small optimizations
[blender.git] / source / blender / blenkernel / intern / exotic.c
1 /*  exotic.c   mei 95     MIXED MODEL
2  * 
3  *  $Id$
4  *
5  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version. The Blender
11  * Foundation also sells licenses for use in proprietary software under
12  * the Blender License.  See http://www.blender.org/BL/ for information
13  * about this.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  *
24  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
25  * All rights reserved.
26  *
27  * The Original Code is: all of this file.
28  *
29  * Contributor(s): none yet.
30  *
31  * ***** END GPL/BL DUAL LICENSE BLOCK *****
32  *  
33  *  eigen videoscape formaat:
34  *
35  * 
36  * lamp:
37  *              3DG2
38                 aantal_lampen
39                 
40                 type
41                 spsi spbl
42                 r, g, b, energy
43                 locx, locy, locz
44                 vecx, vecy, vecz
45
46                 
47         curve / nurbs:
48                 3DG3
49                 5 of 11 (curve of surf)
50                 aantal_nurbs
51                 extr1 extr2
52                 
53                 mat[0][0] mat[0][1] mat[0][2] mat[0][3]
54                 mat[1][0] mat[1][1] mat[1][2] mat[1][3]
55                 ...             
56                 
57                 type
58                 pntsu, pntsv
59                 resolu, resolv
60                 orderu, orderv
61                 flagu, flagv
62                 
63                 (als type==nurb) x y z w
64                                                  x y z w
65                                                  ...
66                 (als type==bez)  xyz xyz xyz h1 h2 h3
67                                                  xyz xyz xyz h1 h2 h3
68                                                  ...
69  *  
70  * 
71  */
72
73
74 #include <ctype.h> /* isdigit, isspace */
75 #include <math.h>
76 #include <stdio.h>
77 #include <stdlib.h>
78 #include <fcntl.h>
79 #include <string.h>
80 #ifndef WIN32 
81 #include <unistd.h>
82 #else
83 #include "BLI_winstuff.h"
84 #include <io.h>
85 #endif
86
87 #include "MEM_guardedalloc.h"
88
89 #include "DNA_object_types.h"
90 #include "DNA_mesh_types.h"
91 #include "DNA_material_types.h"
92 #include "DNA_lamp_types.h"
93 #include "DNA_curve_types.h"
94 #include "DNA_image_types.h"
95 #include "DNA_camera_types.h"
96 #include "DNA_scene_types.h"
97 #include "DNA_view3d_types.h"
98 #include "DNA_userdef_types.h"
99
100 #include "BKE_bad_level_calls.h"
101 #include "BKE_utildefines.h"
102 #include "BLI_blenlib.h"
103 #include "BLI_arithb.h"
104 #include "BLI_editVert.h"
105
106 #include "BKE_global.h"
107 #include "BKE_main.h"
108 #include "BKE_mesh.h"
109 #include "BKE_library.h"
110 #include "BKE_global.h"
111 #include "BKE_object.h"
112 #include "BKE_material.h"
113 #include "BKE_exotic.h"
114 /*  #include "BKE_error.h" */
115 #include "BKE_screen.h"
116 #include "BKE_displist.h"
117 #include "BKE_curve.h"
118
119 #include "BPY_extern.h"
120
121 /***/
122
123 static int is_dxf(char *str);
124 static void dxf_read(char *filename);
125
126 /***/
127
128 static void read_videoscape_mesh(char *str)
129 {
130         Object *ob;
131         Mesh *me;
132         MVert *mvert;
133         MFace *mface;
134         Material *ma;
135         FILE *fp;
136         float *vertdata, *vd, min[3], max[3], cent[3], ftemp;
137         unsigned int color[32], col;
138         int totcol, a, b, verts, tottria=0, totquad=0, totedge=0, poly, nr0, nr, first;
139         short end;
140         char s[50];
141         
142         fp= fopen(str, "rb");
143         if(fp==NULL) {
144                 error("Can't read file");
145                 return;
146         }
147         
148         fscanf(fp, "%40s", s);
149         
150         fscanf(fp, "%d\n", &verts);
151         if(verts<=0) {
152                 fclose(fp);
153                 error("Read error");
154                 return;
155         }
156         
157         if(verts>65000) {
158                 error("too many vertices");
159                 fclose(fp);
160                 return;
161         }
162         
163         INIT_MINMAX(min, max);
164         vd= vertdata= MEM_mallocN(sizeof(float)*3*verts, "videoscapelezer");
165         
166         for(a=0; a<verts; a++) {
167                 fscanf(fp, "%f %f %f", vd, vd+1, vd+2);
168                 DO_MINMAX(vd, min, max);
169                 vd+=3;
170         }
171         
172         /* de vlakken en kleuren tellen */
173         for(a=0; a<32; a++) color[a]= 0;
174         totcol= 0;
175         end= 1;
176         while(end>0) {
177                 end= fscanf(fp,"%d", &poly);
178                 if(end<=0) break;
179         
180                 if(poly==3) tottria++;
181                 else if(poly==4) totquad++;
182                 else totedge+= poly;
183         
184                 for(a=0;a<poly;a++) {
185                         end= fscanf(fp,"%d", &nr);
186                         if(end<=0) break;
187                 }
188                 if(end<=0) break;
189                 
190                 end= fscanf(fp,"%i\n", &col);
191                 col &= 0xF0F0F0;
192                 for(a=0; a<totcol; a++) {
193                         if(color[a]==col) break;
194                 }
195                 if(a>=totcol && totcol<32) {
196                         color[totcol]= col;
197                         totcol++;
198                 }
199         }
200         
201         if(totedge+tottria+totquad>65000) {
202                 printf(" var1: %d, var2: %d, var3: %d \n", totedge, tottria, totquad);
203                 error("too many faces");
204                 MEM_freeN(vertdata);
205                 fclose(fp);
206                 return;
207         }
208         
209         /* nieuw object */
210         ob= add_object(OB_MESH);
211         me= ob->data;
212         me->totvert= verts;
213         me->totface= totedge+tottria+totquad;
214         
215         me->mvert= MEM_callocN(me->totvert*sizeof(MVert), "mverts");
216         if(me->totface) me->mface= MEM_callocN(me->totface*sizeof(MFace), "mface");
217         
218         /* kleuren */
219         if(totcol) {
220                 ob->mat= MEM_callocN(sizeof(void *)*totcol, "ob->mat");
221                 me->mat= MEM_callocN(sizeof(void *)*totcol, "me->mat");
222                 me->totcol= totcol;
223                 ob->totcol= (unsigned char) me->totcol;
224                 ob->actcol= 1;
225         }
226         
227         /* materialen */
228         for(a=0; a<totcol; a++) {
229                 ma= G.main->mat.first;
230                 while(ma) {
231                         if(ma->mtex[0]==0) {
232                                 col= rgb_to_cpack(ma->r, ma->g, ma->b);
233                                 if(color[a]==col) {
234                                         me->mat[a]= ma;
235                                         ma->id.us++;
236                                         break;
237                                 }
238                         }
239                         ma= ma->id.next;
240                 }
241                 if(ma==0) {
242                         ma= add_material("ext");
243                         me->mat[a]= ma;
244                         cpack_to_rgb(color[a], cent, cent+1, cent+2);
245                         ma->r= cent[0];
246                         ma->g= cent[1];
247                         ma->b= cent[2];
248                         automatname(ma);
249                 }
250         }
251         
252         /* verts */
253         
254         cent[0]= (min[0]+max[0])/2.0f;
255         cent[1]= (min[1]+max[1])/2.0f;
256         cent[2]= (min[2]+max[2])/2.0f;
257         VECCOPY(ob->loc, cent);
258         
259         a= me->totvert;
260         vd= vertdata;
261         mvert= me->mvert;
262         while(a--) {
263                 VecSubf(mvert->co, vd, cent);
264                 mvert++;
265                 vd+= 3;
266         }
267         
268         /* faces */
269         if(me->totface) {
270                 rewind(fp);
271         
272                 fscanf(fp, "%40s", s);
273                 fscanf(fp, "%d\n", &verts);
274                 /* fake read */
275                 for(a=0;a<verts;a++) {
276                         fscanf(fp, "%f %f %f", &ftemp, &ftemp, &ftemp);
277                 }
278                 
279                 a= me->totface;
280                 mface= me->mface;
281                 while(a--) {
282                         end= fscanf(fp,"%d", &poly);
283                         if(end<=0) break;
284         
285                         if(poly==3 || poly==4) {
286                                 fscanf(fp,"%d", &nr);
287                                 mface->v1= MIN2(nr, me->totvert-1);
288                                 fscanf(fp,"%d", &nr);
289                                 mface->v2= MIN2(nr, me->totvert-1);
290                                 fscanf(fp,"%d", &nr);
291                                 mface->v3= MIN2(nr, me->totvert-1);
292                                 if(poly==4) {
293                                         if( fscanf(fp,"%d", &nr) <=0 ) break;
294                                         mface->v4= MIN2(nr, me->totvert-1);
295                                 }
296                                 mface->edcode= 3;
297                                 
298                                 test_index_mface(mface, poly);
299                                 
300                                 mface++;
301                         }
302                         else {
303                                 if( fscanf(fp,"%d", &nr0) <=0) break;
304                                 first= nr0;
305                                 for(b=1; b<poly; b++) {
306                                         end= fscanf(fp,"%d", &nr);
307                                         if(end<=0) break;
308                                         nr= MIN2(nr, me->totvert-1);
309                                         mface->v1= nr;
310                                         mface->v2= nr0;
311                                         nr0= nr;
312                                         mface++;
313                                         a--;
314                                 }
315                                 mface->v1= first;
316                                 mface->v2= nr;
317                                 mface->edcode= 1;
318                                 mface++;
319                                 if(end<=0) break;
320                         }
321                         end= fscanf(fp,"%i", &col);
322                         col &= 0xF0F0F0;
323                         if(end<=0) break;
324                         
325                         for(b=0; b<totcol; b++) {
326                                 if(color[b]==col) {
327                                         (mface-1)->mat_nr= b;
328                                         break;
329                                 }
330                         }
331                 }
332         }
333         
334         fclose(fp);
335         MEM_freeN(vertdata);
336         
337         G.obedit= ob;
338         make_editMesh();
339         load_editMesh();
340         free_editMesh();
341         G.obedit= 0;
342         tex_space_mesh(me);
343         waitcursor(1);
344 }
345
346 static void read_radiogour(char *str)
347 {
348         Object *ob;
349         Mesh *me;
350         MVert *mvert;
351         MFace *mface;
352         FILE *fp;
353         float *vertdata, *vd, min[3], max[3], cent[3], ftemp;
354         unsigned int *colv, *colf, *colvertdata;
355         int  itemp, a, b, verts, tottria=0, totquad=0, totedge=0, poly, nr0, nr, first;
356         short end;
357         char s[50];
358         
359         fp= fopen(str, "rb");
360         if(fp==NULL) {
361                 error("Can't read file");
362                 return;
363         }
364         
365         fscanf(fp, "%40s", s);
366         
367         fscanf(fp, "%d\n", &verts);
368         if(verts<=0) {
369                 fclose(fp);
370                 error("Read error");
371                 return;
372         }
373         
374         if(verts>65000) {
375                 error("too many vertices");
376                 fclose(fp);
377                 return;
378         }
379         
380         INIT_MINMAX(min, max);
381         vd= vertdata= MEM_mallocN(sizeof(float)*3*verts, "videoscapelezer");
382         colv= colvertdata= MEM_mallocN(verts*sizeof(float), "coldata");
383         
384         for(a=0; a<verts; a++) {
385                 fscanf(fp, "%f %f %f %i", vd, vd+1, vd+2, colv);
386                 DO_MINMAX(vd, min, max);
387                 vd+=3;
388                 colv++;
389         }
390         
391         /* de vlakken tellen */
392         end= 1;
393         while(end>0) {
394                 end= fscanf(fp,"%d", &poly);
395                 if(end<=0) break;
396         
397                 if(poly==3) tottria++;
398                 else if(poly==4) totquad++;
399                 else totedge+= poly;
400         
401                 for(a=0;a<poly;a++) {
402                         end= fscanf(fp,"%d", &nr);
403                         if(end<=0) break;
404                 }
405                 if(end<=0) break;
406                 
407         }
408         
409         if(totedge+tottria+totquad>65000) {
410                 printf(" var1: %d, var2: %d, var3: %d \n", totedge, tottria, totquad);
411                 error("too many faces");
412                 MEM_freeN(vertdata);
413                 MEM_freeN(colvertdata);
414                 fclose(fp);
415                 return;
416         }
417         
418         /* nieuw object */
419         ob= add_object(OB_MESH);
420         me= ob->data;
421         me->totvert= verts;
422         me->totface= totedge+tottria+totquad;
423         me->flag= 0;
424         me->mvert= MEM_callocN(me->totvert*sizeof(MVert), "mverts");
425         if(me->totface) me->mface= MEM_callocN(me->totface*sizeof(MFace), "mface");
426         
427         /* verts */
428         
429         cent[0]= (min[0]+max[0])/2.0f;
430         cent[1]= (min[1]+max[1])/2.0f;
431         cent[2]= (min[2]+max[2])/2.0f;
432         VECCOPY(ob->loc, cent);
433         
434         a= me->totvert;
435         vd= vertdata;
436         mvert= me->mvert;
437         while(a--) {
438                 VecSubf(mvert->co, vd, cent);
439                 mvert++;
440                 vd+= 3;
441         }
442         
443         /* faces */
444         if(me->totface) {
445                 rewind(fp);
446         
447                 fscanf(fp, "%40s", s);
448                 fscanf(fp, "%d\n", &verts);
449                 for(a=0;a<verts;a++) {
450                         fscanf(fp, "%f %f %f %i", &ftemp, &ftemp, &ftemp, &itemp);
451                 }
452                 
453                 a= me->totface;
454                 mface= me->mface;
455                 while(a--) {
456                         end= fscanf(fp,"%d", &poly);
457                         if(end<=0) break;
458         
459                         if(poly==3 || poly==4) {
460                                 fscanf(fp,"%d", &nr);
461                                 mface->v1= MIN2(nr, me->totvert-1);
462                                 fscanf(fp,"%d", &nr);
463                                 mface->v2= MIN2(nr, me->totvert-1);
464                                 fscanf(fp,"%d", &nr);
465                                 mface->v3= MIN2(nr, me->totvert-1);
466                                 if(poly==4) {
467                                         if( fscanf(fp,"%d", &nr) <=0 ) break;
468                                         mface->v4= MIN2(nr, me->totvert-1);
469                                 }
470                                 mface->edcode= 3;
471                                 
472                                 test_index_mface(mface, poly);
473                                 
474                                 mface++;
475                         }
476                         else {
477                                 if( fscanf(fp,"%d", &nr0) <=0) break;
478                                 first= nr0;
479                                 for(b=1; b<poly; b++) {
480                                         end= fscanf(fp,"%d", &nr);
481                                         if(end<=0) break;
482                                         nr= MIN2(nr, me->totvert-1);
483                                         mface->v1= nr;
484                                         mface->v2= nr0;
485                                         nr0= nr;
486                                         mface++;
487                                         a--;
488                                 }
489                                 mface->v1= first;
490                                 mface->v2= nr;
491                                 mface->edcode= 1;
492                                 mface->flag= ME_SMOOTH;
493                                 
494                                 mface++;
495                                 if(end<=0) break;
496                         }
497                 }
498                 
499                 /* mcol is per vlak 4 kleuren */
500                 me->mcol= MEM_mallocN(4*sizeof(int)*me->totface, "mcol");
501                 colf= (unsigned int *)me->mcol;
502
503                 a= me->totface;
504                 mface= me->mface;
505                 while(a--) {
506                         
507                         colf[0]= colvertdata[mface->v1];
508                         colf[1]= colvertdata[mface->v2];
509                         colf[2]= colvertdata[mface->v3];
510                         colf[3]= colvertdata[mface->v4];
511                         
512                         colf+= 4;
513                         mface++;
514                 }
515                 
516                 MEM_freeN(colvertdata);
517         }
518         
519         fclose(fp);
520         MEM_freeN(vertdata);
521         
522         G.obedit= ob;
523         make_editMesh();
524         load_editMesh();
525         free_editMesh();
526
527         G.obedit= 0;
528         tex_space_mesh(me);
529
530         waitcursor(1);
531 }
532
533
534 static void read_videoscape_lamp(char *str)
535 {
536         Object *ob;
537         Lamp *la;
538         FILE *fp;
539         float vec[3], *q1;
540         int tot, val;
541         char s[50];
542         
543         fp= fopen(str, "rb");
544         if(fp==NULL) {
545                 error("Can't read file");
546                 return;
547         }
548
549         fscanf(fp, "%40s", s);
550         fscanf(fp, "%d\n", &tot);
551         
552         while(tot--) {
553                 ob= add_object(OB_LAMP);
554                 la= ob->data;
555                 
556                 fscanf(fp, "%d\n", &val);
557                 la->type= val;
558                 if(la->type==1) la->type= LA_SPOT;
559                 else if(la->type==2) la->type= LA_SUN;
560                 
561                 fscanf(fp, "%f %f\n", &la->spotsize, &la->spotblend);
562                 
563                 fscanf(fp, "%f %f %f %f\n", &la->r, &la->g, &la->b, &la->energy);               
564                 
565                 fscanf(fp, "%f %f %f\n", ob->loc, ob->loc+1, ob->loc+2);
566                 val= fscanf(fp, "%f %f %f\n", vec, vec+1, vec+2);
567                 q1= vectoquat(vec, 5, 2);
568                 QuatToEul(q1, ob->rot);
569                 
570                 if(val<=0) break;
571                 
572         }
573         fclose(fp);
574 }
575
576 static void read_videoscape_nurbs(char *str)
577 {
578         Object *ob;
579         Curve *cu;
580         Nurb *nu;
581         BezTriple *bezt;
582         BPoint *bp;
583         FILE *fp;
584         float tmat[4][4], omat[3][3], imat[3][3], mat[3][3];
585         int a, tot, type, val;
586         char s[50];
587
588         fp= fopen(str, "rb");
589         if(fp==NULL) {
590                 error("Can't read file");
591                 return;
592         }
593
594         fscanf(fp, "%40s", s);
595         fscanf(fp, "%d\n", &type);
596         
597         if(type==5) ob= add_object(OB_SURF);
598         else ob= add_object(OB_CURVE);
599         cu= ob->data;
600         
601         fscanf(fp, "%d\n", &tot);
602         fscanf(fp, "%d %d\n", &type, &val);
603         
604         cu->ext1= 0.002f*type;
605         cu->ext2= 0.002f*val;
606
607         for(a=0; a<4; a++) fscanf(fp, "%e %e %e %e\n", tmat[a], tmat[a]+1, tmat[a]+2, tmat[a]+3);
608
609         VECCOPY(ob->loc, tmat[3]);
610
611         Mat3CpyMat4(omat, tmat);
612         Mat3ToEul(omat, ob->rot);
613         EulToMat3(ob->rot, mat);
614         Mat3Inv(imat, mat);
615         Mat3MulMat3(tmat, imat, omat);
616         
617         while(tot--) {
618                 nu= (Nurb*)MEM_callocN(sizeof(Nurb),"nu from exotic");
619                 BLI_addtail(&cu->nurb, nu);
620                 
621                 fscanf(fp, "%d\n", &type);
622                 nu->type= type;
623
624                 fscanf(fp, "%d %d\n", &type, &val);
625                 nu->pntsu= type; nu->pntsv= val;
626                 fscanf(fp, "%d %d\n", &type, &val);
627                 nu->resolu= type; nu->resolv= val;
628                 fscanf(fp, "%d %d\n", &type, &val);
629                 nu->orderu= type; nu->orderv= val;
630                 fscanf(fp, "%d %d\n", &type, &val);
631                 nu->flagu= type; nu->flagv= val;
632                 
633                 if( (nu->type & 7)==CU_BEZIER) {
634                         a= nu->pntsu;
635                         nu->bezt= bezt= MEM_callocN(a*sizeof(BezTriple), "bezt from exotic");
636                         while(a--) {
637                                 fscanf(fp, "%f %f %f ", bezt->vec[0], bezt->vec[0]+1, bezt->vec[0]+2);
638                                 Mat4MulVecfl(tmat, bezt->vec[0]);
639                                 fscanf(fp, "%f %f %f ", bezt->vec[1], bezt->vec[1]+1, bezt->vec[1]+2);
640                                 Mat4MulVecfl(tmat, bezt->vec[1]);
641                                 fscanf(fp, "%f %f %f ", bezt->vec[2], bezt->vec[2]+1, bezt->vec[2]+2);
642                                 Mat4MulVecfl(tmat, bezt->vec[2]);
643                                 fscanf(fp, "%d %d\n", &type, &val);
644                                 bezt->h1= type;
645                                 bezt->h2= val;
646                                 bezt++;
647                         }
648                 }
649                 else {
650                         a= nu->pntsu*nu->pntsv;
651                         if(a) {
652                                 nu->bp= bp= MEM_callocN(a*sizeof(BPoint), "bp from exotic");
653                                 while(a--) {
654                                         fscanf(fp, "%f %f %f %f\n", bp->vec, bp->vec+1, bp->vec+2, bp->vec+3);
655                                         Mat4MulVecfl(tmat, bp->vec);
656                                         bp++;
657                                 }
658                                 
659                                 val= KNOTSU(nu);
660                                 nu->knotsu= MEM_mallocN(sizeof(float)*val, "knots");
661                                 for(a=0; a<val; a++) fscanf(fp, "%f\n", nu->knotsu+a);
662                                 
663                                 if(nu->pntsv>1) {
664                                         val= KNOTSV(nu);
665                                         nu->knotsv= MEM_mallocN(sizeof(float)*val, "knots");
666                                         for(a=0; a<val; a++) fscanf(fp, "%f\n", nu->knotsv+a);
667                                 }
668                         }
669                         else {
670                                 BLI_remlink(&cu->nurb, nu);
671                                 MEM_freeN(nu);
672                         }
673                 }
674         }
675         fclose(fp);
676         makeDispList(ob);
677 }
678
679 static void read_videoscape(char *str)
680 {
681         int file, type;
682         unsigned short val, numlen;
683         char name[FILE_MAXDIR+FILE_MAXFILE], head[FILE_MAXFILE], tail[FILE_MAXFILE];
684         
685         strcpy(name, str);
686
687         while( TRUE ) {
688                 file= open(name, O_BINARY|O_RDONLY);
689                 if(file<=0) break;
690                 else {
691                         read(file, &type, 4);
692                         close(file);
693                         
694                         if(type==DDG1) read_videoscape_mesh(name);
695                         else if(type==DDG2) read_videoscape_lamp(name);
696                         else if(type==DDG3) read_videoscape_nurbs(name);
697                 }
698
699                 val = BLI_stringdec(name, head, tail, &numlen);
700                 BLI_stringenc(name, head, tail, numlen, val + 1);
701
702         }
703 }
704
705
706 /* ***************** INVENTOR ******************* */
707
708
709 #define IV_MAXSTACK 500000
710 #define IV_MAXFIELD 10
711 #define IV_MAXCOL 16
712
713 static float *iv_data_stack;
714 static float ivcolors[IV_MAXCOL][3];
715 static Object *ivsurf;
716 static ListBase ivbase;
717
718 struct IvNode {
719         struct IvNode *next, *prev;
720         char *nodename;
721         char *fieldname[IV_MAXFIELD];
722         int datalen[IV_MAXFIELD];
723         float *data[IV_MAXFIELD];
724 };
725
726 static int iv_curcol=0;
727
728 static int iv_colornumber(struct IvNode *iv)
729 {
730         float *fp, fr = 0.0, fg = 0.0, fb = 0.0;
731         int a;
732         char *cp;
733         
734         /* terugzoeken naar laatste materiaal */
735         while(iv) {
736                 if( strcmp(iv->nodename, "Material")==0) {
737                         fp= iv->data[0];
738                         if(fp==0) fp= iv->data[1];
739                         if(fp) {
740                                 fr= fp[0];
741                                 fg= fp[1];
742                                 fb= fp[2];
743                         }
744                         break;
745                 }
746                 else if( strcmp(iv->nodename, "BaseColor")==0) {
747                         fp= iv->data[0];
748                         fr= fp[0];
749                         fg= fp[1];
750                         fb= fp[2];
751                         break;
752                 }
753                 else if( strcmp(iv->nodename, "PackedColor")==0) {
754                         cp= (char *)iv->data[0];
755                         fr= cp[3]/255.0f;
756                         fg= cp[2]/255.0f;
757                         fb= cp[1]/255.0f;
758                         break;
759                 }
760                 iv= iv->prev;
761                 
762         }
763         if(iv==0) return 0;
764         if(iv->datalen[0]<3) return 0;
765         
766         for(a=0; a<iv_curcol; a++) {
767         
768                 if(ivcolors[a][0]== fr)
769                         if(ivcolors[a][1]== fg)
770                                 if(ivcolors[a][2]== fb) return a+1
771                                 ;
772         }
773         
774         if(a>=IV_MAXCOL) a= IV_MAXCOL-1;
775         iv_curcol= a+1;
776         ivcolors[a][0]= fr;
777         ivcolors[a][1]= fg;
778         ivcolors[a][2]= fb;
779         
780         return iv_curcol;
781 }
782
783 static int iv_finddata(struct IvNode *iv, char *field, int fieldnr)
784 {
785         /* zoek naar "field", tel lengte data en maak datablok. return skipdata */
786         float *fp;
787         int len, stackcount, skipdata=0;
788         char *cpa, terminator, str[64];
789         long i;
790         
791         len= strlen(field);
792
793         cpa= iv->nodename+1;
794         while( *cpa != '}' ) {
795                 
796                 if( *cpa == *field ) {
797                         if( strncmp(cpa, field, len)==0 ) {
798                                 iv->fieldname[fieldnr]= cpa;
799                                 
800                                 /* lezen tot aan eerste karakter */
801                                 cpa+= len;
802                                 skipdata+= len;
803                                 *cpa= 0;
804                                 cpa++;
805                                 skipdata++;
806                                 
807                                 while( *cpa==32 || *cpa==13 || *cpa==10 || *cpa==9) cpa++;
808                                 if( *cpa=='[' ) {
809                                         terminator= ']';
810                                         cpa++;
811                                         skipdata++;
812                                 }
813                                 else terminator= 13;
814                                 
815                                 stackcount= 0;
816                                 fp= iv_data_stack;
817                                 
818                                 while( *cpa!=terminator && *cpa != '}' ) {
819                                         
820                                         /* in fact, isdigit should include the dot and minus */
821                                         if( (isdigit(*cpa) || *cpa=='.' || *cpa=='-') && (isspace(cpa[-1]) || cpa[-1]==0 || cpa[-1]==',') ) {
822                                                 if(cpa[1]=='x') {
823                                                         memcpy(str, cpa, 16);
824                                                         str[16]= 0;
825                                                         
826                                                         sscanf(str, "%x", (int *)fp);
827                                                 }
828                                                 else {
829                                                         /* atof doesn't stop after the first float
830                                                          * in a long string at Windows... so we copy 
831                                                          * the float to a new string then atof... */
832                                                          
833                                                         i=strpbrk(cpa, ", \n")-cpa;
834                                                         
835                                                         if (i>63) *fp= 0.0;
836                                                         else {
837                                                                 memcpy(str, cpa, i);
838                                                                 str[i]=0;
839                                                         
840                                                                 *fp= (float) atof(str);
841                                                         }
842                                                 }
843                                                                                                 
844                                                 stackcount++;
845                                                 if(stackcount>=IV_MAXSTACK) {
846                                                         printf("stackoverflow in IV read\n");
847                                                         break;
848                                                 }
849                                                 fp++;
850                                         }
851                                         cpa++;
852                                         skipdata++;
853                                 }
854                                 
855                                 iv->datalen[fieldnr]= stackcount;
856                                 if(stackcount) {
857                                         iv->data[fieldnr]= MEM_mallocN(sizeof(float)*stackcount, "iv_finddata");
858                                         memcpy(iv->data[fieldnr], iv_data_stack, sizeof(float)*stackcount);
859                                 }
860                                 else iv->data[fieldnr]= 0;
861                                 
862                                 return skipdata;
863                         }
864                 }
865                 cpa++;
866                 skipdata++;
867         }
868         
869         return skipdata;
870 }
871
872 static void read_iv_index(float *data, float *baseadr, float *index, int nr, int coordtype)
873 {
874         /* in data schrijven: baseadr met offset index (aantal nr)*/
875         float *fp;
876         int ofs;
877         
878         while(nr--) {
879                 ofs= (int) *index;
880                 fp= baseadr+coordtype*ofs;
881                 VECCOPY(data, fp);
882                 data+= 3;
883                 index++;
884         }
885 }
886
887
888
889 static void read_inventor(char *str, struct ListBase *listb)
890 {
891         struct IvNode *iv, *ivp, *ivn;
892         char *maindata, *md, *cpa;
893         float *index, *data, *fp;
894         int file, filelen, count, lll, face, nr = 0;
895         int skipdata, ok, a, b, tot, first, colnr, coordtype, polytype, *idata;
896         struct DispList *dl;
897         
898         ivbase.first= ivbase.last= 0;
899         iv_curcol= 0;
900         ivsurf= 0;
901         
902         file= open(str, O_BINARY|O_RDONLY);
903         if(file== -1) {
904                 error("Can't read file\n");
905                 return;
906         }
907         filelen= BLI_filesize(file);
908         
909         maindata= MEM_mallocN(filelen, "leesInventor");
910         read(file, maindata, filelen);
911         close(file);
912
913         iv_data_stack= MEM_mallocN(sizeof(float)*IV_MAXSTACK, "ivstack");
914
915         /* eerste preprocess: commentar eruit */
916         md= maindata+20;
917         count= 20;
918         while(count<filelen) {
919                 if( *md=='#' ) {        /* comment */
920                         while( *md!=13 && *md!=10) {    /* enters */
921                                 *md= 32;
922                                 md++;
923                                 count++;
924                                 if(count>=filelen) break;
925                         }
926                 }
927                 md++;
928                 count++;        
929         }
930         
931
932         /* we gaan alles ordenen: welke zijn de nodes en de fields? */
933         md= maindata;
934         count= 0;
935         while(count<filelen) {
936                 if( *md=='{' ) {        /* terug lezen */
937                 
938                         cpa= md-1;
939                         while( *cpa==32 || *cpa==13 || *cpa==10 || *cpa==9) {   /* spaties/enters/tab weg */
940                                 *cpa= 0;
941                                 cpa--;
942                         }               
943                                 
944                         while( *cpa>32 && *cpa<128) cpa--;
945                         cpa++;
946                         *md= 0;
947                         
948                         ok= 0;
949                         skipdata= 0;
950                         iv= MEM_callocN(sizeof(struct IvNode), "leesInventor");
951                         iv->nodename= cpa;
952
953                         if(strcmp(cpa, "Coordinate3")==0 || strcmp(cpa, "Coordinate4")==0) {
954                                 skipdata= iv_finddata(iv, "point", 0);
955                                 ok= 1;
956                         }
957                         else if(strcmp(cpa, "VertexProperty")==0) {
958                                 skipdata= iv_finddata(iv, "vertex", 0);
959                                 ok= 1;
960                         }
961                         else if(strcmp(cpa, "IndexedLineSet")==0) {
962                                 skipdata= iv_finddata(iv, "coordIndex", 0);
963                                 ok= 1;
964                         }
965                         else if(strcmp(cpa, "IndexedTriangleMesh")==0) {
966                                 skipdata= iv_finddata(iv, "coordIndex", 0);
967                                 ok= 1;
968                         }
969                         else if(strcmp(cpa, "IndexedFaceSet")==0) {
970                                 skipdata= iv_finddata(iv, "coordIndex", 0);
971                                 ok= 1;
972                         }
973                         else if(strcmp(cpa, "FaceSet")==0) {
974                                 skipdata= iv_finddata(iv, "numVertices", 0);
975                                 ok= 1;
976                         }
977                         else if(strcmp(cpa, "Material")==0) {
978                                 iv_finddata(iv, "diffuseColor", 0);
979                                 iv_finddata(iv, "ambientColor", 1);
980                                 ok= 1;
981                         }
982                         else if(strcmp(cpa, "BaseColor")==0) {
983                                 iv_finddata(iv, "rgb", 0);
984                                 ok= 1;
985                         }
986                         else if(strcmp(cpa, "PackedColor")==0) {
987                                 iv_finddata(iv, "rgba", 0);
988                                 ok= 1;
989                         }
990                         else if(strcmp(cpa, "QuadMesh")==0) {
991                                 iv_finddata(iv, "verticesPerColumn", 0);
992                                 iv_finddata(iv, "verticesPerRow", 1);
993                                 
994                                 ok= 1;
995                         }
996                         else if(strcmp(cpa, "IndexedTriangleStripSet")==0) {
997                                 skipdata= iv_finddata(iv, "coordIndex", 0);
998                                 ok= 1;
999                         }
1000                         else if(strcmp(cpa, "TriangleStripSet")==0) {
1001                                 skipdata= iv_finddata(iv, "numVertices", 0);
1002                                 ok= 1;
1003                         }
1004                         else if(strcmp(cpa, "IndexedNurbsSurface")==0 || strcmp(cpa, "NurbsSurface")==0) {
1005                                 iv_finddata(iv, "numUControlPoints", 0);
1006                                 iv_finddata(iv, "numVControlPoints", 1);
1007                                 iv_finddata(iv, "uKnotVector", 2);
1008                                 iv_finddata(iv, "vKnotVector", 3);
1009                                 ok= 1;
1010                         }
1011                         else {
1012                                 /* naar 't einde */
1013                                 while( *md != '}') {
1014                                         md++;
1015                                         count++;
1016                                         if(count<filelen) break;
1017                                 }
1018                         }
1019                         
1020                         
1021                         if(ok) {
1022                                 BLI_addtail(&ivbase, iv);
1023                                 md+= skipdata;
1024                                 count+= skipdata;
1025                         }
1026                         else MEM_freeN(iv);
1027                         
1028                 }
1029                 md++;
1030                 count++;
1031         }
1032         
1033         /* nodes samenvoegen */
1034         iv= ivbase.first;
1035         
1036         while(iv) {
1037                 ivn= iv->next;
1038                 
1039                 if( strncmp(iv->nodename, "Indexed", 7)==0) {
1040                         /* terugzoeken: zelfde naam? */
1041                         
1042                         ivp= iv->prev;
1043                         while(ivp) {
1044                                 if(strcmp(iv->nodename, ivp->nodename)==0) break;
1045
1046                                 if(strcmp(ivp->nodename, "Coordinate3")==0 || 
1047                                    strcmp(ivp->nodename, "Coordinate4")==0 ||
1048                                    strcmp(ivp->nodename, "VertexProperty")==0) {
1049                                         ivp= 0;
1050                                         break;
1051                                 }
1052                                 ivp= ivp->prev;
1053                         }
1054                         
1055                         if(ivp) {
1056                                 /* iv bij ivp voegen */
1057                                 
1058                                 tot= iv->datalen[0] + ivp->datalen[0];
1059                                 if(tot) {
1060                                         data= MEM_mallocN(tot*sizeof(float), "samenvoeg iv");
1061                                         memcpy(data, ivp->data[0], sizeof(float)*ivp->datalen[0]);
1062                                         memcpy(data+ivp->datalen[0], iv->data[0], sizeof(float)*iv->datalen[0]);
1063                                         
1064                                         ivp->datalen[0]+= iv->datalen[0];
1065                                         MEM_freeN(ivp->data[0]);
1066                                         ivp->data[0]= data;
1067                                         
1068                                         BLI_remlink(&ivbase, iv);
1069                                         MEM_freeN(iv->data[0]);
1070                                         MEM_freeN(iv);
1071                                 }
1072                         }
1073                 }
1074                 
1075                 iv= ivn;
1076         }
1077
1078         
1079         /* Nodes omzetten naar DispLists */
1080         iv= ivbase.first;
1081         while(iv) {
1082                 
1083                 /* printf(" Node: %s\n", iv->nodename); */
1084                 /* if(iv->fieldname[0]) printf(" Field: %s len %d\n", iv->fieldname[0], iv->datalen[0]); */
1085                 coordtype= 3;
1086                 
1087                 if( strcmp(iv->nodename, "IndexedLineSet")==0 ) {
1088                         
1089                         colnr= iv_colornumber(iv);
1090
1091                         /* terugzoeken naar data */
1092                         ivp= iv;
1093                         while(ivp->prev) {
1094                                 ivp= ivp->prev;
1095                                 if( strcmp(ivp->nodename, "Coordinate3")==0 ) {
1096                                         coordtype= 3;
1097                                         break;
1098                                 }
1099                                 if( strcmp(ivp->nodename, "Coordinate4")==0 ) {
1100                                         coordtype= 4;
1101                                         break;
1102                                 }
1103                         }
1104                         if(ivp) {
1105                         
1106                                 /* tel het aantal lijnen */
1107                                 tot= 0;
1108                                 index= iv->data[0];
1109                                 lll = iv->datalen[0]-1;
1110                                 for(a=0; a<lll; a++) {
1111                                         if(index[0]!= -1 && index[1]!= -1) tot++;
1112                                         index++;
1113                                 }
1114                                 
1115                                 tot*= 2;        /* aantal vertices */
1116                                 dl= MEM_callocN(sizeof(struct DispList)+tot*3*sizeof(float), "leesInventor1");
1117                                 BLI_addtail(listb, dl);
1118                                 dl->type= DL_SEGM;
1119                                 dl->nr= 2;
1120                                 dl->parts= tot/2;
1121                                 dl->col= colnr;
1122                                 data= (float *)(dl+1);
1123                                 
1124                                 index= iv->data[0];
1125                                 for(a=0; a<lll; a++) {
1126                                         if(index[0]!= -1 && index[1]!= -1) {
1127                                                 read_iv_index(data, ivp->data[0], index, 2, coordtype);
1128                                                 data+= 6;
1129                                         }
1130                                         index++;
1131                                 }
1132                         }
1133                 }
1134                 else if( strcmp(iv->nodename, "FaceSet")==0 ) {
1135                         
1136                         colnr= iv_colornumber(iv);
1137                 
1138                         /* terugzoeken naar data */
1139                         ivp= iv;
1140                         while(ivp->prev) {
1141                                 ivp= ivp->prev;
1142                                 if( strcmp(ivp->nodename, "Coordinate3")==0 ) {
1143                                         coordtype= 3;
1144                                         break;
1145                                 }
1146                                 if( strcmp(ivp->nodename, "Coordinate4")==0 ) {
1147                                         coordtype= 4;
1148                                         break;
1149                                 }
1150                         }
1151                         
1152                         if(ivp) {
1153                                 /* tel het aantal driehoeken */
1154                                 tot= 0;
1155                                 
1156                                 index= iv->data[0];
1157                                 polytype= (int) index[0];
1158                                 
1159                                 for(a=0; a<iv->datalen[0]; a++) {
1160                                         if(index[0]== polytype) tot++;  /* een soort? */
1161                                         index++;
1162                                 }
1163                                 
1164                                 
1165                                 tot*= polytype;         /* aantal vertices */
1166                                 dl= MEM_callocN(sizeof(struct DispList)+tot*3*sizeof(float), "leesInventor4");
1167                                 BLI_addtail(listb, dl);
1168                                 dl->type= DL_POLY;
1169                                 dl->nr= polytype;
1170                                 dl->parts= tot/polytype;
1171                                 dl->col= colnr;
1172                                 data= (float *)(dl+1);
1173
1174                                 index= ivp->data[0];
1175                                 first= 1;
1176                                 for(a=0; a<iv->datalen[0]; a++) {
1177                                         
1178                                         VECCOPY(data, index);
1179                                         data+= 3;
1180                                         index+= 3;
1181
1182                                         VECCOPY(data, index);
1183                                         data+= 3;
1184                                         index+= 3;
1185
1186                                         VECCOPY(data, index);
1187                                         data+= 3;
1188                                         index+= 3;
1189
1190                                         if(polytype==4) {
1191                                                 VECCOPY(data, index);
1192                                                 data+= 3;
1193                                                 index+= 3;
1194                                         }
1195                                 }
1196                         }
1197                 }
1198                 else if( strcmp(iv->nodename, "TriangleStripSet")==0 ) {
1199                         
1200                         colnr= iv_colornumber(iv);
1201                 
1202                         /* terugzoeken naar data */
1203                         ivp= iv;
1204                         while(ivp->prev) {
1205                                 ivp= ivp->prev;
1206                                 if( strcmp(ivp->nodename, "Coordinate3")==0 ) {
1207                                         coordtype= 3;
1208                                         break;
1209                                 }
1210                                 if( strcmp(ivp->nodename, "Coordinate4")==0 ) {
1211                                         coordtype= 4;
1212                                         break;
1213                                 }
1214                         }
1215                         
1216                         if(ivp) {
1217                                 /* tel het aantal driehoeken */
1218                                 tot= 0;
1219                                 face= 0;
1220                                 
1221                                 index= iv->data[0];             /* afmeting strip */ 
1222                                 
1223                                 for(a=0; a<iv->datalen[0]; a++) {
1224                                         tot+= (int) index[0];
1225                                         face+= ((int) index[0]) - 2;
1226                                         index++;
1227                                 }
1228                                 
1229                                 dl= MEM_callocN(sizeof(struct DispList), "leesInventor4");
1230                                 dl->verts= MEM_callocN( tot*3*sizeof(float), "dl verts");
1231                                 dl->index= MEM_callocN( face*3*sizeof(int), "dl index");
1232                                 
1233                                 dl->type= DL_INDEX3;
1234                                 dl->nr= tot;
1235                                 dl->parts= face;
1236
1237                                 BLI_addtail(listb, dl);
1238                                 dl->col= colnr;
1239
1240                                 index= iv->data[0];             /* afmeting strip */ 
1241                                 fp= ivp->data[0];               /* vertices */
1242                                 data= dl->verts;
1243                                 idata= dl->index;
1244                                 first= 0;
1245                                 
1246                                 for(a=0; a<iv->datalen[0]; a++) {
1247                                         
1248                                         /* vertices */
1249                                         for(b=0; b<index[0]; b++) {
1250                                                 VECCOPY(data, fp);
1251                                                 data+= 3; 
1252                                                 fp+= coordtype;
1253                                         }
1254                                                 
1255                                         /* indices */
1256                                         lll = index[0] - 2;
1257                                         for(b=0; b<lll; b++) {
1258                                                 idata[0]= first;
1259                                                 idata[1]= first+1;
1260                                                 idata[2]= first+2;
1261                                                 first++;
1262                                                 idata+= 3;
1263                                         }
1264                                         first+= 2;
1265                                         
1266                                         index++;
1267                                 }
1268                         }
1269                 }
1270                 else if( strcmp(iv->nodename, "IndexedFaceSet")==0 ) {
1271                         
1272                         colnr= iv_colornumber(iv);
1273                 
1274                         /* terugzoeken naar data */
1275                         ivp= iv;
1276                         while(ivp->prev) {
1277                                 ivp= ivp->prev;
1278                                 if( strcmp(ivp->nodename, "Coordinate3")==0 ) {
1279                                         coordtype= 3;
1280                                         break;
1281                                 }
1282                                 if( strcmp(ivp->nodename, "Coordinate4")==0 ) {
1283                                         coordtype= 4;
1284                                         break;
1285                                 }
1286                         }
1287                         if(ivp) {
1288                         
1289                                 /* tel het aantal driehoeken */
1290                                 face= 0;
1291                                 index= iv->data[0];
1292                                 lll = iv->datalen[0]-2;
1293                                 for(a=0; lll; a++) {
1294                                         if(index[0]!= -1 && index[1]!= -1 && index[2]!= -1) face++;
1295                                         index++;
1296                                 }
1297
1298                                 /* aantal vertices */
1299                                 tot= ivp->datalen[0]/coordtype;
1300
1301                                 if(tot) {
1302                                         dl= MEM_callocN(sizeof(struct DispList), "leesInventor5");
1303                                         BLI_addtail(listb, dl);
1304                                         dl->type= DL_INDEX3;
1305                                         dl->nr= tot;
1306                                         dl->parts= face;
1307                                         dl->col= colnr;
1308         
1309                                         dl->verts= MEM_callocN( tot*3*sizeof(float), "dl verts");
1310                                         dl->index= MEM_callocN(sizeof(int)*3*face, "dl index");
1311         
1312                                         /* vertices */
1313                                         fp= ivp->data[0];
1314                                         data= dl->verts;
1315                                         for(b=tot; b>0; b--) {
1316                                                 VECCOPY(data, fp);
1317                                                 data+= 3; 
1318                                                 fp+= coordtype;
1319                                         }
1320                                         
1321                                         /* indices */
1322                                         index= iv->data[0];
1323                                         idata= dl->index;
1324                                         first= 1;
1325                                         lll=iv->datalen[0]-2;
1326                                         for(a=0; a<lll; a++) {
1327                                                 
1328                                                 if(index[0]!= -1 && index[1]!= -1 && index[2]!= -1) {
1329         
1330                                                         /* deze truuk is om poly's met meer dan 3 vertices correct te vullen */
1331                                                         if(first) {
1332                                                                 nr= (int) index[0];
1333                                                                 first= 0;
1334                                                         }
1335                                                         idata[0]= nr;
1336                                                         idata[1]= (int) index[1];
1337                                                         idata[2]= (int) index[2];
1338                                                         idata+= 3;
1339                                                 }
1340                                                 else first= 1;
1341                                                 
1342                                                 index++;
1343                                         }
1344                                 }
1345                         }
1346                 }
1347                 else if( strcmp(iv->nodename, "IndexedTriangleMesh")==0 || 
1348                                  strcmp(iv->nodename, "IndexedTriangleStripSet")==0 ) {
1349                         
1350                         colnr= iv_colornumber(iv);
1351                 
1352                         /* terugzoeken naar data */
1353                         ivp= iv;
1354                         while(ivp->prev) {
1355                                 ivp= ivp->prev;
1356                                 if( strcmp(ivp->nodename, "Coordinate3")==0 ) {
1357                                         coordtype= 3;
1358                                         break;
1359                                 }
1360                                 if( strcmp(ivp->nodename, "Coordinate4")==0 ) {
1361                                         coordtype= 4;
1362                                         break;
1363                                 }
1364                         }
1365                         if(ivp) {
1366                         
1367                                 /* tel het aantal driehoeken */
1368                                 face= 0;
1369                                 index= iv->data[0];
1370                                 lll=iv->datalen[0]-2;
1371                                 for(a=0; a<lll; a++) {
1372                                         if(index[0]!= -1 && index[1]!= -1 && index[2]!= -1) face++;
1373                                         index++;
1374                                 }
1375                                 
1376                                 /* aantal vertices */
1377                                 tot= ivp->datalen[0]/coordtype;
1378                                 
1379                                 dl= MEM_callocN(sizeof(struct DispList), "leesInventor6");
1380                                 BLI_addtail(listb, dl);
1381                                 dl->type= DL_INDEX3;
1382                                 dl->nr= tot;
1383                                 dl->parts= face;
1384                                 dl->col= colnr;
1385                                 
1386                                 dl->verts= MEM_callocN( tot*3*sizeof(float), "dl verts");
1387                                 dl->index= MEM_callocN(sizeof(int)*3*face, "dl index");
1388
1389                                 /* vertices */
1390                                 fp= ivp->data[0];
1391                                 data= dl->verts;
1392                                 for(b=tot; b>0; b--) {
1393                                         VECCOPY(data, fp);
1394                                         data+= 3; 
1395                                         fp+= coordtype;
1396                                 }
1397                                 
1398                                 /* indices */
1399                                 index= iv->data[0];
1400                                 idata= dl->index;
1401                                 
1402                                 lll=iv->datalen[0]-2;
1403                                 for(a=lll; a>0; a--) {
1404                                 
1405                                         if(index[0]!= -1 && index[1]!= -1 && index[2]!= -1) {
1406                                                 idata[0]= (int) index[0];
1407                                                 idata[1]= (int) index[1];
1408                                                 idata[2]= (int) index[2];
1409                                                 idata+= 3;
1410                                         }
1411                                         index++;
1412                                 }
1413                         }
1414                 }
1415                 else if( strcmp(iv->nodename, "QuadMesh")==0 ) {
1416                         
1417                         colnr= iv_colornumber(iv);
1418                 
1419                         /* terugzoeken naar data */
1420                         ivp= iv;
1421                         while(ivp->prev) {
1422                                 ivp= ivp->prev;
1423                                 if( strcmp(ivp->nodename, "Coordinate3")==0 ) {
1424                                         coordtype= 3;
1425                                         break;
1426                                 }
1427                                 if( strcmp(ivp->nodename, "VertexProperty")==0 ) {
1428                                         coordtype= 3;
1429                                         break;
1430                                 }
1431                                 if( strcmp(ivp->nodename, "Coordinate4")==0 ) {
1432                                         coordtype= 4;
1433                                         break;
1434                                 }
1435                         }
1436                         
1437                         if(ivp) {
1438                                 tot= (int) (floor(*(iv->data[0])+0.5) * floor(*(iv->data[1])+0.5));
1439
1440                                 if(tot>0) {
1441                                         dl= MEM_callocN(sizeof(struct DispList)+tot*3*sizeof(float), "leesInventor8");
1442                                         BLI_addtail(listb, dl);
1443                                         dl->type= DL_SURF;
1444                                         dl->parts= (int) floor(*(iv->data[0])+0.5);
1445                                         dl->nr= (int) floor(*(iv->data[1])+0.5);
1446                                         dl->col= colnr;
1447                                         data= (float *)(dl+1);
1448                                         memcpy(data, ivp->data[0], tot*3*sizeof(float));
1449                                 }
1450                         }
1451                 }
1452                 else if(strcmp(iv->nodename, "IndexedNurbsSurface")==0 || strcmp(iv->nodename, "NurbsSurface")==0) {
1453                         
1454                         colnr= iv_colornumber(iv);
1455                 
1456                         /* terugzoeken naar data */
1457                         ivp= iv;
1458                         while(ivp->prev) {
1459                                 ivp= ivp->prev;
1460                                 if( strcmp(ivp->nodename, "Coordinate3")==0 ) {
1461                                         coordtype= 3;
1462                                         break;
1463                                 }
1464                                 if( strcmp(ivp->nodename, "Coordinate4")==0 ) {
1465                                         coordtype= 4;
1466                                         break;
1467                                 }
1468                         }
1469                         if(ivp) {
1470                                 a= (int) *(iv->data[0]);
1471                                 b= (int) *(iv->data[1]);
1472                                 
1473                                 tot= a*b;
1474
1475                                 if( (a>=4 || b>=4) && tot>6) {
1476                                         Object *ob;
1477                                         Curve *cu;
1478                                         Nurb *nu;
1479                                         BPoint *bp;
1480                                         
1481                                         if(ivsurf==0) {
1482                                                 ob= add_object(OB_SURF);
1483                                                 ivsurf= ob;
1484                                         }
1485                                         else ob= ivsurf;
1486                                         cu= ob->data;
1487                                         nu = (Nurb*) MEM_callocN(sizeof(Nurb),"addNurbprim") ;
1488                                         BLI_addtail(&cu->nurb, nu);
1489                                         nu->type= CU_NURBS;
1490
1491                                         nu->pntsu= a;
1492                                         nu->pntsv= b;
1493                                         nu->resolu= 2*a;
1494                                         nu->resolv= 2*b;
1495
1496                                         nu->flagu= 0;
1497                                         nu->flagv= 0;
1498                                         
1499                                         nu->bp = bp =
1500                                                 (BPoint*)MEM_callocN(tot * sizeof(BPoint), "addNurbprim3");
1501                                         a= tot;
1502                                         data= ivp->data[0];
1503                                         while(a--) {
1504                                                 VECCOPY(bp->vec, data);
1505                                                 if(coordtype==4) {
1506                                                         bp->vec[3]= data[3];
1507                                                         VecMulf(bp->vec, 1.0f/data[3]);
1508                                                 }
1509                                                 else bp->vec[3]= 1.0;
1510                                                 data+= coordtype;
1511                                                 bp++;
1512                                         }
1513                                         
1514                                         /* iv->datalen[2] / [3] is aantal knots */
1515                                         nu->orderu= iv->datalen[2] - nu->pntsu;
1516                                         nu->orderv= iv->datalen[3] - nu->pntsv;
1517                                         
1518                                         nu->knotsu= MEM_mallocN( sizeof(float)*(iv->datalen[2]), "knots");
1519                                         memcpy(nu->knotsu, iv->data[2], sizeof(float)*(iv->datalen[2]));
1520                                         nu->knotsv= MEM_mallocN( sizeof(float)*(iv->datalen[3]), "knots");
1521                                         memcpy(nu->knotsv, iv->data[3], sizeof(float)*(iv->datalen[3]));                                        
1522
1523                                         switchdirectionNurb(nu);
1524
1525                                 }
1526                                 else {
1527                                         dl= MEM_callocN(sizeof(struct DispList)+tot*3*sizeof(float), "leesInventor3");
1528                                         BLI_addtail(listb, dl);
1529                                         dl->type= DL_SURF;
1530                                         dl->nr= (int) *(iv->data[0]);
1531                                         dl->parts= (int) *(iv->data[1]);
1532                                         dl->col= colnr;
1533                                         data= (float *)(dl+1);
1534                                         
1535                                         a= tot;
1536                                         fp= ivp->data[0];
1537                                         while(a--) {
1538                                                 VECCOPY(data, fp);
1539                                                 fp+= coordtype;
1540                                                 data+= 3;
1541                                         }
1542                                 }
1543                         }
1544                 }
1545                 iv= iv->next;
1546         }
1547
1548         /* vrijgeven */
1549         iv= ivbase.first;
1550         while(iv) {
1551                 for(a=0; a<IV_MAXFIELD; a++) {
1552                         if(iv->data[a]) MEM_freeN(iv->data[a]);
1553                 }
1554                 iv= iv->next;
1555         }
1556
1557         BLI_freelistN(&ivbase);
1558         MEM_freeN(maindata);
1559         MEM_freeN(iv_data_stack);
1560         
1561 }
1562
1563 /* ************************************************************ */
1564
1565 static void displist_to_mesh(DispList *dlfirst)
1566 {
1567         Object *ob;
1568         Mesh *me;
1569         Material *ma;
1570         DispList *dl;
1571         MVert *mvert;
1572         MFace *mface;
1573         float *data, vec[3], min[3], max[3];
1574         int a, b, startve, *idata, totedge=0, tottria=0, totquad=0, totvert=0, totvlak, totcol=0, colnr;
1575         int p1, p2, p3, p4;
1576                 
1577         /* eerst tellen */
1578         INIT_MINMAX(min, max);
1579
1580         dl= dlfirst;
1581         while(dl) {
1582         
1583                 /* PATCH 1 (polyfill) kan hier niet, er wordt geen listbase meegegeven. eerder doen! */
1584                 /* PATCH 2 */
1585                 if(dl->type==DL_SEGM && dl->nr>2) {
1586                         data= (float *)(dl+1);
1587                         if(data[0]==data[3*(dl->nr-1)]) {
1588                                 if(data[1]==data[3*(dl->nr-1)+1]) {
1589                                         if(data[2]==data[3*(dl->nr-1)+2]) {
1590                                                 dl->type= DL_POLY;
1591                                                 dl->nr--;
1592                                         }
1593                                 }
1594                         }
1595                 }
1596                 
1597                 /* kleuren */
1598                 if(dl->col > totcol) totcol= dl->col;
1599                 
1600                 /* afmeting en tellen */
1601                 if(dl->type==DL_SURF) {
1602                         a= dl->nr;
1603                         b= dl->parts;
1604                         if(dl->flag & 1) a++;
1605                         if(dl->flag & 2) b++;
1606                         
1607                         totquad+= a*b;
1608
1609                         totvert+= dl->nr*dl->parts;
1610
1611                         data= (float *)(dl+1);
1612                         for(a= dl->nr*dl->parts; a>0; a--) {
1613                                 DO_MINMAX(data, min, max);
1614                                 data+= 3;
1615                         }
1616                 }
1617                 else if(dl->type==DL_POLY) {
1618                         if(dl->nr==3 || dl->nr==4) {
1619                                 if(dl->nr==3) tottria+= dl->parts;
1620                                 else totquad+= dl->parts;
1621                                 
1622                                 totvert+= dl->nr*dl->parts;
1623
1624                                 data= (float *)(dl+1);
1625                                 for(a= dl->nr*dl->parts; a>0; a--) {
1626                                         DO_MINMAX(data, min, max);
1627                                         data+= 3;
1628                                 }
1629                         }
1630                         else if(dl->nr>4) {
1631                                 
1632                                 tottria+= dl->nr*dl->parts;
1633                                 totvert+= dl->nr*dl->parts;
1634                                 
1635                                 data= (float *)(dl+1);
1636                                 for(a= dl->nr*dl->parts; a>0; a--) {
1637                                         DO_MINMAX(data, min, max);
1638                                         data+= 3;
1639                                 }
1640                                 
1641                         }
1642                 }
1643                 else if(dl->type==DL_INDEX3) {
1644                         tottria+= dl->parts;
1645                         totvert+= dl->nr;
1646                         
1647                         data= dl->verts;
1648                         for(a= dl->nr; a>0; a--) {
1649                                 DO_MINMAX(data, min, max);
1650                                 data+= 3;
1651                         }
1652                 }
1653                 else if(dl->type==DL_SEGM) {
1654                         
1655                         tottria+= (dl->nr-1)*dl->parts;
1656                         totvert+= dl->nr*dl->parts;
1657                         
1658                         data= (float *)(dl+1);
1659                         for(a= dl->nr*dl->parts; a>0; a--) {
1660                                 DO_MINMAX(data, min, max);
1661                                 data+= 3;
1662                         }
1663                 }
1664
1665                 dl= dl->next;
1666         }
1667
1668         if(totvert==0) {
1669                 return;
1670         }
1671         if(totvert>=65000 || tottria>=65000) {
1672                 if (totvert>=65000) {
1673                         error("Too many vertices (%d)", totvert);
1674                 } else {
1675                         error("Too many faces (%d)", tottria);
1676                 }
1677
1678                 return;
1679         }
1680         
1681         if(totcol>16) {
1682                 error("Found more than 16 different colors");
1683                 totcol= 16;
1684         }
1685
1686         vec[0]= (min[0]+max[0])/2;
1687         vec[1]= (min[1]+max[1])/2;
1688         vec[2]= (min[2]+max[2])/2;
1689
1690         ob= add_object(OB_MESH);
1691         VECCOPY(ob->loc, vec);
1692         where_is_object(ob);
1693
1694         me= ob->data;
1695         
1696         /* kleuren */
1697         if(totcol) {
1698                 ob->mat= MEM_callocN(sizeof(void *)*totcol, "ob->mat");
1699                 me->mat= MEM_callocN(sizeof(void *)*totcol, "me->mat");
1700                 me->totcol= totcol;
1701                 ob->totcol= (unsigned char) me->totcol;
1702                 ob->actcol= 1;
1703         }
1704         
1705         /* materialen */
1706         for(a=0; a<totcol; a++) {
1707                 ma= G.main->mat.first;
1708                 while(ma) {
1709                         if(ma->mtex[0]==0) {
1710                                 if(ivcolors[a][0]==ma->r && ivcolors[a][1]==ma->g && ivcolors[a][2]==ma->b) {
1711                                         me->mat[a]= ma;
1712                                         ma->id.us++;
1713                                         break;
1714                                 }
1715                         }
1716                         ma= ma->id.next;
1717                 }
1718                 if(ma==0) {
1719                         ma= add_material("ext");
1720                         me->mat[a]= ma;
1721                         ma->r= ivcolors[a][0];
1722                         ma->g= ivcolors[a][1];
1723                         ma->b= ivcolors[a][2];
1724                         automatname(ma);
1725                 }
1726         }
1727         
1728         totvlak= totquad+tottria+totedge;
1729
1730         printf("Import: %d vertices %d faces\n", totvert, totvlak);
1731         
1732         if(totvert) me->mvert= MEM_callocN(totvert*sizeof(MVert), "mvert");
1733         if(totvlak) me->mface= MEM_callocN(totvlak*sizeof(MFace), "mface");
1734         me->totvert= totvert;
1735         me->totface= totvlak;
1736         
1737         mvert= me->mvert;
1738         mface= me->mface;
1739
1740         startve= 0;
1741
1742         dl= dlfirst;
1743         while(dl) {
1744                 
1745                 colnr= (dl->col>15 ? 15: dl->col);
1746                 if(colnr) colnr--;
1747                 
1748                 if(dl->type==DL_SURF) {
1749                         data= (float *)(dl+1);
1750
1751                         for(a=dl->parts*dl->nr; a>0; a--) {
1752                                 mvert->co[0]= data[0] -vec[0];
1753                                 mvert->co[1]= data[1] -vec[1];
1754                                 mvert->co[2]= data[2] -vec[2];
1755                                 
1756                                 data+=3;
1757                                 mvert++;
1758                         }
1759
1760                         for(a=0; a<dl->parts; a++) {
1761
1762                                 DL_SURFINDEX(dl->flag & 1, dl->flag & 2, dl->nr, dl->parts);
1763                                 p1+= startve; 
1764                                 p2+= startve; 
1765                                 p3+= startve; 
1766                                 p4+= startve;
1767
1768                                 for(; b<dl->nr; b++) {
1769                                 
1770                                         mface->v1= p1;
1771                                         mface->v2= p2;
1772                                         mface->v3= p4;
1773                                         mface->v4= p3;
1774                                         
1775                                         mface->mat_nr= colnr;
1776                                         test_index_mface(mface, 4);
1777                                         
1778                                         mface++;
1779                                         
1780                                         p4= p3; 
1781                                         p3++;
1782                                         p2= p1; 
1783                                         p1++;
1784                                 }
1785                         }
1786                         
1787                         startve += dl->parts*dl->nr;
1788
1789                 }
1790                 else if(dl->type==DL_POLY) {
1791                 
1792                         if(dl->nr==3 || dl->nr==4) {
1793                                 data= (float *)(dl+1);
1794
1795                                 for(a=dl->parts*dl->nr; a>0; a--) {
1796                                         mvert->co[0]= data[0] -vec[0];
1797                                         mvert->co[1]= data[1] -vec[1];
1798                                         mvert->co[2]= data[2] -vec[2];
1799                                         data+=3;
1800                                         mvert++;
1801                                 }
1802
1803                                 for(a=0; a<dl->parts; a++) {
1804                                         if(dl->nr==3) {
1805                                                 mface->v1= startve+a*dl->nr;
1806                                                 mface->v2= startve+a*dl->nr+1;
1807                                                 mface->v3= startve+a*dl->nr+2;
1808                                                 mface->mat_nr= colnr;
1809                                                 test_index_mface(mface, 3);
1810                                                 mface++;
1811                                         }
1812                                         else {
1813                                                 mface->v1= startve+a*dl->nr;
1814                                                 mface->v2= startve+a*dl->nr+1;
1815                                                 mface->v3= startve+a*dl->nr+2;
1816                                                 mface->v4= startve+a*dl->nr+3;
1817                                                 mface->mat_nr= colnr;
1818                                                 test_index_mface(mface, 4);
1819                                                 mface++;
1820                                         }
1821                                 }
1822                                 startve += dl->parts*dl->nr;
1823                         }
1824                         else if(dl->nr>4) {
1825                                 data= (float *)(dl+1);
1826
1827                                 for(a=dl->parts*dl->nr; a>0; a--) {
1828                                         mvert->co[0]= data[0] -vec[0];
1829                                         mvert->co[1]= data[1] -vec[1];
1830                                         mvert->co[2]= data[2] -vec[2];
1831                                         
1832                                         data+=3;
1833                                         mvert++;
1834                                 }
1835
1836                                 for(b=0; b<dl->parts; b++) {
1837                                         for(a=0; a<dl->nr; a++) {
1838                                                 mface->v1= startve+a;
1839                                                 
1840                                                 if(a==dl->nr-1) mface->v2= startve;
1841                                                 else mface->v2= startve+a+1;
1842                                                 
1843                                                 mface->mat_nr= colnr;
1844                                                 test_index_mface(mface, 2);
1845
1846                                                 mface++;
1847                                         }
1848                                         startve += dl->nr;
1849                                 }
1850                         }
1851                 }
1852                 else if(dl->type==DL_INDEX3) {
1853                         data= dl->verts;
1854                         
1855                         for(a=dl->nr; a>0; a--) {
1856                                 mvert->co[0]= data[0] -vec[0];
1857                                 mvert->co[1]= data[1] -vec[1];
1858                                 mvert->co[2]= data[2] -vec[2];
1859                                 data+=3;
1860                                 mvert++;
1861                         }
1862
1863                         idata= dl->index;
1864                         for(b=dl->parts; b>0; b--) {
1865                                 mface->v1= startve+idata[0];
1866                                 mface->v2= startve+idata[1];
1867                                 mface->v3= startve+idata[2];
1868                                 mface->mat_nr= colnr;
1869                                 
1870                                 if (mface->v1>me->totvert-1) mface->v1= me->totvert-1;
1871                                 if (mface->v2>me->totvert-1) mface->v2= me->totvert-1;
1872                                 if (mface->v3>me->totvert-1) mface->v3= me->totvert-1;
1873
1874                                 test_index_mface(mface, 3);
1875                                 mface++;
1876                                 idata+= 3;
1877                         }
1878                         startve += dl->nr;
1879                 }
1880                 else if(dl->type==DL_SEGM) {
1881                         data= (float *)(dl+1);
1882
1883                         for(a=dl->parts*dl->nr; a>0; a--) {
1884                                 mvert->co[0]= data[0] -vec[0];
1885                                 mvert->co[1]= data[1] -vec[1];
1886                                 mvert->co[2]= data[2] -vec[2];
1887                                 data+=3;
1888                                 mvert++;
1889                         }
1890
1891                         for(b=0; b<dl->parts; b++) {
1892                                 for(a=0; a<dl->nr-1; a++) {
1893                                         mface->v1= startve+a;
1894                                         mface->v2= startve+a+1;
1895                                         mface->mat_nr= colnr;
1896                                         test_index_mface(mface, 2);
1897                                         mface++;
1898                                 }
1899                                 startve += dl->nr;
1900                         }
1901                 }
1902                 dl= dl->next;
1903         }
1904
1905         G.obedit= ob;
1906         make_editMesh();
1907         load_editMesh();
1908         free_editMesh();
1909         
1910         G.obedit= 0;
1911         tex_space_mesh(me);
1912
1913 }
1914
1915 static void displist_to_objects(ListBase *lbase)
1916 {
1917         DispList *dl, *first, *prev, *next;
1918         ListBase tempbase;
1919         int maxaantal, curcol, totvert=0, vert;
1920         
1921         /* eerst dit: is nu nog actief */
1922         if(ivsurf) {
1923                 where_is_object(ivsurf);
1924                 docentre_new();
1925         }
1926
1927         dl= lbase->first;
1928         while(dl) {
1929                 next= dl->next;
1930                 
1931                 /* PATCH 1: polyfill */
1932                 if(dl->type==DL_POLY && dl->nr>4) {
1933                         /* oplossing: bij elkaar in aparte listbase zetten */
1934                         ;
1935                 }
1936                 /* PATCH 2: poly's van 2 punten */
1937                 if(dl->type==DL_POLY && dl->nr==2) dl->type= DL_SEGM;
1938                 
1939                 dl= next;
1940         }
1941
1942         /* vertices tellen */
1943
1944         dl= lbase->first;
1945         while(dl) {
1946
1947                 if(dl->type==DL_SURF) totvert+= dl->nr*dl->parts;
1948                 else if(dl->type==DL_POLY) {
1949                         if(dl->nr==3 || dl->nr==4) totvert+= dl->nr*dl->parts;
1950                         else if(dl->nr>4) totvert+= dl->nr*dl->parts;
1951                 }
1952                 else if(dl->type==DL_INDEX3) totvert+= dl->nr;
1953                 else if(dl->type==DL_SEGM) totvert+= dl->nr*dl->parts;
1954
1955                 dl= dl->next;
1956         }
1957
1958         if(totvert==0) {
1959                 
1960                 if(ivsurf==0) error("Found no data");
1961                 if(lbase->first) BLI_freelistN(lbase);
1962                 
1963                 return;
1964         }
1965
1966         maxaantal= 32000;
1967         
1968         if(totvert>maxaantal) {
1969         
1970                 /* probeer kleuren bij elkaar te zetten */
1971                 curcol= 0;
1972                 tempbase.first= tempbase.last= 0;
1973
1974                 while(lbase->first) {
1975                         dl= lbase->first;
1976                         while(dl) {
1977                                 next= dl->next;
1978                                 if(dl->col==curcol) {
1979                                         BLI_remlink(lbase, dl);
1980                                         BLI_addtail(&tempbase, dl);
1981                                         dl->col= 0;
1982                                 }
1983                                 
1984                                 dl= next;
1985                         }
1986                         
1987                         /* in tempbase zitten alle kleuren 'curcol' */
1988                         totvert= 0;
1989                         dl= first= tempbase.first;
1990                         while(dl) {
1991                                 vert= 0;
1992                                 
1993                                 if(dl->type==DL_SURF) vert= dl->nr*dl->parts;
1994                                 else if(dl->type==DL_POLY) {
1995                                         if(dl->nr==3 || dl->nr==4) vert= dl->nr*dl->parts;
1996                                         else if(dl->nr>4) vert= dl->nr*dl->parts;
1997                                 }
1998                                 else if(dl->type==DL_INDEX3) totvert+= dl->nr;
1999                                 else if(dl->type==DL_SEGM) vert= dl->nr*dl->parts;
2000                                 
2001                                 totvert+= vert;
2002                                 if(totvert > maxaantal || dl->next==0) {
2003                                         if(dl->next==0) {
2004                                                 displist_to_mesh(first);
2005                                         }
2006                                         else if(dl->prev) {
2007                                                 prev= dl->prev;
2008                                                 prev->next= 0;
2009                                                 displist_to_mesh(first);
2010                                                 prev->next= dl;
2011                                                 first= dl;
2012                                                 totvert= 0;
2013                                         }
2014                                 }
2015                                 
2016                                 dl= dl->next;
2017                         }
2018                         
2019                         freedisplist(&tempbase);
2020                         
2021                         curcol++;
2022                 }
2023         }
2024         else displist_to_mesh(lbase->first);
2025
2026         freedisplist(lbase);
2027
2028 }
2029
2030 int BKE_read_exotic(char *name)
2031 {
2032         ListBase lbase={0, 0};
2033         int file, len;
2034         char str[32];
2035         int *s0 = (int*) str;
2036         int retval = 0;
2037
2038         // make sure we're not trying to read a directory....
2039
2040         len= strlen(name);
2041         if (name[len-1] !='/' && name[len-1] != '\\') {
2042                 file = open(name, O_BINARY|O_RDONLY);
2043
2044                 if (file <= 0) {
2045                         error("Can't open file: %s", name);
2046                 } else {
2047                         read(file, str, 31);
2048                         close(file);
2049
2050                         if ((*s0 != FORM) && (strncmp(str, "BLEN", 4) != 0)) {
2051
2052                                 waitcursor(1);
2053                                 
2054                                 if(*s0==GOUR) {
2055                                         if(G.obedit) {
2056                                                 error("Unable to perform function in EditMode");
2057                                         } else {
2058                                                 read_radiogour(name);
2059                                                 retval = 1;
2060                                         }
2061                                 }
2062                                 else if ELEM4(*s0, DDG1, DDG2, DDG3, DDG4) {
2063                                         if(G.obedit) {
2064                                                 error("Unable to perform function in EditMode");
2065                                         } else {
2066                                                 read_videoscape(name);
2067                                                 retval = 1;
2068                                         }
2069                                 }
2070                                 else if(strncmp(str, "#Inventor V1.0", 14)==0) {
2071                                         if( strncmp(str+15, "ascii", 5)==0) {
2072                                                 read_inventor(name, &lbase);
2073                                                 displist_to_objects(&lbase);                            
2074                                                 retval = 1;
2075                                         } else {
2076                                                 error("Can only read Inventor 1.0 ascii");
2077                                         }
2078                                 }
2079                                 else if((strncmp(str, "#VRML V1.0 asc", 14)==0)) {
2080                                         read_inventor(name, &lbase);
2081                                         displist_to_objects(&lbase);                            
2082                                         retval = 1;
2083                                 }
2084                                 else if(is_dxf(name)) {
2085                                         dxf_read(name);
2086                                         retval = 1;
2087                                 }
2088                                 // TODO: this should not be in the kernel...
2089                                 else { // unknown format, call Python importloader 
2090                                         if (BPY_call_importloader(name)) {
2091                                                 retval = 1;
2092                                         } else {        
2093                                                 error("Unknown file type or error, check console");
2094                                         }       
2095                                 
2096                                 }       
2097                                 waitcursor(0);
2098                         }
2099                 }
2100         }
2101
2102         if (retval == 1) {
2103                 strcpy(G.sce, name);
2104         }
2105
2106         return (retval);
2107 }
2108
2109
2110 /* ************************ WRITE ************************** */
2111
2112
2113 char videosc_dir[160]= {0, 0};
2114
2115
2116 static void write_videoscape_mesh(Object *ob, char *str)
2117 {
2118         Mesh *me;
2119         Material *ma;
2120         MVert *mvert;
2121         MFace *mface;
2122         FILE *fp;
2123         EditVert *eve;
2124         EditVlak *evl;
2125         unsigned int kleur[32];
2126         float co[3];
2127         int a;
2128         long tot;
2129         char *cp;
2130         
2131         if(ob && ob->type==OB_MESH);
2132         else {
2133                 return;
2134         }
2135
2136         kleur[0]= 0x00C0C0C0;
2137         if(G.order==L_ENDIAN) SWITCH_INT(kleur[0]);
2138
2139         cp= (char *)kleur;
2140         for(a=0; a<ob->totcol; a++, cp+=4) {
2141                 
2142                 ma= give_current_material(ob, a+1);
2143                 if(ma) {
2144                         cp[0]= (unsigned char) (255.0*ma->emit);
2145                         cp[1]= (unsigned char) (255.0*ma->b);
2146                         cp[2]= (unsigned char) (255.0*ma->g);
2147                         cp[3]= (unsigned char) (255.0*ma->r);
2148                 }
2149                 else kleur[a]= 0x00C0C0C0;
2150         
2151                 if(G.order==L_ENDIAN) SWITCH_INT(kleur[a]);
2152                 
2153                 if(a>30) break;
2154         }
2155         
2156         fp= fopen(str, "wb");
2157         if(fp==NULL) return;
2158
2159         fprintf(fp,"3DG1\n");
2160
2161         if(G.obedit) {
2162
2163                 fprintf(fp, "%d\n", G.totvert);
2164         
2165                 tot= 0;
2166                 eve= G.edve.first;
2167                 while(eve) {
2168                         VECCOPY(co, eve->co);
2169                         Mat4MulVecfl(ob->obmat, co);
2170                         fprintf(fp, "%f %f %f\n", co[0], co[1], co[2] );
2171                         eve->vn= (struct EditVert *)tot;
2172                         tot++;
2173                         eve= eve->next;
2174                 }
2175                 evl= G.edvl.first;
2176                 while(evl) {
2177
2178                         if(evl->v4==0) {
2179                                 fprintf(fp, "3 %p %p %p 0x%x\n", evl->v1->vn, evl->v2->vn, evl->v3->vn, kleur[evl->mat_nr]);
2180                         }
2181                         else {
2182                                 fprintf(fp, "4 %p %p %p %p 0x%x\n", evl->v1->vn, evl->v2->vn, evl->v3->vn, evl->v4->vn, kleur[evl->mat_nr]);
2183                         }
2184                         evl= evl->next;
2185                 }
2186         }
2187         else {
2188                 DispList *dl;
2189                 float *extverts=0;
2190                 
2191                 dl= find_displist(&ob->disp, DL_VERTS);
2192                 if(dl) extverts= dl->verts;
2193                 
2194                 me= ob->data;
2195                 
2196                 fprintf(fp, "%d\n", me->totvert);
2197                 
2198                 mvert= me->mvert;
2199                 mface= me->mface;
2200                 for(a=0; a<me->totvert; a++, mvert++) {
2201                         if(extverts) {
2202                                 VECCOPY(co, extverts);
2203                                 extverts+= 3;
2204                         }
2205                         else {
2206                                 VECCOPY(co, mvert->co);
2207                         }
2208                         Mat4MulVecfl(ob->obmat, co);
2209                         fprintf(fp, "%f %f %f\n", co[0], co[1], co[2] );
2210                 }
2211                 for(a=0; a<me->totface; a++, mface++) {
2212                         if(mface->v3==0) {
2213                                 fprintf(fp, "2 %d %d 0x%x\n", mface->v1, mface->v2, kleur[mface->mat_nr]);
2214                         }
2215                         else if(mface->v4==0) {
2216                                 fprintf(fp, "3 %d %d %d 0x%x\n", mface->v1, mface->v2, mface->v3, kleur[mface->mat_nr]);
2217                         }
2218                         else {
2219                                 fprintf(fp, "4 %d %d %d %d 0x%x\n", mface->v1, mface->v2, mface->v3, mface->v4, kleur[mface->mat_nr]);
2220                         }
2221                 }
2222         }
2223         
2224         fclose(fp);
2225         
2226 }
2227
2228
2229 void write_videoscape(char *str)
2230 {
2231         Base *base;
2232         int file, val, lampdone=0;
2233         unsigned short numlen;
2234         char head[FILE_MAXFILE], tail[FILE_MAXFILE];
2235         
2236         if(BLI_testextensie(str,".trace")) str[ strlen(str)-6]= 0;
2237         if(BLI_testextensie(str,".blend")) str[ strlen(str)-6]= 0;
2238         if(BLI_testextensie(str,".ble")) str[ strlen(str)-4]= 0;
2239         if(BLI_testextensie(str,".obj")==0) strcat(str, ".obj");
2240
2241         file= open(str,O_BINARY|O_RDONLY);
2242         close(file);
2243         if(file>-1) if(saveover(str)==0) return;
2244
2245         strcpy(videosc_dir, str);
2246
2247         base= G.scene->base.first;
2248         while(base) {
2249                 if((base->flag & SELECT) && (base->lay & G.scene->lay))  {
2250                         if(base->object->type==OB_MESH) {
2251                                 write_videoscape_mesh(base->object, str);
2252                                 val = BLI_stringdec(str, head, tail, &numlen);
2253                                 BLI_stringenc(str, head, tail, numlen, val + 1);
2254                         }
2255                         else if(base->object->type==OB_CURVE || base->object->type==OB_SURF) {
2256                                 /* write_videoscape_nurbs(base->object, str); */
2257                                 /* val = stringdec(str, head, tail, &numlen); */
2258                                 /* stringenc(str, head, tail, numlen, val + 1); */
2259                         }
2260                         else if(lampdone==0 && base->object->type==OB_LAMP) {
2261                                 /* lampdone= 1; */
2262                                 /* write_videoscape_lamps(str); */
2263                                 /* val = stringdec(str, head, tail, &numlen); */
2264                                 /* stringenc(str, head, tail, numlen, val + 1); */
2265                         }
2266                 }
2267                 base= base->next;
2268         }
2269         
2270         
2271         /* weggooien als nog hogere nummers bestaan */
2272         while(remove(str)==0) {
2273                 
2274                 val = BLI_stringdec(str, head, tail, &numlen);
2275                 BLI_stringenc(str, head, tail, numlen, val + 1);
2276         }
2277 }
2278
2279 /* ******************************* WRITE VRML ***************************** */
2280
2281 static void replace_chars(char *str1, char *str2)
2282 {
2283         int a= strlen(str2);
2284         
2285         str1[a]= 0;
2286         while(a--) {
2287                 if(str2[a]=='.' || str2[a]==' ') str1[a]= '_';
2288                 else str1[a]= str2[a];
2289         }
2290 }
2291
2292
2293 static void write_material_vrml(FILE *fp, Material *ma)
2294 {
2295         char str[32];
2296         
2297         replace_chars(str, ma->id.name+2);
2298         
2299         fprintf(fp, "\tDEF %s\n", str);
2300         fprintf(fp, "\tMaterial {\n");
2301         
2302         fprintf(fp, "\t\tdiffuseColor %f %f %f\n", ma->r, ma->g, ma->b);
2303         fprintf(fp, "\t\tspecularColor %f %f %f\n", ma->specr, ma->specg, ma->specb);
2304         fprintf(fp, "\t\tshininess %f \n", ((float)ma->har)/100.0);
2305         fprintf(fp, "\t\ttransparency %f \n", 1.0-ma->alpha);
2306         
2307         fprintf(fp, "\t}\n");
2308         
2309 }
2310
2311 unsigned int *mcol_to_vcol(Mesh *me)
2312 {
2313         MFace *mface;
2314         unsigned int *mcol, *mcoln, *mcolmain;
2315         int a;
2316
2317         if(me->totface==0 || me->mcol==0) return 0;
2318         
2319         mcoln= mcolmain= MEM_mallocN(sizeof(int)*me->totvert, "mcoln");
2320         mcol = (unsigned int *)me->mcol;
2321         mface= me->mface;
2322         
2323         for(a=me->totface; a>0; a--, mface++) {
2324                 mcoln[mface->v1]= mcol[0];
2325                 mcoln[mface->v2]= mcol[1];
2326                 if(mface->v3) mcoln[mface->v3]= mcol[2];
2327                 if(mface->v4) mcoln[mface->v4]= mcol[3];
2328
2329                 mcol+= 4;
2330         }
2331         
2332         return mcolmain;
2333 }
2334
2335 void mcol_to_rgba(unsigned int col, float *r, float *g, float *b, float *a)
2336 {
2337         char *cp;
2338         
2339         cp = (char *)&col;
2340         
2341         *r= cp[3];
2342         *r /= 255.0;
2343
2344         *g= cp[2];
2345         *g /= 255.0;
2346
2347         *b= cp[1];
2348         *b /= 255.0;
2349
2350         *a= cp[0];
2351         *a /= 255.0;
2352 }
2353
2354 static void write_mesh_vrml(FILE *fp, Mesh *me)
2355 {
2356         Material *ma;
2357         MVert *mvert;
2358         MFace *mface;
2359         TFace *tface;
2360         Image *ima;
2361         int a, b, totcol;
2362         char str[32];
2363         
2364         replace_chars(str, me->id.name+2);
2365
2366         fprintf(fp, "\tDEF %s\n", str);
2367         fprintf(fp, "\tSeparator {\n");
2368         
2369         if(me->tface) {
2370                 ima= ((TFace *)me->tface)->tpage;
2371                 if(ima) {
2372                         fprintf(fp, "\t\tTexture2 {\n");
2373                         fprintf(fp, "\t\t\tfilename %s\n", ima->name);
2374                         fprintf(fp, "\t\t\twrapS REPEAT \n");
2375                         fprintf(fp, "\t\t\twrapT REPEAT \n");
2376                         fprintf(fp, "\t\t}\n");
2377                 }
2378                 tface_to_mcol(me);
2379         }
2380         
2381         if(me->mcol) {
2382                 unsigned int *mcol, *mcolmain;
2383                 float r, g, b, cola;
2384                 
2385                 fprintf(fp, "\t\tMaterial {\n");
2386                 fprintf(fp, "\t\t\tdiffuseColor [\n");
2387                 
2388                 a= me->totvert;
2389                 mcol= mcolmain= mcol_to_vcol(me);
2390                 if(mcol) {
2391                         while(a--) {
2392                                 mcol_to_rgba(*mcol, &r, &g, &b, &cola);
2393                                 fprintf(fp, "\t\t\t\t %f %f %f,\n", r, g, b);
2394                                 mcol++;
2395                         }
2396                         MEM_freeN(mcolmain);
2397                 }
2398                 fprintf(fp, "\t\t\t]\n");
2399                 fprintf(fp, "\t\t}\n");
2400
2401                 fprintf(fp, "\t\tMaterialBinding { value PER_VERTEX_INDEXED }\n");
2402         }
2403
2404
2405         fprintf(fp, "\t\tCoordinate3 {\n");
2406         fprintf(fp, "\t\t\tpoint [\n");
2407         
2408         a= me->totvert;
2409         mvert= me->mvert;
2410         while(a--) {
2411                 fprintf(fp, "\t\t\t\t %f %f %f,\n", mvert->co[0], mvert->co[1], mvert->co[2]);
2412                 mvert++;
2413         }
2414         fprintf(fp, "\t\t\t]\n");
2415         fprintf(fp, "\t\t}\n");
2416         
2417         
2418         totcol= me->totcol;
2419         if(totcol==0) totcol= 1;
2420         
2421         for(b=0; b<totcol; b++) {
2422                 
2423                 if(me->mcol==0) {
2424                         if(me->mat) {
2425                                 ma= me->mat[b];
2426                                 if(ma) {
2427                                         replace_chars(str, ma->id.name+2);
2428
2429                                         fprintf(fp, "\t\tUSE %s\n\n", str);
2430                                 }
2431                         }
2432                 }
2433                 
2434                 if(me->tface) {
2435                         fprintf(fp, "\t\tTextureCoordinate2 {\n");
2436                         fprintf(fp, "\t\t\tpoint [\n");
2437         
2438                         a= me->totface;
2439                         mface= me->mface;
2440                         tface= me->tface;
2441                         while(a--) {
2442                                 if(mface->mat_nr==b) {
2443                                         fprintf(fp, "\t\t\t\t %f %f,\n", tface->uv[0][0], tface->uv[0][1]); 
2444                                         fprintf(fp, "\t\t\t\t %f %f,\n", tface->uv[3][0], tface->uv[3][1]); 
2445                                         if(mface->v3) fprintf(fp, "\t\t\t\t %f %f,\n", tface->uv[2][0], tface->uv[2][1]); 
2446                                         if(mface->v4) fprintf(fp, "\t\t\t\t %f %f,\n", tface->uv[1][0], tface->uv[1][1]); 
2447                                 }
2448                                 mface++;
2449                                 tface++;
2450                         }
2451                         fprintf(fp, "\t\t\t]\n");
2452                         fprintf(fp, "\t\t}\n");
2453                 }
2454
2455                 fprintf(fp, "\t\tIndexedFaceSet {\n");
2456                 fprintf(fp, "\t\t\tcoordIndex [\n");
2457
2458                 a= me->totface;
2459                 mface= me->mface;
2460                 while(a--) {
2461                         if(mface->mat_nr==b) {
2462                                 if(mface->v4) fprintf(fp, "\t\t\t\t %d, %d, %d, %d, -1,\n", mface->v1, mface->v2, mface->v3, mface->v4); 
2463                                 else if(mface->v3) fprintf(fp, "\t\t\t\t %d, %d, %d, -1,\n", mface->v1, mface->v2, mface->v3); 
2464                         }
2465                         mface++;
2466                 }
2467                 fprintf(fp, "\t\t\t]\n");
2468                 fprintf(fp, "\t\t}\n");
2469
2470         }
2471         
2472         fprintf(fp, "\t}\n");
2473         
2474         if(me->tface) {
2475                 MEM_freeN(me->mcol);
2476                 me->mcol= 0;
2477         }
2478
2479 }
2480
2481 static void write_camera_vrml(FILE *fp, Object *ob)
2482 {
2483         Camera *cam;
2484         
2485         if(ob==0) return;
2486         Mat4Invert(ob->imat, ob->obmat);
2487
2488         fprintf(fp, "\tMatrixTransform {\n");
2489
2490         fprintf(fp, "\tmatrix \n");
2491
2492         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]);
2493         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]);
2494         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]);
2495         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]);
2496
2497         fprintf(fp, "\t}\n");
2498
2499         cam= ob->data;
2500
2501         fprintf(fp, "\tPerspectiveCamera {\n");
2502         fprintf(fp, "\t\tfocalDistance %f\n", cam->lens/10.0);
2503         
2504         fprintf(fp, "\t}\n");
2505
2506 }
2507
2508 static void write_object_vrml(FILE *fp, Object *ob)
2509 {
2510         ID *id;
2511         char str[32];
2512         
2513         fprintf(fp, "\tSeparator {\n");
2514         fprintf(fp, "\t\tMatrixTransform {\n");
2515
2516         fprintf(fp, "\t\tmatrix \n");
2517
2518         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]);
2519         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]);
2520         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]);
2521         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]);
2522
2523         fprintf(fp, "\t\t}\n");
2524
2525         id= ob->data;
2526
2527         replace_chars(str, id->name+2);
2528
2529         fprintf(fp, "\t\tUSE %s\n", str);
2530         fprintf(fp, "\t}\n");
2531 }
2532
2533
2534 void write_vrml(char *str)
2535 {
2536         Mesh *me;
2537         Material *ma;
2538         Base *base;
2539         FILE *fp;
2540         
2541         if(BLI_testextensie(str,".blend")) str[ strlen(str)-6]= 0;
2542         if(BLI_testextensie(str,".ble")) str[ strlen(str)-4]= 0;
2543         if(BLI_testextensie(str,".wrl")==0) strcat(str, ".wrl");
2544
2545         if(saveover(str)==0) return;
2546         
2547         fp= fopen(str, "w");
2548         
2549         if(fp==NULL) {
2550                 error("Can't write file");
2551                 return;
2552         }
2553         strcpy(videosc_dir, str);
2554
2555         waitcursor(1);
2556         
2557         /* FIRST: write all the datablocks */
2558         
2559         fprintf(fp, "#VRML V1.0 ascii\n\n# Blender V2.0\n\n# 'Switch' is used as a hack, to ensure it is not part of the drawing\n\n");
2560         fprintf(fp, "Separator {\n");
2561         fprintf(fp, "Switch {\n");
2562
2563         ma= G.main->mat.first;
2564         while(ma) {
2565                 if(ma->id.us) {
2566                         write_material_vrml(fp, ma);
2567                 }
2568                 ma= ma->id.next;
2569         }
2570
2571         me= G.main->mesh.first;
2572         while(me) {
2573                 if(me->id.us) {
2574                         write_mesh_vrml(fp, me);
2575                 }
2576                 me= me->id.next;
2577         }
2578         
2579         /* THEN:Hidden Objects */
2580         fprintf(fp, "\n\t# Hidden Objects, in invisible layers\n\n");
2581         base= G.scene->base.first;
2582         while(base) {
2583                 if(base->object->type== OB_MESH) {
2584                         if( (base->lay & G.scene->lay)==0 ) {
2585                                 write_object_vrml(fp, base->object);
2586                         }
2587                 }
2588                 base= base->next;
2589         }
2590
2591         fprintf(fp, "}\n");
2592         fprintf(fp, "\n# Visible Objects\n\n");
2593         fprintf(fp, "Separator {\n");
2594         
2595         /* The camera */
2596
2597         write_camera_vrml(fp, G.scene->camera);
2598         
2599         /* THEN:The Objects */
2600         
2601         base= G.scene->base.first;
2602         while(base) {
2603                 if(base->object->type== OB_MESH) {
2604                         if(base->lay & G.scene->lay) {
2605                                 write_object_vrml(fp, base->object);
2606                         }
2607                 }
2608                 base= base->next;
2609         }
2610         
2611         fprintf(fp, "}\n");
2612         fprintf(fp, "}\n");
2613         
2614         fclose(fp);
2615         
2616         waitcursor(0);
2617 }
2618
2619
2620 /* ******************************* WRITE DXF ***************************** */
2621
2622 #define write_group(id,data) fprintf(fp, "%d\n%s\n", id, data)
2623
2624 /* A completely wacky function to try and make good
2625 indexed (AutoCAD index) values out of straight rgb 
2626 ones... crazy */
2627
2628 static int rgb_to_dxf_col (float rf, float gf, float bf) 
2629 {
2630         int r= (int) (rf*255.0f);
2631         int g= (int) (gf*255.0f);
2632         int b= (int) (bf*255.0f);
2633         float h,s,v;
2634         int ret;
2635         
2636         /* Grayscale value */
2637         if (((int)r/10)==((int)g/10) && ((int)g/10)==((int)b/10)) ret= 250+((int)r/51);
2638         /* A nice chroma value */
2639         else {
2640                 rgb_to_hsv (rf,gf,bf,&h,&s,&v);
2641                 
2642                 ret= (int) (10.0f + (h*239.0f));
2643                 CLAMP(ret,10,249);
2644                 
2645                 /* If its whitish make the index odd */
2646                 if (s<.5 || v>.5) if(ret%2) ret++;
2647         }
2648         
2649         return ret;
2650 }
2651
2652 /* And its completely wacky complement */
2653
2654 static void dxf_col_to_rgb (int cid, float *rf, float *gf, float *bf)
2655 {
2656         float h, s, v;
2657         
2658         /* Grayscale values */
2659         if (cid>=250 && cid <= 255) {
2660                 *rf= *gf= *bf= (float) ((cid-250)*51)/255;
2661                 CLAMP(*rf, 0.0, 1.0);
2662                 CLAMP(*gf, 0.0, 1.0);
2663                 CLAMP(*bf, 0.0, 1.0);
2664                 
2665         /* Pure values */
2666         } else if (cid<10) {
2667                 switch (cid) {
2668                 case 1:
2669                         *rf=1.0;
2670                         *gf=0.0;
2671                         *bf=0.0;
2672                         break;
2673                 case 2:
2674                         *rf=1.0;
2675                         *gf=1.0;
2676                         *bf=0.0;
2677                         break;
2678                 case 3:
2679                         *gf=1.0;
2680                         *rf=0.0;
2681                         *bf=0.0;
2682                         break;
2683                 case 4:
2684                         *rf=0.0;
2685                         *gf=1.0;
2686                         *bf=1.0;
2687                         break;
2688                 case 5:
2689                         *rf=0.0;
2690                         *gf=0.0;
2691                         *bf=1.0;
2692                         break;
2693                 case 6:
2694                         *rf=1.0;
2695                         *gf=0.0;
2696                         *bf=1.0;
2697                         break;
2698                 case 7:
2699                 default:
2700                         *rf= *gf= *bf= 1.0;
2701                         break;
2702                 }
2703         } else {
2704                 /* Get chroma values */
2705                         
2706                 h= (float) (cid-10)/239;
2707                 CLAMP(h, 0.0, 1.0);
2708                 
2709                 /* If its odd make it a bit whitish */
2710                 if (cid%2) { s=.75; v= 0.25; 
2711                 } else {  s= 0.25; v= 0.75;}
2712                 
2713                 hsv_to_rgb (h, s, v, rf, gf, bf);
2714         }
2715 }
2716
2717 static void write_mesh_dxf(FILE *fp, Mesh *me)
2718 {
2719         Material *ma;
2720         MVert *mvert;
2721         MFace *mface;
2722         int a;
2723         char str[32];
2724         
2725         replace_chars(str, me->id.name+2);
2726
2727         write_group(0, "BLOCK");
2728         
2729         write_group(2, str); /* The name */
2730                 
2731         write_group(8, "Meshes"); /* DXF Layer */
2732         write_group(70, "64"); /* DXF block flags */
2733         
2734         write_group(10, "0.0"); /* X of base */
2735         write_group(20, "0.0"); /* Y of base */
2736         write_group(30, "0.0"); /* Z of base */
2737
2738         write_group(3, str); /* The name (again) */
2739         
2740         write_group(0, "POLYLINE"); /* Start the mesh */
2741         write_group(66, "1"); /* Vertices follow flag */
2742         write_group(8,"Meshes"); /* DXF Layer */
2743
2744         if (me->totcol) {
2745                 ma= me->mat[0];
2746                 if(ma) {
2747                         sprintf(str,"%d",rgb_to_dxf_col(ma->r,ma->g,ma->b));
2748                         write_group(62, str); /* Color index */
2749                 }
2750         }
2751
2752         write_group(70, "64"); /* Polymesh mesh flag */
2753         
2754         fprintf(fp, "71\n%d\n", me->totvert); /* Total vertices */
2755         fprintf(fp, "72\n%d\n", me->totface); /* Total faces */
2756         
2757         /* Write the vertices */
2758         a= me->totvert;
2759         mvert= me->mvert;
2760         while(a--) {
2761                 write_group(0, "VERTEX"); /* Start a new vertex */
2762                 write_group(8, "Meshes"); /* DXF Layer */
2763                 fprintf (fp, "10\n%f\n", mvert->co[0]); /* X cord */
2764                 fprintf (fp, "20\n%f\n", mvert->co[1]); /* Y cord */
2765                 fprintf (fp, "30\n%f\n", mvert->co[2]); /* Z cord */
2766                 write_group(70, "192"); /* Polymesh vertex flag */
2767                                 
2768                 mvert++;
2769         }
2770
2771         /* Write the face entries */
2772         a= me->totface;
2773         mface= me->mface;
2774         while(a--) {
2775                 if (mface->v4 || mface->v3) {
2776                         write_group(0, "VERTEX"); /* Start a new face */
2777                         write_group(8, "Meshes");
2778                 
2779                         /* Write a face color */
2780                         if (me->totcol) {
2781                                 ma= me->mat[mface->mat_nr];
2782                                 if(ma) {
2783                                         sprintf(str,"%d",rgb_to_dxf_col(ma->r,ma->g,ma->b));
2784                                         write_group(62, str); /* Color index */
2785                                 }
2786                         }
2787                         else write_group(62, "254"); /* Color Index */
2788
2789                         /* Not sure what this really corresponds too */
2790                         write_group(10, "0.0"); /* X of base */
2791                         write_group(20, "0.0"); /* Y of base */
2792                         write_group(30, "0.0"); /* Z of base */
2793                 
2794                         write_group(70, "128"); /* Polymesh face flag */
2795                 
2796                         if(mface->v4) {
2797                                 fprintf (fp, "71\n%d\n", mface->v1+1);
2798                                 fprintf (fp, "72\n%d\n", mface->v2+1);
2799                                 fprintf (fp, "73\n%d\n", mface->v3+1);
2800                                 fprintf (fp, "74\n%d\n", mface->v4+1);
2801                         } else if(mface->v3) {
2802                                 fprintf (fp, "71\n%d\n", mface->v1+1);
2803                                 fprintf (fp, "72\n%d\n", mface->v2+1);
2804                                 fprintf (fp, "73\n%d\n", mface->v3+1);
2805                         }
2806                 }
2807                 mface++;
2808         }
2809
2810         write_group(0, "SEQEND");       
2811         
2812         write_group(0, "ENDBLK");
2813 }
2814
2815 static void write_object_dxf(FILE *fp, Object *ob, int layer)
2816 {
2817         ID *id;
2818         char str[32];
2819
2820         id= ob->data;
2821
2822         write_group(0, "INSERT"); /* Start an insert group */
2823         
2824         sprintf(str, "%d", layer);
2825         write_group(8, str);
2826
2827         replace_chars(str, id->name+2);
2828         write_group(2, str);
2829
2830         fprintf (fp, "10\n%f\n", ob->loc[0]); /* X of base */
2831         fprintf (fp, "20\n%f\n", ob->loc[1]); /* Y of base */
2832         fprintf (fp, "30\n%f\n", ob->loc[2]); /* Z of base */
2833         
2834         fprintf (fp, "41\n%f\n", ob->size[0]); /* X scale */
2835         fprintf (fp, "42\n%f\n", ob->size[1]); /* Y scale */
2836         fprintf (fp, "43\n%f\n", ob->size[2]); /* Z scale */
2837         
2838         fprintf (fp, "50\n%f\n", (float) ob->rot[2]*180/M_PI); /* Can only write the Z rot */
2839 }
2840
2841 void write_dxf(char *str)
2842 {
2843         Mesh *me;
2844         Base *base;
2845         FILE *fp;
2846         
2847         if(BLI_testextensie(str,".blend")) str[ strlen(str)-6]= 0;
2848         if(BLI_testextensie(str,".ble")) str[ strlen(str)-4]= 0;
2849         if(BLI_testextensie(str,".dxf")==0) strcat(str, ".dxf");
2850
2851         if (BLI_exists(str))
2852                 if(saveover(str)==0)
2853                         return;
2854         
2855         fp= fopen(str, "w");
2856         
2857         if(fp==NULL) {
2858                 error("Can't write file");
2859                 return;
2860         }
2861         strcpy(videosc_dir, str);
2862         
2863         waitcursor(1);
2864         
2865         /* The header part of the DXF */
2866         
2867         write_group(0, "SECTION");
2868     write_group(2, "HEADER");
2869         write_group(0, "ENDSEC");
2870
2871         /* The blocks part of the DXF */
2872         
2873         write_group(0, "SECTION");
2874     write_group(2, "BLOCKS");
2875
2876         /* Write all the meshes */
2877         me= G.main->mesh.first;
2878         while(me) {
2879                 if(me->id.us) {
2880                         write_mesh_dxf(fp, me);
2881                 }
2882                 me= me->id.next;
2883         }
2884
2885         write_group(0, "ENDSEC");
2886
2887         /* The entities part of the DXF */
2888         
2889   &nb