#
# Copyright © 2020 MSC.Software Corporation
#  
# Permission to use Scripts
#  
# Permission is granted to MSC.Software customers with valid licenses of the MSC Apex™ software 
# to use script files posted on this page (collectively “Scripts”) solely for customers’ own authorized 
# internal use in conjunction with licensed use of the MSC Apex software.  
# No other use is permitted.  No license to MSC Apex or other software is granted under this Permission.
#  
# SCRIPTS ARE PROVIDED ON AN "AS-IS" BASIS, WITH NO WARRANTIES OF ANY KIND.  USE OF THE SCRIPTS IS AT CUSTOMER'S SOLE RISK.
#  
# IN NO EVENT SHALL MSC.SOFTWARE CORPORATION, ITS AFFILIATES, SUBSIDIARIES, SUPPLIERS, AGENTS, REPRESENTATIVES OR EMPLOYEES BE LIABLE FOR ANY 
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, TORT, 
# STRICT LIABILITY, OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION USE OF THE SCRIPTS, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
#  
# Customer shall indemnify, defend and hold MSC.Software Corporation, its affiliates, subsidiaries, suppliers, agents, 
# employees or representatives (collectively, “Indemnified Parties”) harmless from and against any third-party claims 
# brought against Indemnified Parties rising out of or in connection with Customer’s use of the Scripts.
#
import apex
import os

def highlight_stuff(item,r,g,b):
	col1=apex.ColorRGB(r,g,b)					
	#if surf.getVisibility() == 1:		
	item.highlight(col1,2,3)

def getAllParts():
	#get and return all solids from current model
	model_1 = apex.currentModel()
	parts = model_1.getParts(True)
	return parts

def getAllSolids():
	solids = apex.entityCollection()
	parts=getAllParts()
	for part in parts:
		solids += part.getSolids()
	return solids

def getAllSurfs():
	surfs = apex.entityCollection()
	parts=getAllParts()
	for part in parts:
		surfs += part.getSurfaces()
	return surfs







def main(dict={}):

	apex.disableShowOutput()
	_selected = apex.selection.getCurrentSelection()
	ret_dict={}
	
	

	# clear highlights
	solids= getAllSolids()
	surfs=getAllSurfs()

	for solid in solids:
		solid.clearHighlight()
		cells=solid.getCells()
		for cell in cells:
			cell.clearHighlight()
	for surf in surfs:
		surf.clearHighlight()


	if len(_selected) == 0:
		print("Please Select a Mesh Independent Tie")
		ret_dict["body1"]="None"
		ret_dict["body2"]="None"
		return ret_dict
		
	elif len(_selected) > 1 :
		print("Multiple ties selected")
		ret_dict["body1"]="Multiple Selections"
		ret_dict["body2"]="Multiple Selections"
		for item in _selected:
			print(item.getName())
			bodies=item.getTarget()
			highlight_stuff(bodies[0],255,165,0)
			highlight_stuff(bodies[1],255,165,0)

		
	
	# check if selected type is a solid body, a surface body or a mesh indep tie
	else:
		for item in _selected:
			print(item.getName())

			bodies=item.getTarget()
			highlight_stuff(bodies[0],255,0,0)
			highlight_stuff(bodies[1],0,255,0)

		try:
			ret_dict["body1"]=bodies[0].getParent().getName() + " / " + bodies[0].getName()
			ret_dict["body2"]=bodies[1].getParent().getName() + " / " + bodies[1].getName()
		except:
			ret_dict["body1"]="None"
			ret_dict["body2"]="None"
	
	# for all selected contact bodies
	for item in _selected:
		attrib_names_common=['contactType','Tolerance','Bias']
		tmp_attributes=item.getUserAttributes(attrib_names_common)
		for attrib in tmp_attributes:
			ret_dict[attrib.getAttributeName()]=attrib.getStringValue()
			
		
		try:
			print("Contact Type is: ", ret_dict['contactType'])
		except:
			print("No contacts set, defaults apply")
			return ret_dict
			
		
		if ret_dict['contactType']=="Touch":
			attrib_names_touch=['Friction','StressFreeInitial']
			tmp_attributes=item.getUserAttributes(attrib_names_touch)
			for attrib in tmp_attributes:
				ret_dict[attrib.getAttributeName()]=attrib.getStringValue()
		elif ret_dict['contactType']=="Glue":
			attrib_names_glue=['RetainGapOverlaps']
			tmp_attributes=item.getUserAttributes(attrib_names_glue)
			for attrib in tmp_attributes:
				ret_dict[attrib.getAttributeName()]=attrib.getStringValue()
	
	
	#attrib_names=['contactType','Tolerance','Bias','Friction']
	#attrib_names=['contactType','Tolerance','Bias']


	#tmp_attributes=item.getUserAttributes(attrib_names)




	#for attrib in tmp_attributes:
	#	ret_dict[attrib.getAttributeName()]=attrib.getStringValue()

	print(ret_dict)

	return ret_dict