- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi everyone. Firstly I have set an event trigger to run the rule below whenever a Part file is opened. The code then handles the event of user changing the "Thickness" parameter. But unfortunately it fires twice for every sheetmetal .ipt file that it detects the parameter change. I get that it fires twice for every parameter change, but I have still added the "kAfter" condition and still get this problem. I have very limited knowledge of event handlers and I get dizzy trying to understand how it works. Despite repeatedly tweaking different aspects of the code, I couldn't get it to run only once per thickness update.
Also, please let me know:
1. If this approach is efficient and won't cause lags to the user.
2. Will this create duplication of event handling triggers when the user has multiple sheet metal .ipt's open at the same time ?
Sub main
Dim doc As Document = ThisApplication.ActiveDocument
If doc.DocumentType <> kpartdocumentobject
Exit Sub
Else
Logger.Info("Program running on " & doc.DisplayName)
End If
'----CREATION DATE CHECK----
Dim fso As Object = CreateObject("Scripting.FileSystemObject")
Dim filePath As String = doc.FullFileName
Dim creationDate As Date = fso.GetFile(filePath).DateCreated
Dim thresholdDate As Date = "#1/1/2025#"
If creationDate < thresholdDate
Logger.Info("Date check failed: " & creationDate)
Exit Sub
Else
Logger.Info("Date check passed: " & creationDate)
End If
'----SHEET METAL CHECK----
If doc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}"
Dim Mevents As ModelingEvents = ThisApplication.ModelingEvents
AddHandler Mevents.OnParameterChange, AddressOf ParameterHasChanged 'PARAMETER CHANGE EVENT
Dim docevents As DocumentEvents = doc.DocumentEvents
AddHandler docevents.onclose, AddressOf closedoc 'DOCUMENT CLOSED EVENT
End If
End Sub
Sub ParameterHasChanged(doc As Document, Changedparameter As Parameter, BeforeOrAfter As EventTimingEnum, oContext As NameValueMap, ByRef HandlingCode As HandlingCodeEnum)
Logger.Info("Parameter named - " & Changedparameter.Name & " - has changed")
If BeforeOrAfter = kafter
If Changedparameter.Name = "Thickness"
'Thickness has changed
Logger.Info("Initiating Execution due to change in " & Changedparameter.Name)
Dim IsExecuting As Boolean
Execute(doc, IsExecuting)
Logger.Info("Completed Successfully.")
End If
End If
End Sub
Sub closedoc(beforeorAfter As EventTimingEnum, oContext As Object, ByRef handlingcode As HandlingCodeEnum)
'just removes the handler
Dim doc As Document = ThisApplication.ActiveDocument
Dim docevents As DocumentEvents = doc.DocumentEvents
Dim Mevents As ModelingEvents = ThisApplication.ModelingEvents
Dim UIE As UserInterfaceEvents = ThisApplication.UserInterfaceManager.UserInterfaceEvents
If beforeorAfter = EventTimingEnum.kBefore
RemoveHandler Mevents.OnParameterChange, AddressOf ParameterHasChanged
Logger.Info("Event handler for Model Parameter is removed on " & doc.DisplayName)
End If
End Sub
Sub Execute(doc As PartDocument, IsExecuting As Boolean)
If IsExecuting = True
Exit Sub
End If
IsExecuting = True
iLogicVb.Automation.RulesEnabled = False
'Main execution code yet to be added here
'I have added the log line below for now
Logger.Info("Execution !")
iLogicVb.Automation.RulesEnabled = True
IsExecuting = False
End Sub
Solved! Go to Solution.