Sun position: Fix T80379 - Custom startup breaks the add-on
[blender-addons.git] / mesh_tiny_cad / E2F.py
blob70b51298768819f7dfb66f2614735a703e7b0ec6
1 # ##### BEGIN GPL LICENSE BLOCK #####
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software Foundation,
15 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 # ##### END GPL LICENSE BLOCK #####
19 # <pep8 compliant>
22 import bpy
23 import bmesh
24 from mathutils.geometry import intersect_line_plane
27 def failure_message(self):
28 self.report({"WARNING"}, 'select 1 face and 1 detached edge')
30 def failure_message_on_plane(self):
31 msg2 = """\
32 Edge2Face expects the edge to intersect at one point on the plane of the selected face. You're
33 seeing this warning because mathutils.geometry.intersect_line_plane is being called on an edge/face
34 combination that has no clear intersection point ( both points of the edge either touch the same
35 plane as the face or they lie in a plane that is offset along the face's normal )"""
36 lines = msg2.split('\n')
37 for line in lines:
38 self.report({'INFO'}, line)
39 self.report({"WARNING"}, 'No intersection found, see the info panel for details')
42 def extend_vertex(self):
44 obj = bpy.context.edit_object
45 me = obj.data
46 bm = bmesh.from_edit_mesh(me)
47 verts = bm.verts
48 faces = bm.faces
50 planes = [f for f in faces if f.select]
51 if not (len(planes) == 1):
52 failure_message(self)
53 return
55 plane = planes[0]
56 plane_vert_indices = [v for v in plane.verts[:]]
57 all_selected_vert_indices = [v for v in verts if v.select]
59 M = set(plane_vert_indices)
60 N = set(all_selected_vert_indices)
61 O = N.difference(M)
62 O = list(O)
64 if not len(O) == 2:
65 failure_message(self)
66 return
68 (v1_ref, v1), (v2_ref, v2) = [(i, i.co) for i in O]
70 plane_co = plane.calc_center_median()
71 plane_no = plane.normal
73 new_co = intersect_line_plane(v1, v2, plane_co, plane_no, False)
75 if new_co:
76 new_vertex = verts.new(new_co)
77 A_len = (v1 - new_co).length
78 B_len = (v2 - new_co).length
80 vertex_reference = v1_ref if (A_len < B_len) else v2_ref
81 bm.edges.new([vertex_reference, new_vertex])
82 bmesh.update_edit_mesh(me, True)
84 else:
85 failure_message_on_plane(self)
89 class TCEdgeToFace(bpy.types.Operator):
90 '''Extend selected edge towards projected intersection with a selected face'''
91 bl_idname = 'tinycad.edge_to_face'
92 bl_label = 'E2F edge to face'
93 bl_options = {'REGISTER', 'UNDO'}
95 @classmethod
96 def poll(cls, context):
97 ob = context.object
98 return all([bool(ob), ob.type == 'MESH', ob.mode == 'EDIT'])
100 def execute(self, context):
101 extend_vertex(self)
102 return {'FINISHED'}