FreeCAD-macros

Форк
0
/
HighlightDifference.FCMacro 
113 строк · 3.9 Кб
1
# -*- coding: utf-8 -*-
2
"""
3
Compute the difference between two shapes. Additions are marked red, removals
4
are marked green. Both original parts will be half transparent. The volume of
5
the additions and removals are printed in the console.
6
"""
7

8
from __future__ import unicode_literals
9

10
__Name__ = 'Highlight Difference'
11
__Comment__ = 'Compute the difference between two shapes'
12
__Author__ = 'galou and other contributors'
13
__Version__ = '2.2.0'
14
__Date__ = '2021-07-01'
15
__License__ = 'CC0-1.0'
16
__Web__ = 'https://freecadweb.org/wiki/Macro_HighlightDifference'
17
__Wiki__ = 'https://freecadweb.org/wiki/Macro_HighlightDifference'
18
__Icon__ = 'HighlightDifference.svg'
19
__Help__ = 'Select two objects and run'
20
__Status__ = 'Production'
21
__Requires__ = 'FreeCAD V0.17+'
22
__Communication__ = 'https://github.com/FreeCAD/FreeCAD-macros/issues/'
23
__Files__ = 'HighlightDifference.svg'
24

25
from PySide import QtCore  # FreeCAD's special PySide!
26
from PySide.QtGui import QMessageBox  # FreeCAD's special PySide!
27

28
import FreeCAD as app
29
import FreeCADGui as gui
30

31

32
PREFERENCE_PATH = 'User parameter:BaseApp/Preferences/Mod/HighlightDifference'
33

34

35
def error_dialog(msg):
36
    """Create a simple dialog QMessageBox with an error message."""
37
    app.Console.PrintError(msg + '\n')
38
    diag = QMessageBox(QMessageBox.Icon.Critical,
39
                       'Error in macro highlight_difference',
40
                       msg)
41
    diag.setWindowModality(QtCore.Qt.ApplicationModal)
42
    diag.exec_()
43

44

45
def load_and_save_settings():
46
    param_change = 'change_transparency'
47
    param_transparency = 'transparency'
48
    p = app.ParamGet(PREFERENCE_PATH)
49
    change_transparency = p.GetBool(param_change, True)
50
    transparency = p.GetInt(param_transparency, 80)
51
    p.SetBool(param_change, change_transparency)
52
    p.SetInt(param_transparency, transparency)
53
    return change_transparency, transparency
54

55

56
def main():
57
    if len(gui.Selection.getSelection()) < 2:
58
        error_dialog('Select two objects')
59
        return
60

61
    object_a = gui.Selection.getSelection()[0]
62
    object_b = gui.Selection.getSelection()[1]
63
    try:
64
        shape_a = object_a.Shape
65
        shape_b = object_b.Shape
66
        label_a = object_a.Label
67
        label_b = object_b.Label
68
    except AttributeError:
69
        error_dialog('No suitable objects selected, select two objects\n')
70
        return
71

72
    shape_addition = shape_a.cut(shape_b)
73
    if shape_addition.Volume < 1e-6:
74
        app.Console.PrintMessage('No addition from {} to {}\n'.format(
75
            label_a, label_b))
76
    else:
77
        app.Console.PrintMessage(
78
            'Volume of the addition from {} to {}: {}\n'.format(
79
                label_a, label_b, shape_addition.Volume))
80

81
    shape_removal = shape_b.cut(shape_a)
82
    if shape_removal.Volume < 1e-6:
83
        app.Console.PrintMessage('No removal from {} to {}\n'.format(
84
            label_a, label_b))
85
    else:
86
        app.Console.PrintMessage(
87
            'Volume of the removal from {} to {}: {}\n'.format(
88
                label_a, label_b, shape_removal.Volume))
89

90
    if (shape_addition.Volume < 1e-6) and (shape_removal.Volume < 1e-6):
91
        app.Console.PrintMessage('{} and {} have the same shape\n'.format(
92
            label_a, label_b))
93

94
    added = app.ActiveDocument.addObject('Part::Feature')
95
    added.Label = 'Addition ({} − {})'.format(label_a, label_b)
96
    added.Shape = shape_addition
97
    added.ViewObject.ShapeColor = (1.0, 0.0, 0.0, 1.0)
98
    removed = app.ActiveDocument.addObject('Part::Feature')
99
    removed.Label = 'Removal ({} − {})'.format(label_b, label_a)
100
    removed.Shape = shape_removal
101
    removed.ViewObject.ShapeColor = (0.0, 0.5, 0.0, 1.0)
102

103
    change_transparency, transparency = load_and_save_settings()
104
    if change_transparency:
105
        try:
106
            object_a.ViewObject.Transparency = transparency
107
            object_b.ViewObject.Transparency = transparency
108
        except AttributeError:
109
            pass
110

111

112
if __name__ == '__main__':
113
    main()
114

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.