Suppress Duplicate Contacts

Goal

The following script example examines your model to determine if duplicate contact pairs exist and if so, automatically suppresses the duplicate pair. A duplicate pair is a contact pair where the Source and Target scoping are interchangeable, that is, the same.

Code

# Scan contacts to find duplicate pairs.

# Get all contact regions
contacts=ExtAPI.DataModel.GetObjectsByType(DataModelObjectCategory.ContactRegion)

contLocation={}
duplicateCont={}

# create dictionary will all sources and locations from all contacts
for cont in contacts:
    l1=[]
    l2=[]
    for id in cont.SourceLocation.Ids:
        l1.append(id)
    for id in cont.TargetLocation.Ids:
        l2.append(id)        
        contLocation[cont.ObjectId]=[l1,l2]
    
dupList=[]
# Now check for duplicates
for cont in sorted(contLocation.keys()):
    # Retrieve source and location
    source1=contLocation[cont][0]
    target1=contLocation[cont][1]
    for contTest in sorted(contLocation.keys()):
        if contTest != cont and contTest not in dupList: # Don't check contact with itself, don't check if already identified as duplicate
            # Retrieve source and location for test contacts
            source2=contLocation[contTest][0]
            target2=contLocation[contTest][1]            
            if (source1==source2 and target1==target2) or (source2==target1 and source1==target2): 
            # Contacts are duplicate of each other, add to duplicateCont dictionary if not already identified
                if cont in duplicateCont.keys():
                    curList=duplicateCont[cont]
                    curList.append(contTest)
                    d1={cont: curList}
                    duplicateCont.update(d1)
                else:
                    duplicateCont[cont]=[contTest]   
            # This list contains all contacts already identified as duplicate to avoid double detection
                dupList.append(contTest)
                dupList.append(cont)
    
msgCont=''
# Prepare message with list of duplicate contacts and suppress duplicated ones
for cont in duplicateCont.keys():
    cont1=ExtAPI.DataModel.GetObjectById(cont)
# create list with all IDs to check if there are duplicates with a different order 
    for dup in duplicateCont[cont]:
        cont2=ExtAPI.DataModel.GetObjectById(dup)
        msgCont+=cont2.Name + ' is a duplicate of ' + cont1.Name + '\n'
        cont2.Suppressed=True

if msgCont=='':
    # If message is empty, no duplicates have been found
    msg = Ansys.Mechanical.Application.Message('No duplicate contacts found', MessageSeverityType.Warning)
    ExtAPI.Application.Messages.Add(msg)		
else:
    msg = Ansys.Mechanical.Application.Message('Duplicates were found and suppressed: \n'+msgCont, MessageSeverityType.Warning)
    ExtAPI.Application.Messages.Add(msg)