File size: 35,045 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
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
# 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) 2013 WandererFan <wandererfan@gmail.com>                *
# *   Copyright (c) 2019 Zheng, Lei (realthunder)<realthunder.dev@gmail.com>*
# *   Copyright (c) 2020 Carlo Pavan <carlopav@gmail.com>                   *
# *   Copyright (c) 2020 Eliud Cabrera Castillo <e.cabrera-castillo@tum.de> *
# *                                                                         *
# *   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.                                 *
# *                                                                         *
# *   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 PathArray object.

The copies will be placed along a path like a polyline, spline, or bezier
curve, and along the selected subelements.

To Do
-----
The `'PathSubelements'` property must be changed in type, as it does not need
to be an `App::PropertyLinkSubList`.
A `LinkSubList` is to select multiple subelements (edges) from multiple
objects. However, since we need to select a `'Path Object'` already,
which is a single object, the subelements that we can choose must belong
to this `'Path Object'` only.

Therefore, the correct property that must be used is `App::PropertyLinkSub`.
Then in the property editor we will be unable to select more than one object
thus preventing errors of the subelements not matching the `'PathObject'`.

In fact, both `'PathObject'` and `'PathSubelements'`
could be handled with a single `App::PropertyLinkSub` property,
as this property can be used to select a single object,
or a single object with its subelements.

In the future, we could migrate the properties, or outright break
compatibility with older objects by changing both properties
`'PathObject'` and `'PathSubelements'`.

An alternative to this would be to use a single `App::PropertyLinkSubList`.
This would allow us to build PathArrays on multiple objects and multiple
subelements (edges) of those objects at the same time. However, to do this,
the logic in `execute` would have to be changed to account for multiple
objects. Therefore, the first solution is simpler, that is, using
a single property of type `App::PropertyLinkSub`.
"""
## @package patharray
# \ingroup draftobjects
# \brief Provides the object code for the PathArray object.

import FreeCAD as App
import DraftVecUtils
import lazy_loader.lazy_loader as lz

from draftutils.messages import _err, _log, _wrn
from draftutils.translate import translate


def QT_TRANSLATE_NOOP(ctx, txt):
    return txt


from draftobjects.base import DraftObject
from draftobjects.draftlink import DraftLink

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

## \addtogroup draftobjects
# @{


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

    The object distributes copies of an object along a path like a polyline,
    spline, or bezier curve.

    Attributes
    ----------
    Align: bool
        It defaults to `False`.
        It sets whether the object will be specially aligned to the path.

    AlignMode: str
        It defaults to `'Original'`.
        Indicates the type of alignment that will be calculated when
        `Align` is `True`.

        `'Original'` mode is the historic `'Align'` for old (v0.18) documents.
        It is not really the Fernat alignment. It uses the normal parameter
        from `getNormal` (or the default) as a constant, it does not calculate
        curve normal.
        `X` is curve tangent, `Y` is normal parameter, `Z` is the cross product
        `X` x `Y`.

        `'Tangent'` mode is similar to `Original`, but includes a pre-rotation
        (in execute) to align the `Base` object's `X` to `TangentVector`,
        then `X` follows curve tangent, normal input parameter
        is the Z component.

        If `ForceVertical` is `True`, the normal parameter from `getNormal`
        is ignored, and `X` is curve tangent, `Z` is `VerticalVector`,
        and `Y` is the cross product `X` x `Z`.

        `'Frenet'` mode orients the copies to a coordinate system
        along the path.
        `X` is tangent to curve, `Y` is curve normal, `Z` is curve binormal.
        If normal cannot be computed, for example, in a straight line,
        the default is used.

    ForceVertical: bool
        It defaults to `False`.
        If it is `True`, and `AlignMode` is `'Original'` or `'Tangent'`,
        it will use the vector in `VerticalVector` as the `Z` axis.

    StartOffset: float
        It defaults to 0.0.
        It is the length from the start of the path to the first copy.

    EndOffset: float
        It defaults to 0.0.
        It is the length at the end of the path that will not be available
        for object placement.

    ReversePath: bool
        It defaults to False.
        This will walk the path in reverse, also reversing object
        orientation. Start and end offsets will count from opposite ends
        of the path, etc.

    SpacingMode: string
        It defaults to `'Fixed count'`.
        Objects can be spaced to divide the available length evenly
        (`'Fixed count'`, this is the original spacing mode from FreeCAD 1.0),
        or to be placed in given distances along the path from each other:
        `'Fixed spacing'` will keep placing objects for as long as there
        is still space available, while `'Fixed count and spacing'`
        will place a given number of objects (provided they fit in available
        space).

    SpacingUnit: length
        It defaults to 20mm.
        When fixed spacing modes are used, this is the spacing distance
        used. If UseSpacingPattern is also enabled, this is the unit length
        of "1.0" in the spacing pattern (so, default pattern of [1.0, 2.0]
        with default SpacingUnit of 20mm means a spacing pattern of
        20mm, 40mm).

    UseSpacingPattern: bool
        Default is False.
        Enables the SpacingPattern for uneven distribution of objects.
        Will have slightly different effect depending on SpacingMode.

    SpacingPattern: float list
        Default is [1.0, 2.0]
        When UseSpacingPattern is True, this list contains the proportions
        of distances between consecutive object pairs. Can be used in any
        spacing mode. In "fixed spacing" modes SpacingPattern is multiplied
        by SpacingUnit. In flexible spacing modes ("fixed count"), spacing
        pattern defines the proportion of distances.
    """

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

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

        Note: we don't exactly know why the properties are added
        in the `attach` method. They should probably be added in the `__init__`
        method. Maybe this is related to the link behavior of this class.

        For PathLinkArray, DraftLink.attach creates the link to the Base.

        Realthunder: before the big merge, there was only the attach() method
        in the view object proxy, not the object proxy.
        I added that to allow the proxy to override the C++ view provider
        type. The view provider type is normally determined by object's
        C++ API getViewProviderName(), and cannot be overridden by the proxy.
        I introduced the attach() method in proxy to allow the core
        to attach the proxy before creating the C++ view provider.
        """
        self.set_properties(obj)
        super().attach(obj)

    def set_properties(self, obj):
        """Set properties only if they don't exist."""
        if not obj:
            return

        if hasattr(obj, "PropertiesList"):
            properties = obj.PropertiesList
        else:
            properties = []

        self.set_general_properties(obj, properties)
        self.set_spacing_properties(obj, properties)
        self.set_align_properties(obj, properties)

    def set_general_properties(self, obj, properties):
        """Set general properties only if they don't exist."""
        if "Base" not in properties:
            _tip = QT_TRANSLATE_NOOP("App::Property", "The base object that will be duplicated")
            obj.addProperty("App::PropertyLinkGlobal", "Base", "Objects", _tip, locked=True)
            obj.Base = None

        if "PathObject" not in properties:
            _tip = QT_TRANSLATE_NOOP(
                "App::Property",
                "The object along which the copies will be distributed. It must contain 'Edges'.",
            )
            obj.addProperty("App::PropertyLinkGlobal", "PathObject", "Objects", _tip, locked=True)
            obj.PathObject = None

        # TODO: the 'PathSubelements' property must be changed,
        # as it does not need to be an 'App::PropertyLinkSubList'.
        #
        # In fact, both 'PathObject' and 'PathSubelements'
        # could be handled with a single 'App::PropertyLinkSub' property,
        # as this property can be used to select a single object,
        # or a single object with its subelements.
        if "PathSubelements" not in properties:
            _tip = QT_TRANSLATE_NOOP(
                "App::Property",
                "List of connected edges in the 'Path Object'.\nIf these are present, the copies will be created along these subelements only.\nLeave this property empty to create copies along the entire 'Path Object'.",
            )
            obj.addProperty(
                "App::PropertyLinkSubListGlobal", "PathSubelements", "Objects", _tip, locked=True
            )
            obj.PathSubelements = []

        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 self.use_link and "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
            obj.setPropertyStatus("Shape", "Transient")

        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_align_properties(self, obj, properties):
        """Set general properties only if they don't exist."""
        if "ExtraTranslation" not in properties:
            _tip = QT_TRANSLATE_NOOP(
                "App::Property",
                "Additional translation that will be applied to each copy.\nThis is useful to adjust for the difference between shape centre and shape reference point.",
            )
            obj.addProperty(
                "App::PropertyVectorDistance", "ExtraTranslation", "Alignment", _tip, locked=True
            )
            obj.ExtraTranslation = App.Vector(0, 0, 0)

        if "TangentVector" not in properties:
            _tip = QT_TRANSLATE_NOOP("App::Property", "Alignment vector for 'Tangent' mode")
            obj.addProperty("App::PropertyVector", "TangentVector", "Alignment", _tip, locked=True)
            obj.TangentVector = App.Vector(1, 0, 0)

        if "ForceVertical" not in properties:
            _tip = QT_TRANSLATE_NOOP(
                "App::Property",
                "Force use of 'Vertical Vector' as local Z-direction when using 'Original' or 'Tangent' alignment mode",
            )
            obj.addProperty("App::PropertyBool", "ForceVertical", "Alignment", _tip, locked=True)
            obj.ForceVertical = False

        if "VerticalVector" not in properties:
            _tip = QT_TRANSLATE_NOOP(
                "App::Property", "Direction of the local Z axis when 'Force Vertical' is true"
            )
            obj.addProperty("App::PropertyVector", "VerticalVector", "Alignment", _tip, locked=True)
            obj.VerticalVector = App.Vector(0, 0, 1)

        if "AlignMode" not in properties:
            _tip = QT_TRANSLATE_NOOP(
                "App::Property",
                "Method to orient the copies along the path.\n- Original: X is curve tangent, Y is normal, and Z is the cross product.\n- Frenet: aligns the object following the local coordinate system along the path.\n- Tangent: similar to 'Original' but the local X axis is pre-aligned to 'Tangent Vector'.\n\nTo get better results with 'Original' or 'Tangent' you may have to set 'Force Vertical' to true.",
            )
            obj.addProperty("App::PropertyEnumeration", "AlignMode", "Alignment", _tip, locked=True)
            obj.AlignMode = ["Original", "Frenet", "Tangent"]
            obj.AlignMode = "Original"

        if "ReversePath" not in properties:
            _tip = QT_TRANSLATE_NOOP("App::Property", "Walk the path backwards.")
            obj.addProperty("App::PropertyBool", "ReversePath", "Alignment", _tip, locked=True)
            obj.ReversePath = False

        # The Align property must be attached after other align properties
        # so that onChanged works properly
        if "Align" not in properties:
            _tip = QT_TRANSLATE_NOOP(
                "App::Property",
                "Orient the copies along the path depending on the 'Align Mode'.\nOtherwise the copies will have the same orientation as the original Base object.",
            )
            obj.addProperty("App::PropertyBool", "Align", "Alignment", _tip, locked=True)
            obj.Align = False

    def set_spacing_properties(self, obj, properties):

        if "Count" not in properties:
            _tip = QT_TRANSLATE_NOOP("App::Property", "Number of copies to create")
            obj.addProperty("App::PropertyInteger", "Count", "Spacing", _tip, locked=True)
            obj.Count = 4

        if "SpacingMode" not in properties:
            _tip = QT_TRANSLATE_NOOP(
                "App::Property",
                "How copies are spaced.\n"
                + " - Fixed count: available path length (minus start and end offsets) is evenly divided into n.\n"
                + ' - Fixed spacing: start at "Start offset" and place new copies after traveling a fixed distance along the path.\n'
                + ' - Fixed count and spacing: same as "Fixed spacing", but also stop at given number of copies.',
            )
            obj.addProperty("App::PropertyEnumeration", "SpacingMode", "Spacing", _tip, locked=True)
            obj.SpacingMode = ["Fixed count", "Fixed spacing", "Fixed count and spacing"]
            obj.SpacingMode = "Fixed count"

        if "SpacingUnit" not in properties:
            _tip = QT_TRANSLATE_NOOP("App::Property", "Base fixed distance between elements.")
            obj.addProperty("App::PropertyLength", "SpacingUnit", "Spacing", _tip, locked=True)
            obj.SpacingUnit = 20.0
            obj.setPropertyStatus("SpacingUnit", "Hidden")

        if "UseSpacingPattern" not in properties:
            _tip = QT_TRANSLATE_NOOP(
                "App::Property", "Use repeating spacing patterns instead of uniform spacing."
            )
            obj.addProperty("App::PropertyBool", "UseSpacingPattern", "Spacing", _tip, locked=True)
            obj.UseSpacingPattern = False

        if "SpacingPattern" not in properties:
            _tip = QT_TRANSLATE_NOOP(
                "App::Property", "Spacing is multiplied by a corresponding number in this sequence."
            )
            obj.addProperty(
                "App::PropertyFloatList", "SpacingPattern", "Spacing", _tip, locked=True
            )
            obj.SpacingPattern = [1, 2]
            obj.setPropertyStatus("SpacingPattern", "Hidden")

        if "StartOffset" not in properties:
            _tip = QT_TRANSLATE_NOOP(
                "App::Property", "Length from the start of the path to the first copy."
            )
            obj.addProperty("App::PropertyLength", "StartOffset", "Spacing", _tip, locked=True)
            obj.StartOffset = 0.0

        if "EndOffset" not in properties:
            _tip = QT_TRANSLATE_NOOP(
                "App::Property", "Length from the end of the path to the last copy."
            )
            obj.addProperty("App::PropertyLength", "EndOffset", "Spacing", _tip, locked=True)
            obj.EndOffset = 0.0

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

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

        # placement of entire PathArray object
        array_placement = obj.Placement

        w = self.get_wires(obj.PathObject, obj.PathSubelements)
        if not w:
            _err(obj.PathObject.Label + translate("draft", ", path object does not have 'Edges'."))
            return

        base_rotation = obj.Base.Shape.Placement.Rotation
        final_rotation = base_rotation

        if obj.Align and obj.AlignMode == "Tangent" and hasattr(obj, "TangentVector"):
            Xaxis = App.Vector(1.0, 0.0, 0.0)  # default TangentVector

            if not DraftVecUtils.equals(Xaxis, obj.TangentVector):
                # make rotation from TangentVector to X
                pre_rotation = App.Rotation(obj.TangentVector, Xaxis)
                final_rotation = base_rotation.multiply(pre_rotation)

        copy_placements = placements_on_path(
            final_rotation,
            w,
            obj.Count,
            obj.ExtraTranslation,
            obj.Align,
            obj.AlignMode,
            obj.ForceVertical,
            obj.VerticalVector,
            obj.StartOffset.Value,
            obj.EndOffset.Value,
            obj.ReversePath,
            obj.SpacingMode,
            obj.SpacingUnit.Value,
            obj.UseSpacingPattern,
            obj.SpacingPattern,
        )

        self.buildShape(obj, array_placement, copy_placements)
        self.props_changed_clear()
        return not self.use_link

    def get_wires(self, path_object, subelements):
        """Get wires from the path object."""
        if subelements:
            w = self.get_wire_from_subelements(subelements)
        elif hasattr(path_object.Shape, "Wires") and path_object.Shape.Wires:
            w = path_object.Shape.Wires[0]
        elif path_object.Shape.Edges:
            w = Part.Wire(path_object.Shape.Edges)
        else:
            w = None
        return w

    def get_wire_from_subelements(self, subelements):
        """Make a wire from the path object subelements."""
        sl = []
        for sub in subelements:
            edgeNames = sub[1]
            for n in edgeNames:
                e = sub[0].Shape.getElement(n)
                sl.append(e)
        return Part.Wire(Part.__sortEdges__(sl))

    def onChanged(self, obj, prop):
        """Execute when a property is changed."""
        super().onChanged(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.

        Note that when the array is created, some properties will change
        more than once in a seemingly random order.
        """
        # The minus sign removes the Hidden property (show).

        if prop == "SpacingMode":

            # Check if all referenced properties are available:
            for pr in ("SpacingMode", "SpacingUnit", "UseSpacingPattern", "SpacingPattern"):
                if not hasattr(obj, pr):
                    return

            if obj.SpacingMode == "Fixed spacing":
                obj.setPropertyStatus("Count", "Hidden")
                obj.setPropertyStatus("SpacingUnit", "-Hidden")

            elif obj.SpacingMode == "Fixed count":
                obj.setPropertyStatus("Count", "-Hidden")
                obj.setPropertyStatus("SpacingUnit", "Hidden")

            elif obj.SpacingMode == "Fixed count and spacing":
                obj.setPropertyStatus("Count", "-Hidden")
                obj.setPropertyStatus("SpacingUnit", "-Hidden")

        if prop == "UseSpacingPattern":

            # Check if referenced property is available:
            if not hasattr(obj, "SpacingPattern"):
                return

            if obj.UseSpacingPattern:
                obj.setPropertyStatus("SpacingPattern", "-Hidden")
            else:
                obj.setPropertyStatus("SpacingPattern", "Hidden")

        if prop in ("Align", "AlignMode"):

            # Check if all referenced properties are available:
            for pr in ("Align", "AlignMode", "ForceVertical", "VerticalVector", "TangentVector"):
                if not hasattr(obj, pr):
                    return

            if obj.Align:
                obj.setPropertyStatus("AlignMode", "-Hidden")

                if obj.AlignMode == "Frenet":
                    for pr in ("ForceVertical", "VerticalVector"):
                        obj.setPropertyStatus(pr, "Hidden")
                else:
                    for pr in ("ForceVertical", "VerticalVector"):
                        obj.setPropertyStatus(pr, "-Hidden")

                if obj.AlignMode == "Tangent":
                    obj.setPropertyStatus("TangentVector", "-Hidden")
                else:
                    obj.setPropertyStatus("TangentVector", "Hidden")

            else:
                for pr in ("AlignMode", "ForceVertical", "VerticalVector", "TangentVector"):
                    obj.setPropertyStatus(pr, "Hidden")

    def onDocumentRestored(self, obj):
        super().onDocumentRestored(obj)
        # ReversePath was added together with several Spacing properties in v1.1,
        # and PlacementList property was added for non-link arrays in v1.1,
        # obj should be OK if both are present:
        if hasattr(obj, "ReversePath") and hasattr(obj, "PlacementList"):
            return

        if hasattr(obj, "PathObj"):
            _log("v0.19, " + obj.Name + ", migrated 'PathObj' property to 'PathObject'")
        if hasattr(obj, "PathSubs"):
            _log("v0.19, " + obj.Name + ", migrated 'PathSubs' property to 'PathSubelements'")
        if hasattr(obj, "Xlate"):
            _log("v0.19, " + obj.Name + ", migrated 'Xlate' property to 'ExtraTranslation'")
        if not hasattr(obj, "Fuse"):
            _log("v1.0, " + obj.Name + ", added 'Fuse' property")
        if obj.getGroupOfProperty("Count") != "Spacing":
            _log("v1.1, " + obj.Name + ", moved 'Count' property to 'Spacing' subsection")
        if not hasattr(obj, "ReversePath"):
            _log(
                "v1.1, "
                + obj.Name
                + ", "
                + "added 'ReversePath', 'SpacingMode', 'SpacingUnit', 'UseSpacingPattern' "
                + "and 'SpacingPattern' properties"
            )
        if not hasattr(obj, "PlacementList"):
            _log("v1.1, " + obj.Name + ", added hidden property 'PlacementList'")

        self.set_properties(obj)
        obj.setGroupOfProperty("Count", "Spacing")
        if hasattr(obj, "PathObj"):
            obj.PathObject = obj.PathObj
            obj.removeProperty("PathObj")
        if hasattr(obj, "PathSubs"):
            obj.PathSubelements = obj.PathSubs
            obj.removeProperty("PathSubs")
        if hasattr(obj, "Xlate"):
            obj.ExtraTranslation = obj.Xlate
            obj.removeProperty("Xlate")
        self.execute(obj)  # Required to update PlacementList.


# Alias for compatibility with v0.18 and earlier
_PathArray = PathArray


def placements_on_path(
    shapeRotation,
    pathwire,
    count,
    xlate,
    align,
    mode="Original",
    forceNormal=False,
    normalOverride=None,
    startOffset=0.0,
    endOffset=0.0,
    reversePath=False,
    spacingMode="Fixed count",
    spacingUnit=20.0,
    useSpacingPattern=False,
    spacingPattern=[1, 1, 1, 1],
):
    """Calculate the placements of a shape along a given path.

    Copies will be distributed according to spacing mode - evenly or in fixed offsets.
    """

    if mode == "Frenet":
        forceNormal = False

    if forceNormal and normalOverride:
        normal = normalOverride
    else:
        normal = DraftGeomUtils.get_normal(pathwire)
        if normal is None:
            normal = App.Vector(0, 0, 1)

    path = Part.__sortEdges__(pathwire.Edges)

    # if ReversePath is on, walk the path backwards:
    if reversePath:
        path = path[::-1]

    # find cumulative edge end distance
    totalDist = 0
    ends = []
    for e in path:
        totalDist += e.Length
        ends.append(totalDist)

    # if align is True the length of the path cannot be zero:
    minLength = 1e-6 if align else -1e-12

    if startOffset > (totalDist - minLength):
        if startOffset != 0:
            _wrn(translate("draft", "Start Offset too large for path length. Using 0 instead."))
        startOffset = 0

    if endOffset > (totalDist - startOffset - minLength):
        if endOffset != 0:
            _wrn(
                translate(
                    "draft",
                    "End Offset too large for path length minus Start Offset. Using 0 instead.",
                )
            )
        endOffset = 0

    totalDist = totalDist - startOffset - endOffset

    useFlexibleSpacing = spacingMode == "Fixed count"
    useFixedSpacing = spacingMode in ("Fixed spacing", "Fixed count and spacing")

    stopAfterCount = spacingMode in ("Fixed count", "Fixed count and spacing")
    stopAfterDistance = spacingMode in ("Fixed spacing", "Fixed count and spacing")

    spacingUnit = max(spacingUnit, 0)
    # protect from infinite loop when step = 0
    if spacingUnit == 0:
        _wrn(translate("draft", "Spacing unit of 0 is not allowed, using default"))
        spacingUnit = totalDist

    # negative spacing steps are not defined
    spacingPattern = [abs(w) for w in spacingPattern]

    # protect from infinite loop when pattern weights are all zeros
    if sum(spacingPattern) == 0:
        spacingPattern = [spacingUnit]

    isClosedPath = DraftGeomUtils.isReallyClosed(pathwire) and not (startOffset or endOffset)

    count = max(count, 1)

    if useFlexibleSpacing:
        # Spaces between objects will stretch to fill available length

        segCount = count if isClosedPath else count - 1
        segCount = max(segCount, 1)

        if useSpacingPattern:
            # Available length will be non-uniformly divided in proportions from SpacingPattern:
            fullSpacingPattern = [spacingPattern[i % len(spacingPattern)] for i in range(segCount)]
            sumWeights = sum(fullSpacingPattern)
            distPerWeightUnit = totalDist / sumWeights
            steps = [distPerWeightUnit * weight for weight in fullSpacingPattern]

        else:
            # Available length will be evenly divided (the original spacing method):
            steps = [totalDist / segCount]

    if useFixedSpacing:
        # Objects will be placed in specified intervals

        if useSpacingPattern:
            # Intervals will be fixed, but follow a repeating pattern:
            steps = [spacingUnit * mult for mult in spacingPattern]
        else:
            # Each interval will be the same:
            steps = [spacingUnit]

    remains = 0
    travel = startOffset
    endTravel = startOffset + totalDist
    placements = []

    i = 0
    while True:
        # which edge in path should contain this shape?
        for j in range(len(ends)):
            if travel <= ends[j]:
                iend = j
                remains = ends[iend] - travel
                offset = path[iend].Length - remains if not reversePath else remains
                break
        else:
            # avoids problems with float math travel > ends[-1]
            iend = len(ends) - 1
            offset = path[iend].Length if not reversePath else 0

        # place shape at proper spot on proper edge
        pt = path[iend].valueAt(get_parameter_from_v0(path[iend], offset))
        place = calculate_placement(
            shapeRotation,
            path[iend],
            offset,
            pt,
            xlate,
            align,
            normal,
            mode,
            forceNormal,
            reversePath,
        )
        placements.append(place)
        travel += steps[i % len(steps)]
        i = i + 1

        # End conditions:
        if stopAfterDistance and travel > endTravel:
            break
        if stopAfterCount and i >= count:
            break

        # Failsafe:
        if i > 10_000:
            _wrn(translate("draft", "Operation would generate too many objects. Aborting"))
            return placements[0:1]

    return placements


calculatePlacementsOnPath = placements_on_path


def calculate_placement(
    globalRotation,
    edge,
    offset,
    RefPt,
    xlate,
    align,
    normal=App.Vector(0.0, 0.0, 1.0),
    mode="Original",
    overrideNormal=False,
    reversePath=False,
):
    """Orient shape in the local coordinate system at parameter offset.

    http://en.wikipedia.org/wiki/Euler_angles (previous version)
    http://en.wikipedia.org/wiki/Quaternions
    """
    # Default Placement:
    placement = App.Placement()
    placement.Rotation = globalRotation.inverted() if reversePath else globalRotation
    placement.Base = RefPt + placement.Rotation.multVec(xlate)

    if not align:
        return placement

    tol = 1e-6  # App.Rotation() tolerance is 1e-7. Shorter vectors are ignored.
    nullv = App.Vector()

    t = edge.tangentAt(get_parameter_from_v0(edge, offset))

    if t.isEqual(nullv, tol):
        _wrn(translate("draft", "Length of tangent vector is 0. Copy not aligned."))
        return placement

    if reversePath:
        t.multiply(-1)

    # If the length of the normal is zero or if it is parallel to the tangent,
    # we make the vectors equal (n = t). The App.Rotation() algorithm will
    # then replace the normal with a default axis.
    # For the vector with the lowest App.Rotation() priority we use a null
    # vector. Calculating this binormal would not make sense in the mentioned
    # cases. And in all other cases calculating it is not necessary as
    # App.Rotation() will ignore it.

    if mode in ("Original", "Tangent"):
        n = normal
        if n.isEqual(nullv, tol):
            _wrn(translate("draft", "Length of normal vector is 0. Using a default axis instead."))
            n = t
        else:
            n_nor = n.normalize()
            t_nor = t.normalize()
            if n_nor.isEqual(t_nor, tol) or n_nor.isEqual(t_nor.negative(), tol):
                _wrn(
                    translate(
                        "draft",
                        "Tangent and normal vectors are parallel. Normal replaced by a default axis.",
                    )
                )
                n = t

        if overrideNormal:
            onPathRotation = App.Rotation(t, nullv, n, "XZY")  # priority = "XZY"
        else:
            onPathRotation = App.Rotation(t, n, nullv, "XYZ")  # priority = "XYZ"

    elif mode == "Frenet":
        try:
            n = edge.normalAt(get_parameter_from_v0(edge, offset))
        except App.Base.FreeCADError:  # no/infinite normals here
            _wrn(
                translate(
                    "draft", "Cannot calculate normal vector. Using the default normal instead."
                )
            )
            n = normal

        if n.isEqual(nullv, tol):
            _wrn(translate("draft", "Length of normal vector is 0. Using a default axis instead."))
            n = t
        else:
            n_nor = n.normalize()
            t_nor = t.normalize()
            if n_nor.isEqual(t_nor, tol) or n_nor.isEqual(t_nor.negative(), tol):
                _wrn(
                    translate(
                        "draft",
                        "Tangent and normal vectors are parallel. Normal replaced by a default axis.",
                    )
                )
                n = t

        onPathRotation = App.Rotation(t, n, nullv, "XYZ")  # priority = "XYZ"

    else:
        _err(translate("draft", "AlignMode {} is not implemented").format(mode))
        return placement

    placement.Rotation = onPathRotation.multiply(globalRotation)
    placement.Base = RefPt + placement.Rotation.multVec(xlate)

    return placement


calculatePlacement = calculate_placement


def get_parameter_from_v0(edge, offset):
    """Return parameter at distance offset from edge.Vertexes[0].

    sb method in Part.TopoShapeEdge???
    """
    lpt = edge.valueAt(edge.getParameterByLength(0))
    vpt = edge.Vertexes[0].Point

    if not DraftVecUtils.equals(vpt, lpt):
        # this edge is flipped
        length = edge.Length - offset
    else:
        # this edge is right way around
        length = offset

    return edge.getParameterByLength(length)


getParameterFromV0 = get_parameter_from_v0

## @}