Fix syntax for ID keyword.
[blender-staging.git] / source / blender / editors / mesh / 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2004 by Blender Foundation.
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 #include "MEM_guardedalloc.h"
36
37 #include "DNA_mesh_types.h"
38 #include "DNA_meshdata_types.h"
39 #include "DNA_object_types.h"
40 #include "DNA_scene_types.h"
41 #include "DNA_space_types.h"
42 #include "DNA_screen_types.h"
43 #include "DNA_userdef_types.h"
44 #include "DNA_view3d_types.h"
45 #include "DNA_windowmanager_types.h"
46
47 #include "RNA_types.h"
48 #include "RNA_define.h"
49 #include "RNA_access.h"
50
51 #include "BLI_blenlib.h"
52 #include "BLI_math.h"
53 #include "BLI_editVert.h"
54
55 #include "BKE_context.h"
56 #include "BKE_depsgraph.h"
57 #include "BKE_global.h"
58 #include "BKE_library.h"
59 #include "BKE_mesh.h"
60 #include "BKE_object.h"
61 #include "BKE_utildefines.h"
62 #include "BKE_report.h"
63
64 #include "WM_api.h"
65 #include "WM_types.h"
66
67 #include "ED_mesh.h"
68 #include "ED_retopo.h"
69 #include "ED_screen.h"
70 #include "ED_transform.h"
71 #include "ED_util.h"
72 #include "ED_view3d.h"
73 #include "ED_object.h"
74
75 #include "mesh_intern.h"
76
77 /* bpymenu removed XXX */
78
79 /* XXX */
80 #define add_numbut(a, b, c, d, e, f, g) {}
81 /* XXX */
82
83 static float icovert[12][3] = {
84         {0.0f,0.0f,-200.0f}, 
85         {144.72f, -105.144f,-89.443f},
86         {-55.277f, -170.128,-89.443f}, 
87         {-178.885f,0.0f,-89.443f},
88         {-55.277f,170.128f,-89.443f}, 
89         {144.72f,105.144f,-89.443f},
90         {55.277f,-170.128f,89.443f},
91         {-144.72f,-105.144f,89.443f},
92         {-144.72f,105.144f,89.443f},
93         {55.277f,170.128f,89.443f},
94         {178.885f,0.0f,89.443f},
95         {0.0f,0.0f,200.0f}
96 };
97 static short icoface[20][3] = {
98         {1,0,2},
99         {1,0,5},
100         {2,0,3},
101         {3,0,4},
102         {4,0,5},
103         {1,5,10},
104         {2,1,6},
105         {3,2,7},
106         {4,3,8},
107         {5,4,9},
108         {10,1,6},
109         {6,2,7},
110         {7,3,8},
111         {8,4,9},
112         {9,5,10},
113         {6,10,11},
114         {7,6,11},
115         {8,7,11},
116         {9,8,11},
117         {10,9,11}
118 };
119
120 /* *************** add-click-mesh (extrude) operator ************** */
121
122 static int dupli_extrude_cursor(bContext *C, wmOperator *op, wmEvent *event)
123 {
124         ViewContext vc;
125         EditVert *eve, *v1;
126         float min[3], max[3];
127         int done= 0;
128         
129         em_setup_viewcontext(C, &vc);
130         
131         INIT_MINMAX(min, max);
132         
133         for(v1= vc.em->verts.first;v1; v1=v1->next) {
134                 if(v1->f & SELECT) {
135                         DO_MINMAX(v1->co, min, max);
136                         done= 1;
137                 }
138         }
139
140         /* call extrude? */
141         if(done) {
142                 EditEdge *eed;
143                 float vec[3], cent[3], mat[3][3];
144                 float nor[3]= {0.0, 0.0, 0.0};
145                 
146                 /* check for edges that are half selected, use for rotation */
147                 done= 0;
148                 for(eed= vc.em->edges.first; eed; eed= eed->next) {
149                         if( (eed->v1->f & SELECT)+(eed->v2->f & SELECT) == SELECT ) {
150                                 if(eed->v1->f & SELECT) sub_v3_v3v3(vec, eed->v1->co, eed->v2->co);
151                                 else sub_v3_v3v3(vec, eed->v2->co, eed->v1->co);
152                                 add_v3_v3v3(nor, nor, vec);
153                                 done= 1;
154                         }
155                 }
156                 if(done) normalize_v3(nor);
157                 
158                 /* center */
159                 add_v3_v3v3(cent, min, max);
160                 mul_v3_fl(cent, 0.5f);
161                 VECCOPY(min, cent);
162                 
163                 mul_m4_v3(vc.obedit->obmat, min);       // view space
164                 view3d_get_view_aligned_coordinate(&vc, min, event->mval);
165                 invert_m4_m4(vc.obedit->imat, vc.obedit->obmat); 
166                 mul_m4_v3(vc.obedit->imat, min); // back in object space
167                 
168                 sub_v3_v3v3(min, min, cent);
169                 
170                 /* calculate rotation */
171                 unit_m3(mat);
172                 if(done) {
173                         float dot;
174                         
175                         VECCOPY(vec, min);
176                         normalize_v3(vec);
177                         dot= INPR(vec, nor);
178
179                         if( fabs(dot)<0.999) {
180                                 float cross[3], si, q1[4];
181                                 
182                                 cross_v3_v3v3(cross, nor, vec);
183                                 normalize_v3(cross);
184                                 dot= 0.5f*saacos(dot);
185                                 si= (float)sin(dot);
186                                 q1[0]= (float)cos(dot);
187                                 q1[1]= cross[0]*si;
188                                 q1[2]= cross[1]*si;
189                                 q1[3]= cross[2]*si;
190                                 
191                                 quat_to_mat3( mat,q1);
192                         }
193                 }
194                 
195                 extrudeflag(vc.obedit, vc.em, SELECT, nor, 0);
196                 rotateflag(vc.em, SELECT, cent, mat);
197                 translateflag(vc.em, SELECT, min);
198                 
199                 recalc_editnormals(vc.em);
200         }
201         else {
202                 float mat[3][3],imat[3][3];
203                 float *curs= give_cursor(vc.scene, vc.v3d);
204                 
205                 VECCOPY(min, curs);
206                 view3d_get_view_aligned_coordinate(&vc, min, event->mval);
207                 
208                 eve= addvertlist(vc.em, 0, NULL);
209
210                 copy_m3_m4(mat, vc.obedit->obmat);
211                 invert_m3_m3(imat, mat);
212                 
213                 VECCOPY(eve->co, min);
214                 mul_m3_v3(imat, eve->co);
215                 sub_v3_v3v3(eve->co, eve->co, vc.obedit->obmat[3]);
216                 
217                 eve->f= SELECT;
218         }
219         
220         //retopo_do_all();
221         WM_event_add_notifier(C, NC_GEOM|ND_DATA, vc.obedit->data); 
222         DAG_id_flush_update(vc.obedit->data, OB_RECALC_DATA);
223         
224         return OPERATOR_FINISHED;
225 }
226
227 void MESH_OT_dupli_extrude_cursor(wmOperatorType *ot)
228 {
229         /* identifiers */
230         ot->name= "Duplicate or Extrude at 3D Cursor";
231         ot->description= "Duplicate and extrude selected vertices, edges or faces towards 3D Cursor";
232         ot->idname= "MESH_OT_dupli_extrude_cursor";
233         
234         /* api callbacks */
235         ot->invoke= dupli_extrude_cursor;
236         ot->poll= ED_operator_editmesh;
237         
238         /* flags */
239         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
240 }
241
242
243 /* ********************** */
244
245 /* selected faces get hidden edges */
246 int make_fgon(EditMesh *em, wmOperator *op, int make)
247 {
248         EditFace *efa;
249         EditEdge *eed;
250         EditVert *eve;
251         float *nor=NULL;        // reference
252         int done=0;
253         
254         if(make==0) {
255                 for(efa= em->faces.first; efa; efa= efa->next) {
256                         if(efa->f & SELECT) {
257                                 efa->fgonf= 0;
258                                 efa->e1->h &= ~EM_FGON;
259                                 efa->e2->h &= ~EM_FGON;
260                                 efa->e3->h &= ~EM_FGON;
261                                 if(efa->e4) efa->e4->h &= ~EM_FGON;
262                                 done= 1;
263                         }
264                 }
265                 EM_fgon_flags(em);      // redo flags and indices for fgons
266                 
267                 return done;
268         }
269
270         /* tagging edges. rule is:
271            - edge used by exactly 2 selected faces
272            - no vertices allowed with only tagged edges (return)
273            - face normals are allowed to difffer
274          
275         */
276         for(eed= em->edges.first; eed; eed= eed->next) {
277                 eed->f1= 0;     // amount of selected
278                 eed->f2= 0; // amount of unselected
279         }
280         
281         for(efa= em->faces.first; efa; efa= efa->next) {
282                 if(efa->f & SELECT) {
283                         if(nor==NULL) nor= efa->n;
284                         if(efa->e1->f1 < 3) efa->e1->f1++;
285                         if(efa->e2->f1 < 3) efa->e2->f1++;
286                         if(efa->e3->f1 < 3) efa->e3->f1++;
287                         if(efa->e4 && efa->e4->f1 < 3) efa->e4->f1++;
288                 }
289                 else {
290                         if(efa->e1->f2 < 3) efa->e1->f2++;
291                         if(efa->e2->f2 < 3) efa->e2->f2++;
292                         if(efa->e3->f2 < 3) efa->e3->f2++;
293                         if(efa->e4 && efa->e4->f2 < 3) efa->e4->f2++;
294                 }
295         }
296         // now eed->f1 becomes tagged edge
297         for(eed= em->edges.first; eed; eed= eed->next) {
298                 if(eed->f1==2 && eed->f2==0) eed->f1= 1;
299                 else eed->f1= 0;
300         }
301         
302         // no vertices allowed with only tagged edges
303         for(eve= em->verts.first; eve; eve= eve->next) eve->f1= 0;
304         for(eed= em->edges.first; eed; eed= eed->next) {
305                 if(eed->f1) {
306                         eed->v1->f1 |= 1;
307                         eed->v2->f1 |= 1;
308                 }
309                 else {
310                         eed->v1->f1 |= 2;
311                         eed->v2->f1 |= 2;
312                 }
313         }
314         for(eve= em->verts.first; eve; eve= eve->next) {
315                 if(eve->f1==1) break;
316         }
317         if(eve) {
318                 BKE_report(op->reports, RPT_ERROR, "Cannot make a polygon with interior vertices");
319                 return 0;
320         }
321         
322         // check for faces
323         if(nor==NULL) {
324                 BKE_report(op->reports, RPT_ERROR, "No faces were selected to make FGon");
325                 return 0;
326         }
327
328         // and there we go
329         for(eed= em->edges.first; eed; eed= eed->next) {
330                 if(eed->f1) {
331                         eed->h |= EM_FGON;
332                         done= 1;
333                 }
334         }
335         
336         if(done)
337                 EM_fgon_flags(em);      // redo flags and indices for fgons
338         return done;
339 }
340
341 static int make_fgon_exec(bContext *C, wmOperator *op)
342 {
343         Object *obedit= CTX_data_edit_object(C);
344         EditMesh *em= BKE_mesh_get_editmesh(((Mesh *)obedit->data));
345
346         if( make_fgon(em, op, 1) ) {
347                 DAG_id_flush_update(obedit->data, OB_RECALC_DATA);      
348                 WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
349
350                 BKE_mesh_end_editmesh(obedit->data, em);
351                 return OPERATOR_FINISHED;
352         }
353
354         BKE_mesh_end_editmesh(obedit->data, em);
355         return OPERATOR_CANCELLED;
356 }
357
358 void MESH_OT_fgon_make(struct wmOperatorType *ot)
359 {
360         /* identifiers */
361         ot->name= "Make F-gon";
362         ot->description= "Make fgon from selected faces";
363         ot->idname= "MESH_OT_fgon_make";
364         
365         /* api callbacks */
366         ot->exec= make_fgon_exec;
367         ot->poll= ED_operator_editmesh;
368         
369         /* flags */
370         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
371 }
372
373 static int clear_fgon_exec(bContext *C, wmOperator *op)
374 {
375         Object *obedit= CTX_data_edit_object(C);
376         EditMesh *em= BKE_mesh_get_editmesh(((Mesh *)obedit->data));
377         
378         if( make_fgon(em, op, 0) ) {
379                 DAG_id_flush_update(obedit->data, OB_RECALC_DATA);      
380                 WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
381                 
382                 BKE_mesh_end_editmesh(obedit->data, em);
383                 return OPERATOR_FINISHED;
384         }
385
386         BKE_mesh_end_editmesh(obedit->data, em);
387         return OPERATOR_CANCELLED;
388 }
389
390 void MESH_OT_fgon_clear(struct wmOperatorType *ot)
391 {
392         /* identifiers */
393         ot->name= "Clear F-gon";
394         ot->description= "Clear fgon from selected face";
395         ot->idname= "MESH_OT_fgon_clear";
396         
397         /* api callbacks */
398         ot->exec= clear_fgon_exec;
399         ot->poll= ED_operator_editmesh;
400         
401         /* flags */
402         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
403 }
404
405 /* precondition; 4 vertices selected, check for 4 edges and create face */
406 static EditFace *addface_from_edges(EditMesh *em)
407 {
408         EditEdge *eed, *eedar[4]={NULL, NULL, NULL, NULL};
409         EditVert *v1=NULL, *v2=NULL, *v3=NULL, *v4=NULL;
410         int a;
411         
412         /* find the 4 edges */
413         for(eed= em->edges.first; eed; eed= eed->next) {
414                 if( (eed->f & SELECT) || (eed->v1->f & eed->v2->f & SELECT) ) {
415                         if(eedar[0]==NULL) eedar[0]= eed;
416                         else if(eedar[1]==NULL) eedar[1]= eed;
417                         else if(eedar[2]==NULL) eedar[2]= eed;
418                         else eedar[3]= eed;
419                         
420                 }
421         }
422         
423         
424         if(eedar[3]) {
425                 /* first 2 points */
426                 v1= eedar[0]->v1;
427                 v2= eedar[0]->v2;
428                 
429                 /* find the 2 edges connected to first edge */
430                 for(a=1; a<4; a++) {
431                         if( eedar[a]->v1 == v2) v3= eedar[a]->v2;
432                         else if(eedar[a]->v2 == v2) v3= eedar[a]->v1;
433                         else if( eedar[a]->v1 == v1) v4= eedar[a]->v2;
434                         else if(eedar[a]->v2 == v1) v4= eedar[a]->v1;
435                 }
436                 
437                 /* verify if last edge exists */
438                 if(v3 && v4) {
439                         for(a=1; a<4; a++) {
440                                 if( eedar[a]->v1==v3 && eedar[a]->v2==v4) break;
441                                 if( eedar[a]->v2==v3 && eedar[a]->v1==v4) break;
442                         }
443                         if(a!=4) {
444                                 return addfacelist(em, v1, v2, v3, v4, NULL, NULL);
445                         }
446                 }
447         }
448         return NULL;
449 }
450
451 /* ******************************* */
452
453 /* this also allows to prevent triangles being made in quads */
454 static int compareface_overlaps(EditFace *vl1, EditFace *vl2)
455 {
456         EditVert *v1, *v2, *v3, *v4;
457         int equal= 0;
458         
459         v1= vl2->v1;
460         v2= vl2->v2;
461         v3= vl2->v3;
462         v4= vl2->v4;
463         
464         if(vl1==vl2) return 0;
465         
466         if(v4==NULL && vl1->v4==NULL) {
467                 if(vl1->v1==v1 || vl1->v2==v1 || vl1->v3==v1) equal++;
468                 if(vl1->v1==v2 || vl1->v2==v2 || vl1->v3==v2) equal++;
469                 if(vl1->v1==v3 || vl1->v2==v3 || vl1->v3==v3) equal++;
470         }
471         else {
472                 if(vl1->v1==v1 || vl1->v2==v1 || vl1->v3==v1 || vl1->v4==v1) equal++;
473                 if(vl1->v1==v2 || vl1->v2==v2 || vl1->v3==v2 || vl1->v4==v2) equal++;
474                 if(vl1->v1==v3 || vl1->v2==v3 || vl1->v3==v3 || vl1->v4==v3) equal++;
475                 if(vl1->v1==v4 || vl1->v2==v4 || vl1->v3==v4 || vl1->v4==v4) equal++;
476         }
477
478         if(v4 && vl1->v4) {
479                 if(equal==4) return 1;
480         }
481         else 
482                 if(equal>=3) return 1;
483         
484         return 0;
485 }
486
487 /* checks for existence, and for tria overlapping inside quad */
488 static EditFace *exist_face_overlaps(EditMesh *em, EditVert *v1, EditVert *v2, EditVert *v3, EditVert *v4)
489 {
490         EditFace *efa, efatest;
491         
492         efatest.v1= v1;
493         efatest.v2= v2;
494         efatest.v3= v3;
495         efatest.v4= v4;
496         
497         efa= em->faces.first;
498         while(efa) {
499                 if(compareface_overlaps(&efatest, efa)) return efa;
500                 efa= efa->next;
501         }
502         return NULL;
503 }
504
505 /* will be new face smooth or solid? depends on smoothness of face neighbours
506  * of new face, if function return 1, then new face will be smooth, when functio
507  * will return zero, then new face will be solid */
508 static void fix_new_face(EditMesh *em, EditFace *eface)
509 {
510         struct EditFace *efa;
511         struct EditEdge *eed=NULL;
512         struct EditVert *v1 = eface->v1, *v2 = eface->v2, *v3 = eface->v3, *v4 = eface->v4;
513         struct EditVert *ev1=NULL, *ev2=NULL;
514         short smooth=0; /* "total smoothnes" of faces in neighbourhood */
515         short coef;     /* "weight" of smoothness */
516         short count=0;  /* number of edges with same direction as eface */
517         short vi00=0, vi01=0, vi10=0, vi11=0; /* vertex indexes */
518
519         efa = em->faces.first;
520
521         while(efa) {
522
523                 if(efa==eface) {
524                         efa = efa->next;
525                         continue;
526                 }
527
528                 coef = 0;
529                 ev1 = ev2 = NULL;
530                 eed = NULL;
531
532                 if(efa->v1==v1 || efa->v2==v1 || efa->v3==v1 || efa->v4==v1) {
533                         ev1 = v1;
534                         coef++;
535                 }
536                 if(efa->v1==v2 || efa->v2==v2 || efa->v3==v2 || efa->v4==v2) {
537                         if(ev1) ev2 = v2;
538                         else ev1 = v2;
539                         coef++;
540                 }
541                 if(efa->v1==v3 || efa->v2==v3 || efa->v3==v3 || efa->v4==v3) {
542                         if(coef<2) {
543                                 if(ev1) ev2 = v3;
544                                 else ev1 = v3;
545                         }
546                         coef++;
547                 }
548                 if((v4) && (efa->v1==v4 || efa->v2==v4 || efa->v3==v4 || efa->v4==v4)) {
549                         if(ev1 && coef<2) ev2 = v4;
550                         coef++;
551                 }
552
553                 /* "democracy" of smoothness */
554                 if(efa->flag & ME_SMOOTH)
555                         smooth += coef;
556                 else
557                         smooth -= coef;
558
559                 /* try to find edge using vertexes ev1 and ev2 */
560                 if((ev1) && (ev2) && (ev1!=ev2)) eed = findedgelist(em, ev1, ev2);
561
562                 /* has bordering edge of efa same direction as edge of eface ? */
563                 if(eed) {
564                         if(eed->v1==v1) vi00 = 1;
565                         else if(eed->v1==v2) vi00 = 2;
566                         else if(eed->v1==v3) vi00 = 3;
567                         else if(v4 && eed->v1==v4) vi00 = 4;
568
569                         if(eed->v2==v1) vi01 = 1;
570                         else if(eed->v2==v2) vi01 = 2;
571                         else if(eed->v2==v3) vi01 = 3;
572                         else if(v4 && eed->v2==v4) vi01 = 4;
573
574                         if(v4) {
575                                 if(vi01==1 && vi00==4) vi00 = 0;
576                                 if(vi01==4 && vi00==1) vi01 = 0;
577                         }
578                         else {
579                                 if(vi01==1 && vi00==3) vi00 = 0;
580                                 if(vi01==3 && vi00==1) vi01 = 0;
581                         }
582
583                         if(eed->v1==efa->v1) vi10 = 1;
584                         else if(eed->v1==efa->v2) vi10 = 2;
585                         else if(eed->v1==efa->v3) vi10 = 3;
586                         else if(efa->v4 && eed->v1==efa->v4) vi10 = 4;
587
588                         if(eed->v2==efa->v1) vi11 = 1;
589                         else if(eed->v2==efa->v2) vi11 = 2;
590                         else if(eed->v2==efa->v3) vi11 = 3;
591                         else if(efa->v4 && eed->v2==efa->v4) vi11 = 4;
592
593                         if(efa->v4) {
594                                 if(vi11==1 && vi10==4) vi10 = 0;
595                                 if(vi11==4 && vi10==1) vi11 = 0;
596                         }
597                         else {
598                                 if(vi11==1 && vi10==3) vi10 = 0;
599                                 if(vi11==3 && vi10==1) vi11 = 0;
600                         }
601
602                         if(((vi00>vi01) && (vi10>vi11)) ||
603                                 ((vi00<vi01) && (vi10<vi11)))
604                                 count++;
605                         else
606                                 count--;
607                 }
608
609                 efa = efa->next;
610         }
611
612         /* set up smoothness according voting of face in neighbourhood */
613         if(smooth >= 0)
614                 eface->flag |= ME_SMOOTH;
615         else
616                 eface->flag &= ~ME_SMOOTH;
617
618         /* flip face, when too much "face normals" in neighbourhood is different */
619         if(count > 0) {
620                 flipface(em, eface);
621         }
622 }
623
624 /* only adds quads or trias when there's edges already */
625 void addfaces_from_edgenet(EditMesh *em)
626 {
627         EditVert *eve1, *eve2, *eve3, *eve4;
628         
629         for(eve1= em->verts.first; eve1; eve1= eve1->next) {
630                 for(eve2= em->verts.first; (eve1->f & 1) && eve2; eve2= eve2->next) {
631                         if(findedgelist(em, eve1,eve2)) {
632                                 for(eve3= em->verts.first; (eve2->f & 1) && eve3; eve3= eve3->next) {
633                                         if((eve2!=eve3 && (eve3->f & 1) && findedgelist(em, eve1,eve3))) {
634                                                 EditEdge *sh_edge= NULL;
635                                                 EditVert *sh_vert= NULL;
636                                                 
637                                                 sh_edge= findedgelist(em, eve2,eve3);
638                                                 
639                                                 if(sh_edge) { /* Add a triangle */
640                                                         if(!exist_face_overlaps(em, eve1,eve2,eve3,NULL))
641                                                                 fix_new_face(em, addfacelist(em, eve1,eve2,eve3,NULL,NULL,NULL));
642                                                 }
643                                                 else { /* Check for a shared vertex */
644                                                         for(eve4= em->verts.first; eve4; eve4= eve4->next) {
645                                                                 if(eve4!=eve1 && eve4!=eve2 && eve4!=eve3 && (eve4->f & 1) &&
646                                                                    !findedgelist(em, eve1,eve4) && findedgelist(em, eve2,eve4) &&
647                                                                    findedgelist(em, eve3,eve4)) {
648                                                                         sh_vert= eve4;
649                                                                         break;
650                                                                 }
651                                                         }
652                                                         
653                                                         if(sh_vert) {
654                                                                 if(sh_vert) {
655                                                                         if(!exist_face_overlaps(em, eve1,eve2,eve4,eve3))
656                                                                                 fix_new_face(em, addfacelist(em, eve1,eve2,eve4,eve3,NULL,NULL));
657                                                                 }
658                                                         }
659                                                 }
660                                         }
661                                 }
662                         }
663                 }
664         }
665
666         EM_select_flush(em);
667         
668 // XXX  DAG_id_flush_update(obedit->data, OB_RECALC_DATA);
669 }
670
671 static void addedgeface_mesh(EditMesh *em, wmOperator *op)
672 {
673         EditVert *eve, *neweve[4];
674         EditEdge *eed;
675         EditFace *efa;
676         short amount=0;
677
678         /* how many selected ? */
679         if(em->selectmode & SCE_SELECT_EDGE) {
680                 /* in edge mode finding selected vertices means flushing down edge codes... */
681                 /* can't make face with only edge selection info... */
682                 EM_selectmode_set(em);
683         }
684         
685         for(eve= em->verts.first; eve; eve= eve->next) {
686                 if(eve->f & SELECT) {
687                         amount++;
688                         if(amount>4) break;                     
689                         neweve[amount-1]= eve;
690                 }
691         }
692
693         if(amount==2) {
694                 eed= addedgelist(em, neweve[0], neweve[1], NULL);
695                 EM_select_edge(eed, 1);
696
697                 // XXX          DAG_id_flush_update(obedit->data, OB_RECALC_DATA);      
698                 return;
699         }
700         else if(amount > 4) {
701                 addfaces_from_edgenet(em);
702                 return;
703         }
704         else if(amount<2) {
705                 BKE_report(op->reports, RPT_ERROR, "More vertices are needed to make an edge/face");
706                 return;
707         }
708
709         efa= NULL; // check later
710
711         if(amount==3) {
712                 
713                 if(exist_face_overlaps(em, neweve[0], neweve[1], neweve[2], NULL)==0) {
714                         efa= addfacelist(em, neweve[0], neweve[1], neweve[2], 0, NULL, NULL);
715                         EM_select_face(efa, 1);
716                 }
717                 else BKE_report(op->reports, RPT_ERROR, "The selected vertices already form a face");
718         }
719         else if(amount==4) {
720                 /* this test survives when theres 2 triangles */
721                 if(exist_face(em, neweve[0], neweve[1], neweve[2], neweve[3])==0) {
722                         int tria= 0;
723                         
724                         /* remove trias if they exist, 4 cases.... */
725                         if(exist_face(em, neweve[0], neweve[1], neweve[2], NULL)) tria++;
726                         if(exist_face(em, neweve[0], neweve[1], neweve[3], NULL)) tria++;
727                         if(exist_face(em, neweve[0], neweve[2], neweve[3], NULL)) tria++;
728                         if(exist_face(em, neweve[1], neweve[2], neweve[3], NULL)) tria++;
729                 
730                         if(tria==2) join_triangles(em);
731                         else if(exist_face_overlaps(em, neweve[0], neweve[1], neweve[2], neweve[3])==0) {
732                                  /* If there are 4 Verts, But more selected edges, we need to call addfaces_from_edgenet */
733                                         EditEdge *eedcheck;
734                                         int count;
735                                         count = 0;
736                                         for(eedcheck= em->edges.first; eedcheck; eedcheck= eedcheck->next) {
737                                                 if(eedcheck->f & SELECT) {
738                                                         count++;
739                                                 }
740                                         }       
741                                 
742                                 if(count++ > 4){
743                                         addfaces_from_edgenet(em);
744                                         return;
745                                 } else {
746                                 /* if 4 edges exist, we just create the face, convex or not */
747                                         efa= addface_from_edges(em);
748                                         if(efa==NULL) {
749                                                 
750                                                 /* the order of vertices can be anything, 6 cases to check */
751                                                 if( convex(neweve[0]->co, neweve[1]->co, neweve[2]->co, neweve[3]->co) ) {
752                                                         efa= addfacelist(em, neweve[0], neweve[1], neweve[2], neweve[3], NULL, NULL);
753                                                 }
754                                                 else if( convex(neweve[0]->co, neweve[2]->co, neweve[3]->co, neweve[1]->co) ) {
755                                                         efa= addfacelist(em, neweve[0], neweve[2], neweve[3], neweve[1], NULL, NULL);
756                                                 }
757                                                 else if( convex(neweve[0]->co, neweve[2]->co, neweve[1]->co, neweve[3]->co) ) {
758                                                         efa= addfacelist(em, neweve[0], neweve[2], neweve[1], neweve[3], NULL, NULL);
759                                                 }
760                                                 else if( convex(neweve[0]->co, neweve[1]->co, neweve[3]->co, neweve[2]->co) ) {
761                                                         efa= addfacelist(em, neweve[0], neweve[1], neweve[3], neweve[2], NULL, NULL);
762                                                 }
763                                                 else if( convex(neweve[0]->co, neweve[3]->co, neweve[2]->co, neweve[1]->co) ) {
764                                                         efa= addfacelist(em, neweve[0], neweve[3], neweve[2], neweve[1], NULL, NULL);
765                                                 }
766                                                 else if( convex(neweve[0]->co, neweve[3]->co, neweve[1]->co, neweve[2]->co) ) {
767                                                         efa= addfacelist(em, neweve[0], neweve[3], neweve[1], neweve[2], NULL, NULL);
768                                                 }
769                                                 else BKE_report(op->reports, RPT_ERROR, "cannot find nice quad from concave set of vertices");
770
771                                         }
772                                 }
773                         }
774                         else BKE_report(op->reports, RPT_ERROR, "The selected vertices already form a face");
775                 }
776                 else BKE_report(op->reports, RPT_ERROR, "The selected vertices already form a face");
777         }
778         
779         if(efa) {
780                 EM_select_face(efa, 1);
781
782                 fix_new_face(em, efa);
783                 
784                 recalc_editnormals(em);
785         }
786         }
787
788 static int addedgeface_mesh_exec(bContext *C, wmOperator *op)
789 {
790         Object *obedit= CTX_data_edit_object(C);
791         EditMesh *em= BKE_mesh_get_editmesh(((Mesh *)obedit->data));
792         
793         addedgeface_mesh(em, op);
794         
795         DAG_id_flush_update(obedit->data, OB_RECALC_DATA);      
796         WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
797         
798         BKE_mesh_end_editmesh(obedit->data, em);
799         return OPERATOR_FINISHED;
800 }
801
802 void MESH_OT_edge_face_add(wmOperatorType *ot)
803 {
804         /* identifiers */
805         ot->name= "Make Edge/Face";
806         ot->description= "Add an edge or face to selected";
807         ot->idname= "MESH_OT_edge_face_add";
808         
809         /* api callbacks */
810         ot->exec= addedgeface_mesh_exec;
811         ot->poll= ED_operator_editmesh;
812         
813         /* flags */
814         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
815         
816 }
817
818
819
820 /* ************************ primitives ******************* */
821
822 // HACK: these can also be found in cmoview.tga.c, but are here so that they can be found by linker
823 // this hack is only used so that scons+mingw + split-sources hack works
824         // ------------------------------- start copied code
825 /* these are not the monkeys you are looking for */
826 int monkeyo= 4;
827 int monkeynv= 271;
828 int monkeynf= 250;
829 signed char monkeyv[271][3]= {
830 {-71,21,98},{-63,12,88},{-57,7,74},{-82,-3,79},{-82,4,92},
831 {-82,17,100},{-92,21,102},{-101,12,95},{-107,7,83},
832 {-117,31,84},{-109,31,95},{-96,31,102},{-92,42,102},
833 {-101,50,95},{-107,56,83},{-82,66,79},{-82,58,92},
834 {-82,46,100},{-71,42,98},{-63,50,88},{-57,56,74},
835 {-47,31,72},{-55,31,86},{-67,31,97},{-66,31,99},
836 {-70,43,100},{-82,48,103},{-93,43,105},{-98,31,105},
837 {-93,20,105},{-82,31,106},{-82,15,103},{-70,20,100},
838 {-127,55,95},{-127,45,105},{-127,-87,94},{-127,-41,100},
839 {-127,-24,102},{-127,-99,92},{-127,52,77},{-127,73,73},
840 {-127,115,-70},{-127,72,-109},{-127,9,-106},{-127,-49,-45},
841 {-101,-24,72},{-87,-56,73},{-82,-89,73},{-80,-114,68},
842 {-85,-121,67},{-104,-124,71},{-127,-126,74},{-71,-18,68},
843 {-46,-5,69},{-21,19,57},{-17,55,76},{-36,62,80},
844 {-64,77,88},{-86,97,94},{-107,92,97},{-119,63,96},
845 {-106,53,99},{-111,39,98},{-101,12,95},{-79,2,90},
846 {-64,8,86},{-47,24,83},{-45,38,83},{-50,48,85},
847 {-72,56,92},{-95,60,97},{-127,-98,94},{-113,-92,94},
848 {-112,-107,91},{-119,-113,89},{-127,-114,88},{-127,-25,96},
849 {-127,-18,95},{-114,-19,95},{-111,-29,96},{-116,-37,95},
850 {-76,-6,86},{-48,7,80},{-34,26,77},{-32,48,84},
851 {-39,53,93},{-71,70,102},{-87,82,107},{-101,79,109},
852 {-114,55,108},{-111,-13,104},{-100,-57,91},{-95,-90,88},
853 {-93,-105,85},{-97,-117,81},{-106,-119,81},{-127,-121,82},
854 {-127,6,93},{-127,27,98},{-85,61,95},{-106,18,96},
855 {-110,27,97},{-112,-88,94},{-117,-57,96},{-127,-57,96},
856 {-127,-42,95},{-115,-35,100},{-110,-29,102},{-113,-17,100},
857 {-122,-16,100},{-127,-26,106},{-121,-19,104},{-115,-20,104},
858 {-113,-29,106},{-117,-32,103},{-127,-37,103},{-94,-40,71},
859 {-106,-31,91},{-104,-40,91},{-97,-32,71},{-127,-112,88},
860 {-121,-111,88},{-115,-105,91},{-115,-95,93},{-127,-100,84},
861 {-115,-96,85},{-115,-104,82},{-121,-109,81},{-127,-110,81},
862 {-105,28,100},{-103,20,99},{-84,55,97},{-92,54,99},
863 {-73,51,99},{-55,45,89},{-52,37,88},{-53,25,87},
864 {-66,13,92},{-79,8,95},{-98,14,100},{-104,38,100},
865 {-100,48,100},{-97,46,97},{-102,38,97},{-96,16,97},
866 {-79,11,93},{-68,15,90},{-57,27,86},{-56,36,86},
867 {-59,43,87},{-74,50,96},{-91,51,98},{-84,52,96},
868 {-101,22,96},{-102,29,96},{-113,59,78},{-102,85,79},
869 {-84,88,76},{-65,71,71},{-40,58,63},{-25,52,59},
870 {-28,21,48},{-50,0,53},{-71,-12,60},{-127,115,37},
871 {-127,126,-10},{-127,-25,-86},{-127,-59,24},{-127,-125,59},
872 {-127,-103,44},{-127,-73,41},{-127,-62,36},{-18,30,7},
873 {-17,41,-6},{-28,34,-56},{-68,56,-90},{-33,-6,9},
874 {-51,-16,-21},{-45,-1,-55},{-84,7,-85},{-97,-45,52},
875 {-104,-53,33},{-90,-91,49},{-95,-64,50},{-85,-117,51},
876 {-109,-97,47},{-111,-69,46},{-106,-121,56},{-99,-36,55},
877 {-100,-29,60},{-101,-22,64},{-100,-50,21},{-89,-40,-34},
878 {-83,-19,-69},{-69,111,-49},{-69,119,-9},{-69,109,30},
879 {-68,67,55},{-34,52,43},{-46,58,36},{-45,90,7},
880 {-25,72,16},{-25,79,-15},{-45,96,-25},{-45,87,-57},
881 {-25,69,-46},{-48,42,-75},{-65,3,-70},{-22,42,-26},
882 {-75,-22,19},{-72,-25,-27},{-13,52,-30},{-28,-18,-16},
883 {6,-13,-42},{37,7,-55},{46,41,-54},{31,65,-54},
884 {4,61,-40},{3,53,-37},{25,56,-50},{35,37,-52},
885 {28,10,-52},{5,-5,-39},{-21,-9,-17},{-9,46,-28},
886 {-6,39,-37},{-14,-3,-27},{6,0,-47},{25,12,-57},
887 {31,32,-57},{23,46,-56},{4,44,-46},{-19,37,-27},
888 {-20,22,-35},{-30,12,-35},{-22,11,-35},{-19,2,-35},
889 {-23,-2,-35},{-34,0,-9},{-35,-3,-22},{-35,5,-24},
890 {-25,26,-27},{-13,31,-34},{-13,30,-41},{-23,-2,-41},
891 {-18,2,-41},{-21,10,-41},{-29,12,-41},{-19,22,-41},
892 {6,42,-53},{25,44,-62},{34,31,-63},{28,11,-62},
893 {7,0,-54},{-14,-2,-34},{-5,37,-44},{-13,14,-42},
894 {-7,8,-43},{1,16,-47},{-4,22,-45},{3,30,-48},
895 {8,24,-49},{15,27,-50},{12,35,-50},{4,56,-62},
896 {33,60,-70},{48,38,-64},{41,7,-68},{6,-11,-63},
897 {-26,-16,-42},{-17,49,-49},
898 };
899
900 signed char monkeyf[250][4]= {
901 {27,4,5,26}, {25,4,5,24}, {3,6,5,4}, {1,6,5,2}, {5,6,7,4}, 
902 {3,6,7,2}, {5,8,7,6}, {3,8,7,4}, {7,8,9,6}, 
903 {5,8,9,4}, {7,10,9,8}, {5,10,9,6}, {9,10,11,8}, 
904 {7,10,11,6}, {9,12,11,10}, {7,12,11,8}, {11,6,13,12}, 
905 {5,4,13,12}, {3,-2,13,12}, {-3,-4,13,12}, {-5,-10,13,12}, 
906 {-11,-12,14,12}, {-13,-18,14,13}, {-19,4,5,13}, {10,12,4,4}, 
907 {10,11,9,9}, {8,7,9,9}, {7,5,6,6}, {6,3,4,4}, 
908 {5,1,2,2}, {4,-1,0,0}, {3,-3,-2,-2}, {22,67,68,23}, 
909 {20,65,66,21}, {18,63,64,19}, {16,61,62,17}, {14,59,60,15}, 
910 {12,19,48,57}, {18,19,48,47}, {18,19,48,47}, {18,19,48,47}, 
911 {18,19,48,47}, {18,19,48,47}, {18,19,48,47}, {18,19,48,47}, 
912 {18,19,48,47}, {18,-9,-8,47}, {18,27,45,46}, {26,55,43,44}, 
913 {24,41,42,54}, {22,39,40,23}, {20,37,38,21}, {18,35,36,19}, 
914 {16,33,34,17}, {14,31,32,15}, {12,39,30,13}, {11,48,45,38}, 
915 {8,36,-19,9}, {8,-20,44,47}, {42,45,46,43}, {18,19,40,39}, 
916 {16,17,38,37}, {14,15,36,35}, {32,44,43,33}, {12,33,32,42}, 
917 {19,44,43,42}, {40,41,42,-27}, {8,9,39,-28}, {15,43,42,16}, 
918 {13,43,42,14}, {11,43,42,12}, {9,-30,42,10}, {37,12,38,-32}, 
919 {-33,37,45,46}, {-33,40,41,39}, {38,40,41,37}, {36,40,41,35}, 
920 {34,40,41,33}, {36,39,38,37}, {35,40,39,38}, {1,2,14,21}, 
921 {1,2,40,13}, {1,2,40,39}, {1,24,12,39}, {-34,36,38,11}, 
922 {35,38,36,37}, {-37,8,35,37}, {-11,-12,-45,40}, {-11,-12,39,38}, 
923 {-11,-12,37,36}, {-11,-12,35,34}, {33,34,40,41}, {33,34,38,39}, 
924 {33,34,36,37}, {33,-52,34,35}, {33,37,36,34}, {33,35,34,34}, 
925 {8,7,37,36}, {-32,7,35,46}, {-34,-33,45,46}, {4,-33,43,34}, 
926 {-34,-33,41,42}, {-34,-33,39,40}, {-34,-33,37,38}, {-34,-33,35,36}, 
927 {-34,-33,33,34}, {-34,-33,31,32}, {-34,-4,28,30}, {-5,-34,28,27}, 
928 {-35,-44,36,27}, {26,35,36,45}, {24,25,44,45}, {25,23,44,42}, 
929 {25,24,41,40}, {25,24,39,38}, {25,24,37,36}, {25,24,35,34}, 
930 {25,24,33,32}, {25,24,31,30}, {15,24,29,38}, {25,24,27,26}, 
931 {23,12,37,26}, {11,12,35,36}, {-86,-59,36,-80}, {-60,-61,36,35}, 
932 {-62,-63,36,35}, {-64,-65,36,35}, {-66,-67,36,35}, {-68,-69,36,35}, 
933 {-70,-71,36,35}, {-72,-73,36,35}, {-74,-75,36,35}, {42,43,53,58}, 
934 {40,41,57,56}, {38,39,55,57}, {-81,-80,37,56}, {-83,-82,55,52}, 
935 {-85,-84,51,49}, {-87,-86,48,49}, {47,50,51,48}, {46,48,51,49}, 
936 {43,46,49,44}, {-92,-91,45,42}, {-23,49,50,-20}, {-94,40,48,-24}, 
937 {-96,-22,48,49}, {-97,48,21,-90}, {-100,36,50,23}, {22,49,48,-100}, 
938 {-101,47,46,22}, {21,45,35,25}, {33,34,44,41}, {13,14,28,24}, 
939 {-107,26,30,-106}, {14,46,45,15}, {14,44,43,-110}, {-111,42,23,-110}, 
940 {6,7,45,46}, {45,44,47,46}, {45,46,47,48}, {47,46,49,48}, 
941 {17,49,47,48}, {17,36,46,48}, {35,36,44,45}, {35,36,40,43}, 
942 {35,36,38,39}, {-4,-3,37,35}, {-123,34,33,1}, {-9,-8,-7,-6}, 
943 {-10,-7,32,-125}, {-127,-11,-126,-126}, {-7,-6,5,31}, {4,5,33,30}, 
944 {4,39,33,32}, {4,35,32,38}, {20,21,39,38}, {4,37,38,5}, 
945 {-11,-10,36,3}, {-11,15,14,35}, {13,16,34,34}, {-13,14,13,13}, 
946 {-3,1,30,29}, {-3,28,29,1}, {-2,31,28,-1}, {12,13,27,30}, 
947 {-2,26,12,12}, {35,29,42,36}, {34,35,36,33}, {32,35,36,31}, 
948 {30,35,36,29}, {28,35,36,27}, {26,35,36,25}, {34,39,38,35}, 
949 {32,39,38,33}, {30,39,38,31}, {28,39,38,29}, {26,39,38,27}, 
950 {25,31,32,38}, {-18,-17,45,44}, {-18,17,28,44}, {-24,-20,42,-23}, 
951 {11,35,27,14}, {25,28,39,41}, {37,41,40,38}, {34,40,36,35}, 
952 {32,40,39,33}, {30,39,31,40}, {21,29,39,22}, {-31,37,28,4}, 
953 {-32,33,35,36}, {32,33,34,34}, {18,35,36,48}, {34,25,40,35}, 
954 {24,25,38,39}, {24,25,36,37}, {24,25,34,35}, {24,25,32,33}, 
955 {24,13,41,31}, {17,11,41,35}, {15,16,34,35}, {13,14,34,35}, 
956 {11,12,34,35}, {9,10,34,35}, {7,8,34,35}, {26,25,37,36}, 
957 {35,36,37,38}, {37,36,39,38}, {37,38,39,40}, {25,31,36,39}, 
958 {18,34,35,30}, {17,22,30,33}, {19,29,21,20}, {16,26,29,17}, 
959 {24,29,28,25}, {22,31,28,23}, {20,31,30,21}, {18,31,30,19}, 
960 {16,30,17,17}, {-21,-22,35,34}, {-21,-22,33,32}, {-21,-22,31,30}, 
961 {-21,-22,29,28}, {-21,-22,27,26}, {-28,-22,25,31}, {24,28,29,30}, 
962 {23,24,26,27}, {23,24,25,25}, {-69,-35,-32,27}, {-70,26,25,-66}, 
963 {-68,-67,24,-33}, 
964 };
965         // ------------------------------- end copied code
966
967
968 #define PRIM_PLANE              0
969 #define PRIM_CUBE               1
970 #define PRIM_CIRCLE             4
971 #define PRIM_CYLINDER   5
972 #define PRIM_CONE               7
973 #define PRIM_GRID               10
974 #define PRIM_UVSPHERE   11
975 #define PRIM_ICOSPHERE  12
976 #define PRIM_MONKEY             13
977
978 static void make_prim(Object *obedit, int type, float mat[4][4], int tot, int seg,
979                 int subdiv, float dia, float depth, int ext, int fill)
980 {
981         /*
982          * type - for the type of shape
983          * dia - the radius for cone,sphere cylinder etc.
984          * depth - 
985          * ext - extrude
986          * fill - end capping, and option to fill in circle
987          * cent[3] - center of the data. 
988          * */
989         EditMesh *em= BKE_mesh_get_editmesh(((Mesh *)obedit->data));
990         EditVert *eve, *v1=NULL, *v2, *v3, *v4=NULL, *vtop, *vdown;
991         float phi, phid, vec[3];
992         float q[4], cmat[3][3], nor[3]= {0.0, 0.0, 0.0};
993         short a, b;
994         
995         EM_clear_flag_all(em, SELECT);
996
997         phid= 2.0f*(float)M_PI/tot;
998         phi= .25f*(float)M_PI;
999
1000         switch(type) {
1001         case PRIM_GRID: /*  grid */
1002                 /* clear flags */
1003                 eve= em->verts.first;
1004                 while(eve) {
1005                         eve->f= 0;
1006                         eve= eve->next;
1007                 }
1008                 /* one segment first: the X axis */
1009                 phi= 1.0; 
1010                 phid= 2.0/((float)tot-1);
1011                 for(a=0;a<tot;a++) {
1012                         vec[0]= dia*phi;
1013                         vec[1]= - dia;
1014                         vec[2]= 0.0f;
1015                         mul_m4_v3(mat,vec);
1016                         eve= addvertlist(em, vec, NULL);
1017                         eve->f= 1+2+4;
1018                         if (a) {
1019                                 addedgelist(em, eve->prev, eve, NULL);
1020                         }
1021                         phi-=phid;
1022                 }
1023                 /* extrude and translate */
1024                 vec[0]= vec[2]= 0.0;
1025                 vec[1]= dia*phid;
1026                 mul_mat3_m4_v3(mat, vec);
1027                 
1028                 for(a=0;a<seg-1;a++) {
1029                         extrudeflag_vert(obedit, em, 2, nor, 0);        // nor unused
1030                         translateflag(em, 2, vec);
1031                 }
1032                 break;
1033         case PRIM_UVSPHERE: /*  UVsphere */
1034                 
1035                 /* clear all flags */
1036                 eve= em->verts.first;
1037                 while(eve) {
1038                         eve->f= 0;
1039                         eve= eve->next;
1040                 }
1041                 
1042                 /* one segment first */
1043                 phi= 0; 
1044                 phid/=2;
1045                 for(a=0; a<=tot; a++) {
1046                         vec[0]= dia*sin(phi);
1047                         vec[1]= 0.0;
1048                         vec[2]= dia*cos(phi);
1049                         eve= addvertlist(em, vec, NULL);
1050                         eve->f= 1+2+4;
1051                         if(a==0) v1= eve;
1052                         else addedgelist(em, eve->prev, eve, NULL);
1053                         phi+= phid;
1054                 }
1055                 
1056                 /* extrude and rotate */
1057                 phi= M_PI/seg;
1058                 q[0]= cos(phi);
1059                 q[3]= sin(phi);
1060                 q[1]=q[2]= 0;
1061                 quat_to_mat3( cmat,q);
1062                 
1063                 for(a=0; a<seg; a++) {
1064                         extrudeflag_vert(obedit, em, 2, nor, 0); // nor unused
1065                         rotateflag(em, 2, v1->co, cmat);
1066                 }
1067
1068                 removedoublesflag(em, 4, 0, 0.0001);
1069
1070                 /* and now do imat */
1071                 eve= em->verts.first;
1072                 while(eve) {
1073                         if(eve->f & SELECT) {
1074                                 mul_m4_v3(mat,eve->co);
1075                         }
1076                         eve= eve->next;
1077                 }
1078                 break;
1079         case PRIM_ICOSPHERE: /* Icosphere */
1080                 {
1081                         EditVert *eva[12];
1082                         EditEdge *eed;
1083                         
1084                         /* clear all flags */
1085                         eve= em->verts.first;
1086                         while(eve) {
1087                                 eve->f= 0;
1088                                 eve= eve->next;
1089                         }
1090                         dia/=200;
1091                         for(a=0;a<12;a++) {
1092                                 vec[0]= dia*icovert[a][0];
1093                                 vec[1]= dia*icovert[a][1];
1094                                 vec[2]= dia*icovert[a][2];
1095                                 eva[a]= addvertlist(em, vec, NULL);
1096                                 eva[a]->f= 1+2;
1097                         }
1098                         for(a=0;a<20;a++) {
1099                                 EditFace *evtemp;
1100                                 v1= eva[ icoface[a][0] ];
1101                                 v2= eva[ icoface[a][1] ];
1102                                 v3= eva[ icoface[a][2] ];
1103                                 evtemp = addfacelist(em, v1, v2, v3, 0, NULL, NULL);
1104                                 evtemp->e1->f = 1+2;
1105                                 evtemp->e2->f = 1+2;
1106                                 evtemp->e3->f = 1+2;
1107                         }
1108
1109                         dia*=200;
1110                         for(a=1; a<subdiv; a++) esubdivideflag(obedit, em, 2, dia, 0, B_SPHERE,1,0);
1111                         /* and now do imat */
1112                         eve= em->verts.first;
1113                         while(eve) {
1114                                 if(eve->f & 2) {
1115                                         mul_m4_v3(mat,eve->co);
1116                                 }
1117                                 eve= eve->next;
1118                         }
1119                         
1120                         // Clear the flag 2 from the edges
1121                         for(eed=em->edges.first;eed;eed=eed->next){
1122                                 if(eed->f & 2){
1123                                            eed->f &= !2;
1124                                 }   
1125                         }
1126                 }
1127                 break;
1128         case PRIM_MONKEY: /* Monkey */
1129                 {
1130                         //extern int monkeyo, monkeynv, monkeynf;
1131                         //extern signed char monkeyf[][4];
1132                         //extern signed char monkeyv[][3];
1133                         EditVert **tv= MEM_mallocN(sizeof(*tv)*monkeynv*2, "tv");
1134                         int i;
1135
1136                         for (i=0; i<monkeynv; i++) {
1137                                 float v[3];
1138                                 v[0]= (monkeyv[i][0]+127)/128.0, v[1]= monkeyv[i][1]/128.0, v[2]= monkeyv[i][2]/128.0;
1139                                 tv[i]= addvertlist(em, v, NULL);
1140                                 tv[i]->f |= SELECT;
1141                                 tv[monkeynv+i]= (fabs(v[0]= -v[0])<0.001)?tv[i]:addvertlist(em, v, NULL);
1142                                 tv[monkeynv+i]->f |= SELECT;
1143                         }
1144                         for (i=0; i<monkeynf; i++) {
1145                                 addfacelist(em, 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);
1146                                 addfacelist(em, 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);
1147                         }
1148
1149                         MEM_freeN(tv);
1150
1151                         /* and now do imat */
1152                         for(eve= em->verts.first; eve; eve= eve->next) {
1153                                 if(eve->f & SELECT) {
1154                                         mul_m4_v3(mat,eve->co);
1155                                 }
1156                         }
1157                         recalc_editnormals(em);
1158                 }
1159                 break;
1160         default: /* all types except grid, sphere... */
1161                 if(type==PRIM_CONE);
1162                 else if(ext==0) 
1163                         depth= 0.0f;
1164         
1165                 /* vertices */
1166                 vtop= vdown= v1= v2= 0;
1167                 for(b=0; b<=ext; b++) {
1168                         for(a=0; a<tot; a++) {
1169                                 
1170                                 vec[0]= dia*sin(phi);
1171                                 vec[1]= dia*cos(phi);
1172                                 vec[2]= b?depth:-depth;
1173                                 
1174                                 mul_m4_v3(mat, vec);
1175                                 eve= addvertlist(em, vec, NULL);
1176                                 eve->f= SELECT;
1177                                 if(a==0) {
1178                                         if(b==0) v1= eve;
1179                                         else v2= eve;
1180                                 }
1181                                 phi+=phid;
1182                         }
1183                 }
1184                         
1185                 /* center vertices */
1186                 /* type PRIM_CONE can only have 1 one side filled
1187                  * if the cone has no capping, dont add vtop */
1188                 if(type == PRIM_CONE || (fill && !ELEM(type, PRIM_PLANE, PRIM_CUBE))) {
1189                         vec[0]= vec[1]= 0.0f;
1190                         vec[2]= type==PRIM_CONE ? depth : -depth;
1191                         mul_m4_v3(mat, vec);
1192                         vdown= addvertlist(em, vec, NULL);
1193                         if((ext || type==PRIM_CONE) && fill) {
1194                                 vec[0]= vec[1]= 0.0f;
1195                                 vec[2]= type==PRIM_CONE ? -depth : depth;
1196                                 mul_m4_v3(mat,vec);
1197                                 vtop= addvertlist(em, vec, NULL);
1198                         }
1199                 } else {
1200                         vdown= v1;
1201                         vtop= v2;
1202                 }
1203                 if(vtop) vtop->f= SELECT;
1204                 if(vdown) vdown->f= SELECT;
1205         
1206                 /* top and bottom face */
1207                 if(fill || type==PRIM_CONE) {
1208                         if(tot==4 && ELEM(type, PRIM_PLANE, PRIM_CUBE)) {
1209                                 v3= v1->next->next;
1210                                 if(ext) v4= v2->next->next;
1211                                 
1212                                 addfacelist(em, v3, v1->next, v1, v3->next, NULL, NULL);
1213                                 if(ext) addfacelist(em, v2, v2->next, v4, v4->next, NULL, NULL);
1214                                 
1215                         }
1216                         else {
1217                                 v3= v1;
1218                                 v4= v2;
1219                                 for(a=1; a<tot; a++) {
1220                                         addfacelist(em, vdown, v3, v3->next, 0, NULL, NULL);
1221                                         v3= v3->next;
1222                                         if(ext && fill) {
1223                                                 addfacelist(em, vtop, v4, v4->next, 0, NULL, NULL);
1224                                                 v4= v4->next;
1225                                         }
1226                                 }
1227                                 if(!ELEM(type, PRIM_PLANE, PRIM_CUBE)) {
1228                                         addfacelist(em, vdown, v3, v1, 0, NULL, NULL);
1229                                         if(ext) addfacelist(em, vtop, v4, v2, 0, NULL, NULL);
1230                                 }
1231                         }
1232                 }
1233                 else if(type==PRIM_CIRCLE) {  /* we need edges for a circle */
1234                         v3= v1;
1235                         for(a=1;a<tot;a++) {
1236                                 addedgelist(em, v3, v3->next, NULL);
1237                                 v3= v3->next;
1238                         }
1239                         addedgelist(em, v3, v1, NULL);
1240                 }
1241                 /* side faces */
1242                 if(ext) {
1243                         v3= v1;
1244                         v4= v2;
1245                         for(a=1; a<tot; a++) {
1246                                 addfacelist(em, v3, v3->next, v4->next, v4, NULL, NULL);
1247                                 v3= v3->next;
1248                                 v4= v4->next;
1249                         }
1250                         addfacelist(em, v3, v1, v2, v4, NULL, NULL);
1251                 }
1252                 else if(fill && type==PRIM_CONE) {
1253                         /* add the bottom flat area of the cone
1254                          * if capping is disabled dont bother */
1255                         v3= v1;
1256                         for(a=1; a<tot; a++) {
1257                                 addfacelist(em, vtop, v3->next, v3, 0, NULL, NULL);
1258                                 v3= v3->next;
1259                         }
1260                         addfacelist(em, vtop, v1, v3, 0, NULL, NULL);
1261                 }
1262         }
1263         
1264         EM_stats_update(em);
1265         /* simple selection flush OK, based on fact it's a single model */
1266         EM_select_flush(em); /* flushes vertex -> edge -> face selection */
1267         
1268         if(type!=PRIM_PLANE && type!=PRIM_MONKEY)
1269                 EM_recalc_normal_direction(em, 0, 0);   /* otherwise monkey has eyes in wrong direction */
1270
1271         BKE_mesh_end_editmesh(obedit->data, em);
1272 }
1273
1274 /* ********* add primitive operators ************* */
1275
1276 static void make_prim_ext(bContext *C, float *loc, float *rot, int enter_editmode, unsigned int layer, 
1277                 int type, int tot, int seg,
1278                 int subdiv, float dia, float depth, int ext, int fill)
1279 {
1280         Object *obedit= CTX_data_edit_object(C);
1281         int newob = 0;
1282         float mat[4][4];
1283
1284         if(obedit==NULL || obedit->type!=OB_MESH) {
1285                 obedit= ED_object_add_type(C, OB_MESH, loc, rot, FALSE, layer);
1286                 
1287                 /* create editmode */
1288                 ED_object_enter_editmode(C, EM_DO_UNDO|EM_IGNORE_LAYER); /* rare cases the active layer is messed up */
1289                 newob = 1;
1290         }
1291         else DAG_id_flush_update(&obedit->id, OB_RECALC_DATA);
1292
1293         dia *= ED_object_new_primitive_matrix(C, loc, rot, mat);
1294         depth *= ED_object_new_primitive_matrix(C, loc, rot, mat);
1295
1296         make_prim(obedit, type, mat, tot, seg, subdiv, dia, depth, ext, fill);
1297
1298         DAG_id_flush_update(obedit->data, OB_RECALC_DATA);
1299         WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
1300
1301
1302         /* userdef */
1303         if (newob && !enter_editmode) {
1304                 ED_object_exit_editmode(C, EM_FREEDATA); /* adding EM_DO_UNDO messes up operator redo */
1305         }
1306         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, obedit);
1307 }
1308
1309 static int add_primitive_plane_exec(bContext *C, wmOperator *op)
1310 {
1311         int enter_editmode;
1312         unsigned int layer;
1313         float loc[3], rot[3];
1314         
1315         ED_object_add_generic_get_opts(op, loc, rot, &enter_editmode, &layer);
1316
1317         /* sqrt(2.0f) - plane (diameter of 1.41 makes it unit size) */
1318         make_prim_ext(C, loc, rot, enter_editmode, layer,
1319                         PRIM_PLANE, 4, 0, 0, sqrt(2.0f), 0.0f, 0, 1);
1320         return OPERATOR_FINISHED;       
1321 }
1322
1323 void MESH_OT_primitive_plane_add(wmOperatorType *ot)
1324 {
1325         /* identifiers */
1326         ot->name= "Add Plane";
1327         ot->description= "Construct a filled planar mesh with 4 vertices";
1328         ot->idname= "MESH_OT_primitive_plane_add";
1329         
1330         /* api callbacks */
1331         ot->invoke= ED_object_add_generic_invoke;
1332         ot->exec= add_primitive_plane_exec;
1333         ot->poll= ED_operator_scene_editable;
1334         
1335         /* flags */
1336         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1337
1338         ED_object_add_generic_props(ot, TRUE);
1339 }
1340
1341 static int add_primitive_cube_exec(bContext *C, wmOperator *op)
1342 {
1343         int enter_editmode;
1344         unsigned int layer;
1345         float loc[3], rot[3];
1346         
1347         ED_object_add_generic_get_opts(op, loc, rot, &enter_editmode, &layer);
1348
1349         /* sqrt(2.0f) - plane (diameter of 1.41 makes it unit size) */
1350         make_prim_ext(C, loc, rot, enter_editmode, layer,
1351                         PRIM_CUBE, 4, 0, 0, sqrt(2.0f), 1.0f, 1, 1);
1352         return OPERATOR_FINISHED;
1353 }
1354
1355 void MESH_OT_primitive_cube_add(wmOperatorType *ot)
1356 {
1357         /* identifiers */
1358         ot->name= "Add Cube";
1359         ot->description= "Construct a cube mesh";
1360         ot->idname= "MESH_OT_primitive_cube_add";
1361         
1362         /* api callbacks */
1363         ot->invoke= ED_object_add_generic_invoke;
1364         ot->exec= add_primitive_cube_exec;
1365         ot->poll= ED_operator_scene_editable;
1366         
1367         /* flags */
1368         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1369
1370         ED_object_add_generic_props(ot, TRUE);
1371 }
1372
1373 static int add_primitive_circle_exec(bContext *C, wmOperator *op)
1374 {
1375         int enter_editmode;
1376         unsigned int layer;
1377         float loc[3], rot[3];
1378         
1379         ED_object_add_generic_get_opts(op, loc, rot, &enter_editmode, &layer);
1380
1381         make_prim_ext(C, loc, rot, enter_editmode, layer,
1382                         PRIM_CIRCLE, RNA_int_get(op->ptr, "vertices"), 0, 0,
1383                         RNA_float_get(op->ptr,"radius"), 0.0f, 0,
1384                         RNA_boolean_get(op->ptr, "fill"));
1385
1386         return OPERATOR_FINISHED;       
1387 }
1388
1389 void MESH_OT_primitive_circle_add(wmOperatorType *ot)
1390 {
1391         /* identifiers */
1392         ot->name= "Add Circle";
1393         ot->description= "Construct a circle mesh";
1394         ot->idname= "MESH_OT_primitive_circle_add";
1395         
1396         /* api callbacks */
1397         ot->invoke= ED_object_add_generic_invoke;
1398         ot->exec= add_primitive_circle_exec;
1399         ot->poll= ED_operator_scene_editable;
1400         
1401         /* flags */
1402         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1403         
1404         /* props */
1405         RNA_def_int(ot->srna, "vertices", 32, INT_MIN, INT_MAX, "Vertices", "", 3, 500);
1406         RNA_def_float(ot->srna, "radius", 1.0f, 0.0, FLT_MAX, "Radius", "", 0.001, 100.00);
1407         RNA_def_boolean(ot->srna, "fill", 0, "Fill", "");
1408
1409         ED_object_add_generic_props(ot, TRUE);
1410 }
1411
1412 static int add_primitive_tube_exec(bContext *C, wmOperator *op)
1413 {
1414         int enter_editmode;
1415         unsigned int layer;
1416         float loc[3], rot[3];
1417         
1418         ED_object_add_generic_get_opts(op, loc, rot, &enter_editmode, &layer);
1419
1420         make_prim_ext(C, loc, rot, enter_editmode, layer,
1421                         PRIM_CYLINDER, RNA_int_get(op->ptr, "vertices"), 0, 0,
1422                         RNA_float_get(op->ptr,"radius"),
1423                         RNA_float_get(op->ptr, "depth"), 1, 
1424                         RNA_boolean_get(op->ptr, "cap_ends"));
1425
1426         return OPERATOR_FINISHED;
1427 }
1428
1429 void MESH_OT_primitive_tube_add(wmOperatorType *ot)
1430 {
1431         /* identifiers */
1432         ot->name= "Add Tube";
1433         ot->description= "Construct a tube mesh";
1434         ot->idname= "MESH_OT_primitive_tube_add";
1435         
1436         /* api callbacks */
1437         ot->invoke= ED_object_add_generic_invoke;
1438         ot->exec= add_primitive_tube_exec;
1439         ot->poll= ED_operator_scene_editable;
1440         
1441         /* flags */
1442         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1443         
1444         /* props */
1445         RNA_def_int(ot->srna, "vertices", 32, INT_MIN, INT_MAX, "Vertices", "", 2, 500);
1446         RNA_def_float(ot->srna, "radius", 1.0f, 0.0, FLT_MAX, "Radius", "", 0.001, 100.00);
1447         RNA_def_float(ot->srna, "depth", 1.0f, 0.0, FLT_MAX, "Depth", "", 0.001, 100.00);
1448         RNA_def_boolean(ot->srna, "cap_ends", 1, "Cap Ends", "");
1449
1450         ED_object_add_generic_props(ot, TRUE);
1451 }
1452
1453 static int add_primitive_cone_exec(bContext *C, wmOperator *op)
1454 {
1455         int enter_editmode;
1456         unsigned int layer;
1457         float loc[3], rot[3];
1458         
1459         ED_object_add_generic_get_opts(op, loc, rot, &enter_editmode, &layer);
1460
1461         make_prim_ext(C, loc, rot, enter_editmode, layer,
1462                         PRIM_CONE, RNA_int_get(op->ptr, "vertices"), 0, 0,
1463                         RNA_float_get(op->ptr,"radius"), RNA_float_get(op->ptr, "depth"),
1464                         0, RNA_boolean_get(op->ptr, "cap_end"));
1465
1466         return OPERATOR_FINISHED;
1467 }
1468
1469 void MESH_OT_primitive_cone_add(wmOperatorType *ot)
1470 {
1471         /* identifiers */
1472         ot->name= "Add Cone";
1473         ot->description= "Construct a conic mesh (ends filled)";
1474         ot->idname= "MESH_OT_primitive_cone_add";
1475         
1476         /* api callbacks */
1477         ot->invoke= ED_object_add_generic_invoke;
1478         ot->exec= add_primitive_cone_exec;
1479         ot->poll= ED_operator_scene_editable;
1480         
1481         /* flags */
1482         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1483         
1484         /* props */
1485         RNA_def_int(ot->srna, "vertices", 32, INT_MIN, INT_MAX, "Vertices", "", 2, 500);
1486         RNA_def_float(ot->srna, "radius", 1.0f, 0.0, FLT_MAX, "Radius", "", 0.001, 100.00);
1487         RNA_def_float(ot->srna, "depth", 1.0f, 0.0, FLT_MAX, "Depth", "", 0.001, 100.00);
1488         RNA_def_boolean(ot->srna, "cap_end", 0, "Cap End", "");
1489
1490         ED_object_add_generic_props(ot, TRUE);
1491 }
1492
1493 static int add_primitive_grid_exec(bContext *C, wmOperator *op)
1494 {
1495         int enter_editmode;
1496         unsigned int layer;
1497         float loc[3], rot[3];
1498         
1499         ED_object_add_generic_get_opts(op, loc, rot, &enter_editmode, &layer);
1500
1501         make_prim_ext(C, loc, rot, enter_editmode, layer,
1502                         PRIM_GRID, RNA_int_get(op->ptr, "x_subdivisions"),
1503                         RNA_int_get(op->ptr, "y_subdivisions"), 0,
1504                         RNA_float_get(op->ptr,"size"), 0.0f, 0, 1);
1505
1506         return OPERATOR_FINISHED;
1507 }
1508
1509 void MESH_OT_primitive_grid_add(wmOperatorType *ot)
1510 {
1511         /* identifiers */
1512         ot->name= "Add Grid";
1513         ot->description= "Construct a grid mesh";
1514         ot->idname= "MESH_OT_primitive_grid_add";
1515         
1516         /* api callbacks */
1517         ot->invoke= ED_object_add_generic_invoke;
1518         ot->exec= add_primitive_grid_exec;
1519         ot->poll= ED_operator_scene_editable;
1520         
1521         /* flags */
1522         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1523         
1524         /* props */
1525         RNA_def_int(ot->srna, "x_subdivisions", 10, INT_MIN, INT_MAX, "X Subdivisions", "", 3, 1000);
1526         RNA_def_int(ot->srna, "y_subdivisions", 10, INT_MIN, INT_MAX, "Y Subdivisions", "", 3, 1000);
1527         RNA_def_float(ot->srna, "size", 1.0f, 0.0, FLT_MAX, "Size", "", 0.001, FLT_MAX);
1528
1529         ED_object_add_generic_props(ot, TRUE);
1530 }
1531
1532 static int add_primitive_monkey_exec(bContext *C, wmOperator *op)
1533 {
1534         int enter_editmode;
1535         unsigned int layer;
1536         float loc[3], rot[3];
1537         
1538         ED_object_add_generic_get_opts(op, loc, rot, &enter_editmode, &layer);
1539
1540         make_prim_ext(C, loc, rot, enter_editmode, layer,
1541                         PRIM_MONKEY, 0, 0, 2, 0.0f, 0.0f, 0, 0);
1542
1543         return OPERATOR_FINISHED;
1544 }
1545
1546 void MESH_OT_primitive_monkey_add(wmOperatorType *ot)
1547 {
1548         /* identifiers */
1549         ot->name= "Add Monkey";
1550         ot->description= "Construct a Suzanne mesh";
1551         ot->idname= "MESH_OT_primitive_monkey_add";
1552         
1553         /* api callbacks */
1554         ot->invoke= ED_object_add_generic_invoke;
1555         ot->exec= add_primitive_monkey_exec;
1556         ot->poll= ED_operator_scene_editable;
1557         
1558         /* flags */
1559         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1560
1561         ED_object_add_generic_props(ot, TRUE);
1562 }
1563
1564 static int add_primitive_uvsphere_exec(bContext *C, wmOperator *op)
1565 {
1566         int enter_editmode;
1567         unsigned int layer;
1568         float loc[3], rot[3];
1569         
1570         ED_object_add_generic_get_opts(op, loc, rot, &enter_editmode, &layer);
1571
1572         make_prim_ext(C, loc, rot, enter_editmode, layer,
1573                         PRIM_UVSPHERE, RNA_int_get(op->ptr, "rings"),
1574                         RNA_int_get(op->ptr, "segments"), 0,
1575                         RNA_float_get(op->ptr,"size"), 0.0f, 0, 0);
1576
1577         return OPERATOR_FINISHED;       
1578 }
1579
1580 void MESH_OT_primitive_uv_sphere_add(wmOperatorType *ot)
1581 {
1582         /* identifiers */
1583         ot->name= "Add UV Sphere";
1584         ot->description= "Construct a UV sphere mesh";
1585         ot->idname= "MESH_OT_primitive_uv_sphere_add";
1586         
1587         /* api callbacks */
1588         ot->invoke= ED_object_add_generic_invoke;
1589         ot->exec= add_primitive_uvsphere_exec;
1590         ot->poll= ED_operator_scene_editable;
1591         
1592         /* flags */
1593         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1594         
1595         /* props */
1596         RNA_def_int(ot->srna, "segments", 32, INT_MIN, INT_MAX, "Segments", "", 3, 500);
1597         RNA_def_int(ot->srna, "rings", 16, INT_MIN, INT_MAX, "Rings", "", 3, 500);
1598         RNA_def_float(ot->srna, "size", 1.0f, 0.0, FLT_MAX, "Size", "", 0.001, 100.00);
1599
1600         ED_object_add_generic_props(ot, TRUE);
1601 }
1602
1603 static int add_primitive_icosphere_exec(bContext *C, wmOperator *op)
1604 {
1605         int enter_editmode;
1606         unsigned int layer;
1607         float loc[3], rot[3];
1608         
1609         ED_object_add_generic_get_opts(op, loc, rot, &enter_editmode, &layer);
1610
1611         make_prim_ext(C, loc, rot, enter_editmode, layer,
1612                         PRIM_ICOSPHERE, 0, 0, RNA_int_get(op->ptr, "subdivisions"),
1613                         RNA_float_get(op->ptr,"size"), 0.0f, 0, 0);
1614
1615         return OPERATOR_FINISHED;       
1616 }
1617
1618 void MESH_OT_primitive_ico_sphere_add(wmOperatorType *ot)
1619 {
1620         /* identifiers */
1621         ot->name= "Add Ico Sphere";
1622         ot->description= "Construct an Icosphere mesh";
1623         ot->idname= "MESH_OT_primitive_ico_sphere_add";
1624         
1625         /* api callbacks */
1626         ot->invoke= ED_object_add_generic_invoke;
1627         ot->exec= add_primitive_icosphere_exec;
1628         ot->poll= ED_operator_scene_editable;
1629         
1630         /* flags */
1631         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1632         
1633         /* props */
1634         RNA_def_int(ot->srna, "subdivisions", 2, 0, 6, "Subdivisions", "", 0, 8);
1635         RNA_def_float(ot->srna, "size", 1.0f, 0.0f, FLT_MAX, "Size", "", 0.001f, 100.00);
1636
1637         ED_object_add_generic_props(ot, TRUE);
1638 }
1639
1640 /****************** add duplicate operator ***************/
1641
1642 static int mesh_duplicate_exec(bContext *C, wmOperator *op)
1643 {
1644         Object *ob= CTX_data_edit_object(C);
1645         EditMesh *em= BKE_mesh_get_editmesh(ob->data);
1646
1647         adduplicateflag(em, SELECT);
1648
1649         BKE_mesh_end_editmesh(ob->data, em);
1650
1651         DAG_id_flush_update(ob->data, OB_RECALC_DATA);
1652         WM_event_add_notifier(C, NC_GEOM|ND_DATA, ob->data);
1653         
1654         return OPERATOR_FINISHED;
1655 }
1656
1657 static int mesh_duplicate_invoke(bContext *C, wmOperator *op, wmEvent *event)
1658 {
1659         WM_cursor_wait(1);
1660         mesh_duplicate_exec(C, op);
1661         WM_cursor_wait(0);
1662         
1663         return OPERATOR_FINISHED;
1664 }
1665
1666 void MESH_OT_duplicate(wmOperatorType *ot)
1667 {
1668         /* identifiers */
1669         ot->name= "Duplicate";
1670         ot->description= "Duplicate selected vertices, edges or faces";
1671         ot->idname= "MESH_OT_duplicate";
1672         
1673         /* api callbacks */
1674         ot->invoke= mesh_duplicate_invoke;
1675         ot->exec= mesh_duplicate_exec;
1676         
1677         ot->poll= ED_operator_editmesh;
1678         
1679         /* to give to transform */
1680         RNA_def_int(ot->srna, "mode", TFM_TRANSLATION, 0, INT_MAX, "Mode", "", 0, INT_MAX);
1681 }
1682