Correct Contacts Shell Face

This script loops through all contact region objects and (re-)defines Contact Shell Face and Target Shell Face so that the normal vectors point towards each other. Note. The Contact/Target scoping for each contact region object is limited to one face.

Initial:

Transparent

After running the script:

Transparent

import System

'''Correct contact/target shell face so they point towards each other!'''

def dot_product(x, y): return sum([x[i] * y[i] for i in range(len(x))]) < 0

contacts = DataModel.GetObjectsByType(DataModelObjectCategory.ContactRegion)
for contact in contacts:
    contact_face    = contact.SourceLocation.Entities[0] # contact face
    centroid_c      = contact_face.Centroid
    point           = System.Array[float]([centroid_c[0],centroid_c[1],centroid_c[2]])    # units are geometry units.  Usually meters
    v               = contact_face.ParamAtPoint(point)
    normal_vector_c = contact_face.NormalAtParam(v[0],v[1])
    
    target_face     = contact.TargetLocation.Entities[0]
    centroid_t      = target_face.Centroid
    point           = System.Array[float]([centroid_t[0],centroid_t[1],centroid_t[2]])    # units are geometry units.  Usually meters
    u               = target_face.ParamAtPoint(point)
    normal_vector_t = target_face.NormalAtParam(u[0],u[1])
    
    contact_target_vector = [centroid_t[0]-centroid_c[0],centroid_t[1]-centroid_c[1],centroid_t[2]-centroid_c[2]]
    contact_target_vector_rel_contact_normal = dot_product(normal_vector_c,contact_target_vector)
    
    ''' if contact_target_vector_rel_contact_normal  == True, then the vector from contact --> target is opposite the 
    normal vector from the contact face. This sets the <<<<< CONTACT SHELL FACE >>>>>'''
    
    if contact_target_vector_rel_contact_normal: 
        contact.ContactShellFace = ShellFaceType.Bottom
        normal_vector_c = [normal_vector_c[0]*(-1),normal_vector_c[1]*(-1),normal_vector_c[2]*(-1)]
    else: contact.ContactShellFace = ShellFaceType.Top
    
    colinear = dot_product(normal_vector_c,normal_vector_t) # if colinear = -1 --> vectors point in opposite direction
    
    if colinear: contact.TargetShellFace = ShellFaceType.Top
    else: contact.TargetShellFace = ShellFaceType.Bottom