[iLogic] Updating iProperty "Creation Date" to current date in all components of assembly

[iLogic] Updating iProperty "Creation Date" to current date in all components of assembly

michal.lachawiec
Participant Participant
1,163 Views
2 Replies
Message 1 of 3

[iLogic] Updating iProperty "Creation Date" to current date in all components of assembly

michal.lachawiec
Participant
Participant

I just created little script to update creation time (or other) iProperty of all parts within assembly to current date.


I hope it will be useful for someone 
It is simple script and tbh first one that is working 🙂 , if you have any comments how to improve it I will gladly read them.

 

 

 

Dim oDoc As Document

' Define the document
Dim oAssy As Document = ThisApplication.ActiveDocument
Dim allDocs As DocumentsEnumerator = oAssy.AllReferencedDocuments

'Try to update assembly date
Try
oAssy.PropertySets.Item(3).Item(1).Value() = Date.Today.AddDays(0)
Catch
End Try

For Each oDoc In allDocs
	
	Dim oPropSets As PropertySets = oDoc.PropertySets
    Dim oDesTrackProps As PropertySet = oPropSets.Item(3)

'Try to update part date
	Try
	oDesTrackProps.Item(1).Value() = Date.Today.AddDays(0)
	Catch
	End Try
	
Next

 

 

Accepted solutions (1)
1,164 Views
2 Replies
Replies (2)
Message 2 of 3

JelteDeJong
Mentor
Mentor
Accepted solution

There is a software development principle "Don't repeat yourself" (DRY). Your code does not compile with this principle. You wrote the code to update the property twice (WET-code 😉 It is good practice to factor that out to a new function.

Also, you used the item numbers for the PropertySet and the Property. Those numbers are not guaranteed to be always the same. (Maybe the order changes depending on the languae of Inventor or the Inventor version.) I think it's better to use the names. (All names can be found here.)

The last thing I think you can improve is not using "ThisApplication.ActiveDocument" but "ThisDoc.Document". Most of the time you will get the same object but not in all cases. The active document is not always what you expect. In this post, you find some rule that makes use of this difference maybe you find this example useful. 

Anyway this is how I would write this rule:

Public Sub Main()

	Dim doc As Document = ThisDoc.Document
	UpdateCreationDate(doc)

	For Each refDoc In doc.AllReferencedDocuments
		UpdateCreationDate(refDoc)
	Next

End Sub

Private Sub UpdateCreationDate(doc As Document)
	Try
		Dim propSets As PropertySets = doc.PropertySets
		Dim desTrackProps As PropertySet = propSets.Item("Design Tracking Properties")
		Dim propCreatinTime As [Property] = desTrackProps.Item("Creation Time")
		propCreatinTime.Value() = Date.Today
	Catch ex As Exception
		MsgBox("Could not update 'Creation Time' in document: " & doc.DisplayName)
	End Try
End Sub

 

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

Message 3 of 3

will.astill
Advocate
Advocate

Just thought I'd post a little thank you to you both. I'm trying really hard to learn some iLogic and having this kind of forum post is really useful for me and is appreciated.

 

Special thanks also for the link to mod the machine.  You've earned your like 🙂