How to check for potential duplicates in contact?
Pierre Thieffry
Member, Moderator, Employee Posts: 107
✭✭✭✭
A customer wants to perform a check on contacts to find duplicate pairs applied to the same geometry - including the case where target and source are flipped. Mechanical already offers a search for duplicated contacts, but does not check if target and contact are flipped.
How can we check for this?
Tagged:
4
Answers
-
Hi @Pierre Thieffry This doesn't appear to be a question? Would you be able to edit it into the standard Question/Answer format?
2 -
I'm proposing the attached - checks are only performed on contact location and don't take any other property in account:
# 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: # Don't check contact with itself # 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() and contTest not in dupList: curList=duplicateCont[cont] curList.append(contTest) d1={cont: curList} duplicateCont.update(d1) elif contTest not in dupList: 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)
16