svn merge -r 15392:15551 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[blender.git] / source / blender / src / editmesh_add.c
1 /**
2  * $Id: 
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2004 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30
31 #include <stdlib.h>
32 #include <string.h>
33 #include <math.h>
34
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #include "MEM_guardedalloc.h"
40
41
42 #include "DNA_mesh_types.h"
43 #include "DNA_meshdata_types.h"
44 #include "DNA_object_types.h"
45 #include "DNA_scene_types.h"
46 #include "DNA_space_types.h"
47 #include "DNA_screen_types.h"
48 #include "DNA_userdef_types.h"
49 #include "DNA_view3d_types.h"
50
51 #include "BLI_blenlib.h"
52 #include "BLI_arithb.h"
53 #include "BLI_editVert.h"
54
55 #include "BKE_depsgraph.h"
56 #include "BKE_global.h"
57 #include "BKE_library.h"
58 #include "BKE_mesh.h"
59 #include "BKE_object.h"
60 #include "BKE_utildefines.h"
61
62 #ifdef WITH_VERSE
63 #include "BKE_verse.h"
64 #endif
65
66
67 #include "BIF_editmesh.h"
68 #include "BIF_graphics.h"
69 #include "BIF_interface.h"
70 #include "BIF_mywindow.h"
71 #include "BIF_retopo.h"
72 #include "BIF_screen.h"
73 #include "BIF_space.h"
74 #include "BIF_toolbox.h"
75 #include "BIF_transform.h"
76
77 #ifdef WITH_VERSE
78 #include "BIF_verse.h"
79 #endif
80
81 #include "BDR_editobject.h" 
82
83 #include "BSE_view.h"
84 #include "BSE_edit.h"
85
86 #include "blendef.h"
87 #include "multires.h"
88 #include "mydevice.h"
89
90 #include "editmesh.h"
91
92 /* bpymenu */
93 #include "BPY_extern.h"
94 #include "BPY_menus.h"
95
96 static float icovert[12][3] = {
97         {0,0,-200}, 
98         {144.72, -105.144,-89.443},
99         {-55.277, -170.128,-89.443}, 
100         {-178.885,0,-89.443},
101         {-55.277,170.128,-89.443}, 
102         {144.72,105.144,-89.443},
103         {55.277,-170.128,89.443},
104         {-144.72,-105.144,89.443},
105         {-144.72,105.144,89.443},
106         {55.277,170.128,89.443},
107         {178.885,0,89.443},
108         {0,0,200}
109 };
110 static short icoface[20][3] = {
111         {1,0,2},
112         {1,0,5},
113         {2,0,3},
114         {3,0,4},
115         {4,0,5},
116         {1,5,10},
117         {2,1,6},
118         {3,2,7},
119         {4,3,8},
120         {5,4,9},
121         {10,1,6},
122         {6,2,7},
123         {7,3,8},
124         {8,4,9},
125         {9,5,10},
126         {6,10,11},
127         {7,6,11},
128         {8,7,11},
129         {9,8,11},
130         {10,9,11}
131 };
132
133 static void get_view_aligned_coordinate(float *fp)
134 {
135         float dvec[3];
136         short mx, my, mval[2];
137         
138         getmouseco_areawin(mval);
139         mx= mval[0];
140         my= mval[1];
141         
142         project_short_noclip(fp, mval);
143         
144         initgrabz(fp[0], fp[1], fp[2]);
145         
146         if(mval[0]!=IS_CLIPPED) {
147                 window_to_3d(dvec, mval[0]-mx, mval[1]-my);
148                 VecSubf(fp, fp, dvec);
149         }
150 }
151
152 void add_click_mesh(void)
153 {
154         EditMesh *em = G.editMesh;
155         EditVert *eve, *v1;
156         float min[3], max[3];
157         int done= 0;
158         
159         TEST_EDITMESH
160         if(multires_test()) return;
161         
162         INIT_MINMAX(min, max);
163         
164         for(v1= em->verts.first;v1; v1=v1->next) {
165                 if(v1->f & SELECT) {
166                         DO_MINMAX(v1->co, min, max);
167                         done= 1;
168                 }
169         }
170
171         /* call extrude? */
172         if(done) {
173                 EditEdge *eed;
174                 float vec[3], cent[3], mat[3][3];
175                 float nor[3]= {0.0, 0.0, 0.0};
176                 
177                 /* check for edges that are half selected, use for rotation */
178                 done= 0;
179                 for(eed= em->edges.first; eed; eed= eed->next) {
180                         if( (eed->v1->f & SELECT)+(eed->v2->f & SELECT) == SELECT ) {
181                                 if(eed->v1->f & SELECT) VecSubf(vec, eed->v1->co, eed->v2->co);
182                                 else VecSubf(vec, eed->v2->co, eed->v1->co);
183                                 VecAddf(nor, nor, vec);
184                                 done= 1;
185                         }
186                 }
187                 if(done) Normalize(nor);
188                 
189                 /* center */
190                 VecAddf(cent, min, max);
191                 VecMulf(cent, 0.5f);
192                 VECCOPY(min, cent);
193                 
194                 Mat4MulVecfl(G.obedit->obmat, min);     // view space
195                 get_view_aligned_coordinate(min);
196                 Mat4Invert(G.obedit->imat, G.obedit->obmat); 
197                 Mat4MulVecfl(G.obedit->imat, min); // back in object space
198                 
199                 VecSubf(min, min, cent);
200                 
201                 /* calculate rotation */
202                 Mat3One(mat);
203                 if(done) {
204                         float dot;
205                         
206                         VECCOPY(vec, min);
207                         Normalize(vec);
208                         dot= INPR(vec, nor);
209
210                         if( fabs(dot)<0.999) {
211                                 float cross[3], si, q1[4];
212                                 
213                                 Crossf(cross, nor, vec);
214                                 Normalize(cross);
215                                 dot= 0.5f*saacos(dot);
216                                 si= (float)sin(dot);
217                                 q1[0]= (float)cos(dot);
218                                 q1[1]= cross[0]*si;
219                                 q1[2]= cross[1]*si;
220                                 q1[3]= cross[2]*si;
221                                 
222                                 QuatToMat3(q1, mat);
223                         }
224                 }
225                 
226                 extrudeflag(SELECT, nor);
227                 rotateflag(SELECT, cent, mat);
228                 translateflag(SELECT, min);
229                 
230                 recalc_editnormals();
231         }
232         else {
233                 float mat[3][3],imat[3][3];
234                 float *curs= give_cursor();
235                 
236                 eve= addvertlist(0, NULL);
237
238                 Mat3CpyMat4(mat, G.obedit->obmat);
239                 Mat3Inv(imat, mat);
240                 
241                 VECCOPY(eve->co, curs);
242                 VecSubf(eve->co, eve->co, G.obedit->obmat[3]);
243
244                 Mat3MulVecfl(imat, eve->co);
245                 
246                 eve->f= SELECT;
247         }
248         
249         retopo_do_all();
250         
251         countall();
252
253 #ifdef WITH_VERSE
254         if(G.editMesh->vnode) {
255                 sync_all_verseverts_with_editverts((VNode*)G.editMesh->vnode);
256         }
257 #endif
258
259         BIF_undo_push("Add vertex/edge/face");
260         allqueue(REDRAWVIEW3D, 0);
261         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);     
262         
263         while(get_mbut()&R_MOUSE);
264
265 }
266
267 /* selected faces get hidden edges */
268 static void make_fgon(int make)
269 {
270         EditMesh *em = G.editMesh;
271         EditFace *efa;
272         EditEdge *eed;
273         EditVert *eve;
274         float *nor=NULL;        // reference
275         int done=0;
276         
277         if(!make) {
278                 for(efa= em->faces.first; efa; efa= efa->next) {
279                         if(efa->f & SELECT) {
280                                 efa->fgonf= 0;
281                                 efa->e1->h &= ~EM_FGON;
282                                 efa->e2->h &= ~EM_FGON;
283                                 efa->e3->h &= ~EM_FGON;
284                                 if(efa->e4) efa->e4->h &= ~EM_FGON;
285                         }
286                 }
287                 allqueue(REDRAWVIEW3D, 0);
288                 EM_fgon_flags();        // redo flags and indices for fgons
289                 DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);     
290                 BIF_undo_push("Clear FGon");
291                 return;
292         }
293
294         /* tagging edges. rule is:
295            - edge used by exactly 2 selected faces
296            - no vertices allowed with only tagged edges (return)
297            - face normals are allowed to difffer
298          
299         */
300         for(eed= em->edges.first; eed; eed= eed->next) {
301                 eed->f1= 0;     // amount of selected
302                 eed->f2= 0; // amount of unselected
303         }
304         
305         for(efa= em->faces.first; efa; efa= efa->next) {
306                 if(efa->f & SELECT) {
307                         if(nor==NULL) nor= efa->n;
308                         if(efa->e1->f1 < 3) efa->e1->f1++;
309                         if(efa->e2->f1 < 3) efa->e2->f1++;
310                         if(efa->e3->f1 < 3) efa->e3->f1++;
311                         if(efa->e4 && efa->e4->f1 < 3) efa->e4->f1++;
312                 }
313                 else {
314                         if(efa->e1->f2 < 3) efa->e1->f2++;
315                         if(efa->e2->f2 < 3) efa->e2->f2++;
316                         if(efa->e3->f2 < 3) efa->e3->f2++;
317                         if(efa->e4 && efa->e4->f2 < 3) efa->e4->f2++;
318                 }
319         }
320         // now eed->f1 becomes tagged edge
321         for(eed= em->edges.first; eed; eed= eed->next) {
322                 if(eed->f1==2 && eed->f2==0) eed->f1= 1;
323                 else eed->f1= 0;
324         }
325         
326         // no vertices allowed with only tagged edges
327         for(eve= em->verts.first; eve; eve= eve->next) eve->f1= 0;
328         for(eed= em->edges.first; eed; eed= eed->next) {
329                 if(eed->f1) {
330                         eed->v1->f1 |= 1;
331                         eed->v2->f1 |= 1;
332                 }
333                 else {
334                         eed->v1->f1 |= 2;
335                         eed->v2->f1 |= 2;
336                 }
337         }
338         for(eve= em->verts.first; eve; eve= eve->next) {
339                 if(eve->f1==1) break;
340         }
341         if(eve) {
342                 error("Cannot make polygon with interior vertices");
343                 return;
344         }
345         
346         // check for faces
347         if(nor==NULL) {
348                 error("No faces selected to make FGon");
349                 return;
350         }
351
352         // and there we go
353         for(eed= em->edges.first; eed; eed= eed->next) {
354                 if(eed->f1) {
355                         eed->h |= EM_FGON;
356                         done= 1;
357                 }
358         }
359         
360         if(done==0) {
361                 error("Didn't find FGon to create");
362         }
363         else {
364                 EM_fgon_flags();        // redo flags and indices for fgons
365
366                 allqueue(REDRAWVIEW3D, 0);
367                 DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);     
368                 BIF_undo_push("Make FGon");
369         }
370 }
371
372 /* precondition; 4 vertices selected, check for 4 edges and create face */
373 static EditFace *addface_from_edges(void)
374 {
375         EditMesh *em = G.editMesh;
376         EditEdge *eed, *eedar[4]={NULL, NULL, NULL, NULL};
377         EditVert *v1=NULL, *v2=NULL, *v3=NULL, *v4=NULL;
378         int a;
379         
380         /* find the 4 edges */
381         for(eed= em->edges.first; eed; eed= eed->next) {
382                 if( (eed->f & SELECT) || (eed->v1->f & eed->v2->f & SELECT) ) {
383                         if(eedar[0]==NULL) eedar[0]= eed;
384                         else if(eedar[1]==NULL) eedar[1]= eed;
385                         else if(eedar[2]==NULL) eedar[2]= eed;
386                         else eedar[3]= eed;
387                         
388                 }
389         }
390         
391         
392         if(eedar[3]) {
393                 /* first 2 points */
394                 v1= eedar[0]->v1;
395                 v2= eedar[0]->v2;
396                 
397                 /* find the 2 edges connected to first edge */
398                 for(a=1; a<4; a++) {
399                         if( eedar[a]->v1 == v2) v3= eedar[a]->v2;
400                         else if(eedar[a]->v2 == v2) v3= eedar[a]->v1;
401                         else if( eedar[a]->v1 == v1) v4= eedar[a]->v2;
402                         else if(eedar[a]->v2 == v1) v4= eedar[a]->v1;
403                 }
404                 
405                 /* verify if last edge exists */
406                 if(v3 && v4) {
407                         for(a=1; a<4; a++) {
408                                 if( eedar[a]->v1==v3 && eedar[a]->v2==v4) break;
409                                 if( eedar[a]->v2==v3 && eedar[a]->v1==v4) break;
410                         }
411                         if(a!=4) {
412                                 return addfacelist(v1, v2, v3, v4, NULL, NULL);
413                         }
414                 }
415         }
416         return NULL;
417 }
418
419 /* this also allows to prevent triangles being made in quads */
420 static int compareface_overlaps(EditFace *vl1, EditFace *vl2)
421 {
422         EditVert *v1, *v2, *v3, *v4;
423         int equal= 0;
424         
425         v1= vl2->v1;
426         v2= vl2->v2;
427         v3= vl2->v3;
428         v4= vl2->v4;
429         
430         if(vl1==vl2) return 0;
431         
432         if(v4==NULL && vl1->v4==NULL) {
433                 if(vl1->v1==v1 || vl1->v2==v1 || vl1->v3==v1) equal++;
434                 if(vl1->v1==v2 || vl1->v2==v2 || vl1->v3==v2) equal++;
435                 if(vl1->v1==v3 || vl1->v2==v3 || vl1->v3==v3) equal++;
436         }
437         else {
438                 if(vl1->v1==v1 || vl1->v2==v1 || vl1->v3==v1 || vl1->v4==v1) equal++;
439                 if(vl1->v1==v2 || vl1->v2==v2 || vl1->v3==v2 || vl1->v4==v2) equal++;
440                 if(vl1->v1==v3 || vl1->v2==v3 || vl1->v3==v3 || vl1->v4==v3) equal++;
441                 if(vl1->v1==v4 || vl1->v2==v4 || vl1->v3==v4 || vl1->v4==v4) equal++;
442         }
443
444         if(v4 && vl1->v4) {
445                 if(equal==4) return 1;
446         }
447         else 
448                 if(equal>=3) return 1;
449         
450         return 0;
451 }
452
453 /* checks for existance, and for tria overlapping inside quad */
454 static EditFace *exist_face_overlaps(EditVert *v1, EditVert *v2, EditVert *v3, EditVert *v4)
455 {
456         EditMesh *em = G.editMesh;
457         EditFace *efa, efatest;
458         
459         efatest.v1= v1;
460         efatest.v2= v2;
461         efatest.v3= v3;
462         efatest.v4= v4;
463         
464         efa= em->faces.first;
465         while(efa) {
466                 if(compareface_overlaps(&efatest, efa)) return efa;
467                 efa= efa->next;
468         }
469         return NULL;
470 }
471
472 /* will be new face smooth or solid? depends on smoothness of face neighbours
473  * of new face, if function return 1, then new face will be smooth, when functio
474  * will return zero, then new face will be solid */
475 static void fix_new_face(EditFace *eface)
476 {
477         struct EditMesh *em = G.editMesh;
478         struct EditFace *efa;
479         struct EditEdge *eed=NULL;
480         struct EditVert *v1 = eface->v1, *v2 = eface->v2, *v3 = eface->v3, *v4 = eface->v4;
481         struct EditVert *ev1=NULL, *ev2=NULL;
482         short smooth=0; /* "total smoothnes" of faces in neighbourhood */
483         short coef;     /* "weight" of smoothness */
484         short count=0;  /* number of edges with same direction as eface */
485         short vi00=0, vi01=0, vi10=0, vi11=0; /* vertex indexes */
486
487         efa = em->faces.first;
488
489         while(efa) {
490
491                 if(efa==eface) {
492                         efa = efa->next;
493                         continue;
494                 }
495
496                 coef = 0;
497                 ev1 = ev2 = NULL;
498                 eed = NULL;
499
500                 if(efa->v1==v1 || efa->v2==v1 || efa->v3==v1 || efa->v4==v1) {
501                         ev1 = v1;
502                         coef++;
503                 }
504                 if(efa->v1==v2 || efa->v2==v2 || efa->v3==v2 || efa->v4==v2) {
505                         if(ev1) ev2 = v2;
506                         else ev1 = v2;
507                         coef++;
508                 }
509                 if(efa->v1==v3 || efa->v2==v3 || efa->v3==v3 || efa->v4==v3) {
510                         if(coef<2) {
511                                 if(ev1) ev2 = v3;
512                                 else ev1 = v3;
513                         }
514                         coef++;
515                 }
516                 if((v4) && (efa->v1==v4 || efa->v2==v4 || efa->v3==v4 || efa->v4==v4)) {
517                         if(ev1 && coef<2) ev2 = v4;
518                         coef++;
519                 }
520
521                 /* "democracy" of smoothness */
522                 if(efa->flag & ME_SMOOTH)
523                         smooth += coef;
524                 else
525                         smooth -= coef;
526
527                 /* try to find edge using vertexes ev1 and ev2 */
528                 if((ev1) && (ev2) && (ev1!=ev2)) eed = findedgelist(ev1, ev2);
529
530                 /* has bordering edge of efa same direction as edge of eface ? */
531                 if(eed) {
532                         if(eed->v1==v1) vi00 = 1;
533                         else if(eed->v1==v2) vi00 = 2;
534                         else if(eed->v1==v3) vi00 = 3;
535                         else if(v4 && eed->v1==v4) vi00 = 4;
536
537                         if(eed->v2==v1) vi01 = 1;
538                         else if(eed->v2==v2) vi01 = 2;
539                         else if(eed->v2==v3) vi01 = 3;
540                         else if(v4 && eed->v2==v4) vi01 = 4;
541
542                         if(v4) {
543                                 if(vi01==1 && vi00==4) vi00 = 0;
544                                 if(vi01==4 && vi00==1) vi01 = 0;
545                         }
546                         else {
547                                 if(vi01==1 && vi00==3) vi00 = 0;
548                                 if(vi01==3 && vi00==1) vi01 = 0;
549                         }
550
551                         if(eed->v1==efa->v1) vi10 = 1;
552                         else if(eed->v1==efa->v2) vi10 = 2;
553                         else if(eed->v1==efa->v3) vi10 = 3;
554                         else if(efa->v4 && eed->v1==efa->v4) vi10 = 4;
555
556                         if(eed->v2==efa->v1) vi11 = 1;
557                         else if(eed->v2==efa->v2) vi11 = 2;
558                         else if(eed->v2==efa->v3) vi11 = 3;
559                         else if(efa->v4 && eed->v2==efa->v4) vi11 = 4;
560
561                         if(efa->v4) {
562                                 if(vi11==1 && vi10==4) vi10 = 0;
563                                 if(vi11==4 && vi10==1) vi11 = 0;
564                         }
565                         else {
566                                 if(vi11==1 && vi10==3) vi10 = 0;
567                                 if(vi11==3 && vi10==1) vi11 = 0;
568                         }
569
570                         if(((vi00>vi01) && (vi10>vi11)) ||
571                                 ((vi00<vi01) && (vi10<vi11)))
572                                 count++;
573                         else
574                                 count--;
575                 }
576
577                 efa = efa->next;
578         }
579
580         /* set up smoothness according voting of face in neighbourhood */
581         if(smooth >= 0)
582                 eface->flag |= ME_SMOOTH;
583         else
584                 eface->flag &= ~ME_SMOOTH;
585
586         /* flip face, when too much "face normals" in neighbourhood is different */
587         if(count > 0) {
588                 flipface(eface);
589 #ifdef WITH_VERSE
590                 if(eface->vface) {
591                         struct VNode *vnode;
592                         struct VLayer *vlayer;
593                         vnode = (VNode*)((Mesh*)G.obedit->data)->vnode;
594                         vlayer = find_verse_layer_type((VGeomData*)vnode->data, POLYGON_LAYER);
595                         add_item_to_send_queue(&(vlayer->queue), (void*)eface->vface, VERSE_FACE);
596                 }
597 #endif
598         }
599 }
600
601 void addfaces_from_edgenet()
602 {
603         EditVert *eve1, *eve2, *eve3, *eve4;
604         EditMesh *em= G.editMesh;
605         
606         for(eve1= em->verts.first; eve1; eve1= eve1->next) {
607                 for(eve2= em->verts.first; (eve1->f & 1) && eve2; eve2= eve2->next) {
608                         if(findedgelist(eve1,eve2)) {
609                                 for(eve3= em->verts.first; (eve2->f & 1) && eve3; eve3= eve3->next) {
610                                         if((eve2!=eve3 && (eve3->f & 1) && findedgelist(eve1,eve3))) {
611                                                 EditEdge *sh_edge= NULL;
612                                                 EditVert *sh_vert= NULL;
613                                                 
614                                                 sh_edge= findedgelist(eve2,eve3);
615                                                 
616                                                 if(sh_edge) { /* Add a triangle */
617                                                         if(!exist_face_overlaps(eve1,eve2,eve3,NULL))
618                                                                 fix_new_face(addfacelist(eve1,eve2,eve3,NULL,NULL,NULL));
619                                                 }
620                                                 else { /* Check for a shared vertex */
621                                                         for(eve4= em->verts.first; eve4; eve4= eve4->next) {
622                                                                 if(eve4!=eve1 && eve4!=eve2 && eve4!=eve3 && (eve4->f & 1) &&
623                                                                    !findedgelist(eve1,eve4) && findedgelist(eve2,eve4) &&
624                                                                    findedgelist(eve3,eve4)) {
625                                                                         sh_vert= eve4;
626                                                                         break;
627                                                                 }
628                                                         }
629                                                         
630                                                         if(sh_vert) {
631                                                                 if(sh_vert) {
632                                                                         if(!exist_face_overlaps(eve1,eve2,eve4,eve3))
633                                                                                 fix_new_face(addfacelist(eve1,eve2,eve4,eve3,NULL,NULL));
634                                                                 }
635                                                         }
636                                                 }
637                                         }
638                                 }
639                         }
640                 }
641         }
642
643         countall();
644
645         EM_select_flush();
646         
647         BIF_undo_push("Add faces");
648         allqueue(REDRAWVIEW3D, 0);
649         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
650 }
651
652 void addedgeface_mesh(void)
653 {
654         EditMesh *em = G.editMesh;
655         EditVert *eve, *neweve[4];
656         EditEdge *eed;
657         EditFace *efa;
658         short amount=0;
659
660         if( (G.vd->lay & G.obedit->lay)==0 ) return;
661         if(multires_test()) return;
662
663         /* how many selected ? */
664         if(G.scene->selectmode & SCE_SELECT_EDGE) {
665                 /* in edge mode finding selected vertices means flushing down edge codes... */
666                 /* can't make face with only edge selection info... */
667                 EM_selectmode_set();
668         }
669         
670         for(eve= em->verts.first; eve; eve= eve->next) {
671                 if(eve->f & SELECT) {
672                         amount++;
673                         if(amount>4) break;                     
674                         neweve[amount-1]= eve;
675                 }
676         }
677
678         if(amount==2) {
679                 eed= addedgelist(neweve[0], neweve[1], NULL);
680                 EM_select_edge(eed, 1);
681                 BIF_undo_push("Add edge");
682                 allqueue(REDRAWVIEW3D, 0);
683                 countall();
684                 DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);     
685                 return;
686         }
687         else if(amount > 4) {
688                 
689                 /* Python Menu */
690                 BPyMenu *pym;
691                 char menu_number[3];
692                 int i=0, has_pymenu=0, ret;
693                 
694                 /* facemenu, will add python items */
695                 char facemenu[4096]= "Make Faces%t|Auto%x1|Make FGon%x2|Clear FGon%x3";
696                 
697                 /* note that we account for the 10 previous entries with i+4: */
698                 for (pym = BPyMenuTable[PYMENU_MESHFACEKEY]; pym; pym = pym->next, i++) {
699                         
700                         if (!has_pymenu) {
701                                 strcat(facemenu, "|%l");
702                                 has_pymenu = 1;
703                         }
704                         
705                         strcat(facemenu, "|");
706                         strcat(facemenu, pym->name);
707                         strcat(facemenu, " %x");
708                         sprintf(menu_number, "%d", i+4);
709                         strcat(facemenu, menu_number);
710                 }
711                 
712                 ret= pupmenu(facemenu);
713                 
714                 if(ret==1) addfaces_from_edgenet();
715                 else if(ret==2) make_fgon(1);
716                 else if(ret==3) make_fgon(0);
717                 else if (ret >= 4) {
718                         BPY_menu_do_python(PYMENU_MESHFACEKEY, ret - 4);
719                         return;
720                 }
721                 return;
722         }
723         else if(amount<2) {
724                 error("Incorrect number of vertices to make edge/face");
725                 return;
726         }
727
728         efa= NULL; // check later
729
730         if(amount==3) {
731                 
732                 if(exist_face_overlaps(neweve[0], neweve[1], neweve[2], NULL)==0) {
733                         efa= addfacelist(neweve[0], neweve[1], neweve[2], 0, NULL, NULL);
734                         EM_select_face(efa, 1);
735                 }
736                 else error("The selected vertices already form a face");
737         }
738         else if(amount==4) {
739                 /* this test survives when theres 2 triangles */
740                 if(exist_face(neweve[0], neweve[1], neweve[2], neweve[3])==0) {
741                         int tria= 0;
742                         
743                         /* remove trias if they exist, 4 cases.... */
744                         if(exist_face(neweve[0], neweve[1], neweve[2], NULL)) tria++;
745                         if(exist_face(neweve[0], neweve[1], neweve[3], NULL)) tria++;
746                         if(exist_face(neweve[0], neweve[2], neweve[3], NULL)) tria++;
747                         if(exist_face(neweve[1], neweve[2], neweve[3], NULL)) tria++;
748                 
749                         if(tria==2) join_triangles();
750                         else if(exist_face_overlaps(neweve[0], neweve[1], neweve[2], neweve[3])==0) {
751                                  /* If there are 4 Verts, But more selected edges, we need to call addfaces_from_edgenet */
752                                         EditEdge *eedcheck;
753                                         int count;
754                                         count = 0;
755                                         for(eedcheck= em->edges.first; eedcheck; eedcheck= eedcheck->next) {
756                                                 if(eedcheck->f & SELECT) {
757                                                         count++;
758                                                 }
759                                         }       
760                                 
761                                 if(count++ > 4){
762                                         addfaces_from_edgenet();
763                                         return;
764                                 } else {
765                                 /* if 4 edges exist, we just create the face, convex or not */
766                                         efa= addface_from_edges();
767                                         if(efa==NULL) {
768                                                 
769                                                 /* the order of vertices can be anything, 6 cases to check */
770                                                 if( convex(neweve[0]->co, neweve[1]->co, neweve[2]->co, neweve[3]->co) ) {
771                                                         efa= addfacelist(neweve[0], neweve[1], neweve[2], neweve[3], NULL, NULL);
772                                                 }
773                                                 else if( convex(neweve[0]->co, neweve[2]->co, neweve[3]->co, neweve[1]->co) ) {
774                                                         efa= addfacelist(neweve[0], neweve[2], neweve[3], neweve[1], NULL, NULL);
775                                                 }
776                                                 else if( convex(neweve[0]->co, neweve[2]->co, neweve[1]->co, neweve[3]->co) ) {
777                                                         efa= addfacelist(neweve[0], neweve[2], neweve[1], neweve[3], NULL, NULL);
778                                                 }
779                                                 else if( convex(neweve[0]->co, neweve[1]->co, neweve[3]->co, neweve[2]->co) ) {
780                                                         efa= addfacelist(neweve[0], neweve[1], neweve[3], neweve[2], NULL, NULL);
781                                                 }
782                                                 else if( convex(neweve[0]->co, neweve[3]->co, neweve[2]->co, neweve[1]->co) ) {
783                                                         efa= addfacelist(neweve[0], neweve[3], neweve[2], neweve[1], NULL, NULL);
784                                                 }
785                                                 else if( convex(neweve[0]->co, neweve[3]->co, neweve[1]->co, neweve[2]->co) ) {
786                                                         efa= addfacelist(neweve[0], neweve[3], neweve[1], neweve[2], NULL, NULL);
787                                                 }
788                                                 else printf("cannot find nice quad from concave set of vertices\n");
789                                         }
790                                 }
791                         }
792                         else error("The selected vertices already form a face");
793                 }
794                 else error("The selected vertices already form a face");
795         }
796         
797         if(efa) {
798                 EM_select_face(efa, 1);
799
800                 fix_new_face(efa);
801                 
802                 recalc_editnormals();
803                 BIF_undo_push("Add face");
804         }
805         
806         countall();
807         allqueue(REDRAWVIEW3D, 0);
808         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);     
809 }
810
811
812 void adduplicate_mesh(void)
813 {
814
815         TEST_EDITMESH
816         if(multires_test()) return;
817
818         waitcursor(1);
819
820         adduplicateflag(SELECT);
821
822         waitcursor(0);
823         countall(); 
824
825                 /* We need to force immediate calculation here because 
826                 * transform may use derived objects (which are now stale).
827                 *
828                 * This shouldn't be necessary, derived queries should be
829                 * automatically building this data if invalid. Or something.
830                 */
831         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);     
832         object_handle_update(G.obedit);
833
834         BIF_TransformSetUndo("Add Duplicate");
835         initTransform(TFM_TRANSLATION, CTX_NO_PET);
836         Transform();
837 }
838
839 /* check whether an object to add mesh to exists, if not, create one
840 * returns 1 if new object created, else 0 */
841 int confirm_objectExists( Mesh **me, float mat[][3] )
842 {
843         int newob = 0;
844         
845         /* deselectall */
846         EM_clear_flag_all(SELECT);
847         
848         /* if no obedit: new object and enter editmode */
849         if(G.obedit==NULL) {
850                 /* add_object actually returns an object ! :-)
851                 But it also stores the added object struct in
852                 G.scene->basact->object (BASACT->object) */
853
854                 add_object_draw(OB_MESH);
855
856                 G.obedit= BASACT->object;
857                 
858                 where_is_object(G.obedit);
859                 
860                 make_editMesh();
861                 setcursor_space(SPACE_VIEW3D, CURSOR_EDIT);
862                 newob= 1;
863         }
864         *me = G.obedit->data;
865         
866         /* imat and center and size */
867         Mat3CpyMat4(mat, G.obedit->obmat);
868         
869         return newob;
870 }
871
872 void make_prim(int type, float imat[3][3], int tot, int seg,
873                 int subdiv, float dia, float d, int ext, int fill,
874         float cent[3])
875 {
876         /*
877          * type - for the type of shape
878          * dia - the radius for cone,sphere cylinder etc.
879          * d - depth for the cone
880          * ext - ?
881          * fill - end capping, and option to fill in circle
882          * cent[3] - center of the data. 
883          * */
884         
885         EditMesh *em = G.editMesh;
886         EditVert *eve, *v1=NULL, *v2, *v3, *v4=NULL, *vtop, *vdown;
887         float phi, phid, vec[3];
888         float q[4], cmat[3][3], nor[3]= {0.0, 0.0, 0.0};
889         short a, b;
890
891         phid= 2*M_PI/tot;
892         phi= .25*M_PI;
893
894         switch(type) {
895         case 10: /*  grid */
896                 /* clear flags */
897                 eve= em->verts.first;
898                 while(eve) {
899                         eve->f= 0;
900                         eve= eve->next;
901                 }
902                 /* one segment first: the X axis */
903                 phi= 1.0; 
904                 phid= 2.0/((float)tot-1);
905                 for(a=0;a<tot;a++) {
906                         vec[0]= cent[0]+dia*phi;
907                         vec[1]= cent[1]- dia;
908                         vec[2]= cent[2];
909                         Mat3MulVecfl(imat,vec);
910                         eve= addvertlist(vec, NULL);
911                         eve->f= 1+2+4;
912                         if (a) {
913                                 addedgelist(eve->prev, eve, NULL);
914                         }
915                         phi-=phid;
916                 }
917                 /* extrude and translate */
918                 vec[0]= vec[2]= 0.0;
919                 vec[1]= dia*phid;
920                 Mat3MulVecfl(imat, vec);
921                 for(a=0;a<seg-1;a++) {
922                         extrudeflag_vert(2, nor);       // nor unused
923                         translateflag(2, vec);
924                 }
925                 break;
926         case 11: /*  UVsphere */
927                 
928                 /* clear all flags */
929                 eve= em->verts.first;
930                 while(eve) {
931                         eve->f= 0;
932                         eve= eve->next;
933                 }
934                 
935                 /* one segment first */
936                 phi= 0; 
937                 phid/=2;
938                 for(a=0; a<=tot; a++) {
939                         vec[0]= dia*sin(phi);
940                         vec[1]= 0.0;
941                         vec[2]= dia*cos(phi);
942                         eve= addvertlist(vec, NULL);
943                         eve->f= 1+2+4;
944                         if(a==0) v1= eve;
945                         else addedgelist(eve->prev, eve, NULL);
946                         phi+= phid;
947                 }
948                 
949                 /* extrude and rotate */
950                 phi= M_PI/seg;
951                 q[0]= cos(phi);
952                 q[3]= sin(phi);
953                 q[1]=q[2]= 0;
954                 QuatToMat3(q, cmat);
955                 
956                 for(a=0; a<seg; a++) {
957                         extrudeflag_vert(2, nor); // nor unused
958                         rotateflag(2, v1->co, cmat);
959                 }
960
961                 removedoublesflag(4, 0, 0.0001);
962
963                 /* and now do imat */
964                 eve= em->verts.first;
965                 while(eve) {
966                         if(eve->f & SELECT) {
967                                 VecAddf(eve->co,eve->co,cent);
968                                 Mat3MulVecfl(imat,eve->co);
969                         }
970                         eve= eve->next;
971                 }
972                 break;
973         case 12: /* Icosphere */
974                 {
975                         EditVert *eva[12];
976                         EditEdge *eed;
977                         
978                         /* clear all flags */
979                         eve= em->verts.first;
980                         while(eve) {
981                                 eve->f= 0;
982                                 eve= eve->next;
983                         }
984                         dia/=200;
985                         for(a=0;a<12;a++) {
986                                 vec[0]= dia*icovert[a][0];
987                                 vec[1]= dia*icovert[a][1];
988                                 vec[2]= dia*icovert[a][2];
989                                 eva[a]= addvertlist(vec, NULL);
990                                 eva[a]->f= 1+2;
991                         }
992                         for(a=0;a<20;a++) {
993                                 EditFace *evtemp;
994                                 v1= eva[ icoface[a][0] ];
995                                 v2= eva[ icoface[a][1] ];
996                                 v3= eva[ icoface[a][2] ];
997                                 evtemp = addfacelist(v1, v2, v3, 0, NULL, NULL);
998                                 evtemp->e1->f = 1+2;
999                                 evtemp->e2->f = 1+2;
1000                                 evtemp->e3->f = 1+2;
1001                         }
1002
1003                         dia*=200;
1004                         for(a=1; a<subdiv; a++) esubdivideflag(2, dia, 0,1,0);
1005                         /* and now do imat */
1006                         eve= em->verts.first;
1007                         while(eve) {
1008                                 if(eve->f & 2) {
1009                                         VecAddf(eve->co,eve->co,cent);
1010                                         Mat3MulVecfl(imat,eve->co);
1011                                 }
1012                                 eve= eve->next;
1013                         }
1014                         
1015                         // Clear the flag 2 from the edges
1016                         for(eed=em->edges.first;eed;eed=eed->next){
1017                                 if(eed->f & 2){
1018                                            eed->f &= !2;
1019                                 }   
1020                         }
1021                 }
1022                 break;
1023         case 13: /* Monkey */
1024                 {
1025                         extern int monkeyo, monkeynv, monkeynf;
1026                         extern signed char monkeyf[][4];
1027                         extern signed char monkeyv[][3];
1028                         EditVert **tv= MEM_mallocN(sizeof(*tv)*monkeynv*2, "tv");
1029                         EditFace *efa;
1030                         int i;
1031
1032                         for (i=0; i<monkeynv; i++) {
1033                                 float v[3];
1034                                 v[0]= (monkeyv[i][0]+127)/128.0, v[1]= monkeyv[i][1]/128.0, v[2]= monkeyv[i][2]/128.0;
1035                                 tv[i]= addvertlist(v, NULL);
1036                                 tv[i]->f |= SELECT;
1037                                 tv[monkeynv+i]= (fabs(v[0]= -v[0])<0.001)?tv[i]:addvertlist(v, NULL);
1038                                 tv[monkeynv+i]->f |= SELECT;
1039                         }
1040                         for (i=0; i<monkeynf; i++) {
1041                                 efa= addfacelist(tv[monkeyf[i][0]+i-monkeyo], tv[monkeyf[i][1]+i-monkeyo], tv[monkeyf[i][2]+i-monkeyo], (monkeyf[i][3]!=monkeyf[i][2])?tv[monkeyf[i][3]+i-monkeyo]:NULL, NULL, NULL);
1042                                 efa= addfacelist(tv[monkeynv+monkeyf[i][2]+i-monkeyo], tv[monkeynv+monkeyf[i][1]+i-monkeyo], tv[monkeynv+monkeyf[i][0]+i-monkeyo], (monkeyf[i][3]!=monkeyf[i][2])?tv[monkeynv+monkeyf[i][3]+i-monkeyo]:NULL, NULL, NULL);
1043                         }
1044
1045                         MEM_freeN(tv);
1046
1047                         /* and now do imat */
1048                         for(eve= em->verts.first; eve; eve= eve->next) {
1049                                 if(eve->f & SELECT) {
1050                                         VecAddf(eve->co,eve->co,cent);
1051                                         Mat3MulVecfl(imat,eve->co);
1052                                 }
1053                         }
1054                         recalc_editnormals();
1055                 }
1056                 break;
1057         default: /* all types except grid, sphere... */
1058                 if(ext==0 && type!=7) d= 0;
1059         
1060                 /* vertices */
1061                 vtop= vdown= v1= v2= 0;
1062                 for(b=0; b<=ext; b++) {
1063                         for(a=0; a<tot; a++) {
1064                                 
1065                                 vec[0]= cent[0]+dia*sin(phi);
1066                                 vec[1]= cent[1]+dia*cos(phi);
1067                                 vec[2]= cent[2]+d;
1068                                 
1069                                 Mat3MulVecfl(imat, vec);
1070                                 eve= addvertlist(vec, NULL);
1071                                 eve->f= SELECT;
1072                                 if(a==0) {
1073                                         if(b==0) v1= eve;
1074                                         else v2= eve;
1075                                 }
1076                                 phi+=phid;
1077                         }
1078                         d= -d;
1079                 }
1080                 /* center vertices */
1081                 /* type 7, a cone can only have 1 one side filled
1082                  * if the cone has no capping, dont add vtop */
1083                 if((fill && type>1) || type == 7) {
1084                         VECCOPY(vec,cent);
1085                         vec[2]-= -d;
1086                         Mat3MulVecfl(imat,vec);
1087                         vdown= addvertlist(vec, NULL);
1088                         if((ext || type==7) && fill) {
1089                                 VECCOPY(vec,cent);
1090                                 vec[2]-= d;
1091                                 Mat3MulVecfl(imat,vec);
1092                                 vtop= addvertlist(vec, NULL);
1093                         }
1094                 } else {
1095                         vdown= v1;
1096                         vtop= v2;
1097                 }
1098                 if(vtop) vtop->f= SELECT;
1099                 if(vdown) vdown->f= SELECT;
1100         
1101                 /* top and bottom face */
1102                 if(fill || type==7) {
1103                         if(tot==4 && (type==0 || type==1)) {
1104                                 v3= v1->next->next;
1105                                 if(ext) v4= v2->next->next;
1106                                 
1107                                 addfacelist(v3, v1->next, v1, v3->next, NULL, NULL);
1108                                 if(ext) addfacelist(v2, v2->next, v4, v4->next, NULL, NULL);
1109                                 
1110                         }
1111                         else {
1112                                 v3= v1;
1113                                 v4= v2;
1114                                 for(a=1; a<tot; a++) {
1115                                         addfacelist(vdown, v3, v3->next, 0, NULL, NULL);
1116                                         v3= v3->next;
1117                                         if(ext && fill) {
1118                                                 addfacelist(vtop, v4, v4->next, 0, NULL, NULL);
1119                                                 v4= v4->next;
1120                                         }
1121                                 }
1122                                 if(type>1) {
1123                                         addfacelist(vdown, v3, v1, 0, NULL, NULL);
1124                                         if(ext) addfacelist(vtop, v4, v2, 0, NULL, NULL);
1125                                 }
1126                         }
1127                 }
1128                 else if(type==4) {  /* we need edges for a circle */
1129                         v3= v1;
1130                         for(a=1;a<tot;a++) {
1131                                 addedgelist(v3, v3->next, NULL);
1132                                 v3= v3->next;
1133                         }
1134                         addedgelist(v3, v1, NULL);
1135                 }
1136                 /* side faces */
1137                 if(ext) {
1138                         v3= v1;
1139                         v4= v2;
1140                         for(a=1; a<tot; a++) {
1141                                 addfacelist(v3, v3->next, v4->next, v4, NULL, NULL);
1142                                 v3= v3->next;
1143                                 v4= v4->next;
1144                         }
1145                         addfacelist(v3, v1, v2, v4, NULL, NULL);
1146                 }
1147                 else if(type==7 && fill) {
1148                         /* add the bottom flat area of the cone
1149                          * if capping is disabled dont bother */
1150                         v3= v1;
1151                         for(a=1; a<tot; a++) {
1152                                 addfacelist(vtop, v3->next, v3, 0, NULL, NULL);
1153                                 v3= v3->next;
1154                         }
1155                         addfacelist(vtop, v1, v3, 0, NULL, NULL);
1156                 }
1157         }
1158         /* simple selection flush OK, based on fact it's a single model */
1159         EM_select_flush(); /* flushes vertex -> edge -> face selection */
1160         
1161         if(type!=0 && type!=13)
1162                 righthandfaces(1);      /* otherwise monkey has eyes in wrong direction */
1163 }
1164
1165 void add_primitiveMesh(int type)
1166 {
1167         Mesh *me;
1168         float *curs, d, dia, phi, phid, cent[3], imat[3][3], mat[3][3];
1169         float cmat[3][3];
1170         static int tot=32, seg=32, subdiv=2,
1171                 /* so each type remembers its fill setting */
1172                 fill_circle=0, fill_cone=1, fill_cylinder=1;
1173         
1174         int ext=0, fill=0, totoud, newob=0;
1175         char *undostr="Add Primitive";
1176         char *name=NULL;
1177         
1178         if(G.scene->id.lib) return;
1179         
1180         /* this function also comes from an info window */
1181         if ELEM(curarea->spacetype, SPACE_VIEW3D, SPACE_INFO); else return;
1182         if(G.vd==0) return;
1183
1184         if (G.obedit && G.obedit->type==OB_MESH && multires_test()) return;
1185         
1186         /* if editmode exists for other type, it exits */
1187         check_editmode(OB_MESH);
1188         
1189         if(G.f & (G_VERTEXPAINT+G_TEXTUREPAINT)) {
1190                 G.f &= ~(G_VERTEXPAINT+G_TEXTUREPAINT);
1191                 setcursor_space(SPACE_VIEW3D, CURSOR_EDIT);
1192         }
1193
1194         totoud= tot; /* store, and restore when cube/plane */
1195         
1196         dia= G.vd->grid;
1197         d= G.vd->grid;
1198         
1199         /* ext==extrudeflag, tot==amount of vertices in basis */
1200         switch(type) {
1201         case 0:         /* plane */
1202                 tot= 4;
1203                 ext= 0;
1204                 fill= 1;
1205                 newob = confirm_objectExists( &me, mat );
1206                 if(newob) name = "Plane";
1207                 undostr="Add Plane";
1208                 break;
1209         case 1:         /* cube  */
1210                 tot= 4;
1211                 ext= 1;
1212                 fill= 1;
1213                 newob = confirm_objectExists( &me, mat );
1214                 if(newob) name = "Cube";
1215                 undostr="Add Cube";
1216                 break;
1217         case 4:         /* circle  */
1218                 add_numbut(0, NUM|INT, "Vertices:", 3, 500, &tot, NULL);
1219                 add_numbut(1, NUM|FLO, "Radius:", 0.001*G.vd->grid, 100*G.vd->grid, &dia, NULL);
1220                 add_numbut(2, TOG|INT, "Fill", 0, 0, &(fill_circle), NULL);
1221                 if (!(do_clever_numbuts("Add Circle", 3, REDRAW))) return;
1222                 ext= 0;
1223                 fill = fill_circle;
1224                 newob = confirm_objectExists( &me, mat );
1225                 if(newob) name = "Circle";
1226                 undostr="Add Circle";
1227                 break;
1228         case 5:         /* cylinder  */
1229                 d*=2;
1230                 add_numbut(0, NUM|INT, "Vertices:", 2, 500, &tot, NULL);
1231                 add_numbut(1, NUM|FLO, "Radius:", 0.001*G.vd->grid, 100*G.vd->grid, &dia, NULL);
1232                 add_numbut(2, NUM|FLO, "Depth:", 0.001*G.vd->grid, 100*G.vd->grid, &d, NULL);
1233                 add_numbut(3, TOG|INT, "Cap Ends", 0, 0, &(fill_cylinder), NULL);
1234                 if (!(do_clever_numbuts("Add Cylinder", 4, REDRAW))) return;
1235                 ext= 1;
1236                 fill = fill_cylinder;
1237                 d/=2;
1238                 newob = confirm_objectExists( &me, mat );
1239                 if(newob) {
1240                         if (fill)       name = "Cylinder";
1241                         else            name = "Tube";
1242                 }
1243                 undostr="Add Cylinder";
1244                 break;
1245         case 7:         /* cone  */
1246                 d*=2;
1247                 add_numbut(0, NUM|INT, "Vertices:", 2, 500, &tot, NULL);
1248                 add_numbut(1, NUM|FLO, "Radius:", 0.001*G.vd->grid, 100*G.vd->grid, &dia, NULL);
1249                 add_numbut(2, NUM|FLO, "Depth:", 0.001*G.vd->grid, 100*G.vd->grid, &d, NULL);
1250                 add_numbut(3, TOG|INT, "Cap End", 0, 0, &(fill_cone), NULL);
1251                 if (!(do_clever_numbuts("Add Cone", 4, REDRAW))) return;
1252                 d/=2;
1253                 ext= 0;
1254                 fill = fill_cone;
1255                 newob = confirm_objectExists( &me, mat );
1256                 if(newob) name = "Cone";
1257                 undostr="Add Cone";
1258                 break;
1259         case 10:        /* grid */
1260                 add_numbut(0, NUM|INT, "X res:", 3, 1000, &tot, NULL);
1261                 add_numbut(1, NUM|INT, "Y res:", 3, 1000, &seg, NULL);
1262                 if (!(do_clever_numbuts("Add Grid", 2, REDRAW))) return; 
1263                 newob = confirm_objectExists( &me, mat );
1264                 if(newob) name = "Grid";
1265                 undostr="Add Grid";
1266                 break;
1267         case 11:        /* UVsphere */
1268                 add_numbut(0, NUM|INT, "Segments:", 3, 500, &seg, NULL);
1269                 add_numbut(1, NUM|INT, "Rings:", 3, 500, &tot, NULL);
1270                 add_numbut(2, NUM|FLO, "Radius:", 0.001*G.vd->grid, 100*G.vd->grid, &dia, NULL);
1271                 
1272                 if (!(do_clever_numbuts("Add UV Sphere", 3, REDRAW))) return;
1273                 
1274                 newob = confirm_objectExists( &me, mat );
1275                 if(newob) name = "Sphere";
1276                 undostr="Add UV Sphere";
1277                 break;
1278         case 12:        /* Icosphere */
1279                 add_numbut(0, NUM|INT, "Subdivision:", 1, 8, &subdiv, NULL);
1280                 add_numbut(1, NUM|FLO, "Radius:", 0.001*G.vd->grid, 100*G.vd->grid, &dia, NULL);
1281                 if (!(do_clever_numbuts("Add Ico Sphere", 2, REDRAW))) return;
1282                 
1283                 newob = confirm_objectExists( &me, mat );
1284                 if(newob) name = "Sphere";
1285                 undostr="Add Ico Sphere";
1286                 break;
1287         case 13:        /* Monkey */
1288                 newob = confirm_objectExists( &me, mat );
1289                 if(newob) name = "Suzanne";
1290                 undostr="Add Monkey";
1291                 break;
1292         default:
1293                 newob = confirm_objectExists( &me, mat );
1294                 break;
1295         }
1296
1297         if( name!=NULL ) {
1298                 rename_id((ID *)G.obedit, name );
1299                 rename_id((ID *)me, name );
1300         }
1301         
1302         d = -d;
1303         curs= give_cursor();
1304         VECCOPY(cent, curs);
1305         cent[0]-= G.obedit->obmat[3][0];
1306         cent[1]-= G.obedit->obmat[3][1];
1307         cent[2]-= G.obedit->obmat[3][2];
1308
1309         if ( !(newob) || U.flag & USER_ADD_VIEWALIGNED) Mat3CpyMat4(imat, G.vd->viewmat);
1310         else Mat3One(imat);
1311         Mat3MulVecfl(imat, cent);
1312         Mat3MulMat3(cmat, imat, mat);
1313         Mat3Inv(imat,cmat);
1314         
1315         
1316         if(type == 0 || type == 1) /* plane, cube (diameter of 1.41 makes it unit size) */
1317                 dia *= sqrt(2.0);
1318
1319         phid= 2*M_PI/tot;
1320         phi= .25*M_PI;
1321
1322         make_prim(type, imat, tot, seg, subdiv, dia, d, ext, fill, cent);
1323
1324         if(type<2) tot = totoud;
1325
1326         /* simple selection flush OK, based on fact it's a single model */
1327         EM_select_flush(); // flushes vertex -> edge -> face selection
1328         
1329         if(type!=0 && type!=13) righthandfaces(1);      /* otherwise monkey has eyes in wrong direction... */
1330         countall();
1331
1332         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1333         
1334         /* if a new object was created, it stores it in Mesh, for reload original data and undo */
1335         if ( !(newob) || U.flag & USER_ADD_EDITMODE) {
1336                 if(newob) load_editMesh();
1337         } else {
1338                 exit_editmode(2);
1339         }
1340         
1341         allqueue(REDRAWINFO, 1);        /* 1, because header->win==0! */        
1342         allqueue(REDRAWALL, 0);
1343         
1344         BIF_undo_push(undostr);
1345 }
1346