#Animate FreeCAD's Assembly2 


import FreeCAD
import FreeCADGui
import time
import PySide
from PySide import QtGui, QtCore
from a2p_solversystem import solveConstraints







class Go:

	def runloop(self):
		
		ang =FreeCAD.getDocument(FreeCAD.ActiveDocument.Name).getObject(g.constraintname).angle #Read the existing angle

		for i in range(0,g.numOfLoops):			#Start loop
			ang = float(ang)+float(g.changeby)	#Add the degrees to change to existing angle.
			if ang >= 359:						#If the input is greater than 360 it quits
				ang=ang-float(359)
			if ang <= -359:						#If the input is less than -360 it quits
				ang=ang+float(359)
			if ang == 0:						#If the input is less than 0 ?it quits
				ang=ang+float(1)
			print ang
			FreeCAD.getDocument(FreeCAD.ActiveDocument.Name).getObject(g.constraintname).angle = ang   #Rotate 
			doc = FreeCAD.activeDocument()
			solveConstraints(doc)
			doc.recompute()
			solveConstraints(doc)

			
			FreeCAD.ActiveDocument.recompute()				#Recompute
			FreeCADGui.updateGui()							#Update the graphics.
			#time.sleep(1000)                                 #This give time for click detect

class globals:

	changeby = 5		#Enter the angle by degrees  to increment by
	numOfLoops = 50		#Enter number of times you want to run the loop
	constraintname= "need constraint"
	constraint = None
g=globals()





class CreateRotateForm(QtGui.QWidget):
#class CreateForm(QtGui.QMainWindow):
	#Create main form
	#def __init__(self):
	def __init__(self):
		super(CreateRotateForm, self).__init__()
		self.initUI()

	def initUI(self):
		self.setGeometry(1000, 200, 410, 175)#xy,wl						fff
		self.setWindowTitle('Animate A2plus  FeeCAD Ver0.17  Python Ver2.7.15  Windows10.')
		self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)

		
		self.lbRev = QtGui.QLabel(' Animate Rotations \n Rev2019-01-15 for A2+ Dec 2018 release.',self)
		self.lbRev.setFixedWidth(220)
		self.lbRev.move(5, 5)

		self.txbConstraint = QtGui.QLineEdit(self)
		self.txbConstraint.setText('Needs an a-n-g in name')
		self.txbConstraint.setFixedWidth(200)
		self.txbConstraint.setFixedHeight(20)
		self.txbConstraint.move(5, 40)


		self.btnselectConstraint = QtGui.QPushButton('Select Constraint', self)
		self.btnselectConstraint.move(210, 40)
		self.btnselectConstraint.setFixedWidth(100)
		self.btnselectConstraint.setFixedHeight(20)
		self.btnselectConstraint.clicked.connect(lambda:self.selconstraint())

		self.lbSel = QtGui.QLabel(' Select Constraint.',self)
		self.lbSel.setFixedWidth(220)
		self.lbSel.move(5, 20)






		self.txbDegreeIncrement = QtGui.QLineEdit(self)
		self.txbDegreeIncrement.setText('10')
		self.txbDegreeIncrement.setFixedWidth(50)
		self.txbDegreeIncrement.setFixedHeight(20)
		self.txbDegreeIncrement.move(5, 65)

		self.lbInc = QtGui.QLabel('Increment in degress.',self)
		self.lbInc.setFixedWidth(220)
		self.lbInc.move(80, 65)


		self.txbTimesToLoop = QtGui.QLineEdit(self)
		self.txbTimesToLoop.setText('100')
		self.txbTimesToLoop.setFixedWidth(50)
		self.txbTimesToLoop.setFixedHeight(20)
		self.txbTimesToLoop.move(5, 90)
		
		self.lbInc = QtGui.QLabel('Times to loop.',self)
		self.lbInc.setFixedWidth(100)
		self.lbInc.move(110, 90)

		self.btnStartRotate = QtGui.QPushButton('Start Rotate', self)
		self.btnStartRotate.move(100, 120)
		self.btnStartRotate.setFixedWidth(100)
		self.btnStartRotate.setFixedHeight(20)
		self.btnStartRotate.clicked.connect(lambda:self.StartRotate())
			   
		self.txbError = QtGui.QLineEdit(self)
		self.txbError.setText('This selection is not an angle constraint. Please try again.')
		self.txbError.setFixedWidth(200)
		self.txbError.setFixedHeight(50)
		self.txbError.move(5, 100)
		self.txbError.hide()

	def showform(self):
		print('here to show')
		self.show()

	def StartRotate(self):
		if 'ang' not in self.txbConstraint.text():
			self.txbError.setText('This selection is not an angle constraint. Please try again.')
			
			self.txbError.show()

			return
		g.changeby = float(self.txbDegreeIncrement.text())		#Enter the angle by degrees  to increment by
		g.numOfLoops = int(self.txbTimesToLoop.text())		#Enter number of times you want to run the loop
		g.constraintname= self.txbConstraint.text()			#"angleConstraint01_mirror"
		Go().runloop()

	def selconstraint(self):
		print('Selecting')
		self.txbError.hide()
		selObv.SelObserverOn()

		
	
	def getconName(self):
		selObv.SelObserverOff()
		print('here to save')
		ConstraintName = FreeCADGui.Selection.getSelection()[0].Name
		self.txbConstraint.setText(ConstraintName)
		if 'ang' not in ConstraintName:
			self.txbError.setText('This selection is not an angle constraint. Please try again.')
			
			self.txbError.show()

			return
		
		

	def closeEvent(self, event):
		try:
			
			selObv.SelObserverOff()
			self.close()
		except Exception as e:
			print ("Err on Finding " + e.message)


class SelObserver:
#SelObserver().SelObserverOn()
	
	mouse ='off'
	def SelObserverOn(self):
		if self.mouse=='off':
			print('selobv on')
			FreeCADGui.Selection.addObserver(selObv)
			self.mouse='on'
	def SelObserverOff(self):
		if self.mouse=='on':
			FreeCADGui.Selection.removeObserver(selObv)
			#print('selobv off')
			self.mouse='off'

	def setPreselection(self,doc,obj,sub):				# Preselection object
		return

	def addSelection(self,doc,obj,sub,pnt):			   # Selection object
		pass
	def removeSelection(self,doc,obj,sub):				# Delete the selected object
		pass

	def setSelection(self,doc):	
		print('selc')
		if len(FreeCADGui.Selection.getSelection()) >0:
			form.getconName()
selObv =SelObserver()		

form = CreateRotateForm()

def main():	
	
	
	form.showform()

print('Run')
main()