iLogic Programmierung in Inventor: Excel Tabelle mit zwei Startzellen

iLogic Programmierung in Inventor: Excel Tabelle mit zwei Startzellen

trajan20
Contributor Contributor
665 Views
4 Replies
Message 1 of 5

iLogic Programmierung in Inventor: Excel Tabelle mit zwei Startzellen

trajan20
Contributor
Contributor
Hi, 
in meinem Post 
https://forums.autodesk.com/t5/inventor-deutsch/parameter-excel-tabelle-verknupfung-2-startzellen/m-... 
erkläre ich mein Ziel. Eine Antwort hat mir empfohlen, es mit iLogic zu probieren, doch ich komme nicht weiter.
ich glaube, ich habe es geschafft, die Daten aus der Excel-Tabelle jetzt mal zu lesen, doch ich verstehe einfach nicht wie ich jetzt neue Parameter erstelle. Vielleicht habe ich auch eine ganz falsche herangehenesweise. 
Danke im Vorhinein und Mit freundliche n Grüßen
Jan
 
 
Imports Microsoft.Office.Interop.Excel
Imports Inventor
 
' Pfad zur Excel-Tabelle
Dim excelFilePath As String = "..." 
 
' Excel-Anwendung öffnen
Dim excelApp As Object = CreateObject("Excel.Application")
 
' Öffne die Excel-Arbeitsmappe
Dim excelWorkbook As Object = excelApp.Workbooks.Open(excelFilePath)
 
Dim excelWorksheet As Object = excelWorkbook.Sheets(1) 
 
Dim inventorApp As Inventor.Application = ThisApplication
Dim inventorDoc As Inventor.Document = inventorApp.ActiveDocument
 
' Parameterwerte aus der Excel-Tabelle lesen und in Inventor aktualisieren bzw. erstellen
For Each Row As Object In excelWorksheet.UsedRange.Rows
    Dim paramName As String = Row.Cells(1).Value
    Dim paramValue As Double = CDbl(Row.Cells(2).Value)
    Dim paramUnit As String = Row.Cells(3).Value
    Dim paramComment As String = Row.Cells(4).Value
 
    ' Überprüfen, ob der Parameter bereits vorhanden ist
    Dim param As Inventor.Parameter = Nothing
    Try
        param = inventorDoc.ComponentDefinition.Parameters.Item(paramName)
    Catch ex As Exception
        ' Parameter existiert nicht, daher erstellen wir ihn
'       param = inventorDoc.ComponentDefinition.Parameters.UserParameters.Add(paramName, Inventor.ValueTypeEnum.kDouble)
' param = inventorDoc.ComponentDefinition.Parameters.AddByExpression(paramName, paramValue & " " & paramUnit, Inventor.PartFeatureUtils.kModelParameter, "", "")
'       param = inventorDoc.ComponentDefinition.Parameters.UserParameters.Add(paramName, Inventor.ValueTypeEnum.kTextParameter)
'       param.Expression = "0" '
param = inventorDoc.ComponentDefinition.Parameters.UserParameters.Add(paramName, Inventor.ValueTypeEnum.kDouble)
        param.Expression = paramValue & " " & paramUnit
        param.Comment = paramComment
   param.Comment = paramComment
    End Try
 
    ' Den Parameterwert aktualisieren
    param.Value = paramValue
Next
 
' Excel-Objekte freigeben
excelWorkbook.Close(False)
excelApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWorksheet)
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWorkbook)
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp)
0 Likes
Accepted solutions (1)
666 Views
4 Replies
Replies (4)
Message 2 of 5

trajan20
Contributor
Contributor

Update: Ich kannte iLogic Programmierung zu diesem Zeitpunkt noch überhaupt nicht. Nun habe ich es mir beigebracht und verstehe, was für einen Blödsinn ich da eigentlich betrieben habe. nach etlichen Gesprächen mit ChatGPT, bin ich auf folgendes funktionierendes Ergebnis gekommen (kleine Anmerkung im Vorhinein, an einer Regel, welche die Parameter aktualisiert arbeite ich noch):

Imports Microsoft.Office.Interop.Excel
' Importieren der erforderlichen Bibliotheken
 
' Pfad zur Excel-Datei
Dim excelFilePath As String = "...."
 
' Erstellen einer Instanz der Excel-Anwendung
Dim excelApp As Object = CreateObject("Excel.Application")
 
' Öffnen der Excel-Arbeitsmappe
Dim excelWorkbook As Object = excelApp.Workbooks.Open(excelFilePath)
 
Dim excelWorksheet As Object = Nothing ' Initialisieren Sie die Variable vor der Schleife
Dim excelSheetName As String 
Do
    excelSheetName = InputBox("Geben Sie den Blattnamen ein:", "Excel: Blattname", "Tabelle1")
 
    Try     ' Versuchen Sie, auf das Arbeitsblatt zuzugreifen
        excelWorksheet = excelWorkbook.Sheets(excelSheetName)
        Exit Do ' Beenden Sie die Schleife, wenn das Arbeitsblatt erfolgreich gefunden wurde
    Catch ex As Exception
        ' Fehler beim Zugriff auf das Arbeitsblatt
        MsgBox("Ungültiges Arbeitsblatt. Bitte geben Sie einen gültigen Blattnamen ein.", vbExclamation, "Ungültiges Arbeitsblatt")
    End Try
 
Loop
 
' Eingabeaufforderung für die Startspalte und Startzeile, beide Eingaben sind erforderlich
Dim startColumn As String
Dim rowInput As String
Dim startRow As Integer
 
Do
    startColumn = InputBox("Geben Sie die Startspalte (z.B. A) ein:","Excel: Startspalte", "A")
    startRow = 0
    If Not String.IsNullOrWhiteSpace(startColumn) Then
        rowInput = InputBox("Geben Sie die Startzeile (z.B. 1) ein:", "Excel: Startzeile", "3")
        If Integer.TryParse(rowInput, startRow) Then
            ' Gültige Eingabe für Startzeile
            Exit Do ' Beenden Sie die Schleife, wenn beide Eingaben gültig sind
        End If
    End If
 
    MsgBox("Bitte geben Sie gültige Eingaben für Startspalte und Startzeile ein.", vbExclamation, "Ungültige Eingabe")
 
Loop
 
' Zugriff auf das aktive Inventor-Dokument
Dim Doc As Document = ThisDoc.Document
 
' Zugriff auf die Benutzerparameter des Inventor-Dokuments
Dim userParams As UserParameters = Doc.ComponentDefinition.Parameters.UserParameters
 
' Initialisierung von Variablen
Dim i As Integer = 0
Dim paramName As String
Dim paramValueString As String 
Dim paramValue As Double 
Dim paramUnit As String 
Dim paramComment As String 
Dim param As UserParameter
 
' Schleife zur Verarbeitung der Excel-Daten
While True
    ' Lesen der Daten aus der Excel-Tabelle
    paramName = excelWorksheet.Cells(startRow + i, excelWorksheet.Columns(startColumn).Column).Value
    paramValueString = excelWorksheet.Cells(startRow + i, excelWorksheet.Columns(startColumn).Column + 1).Value
 
    ' Überprüfen auf leere oder ungültige Zeilen
    If String.IsNullOrEmpty(paramName) Or String.IsNullOrEmpty(paramValueString) Or Not IsNumeric(paramValueString) Then
        Exit While
    End If
 
    ' Lesen der Einheit und des Kommentars aus der Excel-Tabelle
    paramUnit = excelWorksheet.Cells(startRow + i, excelWorksheet.Columns(startColumn).Column + 2).Value
    paramComment = excelWorksheet.Cells(startRow + i, excelWorksheet.Columns(startColumn).Column + 3).Value
 
    ' Konvertieren des Wert-Strings in einen numerischen Wert
    Try
        paramValue = CDbl(paramValueString)
    Catch ex As System.InvalidCastException
        MsgBox("Ein Fehler ist aufgetreten beim Konvertieren des Strings in eine Double-Zahl: " & ex.Message)
    End Try
 
    ' Versuch, den Parameter in Inventor zu finden, falls vorhanden
    ' Andernfalls Parameter erstellen
    Try
        param = userParams.Item(paramName)
    Catch
        param = userParams.AddByValue(paramName, paramValue / 10, paramUnit) ' Beispiel: Der Wert wird geteilt durch 10
param.Comment = paramComment + " (" + excelSheetName + ")"
    End Try
 
    i = i + 1
End While
 
' Schließen der Excel-Arbeitsmappe und Freigabe der Ressourcen
excelWorkbook.Close(False)
excelApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWorksheet)
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWorkbook)
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp)



0 Likes
Message 3 of 5

A.Acheson
Mentor
Mentor
Accepted solution

Hi @trajan20 

If you want to learn how to add parameters go to the API help here for the userparameters object . There isn't any short cut method for this in the ilogic API so you need the inventor API. So when your at the userparameters.add method your actually add the end of the line but you also need the starting objects for this which are first the document, componentdefinition then parameters object.

Here is the API sample for creating a user parameter written in vba. The code below is it converted from vba to vb.net/ilogic environment. Remove Sub call and end statement and remove the word Set. Any value added via this method needs to be in data base units of cm, otherwise you can use add expression method instead of value and specifiy value and unit together.

 

    ' Get the active document.  Assumes a part document is active.
    Dim partDoc As PartDocument
    partDoc = ThisApplication.ActiveDocument
    
    ' Get the UserParameters collection
    Dim userParams As UserParameters
    userParams = partDoc.ComponentDefinition.Parameters.UserParameters
    
    ' Create a parameter using an expression.  The parameters unit is specified
    ' as millimeters, but the value of the parameter will be 3 inches because
    ' the unit is specified as part of the expression.
    Dim param As Parameter
    param = userParams.AddByExpression("NewParam1", "3 in", kMillimeterLengthUnits)
    
    ' Create a parameter using a value.  When setting by value, the value is always
    ' in database units.  In this case it is a length so it will always be in
    ' centimeters.  The units used for the parameter will be the current length units
    ' of the document because it's defined to use the default display length units.
    param = userParams.AddByValue("NewParam2", 3 * 2.54, kDefaultDisplayLengthUnits)

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
Message 4 of 5

trajan20
Contributor
Contributor

Thank you very much. This helped me understand the topic better and the links are also very useful. This is my re-worked iLogic rule:

Imports Excel = Microsoft.Office.Interop.Excel
Imports System.Runtime.InteropServices
Imports System.Windows.Forms
' Pfad zur Excel-Datei
Dim excelFilePath As String = "..."
 
Dim excelApp As Object = Nothing
Dim excelWorkbook As Object = Nothing
Dim excelWorksheet As Object = Nothing
 
Try
    ' Starten Sie die Excel-Anwendung
    excelApp = CreateObject("Excel.Application")
    
    ' Öffnen der Excel-Arbeitsmappe
    excelWorkbook = excelApp.Workbooks.Open(excelFilePath)
 
Dim excelSheets As Object = excelWorkbook.Sheets
Dim optionen As New List(Of String)
For Each excelSheet As Object In excelSheets
  excelWorksheet = excelWorkbook.Sheets(excelSheet.Name)
optionen.Add(excelSheet.Name)
Next
Dim excelSheetName As String 
Do
' Erstellen Sie das Dialogfeld
Using dialog As New Form()
    ' Dialogfeldtitel festlegen
    dialog.Text = "Dropdown-Liste auswählen"
    
    ' Dialogfeldgröße festlegen
    dialog.Width = 500
    dialog.Height = 300
 
    ' Dropdown-Liste erstellen
    Dim comboBox As New ComboBox()
    ' Fügen Sie die Optionen aus der Liste zur Dropdown-Liste hinzu
    comboBox.Items.AddRange(optionen.ToArray())
    comboBox.DropDownStyle = ComboBoxStyle.DropDownList
    comboBox.Dock = DockStyle.Top
    
    ' OK-Button erstellen
    Dim okButton As New Button()
    okButton.Text = "OK"
    okButton.Dock = DockStyle.Bottom
    
    ' Controls zum Dialogfeld hinzufügen
    dialog.Controls.Add(comboBox)
    dialog.Controls.Add(okButton)
    
    ' Dialogfeld in der Mitte des Bildschirms positionieren
    dialog.StartPosition = FormStartPosition.CenterScreen
    
    ' OK-Button-Klick-Handler hinzufügen
    AddHandler okButton.Click, Sub(sender As Object, E As EventArgs)
        ' Schließen Sie das Dialogfeld
        dialog.Close()
    End Sub
    
    ' Dialogfeld anzeigen
    dialog.ShowDialog()
    
    ' Wenn das Dialogfeld geschlossen wurde, die ausgewählte Option speichern
    excelSheetName = DirectCast(comboBox.SelectedItem, String)
    ' Hier haben Sie die ausgewählte Option
'     MsgBox("Ausgewählte Option: " & ausgewählteOption)
End Using
 
    Try     ' Versuchen Sie, auf das Arbeitsblatt zuzugreifen
        excelWorksheet = excelWorkbook.Sheets(excelSheetName)
        Exit Do ' Beenden Sie die Schleife, wenn das Arbeitsblatt erfolgreich gefunden wurde
    Catch ex As Exception
        ' Fehler beim Zugriff auf das Arbeitsblatt
        MsgBox("Ungültiges Arbeitsblatt. Bitte geben Sie einen gültigen Blattnamen ein.", vbExclamation, "Ungültiges Arbeitsblatt")
    End Try
 
Loop
 
' Eingabeaufforderung für die Startspalte und Startzeile, beide Eingaben sind erforderlich
Dim startColumn As String
Dim rowInput As String
Dim startRow As Integer
 
Do
    startColumn = InputBox("Geben Sie die Startspalte (z.B. A) ein:","Excel: Startspalte", "A")
    startRow = 0
    If Not String.IsNullOrWhiteSpace(startColumn) Then
        rowInput = InputBox("Geben Sie die Startzeile (z.B. 1) ein:", "Excel: Startzeile", "3")
        If Integer.TryParse(rowInput, startRow) Then
            ' Gültige Eingabe für Startzeile
            Exit Do ' Beenden Sie die Schleife, wenn beide Eingaben gültig sind
        End If
    End If
 
    MsgBox("Bitte geben Sie gültige Eingaben für Startspalte und Startzeile ein.", vbExclamation, "Ungültige Eingabe")
 
Loop
 
' Zugriff auf das aktive Inventor-Dokument
Dim Doc As Document = ThisDoc.Document
 
' Zugriff auf die Benutzerparameter des Inventor-Dokuments
Dim userParams As UserParameters = Doc.ComponentDefinition.Parameters.UserParameters
 
' Initialisierung von Variablen
Dim i As Integer = 0
Dim paramName As String
Dim paramValueString As String 
Dim paramValue As Double 
Dim paramUnit As String 
Dim paramComment As String 
Dim param As UserParameter
Dim neueParameter As String = ""
Dim paramExists As Boolean = False
 
' Schleife zur Verarbeitung der Excel-Daten
While True
    ' Lesen der Daten aus der Excel-Tabelle
    paramName = excelWorksheet.Cells(startRow + i, excelWorksheet.Columns(startColumn).Column).Value
    paramValueString = excelWorksheet.Cells(startRow + i, excelWorksheet.Columns(startColumn).Column + 1).Value
 
    ' Überprüfen auf leere oder ungültige Zeilen
    If String.IsNullOrEmpty(paramName) Or String.IsNullOrEmpty(paramValueString) Or Not IsNumeric(paramValueString) Then
        Exit While
    End If
 
    ' Lesen der Einheit und des Kommentars aus der Excel-Tabelle
    paramUnit = excelWorksheet.Cells(startRow + i, excelWorksheet.Columns(startColumn).Column + 2).Value
    paramComment = excelWorksheet.Cells(startRow + i, excelWorksheet.Columns(startColumn).Column + 3).Value
 
    ' Konvertieren des Wert-Strings in einen numerischen Wert
    Try
        paramValue = CDbl(paramValueString)
    Catch ex As System.InvalidCastException
        MsgBox("Ein Fehler ist aufgetreten beim Konvertieren des Strings in eine Double-Zahl: " & ex.Message)
    End Try
 
    ' Versuch, den Parameter in Inventor zu finden, falls vorhanden
    ' Andernfalls Parameter erstellen
 
For Each existingParam As UserParameter In userParams
    If existingParam.Name = paramName Then
        paramExists = True
        Exit For
    End If
Next
 
If Not paramExists Then
param = userParams.AddByExpression(paramName, paramValue.ToString() + paramUnit, paramUnit)
    param.Comment = paramComment & " (" & excelSheetName & ")"
    neueParameter = neueParameter & paramName & ": " & paramValue & paramUnit & vbCrLf
End If
    i = i + 1
 
End While
MessageBox.Show("Folgende Parameter wurden hinzugefügt:" & vbCrLf & neueParameter, "Parameterübersicht")
' Schließen der Excel-Arbeitsmappe und Freigabe der Ressourcen
excelWorkbook.Close(False)
Marshal.ReleaseComObject(excelWorkbook)
excelApp.Quit()
Marshal.ReleaseComObject(excelApp)
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWorksheet)
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWorkbook)
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp)
 
Catch ex As Exception
    ' Fehler beim Öffnen der Excel-Datei
    MessageBox.Show("Ein Fehler ist aufgetreten: " & ex.Message, "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
0 Likes
Message 5 of 5

trajan20
Contributor
Contributor

And this is how I update my parameters:

Imports Microsoft.Office.Interop.Excel
 
Dim excelFilePath As String = "..."
Dim excelApp As Object = CreateObject("Excel.Application")
Dim excelWorkbook As Object = excelApp.Workbooks.Open(excelFilePath)
Dim excelSheets As Object = excelWorkbook.Sheets
Dim excelWorksheet As Object = Nothing
Dim Doc As Document = ThisDoc.Document
Dim userParams As UserParameters = Doc.ComponentDefinition.Parameters.UserParameters
Dim row As Integer 
Dim column As Integer 
Dim paramName As String
Dim paramValueString As String 
Dim paramValue As Double 
Dim paramUnit As String 
Dim paramComment As String 
Dim neueParameter As String = " "
Dim TestDouble As Double 
Dim rowCount As Integer 
 
For Each excelSheet As Object In excelSheets
    excelWorksheet = excelWorkbook.Sheets(excelSheet.Name)
'   MsgBox("Sheet: " + excelSheet.Name)
column = 1
rowCount = excelWorksheet.UsedRange.Rows.Count
row = rowCount
' MsgBox("Die Anzahl der verwendeten Zeilen in der Excel-Datei beträgt: " & rowCount)
    While column < 22
row = 1
    While row < 40
        paramName = excelWorksheet.Cells(row, column).Value
        paramValueString = excelWorksheet.Cells(row, column + 1).Value
If Not String.IsNullOrEmpty(paramName) And Not String.IsNullOrEmpty(paramValueString) And IsNumeric(paramValueString) Then
'MessageBox.Show("Zelle: " + row.ToString() + column.ToString(), "Parameterübersicht")
paramUnit = excelWorksheet.Cells(row, column + 2).Value
paramComment = excelWorksheet.Cells(row, column + 3).Value
Try
        paramValue = CDbl(paramValueString)
    Catch ex As System.InvalidCastException
        MsgBox("Ein Fehler ist aufgetreten beim Konvertieren des Strings in eine Double-Zahl: " & ex.Message)
    End Try
 
            For Each existingParam As UserParameter In userParams
                If existingParam.Name = paramName Then
If Double.TryParse(existingParam.Value, TestDouble) Then
    Console.WriteLine("Converted value: " & TestDouble)
Else
    MsgBox("Ein Fehler ist aufgetreten beim Konvertieren des Strings in eine Double-Zahl: ")
End If 
 
' MessageBox.Show("Vergleich der beiden Double-Werte vor der If-Abfrage (ohne /10):" & vbCrLf + TestDouble.ToString() & vbCrLf + paramValue.ToString(), "Parameterübersicht")
' MessageBox.Show("Vergleich der beiden String-Werte vor der If-Abfrage:" & vbCrLf + existingParam.Expression.ToString() & vbCrLf + paramValue.ToString() + " " + paramUnit, "Parameterübersicht")
' If Not TestDouble = (paramValue / 10) 'Or existingParam.Comment IsNot paramComment + " (" + excelSheetName + ")"
If Not existingParam.Expression.ToString().Trim() = (paramValue.ToString() + " " + paramUnit.ToString()).Trim() Then 'Or existingParam.Comment IsNot paramComment + " (" + excelSheetName + ")"
' MessageBox.Show("Test, dass If-Abfrage funktioniert")
neueParameter &= paramName & ": " & paramValue &paramUnit & vbCrLf
existingParam.Expression = (paramValue).ToString()
                        existingParam.Comment = paramComment + " (" + excelSheetName + ")"                     
End If
 
                    Exit For
                End If
            Next
      End If
        row = row + 1
    End While
column = column + 5
End While
Next
MessageBox.Show("Folgende Parameter wurden aktualisiert:" & vbCrLf & neueParameter, "Parameterübersicht")
excelWorkbook.Close(False)
excelApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWorksheet)
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWorkbook)
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp)

 

0 Likes