# coding: utf-8
#
# 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.
#
# Macro created by MSC Apex MSC_Apex Hawk PRE - Version CLR:535240
# Macro created on Dec 13, 2017 at 10:08:12
# 
# Macro Name = edit contacts
# 
# Macro Description = Edit Apex mesh indep tie by adding custom attributes
# 
# Macro Hot Key = 
# 
# Initialize Apex environment for macro execution
# 
import apex
from apex.construct import Point3D, Point2D

import random
import os
####


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 exectuteOnExit(dict):
	apex.disableShowOutput()
	#apex.enableShowOutput()
	# 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()





def main(dict={}):

	apex.disableShowOutput()

	## Start Connect to GUI code ####

	_selected = apex.selection.getCurrentSelection()

	myModel = apex.currentModel()

	contactType=dict["contactType"]
	TouchTol=dict["TouchTol"]
	GlueTol=dict["GlueTol"]
	Friction=dict["Friction"]
	Bias=dict["Bias"]
	StressFreeInitial=dict["StressFreeInitial"]
	RetainGapOverlaps=dict["RetainGapOverlaps"]

	if len(_selected)  == 0:
		print("No contacts selected")
		return
	else:
		for item in _selected:
			print(item.getName())
			old_name=item.getName()
			if old_name[-2:]=="_T" or old_name[-2:] == "_G":
				old_name=old_name[:-2]
			
			new_name=old_name.replace("ä","ae").replace("ö","oe").replace("å","aa").replace("ü","ue").replace("ß","ss")
			new_name=new_name.replace("Ä","ÄE").replace("Ö","OE").replace("Å","AA").replace("Ü","UE")
			# rename special charcters to avoid issue with nastran export 
			#item.update( name = new_name)
			
			

			bodies=item.getTarget()

			item.addUserAttribute(userAttributeName ="contactType", stringValue = contactType )
			if contactType=="Touch":
				_contact_tol=TouchTol
				item.addUserAttribute(userAttributeName ="Friction", stringValue = Friction )
				item.addUserAttribute(userAttributeName ="StressFreeInitial", stringValue = StressFreeInitial )
				item.removeUserAttributes(userAttributeNames =  [ "RetainGapOverlaps" ] )  
				new_name+="_T"
			else:
				_contact_tol=GlueTol
				item.addUserAttribute(userAttributeName ="RetainGapOverlaps", stringValue = RetainGapOverlaps )
				item.removeUserAttributes(userAttributeNames =  [ "Friction", "StressFreeInitial" ] )
				new_name+="_G"
			item.addUserAttribute(userAttributeName ="Tolerance", stringValue = TouchTol )
			item.addUserAttribute(userAttributeName ="Bias", stringValue = Bias )


			item.update(
			 name = new_name,
			#targetSide1 = _sideTarget1,
			#targetSide2 = _sideTarget2,
			patchToleranceCalculationMethod = apex.AutoManual.Manual,
			tiePatchTolerance = float(_contact_tol),
			#name = 'Mesh Independent Tie 34',
			#description = '',
			)



	## End of Connect to GUI code ####


if __name__ == "__main__":
	# standalone version for debugging
	dict={}
	main()