File size: 22,440 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
/******************************************************************************

 *   Copyright (c) 2012 Jan Rheinländer <jrheinlaender@users.sourceforge.net> *

 *                                                                            *

 *   This file is part of the FreeCAD CAx development system.                 *

 *                                                                            *

 *   This library is free software; you can redistribute it and/or            *

 *   modify it under the terms of the GNU Library General Public              *

 *   License as published by the Free Software Foundation; either             *

 *   version 2 of the License, or (at your option) any later version.         *

 *                                                                            *

 *   This library  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 library; see the file COPYING.LIB. If not,       *

 *   write to the Free Software Foundation, Inc., 59 Temple Place,            *

 *   Suite 330, Boston, MA  02111-1307, USA                                   *

 *                                                                            *

 ******************************************************************************/

#include <limits>

#include <BRepAdaptor_Curve.hxx>
#include <BRepAdaptor_Surface.hxx>
#include <gp_Dir.hxx>
#include <gp_Pln.hxx>
#include <Precision.hxx>
#include <TopoDS.hxx>
#include <TopoDS_Face.hxx>

#include <App/Datums.h>
#include <Base/Axis.h>
#include <Mod/Part/App/Tools.h>
#include <Mod/Part/App/TopoShape.h>
#include <Mod/Part/App/Part2DObject.h>

#include "FeatureLinearPattern.h"
#include "DatumLine.h"
#include "DatumPlane.h"


using namespace PartDesign;

namespace PartDesign
{


PROPERTY_SOURCE(PartDesign::LinearPattern, PartDesign::Transformed)

const App::PropertyIntegerConstraint::Constraints LinearPattern::intOccurrences
    = {1, std::numeric_limits<int>::max(), 1};

const char* LinearPattern::ModeEnums[] = {"Extent", "Spacing", nullptr};

LinearPattern::LinearPattern()
{
    auto initialMode = LinearPatternMode::Extent;

    ADD_PROPERTY_TYPE(
        Direction,
        (nullptr),
        "Direction 1",
        App::Prop_None,
        "The first direction of the pattern. This can be a straight edge, a datum "
        "line, a sketch axis, or the normal of a planar face."
    );
    ADD_PROPERTY_TYPE(
        Reversed,
        (0),
        "Direction 1",
        App::Prop_None,
        "Reverse the first direction of the pattern"
    );
    ADD_PROPERTY_TYPE(
        Mode,
        (long(initialMode)),
        "Direction 1",
        App::Prop_None,
        "Selects how the pattern is dimensioned.\n'Extent': Uses the total length from the first "
        "to the last instance.\n'Spacing': Uses the distance between consecutive instances."
    );
    ADD_PROPERTY_TYPE(
        Length,
        (100.0),
        "Direction 1",
        App::Prop_None,
        "The total length of the pattern, measured from the first to the last "
        "instance. This is only used when the Mode is set to 'Extent'."
    );
    ADD_PROPERTY_TYPE(
        Offset,
        (10.0),
        "Direction 1",
        App::Prop_None,
        "The distance between each instance of the pattern. This is only used when "
        "the Mode is set to 'Spacing'."
    );
    ADD_PROPERTY_TYPE(
        Spacings,
        ({-1.0}),
        "Direction 1",
        App::Prop_None,
        "A list of custom spacings between instances. If a value is -1, the global "
        "'Offset' is used for that spacing. The list should have one less item than "
        "the number of occurrences."
    );
    ADD_PROPERTY_TYPE(
        SpacingPattern,
        ({}),
        "Direction 1",
        App::Prop_None,
        "(Experimental and subject to change. To enable "
        "this in the UI you can add a boolean parameter 'ExperiementalFeature' in "
        "Preferences/Mod/Part)\nDefines a repeating pattern of spacings for the second direction. "
        "For example, a list of [10, 20] will create alternating spacings of 10mm and 20mm."
    );
    ADD_PROPERTY_TYPE(
        Occurrences,
        (2),
        "Direction 1",
        App::Prop_None,
        "The total number of instances in the first direction, including the original feature."
    );
    Occurrences.setConstraints(&intOccurrences);
    Mode.setEnums(ModeEnums);
    setReadWriteStatusForMode(LinearPatternDirection::First);

    ADD_PROPERTY_TYPE(
        Direction2,
        (nullptr),
        "Direction 2",
        App::Prop_None,
        "The second direction of the pattern. This can be a straight edge, a datum "
        "line, a sketch axis, or the normal of a planar face."
    );
    ADD_PROPERTY_TYPE(
        Reversed2,
        (0),
        "Direction 2",
        App::Prop_None,
        "Reverse the second direction of the pattern"
    );
    ADD_PROPERTY_TYPE(
        Mode2,
        (long(initialMode)),
        "Direction 2",
        App::Prop_None,
        "Selects how the pattern is dimensioned in the second direction.\n'Extent': "
        "Uses the total length.\n'Spacing': Uses the distance between instances."
    );
    ADD_PROPERTY_TYPE(
        Length2,
        (100.0),
        "Direction 2",
        App::Prop_None,
        "The total length of the pattern in the second direction, measured from the first to the "
        "last instance. This is only used when the Mode is set to 'Extent'."
    );
    ADD_PROPERTY_TYPE(
        Offset2,
        (10.0),
        "Direction 2",
        App::Prop_None,
        "The distance between each instance of the pattern in the second direction. "
        "This is only used when the Mode is set to 'Spacing'."
    );
    ADD_PROPERTY_TYPE(
        Spacings2,
        ({}),
        "Direction 2",
        App::Prop_None,
        "A list of custom spacings for the second direction. If a value is -1, the global 'Offset' "
        "is used. The list should have one less item than the number of occurrences."
    );
    ADD_PROPERTY_TYPE(
        SpacingPattern2,
        ({}),
        "Direction 2",
        App::Prop_None,
        "(Experimental and subject to change. To enable "
        "this in the UI you can add a boolean parameter 'ExperiementalFeature' in "
        "Preferences/Mod/Part)\nDefines a repeating pattern of spacings for the second direction. "
        "For example, a list of [10, 20] will create alternating spacings of 10mm and 20mm."
    );
    ADD_PROPERTY_TYPE(
        Occurrences2,
        (1),
        "Direction 2",
        App::Prop_None,
        "The total number of instances in the second direction, including the original feature."
    );
    Occurrences2.setConstraints(&intOccurrences);
    Mode2.setEnums(ModeEnums);
    setReadWriteStatusForMode(LinearPatternDirection::Second);
}

short LinearPattern::mustExecute() const

{
    if (Direction.isTouched() || Reversed.isTouched() || Mode.isTouched() ||
        // Length and Offset are mutually exclusive, only one could be updated at once
        Length.isTouched() || Offset.isTouched() || Spacings.isTouched()
        || SpacingPattern.isTouched() || Occurrences.isTouched() || Direction2.isTouched()
        || Reversed2.isTouched() || Mode2.isTouched() || Length2.isTouched() || Offset2.isTouched()
        || Spacings2.isTouched() || SpacingPattern2.isTouched() || Occurrences2.isTouched()) {
        return 1;
    }
    return Transformed::mustExecute();
}

void LinearPattern::setReadWriteStatusForMode(LinearPatternDirection dir)

{
    bool isExtentMode = false;
    if (dir == LinearPatternDirection::First) {
        isExtentMode = (Mode.getValue() == static_cast<long>(LinearPatternMode::Extent));
        Length.setReadOnly(!isExtentMode);
        Offset.setReadOnly(isExtentMode);
    }
    else {
        isExtentMode = (Mode2.getValue() == static_cast<long>(LinearPatternMode::Extent));
        Length2.setReadOnly(!isExtentMode);
        Offset2.setReadOnly(isExtentMode);
    }
}

const std::list<gp_Trsf> LinearPattern::getTransformations(const std::vector<App::DocumentObject*>)

{
    int occurrences = Occurrences.getValue();
    int occurrences2 = Occurrences2.getValue();
    if (occurrences < 1 || occurrences2 < 1) {
        throw Base::ValueError("At least one occurrence required");
    }

    if (occurrences == 1 && occurrences2 == 1) {
        return {gp_Trsf()};
    }

    // make sure spacings are correct size :
    updateSpacings();

    // Calculate the base offset vector and final step positions for each direction
    gp_Vec offset1 = calculateOffsetVector(LinearPatternDirection::First);
    std::vector<gp_Vec> steps1 = calculateSteps(LinearPatternDirection::First, offset1);

    gp_Vec offset2 = calculateOffsetVector(LinearPatternDirection::Second);
    std::vector<gp_Vec> steps2 = calculateSteps(LinearPatternDirection::Second, offset2);

    // Combine the steps from both directions
    std::list<gp_Trsf> transformations;
    for (const auto& step1 : steps1) {
        for (const auto& step2 : steps2) {
            gp_Trsf trans;
            trans.SetTranslation(step1 + step2);
            transformations.push_back(trans);
        }
    }

    return transformations;
}

gp_Vec LinearPattern::calculateOffsetVector(LinearPatternDirection dir) const

{
    bool firstDir = dir == LinearPatternDirection::First;
    const auto& occurrencesProp = firstDir ? Occurrences : Occurrences2;
    int occurrences = occurrencesProp.getValue();
    if (occurrences <= 1) {
        return gp_Vec();  // Return zero vector if no transformation is needed
    }

    const auto& dirProp = firstDir ? Direction : Direction2;
    if (!dirProp.getValue()) {
        return gp_Vec();
    }
    const auto& reversedProp = firstDir ? Reversed : Reversed2;
    const auto& modeProp = firstDir ? Mode : Mode2;
    const auto& lengthProp = firstDir ? Length : Length2;

    double distance = lengthProp.getValue();
    if (distance < Precision::Confusion()) {
        throw Base::ValueError("Pattern length too small");
    }

    gp_Vec offset = getDirectionFromProperty(dirProp);
    if (reversedProp.getValue()) {
        offset.Reverse();
    }

    // For 'Extent' mode, the vector represents one full step.
    // For 'Spacing' mode, it's just a normalized direction vector.
    if (static_cast<LinearPatternMode>(modeProp.getValue()) == LinearPatternMode::Extent) {
        offset *= distance / (occurrences - 1);
    }

    return offset;
}

std::vector<gp_Vec> LinearPattern::calculateSteps(

    LinearPatternDirection dir,

    const gp_Vec& offsetVector

) const

{
    const auto& occurrencesProp = (dir == LinearPatternDirection::First) ? Occurrences : Occurrences2;
    const auto& modeProp = (dir == LinearPatternDirection::First) ? Mode : Mode2;
    const auto& offsetValueProp = (dir == LinearPatternDirection::First) ? Offset : Offset2;
    const auto& spacingsProp = (dir == LinearPatternDirection::First) ? Spacings : Spacings2;
    const auto& spacingPatternProp = (dir == LinearPatternDirection::First) ? SpacingPattern
                                                                            : SpacingPattern2;

    int occurrences = occurrencesProp.getValue();
    std::vector<gp_Vec> steps {gp_Vec()};  // First step is always zero
    steps.reserve(occurrences);

    if (occurrences <= 1) {
        return steps;
    }

    if (modeProp.getValue() == static_cast<int>(LinearPatternMode::Spacing)) {
        const std::vector<double> spacings = spacingsProp.getValues();
        const std::vector<double> pattern = spacingPatternProp.getValues();
        bool usePattern = pattern.size() > 1;
        double cumulativeDistance = 0.0;

        // Spacing priority: individual spacing > pattern > global offset
        const auto spacingAt = [&](unsigned i) {
            if (spacings.at(i - 1) != -1.0) {
                return spacings.at(i - 1);
            }

            if (usePattern) {
                return pattern.at(static_cast<size_t>(fmod(i - 1, pattern.size())));
            }

            return offsetValueProp.getValue();
        };

        for (int i = 1; i < occurrences; ++i) {
            cumulativeDistance += spacingAt(i);
            steps.push_back(offsetVector * cumulativeDistance);
        }
    }
    else {  // Extent Mode
        for (int i = 1; i < occurrences; ++i) {
            steps.push_back(offsetVector * i);
        }
    }

    return steps;
}

gp_Dir LinearPattern::getDirectionFromProperty(const App::PropertyLinkSub& dirProp) const

{
    App::DocumentObject* refObject = dirProp.getValue();
    if (!refObject) {
        throw Base::ValueError("No direction reference specified");
    }

    std::vector<std::string> subStrings = dirProp.getSubValues();
    if (subStrings.empty()) {
        throw Base::ValueError("No direction reference specified");
    }

    gp_Dir dir;
    if (auto* refSketch = freecad_cast<Part::Part2DObject*>(refObject)) {
        Base::Axis axis;
        if (subStrings[0] == "H_Axis") {
            axis = refSketch->getAxis(Part::Part2DObject::H_Axis);
            axis *= refSketch->Placement.getValue();
        }
        else if (subStrings[0] == "V_Axis") {
            axis = refSketch->getAxis(Part::Part2DObject::V_Axis);
            axis *= refSketch->Placement.getValue();
        }
        else if (subStrings[0] == "N_Axis") {
            axis = refSketch->getAxis(Part::Part2DObject::N_Axis);
            axis *= refSketch->Placement.getValue();
        }
        else if (subStrings[0].compare(0, 4, "Axis") == 0) {
            int AxId = std::atoi(subStrings[0].substr(4, 4000).c_str());
            if (AxId >= 0 && AxId < refSketch->getAxisCount()) {
                axis = refSketch->getAxis(AxId);
                axis *= refSketch->Placement.getValue();
            }
        }
        else if (subStrings[0].compare(0, 4, "Edge") == 0) {
            Part::TopoShape refShape = refSketch->Shape.getShape();
            TopoDS_Shape ref = refShape.getSubShape(subStrings[0].c_str());
            TopoDS_Edge refEdge = TopoDS::Edge(ref);
            if (refEdge.IsNull()) {
                throw Base::ValueError("Failed to extract direction edge");
            }
            BRepAdaptor_Curve adapt(refEdge);
            if (adapt.GetType() != GeomAbs_Line) {
                throw Base::TypeError("Direction edge must be a straight line");
            }

            gp_Pnt p = adapt.Line().Location();
            gp_Dir d = adapt.Line().Direction();

            // the axis is not given in local coordinates and mustn't be multiplied with the
            // placement
            axis.setBase(Base::Vector3d(p.X(), p.Y(), p.Z()));
            axis.setDirection(Base::Vector3d(d.X(), d.Y(), d.Z()));
        }
        dir = gp_Dir(axis.getDirection().x, axis.getDirection().y, axis.getDirection().z);
    }
    else if (auto* plane = freecad_cast<PartDesign::Plane*>(refObject)) {
        Base::Vector3d d = plane->getNormal();
        dir = gp_Dir(d.x, d.y, d.z);
    }
    else if (auto* line = freecad_cast<PartDesign::Line*>(refObject)) {
        Base::Vector3d d = line->getDirection();
        dir = gp_Dir(d.x, d.y, d.z);
    }
    else if (auto* plane = freecad_cast<App::Plane*>(refObject)) {
        Base::Vector3d d = plane->getDirection();
        dir = gp_Dir(d.x, d.y, d.z);
    }
    else if (auto* line = freecad_cast<App::Line*>(refObject)) {
        Base::Vector3d d = line->getDirection();
        dir = gp_Dir(d.x, d.y, d.z);
    }
    else if (auto* refFeature = freecad_cast<Part::Feature*>(refObject)) {
        if (subStrings[0].empty()) {
            throw Base::ValueError("No direction reference specified");
        }
        Part::TopoShape refShape = refFeature->Shape.getShape();
        TopoDS_Shape ref = refShape.getSubShape(subStrings[0].c_str());

        if (ref.ShapeType() == TopAbs_FACE) {
            TopoDS_Face refFace = TopoDS::Face(ref);
            if (refFace.IsNull()) {
                throw Base::ValueError("Failed to extract direction plane");
            }
            BRepAdaptor_Surface adapt(refFace);
            if (adapt.GetType() != GeomAbs_Plane) {
                throw Base::TypeError("Direction face must be planar");
            }

            dir = adapt.Plane().Axis().Direction();
        }
        else if (ref.ShapeType() == TopAbs_EDGE) {
            TopoDS_Edge refEdge = TopoDS::Edge(ref);
            if (refEdge.IsNull()) {
                throw Base::ValueError("Failed to extract direction edge");
            }
            BRepAdaptor_Curve adapt(refEdge);
            if (adapt.GetType() != GeomAbs_Line) {
                throw Base::ValueError("Direction edge must be a straight line");
            }

            dir = adapt.Line().Direction();
        }
        else {
            throw Base::ValueError("Direction reference must be edge or face");
        }
    }
    else {
        throw Base::ValueError(
            "Direction reference must be edge/face of a feature or a datum line/plane"
        );
    }

    TopLoc_Location invObjLoc = this->getLocation().Inverted();
    dir.Transform(invObjLoc.Transformation());

    return Base::convertTo<gp_Vec>(dir);
}

void LinearPattern::updateSpacings()

{
    updateSpacings(LinearPatternDirection::First);
    updateSpacings(LinearPatternDirection::Second);
}

void LinearPattern::updateSpacings(LinearPatternDirection dir)

{
    bool isSecondDir = dir == LinearPatternDirection::Second;

    App::PropertyFloatList& spacingsProp = isSecondDir ? Spacings2 : Spacings;
    App::PropertyLength& offsetProp = isSecondDir ? Offset2 : Offset;
    const App::PropertyIntegerConstraint& occurrencesProp = isSecondDir ? Occurrences2 : Occurrences;

    std::vector<double> spacings = spacingsProp.getValues();
    size_t targetCount = occurrencesProp.getValue()
        - 1;  // 1 less spacing than there are occurrences.

    for (auto& spacing : spacings) {
        if (spacing == offsetProp.getValue()) {
            spacing = -1.0;
        }
    }

    if (spacings.size() == targetCount) {
        return;
    }
    if (spacings.size() < targetCount) {
        spacings.reserve(targetCount);
        while (spacings.size() < targetCount) {
            spacings.push_back(-1.0);
        }
    }
    else {
        spacings.resize(targetCount);
    }

    spacingsProp.setValues(spacings);
}

void LinearPattern::handleChangedPropertyType(

    Base::XMLReader& reader,

    const char* TypeName,

    App::Property* prop

)

// transforms properties that had been changed

{
    // property Occurrences had the App::PropertyInteger and was changed to
    // App::PropertyIntegerConstraint
    if (prop == &Occurrences && strcmp(TypeName, "App::PropertyInteger") == 0) {
        App::PropertyInteger OccurrencesProperty;
        // restore the PropertyInteger to be able to set its value
        OccurrencesProperty.Restore(reader);
        Occurrences.setValue(OccurrencesProperty.getValue());
    }
    else {
        Transformed::handleChangedPropertyType(reader, TypeName, prop);
    }
}

void LinearPattern::onChanged(const App::Property* prop)

{
    auto mode = static_cast<LinearPatternMode>(Mode.getValue());
    auto mode2 = static_cast<LinearPatternMode>(Mode2.getValue());

    if (prop == &Mode) {
        setReadWriteStatusForMode(LinearPatternDirection::First);
    }
    else if (prop == &Occurrences) {
        updateSpacings(LinearPatternDirection::First);
        syncLengthAndOffset(LinearPatternDirection::First);
    }
    else if (prop == &Offset && mode == LinearPatternMode::Spacing) {
        syncLengthAndOffset(LinearPatternDirection::First);
    }
    else if (prop == &Length && mode == LinearPatternMode::Extent) {
        syncLengthAndOffset(LinearPatternDirection::First);
    }

    else if (prop == &Mode2) {
        setReadWriteStatusForMode(LinearPatternDirection::Second);
    }
    else if (prop == &Occurrences2) {
        updateSpacings(LinearPatternDirection::Second);
        syncLengthAndOffset(LinearPatternDirection::Second);
    }
    else if (prop == &Offset2 && mode2 == LinearPatternMode::Spacing) {
        syncLengthAndOffset(LinearPatternDirection::Second);
    }
    else if (prop == &Length2 && mode2 == LinearPatternMode::Extent) {
        syncLengthAndOffset(LinearPatternDirection::Second);
    }

    Transformed::onChanged(prop);
}

void LinearPattern::syncLengthAndOffset(LinearPatternDirection dir)

{
    // Get references to the correct properties based on the direction
    auto& modeProp = (dir == LinearPatternDirection::First) ? Mode : Mode2;
    auto& lengthProp = (dir == LinearPatternDirection::First) ? Length : Length2;
    auto& offsetProp = (dir == LinearPatternDirection::First) ? Offset : Offset2;
    auto& occurrencesProp = (dir == LinearPatternDirection::First) ? Occurrences : Occurrences2;

    auto mode = static_cast<LinearPatternMode>(modeProp.getValue());
    int occurrences = occurrencesProp.getValue();
    occurrences = occurrences <= 1 ? 1 : occurrences - 1;

    if (mode == LinearPatternMode::Spacing
        && !lengthProp.testStatus(App::Property::Status::Immutable)) {
        lengthProp.setValue(offsetProp.getValue() * occurrences);
    }
    else if (mode == LinearPatternMode::Extent
             && !offsetProp.testStatus(App::Property::Status::Immutable)) {
        offsetProp.setValue(lengthProp.getValue() / occurrences);
    }
}

}  // namespace PartDesign