diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2016-05-30 17:08:08 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2016-05-30 17:08:08 +0200 |
commit | fd1626b397afe407f68de99486af86beae9625be (patch) | |
tree | 97370129c0b413fbcae24475d11d157acc18fb51 | |
parent | c061d1b281b219019592c543be9ea96381258c96 (diff) | |
download | freecad-boxer-fd1626b397afe407f68de99486af86beae9625be.tar.gz freecad-boxer-fd1626b397afe407f68de99486af86beae9625be.tar.bz2 freecad-boxer-fd1626b397afe407f68de99486af86beae9625be.tar.xz freecad-boxer-fd1626b397afe407f68de99486af86beae9625be.zip |
o Fixed bugs causing the object to be smaller than expected.
o Assembling the sides propertly. Required changes in how the extrusions was done.
o Disabling the cross section generation for now. It is only useful if you don't want to make any changes to the box.
-rw-r--r-- | Boxer.py | 148 |
1 files changed, 87 insertions, 61 deletions
@@ -4,11 +4,13 @@ from FreeCAD import Base, Vector, Matrix import Part from enum import Enum +import math +# These names are silly, should be X, Y, Z instead class MeasurementDirection(Enum): - width = 1 - height = 2 - depth = 3 + width = 1 # X + height = 2 # Z + depth = 3 # Y # X and Y rotation factors class LineDirection(Enum): @@ -26,7 +28,7 @@ class LineDirection(Enum): # Configuration for each line in a box side class SideConfig(Enum): large = (False, True, False, True, False, True, False, True) - medium = (False, False, True, True, False, False, True, True) + medium = (True, True, False, False, True, True, False, False) small = (True, False, True, False, True, False, True, False) def __init__(self, rightInv, rightIncludeStart, downInv, downIncludeStart, leftInv, leftIncludeStart, upInv, upIncludeStart): @@ -64,17 +66,19 @@ class SideConfig(Enum): raise Exception("Unknown direction: " + repr(direction)) class BoxSide(Enum): - front = (MeasurementDirection.width, MeasurementDirection.height, SideConfig.large) - back = (MeasurementDirection.width, MeasurementDirection.height, SideConfig.large) - left = (MeasurementDirection.depth, MeasurementDirection.height, SideConfig.medium) - right = (MeasurementDirection.depth, MeasurementDirection.height, SideConfig.small) - top = (MeasurementDirection.width, MeasurementDirection.depth, SideConfig.small) - bottom = (MeasurementDirection.width, MeasurementDirection.depth, SideConfig.medium) - - def __init__(self, directionX, directionY, sideConfig): + 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)) + + def __init__(self, title, directionX, directionY, sideConfig, extrudeVector): + self.title = title self.directionX = directionX self.directionY = directionY self.sideConfig = sideConfig + self.extrudeVector = Vector(extrudeVector) class NotchConfig(object): def __init__(self, count, size): @@ -87,8 +91,9 @@ class NotchConfig(object): class BoxCfg(object): def __init__(self): self._thickness = 10 -# self._notches = 10 self._notchSize = 10 + self.generateExtrudes = True + self.generateCrossSections = False # Not very useful so off by default self.outerDimmensions(100, 100, 100) self.calculate() @@ -138,7 +143,7 @@ class BoxCfg(object): def createNotchConfig(self, sideLength): count = sideLength / self._notchSize - size = sideLength / (count * 2 + 1) + size = float(sideLength) / float(count * 2 + 1) return NotchConfig(count, size) def calculate(self): @@ -161,13 +166,13 @@ class BoxCfg(object): class VGen(object): def __init__(self, current, dir): - self.dir = dir - self.current = current + self.dir = dir self.list = [] - def move(self, x, y, z): - self.current = self.current.add(Vector(x, y, z)) + def move(self, x, y, z = 0): + pos = Vector(x, y, z) + self.current = self.current.add(pos) self.list.append(self.current) def moveX(self, value): @@ -202,7 +207,15 @@ def makeBoxSide(cfg, boxSide): notchConfigX = cfg.notchConfig(boxSide.directionX) notchConfigY = cfg.notchConfig(boxSide.directionY) - v = [Vector(0, 0, 0)] + # Offset the start so that the inner area starts at (0, 0) + if boxSide.sideConfig is SideConfig.large: + start = Vector(-cfg.thickness, cfg.thickness, 0) + elif boxSide.sideConfig is SideConfig.medium: + start = Vector(-cfg.thickness, 0, 0) + else: + start = Vector(0, 0, 0) + + v = [start] top = line(cfg, v[-1], LineDirection.right, boxSide.sideConfig, notchConfigX); v.extend(top) left = line(cfg, v[-1], LineDirection.down, boxSide.sideConfig, notchConfigY); @@ -215,66 +228,72 @@ def makeBoxSide(cfg, boxSide): def makeBox(doc, cfg): objects = {} + + sep = cfg.outerWidth * 0.05 + + def m(rotX, rotY, moveX, moveY, moveZ): + v = Vector(cfg.innerWidth, cfg.innerDepth, cfg.innerHeight) + v.scale(moveX, moveY, moveZ) + + m = Matrix() + m.rotateX(math.radians(rotX)) + m.rotateY(math.radians(rotY)) + m.move(v) + return m + # front back left # right top bottom - # TODO: replace m.move() with a proper translation matrix so it looks assembled - - m = Matrix() - m.move(cfg.outerWidth * -0.55, 0, 0) s = Part.makePolygon(makeBoxSide(cfg, BoxSide.front)) - s.transformShape(m) + s.transformShape(m(90, 0, -0.5, -0.5, 0.5)) front = doc.addObject("Part::Feature", "Front") front.Shape = s - m = Matrix() - m.move(cfg.outerWidth * 0.55, 0, 0) s = Part.makePolygon(makeBoxSide(cfg, BoxSide.back)) - s.transformShape(m) + s.transformShape(m(90, 0, -0.5, 0.5, 0.5)) back = doc.addObject("Part::Feature", "Back") back.Shape = s - m = Matrix() - m.move(cfg.outerWidth * 1.60, 0, 0) l = Part.makePolygon(makeBoxSide(cfg, BoxSide.left)) - l.transformShape(m) + l.transformShape(m(0, 90, -0.5, 0.5, 0.5)) left = doc.addObject("Part::Feature", "Left") left.Shape = l - m = Matrix() - m.move(cfg.outerWidth * -0.55, cfg.outerWidth * -1.1, 0) l = Part.makePolygon(makeBoxSide(cfg, BoxSide.right)) - l.transformShape(m) + l.transformShape(m(0, 90, 0.5, 0.5, 0.5)) right = doc.addObject("Part::Feature", "Right") right.Shape = l - m = Matrix() - m.move(cfg.outerWidth * 0.55, cfg.outerWidth * -1.1, 0) l = Part.makePolygon(makeBoxSide(cfg, BoxSide.top)) - l.transformShape(m) + l.transformShape(m(0, 0, -0.5, 0.5, 0.5)) top = doc.addObject("Part::Feature", "Top") top.Shape = l - m = Matrix() - m.move(cfg.outerWidth * 1.60, cfg.outerWidth * -1.1, 0) l = Part.makePolygon(makeBoxSide(cfg, BoxSide.bottom)) - l.transformShape(m) + l.transformShape(m(0, 0, -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, name, part): - extrude = doc.addObject("Part::Extrusion", name + "_Extrude") + def makeExtrude(doc, cfg, side, part): + extrude = doc.addObject("Part::Extrusion", side.title + "_Extrude") extrude.Base = part - extrude.Dir = (0, 0, cfg.thickness) + extrude.Dir = side.extrudeVector * cfg.thickness extrude.Solid = (True) extrude.TaperAngle = (0) - extrude.Label = name + ' Extrude' + extrude.Label = side.title + ' Extrude' partView = part.ViewObject extrudeView = extrude.ViewObject @@ -288,12 +307,20 @@ def makeExtrudes(doc, cfg): return extrude objects = {} - objects['front'] = makeExtrude(doc, cfg, "Front", doc.Front) - objects['back'] = makeExtrude(doc, cfg, "Back", doc.Back) - objects['left'] = makeExtrude(doc, cfg, "Left", doc.Left) - objects['right'] = makeExtrude(doc, cfg, "Right", doc.Right) - objects['top'] = makeExtrude(doc, cfg, "Top", doc.Top) - objects['bottom'] = makeExtrude(doc, cfg, "Bottom", doc.Bottom) + 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 + return objects def makeCrossSections(doc, cfg, extrudes): @@ -320,20 +347,19 @@ def make(doc, cfg): # Cretate the underlying parts parts = makeBox(doc, cfg) - # Extrude each part - extrudes = makeExtrudes(doc, cfg) - - # Put all extrudes in a group - box = doc.addObject("App::DocumentObjectGroup", "Box") - for e in extrudes.values(): - box.addObject(e) + if cfg.generateExtrudes: + extrudes = makeExtrudes(doc, cfg) + + box = doc.addObject("App::DocumentObjectGroup", "Box") + for e in extrudes.values(): + box.addObject(e) doc.recompute() - # Create cross sections for export - crossSections = makeCrossSections(doc, cfg, extrudes) - crossSectionsGroup = doc.addObject("App::DocumentObjectGroup", "Cross Sections") - [crossSectionsGroup.addObject(cs) for cs in crossSections.values()] + if cfg.generateCrossSections: + crossSections = makeCrossSections(doc, cfg, extrudes) + crossSectionsGroup = doc.addObject("App::DocumentObjectGroup", "Cross Sections") + [crossSectionsGroup.addObject(cs) for cs in crossSections.values()] def removeEverything(doc): def rm(name): |