Resolve File in Inventor Drawing using iLogic

Resolve File in Inventor Drawing using iLogic

C_Haines_ENG
Collaborator Collaborator
314 Views
4 Replies
Message 1 of 5

Resolve File in Inventor Drawing using iLogic

C_Haines_ENG
Collaborator
Collaborator

I think someone had previously mentioned this might not be possible, but I was wondering if anyone knew how to trigger the "Resolve File" error that occurs when you change the file name of a part number attached to a drawing. (See image) 

 

C_Haines_ENG_0-1709647153824.png

Im looking to automate this so that I can quickly change part names and not have to sit through endless menus to re-establish the link. 

 

Does anyone know how to do this?

 

0 Likes
315 Views
4 Replies
Replies (4)
Message 2 of 5

WCrihfield
Mentor
Mentor

Hi @C_Haines_ENG.  I am not personally familiar with using this technique, but I found an 'Event' that you can 'monitor' for 'file resolution'.  It is located under the FileAccessEvents object, which is located under the Application object.  There are actually two resources available, one is a method (Sub routine) FileAccessEvents.FireOnFileResolution which appears to let you manually fire/trigger the event, and provide some information for it; while the other is an actual Event (FileAccessEvents.OnFileResolution) that we can 'listen for'.  These are not available in the Event Triggers dialog, so you would have to create your own custom event handler code routine for catching the event which contains code for how to react when that event happens.  Those types of codes are usually used within Inventor add-ins, instead of in iLogic rules, but it might be possible to take advantage if it using multiple external iLogic rules, if designed and set-up properly/carefully.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 5

C_Haines_ENG
Collaborator
Collaborator

Im not too worried about triggering the vent automatically, just that the whole file selection is automated.

 

The process would just be opening a batch of files that need resolving, running the iLogic rule and it relinking the part to a part number with the same name as the drawing document.

0 Likes
Message 4 of 5

WCrihfield
Mentor
Mentor

Hi @C_Haines_ENG.  Just a small update...but I also found something else that might be useful to you, on the same subject.  There is a method called ResolveFile available under the DesignProjectManager object, which is available under the Application object.  It appears to just be for getting the FullFileName of the file you are looking for, by supplying a starter directory path for where to start looking for it, and a file name to search for.  So, it may not be super helpful, but at least it is something that is available, so I just wanted to add that info here, just in case it might help some.

Dim sFFN As String = ThisApplication.DesignProjectManager.ResolveFile(SourcePath, DestinationFileName)

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 5

rjay75
Collaborator
Collaborator

I have some code that does this very thing. You will have to modify it to get the new names and paths. But what it does is look through all the document references to for references that are unresolved. Looks up the file name in a spreadsheet that contains the new number, and reformats the file name to the new path. It then uses the ResolveFile method to get the location of the new file. If it finds it it replaces the reference with the new location. If it can't replace the reference (as the files may not be a renamed copy of the file but something different) it replaces the occurrence of the file in the assembly).

 

Imports SysIO = System.IO
Public Sub Main()
	Dim lookupSpreadSheet = "PathToSpreadsheet.xlsx"
	Dim sheetName = "Parts"
	Dim column = "PartNumber"
	
	Dim docDescriptors = New List(Of DocumentDescriptor)
	For Each docDesc As DocumentDescriptor In ThisDoc.Document.ReferencedDocumentDescriptors
		If docDesc.ReferenceMissing AndAlso Not docDesc.ReferenceSuppressed Then
			docDescriptors.Add(docDesc)
		End If
	Next
	For Each doc As Document In ThisDoc.Document.AllReferencedDocuments
		docDescriptors.AddRange(GetMissingReferences(doc))
	Next
	If docDescriptors.Any() Then
		'MessageBox.Show(String.Join(vbCrLf, docDescriptors.Select(
		'Function(d As DocumentDescriptor)
		'	Return GetLookupNumber(d)
		'End Function )))
		Dim newNumbers = New List(Of DescPair)
		For Each desc As DocumentDescriptor In docDescriptors
			Dim nameParts As Parts = GetLookupNumber(desc)
			Dim row = GoExcel.FindRow(lookupSpreadSheet, sheetName, column, "=", nameParts.LookupValue)
			Dim newNumber As String = ""
			If row > 0 Then
				nameParts.LookupResult = GoExcel.CellValue("A" & row).ToString()
			Else
				nameParts.LookupResult = SysIO.Path.GetFileNameWithoutExtension(desc.FullDocumentName)
			End If
			newNumbers.Add(New DescPair() With {.NameParts = nameParts, .Descriptor = desc})
		Next
		newNumbers.ForEach(Sub(d As DescPair) ReplaceReference(d))
	End If
	ThisDoc.Document.Update2(True)
End Sub

Public Function GetMissingReferences(doc As Document) As List(Of DocumentDescriptor)
	Dim docDescriptors = New List(Of DocumentDescriptor)
	For Each docDesc As DocumentDescriptor In doc.ReferencedDocumentDescriptors
		If docDesc.ReferenceMissing AndAlso Not docDesc.ReferenceSuppressed Then
			docDescriptors.Add(docDesc)
		End If
	Next
	Return docDescriptors
End Function

Public Function GetLookupNumber(docDesc As DocumentDescriptor) As Parts
	Dim rParts As Parts = New Parts()
	Dim lookupValue = SysIO.Path.GetFileNameWithoutExtension(docDesc.FullDocumentName)
	Dim nameParts = lookupValue.Split(" ")
	If nameParts.Length > 1 Then
		lookupValue = nameParts(0)
		rParts.Rest = String.Join(" ", nameParts.Skip(1))
	End If
	Dim parts = lookupValue.Split("-")
	If parts.Length > 1 Then
		If parts(0).Length = 2 Then
			lookupValue = "0" & lookupValue
		End If
	End If
	rParts.LookupValue = lookupValue
	Return rParts
End Function

Public Sub ReplaceReference(dp As DescPair)
	Dim docDesc As DocumentDescriptor = dp.Descriptor
	Dim newNumber As String = dp.NameParts.LookupResult
	If Not String.IsNullOrWhiteSpace(dp.NameParts.Rest)
		newNumber = newNumber & " " & dp.NameParts.Rest
	End If
	Dim newName = newNumber & SysIO.Path.GetExtension(docDesc.FullDocumentName)
	Dim oldName = SysIO.Path.GetFileName(docDesc.FullDocumentName)
	Dim newPath = ThisApplication.DesignProjectManager.ResolveFile(SysIO.Path.GetDirectoryName(ThisDoc.Document.FullDocumentName), newName, Nothing)
	If Not String.IsNullOrEmpty(newPath) Then
		Dim repDoc As Document = ThisApplication.Documents.Open(newPath, False)
		Try
			docDesc.ReferencedFileDescriptor.ReplaceReference(newPath)
		Catch
			ReplaceByOccurrence(dp, newPath)
		End Try
		repDoc.Close()
	Else
		Dim oldPath = ThisApplication.DesignProjectManager.ResolveFile(SysIO.Path.GetDirectoryName(ThisDoc.Document.FullDocumentName), oldName, Nothing)
		If Not String.IsNullOrEmpty(oldPath) Then
			Dim repDoc As Document = ThisApplication.Documents.Open(oldPath, False)
			Try
				docDesc.ReferencedFileDescriptor.ReplaceReference(oldPath)
			Catch
				ReplaceByOccurrence(dp, oldPath)
			End Try
			repDoc.Close()
		End If
	End If
End Sub

Public Sub ReplaceByOccurrence(dp As DescPair, newPath As String)
	Dim parent As Document = dp.Descriptor.Parent
	Dim asmDoc As AssemblyDocument = TryCast(parent, AssemblyDocument)
	If asmDoc Is Nothing Then Return
	Dim asmDef As AssemblyComponentDefinition = asmDoc.ComponentDefinition
	For Each occ As ComponentOccurrence In asmDef.Occurrences
		If occ.ReferencedDocumentDescriptor.FullDocumentName = dp.Descriptor.FullDocumentName Then
			occ.Replace(newPath, True)
			Exit For
		End If
	Next
End Sub

Public Structure DescPair
	Public Descriptor As DocumentDescriptor
	Public NameParts As Parts
End Structure

Public Structure Parts 
	Public LookupValue As String
	Public LookupResult As String
	Public Rest As String
End Structure