FreeCAD-macros

Форк
0
/
SimpleProperties.FCMacro 
94 строки · 3.3 Кб
1
#!/usr/bin/python
2
#####################################
3
# Copyright (c) openBrain 2019
4
# Licensed under LGPL v2
5
#
6
# This FreeCAD macro will give basic properties of the selected object (volume, boundbox, ...)
7
#
8
#
9
# Version history :
10
# *0.7 : some typo improvement + commenting
11
# *0.6 : check if selected object has a valid shape
12
# *0.5 : beta release
13
#
14
#####################################
15

16
__Name__ = 'Simple Properties'
17
__Comment__ = 'Gives basic properties of object (volume, boundbox, ...)'
18
__Author__ = 'openBrain'
19
__Version__ = '0.7.1'
20
__Date__ = '2019-07-10'
21
__License__ = 'LGPL v2'
22
__Web__ = 'https://www.freecadweb.org/wiki/Macro_SimpleProperties'
23
__Wiki__ = 'https://www.freecadweb.org/wiki/Macro_SimpleProperties'
24
__Icon__ = ''
25
__Help__ = 'Select an object and run the macro'
26
__Status__ = 'Beta'
27
__Requires__ = 'FreeCAD >= 0.17'
28

29
__dbg__ = False  # True for debugging.
30
g_disp_width = 3  # Set the display format of numbers.
31

32
from PySide import QtGui
33

34
import FreeCAD as app
35
import FreeCADGui as gui
36

37
def cslM(msg): #Print message in console
38
    app.Console.PrintMessage('\n')
39
    app.Console.PrintMessage(msg)
40

41

42
def cslW(msg): #Print warning in console
43
    app.Console.PrintMessage('\n')
44
    app.Console.PrintWarning(msg)
45

46

47
def cslE(msg): #Print error in console
48
    app.Console.PrintMessage('\n')
49
    app.Console.PrintError(msg)
50

51

52
def cslD(msg): #Print debug message in console
53
    if __dbg__:
54
        app.Console.PrintMessage('\n')
55
        app.Console.PrintMessage('Debug: ' + str(msg))
56

57
if __dbg__:  ##Clear report view in debug mode
58
    gui.getMainWindow().findChild(QtGui.QTextEdit, 'Report view').clear()
59

60
cslM('Starting Simple Properties macro')
61

62
if len(gui.Selection.getSelection()) != 1:
63
    # If not exactly one object selected, warn user & quit.
64
    cslE('One and only one object shall be selected ... Exiting')
65
elif not ('Shape' in gui.Selection.getSelection()[0].PropertiesList):
66
    # If selected object has no shape, warn user & exit.
67
    cslE('Selected object has no valid shape ... Exiting')
68
else:
69
    obj = gui.Selection.getSelection()[0]
70
    # Get selected object.
71
    retStr = ''
72
    if len(gui.Selection.getSelectionEx()[0].SubObjects) != 1:
73
        # If several object subobjects have been selected, ignore & warn user.
74
        cslW('None or several subobject(s) selected, will be ignored')
75
    else:
76
        # If one subobject selected.
77
        objEx = gui.Selection.getSelectionEx()[0].SubObjects[0]
78
        if isinstance(objEx, Part.Edge):
79
            # If it's an edge, print its length.
80
            retStr += 'Edge length: {:.{w}g} mm\n'.format(objEx.Length, w=g_disp_width)
81
        elif isinstance(objEx, Part.Face):
82
            # If it's a face, print its area.
83
            retStr += 'Face area: {:.{w}g} m²\n'.format(objEx.Area / 1000000, w=g_disp_width)
84
        else:
85
            # If other (unsupported) type, warn user.
86
            cslD('Subobject type: ' + str(objEx.ShapeType))
87
            cslW('Unsupported type of subobject')
88
    retStr += 'Object volume: {:.{w}g}  l\n'.format(obj.Shape.Volume / 1000000, w=g_disp_width)
89
    bb = obj.Shape.BoundBox  # Get object's bounding box.
90
    retStr += 'Object boundbox : {:.{w}g} x {:.{w}g} x {:.{w}g} mm³\n'.format(
91
        bb.XLength, bb.YLength, bb.ZLength, w=g_disp_width)
92

93
    # Display information in a message box.
94
    QtGui.QMessageBox(QtGui.QMessageBox.Information, 'Object Simple Props', retStr).exec_()
95

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

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

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

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