Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BIX.py « mesh_tiny_cad - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 89908f607ecffffb4e3c8317c6a60b147d4439e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# ##### 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.
#
# ##### END GPL LICENSE BLOCK #####

# <pep8 compliant>


import bpy
import bmesh
from . import cad_module as cm


def add_line_to_bisection(self):

    obj = bpy.context.object
    me = obj.data
    bm = bmesh.from_edit_mesh(me)

    bm.verts.ensure_lookup_table()
    bm.edges.ensure_lookup_table()

    edges = [e for e in bm.edges if e.select and not e.hide]

    if not len(edges) == 2:
        msg = "select two coplanar non parallel edges"
        self.report({"WARNING"}, msg)
        return

    [[v1, v2], [v3, v4]] = [[v.co for v in e.verts] for e in edges]
    print('vectors found:\n', v1, '\n', v2, '\n', v3, '\n', v4)

    dist1 = (v1 - v2).length
    dist2 = (v3 - v4).length
    bdist = min([dist1, dist2])
    edge1 = (v1, v2)
    edge2 = (v3, v4)

    if not cm.test_coplanar(edge1, edge2):
        msg = "edges must be coplanar non parallel edges"
        self.report({"WARNING"}, msg)
        return

    # get pt and pick fartest vertex from (projected) intersections
    pt = cm.get_intersection(edge1, edge2)
    far1 = v2 if (v1 - pt).length < (v2 - pt).length else v1
    far2 = v4 if (v3 - pt).length < (v4 - pt).length else v3
    # print('intersection: ', pt)

    dex1 = far1 - pt
    dex2 = far2 - pt
    dex1 = dex1 * (bdist / dex1.length)
    dex2 = dex2 * (bdist / dex2.length)
    pt2 = pt + (dex1).lerp(dex2, 0.5)
    # print('bisector point:', pt2)

    pt3 = pt2.lerp(pt, 2.0)

    vec1 = bm.verts.new(pt2)
    vec2 = bm.verts.new(pt)
    vec3 = bm.verts.new(pt3)
    bm.edges.new((vec1, vec2))
    bm.edges.new((vec2, vec3))
    bmesh.update_edit_mesh(me)
    # print("done")


class TCLineOnBisection(bpy.types.Operator):
    '''Generate the bisector of two selected edges'''
    bl_idname = 'tinycad.linetobisect'
    bl_label = 'BIX line to bisector'
    bl_options = {'REGISTER', 'UNDO'}

    @classmethod
    def poll(cls, context):
        obj = context.active_object
        return all([obj is not None, obj.type == 'MESH', obj.mode == 'EDIT'])

    def execute(self, context):
        add_line_to_bisection(self)
        return {'FINISHED'}