#!/usr/bin/python3
#**************************************************************************
# Copyright 2016 Rudy du Preez <rudy@asmsa.co.za>
#
# 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.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#**************************************************************************

#--------------------------------------------------------------------------
# Visualization model of the Hermle mill, modified to 5-axis
# with rotary axes B and C added, with moving spindle head
# and rotary axis offsets
#--------------------------------------------------------------------------

from vismach import *
import hal
import math
import sys

c = hal.component("xyzbc-trt-gui")
c.newpin("table-x", hal.HAL_FLOAT, hal.HAL_IN)
c.newpin("saddle-y", hal.HAL_FLOAT, hal.HAL_IN)
c.newpin("spindle-z", hal.HAL_FLOAT, hal.HAL_IN)
c.newpin("tilt-b", hal.HAL_FLOAT, hal.HAL_IN)
c.newpin("rotate-c", hal.HAL_FLOAT, hal.HAL_IN)
c.newpin("z-offset", hal.HAL_FLOAT, hal.HAL_IN)
c.newpin("x-offset", hal.HAL_FLOAT, hal.HAL_IN)
c.newpin("tool-offset", hal.HAL_FLOAT, hal.HAL_IN)
c.ready()

for setting in sys.argv[1:]: exec(setting)

tooltip = Capture()
work = Capture()

tool = Collection([tooltip, CylinderZ(0, 0.2, 6, 3), CylinderZ(6, 3, 70, 3)])
tool = Translate([tool], 0, 0, -20)
tool = Color([1, 0, 0, 0], [tool])
tool = HalTranslate([tool], c, "tool-offset", 0, 0, -1)

spindle = Collection([Color([0, 0.5, 0.5, 0], [CylinderZ(0, 10, 20, 15)]), CylinderZ(20, 20, 135, 20)])
spindle = Color([0, 0.5, 0.5, 0], [spindle])
spindle = Collection([tool, spindle])
spindle = Translate([spindle], 0, 0, 20)

motor = Collection([Color([0, 0.5, 0.5, 0], [CylinderZ(135, 30, 200, 30)])])
motor = Translate([motor], 0, 60, 0)

head = Collection([spindle, Color([0, 0.5, 0.5, 0], [Box(-30, -30, 60, 30, 130, 135)]), motor])
head = Translate([head], 0, 50, -75)
head = HalRotate([head], c, "tilt-b", 1, 0, 1, 0)
head = Rotate([head], -30, 1, 0, 0)
head = Translate([head], 0, 0, 225)
head = HalTranslate([head], c, "table-x", 0, 0.57735027, 1) #0.57735027

ctable = Collection([CylinderX(-20, 100, 20, 100)])
c_IndicatorY = CylinderY(-100, 1, 100, 1)
c_IndicatorY = Translate([c_IndicatorY], 20, 0, 0)
c_IndicatorY = Color([0.5,0.5,0.5,1],[c_IndicatorY])
c_IndicatorZ = CylinderZ(-100, 1, 100, 1)
c_IndicatorZ = Translate([c_IndicatorZ], 20, 0, 0)
c_IndicatorZ = Color([0.5,0.5,0.5,1],[c_IndicatorZ])
ctable1 = Collection([ctable,c_IndicatorY,c_IndicatorZ])
ctable1 = HalRotate([ctable1], c, "rotate-c", 1, 1, 0, 0)
ctable1 = Color([1, 0, 1, 0], [ctable1])
ctable1 = Translate([ctable1], -600, 0, 0)

X_Slide = BoxCentered(300, 100, 1200)
X_Slide = Translate([X_Slide], 0, 150, 300)
X_Slide = Rotate([X_Slide], -30, 1, 0, 0)
Col    = Collection([head,X_Slide])
Col  = HalTranslate([Col], c, "spindle-z", 1, 0, 0)
Col  = HalTranslate([Col], c, "saddle-y", 0, 1, 0)

base = Collection([Box(-700, -200, -500, 700, 200, -200)])
base = Color([0, 1, 0, 0], [base])

cylinder_testx = CylinderX(-1000, 1, 1000, 1)
cylinder_testy = CylinderY(-1000, 1, 1000, 1)
cylinder_testz = CylinderZ(-1000, 1, 1000, 1)



model = Collection([ctable1,base,Col, work, cylinder_testx, cylinder_testy, cylinder_testz])

myhud = Hud()
myhud.show("XYZBC")

main(model, tooltip, work, size=800, hud=myhud, lat=-60, lon=25)
