How to exit a rule?

How to exit a rule?

Anonymous
Not applicable
4,297 Views
9 Replies
Message 1 of 10

How to exit a rule?

Anonymous
Not applicable

Hey,

 

i have a rule wich fills in iproperties from an excel file.

i wanted to add a progressbar wich works but it has a cancel option.

Now i want to make my rule stop when i hit that cancel button.

 

When i hit cancel, it will close the progressbar but not end the rule

 

Sub Main()

doc = ThisDoc.ModelDocument
 'check file type
 
	Dim oAsmCompDef As AssemblyComponentDefinition
	oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

	Dim oLeafOccs As ComponentOccurrencesEnumerator 
    oLeafOccs = oAsmCompDef.Occurrences.AllLeafOccurrences 

	Dim totalCount As Integer 
	totalCount = 0
	
	For Each oOcc In oLeafOccs 
		totalCount = totalCount + 1 
	Next 

	progBar = New TestProgressBar(ThisApplication)
    progBar.Start(totalCount)
	
	Dim cnt As Integer = 0		

For Each oOccurrence In oAsmCompDef.Occurrences.AllReferencedOccurrences(oAsmCompDef)  

   		Dim oUOM As UnitsOfMeasure
  		oUOM = oOccurrence.Definition.Document.UnitsOfMeasure
		
		Dim Aream As String 
		Aream = Round(iProperties.AreaOfComponent(oOccurrence.Name)/1000000,2)
		
   		Dim strArea As String
    	strArea = Aream & "m²"
		
		map = Left(ThisDoc.FileName(False),6) & ".xlsx"
		xls = "C:\VaultWorkspace\Projecten"
		info = xls & "\" & map

		Dim Titel As String
		Titel = GoExcel.CellValue(info, "Pro-info","C10")
		
		Dim Subject As String
		Subject = GoExcel.CellValue("C11")
		
		Dim Manager As String
		Manager = GoExcel.CellValue("C41")
		
		Dim Project As String
		Project = GoExcel.CellValue("C39")
		
		Dim Klant As String
		Klant = GoExcel.CellValue("C33")
		
		Dim Projectnaam As String
		Projectnaam = GoExcel.CellValue("C10")
		
		Dim Subbeschrijving As String
		Subbeschrijving = GoExcel.CellValue("C11")
		
		
		
	If Not TypeOf oOccurrence.Definition Is VirtualComponentDefinition Then

	If Not oOccurrence.Definition.BOMStructure = 51973 Then
	
		cnt = cnt + 1
		progbar.update(cnt,totalCount)
		
		iProperties.Value(oOccurrence.Name, "Custom", "Aream") = strArea 		
		iProperties.Value(oOccurrence.Name, "Summary", "Title") = Titel
		iProperties.Value(oOccurrence.Name, "Summary", "Subject") = Subject
		iProperties.Value(oOccurrence.Name, "Summary", "Manager") = Manager
		iProperties.Value(oOccurrence.Name, "Project", "Project") = Project
		iProperties.Value(oOccurrence.Name, "Custom", "Klant") = Klant
		iProperties.Value(oOccurrence.Name, "Custom", "Projectnaam") = Projectnaam
		iProperties.Value(oOccurrence.Name, "Custom", "Subbeschrijving") = Subbeschrijving
		If Left (oOccurrence.Name,6) = Left (ThisDoc.FileName(0),6) Then
			iProperties.Value(oOccurrence.Name, "Project", "Part Number") = Mid (oOccurrence.Name,8,12)
		Else
			iProperties.Value(oOccurrence.Name, "Project", "Part Number") = " "
		End If
		
	End If
	End If
Next
progBar.Finish()
MsgBox("Finished")
End Sub


Public Class TestProgressBar
    Private WithEvents progBar As Inventor.ProgressBar
    Private invApp As Inventor.Application

    Public Sub New(InventorApp As Inventor.Application)
        invApp = InventorApp
    End Sub

    Public Sub Start(totalCount)
        progBar = invApp.CreateProgressBar(False,totalCount,"Filling in iproperties", True)
    End Sub
	
	Public Sub Update(cnt, totalCount)
		progBar.Message = ("Processing file " & cnt & " of " & totalCount & "...")
		progBar.UpdateProgress 
	End Sub
	
	Public Sub Finish()
		progBar.Close()
	End Sub

    Private Sub progBar_OnCancel() Handles progBar.OnCancel
        progBar.Close()
        MsgBox("Filling in iPoperties is cancelled")
	return	
    End Sub
End Class

 

0 Likes
Accepted solutions (1)
4,298 Views
9 Replies
Replies (9)
Message 2 of 10

bradeneuropeArthur
Mentor
Mentor

use this in your code:

Dim oTransMgr As TransactionManager
oTransMgr = m_inventorApplication.TransactionManager
Dim oTrans As Transaction
oTrans = oTransMgr.StartTransactionForDocumentOpen("Your Command")
Your Command here

oTrans.End()

                 

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 3 of 10

Anonymous
Not applicable

I have put your snippet in my code like this:

but now the cancel button woudn't do anything

 

Public Class TestProgressBar
    Private WithEvents progBar As Inventor.ProgressBar
    Private invApp As Inventor.Application
	
    Public Sub New(InventorApp As Inventor.Application)
        invApp = InventorApp
    End Sub

    Public Sub Start(totalCount)
        progBar = invApp.CreateProgressBar(False,totalCount,"Filling in iproperties", True)
    End Sub
	
	Public Sub Update(cnt, totalCount)
		progBar.Message = ("Processing file " & cnt & " of " & totalCount & "...")
		progBar.UpdateProgress 
	End Sub
	
	Public Sub Finish()
		progBar.Close()
	End Sub

    Private Sub progBar_OnCancel() Handles progBar.OnCancel
		Dim oTransMgr As TransactionManager
		Dim m_inventorApplication As inventor.Application
		oTransMgr = m_inventorApplication.TransactionManager
		Dim oTrans As Transaction
		oTrans = oTransMgr.StartTransactionForDocumentOpen("Description Of the Command")
       
		progBar.Close()
                MsgBox("Filling in iPoperties is cancelled")
		
		oTrans.End()
		
    End Sub
End Class
0 Likes
Message 4 of 10

Anonymous
Not applicable

Got the cancel button working again but this actualy does nothing

i get the message that filling in iproperties is cancelled.

but the rule still runs in sub main

 

Public Class TestProgressBar
    Private WithEvents progBar As Inventor.ProgressBar
    Private invApp As Inventor.Application

    Public Sub New(InventorApp As Inventor.Application)
        invApp = InventorApp
    End Sub

    Public Sub Start(totalCount)
        progBar = invApp.CreateProgressBar(False,totalCount,"Filling in iproperties", True)
    End Sub
	
	Public Sub Update(cnt, totalCount)
		progBar.Message = ("Processing file " & cnt & " of " & totalCount & "...")
		progBar.UpdateProgress 
	End Sub
	
	Public Sub Finish()
		progBar.Close()
	End Sub

    Private Sub progBar_OnCancel() Handles progBar.OnCancel
		Dim oTransMgr As TransactionManager
		oTransMgr = invApp.TransactionManager
		Dim oTrans As Transaction
		oTrans = oTransMgr.StartTransactionForDocumentOpen("Description Of the Command")
	
        progBar.Close()
		oTrans.End()
        MsgBox("Filling in iPoperties is cancelled")
		
		
    End Sub
End Class
0 Likes
Message 5 of 10

philip1009
Advisor
Advisor

That cancel button uses a VB function called Events, and iLogic doesn't support Events, you'll have to go through the Inventor VBA Editor or develop an Add-In to support the Cancel button and other events such as mouse and keyboard interfaces.  I have a solution if you'd like to try it, it doesn't involve anything outside of iLogic, it's just a clever trick I found.  Let me know and I'll post it here.

0 Likes
Message 6 of 10

Anonymous
Not applicable

Thanks for the explanation.

And yes i woud like to try out your trick 🙂

0 Likes
Message 7 of 10

philip1009
Advisor
Advisor
Accepted solution

In your iLogic Browser, go to External Rules and add a rule called Cancel, then put in this code:

SyntaxEditor Code Snippet

Public Class CancelButton
	Dim Cancel As Boolean
	Sub Main
		SharedVariable("Cancel") = True
	End Sub
End Class

Then in Global Forms, add a Form called Cancel, for editing the form, just drag Cancel from External Rules to make a button and that's it.

 

To use this Cancel button on a rule, put these lines before executing the main chunk of your Rule:

 

SyntaxEditor Code Snippet

iLogicVb.RunExternalRule("Cancel")
If SharedVariable("Cancel") = True Then SharedVariable("Cancel") = False
iLogicForm.ShowGlobal("Cancel", FormMode.NonModal)

Running the External Rule creates the Boolean variable "Cancel" and then converts it to a Shared Variable, which is similar to a Public Variable.  The second line changes the variable from True to False to check against while running your rule, then the 3rd line makes the form with the Cancel button pop-up.  Pressing the button again will change the Variable to False

 

While running the loop that takes a while, at the beginning of the loop add this line:

SyntaxEditor Code Snippet

If SharedVariable("Cancel") = True Then GoTo EndRule

This checks if the variable has been changed back to True by the user pressing the Cancel button in the form.  Then just have a line beginning with EndRule: and then the code will skip to that line in the rule.

 

Let me know if you have any questions.

 

EDIT:  Keep in mind that this method isn't the most stable, sometimes you have to click the button a couple of times before your mouse click actually hits the button, and you do have to manually close the form as there's no way for iLogic to close forms.

Message 8 of 10

Anonymous
Not applicable

Thanks in advance, i will test this monday when i am back at work.

0 Likes
Message 9 of 10

Anonymous
Not applicable

Thanks, this works good 🙂

 

but is there a way that i can make the progBar_OnCacel() change the variable?

 

Private Sub progBar_OnCancel() Handles progBar.OnCancel
        progBar.Finish()
        MsgBox("Filling in iPoperties is cancelled")
		SharedVariable("Cancel") = True
    End Sub

When i try this it gives me the error,; object reference not set to an instance of an object

 

That way the global form woudn't be needed 

0 Likes
Message 10 of 10

philip1009
Advisor
Advisor

Unfortunately no, the code to use that cancel button is something along the lines of Private WithEvents Inventor.ProgressBar, then using the sub you have there, and Events are not supported in iLogic.  In order to use Events, you have to rewrite your rule in the VBA Editor or develop an Add-In with Microsoft Visual Studio where you have full access to Visual Basic.  I haven't used either of those methods of code writing, so I won't be much help there.

0 Likes