File size: 12,587 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# 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>                   *
# *   Copyright (c) 2020 FreeCAD Developers                                 *
# *                                                                         *
# *   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.                                 *
# *                                                                         *
# *   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 Library General Public License for more details.                  *
# *                                                                         *
# *   You should have received a copy of the GNU Library General Public     *
# *   License along with this program; if not, write to the Free Software   *
# *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
# *   USA                                                                   *
# *                                                                         *
# ***************************************************************************
"""Provides the object code for the Wire (Polyline) object."""
## @package wire
# \ingroup draftobjects
# \brief Provides the object code for the Wire (Polyline) object.

## \addtogroup draftobjects
# @{
import math
from PySide.QtCore import QT_TRANSLATE_NOOP

import FreeCAD as App
import DraftGeomUtils
import DraftVecUtils
from draftobjects.base import DraftObject
from draftutils import gui_utils
from draftutils import params
from draftutils.messages import _log


class Wire(DraftObject):
    """The Wire object"""

    def __init__(self, obj):
        super().__init__(obj, "Wire")

        _tip = QT_TRANSLATE_NOOP("App::Property", "The vertices of the wire")
        obj.addProperty("App::PropertyVectorList", "Points", "Draft", _tip, locked=True)

        _tip = QT_TRANSLATE_NOOP("App::Property", "If the wire is closed or not")
        obj.addProperty("App::PropertyBool", "Closed", "Draft", _tip, locked=True)

        _tip = QT_TRANSLATE_NOOP(
            "App::Property", "The base object is the wire, it's formed from 2 objects"
        )
        obj.addProperty("App::PropertyLink", "Base", "Draft", _tip, locked=True)

        _tip = QT_TRANSLATE_NOOP(
            "App::Property", "The tool object is the wire, it's formed from 2 objects"
        )
        obj.addProperty("App::PropertyLink", "Tool", "Draft", _tip, locked=True)

        _tip = QT_TRANSLATE_NOOP("App::Property", "The start point of this line")
        obj.addProperty("App::PropertyVectorDistance", "Start", "Draft", _tip, locked=True)

        _tip = QT_TRANSLATE_NOOP("App::Property", "The end point of this line")
        obj.addProperty("App::PropertyVectorDistance", "End", "Draft", _tip, locked=True)

        _tip = QT_TRANSLATE_NOOP("App::Property", "The length of this line")
        obj.addProperty("App::PropertyLength", "Length", "Draft", _tip, locked=True)

        _tip = QT_TRANSLATE_NOOP("App::Property", "Radius to use to fillet the corners")
        obj.addProperty("App::PropertyLength", "FilletRadius", "Draft", _tip, locked=True)

        _tip = QT_TRANSLATE_NOOP("App::Property", "Size of the chamfer to give to the corners")
        obj.addProperty("App::PropertyLength", "ChamferSize", "Draft", _tip, locked=True)

        _tip = QT_TRANSLATE_NOOP("App::Property", "Create a face if this object is closed")
        obj.addProperty("App::PropertyBool", "MakeFace", "Draft", _tip, locked=True)

        _tip = QT_TRANSLATE_NOOP("App::Property", "The number of subdivisions of each edge")
        obj.addProperty("App::PropertyInteger", "Subdivisions", "Draft", _tip, locked=True)

        _tip = QT_TRANSLATE_NOOP("App::Property", "The area of this object")
        obj.addProperty("App::PropertyArea", "Area", "Draft", _tip, locked=True)

        obj.MakeFace = params.get_param("MakeFaceMode")
        obj.Closed = False

    def onDocumentRestored(self, obj):
        super().onDocumentRestored(obj)
        gui_utils.restore_view_object(obj, vp_module="view_wire", vp_class="ViewProviderWire")

        vobj = getattr(obj, "ViewObject", None)
        if vobj is None:
            return

        if hasattr(vobj, "ArrowSize") or hasattr(vobj, "ArrowType") or hasattr(vobj, "EndArrow"):
            self.update_properties_1v1(obj, vobj)

    def update_properties_1v1(self, obj, vobj):
        """Update view properties."""
        vobj.Proxy._set_properties(vobj)
        if getattr(vobj, "EndArrow", False) and hasattr(vobj, "ArrowType"):
            vobj.ArrowTypeEnd = vobj.ArrowType
        if hasattr(vobj, "ArrowSize"):
            vobj.ArrowSizeStart = vobj.ArrowSize
            vobj.ArrowSizeEnd = vobj.ArrowSize
            vobj.setPropertyStatus("ArrowSize", "-LockDynamic")
            vobj.removeProperty("ArrowSize")
        if hasattr(vobj, "ArrowType"):
            vobj.setPropertyStatus("ArrowType", "-LockDynamic")
            vobj.removeProperty("ArrowType")
        if hasattr(vobj, "EndArrow"):
            vobj.setPropertyStatus("EndArrow", "-LockDynamic")
            vobj.removeProperty("EndArrow")
        _log("v1.1, " + obj.Name + ", migrated view properties")

    def execute(self, obj):
        if self.props_changed_placement_only(
            obj
        ):  # Supplying obj is required because of `Base` and `Tool`.
            obj.positionBySupport()
            self.update_start_end(obj)
            self.props_changed_clear()
            return

        import Part

        plm = obj.Placement
        if obj.Base and (not obj.Tool):
            if obj.Base.isDerivedFrom("Sketcher::SketchObject"):
                shape = obj.Base.Shape.copy()
                if obj.Base.Shape.isClosed():
                    if getattr(obj, "MakeFace", True):
                        shape = Part.Face(shape)
                obj.Shape = shape
        elif obj.Base and obj.Tool:
            if hasattr(obj.Base, "Shape") and hasattr(obj.Tool, "Shape"):
                if (not obj.Base.Shape.isNull()) and (not obj.Tool.Shape.isNull()):
                    sh1 = obj.Base.Shape.copy()
                    sh2 = obj.Tool.Shape.copy()
                    shape = sh1.fuse(sh2)
                    if DraftGeomUtils.isCoplanar(shape.Faces):
                        shape = DraftGeomUtils.concatenate(shape)
                        obj.Shape = shape
                        p = []
                        for v in shape.Vertexes:
                            p.append(v.Point)
                        if obj.Points != p:
                            obj.Points = p
        elif obj.Points:
            if obj.Points[0] == obj.Points[-1]:
                if not obj.Closed:
                    obj.Closed = True
                obj.Points.pop()
            if obj.Closed and (len(obj.Points) > 2):
                pts = obj.Points
                if getattr(obj, "Subdivisions", 0) > 0:
                    npts = []
                    for i in range(len(pts)):
                        p1 = pts[i]
                        npts.append(pts[i])
                        if i == len(pts) - 1:
                            p2 = pts[0]
                        else:
                            p2 = pts[i + 1]
                        v = p2.sub(p1)
                        v = DraftVecUtils.scaleTo(v, v.Length / (obj.Subdivisions + 1))
                        for j in range(obj.Subdivisions):
                            npts.append(p1.add(App.Vector(v).multiply(j + 1)))
                    pts = npts
                shape = Part.makePolygon(pts + [pts[0]])
                if "ChamferSize" in obj.PropertiesList:
                    if obj.ChamferSize.Value != 0:
                        w = DraftGeomUtils.filletWire(shape, obj.ChamferSize.Value, chamfer=True)
                        if w:
                            shape = w
                if "FilletRadius" in obj.PropertiesList:
                    if obj.FilletRadius.Value != 0:
                        w = DraftGeomUtils.filletWire(shape, obj.FilletRadius.Value)
                        if w:
                            shape = w
                try:
                    if getattr(obj, "MakeFace", True):
                        shape = Part.Face(shape)
                except Part.OCCError:
                    pass
            else:
                edges = []
                pts = obj.Points[1:]
                lp = obj.Points[0]
                for p in pts:
                    if not DraftVecUtils.equals(lp, p):
                        if getattr(obj, "Subdivisions", 0) > 0:
                            npts = []
                            v = p.sub(lp)
                            v = DraftVecUtils.scaleTo(v, v.Length / (obj.Subdivisions + 1))
                            edges.append(Part.LineSegment(lp, lp.add(v)).toShape())
                            lv = lp.add(v)
                            for j in range(obj.Subdivisions):
                                edges.append(Part.LineSegment(lv, lv.add(v)).toShape())
                                lv = lv.add(v)
                        else:
                            edges.append(Part.LineSegment(lp, p).toShape())
                        lp = p
                try:
                    shape = Part.Wire(edges)
                except Part.OCCError:
                    print("Error wiring edges")
                    shape = None
                if "ChamferSize" in obj.PropertiesList:
                    if obj.ChamferSize.Value != 0:
                        w = DraftGeomUtils.filletWire(shape, obj.ChamferSize.Value, chamfer=True)
                        if w:
                            shape = w
                if "FilletRadius" in obj.PropertiesList:
                    if obj.FilletRadius.Value != 0:
                        w = DraftGeomUtils.filletWire(shape, obj.FilletRadius.Value)
                        if w:
                            shape = w
            if shape:
                obj.Shape = shape
                if hasattr(obj, "Area") and hasattr(shape, "Area"):
                    obj.Area = shape.Area
                if hasattr(obj, "Length"):
                    obj.Length = shape.Length

        obj.Placement = plm
        obj.positionBySupport()
        self.update_start_end(obj)
        self.props_changed_clear()

    def onChanged(self, obj, prop):
        self.props_changed_store(prop)
        tol = 1e-7

        if prop == "Start":
            pts = obj.Points
            invpl = App.Placement(obj.Placement).inverse()
            realfpstart = invpl.multVec(obj.Start)
            if pts:
                if not pts[0].isEqual(realfpstart, tol):
                    pts[0] = realfpstart
                    obj.Points = pts

        elif prop == "End":
            pts = obj.Points
            invpl = App.Placement(obj.Placement).inverse()
            realfpend = invpl.multVec(obj.End)
            if len(pts) > 1:
                if not pts[-1].isEqual(realfpend, tol):
                    pts[-1] = realfpend
                    obj.Points = pts

        elif prop == "Length":
            if len(obj.Points) == 2 and obj.Length.Value > tol:
                vec = obj.Points[-1].sub(obj.Points[0])
                if abs(obj.Length.Value - vec.Length) > tol:
                    vec = DraftVecUtils.scaleTo(vec, obj.Length.Value)
                    obj.Points = [obj.Points[0], obj.Points[0].add(vec)]

    def update_start_end(self, obj):
        tol = 1e-7

        pl = App.Placement(obj.Placement)
        if len(obj.Points) > 1:
            displayfpstart = pl.multVec(obj.Points[0])
            displayfpend = pl.multVec(obj.Points[-1])
            if not obj.Start.isEqual(displayfpstart, tol):
                obj.Start = displayfpstart
            if not obj.End.isEqual(displayfpend, tol):
                obj.End = displayfpend


# Alias for compatibility with v0.18 and earlier
_Wire = Wire

## @}