Attempt to set Custom Iproperty on every open file -Need Help

Attempt to set Custom Iproperty on every open file -Need Help

j3scat
Enthusiast Enthusiast
161 Views
2 Replies
Message 1 of 3

Attempt to set Custom Iproperty on every open file -Need Help

j3scat
Enthusiast
Enthusiast

I have tried countless revisions of my attached file, and I can not change the Custom Iproperty on all open documents.

I am using a 'For each Doc in ThisApplication.Documents.VisibleDocuments' followed by Doc.Activate

I am currently only running this on .ipt documents that are all in the current active project

If I use a messagebox to show the current Doc Name, my for loop is correctly cycling through each document

 

If i use the format iproperties.value(Doc, "Custom", FixProp) = FixVal it will correct the first open file only and no others

If I use the format iproperties.value("Custom", FixProp) = FixVal it will correct the current active file only

 

 I have left all my attempts in my file, but just commented out so you can see what I have tried.

 

I am running this Code as an External Rule

 

Sub ApplyFix(FixType As String, FixProp As String, FixVal As String)
	Dim Doc As Inventor.Document
	'Dim aDoc As Inventor.Document
	'Dim ChkVal As String
	'Dim Fail As Boolean
	'Dim customPropertySet As Inventor.PropertySet
	
	
	For Each Doc In ThisApplication.Documents.VisibleDocuments
		Doc.Activate
		'aDoc = ThisApplication.ActiveEditDocument
		'customPropertySet = ThisDoc.Document.PropertySets.Item("Inventor User Defined Properties")
		'customPropertySet = Doc.PropertySets.Item("Inventor User Defined Properties")
		'customPropertySet = aDoc.PropertySets.item("Inventor User Defined Properties")
		'MessageBox.Show("Document: " & Doc.FullDocumentName,"BF56")
		Select Case FixType
		Case "iProp"
			Select Case FixProp
			Case "Finish"
				iProperties.Value(Doc, "Custom", FixProp) = FixVal
				'iProperties.Value("Custom", FixProp) = FixVal
'				Try
'					Fail = False
'					ChkVal = ""
'					ChkVal = iProperties.Value(Doc, "Custom", FixProp)
'					MessageBox.Show("ChkVal: " & ChkVal, Doc.FullDocumentName & " |BF67")
'					iProperties.Value(Doc, "Custom", FixProp) = FixVal
'					'ChkVal = iProperties.Value(Doc, "Custom", FixProp)
'					MessageBox.Show("FixVal: " & FixVal & vbCrLf & "ChkVal: " & ChkVal, Doc.FullDocumentName & " |BF70")
'					'If ChkVal <> FixVal Then Fail = True
'					If FixVal <> iProperties.Value(Doc, "Custom", FixProp) Then Fail = True
'				Catch
'					Fail = True
'					MessageBox.Show("Attempt set new" & vbCrLf & "FixProp: " & FixProp & " not found", "BF75" & Doc.FullDocumentName)
'					'Doc.PropertySets.Item("Inventor User Defined Properties").Add(FixVal, Inventor.PropertyTypeEnum.kCustomProperty, FixProp)
'				End Try
			End Select
'			If Fail Then
'				'MessageBox.Show("Attempt set new" & vbCrLf & "FixVal: " & FixVal & vbCrLf & "FixProp: " & FixProp,"Fail Trigger |BF80")
'				'Doc.PropertySets.Item("Inventor User Defined Properties").Add(FixVal, FixProp)
'				'ThisApplication.ActiveEditDocument.PropertySets.Item("Inventor User Defined Properties").Add(FixVal, FixProp)
'				'Doc.PropertySets.Item("Inventor User Defined Properties").Add("", FixProp)
'				customPropertySet.Add("", FixProp)
'				iProperties.Value(Doc, "Custom", FixProp) = FixVal
'				MessageBox.Show("set new Pass" & vbCrLf & "FixVal: " & FixVal & vbCrLf & "File: " & aDoc.FullFileName, "Fail Trigger after set prop|BF86")
'				
'			End If
		End Select
	Next
End Sub
0 Likes
Accepted solutions (1)
162 Views
2 Replies
Replies (2)
Message 2 of 3

ryan.rittenhouse
Advocate
Advocate
Accepted solution

iProperties can get a little trick and I recommend doing it entirely through api calls and not the iLogic wrappers when working w/ multiple files/documents. Below I have a working (on my machine at least) example of walking through all the visible files and setting an iProperty. If it exists, it'll be updated. It'll be created if it doesn't exist. Does this work for you?

 

Sub Main()
	
	Dim fixProp As String = "Finish"
	Dim fixVal As String = "A Finish"
	
	For Each doc As Document In ThisApplication.Documents.VisibleDocuments
		IPropertySet(doc, fixProp, fixVal)		
	Next doc
	
End Sub 'Main

''' <summary>
''' Sets the value of an iProperty in the given document.
''' If the property already exists, its value is updated.
''' If the property does not exist, a new property is added with the specified value.
''' </summary>
''' <param name="doc">The document to set the iProperty in.</param>
''' <param name="propName">The name of the iProperty.</param>
''' <param name="propValue">The value to set for the iProperty.</param>
Sub IPropertySet(doc As Document, propName As String, propValue As Object)

    Dim updated As Boolean = False
    Dim propSet As PropertySet = doc.PropertySets.Item("Inventor User Defined Properties")
    
    If Not propSet Is Nothing Then
        For Each prop In propSet
            If prop.Name = propName Then
                prop.Value = propValue
                updated = True
                Exit For
            End If
        Next prop
    End If
    
    If Not updated Then propSet.Add(propValue, propName)

End Sub 'IPropertySet
If this solved your problem, or answered your question, please click Accept Solution.
Message 3 of 3

j3scat
Enthusiast
Enthusiast

That worked amazingly.

 

I'm a little disappointed I didn't figure it out myself. But at the same time, I am aware, I would have never figured this out. Thank you.

0 Likes