File size: 24,392 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
# SPDX-License-Identifier: LGPL-2.1-or-later

# /***************************************************************************
# *   Copyright (c) 2016 Victor Titov (DeepSOIC) <vv.titov@gmail.com>       *
# *                                                                         *
# *   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                                *
# *                                                                         *
# ***************************************************************************/

__title__ = "BOPTools.SplitFeatures module"
__author__ = "DeepSOIC"
__url__ = "https://www.freecad.org"
__doc__ = "Shape splitting document objects (features)."

from . import SplitAPI
import FreeCAD
import Part

if FreeCAD.GuiUp:
    import FreeCADGui
    from PySide import QtCore, QtGui

    # -------------------------- translation-related code -------------------------
    # See forum thread "A new Part tool is being born... JoinFeatures!"
    # https://forum.freecad.org/viewtopic.php?f=22&t=11112&start=30#p90239
    try:
        _fromUtf8 = QtCore.QString.fromUtf8
    except Exception:

        def _fromUtf8(s):
            return s

    translate = FreeCAD.Qt.translate
# --------------------------/translation-related code --------------------------


def getIconPath(icon_dot_svg):
    return icon_dot_svg


# -------------------------- /common stuff ------------------------------------

# -------------------------- BooleanFragments ---------------------------------


def makeBooleanFragments(name):
    """makeBooleanFragments(name): makes an BooleanFragments object."""
    obj = FreeCAD.ActiveDocument.addObject("Part::FeaturePython", name)
    FeatureBooleanFragments(obj)
    if FreeCAD.GuiUp:
        ViewProviderBooleanFragments(obj.ViewObject)
    return obj


class FeatureBooleanFragments:
    """The BooleanFragments feature object."""

    def __init__(self, obj):
        obj.addProperty(
            "App::PropertyLinkList",
            "Objects",
            "BooleanFragments",
            "Object to compute intersections between.",
            locked=True,
        )
        obj.addProperty(
            "App::PropertyEnumeration",
            "Mode",
            "BooleanFragments",
            "- Standard: wires, shells, compsolids remain in one piece.\n"
            "- Split: wires, shells, compsolids are split.\n"
            "- CompSolid: make compsolid from solid fragments.",
            locked=True,
        )
        obj.Mode = ["Standard", "Split", "CompSolid"]
        obj.addProperty(
            "App::PropertyLength",
            "Tolerance",
            "BooleanFragments",
            "Tolerance when intersecting (fuzzy value). "
            "In addition to tolerances of the shapes.",
            locked=True,
        )

        obj.Proxy = self
        self.Type = "FeatureBooleanFragments"

    def execute(self, selfobj):
        shapes = [obj.Shape for obj in selfobj.Objects]
        if len(shapes) == 1 and shapes[0].ShapeType == "Compound":
            shapes = shapes[0].childShapes()
        if len(shapes) < 2:
            raise ValueError(
                "At least two shapes are needed for computing boolean fragments. Got only {num}.".format(
                    num=len(shapes)
                )
            )
        selfobj.Shape = SplitAPI.booleanFragments(shapes, selfobj.Mode, selfobj.Tolerance)


class ViewProviderBooleanFragments:
    """A View Provider for the Part BooleanFragments feature."""

    def __init__(self, vobj):
        vobj.Proxy = self

    def getIcon(self):
        return ":/icons/booleans/Part_BooleanFragments.svg"

    def attach(self, vobj):
        self.ViewObject = vobj
        self.Object = vobj.Object

    def dumps(self):
        return None

    def loads(self, state):
        return None

    def claimChildren(self):
        return self.Object.Objects

    def onDelete(self, feature, subelements):
        try:
            for obj in self.claimChildren():
                obj.ViewObject.show()
        except Exception as err:
            FreeCAD.Console.PrintError("Error in onDelete: " + str(err))
        return True

    def canDragObjects(self):
        return True

    def canDropObjects(self):
        return True

    def canDragObject(self, dragged_object):
        return True

    def canDropObject(self, incoming_object):
        return hasattr(incoming_object, "Shape")

    def dragObject(self, selfvp, dragged_object):
        objs = self.Object.Objects
        objs.remove(dragged_object)
        self.Object.Objects = objs

    def dropObject(self, selfvp, incoming_object):
        self.Object.Objects = self.Object.Objects + [incoming_object]


def cmdCreateBooleanFragmentsFeature(name, mode):
    """cmdCreateBooleanFragmentsFeature(name, mode): implementation of GUI command to create
    BooleanFragments feature (GFA). Mode can be "Standard", "Split", or "CompSolid"."""
    sel = FreeCADGui.Selection.getSelectionEx()
    FreeCAD.ActiveDocument.openTransaction("Create Boolean Fragments")
    FreeCADGui.addModule("BOPTools.SplitFeatures")
    FreeCADGui.doCommand(
        "j = BOPTools.SplitFeatures.makeBooleanFragments(name='{name}')".format(name=name)
    )
    FreeCADGui.doCommand(
        "j.Objects = {sel}".format(
            sel="[" + ", ".join(["App.ActiveDocument." + so.Object.Name for so in sel]) + "]"
        )
    )
    FreeCADGui.doCommand("j.Mode = {mode}".format(mode=repr(mode)))

    try:
        FreeCADGui.doCommand("j.Proxy.execute(j)")
        FreeCADGui.doCommand("j.purgeTouched()")
    except Exception as err:
        mb = QtGui.QMessageBox()
        mb.setIcon(mb.Icon.Warning)
        error_text1 = translate("Part_SplitFeatures", "Computing the result failed with an error:")
        error_text2 = translate(
            "Part_SplitFeatures",
            "Click 'Continue' to create the feature anyway, or 'Abort' to cancel.",
        )
        mb.setText(error_text1 + "\n\n" + str(err) + "\n\n" + error_text2)
        mb.setWindowTitle(translate("Part_SplitFeatures", "Bad Selection", None))
        btnAbort = mb.addButton(QtGui.QMessageBox.StandardButton.Abort)
        btnOK = mb.addButton(
            translate("Part_SplitFeatures", "Continue", None),
            QtGui.QMessageBox.ButtonRole.ActionRole,
        )
        mb.setDefaultButton(btnOK)

        mb.exec_()

        if mb.clickedButton() is btnAbort:
            FreeCAD.ActiveDocument.abortTransaction()
            return

    FreeCADGui.doCommand(
        "for obj in j.ViewObject.Proxy.claimChildren():\n" "    obj.ViewObject.hide()"
    )

    FreeCAD.ActiveDocument.commitTransaction()


class CommandBooleanFragments:
    """Command to create BooleanFragments feature."""

    def GetResources(self):
        return {
            "Pixmap": getIconPath("Part_BooleanFragments.svg"),
            "MenuText": QtCore.QT_TRANSLATE_NOOP("Part_BooleanFragments", "Boolean Fragments"),
            "Accel": "",
            "ToolTip": QtCore.QT_TRANSLATE_NOOP(
                "Part_BooleanFragments",
                "Creates a boolean union which is sliced at the intersections of the selected shapes",
            ),
        }

    def Activated(self):
        if len(FreeCADGui.Selection.getSelectionEx()) >= 1:
            cmdCreateBooleanFragmentsFeature(name="BooleanFragments", mode="Standard")
        else:
            mb = QtGui.QMessageBox()
            mb.setIcon(mb.Icon.Warning)
            mb.setText(
                translate(
                    "Part_SplitFeatures",
                    "Select at least two objects, or one or more compounds. "
                    "If only one compound is selected, the compounded shapes will be intersected between each other "
                    "(otherwise, compounds with self-intersections are invalid).",
                    None,
                )
            )
            mb.setWindowTitle(translate("Part_SplitFeatures", "Bad Selection", None))
            mb.exec_()

    def IsActive(self):
        if FreeCAD.ActiveDocument:
            return True
        else:
            return False


# -------------------------- /BooleanFragments --------------------------------

# -------------------------- Slice --------------------------------------------


def makeSlice(name):
    """makeSlice(name): makes an Slice object."""
    obj = FreeCAD.ActiveDocument.addObject("Part::FeaturePython", name)
    FeatureSlice(obj)
    if FreeCAD.GuiUp:
        ViewProviderSlice(obj.ViewObject)
    return obj


class FeatureSlice:
    """The Slice feature object."""

    def __init__(self, obj):
        obj.addProperty("App::PropertyLink", "Base", "Slice", "Object to be sliced.", locked=True)
        obj.addProperty(
            "App::PropertyLinkList", "Tools", "Slice", "Objects that slice.", locked=True
        )
        obj.addProperty(
            "App::PropertyEnumeration",
            "Mode",
            "Slice",
            "- Standard: wires, shells, compsolids remain in one piece.\n"
            "- Split: wires, shells, compsolids are split.\n"
            "- CompSolid: make compsolid from solid fragments.",
            locked=True,
        )
        obj.Mode = ["Standard", "Split", "CompSolid"]
        obj.addProperty(
            "App::PropertyLength",
            "Tolerance",
            "Slice",
            "Tolerance when intersecting (fuzzy value). "
            "In addition to tolerances of the shapes.",
            locked=True,
        )

        obj.Proxy = self
        self.Type = "FeatureSlice"

    def execute(self, selfobj):
        if len(selfobj.Tools) < 1:
            raise ValueError("No slicing objects supplied!")

        # helper function to get the shape from object or group
        def get_shape(obj):
            """get shape from a part object or compound from a group."""
            if hasattr(obj, "Shape"):
                return obj.Shape
            elif hasattr(obj, "Group"):  # it's a group/container from ie. slice apart
                shapes = []
                for child in obj.Group:
                    if hasattr(child, "Shape"):
                        shapes.append(child.Shape)
                if shapes:
                    return Part.makeCompound(shapes)
            return None

        # get base shape
        base_shape = get_shape(selfobj.Base)
        if base_shape is None or base_shape.isNull():
            raise ValueError("Base object has no valid shape!")

        # get tool shapes
        tool_shapes = []
        for tool in selfobj.Tools:
            shape = get_shape(tool)
            if shape is not None and not shape.isNull():
                tool_shapes.append(shape)

        if len(tool_shapes) < 1:
            raise ValueError("No valid tool shapes available")

        selfobj.Shape = SplitAPI.slice(
            base_shape,
            tool_shapes,
            selfobj.Mode,
            selfobj.Tolerance,
        )


class ViewProviderSlice:
    """A View Provider for the Part Slice feature."""

    def __init__(self, vobj):
        vobj.Proxy = self

    def getIcon(self):
        return ":/icons/booleans/Part_Slice.svg"

    def attach(self, vobj):
        self.ViewObject = vobj
        self.Object = vobj.Object

    def dumps(self):
        return None

    def loads(self, state):
        return None

    def claimChildren(self):
        return [self.Object.Base] + self.Object.Tools

    def onDelete(self, feature, subelements):
        try:
            for obj in self.claimChildren():
                obj.ViewObject.show()
        except Exception as err:
            FreeCAD.Console.PrintError("Error in onDelete: " + str(err))
        return True


def cmdCreateSliceFeature(name, mode, transaction=True):
    """cmdCreateSliceFeature(name, mode): implementation of GUI command to create
    Slice feature. Mode can be "Standard", "Split", or "CompSolid"."""
    sel = FreeCADGui.Selection.getSelectionEx()
    if transaction:
        FreeCAD.ActiveDocument.openTransaction("Create Slice")
    FreeCADGui.addModule("BOPTools.SplitFeatures")
    FreeCADGui.doCommand("f = BOPTools.SplitFeatures.makeSlice(name='{name}')".format(name=name))
    FreeCADGui.doCommand(
        "f.Base = {sel}[0]\n"
        "f.Tools = {sel}[1:]".format(
            sel="[" + ", ".join(["App.ActiveDocument." + so.Object.Name for so in sel]) + "]"
        )
    )
    FreeCADGui.doCommand("f.Mode = {mode}".format(mode=repr(mode)))

    try:
        FreeCADGui.doCommand("f.Proxy.execute(f)")
        FreeCADGui.doCommand("f.purgeTouched()")
    except Exception as err:
        mb = QtGui.QMessageBox()
        mb.setIcon(mb.Icon.Warning)
        error_text1 = translate("Part_SplitFeatures", "Computing the result failed with an error:")
        error_text2 = translate(
            "Part_SplitFeatures",
            "Click 'Continue' to create the feature anyway, or 'Abort' to cancel.",
        )
        mb.setText(error_text1 + "\n\n" + str(err) + "\n\n" + error_text2)
        mb.setWindowTitle(translate("Part_SplitFeatures", "Bad Selection", None))
        btnAbort = mb.addButton(QtGui.QMessageBox.StandardButton.Abort)
        btnOK = mb.addButton(
            translate("Part_SplitFeatures", "Continue", None),
            QtGui.QMessageBox.ButtonRole.ActionRole,
        )
        mb.setDefaultButton(btnOK)

        mb.exec_()

        if mb.clickedButton() is btnAbort:
            if transaction:
                FreeCAD.ActiveDocument.abortTransaction()
            return False

    FreeCADGui.doCommand(
        "for obj in f.ViewObject.Proxy.claimChildren():\n" "    obj.ViewObject.hide()"
    )

    if transaction:
        FreeCAD.ActiveDocument.commitTransaction()
    return True


def cmdSliceApart():
    FreeCAD.ActiveDocument.openTransaction("Slice apart")
    made = cmdCreateSliceFeature(name="Slice", mode="Split", transaction=False)

    if made:
        FreeCADGui.addModule("CompoundTools.Explode")
        FreeCADGui.doCommand("CompoundTools.Explode.explodeCompound(f)")
        FreeCADGui.doCommand("f.ViewObject.hide()")
        FreeCAD.ActiveDocument.commitTransaction()
        FreeCADGui.doCommand("App.ActiveDocument.recompute()")
    else:
        FreeCAD.ActiveDocument.abortTransaction()


class CommandSlice:
    """Command to create Slice feature."""

    def GetResources(self):
        return {
            "Pixmap": getIconPath("Part_Slice.svg"),
            "MenuText": QtCore.QT_TRANSLATE_NOOP("Part_Slice", "Slice to Compound"),
            "Accel": "",
            "ToolTip": QtCore.QT_TRANSLATE_NOOP(
                "Part_Slice",
                "Slices the selected object by using other objects as cutting tools and storing the results in one compound",
            ),
        }

    def Activated(self):
        if len(FreeCADGui.Selection.getSelectionEx()) > 1:
            cmdCreateSliceFeature(name="Slice", mode="Split")
        else:
            mb = QtGui.QMessageBox()
            mb.setIcon(mb.Icon.Warning)
            mb.setText(
                translate(
                    "Part_SplitFeatures",
                    "Select at least two objects. "
                    "The first one is the object to be sliced; "
                    "the rest are objects to slice with.",
                    None,
                )
            )
            mb.setWindowTitle(translate("Part_SplitFeatures", "Bad Selection", None))
            mb.exec_()

    def IsActive(self):
        if FreeCAD.ActiveDocument:
            return True
        else:
            return False


class CommandSliceApart:
    """Command to create exploded Slice feature."""

    def GetResources(self):
        return {
            "Pixmap": getIconPath("Part_SliceApart.svg"),
            "MenuText": QtCore.QT_TRANSLATE_NOOP("Part_SliceApart", "Slice Apart"),
            "Accel": "",
            "ToolTip": QtCore.QT_TRANSLATE_NOOP(
                "Part_SliceApart",
                "Slices the selected object by other objects, and splits it apart, creating a compound filter for each slide",
            ),
        }

    def Activated(self):
        if len(FreeCADGui.Selection.getSelectionEx()) > 1:
            cmdSliceApart()
        else:
            mb = QtGui.QMessageBox()
            mb.setIcon(mb.Icon.Warning)
            mb.setText(
                translate(
                    "Part_SplitFeatures",
                    "Select at least two objects. "
                    "The first one is the object to be sliced; "
                    "the rest are objects to slice with.",
                    None,
                )
            )
            mb.setWindowTitle(translate("Part_SplitFeatures", "Bad Selection", None))
            mb.exec_()

    def IsActive(self):
        if FreeCAD.ActiveDocument:
            return True
        else:
            return False


# -------------------------- /Slice -------------------------------------------

# -------------------------- XOR ----------------------------------------------


def makeXOR(name):
    """makeXOR(name): makes an XOR object."""
    obj = FreeCAD.ActiveDocument.addObject("Part::FeaturePython", name)
    FeatureXOR(obj)
    if FreeCAD.GuiUp:
        ViewProviderXOR(obj.ViewObject)
    return obj


class FeatureXOR:
    """The XOR feature object."""

    def __init__(self, obj):
        obj.addProperty(
            "App::PropertyLinkList",
            "Objects",
            "XOR",
            "Object to compute intersections between.",
            locked=True,
        )
        obj.addProperty(
            "App::PropertyLength",
            "Tolerance",
            "XOR",
            "Tolerance when intersecting (fuzzy value). "
            "In addition to tolerances of the shapes.",
            locked=True,
        )

        obj.Proxy = self
        self.Type = "FeatureXOR"

    def execute(self, selfobj):
        shapes = [obj.Shape for obj in selfobj.Objects]
        if len(shapes) == 1 and shapes[0].ShapeType == "Compound":
            shapes = shapes[0].childShapes()
        if len(shapes) < 2:
            raise ValueError(
                "At least two shapes are needed for computing XOR. Got only {num}.".format(
                    num=len(shapes)
                )
            )
        selfobj.Shape = SplitAPI.xor(shapes, selfobj.Tolerance)


class ViewProviderXOR:
    """A View Provider for the Part XOR feature."""

    def __init__(self, vobj):
        vobj.Proxy = self

    def getIcon(self):
        return ":/icons/booleans/Part_XOR.svg"

    def attach(self, vobj):
        self.ViewObject = vobj
        self.Object = vobj.Object

    def dumps(self):
        return None

    def loads(self, state):
        return None

    def claimChildren(self):
        return self.Object.Objects

    def onDelete(self, feature, subelements):
        try:
            for obj in self.claimChildren():
                obj.ViewObject.show()
        except Exception as err:
            FreeCAD.Console.PrintError("Error in onDelete: " + str(err))
        return True

    def canDragObjects(self):
        return True

    def canDropObjects(self):
        return True

    def canDragObject(self, dragged_object):
        return True

    def canDropObject(self, incoming_object):
        return hasattr(incoming_object, "Shape")

    def dragObject(self, selfvp, dragged_object):
        objs = self.Object.Objects
        objs.remove(dragged_object)
        self.Object.Objects = objs

    def dropObject(self, selfvp, incoming_object):
        self.Object.Objects = self.Object.Objects + [incoming_object]


def cmdCreateXORFeature(name):
    """cmdCreateXORFeature(name): implementation of GUI command to create
    XOR feature (GFA). Mode can be "Standard", "Split", or "CompSolid"."""
    sel = FreeCADGui.Selection.getSelectionEx()
    FreeCAD.ActiveDocument.openTransaction("Create Boolean XOR")
    FreeCADGui.addModule("BOPTools.SplitFeatures")
    FreeCADGui.doCommand("j = BOPTools.SplitFeatures.makeXOR(name='{name}')".format(name=name))
    FreeCADGui.doCommand(
        "j.Objects = {sel}".format(
            sel="[" + ", ".join(["App.ActiveDocument." + so.Object.Name for so in sel]) + "]"
        )
    )

    try:
        FreeCADGui.doCommand("j.Proxy.execute(j)")
        FreeCADGui.doCommand("j.purgeTouched()")
    except Exception as err:
        mb = QtGui.QMessageBox()
        mb.setIcon(mb.Icon.Warning)
        error_text1 = translate("Part_SplitFeatures", "Computing the result failed with an error:")
        error_text2 = translate(
            "Part_SplitFeatures",
            "Click 'Continue' to create the feature anyway, or 'Abort' to cancel.",
        )
        mb.setText(error_text1 + "\n\n" + str(err) + "\n\n" + error_text2)
        mb.setWindowTitle(translate("Part_SplitFeatures", "Bad Selection", None))
        btnAbort = mb.addButton(QtGui.QMessageBox.StandardButton.Abort)
        btnOK = mb.addButton(
            translate("Part_SplitFeatures", "Continue", None),
            QtGui.QMessageBox.ButtonRole.ActionRole,
        )
        mb.setDefaultButton(btnOK)

        mb.exec_()

        if mb.clickedButton() is btnAbort:
            FreeCAD.ActiveDocument.abortTransaction()
            return

    FreeCADGui.doCommand(
        "for obj in j.ViewObject.Proxy.claimChildren():\n" "    obj.ViewObject.hide()"
    )

    FreeCAD.ActiveDocument.commitTransaction()


class CommandXOR:
    """Command to create XOR feature."""

    def GetResources(self):
        return {
            "Pixmap": getIconPath("Part_XOR.svg"),
            "MenuText": QtCore.QT_TRANSLATE_NOOP("Part_XOR", "Boolean XOR"),
            "Accel": "",
            "ToolTip": QtCore.QT_TRANSLATE_NOOP(
                "Part_XOR",
                "Performs an 'exclusive OR' boolean operation with two or more selected objects,\n"
                "or with the shapes inside a compound.\n"
                "Overlapping volumes of the shapes will be removed.",
            ),
        }

    def Activated(self):
        if len(FreeCADGui.Selection.getSelectionEx()) >= 1:
            cmdCreateXORFeature(name="XOR")
        else:
            mb = QtGui.QMessageBox()
            mb.setIcon(mb.Icon.Warning)
            mb.setText(
                translate(
                    "Part_SplitFeatures",
                    "Select at least two objects, or one or more compounds. "
                    "If only one compound is selected, the compounded shapes will be intersected between each other "
                    "(otherwise, compounds with self-intersections are invalid).",
                    None,
                )
            )
            mb.setWindowTitle(translate("Part_SplitFeatures", "Bad Selection", None))
            mb.exec_()

    def IsActive(self):
        if FreeCAD.ActiveDocument:
            return True
        else:
            return False


# -------------------------- /XOR ---------------------------------------------


def addCommands():
    FreeCADGui.addCommand("Part_BooleanFragments", CommandBooleanFragments())
    FreeCADGui.addCommand("Part_Slice", CommandSlice())
    FreeCADGui.addCommand("Part_SliceApart", CommandSliceApart())
    FreeCADGui.addCommand("Part_XOR", CommandXOR())