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