Endless loop when using IsFileInActiveProject inside OnFileResolution

<?xml encoding=”UTF-8″>By Adam Nagy

It seems that in Inventor 2016 the behaviour of IsFileInActiveProject has been modified in a way that now it triggers the OnFileResolution event. Which of course means that if you are calling IsFileInActiveProject from inside OnFileResolution then you are creating an endless loop that will bring Inventor down.

OnFileResolution

I’m not sure yet if this change in behaviour is by-design or not, but it could be. I can imagine that now we allow add-ins to chip in when finding files even in case of using IsFileInActiveProject.

In order to avoid the endless loop, you just need to use a flag variable to signal if OnFileResolution has been called as a result of IsFileInActiveProject or not. Here is my VBA clsEvents class that shows what I mean:

Dim WithEvents faEvents As FileAccessEvents
' To avoid recursion
Private <strong>inside</strong> As Boolean
Private Sub Class_Initialize()
Set faEvents = ThisApplication.FileAccessEvents
inside = False
End Sub
Private Sub faEvents_OnFileResolution( _
ByVal RelativeFileName As String, _
ByVal LibraryName As String, _
CustomLogicalName() As Byte, _
ByVal BeforeOrAfter As EventTimingEnum, _
ByVal Context As NameValueMap, _
FullFileName As String, _
HandlingCode As HandlingCodeEnum)
Dim str As String
Dim loc As LocationTypeEnum
' To avoid recursion we use a flag variable
' called "inside"
If Not <strong>inside</strong> Then
<strong>inside</strong> = True
Dim dpm As DesignProjectManager
Set dpm = ThisApplication.DesignProjectManager
Call dpm.IsFileInActiveProject( _
"1.ipt", _
loc, _
str)
<strong>inside</strong> = False
HandlingCode = HandlingCodeEnum.kEventHandled
End If
End Sub

Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading