External rules/Error Unable to cast COM object of type

External rules/Error Unable to cast COM object of type

antonio.silvaluna
Explorer Explorer
497 Views
3 Replies
Message 1 of 4

External rules/Error Unable to cast COM object of type

antonio.silvaluna
Explorer
Explorer

I have inventor 2020 and I made this code to change the sheetmetal defaults in all sheetmetal parts in an assembly

But this error appears when I run the code.

 

:

Sub Main()
	Dim thisAssyDoc As AssemblyDocument = CType(ThisDoc.Document, AssemblyDocument)
	ChangeToTest(thisAssyDoc)
End Sub

	Sub ChangeToTest(assemblyDoc As AssemblyDocument)
		
		For Each subCompOcc As ComponentOccurrence In assemblyDoc.ComponentDefinition.Occurrences
			
			Dim oDoc As PartDocument
			oDoc = ThisApplication.ActiveDocument
			Dim oCompDef As SheetMetalComponentDefinition
			
			If oCompDef is oDoc.ComponentDefinition Then
				
				oCompDef.SheetMetalStyles.Item("18ga GA JKT (Test)").Activate
				
			ElseIf oDoc.SubType = DocumentTypeEnum.kAssemblyDocumentObject Then
			
			ChangeToTest(oDoc.Definition.Document)
				
			End If
		
		Next
		
	End Sub  

 

0 Likes
Accepted solutions (2)
498 Views
3 Replies
Replies (3)
Message 2 of 4

basautomationservices
Advocate
Advocate
Accepted solution

You're trying to cast the active document which is still an assembly to a partdocument.

 

Dim oDoc As PartDocument
oDoc = ThisApplication.ActiveDocument

Dim oCompDef As SheetMetalComponentDefinition
			

 

Should be 

 

Dim oDoc as Document 
Set oDoc = subCompOcc.Definition.Document

if oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
    Dim prt as PartDocument
    Set prt = CType(oDoc, PartDocument)

    ' your code

elseif oDOc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then

End if
Contact me for custom app development info@basautomationservices.com. Follow below links to view my Inventor appstore apps.

Free apps: Smart Leader | Part Visibility Utility | Mate Origins

Paid apps: Frame Stiffener Tool | Constrain Plane Toggle | Property Editor Pro


Message 3 of 4

antonio.silvaluna
Explorer
Explorer
Thanks I will try this
0 Likes
Message 4 of 4

WCrihfield
Mentor
Mentor
Accepted solution

Just another variation for consideration.  Changed the Sub to work with AssemblyComponentDefinition directly, then no need to access the component document object at all.

Sub Main()
	Dim thisAssyDoc As AssemblyDocument = CType(ThisDoc.Document, AssemblyDocument)
	ChangeToTest(thisAssyDoc.ComponentDefinition)
End Sub

Sub ChangeToTest(AssyDef As AssemblyComponentDefinition)
	For Each subCompOcc As ComponentOccurrence In AssyDef.Occurrences
		If TypeOf subCompOcc.Definition Is AssemblyComponentDefinition Then
			ChangeToTest(subCompOcc.Definition)
		End If
		If Not TypeOf subCompOcc.Definition Is SheetMetalComponentDefinition Then Continue For
		Dim oSMDef As SheetMetalComponentDefinition = subCompOcc.Definition
		Try
			oSMDef.SheetMetalStyles.Item("18ga GA JKT (Test)").Activate
		Catch
		End Try
	Next
End Sub

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes