Removing iTriggers with iLogic

Removing iTriggers with iLogic

donnie.morris
Enthusiast Enthusiast
441 Views
10 Replies
Message 1 of 11

Removing iTriggers with iLogic

donnie.morris
Enthusiast
Enthusiast

Good afternoon,

 

I have a template file that has an iTrigger to open a form after the document is opened. Is there anyway to remove this iTrigger on a SaveAs or when the Template is saved as a new document?

 

Thanks,

0 Likes
442 Views
10 Replies
Replies (10)
Message 2 of 11

WCrihfield
Mentor
Mentor

When you say iTrigger, do you mean that there is an iLogic rule listed under the 'After Open Document' event within the Event Triggers dialog, and that rule launches the form?  If so, is that rule a 'local' rule (saved within the document) or an external rule, and what is its name?

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 11

donnie.morris
Enthusiast
Enthusiast

It is a local rule:

ShowForm

iLogicForm.Show("Test Stand")

 

0 Likes
Message 4 of 11

WCrihfield
Mentor
Mentor

OK.  So your template contains a local rule named "ShowForm", and it is listed under the "After Open Document" event within the Event Triggers dialog right?  If so, this iLogic rule below should work to remove that rule from under that specific event in the Event Triggers.

 

Dim oDoc As Document = ThisDoc.Document
Dim oSet As PropertySet = Nothing
Dim oInternalName As String = "{2C540830-0723-455E-A8E2-891722EB4C3E}"
If oDoc.PropertySets.PropertySetExists(oInternalName, oSet) Then
	If oSet.Count = 0 Then Exit Sub
	For Each oProp As Inventor.Property In oSet
		If oProp.Value = "ShowForm" Then
			If oProp.PropId >= 400 And oProp.PropId <= 499 Then
				oProp.Delete
			End If
		End If
	Next
	If oSet.Dirty Then oDoc.Save
End If

 

However, implementing this rule in such a way that it will only effect the newly created document, instead of the original template document, and only after you use SaveAs, will be the much more challenging part to arrange.  So, I'm thinking that since you generally do not save your template document, and when you use SaveAs, it is saving the new document instead of the template document, we could make this new iLogic rule an external rule, then put this new rule's name under the "After Save Document" event of the template document.  This too will be tricky because the change will be made after a save, instead of before a save, so it will need to be saved again at some point due to the change.  And if you ever save your template directly (not SaveAs), it will remove that rule from the event triggers of the template.  Since I'm not 100% sure if this scenario will effect the new document or the template itself, you may want to try it out on some test files first (like a copy of the template).

 

By the way, here are some links (Link1, Link2, Link3) to a couple of my contribution posts about working with the Event Triggers settings by iLogic, for better reference and understanding about what I'm doing in the code above.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 11

donnie.morris
Enthusiast
Enthusiast

The rule works great, does exactly what I want. The issue is running the rule on the SaveAs file. I have a SaveAs rule that is saving the template to a new file in a new folder but can't seem to make the "Remove iTriggers" fire properly.

 

Here's where I am:

' Get current location of this file
Dim ExportPath As String = ThisDoc.Path

' Check that this file has been saved and actually exists on disk
If String.IsNullOrEmpty(ExportPath) Then
    MsgBox("This file has not yet been saved and doesn't exist on disk! - please save it first",64, "Lord iLogic")
Return
End If

' Define folder browse dialog
Dim Dialog = New FolderBrowserDialog()

' Set options for folder browser dialog
Dialog.SelectedPath = ExportPath
Dialog.ShowNewFolderButton = True
Dialog.Description = "Choose Folder for Export..."

' Show dialog box
If DialogResult.OK = Dialog.ShowDialog() Then
    ' User clicked 'ok' on dialog box - capture the export path
    ExportPath = Dialog.SelectedPath & "\"
    
Else
    ' User clicked 'cancel' on dialog box - exit
    Return
End If
oFileName = iProperties.Value("Project", "Part Number")

' Define the filename of the file to be exported - in this case it is a iam file extension
ExportFilename = oFileName & ".iam"

 'Do export operation (Using save As)
Try
	    ThisDoc.Document.SaveAs(ExportPath & ExportFilename, True)
Catch
    MsgBox("File export failed...", 64, "Lord iLogic")
End Try

' Ask user if they want to open (launch) the file we just exported...
If MsgBox("File exported: " & _
	ExportPath & ExportFilename & vbLf & vbLf & _
	"Do you want to open the Assembly Now?", 36, "Lord iLogic - File Exported") = 6 Then
	
	'Create a NameValue Map
	Dim oNVMap As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
	'Add option to NameValueMap
	oNVMap.Value("DesignViewRepresentation") = "Default"
	
	ThisApplication.Documents.OpenWithOptions(ExportPath & ExportFilename, oNVMap, True)

End If

iLogicVb.RunRule("Remove iTriggers")

 If you think external rules would work better I can explore that path. I also need to add to your rule by removing another iTrigger named "reset", how would that plug into your rule?

0 Likes
Message 6 of 11

donnie.morris
Enthusiast
Enthusiast

Placing this 

iLogicVb.RunRule("Remove iTriggers")

as the fist line of code in the SaveAs rule works perfectly; however, like you stated this removes the iTriggers for the template file. I think the work around is having the template check into Vault. This way the temple can simply be  refreshed from Vault as the reset to the template. Not ideal, but it works.

0 Likes
Message 7 of 11

WCrihfield
Mentor
Mentor

After your SaveAs line, I see that you are using the Documents.Open to open the newly created document.  You can create a Document or AssemblyDocument Type variable before that block of code, then set the value of that variable using that Open line.  Then use that document variable as a reference to the newly created document.  Then use the iLogicVb.Automation.RunRule(oDoc, "Remove iTriggers") method to run the rule (lets you specify target document directly), instead of the regular iLogicVb.RunRule() method.  See if that will work for you.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 8 of 11

donnie.morris
Enthusiast
Enthusiast

Sorry I'm totally new to iLogic and don't quite understand what you mean, I adapted this code from a program I found on this board. 

 

I hate to ask but could you be a little more clear?

Also how would I add another iTrigger to your original code posted?

 

Dim oDoc As Document = ThisDoc.Document
Dim oSet As PropertySet = Nothing
Dim oInternalName As String = "{2C540830-0723-455E-A8E2-891722EB4C3E}"
If oDoc.PropertySets.PropertySetExists(oInternalName, oSet) Then
	If oSet.Count = 0 Then Exit Sub
	For Each oProp As Inventor.Property In oSet
		If oProp.Value = "ShowForm" Then
			If oProp.PropId >= 400 And oProp.PropId <= 499 Then
				oProp.Delete
			End If
		End If
	Next
	For Each oProp As Inventor.Property In oSet
		If oProp.Value = "Reset" Then
			If oProp.PropId >= 400 And oProp.PropId <= 499 Then
				oProp.Delete
			End If
		End If
	Next
	If oSet.Dirty Then oDoc.Save
End If
0 Likes
Message 9 of 11

WCrihfield
Mentor
Mentor

OK.  Since the original template document had the "ShowForm" rule and event trigger set-up within it, the new document created by the SaveAs method will also have that rule and event trigger set-up within it.  So the new document you create with the SaveAs method will need to be opened, either visibly or invisibly, so that we can get rid of the event trigger setting within it.  Otherwise everything we do will be effecting the original template.  And when we open that new document, we need to capture it to a variable, so that we can work with it after we open it.  By capturing the new document to a variable, then using that variable, we make sure we are not effecting the original template document.

 

Since i don't know if the other rule that is set to an event trigger is set to be triggered by the same event, I created a custom Sub routine that we can use multiple times, and it will remove the specified rule from 'every' event it might be listed under, not just from under one specific event, to make it simpler.  Also, if your rule named "Remove iTriggers" is an external rule, then we will have to use a slightly different method to run it (iLogicVb.Automation.RunExternalRule(oDoc, "Remove iTriggers")).  By the way...is the rule doing the SaveAs task also a local rule within that template file, or is it an external rule?

Here is a slightly modified version of the one code you posted, where I am using the other RunRule methods near the end.

 

' Get current location of this file
Dim ExportPath As String = ThisDoc.Path

' Check that this file has been saved and actually exists on disk
If String.IsNullOrEmpty(ExportPath) Then
    MsgBox("This file has not yet been saved and doesn't exist on disk! - please save it first",64, "Lord iLogic")
	Return
End If

If System.IO.Directory.Exists(ExportPath) = False Then
	MsgBox("The directory does not exist.", , "")
	Return
End If

' Define folder browse dialog
Dim Dialog = New System.Windows.Forms.FolderBrowserDialog

' Set options for folder browser dialog
Dialog.SelectedPath = ExportPath
Dialog.ShowNewFolderButton = True
Dialog.Description = "Choose Folder for Export..."

' Show dialog box
If Dialog.ShowDialog = System.Windows.Forms.DialogResult.OK Then
    ' User clicked 'ok' on dialog box - capture the export path
    ExportPath = Dialog.SelectedPath & "\"
Else
    ' User clicked 'cancel' on dialog box - exit
    Return
End If
oFileName = iProperties.Value("Project", "Part Number")

' Define the filename of the file to be exported - in this case it is a iam file extension
ExportFilename = oFileName & ".iam"
Dim oNewFullFileName As String = ExportPath & ExportFilename
 'Do export operation (Using save As)
Try
	ThisDoc.Document.SaveAs(oNewFullFileName, True)
Catch
	MsgBox("File export failed...", vbInformation, "Lord iLogic")
End Try
Dim oDoc As Document = Nothing
' Ask user if they want to open (launch) the file we just exported...
If MsgBox("File exported: " & ExportFilename & vbLf & vbLf & _
	"Do you want to 'Visibly' open the Assembly Now?", vbYesNo + vbQuestion, "Lord iLogic - File Exported") = vbYes Then
	'Create a NameValue Map
	Dim oNVMap As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
	'Add option to NameValueMap
	oNVMap.Value("DesignViewRepresentation") = "Default"
	oDoc = ThisApplication.Documents.OpenWithOptions(oNewFullFileName, oNVMap, True)
	'if the rule named "Remove iTriggers" is a local rule within the new assembly then
	iLogicVb.Automation.RunRule(oDoc, "Remove iTriggers")
	'But if that rule is an external rule, then use the following line:
	'iLogicVb.Automation.RunExternalRule(oDoc, "Remove iTriggers")
	Return
End If
If IsNothing(oDoc) Then
	oDoc = ThisApplication.Documents.Open(oNewFullFileName, False)
	iLogicVb.Automation.RunRule(oDoc, "Remove iTriggers")
	'iLogicVb.Automation.RunExternalRule(oDoc, "Remove iTriggers")
	oDoc.Save
	oDoc.Close
End If

 

And if you want to totally skip the whole other rule named "Remove iTriggers", and just put that code into your SaveAs rule, you can use that custom Sub routine I mentioned above, which I will post below:

 

Sub RemoveRuleFromAllEventTriggers(oDoc As Document, oRuleName As String)
	Dim oSet As PropertySet = Nothing
	Dim oInternalName As String = "{2C540830-0723-455E-A8E2-891722EB4C3E}"
	If oDoc.PropertySets.PropertySetExists(oInternalName, oSet) Then
		If oSet.Count = 0 Then Exit Sub
		For Each oProp As Inventor.Property In oSet
			'an external rule's name will start with "file://"
			If oProp.Value = oRuleName Or _
				oProp.Value = "file://" & oRuleName Then
				oProp.Delete
			End If
		Next
		If oSet.Dirty Then oDoc.Save
	End If
End Sub

 

Then if you choose to use this custom Sub routine, instead of running the "Remove iTriggers" rule, you can call that sub to run similar to this:  (just supply the input document reference we captured, and the name of the rule you want removed from the event triggers of that document.)

 

Sub Main
	Dim oDoc As Document = ThisDoc.Document
	RemoveRuleFromAllEventTriggers(oDoc, "ShowForm")
End Sub

 

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 10 of 11

donnie.morris
Enthusiast
Enthusiast

I have tried all your options and now I'm getting the following error using your original option:

Dim oDoc As Document = ThisDoc.Document
Dim oSet As PropertySet = Nothing
Dim oInternalName As String = "{2C540830-0723-455E-A8E2-891722EB4C3E}"
If oDoc.PropertySets.PropertySetExists(oInternalName, oSet) Then
	If oSet.Count = 0 Then Exit Sub
	For Each oProp As Inventor.Property In oSet
		If oProp.Value = "ShowForm" Then
			If oProp.PropId >= 400 And oProp.PropId <= 499 Then
				oProp.Delete
			End If
		End If
	Next
	If oSet.Dirty Then oDoc.Save
End If

donniemorris_0-1651845417681.png

 

donniemorris_1-1651845483373.png

 

0 Likes
Message 11 of 11

WCrihfield
Mentor
Mentor

According to the info in the 'More Info' tab of that error message, it appears that it is encountering that error at the point it is trying to execute the Save method near the end of the code.  You may need to either comment that line out, or delete it to get past that error.  It may be a timing related issue.  For instance, it may be too soon after the new document's original save for it to save again, because it may still be processing.  There may be a way to use a Try...Catch block with a loop within that delays then tries again, but I'm not sure if all that complexity would be necessary.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes