Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
Preston_Reed
357 Views, 3 Replies

Unable to cast COM object error - for loop erroring out on certain sub assemblies, need help understanding

ilogic help.gif

Hello, I have a few iLogic rules that I use to help automate the iProperties on the configurators.  I use them across most of my configurators but every once in a while it errors out on specific subassemblies or components.  In this case, if I suppress sub assembly VTHFAB4-MCI:1 , there is no error.  So I tried to add an if statement just to skip that sub assembly but I am getting this error.  I need help understanding why this would error out for specific components or sub assemblies.  Also help understanding the error message for the conditional statement.  Any help would be greatly appreciated. 

 

Added conditional statement is on sub iPropPartsProject

 

PREED7QR59_0-1666902507619.png

 

 

'iPropertiesRule
	'This rule will change the various iProperties of the Assembly, SubAssemblies and Piece parts.   
	
	'Requires -  User parameters (Text): Designer, Project

Sub main()
	iProp
	iPropColumn
	iPropPartsProject
	iPropPartsDate
	iPropPartsDesigner
End Sub


'Changes Project of Assembly from the input on the form
Sub iProp
	
	iProperties.Value("Project", "Project") = Project

End Sub


'Changes custom Material iProperty for Assembly and Subassemblies, places catelog part number, places desciption based on material
Sub iPropColumn
	
	Select Case PumpSize

		Case "4.0 in"
			
			iProperties.Value("ColumnPipe:1", "Project", "Stock Number") = "PP4SCH40C"
			iProperties.Value("ColumnPipe:1", "Project", "Description") = "PIPE, 4 in SCH40 - PP4SCH40C"
			iProperties.Value("ColumnPipe:1", "Project", "Part Number") = "4 IN COLUMN"

		Case "6.0 in"
			
			iProperties.Value("ColumnPipe:1", "Project", "Stock Number") = "PP6SCH40C"
			iProperties.Value("ColumnPipe:1", "Project", "Description") = "PIPE, 6 in SCH40 - PP6SCH40C"
			iProperties.Value("ColumnPipe:1", "Project", "Part Number") = "6 IN COLUMN"
			
		Case "8.0 in"
			
			iProperties.Value("ColumnPipe:1", "Project", "Stock Number") = "PP8SCH40C"
			iProperties.Value("ColumnPipe:1", "Project", "Description") = "PIPE, 8 in SCH40 - PP8SCH40C"
			iProperties.Value("ColumnPipe:1", "Project", "Part Number") = "8 IN COLUMN"
	
	End Select
	
End Sub


'Goes through all parts & subassemblies and replaces the Project based on form inputs
Sub iPropPartsProject
	
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

'Iterate through all Of the occurrences
Dim oOccurrence As ComponentOccurrence
For Each oOccurrence In oAsmCompDef.Occurrences.AllReferencedOccurrences(oAsmCompDef)  
	
	If oOccurrence = "VTHFAB4-MCI:1" Then
	
	Else
		'create iprop with default Value
		iProperties.Value("Project", "Project") = Project
		'write to component iprops
		iProperties.Value(oOccurrence.Name, "Project", "Project") = _
		iProperties.Value("Project", "Project")
		MessageBox.Show(oOccurrence.Name, "Title")
	End If
	On Error Resume Next
Next

End Sub


'Pulls current date and goes though and updates date for every part, sub assembly and assembly
Sub iPropPartsDate
	
	Dim strFileName As String
	strFileName = ThisDoc.Document.FullFileName
	Dim oFS As Object
	oFS = CreateObject("Scripting.FileSystemObject")
	Dim oDate1 As String
	'Dim oDate2 As String
	oDate1 = oFS.GetFile(strFileName).DateLastModified
	'oDate2 = oFS.GetFile(strFileName).DateCreated

	'MessageBox.Show(oDate1 & " and " & oDate2)

	Dim oAsmCompDef As AssemblyComponentDefinition
	oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

	'Iterate through all Of the occurrences
	Dim oOccurrence As ComponentOccurrence
	For Each oOccurrence In oAsmCompDef.Occurrences.AllReferencedOccurrences(oAsmCompDef)  

			'create iprop with default Value
			iProperties.Value("Project", "Creation Date") = oDate1
			'write to component iprops
			iProperties.Value(oOccurrence.Name, "Project", "Creation Date") = _
			iProperties.Value("Project", "Creation Date")

		On Error Resume Next
	Next

End Sub


'Pulls designer from form and goes though and updates designer for every part, sub assembly and assembly
Sub iPropPartsDesigner
	
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

'Iterate through all Of the occurrences
Dim oOccurrence As ComponentOccurrence
For Each oOccurrence In oAsmCompDef.Occurrences.AllReferencedOccurrences(oAsmCompDef)  
	
		'create iprop with default Value
		iProperties.Value("Project", "Designer") = Designer
		'write to component iprops
		iProperties.Value(oOccurrence.Name, "Project", "Designer") = _
		iProperties.Value("Project", "Designer")
		
	On Error Resume Next
Next

End Sub
Labels (3)
WCrihfield
in reply to: Preston_Reed

Hi @Preston_Reed.  This following line in that Sub you mentioned is definitely not right.

 

If oOccurrence = "VTHFAB4-MCI:1" Then

 

...It is not accessing the components '.Name' property, so it is comparing a component object to a String, which throws the error.  It should have been like this:

 

If oOccurrence.Name = "VTHFAB4-MCI:1" Then

 

As for why certain components causing problems before you introduced that check in there...it may be cause the component represents something like a Content Center part, or maybe it is otherwise ReadOnly for some reason, therefore when you try to change its iProperty, it doesn't like that.  Just one possibility.

PS.  There are also certain kinds of components that sometimes show up when looping through components that you may not expect, such as a 'virtual' component (for BOM use only), or the welds of a weldment type assembly (yea, there is a component for that, believe it or not).

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Preston_Reed
in reply to: WCrihfield

Hey @WCrihfield , I appreciate you, this helped quite a bit.  It is working now.  I do have a follow up question on ReadOnly or Content Center parts.  We are new to automation and iLogic, just now trying to automate our Cad processes.  We don't have Vault yet, so we have a purchased drive with our parts.  The purchased drive is a mess from years of people doing things differently, so I'm cleaning it up as i run into these issues.  Anyways, I certainly have noticed parts/assemblies which use to be a part of content center, ipart, iassembly etc. because they give error messages.  The only way I know how to immediately distinguish a part like this is one that has the fitting icon

like this - https://knowledge.autodesk.com/support/inventor/learn-explore/caas/sfdcarticles/sfdcarticles/Remove-...

 

Is there another way to distinguish parts that use to be a part of content center, ipart, iassembly ect, or should I just hunt them down once I find errors. (currently I suppress items, run the iLogic and see which is the problem) 

 

Heres the fix for the original problem too:

'iPropertiesRule
	'This rule will change the various iProperties of the Assembly, SubAssemblies and Piece parts.   
	
	'Requires -  User parameters (Text): Designer, Project

Sub main()
	iProp
	iPropColumn
	iPropPartsProject
	iPropPartsDate
	iPropPartsDesigner
End Sub


'Changes Project of Assembly from the input on the form
Sub iProp
	
	iProperties.Value("Project", "Project") = Project

End Sub


'Changes custom Material iProperty for Assembly and Subassemblies, places catelog part number, places desciption based on material
Sub iPropColumn
	
	Select Case PumpSize

		Case "4.0 in"
			
			iProperties.Value("ColumnPipe:1", "Project", "Stock Number") = "PP4SCH40C"
			iProperties.Value("ColumnPipe:1", "Project", "Description") = "PIPE, 4 in SCH40 - PP4SCH40C"
			iProperties.Value("ColumnPipe:1", "Project", "Part Number") = "4 IN COLUMN"

		Case "6.0 in"
			
			iProperties.Value("ColumnPipe:1", "Project", "Stock Number") = "PP6SCH40C"
			iProperties.Value("ColumnPipe:1", "Project", "Description") = "PIPE, 6 in SCH40 - PP6SCH40C"
			iProperties.Value("ColumnPipe:1", "Project", "Part Number") = "6 IN COLUMN"
			
		Case "8.0 in"
			
			iProperties.Value("ColumnPipe:1", "Project", "Stock Number") = "PP8SCH40C"
			iProperties.Value("ColumnPipe:1", "Project", "Description") = "PIPE, 8 in SCH40 - PP8SCH40C"
			iProperties.Value("ColumnPipe:1", "Project", "Part Number") = "8 IN COLUMN"
	
	End Select
	
End Sub


'Goes through all parts & subassemblies and replaces the Project based on form inputs
Sub iPropPartsProject
	
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

'Iterate through all Of the occurrences
Dim oOccurrence As ComponentOccurrence
For Each oOccurrence In oAsmCompDef.Occurrences.AllReferencedOccurrences(oAsmCompDef)  
	
	If oOccurrence.Name = "VTHFAB4-MCI-Simplify:1" Then
	ElseIf oOccurrence.Name = "VTHFAB6-MCIA-Simplify:1" Then
	ElseIf oOccurrence.Name = "VTHFAB8-MCI-Simplify:1" Then	
	
	Else
		'create iprop with default Value
		iProperties.Value("Project", "Project") = Project
		'write to component iprops
		iProperties.Value(oOccurrence.Name, "Project", "Project") = _
		iProperties.Value("Project", "Project")
	End If
	On Error Resume Next
Next

End Sub


'Pulls current date and goes though and updates date for every part, sub assembly and assembly
Sub iPropPartsDate
	
	Dim strFileName As String
	strFileName = ThisDoc.Document.FullFileName
	Dim oFS As Object
	oFS = CreateObject("Scripting.FileSystemObject")
	Dim oDate1 As String
	'Dim oDate2 As String
	oDate1 = oFS.GetFile(strFileName).DateLastModified
	'oDate2 = oFS.GetFile(strFileName).DateCreated

	'MessageBox.Show(oDate1 & " and " & oDate2)

	Dim oAsmCompDef As AssemblyComponentDefinition
	oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

	'Iterate through all Of the occurrences
	Dim oOccurrence As ComponentOccurrence
	For Each oOccurrence In oAsmCompDef.Occurrences.AllReferencedOccurrences(oAsmCompDef)  

	If oOccurrence.Name = "VTHFAB4-MCI-Simplify:1" Then
	ElseIf oOccurrence.Name = "VTHFAB6-MCIA-Simplify:1" Then
	ElseIf oOccurrence.Name = "VTHFAB8-MCI-Simplify:1" Then	
		
	Else
		
			'create iprop with default Value
			iProperties.Value("Project", "Creation Date") = oDate1
			'write to component iprops
			iProperties.Value(oOccurrence.Name, "Project", "Creation Date") = _
			iProperties.Value("Project", "Creation Date")

	End If
		On Error Resume Next
	Next

End Sub


'Pulls designer from form and goes though and updates designer for every part, sub assembly and assembly
Sub iPropPartsDesigner
	
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

'Iterate through all Of the occurrences
Dim oOccurrence As ComponentOccurrence
For Each oOccurrence In oAsmCompDef.Occurrences.AllReferencedOccurrences(oAsmCompDef)  
	
	If oOccurrence.Name = "VTHFAB4-MCI-Simplify:1" Then
	ElseIf oOccurrence.Name = "VTHFAB6-MCIA-Simplify:1" Then
	ElseIf oOccurrence.Name = "VTHFAB8-MCI-Simplify:1" Then	
		
	Else
		'create iprop with default Value
		iProperties.Value("Project", "Designer") = Designer
		'write to component iprops
		iProperties.Value(oOccurrence.Name, "Project", "Designer") = _
		iProperties.Value("Project", "Designer")
	
	End If
	
	On Error Resume Next
Next

End Sub

WCrihfield
in reply to: Preston_Reed

Hi @Preston_Reed.  There are a couple of built-in ways to tell if an Inventor model type document (part or assembly) is a Content Center member, iPart, or iAssembly.  The PartComponentDefinition & AssemblyComponentDefinition objects have a few Properties that you can check for these things.  The PartComponentDefinition has a ReadOnly property called "IsContentMember", which returns a Boolean, and indicates whether or not it represents a Content Center member; and has 2 properties called "IsiPartFactory" & "IsiPartMember", which seem self explanatory; then it also has the 2 "iPartFactory" & "iPartMember" properties too, for after you have checked the other property and want to get the direct specific type of object.  Similarly, the AssemblyComponentDefinition object has the iAssembly related properties, "IsiAssemblyFactory", "IsiAssemblyMember", "iAssemblyFactory", & "iAssemblyMember".  All of those are ReadOnly properties, so you can't change their values directly.  I also know that parts created by the Content Center will contain a couple of extra sets of iProperties, which are specific to Content Center parts.  I honestly have not tried simply changing the value of an iProperty within a Content Center part, to change its status to not be a Content Center part.  Even if this were possible, I would assume that would not solve the problem of it being ReadOnly, if the part is still stored in the directory designated as Content Center storage, because I think that gets treated as a Library type storage.  Also, I think there is a way to break the link between an iPart/iAssembly 'member', and its 'factory', and I believe you can delete the table from a 'factory' document, which will convert it back to a 'regular' model.  I did not work heavily, or for a very long time with iParts or iAssemblies, so I did not fully control them by code while I was using them.  Instead I chose to go the full iLogic configuration templates, and regular file storage route, because that fit our business needs better and gave me more control over everything.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)