File size: 8,609 Bytes
985c397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# SPDX-License-Identifier: LGPL-2.1-or-later

# ***************************************************************************
# *   Copyright (c) 2009, 2010 Yorik van Havre <yorik@uncreated.net>        *
# *   Copyright (c) 2009, 2010 Ken Cline <cline@frii.com>                   *
# *                                                                         *
# *   This file is part of the FreeCAD CAx development system.              *
# *                                                                         *
# *   This program is free software; you can redistribute it and/or modify  *
# *   it under the terms of the GNU Lesser General Public License (LGPL)    *
# *   as published by the Free Software Foundation; either version 2 of     *
# *   the License, or (at your option) any later version.                   *
# *   for detail see the LICENCE text file.                                 *
# *                                                                         *
# *   FreeCAD 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 Library General Public License for more details.                  *
# *                                                                         *
# *   You should have received a copy of the GNU Library General Public     *
# *   License along with FreeCAD; if not, write to the Free Software        *
# *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
# *   USA                                                                   *
# *                                                                         *
# ***************************************************************************
"""Provides various functions to work with edges."""
## @package edges
# \ingroup draftgeoutils
# \brief Provides various functions to work with edges.

import lazy_loader.lazy_loader as lz

import FreeCAD as App
import DraftVecUtils

from draftgeoutils.general import geomType, vec

# Delay import of module until first use because it is heavy
Part = lz.LazyLoader("Part", globals(), "Part")

## \addtogroup draftgeoutils
# @{


def findEdge(anEdge, aList):
    """Return True if edge is found in list of edges."""
    for e in range(len(aList)):
        if str(anEdge.Curve) == str(aList[e].Curve):
            if DraftVecUtils.equals(anEdge.Vertexes[0].Point, aList[e].Vertexes[0].Point):
                if DraftVecUtils.equals(anEdge.Vertexes[-1].Point, aList[e].Vertexes[-1].Point):
                    return e
    return None


def orientEdge(edge, normal=None, make_arc=False):
    """Re-orient the edge such that it is in the XY plane.

    Re-orients `edge` such that it is in the XY plane.
    If `normal` is passed, this is used as the basis for the rotation,
    otherwise the placement of `edge` is used.
    """
    # This 'normalizes' the placement to the xy plane
    edge = edge.copy()
    xyDir = App.Vector(0, 0, 1)
    base = App.Vector(0, 0, 0)

    if normal:
        angle = DraftVecUtils.angle(normal, xyDir) * App.Units.Radian
        axis = normal.cross(xyDir)
    else:
        axis = edge.Placement.Rotation.Axis
        angle = -1 * edge.Placement.Rotation.Angle * App.Units.Radian
    if axis == App.Vector(0.0, 0.0, 0.0):
        axis = App.Vector(0.0, 0.0, 1.0)
    if angle:
        edge.rotate(base, axis, angle)
    if isinstance(edge.Curve, Part.Line):
        return Part.LineSegment(edge.Curve, edge.FirstParameter, edge.LastParameter)
    elif make_arc and isinstance(edge.Curve, Part.Circle) and not edge.Closed:
        return Part.ArcOfCircle(
            edge.Curve, edge.FirstParameter, edge.LastParameter, edge.Curve.Axis.z > 0
        )
    elif make_arc and isinstance(edge.Curve, Part.Ellipse) and not edge.Closed:
        return Part.ArcOfEllipse(
            edge.Curve, edge.FirstParameter, edge.LastParameter, edge.Curve.Axis.z > 0
        )
    return edge.Curve


def isSameLine(e1, e2):
    """Return True if the 2 edges are lines and have the same points."""
    if not isinstance(e1.Curve, Part.LineSegment):
        return False
    if not isinstance(e2.Curve, Part.LineSegment):
        return False

    if DraftVecUtils.equals(e1.Vertexes[0].Point, e2.Vertexes[0].Point) and DraftVecUtils.equals(
        e1.Vertexes[-1].Point, e2.Vertexes[-1].Point
    ):
        return True
    elif DraftVecUtils.equals(e1.Vertexes[-1].Point, e2.Vertexes[0].Point) and DraftVecUtils.equals(
        e1.Vertexes[0].Point, e2.Vertexes[-1].Point
    ):
        return True
    return False


def is_line(bspline):
    """Return True if the given BSpline curve is a straight line."""

    # previous implementation may fail for a multipole straight spline due
    # a second order error in tolerance, which introduce a difference of 1e-14
    # in the values of the tangents. Also, may fail on a periodic spline.
    #    step = bspline.LastParameter/10
    #    b = bspline.tangent(0)
    #
    #    for i in range(10):
    #        if bspline.tangent(i * step) != b:
    #            return False

    start_point = bspline.StartPoint
    end_point = bspline.EndPoint
    dist_start_end = end_point.distanceToPoint(start_point)
    if abs(bspline.length() - dist_start_end) < 1e-7:
        return True

    return False


def invert(shape):
    """Return an inverted copy of the edge or wire contained in the shape."""
    if shape.ShapeType == "Wire":
        edges = [invert(edge) for edge in shape.OrderedEdges]
        edges.reverse()
        return Part.Wire(edges)
    elif shape.ShapeType == "Edge":
        if len(shape.Vertexes) == 1:
            return shape
        if geomType(shape) == "Line":
            return Part.LineSegment(shape.Vertexes[-1].Point, shape.Vertexes[0].Point).toShape()
        elif geomType(shape) == "Circle":
            mp = findMidpoint(shape)
            return Part.Arc(shape.Vertexes[-1].Point, mp, shape.Vertexes[0].Point).toShape()
        elif geomType(shape) in ["BSplineCurve", "BezierCurve"]:
            if isLine(shape.Curve):
                return Part.LineSegment(shape.Vertexes[-1].Point, shape.Vertexes[0].Point).toShape()

        print("DraftGeomUtils.invert: unable to invert", shape.Curve)
        return shape
    else:
        print("DraftGeomUtils.invert: unable to handle", shape.ShapeType)
        return shape


def findMidpoint(edge):
    """Return the midpoint of an edge."""
    if edge.Length == 0:
        return None
    else:
        return edge.valueAt(edge.Curve.parameterAtDistance(edge.Length / 2, edge.FirstParameter))


def getTangent(edge, from_point=None):
    """Return the tangent to an edge, including BSpline and circular arcs.

    If from_point is given, it is used to calculate the tangent,
    only useful for a circular arc.
    """
    if geomType(edge) == "Line":
        return vec(edge)

    elif geomType(edge) == "BSplineCurve" or geomType(edge) == "BezierCurve":
        if not from_point:
            return None
        cp = edge.Curve.parameter(from_point)
        return edge.Curve.tangent(cp)[0]

    elif geomType(edge) == "Circle":
        if not from_point:
            v1 = edge.Vertexes[0].Point.sub(edge.Curve.Center)
        else:
            v1 = from_point.sub(edge.Curve.Center)
        return v1.cross(edge.Curve.Axis)

    return None


def get_referenced_edges(property_value):
    """Return the Edges referenced by the value of a App:PropertyLink, App::PropertyLinkList,
    App::PropertyLinkSub or App::PropertyLinkSubList property."""
    edges = []
    if not isinstance(property_value, list):
        property_value = [property_value]
    for element in property_value:
        if hasattr(element, "Shape") and element.Shape:
            edges += element.Shape.Edges
        elif isinstance(element, tuple) and len(element) == 2:
            object, subelement_names = element
            if hasattr(object, "Shape") and object.Shape:
                if len(subelement_names) == 1 and subelement_names[0] == "":
                    edges += object.Shape.Edges
                else:
                    for subelement_name in subelement_names:
                        if subelement_name.startswith("Edge"):
                            edge_number = int(subelement_name.lstrip("Edge")) - 1
                            if edge_number < len(object.Shape.Edges):
                                edges.append(object.Shape.Edges[edge_number])
    return edges


# compatibility layer

isLine = is_line

## @}