Unable to delete DerivedParameterTable of Assembly in VBA

Unable to delete DerivedParameterTable of Assembly in VBA

daniel.puchta
Enthusiast Enthusiast
519 Views
3 Replies
Message 1 of 4

Unable to delete DerivedParameterTable of Assembly in VBA

daniel.puchta
Enthusiast
Enthusiast

Hi,

I have master assembly, in which there is subassembly, which has derived parameters based on another part. The derived parameters were created in Parameter Window by using Link button (not by Derive part tool). From master assembly I am unable to delete either derived parameters or derived parameter table object. On command DerivedParameterTable.Delete which should delete derived parameter table of subassembly, I keep getting eroor: "Run-time error '-214767259 (80004005)': Method 'Delete' of object 'DerivedParameter Table' Failed"

Funny thing is that this happens only in Inventor 2022 when trying to delete DerivedParameter Table of subassembly. Doing the same for part of assembly is working, and both cases are working in Inventor 2020.
Do you have any idea what could cause this error or how to make deleting derived parameter tables working?

Thank you in advance!

Code:

Sub Main()
Dim oDoc As Inventor.Document
Set oDoc = ThisApplication.ActiveDocument
Dim oAsmCompDef As AssemblyComponentDefinition
Set oAsmCompDef = oDoc.ComponentDefinition
Dim oSubDoc As Inventor.Document
Dim oOccurrence As ComponentOccurrence
For Each oOccurrence In oAsmCompDef.Occurrences
    If oOccurrence.Name = "IP_101_R:1" Then
        Set oSubDoc = oOccurrence.Definition.Document
        Exit For
    End If
Next
Remap_derived_parameters oSubDoc
End Sub
Sub Remap_derived_parameters(oDoc As Inventor.Document)
    ' Create a new Assembly document.
    If oDoc.DocumentType = kPartDocumentObject Then
        Dim oCompDef As PartComponentDefinition
        Set oCompDef = oDoc.ComponentDefinition
        Set oModelParams = oCompDef.Parameters.ModelParameters
        Set oReferenceParams = oCompDef.Parameters.ReferenceParameters
    ElseIf oDoc.DocumentType = kAssemblyDocumentObject Then
        Dim oCompDef2 As AssemblyComponentDefinition
        Set oCompDef2 = oDoc.ComponentDefinition
    End If
    Dim oDerivedParam As DerivedParameter
    Dim DerivedParameterTable As DerivedParameterTable
    
    If oDoc.DocumentType = kPartDocumentObject Then
        For Each DerivedParameterTable In oCompDef.Parameters.DerivedParameterTables
                For Each oDerivedParam In DerivedParameterTable.DerivedParameters '20210819 - před smazáním tabulky smazání parametrů
                    oDerivedParam.Delete
                Next
                DerivedParameterTable.Delete


        Next
    ElseIf oDoc.DocumentType = kAssemblyDocumentObject Then
            For Each DerivedParameterTable In oCompDef2.Parameters.DerivedParameterTables 'tady je novší verze
                'zde začátek vložení
                
                'zde konec vložení
                For Each oDerivedParam In DerivedParameterTable.DerivedParameters '20210819 - před smazáním tabulky smazání parametrů
                    'oDerivedParam.Delete
                Next
                oDoc.Update
                DerivedParameterTable.Delete
                
                

            Next
        

        
    End If
    
End Sub






Public Function BaseFilename(ByVal fullFilename As String) As String
    ' Extract the filename by getting everttgubg to
    ' the right of the last backslash.
    Dim temp As String
    temp = Right(fullFilename, Len(fullFilename) - InStrRev(fullFilename, "\"))
           

    ' Get the base filename by getting everything to
    ' the left of the last period ".".
    ' BaseFilename = Left(temp, InStrRev(temp, ".") - 1)
    BaseFilename = temp
End Function

Sub Delete_derived_tables(oDoc As Inventor.Document)
    If oDoc.DocumentType = kPartDocumentObject Then
        Dim oCompDef As PartComponentDefinition
        Set oCompDef = oDoc.ComponentDefinition
        Set oModelParams = oCompDef.Parameters.ModelParameters
        Set oReferenceParams = oCompDef.Parameters.ReferenceParameters
    ElseIf oDoc.DocumentType = kAssemblyDocumentObject Then
        Dim oCompDef2 As AssemblyComponentDefinition
        Set oCompDef2 = oDoc.ComponentDefinition
    End If
    Dim DerivedParameterTable As DerivedParameterTable

    
    If oDoc.DocumentType = kPartDocumentObject Then
        For Each DerivedParameterTable In oCompDef.Parameters.DerivedParameterTables
                DerivedParameterTable.Delete
        Next
    ElseIf oDoc.DocumentType = kAssemblyDocumentObject Then
            For Each DerivedParameterTable In oCompDef2.Parameters.DerivedParameterTables 'tady je novší verze
                DerivedParameterTable.Delete '20210818 zde chyba
            Next
        
    
        
    End If
    
End Sub

 

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

WCrihfield
Mentor
Mentor

The online help documentation for that method (DerivedParameterTable.Delete) says that it will error if the 'HasReferenceComponent' property returns True.  Have you checked that?

 

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

If you want and have time, I would appreciate your Vote(s) for My IDEAS 💡or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 4

daniel.puchta
Enthusiast
Enthusiast

Hello, thank you for your answer.

Sure I have checked that and the property returns False and the error occurs anyway. I am able to delete the table manually  right before execution of DerivedParameterTable.Delete, but if I Try this by code it does not work. 

Maybe the problem is in somewhere else. Before I start to delete DerivedParameterTable of occurrence, I replace this occurence by the same assembly file but this file is in another subdirectory (it is either by replace occurrence or replace reference file method). When I then try to delete Derived Parameter Table by code, it throws error. If I open the occurrence, and delete the table either by code (so i have opened Assembly in which is the table I want to delete, and oSubDoc is defined as Set oSubDoc = ThisApplication.ActiveDocument) or manually, it works fine. If I try to execute the original code on original occurrence (unreplaced occurrence) it works also fine. 


Before trying to delete derived parameter table of assembly I delete tables of Assembly components and it works. Also in Inventor 2020 the same code worked fine no matter if the occurrence was replaced or not..

Do you have any idea how to make the code working or what to try?

0 Likes
Message 4 of 4

daniel.puchta
Enthusiast
Enthusiast
Accepted solution

Finally I have found a way to make that code working.

The error occurs only when trying to delete derived parameters table of subassembly from main assembly, where Document object is set as Occurrence definition document. Document of Occurence had been before running the code changed (either by replace occurrence or by replace referenced file).

So intead before deleting derived parameters table I open the Subassembly and then define Document object as Active document. Then it works fine, deleting derived parameters table does not throw error.

Working code:

Sub Main()
Dim oDoc As Inventor.Document
Set oDoc = ThisApplication.ActiveDocument
Dim oAsmCompDef As AssemblyComponentDefinition
Set oAsmCompDef = oDoc.ComponentDefinition
Dim oSubDoc As Inventor.Document
Dim oOccurrence As ComponentOccurrence
For Each oOccurrence In oAsmCompDef.Occurrences
    If oOccurrence.Name = "IP_101_R:1" Then
        Set oSubDoc = oOccurrence.Definition.Document
        Exit For
    End If
Next
Remap_derived_parameters oSubDoc
End Sub
Sub Remap_derived_parameters(oDoc As Inventor.Document)
    ' Create a new Assembly document.
    If oDoc.DocumentType = kPartDocumentObject Then
        Dim oCompDef As PartComponentDefinition
        Set oCompDef = oDoc.ComponentDefinition
        Set oModelParams = oCompDef.Parameters.ModelParameters
        Set oReferenceParams = oCompDef.Parameters.ReferenceParameters
    ElseIf oDoc.DocumentType = kAssemblyDocumentObject Then
        Dim oCompDef2 As AssemblyComponentDefinition
        Set oCompDef2 = oDoc.ComponentDefinition
    End If
    Dim oDerivedParam As DerivedParameter
    Dim DerivedParameterTable As DerivedParameterTable
    
    If oDoc.DocumentType = kPartDocumentObject Then
        For Each DerivedParameterTable In oCompDef.Parameters.DerivedParameterTables
                For Each oDerivedParam In DerivedParameterTable.DerivedParameters '20210819 - před smazáním tabulky smazání parametrů
                    oDerivedParam.Delete
                Next
                DerivedParameterTable.Delete


        Next
    ElseIf oDoc.DocumentType = kAssemblyDocumentObject Then
            For Each DerivedParameterTable In oCompDef2.Parameters.DerivedParameterTables 'tady je novší verze
                'zde začátek vložení
                
                'zde konec vložení
                For Each oDerivedParam In DerivedParameterTable.DerivedParameters '20210819 - před smazáním tabulky smazání parametrů
                    'oDerivedParam.Delete
                Next
                oDoc.Update
                DerivedParameterTable.Delete
                
                

            Next
        

        
    End If
    
End Sub






Public Function BaseFilename(ByVal fullFilename As String) As String
    ' Extract the filename by getting everttgubg to
    ' the right of the last backslash.
    Dim temp As String
    temp = Right(fullFilename, Len(fullFilename) - InStrRev(fullFilename, "\"))
           

    ' Get the base filename by getting everything to
    ' the left of the last period ".".
    ' BaseFilename = Left(temp, InStrRev(temp, ".") - 1)
    BaseFilename = temp
End Function

Sub Delete_derived_tables(oDoc As Inventor.Document)
    If oDoc.DocumentType = kPartDocumentObject Then
        Dim oCompDef As PartComponentDefinition
        Set oCompDef = oDoc.ComponentDefinition
        Set oModelParams = oCompDef.Parameters.ModelParameters
        Set oReferenceParams = oCompDef.Parameters.ReferenceParameters
    ElseIf oDoc.DocumentType = kAssemblyDocumentObject Then
	ThisApplication.Documents.Open (oDoc.FullFileName)
    	Set oDoc = ThisApplication.ActiveDocument
        Dim oCompDef2 As AssemblyComponentDefinition
        Set oCompDef2 = oDoc.ComponentDefinition
    End If
    Dim DerivedParameterTable As DerivedParameterTable

    
    If oDoc.DocumentType = kPartDocumentObject Then
        For Each DerivedParameterTable In oCompDef.Parameters.DerivedParameterTables
                DerivedParameterTable.Delete
        Next
    ElseIf oDoc.DocumentType = kAssemblyDocumentObject Then
            For Each DerivedParameterTable In oCompDef2.Parameters.DerivedParameterTables 'tady je novší verze
                DerivedParameterTable.Delete '20210818 zde chyba
            Next
        
    
        
    End If
    
End Sub



0 Likes