From 32139455bfc7cbd3e6886ca195136b7b80a15d74 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Tue, 14 Jun 2016 23:11:16 +0200 Subject: o Working code for disabling sides. Polish required. --- Boxer.py | 217 +++++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 122 insertions(+), 95 deletions(-) (limited to 'Boxer.py') diff --git a/Boxer.py b/Boxer.py index af49dee..f331198 100644 --- a/Boxer.py +++ b/Boxer.py @@ -1,6 +1,3 @@ -# import Boxer; reload(Boxer); import BoxerGui as bg; reload(bg); - -#import FreeCAD from FreeCAD import Base, Vector, Matrix, Placement, Rotation import Part from enum import Enum @@ -88,10 +85,28 @@ class BoxSide(object): self.extrudeVector = Vector(extrudeVector) self.enabled = True self.sideTypesByLineDirections = sideTypesByLineDirections + self._thickness = 10 + + def left(self, lineDirection): + sideType = self.sideTypesByLineDirections[lineDirection][0] + return self.cfg.sides[sideType] - def opositeEnabled(self, lineDirection): - sideType = self.sideTypesByLineDirections[lineDirection] - return self.cfg.sides[sideType].enabled + def opposite(self, lineDirection): + sideType = self.sideTypesByLineDirections[lineDirection][1] + return self.cfg.sides[sideType] + + def right(self, lineDirection): + sideType = self.sideTypesByLineDirections[lineDirection][2] + return self.cfg.sides[sideType] + + @property + def thickness(self): + return self._thickness + + @thickness.setter + def thickness(self, thickness): + self._thickness = thickness + self.cfg.calculate() class NotchConfig(object): @@ -105,65 +120,60 @@ class NotchConfig(object): class BoxCfg(object): def __init__(self): - self._thickness = 10 - self._notchSize = 10 - self.generateExtrudes = True - self.generateCrossSections = False # Not very useful so off by default - self.outerDimmensions(100, 100, 100) + print('BoxCfg.__init__') + # yapf: disable self.sides = { SideType.front: BoxSide(self, 'Front', MeasurementDirection.width, MeasurementDirection.height, SideSize.large, (0, -1, 0), - {LineDirection.right: SideType.top, - LineDirection.down: SideType.right, - LineDirection.left: SideType.bottom, - LineDirection.up: SideType.left}), + {LineDirection.right: (SideType.left, SideType.top, SideType.right), + LineDirection.down: (SideType.top, SideType.right, SideType.bottom), + LineDirection.left: (SideType.right, SideType.bottom, SideType.left), + LineDirection.up: (SideType.bottom, SideType.left, SideType.top)}), SideType.back: BoxSide(self, 'Back', MeasurementDirection.width, MeasurementDirection.height, SideSize.large, (0, 1, 0), - {LineDirection.right: SideType.top, - LineDirection.down: SideType.right, - LineDirection.left: SideType.bottom, - LineDirection.up: SideType.left}), + {LineDirection.right: (SideType.right, SideType.top, SideType.left), + LineDirection.down: (SideType.top, SideType.left, SideType.bottom), + LineDirection.left: (SideType.left, SideType.bottom, SideType.right), + LineDirection.up: (SideType.bottom, SideType.right, SideType.top)}), SideType.left: BoxSide(self, 'Left', MeasurementDirection.depth, MeasurementDirection.height, SideSize.small, (-1, 0, 0), - {LineDirection.right: SideType.top, - LineDirection.down: SideType.front, - LineDirection.left: SideType.bottom, - LineDirection.up: SideType.back}), + {LineDirection.right: (SideType.front, SideType.top, SideType.back), + LineDirection.down: (SideType.top, SideType.back, SideType.bottom), + LineDirection.left: (SideType.back, SideType.bottom, SideType.front), + LineDirection.up: (SideType.bottom, SideType.front, SideType.top)}), SideType.right: BoxSide(self, 'Right', MeasurementDirection.depth, MeasurementDirection.height, SideSize.small, (1, 0, 0), - {LineDirection.right: SideType.top, - LineDirection.down: SideType.front, - LineDirection.left: SideType.bottom, - LineDirection.up: SideType.back}), + {LineDirection.right: (SideType.front, SideType.top, SideType.back), + LineDirection.down: (SideType.top, SideType.back, SideType.bottom), + LineDirection.left: (SideType.back, SideType.bottom, SideType.front), + LineDirection.up: (SideType.bottom, SideType.front, SideType.top)}), SideType.top: BoxSide(self, 'Top', MeasurementDirection.width, MeasurementDirection.depth, SideSize.medium, (0, 0, 1), - {LineDirection.right: SideType.back, - LineDirection.down: SideType.right, - LineDirection.left: SideType.back, - LineDirection.up: SideType.left}), + {LineDirection.right: (SideType.left, SideType.back, SideType.right), + LineDirection.down: (SideType.back, SideType.right, SideType.front), + LineDirection.left: (SideType.right, SideType.front, SideType.left), + LineDirection.up: (SideType.front, SideType.left, SideType.back)}), SideType.bottom: BoxSide(self, 'Bottom', MeasurementDirection.width, MeasurementDirection.depth, SideSize.medium, (0, 0, -1), - {LineDirection.right: SideType.back, - LineDirection.down: SideType.right, - LineDirection.left: SideType.back, - LineDirection.up: SideType.left}) + {LineDirection.right: (SideType.left, SideType.front, SideType.right), + LineDirection.down: (SideType.front, SideType.right, SideType.back), + LineDirection.left: (SideType.right, SideType.back, SideType.left), + LineDirection.up: (SideType.back, SideType.left, SideType.front)}) } # yapf: enable - self.calculate() - @property - def thickness(self): - return self._thickness + self._notchSize = 10 + self.outerDimmensions(100, 100, 100) - @thickness.setter - def thickness(self, thickness): - self._thickness = thickness self.calculate() -# TODO: replace this with a way to set number of notches in each direction -# @property -# def notches(self): -# return self._notches -# -# @notches.setter -# def notches(self, notches): -# self._notches = notches -# self.calculate() + self.generateExtrudes = True + self.generateCrossSections = False # Not very useful so off by default + + for s in self.sides: + s.enabled = False + self.sides[SideType.left].enabled = True + self.sides[SideType.left].thickness = 20 + self.sides[SideType.back].enabled = True + + @property + def thickness(self): + raise Exception('thickness is de-implemented') @property def notchSize(self): @@ -173,7 +183,8 @@ class BoxCfg(object): def notchSize(self, notchSize): self._notchSize = notchSize - def side(self, sideType): + def sideForType(self, sideType): + #print('sideForType: sideType=' + str(sideType)) return self.sides[sideType] def outerDimmensions(self, width, height, depth): @@ -181,7 +192,6 @@ class BoxCfg(object): self.outerHeight = height self.outerDepth = depth self.calculate() - return self def innerLength(self, lineDirection): if lineDirection == MeasurementDirection.width: @@ -209,21 +219,23 @@ class BoxCfg(object): return NotchConfig(count, size) def calculate(self): - self.innerWidth = self.outerWidth - 2 * self._thickness - self.innerHeight = self.outerHeight - 2 * self._thickness - self.innerDepth = self.outerDepth - 2 * self._thickness + self.innerWidth = self.outerWidth - self.sideForType(SideType.left).thickness - self.sideForType(SideType.right).thickness + self.innerHeight = self.outerHeight - self.sideForType(SideType.top).thickness - self.sideForType(SideType.bottom).thickness + self.innerDepth = self.outerDepth - self.sideForType(SideType.front).thickness - self.sideForType(SideType.back).thickness self._notchConfigWidth = self.createNotchConfig(self.innerWidth) self._notchConfigHeight = self.createNotchConfig(self.innerHeight) self._notchConfigDepth = self.createNotchConfig(self.innerDepth) def prt(self): print("Box configuration") - 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)) + for sideType in SideType: + boxSide = self.sideForType(sideType) + print(" " + str(boxSide.title)) + print(" enabled: " + str(boxSide.enabled)) + print(" thickness: " + str(boxSide.thickness)) print("Notch size : " + str(self._notchSize)) print(" Width: " + str(self.notchConfig(MeasurementDirection.width))) print(" Height: " + str(self.notchConfig(MeasurementDirection.height))) @@ -248,61 +260,69 @@ class VGen(object): self.move(self.dir.y_x * value, self.dir.y_y * value, 0) -def line(cfg, start, lineDirection, boxSide, notchConfig, innerLength): +# A line is though drawn from left to right, but is actually automatically +# rotated to go either right, down, left or up by the lineDirection parameter. +def line(cfg, start, boxSide, lineDirection, notchConfig, innerLength): sideSize = boxSide.sideSize + opposite = boxSide.opposite(lineDirection) + left = boxSide.left(lineDirection) + right = boxSide.right(lineDirection) + + print('line: lineDirection=' + str(lineDirection) + ', opposite=' + opposite.title + ', left=' + left.title + ', right=' + right.title) + inv = -1 if sideSize.inv(lineDirection) else 1 includeStart = sideSize.includeStart(lineDirection) - opositeEnabled = boxSide.opositeEnabled(lineDirection) - print("boxSide: " + str(boxSide)) - print("opositeEnabled: " + str(opositeEnabled)) - g = VGen(start, lineDirection) if includeStart: - g.moveX(cfg.thickness) + g.moveX(left.thickness) - if opositeEnabled: + if opposite.enabled: g.moveX(notchConfig.size) for i in range(0, notchConfig.count): - g.moveY(-cfg.thickness * inv) + g.moveY(-opposite.thickness * inv) g.moveX(notchConfig.size) - g.moveY(cfg.thickness * inv) + g.moveY(opposite.thickness * inv) g.moveX(notchConfig.size) else: - print("innerLength: " + str(innerLength)) g.moveX(innerLength) if includeStart: - g.moveX(cfg.thickness) + g.moveX(right.thickness) return g.list def makeBoxSide(cfg, boxSide): + print('makeBoxSide: boxSide=' + boxSide.title) + notchConfigX = cfg.notchConfig(boxSide.directionX) notchConfigY = cfg.notchConfig(boxSide.directionY) innerLengthX = cfg.innerLength(boxSide.directionX) innerLengthY = cfg.innerLength(boxSide.directionY) + opposite = boxSide.opposite(LineDirection.right) + left = boxSide.left(LineDirection.right) + # Offset the start so that the inner area starts at (0, 0) if boxSide.sideSize is SideSize.large: - start = Vector(-cfg.thickness, cfg.thickness, 0) + start = Vector(-left.thickness, opposite.thickness, 0) elif boxSide.sideSize is SideSize.medium: - start = Vector(-cfg.thickness, 0, 0) + start = Vector(-left.thickness, 0, 0) else: start = Vector(0, 0, 0) v = [start] - top = line(cfg, v[-1], LineDirection.right, boxSide, notchConfigX, innerLengthX) + top = line(cfg, v[-1], boxSide, LineDirection.right, notchConfigX, innerLengthX) v.extend(top) - left = line(cfg, v[-1], LineDirection.down, boxSide, notchConfigY, innerLengthY) + left = line(cfg, v[-1], boxSide, LineDirection.down, notchConfigY, innerLengthY) v.extend(left) - bottom = line(cfg, v[-1], LineDirection.left, boxSide, notchConfigX, innerLengthX) + bottom = line(cfg, v[-1], boxSide, LineDirection.left, notchConfigX, innerLengthX) v.extend(bottom) - right = line(cfg, v[-1], LineDirection.up, boxSide, notchConfigY, innerLengthY) + right = line(cfg, v[-1], boxSide, LineDirection.up, notchConfigY, innerLengthY) v.extend(right) return v @@ -312,16 +332,22 @@ def makeParts(doc, cfg): sep = cfg.outerWidth * 0.05 - def m(rotX, rotY, moveX, moveY, moveZ): - if rotX and rotY: - p = Placement(Vector(0, 0, 0), Rotation(Vector(1, 1, 1), 120)) - m = p.toMatrix() - else: - m = Matrix() - if rotX: - m.rotateX(math.radians(90)) - if rotY: - m.rotateY(math.radians(90)) + def m(rotX, rotY, rotZ, moveX, moveY, moveZ): + m = Matrix() + if rotX != 0: + x = Matrix() + x.rotateX(math.radians(rotX)) + m = m * x + + if rotY != 0: + y = Matrix() + y.rotateY(math.radians(rotY)) + m = m * y + + if rotZ != 0: + z = Matrix() + z.rotateZ(math.radians(rotZ)) + m = m * z v = Vector(cfg.innerWidth, cfg.innerDepth, cfg.innerHeight) v.scale(moveX, moveY, moveZ) @@ -331,17 +357,17 @@ def makeParts(doc, cfg): # 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) + SideType.front: m( 90, 0, 0, -0.5, -0.5, 0.5), + SideType.back: m( 90, 180, 0, 0.5, 0.5, 0.5), + SideType.left: m( 90, 90, 0, -0.5, -0.5, 0.5), + SideType.right: m( 90, 90, 0, 0.5, -0.5, 0.5), + SideType.top: m( 0, 0, 0, -0.5, 0.5, 0.5), + SideType.bottom: m( 0, 180, 180, -0.5, -0.5, -0.5) } # yapf: enable for sideType in SideType: - boxSide = cfg.side(sideType) + boxSide = cfg.sideForType(sideType) if not boxSide.enabled: continue @@ -358,11 +384,11 @@ def makeParts(doc, cfg): def makeExtrudes(doc, parts, cfg): def makeExtrude(doc, cfg, sideType): - side = cfg.side(sideType) + side = cfg.sideForType(sideType) part = parts[sideType] extrude = doc.addObject("Part::Extrusion", side.title + "_Extrude") extrude.Base = part - extrude.Dir = side.extrudeVector * cfg.thickness + extrude.Dir = side.extrudeVector * side.thickness extrude.Solid = (True) extrude.TaperAngle = (0) extrude.Label = side.title + ' Extrude' @@ -390,6 +416,7 @@ def makeCrossSections(doc, cfg, extrudes): wires = list() shape = extrude.Shape + raise Exception('cfg.thickness is de-implemented') for i in shape.slice(Base.Vector(0, 0, 1), cfg.thickness / 2): wires.append(i) -- cgit v1.2.3