| 
									
										
										
										
											2013-03-15 13:06:31 +00:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * ***** BEGIN GPL LICENSE BLOCK ***** | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  |  * modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  |  * as published by the Free Software Foundation; either version 2 | 
					
						
							|  |  |  |  * of the License, or (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |  * along with this program; if not, write to the Free Software Foundation, | 
					
						
							|  |  |  |  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2013-04-01 10:18:01 +00:00
										 |  |  |  * The Original Code is Copyright (C) 2013 Blender Foundation. | 
					
						
							| 
									
										
										
										
											2013-03-15 13:06:31 +00:00
										 |  |  |  * All rights reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Contributor(s): Campbell Barton | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * ***** END GPL LICENSE BLOCK ***** | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** \file blender/editors/mesh/editmesh_knife_project.c
 | 
					
						
							|  |  |  |  *  \ingroup edmesh | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "DNA_curve_types.h"
 | 
					
						
							|  |  |  | #include "DNA_object_types.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "BLI_math.h"
 | 
					
						
							|  |  |  | #include "BLI_linklist.h"
 | 
					
						
							|  |  |  | #include "BLI_listbase.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "BKE_mesh.h"
 | 
					
						
							|  |  |  | #include "BKE_context.h"
 | 
					
						
							|  |  |  | #include "BKE_curve.h"
 | 
					
						
							|  |  |  | #include "BKE_cdderivedmesh.h"
 | 
					
						
							| 
									
										
										
										
											2013-04-13 20:31:52 +00:00
										 |  |  | #include "BKE_editmesh.h"
 | 
					
						
							| 
									
										
										
										
											2013-03-15 13:06:31 +00:00
										 |  |  | #include "BKE_report.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-21 11:53:13 +02:00
										 |  |  | #include "DEG_depsgraph.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-01 11:42:11 +00:00
										 |  |  | #include "RNA_define.h"
 | 
					
						
							|  |  |  | #include "RNA_access.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-15 13:06:31 +00:00
										 |  |  | #include "MEM_guardedalloc.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "WM_types.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-20 16:03:34 +00:00
										 |  |  | #include "ED_mesh.h"
 | 
					
						
							| 
									
										
										
										
											2013-03-15 13:06:31 +00:00
										 |  |  | #include "ED_screen.h"
 | 
					
						
							|  |  |  | #include "ED_view3d.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-01 10:18:01 +00:00
										 |  |  | #include "mesh_intern.h"  /* own include */
 | 
					
						
							| 
									
										
										
										
											2013-03-15 13:06:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-21 11:53:13 +02:00
										 |  |  | static LinkNode *knifeproject_poly_from_object(const bContext *C, Scene *scene, Object *ob, LinkNode *polys) | 
					
						
							| 
									
										
										
										
											2013-03-15 13:06:31 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-07-21 11:53:13 +02:00
										 |  |  | 	ARegion *ar = CTX_wm_region(C); | 
					
						
							|  |  |  | 	EvaluationContext eval_ctx; | 
					
						
							| 
									
										
										
										
											2013-03-15 13:06:31 +00:00
										 |  |  | 	DerivedMesh *dm; | 
					
						
							|  |  |  | 	bool dm_needsFree; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-21 11:53:13 +02:00
										 |  |  | 	CTX_data_eval_ctx(C, &eval_ctx); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-15 13:06:31 +00:00
										 |  |  | 	if (ob->type == OB_MESH || ob->derivedFinal) { | 
					
						
							| 
									
										
										
										
											2017-07-21 11:53:13 +02:00
										 |  |  | 		dm = ob->derivedFinal ? ob->derivedFinal : mesh_get_derived_final(&eval_ctx, scene, ob, CD_MASK_BAREMESH); | 
					
						
							| 
									
										
										
										
											2013-03-15 13:06:31 +00:00
										 |  |  | 		dm_needsFree = false; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-07-20 01:30:29 +10:00
										 |  |  | 	else if (ELEM(ob->type, OB_FONT, OB_CURVE, OB_SURF)) { | 
					
						
							| 
									
										
										
										
											2013-03-15 13:06:31 +00:00
										 |  |  | 		dm = CDDM_from_curve(ob); | 
					
						
							|  |  |  | 		dm_needsFree = true; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-03-16 16:10:27 +00:00
										 |  |  | 	else { | 
					
						
							|  |  |  | 		dm = NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-03-15 13:06:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (dm) { | 
					
						
							|  |  |  | 		ListBase nurbslist = {NULL, NULL}; | 
					
						
							|  |  |  | 		float projmat[4][4]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		BKE_mesh_to_curve_nurblist(dm, &nurbslist, 0);  /* wire */ | 
					
						
							|  |  |  | 		BKE_mesh_to_curve_nurblist(dm, &nurbslist, 1);  /* boundary */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		ED_view3d_ob_project_mat_get(ar->regiondata, ob, projmat); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (nurbslist.first) { | 
					
						
							|  |  |  | 			Nurb *nu; | 
					
						
							|  |  |  | 			for (nu = nurbslist.first; nu; nu = nu->next) { | 
					
						
							|  |  |  | 				if (nu->bp) { | 
					
						
							|  |  |  | 					int a; | 
					
						
							|  |  |  | 					BPoint *bp; | 
					
						
							|  |  |  | 					bool is_cyclic = (nu->flagu & CU_NURB_CYCLIC) != 0; | 
					
						
							|  |  |  | 					float (*mval)[2] = MEM_mallocN(sizeof(*mval) * (nu->pntsu + is_cyclic), __func__); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					for (bp = nu->bp, a = 0; a < nu->pntsu; a++, bp++) { | 
					
						
							| 
									
										
										
										
											2013-05-01 17:03:00 +00:00
										 |  |  | 						ED_view3d_project_float_v2_m4(ar, bp->vec, mval[a], projmat); | 
					
						
							| 
									
										
										
										
											2013-03-15 13:06:31 +00:00
										 |  |  | 					} | 
					
						
							|  |  |  | 					if (is_cyclic) { | 
					
						
							|  |  |  | 						copy_v2_v2(mval[a], mval[0]); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					BLI_linklist_prepend(&polys, mval); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		BKE_nurbList_free(&nurbslist); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (dm_needsFree) { | 
					
						
							|  |  |  | 			dm->release(dm); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return polys; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int knifeproject_exec(bContext *C, wmOperator *op) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	Scene *scene = CTX_data_scene(C); | 
					
						
							|  |  |  | 	Object *obedit = CTX_data_edit_object(C); | 
					
						
							| 
									
										
										
										
											2013-04-16 05:59:48 +00:00
										 |  |  | 	BMEditMesh *em = BKE_editmesh_from_object(obedit); | 
					
						
							| 
									
										
										
										
											2013-11-01 11:42:11 +00:00
										 |  |  | 	const bool cut_through = RNA_boolean_get(op->ptr, "cut_through"); | 
					
						
							| 
									
										
										
										
											2013-03-15 13:06:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	LinkNode *polys = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	CTX_DATA_BEGIN (C, Object *, ob, selected_objects) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if (ob != obedit) { | 
					
						
							| 
									
										
										
										
											2017-07-21 11:53:13 +02:00
										 |  |  | 			polys = knifeproject_poly_from_object(C, scene, ob, polys); | 
					
						
							| 
									
										
										
										
											2013-03-15 13:06:31 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	CTX_DATA_END; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (polys) { | 
					
						
							| 
									
										
										
										
											2013-11-01 11:42:11 +00:00
										 |  |  | 		EDBM_mesh_knife(C, polys, true, cut_through); | 
					
						
							| 
									
										
										
										
											2013-03-15 13:06:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		/* select only tagged faces */ | 
					
						
							| 
									
										
										
										
											2013-03-15 13:18:35 +00:00
										 |  |  | 		BM_mesh_elem_hflag_disable_all(em->bm, BM_VERT | BM_EDGE | BM_FACE, BM_ELEM_SELECT, false); | 
					
						
							| 
									
										
										
										
											2013-03-20 16:03:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-21 02:58:21 +00:00
										 |  |  | 		/* not essential, but switch out of vertex mode since the
 | 
					
						
							| 
									
										
										
										
											2013-03-20 16:03:34 +00:00
										 |  |  | 		 * selected regions wont be nicely isolated after flushing. | 
					
						
							|  |  |  | 		 * note: call after de-select to avoid selection flushing */ | 
					
						
							|  |  |  | 		EDBM_selectmode_disable(scene, em, SCE_SELECT_VERTEX, SCE_SELECT_EDGE); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-26 23:34:09 +11:00
										 |  |  | 		BM_mesh_elem_hflag_enable_test(em->bm, BM_FACE, BM_ELEM_SELECT, true, false, BM_ELEM_TAG); | 
					
						
							| 
									
										
										
										
											2013-03-15 13:06:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-15 13:18:35 +00:00
										 |  |  | 		BM_mesh_select_mode_flush(em->bm); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-15 13:06:31 +00:00
										 |  |  | 		BLI_linklist_freeN(polys); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return OPERATOR_FINISHED; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							|  |  |  | 		BKE_report(op->reports, RPT_ERROR, "No other selected objects found to use for projection"); | 
					
						
							|  |  |  | 		return OPERATOR_CANCELLED; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void MESH_OT_knife_project(wmOperatorType *ot) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	/* description */ | 
					
						
							|  |  |  | 	ot->name = "Knife Project"; | 
					
						
							|  |  |  | 	ot->idname = "MESH_OT_knife_project"; | 
					
						
							|  |  |  | 	ot->description = "Use other objects outlines & boundaries to project knife cuts"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* callbacks */ | 
					
						
							|  |  |  | 	ot->exec = knifeproject_exec; | 
					
						
							| 
									
										
										
										
											2015-04-17 08:09:16 +02:00
										 |  |  | 	ot->poll = ED_operator_editmesh_region_view3d; | 
					
						
							| 
									
										
										
										
											2013-03-15 13:06:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* flags */ | 
					
						
							|  |  |  | 	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_BLOCKING; | 
					
						
							| 
									
										
										
										
											2013-11-01 11:42:11 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* parameters */ | 
					
						
							|  |  |  | 	RNA_def_boolean(ot->srna, "cut_through", false, "Cut through", "Cut through all faces, not just visible ones"); | 
					
						
							| 
									
										
										
										
											2013-03-15 13:06:31 +00:00
										 |  |  | } |