# Path: importerUtils.py
# def logInfo(msg, *args):
# 	if (__prmPrefOW__.GetBool("checkLogging", False)): _log("logInfo",    Console.PrintMessage, msg, args)
# 
# def getIconPath(fileName):
# 	return os.path.join(os.path.dirname(__file__), "Resources", "icons", fileName)
# 
# def getTableValue(table, col, row):
# 	try:
# 		return table.get(getCellRef(col, row))
# 	except:
# 		return None
# 
# def setTableValue(table, col, row, val):
# 	if (type(val) == str):
# 		table.set(getCellRef(col, row), val)
# 	else:
# 		if ((sys.version_info.major <= 2) and (type(val) == unicode)):
# 			table.set(getCellRef(col, row), "%s" %(val.encode("utf8")))
# 		else:
# 			table.set(getCellRef(col, row), str(val))
# 
# def logInfo(msg, *args):
# 	if (__prmPrefOW__.GetBool("checkLogging", False)): _log("logInfo",    Console.PrintMessage, msg, args)
# 
# def logWarning(msg, *args):
# 	if (__prmPrefOW__.GetBool("checkWarning", False)): _log("logWarning", Console.PrintWarning, msg, args)
# 
# def logError(msg, *args):
# 	if (__prmPrefOW__.GetBool("checkError", True)):    _log("logError",   Console.PrintError,   msg, args)
# 
# def getCellRef(col, row):
# 	if (isString(col)):
# 		return u"%s%d" %(col, row)
# 	return u"%s%d" %(int2col(col), row)
# 
# def setTableValue(table, col, row, val):
# 	if (type(val) == str):
# 		table.set(getCellRef(col, row), val)
# 	else:
# 		if ((sys.version_info.major <= 2) and (type(val) == unicode)):
# 			table.set(getCellRef(col, row), "%s" %(val.encode("utf8")))
# 		else:
# 			table.set(getCellRef(col, row), str(val))
# 
# def calcAliasname(name):
# 	alias = name.replace(':', '_')
# 	if (IS_CELL_REF.search(name)):
# 		return '%s_' %(alias)
# 	# 45, 48..57, 65..90, 97..122
# 	result = ''
# 	for c in alias:
# 		i = ord(c)
# 		if (i == 45) or (i > 47 and i < 58) or (i > 64 and i < 91) or (i > 96 and i < 123):
# 			result += c
# 		else:
# 			result += '_'
# 	if (result[0] == '_'):
# 		return 'd' + result
# 	return result
# 
# Path: importerClasses.py
# class ParameterTableModel(TableModel):
# 	def __init__(self, parent, mylist, *args):
# 		TableModel.__init__(self, parent, mylist, ['Variant', 'Source', 'Property', 'Parameter', 'Value', 'Units'], *args)
# 	def flags(self, index):
# 		if (index.column() == 0):
# 			return Qt.ItemIsEnabled | Qt.ItemIsEditable | Qt.ItemIsUserCheckable
# 		if (index.column() in [1, 2, 5]): # make object's name and property and unit column read only!
# 			return Qt.ItemIsEnabled
# 		return Qt.ItemIsEnabled |Qt.ItemIsEditable
# 
# class VariantTableModel(TableModel):
# 	def __init__(self, parent, values, *args):
# 		TableModel.__init__(self, parent, values[1:], values[0], *args)
# 	def flags(self, index):
# 		return Qt.ItemIsEnabled | Qt.ItemIsEditable
# 
# Path: importerConstants.py
# DIR_X  = VEC(1, 0, 0)
# 
# DIR_Y  = VEC(0, 1, 0)
# 
# DIR_Z  = VEC(0, 0, 1)


import os, re, sys, Part, FreeCAD, FreeCADGui
from importerUtils   import logInfo, getIconPath, getTableValue, setTableValue, logInfo, logWarning, logError, getCellRef, setTableValue, calcAliasname
from FreeCAD         import Vector as VEC
from importerClasses import ParameterTableModel, VariantTableModel
from math            import degrees, radians, pi, sqrt, cos, sin, atan
from PySide.QtCore   import *
from PySide.QtGui    import *
from importerConstants import DIR_X, DIR_Y, DIR_Z
				for c, constraint in enumerate(obj.Constraints):
					if (constraint.Type in DIM_CONSTRAINTS):
						prp = 'Constraints[%d]' %(c)
						value = getExpression(obj, prp)
						if (value is None):
							value = constraint.Value
							if (constraint.Type == 'Angle'):
								value = degrees(value)
						values.append([False, obj.Name, prp, 'd_%d' %(len(values)), str(value), DIM_CONSTRAINTS[constraint.Type]])
			else:
				for prp in obj.PropertiesList:
					if (obj.getTypeIdOfProperty(prp) in XPR_PROPERTIES):
						value = getExpression(obj, prp)
						if (value is None):
							value = getattr(obj, prp)
							if (hasattr(value, 'Value')):
								value = value.Value
						values.append([False, obj.Name, prp, 'd_%d' %(len(values)), str(value), XPR_PROPERTIES[obj.getTypeIdOfProperty(prp)]])
	return values

def getParametersValues(doc):
	table  = None
	for t in doc.getObjectsByLabel('Parameters'):
		if (t.TypeId == 'Spreadsheet::Sheet'):
			table = t
			break
	if (table is None):
		return None, None, True
	if ((table.get('A1') != 'Parameter') or (table.get('B1') != 'Value')):
		logWarning("Spreadsheet 'Parameters' doesn't meet layout constraints to serve as parameters table!")

# Path: importerUtils.py
# def logInfo(msg, *args):
# 	if (__prmPrefOW__.GetBool("checkLogging", False)): _log("logInfo",    Console.PrintMessage, msg, args)
# 
# def getIconPath(fileName):
# 	return os.path.join(os.path.dirname(__file__), "Resources", "icons", fileName)
# 
# def getTableValue(table, col, row):
# 	try:
# 		return table.get(getCellRef(col, row))
# 	except:
# 		return None
# 
# def setTableValue(table, col, row, val):
# 	if (type(val) == str):
# 		table.set(getCellRef(col, row), val)
# 	else:
# 		if ((sys.version_info.major <= 2) and (type(val) == unicode)):
# 			table.set(getCellRef(col, row), "%s" %(val.encode("utf8")))
# 		else:
# 			table.set(getCellRef(col, row), str(val))
# 
# def logInfo(msg, *args):
# 	if (__prmPrefOW__.GetBool("checkLogging", False)): _log("logInfo",    Console.PrintMessage, msg, args)
# 
# def logWarning(msg, *args):
# 	if (__prmPrefOW__.GetBool("checkWarning", False)): _log("logWarning", Console.PrintWarning, msg, args)
# 
# def logError(msg, *args):
# 	if (__prmPrefOW__.GetBool("checkError", True)):    _log("logError",   Console.PrintError,   msg, args)
# 
# def getCellRef(col, row):
# 	if (isString(col)):
# 		return u"%s%d" %(col, row)
# 	return u"%s%d" %(int2col(col), row)
# 
# def setTableValue(table, col, row, val):
# 	if (type(val) == str):
# 		table.set(getCellRef(col, row), val)
# 	else:
# 		if ((sys.version_info.major <= 2) and (type(val) == unicode)):
# 			table.set(getCellRef(col, row), "%s" %(val.encode("utf8")))
# 		else:
# 			table.set(getCellRef(col, row), str(val))
# 
# def calcAliasname(name):
# 	alias = name.replace(':', '_')
# 	if (IS_CELL_REF.search(name)):
# 		return '%s_' %(alias)
# 	# 45, 48..57, 65..90, 97..122
# 	result = ''
# 	for c in alias:
# 		i = ord(c)
# 		if (i == 45) or (i > 47 and i < 58) or (i > 64 and i < 91) or (i > 96 and i < 123):
# 			result += c
# 		else:
# 			result += '_'
# 	if (result[0] == '_'):
# 		return 'd' + result
# 	return result
# 
# Path: importerClasses.py
# class ParameterTableModel(TableModel):
# 	def __init__(self, parent, mylist, *args):
# 		TableModel.__init__(self, parent, mylist, ['Variant', 'Source', 'Property', 'Parameter', 'Value', 'Units'], *args)
# 	def flags(self, index):
# 		if (index.column() == 0):
# 			return Qt.ItemIsEnabled | Qt.ItemIsEditable | Qt.ItemIsUserCheckable
# 		if (index.column() in [1, 2, 5]): # make object's name and property and unit column read only!
# 			return Qt.ItemIsEnabled
# 		return Qt.ItemIsEnabled |Qt.ItemIsEditable
# 
# class VariantTableModel(TableModel):
# 	def __init__(self, parent, values, *args):
# 		TableModel.__init__(self, parent, values[1:], values[0], *args)
# 	def flags(self, index):
# 		return Qt.ItemIsEnabled | Qt.ItemIsEditable
# 
# Path: importerConstants.py
# DIR_X  = VEC(1, 0, 0)
# 
# DIR_Y  = VEC(0, 1, 0)
# 
# DIR_Z  = VEC(0, 0, 1)


# Path: InventorViewProviders.py
import os, re, sys, Part, FreeCAD, FreeCADGui
from importerUtils   import logInfo, getIconPath, getTableValue, setTableValue, logInfo, logWarning, logError, getCellRef, setTableValue, calcAliasname
from FreeCAD         import Vector as VEC
from importerClasses import ParameterTableModel, VariantTableModel
from math            import degrees, radians, pi, sqrt, cos, sin, atan
from PySide.QtCore   import *
from PySide.QtGui    import *
from importerConstants import DIR_X, DIR_Y, DIR_Z

				for c, constraint in enumerate(obj.Constraints):
					if (constraint.Type in DIM_CONSTRAINTS):
						prp = 'Constraints[%d]' %(c)
						value = getExpression(obj, prp)
						if (value is None):
							value = constraint.Value
							if (constraint.Type == 'Angle'):
								value = degrees(value)
						values.append([False, obj.Name, prp, 'd_%d' %(len(values)), str(value), DIM_CONSTRAINTS[constraint.Type]])
			else:
				for prp in obj.PropertiesList:
					if (obj.getTypeIdOfProperty(prp) in XPR_PROPERTIES):
						value = getExpression(obj, prp)
						if (value is None):
							value = getattr(obj, prp)
							if (hasattr(value, 'Value')):
								value = value.Value
						values.append([False, obj.Name, prp, 'd_%d' %(len(values)), str(value), XPR_PROPERTIES[obj.getTypeIdOfProperty(prp)]])
	return values

def getParametersValues(doc):
	table  = None
	for t in doc.getObjectsByLabel('Parameters'):
		if (t.TypeId == 'Spreadsheet::Sheet'):
			table = t
			break
	if (table is None):
		return None, None, True
	if ((table.get('A1') != 'Parameter') or (table.get('B1') != 'Value')):
		logWarning("Spreadsheet 'Parameters' doesn't meet layout constraints to serve as parameters table!")

		logWarning("First row must be 'Parameter', 'Value', 'Unit', 'Source' - creating new one.")