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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
# 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) 2019, M G Berberich (berberic2, rynn)                   *
# *   Copyright (c) 2020 Eliud Cabrera Castillo <e.cabrera-castillo@tum.de> *
# *                                                                         *
# *   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 Array object.

The `Array` class currently handles three types of arrays,
orthogonal, polar, and circular. In the future, probably they should be
split in separate classes so that they are easier to manage.
"""
## @package array
# \ingroup draftobjects
# \brief Provides the object code for the Array object.

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

import FreeCAD as App
import DraftVecUtils

from draftutils.messages import _log

from draftobjects.draftlink import DraftLink


class Array(DraftLink):
    """The Draft Array object.

    To Do
    -----
    The `Array` class currently handles three types of arrays,
    orthogonal, polar, and circular. In the future, probably they should be
    split in separate classes so that they are easier to manage.
    """

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

    def attach(self, obj):
        """Set up the properties when the object is attached."""
        self.set_properties(obj)
        super(Array, self).attach(obj)

    def onDocumentRestored(self, obj):
        super(Array, self).onDocumentRestored(obj)
        # Count property was added in v0.21 and PlacementList property was added
        # for non-link arrays in v1.1, obj should be OK if both are present:
        if hasattr(obj, "Count") and hasattr(obj, "PlacementList"):
            return

        if not hasattr(obj, "Count"):
            _log("v0.21, " + obj.Name + ", added property 'Count'")
        if not hasattr(obj, "PlacementList"):
            _log("v1.1, " + obj.Name + ", added hidden property 'PlacementList'")

        self.set_general_properties(obj)
        self.execute(obj)  # Required to update Count and/or PlacementList.

    def set_properties(self, obj):
        """Set properties only if they don't exist."""
        self.set_ortho_properties(obj)
        self.set_polar_circular_properties(obj)
        self.set_polar_properties(obj)
        self.set_circular_properties(obj)

        # The ArrayType property (general) must be attached
        # after the other array properties so that onChanged works properly
        self.set_general_properties(obj)
        self.set_link_properties(obj)

    def set_general_properties(self, obj):
        """Set general properties only if they don't exist."""
        properties = obj.PropertiesList

        if "Base" not in properties:
            _tip = QT_TRANSLATE_NOOP("App::Property", "The base object that will be duplicated")
            obj.addProperty("App::PropertyLink", "Base", "Objects", _tip, locked=True)
            obj.Base = None

        if "ArrayType" not in properties:
            _tip = QT_TRANSLATE_NOOP(
                "App::Property",
                "The type of array to create.\n"
                "- Ortho: places the copies "
                "in the direction of the global X, "
                "Y, Z axes.\n"
                "- Polar: places the copies along "
                "a circular arc, up to a specified "
                "angle, and with certain orientation "
                "defined by a center and an axis.\n"
                "- Circular: places the copies "
                "in concentric circles "
                "around the base object.",
            )
            obj.addProperty("App::PropertyEnumeration", "ArrayType", "Objects", _tip, locked=True)
            obj.ArrayType = ["ortho", "polar", "circular"]

        if "Fuse" not in properties:
            _tip = QT_TRANSLATE_NOOP(
                "App::Property",
                "Specifies if the copies "
                "should be fused together "
                "if they touch each other (slower)",
            )
            obj.addProperty("App::PropertyBool", "Fuse", "Objects", _tip, locked=True)
            obj.Fuse = False

        if "Count" not in properties:
            _tip = QT_TRANSLATE_NOOP(
                "App::Property",
                "Total number of elements "
                "in the array.\n"
                "This property is read-only, "
                "as the number depends "
                "on the parameters of the array.",
            )
            obj.addProperty("App::PropertyInteger", "Count", "Objects", _tip, locked=True)
            obj.Count = 0
            obj.setEditorMode("Count", 1)  # Read only

        if not self.use_link:
            if "PlacementList" not in properties:
                _tip = QT_TRANSLATE_NOOP("App::Property", "The placement for each array element")
                obj.addProperty(
                    "App::PropertyPlacementList", "PlacementList", "Objects", _tip, locked=True
                )
                obj.PlacementList = []

    def set_ortho_properties(self, obj):
        """Set orthogonal properties only if they don't exist."""
        properties = obj.PropertiesList

        if "NumberX" not in properties:
            _tip = QT_TRANSLATE_NOOP("App::Property", "Number of copies in X-direction")
            obj.addProperty(
                "App::PropertyInteger", "NumberX", "Orthogonal array", _tip, locked=True
            )
            obj.NumberX = 2

        if "NumberY" not in properties:
            _tip = QT_TRANSLATE_NOOP("App::Property", "Number of copies in Y-direction")
            obj.addProperty(
                "App::PropertyInteger", "NumberY", "Orthogonal array", _tip, locked=True
            )
            obj.NumberY = 2

        if "NumberZ" not in properties:
            _tip = QT_TRANSLATE_NOOP("App::Property", "Number of copies in Z-direction")
            obj.addProperty(
                "App::PropertyInteger", "NumberZ", "Orthogonal array", _tip, locked=True
            )
            obj.NumberZ = 1

        if "IntervalX" not in properties:
            _tip = QT_TRANSLATE_NOOP(
                "App::Property", "Distance and orientation " "of intervals in X-direction"
            )
            obj.addProperty(
                "App::PropertyVectorDistance", "IntervalX", "Orthogonal array", _tip, locked=True
            )
            obj.IntervalX = App.Vector(50, 0, 0)

        if "IntervalY" not in properties:
            _tip = QT_TRANSLATE_NOOP(
                "App::Property", "Distance and orientation " "of intervals in Y-direction"
            )
            obj.addProperty(
                "App::PropertyVectorDistance", "IntervalY", "Orthogonal array", _tip, locked=True
            )
            obj.IntervalY = App.Vector(0, 50, 0)

        if "IntervalZ" not in properties:
            _tip = QT_TRANSLATE_NOOP(
                "App::Property", "Distance and orientation " "of intervals in Z-direction"
            )
            obj.addProperty(
                "App::PropertyVectorDistance", "IntervalZ", "Orthogonal array", _tip, locked=True
            )
            obj.IntervalZ = App.Vector(0, 0, 50)

    def set_polar_circular_properties(self, obj):
        """Set general polar and circular properties if they don't exist."""
        properties = obj.PropertiesList

        if "Axis" not in properties:
            _tip = QT_TRANSLATE_NOOP(
                "App::Property",
                "The axis direction around which "
                "the elements in a polar or "
                "a circular array will be created",
            )
            obj.addProperty(
                "App::PropertyVector", "Axis", "Polar/circular array", _tip, locked=True
            )
            obj.Axis = App.Vector(0, 0, 1)

        if "Center" not in properties:
            _tip = QT_TRANSLATE_NOOP(
                "App::Property",
                "Center point for polar and "
                "circular arrays.\n"
                "The 'Axis' passes through this point.",
            )
            obj.addProperty(
                "App::PropertyVectorDistance", "Center", "Polar/circular array", _tip, locked=True
            )
            obj.Center = App.Vector(0, 0, 0)

        # The AxisReference property must be attached after Axis and Center
        # so that onChanged works properly
        if "AxisReference" not in properties:
            _tip = QT_TRANSLATE_NOOP(
                "App::Property",
                "The axis object that overrides "
                "the value of 'Axis' and 'Center', "
                "for example, a datum line.\n"
                "Its placement, position and rotation, "
                "will be used when creating polar "
                "and circular arrays.\n"
                "Leave this property empty "
                "to be able to set 'Axis' and 'Center' "
                "manually.",
            )
            obj.addProperty(
                "App::PropertyLinkGlobal", "AxisReference", "Objects", _tip, locked=True
            )
            obj.AxisReference = None

    def set_polar_properties(self, obj):
        """Set polar properties only if they don't exist."""
        properties = obj.PropertiesList

        if "NumberPolar" not in properties:
            _tip = QT_TRANSLATE_NOOP("App::Property", "Number of copies in the polar direction")
            obj.addProperty("App::PropertyInteger", "NumberPolar", "Polar array", _tip, locked=True)
            obj.NumberPolar = 5

        if "IntervalAxis" not in properties:
            _tip = QT_TRANSLATE_NOOP(
                "App::Property", "Distance and orientation " "of intervals in 'Axis' direction"
            )
            obj.addProperty(
                "App::PropertyVectorDistance", "IntervalAxis", "Polar array", _tip, locked=True
            )
            obj.IntervalAxis = App.Vector(0, 0, 0)

        if "Angle" not in properties:
            _tip = QT_TRANSLATE_NOOP("App::Property", "Angle to cover with copies")
            obj.addProperty("App::PropertyAngle", "Angle", "Polar array", _tip, locked=True)
            obj.Angle = 360

    def set_circular_properties(self, obj):
        """Set circular properties only if they don't exist."""
        properties = obj.PropertiesList

        if "RadialDistance" not in properties:
            _tip = QT_TRANSLATE_NOOP("App::Property", "Distance between concentric circles")
            obj.addProperty(
                "App::PropertyDistance", "RadialDistance", "Circular array", _tip, locked=True
            )
            obj.RadialDistance = 50

        if "TangentialDistance" not in properties:
            _tip = QT_TRANSLATE_NOOP(
                "App::Property", "Distance between copies " "in the same circle"
            )
            obj.addProperty(
                "App::PropertyDistance", "TangentialDistance", "Circular array", _tip, locked=True
            )
            obj.TangentialDistance = 25

        if "NumberCircles" not in properties:
            _tip = QT_TRANSLATE_NOOP(
                "App::Property",
                "Number of concentric circle. " "The 'Base' object counts as one circle.",
            )
            obj.addProperty(
                "App::PropertyInteger", "NumberCircles", "Circular array", _tip, locked=True
            )
            obj.NumberCircles = 3

        if "Symmetry" not in properties:
            _tip = QT_TRANSLATE_NOOP(
                "App::Property",
                "A parameter that determines "
                "how many symmetry planes "
                "the circular array will have",
            )
            obj.addProperty("App::PropertyInteger", "Symmetry", "Circular array", _tip, locked=True)
            obj.Symmetry = 1

    def set_link_properties(self, obj):
        """Set link properties only if they don't exist."""
        properties = obj.PropertiesList

        if self.use_link:
            if "ExpandArray" not in properties:
                _tip = QT_TRANSLATE_NOOP(
                    "App::Property", "Show the individual array elements " "(only for Link arrays)"
                )
                obj.addProperty("App::PropertyBool", "ExpandArray", "Objects", _tip, locked=True)
                obj.ExpandArray = False

    def linkSetup(self, obj):
        """Set up the object as a link object."""
        super(Array, self).linkSetup(obj)
        obj.configLinkProperty(ElementCount="Count")
        obj.setPropertyStatus("Count", "Hidden")

    def onChanged(self, obj, prop):
        """Execute when a property is changed."""
        super(Array, self).onChanged(obj, prop)
        # print(prop, ": ", getattr(obj, prop))
        self.show_and_hide(obj, prop)

    def show_and_hide(self, obj, prop):
        """Show and hide the properties depending on the touched property."""
        if prop == "AxisReference":
            if obj.AxisReference:
                obj.setEditorMode("Center", 1)
                obj.setEditorMode("Axis", 1)
            else:
                obj.setEditorMode("Center", 0)
                obj.setEditorMode("Axis", 0)

        if prop == "ArrayType":
            if obj.ArrayType == "ortho":
                for pr in ("NumberX", "NumberY", "NumberZ", "IntervalX", "IntervalY", "IntervalZ"):
                    obj.setPropertyStatus(pr, "-Hidden")

                for pr in (
                    "Axis",
                    "Center",
                    "NumberPolar",
                    "Angle",
                    "IntervalAxis",
                    "NumberCircles",
                    "RadialDistance",
                    "TangentialDistance",
                    "Symmetry",
                ):
                    obj.setPropertyStatus(pr, "Hidden")

            if obj.ArrayType == "polar":
                for pr in ("Axis", "Center", "NumberPolar", "Angle", "IntervalAxis"):
                    obj.setPropertyStatus(pr, "-Hidden")

                for pr in (
                    "NumberX",
                    "NumberY",
                    "NumberZ",
                    "IntervalX",
                    "IntervalY",
                    "IntervalZ",
                    "NumberCircles",
                    "RadialDistance",
                    "TangentialDistance",
                    "Symmetry",
                ):
                    obj.setPropertyStatus(pr, "Hidden")

            if obj.ArrayType == "circular":
                for pr in (
                    "Axis",
                    "Center",
                    "NumberCircles",
                    "RadialDistance",
                    "TangentialDistance",
                    "Symmetry",
                ):
                    obj.setPropertyStatus(pr, "-Hidden")

                for pr in (
                    "NumberX",
                    "NumberY",
                    "NumberZ",
                    "IntervalX",
                    "IntervalY",
                    "IntervalZ",
                    "NumberPolar",
                    "Angle",
                    "IntervalAxis",
                ):
                    obj.setPropertyStatus(pr, "Hidden")

    def execute(self, obj):
        """Execute when the object is created or recomputed."""
        if self.props_changed_placement_only(obj) or not obj.Base:
            self.props_changed_clear()
            return

        pl = obj.Placement
        axis = obj.Axis
        center = obj.Center

        if hasattr(obj, "AxisReference") and obj.AxisReference:
            if hasattr(obj.AxisReference, "Placement"):
                reference = obj.AxisReference.Placement
                axis = reference.Rotation * App.Vector(0, 0, 1)
                center = reference.Base
            else:
                _info = (
                    "'AxisReference' has no 'Placement' property. "
                    "Please select a different object to use as "
                    "reference."
                )
                raise TypeError(_info)

        if obj.ArrayType == "ortho":
            pls = rect_placements(
                obj.Base.Placement,
                obj.IntervalX,
                obj.IntervalY,
                obj.IntervalZ,
                obj.NumberX,
                obj.NumberY,
                obj.NumberZ,
            )
        elif obj.ArrayType == "polar":
            av = obj.IntervalAxis if hasattr(obj, "IntervalAxis") else None
            pls = polar_placements(
                obj.Base.Placement, center, obj.Angle.Value, obj.NumberPolar, axis, av
            )
        elif obj.ArrayType == "circular":
            pls = circ_placements(
                obj.Base.Placement,
                obj.RadialDistance,
                obj.TangentialDistance,
                axis,
                center,
                obj.NumberCircles,
                obj.Symmetry,
            )

        self.buildShape(obj, pl, pls)
        self.props_changed_clear()
        return not self.use_link


# Alias for compatibility with v0.18 and earlier
_Array = Array


def rect_placements(base_placement, xvector, yvector, zvector, xnum, ynum, znum):
    """Determine the placements where the rectangular copies will be."""
    pl = base_placement
    placements = [pl.copy()]

    for xcount in range(xnum):
        currentxvector = App.Vector(xvector).multiply(xcount)
        if xcount != 0:
            npl = pl.copy()
            npl.translate(currentxvector)
            placements.append(npl)

        for ycount in range(ynum):
            currentyvector = App.Vector(currentxvector)
            _y_shift = App.Vector(yvector).multiply(ycount)
            currentyvector = currentyvector.add(_y_shift)
            if ycount != 0:
                npl = pl.copy()
                npl.translate(currentyvector)
                placements.append(npl)

            for zcount in range(znum):
                currentzvector = App.Vector(currentyvector)
                _z_shift = App.Vector(zvector).multiply(zcount)
                currentzvector = currentzvector.add(_z_shift)
                if zcount != 0:
                    npl = pl.copy()
                    npl.translate(currentzvector)
                    placements.append(npl)

    return placements


def polar_placements(base_placement, center, angle, number, axis, axisvector):
    """Determine the placements where the polar copies will be."""
    # print("angle ",angle," num ",num)
    placements = [base_placement.copy()]

    if number <= 1:
        return placements

    if angle == 360:
        fraction = float(angle) / number
    else:
        fraction = float(angle) / (number - 1)

    for i in range(number - 1):
        currangle = fraction + (i * fraction)
        npl = base_placement.copy()
        npl.rotate(center, axis, currangle, comp=True)
        if axisvector:
            if not DraftVecUtils.isNull(axisvector):
                npl.translate(App.Vector(axisvector).multiply(i + 1))
        placements.append(npl)

    return placements


def circ_placements(
    base_placement, r_distance, tan_distance, axis, center, circle_number, symmetry
):
    """Determine the placements where the circular copies will be."""
    symmetry = max(1, symmetry)
    lead = (0, 1, 0)

    if axis.x == 0 and axis.z == 0:
        lead = (1, 0, 0)

    direction = axis.cross(App.Vector(lead)).normalize()
    placements = [base_placement.copy()]

    for xcount in range(1, circle_number):
        rc = xcount * r_distance
        trans = App.Vector(direction).multiply(rc)
        c = 2 * rc * math.pi
        n = math.floor(c / tan_distance)
        n = int(math.floor(n / symmetry) * symmetry)
        if n == 0:
            continue

        angle = 360.0 / n
        for ycount in range(0, n):
            npl = base_placement.copy()
            npl.translate(trans)
            npl.rotate(center, axis, ycount * angle, comp=True)
            placements.append(npl)

    return placements


## @}