Set automatic connections through scripting

Pernelle Marone-Hitz
Pernelle Marone-Hitz Member, Moderator, Employee Posts: 871
100 Answers 500 Comments 250 Likes First Anniversary
✭✭✭✭
edited June 2023 in Structures

How can we set automatic contacts through scripting in Mechanical?

Tagged:

Answers

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 871
    100 Answers 500 Comments 250 Likes First Anniversary
    ✭✭✭✭
    Answer ✓

    The following function can be used:

    def SetContacts(analysis):        
        try:
            ExtAPI.Log.WriteMessage("Automation of Contacts Definition")    
            
            connections =  ExtAPI.DataModel.Project.Model.Connections      
            # delete existing contacts
            for connection in connections.Children:
                if connection.DataModelObjectCategory==DataModelObjectCategory.ConnectionGroup:
                    connection.Delete()
            
            # create automatically generated contacts    
            contactGroup=connections.AddConnectionGroup()
            contactGroup.ToleranceType=0 # for slider type
            contactGroup.ToleranceSlider=70 # tolerance    
            contactGroup.CreateAutomaticConnections()       
            ExtAPI.Log.WriteMessage("End of Automation of Contacts Definition")  
        except:
            ExtAPI.Log.WriteMessage("Error : Exception in SetContacts()")
    
  • Ayush Kumar
    Ayush Kumar Member, Moderator, Employee Posts: 470
    100 Answers 250 Likes 100 Comments Second Anniversary
    ✭✭✭✭
    Answer ✓

    We can also skip the contact group creation. The following line does everything automatically:

    Model.Connections.CreateAutomaticConnections()