How to exit sub, nested in for loop?

How to exit sub, nested in for loop?

Anonymous
Not applicable
2,130 Views
2 Replies
Message 1 of 3

How to exit sub, nested in for loop?

Anonymous
Not applicable

Probably a very simple solution to this problem, but I cannot for the life of me figure out how to accomplish this.

 

I have a rule that exports a named .dwg to a named folder. This helps me stay organized when batching out parts pre-CNC.

 

This is all fine except I can't figure out how to get a true "cancel" out my rule. When exporting iParts, I have a for loop that iterates through each member. In that loop a sub is called to do the exporting. However, if I have exported the parts I need, or just want to cancel out early, I can't get out of the loop until all of the members have run. I tried "Return", "Exit Sub" and "GoTo" and couldn't get it. I only managed to exit the sub, but still have to finish looping.

 

Maybe I'm just approaching the whole problem wrong.

 

Thoughts appreciated.

 

Thanks,

'Funnybus

Sub Main
Dim oDocPath As String = ThisDoc.Path
Dim oDoc As Document = ThisDoc.Document
Dim oCompDef As ComponentDefinition = oDoc.ComponentDefinition
    Dim sPrtPath As String = (Left(oDocPath,InStrRev(oDocPath, "\"))&"_CNC")
        If Not System.IO.Directory.Exists(sPrtPath) Then 
            MessageBox.Show("Could not locate CNC folder for this project." & vbLf & vbLf & sPrtPath & vbLf, "Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation)
            Return
        End If
        If oDoc.DocumentType <> kPartDocumentObject Then
            MessageBox.Show("Rule can only be run from a part document.", "Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation)
            Exit Sub
        End If
        If oCompDef.IsiPartFactory Then
			Export2iPart(oCompDef, sPrtPath)
        Else
            Export2Part(sPrtPath)
        End If
End Sub

Private Sub Export2Part (sPrtPath As String)
	Dim sPrtNum As String = iProperties.Value("Project", "Description")
	YesNo(sPrtNum, sPrtPath)
End Sub

Private Sub Export2iPart (oCompDef As ComponentDefinition, sPrtPath As String)
Dim oFactory As iPartFactory = oCompDef.iPartFactory
    For Each oRow As iPartTableRow In oFactory.TableRows
        Dim sPrtNum As String = oRow.memberName
        iPart.ChangeRow("", sPrtNum)
		YesNo(sPrtNum, sPrtPath)
	Next
End Sub

Private Sub YesNo (sPrtNum As String, sPrtPath As String)
Dim sPrtFldr As String = sPrtPath & "\" & sPrtNum
Dim sPrtDwg As String = sPrtFldr & "\" & sPrtNum & ".dwg"
oMSG = MessageBox.Show("Create CNC?" & vbLf & vbLf & "Part Number: " & sPrtNum & vbLf & vbLf & sPrtDwg & vbLf, "Export to CNC", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
	If oMSG = vbYes Then
		If System.IO.Directory.Exists(sPrtFldr)
			oMSG2 = MessageBox.Show("Existing folder found: " & vbLf & vbLf & sPrtFldr & vbLf & vbLf & "Overwrite existing folder?" & vbLf, "Existing Folder Found", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
				If oMSG2 = vbYes Then
					System.IO.Directory.CreateDirectory(sPrtFldr)
					Dim oPrt As PartDocument = ThisApplication.ActiveDocument
					oPrt.SaveAs(sPrtDwg, True)
				Else If oMSG2 = vbCancel Then
					'End rule
				End If
		Else
			System.IO.Directory.CreateDirectory(sPrtFldr)
			Dim oPrt As PartDocument = ThisApplication.ActiveDocument
			oPrt.SaveAs(sPrtDwg, True)
		End If
	Else If oMSG = vbCancel Then
		'End rule
	End If
End Sub

 

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

MechMachineMan
Advisor
Advisor
Accepted solution

Your issue is that you have no return value and no prompting to control the for loop; which is the control structure you need control to.

 

Either you need to modify your code to have the prompt within the for loop - as well as the test and exit code -, or do as below and carry along a "controlling" variable to be able to return information to the controlling loop, so you can test it there.

 

Sub Main
Dim oDocPath As String = ThisDoc.Path
Dim oDoc As Document = ThisDoc.Document
Dim oCompDef As ComponentDefinition = oDoc.ComponentDefinition
    Dim sPrtPath As String = (Left(oDocPath,InStrRev(oDocPath, "\"))&"_CNC")
        If Not System.IO.Directory.Exists(sPrtPath) Then 
            MessageBox.Show("Could not locate CNC folder for this project." & vbLf & vbLf & sPrtPath & vbLf, "Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation)
            Return
        End If
        If oDoc.DocumentType <> kPartDocumentObject Then
            MessageBox.Show("Rule can only be run from a part document.", "Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation)
            Exit Sub
        End If
        If oCompDef.IsiPartFactory Then
			Export2iPart(oCompDef, sPrtPath)
        Else
            Export2Part(sPrtPath)
        End If
End Sub

Private Sub Export2Part (sPrtPath As String)
	Dim sPrtNum As String = iProperties.Value("Project", "Description")
	YesNo(sPrtNum, sPrtPath, True)
End Sub

Private Sub Export2iPart (oCompDef As ComponentDefinition, sPrtPath As String)
Dim oFactory As iPartFactory = oCompDef.iPartFactory
     Dim boolContinue As boolean = True
For Each oRow As iPartTableRow In oFactory.TableRows Dim sPrtNum As String = oRow.memberName iPart.ChangeRow("", sPrtNum) YesNo(sPrtNum, sPrtPath, boolContinue)
If boolContinue = False Then
Exit Sub
End if Next End Sub Private Sub YesNo (sPrtNum As String, sPrtPath As String, ByRef boolContinue As Boolean) Dim sPrtFldr As String = sPrtPath & "\" & sPrtNum Dim sPrtDwg As String = sPrtFldr & "\" & sPrtNum & ".dwg" oMSG = MessageBox.Show("Create CNC?" & vbLf & vbLf & "Part Number: " & sPrtNum & vbLf & vbLf & sPrtDwg & vbLf, "Export to CNC", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) If oMSG = vbYes Then If System.IO.Directory.Exists(sPrtFldr) oMSG2 = MessageBox.Show("Existing folder found: " & vbLf & vbLf & sPrtFldr & vbLf & vbLf & "Overwrite existing folder?" & vbLf, "Existing Folder Found", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1) If oMSG2 = vbYes Then System.IO.Directory.CreateDirectory(sPrtFldr) Dim oPrt As PartDocument = ThisApplication.ActiveDocument oPrt.SaveAs(sPrtDwg, True) Else If oMSG2 = vbCancel Then boolContinue = False End If Else System.IO.Directory.CreateDirectory(sPrtFldr) Dim oPrt As PartDocument = ThisApplication.ActiveDocument oPrt.SaveAs(sPrtDwg, True) End If Else If oMSG = vbCancel Then
boolContinue = False
End If
End Sub

 


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 3 of 3

Anonymous
Not applicable

MechMachineMan,

 

Good stuff, I appreciate the help. Carrying a controlling variable seems like the way to go.

 

Thanks!

0 Likes