VBA
Discuss AutoCAD ActiveX and VBA (Visual Basic for Applications) questions here.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Make an escape from For Next loop

11 REPLIES 11
SOLVED
Reply
Message 1 of 12
Jedimaster
533 Views, 11 Replies

Make an escape from For Next loop

I have a VBA routine that allows the user to select multiple drawing files to be processed.If the user selects several files this can be a lengthy process. There is no espace without shutting down AutoCAD altogther. Is there a way to create an escape from my loop.

 

Sub SelectManyFiles()
 'Get list of drawings
 If FileList.Count > 0 Then
  With FileList
   For i = 1 To .Count

 

    'Need an escape here

 

    dwgName = .Item(i)
    If Dir(dwgName) <> "" Then
     FileSystem.SetAttr (dwgName), vbNormal
     Application.Documents.Open dwgName
     ThisDrawing.Close False
    End If
   Next
  End With
 End If
End Sub

 

Thanks for any input.

11 REPLIES 11
Message 2 of 12
RICVBA
in reply to: Jedimaster

just place an

exit for

as a consequence of a check that you must implement.

for instance you could track the time elapsed from the beginning of your "For-Next" loop and decide to "exit for" if it reaches a limit time

 

on the other hand if you already know a limit file number to process, than you can place an "If" statement before the "For-Next" loop

 

bye

Message 3 of 12
Jedimaster
in reply to: RICVBA

I want the user to have the ablitty to abort the routine.How do I get ot to invoke from esc key or function key.

Message 4 of 12
RICVBA
in reply to: Jedimaster

Sorry but never faced such a need
I'd try and solve it two ways:
1) anytime your loop starts a new iteration you make a form appear for only a few second showing a button to let the user abort the routine. you may want to make the form appear on every definite time interval elapsing
2) event handlers, such as clicking somewhere in the drawing

Message 5 of 12
Jedimaster
in reply to: RICVBA

I tried a modeless userform it kind of works in 2007. 2007 does not show the whole form but does you can click on the red X and close out. In 2010 if I go modeless I have no control at alll until the operation is complete. I am unfarmillar with a click event in the drawing itself.

Message 6 of 12
norman.yuan
in reply to: Jedimaster

Are you using AutoCAD 2010 32-bit or 64-bit?

 

Using VBA modeless form is, in general, not a good choice, or a necessary choice. Did you added AcFocus control tothe form?

 

If your AutoCAD 2010 is 64-bit, things likely got worse because of the fact that VBA reains as 32-bit out-process part, which may make modeless form event nmore difficult to control.

 

For a btach process like opening/closing a list of drawing files in AutoCAD, it should not be a problem with modal form, and you can easily stop the loop by clicking a button or hit Esc key, if the code is written correctly.

Norman Yuan

Drive CAD With Code

EESignature

Message 7 of 12
Jedimaster
in reply to: norman.yuan

So I tried running everthing from the user form, now the dialog does not show up until the batch operation is complete.

Message 8 of 12
norman.yuan
in reply to: Jedimaster

Well, without seeing your code, it is hard to tell where you did is wrong. But ot is fairly simple to run a batch from a modal form and to stop the process.

 

Following is the code to do this (A simple form with 2 buttons on it: "Start" and "Stop"):

 

The macro code:

 

Option Explicit

Public Sub BatchTest()
    UserForm1.Show
End Sub

 

The form code:

 

Option Explicit

Private files() As String
Private Const FILE_FOLDER As String = "C:\Temp"
Private stopped As Boolean
Private app As AutoCAD.AcadApplication

Private Sub cmdStart_Click()

    cmdStart.Enabled = False
    cmdStop.Enabled = True
    stopped = False
    
    Dim i As Integer
    For i = 0 To UBound(files)
    
        lblDrawing.Caption = files(i)
        DoEvents
        
        If stopped Then Exit For
        
        processDrawing FILE_FOLDER & "\" & files(i)
    
    Next
    
    cmdStart.Enabled = True
    cmdStop.Enabled = False
    lblDrawing.Caption = ""
    
    If i < UBound(files) Then
        MsgBox "Processing was stopped before completion!"
    Else
        MsgBox "Processing completed!"
    End If

End Sub

Private Sub cmdStop_Click()
    stopped = True
End Sub

Private Sub processDrawing(fileName As String)
    
    Dim dwg As AcadDocument
    
    Set dwg = app.Documents.Open(fileName)
    
    ''Do something this this document
    
    dwg.Close False
    
End Sub

Private Sub UserForm_Initialize()

    Set app = ThisDrawing.Application
    
    ''Assume there are a list of file names already available
    ReDim files(4)
    files(0) = "Test1.dwg"
    files(1) = "Test2.dwg"
    files(2) = "Test3.dwg"
    files(3) = "Test4.dwg"
    files(4) = "Test5.dwg"
    
End Sub

 

As you can see. there is NO WAY the fialog box not showing until after the processing: the dialog fomr MUST show first so that the process can start (by clicking the "Start" button.

 

The trick to allow the click on "Stop" button to be acccepted is to call DoEvents() in the loop.

 

 

Norman Yuan

Drive CAD With Code

EESignature

Message 9 of 12
Jedimaster
in reply to: norman.yuan

I think my problem was that I did not have a start button. The batch process started from the UserForm_Initialize. Will try again with start button.

Message 10 of 12
norman.yuan
in reply to: Jedimaster

If you want the processing starts as soon as the form is shown, then you can start it in the Activate event handler instead of Initialize event handler. Just to make sure the process only runs once (because if you show a messagebox oon top the form, dismiss the message box would cause Activate event fires again). This way, you can only have "Stop" button on the form, and as soon as the process is completed or stopped, the form is unloaded.

 

You may also handle Form_QueryClose event to prevent user from closing the form accidently by clicking "x" on upper-right corner.

Norman Yuan

Drive CAD With Code

EESignature

Message 11 of 12
cadger
in reply to: norman.yuan

thanks ricvba - it seems that with civil 3d you can right-click and generate a boundary... which is an AEC Polygon and doing intersects with that works.  Not sure yet how to just test for overlaping intersections and not include touching intersections...

Message 12 of 12
cadger
in reply to: cadger

00Ps wrong place...

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

”Boost