iLogic SaveAndReplace

iLogic SaveAndReplace

hkempeneers
Advocate Advocate
631 Views
2 Replies
Message 1 of 3

iLogic SaveAndReplace

hkempeneers
Advocate
Advocate

Hi,

 

In an assembly we have to Save and Replace all referenced documents.

Very briefly: in Factory Design every inserted asset is getting a very long code in the File Name. We need short names.

And for every asset we need an unique filenumber, codes in FDU are not unique even thought it seems so..

I wrote the following code (every thing works, except the SaveAndReplace at the end) but I get the following error message.

Who knows what to do?

 

InvDoc = ThisDoc.Document

 Dim oRefDocs As DocumentsEnumerator = InvDoc.AllReferencedDocuments
 Dim oRefDoc As Document
 For Each oRefDoc In oRefDocs
	 	
		'Only filenames without $
		Dim oNameCheck As String = oRefDoc.FullDocumentName
		'i = MessageBox.Show(oNameCheck, "document nr", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
		If oNameCheck.Contains("$") Then
			'Skip it!
		Else
			Dim oPartNumber As String = oRefDoc.PropertySets("Design Tracking Properties").Item("Part Number").Value
			Dim Time As DateTime = DateTime.Now
			Dim Format As String = "$yyyymmddThhmmss"
			Dim DTG As String = Time.ToString(Format)
			
			'format model file name
			'For now a fixed path is given
			Dim Folder_Location As String = "C:\VaultWS2\WSP Systems\Factory Assets\"
		
'			'Assembly
			If oRefDoc.DocumentType = Inventor.DocumentTypeEnum.kAssemblyDocumentObject Then
				Dim oNewFileName As String = Folder_Location & DTG & "_" & oPartNumber & ".iam"
'				i = MessageBox.Show(oNewFileName, "nieuwe naam assy", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)

'			'Part	
			ElseIf oRefDoc.DocumentType = Inventor.DocumentTypeEnum.kPartDocumentObject Then
				Dim oNewFileName As String = Folder_Location & DTG & "_" & oPartNumber & ".ipt"
'				i = MessageBox.Show(oNewFileName, "nieuwe naam prt", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
		
			End If
		End If
		
'		'Save And Replace 
	     oRefDoc.Document.SaveAs(oNewFileName, True)
		FileChange = True
Next

 iLogicVb.UpdateWhenDone = True	
 

 

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

JhoelForshav
Mentor
Mentor
Accepted solution

Hi @hkempeneers 

Since you dim the variable oNewFileName inside specific if statements it doesn't exist outside them. I moved it so that the object exists all the way. I also removed the big Else portion of the code and wrote the condition to skip documents like this instead:

If oNameCheck.Contains("$") Then Continue For

Finally, since you're traversing the referenced documents, not the assembly occurrences I had to change the SaveAs-line to this:

oRefDoc.SaveAs(oNewFileName, True)

 And added a line to replace the reference in the assembly to the new file:

InvDoc.ComponentDefinition.Occurrences.AllReferencedOccurrences(oRefDoc)(1).Replace(oNewFileName, True)

 

So the complete code looks like this:

InvDoc = ThisDoc.Document

Dim oRefDocs As DocumentsEnumerator = InvDoc.AllReferencedDocuments
Dim oRefDoc As Document
For Each oRefDoc In oRefDocs

	'Only filenames without $
	Dim oNameCheck As String = oRefDoc.FullDocumentName

	'i = MessageBox.Show(oNameCheck, "document nr", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
	If oNameCheck.Contains("$") Then Continue For
	'Skip it!
	Dim oNewFileName As String
	Dim oPartNumber As String = oRefDoc.PropertySets("Design Tracking Properties").Item("Part Number").Value
	Dim Time As DateTime = DateTime.Now
	Dim Format As String = "$yyyymmddThhmmss"
	Dim DTG As String = Time.ToString(Format)

	'format model file name
	'For now a fixed path is given
	Dim Folder_Location As String = "C:\VaultWS2\WSP Systems\Factory Assets\"

	'			'Assembly
	If oRefDoc.DocumentType = Inventor.DocumentTypeEnum.kAssemblyDocumentObject Then
		oNewFileName = Folder_Location & DTG & "_" & oPartNumber & ".iam"
		'				i = MessageBox.Show(oNewFileName, "nieuwe naam assy", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)

		'			'Part	
	ElseIf oRefDoc.DocumentType = Inventor.DocumentTypeEnum.kPartDocumentObject Then
		oNewFileName = Folder_Location & DTG & "_" & oPartNumber & ".ipt"
		'				i = MessageBox.Show(oNewFileName, "nieuwe naam prt", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)

	End If


	'		'Save And Replace 
	oRefDoc.SaveAs(oNewFileName, True) 'Removed ".Document" here - you already have the document object
	'Replace in assembly
	InvDoc.ComponentDefinition.Occurrences.AllReferencedOccurrences(oRefDoc)(1).Replace(oNewFileName, True)
	
	FileChange = True 'Dont know why you have this line
Next

iLogicVb.UpdateWhenDone = True	
Message 3 of 3

hkempeneers
Advocate
Advocate

@JhoelForshav 

It works!

Many thanks, really appreciate

Grtz.