# ~ Matt 2016-07-01, revised 2016-07-10
# Revised 2016-07-16, hopefully still compatible with iter9.

# Not sure where to get FreeCAD's sin/cos functions, so use Python's.
import math

def vectorOfPoint(p):
  return FreeCAD.Vector(p.X, p.Y, p.Z)

def objByLabel(l):
  return App.ActiveDocument.getObjectsByLabel(l)[0]

def deg2rad(x): return x * math.pi / 180

# FreeCAD API is underdocumented... *grumble*
params_live = objByLabel('params_live')
params_staging = objByLabel('params_staging')
# params_staging may have additional intermediate properties that params_live doesn't need.
userProperties = [p for p in params_live.PropertiesList if params_live.getCellFromAlias(p) is not None]  # Bleh; proper API?
for p in userProperties:
	# Plain "get" gives us a Base.Quantity, which we can't set into another spreadsheet.
	# But "getContents" gives us the formula, which we don't want.  Instead use "str" on the Base.Quantity.
	#params_live.set(p, params_staging.getContents(params_staging.getCellFromAlias(p)))
	params_live.set(p, str(params_staging.get(p)))

# freecad-0.16-1.fc23 seems to have a bug when the components of an axis are defined by formulas:
# the formula evaluator sets the components one at a time and fights with the code that normalizes
# the vector, giving a wrong answer.  Instead, set the whole axis at once here.
for l in 'nozzle_bend_start_outer', 'nozzle_bend_start_inner':
	e = objByLabel(l)
	# Careful, do not read params_live, it has not been updated yet!
	e.Placement.Rotation.Axis = FreeCAD.Vector(-math.sin(deg2rad(params_staging.nozzle_bend_start_direction)), math.cos(deg2rad(params_staging.nozzle_bend_start_direction)), 0)

objByLabel('nozzle_bend_path').Points = [
	vectorOfPoint(objByLabel(pl))
	for pl in ['nozzle_bend_start_center_staging', 'nozzle_bend_start_control_staging', 'nozzle_bend_end_control_staging', 'nozzle_bend_end_center_staging']]

# Apparently this is not implicit.
App.ActiveDocument.recompute()
