Add iProperties with a loop and arraylist

Add iProperties with a loop and arraylist

DFitting
Contributor Contributor
1,206 Views
1 Reply
Message 1 of 2

Add iProperties with a loop and arraylist

DFitting
Contributor
Contributor

I am attempting to write some code to create multiple iProperties in my model from the idw. The parameters are in an arraylist and I would like to use a loop to run through the arraylist but I keep getting an error.  I would like to use a loop so I don't have to write out a try catch for each iProperty as the list is continually growing.

 

my code is a little confusing as the list is called oParameterList I should have named it oPropertyList. I intend to fix this  

 

SyntaxEditor Code Snippet

iLogicVb.UpdateWhenDone = True

Dim odrawdoc As DrawingDocument
odrawdoc = ThisApplication.ActiveDocument
Dim userParams As UserParameters = odrawdoc.Parameters.UserParameters
customIDWPropertySet = odrawdoc.PropertySets.Item("Inventor User Defined Properties")

'Look at the model file referenced in the open document
Dim docFile As Document
If ThisDoc.ModelDocument IsNot Nothing Then
docFile = ThisDoc.ModelDocument
Else
MessageBox.Show("This drawing has no model reference", "iLogic")
Return
End If

'format model file name                   
Dim FNamePos As Long
FNamePos = InStrRev(docFile.FullFileName, "\", -1)   
Dim docFName As String 
docFName = Right(docFile.FullFileName, Len(docFile.FullFileName) - FNamePos) 

'define the property set
customPropertySet = docFile.PropertySets.Item("Inventor User Defined Properties")
customIDWPropertySet = odrawdoc.PropertySets.Item("Inventor User Defined Properties")
'look for the custom propety and add it if not found

Dim oCode, oColor, oDesignPSI, oDIA, oEdition, oExBlast, oExCoat, oHydroChart,oInBlast, oInCoat, oLength, oLocation, oNPRO, oPWHT, oTestPSI As String 

Dim oString As Object
Dim i As Integer = 0
Dim oParameterList As New ArrayList 
oParameterList.Add("Code")
oParameterList.Add("Color")
oParameterList.Add("Design_Pressure")
oParameterList.Add("Design_Temp")
oParameterList.Add("Min_Design_Temp")
oParameterList.Add("DiameterIN")
oParameterList.Add("Edition")
oParameterList.Add("External_Blast") 
oParameterList.Add("External_Coat")
oParameterList.Add("HYDRO CHART")
oParameterList.Add("Internal_Blast")
oParameterList.Add("Internal_Coat") 
oParameterList.Add("Length_UL")
oParameterList.Add("Location")
oParameterList.Add("NAME PLATE RUB OFF")
oParameterList.Add("PWHT")
oParameterList.Add("Test_Pressure")

For Each oString In oParameterList
Try
prop = customPropertySet.Item(oParameterList(i)) 
'return
Catch
customPropertySet.Add("", oParameterList(i))
End Try
	
Try
prop = customIDWPropertySet.Item(oParameterList(i))
Catch
customIDWPropertySet.Add("", oParameterList(i))
End Try
i = i + 1
Next

 

0 Likes
Accepted solutions (1)
1,207 Views
1 Reply
Reply (1)
Message 2 of 2

JamieVJohnson2
Collaborator
Collaborator
Accepted solution

Don't use ArrayList, use instead List(of T), where in your case List(of String) should work.  Also if you want a more robust method, separate the block of code for inserting a parameter into its own sub:

Sub Main 'ilogic requires this if you have more than one routine on a page, to tell it where to start.

     MyMainRoutine

End Sub

 

Public Sub MyMainRoutine

    'Define List - ParamNames as list(of string)

    'Populate List  - ParamNames.add(this), paramNames.add(that)...

    For each sParam as string in ParamNames

         booDidItWork as Boolean = WriteCustomProperty(paramName,paramValue,doc,overwrite?)

         If booDidItWork = False then MSGBOX("something went wrong")

     Next

End Sub

 

Public Function WriteCustomProperty(propName,propValue…)

try

     Do the thing

catch

    say something or skip it

end try

Return DidItWork

End Function

 

Personally I use this routine for that purpose:

    ''' <summary>
    ''' Add Custom iProperty To Inventor Document
    ''' </summary>
    ''' <param name="propName">Name of iProperty</param>
    ''' <param name="propValue">Value of iProperty</param>
    ''' <param name="invDoc">Document to work on</param>
    ''' <returns>True if file was changed</returns>
    ''' <remarks>Will look for property, if found, verify value, if different change value, if not found add property and value. Excludes Readonly files, and unsaved new files</remarks>
    Public Function WriteCustomProperty(ByVal propName As String, ByVal propValue As String, ByVal invDoc As Inventor.Document, Optional OverWriteProperty As Boolean = True) As Boolean
        Try
            Dim fi As New System.IO.FileInfo(invDoc.FullFileName)
            If fi.IsReadOnly Then Return False 'don't work with read-only files.  This gives the user control over which files to use and which to skip.
            Dim propSet As PropertySet = invDoc.PropertySets.Item("Inventor User Defined Properties")
            Dim custProp As Inventor.Property = FindiProperty(propSet.Name, propName, invDoc)
            eventLog += vbCrLf & invDoc.FullFileName
            'For Each prop In propSet
            '    If prop.Name = propName Then
            '        custProp = prop
            '        Exit For
            '    End If
            'Next
            If custProp Is Nothing Then
                custProp = propSet.Add(propValue, propName)
                filesEffected += 1
                eventLog += vbCrLf & "Added custom property: " & propName & ":  " & propValue
                Return True
            Else
                If Not String.IsNullOrWhiteSpace(custProp.Value) Then ' has a value first check if expression, then if regular property
                    If OverWriteProperty = True Then
                        If custProp.Expression.StartsWith("=") Then 'is expression
                            If custProp.Expression <> propValue Then
                                custProp.Value = propValue
                                filesEffected += 1
                                eventLog += vbCrLf & "Modified custom property: " & propName & ":  " & propValue
                                Return True
                            End If
                        Else 'is value
                            If custProp.Value <> propValue Then
                                custProp.Value = propValue
                                filesEffected += 1
                                eventLog += vbCrLf & "Modified custom property: " & propName & ":  " & propValue
                                Return True
                            End If
                        End If
                    End If
                Else 'is emtpy - overwrite command does snot apply
                    custProp.Value = propValue
                    filesEffected += 1
                    eventLog += vbCrLf & "Modified custom property: " & propName & ":  " & propValue
                    Return True
                End If
            End If
            Return False
        Catch ex As Exception
            'if property fails to write such as user does not check out or allow file to save, then retun without error.
            MsgBox("Can not write custom property: " & propName, MsgBoxStyle.SystemModal)
            Return False
        End Try
    End Function

Its way more than just add, but you can simplify it for your own needs.

Jamie Johnson : Owner / Sisu Lissom, LLC https://sisulissom.com/