Extraire les coordonnées des pièces d'un assemblage Inventor dans un fichier Excel

Dans ce tutoriel, je vais vous expliquer comment intégrer une règle iLogic, permettant de créer un fichier .csv, qui portera le nom de l’assemblage Inventor, ce fichier comportera plusieurs colonnes, les premières colonnes affichent les coordonnées X,Y,Z des pièces de l'assemblage, puis d'autres colonnes comme Nom de l'occurrence, Nom du fichier, Titre, Project ...

  1. Commencer par créer un nouvel Ensemble :Extraire les coordonnées des pièces d'un assemblage Inventor

  2. Créer une nouvelle règle dans le navigateur iLogic :Extraire les coordonnées des pièces d'un assemblage Inventor
  3. Copier / Coller la règle iLogic ci-dessous :
    Private Sub Main()
        Dim asmDoc As AssemblyDocument
        asmDoc = ThisApplication.ActiveDocument
            
            'Create the Export file (Filename = Assembly Filename + csv)
            Dim ExportFile As String = ThisDoc.PathAndFileName(False) & ".csv"
            Dim oAppend As System.IO.StreamWriter
        oAppend = System.IO.File.CreateText(ExportFile)
            oAppend.WriteLine("Position X" & ";" & "Position Y" & ";" & "Position Z" & ";" & "Rotation" & ";" & "Nom Famille" & ";" _
            & "Nom Occurrence" & ";" & "Nom Fichier" & ";" & "Titre" & ";" & "Description" & ";" & "Designer" & ";" & "Project")
    
            
        ' Occurence Level Limit (1 = Toplevel only, 2 = 1st Sublevel, ...)
        Dim OccLevelLimit As Integer
        OccLevelLimit = 1
            
            ' Set a list of Properties
            Dim oPropList() As String
            oPropList = {"Part Number","Title","Description","Designer","Project","OccurrenceName"}
            
        ' Call the function that traverses the assembly and Gets the props.
        Call GetProp(asmDoc.ComponentDefinition.Occurrences, 0, OccLevelLimit, oAppend, oPropList)
            
            'Display the export file
            oAppend.Flush()
            oAppend.Close()
            ThisDoc.Launch(ExportFile)
        
    End Sub
    
    Private Sub GetProp(Occurrences As ComponentOccurrences, OccLevel As Integer, LevelLimit As Integer, oExportFile As System.IO.StreamWriter, PropertyNames() As String)
        ' Iterate through each of the occurrences in the collection provided.
        Dim Occ As ComponentOccurrence
            'Set the Occurence Level
                   OccLevel = OccLevel + 1
            
            For Each Occ In Occurrences
                   
                   'Get The Annotation text from the Occurence property Title
                   Dim oDoc As Inventor.Document
                   oDoc = Occ.Definition.Document
                                   
                   Dim AnnotationText(PropertyNames.Length) As String
                   
                   For i = 0 To PropertyNames.Length - 1
                                   'Show the Property Name in addition to the Value eg: Title:<Component Title>
                                   'AnnotationText(i) = PropertyNames(i) & ": " & GetPropertyValue(oDoc.Propertysets,PropertyNames(i))
                                   'Only enter the Property Value
                                   AnnotationText(i) = GetPropertyValue(oDoc.PropertySets,PropertyNames(i))
                   Next
                   
                   'Write the Props, Occurence Position, Occurence Rotation and Annotation to the export file
                   Try
                           WriteAnnotation(oExportFile, Occ.Name,Occ.ReferencedDocumentDescriptor.DisplayName, AnnotationText, Occ.MassProperties.CenterOfMass, Occ.Transformation.Translation, Occ.Transformation)
            Catch 
                   
                   End Try
    
            ' If this occurrence is a subassembly, recursively call this
            ' function to traverse through the subassembly if it is in range of the Level limit
            If Occ.DefinitionDocumentType = kAssemblyDocumentObject And OccLevel < LevelLimit Then
                Call GetProp(Occ.SubOccurrences, OccLevel, LevelLimit, oExportFile, PropertyNames)
            End If
                   
        Next
            
    End Sub
    
    Private Sub WriteAnnotation(oWrite As System.IO.StreamWriter, OccName As String, OccFileName As String, Text() As String, Point As Point, Translation As Vector, oTransform As Matrix) 'write to the csv file
    
    'Compute the Occurence Rotation xAngleOffset represents the Inventor occurence rotation in the Z
    Dim aRotAngles(2) As Double
    Call CalculateRotationAngles(oTransform, aRotAngles)
    
    xAngleOffset = FormatNumber(aRotAngles(2), 3)
    'MessageBox.Show("Origin Z Angle = " & xAngleOffset & " deg", "Component Origin Rotation")
    
    
    'xAngleOffset = Acos(oPartX.DotProduct(oXAxis))*180/PI 'Angular offset between component X-axis and assembly X-axis
    
    'Format the CSV record row entry
    'Extracting the occurrence rotation in the Z - this assumes all parts will be parallel to the assembly XY
    oWrite.WriteLine((Floor(Translation.X * 10)) & ";" & (Floor(Translation.Y * 10)) & ";" & (Floor(Translation.Z * 10)) & ";" _
    & xAngleOffset & ";" & Text(0) & ";" & OccName & ";" _
    & OccFileName & ";"& Text(1) & ";" & Text(2) & ";" & Text(3) & ";" & Text(4) & ";")
    
    'Optional code to write the Occurence CoG
    'oWrite.WriteLine("<Position x='" & (Point.X * 10) & "' y='" & (Point.Y * 10) & "' z='" & (Point.Z * 10) & "'/>")
    
    End Sub
    
    Private Function GetPropertyValue(oPropSets As PropertySets, PropertyName As String) As String
    
    Dim oPropSet As PropertySet
    For Each oPropSet In oPropSets    
        ' Iterate through all the Properties in the current set.
       Dim oProp
        For Each oProp In oPropSet
            ' Obtain the Name of the Property
            Dim Name As String
            Name = oProp.Name 
            
            If Name = PropertyName
                   ' Obtain the Value of the Property
                   Dim Value As String
                   Value = oProp.Value
                   Return Value
                   Exit Function
            End If 
            Next
    Next
    Return ""
    End Function
    
    Sub CalculateRotationAngles( _
                ByVal oMatrix As Inventor.Matrix, _
                ByRef aRotAngles() As Double)
        Const TODEGREES As Double = 180 / PI
    
        Dim dB As Double
        Dim dC As Double
        Dim dNumer As Double
        Dim dDenom As Double
        Dim dAcosValue As Double
           
        Dim oRotate As Inventor.Matrix
        Dim oAxis As Inventor.Vector
        Dim oCenter As Inventor.Point
       
        oRotate = ThisApplication.TransientGeometry.CreateMatrix
        oAxis = ThisApplication.TransientGeometry.CreateVector
        oCenter = ThisApplication.TransientGeometry.CreatePoint
    
        oCenter.X = 0
        oCenter.Y = 0
        oCenter.Z = 0
    
        ' Choose aRotAngles[0] about x which transforms axes[2] onto the x-z plane
        '
        dB = oMatrix.Cell(2, 3)
        dC = oMatrix.Cell(3, 3)
    
        dNumer = dC
        dDenom = Sqrt(dB * dB + dC * dC)
    
        ' Make sure we can do the division.  If not, then axes[2] is already in the x-z plane
        If (Abs(dDenom) <= 0.000001) Then
            aRotAngles(0) = 0#
        Else
            If (dNumer / dDenom >= 1#) Then
                dAcosValue = 0#
            Else
                If (dNumer / dDenom <= -1#) Then
                    dAcosValue = PI
                Else
                    dAcosValue = Acos(dNumer / dDenom)
                End If
            End If
       
            aRotAngles(0) = Sign(dB) * dAcosValue
            oAxis.X = 1
            oAxis.Y = 0
            oAxis.Z = 0
    
            Call oRotate.SetToRotation(aRotAngles(0), oAxis, oCenter)
            Call oMatrix.PreMultiplyBy(oRotate)
       End If
    
        '
        ' Choose aRotAngles[1] about y which transforms axes[3] onto the z axis
        '
        If (oMatrix.Cell(3, 3) >= 1#) Then
            dAcosValue = 0#
        Else
            If (oMatrix.Cell(3, 3) <= -1#) Then
                dAcosValue = PI
            Else
                dAcosValue = Acos(oMatrix.Cell(3, 3))
            End If
        End If
    
        aRotAngles(1) = Sign(-oMatrix.Cell(1, 3)) * dAcosValue
        oAxis.X = 0
        oAxis.Y = 1
        oAxis.Z = 0
        Call oRotate.SetToRotation(aRotAngles(1), oAxis, oCenter)
        Call oMatrix.PreMultiplyBy(oRotate)
    
        '
        ' Choose aRotAngles[2] about z which transforms axes[0] onto the x axis
        '
        If (oMatrix.Cell(1, 1) >= 1#) Then
            dAcosValue = 0#
        Else
            If (oMatrix.Cell(1, 1) <= -1#) Then
                dAcosValue = PI
            Else
                dAcosValue = Acos(oMatrix.Cell(1, 1))
            End If
        End If
    
        aRotAngles(2) = Sign(-oMatrix.Cell(2, 1)) * dAcosValue
    
        'if you want to get the result in degrees
        aRotAngles(0) = aRotAngles(0) * TODEGREES
        aRotAngles(1) = aRotAngles(1) * TODEGREES
        aRotAngles(2) = aRotAngles(2) * TODEGREES
    End Sub
    
  4. Cliquer sur Enregistrer puis Fermer :Extraire les coordonnées des pièces d'un assemblage Inventor
  5. Enregistrer l'ensemble en tant que gabarit :Extraire les coordonnées des pièces d'un assemblage Inventor
  6. Créer un nouvel ensemble, puis placer les pièces, Enregistrer l'ensemble :Extraire les coordonnées des pièces d'un assemblage Inventor
  7. Clique droit sur la règle et choisissez "Exécuter la règle" :Run règle
  8. Vous pouvez maintenant visualiser le fichier Excel :Extraire les coordonnées des pièces d'un assemblage InventorA bientôt sur le blog Aplicit !