import FreeCAD from FreeCAD import Gui from PySide import QtGui, QtCore import Boxer, BoxerDockWidget from Boxer import SideType class BoxerDocumentObserver: def __init__(self): pass def slotDeletedDocument(self, doc): print('slotDeletedDocument') gui.dockWidget.ui.generateButton.setEnabled(False) def slotActivateDocument(self, doc): print('slotActivateDocument') gui.dockWidget.ui.generateButton.setEnabled(True) class BoxerGui(object): def __init__(self): self.cfg = Boxer.BoxCfg().outerDimmensions(100, 100, 100) self.cfg.notchUnitWidth = 10 self.cfg.thickness = 10 def setupGui(self): doc = FreeCAD.ActiveDocument self.dockWidget = dockWidget = QtGui.QDockWidget() dockWidget.setWindowTitle("Box config") dockWidget.ui = BoxerDockWidget.Ui_BoxerDockWidget() dockWidget.generateClicked = self.generateButtonClicked dockWidget.fitAllClicked = self.fitAllButtonClicked dockWidget.ui.setupUi(dockWidget) dockWidget.ui.generateButton.setEnabled(doc is not None) dockWidget.ui.outerWidth.setValue(self.cfg.outerWidth) dockWidget.ui.outerHeight.setValue(self.cfg.outerHeight) dockWidget.ui.outerDepth.setValue(self.cfg.outerDepth) dockWidget.ui.thickness.setValue(self.cfg.thickness) dockWidget.ui.notchSize.setValue(self.cfg.notchUnitWidth) self.sideCheckBoxes = [ # yapf: disable (SideType.front, dockWidget.ui.frontCheckBox), (SideType.back, dockWidget.ui.backCheckBox), (SideType.left, dockWidget.ui.leftCheckBox), (SideType.right, dockWidget.ui.rightCheckBox), (SideType.top, dockWidget.ui.topCheckBox), (SideType.bottom, dockWidget.ui.bottomCheckBox) # yapf: enable ] for (sideType, checkBox) in self.sideCheckBoxes: checkBox.setChecked(self.cfg.side(sideType).enabled) self.documentObserver = BoxerDocumentObserver() FreeCAD.addDocumentObserver(self.documentObserver) def generateButtonClicked(self): ui = self.dockWidget.ui self.cfg.outerDimmensions(ui.outerWidth.value(), ui.outerHeight.value(), ui.outerDepth.value()) self.cfg.thickness = self.dockWidget.ui.thickness.value() self.cfg.notchSize = self.dockWidget.ui.notchSize.value() for (sideType, checkBox) in self.sideCheckBoxes: self.cfg.side(sideType).enabled = checkBox.isChecked() self.cfg.prt() doc = FreeCAD.ActiveDocument Boxer.removeEverything(doc) objects = Boxer.make(doc, self.cfg) itemsToSelect = objects['parts'] extrudes = objects['extrudes'] if extrudes is not None: itemsToSelect = extrudes Gui.Selection.clearSelection() [Gui.Selection.addSelection(e) for e in itemsToSelect.values()] Gui.SendMsgToActiveView("ViewSelection") Gui.Selection.clearSelection() # This results in some strange flickering. Perhaps all the items should be set visible=False while rotating Gui.SendMsgToActiveView("ViewAxo") def removeBox(self): doc = FreeCAD.ActiveDocument Boxer.removeEverything(doc) def fitAllButtonClicked(self): Gui.SendMsgToActiveView("ViewFit") gui = BoxerGui() gui.setupGui()