VB - Open Multiple Part and/or Assembly Files from Directory

VB - Open Multiple Part and/or Assembly Files from Directory

Anonymous
Not applicable
1,044 Views
2 Replies
Message 1 of 3

VB - Open Multiple Part and/or Assembly Files from Directory

Anonymous
Not applicable

Hello,

 

I am trying to develop a bit of code for a plug-in that will iterate through a directory and open all .ipt or .iam files that were created after a certain time.

 

 

The current document would always be a file that meets this criteria. 

 

The end goal would be to open each .ipt and .iam file and remove iProperty Fields, then close them all (not including the file that is currently open). 

 

Here is where I am at right now. I have commented out my ideas of what I think should happen, but I am also open to ideas to make this more efficient too. I was thinking if the directory could be sorted by date, maybe it would be easier to a Do While that checks if the oParts/oAssys Creation Date >= oTime.

 

 Dim oTime As String = DateTime.Now()

Dim oActDoc As Inventor.Document = g_inventorApplication.ActiveDocument
Dim oCurrFile = ThisApplication.ActiveDocument.FullFileName
Dim oCurrDir = System.IO.Path.GetDirectoryName(oCurrFile)
Dim NewFilename As String = oActDoc.FullFileName

Dim oFiles = System.IO.Directory.GetFiles(oCurrDir, "*.ipt", System.IO.SearchOption.TopDirectoryOnly)
Dim oAssys = System.IO.Directory.GetFiles(oCurrDir, "*.iam", System.IO.SearchOption.TopDirectoryOnly)

'Get an Array List of all oFiles and oAssys that have a creation date made later than the oTime

'Iterate through array list of all oFiles and oAssys (Skip file if it is oCurrFile)

'System.IO.File.Open(oFiles or oAssys)?
oPropSet = oActDoc.PropertySets("Design Tracking Properties")
oProp = oPropSet.Item("Stock Number")
oProp.Value = ""
oProp = oPropSet.Item("Project")
oProp.Value = ""
'Systems.IO.File.Close(oFiles or oAssys)?

'End Iteration

 

Would this be doable? Let me know if I need to clarify. Thanks!

 

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

clutsa
Collaborator
Collaborator
Accepted solution

How about something like this?

Dim oTime As String = DateTime.Now()

Dim oActDoc As Inventor.Document = ThisApplication.ActiveDocument
Dim oCurrFile = ThisApplication.ActiveDocument.FullFileName
Dim oCurrDir = System.IO.Path.GetDirectoryName(oCurrFile)
Dim NewFilename As String = oActDoc.FullFileName

Dim oFiles = System.IO.Directory.GetFiles(oCurrDir, "*.ipt", System.IO.SearchOption.TopDirectoryOnly)
Dim oAssys = System.IO.Directory.GetFiles(oCurrDir, "*.iam", System.IO.SearchOption.TopDirectoryOnly)
'MessageBox.Show(oFiles.Count, "Title")
Dim MyPartsList As New ArrayList
Dim MyAssysList As New ArrayList

'Get an Array List of all oFiles and oAssys that have a creation date made later than the oTime
If oFiles.Count > 0 Then
	For Each oFile In oFiles
		If System.IO.File.GetCreationTime(oFile) < oTime And oFile.ToString <> ThisDoc.PathAndFileName(True) Then MyPartsList.Add(oFile.ToString)
	Next
	'MessageBox.Show(MyPartsList.Count, "Title")
End If
If oAssys.Count > 0 Then
	For Each oAssy In oAssys
		If System.IO.File.GetCreationTime(oAssy) < oTime And oAssy.ToString <> ThisDoc.PathAndFileName(True) Then MyAssysList.Add(oAssy.ToString)
	Next
	'MessageBox.Show(MyAssysList.Count, "Title")
End If
If MyPartsList.Count > 0 Then
	For Each oFile In MyPartsList
		ThisDoc.Launch(oFile)
		Dim EditFile As PartDocument = ThisApplication.Documents.ItemByName(oFile)
		oPropSet = EditFile.PropertySets("Design Tracking Properties")
		oProp = oPropSet.Item("Stock Number")
		oProp.Value = ""
		oProp = oPropSet.Item("Project")
		oProp.Value = ""
		EditFile.Save
		EditFile.Close
	Next
End If
If MyAssysList.Count > 0 Then
	For Each oFile In MyAssysList
		ThisDoc.Launch(oFile)
		Dim EditFile As AssemblyDocument = ThisApplication.Documents.ItemByName(oFile)
		oPropSet = EditFile.PropertySets("Design Tracking Properties")
		oProp = oPropSet.Item("Stock Number")
		oProp.Value = ""
		oProp = oPropSet.Item("Project")
		oProp.Value = ""
		EditFile.Save
		EditFile.Close
	Next
End If
If I've helped you, please help me by supporting this idea.
Mass Override for Each Model State

Custom Glyph Icon for iMates

Message 3 of 3

Anonymous
Not applicable

Happy Day to Me! Thank you SO much!

 

There were a couple modifications:

 

I had to make oFiles.Count into oFiles.Length to get it to work

 

Not sure if its because I was using VB.Net but I could not use PathandFileName in ThisDoc.PathandFileName(True). But I changed this to oActDoc.FullFileName

 

Other then that worked perfectly. Thanks again!

0 Likes