Export_3ds: Added distance cue chunk export
[blender-addons.git] / mesh_tiny_cad / E2F.py
blob2205b51a182ef6ba0e610e93a0a789064fbe2c8a
1 # SPDX-FileCopyrightText: 2016-2022 Blender Foundation
3 # SPDX-License-Identifier: GPL-2.0-or-later
6 import bpy
7 import bmesh
8 from mathutils.geometry import intersect_line_plane
11 def failure_message(self):
12 self.report({"WARNING"}, 'select 1 face and 1 detached edge')
14 def failure_message_on_plane(self):
15 msg2 = """\
16 Edge2Face expects the edge to intersect at one point on the plane of the selected face. You're
17 seeing this warning because mathutils.geometry.intersect_line_plane is being called on an edge/face
18 combination that has no clear intersection point ( both points of the edge either touch the same
19 plane as the face or they lie in a plane that is offset along the face's normal )"""
20 lines = msg2.split('\n')
21 for line in lines:
22 self.report({'INFO'}, line)
23 self.report({"WARNING"}, 'No intersection found, see the info panel for details')
26 def extend_vertex(self):
28 obj = bpy.context.edit_object
29 me = obj.data
30 bm = bmesh.from_edit_mesh(me)
31 verts = bm.verts
32 faces = bm.faces
34 planes = [f for f in faces if f.select]
35 if not (len(planes) == 1):
36 failure_message(self)
37 return
39 plane = planes[0]
40 plane_vert_indices = [v for v in plane.verts[:]]
41 all_selected_vert_indices = [v for v in verts if v.select]
43 M = set(plane_vert_indices)
44 N = set(all_selected_vert_indices)
45 O = N.difference(M)
46 O = list(O)
48 if not len(O) == 2:
49 failure_message(self)
50 return
52 (v1_ref, v1), (v2_ref, v2) = [(i, i.co) for i in O]
54 plane_co = plane.calc_center_median()
55 plane_no = plane.normal
57 new_co = intersect_line_plane(v1, v2, plane_co, plane_no, False)
59 if new_co:
60 new_vertex = verts.new(new_co)
61 A_len = (v1 - new_co).length
62 B_len = (v2 - new_co).length
64 vertex_reference = v1_ref if (A_len < B_len) else v2_ref
65 bm.edges.new([vertex_reference, new_vertex])
66 bmesh.update_edit_mesh(me, loop_triangles=True)
68 else:
69 failure_message_on_plane(self)
73 class TCEdgeToFace(bpy.types.Operator):
74 '''Extend selected edge towards projected intersection with a selected face'''
75 bl_idname = 'tinycad.edge_to_face'
76 bl_label = 'E2F edge to face'
77 bl_options = {'REGISTER', 'UNDO'}
79 @classmethod
80 def poll(cls, context):
81 ob = context.object
82 return all([bool(ob), ob.type == 'MESH', ob.mode == 'EDIT'])
84 def execute(self, context):
85 extend_vertex(self)
86 return {'FINISHED'}