For Each loop causes code to stop

For Each loop causes code to stop

shastu
Advisor Advisor
369 Views
3 Replies
Message 1 of 4

For Each loop causes code to stop

shastu
Advisor
Advisor

I have some code that is causing my code to stop until I move the mouse.  What is weird is if I run it enough times the problem goes away but if I then close Inventor and reopen the problem comes back.  It doesn't make any sense to me why moving my mouse would make it continue, but it does.  Is there anyway I can re-write this better to make the problem go away?  Thanks in advance.

 

Dim oApp As Inventor.Application
Set oApp = ThisApplication

Dim ODoc As DrawingDocument
Set ODoc = oApp.ActiveDocument

Dim oSheet As DrawingViews
Set oSheet = ODoc.ActiveSheet.DrawingViews

Dim sheetsB As Sheets
Set sheetsB = ODoc.Sheets
Dim sheetB As Sheet

For Each sheetB In sheetsB 'rotates through each sheet
sheetB.Activate
sheetB.update

Dim oDrgViews As DrawingViews
Set oDrgViews = sheetB.DrawingViews
Dim oDrgView As DrawingView
Debug.Print "loop_start" & Time

For Each oDrgView In oDrgViews
Number = Number + 1
If Number = 1 Then
ViewHeight = oDrgView.Height
viewWidth = oDrgView.Width
End If
If Number = 2 Then
ViewLength = oDrgView.Width
End If

If Number = 4 Then
ViewHeightIso = oDrgView.Height
ViewWidthIso = oDrgView.Width
End If

Next
Next

Debug.Print "loop_end" & Time

0 Likes
370 Views
3 Replies
Replies (3)
Message 2 of 4

JelteDeJong
Mentor
Mentor

You might want to have a look at this thread. With 2 possible solutions:

https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/why-execution-is-faster-when-mouse-m...

 

Option 1:

Application.UserInterfaceManager.UserInteractionDisabled = true
 ' Your code here
Application.UserInterfaceManager.UserInteractionDisabled = false

 

Option 2:

Dim savedEnableBU = ThisApplication.DrawingOptions.EnableBackgroundUpdates
ThisApplication.DrawingOptions.EnableBackgroundUpdates = False
Try 
   ' Your code here
Finally
  ThisApplication.DrawingOptions.EnableBackgroundUpdates = savedEnableBU
End Try

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

0 Likes
Message 3 of 4

shastu
Advisor
Advisor

I thought Option 1 worked the first time I tried it.  But now it doesn't again.  I am so confused.  it is the values that I am capturing that is causing it.  If I comment them out, then it isn't a problem.

0 Likes
Message 4 of 4

shastu
Advisor
Advisor

Also if I create a msgbox before the first value is captured (oDrgView.Height) then it is fast as well.  Trying to think of any command that I could use to keep it going and not slow down.  If I actually wait long enough it will eventually continue.  There has to be a way to fix this.

0 Likes