FreeCAD-macros

Форк
0
/
HighlightCommon.FCMacro 
144 строки · 5.2 Кб
1
# -*- coding: utf-8 -*-
2

3
from __future__ import unicode_literals
4

5
__Name__ = 'Highlight Common parts'
6
__Comment__ = 'Compute the common parts between selected shapes'
7
__Author__ = 'JMG, galou and other contributors'
8
__Version__ = '2.3.2'
9
__Date__ = '2021-07-01'
10
__License__ = 'CC0-1.0'
11
__Web__ = 'https://freecadweb.org/wiki/Macro_HighlightCommon'
12
__Wiki__ = 'https://freecadweb.org/wiki/Macro_HighlightCommon'
13
__Icon__ = 'HighlightCommon.png'
14
__Help__ = 'Select at least two objects and run'
15
__Status__ = 'Production'
16
__Requires__ = 'FreeCAD V0.17+'
17
__Communication__ = 'https://github.com/FreeCAD/FreeCAD-macros/issues/'
18
__Files__ = 'HighlightCommon.png'
19

20
from PySide.QtGui import QMessageBox  # FreeCAD's special PySide!
21

22
import FreeCAD as app
23
import FreeCADGui as gui
24

25

26
PREFERENCE_PATH = 'User parameter:BaseApp/Preferences/Mod/HighlightCommon'
27

28

29
def load_and_save_settings():
30
    param_change = 'change_transparency'
31
    param_transparency = 'transparency'
32
    p = app.ParamGet(PREFERENCE_PATH)
33
    change_transparency = p.GetBool(param_change, True)
34
    transparency = p.GetInt(param_transparency, 80)
35
    p.SetBool(param_change, change_transparency)
36
    p.SetInt(param_transparency, transparency)
37
    return change_transparency, transparency
38

39

40
def main():
41
    # Store active doc in case it may change during run.
42
    doc = app.activeDocument()
43

44
    if doc is None:
45
        return
46

47
    change_transparency, transparency = load_and_save_settings()
48

49
    colli_grp = None  # Group object to group collisions.
50
    colli_max = 0  # Maximum collision volume.
51

52
    # Open a transaction in undo pile.
53
    doc.openTransaction('Seeking collisions')
54

55
    objectsToEnumerate = gui.Selection.getSelection()
56
    if len(objectsToEnumerate) < 2:
57
        objectsToEnumerate = doc.Objects
58

59
    # Ensure list of unique objects.
60
    object_list = []
61
    for obj in objectsToEnumerate:
62
        if ((obj not in object_list)
63
                and hasattr(obj, 'Shape')
64
                and hasattr(obj, 'getGlobalPlacement')
65
                and hasattr(obj, 'Label')):
66
            object_list.append(obj)
67

68
    # Going through selected objects (object A).
69
    for i, object_a in enumerate(object_list):
70
        shape_a = object_a.Shape.copy()
71
        shape_a.Placement = object_a.getGlobalPlacement()
72
        label_a = object_a.Label
73

74
        # Making selected objects transparent.
75
        if change_transparency:
76
            try:
77
                object_a.ViewObject.Transparency = transparency
78
            except AttributeError:
79
                pass
80

81
        # Comparing object A with all
82
        # following ones in the list (object B).
83
        for object_b in object_list[(i + 1):]:
84

85
            shape_b = object_b.Shape.copy()
86
            shape_b.Placement = object_b.getGlobalPlacement()
87
            label_b = object_b.Label
88
            common = shape_a.common(shape_b)
89

90
            # Making selected objects transparent.
91
            if change_transparency:
92
                try:
93
                    object_b.ViewObject.Transparency = transparency
94
                except AttributeError:
95
                    pass
96

97
            # If object A & object B have a collision
98
            # display a message with collision volume and
99
            # add a new representative shape in the group.
100
            if common.Volume > 1e-6:
101
                app.Console.PrintMessage(
102
                    'Volume of the intersection between {} and {}: {:.3f} mm³\n'.format(
103
                        label_a,
104
                        label_b,
105
                        common.Volume))
106
                colli_max = common.Volume if common.Volume > colli_max else colli_max
107
                if not colli_grp:
108
                    # Create group if it doesn't already exist.
109
                    colli_grp = doc.addObject('App::DocumentObjectGroup',
110
                                              'Collisions')
111
                intersection_object = doc.addObject(
112
                    'Part::Feature')
113
                intersection_object.Label = '{} - {}'.format(
114
                    label_a, label_b)
115
                intersection_object.Shape = common
116
                intersection_object.ViewObject.ShapeColor = (1.0, 0.0, 0.0, 1.0)
117
                colli_grp.addObject(intersection_object)
118
            else:
119
                # If no collision, just inform the user.
120
                app.Console.PrintMessage(
121
                    'No intersection between {} and {}\n'.format(
122
                        label_a,
123
                        label_b))
124

125
    # If collisions have been found, commit the undo transaction and
126
    # give a summary (count + max value) to the user.
127
    if colli_grp:
128
        doc.commitTransaction()
129
        app.Console.PrintMessage(
130
            '{} collision(s) found between selected objects\nMaximum collision: {:.3f} mm³\n'.format(
131
                len(colli_grp.Group),
132
                colli_max))
133
        doc.recompute()
134
    else:
135
        # If no collision has been found, just inform the user about it.
136
        doc.abortTransaction()
137
        if len(object_list) >= 2:
138
            app.Console.PrintWarning('No collision found between selected objects\n')
139
        else:
140
            app.Console.PrintWarning('No suitable objects selected, select at least two objects\n')
141

142

143
if __name__ == '__main__':
144
    main()
145

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

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

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

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