FreeCAD-macros
113 строк · 3.9 Кб
1# -*- coding: utf-8 -*-
2"""
3Compute the difference between two shapes. Additions are marked red, removals
4are marked green. Both original parts will be half transparent. The volume of
5the additions and removals are printed in the console.
6"""
7
8from __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
25from PySide import QtCore # FreeCAD's special PySide!
26from PySide.QtGui import QMessageBox # FreeCAD's special PySide!
27
28import FreeCAD as app
29import FreeCADGui as gui
30
31
32PREFERENCE_PATH = 'User parameter:BaseApp/Preferences/Mod/HighlightDifference'
33
34
35def error_dialog(msg):
36"""Create a simple dialog QMessageBox with an error message."""
37app.Console.PrintError(msg + '\n')
38diag = QMessageBox(QMessageBox.Icon.Critical,
39'Error in macro highlight_difference',
40msg)
41diag.setWindowModality(QtCore.Qt.ApplicationModal)
42diag.exec_()
43
44
45def load_and_save_settings():
46param_change = 'change_transparency'
47param_transparency = 'transparency'
48p = app.ParamGet(PREFERENCE_PATH)
49change_transparency = p.GetBool(param_change, True)
50transparency = p.GetInt(param_transparency, 80)
51p.SetBool(param_change, change_transparency)
52p.SetInt(param_transparency, transparency)
53return change_transparency, transparency
54
55
56def main():
57if len(gui.Selection.getSelection()) < 2:
58error_dialog('Select two objects')
59return
60
61object_a = gui.Selection.getSelection()[0]
62object_b = gui.Selection.getSelection()[1]
63try:
64shape_a = object_a.Shape
65shape_b = object_b.Shape
66label_a = object_a.Label
67label_b = object_b.Label
68except AttributeError:
69error_dialog('No suitable objects selected, select two objects\n')
70return
71
72shape_addition = shape_a.cut(shape_b)
73if shape_addition.Volume < 1e-6:
74app.Console.PrintMessage('No addition from {} to {}\n'.format(
75label_a, label_b))
76else:
77app.Console.PrintMessage(
78'Volume of the addition from {} to {}: {}\n'.format(
79label_a, label_b, shape_addition.Volume))
80
81shape_removal = shape_b.cut(shape_a)
82if shape_removal.Volume < 1e-6:
83app.Console.PrintMessage('No removal from {} to {}\n'.format(
84label_a, label_b))
85else:
86app.Console.PrintMessage(
87'Volume of the removal from {} to {}: {}\n'.format(
88label_a, label_b, shape_removal.Volume))
89
90if (shape_addition.Volume < 1e-6) and (shape_removal.Volume < 1e-6):
91app.Console.PrintMessage('{} and {} have the same shape\n'.format(
92label_a, label_b))
93
94added = app.ActiveDocument.addObject('Part::Feature')
95added.Label = 'Addition ({} − {})'.format(label_a, label_b)
96added.Shape = shape_addition
97added.ViewObject.ShapeColor = (1.0, 0.0, 0.0, 1.0)
98removed = app.ActiveDocument.addObject('Part::Feature')
99removed.Label = 'Removal ({} − {})'.format(label_b, label_a)
100removed.Shape = shape_removal
101removed.ViewObject.ShapeColor = (0.0, 0.5, 0.0, 1.0)
102
103change_transparency, transparency = load_and_save_settings()
104if change_transparency:
105try:
106object_a.ViewObject.Transparency = transparency
107object_b.ViewObject.Transparency = transparency
108except AttributeError:
109pass
110
111
112if __name__ == '__main__':
113main()
114