Need a rule to trigger an event when performing an action in a form

Need a rule to trigger an event when performing an action in a form

khantheking1995
Contributor Contributor
487 Views
6 Replies
Message 1 of 7

Need a rule to trigger an event when performing an action in a form

khantheking1995
Contributor
Contributor

I have two code mentioned below that is used in the form(Image 1), PartSelect button activates Partselect rule and pulls data and the StockNumBeamList drops down trigger an external rule "ChangeBeam" that i need to add manually everytime (image 2) I create a assembly so I need a lines to automating this process  so I want lines need to be added to "Partselect" rule that add "ChangeBeam" rule to eventtrigger (as in image 2) If its not already added.

Image  1

khantheking1995_0-1698565844163.png

Image 2

khantheking1995_1-1698565943227.png

"Partselect" Rule.

 

Sub main
	Dim oInvApp As Inventor.Application = ThisApplication
	Dim oAsmDoc As AssemblyDocument = oInvApp.ActiveDocument
	Dim oContentCenter As ContentCenter = oInvApp.ContentCenter
	Dim oUsParams As UserParameters = oAsmDoc.ComponentDefinition.Parameters.UserParameters
	
	Dim oUOfM As UnitsOfMeasure = oAsmDoc.UnitsOfMeasure
	Dim oCustom As PropertySet = oAsmDoc.PropertySets("Inventor User Defined Properties")
	Dim oDesignProps As PropertySet = oAsmDoc.PropertySets("Design Tracking Properties")
	
	Dim oWeigt_KG, oWeigt_Lbs, oStockNumb As Inventor.Property
	Try : oWeigt_KG = oCustom("Existing size Weight Kg")
	Catch : oWeigt_KG = oCustom.Add("", "Existing size Weight Kg") : End Try
	Try : oWeigt_Lbs = oCustom("Existing Part Weight Lbs")
	Catch : oWeigt_Lbs = oCustom.Add("", "Existing Part Weight Lbs") : End Try
	Try : oCustom("CAM-Weight Kg").Value = ""
	Catch : oCustom.Add("", "CAM-Weight Kg") : End Try
	Try : oCustom("CAM-Weight Lbs").Value = ""
	Catch : oCustom.Add("", "CAM-Weight Lbs") : End Try
	oStockNumb = oDesignProps("Stock Number")
	oWeigt_KG.Value = ""
	oWeigt_Lbs.Value = ""
	oStockNumb.Value = ""
	
	Dim oOcc As ComponentOccurrence
	oOcc = oInvApp.CommandManager.Pick(SelectionFilterEnum.kAssemblyLeafOccurrenceFilter, _
										"Select component...")
	If oOcc Is Nothing Then Exit Sub
		
	Dim oDef As PartComponentDefinition = oOcc.Definition
	Dim oCCPropSet As PropertySet
	Try : oCCPropSet = oDef.Document.PropertySets.Item("Content Library Component Properties")
	Catch : MessageBox.Show("The Part is not a CC part.") : End Try
	
	Dim dMass As Double = oOcc.MassProperties.Mass
	oWeigt_KG.Value = Round(dMass, 2)
	oWeigt_Lbs.Value = Round(oUOfM.ConvertUnits(dMass, UnitsTypeEnum.kKilogramMassUnits, _
												UnitsTypeEnum.kLbMassMassUnits), 2)
	oStockNumb.Value = oDef.Document.PropertySets("Design Tracking Properties") _
												("Stock Number").Value	
	
	Dim familyId As Inventor.Property = oCCPropSet.Item("FamilyId")
	Dim MemberId As String = oCCPropSet.Item("MemberId").Value
	Dim oFamily As ContentFamily
	oFamily = oContentCenter.GetContentObject("v3#" & familyId.Value & "#")
	Dim oContentNode As ContentTreeViewNode = oContentCenter.TreeViewTopNode
	Dim oLengthM As UserParameter
	Dim oRow As ContentTableRow = oFamily.TableRows.Item(MemberId)
	Dim iCount As Integer = oFamily.TableRows.Count - 1
    Dim iStockNumb As Integer = GetNumbColomn(oFamily.TableColumns, {"Stock Number" })
    If iStockNumb = -1 Then
        MessageBox.Show("Failed to get column - Stock Number", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Exit Sub
    End If

    Dim iWeight As Integer = GetNumbColomn(oFamily.TableColumns, {"Specific mass", "Mass","Mass [Kg/mm]"})
    If iWeight = -1 Then
        MessageBox.Show("Failed to get column - Mass", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Exit Sub
    End If
	Dim sStockNumb(iCount), sWeight(iCount) As String
	For i As Integer = 0 To iCount
		sStockNumb(i) = """" & oFamily.TableRows(i + 1)(iStockNumb).Value & """"		
		sWeight(i) = """" & oUOfM.ConvertUnits(oFamily.TableRows(i + 1)(iWeight).Value,
							oFamily.TableColumns(iWeight).Units, "kg/m") & """"
	Next i
	Dim oColumn As ContentTableColumn = oFamily.TableColumns("STOCKNUMBER")
	Call SetParamList(oUsParams, "StockNumbBeamList", sStockNumb)
	Call SetParamList(oUsParams, "WeightKg", sWeight)
	Try : oLengthM = oUsParams("LengthM") : Catch
	oLengthM = oUsParams.AddByValue("LengthM", Nothing, _
										UnitsTypeEnum.kMeterLengthUnits) : End Try
	oLengthM.Value = oDef.Parameters.UserParameters("B_L").Value
End Sub

Private Function SetParamList(ByVal oUsParams As UserParameters,
								ByVal sNameParam As String,
								ByVal sList() As String)
	Dim oUsParam As UserParameter
	Try : oUsParam = oUsParams(sNameParam)
	Catch
		oUsParam = oUsParams.AddByValue(sNameParam, "", _
										UnitsTypeEnum.kTextUnits)
	End Try
	Call oUsParam.ExpressionList.ClearAll()
	oExpList = oUsParam.ExpressionList
	Call oExpList.SetExpressionList(sList, True, 1)
	If oExpList.AllowCustomValues Then oExpList.AllowCustomValues = False
	If Not oExpList.CustomOrder Then oExpList.CustomOrder = True
End Function

Private Function GetNumbColomn(ByVal oColomn As ContentTableColumns, ByVal sName() As String) As Integer
    For i As Integer = 1 To oColomn.Count
        If sName.Any(Function(name) oColomn(i).DisplayHeading.ToLower().Contains(name.ToLower())) Then
            Return i
        End If
    Next i
    Return -1
End Function

"ChangeBeam" List

 

Dim oDoc As AssemblyDocument = ThisDoc.Document
Dim oUOfM As UnitsOfMeasure = oDoc.UnitsOfMeasure
Dim oCustom As PropertySet = oDoc.PropertySets("Inventor User Defined Properties")
Dim oDef As AssemblyComponentDefinition = oDoc.ComponentDefinition
Dim dMassN As Double = Round(oDef.MassProperties.Mass, 2)
Dim oUsParams As UserParameters = oDef.Parameters.UserParameters
Dim oListBeam, oWeightList, oLengthM As UserParameter
Try 
	oListBeam = oUsParams("StockNumbBeamList")
	oWeightList = oUsParams("WeightKg")
	oLengthM = oUsParams("LengthM")
Catch : Exit Sub : End Try
If String.IsNullOrEmpty(oListBeam.Value) Then Exit Sub
Dim oWeigt_KG, oWeigt_Lbs, oNewAsm_KG, oStockNumb As Inventor.Property
Try : oCustom("Existing Assembly weight(Kg)").Value = dMassN
Catch : oCustom.Add(dMassN, "Existing Assembly weight(Kg)") : End Try
Try : oNewAsm_KG = oCustom("New Assembly weight(Kg)")
Catch : oNewAsm_KG = oCustom.Add("", "New Assembly weight(Kg)") : End Try
Try : oExWeigt_KG = oCustom("Existing size Weight Kg")
Catch : oExWeigt_KG = oCustom.Add("", "Existing size Weight Kg") : End Try
Try : oWeigt_KG = oCustom("CAM-Weight Kg")
Catch : oWeigt_KG = oCustom.Add("", "CAM-Weight Kg") : End Try
Try : oWeigt_Lbs = oCustom("CAM-Weight Lbs")
Catch : oWeigt_Lbs = oCustom.Add("", "CAM-Weight Lbs") : End Try
For i As Integer = 1 To oListBeam.ExpressionList.Count
	If oListBeam.ExpressionList(i) = oListBeam.Expression Then
		Dim dLengthM As Double
		dLengthM = oUOfM.ConvertUnits(oLengthM.Value, kCentimeterLengthUnits, kMeterLengthUnits)
		Dim sWeight As String = oWeightList.ExpressionList(i)
		Dim dKG As Double = sWeight.Substring(1, sWeight.Length-2) * dLengthM
		oWeigt_KG.Value = Round(dKG, 2)
		oWeigt_Lbs.Value = Round(oUOfM.ConvertUnits(dKG, UnitsTypeEnum.kKilogramMassUnits, _
												UnitsTypeEnum.kLbMassMassUnits), 2)
		oNewAsm_KG.Value = Round(dMassN - oExWeigt_KG.Value + dKG, 2)
	End If
Next

 @Andrii_Humeniuk 

0 Likes
Accepted solutions (1)
488 Views
6 Replies
Replies (6)
Message 2 of 7

WCrihfield
Mentor
Mentor

Hi @khantheking1995.  I have a couple questions.  Is the form shown in Image1 always a local iLogic Form (saved within that document), or a global iLogic Form?  You also mentioned that the rule named "ChangeBeam" was an external, but it looks like an internal one in Image2.  Just want to make sure we have all the details correct first.  Is your plan to have both the rules be external ones, or just the "ChangeBeam" rule?

 

These may seem like small details, but they can make a difference in how certain things need to be coded, especially where the Event Triggers automation part is concerned.  If the form is (or will be) a global one (instead of internal), then you may want to add an Apply button to the form, if you want to see changes happen while the form remains open.  This is because changes made in a global form need to be 'sent' to the document with the Apply button, before any changes will happen.  When using an internal form, the changes are sent as the are made in the form.  Big difference.  Another important detail is the settings behind the button that runs the rule.  While that button is highlighted, make sure the 'On Click Action' property of the button it is set to one of the options the last two options which start with "Apply".  However, that option will only be available if the rule it is supposed to run is an internal one, not an external one, so keep that in mind also.  When we add the rule to the Event Triggers by code, that adds an iProperty object to a hidden PropertySet, and the value of the iProperty is usually just the name of the rule.  However, if the rule in an external one, we need to add extra text to the start of the rule's name to make it work, so knowing whether the rule is internal or external is important at that step.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 7

WCrihfield
Mentor
Mentor

Hi @khantheking1995.  I am not sure how familiar you are with the process of adding a rule to the event triggers by code, but it is fairly complicated, and there is no 'official' documentation for it.  I will provide an example iLogic rule that can be used for that one narrow specific task of adding an external rule named "ChangeBeam" to the "Any User Parameter Change" event in the 'This Document' tab of the Event Triggers, but it may be difficult to reuse for adding other rules to other events, because every event has a different internal name that must be used as the first part of the Property name, and every event needs to be put within the correct PropId range for that event, or it will not work.  That document must also have something already in it related to the iLogic add-in for it to work properly, so this routine includes adding a temporary internal iLogic rule to the document, then deleting it again when its done.  You may be able to run this rule from within one of the other rules, where appropriate.  I'm about to leave for the day though, so I'll try to check back tomorrow.

Sub Main
	Dim oDoc As Document = ThisDoc.Document
	Dim sRuleName As String = "ChangeBeam"
	If iLogicVb.Automation.GetRule(oDoc, sRuleName) Is Nothing Then
		sRuleName = "file://" & sRuleName
	End If
	Dim iLogicClientId As String = "{3BDD8D79-2179-4B11-8A5A-257B1C0263AC}"
	If oDoc.DocumentInterests.HasInterest(iLogicClientId) = False Then
		iLogicVb.Automation.AddRule(oDoc, "DeleteMe", "")
	End If
	Dim sEventType As String = "AfterAnyUserParamChange"
	Dim oSet As PropertySet = Nothing
	Dim sInternalName As String = "{2C540830-0723-455E-A8E2-891722EB4C3E}"
	If Not oDoc.PropertySets.PropertySetExists(sInternalName, oSet) Then
		oSet = oDoc.PropertySets.Add("_iLogicEventsRules", sInternalName)
		oNewlyCreated = True
	End If
	Dim oProp As Inventor.Property = Nothing
	For iPropID As Integer = 3000 To 3099 'range is specific to each event
		Try
			oProp = oSet.ItemByPropId(iPropID)
			If oProp.Value = sRuleName Then
				'MsgBox("This rule has already been added to this event trigger.  Exiting.", vbExclamation, "")
				Logger.Info("This rule has already been added to this event trigger.")
				If iLogicVb.Automation.GetRule(oDoc, "DeleteMe") IsNot Nothing Then
					iLogicVb.Automation.DeleteRule(oDoc, "DeleteMe")
				End If
				Exit Sub 'exit routine
			End If
			Continue For 'there is already an existing rule at this iPropID, so skip to next
		Catch
			'no property was found with that PropID, and this rule has not been found, so now create one for this rule
			oProp = oSet.Add(sRuleName, sEventType & iPropID, iPropID)
			Exit For 'exit the loop, because we have already added the rule to the event
		End Try
	Next
	If iLogicVb.Automation.GetRule(oDoc, "DeleteMe") IsNot Nothing Then
		iLogicVb.Automation.DeleteRule(oDoc, "DeleteMe")
	End If
End Sub

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 7

khantheking1995
Contributor
Contributor
It is a global ilogic form and all are external rules. earlier i created as local form and rule in document then I understoood global form and external rule will be easy to do a one time setup and I can use it on all assemblies throught
0 Likes
Message 5 of 7

khantheking1995
Contributor
Contributor
I want this to be a one time setup on any system so that it can easily used. so going for external rule and global Ilogic form
0 Likes
Message 6 of 7

khantheking1995
Contributor
Contributor

I placed the Ilogic in the location as below 

"C:\Users\Public\ilogics"

khantheking1995_0-1698802451696.png

 

0 Likes
Message 7 of 7

WCrihfield
Mentor
Mentor
Accepted solution

You may already know this, but just keep in mind that wherever you want to locate your external iLogic rules, you should include that directory within the iLogic Configuration settings, under the External Rule Directories section.  Doing so has multiple advantages, and may also be needed when adding that rule to the Event Triggers.

WCrihfield_0-1698837655026.png 

WCrihfield_1-1698838153984.png

 

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes