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

pdt_bix.py « precision_drawing_tools - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bf3e8ab8c47ec547a8c4f0fa6e3da1c5f540a333 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# ##### 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>
#
# ----------------------------------------------------------
# Author: Zeffii
# Modified by: Alan Odom (Clockmender) & Rune Morling (ermo)
# ----------------------------------------------------------
#
#
import bpy
import bmesh
from . import pdt_cad_module as cm
from .pdt_msg_strings import (
    PDT_ERR_2CPNPE,
    PDT_ERR_NCEDGES,
    PDT_ERR_EDOB_MODE,
)
from .pdt_functions import debug, oops


def add_line_to_bisection(context):
    """Computes Bisector of 2 Co-Planar Edges.

    Args:
        context: Blender bpy.context instance

    Returns:
        Nothing.
    """

    obj = context.object
    if all([bool(obj), obj.type == "MESH", obj.mode == "EDIT"]):
        pg = context.scene.pdt_pg
        obj_data = obj.data
        bm = bmesh.from_edit_mesh(obj_data)

        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:
            pg.error = f"{PDT_ERR_2CPNPE}"
            context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
            return

        [[vector_a, vector_b], [vector_c, vector_d]] = [[v.co for v in e.verts] for e in edges]
        debug(f"vectors found:\n {vector_a}\n {vector_b}\n {vector_c}\n {vector_d}")

        dist1 = (vector_a - vector_b).length
        dist2 = (vector_c - vector_d).length
        bdist = min([dist1, dist2])
        edge1 = (vector_a, vector_b)
        edge2 = (vector_c, vector_d)

        if not cm.test_coplanar(edge1, edge2):
            pg.error = PDT_ERR_NCEDGES
            context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
            return

        # get intersect_point and pick farthest vertex from (projected) intersections
        intersect_point = cm.get_intersection(edge1, edge2)
        far1 = (
            vector_b
            if (vector_a - intersect_point).length < (vector_b - intersect_point).length
            else vector_a
        )
        far2 = (
            vector_d
            if (vector_c - intersect_point).length < (vector_d - intersect_point).length
            else vector_c
        )

        dex1 = far1 - intersect_point
        dex2 = far2 - intersect_point
        dex1 = dex1 * (bdist / dex1.length)
        dex2 = dex2 * (bdist / dex2.length)
        intersect_point2 = intersect_point + (dex1).lerp(dex2, 0.5)
        intersect_point3 = intersect_point2.lerp(intersect_point, 2.0)

        vec1 = bm.verts.new(intersect_point2)
        vec2 = bm.verts.new(intersect_point)
        vec3 = bm.verts.new(intersect_point3)
        bm.edges.new((vec1, vec2))
        bm.edges.new((vec2, vec3))
        bmesh.ops.remove_doubles(bm, verts=bm.verts, dist=0.0001)
        bmesh.update_edit_mesh(obj_data)
    else:
        pg.error = f"{PDT_ERR_EDOB_MODE},{obj.mode})"
        context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
        return


class PDT_OT_LineOnBisection(bpy.types.Operator):
    """Create Bisector between 2 Selected Edges"""

    bl_idname = "pdt.linetobisect"
    bl_label = "Add Edges Bisector"
    bl_options = {"REGISTER", "UNDO"}

    @classmethod
    def poll(cls, context):
        """Only allow operation on a mesh object in EDIT mode.

        Args:
            context: Blender bpy.context instance.

        Returns:
            Boolean.
        """

        obj = context.active_object
        if obj is None:
            return False
        return all([obj is not None, obj.type == "MESH", obj.mode == "EDIT"])

    def execute(self, context):
        """Computes Bisector of 2 Co-Planar Edges.

        Note:
            Requires an Active Object
            Active Object must be Mesh type
            Active Object must be in Edit Mode.

        Args:
            context: Blender bpy.context instance.

        Returns:
            Status Set.
        """

        pg = context.scene.pdt_pg
        pg.command = f"bis"
        return {"FINISHED"}