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