FreeCAD-macros
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.30g_disp_width = 3 # Set the display format of numbers.31
32from PySide import QtGui33
34import FreeCAD as app35import FreeCADGui as gui36
37def cslM(msg): #Print message in console38app.Console.PrintMessage('\n')39app.Console.PrintMessage(msg)40
41
42def cslW(msg): #Print warning in console43app.Console.PrintMessage('\n')44app.Console.PrintWarning(msg)45
46
47def cslE(msg): #Print error in console48app.Console.PrintMessage('\n')49app.Console.PrintError(msg)50
51
52def cslD(msg): #Print debug message in console53if __dbg__:54app.Console.PrintMessage('\n')55app.Console.PrintMessage('Debug: ' + str(msg))56
57if __dbg__: ##Clear report view in debug mode58gui.getMainWindow().findChild(QtGui.QTextEdit, 'Report view').clear()59
60cslM('Starting Simple Properties macro')61
62if len(gui.Selection.getSelection()) != 1:63# If not exactly one object selected, warn user & quit.64cslE('One and only one object shall be selected ... Exiting')65elif not ('Shape' in gui.Selection.getSelection()[0].PropertiesList):66# If selected object has no shape, warn user & exit.67cslE('Selected object has no valid shape ... Exiting')68else:69obj = gui.Selection.getSelection()[0]70# Get selected object.71retStr = ''72if len(gui.Selection.getSelectionEx()[0].SubObjects) != 1:73# If several object subobjects have been selected, ignore & warn user.74cslW('None or several subobject(s) selected, will be ignored')75else:76# If one subobject selected.77objEx = gui.Selection.getSelectionEx()[0].SubObjects[0]78if isinstance(objEx, Part.Edge):79# If it's an edge, print its length.80retStr += 'Edge length: {:.{w}g} mm\n'.format(objEx.Length, w=g_disp_width)81elif isinstance(objEx, Part.Face):82# If it's a face, print its area.83retStr += 'Face area: {:.{w}g} m²\n'.format(objEx.Area / 1000000, w=g_disp_width)84else:85# If other (unsupported) type, warn user.86cslD('Subobject type: ' + str(objEx.ShapeType))87cslW('Unsupported type of subobject')88retStr += 'Object volume: {:.{w}g} l\n'.format(obj.Shape.Volume / 1000000, w=g_disp_width)89bb = obj.Shape.BoundBox # Get object's bounding box.90retStr += 'Object boundbox : {:.{w}g} x {:.{w}g} x {:.{w}g} mm³\n'.format(91bb.XLength, bb.YLength, bb.ZLength, w=g_disp_width)92
93# Display information in a message box.94QtGui.QMessageBox(QtGui.QMessageBox.Information, 'Object Simple Props', retStr).exec_()95