I am trying to write a macro that analyses a body object while it is being edited by an user. I assume everything takes place in the PartDesign workbench, where a body has been created to model an object. An initial geometry is created (e.g. by extruding a sketch) then others features are added (by the user) sequentially until the design is complete. This is basically the Feature Editing Methodology (https://wiki.freecadweb.org/Feature_editing) for modeling a part. Throughout the modeling, the body changes as a result of the features used by the designer (see image below) What I am trying to do is a macro that triggers some instructions whenever a new feature is added, in other words whenever the body changes (e.g. compute the volume of the body and possibly raise a warning if it is larger than a certain threshold). My question is: how to track this change in the body object?
The macro takes the form of a window as shown below
Here is the code for that macro:
Code: Select all
import FreeCAD
from PySide import QtGui, QtCore
class Analysis(QtGui.QWidget):
def __init__(self):
super(Analysis, self).__init__()
self.initUI()
def initUI(self):
self.maxVolumeLbl = QtGui.QLabel("Threshold:",self)
self.maxVolumeLbl.move(30,20)
self.MaxVolume = 10000
qle = QtGui.QLineEdit(self)
qle.move(100, 15)
qle.textChanged[str].connect(self.onChanged)
self.BodyVolumelbl1 = QtGui.QLabel("Body volume:",self)
self.BodyVolumelbl1.move(30,50)
self.BodyVolumelbl2 = QtGui.QLabel(" ", self)
self.BodyVolumelbl2.move(150, 50)
pushButtonAnalysis = QtGui.QPushButton(" Launch Analysis! ",self)
pushButtonAnalysis.clicked.connect(self.onAnalysis)
pushButtonAnalysis.move(40, 85)
self.setGeometry(300, 300, 300, 120)
self.setWindowTitle('Body Volume Analysis')
self.show()
def onChanged(self,value):
self.MaxVolume = float(value)
def onAnalysis(self):
v = FreeCAD.ActiveDocument.Body.Shape.Volume
self.BodyVolumelbl2.setText("{:.2f}".format(v))
if v>self.MaxVolume:
warn = QtGui.QMessageBox(QtGui.QMessageBox.Warning,'Volume issue!','Volume is higher than maximum volume')
warn.exec_()
test = Analysis()
Any help would be greatly appreciated.
Thanks in advance.