Custom Iproperties reading value using ilogic problem

Custom Iproperties reading value using ilogic problem

infoEHE7X
Participant Participant
226 Views
2 Replies
Message 1 of 3

Custom Iproperties reading value using ilogic problem

infoEHE7X
Participant
Participant

The code below has no issue as I have used it for taking value of other iproperties, however for the custom iproperties it cannot detect the corresponding value from the attached file.

 

Imports System.IO
Imports System.Windows.Forms

Sub Main()
' Get the assembly file path from the user
Dim assemblyFilePath As String = GetFilePath("Select the assembly file", "Assembly Files (*.iam)|*.iam")
If assemblyFilePath = "" Then
MsgBox("No assembly file selected.")
Exit Sub
End If

' Open the selected assembly file
Dim asmDoc As AssemblyDocument = ThisApplication.Documents.Open(assemblyFilePath)

' Define the custom property names to retrieve
Dim propertyNames As String() = {"MARK", "QTY", "SERVICE"}

' Fetch and display each custom iProperty
Dim result As String = "Custom iProperty Values for Assembly:" & vbCrLf
For Each propName As String In propertyNames
Dim value As String = GetiPropertyValue(asmDoc, propName)
result &= propName & ": " & value & vbCrLf
Next

' Show the result to the user
MsgBox(result, MsgBoxStyle.Information, "Custom iProperties")
End Sub

' Function to retrieve the custom iProperty value
Function GetiPropertyValue(oDoc As Document, propertyName As String) As String
Try
Return CStr(iProperties.Value("Custom", propertyName))
Catch ex As Exception
Return "N/A"
End Try
End Function

' Helper function to get file path
Function GetFilePath(prompt As String, filter As String) As String
Dim openFileDialog As New OpenFileDialog()
openFileDialog.Title = prompt
openFileDialog.Filter = filter
If openFileDialog.ShowDialog() = DialogResult.OK Then
Return openFileDialog.FileName
End If
Return ""
End Function

0 Likes
Accepted solutions (1)
227 Views
2 Replies
Replies (2)
Message 2 of 3

Michael.Navara
Advisor
Advisor
Accepted solution

This can be because iLogic iProperties object returns properties from the document in which context the rule is running. Try to use standard API for iProperties in this case.

 

Sub Main()
    ' Get the assembly file path from the user
    Dim assemblyFilePath As String = GetFilePath("Select the assembly file", "Assembly Files (*.iam)|*.iam")
    If assemblyFilePath = "" Then
        MsgBox("No assembly file selected.")
        Exit Sub
    End If

    ' Open the selected assembly file
    Dim asmDoc As AssemblyDocument = ThisApplication.Documents.Open(assemblyFilePath)

    ' Define the custom property names to retrieve
    Dim propertyNames As String() = {"MARK", "QTY", "SERVICE"}

    ' Fetch and display each custom iProperty
    Dim result As String = "Custom iProperty Values for Assembly:" & vbCrLf
    For Each propName As String In propertyNames
        Dim value As String = GetiPropertyValue(asmDoc, propName)
        result &= propName & ": " & value & vbCrLf
    Next

    ' Show the result to the user
    MsgBox(result, MsgBoxStyle.Information, "Custom iProperties")
End Sub

' Function to retrieve the custom iProperty value
Function GetiPropertyValue(oDoc As Document, propertyName As String) As String
    'Try
    '    Return CStr(iProperties.Value("Custom", propertyName))
    'Catch ex As Exception
    '    Return "N/A"
    'End Try
    Try
        oDoc.PropertySets("{D5CDD505-2E9C-101B-9397-08002B2CF9AE}")(propertyName).Value.ToString()
    Catch ex As Exception
        Return "N/A"
    End Try
End Function

' Helper function to get file path
Function GetFilePath(prompt As String, filter As String) As String
    Dim openFileDialog As New OpenFileDialog()
    openFileDialog.Title = prompt
    openFileDialog.Filter = filter
    If openFileDialog.ShowDialog() = DialogResult.OK Then
        Return openFileDialog.FileName
    End If
    Return ""
End Function

 

Note: Please use code snippet block for code samples. It is much more readable. 😀

0 Likes
Message 3 of 3

infoEHE7X
Participant
Participant

Thanks Michael, Here is the clean code:

 

Imports System.IO
Imports System.Windows.Forms

Sub Main()
    ' Get the assembly file path from the user
    Dim assemblyFilePath As String = GetFilePath("Select the assembly file", "Assembly Files (*.iam)|*.iam")
    If assemblyFilePath = "" Then
        MsgBox("No assembly file selected.")
        Exit Sub
    End If

    ' Open the selected assembly file
    Dim asmDoc As AssemblyDocument = ThisApplication.Documents.Open(assemblyFilePath)

    ' Define the custom property names to retrieve
    Dim propertyNames As String() = {"MARK", "QTY", "SERVICE"}

    ' Fetch and display each custom iProperty
    Dim result As String = "Custom iProperty Values for Assembly:" & vbCrLf
    For Each propName As String In propertyNames
        Dim value As String = GetiPropertyValue(asmDoc, propName)
        result &= propName & ": " & value & vbCrLf
    Next

    ' Show the result to the user
    MsgBox(result, MsgBoxStyle.Information, "Custom iProperties")
End Sub

' Function to retrieve the custom iProperty value
Function GetiPropertyValue(oDoc As Document, propertyName As String) As String
    Try
        ' Access the "Inventor User Defined Properties" property set
        Dim customPropertySet As PropertySet = oDoc.PropertySets.Item("Inventor User Defined Properties")
        Dim propValue As Object = customPropertySet.Item(propertyName).Value
        If propValue IsNot Nothing Then
            Return propValue.ToString()
        End If
    Catch ex As Exception
        ' If the property does not exist, return "N/A"
    End Try
    Return "N/A"
End Function

' Helper function to get file path
Function GetFilePath(prompt As String, filter As String) As String
    Dim openFileDialog As New OpenFileDialog()
    openFileDialog.Title = prompt
    openFileDialog.Filter = filter
    If openFileDialog.ShowDialog() = DialogResult.OK Then
        Return openFileDialog.FileName
    End If
    Return ""
End Function

 

0 Likes