Vai al contenuto

#02 – Trova le interferenze tra le parti all’interno di un file assemblato

Descrizione :

Analizza ed evidenzia le interferenze tra le parti selezionate, evidenziandole in sequenza.
E’ simile alla funzione nativa presente in Inventor “ANALIZZA —>ANALIZZA INTERFERENZE”, ha il vantaggio, se non si seleziona nulla, di verificare se vi è interferenza tra tutte le parti presenti nell’assemblato. Il codice è ricavato da un esempio presente nell’help di Inventor!

Come funziona :

Funziona esclusivamente all’interno di un assemblato (*.iam).
Si può selezionare le parti desiderate poi lanciare il plugin che analizza ll’assemblato verificando se vi sono interferenze tra le parti selezionate (tutte vs tutte).
Se non si seleziona niente la verifica viene fatta tra tutte le parti presenti nell’assieme.

Codice :

'June 2023
'edited by Paolo Campagna and Roberto Franchi (CAD-PLUGIN-LAB) -> https://cad-plugin-lab.com
'original code from Inventor 2023 API help
Public Sub Interference()
    Dim oDoc As AssemblyDocument
    Set oDoc = ThisApplication.ActiveDocument
    
    ' Find all selected occurrences and add them to an ObjectCollection.
    Dim oSelectedOccs As ObjectCollection
    Set oSelectedOccs = ThisApplication.TransientObjects.CreateObjectCollection
    Dim i As Long
    For i = 1 To oDoc.SelectSet.count
        If oDoc.SelectSet.Item(i).Type = kComponentOccurrenceObject Then
            oSelectedOccs.Add oDoc.SelectSet.Item(i)
        End If
    Next
    
    ' If no occurrences are selected check for interference of
    ' all parts against all parts.  If one occurrence is selected, check
    ' for interference between that occurrence and the rest of the assembly.
    ' If more than one occurrence is selected let the user decide if it
    ' should check for interference between the parts in the selection or
    ' between the selected parts and the rest of the assembly.
    Dim oResults As InterferenceResults
    Dim oCheckSet As ObjectCollection
    Set oCheckSet = ThisApplication.TransientObjects.CreateObjectCollection
    If oSelectedOccs.count = 0 Then
        ' Add all occurrences to the object collection
        Dim oOcc As ComponentOccurrence
        For Each oOcc In oDoc.ComponentDefinition.Occurrences
            oCheckSet.Add oOcc
        Next
        
        ' Get the interference between everything.
        Set oResults = oDoc.ComponentDefinition.AnalyzeInterference(oCheckSet)
    ElseIf oSelectedOccs.count = 1 Then
        ' Add all occurrences except the selected occurrence to the object collection.
        For Each oOcc In oDoc.ComponentDefinition.Occurrences
            If Not oOcc Is oSelectedOccs.Item(1) Then
                oCheckSet.Add oOcc
            End If
        Next
        
        ' Get the interference between the selected occurrence everything else.
        Set oResults = oDoc.ComponentDefinition.AnalyzeInterference(oSelectedOccs, oCheckSet)
    Else
        If MsgBox("Check interference between selected occurrences and all other occurrences?", vbYesNo + vbQuestion) = vbYes Then
            ' Add all occurrences except the selected occurrences to the object collection.
            For Each oOcc In oDoc.ComponentDefinition.Occurrences
                ' Check to see if this occurrences is already selected.
                Dim bSelected As Boolean
                bSelected = False
                For i = 1 To oSelectedOccs.count
                    If oSelectedOccs.Item(i) Is oOcc Then
                        bSelected = True
                        Exit For
                    End If
                Next
                
                If Not bSelected Then
                    oCheckSet.Add oOcc
                End If
            Next
           
            ' Check interference between the selected items.
            Set oResults = oDoc.ComponentDefinition.AnalyzeInterference(oSelectedOccs, oCheckSet)
        Else
            ' Check interference between the selected items.
            Set oResults = oDoc.ComponentDefinition.AnalyzeInterference(oSelectedOccs)
        End If
    End If
    
    If oResults.count = 1 Then
        MsgBox "There is 1 interference."
    ElseIf oResults.count > 1 Then
        MsgBox "There are " & oResults.count & " interferences."
    End If
    
    If oResults.count > 0 Then
        Dim oHS1 As HighlightSet
        Set oHS1 = oDoc.HighlightSets.Add
        oHS1.Color = ThisApplication.TransientObjects.CreateColor(255, 0, 0)
        Dim oHS2 As HighlightSet
        Set oHS2 = oDoc.HighlightSets.Add
        oHS2.Color = ThisApplication.TransientObjects.CreateColor(0, 255, 0)
        
        For i = 1 To oResults.count
            oHS1.Clear
            oHS2.Clear
            oHS1.AddItem oResults.Item(i).OccurrenceOne
            oHS2.AddItem oResults.Item(i).OccurrenceTwo
            MsgBox "Occurrences are highlighted from interference " & i
        Next
        
        oHS1.Clear
        oHS2.Clear
    Else
        MsgBox "There is no interference."
    End If
End Sub

Come utilizzarlo :

Per rendere operativo questo plugin puoi copiare ed incollare il testo del codice all’interno di una regola iLogic, creare una macro VBA, oppure utilizzarlo per creare altri plugin in VB.NET.

Puoi scaricare direttamente il file di testo:
#02-Trova le interferenze tra le parti all’interno di un file assemblato

Per maggiori informazioni sull’utilizzo del codice riferirsi alla pagina dedicata.

Origine :

Autodesk Inventor 2023 API Help

IT