Get the first occurrence of each referenced document in an assembly

Get the first occurrence of each referenced document in an assembly

nhowardVJQ4A
Contributor Contributor
2,031 Views
16 Replies
Message 1 of 17

Get the first occurrence of each referenced document in an assembly

nhowardVJQ4A
Contributor
Contributor

I'm trying to right some code to check an assembly for old revision parts. We don't use Vault or any sort of PDM so when we up issue a part it gets placed into a sub folder call "OldIssues". 

 

I have written some code which cycles through each referenced document in the assembly and checks to see if it's in the OldIssues folder, if it is than the user gets the option to replace it with the latest issue. What I need to get is the first occurrence of the out of date component to be able to use the Component.Replace function.

 

Please see code below;

 

Any help would be greatly appreciated.

 

Thanks, Nathan

 

'get the active assembly document
Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oDoc As Document

'cycle through all referenced documents
For Each oDoc In oAsmDoc.ReferencedDocuments
		
	'remove file extension from file name
	oExtPosn = InStrRev(oDoc.DisplayName, ".", -1)
	oFName = Left(oDoc.DisplayName, oExtPosn -1)
		
	'get current folder name
	oNamePosn = InStrRev(oDoc.FullFileName, "\", -1)
	oFilePath = Left(oDoc.FullFileName, oNamePosn - 1)
	oFolderPosn = InStrRev(oFilePath, "\", -1)
	oFolderName = Right(oFilePath, Len(oFilePath) - oFolderPosn)
		
'if document is in "OldIssues" folder then it must be out of date		
If oFolderName = "OldIssues" Then
	
'remove "OldIssues" from folder path 
oFilePath2 = Left(oDoc.FullFileName, oNamePosn - 10)	

'confirm users wants to replace old issue
oChoice = MessageBox.Show(oFName & " is out of date." + vbCrLf + "Would you like to replace it?", "Replace Document", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
	If oChoice = 7 Then 'do nothing
		
	Else
		'replace old issue with latest
		
		
		
			'Component.Replace(????, "OtherPartfilename.ipt", True)
	
		
	End If
	
InventorVb.DocumentUpdate()
	
End If

InventorVb.DocumentUpdate()

Next

 

0 Likes
Accepted solutions (1)
2,032 Views
16 Replies
Replies (16)
Message 2 of 17

JhoelForshav
Mentor
Mentor

Hi @nhowardVJQ4A 

Something like this maybe? 🙂

'get the active assembly document
Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oDoc As Document

'cycle through all referenced documents
For Each oDoc In oAsmDoc.ReferencedDocuments
		
	'remove file extension from file name
	oExtPosn = InStrRev(oDoc.DisplayName, ".", -1)
	oFName = Left(oDoc.DisplayName, oExtPosn -1)
		
	'get current folder name
	oNamePosn = InStrRev(oDoc.FullFileName, "\", -1)
	oFilePath = Left(oDoc.FullFileName, oNamePosn - 1)
	oFolderPosn = InStrRev(oFilePath, "\", -1)
	oFolderName = Right(oFilePath, Len(oFilePath) - oFolderPosn)
		
'if document is in "OldIssues" folder then it must be out of date		
If oFolderName = "OldIssues" Then
	
'remove "OldIssues" from folder path 
oFilePath2 = Left(oDoc.FullFileName, oNamePosn - 10)	

'confirm users wants to replace old issue
oChoice = MessageBox.Show(oFName & " is out of date." + vbCrLf + "Would you like to replace it?", "Replace Document", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
	If oChoice = 7 Then 'do nothing
		
	Else
		Dim RefOccs As ComponentOccurrencesEnumerator = oAsmDoc.ComponentDefinition.Occurrences.AllReferencedOccurrences(oDoc)
		If RefOccs.Count > 0 Then RefOccs(1).Replace("OtherPartfilename.ipt", True)
	End If
	
InventorVb.DocumentUpdate()
	
End If

InventorVb.DocumentUpdate()

Next
Message 3 of 17

nhowardVJQ4A
Contributor
Contributor

Hi @JhoelForshav 

Thanks for you reply, unfortunately that didn't work for me but I've had a play and managed to come up with this, which works providing the node name ends with ":1".

 

Is there a way to count :1, :2, :3 etc until it finds one that works? Or would you recommend trying to get the actual name of the first occurrence of a component?

 

Thanks 

 

 

oDoc1 = ThisDoc.Document
oNamer = "Update"

Dim UNDO As Transaction 
UNDO = ThisApplication.TransactionManager.StartTransaction(oDoc1, oNamer)

'get the active assembly document
Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oDoc As Document

'cycle through referenced documents
For Each oDoc In oAsmDoc.ReferencedDocuments
		
	'get position of file extension
	oExtPosn = InStrRev(oDoc.DisplayName, ".", -1)
	'remove file extension from part number
	oFName = Left(oDoc.DisplayName, oExtPosn - 1)
	'get file extension
	oExt = Right(oDoc.DisplayName, Len(oDoc.DisplayName) - oExtPosn)
	'remove issue from part number
	oFName2 = Left(oFName,11)
	
	'get position of last "\"
	oNamePosn = InStrRev(oDoc.FullFileName, "\", -1)
	'get current file path
	oFilePath = Left(oDoc.FullFileName, oNamePosn - 1)
	'get current folder position
	oFolderPosn = InStrRev(oFilePath, "\", -1)
	'get current folder name
	oFolderName = Right(oFilePath, Len(oFilePath) - oFolderPosn)
		
'if document is in "OldIssues" folder then it must be out of date		
If oFolderName = "OldIssues" Then
	
'remove "OldIssues" from folder path 
oFilePath2 = Left(oDoc.FullFileName, oNamePosn - 10)	

'confirm users wants to replace old issue
oChoice = MessageBox.Show(oFName & " is out of date." + vbCrLf + "Would you like to replace it?", "Replace Document", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
	If oChoice = 7 Then 'do nothing
		
	Else
		'search for latest issue. assumes there is only one model found which is correct if up-issuing procedure is followed
		Dim models () As String = System.IO.Directory.GetFiles(oFilePath2,oFName2 & "*." & oExt,System.IO.SearchOption.TopDirectoryOnly) 
		
		'replace old issue with latest
		For Each model As String In models 
		Component.Replace(oFName & ":1", model, True)
		Next
			
	End If
		
End If

Next

InventorVb.DocumentUpdate()

MessageBox.Show("Assembly is up to date", "Task Complete", MessageBoxButtons.OK, MessageBoxIcon.Information)

UNDO.End

 

0 Likes
Message 4 of 17

JhoelForshav
Mentor
Mentor

Could you tell me why it didn't work? I assume you changed "OtherPartfilename.ipt" to a valid filepath?

 

0 Likes
Message 5 of 17

nhowardVJQ4A
Contributor
Contributor

Yes I updated the file path. Please see screenshots of the error below;1.JPG2.JPG

0 Likes
Message 6 of 17

JhoelForshav
Mentor
Mentor

@nhowardVJQ4A 

This should work 🙂

Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oDoc As Document

'cycle through all referenced documents
For Each oDoc In oAsmDoc.AllReferencedDocuments
	Dim RefOccs As ComponentOccurrencesEnumerator = oAsmDoc.ComponentDefinition.Occurrences.AllReferencedOccurrences(oDoc)
	If RefOccs.Count = 0 Then Continue For
	If oDoc.FullFileName = "" Then Continue For
	Dim oName As String = System.IO.Path.GetFileName(oDoc.FullFileName)
	Dim oPath() As String = System.IO.Path.GetDirectoryName(oDoc.FullFileName).Split("\")
	Dim oFolderName As String = oPath(oPath.Length - 1)
	'if document is in "OldIssues" folder then it must be out of date		
	If oFolderName = "OldIssues" Then
		Dim oFilePath2 As String
		For i = 0 To oPath.Length - 2
			oFilePath2 = oFilePath2 & oPath(i) & "\"
		Next
		Dim oChoice As DialogResult = MessageBox.Show(oName & " is out of date." + vbCrLf + "Would you like to replace it?", "Replace Document", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
		If oChoice = DialogResult.Yes
			Try
				RefOccs(1).Replace(oFilePath2 & oName, True)
			Catch
				MessageBox.Show(oFilePath2 & oName & " Doesn't exist!", "File to replace with doesn't exist", MessageBoxButtons.OK, MessageBoxIcon.Error)
			End Try
		End If
	End If
Next
InventorVb.DocumentUpdate()
0 Likes
Message 7 of 17

nhowardVJQ4A
Contributor
Contributor

That works, however the file it is looking for will never exist as the new file will have a suffix of " IssXX", XX being anything from 02 to 99.

 

At first issue the file will be 7905-044-001. When revised it becomes 7905-044-001 Iss02, then Iss03 and so on. It may be the case that an update is not sequential i.e. 7905-044-001 Iss05 is latest but 7905-044-001 is in the assembly. 

0 Likes
Message 8 of 17

nhowardVJQ4A
Contributor
Contributor

Seems to work?

 

Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oDoc As Document
For Each oDoc In oAsmDoc.ReferencedDocuments
	If oDoc.FullFileName = "" Then Continue For
	Dim oName As String = System.IO.Path.GetFileName(oDoc.FullFileName)
	'remove " IssXX"
	Dim oName2 As String = Left(oName, 11)
	'extract file extension
	Dim FileExt As String = Right(oName, 4)
	Dim oPath() As String = System.IO.Path.GetDirectoryName(oDoc.FullFileName).Split("\")
	Dim oFolderName As String = oPath(oPath.Length - 1)

	If oFolderName = "OldIssues" Then
		Dim oFilePath2 As String
		For i = 0 To oPath.Length - 2
			oFilePath2 = oFilePath2 & oPath(i) & "\"
		Next
		Dim oChoice As DialogResult = MessageBox.Show(oName & " is out of date." + vbCrLf + "Would you like to replace it?", "Replace Document", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
		If oChoice = DialogResult.Yes
			Dim RefOccs As ComponentOccurrencesEnumerator = oAsmDoc.ComponentDefinition.Occurrences.AllReferencedOccurrences(oDoc)
			If RefOccs.Count > 0 Then
				Try
					Dim models () As String = System.IO.Directory.GetFiles(oFilePath2,oName2 & "*" & FileExt,System.IO.SearchOption.TopDirectoryOnly) 
					For Each model As String In models
						RefOccs(1).Replace(model, True)
					Next					
				Catch
					MessageBox.Show(oFilePath2 & oName & " doesn't exist!", "File to replace with doesn't exist", MessageBoxButtons.OK, MessageBoxIcon.Error)
				End Try
			End If
		End If
	End If
Next
InventorVb.DocumentUpdate()

MessageBox.Show("Assembly is up-to-date", "Update complete")
0 Likes
Message 9 of 17

JhoelForshav
Mentor
Mentor

Does the file to be replaced (the file in OldIssues) have a suffix, and the file that should replace the file has a suffix also?

0 Likes
Message 10 of 17

nhowardVJQ4A
Contributor
Contributor

The file in OldIssues folder may not have a suffix, but the file to replace it will.

0 Likes
Message 11 of 17

JhoelForshav
Mentor
Mentor

Ok,

I'm thinking something like this then?

Remove the suffix from the OldIssue-document name for comparison if it has one, then for the document to replace with, we find the one with the highest suffix 🙂

 

 

Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oDoc As Document

'cycle through all referenced documents
For Each oDoc In oAsmDoc.AllReferencedDocuments
	Dim RefOccs As ComponentOccurrencesEnumerator = oAsmDoc.ComponentDefinition.Occurrences.AllReferencedOccurrences(oDoc)
	If RefOccs.Count = 0 Then Continue For
	If oDoc.FullFileName = "" Then Continue For
	Dim oName As String = System.IO.Path.GetFileName(oDoc.FullFileName)
	Dim FileExt As String = System.IO.Path.GetExtension(oDoc.FullFileName)
	Dim oName_NoExt As String = System.IO.Path.GetFileNameWithoutExtension(oDoc.FullFileName)
	'Remove suffix if exists
	If oName_NoExt.Split(" ")(oName_NoExt.Split(" ").Length - 1).Contains("Iss") Then _
		oName_NoExt = oName_NoExt.Replace(oName_NoExt.Split(" ")(oName_NoExt.Split(" ").Length - 1), "")
	Dim oPath() As String = System.IO.Path.GetDirectoryName(oDoc.FullFileName).Split("\")
	Dim oFolderName As String = oPath(oPath.Length - 1)
	'if document is in "OldIssues" folder then it must be out of date		
	If oFolderName = "OldIssues" Then
		Dim oFilePath2 As String
		For i = 0 To oPath.Length - 2
			oFilePath2 = oFilePath2 & oPath(i) & "\"
		Next
		Dim oChoice As DialogResult = MessageBox.Show(oName & " is out of date." + vbCrLf + "Would you like to replace it?", "Replace Document", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
		If oChoice = DialogResult.Yes
			'Find file with highest suffix
			Dim models() As String = System.IO.Directory.GetFiles(oFilePath2, oName_NoExt & "*" & FileExt, System.IO.SearchOption.TopDirectoryOnly) 
			Dim oReplaceDoc As String
			Dim oNum As Integer = 0
			For Each model As String In models
				Dim modelName As String = System.IO.Path.GetFileNameWithoutExtension(model)
				Dim oModelNum As Integer = CInt(modelName.Substring(modelName.Length - 2))
				If oModelNum > oNum Then oReplaceDoc = model
			Next
			Try
				RefOccs(1).Replace(oReplaceDoc, True)
			Catch
				MessageBox.Show("Couldn't replace file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
			End Try
		End If
	End If
Next
InventorVb.DocumentUpdate()

 

0 Likes
Message 12 of 17

nhowardVJQ4A
Contributor
Contributor

It works perfectly if there is only one out of issue part in the assembly. If there is more than one, it will replace the first part and then throw up an error;

1.JPG

 

2.JPG

 

Also, ideally I'd only like to update parts/assemblies in the top level, at the moment it is replacing everything that is out of date even parts in a sub assemblies.

 

Many thanks for all your help.

0 Likes
Message 13 of 17

JhoelForshav
Mentor
Mentor

Seems like GetFiles fails due to a non valid filepath...

Run the rule like this to only look at top level components, and also giva back the filepath in a messagebox so we can see what's wrong 🙂

 

Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oDoc As Document

'cycle through all referenced documents
For Each oDoc In oAsmDoc.ReferencedDocuments
	Dim RefOccs As ComponentOccurrencesEnumerator = oAsmDoc.ComponentDefinition.Occurrences.AllReferencedOccurrences(oDoc)
	If RefOccs.Count = 0 Then Continue For
	If oDoc.FullFileName = "" Then Continue For
	Dim oName As String = System.IO.Path.GetFileName(oDoc.FullFileName)
	Dim FileExt As String = System.IO.Path.GetExtension(oDoc.FullFileName)
	Dim oName_NoExt As String = System.IO.Path.GetFileNameWithoutExtension(oDoc.FullFileName)
	'Remove suffix if exists
	If oName_NoExt.Split(" ")(oName_NoExt.Split(" ").Length - 1).Contains("Iss") Then _
		oName_NoExt = oName_NoExt.Replace(oName_NoExt.Split(" ")(oName_NoExt.Split(" ").Length - 1), "")
	Dim oPath() As String = System.IO.Path.GetDirectoryName(oDoc.FullFileName).Split("\")
	Dim oFolderName As String = oPath(oPath.Length - 1)
	'if document is in "OldIssues" folder then it must be out of date		
	If oFolderName = "OldIssues" Then
		Dim oFilePath2 As String
		For i = 0 To oPath.Length - 2
			oFilePath2 = oFilePath2 & oPath(i) & "\"
		Next
		'Just to see what it returns
		MsgBox(oFilePath2)
		
		Dim oChoice As DialogResult = MessageBox.Show(oName & " is out of date." + vbCrLf + "Would you like to replace it?", "Replace Document", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
		If oChoice = DialogResult.Yes
			'Find file with highest suffix
			Dim models() As String = System.IO.Directory.GetFiles(oFilePath2, oName_NoExt & "*" & FileExt, System.IO.SearchOption.TopDirectoryOnly) 
			Dim oReplaceDoc As String
			Dim oNum As Integer = 0
			For Each model As String In models
				Dim modelName As String = System.IO.Path.GetFileNameWithoutExtension(model)
				Dim oModelNum As Integer = CInt(modelName.Substring(modelName.Length - 2))
				If oModelNum > oNum Then oReplaceDoc = model
			Next
			Try
				RefOccs(1).Replace(oReplaceDoc, True)
			Catch
				MessageBox.Show("Couldn't replace file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
			End Try
		End If
	End If
Next
InventorVb.DocumentUpdate()
0 Likes
Message 14 of 17

nhowardVJQ4A
Contributor
Contributor

After replacing the first part, its adds the filepath on again for the second...

 

1.JPG

 

0 Likes
Message 15 of 17

nhowardVJQ4A
Contributor
Contributor

Also, it is still updating sub assembly parts.

 

See example below;

7905-44-040 Iss03 is the latest revision and 7905-44-001 is out of date, after running the ilogic it updates the part in the sub assembly and doesn't even touch the one in the top level assembly.

 

1.JPG

 

2.JPG

 

0 Likes
Message 16 of 17

JhoelForshav
Mentor
Mentor
Accepted solution

How about now?

Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oDoc As Document

'cycle through all referenced documents
For Each oDoc In oAsmDoc.ReferencedDocuments
	Dim RefOccs As ComponentOccurrencesEnumerator = oAsmDoc.ComponentDefinition.Occurrences.AllReferencedOccurrences(oDoc)
	If RefOccs.Count = 0 Then Continue For
	If oDoc.FullFileName = "" Then Continue For
	Dim oName As String = System.IO.Path.GetFileName(oDoc.FullFileName)
	Dim FileExt As String = System.IO.Path.GetExtension(oDoc.FullFileName)
	Dim oName_NoExt As String = System.IO.Path.GetFileNameWithoutExtension(oDoc.FullFileName)
	'Remove suffix if exists
	If oName_NoExt.Split(" ")(oName_NoExt.Split(" ").Length - 1).Contains("Iss") Then _
		oName_NoExt = oName_NoExt.Replace(oName_NoExt.Split(" ")(oName_NoExt.Split(" ").Length - 1), "")
	Dim oPath() As String = System.IO.Path.GetDirectoryName(oDoc.FullFileName).Split("\")
	Dim oFolderName As String = oPath(oPath.Length - 1)
	'if document is in "OldIssues" folder then it must be out of date		
	If oFolderName = "OldIssues" Then
		Dim oFilePath2 As String = ""
		For i = 0 To oPath.Length - 2
			oFilePath2 = oFilePath2 & oPath(i) & "\"
		Next
		'Just to see what it returns
		MsgBox(oFilePath2)
		
		Dim oChoice As DialogResult = MessageBox.Show(oName & " is out of date." + vbCrLf + "Would you like to replace it?", "Replace Document", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
		If oChoice = DialogResult.Yes
			'Find file with highest suffix
			Dim models() As String = System.IO.Directory.GetFiles(oFilePath2, oName_NoExt & "*" & FileExt, System.IO.SearchOption.TopDirectoryOnly) 
			Dim oReplaceDoc As String
			Dim oNum As Integer = 0
			For Each model As String In models
				Dim modelName As String = System.IO.Path.GetFileNameWithoutExtension(model)
				Dim oModelNum As Integer = CInt(modelName.Substring(modelName.Length - 2))
				If oModelNum > oNum Then oReplaceDoc = model
			Next
			Try
				For Each oOcc As ComponentOccurrence In RefOccs
					If oOcc.OccurrencePath.Count = 1 'Top level
						oOcc.Replace(oReplaceDoc, False)
					End If
				Next
			Catch
				MessageBox.Show("Couldn't replace file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
			End Try
		End If
	End If
Next
InventorVb.DocumentUpdate()
Message 17 of 17

nhowardVJQ4A
Contributor
Contributor

Perfect, thank you @JhoelForshav