From 45611fe3339d11169b1e107635d4f037cad0d7b5 Mon Sep 17 00:00:00 2001 From: spo Date: Fri, 30 Oct 2015 15:21:27 +0300 Subject: [PATCH] Modify box macros and interface to match style and structure requirements. --- src/PythonAPI/extension/box.py | 48 +++++++--- src/PythonAddons/macros/box/feature.py | 118 ++++++++++++------------- 2 files changed, 95 insertions(+), 71 deletions(-) diff --git a/src/PythonAPI/extension/box.py b/src/PythonAPI/extension/box.py index 9377fe345..4cdfa75a2 100644 --- a/src/PythonAPI/extension/box.py +++ b/src/PythonAPI/extension/box.py @@ -3,26 +3,50 @@ Author: Daniel Brunier-Coulin Copyright (C) 2014-20xx CEA/DEN, EDF R&D """ -import model +from model import Interface from macros.box.feature import BoxFeature as MY -def addBox(self, container, *args): +def addBox(container, *args): feature = container.addFeature(MY.ID()) return Box(feature, *args) -class Box(model.Interface): +class Box(Interface): """Executes the macro-feature Box.""" - def __init__(self, feature, dx, dy, dz): - model.Interface.__init__(self, feature) + def __init__(self, feature, *args): + Interface.__init__(self, feature) + assert(self._feature.getKind() == MY.ID()) - self.setRealInput(MY.WIDTH_ID(), dx) - self.setRealInput(MY.LENGTH_ID(), dy) - self.setRealInput(MY.HEIGHT_ID(), dz) + self._width = self._feature.real(MY.WIDTH_ID()) + self._length = self._feature.real(MY.LENGTH_ID()) + self._height = self._feature.real(MY.HEIGHT_ID()) - if self.areInputValid(): - self.execute() - else: - raise Exception("cannot make the Box") + assert(self._width) + assert(self._length) + assert(self._height) + + if not args: + return + + assert(len(args) == 3) + dx, dy, dz = args + self.setWidth(dx) + self.setLength(dy) + self.setHeight(dz) + + self._execute() + pass + + def setWidth(self, width): + self._fill_attribute(self._width, width) + pass + + def setLength(self, length): + self._fill_attribute(self._length, length) + pass + + def setHeight(self, height): + self._fill_attribute(self._height, height) + pass diff --git a/src/PythonAddons/macros/box/feature.py b/src/PythonAddons/macros/box/feature.py index d5d5e8e07..f160a396e 100644 --- a/src/PythonAddons/macros/box/feature.py +++ b/src/PythonAddons/macros/box/feature.py @@ -3,88 +3,88 @@ Authors: Renaud Nedelec - Daniel Brunier-Coulin Copyright (C) 2014-20xx CEA/DEN, EDF R&D """ -import modeler +import model import geom -class BoxFeature(modeler.Feature): +class BoxFeature(model.Feature): # Initializations - def __init__(self): - modeler.Feature.__init__(self) + def __init__(self): + model.Feature.__init__(self) - @staticmethod - def ID(): - return "Box" + @staticmethod + def ID(): + return "Box" - @staticmethod - def WIDTH_ID(): - return "width" + @staticmethod + def WIDTH_ID(): + return "width" - @staticmethod - def LENGTH_ID(): - return "length" + @staticmethod + def LENGTH_ID(): + return "length" - @staticmethod - def HEIGHT_ID(): - return "height" + @staticmethod + def HEIGHT_ID(): + return "height" + + def getKind(self): + return BoxFeature.ID() - def getKind(self): - return BoxFeature.ID() - # Creation of the box at default size - def initAttributes(self): + def initAttributes(self): + + # Creating the input arguments of the feature + self.addRealInput(self.WIDTH_ID()) + self.addRealInput(self.LENGTH_ID()) + self.addRealInput(self.HEIGHT_ID()) - # Creating the input arguments of the feature - self.addRealInput( self.WIDTH_ID() ) - self.addRealInput( self.LENGTH_ID() ) - self.addRealInput( self.HEIGHT_ID() ) + # Creating the base of the box with unit values + mypart = model.activeDocument() + xoy = model.defaultPlane("XOY") - # Creating the base of the box with unit values - mypart = modeler.activeDocument() - xoy = modeler.defaultPlane("XOY") + self.base = model.addSketch(mypart, xoy) - self.base = modeler.addSketch( mypart, xoy ) + p1 = geom.Pnt2d(0, 0) + p2 = geom.Pnt2d(0, 1) + p3 = geom.Pnt2d(1, 1) + p4 = geom.Pnt2d(1, 0) - p1 = geom.Pnt2d( 0, 0 ) - p2 = geom.Pnt2d( 0, 1 ) - p3 = geom.Pnt2d( 1, 1 ) - p4 = geom.Pnt2d( 1, 0 ) + line = self.base.addPolygon(p1, p2, p3, p4) - line = self.base.addPolygon(p1, p2, p3, p4) + self.base.setParallel(line[0].result(), line[2].result()) + self.base.setParallel(line[1].result(), line[3].result()) + self.base.setPerpendicular(line[0].result(), line[3].result()) - self.base.setParallel( line[0].result(), line[2].result() ) - self.base.setParallel( line[1].result(), line[3].result() ) - self.base.setPerpendicular( line[0].result(), line[3].result() ) + # Setting the size of the base with default values + self.width = self.base.setLength(line[0].result(), 50) # Keeps the constraint for edition + self.length = self.base.setLength(line[3].result(), 50) # Keeps the constraint for edition - # Setting the size of the base with default values - self.width = self.base.setLength( line[0].result(), 50 ) # Keeps the constraint for edition - self.length = self.base.setLength( line[3].result(), 50 ) # Keeps the constraint for edition + # Creating the extrusion (the box) at default size + self.box = model.addExtrusion(mypart, self.base.selectFace(), 50) - # Creating the extrusion (the box) at default size - self.box = modeler.addExtrusion( mypart, self.base.selectFace(), 50 ) - # Edition of the box at user size - def execute(self): - # Retrieving the user inputs - width = self.getRealInput( self.WIDTH_ID() ) - length = self.getRealInput( self.LENGTH_ID() ) - height = self.getRealInput( self.HEIGHT_ID() ) - - # Editing the box - self.base.setValue( self.width, width ) - self.base.setValue( self.length, length ) - self.box.setSize( height ) - - # Publishing the result: not needed for Macro feature - #self.addResult( self.box.result() ) - - def isMacro(self): - # Box feature is macro: removes itself on the creation transaction finish - return True + def execute(self): + # Retrieving the user inputs + width = self.getRealInput(self.WIDTH_ID()) + length = self.getRealInput(self.LENGTH_ID()) + height = self.getRealInput(self.HEIGHT_ID()) + + # Editing the box + self.base.setValue(self.width, width) + self.base.setValue(self.length, length) + self.box.setSize(height) + + # Publishing the result: not needed for Macro feature + # self.addResult( self.box.result() ) + + def isMacro(self): + # Box feature is macro: removes itself on the creation transaction finish + return True -- 2.39.2