From 05347f2f8df2437c9a915858bc89035901feda5d Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Fri, 10 Jun 2016 06:01:19 +0200 Subject: o Basic support for disabling sides. --- Boxer.py | 142 ++++++++++++++++++++++++++++++--------------------------------- 1 file changed, 67 insertions(+), 75 deletions(-) (limited to 'Boxer.py') diff --git a/Boxer.py b/Boxer.py index a15b03a..be2ee80 100644 --- a/Boxer.py +++ b/Boxer.py @@ -69,20 +69,23 @@ class SideConfig(Enum): raise Exception("Unknown direction: " + repr(direction)) -class BoxSide(Enum): - front = ('Front', MeasurementDirection.width, MeasurementDirection.height, SideConfig.large, (0, -1, 0)) - back = ('Back', MeasurementDirection.width, MeasurementDirection.height, SideConfig.large, (0, 1, 0)) - left = ('Left', MeasurementDirection.depth, MeasurementDirection.height, SideConfig.small, (-1, 0, 0)) - right = ('Right', MeasurementDirection.depth, MeasurementDirection.height, SideConfig.small, (1, 0, 0)) - top = ('Top', MeasurementDirection.width, MeasurementDirection.depth, SideConfig.medium, (0, 0, 1)) - bottom = ('Bottom', MeasurementDirection.width, MeasurementDirection.depth, SideConfig.medium, (0, 0, -1)) +class SideType(Enum): + front = 1 + back = 2 + left = 3 + right = 4 + top = 5 + bottom = 6 + +class BoxSide(object): def __init__(self, title, directionX, directionY, sideConfig, extrudeVector): self.title = title self.directionX = directionX self.directionY = directionY self.sideConfig = sideConfig self.extrudeVector = Vector(extrudeVector) + self.enabled = True class NotchConfig(object): @@ -101,6 +104,14 @@ class BoxCfg(object): self.generateExtrudes = True self.generateCrossSections = False # Not very useful so off by default self.outerDimmensions(100, 100, 100) + self.sides = { + SideType.front: BoxSide('Front', MeasurementDirection.width, MeasurementDirection.height, SideConfig.large, (0, -1, 0)), + SideType.back: BoxSide('Back', MeasurementDirection.width, MeasurementDirection.height, SideConfig.large, (0, 1, 0)), + SideType.left: BoxSide('Left', MeasurementDirection.depth, MeasurementDirection.height, SideConfig.small, (-1, 0, 0)), + SideType.right: BoxSide('Right', MeasurementDirection.depth, MeasurementDirection.height, SideConfig.small, (1, 0, 0)), + SideType.top: BoxSide('Top', MeasurementDirection.width, MeasurementDirection.depth, SideConfig.medium, (0, 0, 1)), + SideType.bottom: BoxSide('Bottom', MeasurementDirection.width, MeasurementDirection.depth, SideConfig.medium, (0, 0, -1)) + } self.calculate() @property @@ -130,6 +141,9 @@ class BoxCfg(object): def notchSize(self, notchSize): self._notchSize = notchSize + def side(self, sideType): + return self.sides[sideType] + def outerDimmensions(self, width, height, depth): self.outerWidth = width self.outerHeight = height @@ -165,6 +179,9 @@ class BoxCfg(object): print("Thickness : " + str(self._thickness)) print("Outer w/h/d: " + str(self.outerWidth) + "/" + str(self.outerHeight) + "/" + str(self.outerDepth)) print("Inner w/h/d: " + str(self.innerWidth) + "/" + str(self.innerHeight) + "/" + str(self.innerDepth)) + print("Sides:") + for (sideType, boxSide) in self.sides.items(): + print(" {:<9}: enabled: {}".format(boxSide.title, boxSide.enabled)) print("Notch size : " + str(self._notchSize)) print(" Width: " + str(self.notchConfig(MeasurementDirection.width))) print(" Height: " + str(self.notchConfig(MeasurementDirection.height))) @@ -236,8 +253,8 @@ def makeBoxSide(cfg, boxSide): return v -def makeBox(doc, cfg): - objects = {} +def makeParts(doc, cfg): + parts = {} sep = cfg.outerWidth * 0.05 @@ -258,51 +275,37 @@ def makeBox(doc, cfg): m.move(v) return m - # front back left - # right top bottom - - s = Part.makePolygon(makeBoxSide(cfg, BoxSide.front)) - s.transformShape(m(True, False, -0.5, -0.5, 0.5)) - front = doc.addObject("Part::Feature", "Front") - front.Shape = s - - s = Part.makePolygon(makeBoxSide(cfg, BoxSide.back)) - s.transformShape(m(True, False, -0.5, 0.5, 0.5)) - back = doc.addObject("Part::Feature", "Back") - back.Shape = s - - l = Part.makePolygon(makeBoxSide(cfg, BoxSide.left)) - l.transformShape(m(True, True, -0.5, -0.5, 0.5)) - left = doc.addObject("Part::Feature", "Left") - left.Shape = l - - l = Part.makePolygon(makeBoxSide(cfg, BoxSide.right)) - l.transformShape(m(True, True, 0.5, -0.5, 0.5)) - right = doc.addObject("Part::Feature", "Right") - right.Shape = l - - l = Part.makePolygon(makeBoxSide(cfg, BoxSide.top)) - l.transformShape(m(False, False, -0.5, 0.5, 0.5)) - top = doc.addObject("Part::Feature", "Top") - top.Shape = l - - l = Part.makePolygon(makeBoxSide(cfg, BoxSide.bottom)) - l.transformShape(m(False, False, -0.5, 0.5, -0.5)) - bottom = doc.addObject("Part::Feature", "Bottom") - bottom.Shape = l - - #front.ViewObject.Visibility = False - #back.ViewObject.Visibility = False - #left.ViewObject.Visibility = False - #right.ViewObject.Visibility = False - #top.ViewObject.Visibility = False - #bottom.ViewObject.Visibility = False - - return {'front': front, 'back': back, 'left': left, 'right': right, 'top': top, 'bottom': bottom} - - -def makeExtrudes(doc, cfg): - def makeExtrude(doc, cfg, side, part): + # yapf: disable + ms = { + SideType.front: m(True, False, -0.5, -0.5, 0.5), + SideType.back: m(True, False, -0.5, 0.5, 0.5), + SideType.left: m(True, True, -0.5, -0.5, 0.5), + SideType.right: m(True, True, 0.5, -0.5, 0.5), + SideType.top: m(False, False, -0.5, 0.5, 0.5), + SideType.bottom: m(False, False, -0.5, 0.5, -0.5) + } + # yapf: enable + + for sideType in SideType: + boxSide = cfg.side(sideType) + + if not boxSide.enabled: + continue + + s = Part.makePolygon(makeBoxSide(cfg, boxSide)) + s.transformShape(ms[sideType]) + feature = doc.addObject("Part::Feature", boxSide.title + '_Part') + feature.Label = boxSide.title + ' Part' + feature.Shape = s + parts[sideType] = feature + + return parts + + +def makeExtrudes(doc, parts, cfg): + def makeExtrude(doc, cfg, sideType): + side = cfg.side(sideType) + part = parts[sideType] extrude = doc.addObject("Part::Extrusion", side.title + "_Extrude") extrude.Base = part extrude.Dir = side.extrudeVector * cfg.thickness @@ -321,22 +324,11 @@ def makeExtrudes(doc, cfg): return extrude - objects = {} - objects['front'] = makeExtrude(doc, cfg, BoxSide.front, doc.Front) - objects['back'] = makeExtrude(doc, cfg, BoxSide.back, doc.Back) - objects['left'] = makeExtrude(doc, cfg, BoxSide.left, doc.Left) - objects['right'] = makeExtrude(doc, cfg, BoxSide.right, doc.Right) - objects['top'] = makeExtrude(doc, cfg, BoxSide.top, doc.Top) - objects['bottom'] = makeExtrude(doc, cfg, BoxSide.bottom, doc.Bottom) - - #objects['front'].ViewObject.Visibility = False - #objects['back'].ViewObject.Visibility = False - #objects['left'].ViewObject.Visibility = False - #objects['right'].ViewObject.Visibility = False - #objects['top'].ViewObject.Visibility = False - #objects['bottom'].ViewObject.Visibility = False + extrudes = {} + for sideType, part in parts.items(): + extrudes[sideType] = makeExtrude(doc, cfg, sideType) - return objects + return extrudes def makeCrossSections(doc, cfg, extrudes): @@ -354,18 +346,18 @@ def makeCrossSections(doc, cfg, extrudes): cs.purgeTouched() return cs - objects = {} + crossSections = {} for key, extrude in extrudes.items(): - objects[key] = makeCrossSection(extrude, key.title()) - return objects + crossSections[key] = makeCrossSection(extrude, key.title()) + return crossSections def make(doc, cfg): # Cretate the underlying parts - parts = makeBox(doc, cfg) + parts = makeParts(doc, cfg) if cfg.generateExtrudes: - extrudes = makeExtrudes(doc, cfg) + extrudes = makeExtrudes(doc, parts, cfg) box = doc.addObject("App::DocumentObjectGroup", "Box") for e in extrudes.values(): @@ -393,4 +385,4 @@ def removeEverything(doc): for key in ['Front', 'Back', 'Left', 'Right', 'Top', 'Bottom']: rm(key + '_Cross_Section') rm(key + '_Extrude') - rm(key) + rm(key + '_Part') -- cgit v1.2.3