#!/usr/bin/python
#
# hc.py
#
# Written 2015 by Werner Almesberger
# Copyright 2015 by Werner Almesberger
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#


import FreeCAD, Part
from FreeCAD import Base


# ----- Parameters ------------------------------------------------------------


x = 50.0	# X size
y = 40.0	# Y size

wall = 1.5	# wall thickness
gap = 0.2	# wall-to-PCB gap

z_base = 1.5	# thickness of bottom
z_wall = 5.0	# total height of walls, from Z = 0


# ----- Helper functions ------------------------------------------------------


def v(x, y, z):
	return Base.Vector(x, y, z)


def extrude_shape(shape, z):
	wire = Part.Wire(shape.Edges)
	face = Part.Face(wire)

	return face.extrude(v(0, 0, z))


def rect(x, y, z):
	bottom = Part.Line(v(0, 0, 0), v(x, 0, 0))
	top = Part.Line(v(0, y, 0), v(x, y, 0))
	left = Part.Line(v(0, 0, 0), v(0, y, 0))
	right = Part.Line(v(x, 0, 0), v(x, y, 0))

	s = Part.Shape([ bottom, top, left, right ])
	return extrude_shape(s, z)


def move(obj, x, y, z):
	obj.translate(v(x, y, z))


def visualize(shape, name, color, trans):
	obj = doc.addObject("Part::Feature", name)
	obj.Shape = shape
	obj.ViewObject.ShapeColor = color
	obj.ViewObject.Transparency = trans


# ----- Build the object ------------------------------------------------------


doc = FreeCAD.newDocument()

outside = rect(x + 2 * (wall + gap), y + 2 * (wall + gap), z_wall)
inside = rect(x, y, z_wall - z_base)
move(inside, wall + gap, wall + gap, z_base)
case = outside.cut(inside)

visualize(case, "Case", (0.7, 0.7, 0.7), 50)

case.exportStl("hc.stl")
