Pick Method on Triggered Rule not Working

Pick Method on Triggered Rule not Working

mgaunt3DSG
Enthusiast Enthusiast
232 Views
4 Replies
Message 1 of 5

Pick Method on Triggered Rule not Working

mgaunt3DSG
Enthusiast
Enthusiast

I've made a Rule that swaps out material specific Appearances for my Part Files that I'd like to trigger automatically using the Material Change Event Trigger. I make use of the CommandManager.Pick Method to select exceptions.

 

Running the rule manually works fine, but when I add the Rule to the Material Change Event Trigger for Parts I get a "Expecting object to be local" or "(0x80004005 (E_FAIL))" error from the line with the Pick Method.

 

This is the code that I have in the Rule:

While True
	Dim oFace As Face = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceFilter, "Select a face to read the Appearance")
	
	If IsNothing(oFace) Then Exit While
	
	MessageBox.Show(oFace.Appearance.DisplayName)
End While

 

I am using Inventor 2025.2

0 Likes
233 Views
4 Replies
Replies (4)
Message 2 of 5

WCrihfield
Mentor
Mentor

Hi @mgaunt3DSG.

  • Is that the whole rule?
    • Because it sounds like the rule was changing appearances, not just getting visual feedback about the display name of appearances.
  • Is that an external or internal rule?
  • Was that rule put into the Event Triggers dialog on the 'Parts' tab (to effect all parts), or on the 'This Document' tab for specific parts?
  • Does it get triggered to run while an assembly is the 'active' document (while visibly editing an assembly, or an assembly was active before the code process started)?

When we use the iLogic 'Event Triggers' dialog to automate running our iLogic rules, we introduce the possibility of that rule getting triggered to run while the document the event was triggered by is not the 'active' document.  Since the 'Pick' function is designed to act upon the 'active' document, it may not like the awkward situation of a component occurrence in an assembly representing a part within an assembly being the source of the event, causing the rule to run which is trying to act upon that part, but that part is not the 'active' document, an assembly is.

 

And the built-in behavior of the iLogic Event Triggers tries to make sure that any rule that gets triggered to run, tries to act upon the document which the event originated from.  But sometimes the rule may need to be set up correctly to take full advantage of that behavior.

Just one theory that came to mind, but not sure if that is the problem in this case...just guessing.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 5

mgaunt3DSG
Enthusiast
Enthusiast
  • The code is just a minimized snippet of the larger rule I have written; it generates the same error.
  • It is an external rule.
  • It is under the 'Parts' tab.
  • The code snippet does get triggered and has the same problem whether I have the part document open of if I am editing it in an Assembly.
    • Thanks for mentioning this, I found that running my larger rule manually fails if I am editing the part in an Assembly while using the Pick Method. Additionally, the Pick Method allows me to select faces that are from other components in the Assembly and there doesn't appear to be a way to limit the Pick Method further via Selection Filter Enum.
0 Likes
Message 4 of 5

WCrihfield
Mentor
Mentor

Since I am not familiar with that odd error message, I can only guess at what could be causing it.  But it seems like you would want to know specifically which 'part' just had its material changed, when that rule gets triggered to run by the 'material change' event, instead of just randomly selecting some faces of whatever model happens to be on your screen at that moment in time.  Because of that, I would suggest a 'tweak' within your main code, but not sure if you already have something like this in that rule.

Dim oEventDoc As Inventor.Document = ThisDoc.Document
Dim oActiveDoc As Inventor.Document = ThisApplication.ActiveDocument
If oEventDoc IsNot oActiveDoc Then
'don't react to this event, maybe use Return, or Exit Sub
Else
'react to the event with the rest of your code here
End If

...or some variation using a similar strategy.  You could also check if the active document is an assembly, and if so, attempt to find one (or more) component occurrences within that assembly that reference the 'event document', then use a HighlightSet to highlight which ones just had their material changes.  But since I am not familiar with your assemblies, figuring out which ones may have just been effected by a material change may already be super obvious, and that helper may not be needed.  Just some thoughts.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 5

mgaunt3DSG
Enthusiast
Enthusiast

I apologize for the confusion, I am not being clear. The snippet I have provided is from me debugging my larger rule, the message box in the snippet is just letting me know it is working right. The larger rule loops through all the features in a part file and swaps the appearance of the same to a color coordinated version of the body appearance. I am using the Pick Method to toggle the selected face's appearance between 'Precision Machined' and feature appearance.

 

For clarity, here is the larger rule as I have it now:

Option Explicit On
Sub main
	' Get the active document
	Dim oDoc As PartDocument = ThisApplication.ActiveEditDocument
	
	' Get the component definition
	Dim oCompDef As PartComponentDefinition = oDoc.ComponentDefinition
	
	' Define colors
	Dim oTG As TransientObjects = ThisApplication.TransientObjects
	Dim oOrange As Color = oTG.CreateColor(255, 128, 26)
	Dim oGreen As Color = oTG.CreateColor(0, 140, 26)
	Dim oBlue As Color = oTG.CreateColor(0, 128, 255)
	
	' Loop bodies
	Dim oBody As SurfaceBody
	For Each oBody In oCompDef.SurfaceBodies
		' Get Appearance to modify
		Dim oAppearance As Asset = oBody.Appearance
		Dim oAppearanceName As String = oAppearance.DisplayName
		
		If oAppearance.LocalType = kGenericAssetType Then
			' Copy Appearance
			oAppearance = oAppearance.Duplicate
		 	
			If oAppearance.Item("generic_diffuse").HasConnectedTexture = True Then
				Dim oImage As String
				oImage = oAppearance.Item("generic_diffuse").ConnectedTexture.Item("unifiedbitmap_Bitmap").Value
				oAppearance.Item("generic_diffuse").HasConnectedTexture = False
				
				' Add Texture to bumpmap if bumpmap does not exist
				If oImage <> "" And oAppearance.Item("generic_bump_map").HasConnectedTexture = False Then
				    oAppearance.Item("generic_bump_map").HasConnectedTexture = True
				    oAppearance.Item("generic_bump_map").ConnectedTexture.Item("unifiedbitmap_Bitmap").Value = oImage
				End If
			End If
		Else
			Try
				' Find "Fixed" Appearance
				oAppearanceName = oAppearanceName & " - Fixed"
				
				' define appearance library by name
				Dim assetLib As AssetLibrary
				assetLib = ThisApplication.AssetLibraries.Item("Inventor Material Library")
				
				' define colors to work with
				Dim libAsset1 As Asset
				libAsset1 = assetLib.AppearanceAssets.Item(oAppearanceName)
				
				' try to Copy the asset locally.
				Try
					oAppearance = libAsset1.CopyTo(oDoc)
				Catch
				' assume error means it's already local
					oAppearance = oDoc.Assets(oAppearanceName)
				End Try
			Catch
				MessageBox.Show("Unable to find " & oAppearanceName & " in Inventor Material Library", "No Fixed Appearance") ', MessageBoxIcon.Error)
				Exit Sub
			End Try
		End If
		
		' Define Appearances
		Dim oOrangeAppearance As Asset = GetOrCreateAppearanceAsset(oAppearance, oAppearanceName & " - Non-Machined", oOrange)
		
		oAppearance.Item("generic_bump_map").HasConnectedTexture = True
		oAppearance.Item("generic_bump_map").ConnectedTexture.Item("unifiedbitmap_Bitmap").Value = "C:\Users\Public\Documents\Autodesk\Inventor 2025\Textures\surfaces\Metal_15.bmp"
		Dim oGreenAppearance As Asset = GetOrCreateAppearanceAsset(oAppearance, oAppearanceName & " - Rough Cut", oGreen)
		Dim oBlueAppearance As Asset = GetOrCreateAppearanceAsset(oAppearance, oAppearanceName & " - Machined", oBlue)
		
		oAppearance.Item("generic_bump_map").ConnectedTexture.Item("unifiedbitmap_Bitmap").Value = "C:\Users\Public\Documents\Autodesk\Inventor 2025\Textures\bumpmaps\Metal_15_bump.bmp"
		Dim oGreenHoleAppearance As Asset = GetOrCreateAppearanceAsset(oAppearance, oAppearanceName & " - Rough Hole", oGreen)
		Dim oBlueHoleAppearance As Asset = GetOrCreateAppearanceAsset(oAppearance, oAppearanceName & " - Precision Hole", oBlue)
		
		' Loop through Features
		Dim oFeature As PartFeature
		For i As Integer = 1 To oBody.AffectedByFeatures.Count
			oFeature = oBody.AffectedByFeatures.Item(i)
			If TypeOf oFeature Is HoleFeature Then
				If oFeature.IsClearanceHole Or oFeature.Tapped Then
					Feature.Color(oFeature.Name) = oGreenHoleAppearance.Name
				Else
					Feature.Color(oFeature.Name) = oBlueHoleAppearance.Name
				End If
			Else
				Feature.Color(oFeature.Name) = oGreenAppearance.Name
			End If
		Next i
		
		' Set first Feature to non-machined
		oFeature = oBody.CreatedByFeature
		Feature.Color(oFeature.Name) = oOrangeAppearance.Name
		
		' Determine cut direction in the future?
	Next oBody
	
	' Update Override Faces
	Dim oFaces As ObjectCollection
	oFaces = oDoc.AttributeManager.FindObjects("com.autodesk.inventor.FaceAttributes", "FaceColor")
	
	Dim oFace As Face	
	For Each oFace In oFaces
		oBody = oFace.Parent
		Dim oAppearance As Asset = oBody.Appearance
		
		If oFace.Appearance.DisplayName Like "*Rough Cut" Then
			oFace.Appearance = oDoc.Assets(oAppearance.DisplayName & " - Rough Cut")
		Else If oFace.Appearance.DisplayName Like "* Machined" Then
			oFace.Appearance = oDoc.Assets(oAppearance.DisplayName & " - Machined")
		Else If oFace.Appearance.DisplayName Like "*Rough Hole" Then
			oFace.Appearance = oDoc.Assets(oAppearance.DisplayName & " - Rough Hole")
		Else If oFace.Appearance.DisplayName Like "*Precision Hole" Then
			oFace.Appearance = oDoc.Assets(oAppearance.DisplayName & " - Precision Hole")
		End If
	Next oFace
	
	' Pick Precision Machined Surfaces
	While True
		oFace = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceFilter, "Select a face to toggle Precision Machined")
		
		' If nothing gets selected then we're done	
		If IsNothing(oFace) Then Exit While
		
		oBody = oFace.Parent
		Dim oAppearance As Asset = oBody.Appearance
		Try
			oAppearance = oDoc.Assets(oAppearance.DisplayName & " - Fixed")
		Catch
		End Try
		
		If oFace.AppearanceSourceType = AppearanceSourceTypeEnum.kFeatureAppearance Then
			If TypeOf oFace.CreatedByFeature Is HoleFeature Then
				oFace.Appearance = oDoc.Assets(oAppearance.DisplayName & " - Precision Hole")
			Else
				oFace.Appearance = oDoc.Assets(oAppearance.DisplayName & " - Machined")
			End If
		Else
			oFace.AppearanceSourceType = AppearanceSourceTypeEnum.kFeatureAppearance
		End If
	End While
	
	' Delete unused Appearances
	Dim foundUnused As Boolean
	Do
		foundUnused = False
		Dim a As Asset
		For Each a In oDoc.Assets
			If Not a.IsUsed And a.AssetType = kAssetTypeAppearance Then
				a.Delete
				foundUnused = True
			End If
		Next
	Loop While foundUnused
End Sub

Function GetOrCreateAppearanceAsset(ByVal oAppearance As Asset, ByVal oName As String, ByVal oColor As Color) As Asset
	Dim oAssets As Assets = ThisApplication.ActiveEditDocument.Assets 
	Dim oAsset As Asset
	Try ' If Appearance exists
		oAsset = oAssets.Item(oName)
	Catch ' If Appearance doesn't exist
		oAsset = oAppearance.Duplicate
		oAsset.DisplayName = oName
		Dim oApColor As ColorAssetValue = oAsset.Item("generic_diffuse")
		oApColor.Value = oColor
	End Try
	Return oAsset
End Function

 

0 Likes